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: pick_slot sends a chat's next turn to the slot that holds it, found by its turn checkpoint (#2518)

Niko1221dd08a7d0ec42
diff --git a/serve/server.py b/serve/server.py
index d22518cb6..3ca03b03b 100644
--- a/serve/server.py
+++ b/serve/server.py
@@ -1835,6 +1835,7 @@ class StrataEngine:
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
+ TURN_ID = 248045 # <|im_start|>: the engine's default --turn-token (its turn checkpoint sits at the last one)
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
@@ -1844,18 +1845,30 @@ class StrataEngine:
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
+ if gs > 1:
+ # pipelined groups of several slots: the engine keeps no idle slot below a busy one as a conversation cache
+ # (its pad row writes the slot), 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):
+ # what of `prompt` the slot's cache serves: the slot's whole held stream (the prompt and what it decoded)
+ # when this prompt continues it, else its turn checkpoint - the engine takes one at the last <|im_start|> of
+ # a prompt (the next request of the chat renders the history again, but not the reply it decoded or the
+ # assistant header), so a prompt that agrees with the held tokens up to there is that chat's next turn
+ # too (#2518: it was only found by the engine's own source search, after pick_slot had sent it to another
+ # slot and the request in that slot had overwritten the chat whose cache the next one needed)
h = self.slot_held[b]
- return len(h) if h and len(h) < len(prompt) and prompt[:len(h)] == h else 0
+ if not h:
+ return 0
+ if len(h) < len(prompt) and prompt[:len(h)] == h:
+ return len(h)
+ k = len(h) - 1 - h[::-1].index(self.TURN_ID) if self.TURN_ID in h else 0
+ return k if 0 < k < len(prompt) and prompt[:k] == h[:k] else 0
best = max(free, key=held_prefix)
# 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):
diff --git a/serve/test_parallel.py b/serve/test_parallel.py
index 1a638919e..bde520845 100644
--- a/serve/test_parallel.py
+++ b/serve/test_parallel.py
@@ -248,6 +248,34 @@ 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_a_rewritten_next_turn_goes_to_the_slot_of_its_chat(self):
+ # #2518: two chats in two slots; each next turn renders the history again but not the reply it decoded or the
+ # assistant header, so the held stream (prompt + reply) is no start of it. The engine's turn checkpoint sits at
+ # the last <|im_start|> of the old prompt: the prompt that agrees up to there belongs to that slot. Before, both
+ # went to the least recently used slot in turn, and the first one overwrote the other chat's cache.
+ T = StrataEngine.TURN_ID
+ hist_a, hist_b = list(range(1000, 1600)), list(range(2000, 2600))
+ held_a = hist_a + [T, 7, 8, 9] + [50, 51, 52] # prompt (ends with the assistant header) + reply
+ held_b = hist_b + [T, 7, 8, 9] + [60, 61]
+ e = self.engine(2)
+ e.slot_held = [held_a, held_b]
+ e.slot_used = [10.0, 20.0] # slot 0 was used longer ago
+ next_a = hist_a + [T, 5, 6, 7, 8, 9] + list(range(3000, 3100)) # the history again, then new turns
+ next_b = hist_b + [T, 5, 6, 7, 8, 9] + list(range(3000, 3100))
+ self.assertEqual(e.pick_slot(next_b), 1) # the LRU slot 0 holds the other chat
+ self.assertEqual(e.pick_slot(next_a), 0)
+ e.slot_busy[0] = True # a busy slot is never picked
+ self.assertEqual(e.pick_slot(next_a), 1)
+ e.slot_busy[0] = False
+ # a chat that agrees with neither (a new one, or one that rewrote its history) keeps the old order
+ self.assertEqual(e.pick_slot(list(range(5000, 5200))), 0)
+ self.assertEqual(e.pick_slot(hist_a[:300] + [T, 5, 6] + list(range(3000, 3100))), 0)
+ e.slot_held[0] = [] # an empty slot still comes before evicting one
+ self.assertEqual(e.pick_slot(list(range(5000, 5200))), 0)
+ # the whole held stream continued is still the longer match
+ e.slot_held = [held_a, held_b + [T, 1, 2]]
+ self.assertEqual(e.pick_slot(held_b + [T, 1, 2, 3, 4]), 1)
+
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
diff --git a/serve/test_slot_alloc.py b/serve/test_slot_alloc.py
index 474b4f78e..b2b4d35be 100644
--- a/serve/test_slot_alloc.py
+++ b/serve/test_slot_alloc.py
@@ -79,5 +79,30 @@ class OneGroup(unittest.TestCase):
self.assertEqual(e.pick_slot([1, 2, 3]), 3) # else the least recently used
+class OneSlotGroups(unittest.TestCase):
+ """--batch 2 on a two-GPU layer split: two pipelined groups of ONE slot. No slot sits below another in its group,
+ so no pad row writes an idle slot and each one keeps its chat (#2518): the next turn goes back to it."""
+
+ def test_the_slot_of_the_chat(self):
+ T = StrataEngine.TURN_ID
+ e = engine(2, 2)
+ self.assertEqual(e.slot_gs, 1)
+ hist_a, hist_b = list(range(1000, 1700)), list(range(2000, 2700))
+ e.slot_held = [hist_a + [T, 7, 8] + [50, 51], hist_b + [T, 7, 8] + [60]]
+ e.slot_used = [10.0, 20.0]
+ again = lambda h: h + [T, 5, 6] + list(range(3000, 3050))
+ self.assertEqual(e.pick_slot(again(hist_b)), 1)
+ self.assertEqual(e.pick_slot(again(hist_a)), 0)
+ self.assertEqual(e.pick_slot(list(range(5000, 5100))), 0) # a new chat: the least recently used one
+
+ def test_groups_of_several_slots_still_ignore_held_tokens(self):
+ T = StrataEngine.TURN_ID
+ e = engine(4, 2)
+ self.assertEqual(e.slot_gs, 2)
+ hist = list(range(1000, 1700))
+ e.slot_held[3] = hist + [T, 7, 8] + [50]
+ self.assertEqual(e.pick_slot(hist + [T, 5, 6] + list(range(3000, 3050))), 0)
+
+
if __name__ == "__main__":
unittest.main()