One bad number in the padding used to throw away a good sequence

Companion illustration for NVIDIA-NeMo/RL #4171. Permalinks pin the PR head ae5a6d9; the removed code is linked at the base commit 612d527, since it no longer exists at head.

Two pages on this PR, one question each β€” this one: a fix to the filter that was already on.

Before
A NaN or Β±Inf logprob sitting in a padded slot β€” a position the filter is supposed to ignore β€” poisoned the score for the whole sequence and got it thrown away.
This PR
Zeroes the ignored positions before doing any arithmetic on them, so padding cannot affect the score.
Status
Fixed by this PR. This is a change to the existing filter, which is on whenever seq_logprob_error_threshold is set β€” you do not need the PR's new opt-in mode to get it.

Sequences in a batch have different lengths, so short ones are padded out to a common width. A token_mask marks which slots are real response tokens. The filter scores a sequence by averaging exp(|generation_logprob βˆ’ policy_logprob|) over the real tokens only, and drops the sequence if that average is above the threshold. The padded slots hold whatever happened to be in memory β€” often -inf, because that is what masked-out logits become. The old code multiplied by the mask inside the exponent to skip them, and that is where it went wrong: in IEEE floating point, inf Γ— 0 is not 0. It is NaN.

One sequence, four slots, three of them real

One sequence. Slots 1-3 are real response tokens; slot 4 is padding. Threshold = 2.0 slot token_mask |error| 1 2 3 4 (pad) 1 1 1 0 0.5 0.2 0.9 -inf old multiply inside exp exp(|error| Γ— mask) Γ— mask 1.65 1.22 2.46 NaN inf Γ— 0 = NaN, not 0 mean over the 3 real slots = NaN β†’ NaN ≀ 2.0 is false β†’ sequence dropped the removed line, at the base commit β€” grpo.py:2762 new zero first, then exp where(mask, |error|, 0) β†’ exp(...) Γ— mask 1.65 1.22 2.46 0 the -inf is replaced before any arithmetic mean over the 3 real slots = 1.78 β†’ 1.78 ≀ 2.0 β†’ sequence kept utils.py:236

The change is one line

old num = (torch.exp(lp_error * mask) * mask).sum(dim=-1) # ^^^^^^^^^^^^^^^^^ (-inf) * 0 -> NaN, and NaN spreads to the sum new error = torch.where(mask.bool(), (generation_logprobs - policy_logprobs).abs(), 0.0) errors = (torch.exp(error * mask) * mask).sum(dim=-1) / counts.clamp(min=1) # ^^^^^^^^^^^^^^ the ignored slots are already 0 by the time exp runs

Running the numbers from the figure: exp(0.5) + exp(0.2) + exp(0.9) is 1.6487 + 1.2214 + 2.4596 = 5.3297, divided by the 3 real tokens gives 1.7766, which is under the threshold of 2.0. Under the old formula the fourth slot turned that sum into NaN, and because every comparison against NaN is false, the sequence failed the threshold test and was dropped.

A bad number on a real token still rejects the sequence, which is the behaviour you want. exp(inf) is inf and exp(NaN) is NaN, and neither is ever below a finite threshold. The fix only stops slots the filter was already told to ignore from having a say.

Why this matters more than it looks. This filter is not the PR's new opt-in mode β€” it runs on any config that sets seq_logprob_error_threshold, and four shipped recipes set it to 2. So the old behaviour was quietly throwing away training data whenever a padded slot held a non-finite logprob, and the sequence itself was perfectly fine. Nothing logged a warning; the sequence just counted as filtered. If you have been watching num_masked_seqs_by_logprob_error and wondering why it was higher than you expected, this is a candidate explanation.