trl icon indicating copy to clipboard operation
trl copied to clipboard

Add UP (Unbounded Positive) loss as a GRPO loss_type

Open yashb98 opened this issue 4 months ago • 2 comments

Feature request

I'd like to add a loss_type="up" option to GRPO, implementing the Unbounded Positive (UP) objective from https://huggingface.co/papers/2607.06987 (Fan et al., ByteDance Seed).

UP routes tokens by the sign of the advantage (Eq. 14 in the paper). For tokens with positive advantage, the importance ratio is replaced with a self-anchored one, π_θ / sg(π_θ). Its forward value is exactly 1 and its gradient is the plain REINFORCE gradient  ∇log π_θ, with no dependence on π_old, so positive updates never get cut by the trust region. Tokens with non-positive advantage keep the normal clipped surrogate, unchanged.

The core of the positive branch is two lines (the rest of the change is dispatch, metrics, and tests):


anchored = per_token_logps - per_token_logps.detach()
loss = -(anchored.exp()) * advantages   # positive-advantage branch

Motivation

TRL already exposes several paper losses through the same loss_type dispatch (cispo, sapo, luspo, vespo), each as a small branch in _compute_loss, and UP slots in the same way. Its negative side is literally the existing dapo clipped surrogate, so most of the code is already there. A side effect is that epsilon_high becomes inert for this loss: with the positive side unbounded, the upper clip can never bind (the paper's UP-DAPO variant simply drops it). The branch doesn't currently warn about that, and I can surface a warning if you'd prefer.

Testing is also cheap here. The positive branch is gradient-equivalent to REINFORCE, so an exact gradient-identity test covers it, not just end-to-end smoke runs.

Your contribution

Working branch: https://github.com/yashb98/trl/tree/feat/grpo-up-loss (diff vs main: https://github.com/huggingface/trl/compare/main...yashb98:trl:feat/grpo-up-loss). It adds:

  • loss_type="up" in GRPOConfig (docstring) plus the _compute_loss branch, with DAPO-style global-token aggregation
  • clip metrics logged on the negative side only, since the positive side has nothing to clip
  • importance_sampling_level="sequence" support, which maps to the paper's UP-GSPO variant (Eq. 16)
  • NotImplementedError on the Liger path for now
  • a paper_index.md entry

Tests cover the gradient identity vs REINFORCE for positive advantages (including a check that the result ignores a stale old_per_token_logps), an exact match with dapo for non-positive advantages, and "up" added to the parametrized loss-type test. They pass locally (CPU runners), and the same branch ran a 200-step-per-arm GPU A/B end to end, so the loss has been exercised in a real training loop, not just unit tests.

That A/B was on my own box (a single GB10): Qwen3-0.6B on GSM8K, 200 steps, dapo vs up, off-policy with num_iterations=4, one seed per arm. The mechanism checks out: dapo's positive-side clip fraction stays nonzero across the run while up never clips positive tokens (by construction there is no ratio to clip), and up's entropy rises through training (last-10-step average about 1.87 vs 0.43 for dapo, both arms starting at 0.47), which is the direction the paper's Fig. 3(a) predicts, with comparable grad norms. Reward did not beat dapo inside 200 steps (about 0.31 vs 0.60 on the last 10 steps). The paper only reports gains at 8 to 14B (plus a 30B MoE and a VLM) and I can't test that scale on this hardware, so I'd treat the run as a mechanism check, not evidence about reward either way. I can share the exact training script.

4-panel A/B: positive-side clip fraction, policy entropy, grad norm, reward (dapo vs up, Qwen3-0.6B, GSM8K, 200 steps) Image

Reward panel included for completeness; see the scale caveat above.

One design question I'd want your take on: the positive branch replaces the IS ratio with the anchored one, so as far as I can tell the ratio-based vLLM logprob-mismatch corrections (the truncate modes) would not act on positive tokens, while the default sequence_maskking happens after the loss dispatch. I can document that or add a warning on thecombination, whichever you prefer.

The branch is ready; happy to open a PR.

yashb98 avatar Jul 15 '26 21:07 yashb98

Follow-up with more data, because the 200-step A/B above had a confound I only caught later. Going back through those logs, about 42% of up's late completions (last 20 logged steps) were hitting the 512-token cap and effectively scoring 0. So that run couldn't tell "exploration doesn't convert to reward" apart from "up's longer chains just need a bigger budget". I re-ran the same A/B at 2000 steps per arm to settle it: same config (Qwen3-0.6B, GSM8K train[:512], seed 42, num_iterations=4, beta=0.0, constant lr 2e-6 with a 10-step warmup), generation through vLLM colocate this time.

The longer run settles it. At 0.6B with beta=0, up collapses, and the completion budget is not the reason. I still think this ships as a loss_type, but with a documented small-scale caveat, and these runs say exactly what that caveat should be.

  • up: entropy goes above 2.0 nats for good by step ~236 (peaking above 6) and reward collapses with it. Through most of the collapsed regime (steps ~400 to 1100) its completions mostly finish (truncation ~7% on average, mean length ~254) and still score ~0.015. Even at the end, where truncation grows back to ~32%, accuracy conditional on finishing is under 2%. Finished-but-wrong output, not a budget artifact.
  • dapo: first touches 2.0 at ~661 but doesn't sustain it until ~990, roughly 4x later than up. Most of its late reward decline is a completion-budget artifact: 68% of its completions truncate over steps 1800 to 2000, while accuracy conditional on finishing dips mid-run but recovers to ~0.63 to 0.69 by the end (run mean ~0.57), never within an order of magnitude of up's.

Mechanistically this is what the loss definitions predict: dapo's positive-side clip fires on ~68% of logged steps here (high-clip fraction up to 0.065) and acts as a brake on entropy growth, and up removes exactly that brake. Worth stating plainly: the paper's UP-DAPO follows DAPO in omitting the KL penalty (beta=0), which is what I ran, so this looks like a small-scale limit of the method as specified rather than an ablation beyond it.

The paper's UP-GRPO variant keeps the KL term, so I ran a third arm to test exactly that: same config, loss_type="up", beta=0.04. It rescues the run completely. Entropy never crosses 2.0 in 2000 steps (boxed around ~1.0 the whole way, max 1.81), the KL term stays bounded (end 0.42, max 0.64), and reward stabilizes at ~0.34 to 0.38. Two honest readings of where that lands against dapo: on raw late-run reward the anchored up arm is actually the best of the three (~0.34 vs dapo's ~0.21, since 68% of dapo's late completions truncate at the 512 cap), while on accuracy conditional on finishing dapo still leads (~0.66 vs ~0.39). Either way the small-scale recipe is clear: up needs beta>0 on small models, and with it the collapse disappears.

One more data point, one scale up: a Qwen3-4B full fine-tune, 500 steps per arm, otherwise the same config. Same direction. Both arms start at ~0.87 reward (GSM8K is nearly saturated for 4B), and dapo holds 0.69 to 0.92 throughout (accuracy conditional on finishing 0.84 to 0.94). up's entropy passes 2.0 by step ~173, truncation blows up to ~51 to 59%, and reward drops to ~0.20 to 0.35 (final last-20: 0.30 vs dapo's 0.69). Interestingly the 4B arm partially self-recovers late (entropy back to ~1.1, conditional accuracy back to ~0.71) instead of dying outright like 0.6B, but it never gets back near dapo. So the beta=0 instability is not a sub-billion artifact; it persists at 4B. Fair caveats: GSM8K leaves little headroom for exploration gains at this scale, and my lr/batch are not the paper's 8 to 14B settings.

curves: 2000-step dapo vs up, the 3-arm KL-anchor overlay, and the 4B run Image Image Image

Extra detail: both 0.6B arms peak early (smoothed reward ~0.7 near step 10). up's grad norm goes to ~0 by the end of the beta=0 run, since all-zero reward groups leave no advantage signal. Caveats: one seed per arm; a different generation backend from the 200-step run, so the within-run comparisons are the clean ones; vLLM logprob handling was left at TRL defaults (no importance-sampling correction configured).

If shipping this as a loss_type with the documented recipe (use beta>0 below the paper's 8 to 14B regime, same doc pattern as the other paper losses) sounds right, I'll fold it into the docs and open the PR.

yashb98 avatar Jul 16 '26 21:07 yashb98

Hey @yashb98, sorry for the slow reply, we're catching up on the backlog. Thanks for the careful write-up, and especially for posting the negative results.

We're going to pass on UP for now, both as a core loss_type and in trl.experimental, so I'm closing #6579 too. It's still a single-requester thread with no 👍 after three months, the method hasn't been adopted by other RL libraries yet, and your own runs show the paper's UP-DAPO recipe collapsing at 0.6B and 4B without a KL term. We're also trimming trl.experimental of paper implementations that didn't gain traction (see #7182), so we'd rather not add new ones on thin signal.

Since your implementation is a self-contained GRPOTrainer subclass overriding _compute_loss, it runs as-is from your own fork or package. Happy to reopen if more people ask for it or the method picks up adoption elsewhere.

qgallouedec avatar Oct 06 '26 16:10 qgallouedec