Skip to content

[rl] Score centering for DPPOLoss - #6384

Open
tianyu-l wants to merge 1 commit into
verifiers-topk-logprobsfrom
dppo-score-centering
Open

tianyu-l wants to merge 1 commit into
verifiers-topk-logprobsfrom
dppo-score-centering

Conversation

@tianyu-l

@tianyu-l tianyu-l commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary (human-written)

Why

DPPOLoss (#6376) rejects score centering (#6378). Score centering folds the generator's tail (the tokens outside its top-k) into the top-k with one weight, which needs every tail token to share a weight. Under the tail model q_v = rho * p_v every tail token has the ratio 1 / rho, but DPPO also drops a token once sign(A) * (p - q) > divergence_threshold, which reads p itself. On a tail token that is sign(A) * (1 - rho) * p_v.

That term grows with p_v, so the weight at the largest p_v a tail token can have decides for every tail token. Under the tail model that is

p_max = min(trainer tail mass, min_H q / rho)     (a tail token is outside the generator's top-k: rho * p_v = q_v <= min_H q)

If sign(A) * (1 - rho) * p_max stays within the threshold (0.2), no tail token is dropped under the tail model, every tail token gets min(1 / rho, max_ratio), and the existing fold is exact for DPPO. With k = 32, min_H q <= 1/32, so crossing needs rho < 0.14 with A > 0 and a trainer tail mass above 0.2.

Example. k = 32, top-k mass 0.999 on both sides (the default recipes' runs show 1.00):

tail masses     trainer 0.0010, generator 0.0012
rho = 1.2       p_max <= 0.0010, so a tail token moves by at most |1 - 1.2| * 0.0010 = 0.0002 <= 0.2
-> no tail token is dropped; tail weight min(1 / 1.2, 5) = 0.83, alpha = rho * 0.83 = 1

What changed

  • PolicyGradientLoss.log_token_weight(log_ratio, advantages, trainer_logprobs): the weight may read log p. DAPO/GRPO, CISPO and IcePop ignore it (del, mechanical).
  • DPPOLoss.log_token_weight: _dropped's rule with q = p / r, else min(r, max_ratio) on the clamped ratio. Its __post_init__ ban on score_centering is removed.
  • ScoreCentering: passes the trainer's top-k logprobs for the head, and log p_max for the tail weight. Docstring and losses/README.md state when DPPO's fold is exact; past the bound the whole tail counts as dropped.
  • Recipe rl_dppo_score_centering_qwen3_0_6b_varlen in torchtitan_recipes/tests/rl/ (next to the other unverified loss recipes).

Alternatives considered

  • A full-vocabulary tail term (each tail token's own drop decision under the tail model): exact past the bound too, but one more [T, V_local] pass on every step for a case that needs a top-k missing > 20% of the mass.
  • The largest trainer probability among the actual tail tokens: tighter still, but it needs a full-vocabulary masked max (and a TP all-reduce) per step. p_max above costs a [T, k] min.
  • The whole tail's trainer mass alone as p_max: crosses the threshold as soon as the tail masses differ by 0.2, though no single tail token moves that far (the test below fails with it).

Numerics

  • Losses other than DPPO: unchanged ops (they ignore the new argument).

  • DPPO without score centering: unchanged (token_loss is untouched).

  • DPPO with score centering, end to end: rl_dppo_score_centering_qwen3_0_6b_varlen vs rl_dppo_qwen3_0_6b_varlen, 10 steps on real Qwen3-0.6B, seed 42, deterministic, lockstep, trainer TP=2 + generator TP=2 (4x GB300), compiled loss region, not batch-invariant. Both train with finite loss and gradients (mean reward 0.482 -> 0.260 vs 0.482 -> 0.255; ten lockstep steps don't compare learning). No sampled token is dropped or truncated, and coefficient_l1 stays below 4e-10: with the weight equal to r almost everywhere, q * w = p and the correction is ~0, as expected. (Run at the commit before the min_H q / rho bound; with top-k mass 1.00 neither bound binds.)

  • DPPO with score centering under stale rollouts (target_offpolicy_steps=3, mean policy age ~2.9 steps; the same setup as [rl] Score centering: generator top-k logprobs and ScoreCentering for PolicyGradientLoss #6378's table): the trust region drops 0.2-0.4% of tokens and coefficient_l1 is 2.4e-3 to 3.9e-3. So score centering does change DPPO's update there, unlike the lockstep run above. Mean reward over the last 20 of 100 steps, seeds 42/43/44: 0.454 / 0.451 / 0.458 with score centering, 0.455 / 0.437 / 0.421 without. Neither collapsed. That is slightly better and less spread, but too few seeds to claim a gain.

Testing

  • test_score_centering.py: dppo joins the parametrized cases (with a tight trust region, threshold 0.15 and max_ratio 1.5, so the drift tests see dropped and truncated tokens):
    • log_token_weight equals the gradient token_loss puts on each token's score, including the drop rule (catches a weight that drifts from _dropped).
    • Full-vocabulary head, large mismatch: the expected update is 0 with score centering and nonzero without.
    • Top-16 head of 64 with a proportional tail at three tail scales: exactly 0. At tail scale 0.5 the tail masses differ by 0.24 (past DPPO's 0.15 threshold) while no tail token moves past it, so the test fails with the looser tail-mass bound (checked) and with a wrong tail weight or alpha.
    • Generator equal to the trainer: bitwise no-op.
  • test_policy_loss_configs.py builds the new recipe.
  • pytest tests/rl/unit_tests/cpu: 913 passed, 5 skipped

Known issues / follow-ups

Stacked on #6380; next: #6385 (top-k from the label logprob). Past the bound the fold treats the whole tail as dropped, an approximation the README documents.

@pytorch-bot pytorch-bot Bot added ciflow/fake-pg Run 1-GPU Fake PG integration tests ciflow/rl labels Nov 25, 2026
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Nov 25, 2026
@github-actions
github-actions Bot requested a review from pzhan9 November 25, 2026 08:09
@tianyu-l

Copy link
Copy Markdown
Contributor Author

@claude review

@claude

claude Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @tianyu-l's task in 2m 48s —— View job


Needs discussion -- review posted. The tail-bound math checks out, but the only run shows score centering is a no-op for DPPO, and the human-written summary is empty.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: Needs discussion -- The math is correct: below the bound the tail fold is exact for DPPO. But the only end-to-end run shows score centering does nothing for DPPO there (coefficient_l1 < 4e-10), so maintainers need to decide whether DPPO + score centering is worth supporting before it lands.

What this PR does. It lifts DPPOLoss's ban on score_centering.

  • log_token_weight gains a trainer_logprobs argument, so DPPO's weight can apply its drop rule (sign(A) * (p - q) > threshold). DAPO/GRPO, CISPO and IcePop ignore the argument (mechanical).
  • For the tail, ScoreCentering evaluates the weight at p_max = min(trainer tail mass, min_H q / rho). Under the tail model a tail token moves by sign(A) * (1 - rho) * p_v, which is linear in p_v. So if the token at p_max is kept, every tail token is kept, and alpha = rho * min(1/rho, max_ratio) is exact.
  • Past the bound, alpha = 0: the whole tail counts as dropped.

Example. k = 32, A = +1, threshold 0.2:

rho = 1.2, tail masses 0.0010 / 0.0012
  -> (1 - 1.2) * p_max < 0: nothing dropped, alpha = 1.2 * (1/1.2) = 1 (exact, same as main's CISPO path)
rho = 0.1, trainer tail 0.5, min_H q = 0.03
  -> p_max = min(0.5, 0.3) = 0.3, 0.9 * 0.3 = 0.27 > 0.2
  -> alpha = 0, though tail tokens with p_v < 0.22 should keep weight min(10, 5) = 5
Before: DPPOLoss.Config(score_centering=...) raises ValueError.

Must fix

  1. The PR description has an empty "Summary (human-written)" section. Please fill it in.
  2. There is no evidence of value yet. The only run (rl_dppo_score_centering_qwen3_0_6b_varlen vs rl_dppo_qwen3_0_6b_varlen) shows coefficient_l1 < 4e-10, with no token dropped or truncated, so score centering is a no-op for DPPO in that setting. The PR changes the log_token_weight interface on all five losses and adds a recipe for this combination. The description should name the setting where DPPO + score centering changes the update (stale or off-policy rollouts, a quantized generator, any run where DPPO actually drops tokens) and ideally show a run there. Without one, it is a supported combination with no measured effect. That run was also made at the commit before the min_H q / rho bound, so it doesn't cover HEAD's tail code (with top-k mass 1.00, neither bound binds).

Should consider

  • torchtitan/rl/trainer/losses/score_centering.py:148 -- when the tail bound crosses and the "whole tail dropped" approximation turns on, no metric shows it (inline).
  • torchtitan/rl/trainer/losses/dppo.py:84 -- the drop rule is duplicated between log_token_weight and _dropped, and the two compute q differently (inline).
  • torchtitan/rl/trainer/losses/README.md:69 -- this paragraph duplicates the new ScoreCentering docstring paragraph (inline).

Questions

  • Past the bound, is "whole tail dropped" (alpha = 0) better than "whole tail kept" (alpha = rho * w(1/rho))? The large-p_v tail tokens, which carry most of the tail mass, are the ones dropped. But nothing in the PR shows which choice is closer to the full-vocabulary correction.
Details
  • Bound check. Tail tokens lie outside the generator's top-k, so q_v <= min_H q, and under the tail model p_v = q_v / rho <= min_H q / rho. Also p_v <= trainer tail mass.
    • With A < 0, prob_change = (rho - 1) * p_v < rho * p_v = q_v <= 1/k, so for k = 32 it can never cross 0.2.
    • With A > 0, crossing needs (1 - rho) / rho > 6.4, i.e. rho < 0.135. The "0.14" in the docs matches.
    • The bound depends on k: the user can configure num_topk_logprobs down to 1, where min_H q can approach 1. The docs say "e.g. k = 32", which is fine.
  • Head coefficients. log_token_weight(trainer - generator, A, trainer) rebuilds q = exp(log p - log r), which matches _dropped up to fp32 rounding. On masked rows the inputs are 0 and amin gives 0. The coefficients are masked by row_mask anyway, so no NaN reaches the loss.
  • Interface change. Adding trainer_logprobs to every loss for one consumer is justified. The alternative (trainer_logprobs, generator_logprobs) signature would rebuild -log_rho as log p_max - (log p_max + log_rho) for the other losses, which changes their tail rounding (and gives NaN when log p_max = -inf). The current signature keeps CISPO/IcePop/DAPO bitwise unchanged.
  • TP / compile. The top-k logprobs are global after compute_topk_logprobs, amin / minimum are local [T, k] ops, and there are no host syncs.
  • I found no remaining callers or subclasses of log_token_weight beyond the five losses and the test.

Not verified: I couldn't run the CPU tests in this environment, and I have no GPU run of the new recipe at HEAD.

Comment on lines +148 to +155
# tail token is outside the generator's top-k, q_v = rho * p_v <= min_H q.
log_max_tail_trainer_prob = torch.minimum(
log_trainer_tail_mass, generator_logprobs.amin(-1) - log_rho
)
alpha = torch.exp(
log_rho
+ log_token_weight(-log_rho, advantages, log_max_tail_trainer_prob)
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the bound crosses, the approximation turns on with no signal. Past sign(A) * (1 - rho) * p_max > divergence_threshold, alpha becomes 0 for the row, so the whole tail counts as dropped. None of the metrics show when this happens: coefficient_l1 stays nonzero and the top-k masses don't say which rows crossed. The README calls this a documented approximation, but a user running stale or quantized rollouts, where rho can get small, has no way to tell whether it is active.

Two options:

  • Report the fraction of loss rows whose tail weight is -inf (e.g. score_centering/tail_zero_weight_frac). For IcePop that rows-outside-band case is exact, so the metric means "tail fully zero-weighted", not "approximate".
  • If that metric isn't wanted, say in the description why invisibility is fine here.

Comment thread torchtitan/rl/trainer/losses/dppo.py Outdated
Comment on lines +83 to +87
# _dropped's rule, with p_gen = p / r.
prob_change = advantages.sign() * (
torch.exp(trainer_logprobs) - torch.exp(trainer_logprobs - log_ratio)
)
# The weight reads PolicyGradientLoss's ratio, whose |log r| is clamped.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The drop rule now lives in two places: here, and in _dropped (line 103). The two even compute q differently (exp(log p - log r) here, exp(log q) there). The gradient-match test guards them for now, but any change to the rule, such as the binary-KL TODO, has to edit both.

A small module-level _prob_change(trainer_logprobs, generator_logprobs, advantages) could serve both. log_token_weight would call it with trainer_logprobs - log_ratio, and _dropped with the ScoredTokens fields. That keeps one copy of the rule at about the same LoC.

Comment thread torchtitan/rl/trainer/losses/README.md Outdated
Comment on lines +69 to +74
- `DPPOLoss`: its trust region makes a tail token's weight depend on its own `p_v`, but the
top-k tail model folds the tail with one weight. Under the tail model a tail token moves by
`(1 - rho) * p_v`, and `p_v` is at most `min(trainer tail mass, min_H q / rho)` (a tail
token is outside the generator's top-k), so the fold is exact unless that bound moves past
`divergence_threshold` (0.2). With k = 32, `min_H q <= 1/32`, so that needs `rho < 0.14`
with `A > 0` and a trainer tail above 0.2. Past it the whole tail counts as dropped.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This paragraph repeats ScoreCentering's new docstring paragraph (score_centering.py:46-52) almost word for word, including the k = 32 / rho < 0.14 derivation. Line 51 of this README already points readers to the docstring for the coefficient formula. Keeping the derivation in one place avoids the two copies drifting apart. One option is a one-line bullet here ("DPPOLoss: exact unless a tail token can move past divergence_threshold; see ScoreCentering"), with the derivation kept only in the docstring.

@tianyu-l

Copy link
Copy Markdown
Contributor Author

@claude review again

@claude

claude Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @tianyu-l's task in 1m 24s —— View job


Approve -- please address the items before merging -- review posted.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: Approve -- please address the items below before merging. The stale-rollout run shows that score centering changes DPPO's update there, and the drop rule now has a single copy. What's left are edits to the description and the README. A maintainer still needs to review.

What changed since the last review (5db4552, one commit, force-pushed):

  • Value: resolved. With target_offpolicy_steps=3, the trust region drops 0.2-0.4% of tokens and coefficient_l1 is 2.4e-3 to 3.9e-3. So the combination is no longer a measured no-op. The reward gain over 3 seeds is within noise, and the description says so.
  • Duplicated drop rule: resolved. _dropped(trainer_logprobs, generator_logprobs, advantages) now serves token_loss, token_metrics and log_token_weight. log_token_weight passes log q = log p - log r.
  • README duplicating the docstring: resolved. It is now a short pointer to ScoreCentering's docstring, but the new wording is ambiguous (inline).

Example. Same as before. With k = 32 and A = +1, the tail at rho = 1.2 is kept, with alpha = 1 (exact). At rho = 0.1 with a trainer tail of 0.5, alpha = 0. Before this PR, DPPOLoss.Config(score_centering=...) raised.

Must fix

  1. The "Summary (human-written)" section is still empty (still open).
  2. torchtitan/rl/trainer/losses/README.md:71-73 -- "that needs rho < 0.14" reads as the condition for exactness rather than for crossing, and the tail-mass condition was dropped (inline, with a suggestion).

Should consider

  • torchtitan/rl/trainer/losses/score_centering.py:148 -- still open, no change in code: no metric shows when the "whole tail dropped" approximation turns on. With top-k mass near 1.00 in every run so far, the bound has never bound. A user with a small num_topk_logprobs or a quantized generator has no signal that it does.

Questions

  • Still open: past the bound, why is alpha = 0 (whole tail dropped) preferred over alpha = rho * w(1/rho) (whole tail kept)?
  • Was the stale-rollout run made at HEAD? Even if it was, top-k mass near 1.00 means neither tail bound binds there, so HEAD's min_H q / rho path has CPU test coverage only.
Details
  • _dropped's arguments in log_token_weight. On the head, trainer_logprobs - (trainer_logprobs - generator_logprobs) equals generator_logprobs up to fp32 rounding, which the gradient-match test covers. On the tail, q = p_max * rho, which matches the docstring.
  • Edge cases. A -inf top-k generator logprob gives log p_max = -inf, so p = q = 0: the token is not dropped and alpha stays finite. Masked rows are zeroed by row_mask.
  • Tests. The proportional-tail test moved from vocab 16 / top-4 to vocab 64 / top-16 with flatter logits, for every loss. At tail scale 0.5 it covers the below-bound case where the tail masses differ by more than the threshold. Past the bound (alpha = 0), the approximation is not tested, which is reasonable since it isn't exact.

Not verified: I couldn't read replies on the earlier inline threads or run the CPU tests in this environment.

Comment on lines +71 to +73
- `DPPOLoss`: its trust region reads each tail token's own `p_v`, so the tail fold is exact
only while no tail token can move past `divergence_threshold` (see `ScoreCentering`'s
docstring). With k = 32 that needs `rho < 0.14` with `A > 0`.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After the trim, "that needs rho < 0.14" can be read the wrong way round. "That" is meant to be the crossing (a tail token moving past the threshold), but it reads as the condition for the fold to be exact. The trim also dropped the second condition (trainer tail mass above 0.2), which the docstring and the PR description both state. Saying what breaks exactness makes it unambiguous:

Suggested change
- `DPPOLoss`: its trust region reads each tail token's own `p_v`, so the tail fold is exact
only while no tail token can move past `divergence_threshold` (see `ScoreCentering`'s
docstring). With k = 32 that needs `rho < 0.14` with `A > 0`.
- `DPPOLoss`: its trust region reads each tail token's own `p_v`, so the tail fold is exact
only while no tail token can move past `divergence_threshold` (see `ScoreCentering`'s
docstring). With k = 32 a tail token can cross only if `rho < 0.14`, `A > 0` and the
trainer's tail mass is above 0.2; past that the whole tail counts as dropped.

@tianyu-l
tianyu-l force-pushed the dppo-score-centering branch from 27b167c to 9d5cf4d Compare November 25, 2026 23:15
@felipemello1

Copy link
Copy Markdown
Contributor

Reply from claude: image

This PR lets DPPOLoss use score centering by checking DPPO's drop rule once, at the largest probability a tail token can have, and with k = 32 that check can only fail if rho < 0.14, A > 0 and the trainer's tail mass is above 0.2.

@tianyu-l
tianyu-l force-pushed the dppo-score-centering branch from 9d5cf4d to d9bf505 Compare November 26, 2026 04:08
DPPO's trust region makes a token's weight read p as well as p / q, so
log_token_weight takes the token's trainer logprob. The top-k tail is
folded with the weight at the tail's whole trainer mass: exact while the
two tail masses differ by at most the trust-region threshold.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/fake-pg Run 1-GPU Fake PG integration tests ciflow/rl CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants