Files
giant/giant/model/network.py
T
lars 4692cee699
CI / Lint (ruff check) (push) Successful in 30s
CI / Format (ruff format) (push) Successful in 30s
CI / Sync project version with tag (push) Has been skipped
CI / Type check (ty) (push) Successful in 43s
CI / Lint (ruff check) (pull_request) Successful in 40s
CI / Format (ruff format) (pull_request) Successful in 43s
CI / Sync project version with tag (pull_request) Has been skipped
CI / Type check (ty) (pull_request) Successful in 45s
CI / Tests (pull_request) Successful in 3m24s
CI / Tests (push) Successful in 3m33s
Give the cond_cat/cond_cont column layout one owner (gitea #37)
The conditioning arrays' column order was written down three times — twice
in giant/data/transforms.py (build_cond_features and build_features each
built cond_cont and cond_cat from scratch) and again in
giant/model/encoders.py (cat_col_layout, plus hand-written
COND_DIM_BASE + PARTICLE_PHYS_DIM slicing in ConditionEncoder). The three
were held in sync only by parallel comments, so a wrong column order
produced silently mis-indexed features rather than an exception.

The drift had already happened, twice, both times in build_features:

- 5b63dfd added per-axis vocab-lookup strictness (an out-of-vocab
  pdg/material must not KeyError under "physical"/"onehot", where the
  index is never read) to build_cond_features only.
- _cond_normalizer_transform's legacy-normalizer padding, which keeps a
  pre-physical-conditioning 8-wide cond normalizer loadable, was likewise
  only wired into build_cond_features — so `giant predict` on such a
  checkpoint died with a broadcast error.

New giant/cond_layout.py holds a frozen CondLayout built from the
(particle, material) mode pair, exposing named cond_cont slices
(base/particle_phys/material_phys) and cond_cat columns
(PDG_COL/MAT_COL/particle_topn_col/material_topn_col/cat_dim). Both
builders now share one _build_cond_arrays, ConditionEncoder reads its
slices off the same object, and PdgRouter/ProcessRouter use the named
dense-vocab columns instead of literal 0/1. CondLayout also absorbs the
two duplicated axis-type validations, keeping their message text verbatim.

Decisions taken while planning:

- Scope is CondLayout only. The issue's second half — a
  CONDITIONING_AXIS_REGISTRY registering (feature_columns, encoder_module)
  as a pair — is deferred: it would force ConditioningConfig's fixed
  particle/material fields into a dynamic axis map and ripple through
  pipeline.py, checkpoint_io.py and rollout.py, i.e. a config-schema break
  with no consumer yet.
- The two divergences above are unified onto build_cond_features'
  behaviour rather than preserved as parameters, so the new single source
  of truth doesn't carry the old split forward. Each gets a regression
  test that fails before this commit.
- cat_col_layout is replaced outright (deleted, dropped from network.py's
  __all__, its four tests rewritten against CondLayout) rather than kept
  as a wrapper — two spellings of the same fact is the defect itself.

cond_cat's width is now the layout's call rather than "did the caller pass
a map", so an "onehot" axis without its top-N map raises instead of
yielding a narrower array that ConditionEncoder would index out of bounds.
pipeline.py's normalizer-fitting pass reads only cond_cont but had to be
handed the maps to satisfy that.

No parameter, buffer or state_dict change; existing checkpoints load
unchanged, and the protected migration surfaces are untouched.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-14 14:16:20 +02:00

135 lines
3.1 KiB
Python

"""Re-export shim.
`network.py` used to hold every network-related class in one 1742-line file.
It's now split by concern into `giant/model/{layers,encoders,routers,trunks,
history,models,_legacy,builders}.py` (issues.md Issue 8); this module just
re-exports the public surface so every existing `from giant.model.network
import X` call site keeps working unchanged.
"""
from giant.model._legacy import _migrate_legacy_model_config, migrate_legacy_state_dict
from giant.model.builders import build_critics, build_models
from giant.model.encoders import ConditionEncoder
from giant.model.history import (
HISTORY_REGISTRY,
AttentionHistory,
HistoryEncoder,
MarkovHistory,
_CausalAttnBlock,
build_history,
register_history,
)
from giant.model.layers import (
BLOCK_REGISTRY,
AdaLNResBlock,
ContextAdapter,
FilmResBlock,
ResBlock,
SinusoidalEmbedding,
_make_axis_mlp,
build_block,
build_mlp_head,
register_block,
)
from giant.model.models import (
CriticModel,
Stage1Model,
Stage2Autoregressive,
Stage2OneShot,
resolve_type_n_classes,
stage2_trunk_sec_dim,
stage2_type_dim,
)
from giant.model.objectives import (
OBJECTIVE_REGISTRY,
DdpmObjective,
FlowObjective,
Objective,
WganObjective,
build_objective,
register_objective,
)
from giant.model.routers import (
ROUTER_REGISTRY,
ComposedRouter,
EnergyRouter,
PdgRouter,
ProcessRouter,
Router,
_build_router_from_cfg,
_check_router_conditioning_compat,
_parse_composed_axes,
build_composed_router,
build_router,
register_router,
)
from giant.model.trunks import (
TRUNK_REGISTRY,
ExpertTrunk,
RoutedTrunk,
Trunk,
_route_forward,
build_expert_body,
build_trunk,
register_trunk,
)
__all__ = [
"AdaLNResBlock",
"AttentionHistory",
"BLOCK_REGISTRY",
"ComposedRouter",
"ConditionEncoder",
"ContextAdapter",
"CriticModel",
"DdpmObjective",
"EnergyRouter",
"ExpertTrunk",
"FilmResBlock",
"FlowObjective",
"HISTORY_REGISTRY",
"HistoryEncoder",
"MarkovHistory",
"OBJECTIVE_REGISTRY",
"Objective",
"PdgRouter",
"ProcessRouter",
"ROUTER_REGISTRY",
"ResBlock",
"RoutedTrunk",
"Router",
"SinusoidalEmbedding",
"Stage1Model",
"Stage2Autoregressive",
"Stage2OneShot",
"TRUNK_REGISTRY",
"Trunk",
"WganObjective",
"_CausalAttnBlock",
"_build_router_from_cfg",
"_check_router_conditioning_compat",
"_make_axis_mlp",
"_migrate_legacy_model_config",
"_parse_composed_axes",
"_route_forward",
"build_block",
"build_composed_router",
"build_critics",
"build_expert_body",
"build_history",
"build_mlp_head",
"build_models",
"build_objective",
"build_router",
"build_trunk",
"migrate_legacy_state_dict",
"register_block",
"register_history",
"register_objective",
"register_router",
"register_trunk",
"resolve_type_n_classes",
"stage2_trunk_sec_dim",
"stage2_type_dim",
]