Skip to content

[algo] fix: normalize GDPO advantages over responses#6497

Open
lucky9-cyou wants to merge 1 commit into
verl-project:mainfrom
lucky9-cyou:fix/gdpo-batch-normalization
Open

[algo] fix: normalize GDPO advantages over responses#6497
lucky9-cyou wants to merge 1 commit into
verl-project:mainfrom
lucky9-cyou:fix/gdpo-batch-normalization

Conversation

@lucky9-cyou
Copy link
Copy Markdown

@lucky9-cyou lucky9-cyou commented May 27, 2026

What does this PR do?

This PR updates GDPO's final batch-level advantage normalization to operate over generated responses instead of valid response tokens.
In the current GDPO implementation, compute_grpo_outcome_advantage returns outcome-level advantages with token shape, i.e. one scalar advantage is broadcast to all valid response tokens. The final GDPO normalization then applies:

advantages = verl_F.masked_whiten(new_advantage, response_mask) * response_mask

This computes whitening statistics over all valid tokens. As a result, longer responses contribute more copies of the same outcome-level advantage to the batch mean/std.

Checklist Before Starting

  • Search for similar PRs. Paste at least one query link here: ...
  • Format the PR title as [{modules}] {type}: {description} (This will be checked by the CI)
    • {modules} include fsdp, megatron, veomni, sglang, vllm, rollout, trainer, ci, training_utils, recipe, hardware, deployment, ray, worker, single_controller, misc, perf, model, algo, env, tool, ckpt, doc, data, cfg, reward, fully_async, one_step_off
    • If this PR involves multiple modules, separate them with , like [megatron, fsdp, doc]
    • {type} is in feat, fix, refactor, chore, test
    • If this PR breaks any API (CLI arguments, config, function signature, etc.), add [BREAKING] to the beginning of the title.
    • Example: [BREAKING][fsdp, megatron] feat: dynamic batching

Test

For changes that can not be tested by CI (e.g., algorithm implementation, new model support), validate by experiment(s) and show results like training curve plots, evaluation results, etc.

API and Usage Example

Demonstrate how the API changes if any, and provide usage example(s) if possible.

# Add code snippet or script demonstrating how to use this

Design & Code Changes

Demonstrate the high-level design if this PR is complex, and list the specific changes.

Checklist Before Submitting

Important

Please check all the following items before requesting a review, otherwise the reviewer might deprioritize this PR for review.

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 modifies the advantage calculation in compute_gdpo_outcome_advantage to compute and whiten advantages at the response level rather than the token level. The reviewer identified a critical issue where whitening a single valid sequence (or a batch size of 1) can cause a division-by-zero crash, and provided a code suggestion to safely handle this edge case.

Comment on lines +466 to +468
response_level_advantage = verl_F.masked_mean(new_advantage, response_mask, axis=-1)
response_level_mask = response_mask.sum(dim=-1) > 0
response_level_advantage = verl_F.masked_whiten(response_level_advantage, response_level_mask)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

critical

If the batch size is 1, or if only one sequence in the batch has a valid response (i.e., response_level_mask.sum() is 1), verl_F.masked_whiten will call masked_var which raises a ValueError: The sum of the mask is one, which can cause a division by zero. This will crash the training or validation loop.

To prevent this, we should check if response_level_mask.sum() > 1 before applying masked_whiten. If there is at most one valid sequence, we can safely set the whitened advantages to zero. Additionally, converting response_level_mask to the same dtype as response_level_advantage ensures compatibility across different PyTorch versions and hardware backends.

Suggested change
response_level_advantage = verl_F.masked_mean(new_advantage, response_mask, axis=-1)
response_level_mask = response_mask.sum(dim=-1) > 0
response_level_advantage = verl_F.masked_whiten(response_level_advantage, response_level_mask)
response_level_advantage = verl_F.masked_mean(new_advantage, response_mask, axis=-1)
response_level_mask = (response_mask.sum(dim=-1) > 0).to(response_level_advantage.dtype)
if response_level_mask.sum() > 1:
response_level_advantage = verl_F.masked_whiten(response_level_advantage, response_level_mask)
else:
response_level_advantage = torch.zeros_like(response_level_advantage)

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.

1 participant