Skip to content

fix: floor GRPO advantage std guard against fp32 leave-one-out residue - #4161

Closed
imxj wants to merge 1 commit into
NVIDIA-NeMo:mainfrom
imxj:jinx/grpo-std-floor
Closed

imxj wants to merge 1 commit into
NVIDIA-NeMo:mainfrom
imxj:jinx/grpo-std-floor

Conversation

@imxj

@imxj imxj commented Sep 17, 2026

Copy link
Copy Markdown

What does this PR do ?

Floor the GRPO/GDPO advantage std guard at 1e-3 so fp32 leave-one-out residue can no longer amplify a singleton's advantage by ~1000x.

Problem

With use_leave_one_out_baseline: true and normalize_rewards: true, GRPOAdvantageEstimator divides
reward - baseline by the leave-one-out std whenever std > 0. calculate_baseline_and_std_per_prompt
computes that std as sqrt(E[x^2] - E[x]^2) in fp32 matmuls. A group that splits 7-vs-1 on a reward that is
not exactly representable in fp32 (0.35, 0.4, 0.3, ...) gives the singleton a leave-one-out set of identical
rewards whose true std is 0 but whose fp32 result is ~1.6e-4. That passes the guard and the singleton's
advantage is divided by the residue: 0.6 / 1.6e-4 ~ 4000x. Observed in a 32x8 web-agent GRPO run
(advantages/max 7428, loss -26, grad_norm 35 on one step). Binary (dyadic) rewards cancel exactly and never hit it.

Fix

Threshold both the GRPO and GDPO guards at STD_FLOOR = 1e-3 (rewards are O(1); a genuine std below that
carries no usable signal). Groups under the floor keep the unnormalized reward - baseline.
Regression test tests/unit/algorithms/test_advantage_std_floor.py reproduces the residue on CPU and checks the
singleton advantage stays at -0.35.

Additional Information

The floor removes the catastrophic exact-zero case only. Leave-one-out std normalization remains unbounded for
continuous rewards: [1, 0 x6, 0.1] still normalizes to 26, [1, 0 x6, 0.01] to 264. Standard GRPO
(use_leave_one_out_baseline: false) is bounded by (G-1)/sqrt(G) and RLOO proper (LOO baseline, no std)
by the reward range. Users with non-binary rewards should pick one of those; a docs note may be worth adding.

With use_leave_one_out_baseline + normalize_rewards, a group that splits
7-vs-1 gives the singleton a leave-one-out set of identical rewards whose
true std is 0. calculate_baseline_and_std_per_prompt computes std as
sqrt(E[x^2]-E[x]^2) in fp32 matmuls, so non-dyadic rewards (0.35) leave a
~1.6e-4 residue that passes the old 'std > 0' guard and the singleton's
advantage is divided by it. Wireup job 1973369 (32x8 web RL, CC v2) step 5
logged advantages/max 7428, loss -26, grad_norm 35 from exactly this.

Threshold both GRPO and GDPO guards at STD_FLOOR = 1e-3 (same fix as the
v1 training branch, b27dcff0c) and add a regression test that reproduces
the residue and checks the singleton advantage stays unnormalized.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Signed-off-by: jinx <[email protected]>
(cherry picked from commit 502853df36041b155fd77577f7c50cb51d9a55de)
@imxj
imxj requested review from a team as code owners September 17, 2026 04:08
@copy-pr-bot

copy-pr-bot Bot commented Sep 17, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@jayl940712

Copy link
Copy Markdown

Adding the following note. This is crucial if training with non-binary intermediate rewards (such as 0.2 out of 0-1).

I found a potentially important interaction between use_leave_one_out_baseline=true and grpo.normalize_rewards=true.

With both enabled, the advantage is calculated using leave-one-out statistics:

(reward - mean(other_rewards)) / std(other_rewards)

For example, consider 16 rollout rewards:

[1.0, 0.0, 0.0, ..., 0.1]

For the rollout with reward 1.0, the leave-one-out group is [0.0, ..., 0.0, 0.1]. Its mean is approximately 0.0067 and its sample standard deviation is approximately 0.0258, producing:

(1.0 - 0.0067) / 0.0258 ≈ 38.5

Therefore, this combination can produce extremely large advantages whenever one rollout is an outlier and the remaining rewards have low variance. This is not limited to non-binary rewards: with [1.0, 0.0, ..., 0.0], the successful rollout’s leave-one-out standard deviation is zero, so its normalized advantage is determined by the implementation’s epsilon or zero-variance handling.

This differs from the standard formulations I found:

GRPO uses reward normalization without a leave-one-out baseline.
RLOO uses a leave-one-out baseline without dividing by the reward standard deviation.

For our bounded [0, 1] reward setting, using normalize_rewards=false would give the standard RLOO-style advantage:

reward - mean(other_rewards)

This keeps the advantage bounded within [-1, 1] and avoids amplification caused by a near-zero leave-one-out standard deviation.

Thus it is not advised to turn both use_leave_one_out_baseline=true and grpo.normalize_rewards=true if using non-binary intermediate rewards.

@svcnvidia-nemo-ci svcnvidia-nemo-ci added the waiting-on-customer Waiting on the original author to respond label Sep 17, 2026
@jayl940712

Copy link
Copy Markdown

Potentially a duplicate with #4158

@svcnvidia-nemo-ci svcnvidia-nemo-ci removed the waiting-on-customer Waiting on the original author to respond label Sep 17, 2026
@aroshanghias-nvd

Copy link
Copy Markdown
Contributor

Superseded by #4158, which detects identical comparison rewards exactly without introducing an arbitrary standard-deviation cutoff. The dynamic-sampling concern raised there should be addressed before merge: #4158 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants