serve: a slot request that ends at its stop token reads its BDONE before it leaves, so its decode time and tokens are the slot leg's (#1869)

This commit is contained in:
Niko1221 committed 2026-10-11 00:56:31 +02:00
1 parent 5f9bbf61c2
commit 85d90002b6
2 files changed
+60 -4

No files matched your search

+40 -4
View File
@@ -1113,6 +1113,31 @@ class StrataEngine:
return line
return None
def _apply_bdone(self, f: list[str]) -> None:
"""`BDONE <slot> <produced> <finish> <ms>` into this request's figures."""
if len(f) >= 5 and isinstance(self.last, dict):
self.last = {**self.last, "finish": f[3], "decode_ms": float(f[4]),
"generated": int(f[2]) if f[2].isdigit() else self.last.get("generated")}
def _bdone_after_eos(self, slot: int, wait_s: float = 2.0):
"""#1869: a request in a slot whose consumer left at the stop token has its BDONE right behind it (the engine prints it
in the same window). Read it here, so the request's figures are the slot leg's (the background wait would eat it,
and the request kept the admission's single token). None when it does not come in `wait_s`: then the background
wait takes over."""
if slot >= len(self.slot_q):
return None
end = time.monotonic() + wait_s
while time.monotonic() < end:
try:
line = self.slot_q[slot].get(timeout=max(0.05, end - time.monotonic()))
except queue.Empty:
return None
if line is None:
return None
if line.startswith("BDONE "):
return line
return None
def _release_slot_when_done(self, slot: int, stream: list[int] | None = None):
"""A slot whose consumer left (a stop token, a stop string, a disconnect): BSTOP it and free it once the engine
says BDONE (in the background). `stream`: the prompt and every token of it so far - with the tokens still to
@@ -1447,9 +1472,7 @@ class StrataEngine:
elif line.startswith("BDONE "):
phase = "none"
f = line.split()
if len(f) >= 5 and isinstance(self.last, dict):
self.last = {**self.last, "finish": f[3], "decode_ms": float(f[4]),
"generated": int(f[2]) if f[2].isdigit() else self.last.get("generated")}
self._apply_bdone(f)
# what the slot's sessions hold now: the prompt and every token fed (all but the last one)
self.slot_held[slot] = list(prompt) + out[gen0:-1] if len(out) > gen0 else []
if (going_solo and f[3:4] == ["cancel"] and not cancel.is_set() and len(out) < int(max_new)
@@ -1488,6 +1511,12 @@ class StrataEngine:
phase = "slot"
except EngineDied:
pass
bdone_in = False
if phase == "slot" and slot is not None and out and out[-1] in EOS_IDS and self.gen == born:
bd = self._bdone_after_eos(slot) # (#1869) the slot's figures, not the admission's
if bd is not None:
self._apply_bdone(bd.split())
bdone_in = True
if reused0 is not None and isinstance(self.last, dict):
self.last = {**self.last, "reused": reused0}
if carry and isinstance(self.last, dict):
@@ -1505,7 +1534,14 @@ class StrataEngine:
if slot is not None:
self.slot_live[slot] = None
self.slot_used[slot] = time.time()
if phase == "slot":
if phase == "slot" and bdone_in:
stream = list(prompt) + out[gen0:] if gen0 is not None and len(out) > gen0 else None
self.slot_held[slot] = stream[:-1] if stream else [] # as the BDONE wait leaves it
with self.slot_cv:
self.slot_busy[slot] = False
self.slot_claims.discard(slot)
self.slot_cv.notify_all()
elif phase == "slot":
self.slot_held[slot] = []
stream = list(prompt) + out[gen0:] if gen0 is not None and len(out) > gen0 else None
self._release_slot_when_done(slot, stream) # freed at its BDONE
+20
View File
@@ -570,6 +570,26 @@ class ParallelService(unittest.TestCase):
self.assertEqual(u["input_tokens"] + u["cache_read_input_tokens"], prompt, u)
self.assertEqual(u["cache_read_input_tokens"], 3, u) # the first read's reuse, not the continuation's
def test_a_slot_request_ended_by_its_stop_token_reports_the_slot_leg(self):
"""#1869: the consumer leaves at the stop token, before the BDONE: the request kept the admission's one token and
its few ms. Its figures are the slot leg's now (finish=stop was the broken case, finish=length read the BDONE)."""
import serve.server as server
self.start(2, slot_cache=True, reuse=True, more=("--readfield",))
res = {}
with mock.patch.object(server, "EOS_IDS", {257}): # the fake engine's stop token
a = threading.Thread(target=lambda: res.setdefault("a", self.chat("List three primes please", 200)))
b = threading.Thread(target=lambda: res.setdefault("b", self.chat("Name two rivers in Germany", 200)))
a.start(); b.start(); a.join(30); b.join(30)
self.assertEqual(len(self.svc.history), 2)
for h in self.svc.history:
self.assertEqual(h["engine_generated"], h["output_tokens"], h)
self.assertGreaterEqual(h["decode_ms"], 1.0, h)
for r in res.values():
t = r["timings"]
self.assertEqual(t["prompt_n"] + t["cache_n"], r["usage"]["prompt_tokens"], t)
self.assertEqual(t["predicted_n"], 10, t)
self.assertFalse(any(self.engine.slot_busy)) # the slots are free (no BDONE left for a wait)
def test_a_request_moved_between_solo_and_slot_keeps_its_totals(self):
"""#1869/#1870: a request that read its prompt and decoded on the solo path, was STOPped when another arrived and
went on in a slot (its admission copies the held prefix: prompt_read 1, one token) reports the prompt it read and