Leave-One-Out Baseline and Advantage Normalization are incompatible
Leave-One-Out Baseline with Advantage Normalization Issue
Describe the bug
When normalize_rewards=True and use_leave_one_out_baseline=True are both enabled in GRPO configuration, normalized advantages can explode to extreme values (e.g., ±1,000,000). This occurs when all leave-one-out baseline samples for a response have identical reward values, resulting in zero standard deviation and division by near-zero epsilon during normalization.
Steps/Code to reproduce bug
- Configure GRPO with both flags enabled:
grpo:
normalize_rewards: true
use_leave_one_out_baseline: true
num_generations_per_prompt: 4
-
Use an environment with sparse or binary rewards (e.g., 0/1 for pass/fail)
-
Generate responses where one response succeeds while others fail:
# Example: 4 responses for the same prompt
rewards = [1.0, 0.0, 0.0, 0.0]
- Observe the normalized advantages output:
Normalized advantages stats: min=-1000000.0000, max=1000000.0000, mean=1953.1133, std=76596.4688
Expected behavior
Advantages should be normalized to reasonable values (typically in the range of [-10, 10]) without numerical explosions.
Root Cause
The issue arises from the mathematical properties of leave-one-out baseline calculation:
-
Leave-one-out baseline calculation: For a response with reward
r_i, the baseline excludes that response:baseline_i = mean(r_1, ..., r_{i-1}, r_{i+1}, ..., r_n) std_i = std(r_1, ..., r_{i-1}, r_{i+1}, ..., r_n) -
The problem case: With rewards
[1.0, 0.0, 0.0, 0.0]:- Response 0 (reward=1.0): baseline computed from
[0.0, 0.0, 0.0]std = 0.0(all values identical)
- Response 1-3 (reward=0.0): baseline computed from
[1.0, 0.0, 0.0]std ≈ 0.577(non-zero variance)
- Response 0 (reward=1.0): baseline computed from
-
Normalization explosion: When normalizing advantages:
normalized_advantage = advantage / (std + epsilon) # For response 0: advantage / (0.0 + 1e-6) = advantage * 1,000,000 -
Why this happens frequently: This is NOT an edge case with sparse/binary rewards:
- Binary rewards (pass/fail): Common in code generation, math problems
- Quantized rewards: Common in discrete evaluation metrics
- Any case where all leave-one-out samples are identical
Mathematical Explanation
The leave-one-out baseline correctly computes std=0 when all leave-one-out samples are identical. This is the mathematically correct behavior for the baseline calculation. However, this creates an inherent incompatibility with advantage normalization:
- Leave-one-out baseline: Can legitimately produce
std=0 - Advantage normalization: Requires
std > 0to avoid division issues
These two features have fundamentally incompatible mathematical properties.
Solutions
Option 1: Raise Error (Current Implementation)
Detect the incompatible configuration and raise an error with a clear message:
if grpo_config["normalize_rewards"] and grpo_config["use_leave_one_out_baseline"]:
raise ValueError(
"normalize_rewards=True and use_leave_one_out_baseline=True are incompatible. "
"Please set one of these to False."
)
Recommendation: Users should choose one of:
normalize_rewards=Truewithuse_leave_one_out_baseline=Falsenormalize_rewards=Falsewithuse_leave_one_out_baseline=True
Option 2: Clamp Standard Deviation (Make Compatible)
Modify calculate_baseline_and_std_per_prompt to clamp std to a minimum value:
std[prompt_idx] = (
(
(prompt_baseline_square - prompt_baseline.square())
* (num_valid / (num_valid - 1))
)
.sqrt()
.nan_to_num(0)
.clamp(min=1.0) # Prevent division issues
)
Semantics: When std < 1.0, set to 1.0, effectively skipping normalization for low-variance samples:
- Zero variance →
normalized_advantage = advantage / 1.0 = advantage(no normalization) - High variance → normal normalization occurs
Option 3: Conditional Normalization
Only normalize advantages where std exceeds a threshold:
def normalize_advantages_with_epsilon(
advantages: torch.Tensor,
std: torch.Tensor,
epsilon: float = 1e-6,
min_std_threshold: float = 1e-2,
) -> torch.Tensor:
normalized = advantages.clone()
mask = std > min_std_threshold
normalized[mask] = advantages[mask] / std[mask].unsqueeze(-1)
return normalized
Additional context
- This issue was discovered with the stats:
min=-1000000.0000, max=1000000.0000, mean=1953.1133, std=76596.4688 - The factor of ~1,000,000 comes directly from
1/epsilon = 1/1e-6 = 1,000,000 - The issue is more prevalent than initially thought - it occurs whenever all leave-one-out samples have identical values, regardless of the number of samples per prompt
- Common scenarios: binary rewards, sparse rewards, quantized rewards, discrete evaluation metrics
- The RLOO paper (https://arxiv.org/abs/2402.14740) focuses on variance reduction but doesn't address this normalization incompatibility
Files Affected
nemo_rl/algorithms/grpo.py: Main GRPO training loopnemo_rl/algorithms/utils.py:calculate_baseline_and_std_per_promptfunction
QA RCCA Analysis
RCCA Status: Done RCCA Category: covered Test Action: none
Fix PR: https://github.com/NVIDIA-NeMo/RL/pull/1519 Root Cause: fix PR includes upstream test coverage. Trigger Config: Leave-One-Out Baseline and Advantage Normalization are incompatible
Coverage Assessment:
| Area | Result |
|---|---|
| Existing Test | Upstream fix PR test coverage |
| Coverage Status | COVERED |
| Gap | N/A |
| Corrective Action | No nmfw_tests addition required; fix PR includes upstream test coverage or equivalent regression coverage. |
Conclusion: RCCA classification completed for this closed NeMo-RL GitHub issue.