okami.mom/strata

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

Niko1221Merge commit 'b8d604d28' into f-clean-1ab0fc432ed

main
5.3 KiB115 linesraw
1"""batch_gate.py - the parallel-requests gate for strata-q36 (the tools/batch_test.py idea over the serve protocol).
2
3  python q36_batch_gate.py --exe E --args "--model M --max-context 8192 --batch 4" --slots 4 --n 128 [--golden G.json]
4                       [--long 3000] [--keys "temperature=0.7 seed=3"] [--out F.json]
5
61. every prompt alone (GEN) on the same engine -> solo tokens (and, with --golden, equal to the base engine's);
72. the first --slots prompts through BGEN, admitted one after the other, then decoding together -> every slot's tokens
8   must equal its solo tokens; the decode rate of the window phase;
93. --waves: the 10 prompts in waves of --slots (slot reuse: the cached state of a finished slot is taken over);
104. --long L: a prompt of about L tokens admitted into the last slot while the others decode (its prompt chunks run
11   between their windows); the others' tokens must still equal their solo tokens, and so must the long one's.
12"""
13from pathlib import Path
14import argparse, json, shlex, sys
15sys.path.insert(0, __file__.rsplit("/", 1)[0] if "/" in __file__ else ".")
16from q36_harness import Eng, load_prompts
17
18
19def same(a, b):
20    return a == b
21
22
23ap = argparse.ArgumentParser()
24ap.add_argument("--exe", required=True)
25ap.add_argument("--args", default="")
26ap.add_argument("--slots", type=int, default=4)
27ap.add_argument("--n", type=int, default=128)
28ap.add_argument("--prompts", default=str(Path(__file__).resolve().parent / "parity_prompts.ids"))
29ap.add_argument("--golden", default=None)
30ap.add_argument("--keys", default="")
31ap.add_argument("--long", type=int, default=0)
32ap.add_argument("--waves", action="store_true")
33ap.add_argument("--skip-solo-bench", action="store_true")
34ap.add_argument("--out", default=None)
35ap.add_argument("--log", default=None)
36ap.add_argument("--env", default="")
37a = ap.parse_args()
38env = dict(kv.split("=", 1) for kv in a.env.split() if "=" in kv)
39eng = Eng(a.exe, shlex.split(a.args), env=env, log=a.log)
40print("ready:", eng.ready, "info:", eng.info, "load %.1fs" % eng.load_s, flush=True)
41prompts = load_prompts(a.prompts)
42ok = True
43res = {"args": a.args, "slots": a.slots}
44
45# 1. solo
46solo, solo_rate = [], []
47for i, ids in enumerate(prompts):
48    toks, done, ttft, wall = eng.solo(ids, a.n, a.keys)
49    solo.append(toks)
50    solo_rate.append(len(toks) / max(1e-9, wall - (ttft or 0)))
51print("solo: %d prompts, decode %.1f tok/s median" % (len(solo), sorted(solo_rate)[len(solo_rate) // 2]), flush=True)
52if a.golden:
53    g = json.load(open(a.golden))["tokens"]
54    eq = [same(x, y) for x, y in zip(solo, g)]
55    print("solo vs golden (base engine):", "identical %d of %d" % (sum(eq), len(eq)), flush=True)
56    ok &= all(eq)
57res["solo_tok_s_median"] = sorted(solo_rate)[len(solo_rate) // 2]
58
59# 2. batch
60S = min(a.slots, len(prompts))
61reqs = [{"ids": prompts[i], "n": a.n, "slot": i, "keys": a.keys} for i in range(S)]
62r = eng.batch(reqs)
63eq = [same(r["tokens"][i], solo[i]) for i in range(S)]
64print("batch %d: %d of %d slots identical to solo | wall %.2fs, window phase %d tokens in %.2fs = %.1f tok/s (aggregate)"
65      % (S, sum(eq), S, r["wall"], r["tail_tokens"], r["tail_s"], r["tail_tok_s"]), flush=True)
66for i, e in enumerate(eq):
67    if not e:
68        x, y = r["tokens"][i], solo[i]
69        k = next((k for k in range(min(len(x), len(y))) if x[k] != y[k]), min(len(x), len(y)))
70        print("  slot %d differs at token %d (lens %d / %d)" % (i, k, len(x), len(y)))
71ok &= all(eq)
72res["batch"] = {"slots": S, "identical": sum(eq), "wall": r["wall"], "tail_tok_s": r["tail_tok_s"], "tail_tokens": r["tail_tokens"]}
73
74# 3. waves (slot reuse)
75if a.waves:
76    wave_ok = True
77    for w0 in range(0, len(prompts), S):
78        idx = list(range(w0, min(w0 + S, len(prompts))))
79        rq = [{"ids": prompts[i], "n": a.n, "slot": j, "keys": a.keys} for j, i in enumerate(idx)]
80        rr = eng.batch(rq)
81        for j, i in enumerate(idx):
82            if not same(rr["tokens"][j], solo[i]):
83                wave_ok = False
84                print("  wave %d prompt %d differs" % (w0 // S, i))
85    print("waves of %d: %s" % (S, "all identical" if wave_ok else "DIFFERENT"), flush=True)
86    ok &= wave_ok
87
88# 4. a long prompt admitted while the others decode
89if a.long and S >= 2:
90    base = prompts[0]
91    filler = []
92    while len(filler) < a.long:
93        for p in prompts:
94            filler += p[3:-8] if len(p) > 12 else p
95    longids = filler[:a.long] + prompts[1][-8:]
96    toks, done, ttft, wall = eng.solo(longids, 48, a.keys)
97    reqs = [{"ids": prompts[i], "n": a.n * 2, "slot": i, "keys": a.keys} for i in range(S - 1)]
98    # the first S-1 are admitted first; then the long one is admitted while they decode
99    longreq = {"ids": longids, "n": 48, "slot": S - 1, "keys": a.keys}
100    solo2 = []
101    for i in range(S - 1):
102        t2, _, _, _ = eng.solo(prompts[i], a.n * 2, a.keys)
103        solo2.append(t2)
104    rr = eng.batch(reqs + [longreq])
105    eq = [same(rr["tokens"][i], solo2[i]) for i in range(S - 1)] + [same(rr["tokens"][S - 1], toks)]
106    print("long prompt (%d tokens) admitted beside %d decoding slots: %s | admission stamps %s"
107          % (len(longids), S - 1, "%d of %d identical" % (sum(eq), len(eq)), ["%.2f" % x for x in rr["admit_stamps"]]), flush=True)
108    ok &= all(eq)
109
110res["ok"] = ok
111if a.out:
112    json.dump(res, open(a.out, "w"))
113eng.close()
114print("GATE", "PASS" if ok else "FAIL", flush=True)
115sys.exit(0 if ok else 1)