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.9 KiB88 linesraw
1#!/usr/bin/env python3
2"""Does the engine hold a context past the installed 64K? Copy the shipped config, raise the context to 128K, and send a
3100K-token prompt with a hidden code. Success = an answer that recalls it; failure is recorded as a limit.
4
5    python dgx_128k.py CONFIG OUT.json [--tokens 100000] [--ctx 131072]
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 100000
32    ctx = int(sys.argv[4]) if len(sys.argv) > 4 else 131072
33    port = 8091
34    cfg = json.loads(Path(cfg_path).read_text())
35    a = cfg["args"]
36    if "--max-context" in a:
37        a[a.index("--max-context") + 1] = str(ctx)
38    if "--kv-resident" in a:
39        a[a.index("--kv-resident") + 1] = "32768"
40    cfg["port"] = port
41    cfg["log"] = "/tmp/long-128k.log"
42    tmp = Path("/tmp/long-128k.json")
43    tmp.write_text(json.dumps(cfg, indent=1))
44    proc = subprocess.Popen([sys.executable, str(ROOT / "serve" / "server.py"), "--engine", "strata", "--config",
45                             str(tmp), "--port", str(port)], cwd=str(ROOT),
46                            stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
47    try:
48        t0 = time.time()
49        while time.time() - t0 < 1800:
50            if proc.poll() is not None:
51                raise SystemExit(f"server exited {proc.returncode}")
52            try:
53                with urllib.request.urlopen(f"http://127.0.0.1:{port}/v1/models", timeout=5) as r:
54                    if r.status == 200:
55                        break
56            except Exception:
57                pass
58            time.sleep(3)
59        print(f"ready in {time.time() - t0:.0f} s", flush=True)
60        rng = random.Random(11)
61        code = "271828"
62        prompt = build(tokens, code, rng)
63        body = json.dumps({"messages": [{"role": "user", "content": prompt}], "max_tokens": 32, "temperature": 0,
64                           "chat_template_kwargs": {"enable_thinking": False}}).encode()
65        req = urllib.request.Request(f"http://127.0.0.1:{port}/v1/chat/completions", data=body,
66                                     headers={"Content-Type": "application/json"})
67        t1 = time.time()
68        with urllib.request.urlopen(req, timeout=5400) as r:
69            out = json.loads(r.read())
70        wall = time.time() - t1
71        t = out.get("timings") or {}
72        answer = out["choices"][0]["message"].get("content", "")
73        row = {"tokens": tokens, "ctx": ctx, "prompt_n": t.get("prompt_n"),
74               "prompt_per_second": t.get("prompt_per_second"),
75               "predicted_per_second": t.get("predicted_per_second"), "wall_s": round(wall, 1),
76               "answer": answer[:80], "recalled": code in answer}
77        Path(out_path).write_text(json.dumps(row, indent=1))
78        print(json.dumps(row))
79    finally:
80        proc.terminate()
81        try:
82            proc.wait(180)
83        except subprocess.TimeoutExpired:
84            proc.kill()
85
86
87if __name__ == "__main__":
88    main()