okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-clean-3 (321794636): docs and history (cleanup step 3, items 3.0-3.10); release manifest and capability table with checks, history/ with pointers, link check, tools/ split, bench index; owner OK'd the wording 11-082b36e8a02

main
3.8 KiB88 linesraw
1#!/usr/bin/env python3
2"""#1604: with --batch N --batch-groups N on a layer split, two long conversations that alternate must reuse their slot
3(or the conversation cache) on every later turn instead of reading their prompt from token 0.
4
5  python3 research/tools/live-probes-2026-10/batch_groups_cache_test.py CFG OUT.json
6  STRATA_EXTRA_ARGS="--batch 2 --batch-groups 2 --prompt-cache 4 --conversation-cache-mib 4096 --conversation-cache-slots 3"
7
8Three rounds; each round runs conversation A and conversation B at the same time (so both go through the batch slots),
9the next round continues each one (prompt + its answer + a new user turn).  The engine log says per request how many
10prompt tokens were reused.  PASS when every request of rounds 2 and 3 reused at least 90 % of the previous round's
11prompt.  The answers (token ids) are written to OUT.json to compare two runs.
12"""
13import json, os, re, sys, threading
14from pathlib import Path
15
16REPO = Path(os.environ.get("STRATA_REPO", Path(__file__).resolve().parents[3]))
17sys.path.insert(0, str(REPO / "tools")); sys.path.insert(0, str(REPO))
18import strata_tokenizer as ST
19from serve.server import StrataEngine, child_env, engine_args
20
21cfg = json.load(open(sys.argv[1]))
22tp = Path(cfg["tokenizer"])
23vocab = json.loads((tp / "vocab.json").read_text())
24toks = [None] * len(vocab)
25for t, i in vocab.items():
26    toks[i] = t
27tok = ST.Tokenizer(toks, (tp / "merges.txt").read_text().split("\n"), json.loads((tp / "token_type.json").read_text()))
28if os.environ.get("GPUS"):   # e.g. GPUS=0,1: a layer split over those cards (SPLIT=auto)
29    cfg["gpu"] = [int(x) for x in os.environ["GPUS"].split(",")]
30    cfg["layer_split"] = os.environ.get("SPLIT", "auto")
31args = engine_args(cfg) + os.environ.get("STRATA_EXTRA_ARGS", "").split()
32log = os.environ.get("SMOKE_LOG", cfg["log"])
33eng = StrataEngine(os.environ.get("STRATA_EXE", cfg["exe"]), args, cwd=cfg.get("cwd"), log=log, env=child_env(cfg))
34GEN = "<|im_end|>\n<|im_start|>assistant\n<think>\n\n</think>\n\n"
35
36
37def enc(text):
38    return tok.encode(text, parse_special=True)
39
40
41def first(tag, n):
42    return enc(f"<|im_start|>user\n[{tag}] " + " ".join(f"Entry {i} of {tag}: value {i * 13 % 97}." for i in range(n))
43               + "\nCount from 1 to 80 separated by commas." + GEN)
44
45
46conv = {"A": first("alpha", 250), "B": first("bravo", 250)}
47answers = {"A": [], "B": []}
48prev_len = {}
49rounds = []
50
51
52def one(name, ids, out):
53    res = [t for t in eng.generate(ids, 80, {"temperature": 0}, threading.Event()) if t is not None]
54    out[name] = res
55
56
57for rnd in range(3):
58    lens = {k: len(v) for k, v in conv.items()}
59    out = {}
60    ts = [threading.Thread(target=one, args=(k, conv[k], out)) for k in ("A", "B")]
61    for t in ts:
62        t.start()
63    for t in ts:
64        t.join()
65    rounds.append({"prompt_lens": lens, "answers": out})
66    print(f"round {rnd + 1}: prompts {lens}, answers {[len(v) for v in out.values()]}", flush=True)
67    for k in conv:
68        answers[k].append(out[k])
69        conv[k] = conv[k] + out[k] + enc(f"<|im_end|>\n<|im_start|>user\nNow count from {rnd + 2} to {rnd + 60}." + GEN)
70eng.proc.stdin.close()
71try:
72    eng.proc.wait(60)
73except Exception:
74    eng.proc.kill()
75json.dump(rounds, open(sys.argv[2], "w"))
76
77text = open(log, errors="replace").read()
78reqs = [(int(a), int(b)) for a, b in re.findall(r"strata serve: prompt (\d+) tokens = (\d+) reused", text)]
79print("requests (prompt tokens, reused):", reqs)
80print("slot gave back lines:", len(re.findall(r"gave back", text)), " conversation cache restores:",
81      len(re.findall(r"conversation cache: restored", text)))
82# rounds 2 and 3 are the last 4 requests of the log
83ok = len(reqs) >= 6
84for p, r in reqs[2:]:
85    if r < 0.9 * (p - 140):   # the previous prompt + answer is ~all of it but the new turn
86        ok = False
87print("PASS" if ok else "FAIL")
88sys.exit(0 if ok else 1)