Datasets:
Download code/recompute_256.py from Boesgaard/delivered-sooner: direct link, hf CLI and curl.
- Browser
- Download file 12.9 kB
-
https://huggingface.co/datasets/Boesgaard/delivered-sooner/resolve/main/code/recompute_256.py
- Command line
-
hf download hf://datasets/Boesgaard/delivered-sooner/code/recompute_256.py
-
curl -L -o recompute_256.py https://huggingface.co/datasets/Boesgaard/delivered-sooner/resolve/main/code/recompute_256.py
12.9 kB
| """Independent recomputation of the 256-question panel from the scored question files. | |
| uv run --no-project --with numpy python code/recompute_256.py > outputs/recompute_256.json | |
| Bootstrap: paired questions resampled within each of the four source strata, 10,000 draws, percentile intervals. | |
| The seed differs from the registered analysis on purpose, so intervals agree with the paper only up to Monte Carlo | |
| error; point estimates agree exactly. Output equals stats/audit/recompute_256.json. | |
| """ | |
| import hashlib | |
| import json | |
| import sys | |
| from pathlib import Path | |
| import numpy as np | |
| ROOT = Path(__file__).resolve().parent.parent | |
| RAW = ROOT / "records" | |
| ARMS = { | |
| "base": "confirm_baseline/scored/base.scored.jsonl", | |
| "late": "confirm_broad/scored/base_mlp_all_l16_31.scored.jsonl", | |
| "concise": "confirm_concise/scored/concise.scored.jsonl", | |
| "edit": "confirm_wsp/scored/base_wsp_pair4_a6.scored.jsonl", | |
| "random": "confirm_wsp/scored/base_wsp_pair4_random.scored.jsonl", | |
| "all32": "confirm_full_mlp_forward/scored/base_mlp_all.scored.jsonl", | |
| "reverse": "confirm_full_mlp_reverse/scored/concise_restore_mlp_all.scored.jsonl", | |
| } | |
| AREAS = ("hardmath", "mathconfirm", "sgpqa", "retention") | |
| DRAWS = 10_000 | |
| SEED = 20261129 # deliberately independent of the paper's seeds | |
| BUDGETS = (4096, 8192, 16384, 32768) | |
| def sha(path): | |
| return hashlib.sha256(path.read_bytes()).hexdigest() | |
| def load(): | |
| inputs = [json.loads(x) for x in (RAW / "confirmation256/panel.jsonl").read_text().splitlines() if x.strip()] | |
| keys = sorted((r["set"], r["id"], r["sample"]) for r in inputs) | |
| meta = {(r["set"], r["id"], r["sample"]): r for r in inputs} | |
| assert len(keys) == 256 and len({k[1] for k in keys}) == 256 | |
| data, hashes = {}, {} | |
| for arm, rel in ARMS.items(): | |
| path = RAW / rel | |
| hashes[arm] = sha(path) | |
| rows = {} | |
| for line in path.read_text().splitlines(): | |
| if not line.strip(): | |
| continue | |
| r = json.loads(line) | |
| k = (r["set"], r["id"], r["sample"]) | |
| assert k not in rows and r["status"] == "scored" | |
| assert r["seed"] == meta[k]["seed"] and r["area"] == meta[k]["area"] | |
| rows[k] = r | |
| assert set(rows) == set(keys), arm | |
| ordered = [rows[k] for k in keys] | |
| data[arm] = { | |
| "thinking": np.array([r["thinking_tokens"] for r in ordered], float), | |
| "generated": np.array([r["generated_tokens"] for r in ordered], float), | |
| "native": np.array([r["native_generated_tokens"] for r in ordered], float), | |
| "natural": np.array([int(r["natural_completion"] and r["native_correct_v2"]) for r in ordered]), | |
| "final": np.array([int(r["correct_v2"]) for r in ordered]), | |
| "final_v3": np.array([int(r["correct_v3"]) for r in ordered]), | |
| "forced": np.array([int(r["forced"]) for r in ordered]), | |
| "completed": np.array([int(r["natural_completion"]) for r in ordered]), | |
| "at_cap": np.array([int(r["native_generated_tokens"] == r["max_new_tokens"]) for r in ordered]), | |
| "has_answer": np.array([int(bool(r["has_answer"])) for r in ordered]), | |
| } | |
| areas = np.array([meta[k]["area"] for k in keys]) | |
| surfaces = set() | |
| for rel in ARMS.values(): | |
| for line in (RAW / rel).read_text().splitlines(): | |
| if line.strip(): | |
| surfaces.add(json.loads(line)["comparison_surface_hash"]) | |
| return keys, areas, data, hashes, surfaces | |
| def boot_indices(areas, rng): | |
| idx = np.zeros((DRAWS, len(areas)), dtype=np.int64) | |
| col = 0 | |
| for area in AREAS: | |
| members = np.flatnonzero(areas == area) | |
| picks = rng.integers(len(members), size=(DRAWS, len(members))) | |
| idx[:, col:col + len(members)] = members[picks] | |
| col += len(members) | |
| assert col == len(areas) | |
| return idx | |
| def ci(x): | |
| return [float(v) for v in np.quantile(x, [0.025, 0.975])] | |
| def natural_within(arm, budget): | |
| # Natural completion with a correct native answer and the native trajectory no longer than the budget. | |
| return (arm["natural"] == 1) & (arm["native"] <= budget) | |
| def main(): | |
| keys, areas, data, hashes, surfaces = load() | |
| rng = np.random.Generator(np.random.PCG64(SEED)) | |
| idx = boot_indices(areas, rng) | |
| out = {"hashes": hashes, "serving_surfaces": sorted(surfaces), "arms": {}, "vs_base": {}} | |
| for name, a in data.items(): | |
| out["arms"][name] = { | |
| "n": len(keys), | |
| "thinking_sum": int(a["thinking"].sum()), "generated_sum": int(a["generated"].sum()), | |
| "thinking_mean": float(a["thinking"].mean()), "generated_mean": float(a["generated"].mean()), | |
| "natural": int(a["natural"].sum()), "final": int(a["final"].sum()), "final_v3": int(a["final_v3"].sum()), | |
| "forced": int(a["forced"].sum()), "at_cap": int(a["at_cap"].sum()), | |
| "natural_within": {b: int(natural_within(a, b).sum()) for b in BUDGETS}, | |
| "generated_per_final_correct": float(a["generated"].sum() / a["final"].sum()), | |
| "by_area_generated_ratio_vs_base": None, | |
| } | |
| b = data["base"] | |
| for name in ("late", "concise", "edit", "random", "all32", "reverse"): | |
| a = data[name] | |
| rep = {} | |
| for metric in ("generated", "thinking"): | |
| ratio = a[metric].sum() / b[metric].sum() | |
| ratios = a[metric][idx].sum(1) / b[metric][idx].sum(1) | |
| rep[metric] = {"ratio": float(ratio), "ratio_ci": ci(ratios), | |
| "percent_fewer": float(100 * (1 - ratio)), "percent_fewer_ci": ci(100 * (1 - ratios)), | |
| "saved_total": int(b[metric].sum() - a[metric].sum()), | |
| "mean_saved": float((b[metric] - a[metric]).mean()), | |
| "mean_saved_ci": ci((b[metric] - a[metric])[idx].mean(1))} | |
| for metric in ("natural", "final", "final_v3"): | |
| d = a[metric] - b[metric] | |
| rep[metric] = {"diff_pp": float(100 * d.mean()), "ci": ci(100 * d[idx].mean(1)), | |
| "gains": int((d > 0).sum()), "losses": int((d < 0).sum())} | |
| rep["budget"] = {} | |
| for budget in BUDGETS: | |
| nb, na = natural_within(b, budget).astype(int), natural_within(a, budget).astype(int) | |
| d = na - nb | |
| rep["budget"][budget] = {"base": int(nb.sum()), "arm": int(na.sum()), "gains": int((d > 0).sum()), | |
| "losses": int((d < 0).sum()), "diff_pp": float(100 * d.mean()), | |
| "ci": ci(100 * d[idx].mean(1))} | |
| rep["shorter_thinking_questions"] = int((a["thinking"] < b["thinking"]).sum()) | |
| rep["shorter_generated_questions"] = int((a["generated"] < b["generated"]).sum()) | |
| rep["by_area_generated_ratio"] = {ar: float(a["generated"][areas == ar].sum() / b["generated"][areas == ar].sum()) for ar in AREAS} | |
| rep["by_area_counts"] = {ar: {"natural": [int(b["natural"][areas == ar].sum()), int(a["natural"][areas == ar].sum())], | |
| "final": [int(b["final"][areas == ar].sum()), int(a["final"][areas == ar].sum())]} for ar in AREAS} | |
| # cost per correct final answer | |
| cpc_b = b["generated"].sum() / b["final"].sum() | |
| cpc_a = a["generated"].sum() / a["final"].sum() | |
| boot_b = b["generated"][idx].sum(1) / b["final"][idx].sum(1) | |
| boot_a = a["generated"][idx].sum(1) / a["final"][idx].sum(1) | |
| rep["cost_per_correct"] = {"base": float(cpc_b), "arm": float(cpc_a), "saving_pct": float(100 * (1 - cpc_a / cpc_b)), | |
| "saving_ci": ci(100 * (1 - boot_a / boot_b))} | |
| both = (a["final"] == 1) & (b["final"] == 1) | |
| sub_ratio = a["generated"][both].sum() / b["generated"][both].sum() | |
| # bootstrap within the both-correct subset, resampling the subset's members within strata | |
| rng2 = np.random.Generator(np.random.PCG64(SEED + 1)) | |
| sub_areas = areas[both] | |
| bi = boot_indices_subset(sub_areas, rng2) | |
| sb, sa = b["generated"][both], a["generated"][both] | |
| rep["both_final_correct"] = {"n": int(both.sum()), "percent_fewer_generated": float(100 * (1 - sub_ratio)), | |
| "percent_fewer_ci": ci(100 * (1 - sa[bi].sum(1) / sb[bi].sum(1))), | |
| "saved_tokens": int(sb.sum() - sa.sum())} | |
| # losses and finishing | |
| loss = (b["final"] == 1) & (a["final"] == 0) | |
| rep["final_losses_detail"] = {"losses": int(loss.sum()), | |
| "both_finished_naturally": int((loss & (a["completed"] == 1) & (b["completed"] == 1)).sum()), | |
| "arm_fewer_generated": int((loss & (a["generated"] < b["generated"])).sum()), | |
| "arm_missing_answer": int((loss & (a["has_answer"] == 0)).sum())} | |
| gain = (b["final"] == 0) & (a["final"] == 1) | |
| rep["v3_retained"] = {"gains_v2": int(gain.sum()), "gains_also_v3": int((gain & (a["final_v3"] == 1) & (b["final_v3"] == 0)).sum()), | |
| "losses_v2": int(loss.sum()), "losses_also_v3": int((loss & (a["final_v3"] == 0) & (b["final_v3"] == 1)).sum())} | |
| out["vs_base"][name] = rep | |
| # transfer fraction late / concise and all32 / concise | |
| c, l = data["concise"], data["late"] | |
| for armname in ("late", "all32"): | |
| l = data[armname] | |
| frac = {} | |
| for metric in ("generated", "thinking"): | |
| num = b[metric].sum() - l[metric].sum() | |
| den = b[metric].sum() - c[metric].sum() | |
| bn = b[metric][idx].sum(1) - l[metric][idx].sum(1) | |
| bd = b[metric][idx].sum(1) - c[metric][idx].sum(1) | |
| frac[metric] = {"fraction": float(num / den), "ci": ci(bn / bd)} | |
| out[f"{armname}_fraction_of_concise"] = frac | |
| l = data["late"] | |
| a32 = data["all32"] | |
| out["all32_vs_concise"] = {} | |
| out["all32_vs_late"] = {} | |
| for metric in ("natural", "final"): | |
| for other, key in ((c, "all32_vs_concise"), (l, "all32_vs_late")): | |
| d = a32[metric] - other[metric] | |
| out[key][metric] = {"diff_pp": float(100 * d.mean()), "ci": ci(100 * d[idx].mean(1)), | |
| "gain": int((d > 0).sum()), "loss": int((d < 0).sum())} | |
| cells4 = {} | |
| for p_, l_, a_, c_ in zip(b["final"], l["final"], a32["final"], c["final"]): | |
| k = f"{p_}{l_}{a_}{c_}" | |
| cells4[k] = cells4.get(k, 0) + 1 | |
| out["four_arm_final_cells_parent_late_all32_concise"] = dict(sorted(cells4.items())) | |
| rv = data["reverse"] | |
| out["reverse_vs_concise"] = {} | |
| for metric in ("generated", "thinking"): | |
| d = rv[metric] - c[metric] | |
| out["reverse_vs_concise"][metric] = {"mean_diff": float(d.mean()), "ci": ci(d[idx].mean(1))} | |
| num = rv[metric][idx].sum(1) - c[metric][idx].sum(1) | |
| den = b[metric][idx].sum(1) - c[metric][idx].sum(1) | |
| out["reverse_vs_concise"][metric]["undone_fraction"] = float((rv[metric].sum() - c[metric].sum()) / (b[metric].sum() - c[metric].sum())) | |
| out["reverse_vs_concise"][metric]["undone_ci"] = ci(num / den) | |
| for metric in ("natural", "final"): | |
| d = rv[metric] - c[metric] | |
| out["reverse_vs_concise"][metric] = {"diff_pp": float(100 * d.mean()), "ci": ci(100 * d[idx].mean(1)), | |
| "gain": int((d > 0).sum()), "loss": int((d < 0).sum())} | |
| # late vs concise accuracy | |
| lc = {} | |
| for metric in ("natural", "final"): | |
| d = l[metric] - c[metric] | |
| lc[metric] = {"diff_pp": float(100 * d.mean()), "ci": ci(100 * d[idx].mean(1)), | |
| "late_gain": int((d > 0).sum()), "late_loss": int((d < 0).sum())} | |
| out["late_vs_concise"] = lc | |
| cells = {} | |
| for p, q, r in zip(b["final"], l["final"], c["final"]): | |
| k = f"{p}{q}{r}" | |
| cells[k] = cells.get(k, 0) + 1 | |
| out["three_arm_final_cells_parent_late_concise"] = dict(sorted(cells.items())) | |
| # edit vs random | |
| e, r = data["edit"], data["random"] | |
| er = {"thinking_ratio": float(e["thinking"].sum() / r["thinking"].sum()), | |
| "thinking_ratio_ci": ci(e["thinking"][idx].sum(1) / r["thinking"][idx].sum(1))} | |
| for metric in ("natural", "final"): | |
| d = e[metric] - r[metric] | |
| er[metric] = {"diff_pp": float(100 * d.mean()), "ci": ci(100 * d[idx].mean(1))} | |
| out["edit_vs_random"] = er | |
| json.dump(out, sys.stdout, indent=1) | |
| def boot_indices_subset(sub_areas, rng): | |
| idx = np.zeros((DRAWS, len(sub_areas)), dtype=np.int64) | |
| col = 0 | |
| for area in AREAS: | |
| members = np.flatnonzero(sub_areas == area) | |
| if len(members) == 0: | |
| continue | |
| picks = rng.integers(len(members), size=(DRAWS, len(members))) | |
| idx[:, col:col + len(members)] = members[picks] | |
| col += len(members) | |
| return idx | |
| if __name__ == "__main__": | |
| main() | |