#!/usr/bin/env python3 """Memory-compaction activity inside the decode session of each run (stdlib only): python3 -I baseline-compact.py RUNROOT For every VALID run: the CPU ticks (1 tick = 10 ms) of kcompactd0 / kswapd0 in the full samples (every 15 s, top-5 CPU consumers per interval) that fall between the first and the last client line (the session: prefill + decode of the five turns), the same before the session (engine load), and the /proc/vmstat deltas across the session (compact_stall, compact_fail, compact_migrate_scanned, thp_fault_fallback, allocstall_normal, pgscan_kswapd, pgscan_direct). Then, per arm, the correlation of ms/round and tok/s with each. """ import glob, json, math, os, re, statistics as st, sys ROOT = sys.argv[1] def pearson(x, y): if len(x) < 4: 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) 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"))) ts = [float(l.split(" ", 1)[0]) for l in open(os.path.join(d, "client.ts.log"), errors="replace") if l.split(" ", 1)[0].replace(".", "").isdigit()] t0, t1 = min(ts), max(ts) S = [json.loads(l) for l in open(os.path.join(d, "state.jsonl")) if '"kind":"full"' in l] inside = [s for s in S if t0 <= s["t"] <= t1 + 15] before = [s for s in S if s["t"] < t0] def ticks(ss, comm): return sum(tk for s in ss for p, c, tk in s.get("others_top", []) if c == comm) r = dict(run=name, arm=arm, mean=st.mean(t["decode_tok_s"] for t in turns), sess_s=t1 - t0, kc_in=ticks(inside, "kcompactd0"), ks_in=ticks(inside, "kswapd0"), kc_before=ticks(before, "kcompactd0"), ks_before=ticks(before, "kswapd0")) if len(inside) >= 2: a, b = inside[0]["vmstat"], inside[-1]["vmstat"] for k in ("compact_stall", "compact_fail", "compact_migrate_scanned", "compact_free_scanned", "thp_fault_alloc", "thp_fault_fallback", "allocstall_normal", "pgscan_kswapd", "pgscan_direct", "pgsteal_direct"): r["d_" + k] = b.get(k, 0) - a.get(k, 0) # ms/round mr = [] if arm == "ours": oc = [(int(m.group(1)), int(m.group(2))) for m in (re.search(r"request outcome=completed produced=(\d+) rounds=(\d+)", l) for l in open(os.path.join(d, "inference.log"), errors="replace")) if m][-5:] mr = [1000.0 * (t.get("output_tokens", 300) / t["decode_tok_s"]) / rd for (p, rd), t in zip(oc, turns)] else: for l in [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:]: m = re.search(r"(\d+) generated in (\d+) ms.*drafts accepted (\d+) of (\d+)", l) if m: mr.append(int(m.group(2)) / (int(m.group(1)) - int(m.group(3)))) r["ms_round"] = st.mean(mr) if mr else None rows.append(r) cols = ["kc_in", "ks_in", "kc_before", "d_compact_stall", "d_compact_fail", "d_compact_migrate_scanned", "d_thp_fault_fallback", "d_allocstall_normal", "d_pgscan_kswapd", "d_pgscan_direct"] print("run tok/s ms/rd sess_s " + " ".join("%12s" % c[-12:] for c in cols)) for r in rows: print("%-12s %6.1f %5.2f %6.0f " % (r["run"], r["mean"], r["ms_round"] or 0, r["sess_s"]) + " ".join("%12s" % r.get(c, "-") for c in cols)) for arm in ("ours", "v1", "v2", "vanilla(v1+v2)"): rs = [r for r in rows if (r["arm"] == arm or (arm.startswith("vanilla") and r["arm"] in ("v1", "v2")))] if len(rs) < 4: continue print("\n%s (n=%d): correlation with ms/round and with all-turn tok/s" % (arm, len(rs))) for c in cols: pts = [(r[c], r["ms_round"], r["mean"]) for r in rs if r.get(c) is not None and r["ms_round"]] if len(pts) >= 4 and len(set(p[0] for p in pts)) > 1: a, b, m = pearson([p[0] for p in pts], [p[1] for p in pts]), None, pearson([p[0] for p in pts], [p[2] for p in pts]) print(" %-28s r(ms/round)=%.2f r(tok/s)=%.2f range %s..%s" % (c, a, m, min(p[0] for p in pts), max(p[0] for p in pts)))