From e21dc577a8b2767d241cc61d6ecb840233f75cd8 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Wed, 7 Oct 2026 01:30:44 +0200 Subject: [PATCH] batch: with --batch-groups a second request goes to the other pipeline group, and idle slots no longer cost rows in a group's window (#1249) On a layer split with --batch-groups, two concurrent requests ran at about half speed: the slot choice could put both in one group, and a group's window carried a row for every slot, active or not. Slot choice now prefers the group with the fewest busy slots (a held prefix of 512 tokens or more still wins), and a window covers the slots up to its last active one. Without --batch-groups the slot choice is exactly as before. Co-authored-by: crazyaimachine <209005491+crazyaimachine@users.noreply.github.com> --- serve/server.py | 17 +++++++++++++++-- serve/test_parallel.py | 16 ++++++++++++++++ src/program/generate.cpp | 9 ++++++--- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/serve/server.py b/serve/server.py index 005ab72f..a1e4d407 100644 --- a/serve/server.py +++ b/serve/server.py @@ -653,6 +653,8 @@ class StrataEngine: 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, .. self.slot_order = [g * gs + t for t in range(gs) for g in range(groups)] + self.slot_group = [b // gs if gs else 0 for b in range(self.batch)] # each slot's pipeline group + self.slot_groups = groups self.slot_q = [queue.Queue() for _ in range(self.batch)] self.slot_busy = [False] * self.batch # what each slot's sessions hold (prompt + every token a window fed), so a conversation's next turn goes to @@ -1313,6 +1315,9 @@ class StrataEngine: self.slot_busy[slot] = False self.slot_cv.notify_all() + slot_groups = 1 # --batch-groups (set when the engine starts) + SLOT_PREFIX_MIN = 512 # a held prefix shorter than this is not worth unbalancing the pipeline's groups + def pick_slot(self, prompt: list[int]) -> int | None: """A free slot for `prompt` (the caller holds slot_cv): the one whose held tokens are the longest start of the prompt (the engine then reads only the rest), else an empty one, else the one used longest ago - so the @@ -1324,9 +1329,17 @@ class StrataEngine: h = self.slot_held[b] return len(h) if h and len(h) < len(prompt) and prompt[:len(h)] == h else 0 best = max(free, key=held_prefix) - if held_prefix(best) > 0: + # without --batch-groups a held prefix of any length is taken, as before + if held_prefix(best) >= (self.SLOT_PREFIX_MIN if self.slot_groups > 1 else 1): return best - return min(free, key=lambda b: (bool(self.slot_held[b]), self.slot_used[b])) + # with --batch-groups: the group with the fewest busy slots first, so the requests spread over the + # pipeline's stages (two requests in one group leave the other stage idle); then empty, then least recent + busy_in = {} + for b in range(self.batch): + if self.slot_busy[b]: + busy_in[self.slot_group[b]] = busy_in.get(self.slot_group[b], 0) + 1 + return min(free, key=lambda b: (busy_in.get(self.slot_group[b], 0) if self.slot_groups > 1 else 0, + bool(self.slot_held[b]), self.slot_used[b])) def slots_view(self) -> list[dict]: """/metrics: each batch slot - idle (with the tokens it holds for a next turn), reading or decoding.""" diff --git a/serve/test_parallel.py b/serve/test_parallel.py index 962ffbf2..429e2cf8 100644 --- a/serve/test_parallel.py +++ b/serve/test_parallel.py @@ -199,6 +199,7 @@ class PickSlot(unittest.TestCase): e.slot_held = [[] for _ in range(n)] e.slot_used = [0.0] * n e.slot_live = [None] * n + e.slot_group = [0] * n return e def test_the_slot_that_holds_the_conversation(self): @@ -215,6 +216,21 @@ class PickSlot(unittest.TestCase): e.slot_busy = [False] * 3 self.assertEqual(e.pick_slot([1, 2, 3]), 1) # a held prompt is never the WHOLE prompt + def test_batch_groups_spread_requests_over_the_groups(self): + # #1249: with --batch-groups 2 over 4 slots (groups {0,1} and {2,3}) a second request goes to the other group, + # and a short held prefix does not pull it back into the busy one + e = self.engine(4) + e.slot_groups = 2 + e.slot_group = [0, 0, 1, 1] + e.slot_busy[0] = True + e.slot_held[1] = [1, 2, 3] + self.assertEqual(e.pick_slot([1, 2, 3, 4, 5]), 2) + e.slot_held[1] = list(range(1, 601)) # a long prefix is worth the unbalanced group + self.assertEqual(e.pick_slot(list(range(1, 700))), 1) + e.slot_groups, e.slot_group = 1, [0] * 4 # without groups: any held prefix, as before + e.slot_held[1] = [1, 2, 3] + self.assertEqual(e.pick_slot([1, 2, 3, 4, 5]), 1) + def test_slots_view(self): e = self.engine(2) e.slot_held[1] = [1, 2] diff --git a/src/program/generate.cpp b/src/program/generate.cpp index b827c59f..f4ee31bb 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -8099,6 +8099,7 @@ int main(int argc, char** argv) { int32_t tok[strata::kernels::kVerifyMaxT] = {}; int64_t pos[strata::kernels::kVerifyMaxT] = {}; int64_t since = 0; ///< tick it started waiting (fairness) + int S = 0; ///< slots in its window: up to its last active one (idle ones cost rows) }; std::vector pg((size_t) (piped ? o.batch_groups : 0)); std::vector stage_group((size_t) n_pipe, -1); @@ -8129,7 +8130,7 @@ int main(int argc, char** argv) { if (k + 1 < n_pipe) { G.stage = k + 1; G.since = pipe_tick; continue; } // the last stage: the group's picks const int32_t* outb = vk.batch_out(); - for (int t = 0; t < GS; ++t) { + for (int t = 0; t < G.S; ++t) { BSlot& sl = bs[(size_t) (gi * GS + t)]; if (!sl.active) continue; const int32_t y = outb[t]; @@ -8173,7 +8174,9 @@ int main(int argc, char** argv) { if (pick < 0) continue; rr = pick + 1; PGroup& G = pg[(size_t) pick]; - for (int t = 0; t < GS; ++t) { + G.S = 0; + for (int t = 0; t < GS; ++t) if (bs[(size_t) (pick * GS + t)].active) G.S = t + 1; + for (int t = 0; t < G.S; ++t) { BSlot& sl = bs[(size_t) (pick * GS + t)]; G.tok[t] = sl.active ? sl.x : 0; G.pos[t] = sl.active ? sl.p : 0; @@ -8186,7 +8189,7 @@ int main(int argc, char** argv) { } PGroup& G = pg[(size_t) pick]; strata::core::progress().busy.store(true); - if (!stage_verifier(k).batch_launch(pick * GS, GS, G.tok, G.pos, err)) { + if (!stage_verifier(k).batch_launch(pick * GS, G.S, G.tok, G.pos, err)) { std::printf("ERR %s\n", err.c_str()); return false; }