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.6 KiB81 linesraw
1#!/usr/bin/env python3
2"""Is decode at 56K limited by KV streaming? Raise --kv-resident to the whole context if it fits, and re-measure the same
356K prompt. Writes one row; compare it with the 31.0 tok/s of the shipped 32,768-cell setting.
4
5    python dgx_kvresident.py CONFIG OUT.json [--tokens 56000]
6"""
7import json
8import random
9import subprocess
10import sys
11import time
12import urllib.request
13from pathlib import Path
14
15ROOT = Path(__file__).resolve().parents[3]
16WORDS = ("river stone market lantern copper winter garden signal harbor needle orbit velvet canyon ember ledger quartz "
17         "meadow anchor violet thunder cedar marble piston ribbon cobalt falcon glacier hollow ivory juniper kernel").split()
18
19
20def build(tokens, code, rng):
21    body = [rng.choice(WORDS) for _ in range(int(tokens / 1.35))]
22    at = rng.randrange(len(body) // 3, 2 * len(body) // 3)
23    body[at:at] = [f"The access code for the harbor gate is {code}."]
24    return ("Read the following log carefully, then answer the question at the end.\n\n" + " ".join(body) +
25            "\n\nQuestion: what is the access code for the harbor gate? Answer with the code only.")
26
27
28def main():
29    cfg_path, out_path = sys.argv[1], sys.argv[2]
30    tokens = int(sys.argv[3]) if len(sys.argv) > 3 else 56000
31    port = 8091
32    cfg = json.loads(Path(cfg_path).read_text())
33    cfg["port"] = port
34    cfg["log"] = "/tmp/kvresident.log"
35    tmp = Path("/tmp/kvresident.json")
36    tmp.write_text(json.dumps(cfg, indent=1))
37    proc = subprocess.Popen([sys.executable, str(ROOT / "serve" / "server.py"), "--engine", "strata", "--config",
38                             str(tmp), "--port", str(port)], cwd=str(ROOT),
39                            stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
40    try:
41        t0 = time.time()
42        while time.time() - t0 < 1800:
43            if proc.poll() is not None:
44                raise SystemExit(f"server exited {proc.returncode} (see /tmp/kvresident.log)")
45            try:
46                with urllib.request.urlopen(f"http://127.0.0.1:{port}/v1/models", timeout=5) as r:
47                    if r.status == 200:
48                        break
49            except Exception:
50                pass
51            time.sleep(3)
52        print(f"ready in {time.time() - t0:.0f} s", flush=True)
53        rng = random.Random(7)
54        code = "481516"
55        prompt = build(tokens, code, rng)
56        body = json.dumps({"messages": [{"role": "user", "content": prompt}], "max_tokens": 64, "temperature": 0,
57                           "chat_template_kwargs": {"enable_thinking": False}}).encode()
58        req = urllib.request.Request(f"http://127.0.0.1:{port}/v1/chat/completions", data=body,
59                                     headers={"Content-Type": "application/json"})
60        t1 = time.time()
61        with urllib.request.urlopen(req, timeout=3600) as r:
62            out = json.loads(r.read())
63        wall = time.time() - t1
64        t = out.get("timings") or {}
65        answer = out["choices"][0]["message"].get("content", "")
66        row = {"tokens": tokens, "kv_resident": cfg["args"][cfg["args"].index("--kv-resident") + 1],
67               "prompt_n": t.get("prompt_n"), "prompt_per_second": t.get("prompt_per_second"),
68               "predicted_n": t.get("predicted_n"), "predicted_per_second": t.get("predicted_per_second"),
69               "wall_s": round(wall, 1), "recalled": code in answer}
70        Path(out_path).write_text(json.dumps(row, indent=1))
71        print(json.dumps(row))
72    finally:
73        proc.terminate()
74        try:
75            proc.wait(180)
76        except subprocess.TimeoutExpired:
77            proc.kill()
78
79
80if __name__ == "__main__":
81    main()