Datasets:
Download code/figure2.py from Boesgaard/delivered-sooner: direct link, hf CLI and curl.
- Browser
- Download file 5.62 kB
-
https://huggingface.co/datasets/Boesgaard/delivered-sooner/resolve/main/code/figure2.py
- Command line
-
hf download hf://datasets/Boesgaard/delivered-sooner/code/figure2.py
-
curl -L -o figure2.py https://huggingface.co/datasets/Boesgaard/delivered-sooner/resolve/main/code/figure2.py
5.62 kB
| """Figure 2: the signed edit on 64 competition-mathematics problems with an uninterrupted 65,536-token budget. | |
| uv run --no-project --with matplotlib python code/figure2.py | |
| Reads the three scored summaries in records/signed_hard64_at65536_*/scored/ and asserts the values printed in the | |
| paper. Writes figures/figure2.pdf. Style matches figure1.py (TeX Gyre Heros, the paper's ink and rule colours; the | |
| parent and transplant colours of Figure 1; a muted violet for the subtracted edit). | |
| """ | |
| import json | |
| import subprocess | |
| import tempfile | |
| from pathlib import Path | |
| import matplotlib | |
| matplotlib.use("pdf") | |
| import matplotlib.pyplot as plt # noqa: E402 | |
| from matplotlib import font_manager # noqa: E402 | |
| from matplotlib.patches import Rectangle # noqa: E402 | |
| HERE = Path(__file__).resolve().parent.parent | |
| SUMMARIES = { | |
| "base": "signed_hard64_at65536_base/scored/base.SUMMARY.json", | |
| "plus1": "signed_hard64_at65536_plus1/scored/base_mlp_all.SUMMARY.json", | |
| "minus1": "signed_hard64_at65536_negative/scored/base_mlp_all_alpha_m100.SUMMARY.json", | |
| } | |
| S = {k: json.load(open(HERE / "records" / rel)) for k, rel in SUMMARIES.items()} | |
| arm = {k: next(iter(v["arms"].values())) if "arms" in v else v for k, v in S.items()} | |
| gen = {k: arm[k]["generated_tokens_total"] / arm[k]["expected_records"] for k in arm} | |
| nat = {k: arm[k]["natural_correct_v2"] for k in arm} | |
| forced = {k: arm[k]["forced_correct_v2"] for k in arm} | |
| capped = {k: arm[k]["forced_records"] for k in arm} | |
| assert (nat["base"], nat["plus1"], nat["minus1"]) == (51, 51, 43) | |
| assert (nat["base"] + forced["base"], nat["plus1"] + forced["plus1"], nat["minus1"] + forced["minus1"]) == (53, 51, 45) | |
| assert (capped["base"], capped["plus1"], capped["minus1"]) == (7, 3, 17) | |
| d_plus = 1 - arm["plus1"]["generated_tokens_total"] / arm["base"]["generated_tokens_total"] | |
| d_minus = arm["minus1"]["generated_tokens_total"] / arm["base"]["generated_tokens_total"] - 1 | |
| assert round(100 * d_plus, 2) == 28.11, d_plus | |
| INK, MUTED, CAP, RULE, GRID = "#1D1D1B", "#6B6A66", "#55544F", "#BDBCB6", "#E3E2DD" | |
| COL = {"base": "#A65E1E", "plus1": "#008C80", "minus1": "#7A5C8F"} | |
| LABEL = {"base": "Parent", "plus1": "+ MLP update\n(transplant)", "minus1": "− MLP update\n(subtracted)"} | |
| def heros(): | |
| tmp = Path(tempfile.mkdtemp(prefix="heros_")) | |
| try: | |
| for face in ("regular", "bold"): | |
| p = tmp / f"texgyreheros-{face}.otf" | |
| data = subprocess.run(["tectonic", "-X", "bundle", "cat", p.name], capture_output=True, check=True).stdout | |
| if len(data) < 10_000: | |
| raise RuntimeError | |
| p.write_bytes(data) | |
| font_manager.fontManager.addfont(str(p)) | |
| return "TeX Gyre Heros" | |
| except (OSError, subprocess.CalledProcessError, RuntimeError): | |
| return "Helvetica" | |
| plt.rcParams.update({"font.family": "sans-serif", "font.sans-serif": [heros(), "Helvetica", "Arial"], "font.size": 7.5, | |
| "pdf.fonttype": 42, "axes.edgecolor": RULE, "axes.linewidth": 0.6, "axes.labelcolor": CAP, | |
| "axes.labelsize": 7.2, "xtick.color": MUTED, "ytick.color": MUTED, "xtick.labelcolor": CAP, | |
| "ytick.labelcolor": CAP, "xtick.labelsize": 7, "ytick.labelsize": 7, "xtick.major.size": 0, | |
| "ytick.major.size": 2.5, "ytick.major.width": 0.6}) | |
| fig, (a, b) = plt.subplots(1, 2, figsize=(6.3, 2.35), gridspec_kw={"wspace": 0.32}) | |
| keys = ["base", "plus1", "minus1"] | |
| xs = range(3) | |
| for ax in (a, b): | |
| ax.spines[["top", "right"]].set_visible(False) | |
| ax.set_xticks(list(xs), [LABEL[k] for k in keys]) | |
| ax.grid(axis="y", color=GRID, linewidth=0.5, zorder=0) | |
| # (a) tokens per problem | |
| for x, k in zip(xs, keys): | |
| a.add_patch(Rectangle((x - 0.32, 0), 0.64, gen[k], facecolor=COL[k], edgecolor="none", zorder=2)) | |
| a.text(x, gen[k] + 700, f"{round(gen[k]):,}", ha="center", va="bottom", fontsize=7.6, fontweight="bold", color=INK) | |
| a.text(1, gen["plus1"] / 2, f"−{100 * d_plus:.1f}%", ha="center", va="center", fontsize=7.4, fontweight="bold", color="white", zorder=3) | |
| a.text(2, gen["minus1"] / 2, f"+{100 * d_minus:.1f}%", ha="center", va="center", fontsize=7.4, fontweight="bold", color="white", zorder=3) | |
| a.set_xlim(-0.6, 2.6) | |
| a.set_ylim(0, 50000) | |
| a.set_ylabel("Generated tokens per problem") | |
| a.yaxis.set_major_formatter(matplotlib.ticker.FuncFormatter(lambda v, _: "0" if v == 0 else f"{int(v / 1000)}k")) | |
| a.set_title("(a) Length", loc="left", fontsize=8.6, fontweight="bold", color=INK) | |
| # (b) correct answers: delivered naturally (solid) + recovered only by a forced answer (pale) | |
| for x, k in zip(xs, keys): | |
| b.add_patch(Rectangle((x - 0.32, 0), 0.64, nat[k], facecolor=COL[k], edgecolor="none", zorder=2)) | |
| if forced[k]: | |
| b.add_patch(Rectangle((x - 0.32, nat[k]), 0.64, forced[k], facecolor=COL[k], alpha=0.32, edgecolor="none", zorder=2)) | |
| b.text(x, nat[k] / 2, f"{nat[k]}", ha="center", va="center", fontsize=9, fontweight="bold", color="white", zorder=3) | |
| b.text(x, nat[k] + forced[k] + 0.8, f"{nat[k] + forced[k]} with forced", ha="center", va="bottom", fontsize=6.6, color=CAP) | |
| b.set_xticks(list(xs), [LABEL[k] + f"\nceiling hits: {capped[k]}" for k in keys]) | |
| b.set_xlim(-0.6, 2.6) | |
| b.set_ylim(0, 64) | |
| b.set_yticks([0, 16, 32, 48, 64]) | |
| b.set_ylabel("Correct of 64 problems") | |
| b.set_title("(b) Correct answers delivered", loc="left", fontsize=8.6, fontweight="bold", color=INK) | |
| out = HERE / "figures" / "figure2.pdf" | |
| out.parent.mkdir(exist_ok=True) | |
| fig.savefig(out, bbox_inches="tight", pad_inches=0.02, metadata={"CreationDate": None}) | |
| print("wrote", out, {k: round(gen[k]) for k in keys}, nat, forced, capped) | |