trl icon indicating copy to clipboard operation
trl copied to clipboard

Add reasoning-focused reward utilities (length-scaled accuracy, gradu…

Open surajsharan opened this issue 5 months ago • 0 comments

What does this PR do?

With reference to the issue https://github.com/huggingface/trl/issues/6221 Adds two reasoning-focused 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, implementing GRPO-LEAD (https://huggingface.co/papers/2504.09696).

Within each group of completions sharing a prompt:

  • the length of every correct completion is standardized to a z-score z (token length from completion_ids when available, else character length);
  • a correct completion receives exp(-alpha * z), so shorter-than-average correct completions get more reward than longer ones (the group's correct rewards have geometric mean ≈ 1.0);
  • incorrect completions receive incorrect_reward (default -1.0), shifting break-even above 50% so guessing only pays once the model is confident;
  • unparseable gold is passed through as None (skips the example), consistent with reasoning_accuracy_reward;
  • alpha=0 recovers a plain {-1, +1} accuracy reward.

It forwards log_extra to reasoning_accuracy_reward and additionally emits a length_z column, so the diagnostic columns (solution, gold_parsed, answer_parsed) continue to appear in the completions table alongside the new one.

Relationship to get_cosine_scaled_reward (#6066). Both favor shorter correct completions, but they are complementary:

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

The docstring and the paper_index.md entry both call out this distinction.

2. graduated_format_reward(completions, **kwargs)

A graduated variant of think_format_reward that gives partial credit for partial <think>...</think> structure, for a denser signal at cold start:

Reward Condition
1.0 matches the strict think_format_reward pattern
0.5 both <think> and </think> present but not the strict pattern (nested tags, closer-before-opener, content before opener)
0.25 exactly one of the two tags present
0.0 neither tag present

Implementation notes

  • get_length_scaled_accuracy_reward is implemented as a picklable callable class (_LengthScaledAccuracyReward, with __name__ = "length_scaled_accuracy_reward"), matching the pattern used for get_cosine_scaled_reward / get_soft_overlong_punishment. This keeps it usable from the async GRPO rollout worker, which pickles reward funcs to a spawned child process (closures can't be pickled).
  • Both functions live in the existing trl/rewards/accuracy_rewards.py and format_rewards.py, exported via trl/rewards/__init__.py — no new modules or abstractions.
  • paper_index.md gains a GRPO-LEAD subsection that cross-references get_cosine_scaled_reward (per the paper-implementation guideline). graduated_format_reward is a shaping utility, not a paper reimplementation, so it gets no paper_index entry.
  • docs/source/rewards.md gets autodoc entries for both.

Tests

tests/test_rewards.py adds TestGraduatedFormatReward (4 cases) and TestLengthScaledAccuracyReward (8 cases: shorter-beats-longer, incorrect punishment, unparseable-gold passthrough, per-group independence, the completion_ids token-length path, alpha=0 fallback, log_extra column-preservation, and picklability). pytest tests/test_rewards.py → 50 passed.

Fixes #6221

Before submitting

  • [ ] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • [x] Did you read the contributor guideline, Pull Request section?
  • [x] Was this discussed/approved via a GitHub issue? Please add a link to it if that's the case. → #6221
  • [x] Did you make sure to update the documentation with your changes?
  • [x] Did you write any new necessary tests?

AI writing disclosure

We welcome the use of AI tools to help with contributions. For transparency and to help us improve our review process, please indicate the level of AI involvement in this PR.

  • [ ] No AI usage: the PR was written entirely by a human.
  • [x] AI-assisted: some parts were suggested or improved by AI, but the PR was written and reviewed by a human.
  • [ ] AI-generated: the PR was mostly or fully generated by an AI tool.

Who can review?

@qgallouedec

surajsharan avatar Jun 30 '26 10:06 surajsharan