okami.mom/strata

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

Niko1221Merge f-q36-vp (Qwen3.6 parallel requests and pictures)db4dd65e5

main
4.8 KiB103 linesraw
1"""par_bench.py - aggregate decode throughput of N requests at once: strata-q36 (--batch) and llama.cpp (llama-server -np N),
2same GGUF, same prompt ids, greedy, 128 new tokens each.  One JSON line per (engine, N, round).
3
4  python q36_par_bench.py strata --exe E --args "--model M --max-context 8192 --batch 8 --mtp 0" --ns 1,2,4,8 --rounds 3 --out F
5  python q36_par_bench.py llama --server BIN --model M --ns 1,2,4,8 --rounds 3 --out F [--port 8197]
6
7The strata wall time runs from the first BGEN to the last BDONE (admissions included); the llama one from the first request
8sent to the last answer (prompt reading included); tok/s = tokens produced / wall.  `tail_tok_s` (strata): the window phase
9only, after the last admission."""
10from pathlib import Path
11import argparse, json, os, shlex, subprocess, sys, threading, time, urllib.request
12sys.path.insert(0, __file__.rsplit("/", 1)[0] if "/" in __file__ else ".")
13from q36_harness import Eng, load_prompts
14
15ORDER = [0, 1, 2, 3, 5, 8, 0, 1]   # the prompts that run the full 128 tokens, repeated to fill 8 slots
16
17
18def strata(a):
19    prompts = load_prompts(a.prompts)
20    eng = Eng(a.exe, shlex.split(a.args), log=a.log)
21    out = open(a.out, "a")
22    for n in [int(x) for x in a.ns.split(",")]:
23        for rnd in range(a.rounds):
24            if n == 1 and not eng.info.get("batch_slots"):   # an engine without slots: the request alone (GEN)
25                toks, done, ttft, wall = eng.solo(prompts[ORDER[0]], a.n)
26                row = {"engine": "strata", "n": 1, "round": rnd, "tokens": len(toks), "wall": wall,
27                       "tok_s": len(toks) / wall, "tail_tok_s": len(toks) / max(1e-9, wall - (ttft or 0))}
28            else:
29                reqs = [{"ids": prompts[ORDER[i]], "n": a.n, "slot": i} for i in range(n)]
30                r = eng.batch(reqs)
31                tot = sum(len(t) for t in r["tokens"])
32                row = {"engine": "strata", "n": n, "round": rnd, "tokens": tot, "wall": r["wall"], "tok_s": tot / r["wall"],
33                       "tail_tok_s": r["tail_tok_s"]}
34            print(json.dumps(row), flush=True)
35            out.write(json.dumps(row) + "\n")
36            out.flush()
37    eng.close()
38
39
40def post(port, body):
41    return json.loads(urllib.request.urlopen(urllib.request.Request(f"http://127.0.0.1:{port}/completion", json.dumps(body).encode(),
42                                             {"Content-Type": "application/json"}), timeout=900).read())
43
44
45def llama(a):
46    prompts = load_prompts(a.prompts)
47    out = open(a.out, "a")
48    for n in [int(x) for x in a.ns.split(",")]:
49        cmd = [a.server, "-m", a.model, "-ngl", "99", "-fa", "on", "-c", str(8192 * n), "-np", str(n), "--port", str(a.port),
50               "--no-webui", "-t", "16"] + shlex.split(a.llama_args)
51        log = open(a.log or os.devnull, "a")
52        p = subprocess.Popen(cmd, stdout=log, stderr=log)
53        try:
54            for _ in range(240):
55                try:
56                    urllib.request.urlopen(f"http://127.0.0.1:{a.port}/health", timeout=2)
57                    break
58                except Exception:  # noqa: BLE001
59                    time.sleep(1)
60            for rnd in range(a.rounds + 1):   # round 0: the warm-up
61                res = [None] * n
62
63                def one(i):
64                    res[i] = post(a.port, {"prompt": prompts[ORDER[i]], "n_predict": a.n, "temperature": 0, "top_k": 1,
65                                           "cache_prompt": False, "id_slot": i})
66                ts = [threading.Thread(target=one, args=(i,)) for i in range(n)]
67                t0 = time.time()
68                for t in ts:
69                    t.start()
70                for t in ts:
71                    t.join()
72                wall = time.time() - t0
73                tot = sum(r["tokens_predicted"] for r in res)
74                if rnd == 0:
75                    continue
76                row = {"engine": "llama", "n": n, "round": rnd - 1, "tokens": tot, "wall": wall, "tok_s": tot / wall}
77                print(json.dumps(row), flush=True)
78                out.write(json.dumps(row) + "\n")
79                out.flush()
80        finally:
81            p.terminate()
82            try:
83                p.wait(timeout=60)
84            except subprocess.TimeoutExpired:
85                p.kill()
86
87
88ap = argparse.ArgumentParser()
89ap.add_argument("mode", choices=["strata", "llama"])
90ap.add_argument("--exe")
91ap.add_argument("--args", default="")
92ap.add_argument("--server")
93ap.add_argument("--model")
94ap.add_argument("--llama-args", default="")
95ap.add_argument("--ns", default="1,2,4,8")
96ap.add_argument("--rounds", type=int, default=3)
97ap.add_argument("--n", type=int, default=128)
98ap.add_argument("--prompts", default=str(Path(__file__).resolve().parent / "parity_prompts.ids"))
99ap.add_argument("--port", type=int, default=8197)
100ap.add_argument("--out", required=True)
101ap.add_argument("--log", default=None)
102a = ap.parse_args()
103(strata if a.mode == "strata" else llama)(a)