"""Score Exp 0 smoke runs and compare configs. Usage: python score_smoke.py V-BF16 V-FP8 ... Joins vllm bench serve `generated_texts[i]` with smoke JSONL row i (inputs are unshuffled), then reports per-dataset accuracy. LCB is not executed here: it only checks that a code block was produced. KL is computed between each config and the first one. """ import json import math import re import sys from pathlib import Path B = Path.home() / "bench" S = B / "data" / "smoke" FILES = {"ruler_4k": "ruler_4k_26.jsonl", "ruler_128k": "ruler_128k_13.jsonl", "docvqa": "docvqa_20.jsonl", "gpqa": "gpqa_8.jsonl", "lcb": "lcb_10.jsonl"} def lev(a, b): prev = list(range(len(b) + 1)) for i, ca in enumerate(a, 1): cur = [i] for j, cb in enumerate(b, 1): cur.append(min(prev[j] + 1, cur[j - 1] + 1, prev[j - 1] + (ca != cb))) prev = cur return prev[-1] def anls(pred, answers): p = pred.strip().lower() best = 0.0 for g in answers: g = g.strip().lower() nl = lev(p, g) / max(len(p), len(g), 1) best = max(best, 1 - nl if nl < 0.5 else 0.0) return best def score(ds, row, text): if ds.startswith("ruler"): return sum(a.lower() in text.lower() for a in row["answers"]) / len(row["answers"]) if ds == "docvqa": return anls(text.strip().split("\n")[-1], row["answers"]) if ds == "gpqa": m = re.findall(r"ANSWER:\s*\(?([ABCD])\)?", text) return float(bool(m) and m[-1] == row["correct_letter"]) if ds == "lcb": return float("```python" in text) def kl(p_top, q_top): """KL(P||Q) over P's top-k; Q's missing tokens get Q's smallest logprob.""" q = dict(q_top) floor = min(q.values()) if q else -30.0 return sum(math.exp(lp) * (lp - q.get(t, floor)) for t, lp in p_top) cfgs = sys.argv[1:] print(f"{'dataset':<12}" + "".join(f"{c:>14}" for c in cfgs)) for ds, fn in FILES.items(): rows = [json.loads(l) for l in open(S / fn)] line = f"{ds:<12}" for c in cfgs: f = B / "exp0" / c / f"{ds}.json" if not f.exists(): line += f"{'missing':>14}" continue res = json.loads(f.read_text()) texts, errs = res["generated_texts"], res.get("errors", []) vals = [score(ds, r, t) for r, t in zip(rows, texts)] nerr = sum(1 for e in errs if e) line += f"{sum(vals) / len(vals):>10.3f}" + (f" e{nerr:<2}" if nerr else " ") print(line) base = B / "exp0" / cfgs[0] / "kl_probe.json" if base.exists(): P = json.loads(base.read_text()) print(f"\nKL vs {cfgs[0]} (mean per token) / top-1 agreement") for c in cfgs[1:]: f = B / "exp0" / c / "kl_probe.json" if not f.exists(): print(f"{c}: missing"); continue Q = json.loads(f.read_text()) kls, agree, n = 0.0, 0, 0 for tp, tq in zip(P, Q): for pp, qq in zip(tp["positions"], tq["positions"]): if pp["top"] and qq["top"]: kls += kl(pp["top"], qq["top"]); agree += pp["top"][0][0] == qq["top"][0][0]; n += 1 print(f"{c}: KL={kls / max(n, 1):.5f} top1={agree / max(n, 1):.4f} tokens={n}")