firstlight 0.1.0.dev0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
firstlight/replay.py ADDED
@@ -0,0 +1,104 @@
1
+ """Replay the paper's check table from the records in ``firstlight/data``."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import json
6
+ from dataclasses import dataclass
7
+ from pathlib import Path
8
+
9
+ from .check import calibrate
10
+
11
+ DATA = Path(__file__).parent / "data"
12
+ # The research records label builds PASS, WARN and FAIL; the docs call them correct,
13
+ # degraded and broken.
14
+ LABEL = {"PASS": "correct", "WARN": "degraded", "FAIL": "broken"}
15
+ # Apply the paper's label rule to the host simulation.
16
+ CORRECT_SHARE = 0.95
17
+
18
+
19
+ @dataclass(frozen=True)
20
+ class Misses:
21
+ """The result of one signal on one held-out set, at a threshold set on other builds.
22
+
23
+ Parameters
24
+ ----------
25
+ missed : int
26
+ The broken builds that pass.
27
+ broken : int
28
+ The broken builds in the set.
29
+ flagged : int
30
+ The correct builds that fail.
31
+ correct : int
32
+ The correct builds in the set.
33
+ """
34
+
35
+ missed: int
36
+ broken: int
37
+ flagged: int
38
+ correct: int
39
+
40
+
41
+ def _read(name: str) -> dict:
42
+ return json.loads((DATA / name).read_text())
43
+
44
+
45
+ def _misses(train: list[tuple[float, str]], test: list[tuple[float, str]]) -> Misses:
46
+ """Return the misses on ``test`` at the lowest score of a correct build in ``train``."""
47
+ threshold = calibrate(train)
48
+ return Misses(
49
+ missed=sum(s >= threshold for s, lab in test if lab == "broken"),
50
+ broken=sum(lab == "broken" for _, lab in test),
51
+ flagged=sum(s < threshold for s, lab in test if lab == "correct"),
52
+ correct=sum(lab == "correct" for _, lab in test),
53
+ )
54
+
55
+
56
+ def replay() -> dict[str, dict[str, Misses]]:
57
+ """Return the misses of each signal on the held-out phones and on the A100.
58
+
59
+ Each threshold is the lowest score of a correct build on the set that sets it: the first
60
+ phone set for the phones, the L40S builds for the A100. The host simulation needs no
61
+ threshold: a broken build passes it when its simulated graph is correct by the label rule.
62
+
63
+ Returns
64
+ -------
65
+ dict[str, dict[str, Misses]]
66
+ For each signal, the misses on ``phones`` and on ``a100``, where they are recorded.
67
+ """
68
+ first = _read("phone_first_set.json")["builds"]
69
+ held = _read("phone_held_out.json")["rows"]
70
+ gpu = [r for r in _read("gpu_builds.json")["rows"] if r.get("verdict")]
71
+ l40s = [r for r in gpu if r["gpu"] == "l40s"]
72
+ a100 = [r for r in gpu if r["gpu"] == "a100"]
73
+
74
+ def phones(train, test):
75
+ return _misses(
76
+ [(train(b), LABEL[b["label"]]) for b in first],
77
+ [(test(r), LABEL[r["label"]]) for r in held],
78
+ )
79
+
80
+ def gpus(key):
81
+ return _misses(
82
+ [(r[key], LABEL[r["verdict"]]) for r in l40s],
83
+ [(r[key], LABEL[r["verdict"]]) for r in a100],
84
+ )
85
+
86
+ sim = {(r["task"], r["prec"]): r["sim_exact_s8"] for r in _read("host_simulation.json")["rows"]}
87
+ broken = [r for r in held if LABEL[r["label"]] == "broken"]
88
+ simulated = [r for r in broken if (r["task"], r["prec"]) in sim]
89
+ passed = sum(sim[r["task"], r["prec"]] >= CORRECT_SHARE * r["host_exact_s8"] for r in simulated)
90
+ # The first set stores 1 - cosine and 1 - agreement; the held-out set stores the values.
91
+ return {
92
+ "hidden state, iteration 2": {
93
+ "phones": phones(lambda b: 1 - b["scores"]["cosdist_fp32_s2"], lambda r: r["phi_s2_H"]),
94
+ "a100": gpus("cos_l2"),
95
+ },
96
+ "hidden state, iteration 1": {"a100": gpus("cos_l1")},
97
+ "output agreement, iteration 2": {
98
+ "phones": phones(
99
+ lambda b: 1 - b["scores"]["pred_agree_s2"], lambda r: r["pred_agree_s2"]
100
+ ),
101
+ "a100": gpus("agree_loop2"),
102
+ },
103
+ "host simulation": {"phones": Misses(passed, len(simulated), 0, 0)},
104
+ }