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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
|
#!/usr/bin/env python3
"""Analyze the paired overclamped CLLN scaling confirmation."""
from __future__ import annotations
import argparse
import csv
import json
from pathlib import Path
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
from analyze_coupled_ladder_scaling import (
PRIMARY_METRICS,
RESOURCE_METRICS,
bootstrap_mean,
merge_reports,
task_cluster_values,
task_size_matrix,
)
METHOD_ORDER = ("overclamp_clean", "overclamp", "overclamp_sdil")
DISPLAY = {
"overclamp_clean": "Clean overclamp",
"overclamp": "Imperfect overclamp",
"overclamp_sdil": "Overclamp + SDIL",
}
STYLE = {
"overclamp_clean": dict(color="#222222", marker="^", linestyle=":"),
"overclamp": dict(color="#E69F00", marker="s", linestyle="--"),
"overclamp_sdil": dict(color="#0072B2", marker="o", linestyle="-"),
}
FROZEN_SIZES = (4, 8, 12, 16, 24, 32)
FROZEN_DEVICE_SEEDS = (20260830, 20260831, 20260832)
FROZEN_LEARNING_TIMES = {
4: 0.01,
8: 0.01,
12: 0.03,
16: 0.03,
24: 0.03,
32: 0.01,
}
FROZEN_OVERCLAMP_TIMES = {
4: 0.25,
8: 0.25,
12: 0.25,
16: 2.5,
24: 2.5,
32: 2.5,
}
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument("--sources", type=Path, nargs="+", required=True)
parser.add_argument(
"--output-analysis",
type=Path,
default=Path("results/coupled_ladder/p3_overclamp_scaling_analysis.json"),
)
parser.add_argument(
"--output-csv",
type=Path,
default=Path("results/coupled_ladder/p3_overclamp_scaling_source.csv"),
)
parser.add_argument(
"--output-figure",
type=Path,
default=Path("results/figs/figure_clln_overclamp_scaling_confirmation"),
)
parser.add_argument("--bootstrap-replicates", type=int, default=20000)
parser.add_argument("--bootstrap-seed", type=int, default=20260829)
parser.add_argument("--confirmatory", action="store_true")
return parser.parse_args()
def validate_confirmation(reports: list[dict], records: list[dict]) -> None:
expected = {
(side, task, seed)
for side in FROZEN_SIZES
for task in range(40)
for seed in FROZEN_DEVICE_SEEDS
}
actual = {
(record["side"], record["task_index"], record["device_seed"])
for record in records
}
if actual != expected:
raise ValueError(
f"incomplete confirmation: {len(expected - actual)} missing, "
f"{len(actual - expected)} extra cells"
)
required = set(METHOD_ORDER)
observed_sizes = set()
for record in records:
if set(record["methods"]) != required:
raise ValueError("every cell must contain the three overclamp methods")
if any(value["status"] != "completed" for value in record["methods"].values()):
raise ValueError("all confirmatory methods must complete")
for report in reports:
protocol = report["protocol"]
if not report.get("confirmatory", False):
raise ValueError("every source must be labeled confirmatory")
if protocol["rotations_per_input_diameter"] != 8:
raise ValueError("confirmation requires all eight task rotations")
if protocol["epochs"] != 600:
raise ValueError("confirmation requires 600 epochs")
if tuple(protocol["device_seeds"]) != FROZEN_DEVICE_SEEDS:
raise ValueError("confirmation device seeds do not match")
if set(protocol["methods"]) != required:
raise ValueError("confirmation methods do not match")
for side in protocol["sizes"]:
observed_sizes.add(side)
learning_time = float(
protocol["learning_time_seconds_by_side"][str(side)]
)
overclamp_time = float(
protocol["overclamp_time_seconds_per_v_by_side"][str(side)]
)
if abs(learning_time - FROZEN_LEARNING_TIMES[side]) > 1e-15:
raise ValueError(f"side {side} uses an unfrozen learning exposure")
if abs(overclamp_time - FROZEN_OVERCLAMP_TIMES[side]) > 1e-15:
raise ValueError(f"side {side} uses an unfrozen overclamp exposure")
if observed_sizes != set(FROZEN_SIZES):
raise ValueError("sources do not cover all frozen sizes")
def slope_bootstrap(
records: list[dict],
sizes: list[int],
edges: np.ndarray,
metric: str,
replicates: int,
seed: int,
) -> dict:
matrices = {
method: task_size_matrix(records, sizes, method, metric)[1]
for method in METHOD_ORDER
}
if metric == "reached_stable_zero_error":
matrices = {method: 1.0 - values for method, values in matrices.items()}
x = np.log10(edges)
def slope(matrix: np.ndarray) -> float:
return float(np.polyfit(x, np.mean(matrix, axis=0), 1)[0])
raw_excess = matrices["overclamp"] - matrices["overclamp_clean"]
sdil_excess = matrices["overclamp_sdil"] - matrices["overclamp_clean"]
point_raw = slope(raw_excess)
point_sdil = slope(sdil_excess)
point_difference = point_raw - point_sdil
point_reduction = (
100.0 * point_difference / point_raw if point_raw > 0.0 else None
)
rng = np.random.default_rng(seed)
raw_slopes = []
sdil_slopes = []
differences = []
reductions = []
for _ in range(replicates):
sample = rng.integers(0, len(raw_excess), size=len(raw_excess))
raw_slope = slope(raw_excess[sample])
sdil_slope = slope(sdil_excess[sample])
raw_slopes.append(raw_slope)
sdil_slopes.append(sdil_slope)
differences.append(raw_slope - sdil_slope)
if raw_slope > 1e-12:
reductions.append(100.0 * (raw_slope - sdil_slope) / raw_slope)
def interval(values: list[float]) -> list[float]:
return [float(value) for value in np.percentile(values, (2.5, 97.5))]
return {
"x_axis": "log10 learnable edges",
"metric": metric,
"imperfect_overclamp_excess_slope": point_raw,
"imperfect_overclamp_excess_slope_95ci": interval(raw_slopes),
"overclamp_sdil_excess_slope": point_sdil,
"overclamp_sdil_excess_slope_95ci": interval(sdil_slopes),
"paired_slope_difference": point_difference,
"paired_slope_difference_95ci": interval(differences),
"relative_slope_reduction_percent": point_reduction,
"relative_slope_reduction_95ci": interval(reductions) if reductions else None,
}
def build_analysis(
records: list[dict], replicates: int, seed: int, confirmatory: bool
) -> dict:
sizes = sorted({record["side"] for record in records})
edges = np.asarray([
next(
record["learnable_edges"]
for record in records
if record["side"] == side
)
for side in sizes
])
rng = np.random.default_rng(seed)
summaries = {}
for side in sizes:
methods = {}
for method in METHOD_ORDER:
methods[method] = {}
for metric in PRIMARY_METRICS + RESOURCE_METRICS:
_, values = task_cluster_values(records, side, method, metric)
mean, interval = bootstrap_mean(values, rng, replicates)
methods[method][metric] = {
"mean": mean,
"task_bootstrap_95ci": interval,
}
clean = methods["overclamp_clean"]["classification_error"]["mean"]
raw = methods["overclamp"]["classification_error"]["mean"]
sdil = methods["overclamp_sdil"]["classification_error"]["mean"]
summaries[str(side)] = {
"learnable_edges": int(edges[sizes.index(side)]),
"methods": methods,
"raw_to_clean_gap_closed": (
float((raw - sdil) / (raw - clean))
if abs(raw - clean) > 1e-15
else None
),
}
return {
"analysis": "overclamped_digital_coupled_ladder_scaling",
"confirmatory": confirmatory,
"bootstrap": {
"unit": "task; three component draws averaged within task",
"task_clusters": len({record["task_index"] for record in records}),
"component_draws_per_task_size": len({
record["device_seed"] for record in records
}),
"replicates": replicates,
"seed": seed,
"interval": "percentile 95%",
},
"sizes": sizes,
"summaries": summaries,
"excess_error_scaling": slope_bootstrap(
records, sizes, edges, "classification_error", replicates, seed + 1
),
"excess_error_auc_scaling": slope_bootstrap(
records, sizes, edges, "classification_error_auc", replicates, seed + 2
),
"excess_stable_failure_scaling": slope_bootstrap(
records,
sizes,
edges,
"reached_stable_zero_error",
replicates,
seed + 3,
),
}
def write_csv(path: Path, analysis: dict) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
with path.open("w", newline="") as stream:
writer = csv.writer(stream)
writer.writerow((
"side", "learnable_edges", "method", "metric", "mean", "ci_low", "ci_high"
))
for side, summary in analysis["summaries"].items():
for method in METHOD_ORDER:
for metric, values in summary["methods"][method].items():
writer.writerow((
side,
summary["learnable_edges"],
method,
metric,
values["mean"],
values["task_bootstrap_95ci"][0],
values["task_bootstrap_95ci"][1],
))
def plot_figure(path: Path, analysis: dict) -> None:
mpl.rcParams.update({
"font.family": "DejaVu Sans",
"font.size": 8.5,
"axes.labelsize": 9,
"axes.titlesize": 9.5,
"legend.fontsize": 8,
"xtick.labelsize": 8,
"ytick.labelsize": 8,
"axes.spines.top": False,
"axes.spines.right": False,
"svg.fonttype": "none",
"pdf.fonttype": 42,
"figure.facecolor": "white",
"axes.facecolor": "white",
})
sizes = analysis["sizes"]
edges = np.asarray([
analysis["summaries"][str(side)]["learnable_edges"] for side in sizes
])
edge_labels = [
f"{value / 1000.0:.1f}k" if value >= 1000 else str(value)
for value in edges
]
panels = (
("classification_error", "Final classification error (%)", 100.0),
("classification_error_auc", "Classification-error AUC", 1.0),
("reached_stable_zero_error", "Stable failure fraction (%)", 100.0),
)
slope_keys = (
"excess_error_scaling",
"excess_error_auc_scaling",
"excess_stable_failure_scaling",
)
figure, axes = plt.subplots(1, 3, figsize=(10.6, 3.05))
for panel_index, (metric, ylabel, scale) in enumerate(panels):
axis = axes[panel_index]
for method in METHOD_ORDER:
intervals = np.asarray([
analysis["summaries"][str(side)]["methods"][method][metric][
"task_bootstrap_95ci"
]
for side in sizes
])
means = np.asarray([
analysis["summaries"][str(side)]["methods"][method][metric]["mean"]
for side in sizes
])
if metric == "reached_stable_zero_error":
means = 1.0 - means
intervals = np.column_stack((
1.0 - intervals[:, 1], 1.0 - intervals[:, 0]
))
means *= scale
intervals *= scale
axis.errorbar(
edges,
means,
yerr=np.vstack((means - intervals[:, 0], intervals[:, 1] - means)),
linewidth=1.7 if method == "overclamp_sdil" else 1.15,
markersize=4.5,
capsize=2.0,
label=DISPLAY[method],
**STYLE[method],
)
scaling = analysis[slope_keys[panel_index]]
reduction = scaling["relative_slope_reduction_percent"]
resolved = scaling["paired_slope_difference_95ci"][0] > 0.0
title = f"({chr(97 + panel_index)}) {ylabel.split(' (')[0]}"
if reduction is not None and resolved:
title += f"\n{reduction:.0f}% lower growth slope"
else:
title += "\nNo resolved slope reduction"
axis.set_title(title)
axis.set_xscale("log", base=2)
axis.set_xticks(edges, edge_labels)
axis.set_xlabel("Learnable edges")
axis.set_ylabel(ylabel)
axis.grid(axis="y", color="#D9D9D9", linewidth=0.55, alpha=0.8)
handles, labels = axes[0].get_legend_handles_labels()
figure.legend(
handles, labels, loc="upper center", ncol=3, frameon=False,
bbox_to_anchor=(0.5, 1.02),
)
evidence = "Confirmation" if analysis["confirmatory"] else "Exploratory"
figure.text(
0.995,
0.005,
f"{evidence}: {analysis['bootstrap']['task_clusters']} tasks, "
f"{analysis['bootstrap']['component_draws_per_task_size']} component draws",
ha="right",
va="bottom",
fontsize=7,
color="#666666",
)
figure.tight_layout(rect=(0.0, 0.04, 1.0, 0.92), w_pad=2.0)
path.parent.mkdir(parents=True, exist_ok=True)
figure.savefig(path.with_suffix(".svg"), bbox_inches="tight")
figure.savefig(path.with_suffix(".pdf"), bbox_inches="tight")
figure.savefig(path.with_suffix(".png"), dpi=240, bbox_inches="tight")
plt.close(figure)
def main() -> None:
args = parse_args()
reports = [json.loads(path.read_text()) for path in args.sources]
records = merge_reports(reports)
if args.confirmatory:
validate_confirmation(reports, records)
analysis = build_analysis(
records, args.bootstrap_replicates, args.bootstrap_seed, args.confirmatory
)
analysis["sources"] = [str(path) for path in args.sources]
args.output_analysis.parent.mkdir(parents=True, exist_ok=True)
args.output_analysis.write_text(json.dumps(analysis, indent=2) + "\n")
write_csv(args.output_csv, analysis)
plot_figure(args.output_figure, analysis)
print(json.dumps({key: analysis[key] for key in (
"excess_error_scaling",
"excess_error_auc_scaling",
"excess_stable_failure_scaling",
)}, indent=2))
if __name__ == "__main__":
main()
|