Companion illustration for
NVIDIA-NeMo/RL #4171.
All permalinks pin the PR head ae5a6d9.
Two pages on this PR, one question each โ this one: three ways to normalize the same gradient, and why the PR picks the one it does.
grpo.seq_logprob_error_threshold forced an extra forward pass over the whole batch, purely to get the logprobs the filter needed.loss_fn.seq_logprob_error_in_loss. The filter runs inside the training forward, and the worker corrects the gradient once the loop is done.GRPO can throw away a sequence when the logprobs from the generator and from the trainer
disagree too much. Each microbatch's loss divides by the global number of surviving
response tokens โ global meaning across every microbatch and every data-parallel rank โ so that
once the gradients accumulate and get summed, the total comes out as
(sum of contributions) / (global count).
Below: the flow today, the flow this PR adds, and the third shape raised in review. All three land on the identical gradient. Same batch throughout โ three sequences of 7, 9 and 4 response tokens, split across two microbatches, where the 4-token one fails the threshold.
Nothing to correct: G was the right denominator from the start, because the rejects
were already zeroed out of sample_mask before
process_global_batch
counted them.
This mode requires force_on_policy_ratio: true, and under that flag the loss does
not even load prev_logprobs
(loss_functions.py:514-516)
and then overwrites it with curr_logprobs.detach()
(:528-529).
So that pass produced a tensor the loss throws away โ its only consumer was the filter.
The review question on this PR was why the loss divides by 20 at all, when it is
going to be corrected away. The alternative: have the loss divide by nothing, let the raw sum
accumulate, and divide by the survivor count once at the end. That is not hypothetical โ it is
how the split-API path in this same file already works, and it passes a literal placeholder of
1.0 into the loss to do it.
So paths 2 and 3 are the same single multiply on the finished gradient. The
survivor count is not knowable any earlier in either one. The only thing that differs is which
constant the loss divides by on the way in โ 20 or 1 โ and that choice
has knock-on effects:
| 2 ยท divide by G = 20 | 3 ยท divide by 1 | |
|---|---|---|
| End-of-step multiply | ร 20/16 | ร 1/16 |
| Final gradient | S / 16 | S / 16 โ identical |
| Loss metrics | Already on the denominator everything else expects. rescale_loss_metrics only touches the keys the loss advertises. |
Every metric comes back un-normalized and has to be undone downstream โ which is exactly what the metric_normalizations machinery exists for on that path. |
| MoE aux-loss hook | The hook is hardcoded to 1/global_valid_toks (:1186), which is the same value as C โ so main and aux come out of the backward on one denominator, and the single end multiply takes both to 1/K. |
The hook still says 1/global_valid_toks, but the main loss is now on 1/1. They leave the backward already off by G, and the end multiply scales both and keeps the gap. That line has to change too. |
| Numerics | No difference. G is at most 2^18 here (512 sequences ร 512 tokens), against bf16's 2^-126 โฆ 2^128 โ roughly 100 spare binades at either end, so neither form comes near overflow or underflow. FP8 and NVFP4 pick their scale factors from each tensor's own amax, so a constant factor is absorbed exactly and relative error is unchanged. And bf16's mantissa is 8 bits at every exponent, so precision does not vary with scale. |
|
The aux-loss row is the one that bites, because those hooks sit at a different place in the
code from the loss's own divide and name the same quantity by hand. MoE is a live case โ this
feature does not reject it, and a shipped MoE recipe already sets
seq_logprob_error_threshold. MTP is not: the validator refuses it outright
(grpo.py:485-491),
along with draft models, positive-example NLL and distillation.
What you get, and what you give up. You save one forward pass over the whole batch per step โ measured at 17.8% lower mean step time on a 1.5B model over 450 steps. You pay in two ways. Rejected sequences still cost a full forward and backward, because the filter only learns they are bad partway through. And the decision now comes from the training forward rather than a separate inference forward, so FP8 scale history and packing can change which sequences pass; the PR says plainly that bitwise agreement with the old path is not expected.