Repository navigation
Expand file tree
/
Copy pathqualification.py
More file actions
363 lines (331 loc) · 13.3 KB
/
Copy pathqualification.py
File metadata and controls
363 lines (331 loc) · 13.3 KB
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
"""Validation-only qualification for the committed baseline recipes.
A literature-derived hyperparameter point is a strong candidate, not proof of a
global optimum. This module defines bounded candidate neighborhoods and the
selection/locking machinery needed to promote one candidate to the frozen
reference without consulting protected test metrics.
"""
from __future__ import annotations
from dataclasses import asdict, dataclass, replace
import hashlib
import json
import math
from pathlib import Path
from typing import Any, Iterable, Mapping, Sequence
import numpy as np
import pandas as pd
from .config import BaselineConfig
from .statistics import student_t_critical_95
from .vit_final import ViTBaselineConfig
@dataclass(frozen=True)
class QualificationCandidate:
baseline: str
optimizer: str
label: str
configuration: Mapping[str, Any]
candidate_id: str
is_committed_center: bool = False
def _candidate(
baseline: str,
optimizer: str,
label: str,
configuration: Mapping[str, Any],
*,
is_committed_center: bool = False,
) -> QualificationCandidate:
payload = {
"baseline": baseline,
"optimizer": optimizer,
"configuration": dict(configuration),
}
canonical = json.dumps(payload, sort_keys=True, separators=(",", ":"))
candidate_id = hashlib.sha256(canonical.encode("utf-8")).hexdigest()[:12]
return QualificationCandidate(
baseline=baseline,
optimizer=optimizer,
label=label,
configuration=dict(configuration),
candidate_id=candidate_id,
is_committed_center=bool(is_committed_center),
)
def mnist_candidates(optimizer: str) -> tuple[QualificationCandidate, ...]:
"""Return a bounded, source-centered MNIST candidate neighborhood."""
optimizer = str(optimizer)
base = BaselineConfig(optimizer=optimizer)
if optimizer == "sgd_momentum":
configs = (
("committed", base, True),
("lr_0p03", replace(base, sgd_learning_rate=0.03, sgd_min_learning_rate=3e-4), False),
("lr_0p08", replace(base, sgd_learning_rate=0.08, sgd_min_learning_rate=8e-4), False),
("no_decay", replace(base, sgd_weight_decay=0.0), False),
("wd_5e-4", replace(base, sgd_weight_decay=5e-4), False),
("warmup_1", replace(base, sgd_warmup_epochs=1), False),
)
elif optimizer == "adamw":
configs = (
("committed", base, True),
("lr_5e-4", replace(base, adamw_learning_rate=5e-4, adamw_min_learning_rate=5e-6), False),
("lr_2e-3", replace(base, adamw_learning_rate=2e-3, adamw_min_learning_rate=2e-5), False),
("wd_1e-3", replace(base, adamw_weight_decay=1e-3), False),
("no_decay", replace(base, adamw_weight_decay=0.0), False),
("no_warmup", replace(base, adamw_warmup_epochs=0), False),
)
elif optimizer == "sgd_momentum_muon":
configs = (
("committed", base, True),
("matrix_lr_0p01", replace(base, muon_learning_rate=0.01, muon_min_learning_rate=0.001), False),
("matrix_lr_0p04", replace(base, muon_learning_rate=0.04, muon_min_learning_rate=0.004), False),
("no_matrix_decay", replace(base, muon_weight_decay=0.0), False),
("aux_lr_1e-4", replace(base, muon_aux_learning_rate=1e-4, muon_aux_min_learning_rate=1e-5), False),
("aux_lr_6e-4", replace(base, muon_aux_learning_rate=6e-4, muon_aux_min_learning_rate=6e-5), False),
)
else:
raise ValueError(f"unknown MNIST optimizer: {optimizer!r}")
result = []
for label, config, center in configs:
config.validate()
result.append(
_candidate(
"mnist_mlp3",
optimizer,
label,
asdict(config),
is_committed_center=center,
)
)
return tuple(result)
def vit_candidates(optimizer: str) -> tuple[QualificationCandidate, ...]:
"""Return a bounded candidate neighborhood for the selected small ViT."""
optimizer = str(optimizer)
base = ViTBaselineConfig()
if optimizer == "sgd_momentum":
configs = (
("committed", base, True),
("lr_0p05", replace(base, sgd_lr=0.05, sgd_min_lr=5e-4), False),
("lr_0p20", replace(base, sgd_lr=0.20, sgd_min_lr=2e-3), False),
("wd_1e-3", replace(base, sgd_weight_decay=1e-3), False),
("warmup_start_1e-4", replace(base, sgd_warmup_start_lr=1e-4), False),
)
elif optimizer == "adamw":
configs = (
("committed", base, True),
("lr_2p5e-4", replace(base, adamw_lr=2.5e-4), False),
("lr_5e-4", replace(base, adamw_lr=5e-4), False),
("lr_1e-3", replace(base, adamw_lr=1e-3), False),
("wd_0p03", replace(base, adamw_weight_decay=0.03), False),
("wd_0p10", replace(base, adamw_weight_decay=0.10), False),
)
elif optimizer == "muon":
configs = (
("committed", base, True),
("matrix_lr_0p01", replace(base, muon_lr=0.01, muon_min_lr=0.001), False),
("matrix_lr_0p04", replace(base, muon_lr=0.04, muon_min_lr=0.004), False),
("no_matrix_decay", replace(base, muon_weight_decay=0.0), False),
("aux_lr_1e-4", replace(base, muon_aux_lr=1e-4, muon_aux_min_lr=1e-5), False),
("aux_lr_6e-4", replace(base, muon_aux_lr=6e-4, muon_aux_min_lr=6e-5), False),
)
else:
raise ValueError(f"unknown ViT optimizer: {optimizer!r}")
result = []
for label, config, center in configs:
config.validate()
result.append(
_candidate(
"cifar10_small_vit",
optimizer,
label,
asdict(config),
is_committed_center=center,
)
)
return tuple(result)
def one_head_profile_candidates(optimizer: str) -> tuple[QualificationCandidate, ...]:
"""Return optimizer-profile overrides around the one-head reference point."""
optimizer = str(optimizer)
if optimizer == "sgd_momentum":
overrides = (
("committed", {}, True),
("lr_0p03", {"learning_rate": 0.03, "min_learning_rate": 0.003}, False),
("lr_0p08", {"learning_rate": 0.08, "min_learning_rate": 0.008}, False),
("no_decay", {"weight_decay": 0.0}, False),
("warmup_0p05", {"warmup_fraction": 0.05}, False),
)
elif optimizer == "adamw":
overrides = (
("committed", {}, True),
("lr_3e-4", {"learning_rate": 3e-4, "min_learning_rate": 3e-5}, False),
("lr_1e-3", {"learning_rate": 1e-3, "min_learning_rate": 1e-4}, False),
("wd_0p05", {"weight_decay": 0.05}, False),
("wd_0p20", {"weight_decay": 0.20}, False),
)
elif optimizer == "muon":
overrides = (
("committed", {}, True),
("matrix_lr_0p01", {"matrix_learning_rate": 0.01, "matrix_min_learning_rate": 0.001}, False),
("matrix_lr_0p04", {"matrix_learning_rate": 0.04, "matrix_min_learning_rate": 0.004}, False),
("no_matrix_decay", {"matrix_weight_decay": 0.0}, False),
("aux_lr_1e-4", {"aux_learning_rate": 1e-4, "aux_min_learning_rate": 1e-5}, False),
("aux_lr_6e-4", {"aux_learning_rate": 6e-4, "aux_min_learning_rate": 6e-5}, False),
)
else:
raise ValueError(f"unknown one-head optimizer: {optimizer!r}")
return tuple(
_candidate(
"fineweb_one_head_nanogpt",
optimizer,
label,
override,
is_committed_center=center,
)
for label, override, center in overrides
)
def _mean_ci95(values: Iterable[float]) -> dict[str, float]:
array = np.asarray(list(values), dtype=float)
array = array[np.isfinite(array)]
n = int(array.size)
if n == 0:
return {
"n": 0,
"mean": np.nan,
"std": np.nan,
"ci95_half_width": np.nan,
"ci95_low": np.nan,
"ci95_high": np.nan,
}
mean = float(array.mean())
if n == 1:
return {
"n": 1,
"mean": mean,
"std": np.nan,
"ci95_half_width": np.nan,
"ci95_low": np.nan,
"ci95_high": np.nan,
}
std = float(array.std(ddof=1))
half = student_t_critical_95(n) * std / math.sqrt(n)
return {
"n": n,
"mean": mean,
"std": std,
"ci95_half_width": half,
"ci95_low": mean - half,
"ci95_high": mean + half,
}
def rank_validation_candidates(
history: pd.DataFrame,
*,
expected_seeds: Sequence[int] | None = None,
candidate_column: str = "candidate_id",
seed_column: str = "seed",
epoch_column: str = "epoch",
validation_loss_column: str = "validation_loss",
validation_accuracy_column: str = "validation_accuracy",
) -> tuple[pd.DataFrame, pd.DataFrame]:
"""Rank candidates using validation loss only.
One checkpoint is selected per candidate/seed by minimum validation loss,
with earlier epoch as the deterministic tie-break. Test columns may exist in
the input but are never read by this function.
"""
required = {
candidate_column,
seed_column,
epoch_column,
validation_loss_column,
}
missing = required.difference(history.columns)
if missing:
raise KeyError(f"qualification history is missing: {sorted(missing)}")
selected_rows = []
for (candidate_id, seed), run in history.groupby(
[candidate_column, seed_column], sort=True
):
candidates = run.dropna(subset=[validation_loss_column]).sort_values(
[validation_loss_column, epoch_column], ascending=[True, True]
)
if candidates.empty:
raise RuntimeError(
f"candidate={candidate_id}, seed={seed} has no finite validation loss"
)
selected_rows.append(candidates.iloc[0].copy())
selected = pd.DataFrame(selected_rows).reset_index(drop=True)
expected = (
tuple(sorted(int(seed) for seed in expected_seeds))
if expected_seeds is not None
else None
)
rows = []
for candidate_id, group in selected.groupby(candidate_column, sort=True):
observed = tuple(sorted(group[seed_column].astype(int).unique()))
if expected is not None and observed != expected:
raise RuntimeError(
f"candidate {candidate_id} has seeds {observed}, expected {expected}"
)
loss = _mean_ci95(group[validation_loss_column])
accuracy = (
_mean_ci95(group[validation_accuracy_column])
if validation_accuracy_column in group
else _mean_ci95([])
)
rows.append(
{
"candidate_id": candidate_id,
"n": loss["n"],
"mean_best_validation_loss": loss["mean"],
"validation_loss_std": loss["std"],
"validation_loss_ci95_half_width": loss["ci95_half_width"],
"validation_loss_ci95_low": loss["ci95_low"],
"validation_loss_ci95_high": loss["ci95_high"],
"mean_validation_accuracy_at_selection": accuracy["mean"],
"mean_selected_epoch": float(group[epoch_column].mean()),
}
)
leaderboard = pd.DataFrame(rows).sort_values(
[
"mean_best_validation_loss",
"mean_validation_accuracy_at_selection",
"candidate_id",
],
ascending=[True, False, True],
).reset_index(drop=True)
leaderboard.insert(0, "validation_rank", np.arange(1, len(leaderboard) + 1))
return leaderboard, selected
def freeze_winner(
path: str | Path,
*,
candidate: QualificationCandidate,
leaderboard: pd.DataFrame,
evidence_paths: Sequence[str | Path],
source_commit: str,
data_identity: Mapping[str, Any],
) -> Path:
"""Persist the exact validation-selected reference configuration."""
if leaderboard.empty:
raise ValueError("leaderboard is empty")
winner = str(leaderboard.iloc[0]["candidate_id"])
if winner != candidate.candidate_id:
raise ValueError(
f"candidate {candidate.candidate_id} is not leaderboard winner {winner}"
)
payload = {
"schema_version": 1,
"baseline": candidate.baseline,
"optimizer": candidate.optimizer,
"candidate_id": candidate.candidate_id,
"candidate_label": candidate.label,
"configuration": dict(candidate.configuration),
"source_commit": str(source_commit),
"data_identity": dict(data_identity),
"selection_metric": "minimum validation loss per run, averaged across complete runs",
"tie_break": "higher validation accuracy, then deterministic candidate id",
"protected_test_used_for_selection": False,
"evidence_paths": [str(Path(item)) for item in evidence_paths],
"leaderboard": leaderboard.to_dict("records"),
}
destination = Path(path)
destination.parent.mkdir(parents=True, exist_ok=True)
temporary = destination.with_suffix(destination.suffix + ".tmp")
temporary.write_text(
json.dumps(payload, indent=2, sort_keys=True), encoding="utf-8"
)
temporary.replace(destination)
return destination