fix(grpo): normalize rewards per query and unique rollout by huanhuan6666 · Pull Request #2446 · THUDM/slime · GitHub
Skip to content

fix(grpo): normalize rewards per query and unique rollout - #2446

Open
huanhuan6666 wants to merge 2 commits into
THUDM:mainfrom
huanhuan6666:fix/episode-grpo-normalization
Open

huanhuan6666 wants to merge 2 commits into
THUDM:mainfrom
huanhuan6666:fix/episode-grpo-normalization

Conversation

@huanhuan6666

Copy link
Copy Markdown

Fixes #2445.

Context compaction can turn one sampled rollout into multiple training rows. BatchBuilder._post_process_rewards currently normalizes the whole batch when the row count differs from rollout_batch_size * n_samples_per_prompt. This mixes unrelated queries. Grouping rows by query alone also overweights episodes that produce more rows.

This change groups by Sample.group_index, computes the mean and optional sample standard deviation over distinct rollout_id values within each query, then broadcasts each episode's normalized reward to its rows in their original order. For example, query A with episode rewards [1, 0] and query B with [0, 0] keep centered rewards [0.5, -0.5] and [0, 0], even when episodes are split during compaction.

  • Preserve fixed-size positional grouping for legacy generators that omit all query IDs; reject partially missing IDs or detectable ambiguous fan-out.
  • Treat missing rollout IDs as independent rows, consistent with BatchBuilder.convert.
  • Return zero for singleton groups and reject conflicting rewards within an episode. Custom reward postprocessing remains available for segment-specific reward semantics.
  • Cover the existing GRPO, GSPO, CISPO and REINFORCE++ baseline normalization modes without changing loss reduction or token masks.

Validation: 86 CPU tests passed, including 27 new reward regressions and the existing trajectory-manager branching, Sample, rollout-data and sample-hook tests. The regression exercises the real TrajectoryManager -> BatchBuilder.convert -> get_grpo_returns path with and without compaction. On the unmodified base, 19 of the 27 new cases fail. The new test file is registered in CPU CI; Black 24.3.0, isort 5.13.2, Ruff 0.14.7 and git diff --check pass. No GPU training run was performed.

Related work: #2062 already proposes the same query/rollout grouping principle; #2204 addresses explicit query grouping. This is a focused implementation against the current BatchBuilder, with native compaction regression coverage and strict handling of ambiguous legacy batches. Both earlier PRs remain unmerged as of this submission preparation.

This branch has not been deployed

No deployments
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.

[Bug] Native-harness compaction breaks episode-level GRPO normalization after trajectory splitting

1 participant