Skip to content

fix(grpo): ReplayBuffer checkpointing and fix reservation leaks - #2651

Merged
yfw merged 8 commits into
mainfrom
amahishi/replay-buffer-watermark-fix
Jun 9, 2026
Merged

yfw merged 8 commits into
mainfrom
amahishi/replay-buffer-watermark-fix

Conversation

@macandro96

@macandro96 macandro96 commented May 31, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do ?

  • Adds replay-buffer checkpointing via state_dict()/load_state_dict(), saved as replay_buffer.pt alongside the dataloader state and restored on resume.

  • Advance last_target_weight_already_generated in sample() (on batch consumption) instead of add(), and release per-target generation reservations only once all spawned workers complete. This prevents reservation leaks and premature target-skipping in the async collector.

Note

Builds on top of #2632 and hence it needs to be merged before this is merged.

Issues

Fixes increase last_target_weight_already_generated only when whole batch is generated and consumed part of #1906

Usage

  • You can potentially add a usage example below
# Add a code snippet demonstrating how to use this

Before your PR is "Ready for review"

Pre checks:

  • Make sure you read and followed Contributor guidelines
  • Did you write any new necessary tests?
  • Did you run the unit tests and functional tests locally? Visit our Testing Guide for how to run tests
  • Did you add or update any necessary documentation? Visit our Document Development Guide for how to write, build and test the docs.

Additional Information on checkpointing

Restore Behavior

On restore, the replay buffer is prepared for the current training step:

  • drops trajectories for past target steps
  • preserves current/future target steps so incomplete restored batches can be gap-filled
  • resets last_target_weight_already_generated to current_step - 1 so the collector re-checks current/future targets by count
  • handles empty restored buffers without skipping the resumed step
  • removes restored trajectories that are stale for their target step when max_age_steps is provided
  • truncates after restore cleanup if current max_size is smaller than the checkpointed buffer

Behavioral Differences From Upstream Commit

  • Empty replay-buffer restore resets last_target_weight_already_generated to current_step - 1.

    • Reason: upstream only prepared the buffer when restored trajectories existed. If the restored buffer was empty but the checkpoint watermark was high, the collector could skip the resumed step and training would wait forever.
  • Restore truncation happens after resume cleanup, not before.

    • Reason: upstream truncated immediately and kept the oldest entries. If max_size shrank, this could keep past-step trajectories and discard current/future ones needed for resume.
  • When current_training_step is known, max-size truncation prioritizes the nearest current/future target steps.

    • Reason: after dropping past targets, the most useful restored trajectories are for the current training step first, then later steps. Keeping oldest-by-index is not necessarily the right behavior after resume.
  • Restore can remove trajectories that are already stale for their target step via max_age_steps.

    • Reason: without this, restored stale trajectories could remain in the buffer until sample() evicts them later. Cleaning them during restore makes the restored state match what training can actually consume.
  • GRPO passes max_trajectory_age_steps into replay-buffer restore.

    • Reason: restore needs the configured staleness window to remove stale restored trajectories consistently.

@macandro96
macandro96 requested review from a team as code owners May 31, 2026 23:51
@macandro96 macandro96 added super-v3 CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version) labels May 31, 2026
@copy-pr-bot

copy-pr-bot Bot commented May 31, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@macandro96
macandro96 force-pushed the amahishi/replay-buffer-watermark-fix branch from 2ba5f1d to de7bbd3 Compare June 1, 2026 14:57
@macandro96

Copy link
Copy Markdown
Contributor Author

/ok to test de7bbd3

@yfw yfw left a comment •

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.

Team review — PR #2651

The replay-buffer checkpointing looks sound: state_dict / load_state_dict are symmetric, the snapshot is taken under the buffer lock, the state is saved alongside the other checkpoint artifacts inside finalize_checkpoint, and resume validates the checkpoint shape, drops past targets, evicts stale trajectories, keeps incomplete future targets for gap-fill, and truncates to max_size without dropping the current step's batch or creating target gaps. Restore from a pre-checkpointing run falls back to an empty buffer gracefully. It's well covered at the unit level (state_dict / load_state_dict round-trips, torch.save round-trip, gap-fill accounting, stale eviction, empty-state reset, max_size change, and validation errors).

Also fixes two real bugs in the async replay buffer: (1) premature reservation release (the old code released at prompt_idx == 0 while later workers were still in flight, letting a subsequent batch re-reserve the same target and over-generate), and (2) premature watermark advancement in add() that could skip a partially-filled target and stall training.

@macandro96 Let's fold the close #2632 and just work on merging this one. Can you also add some docs to docs/guides/async-grpo.md about the replay buffer checkpointing since this is new behavior?

Generated by Claude Code

@macandro96 macandro96 changed the title fix(grpo): release async replay-buffer reservations on batch consumption fix(grpo): ReplayBuffer checkpointing and release async reservations on batch consumption Jun 8, 2026
@macandro96

macandro96 commented Jun 8, 2026 •

Copy link
Copy Markdown
Contributor Author

Team review — PR #2651

The replay-buffer checkpointing looks sound: state_dict / load_state_dict are symmetric, the snapshot is taken under the buffer lock, the state is saved alongside the other checkpoint artifacts inside finalize_checkpoint, and resume validates the checkpoint shape, drops past targets, evicts stale trajectories, keeps incomplete future targets for gap-fill, and truncates to max_size without dropping the current step's batch or creating target gaps. Restore from a pre-checkpointing run falls back to an empty buffer gracefully. It's well covered at the unit level (state_dict / load_state_dict round-trips, torch.save round-trip, gap-fill accounting, stale eviction, empty-state reset, max_size change, and validation errors).

Also fixes two real bugs in the async replay buffer: (1) premature reservation release (the old code released at prompt_idx == 0 while later workers were still in flight, letting a subsequent batch re-reserve the same target and over-generate), and (2) premature watermark advancement in add() that could skip a partially-filled target and stall training.

@macandro96 Let's fold the close #2632 and just work on merging this one. Can you also add some docs to docs/guides/async-grpo.md about the replay buffer checkpointing since this is new behavior?

Generated by Claude Code

Do you think its a good idea to add a function test for replay-buffer checkpointing? @yfw

@macandro96
macandro96 requested a review from a team as a code owner June 8, 2026 16:23
@github-actions github-actions Bot added the Documentation Improvements or additions to documentation label Jun 8, 2026
@macandro96 macandro96 changed the title fix(grpo): ReplayBuffer checkpointing and release async reservations on batch consumption fix(grpo): ReplayBuffer checkpointing and fix reservation leaks Jun 8, 2026
@macandro96
macandro96 force-pushed the amahishi/replay-buffer-watermark-fix branch from 388000e to 60fde00 Compare June 8, 2026 16:25
@macandro96

Copy link
Copy Markdown
Contributor Author

/ok to test 60fde00

@yfw

yfw commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

Do you think its a good idea to add a function test for replay-buffer checkpointing? @yfw

Yes makes sense! If it is easy to do, would be good to have an e2e functional test

@macandro96
macandro96 force-pushed the amahishi/replay-buffer-watermark-fix branch from 6c2a2b2 to 96f50ef Compare June 9, 2026 04:58
@macandro96 macandro96 removed the CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version) label Jun 9, 2026
@macandro96
macandro96 force-pushed the amahishi/replay-buffer-watermark-fix branch from c121045 to 817aaed Compare June 9, 2026 15:00
@macandro96

Copy link
Copy Markdown
Contributor Author

/ok to test 817aaed

Anish Mahishi and others added 8 commits June 9, 2026 11:23
Advance last_target_weight_already_generated in sample() (on batch
consumption) instead of add(), and release per-target generation
reservations only once all spawned workers complete. This prevents
reservation leaks and premature target-skipping in the async collector.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Signed-off-by: Anish Mahishi <[email protected]>
- Add test_process_batch_gap_fill_spawns_only_needed to cover the
  partial gap-fill branch in _process_batch (num_prompts_to_generate <
  num_prompts_in_batch), addressing review feedback from yfw.
- Add Checkpointing section to docs/guides/async-grpo.md documenting
  replay_buffer.pt save/restore behaviour, the restore cleanup sequence,
  and gap-fill after resume.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Signed-off-by: Anish Mahishi <[email protected]>
Add grpo_async_replay_buffer_checkpoint.sh functional test that runs
async GRPO for 2 steps, saves a checkpoint, resumes for 2 more steps,
and asserts the replay buffer was restored from replay_buffer.pt.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Signed-off-by: Anish Mahishi <[email protected]>
Co-authored-by: Terry Kong <[email protected]>
Signed-off-by: Anish Mahishi <[email protected]>
Added a one-line comment so a future reader doesn't "harden" this to weights_only=True and silently break restore

Co-authored-by: Terry Kong <[email protected]>
Signed-off-by: Anish Mahishi <[email protected]>
@macandro96
macandro96 force-pushed the amahishi/replay-buffer-watermark-fix branch from 817aaed to 5442990 Compare June 9, 2026 19:12
@macandro96

Copy link
Copy Markdown
Contributor Author

/ok to test 5442990

@yfw
yfw merged commit 05967a8 into main Jun 9, 2026
155 of 158 checks passed
@yfw
yfw deleted the amahishi/replay-buffer-watermark-fix branch June 9, 2026 23:15
pengdurice pushed a commit to pengdurice/RL that referenced this pull request Jun 12, 2026
…IA-NeMo#2651)

Signed-off-by: Anish Mahishi <[email protected]>
Signed-off-by: Anish Mahishi <[email protected]>
Signed-off-by: Anish Mahishi <[email protected]>
Co-authored-by: Anish Mahishi <[email protected]>
Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
Co-authored-by: Anish Mahishi <[email protected]>
Co-authored-by: Terry Kong <[email protected]>

This branch was previously deployed

3 inactive (1 outdated) deployments
public — 54429907 Deployed Jun 9, 2026 by copy-pr-bot[bot] via release / finalize / publish-docs #893
test — 54429907 Deployed Jun 9, 2026 by copy-pr-bot[bot] via Wait in test approval queue #12158
nemo-ci — de7bbd39 Deployed Jun 1, 2026 by copy-pr-bot[bot] via Docs_Tests #11846
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI:L1 Run doctests, unit tests, and functional tests Documentation Improvements or additions to documentation super-v3

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants