NVIDIA-NeMo/RL #4818
(adds loss_fn.score_centering and token_capture.top_logprobs). Permalinks pin head f0d9860e.
Six pages on this PR, one question each — this one: what the feature adds, next to what was there.
Step 1, the loss NeMo-RL computes. For one sampled token y, the clipped loss is
(clipped_pg.py L142–L167):
p is the trainer now (it gets gradients). pold is the trainer when the batch's log-probs were taken (fixed data). w = pold(y) / q(y) is the vLLM-to-trainer importance weight, detached (loss_functions.py L916); with IS off, w = 1.
Step 2, its gradient. Only log p(y) carries gradient. Because r = elog p(y) − const, the derivative of r is r times the derivative of log p(y). So for a token that is not clipped, the gradient is −A·w·r·∇log p(y). For a clipped token, the max picks the clamped term, which is a constant, so the gradient is 0. On the first optimizer step for a batch, p = pold, so r = 1. With IS off, w = 1 too. What is left is −A·∇log p(y): the same gradient as the simple loss −A·log p(y). That is all "raise log p(y), scaled by A" means.
Careful: "r = 1" is a value, not a constant. Plugging r = 1 into the loss before taking the
gradient gives −w·A, a number with no p in it and no gradient. That is the wrong order. r is a function,
r(θ) = pθ(y) / pold(y): the top moves with the weights, the bottom is a frozen number. At the current weights it
happens to equal 1, but its slope is not 0. Same as f(x) = x / 3 at x = 3: the value is 1, the slope is 1/3. So take the gradient
first (−A·w·r·∇log p(y)), then plug in r = 1. NeMo-RL does exactly this with force_on_policy_ratio:
r = exp(log p − log p.detach()) is exactly 1 in value, but its gradient is ∇log p
(clipped_pg.py L139). In torch, −A·r with that r and −A·log p(y)
give the same gradient to every digit.
Step 3, ∇log p(y) on the logits. p is a softmax over the logits z, so:
The first part is +1 on the sampled id only; write it as a one-hot vector ey (1 at id y, 0 everywhere else). The second part is −pk on every id. So the push is ey − p: "one-hot at the sampled id" minus "the trainer's probabilities". Every number in it is the trainer's; this is plain calculus, not importance sampling. vLLM's only role is choosing which id gets the 1. With y = 17 and trainer probabilities 0.60, 0.15 and 0.25 for the rest, that is +0.40, −0.15, −0.25: the top row below.
Now add vLLM's distribution q and subtract it again. Nothing changes; it only regroups the same push:
Why q? Because y was drawn from q, so the one-hot ey, averaged over vLLM's draws, is exactly q. (Here: id 17 gets picked 72% of the time, id 4 18%, the rest 10%.)
Why the sign of A decides the direction. A training step moves each logit by A × (its part of the push). For the pull part that is A × (q − p): +0.12 on id 17, +0.03 on id 4, −0.15 on the rest. q − p is positive exactly where vLLM likes a token more than the trainer does, and negative where it likes it less. With A > 0 the step raises the logits vLLM favors more and lowers the others, so after the softmax the trainer looks more like vLLM. With A < 0 every sign flips and the trainer moves away from vLLM. One step from the example, computed with the softmax:
| id 17 | id 4 | rest | total gap to vLLM | |
|---|---|---|---|---|
| vLLM q | 0.72 | 0.18 | 0.10 | — |
| trainer p, before | 0.60 | 0.15 | 0.25 | 0.30 |
| after the pull, A = +1 | 0.65 | 0.15 | 0.21 | 0.21 (closer) |
| after the pull, A = −1 | 0.55 | 0.15 | 0.30 | 0.40 (farther) |
"Total gap" adds up |p − q| over the three columns. Nothing in this move depends on whether id 17 was a good choice: only A's sign does. That is why it is noise from the point of view of learning, and why score centering removes it.
The pull is zero only when vLLM and the trainer agree exactly. They rarely do: they use different kernels and precision, and in async runs vLLM's weights trail the trainer's by a step or more. Score centering subtracts the pull and keeps only the signal, so on the logits the update becomes A × (sampled token − vLLM's distribution). To subtract the pull, the trainer needs q, vLLM's probability for every token.
Why only the top-K, not the full vocabulary. The exact pull needs q over the whole vocabulary at every position. That is too big to move: about 152,000 numbers per token, roughly 18.5 GiB for one 32K-token response in fp32. So the PR records only vLLM's K most likely tokens per position: about 32 MiB per response at K = 128. It is a trade-off for size. The K recorded tokens are exact. Everything else is an estimate.
How the rest is estimated. For the tokens outside the top-K, vLLM tells us only one thing: their total, 1 − (sum of the top-K), which is 0.10 here. It does not say how that 0.10 is split between those tokens. The PR assumes vLLM splits it in the same proportions as the trainer does, and scales the trainer's numbers to the right total:
| id 17 | id 4 | token a | token b | |
|---|---|---|---|---|
| vLLM q (recorded) | 0.72 | 0.18 | only the total is known: 0.10 | |
| trainer p | 0.60 | 0.15 | 0.15 | 0.10 |
| estimate of vLLM's q | 0.72 (exact) | 0.18 (exact) | 0.4 × 0.15 = 0.06 | 0.4 × 0.10 = 0.04 |
The scale factor is ρ = vLLM's leftover ÷ the trainer's leftover = 0.10 ÷ 0.25 = 0.4. If vLLM really
split it 0.08 / 0.02, the estimate is off in its shape but still has the right total. Both splits add up to the same
leftover, so the total error is at most twice the leftover mass. The run logs that mass as 1 − score_centering_head_mass. A larger K shrinks it.
What if vLLM samples with a top_k = K filter? Then vLLM can only pick those K tokens.
With processed_logprobs they sum to 1, so the leftover is 0, ρ = 0, and the recorded K tokens are vLLM's whole
distribution, with nothing estimated. That case is not available today: the Gym/HTTP path forces top_k off, and
this PR rejects top_k with score centering
(config.py L1419).
It would also need the trainer to use the same K tokens, which is what
sampling-mask replay provides.
Do vLLM's top-K and the trainer's top-K have to agree? No. The trainer never picks its own
top-K. It runs a full-vocabulary softmax and reads its probability at vLLM's K ids
(get_policy_logprobs_at_sampler_topk).
Both sides sample without filters, so every token is possible on both sides and there is no support to line up. A token the trainer
likes but vLLM did not rank in its top-K simply lands in the "rest" and gets an estimated q. That only adds tail error, which
stays under twice vLLM's leftover mass whatever the trainer's ranking. Checked with the real function, vLLM's top-2 = ids 17, 4,
true vLLM rest = 0.08 / 0.02:
| Trainer p over (17, 4, a, b) | PR's correction | exact q − p | total error |
|---|---|---|---|
| 0.60, 0.15, 0.15, 0.10 (same top-2 as vLLM) | 0.12, 0.03, −0.09, −0.06 | 0.12, 0.03, −0.07, −0.08 | 0.04 |
| 0.20, 0.10, 0.65, 0.05 (trainer's top token a is outside vLLM's top-2) | 0.52, 0.08, −0.557, −0.043 | 0.52, 0.08, −0.57, −0.03 | 0.026 |
The two recorded ids are exact in both rows. What sets the error is how much mass vLLM puts outside its own top-K, not whether the two rankings match.
In the code: how the added term is built. The goal is a loss whose gradient is exactly minus the pull, so the two cancel. Work at one position t (dropping t from the names); the code does this at every generated position and then averages, like the main loss.
.detach()) means. c contains p, so without sg its own
dependence on p would add extra gradient and the term would no longer equal the pull. It is the same reason A is detached in
policy gradient. c is not an importance-sampling ratio.Why log p and not pold? The pull was worked out for the first optimizer step on a batch, where r = p / pold = 1 and the main loss's gradient is −A·w·∇log p(y), with no ratio. The correction copies that case: it uses the current p and has no r. After more steps on the same batch r ≠ 1, or a token gets clipped. Then the main loss's real pull changes but the correction does not, so they cancel only roughly (the "PPO catch" below). The PR's guide says the same: the main term uses the previous policy, the correction uses the current one.
sampler_topk_token_ids): vLLM's top-K ids at position t. The trainer reads its own probability at these ids.sampler_topk_logprobs): vLLM's probability for each id in Ht. Fixed data.policy_logprobs_at_sampler_topk): the trainer's current probability at those ids, over the full vocabulary. Gradients flow through log p.Shapes: ids [B, S, K] → trainer log-probs at those ids [B, S−1, K] → sum over K
(score_centering.py L89)
→ [B, S−1] → × A, then the same masked mean over tokens as the main loss
(loss_functions.py L1092).
Prompt and tool positions are masked out. The IS mode changes only w, and so only m and α.
Each mode changes w, the weight the main loss gives the sampled token. Steps 1–2 above then give m and α, and c = m − α·p. Same example as above: q = 0.72, 0.18 and p = 0.60, 0.15 at ids 17 and 4, ρ = 0.4.
Mode (loss_fn settings) | Main loss weights the sampled token by w | Head mv | Rest α | c at ids 17, 4 |
|---|---|---|---|---|
IS offuse_importance_sampling_correction: false | 1 | qv | ρ | 0.48, 0.12 = q − 0.4·p |
| Plain IS correction on, truncated_importance_sampling_type: null | p/q | pv | 1 | 0, 0 IS already removes the pull |
TIS, bounds [0.5, 2]type: tis | clamp(p/q, 0.5, 2) | clamp(pv, 0.5qv, 2qv) = pv here | clamp(1, 0.5ρ, 2ρ) = 0.8 | 0.12, 0.03 = p − 0.8·p |
ICEPOP, bounds [0.5, 2]type: icepop | p/q if inside the bounds, else 0 | pv if p/q inside, else 0 (here: inside) | 1 if 1/ρ inside, else 0 (1/ρ = 2.5: outside → 0) | 0.60, 0.15 = p − 0·p |
| ICEPOP, bounds [0.5, 3] | same | pv | 1 (2.5 is inside) | 0, 0 |
The whole per-token loss in each mode. y is the sampled id, first part = the existing clipped loss, second part = the score-centering term. r = p(y)/pold(y) is the PPO ratio; u = pold(y)/q(y) is the vLLM-to-trainer weight (loss_functions.py L938); ε is the PPO clip. The KL term, if on, is unchanged and left out. Each ℓ is then averaged over tokens.
IS off
Plain IS
TIS [lo, hi]
ICEPOP [lo, hi]
Read across a row: whatever weight the mode puts on the main loss (nothing, u, a clamped u, or a cut-off u), the second part uses the same rule on vLLM's candidates, so the pull it removes is the one that mode's main loss creates (exact on the top-K ids, estimated on the rest). Plain IS is the case where the main loss creates no pull, so the second part is zero. The code computes the second part with the current p where the main part uses pold; on the first step of a batch they are equal.
Bounds come from truncated_importance_sampling_ratio_min and truncated_importance_sampling_ratio
(the four branches).
The PPO catch: the table assumes the main loss really applies weight w. PPO clipping sets the main loss's gradient to 0 on a
clipped token, but c does not know that, so the correction still pushes there. The cancellation is exact only when the PPO ratio is 1,
for example with one optimizer step per batch and force_on_policy_ratio: true.
Example: K = 2, a response of 3 tokens after a 5-token prompt (row length 8). At the token shown, vLLM's top-2 are ids 17 and 4 with q = 0.72, 0.18 (head 0.9); the trainer has p = 0.6, 0.15 at those ids (head 0.75), so ρ = 0.1 ÷ 0.25 = 0.4. Values computed with the real function.
Where they meet: both lanes use the same staging write
(TQTokenSink.stage),
the same finalizer and the same loss object. After the merge point, only two things differ: two extra
columns per token (fetched by the trainer only when centering is on,
_train_fields_for_step),
and one added loss term. Setup rejects the cases it cannot handle: non-vLLM generation, filtered sampling
(top_p<1, top_k), and non-unit temperature without processed logprobs.
top_logprobs>0 without centering is not paid for nothing.
The PR also changed the existing image-capture path, and that change breaks multi-turn VLM runs even with centering off:
see the next page.
🟠 review comment C2 — vllm_worker_async.py:1411 🟡 review comment C6 — config.py:1376