Config validation and finalization cleanup
A few things about validate() and finalize() can be checked and cleaned up:
- Duplicate fields in different configs should be explicitly checked in a single container-level helper function. This should be called in validate. Check values explicitly rather than using a loop to reduce LoC.
- Boundary of validate and finalize should be confirmed across the configs. validate should contain only assertions and should be repeatedly callable. Finalize functions, which handle runtime updates (and assertions based on these updates) should only run once and need not be idempotent.
we should also split up post-init for TransformerConfig (and other remaining MCore config classes) into finalize and validate . Right now we need this subclass as a workaround.
Narrowing this to configs that can appear inside the existing PretrainConfigContainer and InferenceConfigContainer definitions:
| MCore config | Container location | Remaining lifecycle work |
|---|---|---|
ModelParallelConfig |
Both, as the base of model.transformer |
Separate parallelism checks from default resolution and derived-field updates. |
TransformerConfig |
Both, at model.transformer |
Separate validation from derived dimensions, initialization functions, and other updates. |
MLATransformerConfig |
Both, as a model.transformer subtype |
Split the inherited lifecycle and MLA-specific checks and updates. Bridge already has a deferred post-init wrapper for this class too. |
HeterogeneousTransformerConfig |
Both, as a model.transformer subtype |
Separate validation from file loading, JSON parsing, and per-block config construction. |
KDALayerConfig |
Both, permitted as a model.transformer subtype |
Already has validate_kda(), but still invokes the inherited transformer post-init. |
WideResidualConfig |
Both, at model.transformer.wide_residual |
Expose its construction-time checks through a repeatable validate(). |
MultimodalDatasetConfig is also technically accepted by PretrainConfigContainer.data.dataset because it subclasses GPTDatasetConfig, but the container does not currently provide a multimodal dataset build path.
The main optimizer, DDP, and GPT dataset configs already expose explicit lifecycle methods. MCore’s runtime InferenceConfig, MambaInferenceStateConfig, and MimoModelConfig are constructed later rather than stored in these containers, so those can be tracked separately.
The split should follow the issue’s existing boundary: repeatable, non-mutating checks in validate(), with default resolution and derived-field updates in finalize().
Hi @maanug-nv — thanks for scoping this out so clearly. Before I start implementing, I wanted to check if you or your team are already working on this internally, or if it's open for an external PR.
If it's open, here is how I plan to structure the changes:
-
Follow the existing
DistributedDataParallelConfig/ConfigContainerBaselifecycle:validate(self) -> None: assertions only, non-mutating, safe to call repeatedly.finalize(self) -> None: default resolution, derived-field updates, file/JSON parsing, and post-update assertions (runs once via container/modelfinalize()).
-
Config-by-config split (PR 1):
WideResidualConfig(megatron/core/transformer/wide_residual_config.py): extract__post_init__checks intovalidate()and callself.validate()from__post_init__.ModelParallelConfig(megatron/core/model_parallel_config.py): separate parallelism assertions intovalidate()and default/derived-field updates intofinalize().TransformerConfig&MLATransformerConfig(megatron/core/transformer/transformer_config.py): split__post_init__intovalidate()andfinalize(), and check if the deferred__post_init__workaround inmegatron/bridge/models/transformer_config.pycan be simplified/removed.KDALayerConfig(megatron/core/ssm/kda_layer_config.py): wirevalidate_kda()intovalidate()alongsidesuper().validate().HeterogeneousTransformerConfig(megatron/core/transformer/heterogeneous/heterogeneous_config.py): separate validation from file loading, JSON parsing, and per-block config construction.
-
Container duplicate-field helper (PR 2):
- Add a single container-level helper called from
validate()inPretrainConfigContainer/InferenceConfigContainer(megatron/training/config/container.py) checking overlapping fields explicitly (without a loop). - Add unit tests for
validate()idempotency, post-edit validation, and duplicate-field checks.
- Add a single container-level helper called from
Two quick questions before I begin:
- Should standalone MCore configs still call
finalize()inside__post_init__by default for backward compatibility (or onlyvalidate()), with containers/Bridge callingfinalize()explicitly? - Would you prefer this as a 2-PR stack (configs first, then container helper + Bridge cleanup) or a single PR?