Skip to content

Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.

C++

serve: with pipelined groups, a request fills a hole below a busy slot of its group first (#793)

Niko12212c7c02ab722b
diff --git a/serve/server.py b/serve/server.py
index d3b31c90a..bdf8a1d94 100644
--- a/serve/server.py
+++ b/serve/server.py
@@ -697,6 +697,11 @@ class StrataEngine:
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
+ # pipelined groups (the engine says how many it runs: INFO batch_groups; older engines: the args). A pipelined
+ # group's window spans its slots up to its last busy one, and the engine keeps no pipelined slot as a
+ # conversation cache - see pick_slot
+ groups_run = int(self.info.get("batch_groups") or groups)
+ self.slot_gs = gs if groups > 1 and groups_run > 1 else 0
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
@@ -1390,6 +1395,16 @@ class StrataEngine:
free = [b for b in self.slot_order if not self.slot_busy[b]]
if not free:
return None
+ gs = getattr(self, "slot_gs", 0)
+ if gs:
+ # pipelined groups: the engine keeps no pipelined slot as a conversation cache, so nothing is worth keeping
+ # free for a next turn. A group's window spans its slots up to its last busy one, so first fill a hole
+ # below a busy slot of the same group (it costs no extra row), then follow the spreading order
+ # (reported on #793: least-recently-used picks left leading holes, [_ B], that cost a whole row each)
+ def fills_hole(b):
+ g0 = b - b % gs
+ return any(self.slot_busy[x] for x in range(b + 1, g0 + gs))
+ return min(free, key=lambda b: (not fills_hole(b), self.slot_order.index(b)))
def held_prefix(b):
h = self.slot_held[b]
return len(h) if h and len(h) < len(prompt) and prompt[:len(h)] == h else 0
diff --git a/serve/test_slot_alloc.py b/serve/test_slot_alloc.py
new file mode 100644
index 000000000..474b4f78e
--- /dev/null
+++ b/serve/test_slot_alloc.py
@@ -0,0 +1,83 @@
+"""serve/test_slot_alloc.py - which batch slot a request gets (StrataEngine.pick_slot), no engine needed.
+
+ python -m unittest serve.test_slot_alloc -v
+"""
+from __future__ import annotations
+
+import random
+import sys
+import unittest
+from pathlib import Path
+
+sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
+from serve.server import StrataEngine # noqa: E402
+
+
+def engine(batch, groups, pipelined=True):
+ e = object.__new__(StrataEngine) # pick_slot reads only the slot tables
+ gs = batch // groups
+ e.batch = batch
+ e.slot_order = [g * gs + t for t in range(gs) for g in range(groups)]
+ e.slot_busy = [False] * batch
+ e.slot_held = [[] for _ in range(batch)]
+ e.slot_used = [0.0] * batch
+ e.slot_gs = gs if pipelined and groups > 1 else 0
+ return e
+
+
+class Pipelined(unittest.TestCase):
+ def test_spreads_over_the_groups_first(self):
+ e = engine(6, 3)
+ got = []
+ for _ in range(6):
+ b = e.pick_slot([1, 2, 3])
+ e.slot_busy[b] = True
+ got.append(b)
+ self.assertEqual(got, [0, 2, 4, 1, 3, 5])
+
+ def test_fills_a_leading_hole_first(self):
+ # #793's report: a group left as [_ B] costs a whole row; the next request goes into that hole
+ e = engine(6, 3)
+ e.slot_busy[3] = True # group 1 = [_ B]
+ e.slot_used[0] = 0.0 # least recently used: what the LRU fallback picked
+ e.slot_used[2] = 99.0
+ self.assertEqual(e.pick_slot([1]), 2)
+
+ def test_never_opens_a_hole_below_the_pick(self):
+ for batch, groups in ((6, 3), (8, 4), (8, 2), (4, 2)):
+ gs = batch // groups
+ e, rng = engine(batch, groups), random.Random(batch * 10 + groups)
+ for _ in range(2000):
+ busy = [b for b in range(batch) if e.slot_busy[b]]
+ if busy and (rng.random() < 0.45 or len(busy) == batch):
+ e.slot_busy[rng.choice(busy)] = False # a request ends, anywhere
+ continue
+ b = e.pick_slot([7])
+ g0 = b - b % gs
+ with self.subTest(batch=batch, groups=groups):
+ self.assertTrue(all(e.slot_busy[x] for x in range(g0, b)),
+ f"slot {b} picked with a free slot below it in its group: {e.slot_busy}")
+ e.slot_busy[b] = True
+
+ def test_held_tokens_are_ignored(self):
+ # the engine keeps no pipelined slot as a cache: a held prefix must not pull a request into a hole-making slot
+ e = engine(6, 3)
+ e.slot_held[5] = [1, 2]
+ self.assertEqual(e.pick_slot([1, 2, 3]), 0)
+
+
+class OneGroup(unittest.TestCase):
+ def test_reuse_then_least_recently_used(self):
+ e = engine(4, 1, pipelined=False)
+ e.slot_held[2] = [1, 2]
+ self.assertEqual(e.pick_slot([1, 2, 3]), 2) # its conversation's next turn
+ e.slot_held[2] = [9]
+ e.slot_held = [[5], [], [9], [6]]
+ self.assertEqual(e.pick_slot([1, 2, 3]), 1) # an empty slot before evicting a held one
+ e.slot_held[1] = [8]
+ e.slot_used = [5.0, 3.0, 9.0, 1.0]
+ self.assertEqual(e.pick_slot([1, 2, 3]), 3) # else the least recently used
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/src/program/generate.cpp b/src/program/generate.cpp
index 800bd0a79..914d37f5d 100644
--- a/src/program/generate.cpp
+++ b/src/program/generate.cpp
@@ -7921,7 +7921,8 @@ int main(int argc, char** argv) {
o.spec_min_p, (long long) o.conversation_cache_mib, o.conversation_cache_slots,
(long long) o.conversation_cache_min_free_mib, (long long) o.tail_role_token, xcache.segmented() ? 1 : 0,
o.batch > 0 ? (" batch_slots=" + std::to_string(o.batch) +
- " slot_cache=" + std::to_string(o.prompt_cache > 0 ? 1 : 0)).c_str() : "");
+ " slot_cache=" + std::to_string(o.prompt_cache > 0 ? 1 : 0) +
+ " batch_groups=" + std::to_string(o.batch_groups)).c_str() : "");
}
// issue #29: a request whose heartbeat (tokens, prompt chunks, verify windows) stops for this long is stuck on
// a flag nobody will raise - end the engine with where it was, so the server starts it again instead of the