Why the new "zero tail" rule switches off part of score centering even when replay is off

NVIDIA-NeMo/RL #5006, head 77d3b094. Raised first by a reviewer in this thread.

Before
#4818's estimator scales the trainer's tail by ฯ = sampler tail รท trainer tail, with the trainer tail clamped at 1e-6.
This PR
Sets ฯ = 0 whenever the trainer tail is at most 1e-6 (score_centering.py L74-L81), for every caller, replay or not.
Status
Fixed by the author in af11112532, after a reviewer flagged it. compute_score_centering_term now takes sampler_replay and zeroes ฯ only under replay; the PR's tests now cover the small-tail case. This page shows what the bug was.

Three pages on this PR, one question each. This one: what the new ฯ = 0 rule does outside replay.

Background. Score centering adds a correction built from vLLM's captured top-K list. Tokens outside the list (the "tail") are unseen, so their share is estimated as ฯ ยท p: the trainer's own tail probabilities, scaled so the total matches vLLM's missing mass. Under replay the list holds every token vLLM could sample, so there is no tail. There, any ฯ gives the same gradient; only the logged loss value changes (with #4818's formula it can jump from โˆ’1.42 to +140), which is why the author set it to 0.

The same token, either side of 1e-6

The reviewer's example, replay off: the trainer gives p = 0.6, 0.4 โˆ’ t, t to three tokens; vLLM gives q = 0.5, 0.3, 0.2 and captured only the first two (K = 2). The tail token holds 0.2 of vLLM's mass. Values computed on CPU with the base and head versions of the function.

0.20 0.10 0 GRADIENT ON THE TAIL TOKEN'S LOGIT (IS off) 1e-75e-79e-71.1e-62e-6 TRAINER'S PROBABILITY OUTSIDE THE CAPTURED LIST (log scale) 1e-6 cutoff (this PR) exact: q_tail โˆ’ p_tail โ‰ˆ 0.2 #4818 (main today) this PR: 0 below the cutoff
  1. The right gradient on the tail token is about qtail โˆ’ ptail = 0.2 at every t.
  2. #4818 gets close and fades smoothly as t shrinks (0.18 at t = 9e-7), because of its 1e-6 clamp.
  3. This PR gives the full 0.2 at t = 1.1e-6 and nothing at t = 9e-7: the tail correction switches off. Under truncated IS the whole correction goes to about 0.

The fix โ€” landed in af11112532

Where the fix landed: RL only. compute_score_centering_term takes a new sampler_replay argument. ClippedPGLossFn sets it when the batch has a top-k/top-p support mask, which only filtered sampling produces; setup already requires replay for score centering under filters, so the two always agree.

@@ nemo_rl/algorithms/loss/score_centering.py ยท compute_score_centering_term (af11112532)
-    trainer_tail = 1.0 - p.sum(-1)
-    rho = torch.where(
-        trainer_tail > 1e-6,
-        (1.0 - head_mass).clamp(min=0.0) / trainer_tail.clamp(min=1e-6),
-        0.0,
-    )
+    if sampler_replay:
+        rho = torch.zeros_like(head_mass)
+    else:
+        rho = (1.0 - head_mass).clamp(min=0.0) / (1.0 - p.sum(-1)).clamp(min=1e-6)

The else branch is #4818\'s line, unchanged. The PR\'s replay no-tail test now uses a replay-shaped input, and a new test checks the reviewer\'s example (trainer tail 9e-7) without replay.

So what. The rule was right for replay and wrong elsewhere. Tying it to replay instead of to the trainer's tail size keeps replay's logged loss clean and gives plain score centering back the tail gradient it had in #4818.