Skip to content

REAL Loss (Rewards as Labels) for GRPO Training#8424

Merged
hjh0119 merged 12 commits intomodelscope:mainfrom
li2zhi:real
Apr 7, 2026
Merged

REAL Loss (Rewards as Labels) for GRPO Training#8424
hjh0119 merged 12 commits intomodelscope:mainfrom
li2zhi:real

Conversation

@li2zhi
Copy link
Copy Markdown
Contributor

@li2zhi li2zhi commented Mar 25, 2026

PR type

  • Bug Fix
  • New Feature
  • Document Updates
  • More Models or Datasets Support

PR information

This PR introduces REAL (Rewards as Labels) as a new loss type for GRPO training, along with corresponding documentation.

REAL reformulates the policy optimization objective into a group-wise classification problem by directly treating rewards as labels, instead of relying on advantage estimation. This approach improves gradient behavior and training stability.

real_framework

Motivation

The standard GRPO loss suffers from two issues:

  • Gradient misassignment: correct but low-confidence tokens receive insufficient updates
  • Gradient domination: a few overconfident incorrect tokens can dominate the gradient
real

REAL addresses these issues by introducing a classification-based objective with bounded and smoother gradient scaling.

Changes

  • Add real as a new option for loss_type
  • Implement REAL loss based on group-wise relative log-probabilities
  • Add temperature hyperparameter real_tau
  • Update documentation with theory, usage, and configuration details

Usage

......
--loss_type real \
--scale_rewards none \
--real_tau 0.5

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request integrates the REAL (Rewards as Labels) loss into the GRPO training framework. By treating rewards directly as labels and formulating policy optimization as a group-wise classification problem, REAL aims to enhance gradient behavior and training stability. This approach specifically addresses common issues in standard GRPO, such as gradient misassignment for positive samples and gradient domination by negative samples, leading to more robust and controlled model updates.

Highlights

  • New Loss Type: Introduced REAL (Rewards as Labels) as a new loss type for GRPO training.
  • Core Implementation: Implemented the REAL loss mechanism, which reframes policy optimization as a group-wise classification problem by directly treating rewards as labels.
  • New Hyperparameter: Added a new hyperparameter, real_tau, to control the temperature for the REAL loss, influencing decision boundary sharpness.
  • Documentation and Examples: Provided comprehensive documentation in both Chinese and English, along with an example training script demonstrating the usage of REAL loss.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces the REAL (Rewards as Labels) algorithm, including new documentation in both Chinese and English, an example training script, and the implementation of the REAL loss calculation within the GRPO trainer. Key feedback points out critical issues in the REAL loss implementation, specifically that it incorrectly uses advantages instead of raw rewards for classification and that the valid_mask logic improperly filters out groups containing only positive or negative samples, leading to incorrect loss computation. Additionally, documentation issues were noted, including incorrect links to the training script and an inaccurate docstring for real_tau regarding the gradient magnitude's upper bound.

Comment thread swift/rlhf_trainers/grpo_trainer.py
Comment thread swift/rlhf_trainers/grpo_trainer.py
Comment thread docs/source/Instruction/GRPO/AdvancedResearch/REAL.md Outdated
Comment thread docs/source_en/Instruction/GRPO/AdvancedResearch/REAL.md Outdated
Comment thread swift/rlhf_trainers/args_mixin.py Outdated
li2zhi and others added 3 commits March 25, 2026 15:06
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@hjh0119 hjh0119 self-assigned this Mar 25, 2026
| Parameter | Type | Default | Description |
|-----------|------|---------|--------------------------------------------------------------------|
| `--loss_type` | `str` | - | Set to `real` |
| `--scale_rewards` | `str` | - | Set to `none` (disable normalization) |
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Maybe we should add parameter checks or auto-set scale_rewards to None when loss_type="real"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done. Thanks!

pos_input = (-scaled_scores).masked_fill(~batch_pos_mask, float('-inf'))
pos_loss = torch.logsumexp(torch.cat([pos_input, zeros], dim=1), dim=1)

loss = (neg_loss + pos_loss).sum() / group_rewards.size(0)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

should we account for the number of valid samples instead?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

In the current implementation, invalid groups are not dropped but contribute zero loss (via masking + logsumexp). Therefore, the objective can be viewed as an expectation over the full data distribution:

$$ \mathcal{L}=\frac{1}{N}\sum_{i=1}^NL_i $$

where invalid groups naturally have: $L_i = 0$

If we instead normalize by the number of valid samples, the objective becomes a conditional expectation over valid groups only, which introduces bias relative to the original sampling distribution.

In addition, since the number of valid samples can vary across batches and training stages, such normalization would lead to unstable gradient scaling (effectively changing the learning rate dynamically).

@hjh0119
Copy link
Copy Markdown
Collaborator

hjh0119 commented Mar 31, 2026

Sorry for the late review. I've left a few minor comments for your reference


# disable normalization, REAL https://arxiv.org/abs/2602.05630
if self.loss_type == 'real':
self.scale_rewards = 'none'
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Perhaps we need to prompt the user that scale_rewards has been overridden (logger.info)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for pointing this out

@hjh0119
Copy link
Copy Markdown
Collaborator

hjh0119 commented Apr 1, 2026

cool! Are there any experimental results?

@li2zhi
Copy link
Copy Markdown
Contributor Author

li2zhi commented Apr 1, 2026

cool! Are there any experimental results?

We are currently reproducing the experimental results using models and datasets that differ from those in the paper. Once we obtain stable results, we will update them in the documentation~

@li2zhi
Copy link
Copy Markdown
Contributor Author

li2zhi commented Apr 7, 2026

Sorry for the delayed update on the experimental results.

Due to limited GPU resources, it is currently infeasible for us to run full-parameter training on larger models under long-context settings. Therefore, we conducted experiments using Qwen2.5-0.5B and selected NuminaMath-TIR as the training dataset. We sampled 4k instances with a fixed random seed (42).

For a fair comparison, the two runs share identical training configurations, with the only difference being the loss_type. The KL coefficient (kl_beta) is set to 0.001 in both cases.

The experimental results are as follows:

Additionally, since the base capability of this relatively small model is limited, its performance on several standard evaluation benchmarks is initially quite low. After applying both GRPO and REAL training, we did not observe significant improvements on these benchmarks, so we have not included those results here.

Methods NuminaMath Math500 AMC23 Avg.
baseline 0.282 0.226 0.072 0.193
GRPO(beta=0.001) 0.334 0.195 0.084 0.204
REAL(beta=0.001) 0.363 0.254 0.075 0.231

@hjh0119
Copy link
Copy Markdown
Collaborator

hjh0119 commented Apr 7, 2026

Thanks for your contribution. Could you also add the corresponding implementation to the Megatron part?

@li2zhi
Copy link
Copy Markdown
Contributor Author

li2zhi commented Apr 7, 2026

I’m happy to add the corresponding implementation for the Megatron part as well. To keep this PR focused and easier to review, would it be acceptable if I implement the Megatron support in a separate follow-up PR?

@hjh0119
Copy link
Copy Markdown
Collaborator

hjh0119 commented Apr 7, 2026

sure, let's merge this one first

@hjh0119 hjh0119 merged commit dab77b4 into modelscope:main Apr 7, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants