#!/usr/bin/env python3 """Parse sbob-cmp-q-* job logs into quant_matrix.{json,md}. Each eval_ensemble.py run prints a final table: val set examples loss ppl spongebob 1434 2.0123 7.48 ... Rows = 11 val sets; we collect them per config (base / ensemble / 10 per-char). """ import json, re, subprocess, sys, os NS = "sbob" SLUGS = ["spongebob", "patrick", "squidward", "mr_krabs", "plankton", "sandy", "mrs_puff", "karen", "gary", "pearl", "_ensemble"] # config key -> k8s job name CONFIGS = {"base": "sbob-cmp-q-base", "ensemble": "sbob-cmp-q-ensemble"} for s in SLUGS[:-1]: CONFIGS[s] = "sbob-cmp-q-" + s.replace("_", "-") ROW_RE = re.compile(r"^(_?[a-z_]+)\s+(\d+)\s+([0-9.]+)\s+([0-9.]+)\s*$") def logs(job): out = subprocess.run( ["kubectl", "-n", NS, "logs", f"job/{job}", "--tail=200"], capture_output=True, text=True) return out.stdout def parse(text): rows = {} for ln in text.splitlines(): m = ROW_RE.match(ln.strip()) if m and m.group(1) in SLUGS: rows[m.group(1)] = {"examples": int(m.group(2)), "loss": float(m.group(3)), "ppl": float(m.group(4))} return rows def main(): matrix, missing = {}, [] for cfg, job in CONFIGS.items(): rows = parse(logs(job)) if len(rows) != len(SLUGS): missing.append((cfg, sorted(set(SLUGS) - set(rows)))) matrix[cfg] = rows out_dir = os.path.dirname(os.path.abspath(__file__)) with open(os.path.join(out_dir, "quant_matrix.json"), "w") as f: json.dump(matrix, f, indent=2) cfg_order = ["base", "ensemble"] + SLUGS[:-1] # main table: rows = val sets, cols = configs, cell = loss, bold row-best lines = ["# Quant matrix — masked val loss (gemma-4-31B base vs LoRAs)", "", "Configs: `base` = no adapter; `ensemble` = all-speaker r=64 LoRA; " "others = dedicated per-character r=32 LoRAs. " "Each column is one 1-GPU job evaluating all 11 val sets " "(token-weighted mean masked NLL; lower is better). 2026-06-10.", "", "| val set \\ config | " + " | ".join(cfg_order) + " |", "|---|" + "---|" * len(cfg_order)] flags = [] for slug in SLUGS: vals = {c: matrix.get(c, {}).get(slug, {}).get("loss") for c in cfg_order} present = {c: v for c, v in vals.items() if v is not None} best = min(present.values()) if present else None cells = [] for c in cfg_order: v = vals[c] if v is None: cells.append("—") continue if v > 5 or v < 0.5: flags.append(f"{c} on {slug}: loss {v}") cells.append(f"**{v:.4f}**" if v == best else f"{v:.4f}") n = matrix.get("base", {}).get(slug, {}).get("examples", "?") lines.append(f"| {slug} (n={n}) | " + " | ".join(cells) + " |") # summary row: mean delta vs base across the 11 val sets, per config deltas = [] for c in cfg_order: ds = [matrix[c][s]["loss"] - matrix["base"][s]["loss"] for s in SLUGS if s in matrix.get(c, {}) and s in matrix.get("base", {})] deltas.append(f"{sum(ds)/len(ds):+.4f}" if ds else "—") lines.append("| **mean Δ vs base (11 sets)** | " + " | ".join(deltas) + " |") # per-character summary: base vs dedicated vs ensemble on the char's own val set lines += ["", "## Ensemble vs dedicated, on each character's own val set", "", "| val set | base | dedicated | ensemble | ens Δ vs base | ens Δ vs dedicated |", "|---|---|---|---|---|---|"] for slug in SLUGS[:-1]: b = matrix.get("base", {}).get(slug, {}).get("loss") d = matrix.get(slug, {}).get(slug, {}).get("loss") e = matrix.get("ensemble", {}).get(slug, {}).get("loss") if None in (b, d, e): lines.append(f"| {slug} | — | — | — | — | — |") continue lines.append(f"| {slug} | {b:.4f} | {d:.4f} | {e:.4f} | " f"{e-b:+.4f} | {e-d:+.4f} |") b = matrix.get("base", {}).get("_ensemble", {}).get("loss") e = matrix.get("ensemble", {}).get("_ensemble", {}).get("loss") if b is not None and e is not None: lines.append(f"| _ensemble (own set) | {b:.4f} | — | {e:.4f} | {e-b:+.4f} | — |") # sanity checks lines += ["", "## Sanity checks", ""] for slug, known in [("squidward", 1.935), ("spongebob", 1.882)]: v = matrix.get("ensemble", {}).get(slug, {}).get("loss") if v is None: lines.append(f"- ensemble on {slug}: MISSING (expected ~{known})") else: ok = abs(v - known) < 0.05 lines.append(f"- ensemble on {slug}: {v:.4f} vs known {known} -> " + ("OK" if ok else "MISMATCH")) lines.append("- degenerate cells (loss >5 or <0.5): " + ("; ".join(flags) if flags else "none")) if missing: lines.append("- incomplete configs: " + "; ".join( f"{c} missing {m}" for c, m in missing)) with open(os.path.join(out_dir, "quant_matrix.md"), "w") as f: f.write("\n".join(lines) + "\n") print(json.dumps({"missing": missing, "flags": flags}, indent=2)) if __name__ == "__main__": main()