Skip to content

feat(sc): add checkpoint save/restore to SingleController - #3138

Merged
yuki-97 merged 3 commits into
NVIDIA-NeMo:yukih/sc-entrypointfrom
haitian-nvidia:haitianj/sc-checkpoint
Jul 14, 2026
Merged

yuki-97 merged 3 commits into
NVIDIA-NeMo:yukih/sc-entrypointfrom
haitian-nvidia:haitianj/sc-checkpoint

Conversation

@haitian-nvidia

@haitian-nvidia haitian-nvidia commented Jul 9, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

Adds checkpoint save/restore to the single-controller (SC) RL loop, so a run can
resume from the latest checkpoint instead of starting over. It reuses the legacy
GRPO checkpoint machinery (CheckpointManager, GRPOSaveState,
load_dataloader_state) and keeps the same on-disk layout, extending it to the
state SC owns:

  • Model weights + optimizer + training state: setup builds a
    CheckpointManager, loads the latest GRPOSaveState (or defaults),
    propagates pretrained_checkpoint, and passes the weights/optimizer resume
    paths into _build_trainer. The actor restores
    _trainer_version/_train_steps/_max_rollout_version and
    consumed_samples/total_valid_tokens, and after each train step saves on
    save_period, the last step, or a checkpoint_must_save_by timeout (top-k
    metric_name supported), stopping early on timeout.
  • Dataloader position + epoch: train_dataloader.pt is written on save and
    restored via load_dataloader_state on setup; current_epoch is persisted
    and restored.
  • Replay buffer (TQReplayBuffer): state_dict fetches each ready group's
    rows from the DataPlane into an envelope; load_state_dict validates and
    truncates to the current capacity before any DataPlane write, then re-puts
    each group. run() restores it before the pumps start.

Notable design points

  • The replay buffer is saved only when over_sampling=True. Under the
    strict quota (over_sampling=False) each weight version gets exactly one
    dispatched batch and the pump never re-dispatches. A checkpoint can only hold
    committed groups, so a partially-committed window could never be completed
    on resume — the train pump would wait forever for groups that will never
    arrive (an immediate stall under force_in_order). Instead, resume restores
    _max_rollout_version = current_step - 1 and regenerates the whole window
    from the restored dataloader position. With over_sampling=True there is no
    quota: restored groups are surplus, missing ones are regenerated by the
    free-running pump, and stale ones are evicted — so persisting the buffer is
    safe and saves wasted generation.
  • Restore never blocks. run() acquires one _buffer_capacity permit per
    restored group before the pumps start; if the restored count exceeded
    capacity, that acquisition would hang run() forever (no pump is running yet
    to release a permit). load_state_dict's truncation to the current
    max_buffered_rollouts guarantees K <= capacity, asserted before acquiring
    — this closes a resume deadlock when resuming onto a smaller-capacity config.
  • The state_dict snapshot is taken synchronously on the event loop before
    any await, so it cannot be torn by the rollout pump committing into the same
    buffer concurrently.
  • In-flight rollouts are not persisted (reserved-but-uncommitted slots),
    matching legacy — they are regenerated on resume.
  • Backward compatible: a checkpoint written before dataloader / replay-buffer
    support (missing train_dataloader.pt / replay_buffer.pt) resumes with a
    fresh dataloader position / empty buffer and a warning, not a crash.

Testing

  • Unit: test_sc_checkpointing.py + test_tq_replay_buffer.py — counter
    restore, save triggers, dataloader position round-trip, and replay-buffer
    round-trip / preflight validation / permit accounting (incl. the
    K == capacity no-hang case).
  • Functional: tests/functional/grpo_checkpoint_single_controller.sh — a
    two-run save→restart on 2 GPUs (Qwen3-0.6B, Megatron), verifying
    weights/dataloader/replay-buffer restore and resume at the correct step
    (step 2 → resume → step 4).

🤖 Generated with Claude Code

Port GRPO weight checkpointing into the single-controller loop:

- setup: build a CheckpointManager, load the latest checkpoint's
  GRPOSaveState (or defaults), propagate pretrained_checkpoint into the
  policy config, and pass weights/optimizer resume paths into
  _build_trainer; carry save_state in SingleControllerBundle.
- actor: restore _trainer_version/_train_steps/_max_rollout_version and
  consumed_samples/total_valid_tokens from save_state; after each train
  step, save on save_period, last step, or checkpoint_must_save_by
  timeout (top-k metric_name supported), and stop early on timeout.
- tests: add test_sc_checkpointing.py; update setup and rollout-pump
  tests for the new bundle field.

Co-Authored-By: Claude Fable 5 <[email protected]>
Signed-off-by: haitian-nvidia <[email protected]>
@haitian-nvidia
haitian-nvidia requested review from a team as code owners July 9, 2026 17:34
@copy-pr-bot

copy-pr-bot Bot commented Jul 9, 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.

Extend SC checkpointing (phase 2 of checkpointing-plan.md) so resumed
runs continue from the saved dataset position instead of replaying
prompts from the top:

- Save: snapshot StatefulDataLoader.state_dict() synchronously on the
  actor event loop (race-free vs the rollout pump iterating the same
  dataloader) and write train_dataloader.pt via asyncio.to_thread
  between policy save_checkpoint and finalize_checkpoint, matching the
  legacy order.
- Restore: setup_single_controller loads train_dataloader.pt via
  load_dataloader_state (dataset-swap guard included) right after the
  dataloader is built; pre-phase-2 checkpoints without the file resume
  with a fresh position and a warning instead of crashing.
- Epoch: the rollout pump's epoch counter is now persisted as
  current_epoch in training_info.json and restored on resume.

Signed-off-by: haitian-nvidia <[email protected]>
Extend SC checkpointing to persist and restore the TQReplayBuffer across a
restart, so committed-but-untrained rollout groups survive a resume instead
of being regenerated. Active only when over_sampling=True; under the strict
quota (over_sampling=False) the restored window can never be completed, so
the buffer is intentionally not saved there.

- state_dict snapshots the ready slots on the event loop, then fetches each
  group's rows from the DataPlane into a {partition_id, saved_capacity,
  groups} envelope.
- load_state_dict validates the envelope and truncates to the current
  capacity before any DataPlane write, then re-puts each group and returns
  the restored count. Load only loads: staleness is left to the train pump's
  first sampler.evict, which drops out-of-window restored groups and
  releases their capacity permits.
- run() restores the buffer before the pumps start and acquires one
  _buffer_capacity permit per restored group; truncation guarantees the
  count never exceeds capacity, so the acquisitions cannot block (a blocking
  acquire here would hang run() with no pump to release).
- save writes replay_buffer.pt between the dataloader save and
  finalize_checkpoint; SingleControllerBundle now carries
  last_checkpoint_path.

Signed-off-by: haitian-nvidia <[email protected]>
@haitian-nvidia haitian-nvidia changed the title feat(sc): add weight checkpoint save/restore to SingleController feat(sc): add checkpoint save/restore to SingleController Jul 13, 2026

@yuki-97 yuki-97 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.

thanks for supporting this, LGTM and merged into SC branch.

@yuki-97
yuki-97 merged commit d1b196c into NVIDIA-NeMo:yukih/sc-entrypoint Jul 14, 2026
15 checks passed
yuki-97 pushed a commit that referenced this pull request Jul 15, 2026
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.

2 participants