Repository navigation
feat(grpo): support detached MTP heads with in-loss sequence filtering - #5095
Merged
Merged
Conversation
loss_fn.seq_logprob_error_in_loss rejected any MTP configuration. The MTP loss mask is built before the forward pass, so MTP sums its loss over the pre-filter tokens, while in-loss filtering normalizes gradients by the survivor count. Without a correction MTP gradients would be divided by the survivor count instead of the token count they were summed over. With mtp_detach_heads=true the MTP loss reaches only MTP-tagged parameters, so their denominator can be corrected independently: - SingleController finish_train_step reduces the pre-filter token count together with the survivor counts and uses it for the existing MTP gradient correction. - The non-streaming trainer undoes the survivor rescale on MTP-tagged parameters after scaling all gradients by G/K. MTP heads therefore keep training on every generated sequence, including ones the threshold rejects. Attached heads remain rejected: their gradient is mixed into shared parameters that are rescaled by survivors. Signed-off-by: Sriharsha Niverty <[email protected]>
snivertynv
requested review from
mehraakash,
parthmannan,
saumishr and
terrykong
November 5, 2026 18:29
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.
loss_fn.seq_logprob_error_in_loss rejected any MTP configuration. The MTP loss mask is built before the forward pass, so MTP sums its loss over the pre-filter tokens, while in-loss filtering normalizes gradients by the survivor count. Without a correction MTP gradients would be divided by the survivor count instead of the token count they were summed over.
With mtp_detach_heads=true the MTP loss reaches only MTP-tagged parameters, so their denominator can be corrected independently:
MTP heads therefore keep training on every generated sequence, including ones the threshold rejects. Attached heads remain rejected: their gradient is mixed into shared parameters that are rescaled by survivors.
What does this PR do ?
Allows
loss_fn.seq_logprob_error_in_losswith MTP whenpolicy.megatron_cfg.mtp_detach_heads=true, in both the SingleController and non-streaming GRPO trainers. MTP with attached heads is still rejected. Stacked on #5080 .Why
The MTP loss mask is built before the forward pass, so MTP sums its loss over all pre-filter tokens, while in-loss filtering normalizes gradients by the survivor count. Without a correction, MTP gradients would be divided by the survivor count instead of the token count they were summed over. With detached heads, the MTP loss only reaches MTP-tagged parameters, so their denominator can be corrected on its own:
finish_train_stepreduces the pre-filter token count alongside the survivor counts and uses it for the existing MTP gradient correction.MTP heads therefore keep training on every generated sequence, including ones the threshold rejects. Attached heads mix the MTP gradient into shared parameters that are rescaled by survivors, so they remain unsupported.
Validation
Multi-node SingleController GRPO on an MoE model with 5 detached MTP heads,
ready_firstat lag 1, 512 prompts × 16 generations per step. Baseline: the same recipe with the separate policy-logprob pass. Steps 2–30, mean (step-to-step stdev), one seed per run:All metrics stay within one step-to-step standard deviation, including all five MTP heads' losses and acceptance rates. With MTP enabled the separate logprob pass is more expensive, so removing it saves more per step than without MTP.
Tests
finish_train_step: main gradients divided by survivors, MTP gradients by the pre-filter token count.