Repository navigation
fix(loss): exclude invalid policy support from actor ratios - #4367
Conversation
Signed-off-by: wdykas <wdykas@nvidia.com>
|
/ok to test 9c10c40 |
Removing the post-processor masking left -inf at prompt positions and NaN at automodel padding in prev_logprobs, and -inf at out-of-support tokens reached KL-in-reward and OPD advantages. Zero logprobs outside the valid token mask again (keeping -inf only on valid tokens for the actor loss), use torch.where for the automodel padding mask, and give excluded tokens zero KL-in-reward penalty and zero OPD advantage. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: root <root@nvl72049-T18.cm.cluster>
|
/ok to test f40a22d |
|
/claude review |
|
Claude Code Review (reposted) — automated review from workflow run 36876625666 at I've completed a thorough review of PR #4367, reading the full diff, the surrounding loss-function and advantage-estimator source, all downstream Review summaryLGTM. This is a clean, carefully-scoped fix with strong test coverage. I verified the following and found no critical issues:
Test coverage is comprehensive: token- and sequence-level, each invalid source, fully-excluded responses, unfiltered-KL preservation, a real GPU packed path asserting the exact Nothing to flag. |
terrykong
left a comment
There was a problem hiding this comment.
@mehraakash could you help review?
|
@mehraakash any update? |
|
LGTM actually! |
|
@mehraakash thanks for the quick response! I will resolve conflicts for merge |
…tics Signed-off-by: root <root@nvl72018-T16.cm.cluster>
|
/ok to test 984e315 |
…erfaces Signed-off-by: root <root@nvl72137-T02.cm.cluster>
|
/ok to test 3faf55a |
Signed-off-by: root <root@nvl72173-T10.cm.cluster>
|
/ok to test ca01e85 |
terrykong
left a comment
There was a problem hiding this comment.
Thanks for this. Building the actor mask in one place in the loss, with separate KL masks, is a clean design, and torch.where instead of multiplying -inf by a mask is the right tool.
First: the PR has merge conflicts with main. Please rebase and re-run CI. (All L0 lanes are green at ca01e85; functional tests did not run.)
Please rewrite the description against main today. #4154 (on main since 2026-10-06) already removes tokens outside the top-k/top-p set from token_mask: the curr side in prepare_loss_input (loss_input.py#L269-L280, linked at the base) and the prev side in the worker, written back by the driver (grpo.py#L3604-L3607). So the actor ratio bug in "Previously …" is already fixed on main. Compared with main, this PR:
- keeps reference KL on those tokens — a real improvement, since KL uses unfiltered logprobs;
- counts them in
global_valid_toks, so the actor loss is slightly scaled down (data.py#L1199-L1204). "Existing global rollout-batch normalization factors remain unchanged" is no longer accurate; - makes the sequence-error check reject whole responses (see the sequence-error comment on loss_input.py#L270);
- drops fully excluded responses.
It also says "all seven changed files"; there are 13.
Most important: the sequence-error regression on loss_input.py#L270. With that fixed and the now-unused token_mask return path removed (comment on megatron/train.py#L869-L873), this PR becomes a net simplification over #4154.
Visual explainer: https://terrykong.github.io/gh-pages-poc/terryk/pr-4367-unsupported-token.html
Generated by Claude Code
Signed-off-by: root <root@nvl72010-T18.cm.cluster>
Signed-off-by: root <root@nvl72010-T18.cm.cluster>
|
/ok to test 60fb026 |
Signed-off-by: root <root@nvl72010-T18.cm.cluster>
|
/ok to test 6b6f332 |
Signed-off-by: root <root@nvl72150-T16.cm.cluster>
|
/ok to test d9168c7 |
Signed-off-by: wdykas <wdykas@nvidia.com>
|
/ok to test 72c972f |
Signed-off-by: wdykas <wdykas@nvidia.com>
|
/ok to test 372c041 |
terrykong
left a comment
There was a problem hiding this comment.
lgtm. just needs tests to be fixed
Signed-off-by: wdykas <wdykas@nvidia.com>
|
/ok to test 926728e |
Signed-off-by: wdykas <wdykas@nvidia.com>
|
/ok to test 4334212 |
What does this PR do?
Keep filtered policy support information until the actor loss can exclude invalid tokens consistently.
Previously, current/previous logprob preparation replaced out-of-support
-infvalues with zero but discarded the corresponding validity mask. The actor then treated those zeros as log probability one. In GSPO, one such token can alter the response-wide ratio and clip an otherwise valid token's gradient to zero.Preserve support information through ordinary and packed logprob preparation and Megatron/Automodel previous-logprob computation. Previous logprobs keep -inf only on valid tokens; prompt and padding positions are still zeroed (Automodel padding now uses
torch.where, avoiding-inf * 0 = NaN). At the actor loss, combine current, previous and generation validity with the existing token/sample masks before sanitizing values and calculating ratios, diagnostics and reductions. Fully excluded responses contribute no actor loss. Keep separate masks and generation logprobs for unfiltered reference KL so actor support exclusion does not silently drop valid KL terms.Issues
No linked GitHub issue.
Consumers of
prev_logprobsoutside the actor loss must not see the preserved -inf: KL-in-reward (Reinforce++/GAE) and the OPD advantage give zero contribution at tokens outside the policy support. The unusedmask_out_neg_inf_logprobshelper is replaced bymask_filtered_logprobs_outside_tokens.Sequence-level logprob-error filtering excludes unsupported negative-infinity tokens from its diagnostic mask when top-k/top-p filtering is active. This applies to rollout-side GRPO/PPO filtering (including the single controller) and the single-forward in-loss check. An unsupported token therefore does not reject the whole response or discard otherwise valid reference-KL terms. NaN and positive infinity remain visible; without top-k/top-p, negative infinity still fails a finite sequence threshold. The generic
compute_seq_logprob_errorshelper keeps its strict semantics.policy_support_excluded_tokensreports the raw count removed from the actor mask in both full and minimal metrics.Usage
No new configuration. Applies to token-level and sequence-level importance ratios, including GSPO. Filtered-out positions no longer influence actor ratios or gradients.
Sequence averages use the surviving actor tokens. Existing global rollout-batch normalization factors remain unchanged; this does not introduce per-microbatch survivor renormalization. The existing explicit in-loss survivor-normalization mode receives updated survivor counts.
Validation
Before your PR is "Ready for review"
Additional Information
This does not make training and inference logits identical or align their top-p implementations; it handles tokens outside policy support correctly when a mismatch occurs. CPU tests patch only a hardcoded target
.cuda()placement; filtering, loss and autograd math run normally. The packed regression runs on GPU without that placement patch.CI:L0 only; no CI:L1 label is added.