summaryrefslogtreecommitdiff
path: root/experiments/kp_teaching_signal_ablation.py
blob: c636a030d5f7f5ee0bc4974e4eac954d338e546b (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
#!/usr/bin/env python3
"""Run the frozen raw-KP controls for the KTS-1 validation endpoint."""
import argparse
import hashlib
import json
import os
import subprocess
import sys


ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PROTOCOL = os.path.join(ROOT, "KP_TEACHING_SIGNAL_ABLATION.md")
PREREQUISITE = os.path.join(
    ROOT, "results", "kp_dynamic_projection_full_gate.json")
RULES = ("raw", "matched")


def sha256(path):
    digest = hashlib.sha256()
    with open(path, "rb") as handle:
        for chunk in iter(lambda: handle.read(1024 * 1024), b""):
            digest.update(chunk)
    return digest.hexdigest()


def git_output(*args):
    return subprocess.run(
        ["git", *args], cwd=ROOT, check=True, capture_output=True,
        text=True).stdout.strip()


def require_source():
    with open(PREREQUISITE) as handle:
        gate = json.load(handle)
    if not (
        gate.get("protocol") == "kp_dynamic_neutral_projection_full_v1"
        and gate.get("status") == "passed"
        and gate.get("independent_confirmation_opened") is True
    ):
        raise RuntimeError("KTS-1 requires the passed dynamic validation gate")
    required = [
        PROTOCOL,
        os.path.abspath(__file__),
        os.path.join(ROOT, "experiments", "analyze_kp_teaching_signal.py"),
        os.path.join(ROOT, "experiments", "conv_local_smoke.py"),
        os.path.join(ROOT, "experiments", "conv_run.py"),
        os.path.join(ROOT, "sdil", "conv.py"),
    ]
    for path in required:
        relative = os.path.relpath(path, ROOT)
        subprocess.run(
            ["git", "ls-files", "--error-unmatch", relative],
            cwd=ROOT, check=True, capture_output=True)
    if git_output("status", "--porcelain", "--untracked-files=no"):
        raise RuntimeError("KTS-1 requires a clean tracked source")
    return {
        "git_commit": git_output("rev-parse", "HEAD"),
        "protocol_sha256": sha256(PROTOCOL),
        "prerequisite_sha256": sha256(PREREQUISITE),
    }


def command(device, rule, path):
    return [
        sys.executable, "experiments/conv_run.py",
        "--device", device,
        "--depth", "20",
        "--width", "16",
        "--seed", "0",
        "--loader_seed", "0",
        "--batch_size", "128",
        "--epochs", "200",
        "--train_limit", "0",
        "--val_examples", "5000",
        "--split_seed", "2027",
        "--eval_split", "validation",
        "--eval_every", "0",
        "--augment_train", "1",
        "--lr_schedule", "step",
        "--lr_milestones", "100,150",
        "--lr_gamma", "0.1",
        "--warmup_epochs", "0",
        "--momentum", "0.9",
        "--weight_decay", "1e-4",
        "--normalization", "batchnorm",
        "--mode", "kp_traffic",
        "--lr", "0.1",
        "--output_lr", "0.1",
        "--learn_P", "1",
        "--eta_P", "0.1",
        "--predictor_mode", "closed_form",
        "--predictor_warmup_steps", "1",
        "--predictor_every", "0",
        "--neutral_projection", "1",
        "--traffic_rule", rule,
        "--traffic_ratio", "4.0",
        "--traffic_seed", "4000",
        "--traffic_calibration_examples", "64",
        "--alignment_probe", "32",
        "--out", path,
    ]


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--device", default="cuda")
    parser.add_argument("--rule", choices=("all",) + RULES, default="all")
    parser.add_argument(
        "--outdir", default="results/kp_teaching_signal_ablation")
    parser.add_argument("--dry-run", action="store_true")
    args = parser.parse_args()
    source = require_source()
    rules = RULES if args.rule == "all" else (args.rule,)
    os.makedirs(args.outdir, exist_ok=True)
    for rule in rules:
        path = os.path.join(args.outdir, f"{rule}.json")
        if os.path.exists(path):
            raise RuntimeError(f"refusing to overwrite frozen endpoint: {path}")
        cmd = command(args.device, rule, path)
        print(json.dumps({
            "rule": rule,
            "command": cmd,
            "source": source,
        }, sort_keys=True), flush=True)
        if not args.dry_run:
            subprocess.run(cmd, cwd=ROOT, check=True)


if __name__ == "__main__":
    main()