#!/usr/bin/env python3 """Turn-level view of the baseline-pair runs (stdlib only): python3 -I baseline-turns.py RUNROOT One row per (run, turn): decode tok/s, tokens/round and ms/round (vanilla: engine line "N generated in X ms, drafts accepted A of M" -> rounds = N - A; ours: request outcome rounds), and the machine state inside that turn's decode window (decode seconds = output_tokens / decode_tok_s, ending at the timestamp of the turn's client line): mean scaling_cur_freq of the cores with utilisation > 40 % (fast samples every 2 s, so a window holds 1-2 samples), the count of such cores, GPU SM clock of the full sample when one falls in the window. Then, per arm: the correlation of tok/s, ms/round and tokens/round with the busy-core clock over all turns, and the spread of ms/round and tokens/round (variance split: how much of the tok/s variance is acceptance (tokens/round) and how much is round time (ms/round)). """ import glob, json, math, os, re, statistics as st, sys ROOT = sys.argv[1] def pearson(x, y): if len(x) < 5: return None mx, my = st.mean(x), st.mean(y) sx = math.sqrt(sum((a - mx) ** 2 for a in x)); sy = math.sqrt(sum((b - my) ** 2 for b in y)) return None if sx == 0 or sy == 0 else sum((a - mx) * (b - my) for a, b in zip(x, y)) / (sx * sy) def jl(p): out = [] if os.path.exists(p): for ln in open(p, errors="replace"): try: out.append(json.loads(ln)) except Exception: pass return out rows = [] for d in sorted(glob.glob(os.path.join(ROOT, "b*p*-*"))): v = os.path.join(d, "VALIDITY.txt") if not os.path.exists(v) or not open(v).read().startswith("VALID"): continue name = os.path.basename(d); arm = name.split("-", 1)[1] turns = json.load(open(os.path.join(d, "long-session.json"))) tr, mr = [], [] if arm == "ours": oc = [] for ln in open(os.path.join(d, "inference.log"), errors="replace"): m = re.search(r"request outcome=completed produced=(\d+) rounds=(\d+)", ln) if m: oc.append((int(m.group(1)), int(m.group(2)))) oc = oc[-5:] for (p, r), t in zip(oc, turns): tr.append(p / r); mr.append(1000.0 * (t.get("output_tokens", 300) / t["decode_tok_s"]) / r) else: ls = [l for l in open(os.path.join(d, "engine.log"), errors="replace") if "strata serve: prompt" in l and "generated in" in l][-5:] for ln in ls: m = re.search(r"(\d+) generated in (\d+) ms.*drafts accepted (\d+) of (\d+)", ln) if m: g, ms, a = int(m.group(1)), int(m.group(2)), int(m.group(3)); r = g - a tr.append(g / r); mr.append(ms / r) win, k = [], 0 for ln in open(os.path.join(d, "client.ts.log"), errors="replace"): try: ts, rest = ln.split(" ", 1) if rest.startswith("{") and '"decode_tok_s"' in rest and k < len(turns): T = float(ts); win.append((T - turns[k].get("output_tokens", 300) / turns[k]["decode_tok_s"], T)); k += 1 except Exception: pass S = jl(os.path.join(d, "state.jsonl")) for i, t in enumerate(turns): if i >= len(win) or i >= len(tr): continue a, b = win[i] mh, nb, gs = [], [], [] for s in S: if a <= s["t"] <= b and s.get("cpu_util"): f = [c for c, u in enumerate(s["cpu_util"]) if u is not None and u > 40 and s["cpu_khz"][c]] if f: mh.append(st.mean(s["cpu_khz"][c] for c in f) / 1000.0); nb.append(len(f)) if a <= s["t"] <= b and s.get("kind") == "full" and s.get("gpu", {}).get("sm_mhz"): gs.append(float(s["gpu"]["sm_mhz"])) rows.append(dict(run=name, arm=arm, turn=i + 1, tok_s=t["decode_tok_s"], tr=tr[i], mr=mr[i], mhz=st.mean(mh) if mh else None, nb=st.mean(nb) if nb else None, gsm=st.mean(gs) if gs else None)) print("run turn tok/s tok/rd ms/rd busyMHz nbusy gpuSM") for r in rows: print("%-12s %d %7.1f %6.2f %6.2f %7s %5s %6s" % (r["run"], r["turn"], r["tok_s"], r["tr"], r["mr"], "-" if r["mhz"] is None else "%.0f" % r["mhz"], "-" if r["nb"] is None else "%.1f" % r["nb"], "-" if r["gsm"] is None else "%.0f" % r["gsm"])) print() for arm in ("ours", "v1", "v2"): rs = [r for r in rows if r["arm"] == arm] if not rs: continue ts, ms_, trs = [r["tok_s"] for r in rs], [r["mr"] for r in rs], [r["tr"] for r in rs] print("%s: %d turns | tok/s mean %.1f sd %.1f | ms/round mean %.2f sd %.2f | tok/round mean %.2f sd %.2f | CV tok/s %.1f%%, ms/round %.1f%%, tok/round %.1f%%" % ( arm, len(rs), st.mean(ts), st.stdev(ts), st.mean(ms_), st.stdev(ms_), st.mean(trs), st.stdev(trs), 100 * st.stdev(ts) / st.mean(ts), 100 * st.stdev(ms_) / st.mean(ms_), 100 * st.stdev(trs) / st.mean(trs))) # tok/s = tok/round / (ms/round / 1000): log-variance split lt, lm, lr = [math.log(x) for x in ts], [math.log(x) for x in ms_], [math.log(x) for x in trs] cov_r = st.mean([(a - st.mean(lr)) * (b - st.mean(lt)) for a, b in zip(lr, lt)]); cov_m = st.mean([(-a + st.mean(lm)) * (b - st.mean(lt)) for a, b in zip(lm, lt)]) vt = st.pvariance(lt) if vt > 0: print(" share of log tok/s variance: acceptance (tok/round) %.0f %%, round time (ms/round) %.0f %%" % (100 * cov_r / vt, 100 * cov_m / vt)) pts = [(r["mhz"], r["tok_s"], r["mr"], r["tr"]) for r in rs if r["mhz"]] if len(pts) >= 5: print(" busy-core MHz in the decode window (n=%d, range %.0f..%.0f): r(tok/s)=%s r(ms/round)=%s r(tok/round)=%s" % ( len(pts), min(p[0] for p in pts), max(p[0] for p in pts), *["%.2f" % pearson([p[0] for p in pts], [p[i] for p in pts]) if pearson([p[0] for p in pts], [p[i] for p in pts]) is not None else "-" for i in (1, 2, 3)])) byturn = {} for r in rs: byturn.setdefault(r["turn"], []).append(r["tok_s"]) print(" tok/s by turn (mean, sd): " + " ".join("T%d %.1f/%.1f" % (k, st.mean(v), st.stdev(v) if len(v) > 1 else 0) for k, v in sorted(byturn.items())))