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.4 KiB82 linesraw
1#!/usr/bin/env python3
2"""Quality check for a numerically different path (the fused IQ prompt kernels): run the same greedy prompts through
3two configs and compare the answers.
4
5    python quality_fused.py strata-iq3s-nofused.json strata-iq3s-best.json out.json
6"""
7import json
8import subprocess
9import sys
10import time
11import urllib.request
12from pathlib import Path
13
14ROOT = Path(__file__).resolve().parents[3]
15PROMPTS = [
16    "Write a Python function that merges two sorted lists into one sorted list, with a docstring and two tests.",
17    "Explain in two short paragraphs how a heat pump moves heat from outside to inside a house.",
18    "A train leaves at 09:15 and travels 210 km at 84 km/h. At what time does it arrive? Show the arithmetic.",
19    "Summarize the main trade-offs between 4-bit and 8-bit weight quantization for LLM inference.",
20    "List five concrete steps to debug a CUDA kernel that returns wrong results, one sentence each.",
21]
22
23
24def post(port, prompt, max_tokens=160):
25    body = json.dumps({"messages": [{"role": "user", "content": prompt}], "max_tokens": max_tokens,
26                       "temperature": 0, "chat_template_kwargs": {"enable_thinking": False}}).encode()
27    req = urllib.request.Request(f"http://127.0.0.1:{port}/v1/chat/completions", data=body,
28                                 headers={"Content-Type": "application/json"})
29    with urllib.request.urlopen(req, timeout=1800) as r:
30        out = json.loads(r.read())
31    return out["choices"][0]["message"].get("content", ""), out.get("timings") or {}
32
33
34def run(cfg, port, tag):
35    cpath = Path(f"/tmp/quality-{tag}.json")
36    c = json.loads(Path(cfg).read_text())
37    c["port"] = port
38    c["log"] = f"/tmp/quality-{tag}.log"
39    cpath.write_text(json.dumps(c, indent=1))
40    proc = subprocess.Popen([sys.executable, str(ROOT / "serve" / "server.py"), "--engine", "strata", "--config",
41                             str(cpath), "--port", str(port)], cwd=str(ROOT),
42                            stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
43    try:
44        for _ in range(600):
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(2)
52        post(port, "Say hi.", 8)
53        out = []
54        for p in PROMPTS:
55            txt, t = post(port, p)
56            out.append(txt)
57            print(f"  {tag}: {t.get('prompt_per_second')} tok/s prompt, {t.get('predicted_per_second')} tok/s decode")
58        return out
59    finally:
60        proc.terminate()
61        try:
62            proc.wait(60)
63        except subprocess.TimeoutExpired:
64            proc.kill()
65        time.sleep(5)
66
67
68a = run(sys.argv[1], 8097, "a")
69b = run(sys.argv[2], 8097, "b")
70res = []
71same = 0
72for i, (x, y) in enumerate(zip(a, b)):
73    eq = x == y
74    same += eq
75    pre = 0
76    while pre < min(len(x), len(y)) and x[pre] == y[pre]:
77        pre += 1
78    res.append({"prompt": PROMPTS[i], "identical": eq, "common_prefix_chars": pre,
79                "len_a": len(x), "len_b": len(y), "a": x, "b": y})
80    print(f"prompt {i}: {'IDENTICAL' if eq else 'differs'} (prefix {pre} chars, {len(x)} vs {len(y)} chars)")
81Path(sys.argv[3]).write_text(json.dumps({"identical": same, "of": len(PROMPTS), "items": res}, indent=1))
82print(f"{same} of {len(PROMPTS)} answers identical")