From e15937e5f7f5274cc92ada1325e38bb796fcc310 Mon Sep 17 00:00:00 2001 From: Shogo Okada <33060490+okada-shogo@users.noreply.github.com> Date: Tue, 24 Nov 2026 07:57:39 +0900 Subject: [PATCH] serve: turn_at on the batch slots' GEN and BGEN lines, as one request's With "parallel" 2 or more, every request goes through the slot path, whose GEN and BGEN lines had no turn_at=N. The one-request path sends it, and the engine reads a prompt in calls that stop at that checkpoint. Inkling 2's greedy answer depends on how a prompt's rows are grouped into calls, so the same chat answered differently at "parallel" 1 and at 4 or 8 (reported by VecSzn on PR 226), even with every expert in VRAM: on an RTX 3090 with all 3,072 experts on the card, the same five GEN lines with turn_at removed changed two of five answers (at tokens 7 and 47), while --batch 0, 4 and 4-at-once gave the same ids for the same lines. The slot path now adds turn_key() to its keys, except for Hy4, as the one-request path does. The fake engine in test_parallel can log the raw lines (--rawlog); SlotTurnAt checks that a request alone and two admissions each carry the turn_at of their prompt (both fail without the fix). Co-Authored-By: Claude Opus 5.5 --- terminal/serve/server.py | 3 +++ terminal/serve/test_parallel.py | 36 +++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/terminal/serve/server.py b/terminal/serve/server.py index 698f2368fe..5c5c2b7ba8 100644 --- a/terminal/serve/server.py +++ b/terminal/serve/server.py @@ -2127,6 +2127,9 @@ def generate_batched(self, ids, max_new, sampling, cancel, embeddings=None): born = self.gen # the engine process this request is sent to hold = (sampling or {}).get(HOLD_KEY) # the thinking budget's hold (P2b): the reasoning tokens left when this pass starts, and 's id keys0 = self.sampling_keys({k: v for k, v in (sampling or {}).items() if k != HOLD_KEY}, hy4=getattr(self, "hy4_engine", False)) # (the hold's key goes on per leg: leg_keys below) + # turn_at as the one-request path sends it: the engine reads a prompt in calls that stop at that checkpoint, + # and Inkling 2's greedy answer depends on how a prompt's rows are grouped into calls + keys0 += "" if getattr(self, "hy4_engine", False) else self.turn_key(ids) # logprobs: only an admission's token can have an LP line here (the slot's "BT" lines carry none), so the # server refuses these requests unless max_tokens is 1. The lines go to the request itself, through its # cancel Event: several requests read control lines at once, and the engine's lp_lines is one attribute. diff --git a/terminal/serve/test_parallel.py b/terminal/serve/test_parallel.py index 8907151902..01138b1952 100644 --- a/terminal/serve/test_parallel.py +++ b/terminal/serve/test_parallel.py @@ -104,6 +104,8 @@ def window(): # one batch window: every active slot one token if "--fake-stage-sample" in args and processed and not active: print("PSTAT 4 20000 16000 14000 12000 10000", flush=True) log = open(args[args.index("--log") + 1], "a") if "--log" in args else None +# --rawlog PATH: every GEN / BGEN line as the server sent it (its keys, e.g. turn_at) +rawlog = open(args[args.index("--rawlog") + 1], "a") if "--rawlog" in args else None PPK = int(args[args.index("--pp-every") + 1]) if "--pp-every" in args else 1 READF = "--readfield" in args # DONE's 15th field: the prompt tokens the engine read (a continued prompt: the 1 it copies) REUSE = "--reuse" in args # DONE's reused field as the engine gives it: a continued prompt is all held but its last @@ -127,6 +129,8 @@ def reused(ids): # token (the request's own output included), a fre stopped.add(b) continue if line.startswith(("GEN ", "BGEN ")): + if rawlog: + rawlog.write(line + "\n"); rawlog.flush() f = line.split() slot = int(f[1]) if f[0] == "BGEN" else None max_new = int(f[2] if f[0] == "BGEN" else f[1]) @@ -1293,5 +1297,37 @@ def test_explicit_values_reach_the_engine(self): self.assertEqual(env["STRATA_CKPT_POOL"], "0") +class SlotTurnAt(FakeEngineCase): + """With batch slots every GEN and BGEN names the prompt's last turn (turn_at), as one request's GEN does: the + engine reads a prompt in calls that stop there, and Inkling 2's greedy answer depends on that grouping.""" + + def raw(self): + return [line.split() for line in self.rawlog.read_text().splitlines()] + + def start_raw(self, slots): + self.rawlog = Path(tempfile.mkdtemp()) / "raw.log" + self.addCleanup(lambda: self.rawlog.parent.exists() and __import__("shutil").rmtree(self.rawlog.parent)) + self.start(slots, more=("--rawlog", str(self.rawlog))) + + def expected(self, f): + return self.engine.turn_key([int(x) for x in f[-1].split(",")]).strip() + + def test_a_request_alone_carries_turn_at(self): + self.start_raw(2) + self.chat("hello there", max_tokens=8) + (f,) = self.raw() + self.assertEqual(f[0], "GEN", f) + self.assertTrue(self.expected(f).startswith("turn_at="), "the template's turn opener is in the prompt") + self.assertIn(self.expected(f), f) + + def test_admissions_carry_turn_at(self): + self.start_raw(2) + BatchLegAccounting.queued_chats(self) # two requests waiting at once: both go to slots + bgen = [f for f in self.raw() if f[0] == "BGEN"] + self.assertGreaterEqual(len(bgen), 2, self.raw()) + for f in bgen: + self.assertIn(self.expected(f), f) + + if __name__ == "__main__": unittest.main()