File size: 7,415 Bytes
f36cc9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
"""Write the appendix summary table (one row per main intervention, generation and fixed-prefix effects side by side).

    python code/summary_table.py

Inputs (package files):
  stats/main256/late16_fraction_of_donor_saving.json, stats/main256/all32_fraction_of_donor_saving.json
      share of the donor's sampled-token saving recovered by the late and all-MLP transplants
  stats/audit/registered_draws_all_arms.json
      paired effects on the 256-question panel recomputed from the scored question files with the registered draws
      of confirm.py (code/registered_draws_all_arms.py): final-accuracy differences for every arm and the
      donor-saving share of the two compact edits
  stats/main256/reciprocal_four_cell.json             registered four-cell analysis (reverse hybrid)
  stats/mechanism/deep_prefix_kl.json                 deep-prefix KL fractions (48 traces, check set)
  stats/mechanism/early_prefix_kl_RESULTS_4.json      first-128-token KL fractions (256 questions)
  stats/mechanism/early_prefix_kl_RESULTS_5.json      the same analysis with the reverse arm
  stats/mechanism/joint_sparse_program_fitted256.json joint sparse program, early KL removed
  stats/mechanism/whole_update_geometry.json          changed BF16 entries for the full donor and all 96 MLP matrices
  stats/mechanism/late48_changed_entries.json         changed entries of the 48 late MLP matrices
Writes outputs/summary_table.tex.
"""
import json
from decimal import ROUND_HALF_UP, Decimal
from pathlib import Path

ROOT = Path(__file__).resolve().parent.parent


def load(rel):
    return json.loads((ROOT / rel).read_text())


def r1(v):
    """Round half away from zero to one decimal."""
    return Decimal(repr(v)).quantize(Decimal("0.1"), rounding=ROUND_HALF_UP)


def signed(v):
    d = r1(v)
    return ("0.0" if d == 0 else f"{d:+}").replace("-", "$-$")


def plain(v):
    return f"{r1(v)}".replace("-", "$-$")


def interval(point, lo, hi, fmt):
    return f"{fmt(point)} [{fmt(lo)}, {fmt(hi)}]"


def share(entry):
    return interval(100 * entry["point"], 100 * entry["ci95"][0], 100 * entry["ci95"][1], plain)


def share_draws(entry):
    return interval(100 * entry["point"], 100 * entry["ci"][0], 100 * entry["ci"][1], plain)


def final(effect):
    f = effect["final"]
    return interval(f["difference_pp"], *f["difference_pp_95ci"], signed)


def millions(count):
    return f"{r1(count / 1e6):,}"


def main():
    draws = load("stats/audit/registered_draws_all_arms.json")
    late_share = load("stats/main256/late16_fraction_of_donor_saving.json")["result"]["generated"]["edit_fraction_of_real_donor_saving"]
    full_share = load("stats/main256/all32_fraction_of_donor_saving.json")["result"]["generated"]["edit_fraction_of_real_donor_saving"]
    deep = load("stats/mechanism/deep_prefix_kl.json")["contrasts"]
    early = load("stats/mechanism/early_prefix_kl_RESULTS_4.json")["summary"]
    early5 = load("stats/mechanism/early_prefix_kl_RESULTS_5.json")["summary"]  # superset of RESULTS_4 with the reverse arm
    assert all(early5[k] == early[k] for k in early)
    # registered four-cell (parent/forward/reverse/donor) analysis, full_network_factorial REGISTRATION.json (18:02 UTC)
    factorial = load("stats/main256/reciprocal_four_cell.json")["all_questions"]["actual_sampled_tokens"]
    kept = factorial["fractions_of_full_training_gap"]["paired_ratios_of_means"]["remaining_gain_after_reciprocal_nonMLP_on_base_MLP"]
    joint = load("stats/mechanism/joint_sparse_program_fitted256.json")["groups"]["all"]["teacher_KL_removed"]
    geometry = load("stats/mechanism/whole_update_geometry.json")
    late_changed = sum(t["changed"] for t in load("stats/mechanism/late48_changed_entries.json")["tensors"].values())
    assert late_changed == 923_758_022
    assert abs(late_share["point"] - draws["late_fraction_generated"]["point"]) < 1e-12
    assert abs(full_share["point"] - draws["all32_fraction_generated"]["point"]) < 1e-12

    def pct(entry):
        return interval(100 * entry["point"], 100 * entry["ci95"][0], 100 * entry["ci95"][1], plain)

    rows = [
        ("Concise-9B (donor)", millions(geometry["all"]["changed"]), "100", final(draws["concise_vs_base"]), "100", "100"),
        ("Trained MLPs 0--31", millions(geometry["groups"]["mlp"]["changed"]), share(full_share),
         final(draws["all32_vs_base"]), pct(deep["base_mlp_all"]["check-all"]["fraction_kl_removed"]),
         pct(early["base_mlp_all"]["fraction_of_teacher_KL_removed"])),
        ("Trained MLPs 16--31", millions(late_changed), share(late_share), final(draws["late_vs_base"]),
         pct(deep["base_mlp_all_l16_31"]["check-all"]["fraction_kl_removed"]),
         pct(early["base_mlp_all_l16_31"]["fraction_of_teacher_KL_removed"])),
        ("Concise-9B, parent MLPs$^b$", millions(geometry["all"]["changed"] - geometry["groups"]["mlp"]["changed"]),
         share({"point": kept["estimate"], "ci95": kept["ci95"]}), final(draws["reverse_vs_base"]), "--",
         pct(early5["concise_restore_mlp_all"]["fraction_of_teacher_KL_removed"])),
        ("Joint sparse program$^c$", millions(8_184_586), "--", "--", "--", pct(joint)),
        ("Activation edit", millions(518_958), share_draws(draws["edit_fraction_generated"]), final(draws["edit_vs_base"]), "--", "--"),
        ("Random direction", "same$^a$", share_draws(draws["random_fraction_generated"]), final(draws["random_vs_base"]), "--", "--"),
    ]
    lines = [
        r"\begin{table}[tb]\centering\footnotesize",
        r"\caption{Main interventions and their measured effects. Changed weights are BF16 entries, in millions, that differ "
        r"from the parent. The generation columns compare each checkpoint with the parent on 256 held-out questions "
        r"(one sample per question, 32,768-token budget): the share of the donor's sampled-token saving that it "
        r"recovers, and its difference in final accuracy, with 95\% paired question-bootstrap intervals stratified by "
        r"source. KL removed is the share of donor-to-parent next-token divergence removed at shared parent prefixes, "
        r"deep in 48 reasoning traces and over the first 128 thinking tokens of the 256 questions (Section~\ref{sec:kl}). "
        r"Both shares are 100 by definition for the donor. A dash marks a test that was not run. "
        r"$^a$The same 128 columns as the activation edit, with a random direction of matched update norm. "
        r"$^b$Concise-9B with the parent's MLPs restored in all 32 layers; it keeps 23.4\% of the saving, so restoring the "
        r"MLPs undoes the other 76.6\% (Section~\ref{sec:transfer}). "
        r"$^c$64 fitted parent down-projection columns in each of the 32 layers (Section~\ref{sec:sparse}).}\label{tab:summary}",
        r"\setlength{\tabcolsep}{4.5pt}",
        r"\begin{tabular}{@{}lrllll@{}}\toprule",
        r"& Changed & \multicolumn{2}{c}{Generation, 256 questions} & \multicolumn{2}{c}{KL removed (\%)}\\"
        r"\cmidrule(lr){3-4}\cmidrule(l){5-6}",
        r"Intervention & weights (M) & Saving recovered (\%) & Final accuracy (pp) & Deep & Early\\\midrule",
    ]
    for row in rows:
        lines.append(" & ".join(row) + r"\\")
    lines += [r"\bottomrule\end{tabular}", r"\end{table}"]
    (ROOT / "outputs").mkdir(exist_ok=True)
    (ROOT / "outputs" / "summary_table.tex").write_text("\n".join(lines) + "\n")


if __name__ == "__main__":
    main()