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"""Coverage past 32K: one 60K-token prompt on the 64K-context configuration, with a hidden code, so the run measures prompt
3throughput at that length and checks recall in the same request.
4
5    python long_context.py CONFIG OUT.json [--tokens 60000]
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 "
18         "lattice mirror nectar oasis pepper quarry raven saddle tundra umber vessel willow xenon yellow zephyr").split()
19
20
21def build(tokens, code, rng):
22    body = [rng.choice(WORDS) for _ in range(int(tokens / 1.35))]
23    at = rng.randrange(len(body) // 3, 2 * len(body) // 3)
24    body[at:at] = [f"The access code for the harbor gate is {code}."]
25    return ("Read the following log carefully, then answer the question at the end.\n\n" + " ".join(body) +
26            "\n\nQuestion: what is the access code for the harbor gate? Answer with the code only.")
27
28
29def main():
30    cfg_path, out_path = sys.argv[1], sys.argv[2]
31    tokens = int(sys.argv[3]) if len(sys.argv) > 3 else 60000
32    port = 8091
33    cfg = json.loads(Path(cfg_path).read_text())
34    cfg["port"] = port
35    cfg["log"] = "/tmp/long-context.log"
36    tmp = Path("/tmp/long-context.json")
37    tmp.write_text(json.dumps(cfg, indent=1))
38    proc = subprocess.Popen([sys.executable, str(ROOT / "serve" / "server.py"), "--engine", "strata", "--config",
39                             str(tmp), "--port", str(port)], cwd=str(ROOT),
40                            stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
41    try:
42        t0 = time.time()
43        while time.time() - t0 < 1800:
44            if proc.poll() is not None:
45                raise SystemExit(f"server exited {proc.returncode}")
46            try:
47                with urllib.request.urlopen(f"http://127.0.0.1:{port}/v1/models", timeout=5) as r:
48                    if r.status == 200:
49                        break
50            except Exception:
51                pass
52            time.sleep(3)
53        print(f"ready in {time.time() - t0:.0f} s", flush=True)
54        rng = random.Random(7)
55        code = "481516"
56        prompt = build(tokens, code, rng)
57        body = json.dumps({"messages": [{"role": "user", "content": prompt}], "max_tokens": 32, "temperature": 0,
58                           "chat_template_kwargs": {"enable_thinking": False}}).encode()
59        req = urllib.request.Request(f"http://127.0.0.1:{port}/v1/chat/completions", data=body,
60                                     headers={"Content-Type": "application/json"})
61        t1 = time.time()
62        with urllib.request.urlopen(req, timeout=3600) as r:
63            out = json.loads(r.read())
64        wall = time.time() - t1
65        t = out.get("timings") or {}
66        answer = out["choices"][0]["message"].get("content", "")
67        row = {"tokens": tokens, "prompt_n": t.get("prompt_n"), "prompt_per_second": t.get("prompt_per_second"),
68               "predicted_per_second": t.get("predicted_per_second"), "wall_s": round(wall, 1),
69               "answer": answer[:80], "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()