summaryrefslogtreecommitdiff
path: root/experiments/analyze_kp_dynamic_projection_short.py
blob: 8f338748a3505380a24b1a66a678c74138e23454 (plain)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/usr/bin/env python3
"""Audit the frozen D2 short dynamic-projection validation endpoint."""
import argparse
import json
import math
import os


SPLIT_HASH = "8328b206a97c420e49e54e3eca4abe3274c4756b084355784ea3fb8059e4515b"
BP_EPOCH20_MACS = 109_487_808_000_000
KP_SHORT_ACCURACY = 0.8266
MT1_ACCURACY = {"raw": 0.1, "matched": 0.1}


def numeric_leaves(value):
    if isinstance(value, bool) or value is None:
        return
    if isinstance(value, (int, float)):
        yield float(value)
    elif isinstance(value, dict):
        for child in value.values():
            yield from numeric_leaves(child)
    elif isinstance(value, (list, tuple)):
        for child in value:
            yield from numeric_leaves(child)


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--input", default="results/kp_dynamic_projection_short/dynamic.json")
    parser.add_argument(
        "--d1_gate", default="results/kp_dynamic_projection_gate.json")
    parser.add_argument(
        "--mt1_gate", default="results/kp_innovation_short_gate.json")
    parser.add_argument(
        "--out", default="results/kp_dynamic_projection_short_gate.json")
    args = parser.parse_args()
    with open(args.d1_gate) as handle:
        d1 = json.load(handle)
    if (d1.get("protocol") !=
            "kp_dynamic_neutral_projection_training_prefix_v1"
            or d1.get("status") != "passed"):
        raise ValueError("D2 requires the audited D1 pass")
    with open(args.mt1_gate) as handle:
        mt1 = json.load(handle)
    if (mt1.get("protocol") != "kp_mixed_traffic_short_v1"
            or mt1.get("status") != "failed"):
        raise ValueError("D2 requires the frozen failed MT-1 comparator")
    for rule, accuracy in MT1_ACCURACY.items():
        if float(mt1["metrics"]["accuracy"][rule]) != accuracy:
            raise ValueError(f"MT-1 {rule} endpoint drift")

    with open(args.input) as handle:
        record = json.load(handle)
    expected = {
        "mode": "kp_traffic", "traffic_rule": "innovation",
        "predictor_mode": "closed_form", "neutral_projection": 1,
        "depth": 20, "width": 16, "seed": 0, "loader_seed": 0,
        "batch_size": 128, "epochs": 20, "train_limit": 0,
        "val_examples": 5000, "split_seed": 2027,
        "eval_split": "validation", "eval_every": 0,
        "augment_train": 1, "lr": 0.1, "output_lr": 0.1,
        "lr_schedule": "step", "lr_milestones": "100,150",
        "lr_gamma": 0.1, "warmup_epochs": 0, "momentum": 0.9,
        "weight_decay": 1e-4, "normalization": "batchnorm",
        "a_scale": 1.0, "traffic_seed": 4000, "traffic_ratio": 4.0,
        "traffic_calibration_examples": 64, "learn_P": 1,
        "eta_P": 0.1, "predictor_warmup_steps": 1,
        "predictor_every": 0, "alignment_probe": 32,
    }
    for key, value in expected.items():
        if record["args"].get(key) != value:
            raise ValueError(f"D2 {key} drift")
    if record["provenance"]["git_tracked_dirty"]:
        raise ValueError("tracked-dirty D2 record")
    if record["split"]["validation_index_sha256"] != SPLIT_HASH:
        raise ValueError("D2 split drift")
    evaluation = record["evaluation_protocol"]
    if (evaluation["validation_evaluations"] != 1
            or evaluation["test_evaluations"] != 0
            or evaluation["test_used_for_selection"] is not False):
        raise ValueError("D2 evaluation boundary drift")
    if record.get("calibration_metric_space") != (
            "reciprocal_local_activity_products_with_mixed_apical_traffic"):
        raise ValueError("D2 metric-space drift")
    warmup = record.get("predictor_warmup", {})
    if (warmup.get("mode") != "closed_form"
            or warmup.get("steps") != 1
            or warmup.get("examples") != 64
            or warmup.get("instruction_present") is not False
            or warmup.get("task_loader_state_restored") is not True
            or warmup.get("reuses_traffic_calibration_forward") is not True):
        raise ValueError("D2 neutral slow-fit invariant failed")
    epochs = record["epochs"]
    if len(epochs) != 20 or any(row["epoch"] != index + 1
                                for index, row in enumerate(epochs)):
        raise ValueError("D2 epoch trajectory is incomplete")

    projection = [row.get("neutral_projection") for row in epochs]
    mixed = [row.get("mixed_apical") for row in epochs]
    tracking = [row.get("feedback_tracking") for row in epochs]
    if any(value is None for value in projection + mixed + tracking):
        raise ValueError("D2 audited trajectory is incomplete")
    final = record["final"]
    diagnostics = record["diagnostics"]
    accuracy = float(final["accuracy"])
    early = float(diagnostics["early_third_mean"])
    final_feedback = float(diagnostics["mean_feedback_forward_cosine"])
    late_feedback = sum(float(value["mean_feedback_forward_cosine"])
                        for value in tracking[10:]) / 10
    maximum_signal_ratio_error = max(abs(
        float(values["teaching_rms"])
        / max(float(values["instruction_rms"]), 1e-30) - 1.0)
        for values in mixed)
    maximum_post_ratio = max(float(value[
        "maximum_post_projection_traffic_rms_ratio"])
        for value in projection)
    maximum_post_slope = max(float(value[
        "maximum_absolute_post_projection_soma_slope"])
        for value in projection)
    maximum_pre_ratio = max(float(value[
        "maximum_pre_projection_traffic_rms_ratio"])
        for value in projection)
    instruction_observations = sum(int(value["instruction_observations"])
                                   for value in projection)
    initial_ratio_error = max(abs(float(value) - 4.0) for value in
                              record["traffic_calibration"][
                                  "realized_traffic_instruction_rms_ratio"])
    work = record["work"]
    counters = record["counters"]
    mac_ratio = float(work["total_macs_estimate"]) / BP_EPOCH20_MACS
    all_finite = bool(final["finite"]) and all(
        math.isfinite(value) for value in numeric_leaves({
            "final": final, "epochs": epochs, "diagnostics": diagnostics,
            "warmup": warmup, "traffic": record["traffic_calibration"],
            "work": work,
        }))

    checks = {
        "record_trajectory_and_diagnostics_finite": all_finite,
        "accuracy_at_least_0p80": accuracy >= 0.80,
        "within_3_points_of_clean_kp_short": (
            accuracy >= KP_SHORT_ACCURACY - 0.03),
        "at_least_70_points_above_mt1_raw_and_matched": all(
            accuracy - value >= 0.70 for value in MT1_ACCURACY.values()),
        "early_alignment_at_least_0p70": early >= 0.70,
        "final_feedback_cosine_at_least_0p80": final_feedback >= 0.80,
        "epoch11_to20_feedback_cosine_at_least_0p70": late_feedback >= 0.70,
        "used_instruction_rms_ratio_within_1e_minus_4": (
            maximum_signal_ratio_error <= 1e-4),
        "post_projection_traffic_ratio_at_most_1e_minus_5": (
            maximum_post_ratio <= 1e-5),
        "post_projection_soma_slope_at_most_1e_minus_5": (
            maximum_post_slope <= 1e-5),
        "zero_instruction_observations_in_fast_fit": (
            instruction_observations == 0),
        "one_frozen_64_example_slow_fit": (
            counters["predictor_update_examples"] == 64
            and counters["predictor_warmup_examples"] == 0
            and record["args"]["predictor_every"] == 0),
        "projection_observes_each_ordinary_example": (
            counters["neutral_projection_examples"]
            == counters["ordinary_examples"] == 900000),
        "initial_traffic_ratio_error_at_most_1e_minus_5": (
            initial_ratio_error <= 1e-5),
        "zero_task_loss_queries": work["logical_batch_loss_queries"] == 0,
        "macs_at_most_1p34x_bp": mac_ratio <= 1.34,
        "elementwise_and_neutral_cost_reported": (
            work["elementwise_operations_estimate"] > 0
            and work["neutral_projection_observations"] == 900000),
        "peak_allocated_memory_at_most_2p5_gib": (
            record["hardware"]["peak_memory_allocated_bytes"]
            <= int(2.5 * 1024 ** 3)),
        "one_validation_and_zero_test_evaluations": (
            evaluation["validation_evaluations"] == 1
            and evaluation["test_evaluations"] == 0),
    }
    passed = all(checks.values())
    output = {
        "protocol": "kp_dynamic_neutral_projection_short_v1",
        "status": "passed" if passed else "failed",
        "checks": checks,
        "metrics": {
            "accuracy": accuracy,
            "loss": float(final["loss"]),
            "clean_kp_short_accuracy": KP_SHORT_ACCURACY,
            "gain_over_mt1": {key: accuracy - value
                              for key, value in MT1_ACCURACY.items()},
            "early_third_alignment": early,
            "final_feedback_forward_cosine": final_feedback,
            "epoch11_to20_feedback_forward_cosine": late_feedback,
            "maximum_used_instruction_rms_ratio_error": (
                maximum_signal_ratio_error),
            "maximum_pre_projection_traffic_rms_ratio": maximum_pre_ratio,
            "maximum_post_projection_traffic_rms_ratio": maximum_post_ratio,
            "maximum_post_projection_soma_slope": maximum_post_slope,
            "total_macs": int(work["total_macs_estimate"]),
            "mac_ratio_to_bp": mac_ratio,
            "elementwise_operations_estimate": int(
                work["elementwise_operations_estimate"]),
            "peak_memory_allocated_bytes": int(record["hardware"][
                "peak_memory_allocated_bytes"]),
            "wall_s": float(record["timing"]["total_timed_wall_s"]),
            "source_commit": record["provenance"]["git_commit"],
        },
        "full_validation_opened": passed,
        "confirmation_test_seeds_touched": False,
        "review_score_before": 5,
        "review_score_after": 5,
        "score_change_rule": (
            "a one-seed short validation endpoint cannot raise the score"),
    }
    os.makedirs(os.path.dirname(os.path.abspath(args.out)), exist_ok=True)
    with open(args.out, "w") as handle:
        json.dump(output, handle, indent=2, sort_keys=True)
        handle.write("\n")
    print(json.dumps(output, indent=2))


if __name__ == "__main__":
    main()