Repository navigation
fix(grpo): group advantages by prompt identity, not prompt tokens - #4495
linj-glitch wants to merge 2 commits into
Conversation
grpo_train and async_grpo_train computed the per-prompt baseline/std with an all-ones valid mask and called adv_estimator.compute_advantage without valid_mask. Rows the environment flags with mask_sample, and rows that already carry loss_multiplier == 0, therefore still voted in their siblings' baseline, and in the dynamic-sampling std / trivial-group check, so a group whose only reward variance came from a masked row was kept as if it had signal. Add _baseline_valid_mask, taken before overlong filtering rewrites loss_multiplier (truncated rollouts keep voting as before), and pass it to the dynamic-sampling baseline/std, calculate_trivial_reward_distributions, and compute_advantage(valid_mask=...) in both loops. This matches the SingleController path, which already passes its final sample mask as valid_mask. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Lin Jia <[email protected]>
grpo_train and async_grpo_train grouped rows for the baseline/std (and for the dynamic-sampling std and trivial-group check) with torch.unique over the first-prompt token rows read back from the rollout. A multi-turn rollout whose first message differs from its siblings' (agent harnesses that rewrite or compact the history) became a singleton group: its baseline equals its reward, so it trains with advantage 0, and its real group shrinks. Identical prompts drawn twice into one batch were also merged into a single group. Name the group explicitly instead. The sync loop builds a [B, 1] id from each row's position in the repeat_interleave'd batch (offset per generation batch so dynamic-sampling caches never collide) before the rollout and carries it in the batch as prompt_group_ids; the async loop uses the position of each replay-buffer entry, which holds exactly one prompt group. Single-turn batches without duplicate prompts group exactly as before. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Lin Jia <[email protected]>
7bc1c69 to
8cd2a5e
Compare
|
Thanks for putting this together. #3882 is the broader fix for grouping GRPO advantages by rollout identity rather than prompt-token equality. To avoid maintaining two competing implementations of the same fix, please close this PR in favor of #3882. The separate valid-mask work in #4490 can continue independently. |
Keep independent prompt draws and rewritten rollout histories in their original GRPO groups. Carry stable keys through legacy dynamic sampling and async replay, and reuse explicit TQ group tags in sync advantage consumers. Adapt the identity regression tests from PR #4495 while keeping its separate valid-mask dependency out of scope. Co-authored-by: Lin Jia <[email protected]> Signed-off-by: Ali Roshan Ghias <[email protected]>
|
Agreed, thanks for landing #3882 (724344f). It does everything this PR set out to do and more: the sync loop mints For the record, this PR's regression tests already live on main, so nothing needs to be ported: the The valid-mask change ( |
linj/grpo-baseline-valid-mask)What does this PR do ?
grpo_trainandasync_grpo_trainnow group rows for the GRPO baseline/std, and for the dynamic-sampling checks, by the prompt each row was generated from. Previously they grouped by the first-prompt tokens read back from the rollout.Issues
None.
Usage
No new configuration.
Before your PR is "Ready for review"
Pre checks:
Additional Information
Depends on:
linj/grpo-baseline-valid-mask(it edits the same lines).Root cause. Both legacy loops derive the group of each row with
torch.unique(prompt_rows, dim=0)insidecalculate_baseline_and_std_per_prompt. Ingrpo_train, the prompt rows for dynamic sampling are the rollout'sinput_ids; for the advantage they are the first-prompt messages extracted withextract_initial_prompt_messages. Inasync_grpo_trainthey are the extracted first-prompt messages. For multi-turn NeMo-Gym rollouts, these rows come from the rollout output, not from the dataset prompt. When an agent harness rewrites or compacts its history, the first message of one rollout can differ from its siblings', even though all of them were generated from the same prompt.How it manifests.
Fix. The group is now named explicitly instead of being inferred from tokens:
grpo_trainbuilds a[B, 1]id from each row's position in therepeat_interleaved batch (_prompt_group_ids) before the rollout. Every rollout path returns rows in input order, and an assertion checks the row count. The ids are offset per generation batch, so groups cached by dynamic sampling never collide with groups from a later batch of the same step. The ids are carried in the batch asprompt_group_ids, so they survive dynamic-sampling filtering, slicing, and caching. They are used for the dynamic-sampling baseline/std,calculate_trivial_reward_distributions, andcompute_advantage(prompt_ids=...).async_grpo_trainuses the position of each replay-buffer entry (_trajectory_group_ids). Each entry holds exactly one prompt group.For single-turn batches without duplicate prompts, the groups are identical to before.
calculate_baseline_and_std_per_promptand the estimators are unchanged; they already accept any[B, k]id tensor.Behavior change to flag. Duplicate prompts in one batch are now separate groups. I believe this matches the GRPO definition (one group per sampled prompt) and the SingleController path, which groups by group id.
Not covered. The TransferQueue path (
grpo_sync.py) still groups by prompt tokens.Tests. New tests in
tests/unit/algorithms/test_grpo.py:TestPromptGroupIds: id shape and dtype, disjoint ids across generation batches, rejection of partial groups, trajectory ids, and a rewritten-first-message row that stays in its group (singleton under token grouping).test_grpo_train_groups_by_prompt_not_by_returned_prompt_tokens: insidegrpo_trainwith dynamic sampling, a rollout whose first message was rewritten still shares its group.test_grpo_train_passes_prompt_group_ids_to_advantage_estimator:grpo_trainandasync_grpo_train.Commands run (CPU only):
🤖 Generated with Claude Code