File size: 42,148 Bytes
4a9362b | 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 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 | """Zero-science eager-runtime admission for the two final RGB producers.
The probe uses disposable fresh models and a bounded number of optimizer steps.
It compares the final all-TRAIN effective-batch route with the retained V13
objective, validates a serialized resume, and never opens a scientific fit.
CUDA graphs and ``torch.compile`` are intentionally outside this module.
"""
# ruff: noqa: ANN401 -- the retained RGB source and recipe protocols are dynamic
from __future__ import annotations
import argparse
import copy
import gc
import hashlib
import io
import json
import math
import os
import random
import tempfile
from collections.abc import Mapping, Sequence
from dataclasses import dataclass
from pathlib import Path
from typing import Any, cast
import numpy as np
import torch
from torch import Tensor, nn
from e_jepa_ttc.losses.causal_scale_ttc import CausalScaleTTCLossConfig
from e_jepa_ttc.training.causal_scale_eap import _autocast, _foreground_only_loss_config
from operational.final_refit.producer_plan import runtime_source_paths
from operational.final_refit.producers import (
BoundedRGBPrefetch,
ParallelRGBDecoder,
_final_memory_allowed,
build_rgb_recipe,
configure_rgb_runtime,
make_effective_batch,
producer_objective,
)
from operational.final_refit.rgb import AllTrainRGBSource, load_all_train_source
from operational.rgb_port.recipe import ProducerRecipe, file_sha256
from operational.rgb_port.train_producers import (
_producer_loss,
_resource_guard,
build_producer,
)
from operational.rgb_port_revision.loss import EffectiveBatch
SCHEMA = "final_refit_rgb_eager_runtime_admission_v1"
FIT_IDS = ("R_A5", "R_C2F")
REGIMES = {"foreground_warmup": 1, "full_objective": 4}
CASE_KINDS = ("T2", "T3", "MIXED_T2_T3")
DIAGNOSTIC_SHA256 = "13e37aba488fabde7c9b2f18cf317ba6af37def654f2446c3360b434fc71fd06"
CONTROL_POLICY = {
"schema": "final_refit_rgb_deterministic_transport_strict_policy_v1",
"diagnostic_sha256": DIAGNOSTIC_SHA256,
"strict_atol": 2e-5,
"strict_rtol": 2e-4,
"numerical_rule": "strict_candidate_reference_and_strict_reference_reference_control",
"exact_gates": [
"effective_batch",
"initial_state",
"rng",
"sampler_generator",
"serialized_resume",
],
"scope": "same_device_same_recipe_same_seed_same_case_bf16_eager_only",
}
CONTROL_POLICY_SHA256 = hashlib.sha256(
json.dumps(CONTROL_POLICY, sort_keys=True, separators=(",", ":")).encode("utf-8")
).hexdigest()
def _sha256(path: Path) -> str:
with path.resolve(strict=True).open("rb") as stream:
return hashlib.file_digest(stream, "sha256").hexdigest()
def _atomic_json(path: Path, value: Mapping[str, Any]) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
encoded = json.dumps(value, indent=2, sort_keys=True, allow_nan=False) + "\n"
descriptor, temporary = tempfile.mkstemp(prefix=f".{path.name}.", dir=path.parent)
try:
with os.fdopen(descriptor, "w", encoding="utf-8", newline="\n") as stream:
stream.write(encoded)
stream.flush()
os.fsync(stream.fileno())
os.replace(temporary, path)
finally:
Path(temporary).unlink(missing_ok=True)
@dataclass(frozen=True)
class CaseSelection:
"""Observed native rows used by one bounded runtime case."""
kind: str
indices: tuple[int, ...]
observed_keys: tuple[tuple[str, int], ...]
def select_observed_cases(source: Any, *, batch_size: int) -> tuple[CaseSelection, ...]:
"""Select deterministic observed T2, T3 and mixed rows without targets."""
if batch_size < 2 or batch_size > 8:
raise ValueError("Technical case batch size must be in [2,8]")
by_key: dict[tuple[str, int], list[int]] = {}
for index, raw in enumerate(source.batch_keys):
key = str(raw[0]), int(raw[1])
if key[1] in {2, 3}:
by_key.setdefault(key, []).append(index)
choices: dict[int, tuple[tuple[str, int], list[int]]] = {}
for frames in (2, 3):
matches = [
(key, values)
for key, values in sorted(by_key.items())
if key[1] == frames and len(values) >= batch_size
]
if not matches:
raise ValueError(f"No observed homogeneous T{frames} technical batch")
choices[frames] = matches[0]
half = batch_size // 2
other = batch_size - half
key2, rows2 = choices[2]
key3, rows3 = choices[3]
selections = (
CaseSelection("T2", tuple(rows2[:batch_size]), (key2,)),
CaseSelection("T3", tuple(rows3[:batch_size]), (key3,)),
CaseSelection(
"MIXED_T2_T3",
tuple(rows2[:half] + rows3[:other]),
tuple(sorted({key2, key3})),
),
)
if tuple(item.kind for item in selections) != CASE_KINDS:
raise AssertionError("Technical case coverage changed")
return selections
def _reference_batch(
source: AllTrainRGBSource, indices: Sequence[int], recipe: ProducerRecipe
) -> EffectiveBatch:
"""Build the retained V13 grouping independently of the final wrapper."""
groups: dict[tuple[str, int], list[int]] = {}
for index in indices:
groups.setdefault(source.batch_keys[int(index)], []).append(int(index))
parts = [
source.batch(group[start : start + recipe.microbatch_size], "rgb")
for _, group in sorted(groups.items())
for start in range(0, len(group), recipe.microbatch_size)
]
return EffectiveBatch(parts)
def _batch_tensors(batch: EffectiveBatch) -> dict[str, Tensor]:
result: dict[str, Tensor] = {}
names = (
"events",
"delta_t_s",
"target_ttc_s",
"boxes_xyxy",
"dinov3_relation_targets",
"dinov3_relation_valid",
)
for part_index, part in enumerate(batch.parts):
for name in names:
value = getattr(part, name)
if value is not None:
result[f"part{part_index}.{name}"] = value.detach().cpu()
return result
def _batch_identity(batch: EffectiveBatch) -> list[dict[str, Any]]:
return [
{
"sample_tokens": list(part.sample_tokens),
"sequence_ids": list(part.sequence_ids),
"track_ids": list(part.track_ids),
"shape": list(part.events.shape),
}
for part in batch.parts
]
def _same(left: Any, right: Any, *, atol: float = 0.0, rtol: float = 0.0) -> bool:
if isinstance(left, Tensor) and isinstance(right, Tensor):
if left.shape != right.shape or left.dtype != right.dtype:
return False
if left.is_floating_point() or left.is_complex():
if not bool(torch.isfinite(left).all()) or not bool(torch.isfinite(right).all()):
return False
return bool(torch.allclose(left, right, atol=atol, rtol=rtol))
return bool(torch.equal(left, right))
if isinstance(left, np.ndarray) and isinstance(right, np.ndarray):
return bool(np.array_equal(left, right))
if isinstance(left, Mapping) and isinstance(right, Mapping):
return tuple(left) == tuple(right) and all(
_same(left[key], right[key], atol=atol, rtol=rtol) for key in left
)
if isinstance(left, (tuple, list)) and isinstance(right, type(left)):
return len(left) == len(right) and all(
_same(a, b, atol=atol, rtol=rtol) for a, b in zip(left, right, strict=True)
)
if isinstance(left, (float, np.floating)):
if not isinstance(right, (float, np.floating)):
return False
return bool(math.isfinite(float(left)) and math.isfinite(float(right)) and left == right)
if isinstance(right, (float, np.floating)):
return False
return bool(left == right)
def _rng() -> dict[str, Any]:
return {
"python": random.getstate(),
"numpy": np.random.get_state(),
"cpu": torch.get_rng_state().clone(),
"cuda": [item.clone() for item in torch.cuda.get_rng_state_all()],
}
def _restore_rng(value: Mapping[str, Any]) -> None:
random.setstate(value["python"])
np.random.set_state(value["numpy"])
torch.set_rng_state(value["cpu"].cpu())
torch.cuda.set_rng_state_all([item.cpu() for item in value["cuda"]])
def _optimizer(model: nn.Module, recipe: ProducerRecipe) -> torch.optim.AdamW:
return torch.optim.AdamW(
model.parameters(),
lr=float(recipe.training_config["learning_rate"]),
weight_decay=float(recipe.training_config["weight_decay"]),
)
def _scheduler(optimizer: torch.optim.Optimizer, recipe: ProducerRecipe) -> Any:
return torch.optim.lr_scheduler.CosineAnnealingLR(
optimizer,
T_max=recipe.epochs,
eta_min=float(recipe.training_config["minimum_learning_rate"]),
)
def _reference_objective(
model: nn.Module, batch: EffectiveBatch, recipe: ProducerRecipe, epoch: int
) -> tuple[Tensor, Mapping[str, Tensor]]:
config = CausalScaleTTCLossConfig(**recipe.loss_config)
selected = _foreground_only_loss_config(config) if epoch <= 3 else config
return _producer_loss(cast(Any, model), batch, selected)
def _step(
model: nn.Module,
optimizer: torch.optim.Optimizer,
objective: Any,
*,
precision: str,
clip_norm: float,
) -> dict[str, Any]:
optimizer.zero_grad(set_to_none=True)
with _autocast(torch.device("cuda"), precision):
loss, components = objective(model)
if not bool(torch.isfinite(loss)):
raise FloatingPointError("Eager admission produced nonfinite loss")
loss.backward()
raw = {
name: value.grad.detach().clone()
for name, value in model.named_parameters()
if value.grad is not None
}
clip = torch.nn.utils.clip_grad_norm_(model.parameters(), clip_norm, error_if_nonfinite=True)
clipped = {
name: value.grad.detach().clone()
for name, value in model.named_parameters()
if value.grad is not None
}
optimizer.step()
torch.cuda.synchronize()
return {
"loss": loss.detach().clone(),
"components": {name: value.detach().clone() for name, value in components.items()},
"raw_gradients": raw,
"clip_norm": torch.as_tensor(clip).detach().clone(),
"clipped_gradients": clipped,
}
def _compare(left: Any, right: Any, *, atol: float, rtol: float) -> dict[str, Any]:
return {"passed": _same(left, right, atol=atol, rtol=rtol), "atol": atol, "rtol": rtol}
def _tensor_leaves(value: Any, prefix: str = "root") -> dict[str, Tensor]:
result: dict[str, Tensor] = {}
if isinstance(value, Tensor):
# Optimizer state mixes CPU step counters with CUDA moment tensors.
# Normalize diagnostic copies only; this never changes the training state.
result[prefix] = value.detach().float().cpu()
elif isinstance(value, Mapping):
for name, item in value.items():
result.update(_tensor_leaves(item, f"{prefix}.{name}"))
elif isinstance(value, (tuple, list)):
for index, item in enumerate(value):
result.update(_tensor_leaves(item, f"{prefix}.{index}"))
return result
def _aggregate_difference(left: Any, right: Any, *, atol: float, rtol: float) -> dict[str, Any]:
a, b = _tensor_leaves(left), _tensor_leaves(right)
if set(a) != set(b):
return {
"names_equal": False,
"original_allclose": False,
"max_abs": math.inf,
"relative_l2": math.inf,
}
deltas: list[Tensor] = []
left_values: list[Tensor] = []
right_values: list[Tensor] = []
original = True
finite = True
for name in sorted(a):
if a[name].shape != b[name].shape:
return {
"names_equal": True,
"shapes_equal": False,
"original_allclose": False,
"max_abs": math.inf,
"relative_l2": math.inf,
}
x, y = a[name].reshape(-1), b[name].reshape(-1)
finite = finite and bool(torch.isfinite(x).all()) and bool(torch.isfinite(y).all())
original = original and bool(torch.allclose(x, y, atol=atol, rtol=rtol))
if x.numel():
deltas.append(x - y)
left_values.append(x)
right_values.append(y)
if not deltas:
exact = _same(left, right, atol=atol, rtol=rtol)
return {
"names_equal": True,
"shapes_equal": True,
"original_allclose": exact,
"max_abs": 0.0 if exact else math.inf,
"relative_l2": 0.0 if exact else math.inf,
"finite": exact,
}
if not finite:
return {
"names_equal": True,
"shapes_equal": True,
"original_allclose": False,
"max_abs": math.inf,
"relative_l2": math.inf,
"finite": False,
}
delta = torch.cat(deltas)
joined_left, joined_right = torch.cat(left_values), torch.cat(right_values)
denominator = max(
float(torch.linalg.vector_norm(joined_left)),
float(torch.linalg.vector_norm(joined_right)),
1e-30,
)
return {
"names_equal": True,
"shapes_equal": True,
"original_allclose": original,
"max_abs": float(delta.abs().max()),
"relative_l2": float(torch.linalg.vector_norm(delta)) / denominator,
"finite": True,
}
def _controlled_compare(
candidate: Any,
reference: Any,
control_left: Any,
control_right: Any,
*,
atol: float,
rtol: float,
) -> dict[str, Any]:
"""Require strict parity on both candidate/reference and the informative A/A control."""
observed = _aggregate_difference(candidate, reference, atol=atol, rtol=rtol)
control = _aggregate_difference(control_left, control_right, atol=atol, rtol=rtol)
controlled = (
bool(observed.get("names_equal"))
and bool(observed.get("shapes_equal", True))
and bool(observed.get("finite"))
and bool(control.get("names_equal"))
and bool(control.get("shapes_equal", True))
and bool(control.get("finite"))
and math.isfinite(float(observed["max_abs"]))
and math.isfinite(float(observed["relative_l2"]))
and math.isfinite(float(control["max_abs"]))
and math.isfinite(float(control["relative_l2"]))
and float(observed["max_abs"]) <= float(control["max_abs"])
and float(observed["relative_l2"]) <= float(control["relative_l2"])
)
return {
"passed": bool(observed["original_allclose"]) and bool(control["original_allclose"]),
"strict_original_threshold_passed": bool(observed["original_allclose"]),
"strict_control_threshold_passed": bool(control["original_allclose"]),
"control_dominance_passed": controlled,
"candidate_reference": observed,
"reference_reference_control": control,
"policy": CONTROL_POLICY["numerical_rule"],
"atol": atol,
"rtol": rtol,
}
def _recompute_reported_numerical_pass(value: Mapping[str, Any]) -> bool:
"""Recompute a serialized comparison decision instead of trusting its flags."""
observed = value.get("candidate_reference", {})
control = value.get("reference_reference_control", {})
if not isinstance(observed, Mapping) or not isinstance(control, Mapping):
return False
try:
metrics = tuple(
float(item[name]) for item in (observed, control) for name in ("max_abs", "relative_l2")
)
except (KeyError, TypeError, ValueError):
return False
valid = all(
item.get("names_equal") is True
and item.get("shapes_equal", True) is True
and item.get("finite") is True
for item in (observed, control)
) and all(math.isfinite(metric) for metric in metrics)
controlled = valid and metrics[0] <= metrics[2] and metrics[1] <= metrics[3]
strict = (
value.get("strict_original_threshold_passed") is True
and value.get("strict_control_threshold_passed") is True
and valid
)
recomputed = strict
return (
value.get("control_dominance_passed") is controlled
and value.get("passed") is recomputed
and recomputed
)
def _serialize_resume(
model: nn.Module,
optimizer: torch.optim.Optimizer,
scheduler: Any,
generator: torch.Generator,
*,
recipe: ProducerRecipe,
cursor: Mapping[str, int],
) -> tuple[nn.Module, torch.optim.Optimizer, Any, torch.Generator, dict[str, int]]:
payload = {
"model": model.state_dict(),
"optimizer": optimizer.state_dict(),
"scheduler": scheduler.state_dict(),
"generator": generator.get_state(),
"rng": _rng(),
"cursor": dict(cursor),
}
buffer = io.BytesIO()
torch.save(payload, buffer)
buffer.seek(0)
loaded = torch.load(buffer, map_location="cuda", weights_only=False)
restored_model = build_producer(recipe).cuda().train()
restored_optimizer = _optimizer(restored_model, recipe)
restored_scheduler = _scheduler(restored_optimizer, recipe)
restored_generator = torch.Generator().manual_seed(0)
restored_model.load_state_dict(loaded["model"])
restored_optimizer.load_state_dict(loaded["optimizer"])
restored_scheduler.load_state_dict(loaded["scheduler"])
restored_generator.set_state(loaded["generator"].cpu())
_restore_rng(loaded["rng"])
return (
restored_model,
restored_optimizer,
restored_scheduler,
restored_generator,
dict(loaded["cursor"]),
)
def _run_case(
*,
source: AllTrainRGBSource,
recipe: ProducerRecipe,
selection: CaseSelection,
regime: str,
updates: int,
) -> dict[str, Any]:
epoch = REGIMES[regime]
candidate_host = make_effective_batch(source, selection.indices, recipe)
reference_host = _reference_batch(source, selection.indices, recipe)
batch_parity = _same(_batch_tensors(candidate_host), _batch_tensors(reference_host)) and _same(
_batch_identity(candidate_host), _batch_identity(reference_host)
)
candidate_batch = candidate_host.to(torch.device("cuda"))
reference_batch = reference_host.to(torch.device("cuda"))
torch.manual_seed(recipe.seed)
torch.cuda.manual_seed_all(recipe.seed)
candidate = build_producer(recipe).cuda().train()
initial = copy.deepcopy(candidate.state_dict())
reference = build_producer(recipe).cuda().train()
reference.load_state_dict(initial)
control_left = build_producer(recipe).cuda().train()
control_left.load_state_dict(initial)
control_right = build_producer(recipe).cuda().train()
control_right.load_state_dict(initial)
initial_state_equal = all(
_same(candidate.state_dict(), model.state_dict())
for model in (reference, control_left, control_right)
)
candidate_optimizer = _optimizer(candidate, recipe)
reference_optimizer = _optimizer(reference, recipe)
control_left_optimizer = _optimizer(control_left, recipe)
control_right_optimizer = _optimizer(control_right, recipe)
candidate_scheduler = _scheduler(candidate_optimizer, recipe)
reference_scheduler = _scheduler(reference_optimizer, recipe)
control_left_scheduler = _scheduler(control_left_optimizer, recipe)
control_right_scheduler = _scheduler(control_right_optimizer, recipe)
candidate_generator = torch.Generator().manual_seed(recipe.seed)
reference_generator = torch.Generator().manual_seed(recipe.seed)
control_left_generator = torch.Generator().manual_seed(recipe.seed)
control_right_generator = torch.Generator().manual_seed(recipe.seed)
precision = str(recipe.training_config["precision"])
clip_norm = float(recipe.training_config["grad_clip_norm"])
tolerance = {"atol": 2e-5, "rtol": 2e-4}
comparisons: list[dict[str, Any]] = []
resume_exact = False
cursor = {"epoch": epoch, "technical_update": 0}
for update in range(updates):
state = _rng()
candidate_result = _step(
candidate,
candidate_optimizer,
lambda model: producer_objective(model, candidate_batch, recipe, epoch),
precision=precision,
clip_norm=clip_norm,
)
candidate_rng = _rng()
_restore_rng(state)
reference_result = _step(
reference,
reference_optimizer,
lambda model: _reference_objective(model, reference_batch, recipe, epoch),
precision=precision,
clip_norm=clip_norm,
)
reference_rng = _rng()
_restore_rng(state)
control_left_result = _step(
control_left,
control_left_optimizer,
lambda model: _reference_objective(model, reference_batch, recipe, epoch),
precision=precision,
clip_norm=clip_norm,
)
control_left_rng = _rng()
_restore_rng(state)
control_right_result = _step(
control_right,
control_right_optimizer,
lambda model: _reference_objective(model, reference_batch, recipe, epoch),
precision=precision,
clip_norm=clip_norm,
)
control_right_rng = _rng()
torch.randperm(len(selection.indices), generator=candidate_generator)
torch.randperm(len(selection.indices), generator=reference_generator)
torch.randperm(len(selection.indices), generator=control_left_generator)
torch.randperm(len(selection.indices), generator=control_right_generator)
cursor["technical_update"] = update + 1
comparisons.append(
{
"update": update + 1,
"loss": _controlled_compare(
candidate_result["loss"],
reference_result["loss"],
control_left_result["loss"],
control_right_result["loss"],
**tolerance,
),
"components": _controlled_compare(
candidate_result["components"],
reference_result["components"],
control_left_result["components"],
control_right_result["components"],
**tolerance,
),
"raw_gradients": _controlled_compare(
candidate_result["raw_gradients"],
reference_result["raw_gradients"],
control_left_result["raw_gradients"],
control_right_result["raw_gradients"],
**tolerance,
),
"clip_norm": _controlled_compare(
candidate_result["clip_norm"],
reference_result["clip_norm"],
control_left_result["clip_norm"],
control_right_result["clip_norm"],
**tolerance,
),
"clipped_gradients": _controlled_compare(
candidate_result["clipped_gradients"],
reference_result["clipped_gradients"],
control_left_result["clipped_gradients"],
control_right_result["clipped_gradients"],
**tolerance,
),
"model_after_step": _controlled_compare(
candidate.state_dict(),
reference.state_dict(),
control_left.state_dict(),
control_right.state_dict(),
**tolerance,
),
"optimizer_after_step": _controlled_compare(
candidate_optimizer.state_dict(),
reference_optimizer.state_dict(),
control_left_optimizer.state_dict(),
control_right_optimizer.state_dict(),
**tolerance,
),
"scheduler_after_step": _controlled_compare(
candidate_scheduler.state_dict(),
reference_scheduler.state_dict(),
control_left_scheduler.state_dict(),
control_right_scheduler.state_dict(),
**tolerance,
),
"rng_equal": all(
_same(candidate_rng, value)
for value in (reference_rng, control_left_rng, control_right_rng)
),
"sampler_generator_equal": all(
torch.equal(candidate_generator.get_state(), value.get_state())
for value in (
reference_generator,
control_left_generator,
control_right_generator,
)
),
}
)
if update == 0:
before = {
"model": copy.deepcopy(candidate.state_dict()),
"optimizer": copy.deepcopy(candidate_optimizer.state_dict()),
"scheduler": copy.deepcopy(candidate_scheduler.state_dict()),
"generator": candidate_generator.get_state().clone(),
"rng": _rng(),
"cursor": dict(cursor),
}
restored = _serialize_resume(
candidate,
candidate_optimizer,
candidate_scheduler,
candidate_generator,
recipe=recipe,
cursor=cursor,
)
del candidate, candidate_optimizer, candidate_scheduler, candidate_generator
candidate, candidate_optimizer, candidate_scheduler, candidate_generator, cursor = (
restored
)
resume_exact = (
_same(candidate.state_dict(), before["model"])
and _same(candidate_optimizer.state_dict(), before["optimizer"])
and _same(candidate_scheduler.state_dict(), before["scheduler"])
and torch.equal(candidate_generator.get_state(), before["generator"])
and _same(_rng(), before["rng"])
and cursor == before["cursor"]
)
fields = (
"loss",
"components",
"raw_gradients",
"clip_norm",
"clipped_gradients",
"model_after_step",
"optimizer_after_step",
"scheduler_after_step",
)
passed = (
batch_parity
and initial_state_equal
and resume_exact
and all(
all(bool(item[field]["passed"]) for field in fields)
and bool(item["rng_equal"])
and bool(item["sampler_generator_equal"])
for item in comparisons
)
)
return {
"fit_id": recipe.fit_id,
"regime": regime,
"epoch": epoch,
"case_kind": selection.kind,
"indices": list(selection.indices),
"observed_keys": [list(item) for item in selection.observed_keys],
"batch_parity": batch_parity,
"initial_state_equal": initial_state_equal,
"resume_exact": resume_exact,
"seed": recipe.seed,
"recipe_identity_sha256": recipe.identity_sha256,
"comparisons": comparisons,
"candidate_technical_optimizer_steps": updates,
"reference_technical_optimizer_steps": updates,
"control_technical_optimizer_steps": 2 * updates,
"control_policy_sha256": CONTROL_POLICY_SHA256,
"scientific_updates": 0,
"passed": passed,
}
def _runtime_route_admission(
source: AllTrainRGBSource, recipes: Mapping[str, ProducerRecipe]
) -> dict[str, Any]:
"""Exercise the production decoder/prefetch and one full-B32 forward per arm."""
by_key: dict[tuple[str, int], list[int]] = {}
for index, raw in enumerate(source.batch_keys):
by_key.setdefault((str(raw[0]), int(raw[1])), []).append(index)
eligible = [
(key, rows)
for key, rows in sorted(by_key.items(), key=lambda item: (-item[0][1], item[0][0]))
if len(rows) >= 32
]
if not eligible:
raise ValueError("Runtime route admission requires an observed physical B32 batch")
admitted_key, admitted_rows = eligible[0]
indices = tuple(admitted_rows[:32])
selected = set(indices)
order = torch.tensor(
[*indices, *(index for index in range(source.population_size) if index not in selected)],
dtype=torch.int64,
)
reference = make_effective_batch(source, indices, recipes["R_A5"])
cursor: dict[str, Any] = {"order": order, "position": 0}
decoder = ParallelRGBDecoder(source, workers=4, max_pending_bytes=128 * 1024**2)
prefetch = BoundedRGBPrefetch(source, recipes["R_A5"], cursor, depth=2)
try:
actual = prefetch.get(indices)
batch_parity = _same(_batch_tensors(actual), _batch_tensors(reference)) and _same(
_batch_identity(actual), _batch_identity(reference)
)
device_batch = actual.to(torch.device("cuda"))
torch.cuda.reset_peak_memory_stats()
forwards: dict[str, Any] = {}
for fit_id, recipe in recipes.items():
torch.manual_seed(recipe.seed)
torch.cuda.manual_seed_all(recipe.seed)
model = build_producer(recipe).cuda().train()
with (
torch.no_grad(),
_autocast(torch.device("cuda"), str(recipe.training_config["precision"])),
):
loss, components = producer_objective(model, device_batch, recipe, 4)
torch.cuda.synchronize()
finite = bool(torch.isfinite(loss)) and all(
bool(torch.isfinite(value).all()) for value in components.values()
)
forwards[fit_id] = {
"finite": finite,
"loss": float(loss.detach().float().cpu()),
"component_names": sorted(components),
}
del model, loss, components
torch.cuda.empty_cache()
peak = int(torch.cuda.max_memory_allocated())
passed = batch_parity and all(item["finite"] for item in forwards.values())
return {
"passed": passed,
"effective_batch_size": 32,
"physical_batch_key": list(admitted_key),
"physical_part_shapes": [list(part.events.shape) for part in actual.parts],
"indices": list(indices),
"batch_parity": batch_parity,
"decoder": decoder.snapshot(),
"prefetch": prefetch.snapshot(),
"forward_only": forwards,
"technical_forward_only_passes": len(forwards),
"technical_optimizer_steps": 0,
"scientific_updates": 0,
"peak_cuda_memory_bytes": peak,
"scope": "full_B32_decode_prefetch_forward_only_no_backward",
}
finally:
prefetch.close()
decoder.close()
torch.cuda.empty_cache()
gc.collect()
def validate_receipt(payload: Mapping[str, Any]) -> None:
"""Fail closed unless every arm/regime/observed-time case passed."""
if (
payload.get("schema") != SCHEMA
or payload.get("status") != "PASS"
or payload.get("scientific_updates") != 0
):
raise ValueError("Eager admission schema or science accounting differs")
cases = payload.get("cases", [])
expected = {
(fit_id, regime, kind) for fit_id in FIT_IDS for regime in REGIMES for kind in CASE_KINDS
}
observed = {(item.get("fit_id"), item.get("regime"), item.get("case_kind")) for item in cases}
if observed != expected or len(cases) != len(expected):
raise ValueError("Eager admission case coverage or parity failed")
fields = (
"loss",
"components",
"raw_gradients",
"clip_norm",
"clipped_gradients",
"model_after_step",
"optimizer_after_step",
"scheduler_after_step",
)
expected_updates = int(payload.get("updates_per_case_per_path", 0))
if expected_updates not in {2, 3}:
raise ValueError("Eager admission update count differs")
for case in cases:
comparisons = case.get("comparisons", [])
exact = (
case.get("batch_parity") is True
and case.get("initial_state_equal") is True
and case.get("resume_exact") is True
and case.get("scientific_updates") == 0
and len(comparisons) == expected_updates
and case.get("candidate_technical_optimizer_steps") == expected_updates
and case.get("reference_technical_optimizer_steps") == expected_updates
and case.get("control_technical_optimizer_steps") == 2 * expected_updates
)
numerical = all(
item.get("update") == index
and item.get("rng_equal") is True
and item.get("sampler_generator_equal") is True
and all(
isinstance(item.get(field), Mapping)
and item[field].get("policy") == CONTROL_POLICY["numerical_rule"]
and _recompute_reported_numerical_pass(item[field])
for field in fields
)
for index, item in enumerate(comparisons, start=1)
)
if not exact or not numerical or case.get("passed") is not True:
raise ValueError("Eager admission case exact or numerical gate failed")
if payload.get("technical_optimizer_steps") != sum(
int(item["candidate_technical_optimizer_steps"])
+ int(item["reference_technical_optimizer_steps"])
+ int(item["control_technical_optimizer_steps"])
for item in cases
):
raise ValueError("Eager admission technical accounting differs")
if payload.get("control_policy") != CONTROL_POLICY:
raise ValueError("Eager admission numerical control policy object differs")
if payload.get("control_policy_sha256") != CONTROL_POLICY_SHA256 or any(
item.get("control_policy_sha256") != CONTROL_POLICY_SHA256 for item in cases
):
raise ValueError("Eager admission numerical control policy differs")
runtime_route = payload.get("runtime_route", {})
if (
payload.get("runtime_admission") is not True
or payload.get("producer_plan_runtime_admission") != "PASS"
or runtime_route.get("passed") is not True
or runtime_route.get("effective_batch_size") != 32
or runtime_route.get("technical_optimizer_steps") != 0
or runtime_route.get("scientific_updates") != 0
or runtime_route.get("technical_forward_only_passes") != len(FIT_IDS)
):
raise ValueError("Production decoder/prefetch B32 forward admission failed")
def _executed_hashes(repository: Path) -> dict[str, str]:
paths = {
"operational/final_refit/eager_probe.py": Path(__file__).resolve(),
**runtime_source_paths(repository),
"configs/rgb_port/producers.json": repository / "configs/rgb_port/producers.json",
}
return {name: _sha256(path) for name, path in paths.items()}
def run(args: argparse.Namespace) -> dict[str, Any]:
"""Execute bounded disposable eager cases and persist progress after each."""
runtime_settings = configure_rgb_runtime()
if args.updates_per_case not in {2, 3}:
raise ValueError("updates-per-case must be 2 or 3")
repository = Path(__file__).resolve().parents[2]
descriptor = args.source_descriptor.resolve(strict=True)
teacher = args.teacher_manifest.resolve(strict=True)
config = args.config.resolve(strict=True)
allowed, resources = _resource_guard(
args.output.resolve().parent, checkpoint_reservation=512 * 1024**2
)
if not _final_memory_allowed(allowed, resources):
return {
"schema": SCHEMA,
"status": "RESOURCE_BLOCKED",
"scientific_updates": 0,
"technical_optimizer_steps": 0,
"technical_forward_only_passes": 0,
"cases": [],
"resources": resources,
"runtime_admission": False,
}
if not torch.cuda.is_available():
raise RuntimeError("Eager CUDA admission requested but CUDA is unavailable")
source = load_all_train_source(descriptor, teacher)
cases: list[dict[str, Any]] = []
technical = 0
runtime_route: dict[str, Any] | None = None
try:
selections = select_observed_cases(source, batch_size=args.case_batch_size)
recipes = {
fit_id: build_rgb_recipe(config, fit_id, descriptor, source, microbatch_size=32)
for fit_id in FIT_IDS
}
base: dict[str, Any] = {
"schema": SCHEMA,
"runtime_settings": runtime_settings,
"status": "RUNNING",
"backend": "eager_bf16_no_compile_no_graphs",
"scientific_updates": 0,
"technical_optimizer_steps": 0,
"technical_forward_only_passes": 0,
"updates_per_case_per_path": args.updates_per_case,
"control_policy": CONTROL_POLICY,
"control_policy_sha256": CONTROL_POLICY_SHA256,
"source_descriptor_sha256": file_sha256(descriptor),
"teacher_manifest_sha256": file_sha256(teacher),
"source_identity": dict(source.identity),
"executed_source_sha256": _executed_hashes(repository),
"resources": resources,
"cases": cases,
"runtime_admission": False,
}
_atomic_json(args.output.resolve(), base)
runtime_route = _runtime_route_admission(source, recipes)
_atomic_json(
args.output.resolve(),
{
**base,
"technical_forward_only_passes": runtime_route["technical_forward_only_passes"],
"runtime_route": runtime_route,
},
)
if not runtime_route["passed"]:
return {
**base,
"status": "FAILED",
"technical_forward_only_passes": runtime_route["technical_forward_only_passes"],
"runtime_route": runtime_route,
"runtime_admission": False,
"failure": "Production decoder/prefetch B32 route parity failed",
}
for fit_id in FIT_IDS:
recipe = recipes[fit_id]
for regime in REGIMES:
for selection in selections:
result = _run_case(
source=source,
recipe=recipe,
selection=selection,
regime=regime,
updates=args.updates_per_case,
)
cases.append(result)
technical += 4 * args.updates_per_case
_atomic_json(
args.output.resolve(),
{
**base,
"technical_optimizer_steps": technical,
"technical_forward_only_passes": runtime_route[
"technical_forward_only_passes"
],
"runtime_route": runtime_route,
"cases": cases,
},
)
torch.cuda.empty_cache()
gc.collect()
payload = {
**base,
"status": "PASS",
"technical_optimizer_steps": technical,
"technical_forward_only_passes": runtime_route["technical_forward_only_passes"],
"runtime_route": runtime_route,
"cases": cases,
"runtime_admission": True,
"producer_plan_runtime_admission": "PASS",
}
try:
validate_receipt(payload)
except ValueError as error:
return {
**payload,
"status": "FAILED",
"runtime_admission": False,
"producer_plan_runtime_admission": "FAILED",
"failure": str(error),
}
return payload
except MemoryError as error:
return {
"schema": SCHEMA,
"status": "RESOURCE_FAILURE",
"backend": "eager_bf16_no_compile_no_graphs",
"scientific_updates": 0,
"technical_optimizer_steps": technical,
"technical_forward_only_passes": (
0
if runtime_route is None
else runtime_route.get("technical_forward_only_passes", 0)
),
"runtime_route": runtime_route,
"technical_work_after_last_receipt": "UNKNOWN_NOT_RECOVERABLE",
"cases": cases,
"runtime_admission": False,
"error": {"type": type(error).__name__, "message": str(error)},
}
finally:
source.close()
torch.cuda.empty_cache()
gc.collect()
def parser() -> argparse.ArgumentParser:
result = argparse.ArgumentParser(description=__doc__)
result.add_argument("--source-descriptor", type=Path, required=True)
result.add_argument("--teacher-manifest", type=Path, required=True)
result.add_argument("--config", type=Path, default=Path("configs/rgb_port/producers.json"))
result.add_argument("--output", type=Path, required=True)
result.add_argument("--updates-per-case", type=int, choices=(2, 3), default=2)
result.add_argument("--case-batch-size", type=int, choices=range(2, 9), default=4)
return result
def main(argv: Sequence[str] | None = None) -> int:
args = parser().parse_args(argv)
payload = run(args)
_atomic_json(args.output.resolve(), payload)
print(
json.dumps({"status": payload["status"], "runtime_admission": payload["runtime_admission"]})
)
return 0 if payload["status"] == "PASS" else 3
if __name__ == "__main__":
raise SystemExit(main())
__all__ = ["CaseSelection", "main", "parser", "select_observed_cases", "validate_receipt"]
|