fix: normalize rewards per-group when sample counts are unequal#1655
Open
dubin555 wants to merge 1 commit intoTHUDM:mainfrom
Open
fix: normalize rewards per-group when sample counts are unequal#1655dubin555 wants to merge 1 commit intoTHUDM:mainfrom
dubin555 wants to merge 1 commit intoTHUDM:mainfrom
Conversation
…M#1414) When training samples have unequal group sizes, `_post_process_rewards` fell into the else branch which did `rewards.view(-1, rewards.shape[-1])` on a 1D tensor. Since `shape[-1]` equals the total sample count, this reshaped to (1, N), causing `mean(dim=-1)` to compute a single global mean instead of per-group means — making group normalization incorrect. Fix: use `sample.group_index` to identify groups and compute per-group mean (and optionally std) normalization via masked operations. The equal-group-size path (if branch) is unchanged. Closes THUDM#1414
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Based on the verification report and the repository's PR style (which tends to be concise), here's the PR body:
Summary
Fix group-level reward normalization in
_post_process_rewardsthat silently degrades to batch-level normalization when training samples have unequal group sizes (e.g., due to early termination or aborted generations).Root cause: In the
elsebranch (unequal group sizes),rewards.view(-1, rewards.shape[-1])on a 1D tensor of size N produces shape(1, N), somean(dim=-1)computes a single global mean across all samples rather than per-group means. This makes the normalization incorrect — groups with higher raw rewards remain biased high, and groups with lower raw rewards remain biased low.Fix: Use
sample.group_indexto compute per-group mean (and optionally std) via masked operations. The equal-group-size path (reshape-based) is unchanged.Before (buggy):
After (fixed):
Closes #1414
Changes
slime/ray/rollout.py: Replaceview(-1, shape[-1])reshape with per-group masked normalization usinggroup_indextests/test_group_norm_unequal.py: Add regression test suite (5 cases: mean normalization, std normalization, single-sample groups, equal-group backward compatibility, and explicit verification that old logic was buggy)Test plan
test_chunked_gae9/9,test_fsdp_import1/1)