Skip to content

[BugFix] Zero GRPO advantage for groups of identical fractional rewards - #4610

Merged
theap06 merged 1 commit into
pytorch:mainfrom
catherinelee274:grpo-uniform-reward-advantage
Oct 24, 2026
Merged

theap06 merged 1 commit into
pytorch:mainfrom
catherinelee274:grpo-uniform-reward-advantage

Conversation

@catherinelee274

Copy link
Copy Markdown
Contributor

Description

MCAdvantage normalizes each group as (r - mean) / std.clamp_min(1e-6). When every reward in a group is the same fraction (0.7, 0.3, 1/3, ...), the float32 mean is off by a rounding step. That leaves r - mean and std both around 1e-8, and dividing the residue by the 1e-6 clamp gives every sample in the group the same nonzero advantage, when it should be zero:

setup advantage before
8 trajectories, all rewards 0.7, per-step mode 0.0596
same, trajectory_return="sum" 0.0596
same, plus keep_return_bounds=(0, 1) 0.0596
raw formula, 1024 values of 0.9 0.238

Every sample gets the same sign, so the update pushes the whole group up or down when the group carries no learning signal. Dynamic sampling doesn't catch this: the group's mean falls inside the usual bounds, which only drop all-0 or all-1 groups. The same issue was reported in miles#4019.

Fix

A new private helper, _group_mean_std, reduces the group after shifting it by its minimum. Identical values then have an exact mean and an exactly zero std, so their advantage is exactly 0. There is no tolerance to tune and no Python branch on tensor values, so the fix stays compile-friendly. The variance is unchanged by the shift, and for non-uniform groups the result matches the previous one up to rounding. The helper is used in both the per-step path and the trajectory_return path.

Tests

  • Added TestMCAdvantage::test_mc_advantage_uniform_fractional_rewards, run in per-step and trajectory_return="sum" mode. It asserts that a group of identical 0.7 rewards gets an advantage of exactly zero. Both cases fail on main and pass with this change.
  • pytest test/llm/test_llm_objectives.py -k MCAdvantage: 25 passed.

🤖 Generated with Claude Code

MCAdvantage normalized each group as (r - mean) / std.clamp_min(1e-6).
When every reward in a group is the same fraction (e.g. 0.7), the
float32 mean is off by a rounding step, so r - mean and std are both
~1e-8. Dividing that residue by the 1e-6 clamp gave every sample in the
group the same nonzero advantage (0.06 for a group of 8 at 0.7, up to
0.24 for larger groups), biasing updates on groups that carry no
learning signal. Dynamic sampling does not catch this, since such a
group's mean lies inside the usual keep_return_bounds.

Reduce the group shifted by its minimum instead: identical values then
have an exact mean and a zero std, and their advantage is exactly zero.
Applies to both the per-step and the trajectory_return paths.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@pytorch-bot

pytorch-bot Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/rl/4610

Note: Links to docs will display an error until the docs builds have been completed.

❌ 1 New Failure

As of commit b1712a0 with merge base b763b5c (image):

NEW FAILURE - The following job has failed:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla

meta-cla Bot commented Oct 23, 2026

Copy link
Copy Markdown

Hi @catherinelee274!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@github-actions github-actions Bot added BugFix Objectives llm/ LLM-related PR, triggers LLM CI tests and removed BugFix labels Oct 23, 2026

@catherinelee274 catherinelee274 left a comment

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.

I have signed the cla.

@theap06 theap06 left a comment •

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.

Thanks for contributing and catching the rounding issue! Just waiting on CI.

@meta-cla

meta-cla Bot commented Oct 23, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 23, 2026
@catherinelee274

catherinelee274 commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor Author

Seems the failing test is not related to my change: Could not load credentials from any providers. Could a contributor fix this

@theap06

theap06 commented Oct 23, 2026 •

Copy link
Copy Markdown
Collaborator

We have been meaning to fix this as this issue. this issue stems from a migration from ec2 to osdc jobs. However, this job isn't really relevant for your diff here.

@theap06
theap06 merged commit 13da395 into pytorch:main Oct 24, 2026
155 of 156 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. llm/ LLM-related PR, triggers LLM CI tests Objectives

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants