Megatron-LM icon indicating copy to clipboard operation
Megatron-LM copied to clipboard

Config validation and finalization cleanup

Open maanug-nv opened this issue 1 month ago • 3 comments

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.

maanug-nv avatar Oct 22 '26 18:10 maanug-nv

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.

maanug-nv avatar Dec 01 '26 00:12 maanug-nv

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().

maanug-nv avatar Dec 01 '26 00:12 maanug-nv

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:

  1. Follow the existing DistributedDataParallelConfig / ConfigContainerBase lifecycle:

    • 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/model finalize()).
  2. Config-by-config split (PR 1):

    • WideResidualConfig (megatron/core/transformer/wide_residual_config.py): extract __post_init__ checks into validate() and call self.validate() from __post_init__.
    • ModelParallelConfig (megatron/core/model_parallel_config.py): separate parallelism assertions into validate() and default/derived-field updates into finalize().
    • TransformerConfig & MLATransformerConfig (megatron/core/transformer/transformer_config.py): split __post_init__ into validate() and finalize(), and check if the deferred __post_init__ workaround in megatron/bridge/models/transformer_config.py can be simplified/removed.
    • KDALayerConfig (megatron/core/ssm/kda_layer_config.py): wire validate_kda() into validate() alongside super().validate().
    • HeterogeneousTransformerConfig (megatron/core/transformer/heterogeneous/heterogeneous_config.py): separate validation from file loading, JSON parsing, and per-block config construction.
  3. Container duplicate-field helper (PR 2):

    • Add a single container-level helper called from validate() in PretrainConfigContainer / 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.

Two quick questions before I begin:

  1. Should standalone MCore configs still call finalize() inside __post_init__ by default for backward compatibility (or only validate()), with containers/Bridge calling finalize() explicitly?
  2. Would you prefer this as a 2-PR stack (configs first, then container helper + Bridge cleanup) or a single PR?

ashp15205 avatar Dec 01 '26 18:12 ashp15205