trl icon indicating copy to clipboard operation
trl copied to clipboard

[Feature] Reasoning reward utilities: group-relative length-scaled accuracy (GRPO-LEAD) + graduated format reward

Open surajsharan opened this issue 5 months ago • 10 comments

Feature request

Add two reward functions to trl.rewards, both built on existing primitives:

  1. get_length_scaled_accuracy_reward(alpha=0.5, incorrect_reward=-1.0, reasoning_delimiters=None) : a reward-function factory that wraps reasoning_accuracy_reward and scales the reward of correct completions by their length relative to the other correct completions of the same prompt (a z-score within the GRPO group). Implements the length-aware shaping from GRPO-LEAD (https://huggingface.co/papers/2504.09696).

  2. graduated_format_reward(completions, **kwargs) : a graduated variant of think_format_reward that gives partial credit (1.0 / 0.5 / 0.25 / 0.0) for partial <think>...</think> structure, providing a denser signal during cold-start training when strict-format matches are rare.

Motivation

Length-scaled accuracy : how it differs from the existing get_cosine_scaled_reward. TRL already has get_cosine_scaled_reward (#6066), which also rewards shorter correct completions. The two are complementary, not redundant:

get_cosine_scaled_reward (existing) get_length_scaled_accuracy_reward (proposed)
Length reference absolute token count vs a fixed max_len budget group-relative z-score among the prompt's correct completions
Needs a length budget? yes (max_len) no — adapts to each prompt's own length distribution
Shaping curve cosine interpolation between fixed bounds exp(-alpha * z); a group's correct rewards have geometric mean 1.0
Paper Demystifying Long CoT GRPO-LEAD

The group-relative formulation is useful when there is no good global length budget (mixed-difficulty data): on an easy prompt where every completion is short, and on a hard prompt where every completion is long, GRPO-LEAD still applies the same relative pressure toward the shorter correct answers, because it standardizes within the group. A fixed max_len schedule cannot do that.

Graduated format reward. think_format_reward is binary: a completion that emits only <think> (or both tags in the wrong order) scores exactly 0.0, identical to a completion with no tags at all. Early in training that signal is very sparse. A graduated version gives the policy a gradient to climb toward the target structure. Nothing equivalent exists in trl.rewards today.

Both functions follow existing conventions: get_length_scaled_accuracy_reward mirrors the picklable callable-class factory pattern of get_cosine_scaled_reward / get_soft_overlong_punishment (so it works under the async GRPO rollout worker), and graduated_format_reward sits alongside think_format_reward.

Your contribution

I have an implementation ready (functions, docs, a paper_index.md entry for GRPO-LEAD that cross-references the cosine reward, and unit tests including a picklability test) and will open a PR referencing this issue.

surajsharan avatar Jun 30 '26 10:06 surajsharan

Opened #6222 with the implementation.

surajsharan avatar Jun 30 '26 10:06 surajsharan

Hi @qgallouedec friendly ping on #6222 whenever you have bandwidth. Happy to address any feedback quickly.

surajsharan avatar Jul 06 '26 18:07 surajsharan

Hey @surajsharan, thanks for putting together a ready PR (#6222) for this. It's a clean additive change (new functions in trl.rewards only, no trainer or config surface touched), but so far you're the only one asking for GRPO-LEAD-style group-relative length shaping, so it's queued behind higher-signal items rather than reviewed right away.

Leaving this open. If others hit the same need for group-relative length shaping, a +1 here helps with prioritizing the review.

qgallouedec avatar Aug 20 '26 03:08 qgallouedec

Thanks @qgallouedec, that makes sense. I've updated this PR:

  • Rebased onto latest main. The earlier conflict was just GFPO/PAPO being removed upstream; resolved.

  • Narrowed the scope to only the GRPO-LEAD reward (get_length_scaled_accuracy_reward). I dropped the unrelated graduated_format_reward so it's a single, self-contained paper implementation: new function in trl.rewards only, paper_index.md entry included, no trainer/config surface touched.

On the motivation: this targets overthinking in reasoning models. Within a GRPO group it rewards shorter correct completions over longer ones via a group-relative z-score (exp(-alpha·z)), so it needs no fixed length budget, unlike get_cosine_scaled_reward.

Happy to add a short end-to-end example or benchmark snippet if that'd help. No rush on timing, leaving it ready whenever it fits the queue.

surajsharan avatar Sep 04 '26 18:09 surajsharan

Thanks for narrowing it to the single reward. The wait is still about signal rather than the code: no new askers since my last comment, and GRPO-LEAD is not implemented in verl, axolotl, unsloth or OpenRLHF either.

One thing worth settling while it sits: GRPO standardizes rewards within each group by default, so exp(-alpha*z) gets re-standardized downstream.

https://github.com/huggingface/trl/blob/b0fef2ff4cc00ac9c2076f72e34ce2f6ff1ac07b/trl/trainer/grpo_config.py#L784-L785

At alpha=0.5 the resulting advantages come out nearly identical to plain -z, so alpha only really bites with scale_rewards="none" or alpha above 1.

Maybe a better option is to just provide an example of GRPO-LEAD in our example folder?

qgallouedec avatar Sep 22 '26 23:09 qgallouedec

Thanks @qgallouedec, an example in the examples folder also works, happy to go that route.

On the standardization point: you're right. Under GRPO's default group standardization, exp(-alpha*z) largely collapses to plain -z for a group of all-correct completions. I checked it numerically and at alpha=0.5 the resulting advantages track -z very closely (~0.98 rank correlation), so alpha is mostly cosmetic in that regime.

The one place alpha keeps biting even with standardization is mixed groups (some correct, some incorrect): there it sets how strongly length competes with correctness. At alpha=0.5 the within-correct advantage spread is roughly 3/4 of the correct-vs-incorrect gap, and higher alpha pushes length to dominate. And of course it bites directly under scale_rewards="none". Worth spelling this out in the example so the alpha guidance is honest rather than implying it's a strong knob at defaults.

One scoping question before I write it: would you prefer

  1. a minimal example showing just the length-scaled accuracy shaping (reward defined inline in the script, no trl.rewards surface), or
  2. a fuller GRPO-LEAD reproduction that also includes the difficulty-aware advantage reweighting from the paper, which would touch advantage computation rather than living purely in a reward function?

Happy to build to whichever scope you prefer.

surajsharan avatar Sep 23 '26 10:09 surajsharan

Option 1 please: a minimal example with the reward defined inline, no trl.rewards surface, and the alpha note you wrote above (mostly cosmetic under default group standardization, matters with scale_rewards="none" or mixed groups). The difficulty-aware reweighting would touch GRPO's advantage path, so let's leave it out. You can close #6222 and open the example PR instead, thks!

qgallouedec avatar Oct 02 '26 20:10 qgallouedec

Hi @qgallouedec, I saw that #6222 was closed with the plan to provide an example script in examples/ demonstrating length-scaled reasoning rewards (combating overthinking in GRPO). Since #6222 was closed without the follow-up example PR, I would love to pick this up and submit the example script if you're happy to assign it to me!

pfolex avatar Oct 24 '26 04:10 pfolex

Closing for now since the example hasn't come. @surajsharan if you still want to write it (the minimal version from my last comment), open the PR and link it here.

qgallouedec avatar Dec 02 '26 11:12 qgallouedec

Opened #8745 with the minimal version: reward defined inline in examples/grpo_lead_math/, no trl.rewards surface, and the alpha note in the docstring. Sorry for the delay!

surajsharan avatar Dec 02 '26 17:12 surajsharan