Skip to content

feat(grpo): support detached MTP heads with in-loss sequence filtering - #5095

Merged
snivertynv merged 1 commit into
sc-seq-logprob-in-lossfrom
sc-inloss-mtp
Nov 5, 2026
Merged

snivertynv merged 1 commit into
sc-seq-logprob-in-lossfrom
sc-inloss-mtp

Conversation

@snivertynv

Copy link
Copy Markdown
Contributor

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.

What does this PR do ?

Allows loss_fn.seq_logprob_error_in_loss with MTP when policy.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:

  • SingleController finish_train_step reduces the pre-filter token count alongside the survivor counts and uses it for the existing MTP gradient correction.
  • The non-streaming trainer undoes the survivor rescale on MTP-tagged parameters.

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_first at 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:

Metric Separate pass In-loss Δ
reward 0.891 (0.072) 0.888 (0.075) −0.3%
loss 0.086 (0.024) 0.088 (0.022) +2.2%
grad norm 0.0289 (0.0036) 0.0304 (0.0053) +5.1%
MTP head 1 loss 0.640 (0.112) 0.675 (0.084) +5.4%
MTP head 1 acceptance rate 75.6% (2.6) 74.4% (1.5) −1.2 pt
sequences dropped / step (of 8192) 3.5 3.0
step time 453 s 366 s −19%
trained tokens / s 101.9k 123.4k +21%

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

  • Validator: detached MTP accepted, attached MTP rejected, in both trainers.
  • SingleController finish_train_step: main gradients divided by survivors, MTP gradients by the pre-filter token count.
  • Non-streaming normalizer: survivor rescale undone on MTP parameters.

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 snivertynv self-assigned this Nov 5, 2026
@snivertynv snivertynv added the CI:L1 Run doctests, unit tests, and functional tests label Nov 5, 2026
@copy-pr-bot

copy-pr-bot Bot commented Nov 5, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@github-actions github-actions Bot added the Documentation Improvements or additions to documentation label Nov 5, 2026
@terrykong terrykong added CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version) and removed CI:L1 Run doctests, unit tests, and functional tests labels Nov 5, 2026
@snivertynv
snivertynv merged commit 0573f23 into sc-seq-logprob-in-loss Nov 5, 2026
11 checks passed
@snivertynv
snivertynv deleted the sc-inloss-mtp branch November 5, 2026 22:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version) Documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants