Repository navigation
fix(training): realign the distributed optimizer's parameter index with its group order - #6426
Closed
kamran-nvidia wants to merge 1 commit into
Closed
kamran-nvidia wants to merge 1 commit into
kamran-nvidia wants to merge 1 commit into
Conversation
…th its group order Megatron-Core's DistributedOptimizer records each parameter's position within its optimizer group in gradient-buffer order (_build_optimizer_group_ranges), but stores the group's shards as all FP32 shards followed by all 16-bit shards (_build_model_and_main_param_groups). When a group mixes FP32 parameters, for example ones marked with mark_keep_in_fp32, with 16-bit ones and a 16-bit buffer comes first, model_param_group_index_map points at other parameters' shards. Only optimizer checkpointing reads that map, so training is unaffected. Saving optimizer state fails in sharded_param_state_dp_reshardable with "AssertionError: (torch.Size([...]), start, end)", and a load could restore state onto the wrong parameters wherever the sizes happen to agree. Until Megatron-Core fixes the ordering, setup_optimizer rebuilds the map in the stored order for every DistributedOptimizer (including inside a ChainedOptimizer). Already aligned maps are left unchanged, so the step becomes a no-op once the upstream fix lands. Tests: stand-in optimizers cover the realignment, chained and non-distributed optimizers, and unknown parameters. A one-GPU test builds a real DDP model with a BF16 buffer ahead of an FP32 one, takes an Adam step, checks that each parameter's optimizer state matches its own range and values, and builds the dp_reshardable checkpoint state; with the realignment disabled it fails. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Kamran Jafari <kjafarisadeg@nvidia.com>
Contributor
Author
|
Upstream fix in NVIDIA/Megatron-LM#7336 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do ?
Works around a Megatron-Core
DistributedOptimizerbug that makes optimizer checkpoint saves fail when an optimizer group mixes FP32 and 16-bit trainable parameters.setup_optimizernow rebuilds the optimizer's parameter index in the order the group actually stores its shards.The bug.
_build_optimizer_group_rangesrecords each parameter's position in its optimizer group in gradient-buffer order._build_model_and_main_param_groupsthen stores the group as all FP32 shards followed by all 16-bit shards. When a 16-bit buffer comes before an FP32 one,model_param_group_index_mappoints at other parameters' shards. FP32 parameters typically come frommark_keep_in_fp32, which MCore uses for hyper-connection, CSA, and MTP-head parameters.param_groupsdirectly.sharded_param_state_dp_reshardable, for example withAssertionError: (torch.Size([8192]), 8192, 12288).The same code is on Megatron-LM
maintoday. This workaround is meant to stay until an upstream fix lands; it leaves already-aligned maps untouched, so it then becomes a no-op.Changelog
training/optim.py:_realign_distributed_optimizer_param_index, called at the end ofsetup_optimizer, rebuildsmodel_param_group_index_mapfor everyDistributedOptimizer, including inside aChainedOptimizer. It logs the number of corrected entries and leaves the map unchanged if MCore's groups and index ever disagree on the parameter set.tests/unit_tests/training/test_optim.py:dp_reshardablecheckpoint state.Testing
e30d863a(the current pin), GB200: the 5 new tests pass, and the rest oftest_optim.pypasses.AssertionErroron theparamstate, so it reproduces the bug.Before your PR is "Ready for review"
Pre checks:
Additional Information
megatron/core/optimizer/distrib_optimizer.py); an upstream PR will follow.🤖 Generated with Claude Code