"""Results-dependent LaTeX tables + macros (called by scripts/make_tables.py). Reads run manifests/metrics under experiments/runs (local copies), results/raw/*.json and results/analyzed/*.json. Every number is traceable to a run id.""" from __future__ import annotations import glob, json, math from collections import defaultdict from pathlib import Path import numpy as np OUT = Path("paper/tables") RUNS = "experiments/runs" def _runs(prefix): return sorted(d for d in glob.glob(f"{RUNS}/{prefix}_seed*") if Path(d, "manifest.json").exists()) def _final_val(run): rows = [json.loads(l) for l in open(Path(run) / "metrics.jsonl")] last = [r for r in rows if any(k.startswith("val_loss_b") for k in r)] return last[-1] if last else {} def table_seed_matrix(macro): """Phase 4 decisive matrix: PRISME vs global recurrence vs dense, 5 seeds, val loss by budget and exact match.""" systems = [("p4v2_prisme", "PRISME sparse editor"), ("p4v2_gr", "Global recurrence (inspired)"), ("p4v2_dense", "Dense 16-slot writes")] rows, raw = [], {} for pre, lab in systems: runs = [r for r in _runs(pre) if json.load(open(Path(r) / "manifest.json")).get("state") == "completed"] if not runs: continue vals = defaultdict(list); ems = defaultdict(list) for r in runs: fv = _final_val(r) for k, v in fv.items(): if k.startswith("val_loss_b"): vals[int(k[10:])].append(v) fem = json.load(open(Path(r) / "manifest.json")).get("final_exact_match", {}) for split, d in fem.items(): for b, e in d.items(): ems[(split, int(b))].append(e["accuracy"]) raw[pre] = {"n_seeds": len(runs), "val": {b: v for b, v in vals.items()}, "em": {f"{s}_b{b}": v for (s, b), v in ems.items()}, "run_ids": [Path(r).name for r in runs]} rows.append((lab, len(runs), vals, ems)) if not rows: return Path("results/analyzed").mkdir(parents=True, exist_ok=True) Path("results/analyzed/seed_matrix_v2.json").write_text(json.dumps(raw, indent=1)) def ms(v): v = np.asarray(v, float) return f"{v.mean():.4f} $\\pm$ {v.std(ddof=1):.4f}" if len(v) > 1 else f"{v.mean():.4f}" def pct(v): v = 100 * np.asarray(v, float) return f"{v.mean():.1f} $\\pm$ {v.std(ddof=1):.1f}" if len(v) > 1 else f"{v.mean():.1f}" head = r"""\begin{table}[t]\centering\small \caption{Phase 4 decisive comparison on the frozen 5.6M-parameter synthetic backbone (char-level): held-out answer-span loss on dev-iid under random forced actions at fixed budgets, and free-generation exact match (dev-iid, 1,000 stratified items). Mean $\pm$ SD over independent training seeds of the adapter; the backbone, data, recipe and symmetry-breaking device are identical. Budget 0 is the frozen backbone for every system.} \label{tab:seedmatrix} \begin{tabular}{llrrrrr}\toprule System & seeds & loss b=0 & loss b=1 & loss b=2 & loss b=4 & loss b=8\\\midrule """ body = "" for lab, n, vals, ems in rows: body += f"{lab} & {n} & " + " & ".join(ms(vals.get(b, [float('nan')])) for b in [0, 1, 2, 4, 8]) + r"\\" + "\n" body += r"\midrule" + "\n" + r"\multicolumn{7}{l}{\emph{Exact match (\%), dev-iid}: b=0 / b=1 / b=2 / b=8}\\" + "\n" for lab, n, vals, ems in rows: cells = [pct(ems.get(("dev_iid", b), [float("nan")])) for b in [0, 1, 2, 8]] body += f"{lab} & {n} & \\multicolumn{{5}}{{l}}{{{' / '.join(cells)}}}\\\\\n" body += r"\multicolumn{7}{l}{\emph{Exact match (\%), held-out template / depth / compose at b=2}}\\" + "\n" for lab, n, vals, ems in rows: cells = [pct(ems.get((s, 2), [float("nan")])) for s in ["dev_template", "dev_depth", "dev_compose"]] body += f"{lab} & {n} & \\multicolumn{{5}}{{l}}{{{' / '.join(cells)}}}\\\\\n" (OUT / "seed_matrix.tex").write_text(head + body + r"\bottomrule\end{tabular}\end{table}" + "\n") for pre, lab in systems: tag = pre.replace("p4v2_", "vtwo") if pre not in raw: # system not (yet) completed: emit explicit placeholders so the manuscript compiles and the audit flags them macro(f"{tag}seeds", "0") for bn in ("one", "two", "four", "eight"): macro(f"{tag}gainb{bn}", "n/a") for e in ("embzero", "embtwo", "embeight"): macro(f"{tag}{e}", "n/a") if pre in raw: v = raw[pre]["val"] for b, bn in [(1, "one"), (2, "two"), (4, "four"), (8, "eight")]: if 0 in v and b in v: macro(f"{tag}gainb{bn}", f"{100 * (1 - np.mean(v[b]) / np.mean(v[0])):.1f}") macro(f"{tag}seeds", str(raw[pre]["n_seeds"])) em = raw[pre]["em"] if "dev_iid_b0" in em and "dev_iid_b2" in em: macro(f"{tag}embzero", f"{100 * np.mean(em['dev_iid_b0']):.1f}"); macro(f"{tag}embtwo", f"{100 * np.mean(em['dev_iid_b2']):.1f}") macro(f"{tag}embeight", f"{100 * np.mean(em.get('dev_iid_b8', em['dev_iid_b2'])):.1f}") macro("vtwobzeroloss", f"{np.mean(raw[systems[0][0]]['val'][0]):.4f}" if systems[0][0] in raw else "n/a") def table_routing(macro): files = sorted(glob.glob("results/raw/routing_v1ctrl.json")) or sorted(glob.glob("results/raw/routing_*.json")) if not files: return d = json.load(open(files[-1])) head = r"""\begin{table}[t]\centering\small \caption{Routing comparison on dev-iid (tiny scale, editor checkpoint of the locked run): held-out answer-span loss, executed edits per token and analytic extra operations per token (relative to the backbone). Oracles read the target and are diagnostic ceilings, not deployable systems.} \label{tab:routing} \begin{tabular}{llrrr}\toprule Policy & setting & loss & edits/token & extra ops\\\midrule """ body = "" for r in d["records"]: if r["policy"] == "oracle_two_step": continue setting = ", ".join(f"{k}={v}" for k, v in r.items() if k in ("budget", "threshold", "lam", "kap") and v is not None) body += f"{r['policy'].replace('_', ' ')}{' (diag.)' if r.get('diagnostic') else ''} & {setting} & {r['loss']:.4f} & {r['edits_per_token']:.2f} & {r['extra_ops_ratio']:.3f}\\\\\n" def pick(pol, **kw): for r in d["records"]: if r["policy"] == pol and all(r.get(k) == v for k, v in kw.items()): return r return None b0 = pick("budget0") if b0: macro("routingbzero", f"{b0['loss']:.4f}") for pol, tag in [("uniform", "uniform"), ("random_forced", "random"), ("fixed_pair", "pair")]: for b in (1, 2, 8): r = pick(pol, budget=b) if r: macro(f"routing{tag}b{'one' if b == 1 else 'two' if b == 2 else 'eight'}", f"{100 * (r['loss'] / b0['loss'] - 1):+.1f}") r = pick("controller_locked") if r: macro("routingctrllocked", f"{100 * (r['loss'] / b0['loss'] - 1):+.1f}"); macro("routingctrlledits", f"{r['edits_per_token']:.2f}"); macro("routingctrllambda", f"{r['lam']}") for b in (1, 8): r = pick("oracle1_greedy", budget=b) if r: macro(f"routingoracleb{'one' if b == 1 else 'eight'}", f"{100 * (r['loss'] / b0['loss'] - 1):+.1f}"); macro(f"routingoracleedits{'one' if b == 1 else 'eight'}", f"{r['edits_per_token']:.2f}") best_ent = min((r for r in d["records"] if r["policy"] == "entropy"), key=lambda r: r["loss"], default=None) best_norm = min((r for r in d["records"] if r["policy"] == "norm"), key=lambda r: r["loss"], default=None) if best_ent: macro("routingentropybest", f"{100 * (best_ent['loss'] / b0['loss'] - 1):+.1f}") if best_norm: macro("routingnormbest", f"{100 * (best_norm['loss'] / b0['loss'] - 1):+.1f}") two = [r for r in d["records"] if r["policy"] == "oracle_two_step"] if two: t = two[0] body += r"\midrule" + f"\\multicolumn{{5}}{{l}}{{Two-step oracle (diag.): one-step {t['one_step_gain']:.4f}, two-step {t['two_step_gain']:.4f}, greedy-then-best {t['greedy_then_best_gain']:.4f} nats/token}}\\\\\n" macro("oracleonestep", f"{t['one_step_gain']:.4f}"); macro("oracletwostep", f"{t['two_step_gain']:.4f}") (OUT / "routing.tex").write_text(head + body + r"\bottomrule\end{tabular}\end{table}" + "\n") def table_controller(macro): runs = sorted(glob.glob(f"{RUNS}/controller_p4*_seed*/manifest.json")) if not runs: return head = r"""\begin{table}[t]\centering\small \caption{Value-controller fit on logged counterfactual labels (tiny scale). Calibration-partition diagnostics: Huber loss of $\hat g$, sign accuracy, Brier score and ECE of the harm head, one-step oracle opportunity and the regret of the controller's greedy choice on the exhaustive-audit states (captured fraction $=1-$ regret/opportunity).} \label{tab:controller} \begin{tabular}{lrrrrrrr}\toprule Controller features & labels & Huber & sign acc. & Brier & ECE & opportunity & captured\\\midrule """ body = "" for mp in runs: m = json.load(open(mp)) if m.get("state") != "completed": continue ev = m.get("eval_calib", {}) feats = "h + entropy" if "hent" in m["run_id"] else "z only" body += f"{feats} & {ev.get('n_labels', 0):,} & {ev.get('huber', float('nan')):.4f} & {ev.get('sign_acc', float('nan')):.3f} & {ev.get('harm_brier', float('nan')):.3f} & {ev.get('harm_ece', float('nan')):.3f} & {ev.get('oracle_opportunity_per_state', float('nan')):.4f} & {ev.get('captured_fraction', float('nan')):.3f}\\\\\n" macro("ctrl" + ("hent" if "hent" in m["run_id"] else "z") + "signacc", f"{100 * ev.get('sign_acc', float('nan')):.1f}") (OUT / "controller.tex").write_text(head + body + r"\bottomrule\end{tabular}\end{table}" + "\n") def table_labels(macro): files = sorted(glob.glob("results/analyzed/labels_labels_v1ctrl*.json")) or sorted(glob.glob("results/analyzed/labels_*.json")) if not files: return d = json.load(open(files[-1])) for part in ("train", "calib"): if part in d and "audit" in d[part]: a = d[part]["audit"] macro(f"labels{part}within", f"{100 * a['within_share']:.0f}") macro(f"labels{part}allpos", f"{100 * a['frac_states_all_positive']:.0f}") macro(f"labels{part}allneg", f"{100 * a['frac_states_all_negative']:.0f}") macro(f"labels{part}oracle", f"{a['oracle_one_step_mean']:.4f}") macro(f"labels{part}stoponly", f"{a['oracle_state_level_mean']:.4f}") macro(f"labels{part}random", f"{a['random_action_mean']:.4f}") macro(f"labels{part}nstates", f"{a['n_states']:,}") if "random_gain_high_vs_low_base_loss" in a: macro(f"labels{part}oraclehigh", f"{a['oracle_gain_high_vs_low_base_loss']['high']:.4f}") macro(f"labels{part}randomhigh", f"{a['random_gain_high_vs_low_base_loss']['high']:+.4f}") macro(f"labels{part}topdecoracle", f"{a['top_decile_base_loss']['oracle']:.3f}"); macro(f"labels{part}topdecrandom", f"{a['top_decile_base_loss']['random']:+.3f}") macro(f"labels{part}fracposhigh", f"{100 * a['frac_actions_positive_high_vs_low']['high']:.0f}"); macro(f"labels{part}fracposlow", f"{100 * a['frac_actions_positive_high_vs_low']['low']:.0f}") def table_latency(macro): files = sorted(glob.glob("results/raw/latency_*.json")) if not files: return d = json.load(open(files[-1])) head = r"""\begin{table}[t]\centering\small \caption{Measured decode latency per generated token (fp32, prompt %d tokens, %d generated, warm-up and synchronisation), device %s. Median and p95 over generated tokens of three repetitions.} \label{tab:latency} \begin{tabular}{llrrrr}\toprule Policy & budget & batch & median ms & p95 ms & cache bytes\\\midrule """ % (d["prompt_len"], d["n_new"], d["device"].replace("_", " ")) body = "" for r in d["records"]: body += f"{r['policy'].replace('_', ' ')} & {r['budget']} & {r['batch']} & {r['median_ms']:.2f} & {r['p95_ms']:.2f} & {r['cache_bytes']:,}\\\\\n" (OUT / "latency.tex").write_text(head + body + r"\bottomrule\end{tabular}\end{table}" + "\n") b0 = [r for r in d["records"] if r["policy"] == "budget0" and r["batch"] == 1] if b0: macro("latencybzero", f"{b0[0]['median_ms']:.2f}") def make_all(macro): table_seed_matrix(macro); table_routing(macro); table_controller(macro); table_labels(macro); table_latency(macro) def table_routing_seeds(macro): """Per-seed routing summary for the v2 PRISME editors: budget 0, best fixed schedule, locked controller, oracle.""" files = sorted(glob.glob("results/raw/routing_v2s*.json")) if not files: return rows = [] for f in files: d = json.load(open(f)); recs = d["records"] def pick(pol, **kw): for r in recs: if r["policy"] == pol and all(r.get(k) == v for k, v in kw.items()): return r b0 = pick("budget0"); fixed = [r for r in recs if r["policy"] in ("random_forced", "uniform", "fixed_pair")] best_fixed = min(fixed, key=lambda r: r["loss"]); ctl = pick("controller_locked"); ora = pick("oracle1_greedy", budget=8) ent = min((r for r in recs if r["policy"] == "entropy"), key=lambda r: r["loss"]); nrm = min((r for r in recs if r["policy"] == "norm"), key=lambda r: r["loss"]) seed = Path(f).stem.replace("routing_v2s", "") man = sorted(glob.glob(f"{RUNS}/controller_v2s{seed}_seed*/manifest.json")) ev = json.load(open(man[-1])).get("eval_calib", {}) if man else {} rows.append({"seed": seed, "b0": b0["loss"], "best_fixed": best_fixed["loss"], "best_fixed_name": f"{best_fixed['policy'].replace('_', ' ')} b={best_fixed['budget']}", "entropy": ent["loss"], "norm": nrm["loss"], "ctrl": ctl["loss"], "ctrl_edits": ctl["edits_per_token"], "ctrl_lam": ctl["lam"], "ctrl_kap": ctl["kap"], "oracle8": ora["loss"], "oracle8_edits": ora["edits_per_token"], "sign_acc": ev.get("sign_acc", float("nan")), "captured": ev.get("captured_fraction", float("nan"))}) Path("results/analyzed/routing_seeds_v2.json").write_text(json.dumps(rows, indent=1)) head = r"""\begin{table}[t]\centering\small \caption{Learned routing per adapter seed (v2 backbone, dev-iid, 640 items): held-out answer-span loss of the frozen backbone, of the best fixed schedule (named), of the best entropy and norm routers, of the locked controller ($\lambda,\kappa$ selected on calibration rollouts; edits per token in parentheses), and of the diagnostic greedy one-step oracle at budget 8. Calibration sign accuracy and captured fraction of the oracle opportunity for each controller.} \label{tab:routingseeds} \begin{tabular}{lrlrrrrrr}\toprule seed & budget 0 & best fixed & entropy & norm & controller (edits) & oracle b=8 (edits) & sign acc. & captured\\\midrule """ body = "" for r in rows: body += (f"{r['seed']} & {r['b0']:.4f} & {r['best_fixed']:.4f} ({r['best_fixed_name']}) & {r['entropy']:.4f} & {r['norm']:.4f} & " f"{r['ctrl']:.4f} ({r['ctrl_edits']:.2f}) & {r['oracle8']:.4f} ({r['oracle8_edits']:.2f}) & {r['sign_acc']:.3f} & {r['captured']:+.3f}\\\\\n") (OUT / "routing_seeds.tex").write_text(head + body + r"\bottomrule\end{tabular}\end{table}" + "\n") b0 = np.array([r["b0"] for r in rows]); ctl = np.array([r["ctrl"] for r in rows]); bf = np.array([r["best_fixed"] for r in rows]); ora = np.array([r["oracle8"] for r in rows]) macro("vtwoctrlseeds", str(len(rows))); macro("vtwoctrlrel", f"{100 * (ctl / b0 - 1).mean():+.1f}"); macro("vtwoctrlrelsd", f"{100 * (ctl / b0 - 1).std(ddof=1) if len(rows) > 1 else 0:.1f}") macro("vtwobestfixedrel", f"{100 * (bf / b0 - 1).mean():+.1f}"); macro("vtwooraclerel", f"{100 * (ora / b0 - 1).mean():+.1f}") macro("vtwoctrlbetter", str(int((ctl < b0).sum()))) _old_make_all = make_all def make_all(macro): # noqa: F811 _old_make_all(macro); table_routing_seeds(macro)