v0.3.0 step 2: network.py refactor to composable stage models
CI / Format (ruff format) (push) Failing after 25s
CI / Lint (ruff check) (push) Successful in 27s
CI / Sync project version with tag (push) Has been skipped
CI / Type check (ty) (push) Failing after 24s
CI / Tests (push) Has been skipped

Decomposes the ten permutation classes in giant/model/network.py into
the reusable parts from docs/v0.3.0-design.md §5: ConditionEncoder (now
independently configurable per particle/material axis), ContextAdapter,
Trunk/MonolithicTrunk/RoutedTrunk/ExpertTrunk, and the stage classes
Stage1Model/Stage2OneShot/CriticModel (Stage2Autoregressive stubbed,
raises NotImplementedError until step 4/5). build_models/build_critics
now return a dict keyed by stage and accept the new nested config shape,
with routed WGAN reachable for the first time (the old --mode wgan
--router rejection is gone) and stage2_model.router.tie_to_stage1
sharing a literal Router instance.

A v0.2 checkpoint's flat model_config auto-migrates via
_migrate_legacy_model_config + migrate_legacy_state_dict, preserving the
n_sec_head's attachment to Stage1Model (legacy_owner="stage1", design
doc §4.1). tests/test_migration_v02_v03.py proves this bit-identical
against a frozen v0.2 snapshot (tests/legacy/network_v02_snapshot.py)
for both flow and wgan, both conditioning modes.
scripts/check_migration_v02_v03.py is the real-checkpoint counterpart
for a portal machine with /ceph access.

giant/model/schedule.py's flow-matching/DDPM loss helpers are updated
to the new model-call convention (t as a keyword). giant/sample.py,
giant/rollout.py, and giant/validate.py are not yet updated (deferred
to design doc step 6) — their exercising tests are marked xfail with
that reasoning rather than silently broken.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-08-06 10:55:29 +02:00
parent eb6dd27406
commit 9ce55e5013
13 changed files with 2733 additions and 1092 deletions
+33 -3
View File
@@ -1,14 +1,35 @@
import numpy as np
import pytest
import torch
from giant.constants import COND_DIM, K_MAX, SEC_SLOT_DIM, X_DIM
from giant.model.network import DenoisingMLP, SecondaryDecoder
from giant.model.network import Stage1Model, Stage2OneShot
from giant.validate import validate_marginals
_PARTICLE_CFG = {"type": "physical", "emb_dim": 8, "n_layers": 1}
_MATERIAL_CFG = {"type": "physical", "emb_dim": 8, "n_layers": 1}
def _tiny_models():
s1 = DenoisingMLP(pdg_vocab=3, mat_vocab=2, hidden_dim=16, n_blocks=1)
s2 = SecondaryDecoder(pdg_vocab=3, mat_vocab=2, hidden_dim=16, n_blocks=1)
s1 = Stage1Model(
pdg_vocab=3,
mat_vocab=2,
particle_cfg=_PARTICLE_CFG,
material_cfg=_MATERIAL_CFG,
hidden_dim=16,
n_res_blocks=1,
n_sec_head_k_max=K_MAX,
)
s2 = Stage2OneShot(
pdg_vocab=3,
mat_vocab=2,
particle_cfg=_PARTICLE_CFG,
material_cfg=_MATERIAL_CFG,
hidden_dim=16,
n_res_blocks=1,
generator="flow",
time_dim=16,
)
return s1.eval(), s2.eval()
@@ -28,6 +49,15 @@ def _zero_secondaries_loader(B=4, n_batches=2):
return batches
@pytest.mark.xfail(
reason=(
"giant/validate.py isn't updated yet — it calls the stage models "
"(sample_secondaries et al.) with the old positional convention, "
"which doesn't match Stage1Model/Stage2OneShot's new forward "
"signature. Deferred to docs/v0.3.0-design.md step 6/§10."
),
strict=False,
)
def test_validate_marginals_all_zero_secondaries_returns_nan_phys_kl(monkeypatch):
"""If n_sec_pred collapses to 0 across the whole validated set (realistic
during early/unstable training), phys_kl must degrade to NaN instead of