Files
leleka/scripts/e2e/codex/speed/ab_report.py
T

104 lines
4.5 KiB
Python

#!/usr/bin/env python3
"""Paired A/B report of the speed program (offline analysis, stdlib only; never part of serving).
ab_report.py RUN_DIR BASE VAR [--skip 3]
RUN_DIR holds one directory per block named NAME.BLOCK (written by spd_ab_job.sh: engine.log, replay.jsonl).
For every block number present for both BASE and VAR the engine's per-request events give: decode tok/s (sum output tokens / sum decode ms),
tokens per round, ms per round, end-to-end tok/s (output / (prefill + decode)); the first --skip requests of each block (warm-up) are dropped.
The paired difference VAR - BASE per block pair is reported as mean, relative mean and a two-sided 95 % t interval; the identity record compares `out_sha`
of every request between the two members of each pair (equal = the greedy output is byte-identical).
"""
import json
import math
import os
import re
import sys
T975 = {1: 12.706, 2: 4.303, 3: 3.182, 4: 2.776, 5: 2.571, 6: 2.447, 7: 2.365, 8: 2.306, 9: 2.262, 10: 2.228, 11: 2.201, 12: 2.179, 13: 2.160, 14: 2.145,
15: 2.131, 16: 2.120, 17: 2.110, 18: 2.101, 19: 2.093, 20: 2.086}
def requests(path, skip):
out = []
for line in open(path, errors="replace"):
if line.startswith("{") and '"ev":"request"' in line and '"comp":"engine"' in line:
j = json.loads(line)
if j["ptok"] > 200:
out.append(j)
return out[skip:]
def metrics(d, skip):
r = requests(os.path.join(d, "engine.log"), skip)
if not r:
return None
o = sum(j["otok"] for j in r)
dec = sum(j["dec_ms"] for j in r)
pf = sum(j["pf_ms"] for j in r)
rounds = sum(j["rounds"] for j in r)
return {"n": len(r), "dec_tps": o / dec * 1e3, "tok_round": o / rounds, "ms_round": dec / rounds, "e2e_tps": o / (pf + dec) * 1e3,
"vram_pct": sum(j.get("tier_vram_pct", 0) for j in r) / len(r)}
def hashes(d):
out = {}
p = os.path.join(d, "replay.jsonl")
if os.path.exists(p):
for line in open(p):
j = json.loads(line)
out[j["seq"]] = (j.get("out_sha"), j.get("output_tokens"), j.get("error"))
return out
def ci(xs):
n = len(xs)
m = sum(xs) / n
if n < 2:
return m, float("nan")
sd = math.sqrt(sum((x - m) ** 2 for x in xs) / (n - 1))
return m, T975.get(n - 1, 1.96) * sd / math.sqrt(n)
def main():
a = [x for x in sys.argv[1:] if not x.startswith("--")]
skip = 3
if "--skip" in sys.argv:
skip = int(sys.argv[sys.argv.index("--skip") + 1])
a = [x for x in a if x != str(skip)]
run, base, var = a[0], a[1], a[2]
blocks = sorted({d.split(".", 1)[1] for d in os.listdir(run) if d.startswith(base + ".")} & {d.split(".", 1)[1] for d in os.listdir(run) if d.startswith(var + ".")},
key=lambda s: (len(s), s))
rows = []
ident = []
for b in blocks:
mb, mv = metrics(os.path.join(run, base + "." + b), skip), metrics(os.path.join(run, var + "." + b), skip)
if not mb or not mv:
continue
rows.append((b, mb, mv))
hb, hv = hashes(os.path.join(run, base + "." + b)), hashes(os.path.join(run, var + "." + b))
common = sorted(set(hb) & set(hv))
ident.append((b, len(common), sum(1 for s in common if hb[s][0] and hb[s][0] == hv[s][0]), sum(1 for s in common if hb[s][2] or hv[s][2])))
print("blocks paired: %d (%s vs %s), requests per block after warm-up skip %d: %s" % (len(rows), var, base, skip, ",".join(str(r[1]["n"]) for r in rows)))
for key, label in (("dec_tps", "decode tok/s"), ("e2e_tps", "end-to-end tok/s"), ("tok_round", "tokens/round"), ("ms_round", "ms/round"), ("vram_pct", "VRAM % of routed")):
bs = [r[1][key] for r in rows]
vs = [r[2][key] for r in rows]
ds = [v - b for b, v in zip(bs, vs)]
if not ds:
continue
mb, _ = ci(bs)
mv, _ = ci(vs)
md, h = ci(ds)
print("%-18s %s %.2f %s %.2f diff %+.2f +- %.2f (%+.2f %%, 95%% CI %+.2f..%+.2f %%)" % (
label, base, mb, var, mv, md, h, 100 * md / mb, 100 * (md - h) / mb, 100 * (md + h) / mb))
print("identity (requests with equal out_sha / compared / errors) per block:")
for b, n, eq, er in ident:
print(" block %s: %d / %d / %d" % (b, eq, n, er))
tot = sum(x[1] for x in ident)
eq = sum(x[2] for x in ident)
print("identity total: %d of %d requests byte-identical%s" % (eq, tot, " -> PASS" if tot and eq == tot else " -> NOT identical (see per block)"))
if __name__ == "__main__":
sys.exit(main())