What score centering adds to token capture and to the policy-gradient loss

NVIDIA-NeMo/RL #4818 (adds loss_fn.score_centering and token_capture.top_logprobs). Permalinks pin head f0d9860e.

Before
Token capture stored only the tokens vLLM sampled. The loss pushed up the sampled token, weighted by its advantage, and nothing removed the pull toward whatever vLLM happened to favor.
This PR
vLLM also returns its top-K candidates per token. They travel with the tokens to the trainer, and the loss adds one term that subtracts the update vLLM's distribution would cause on its own. Off by default.
Status
New feature, in review. The math checks out for all four importance-sampling modes, and GPU CI passes, including the score-centering smoke run and the TP/CP gather checks. Open: a regression in the existing image-capture path (next page), Python extraction on the vLLM event loop, setup checks, and no throughput or convergence numbers yet.

Six pages on this PR, one question each — this one: what the feature adds, next to what was there.

Background. In GRPO/PPO, vLLM samples a response and the trainer then raises the probability of each sampled token in proportion to its advantage. Token capture is the existing path that ships the sampled token ids from vLLM to the trainer. Score centering (arXiv:2609.20807) needs more than the sampled token: it needs vLLM's probability for K candidates at every position. Dotted words have a short explanation: hover or tap them.

Why: the plain update hides a pull toward vLLM

Step 1, the loss NeMo-RL computes. For one sampled token y, the clipped loss is (clipped_pg.py L142–L167):

\ell = \mathrm{sg}[w]\cdot \max\!\big(-A\,r,\; -A\,\operatorname{clip}(r,\,1-\epsilon,\,1+\epsilon)\big), \qquad r = \frac{p(y)}{p_{\text{old}}(y)} = e^{\log p(y) - \log p_{\text{old}}(y)}

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:

\log p(y) = z_y - \log \sum_j e^{z_j} \quad\Longrightarrow\quad \frac{\partial \log p(y)}{\partial z_k} = \mathbf{1}[k = y] - p_k

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:

\underbrace{e_y - p}_{\text{plain push}} \;=\; \underbrace{\htmlClass{t2}{(e_y - q)}}_{\text{signal}} \;+\; \underbrace{\htmlClass{t4}{(q - p)}}_{\text{pull toward vLLM}}

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 17id 4resttotal gap to vLLM
vLLM q0.720.180.10—
trainer p, before0.600.150.250.30
after the pull, A = +10.650.150.210.21 (closer)
after the pull, A = −10.550.150.300.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.

id 17 (sampled) id 4 rest of vocab plain update today push on each logit for sampled id 17 +0.40 -0.15 -0.25 sum 0 = signal id 17 vs. what vLLM expected +0.28 -0.18 -0.10 sum 0 + pull toward vLLM the same whichever id was sampled +0.12 +0.03 -0.15 sum 0 kept by score centering removed by score centering A = +1 · vLLM q: .72, .18, rest .10 · trainer p: .60, .15, rest .25

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 17id 4token atoken b
vLLM q (recorded)0.720.18only the total is known: 0.10
trainer p0.600.150.150.10
estimate of vLLM's q0.72 (exact)0.18 (exact)0.4 × 0.15 = 0.060.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 correctionexact q − ptotal error
0.60, 0.15, 0.15, 0.10 (same top-2 as vLLM)0.12, 0.03, −0.09, −0.060.12, 0.03, −0.07, −0.080.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.0430.52, 0.08, −0.57, −0.030.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.

  1. Write the pull for every id. On average over what vLLM samples, the main loss pushes each id v along ∇log pv with weight qv · wv: how often vLLM picks v, times the weight w the main loss gives v when it is picked. With IS off w = 1, so the weight is just qv.
  2. Name the two kinds of weight. For the top-K ids, qv is recorded, so the weight is known exactly: call it mv ("head weight", mv = qv·wv). For every other id, qv is only estimated as ρ·pv, so its weight is pv times a single factor: call that factor α ("rest factor", α = ρ·w; with IS off α = ρ). m and α are not new physics. They are just q·w written for the two groups of ids.
  3. Move the "rest" onto the top-K ids. Summing α·pv·∇log pv over ~152,000 ids is expensive. But the trainer's probabilities always add up to 1, so Σall v pv∇log pv = ∇(Σ pv) = ∇1 = 0. So the rest's sum is exactly minus the same sum over the top-K ids. The whole pull becomes a sum over the K ids only, with one number per id: cv = mv − α·pv.
  4. Turn that into a loss. To get gradient Σ cv∇log pv, use the loss Σ cv·log pv and treat each cv as a fixed number. That is what sg (stop-gradient, .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.

\mathcal{L}_{\text{new}} = \underbrace{\htmlClass{t1}{\mathcal{L}_{\text{clipped PG}}}}_{\text{(1) unchanged}} \;+\; \underbrace{\htmlClass{t2}{\operatorname{mean}_t\, A_t \sum_{v \in H_t} \mathrm{sg}\!\left[c_{t,v}\right] \log p_t(v)}}_{\text{(2) new}}, \qquad \htmlClass{t3}{c_{t,v} = m_{t,v} - \alpha_t\, p_t(v)}

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 α.

What each importance-sampling mode changes

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 wHead mvRest αc at ids 17, 4
IS off
use_importance_sampling_correction: false
1qvρ0.48, 0.12
= q − 0.4·p
Plain IS
correction on, truncated_importance_sampling_type: null
p/qpv10, 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 hereclamp(1, 0.5ρ, 2ρ) = 0.80.12, 0.03
= p − 0.8·p
ICEPOP, bounds [0.5, 2]
type: icepop
p/q if inside the bounds, else 0pv 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]samepv1 (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

\ell = \htmlClass{t1}{\max\!\big(-A\,r,\; -A\,\operatorname{clip}(r,1{-}\epsilon,1{+}\epsilon)\big)} \;+\; \htmlClass{t2}{A\sum_{v\in H}\mathrm{sg}\big[\,q_v - \rho\,p_v\,\big]\log p_v}

Plain IS

\ell = \htmlClass{t1}{\mathrm{sg}[u]\cdot\max\!\big(-A\,r,\; -A\,\operatorname{clip}(r,1{-}\epsilon,1{+}\epsilon)\big)} \;+\; \htmlClass{t2}{A\sum_{v\in H}\mathrm{sg}\big[\,p_v - p_v\,\big]\log p_v} \;=\; \htmlClass{t1}{\mathrm{sg}[u]\cdot\max\!\big(-A\,r,\; -A\,\operatorname{clip}(r,1{-}\epsilon,1{+}\epsilon)\big)}

TIS [lo, hi]

\ell = \htmlClass{t1}{\mathrm{sg}\big[\operatorname{clip}(u,\,lo,\,hi)\big]\cdot\max\!\big(-A\,r,\; -A\,\operatorname{clip}(r,1{-}\epsilon,1{+}\epsilon)\big)} \;+\; \htmlClass{t2}{A\sum_{v\in H}\mathrm{sg}\big[\operatorname{clip}(p_v,\,lo\,q_v,\,hi\,q_v) - \operatorname{clip}(1,\,lo\,\rho,\,hi\,\rho)\,p_v\big]\log p_v}

ICEPOP [lo, hi]

\ell = \htmlClass{t1}{\mathrm{sg}\big[u\cdot\mathbf{1}[lo \le u \le hi]\big]\cdot\max\!\big(-A\,r,\; -A\,\operatorname{clip}(r,1{-}\epsilon,1{+}\epsilon)\big)} \;+\; \htmlClass{t2}{A\sum_{v\in H}\mathrm{sg}\big[p_v\,\mathbf{1}[lo \le \tfrac{p_v}{q_v} \le hi] - \mathbf{1}[lo \le \tfrac{1}{\rho} \le hi]\,p_v\big]\log p_v}

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.

One token, both paths, same example

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.

token capture today (centering off) with score centering, K = 2 (this PR) 1 · REQUEST TO vLLM {messages:[…], temperature:1.0} Gym sends; RL captures the call {…, logprobs:true, top_logprobs:2} nemo_gym.py: Gym sampling_overrides 2 · vLLM WORKER STAGES THE CALL tokens [3] = [17, 902, 2] (+ image tensors when the model has a processor) one staging write + sampler_topk_token_ids [3,2] = [[17,4],…] int32 + sampler_topk_logprobs [3,2] = [[-0.33,-1.71],…] f32 extract_score_centering_samples (Python, per entry) TQTokenSink.stage(...) — tokens, images and top-K go in the same single put same code 3 · CPU FINALIZER BUILDS THE ROW input_ids [8], token_mask [8] prompt + generated tokens + top-K columns [8,2]; 5 prompt rows = 0 rollout_reassembler.py: zero-fill + shape check 4 · TRAINER FORWARD next_token_logprobs [B, 7] log p(sampled token) + log p at ids [17,4] = [-0.51, -1.90] [B,7,2] get_policy_logprobs_at_sampler_topk (TP/CP gather) 5 · LOSS ClippedPGLossFn — the clipped policy-gradient term, unchanged same code (nothing more) + A · Σ c·log p, c = q − ρp = [0.48, 0.12] metric: score_centering_head_mass = 0.9 shared code added by this PR

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.

What it costs. The two columns are 8·T·K bytes per row (32 MiB for a 32K-token row at K=128), stored and moved twice. On the vLLM side, the per-entry Python extraction runs on the server's event loop: on CPU it took 0.83 s at 8K tokens and 2.7 s at 32K tokens with K=128, during which that server answers no other request. CI shows it runs; no throughput, memory or convergence numbers exist yet. The review asks for those, plus a guard so 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