[megatron] Fix loss aggregation for context parallelism (CP) in Megatron#1420
Merged
erictang000 merged 2 commits intomainfrom Mar 31, 2026
Merged
[megatron] Fix loss aggregation for context parallelism (CP) in Megatron#1420erictang000 merged 2 commits intomainfrom
erictang000 merged 2 commits intomainfrom
Conversation
Contributor
There was a problem hiding this comment.
Code Review
This pull request updates the Megatron worker and model wrapper to correctly handle context parallel ranks in data parallel size calculations and metric reductions. A review comment points out that increasing the global absolute tolerance in the CI tests to 0.25 may be too permissive for metrics with small magnitudes, such as the learning rate, and suggests implementing per-metric or relative tolerance checks instead.
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.
Fixes
test_megatron_train[tp2_cp2_policy_seq_packing_no_entropy_loss]failing after #1296.Problem
The loss refactor in #1296 introduced two CP-specific bugs:
all_reduce_metricsusedget_data_parallel_group(with_context_parallel=True), which includes CP ranks in the reduction group. Withsum_loss_metrics=True, this sumspolicy_lossacross CP ranks. But sincepostprocess_packed_seqsalready gathers logprobs across CP before computing the loss, all CP ranks produce identical metrics — so summing doubles the value. This caused the ~2x discrepancy (-28.43FSDP vs-57.36Megatron).grad_sum_correction_factorusedget_data_parallel_world_size()(without CP), but Megatron'sfinalize_model_gradsaverages gradients across the full DP+CP group. The correction was therefore1/CP_sizetoo small.Fix
get_data_parallel_group(with_context_parallel=False)for the metrics all-reduce, since metrics are already complete on each CP rank.get_data_parallel_world_size(with_context_parallel=True)for the gradient correction factor, matching the group thatfinalize_model_gradsreduces over.Both changes are no-ops when CP=1.