From ca001df5714c6410d6d6af617b0ee2004441f138 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Thu, 8 Oct 2026 07:10:59 +0200 Subject: [PATCH] batch: a layer split with --batch pipelines one group per stage by default (--batch-groups 1 opts out); server listen backlog 256 (30-40 simultaneous clients got connection resets) --- docs/BATCHING.md | 6 +++--- serve/server.py | 10 +++++++--- serve/test_parallel.py | 6 ++++++ src/program/generate.cpp | 4 ++++ 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/docs/BATCHING.md b/docs/BATCHING.md index 6978a990..893064f3 100644 --- a/docs/BATCHING.md +++ b/docs/BATCHING.md @@ -36,8 +36,8 @@ With a layer split, the engine options go into the config's `args`: | Option | What it does | | --- | --- | | `"parallel": N` / `--batch N` / `--slots N` (2..8 normally) | up to N conversations have batch slots; more requests wait for a free slot. Each slot gets its own state (a session carved like the stage's own: GDN recurrence, QSA K/V and indexer, PLE history) on every GPU of the split. With grouped MTP, more than 8 slots can rotate through eight-row windows if memory permits. | -| `--batch-groups G` | with a layer split: the N slots in G groups that flow through the GPUs as a pipeline (GPU k runs one group while GPU k+1 runs another). G must divide N. 1 = all slots in one window, GPU after GPU. | -| `--batch-groups auto` | with a layer split: the engine picks the groups itself, one per GPU stage (the most that divide the slots; 8 slots on 4 GPUs = 4 groups of 2) and says so (`INFO batch_groups=G`). Measured on 4 x R9700, 8 clients: 155.6 tok/s against 86.2 with the default one group (5 of 5 interleaved pairs). Opt-in. | +| `--batch-groups G` | with a layer split (default: auto, below): the N slots in G groups that flow through the GPUs as a pipeline (GPU k runs one group while GPU k+1 runs another). G must divide N. 1 = all slots in one window, GPU after GPU. | +| `--batch-groups auto` | the default on a layer split since 0.1.41 (give no `--batch-groups`): the engine pipelines one group per GPU stage (the most that divide the slots; 8 slots on 4 GPUs = 4 groups of 2) and says so (`INFO batch_groups=G`). `--batch-groups 1` turns it off (all slots in one window, GPU after GPU). Measured, 8 clients, total tok/s against one group: 4 x R9700 166 against 86, 2 GPUs 109 against 78, 3 GPUs 110 against 78. | | `--trim-stage-weights` | with an **explicit** `--layer-split` (e.g. `12,24,36`, not `auto`): every GPU loads only the dense weights of its own layers instead of the whole model's (the same as `STRATA_STAGE_TRIM=1`, PR #639). The VRAM this frees goes to the expert cache. Useful without `--batch` too. | The engine never refuses a count it cannot run: it says so in its log and runs what it can - at most 8 slots by @@ -129,7 +129,7 @@ counter-based draw (Philox(seed, position)). - A prompt shorter than one chunk is read in one piece (the slots wait for it); a read gives way only at a chunk boundary, and not for pictures. - Admissions are one at a time: two new long prompts are read one after the other. -- `--batch-groups` needs every stage on its own GPU; a pipelined slot is not kept as a conversation cache. +- `--batch-groups` needs every stage on its own GPU. A pipelined slot is a conversation cache again (0.1.41): a request left alone in its slot goes back to the solo path with its drafts, as on one GPU. - The slot sessions take VRAM (above) and, with KV streaming, pinned RAM. ## Measured diff --git a/serve/server.py b/serve/server.py index dc3b91ad..fbf28a6e 100644 --- a/serve/server.py +++ b/serve/server.py @@ -690,9 +690,10 @@ class StrataEngine: if asked and self.batch != asked: print(f"[strata] parallel requests: {asked} asked, the engine runs {self.batch or 'one at a time'} " "(its log says why)", flush=True) - _bg = args[args.index("--batch-groups") + 1] if "--batch-groups" in args else "1" - # `--batch-groups auto`: the engine picks the groups and says so (INFO batch_groups=G) - groups = int(self.info.get("batch_groups") or 1) if _bg == "auto" else int(_bg) + _bg = args[args.index("--batch-groups") + 1] if "--batch-groups" in args else "default" + # no `--batch-groups` (the default on a layer split) or `auto`: the engine picks the groups and says so + # (INFO batch_groups=G); a number is the group count asked for (1 = off) + groups = int(self.info.get("batch_groups") or 1) if _bg in ("auto", "default") else int(_bg) groups = groups if self.batch and groups > 0 and self.batch % groups == 0 else 1 gs = self.batch // groups if self.batch else 0 # slots in the order that spreads requests over the pipeline's groups first: 0, gs, 2gs, .., 1, gs+1, .. @@ -4966,6 +4967,9 @@ class Server(ThreadingHTTPServer): # On Windows SO_REUSEADDR lets a second server bind a port that is already serving, and requests then land on # either one (a forgotten second start of run-.bat). Without it the second start fails loudly instead. allow_reuse_address = os.name != "nt" + # socketserver listens with a backlog of 5: a burst of 30-40 clients at once got "connection reset by peer" on the + # first ones (measured on 4 x R9700, also with one request at a time); the requests wait in the server, not the kernel + request_queue_size = 256 def handle_error(self, request, client_address): if not isinstance(sys.exc_info()[1], ConnectionError): # a client that hangs up needs no stack trace diff --git a/serve/test_parallel.py b/serve/test_parallel.py index f3381e27..8420eef9 100644 --- a/serve/test_parallel.py +++ b/serve/test_parallel.py @@ -313,6 +313,12 @@ class ParallelService(unittest.TestCase): self.tearDown(); self.httpd = self.engine = self.tmp = None self.start(8, more=["--batch-groups", "2"]) self.assertEqual(self.engine.slot_groups, 2) + self.tearDown(); self.httpd = self.engine = self.tmp = None + self.start(8, says_groups=4) # 0.1.41: no flag on a layer split = the engine's choice + self.assertEqual(self.engine.slot_groups, 4) + self.tearDown(); self.httpd = self.engine = self.tmp = None + self.start(8, says_groups=4, more=["--batch-groups", "1"]) # the opt-out + self.assertEqual(self.engine.slot_groups, 1) def test_stop_strings_in_a_batch_slot(self): """#454: a stop string cuts the answer in --batch mode too, and the slot is freed for the next request.""" diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 6e7cee50..d0fe9126 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -535,6 +535,7 @@ struct Options { /// The --batch slots in this many groups pipelined through the stages of a layer split (stage k /// runs one group while stage k+1 runs another). 1 = every slot in one window, stage after stage. int batch_groups = 1; + bool batch_groups_set = false; ///< --batch-groups was given (a number or auto): no default then bool batch_groups_auto = false; ///< --batch-groups auto (#417 stage 1): one group per stage of a layer split, if it divides --batch bool batch_mtp = false; ///< --batch-mtp / STRATA_BATCH_MTP=1 (opt-in): one MTP proposal per batch slot std::string spec_oracle; @@ -1732,6 +1733,7 @@ int main(int argc, char** argv) { else if (a == "--trim-stage-weights") o.trim_stage_weights = true; else if (a == "--batch-groups") { const char* bgv = next("--batch-groups"); + o.batch_groups_set = true; if (std::strcmp(bgv, "auto") == 0) o.batch_groups_auto = true; else o.batch_groups = std::atoi(bgv); } @@ -3810,6 +3812,8 @@ int main(int argc, char** argv) { // --batch-groups auto: the pipeline needs one group per GPU stage to keep every card busy (4 x R9700, 8 clients: // 90 tok/s in one group, 136 in 2, 166 in 4). The most groups, at most one per stage, that divide the slots. auto resolve_groups_auto = [&] { + // default (0.1.41): a layer split with --batch pipelines one group per stage; --batch-groups 1 opts out + if (!o.batch_groups_set && !stages.empty() && o.batch > 1) o.batch_groups_auto = true; if (!o.batch_groups_auto) return; int best = 1; for (int d = 2; d <= (int) stages.size() + 1 && d <= o.batch; ++d)