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++

#465 tools: batch_interleave_test.py (a prompt read beside decoding slots, BYIELD, a next turn from its slot); batch_test.py on Windows

Niko122194e4b623c0ac
diff --git a/serve/server.py b/serve/server.py
index 15e8de13d..241503542 100644
--- a/serve/server.py
+++ b/serve/server.py
@@ -2017,11 +2017,11 @@ class Service:
if par: # #465: the requests running together, slot by slot
with self.status_lock:
running = len(self.live_reqs)
- waiting = int(getattr(self.engine, "waiting", 0) or 0)
slots = self.engine.slots_view() if hasattr(self.engine, "slots_view") else []
- busy_slots = sum(1 for x in slots if x["state"] != "idle")
- live.update(parallel=par, running=running, slots=slots,
- waiting_for_slot=max(0, running - busy_slots) if running > busy_slots else waiting)
+ # the requests in flight that are not in a slot: the one alone on the solo path, or waiting their turn
+ in_slots = sum(1 for x in slots if x["state"] != "idle")
+ live.update(parallel=par, running=running, slots=slots, outside_slots=max(0, running - in_slots),
+ waiting=int(getattr(self.engine, "waiting", 0) or 0))
if running and state == "idle":
live["state"] = "generating"
engine = {"model": self.model, "max_context": self.engine.max_context, "images": self.vision is not None,
diff --git a/tools/batch_interleave_test.py b/tools/batch_interleave_test.py
new file mode 100644
index 000000000..9b2a1a7ff
--- /dev/null
+++ b/tools/batch_interleave_test.py
@@ -0,0 +1,189 @@
+#!/usr/bin/env python3
+"""Batch slots, the parts batch_test.py does not reach (#465): a long prompt read WHILE other slots decode (its
+chunks interleaved with their windows), and a conversation's next turn continued from the slot that holds it.
+
+ 1. solo references (GEN): A, then A's next turn (from the live session), B, C (a long prompt)
+ 2. the same in the slots: A in slot 0, B in slot 1 next to it, then C in slot 2 while A and B decode - every slot's
+ tokens must equal its solo tokens
+ 3. A's next turn (BGEN into slot 0, which still holds A): read from the slot, its tokens must equal the solo next turn
+
+Exact comparisons need the same settings as batch_test.py (this script sets STRATA_IQ_MT_MIN=1):
+ python tools/batch_interleave_test.py --exe build/strata --config strata-<model>.json \\
+ --extra "--pcie-frac 0 --adapt-every 1000000 --no-prefill-borrow"
+Without --no-prefill-borrow the slots decoding during C's read see the expert cache without the slots C's prompt
+borrowed (those experts run on the CPU, which rounds differently): A's and B's text may then drift from solo.
+"""
+import argparse, json, os, sys, time
+from pathlib import Path
+
+sys.path.insert(0, str(Path(__file__).resolve().parent))
+from batch_test import Engine, tokenizer # noqa: E402
+
+LONG = ("The history of mathematics is long and full of surprising turns. Early civilizations counted with tally marks, "
+ "then with symbols, and later with place-value systems that made arithmetic far easier. ")
+
+
+def run(eng, out, line, slot=None):
+ """Send one GEN/BGEN; returns (tokens of the request line, the BADM flag) - BT/BDONE of other slots are kept."""
+ eng.send(line)
+ got = []
+ for l in out:
+ if l.startswith("T "):
+ got.append(int(l.split()[1]))
+ elif l.startswith(("BT ", "BDONE ")):
+ eng.pending.append(l)
+ elif l.startswith("ERR"):
+ raise SystemExit("engine: " + l)
+ elif slot is None and l.startswith("DONE"):
+ return got, None
+ elif slot is not None and l.startswith("BADM "):
+ return got, l.split()[2] == "1"
+ raise SystemExit(f"the engine ended during: {line[:40]} (see {eng.log_path})")
+
+
+def main():
+ ap = argparse.ArgumentParser()
+ ap.add_argument("--exe", required=True)
+ ap.add_argument("--config", required=True)
+ ap.add_argument("--max-new", type=int, default=200)
+ ap.add_argument("--long", type=int, default=5000, help="tokens of the long prompt C (several prompt chunks)")
+ ap.add_argument("--extra", default="")
+ a = ap.parse_args()
+ cfg = json.loads(Path(a.config).read_text())
+ tok = tokenizer(cfg["tokenizer"])
+
+ def chat(q):
+ return tok.encode(f"<|im_start|>user\n{q}<|im_end|>\n<|im_start|>assistant\n<think>\n\n</think>\n\n",
+ parse_special=True)
+ body = LONG * (a.long // max(1, len(tok.encode(LONG))) + 1)
+ A = chat("Write a Python function that merges overlapping intervals, then explain it.")
+ B = chat("Compare TCP and QUIC: handshake, congestion control, multiplexing.")
+ C = chat(body + "\nSummarize the text above in three sentences.")
+ D = chat(body + "\nWhat are the three most important ideas in the text above?")
+ E = chat("Describe the life cycle of a star like the Sun.")
+ print(f"prompts: A {len(A)}, B {len(B)}, C {len(C)} tokens", flush=True)
+ eng = Engine(a.exe, cfg, 3, {"STRATA_IQ_MT_MIN": "1"}, a.extra.split())
+ eng.pending = []
+ out = eng.lines()
+ ids = lambda v: ",".join(map(str, v))
+ M = a.max_new
+
+ # 1. solo
+ sA, _ = run(eng, out, f"GEN {M} {ids(A)}")
+ A2 = A + sA + chat("Now do the same in Rust.")
+ sA2, _ = run(eng, out, f"GEN {M} {ids(A2)}")
+ sB, _ = run(eng, out, f"GEN {M} {ids(B)}")
+ sC, _ = run(eng, out, f"GEN {M} {ids(C)}")
+ sD, _ = run(eng, out, f"GEN {M} {ids(D)}")
+ sE, _ = run(eng, out, f"GEN {M} {ids(E)}")
+ print(f"solo: A {len(sA)}, A2 {len(sA2)}, B {len(sB)}, C {len(sC)}, D {len(sD)}, E {len(sE)} tokens", flush=True)
+
+ # 2. the slots: A, B, then the long C while they decode
+ got = {0: [], 1: [], 2: []}
+ done = {}
+ t0 = time.time()
+ for s, P in ((0, A), (1, B), (2, C)):
+ first, cont = run(eng, out, f"BGEN {s} {M} {ids(P)}", slot=s)
+ got[s] += first
+ if not cont:
+ done[s] = True
+ t_admit = time.time() - t0
+ during_c = sum(1 for l in eng.pending if l.startswith("BT ")) # windows the slots ran while C was admitted
+
+ def drain():
+ while eng.pending or len(done) < 3:
+ l = eng.pending.pop(0) if eng.pending else next(out)
+ if l.startswith("BT "):
+ _, s, y = l.split()
+ got[int(s)].append(int(y))
+ elif l.startswith("BDONE "):
+ done[int(l.split()[1])] = True
+ elif l.startswith("ERR"):
+ raise SystemExit("engine: " + l)
+ drain()
+ print(f"slots: admissions {t_admit:.1f}s; {during_c} slot tokens arrived while the admissions read their prompts",
+ flush=True)
+ ok = True
+ for s, ref, name in ((0, sA, "A"), (1, sB, "B"), (2, sC, "C")):
+ same = got[s] == ref
+ ok &= same
+ d = next((k for k in range(min(len(got[s]), len(ref))) if got[s][k] != ref[k]), None)
+ print(f"slot {s} ({name}): {len(got[s])} tokens, solo {len(ref)}: "
+ f"{'IDENTICAL' if same else f'DIFFERS at {d}'}", flush=True)
+
+ # 3. A's next turn from slot 0 (the engine's log says "slot 0 gave back ...")
+ first, cont = run(eng, out, f"BGEN 0 {M} {ids(A2)}", slot=0)
+ got2, done = first, {}
+ if cont:
+ while True:
+ l = eng.pending.pop(0) if eng.pending else next(out)
+ if l.startswith("BT 0 "):
+ got2.append(int(l.split()[2]))
+ elif l.startswith("BDONE 0 "):
+ break
+ same = got2 == sA2
+ ok &= same
+ d = next((k for k in range(min(len(got2), len(sA2))) if got2[k] != sA2[k]), None)
+ print(f"A's next turn from its slot: {len(got2)} tokens, solo {len(sA2)}: "
+ f"{'IDENTICAL' if same else f'DIFFERS at {d}'}", flush=True)
+ # 4. a long prompt D gives way (BYIELD) to a short E at its first chunk boundary, then goes on from its slot
+ eng.send(f"BGEN 2 {M} {ids(D)}")
+ eng.send("BYIELD 2")
+ yielded = None
+ for l in out:
+ if l.startswith("YIELDED "):
+ yielded = l
+ elif l.startswith(("BT ", "BDONE ")):
+ eng.pending.append(l)
+ elif l.startswith("ERR"):
+ raise SystemExit("engine: " + l)
+ elif l.startswith("BADM 2 "):
+ break
+ print(f"D gave way: {yielded}", flush=True)
+ ok &= yielded is not None
+ got4 = {1: [], 2: []}
+ first, cont1 = run(eng, out, f"BGEN 1 {M} {ids(E)}", slot=1)
+ got4[1] += first
+ first, cont2 = run(eng, out, f"BGEN 2 {M} {ids(D)}", slot=2)
+ got4[2] += first
+ done4 = {s for s, c in ((1, cont1), (2, cont2)) if not c}
+ while len(done4) < 2:
+ l = eng.pending.pop(0) if eng.pending else next(out)
+ if l.startswith("BT "):
+ _, s, y = l.split()
+ if int(s) in got4:
+ got4[int(s)].append(int(y))
+ elif l.startswith("BDONE "):
+ done4.add(int(l.split()[1]))
+ for s, ref, name in ((1, sE, "E (short, admitted while D waited)"), (2, sD, "D (gave way, then went on)")):
+ same = got4[s] == ref
+ ok &= same
+ d = next((k for k in range(min(len(got4[s]), len(ref))) if got4[s][k] != ref[k]), None)
+ print(f"slot {s} {name}: {len(got4[s])} tokens, solo {len(ref)}: "
+ f"{'IDENTICAL' if same else f'DIFFERS at {d}'}", flush=True)
+ # 5. (a measurement, not a check) a SOLO next turn continued from slot 0: its tokens are exact either way, but the
+ # draft layer's own K/V was built for another conversation, so fewer drafts may be accepted than in a solo next
+ # turn whose drafter read the conversation (A2 in step 1)
+ A3 = A2 + got2 + chat("Now in Go.")
+ eng.send(f"GEN {M} {ids(A3)}")
+ for l in out:
+ if l.startswith(("BT ", "BDONE ")):
+ eng.pending.append(l)
+ elif l.startswith("DONE"):
+ f = l.split()
+ print(f"solo next turn from a slot: drafts accepted {f[6]} of {f[7]} ({f[1]} tokens in {f[4]} ms)",
+ flush=True)
+ break
+ eng.send("QUIT")
+ eng.p.wait(timeout=180)
+ log = Path(eng.log_path).read_text(errors="replace")
+ for l in log.splitlines():
+ if "drafts accepted" in l:
+ print(" log:", l.split("strata serve: ")[-1][:160], flush=True)
+ for key in ("gave back", "takes", "the prompt was read in", "gives way"):
+ print(f"engine log '{key}': {log.count(key)} lines", flush=True)
+ return 0 if ok else 2
+
+
+if __name__ == "__main__":
+ sys.exit(main())
diff --git a/tools/batch_test.py b/tools/batch_test.py
index 835b1c4e8..4cb76027d 100644
--- a/tools/batch_test.py
+++ b/tools/batch_test.py
@@ -5,7 +5,7 @@ windows (BGEN), greedy. Every slot's tokens must equal its solo tokens; prints
python3 tools/batch_test.py --exe engine/strata --config strata-<model>.json --batch 4 --n 4 \
--extra "--layer-split 12,24,36 --trim-stage-weights --pcie-frac 0 --adapt-every 1000000"
"""
-import argparse, json, os, subprocess, sys, threading, time
+import argparse, json, os, subprocess, sys, tempfile, threading, time
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent))
@@ -44,13 +44,16 @@ class Engine:
args += extra_args
env = dict(os.environ, **extra_env)
env["LD_LIBRARY_PATH"] = ":".join(cfg.get("lib_dirs", []) + [env.get("LD_LIBRARY_PATH", "")])
- self.log = open("/tmp/batch_test_engine.log", "w")
+ if os.name == "nt":
+ env["PATH"] = os.pathsep.join(cfg.get("lib_dirs", []) + [env.get("PATH", "")])
+ self.log_path = os.environ.get("BATCH_TEST_LOG") or os.path.join(tempfile.gettempdir(), "batch_test_engine.log")
+ self.log = open(self.log_path, "w")
self.p = subprocess.Popen([exe, "--serve", *args], cwd=cfg.get("cwd"), stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=self.log, text=True, bufsize=1, env=env)
for line in self.p.stdout:
if line.startswith("READY"):
return
- raise SystemExit("the engine ended before READY - see /tmp/batch_test_engine.log")
+ raise SystemExit(f"the engine ended before READY - see {self.log_path}")
def send(self, line):
self.p.stdin.write(line + "\n")
@@ -71,6 +74,7 @@ def main():
ap.add_argument("--skip-solo", action="store_true")
ap.add_argument("--keys", default="", help='sampling keys for every request, e.g. "temperature=0.7 top_k=20"')
ap.add_argument("--extra", default="", help='more engine arguments in one string, e.g. "--adapt-every 1000000"')
+ ap.add_argument("--mt-min", default="1", help="STRATA_IQ_MT_MIN for the engine (1: exact; empty: the default)")
a = ap.parse_args()
cfg = json.loads(Path(a.config).read_text())
tok = tokenizer(cfg["tokenizer"])
@@ -78,7 +82,10 @@ def main():
for q in QUESTIONS[: a.n]:
text = f"<|im_start|>user\n{q}<|im_end|>\n<|im_start|>assistant\n<think>\n\n</think>\n\n"
prompts.append(tok.encode(text, parse_special=True))
- eng = Engine(a.exe, cfg, a.batch, {"STRATA_IQ_MT_MIN": "1", "STRATA_DECODE_TIMING": "1", **({"STRATA_VERIFY_PROFILE": "1"} if os.environ.get("PROF") else {})}, a.extra.split())
+ env = {"STRATA_DECODE_TIMING": "1", **({"STRATA_VERIFY_PROFILE": "1"} if os.environ.get("PROF") else {})}
+ if a.mt_min:
+ env["STRATA_IQ_MT_MIN"] = a.mt_min
+ eng = Engine(a.exe, cfg, a.batch, env, a.extra.split())
out = eng.lines()
solo = []
@@ -144,7 +151,7 @@ def main():
ok &= same
first_diff = next((k for k in range(min(len(b), len(solo[i]))) if b[k] != solo[i][k]), None)
print(f"slot {i}: {len(b)} tokens, solo {len(solo[i])}: {'IDENTICAL' if same else f'DIFFERS at {first_diff}'}")
- print(" ", repr(tok.decode(b)[:160]))
+ print(" ", ascii(tok.decode(b)[:160]))
eng.send("QUIT")
eng.p.wait(timeout=180) # the next run needs the GPUs back
return 0 if ok else 2