summaryrefslogtreecommitdiff
path: root/experiments/bci_v2_calibrated_confirmation.py
blob: ae79708ae00cdfe1188ffb0a28f372499e5e0193 (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
#!/usr/bin/env python3
"""Run one untouched calibration-split oral-B-v2 confirmation cell."""
import argparse
import json
import os
import sys

sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from experiments.bci_v2_calibrated_run import (
    PROTOCOL_PATH,
    finite_tree,
    provenance,
    require_parent_gates,
    run_cell,
    sha256,
    source_paths,
)
from experiments.bci_v2_recovery_run import FIXED_CONFIG


ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
R1_GATE_PATH = os.path.join(
    ROOT, "results", "bci_v2_calibrated_dev_gate.json"
)
TASK_SEEDS = tuple(range(30, 36))
MODEL_SEEDS = tuple(range(5))


def require_r1_gate(r1):
    digests = {
        name: sha256(path)
        for name, path in source_paths().items()
    }
    if not (
        r1.get("protocol")
        == "oral_b_v2_calibrated_recovery_development_v1"
        and r1.get("status") == "passed"
        and r1.get("complete_grid") is True
        and r1.get("fixed_config") == FIXED_CONFIG
        and r1.get("confirmation_seeds_touched") is False
        and r1.get("calibrated_confirmation_opened") is True
        and r1.get("review_score_after") == 7
        and r1.get("input_sha256") == digests
    ):
        raise ValueError(
            "calibrated R2 requires the complete eligible R1 gate"
        )


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--task-seed", type=int, choices=TASK_SEEDS, required=True
    )
    parser.add_argument(
        "--model-seed", type=int, choices=MODEL_SEEDS, required=True
    )
    parser.add_argument(
        "--r1-gate",
        default="results/bci_v2_calibrated_dev_gate.json",
    )
    parser.add_argument(
        "--outdir", default="results/bci_v2_calibrated_confirmation"
    )
    args = parser.parse_args()
    require_parent_gates()
    with open(args.r1_gate) as handle:
        r1 = json.load(handle)
    require_r1_gate(r1)
    source = provenance({
        "development_runner": os.path.join(
            ROOT, "experiments", "bci_v2_calibrated_run.py"
        ),
        "runner": os.path.abspath(__file__),
        "r1_gate": os.path.abspath(args.r1_gate),
    })
    if (
        source["git_tracked_dirty"]
        or not all(source["tracked_inputs"].values())
    ):
        raise RuntimeError(
            "calibrated R2 requires clean, tracked, frozen inputs"
        )
    cell = run_cell(
        args.task_seed,
        args.model_seed,
        split="untouched_confirmation",
        performance_seed_offset=590_000,
        calibration_seed_offset=600_000,
        challenge_seed_offset=610_000,
    )
    result = {
        "schema_version": 4,
        "protocol": {
            "name":
                "oral_b_v2_calibrated_recovery_confirmation_v1",
            "split": "untouched_confirmation",
            "training_task_seed": args.task_seed,
            "model_seed": args.model_seed,
            "fixed_config": FIXED_CONFIG,
            "calibration_uses_outcome_labels": False,
            "no_further_selection": True,
            "confirmation_grid_size": (
                len(TASK_SEEDS) * len(MODEL_SEEDS)
            ),
            "protocol_sha256": sha256(PROTOCOL_PATH),
            "r1_gate_sha256": sha256(args.r1_gate),
        },
        "args": vars(args),
        "provenance": source,
        **cell,
    }
    result["finite"] = finite_tree(result)
    if not result["finite"]:
        raise RuntimeError("non-finite calibrated confirmation record")
    os.makedirs(args.outdir, exist_ok=True)
    path = os.path.join(
        args.outdir,
        (
            f"bci_v2_calibrated_confirm_t{args.task_seed}"
            f"_m{args.model_seed}.json"
        ),
    )
    if os.path.exists(path):
        raise FileExistsError(f"refusing to overwrite {path}")
    with open(path, "w") as handle:
        json.dump(result, handle, indent=2, sort_keys=True)
        handle.write("\n")
    print(json.dumps({
        "path": path,
        "intact_final": result["conditions"]["intact"]["final_success"],
        "challenge_success_fraction": result["signatures"][
            "challenge_success_fraction"
        ],
        "terminal_outcome_accuracy": result["signatures"][
            "terminal_residual_outcome_balanced_acc"
        ],
        "finite": result["finite"],
    }, indent=2))


if __name__ == "__main__":
    main()