okami.mom/strata

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

Niko1221Merge f-142b-serve into f-1426e1085c1d

main
5.0 KiB132 linesraw
1"""Two arms on the same box, same prompts and image: the five STRATA_* opt-ins off, then on (the live runner).
2
3Arm "optins-off": the live runner container is stopped (not removed) and a clone is started from its own
4`docker inspect` config with only those five variables dropped. Arm "optins-on": the live container itself,
5restarted. Each arm: fresh engine process, cold first request, two warm-ups, 3x 4,096 / 32,768 / 128,000
6(bench.py), then the needle checks. VRAM per card is sampled every second. Whatever happens, the clone is
7removed and the live container is left running.
8"""
9
10import json
11import signal
12import subprocess
13import threading
14import time
15import urllib.request
16from pathlib import Path
17
18C = "local-llm-runner-cluster-7900s-strata-bca809cd"
19CLONE = C + "-optins-off"
20D = Path.home() / "scratch/strata_bench2"
21OPT_INS = ("STRATA_SPLIT_OWN", "STRATA_STAGE_TRIM", "STRATA_PF_FUSED", "STRATA_PF_GEMM", "STRATA_PF_SWITCH_MIN_T")
22
23
24def sh(*args, check=True):
25    return subprocess.run(args, capture_output=True, text=True, check=check)
26
27
28def say(*a):
29    print(time.strftime("%H:%M:%S"), *a, flush=True)
30
31
32def status():
33    try:
34        with urllib.request.urlopen("http://127.0.0.1:8086/v1/status", timeout=5) as r:
35            return json.load(r)
36    except OSError:
37        return None
38
39
40def wait_loaded(t0, label):
41    deadline = time.time() + 900
42    while True:
43        s = status()
44        if s and s.get("loaded") and s.get("uptime_s", 1e9) <= time.time() - t0 + 5:
45            say(label, "loaded after", round(time.time() - t0), "s")
46            return s
47        if time.time() > deadline:
48            raise SystemExit(f"{label}: not loaded after 15 min")
49        time.sleep(2)
50
51
52stop = threading.Event()
53
54
55def sampler():
56    with (D / "vram.jsonl").open("a") as f:
57        while not stop.is_set():
58            out = sh("rocm-smi", "--showmeminfo", "vram", "--json", check=False).stdout
59            try:
60                used = {k: int(v["VRAM Total Used Memory (B)"]) for k, v in json.loads(out).items()}
61            except (ValueError, KeyError):
62                used = None
63            f.write(json.dumps({"t": round(time.time(), 2), "used_b": used}) + "\n")
64            f.flush()
65            stop.wait(1)
66
67
68def bench(arm, container, t0):
69    s = wait_loaded(t0, arm)
70    (D / f"status_after_load-{arm}.json").write_text(json.dumps(s, indent=1))
71    env = json.loads(sh("docker", "inspect", container).stdout)[0]["Config"]["Env"]
72    (D / f"container_env-{arm}.txt").write_text("".join(e + "\n" for e in env if e.startswith(("STRATA_", "HIP_"))))
73    time.sleep(5)
74    subprocess.run(["python3", "bench.py", "prompts.jsonl", f"matrix-{arm}.jsonl"], cwd=D, check=True)
75    say(arm, "matrix done")
76    with (D / f"needles-{arm}.log").open("w") as log:
77        subprocess.run(["python3", str(D / "src/tools/needle_bench.py"), "--url", "http://127.0.0.1:8086",
78                        "--lengths", "32k,128k", "--depths", "10,50,90", "--out", str(D / f"needles-{arm}.json")],
79                       cwd=D, stdout=log, stderr=subprocess.STDOUT, check=True)
80    say(arm, "needles done")
81    since = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime(t0 - 1))
82    logs = sh("docker", "logs", "--since", since, container, check=False)
83    (D / f"engine-{arm}.log").write_text(logs.stdout + logs.stderr)
84
85
86def handler(signum, frame):
87    raise SystemExit(f"signal {signum}")
88
89
90signal.signal(signal.SIGTERM, handler)
91signal.signal(signal.SIGINT, handler)
92
93orig = json.loads(sh("docker", "inspect", C).stdout)[0]
94cfg, host = orig["Config"], orig["HostConfig"]
95assert not any(k in " ".join(cfg["Cmd"]) for k in OPT_INS), "opt-ins in the command line"
96assert all(any(e.startswith(k + "=") for e in cfg["Env"]) for k in OPT_INS), "live env lacks the opt-ins"
97run = ["docker", "run", "-d", "--name", CLONE, "--network", host["NetworkMode"], "--ipc", host["IpcMode"],
98       "--shm-size", str(host["ShmSize"])]
99for g in host.get("GroupAdd") or []:
100    run += ["--group-add", g]
101for d in host.get("Devices") or []:
102    run += ["--device", f"{d['PathOnHost']}:{d['PathInContainer']}"]
103for b in host.get("Binds") or []:
104    run += ["-v", b]
105for e in cfg["Env"]:
106    if e.split("=", 1)[0] not in OPT_INS:
107        run += ["-e", e]
108run += [cfg["Image"], *cfg["Cmd"]]
109
110(D / "vram.jsonl").unlink(missing_ok=True)
111threading.Thread(target=sampler, daemon=True).start()
112try:
113    t0 = time.time()
114    say("optins-off: stop live container, start clone without", ",".join(OPT_INS))
115    sh("docker", "stop", C)
116    sh(*run)
117    bench("optins-off", CLONE, t0)
118    sh("docker", "rm", "-f", CLONE)
119    say("optins-off: clone removed")
120
121    t0 = time.time()
122    say("optins-on: start live container (fresh process)")
123    sh("docker", "start", C)
124    bench("optins-on", C, t0)
125finally:
126    stop.set()
127    sh("docker", "rm", "-f", CLONE, check=False)
128    if json.loads(sh("docker", "inspect", C).stdout)[0]["State"]["Running"] is not True:
129        sh("docker", "start", C, check=False)
130        say("live container started again")
131    say("live container running:", json.loads(sh("docker", "inspect", C).stdout)[0]["State"]["Running"])
132    print("ALL_DONE", flush=True)