diff --git a/docs/TOOLS.md b/docs/TOOLS.md index ad98b804..86f71552 100644 --- a/docs/TOOLS.md +++ b/docs/TOOLS.md @@ -525,7 +525,7 @@ need nothing but `curl` and `jq`. Scripts resolve each other by `$(dirname "$0") it is on. `oracle.sh` starts its own server and stops whatever listens on its port (`P`, default 8302). Everything else — `cstep2.sh`, `ident.sh`, `tierident.sh`, `tiercross.sh`, `mctools.sh`, `mcstream.sh`, `hfget.sh`, `convfn.sh`, `convdspark.sh`, `fnpf.sh`, `fnconc.sh`, `fndrop.sh`, -`apikeys.sh`, `diskstall.py` — leaves a live serve alone. +`apikeys.sh`, `diskstall.py`, `cardclock.py` — leaves a live serve alone. `fndrop.sh` is the one to read the description of first: it leaves the PROCESS alone and deliberately disconnects clients from it, which is not the same promise. @@ -562,6 +562,7 @@ deployment. `P` selects the port. | `cstep2.sh` | steady-state decode ms/step at concurrency | `P` `LEVELS` `MT` `WIN` | | `ident.sh` | run-to-run reproducibility at a fixed configuration: one hash per (temperature, question) | `P` | | `diskstall.py` | what restoring a conversation from the disk tier costs: the returning request's time to first token, and the longest stall it puts on a stream decoding beside it | `--port` `--session` `--fill` | +| `cardclock.py` | what holds each card's clock under a sustained load (power limit, temperature or current limit, or the top of the voltage/frequency curve) and which card the tensor-parallel step waits for, with the engine's step counters per window beside it. Drives its own greedy load; `--watch` only samples while real traffic drives the server. Never writes to a card | `--port` `--conc` `--gen` `--seconds` `--window` `--seed` `--watch` `--json` `--compare` | `fndrop.sh` exists because a disconnect reaps a request at an arbitrary point in the step loop, and every other harness here waits for its requests, so none of them can see what holds a reference @@ -586,6 +587,20 @@ C steps tok tok/step ms/step accept% `cstep2.sh` waits until exactly C sequences are running before sampling and re-checks at the end: a window whose edges carry fewer than C describes no concurrency at all. +`cardclock.py` exists because the same image reads several percent apart from one boot to the next +on workstation cards. A tensor-parallel step runs at its slowest card's clock, and under a sustained +load the firmware picks each card's clock. A throttler bit counts only when the card's clock is +lower while it is set: some firmware keeps a bit on at idle (on Radeon AI PRO R9700, +`TEMP_HOTSPOT`), so a bit the idle baseline also shows is listed and ignored. When it names a +setting (power cap, fan target, voltage offset) it prints the command and changes nothing. That +write is for a moment when no process holds `/dev/kfd`. Under load, an SMU write on gfx1201 has +reset the machine. + +```sh +scripts/cardclock.py --port 8100 --conc 8 --seconds 180 --seed 7 --json before.json +scripts/cardclock.py --compare before.json after.json +``` + ### Measuring with a server of its own | | | diff --git a/scripts/README.md b/scripts/README.md index 975fd399..0fd23496 100644 --- a/scripts/README.md +++ b/scripts/README.md @@ -35,6 +35,7 @@ header lists its own). The ones that only attach to a live server need nothing b | `mctrace.sh` | A MiniCPM5-2B decode trace under `rocprofv3`, formatted for `libr4d/isa/tsum.sh`. The `blocks` column is what the trace is for. | | `ident.sh` | Run-to-run reproducibility at a fixed configuration: one hash per (temperature, question), to compare against another run of the same configuration. A speculative run is not expected to match a non-speculative one byte for byte — the split-K count is chosen from M, so the two take different reduction orders and can flip a near-tie late in a long answer — but a divergence within the first sentence or two is a defect. | | `diskstall.py` | What restoring a conversation from the disk tier costs: the returning request's time to first token, and the longest stall it puts on a stream decoding beside it. | +| `cardclock.py` | What holds each card's clock under a sustained load, and which card a tensor-parallel step waits for. The step ends when its slowest rank finishes, and under a sustained load the firmware picks that rank's clock: the power limit, a temperature or current limit, or the top of its voltage/frequency curve. Reads every AMD card's `gpu_metrics` table ten times a second and counts a throttler bit only when the clock is lower while it is set (some firmware keeps a bit on at idle). The engine's own step counters sit beside it per window, so a slow window is told apart first: slower steps, fewer tokens per step (the text), or host time between steps. Drives its own greedy load, or with `--watch` only samples a server that real traffic is driving. `--compare` puts two saved runs side by side. Read-only on the cards. | | `oracle.sh` | Every op compared against libref on the operands the model actually issued, which is the gap rad-kbench cannot close: its operands are its own, dense and rank-2, and an engine's are slices of arenas and weights off a file. Reads for two things — a MISMATCH, and the NOT CHECKED lines that name the rows whose weight layout has no published inverse and therefore cannot be compared at all. | ## Regression checks against a live server diff --git a/scripts/cardclock.py b/scripts/cardclock.py new file mode 100755 index 00000000..d6f9b177 --- /dev/null +++ b/scripts/cardclock.py @@ -0,0 +1,370 @@ +#!/usr/bin/env python3 +# cardclock.py -- what holds each card's clock under a sustained load, and which card sets the step. +# +# A tensor-parallel step ends when its slowest rank finishes: every rank waits for the others in the +# all-reduce. So the clock that decides decode speed is the LOWEST of the cards', and on a +# workstation card under a sustained load the firmware -- not the engine -- picks that clock: the +# power limit (PPT), a temperature limit, a current limit. The same image then reads several percent +# apart from one boot to the next, depending on which card heats first and how hard its power limit +# bites. This puts a number on that: +# +# * drives a sustained greedy load at --conc against a live server (or, with --watch, only samples +# while something else drives it -- real traffic on a deployment), and reports tok/s per --window +# seconds; +# * beside it, the engine's own step counters per window (/metrics): step ms, tokens per decode step, +# draft acceptance, busy share and host gap per step -- so a slow window is attributed to slower +# steps, to the text (fewer tokens a step), or to the host, before anyone blames a clock. The +# engine's tok/s counts every request the server ran in the window, the load's only this script's: +# a gap between the two is someone else's traffic; +# * reads every AMD card's SMU metrics table (/sys/class/drm/card*/device/gpu_metrics) ten times a +# second: clocks, temperatures (edge, hotspot, memory, the GFX/SOC/MEM voltage regulators), power, +# fan, and the throttler bits the firmware itself sets -- READ-ONLY, no root needed; +# * names the card the step waits for and what holds it, and the setting that addresses that; +# * --json saves a run; --compare puts two of them side by side (before / after a setting change). +# +# scripts/cardclock.py [--port 8100] [--conc 8] [--seconds 180] [--gen 1024] [--json run.json] +# scripts/cardclock.py --watch [--seconds 300] [--json run.json] +# scripts/cardclock.py --compare before.json after.json +# +# Reading the throttler bits: a bit counts only when it is set under load AND the card's clock is +# lower while it is set. Some firmware keeps a bit set all the time -- on Radeon AI PRO R9700 +# (gfx1201) TEMP_HOTSPOT reads set at idle and at 45 C with no effect on the clock -- so a bit that +# the idle baseline also shows is listed and ignored. +# +# Changing a setting this points at (power1_cap, fan target, voltage offset) is a write to the SMU: +# do it ONLY while no process holds /dev/kfd -- stop the server first. On a gfx1201 box such a write +# under load has reset the machine. Check with `sudo find /proc/[0-9]*/fd -lname /dev/kfd`, not +# `lsof /dev/kfd`: lsof on the host does not see a containerised server's handle (the container's +# /dev/kfd is its own node). None of these settings survive a reboot. Undervolts in +# particular must be proven under a SUSTAINED hot load, not a short test: an offset that passes a +# minute-long selftest can wedge a card twenty minutes into real traffic once it runs at 90 C. +# +# Needs Linux amdgpu with a gpu_metrics v1.3 table (RDNA3 / RDNA4 discrete cards); any other layout +# is refused rather than misread. +import argparse, glob, json, os, random, re, statistics as st, struct, sys, threading, time, urllib.request + +BITS = {0: "PPT0", 1: "PPT1", 2: "PPT2", 3: "PPT3", 4: "SPL", 5: "FPPT", 6: "SPPT", 7: "SPPT_APU", + 16: "TDC_GFX", 17: "TDC_SOC", 18: "TDC_MEM", 19: "TDC_VDD", 20: "TDC_CVIP", 21: "EDC_CPU", + 22: "EDC_GFX", 23: "APCC", 32: "TEMP_GPU", 33: "TEMP_CORE", 34: "TEMP_MEM", 35: "TEMP_EDGE", + 36: "TEMP_HOTSPOT", 37: "TEMP_SOC", 38: "TEMP_VR_GFX", 39: "TEMP_VR_SOC", 40: "TEMP_VR_MEM0", + 41: "TEMP_VR_MEM1", 42: "TEMP_LIQUID0", 43: "TEMP_LIQUID1", 44: "VRHOT0", 45: "VRHOT1", + 46: "PROCHOT_CPU", 47: "PROCHOT_GFX", 56: "PPM", 57: "FIT"} # amdgpu_smu.h SMU_THROTTLER_*_BIT +V13 = " 5) or (rpm and abs(r["fan"] - rpm) > 400) or not 300 < r["mv"] < 1600 + if bad: raise SystemExit(f"{self.bdf}: decoded metrics disagree with hwmon (hotspot {r['hot']} vs {jn}, fan {r['fan']} vs {rpm}, " + f"{r['mv']} mV) -- refusing to report from a misread table") + + def settings(self): + od = rd(self.dev + "/pp_od_clk_voltage") or "" + m = re.search(r"OD_VDDGFX_OFFSET:\s*\n\s*(-?\d+)mV", od) + ft = rd(self.dev + "/gpu_od/fan_ctrl/fan_target_temperature") or "" + ft = re.search(r"FAN_TARGET_TEMPERATURE:\s*\n\s*(\d+)", ft) + w = lambda f: rd(f"{self.hwmon}/{f}", int, 0) // 1_000_000 if self.hwmon else None + return {"cap": w("power1_cap"), "cap_max": w("power1_cap_max"), "cap_default": w("power1_cap_default"), + "vddgfx_offset_mv": int(m.group(1)) if m else None, "fan_target": int(ft.group(1)) if ft else None, + "dev": self.dev, "hwmon": self.hwmon} + + +def find_cards(): + out = {} + for dev in sorted(glob.glob("/sys/class/drm/card*/device")): + if not re.fullmatch(r"card\d+", dev.split("/")[-2]): continue + if rd(dev + "/vendor") != "0x1002" or not os.path.exists(dev + "/gpu_metrics"): continue + c = Card(dev); out.setdefault(c.bdf, c) + if not out: raise SystemExit("no AMD card with a gpu_metrics table under /sys/class/drm") + return list(out.values()) + + +class Sampler(threading.Thread): + def __init__(self, cards, interval): + super().__init__(daemon=True); self.cards, self.iv, self.rows, self.stop = cards, interval, [], threading.Event() + def run(self): + while not self.stop.is_set(): + t = time.time() + for c in self.cards: + try: self.rows.append({"t": t, "card": c.bdf, **c.read()}) + except OSError: pass + self.stop.wait(max(0.0, self.iv - (time.time() - t))) + + +TOPICS = ["how a B-tree splits and merges nodes", "the history of the printing press", "how TCP congestion control works", + "the water cycle and its effect on climate", "how a compiler register allocator works", "the economics of container shipping", + "how vaccines train the immune system", "the design of a key-value store", "how bridges carry load", "the life of a star"] + + +def post(base, body, timeout=900): + return json.load(urllib.request.urlopen(urllib.request.Request(base + "/v1/chat/completions", data=json.dumps(body).encode(), + headers={"content-type": "application/json"}), timeout=timeout)) + + +ENGINE = ("decode_tokens_total", "engine_steps_total", "engine_steps_decode_total", "engine_steps_mixed_total", + "engine_step_seconds_total", "engine_step_seconds_decode_total", "draft_tokens_total", "draft_accepted_total") + + +def engine_counters(base): + """The engine's own step counters (Radiance /metrics, summed over labels); None if the server has none.""" + try: m = urllib.request.urlopen(base + "/metrics", timeout=5).read().decode() + except Exception: return None + out = {} + for k in ENGINE: + v = re.findall(rf"^radiance:{k}(?:\{{[^}}]*\}})? ([0-9.e+-]+)$", m, re.M) + if v: out[k] = sum(float(x) for x in v) + return out or None + + +def engine_windows(snaps, t0, t1, w): + """Per window: what the engine did -- step ms, tokens per decode step (speculation), draft acceptance, the share of + wall time inside steps (busy) and the host gap per step. Tells a slow window's cause apart: slower steps = the GPUs + / interconnect; fewer tokens per step = the text (speculation accepts less); a bigger gap = the host.""" + out = []; k = 0 + def at(t): # last snapshot at or before t + c = [s for s in snaps if s[0] <= t]; return c[-1] if c else None + while t0 + (k + 1) * w <= t1 + 0.5: + a, b = at(t0 + k * w), at(t0 + (k + 1) * w); k += 1 + if not a or not b or b[0] <= a[0]: out.append(None); continue + d = {x: b[1].get(x, 0) - a[1].get(x, 0) for x in ENGINE}; wall = b[0] - a[0] + sd, so = max(d["engine_steps_decode_total"], 1), max(d["engine_steps_total"], 1) + out.append({"tok_s": round(d["decode_tokens_total"] / wall, 1), + "dec_ms": round(d["engine_step_seconds_decode_total"] / sd * 1e3, 2), + "tok_step": round(d["decode_tokens_total"] / sd, 2), + "accept": round(d["draft_accepted_total"] / d["draft_tokens_total"], 3) if d["draft_tokens_total"] else None, + "busy": round(d["engine_step_seconds_total"] / wall, 3), + "gap_ms": round((wall - d["engine_step_seconds_total"]) / so * 1e3, 2), + "steps_s": round(so / wall, 1), "mixed": int(d["engine_steps_mixed_total"])}) + return out + + +def load(a, base, model): + done, stop, stall = [], threading.Event(), {} + def worker(i): + # --seed: every run sends the same prompts in the same order per stream (the text drives speculation, so a + # run-to-run A/B wants it fixed); without it, a fresh mix each run + rnd = random.Random(i * 7919 + (a.seed if a.seed is not None else int(time.time()))) + while not stop.is_set(): + t0 = time.time(); tag, topic = rnd.getrandbits(48), rnd.randrange(len(TOPICS)) + body = {"model": model, "max_tokens": a.gen, "temperature": 0, "messages": [{"role": "user", "content": + f"[{tag:x}] Write a long, detailed, multi-section explanation of {TOPICS[topic]}."}]} + try: n = post(base, body)["usage"]["completion_tokens"] + except Exception as e: done.append((t0, time.time(), 0, str(e)[:120], i, topic)); time.sleep(1); continue + done.append((t0, time.time(), n, None, i, topic)) + ws = [threading.Thread(target=lambda: post(base, {"model": model, "max_tokens": 64, "temperature": 0, # warm-up: the first + "messages": [{"role": "user", "content": "Say hello."}]}), daemon=True) for _ in range(a.conc)] # requests after a boot read slow + [w.start() for w in ws]; [w.join() for w in ws] + snaps = [] + def tokens(): + c = engine_counters(base) + if c: snaps.append((time.time(), c)) + return c.get("decode_tokens_total") if c else None + last = tokens() # the baseline the first window is differenced against + t_start = time.time(); end = t_start + a.seconds + th = [threading.Thread(target=worker, args=(i,), daemon=True) for i in range(a.conc)]; [t.start() for t in th] + last_t = time.time() + while time.time() < end: # stall guard: a wedged rank stops every token + time.sleep(2); now = tokens() + if now is None or now != last: last, last_t = now, time.time() + elif time.time() - last_t > a.stall: + stall["at"] = time.time(); print(f"STALL: the server produced no token for {a.stall:.0f} s -- a rank may be wedged; " + "check the server log ('a synchronisation has waited') before anything else", flush=True) + break + stop.set(); [t.join(timeout=5 if stall else a.gen) for t in th] + return t_start, min(end, stall.get("at", end)), done, bool(stall), snaps + + +def windows(t0, t1, done, w): + out = []; k = 0 + while t0 + (k + 1) * w <= t1 + 0.5: + a, b = t0 + k * w, t0 + (k + 1) * w; tok = 0.0 + for s, e, n, *_ in done: + if n and e > a and s < b: tok += n * (min(b, e) - max(a, s)) / max(e - s, 1e-6) + out.append(round(tok / w, 1)); k += 1 + return out + + +def pct(xs, q): xs = sorted(xs); return xs[int(q * (len(xs) - 1))] if xs else None + + +def analyse(cards, rows, t0, t1, idle_rows, settings): + rep = {} + for c in cards: + rs = [r for r in rows if r["card"] == c.bdf and t0 <= r["t"] <= t1 and r["act"] >= 50] + if len(rs) < 20: continue + idle = [r for r in idle_rows if r["card"] == c.bdf] + flags = {} + for bit, name in BITS.items(): + on = [r for r in rs if r["indep"] >> bit & 1] + if not on: continue + off = [r for r in rs if not r["indep"] >> bit & 1] + idle_on = sum(1 for r in idle if r["indep"] >> bit & 1) / len(idle) if idle else 0.0 + c_on, c_off = st.median(r["clk"] for r in on), (st.median(r["clk"] for r in off) if off else None) + share = len(on) / len(rs) + if idle_on >= 0.2: status = "idle" # the firmware sets it with no load: says nothing about the load + elif share > 0.95: # never clear under load: no clear samples to compare against -- + cap = settings[c.bdf]["cap"] or 0 # but a power limit set throughout at the cap is the cap + at_cap = name.startswith(("PPT", "SPL", "FPPT", "SPPT")) and cap and st.median(r["W"] for r in rs) >= 0.95 * cap + status = "binding" if at_cap else "always" + elif share >= 0.10 and c_on < c_off * 0.99: status = "binding" + else: status = "no-effect" + flags[name] = {"share": round(share, 3), "clk_on": round(c_on), "clk_off": round(c_off) if c_off else None, + "idle_share": round(idle_on, 2), "status": status, "binding": status == "binding"} + rep[c.bdf] = {"samples": len(rs), "clk_p10": pct([r["clk"] for r in rs], .1), "clk_p50": pct([r["clk"] for r in rs], .5), + "clk_p90": pct([r["clk"] for r in rs], .9), "W_p50": pct([r["W"] for r in rs], .5), "W_max": max(r["W"] for r in rs), + "hot_max": max(r["hot"] for r in rs), "mem_max": max(r["mem"] for r in rs), "vrgfx_max": max(r["vrgfx"] for r in rs), + "fan_max": max(r["fan"] for r in rs), "mv_p50": pct([r["mv"] for r in rs], .5), "flags": flags} + return rep + + +def advise(bdf, r, s): + lines = [] + for name, f in sorted(r["flags"].items(), key=lambda x: -x[1]["share"]): + if not f["binding"]: continue + for prefixes, what, fix, cmd in ADVICE: + if name.startswith(prefixes): + vs = f"{f['clk_on']} MHz while set vs {f['clk_off']} MHz while clear" if f["clk_off"] and f["clk_on"] < f["clk_off"] else \ + f"set nearly throughout, at {r['W_p50']} W of a {s.get('cap')} W cap" + lines.append(f" {name} ({what}) holds it {f['share']:.0%} of the time: {vs} -> " + f"{fix.format(**{k: s.get(k) for k in ('cap', 'cap_max', 'fan_target')})}") + if cmd: lines.append(f" {cmd.format(cap_max=s.get('cap_max'), hwmon=s.get('hwmon'), dev=s.get('dev'))}") + break + return lines + + +def report(res): + rep, sets, tps = res["cards"], res["settings"], res["tok_s_windows"] + if tps: print(f"load: c{res['conc']} gen {res['gen']} for {res['seconds']:.0f} s -> tok/s per {res['window']} s: " + f"{' '.join(f'{x:.0f}' for x in tps)} (median {st.median(tps[1:] or tps):.0f}, first window -- the prefills -- left out)") + eng = [e for e in res.get("engine_windows") or [] if e] + if eng: + f = lambda k: " ".join(f"{e[k]}" if e[k] is not None else "-" for e in res["engine_windows"] if e) + print(f"engine per window: tok/s {f('tok_s')}\n step ms {f('dec_ms')}\n tok/step {f('tok_step')}\n" + f" accept {f('accept')}\n busy {f('busy')} host gap ms {f('gap_ms')}") + if not rep: + print("no card was busy (>= 50 %) for 20 samples in the window -- nothing to attribute"); return + print(f"{'card':14s} {'clock p10/p50/p90':>18s} {'W p50/max cap':>15s} {'hot':>4s} {'mem':>4s} {'VR':>4s} {'fan':>5s} {'mV':>5s} offset limits that bind (share of loaded samples)") + for bdf, r in sorted(rep.items()): + s = sets[bdf]; bind = ", ".join(f"{n} {f['share']:.0%}" for n, f in r["flags"].items() if f["binding"]) or "none" + print(f"{bdf:14s} {r['clk_p10']:>6}/{r['clk_p50']}/{r['clk_p90']} {r['W_p50']:>5}/{r['W_max']} {s['cap']:>3} " + f"{r['hot_max']:>4} {r['mem_max']:>4} {r['vrgfx_max']:>4} {r['fan_max']:>5} {r['mv_p50']:>5} {str(s['vddgfx_offset_mv'] if s['vddgfx_offset_mv'] is not None else '-'):>6} {bind}") + ign = [n for n, f in r["flags"].items() if f["status"] == "idle"] + alw = [n for n, f in r["flags"].items() if f["status"] == "always"] + if ign: print(f"{'':14s} ignored, also set at idle: {', '.join(ign)}") + if alw: print(f"{'':14s} set through the whole load, never clear -- cannot tell whether it limits: {', '.join(alw)}") + if len(rep) >= 2: + order = sorted(rep.items(), key=lambda x: x[1]["clk_p50"]); (b0, r0), (_, r1) = order[0], order[1] + gap = 1 - r0["clk_p50"] / r1["clk_p50"] + if gap >= 0.015: + print(f"\nthe step waits for {b0}: its median clock {r0['clk_p50']} MHz is {gap:.1%} under the next card's") + ad = advise(b0, r0, sets[b0]) + for l in ad: print(l) + if not ad and r0["clk_p90"] <= r0["clk_p50"] * 1.01: + peers = ", ".join(f"{r['mv_p50']} mV" for b, r in order[1:]) + print(f" no limit binds and its clock sits flat at ~{r0['clk_p90']} MHz on {r0['mv_p50']} mV (others: {peers}): it is at the\n" + f" top of its voltage/frequency curve. A higher power cap or more cooling will not move it; only a voltage\n" + f" offset would -- and an offset must be proven under a sustained hot load, not a short test.") + else: print(f"\nno single straggler: the busiest cards' median clocks are within {gap:.1%} of each other") + for b, r in order[1:]: + ad = advise(b, r, sets[b]) + if ad: print(f"also on {b}:"); [print(l) for l in ad] + print("\nwrite a setting only with the server stopped -- nothing may hold /dev/kfd " + "(sudo find /proc/[0-9]*/fd -lname /dev/kfd); none survive a reboot.") + + +def compare(pa, pb): + A, B = json.load(open(pa)), json.load(open(pb)) + ta, tb = A["tok_s_windows"][1:], B["tok_s_windows"][1:] # first window = the prefills + if ta and tb: + ma, mb = st.median(ta), st.median(tb); iq = lambda x: pct(x, .75) - pct(x, .25) + spread = max(iq(ta), iq(tb)); d = mb - ma + print(f"tok/s per window A median {ma:.0f} (IQR {iq(ta):.0f}) B median {mb:.0f} (IQR {iq(tb):.0f}) B-A {d:+.0f} ({d / ma:+.1%}) -> " + + ("beyond either run's window spread" if abs(d) > spread else "within the window spread")) + print(f"{'card':14s} {'clock p10 A->B':>16s} {'clock p50 A->B':>16s} {'W p50 A->B':>12s} {'cap A->B':>10s} {'hot max A->B':>14s} binding limits A | B") + for bdf in sorted(set(A["cards"]) | set(B["cards"])): + a, b = A["cards"].get(bdf), B["cards"].get(bdf) + if not (a and b): print(f"{bdf:14s} only in {'A' if a else 'B'}"); continue + bind = lambda r: ",".join(f"{n} {f['share']:.0%}" for n, f in r["flags"].items() if f["binding"]) or "none" + print(f"{bdf:14s} {a['clk_p10']:>7}->{b['clk_p10']:<7} {a['clk_p50']:>7}->{b['clk_p50']:<7} {a['W_p50']:>5}->{b['W_p50']:<5} " + f"{A['settings'][bdf]['cap']:>4}->{B['settings'][bdf]['cap']:<4} {a['hot_max']:>6}->{b['hot_max']:<6} {bind(a)} | {bind(b)}") + print("one run each is one boot: on these cards boots differ by several percent -- repeat each side >= 3 times, interleaved.") + + +ap = argparse.ArgumentParser(description="what holds each card's clock under load (read-only)") +ap.add_argument("--port", type=int, default=8100); ap.add_argument("--host", default="127.0.0.1"); ap.add_argument("--model") +ap.add_argument("--conc", type=int, default=8); ap.add_argument("--gen", type=int, default=1024) +ap.add_argument("--seconds", type=float, default=180); ap.add_argument("--window", type=int, default=20) +ap.add_argument("--interval", type=float, default=0.1); ap.add_argument("--stall", type=float, default=20) +ap.add_argument("--cool", type=int, help="wait (up to 5 min) until every card's hotspot is at or under this before loading") +ap.add_argument("--watch", action="store_true", help="sample only; drive the load yourself") +ap.add_argument("--json"); ap.add_argument("--raw", help="also write every sample (JSON lines)") +ap.add_argument("--compare", nargs=2, metavar=("A.json", "B.json")) +ap.add_argument("--seed", type=int, help="fixed prompt seed: the same text every run (speculation makes tok/s text-dependent)") +a = ap.parse_args() +if a.compare: compare(*a.compare); sys.exit(0) +cards = find_cards(); [c.check() for c in cards] +base = f"http://{a.host}:{a.port}" +if a.cool: + t = time.time() + while time.time() - t < 300 and max(c.read()["hot"] for c in cards) > a.cool: time.sleep(2) +smp = Sampler(cards, a.interval); smp.start() +time.sleep(5); idle = [r for r in smp.rows if r["act"] < 20] # idle baseline: which bits the firmware sets with no load +settings = {c.bdf: c.settings() for c in cards} +if a.watch: # no load of our own; the server's step counters if one answers on --port + snaps = [] + def snap(): + c = engine_counters(base) + if c: snaps.append((time.time(), c)) + snap(); t0 = time.time() + while time.time() - t0 < a.seconds: time.sleep(min(2, max(0.0, a.seconds - (time.time() - t0)))); snap() + t1 = time.time(); tps, stalled, conc, done = [], False, None, [] + eng = engine_windows(snaps, t0, t1, a.window) if snaps else [] +else: + model = a.model or json.load(urllib.request.urlopen(base + "/v1/models", timeout=10))["data"][0]["id"] + t0, t1, done, stalled, snaps = load(a, base, model); tps = windows(t0, t1, done, a.window); conc = a.conc + eng = engine_windows(snaps, t0, t1, a.window) + errs = [d[3] for d in done if d[3]] + if errs: print(f"{len(errs)} request(s) failed, first: {errs[0]}") +smp.stop.set(); smp.join() +res = {"when": time.strftime("%F %T"), "conc": conc, "gen": a.gen, "seconds": t1 - t0, "window": a.window, "stalled": stalled, + "seed": a.seed, "t0": t0, "tok_s_windows": tps, "engine_windows": eng, + "requests": [{"t0": round(d[0], 2), "t1": round(d[1], 2), "tokens": d[2], "err": d[3], "stream": d[4], "topic": d[5]} for d in done], + "settings": settings, "cards": analyse(cards, smp.rows, t0, t1, idle, settings)} +report(res) +if a.json: json.dump(res, open(a.json, "w"), indent=1) +if a.raw: + with open(a.raw, "w") as f: [f.write(json.dumps(r) + "\n") for r in smp.rows] +sys.exit(3 if stalled else 0)