Skip to content

perf: finalize async checkpoints at step boundaries - #4859

Merged
terrykong merged 13 commits into
mainfrom
sniverty/sft-v2-defer-async-ckpt-finalize
Oct 29, 2026
Merged

terrykong merged 13 commits into
mainfrom
sniverty/sft-v2-defer-async-ckpt-finalize

Conversation

@snivertynv

Copy link
Copy Markdown
Contributor

What does this PR do ?

SFT v2 and the SingleController finalize an async policy save from a CheckpointManager background thread that calls the blocking finalize_async_save on the policy workers. However, since Ray runs one actor call at a time, the next policy-worker call queues behind the whole disk write. This blocks the next training step from running, defeating the entire purpose of an asynchronous checkpoint save.

We add new opt-in flags sft.defer_async_checkpoint_finalize (SFT v2) and async_rl.defer_async_checkpoint_finalize (SingleController), both requiring policy.megatron_cfg.checkpoint.async_save

The save is recorded as pending and the training loop calls Policy.poll_async_save() (NVRx maybe_finalize_async_save(blocking=False) on every worker) at each step boundary, before the step's first policy-worker call. NVRx agrees on completion with an all-reduce, so the finalize collectives run on all ranks at the same point, and only once every write is done.

The write therefore is now able to actually overlap training while the next save and training exit still finalize blocking. This is the same pattern as Megatron-LM/Bridge's own loops. The SingleController times the poll as timing/train/checkpoint_finalize.

Usage

Both flags are off by default and require Megatron async checkpointing
(policy.megatron_cfg.checkpoint.async_save: true); setup rejects them otherwise.

SFT v2

sft:
  defer_async_checkpoint_finalize: true
policy:
  megatron_cfg:
    checkpoint:
      async_save: true
      ckpt_assume_constant_structure: true   # optional, makes saves after the first much cheaper
uv run examples/run_sft_v2.py \
  --config examples/configs/recipes/vlm/vlm_sft-qwen2.5-vl-3b-instruct-clevr-1n2g-megatrontp1-energon.v1.yaml \
  policy.megatron_cfg.checkpoint.async_save=true \
  sft.defer_async_checkpoint_finalize=true

GRPO / PPO SingleController (benefits non-colocated generation)

async_rl:
  defer_async_checkpoint_finalize: true
policy:
  megatron_cfg:
    checkpoint:
      async_save: true
uv run examples/run_grpo_single_controller.py \
  --config examples/configs/grpo_math_1B_megatron_single_controller.yaml \
  policy.megatron_cfg.checkpoint.async_save=true \
  async_rl.defer_async_checkpoint_finalize=true

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

Perf impact recorded on a 32-node SFT v2 run:

before with the flag
finalize that blocks training 8.1 s right after the save (mostly waiting for the write) ~1.0 s at a later step boundary (gather + rank-0 .metadata + barrier)
disk write inside that wait overlaps training

@snivertynv snivertynv self-assigned this Oct 22, 2026
@snivertynv snivertynv added the CI:L1 Run doctests, unit tests, and functional tests label Oct 22, 2026
@copy-pr-bot

copy-pr-bot Bot commented Oct 22, 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.

@snivertynv snivertynv changed the title perf: finalize async checkpoints at step boundaries (SFT v2 + GRPO single controller) perf: finalize async checkpoints at step boundaries Oct 22, 2026
@snivertynv

Copy link
Copy Markdown
Contributor Author

/ok to test 0f9b387

@snivertynv

Copy link
Copy Markdown
Contributor Author

/ok to test 0a4103f

@saumishr saumishr 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.

The PR is well-built: real before/after numbers, correct opt-in gating, 16 new tests that all pass. Six comments — one bug, five polish/doc items. Nothing I would block on, since defer_async_checkpoint_finalize resolves to false in all 27 shipped single-controller configs.

The one substantive finding: a deferred save is silently dropped when a run raises after the save. With the flag on, begin_finalization is skipped at save time, so teardown has no rename thread to join and a complete tmp_step_N never becomes step_N. It cannot bite anyone today, but the PR advertises the flag in three exemplars.

Checked and found correct — recorded so they are not re-litigated:

  • The blocking carve-out is rank-uniform. Both inputs are config-derived, and _async_checkpoint_cuda_cache_active has exactly two writers, both under the same predicate, both fanned out without run_rank_0_only_axes.
  • _pending_checkpoint has no asyncio race. Every mutation runs on the single train-pump task, _save_rollout_checkpoint never touches it, and there are no signal handlers in single_controller.py.
  • The duplicated _finish_pending_save is the right duplication. Three of its five lines differ for forced reasons — sync vs asyncio.to_thread, (path, step) vs path because only the controller writes latest_checkpoint_status.json, and the lock because _save_rollout_checkpoint publishes from its own task.
  • The raising NotImplementedError default is the right shape. The "safe" default of return True would be actively wrong for Automodel, whose own docstring warns that renaming before dcp.async_save completes yields a checkpoint missing its optimizer shards.
  • The poll_async_save docstring's NVRx claim is accurate, and understated if anything: at the pinned nvidia-resiliency-ext there are two default-group collectives, not one, and mismatched finalization asserts rather than silently diverging.

All 16 new tests pass CPU-only at head (12 plain, 4 mcore-marked). pre-commit is clean.

Comment thread nemo_rl/algorithms/sft_v2.py Resolved
Comment thread nemo_rl/algorithms/sft.py Resolved
SFT v2 finalizes an async save from a CheckpointManager background thread
that calls finalize_async_save(blocking=True) on the policy workers. Ray
runs one actor call at a time, so the next step's train call queues behind
it and training waits for the whole disk write: on 32-node NM4 Nano SFT,
the finalize after each steady-state save blocked training for 8.1 s.

With sft.defer_async_checkpoint_finalize, the controller records the save
as pending and calls Policy.poll_async_save() at each step boundary, which
runs maybe_finalize_async_save(blocking=False) on every worker. NVRx agrees
on completion with an all-reduce, so the finalize collectives run on all
ranks at the same point between steps, and only once every write is done;
the write itself overlaps training. tmp_step_N is renamed to step_N after
that finalize. The next save and training exit still finalize blocking.
On the same setup the finalize then costs ~1 s at a later step boundary
(gathering write results and writing .metadata), instead of 8.1 s.

Off by default. Requires policy.megatron_cfg.checkpoint.async_save; SFT v1
rejects it.

Signed-off-by: sniverty <[email protected]>
…le controller

Same problem as SFT v2: the SingleController finalizes an async policy save
from a CheckpointManager background thread that calls the blocking
finalize_async_save on the policy workers, so the next policy-worker call
queues behind the whole disk write. That thread also submits worker calls
concurrently with the train pump.

With async_rl.defer_async_checkpoint_finalize, the save is recorded as
pending and the train pump polls Policy.poll_async_save() at the start of
each step, before the step's first policy-worker call. Once every rank's
write is done the finalize runs there (timed as checkpoint_finalize), and
tmp_step_N is renamed to step_N under the checkpoint save lock;
latest_checkpoint_status.json is written at that point. The next save and
train-pump exit still finalize blocking. Rollout snapshots already skip
until the matching step_N is published. Only non-colocated generation
benefits: colocated refits finalize before offloading anyway.

The flag requires the Megatron policy backend with
megatron_cfg.checkpoint.async_save. The base worker's poll_async_save now
raises instead of returning True, so a backend without non-blocking
finalize can't report an unfinished write as done.

Signed-off-by: sniverty <[email protected]>
The polling trainer recorded every checkpoint directory at each
save_checkpoint call, which includes the tmp_step_N the controller creates
for the save in progress. Record only published step_N dirs.

Signed-off-by: sniverty <[email protected]>
With defer_async_checkpoint_finalize, a save is published only by a later
step-boundary poll or by the blocking finalize at the end of the loop. A run
that raised after the save, or a single-controller run stopped after a
rollout checkpoint, skipped both, leaving a complete tmp_step_N that resume
never reads. Finalize and publish it during teardown, before the checkpoint
manager closes so its shutdown joins the rename. Teardown failures are
reported without masking the original error.

Tests also check that the single-controller poll never lands inside an
open train step.

Signed-off-by: sniverty <[email protected]>
The poll's finalize all-reduces on the default process group, so it must
run between steps. Enforce that instead of relying on the callers.

Signed-off-by: sniverty <[email protected]>
Document that a crash between a deferred save and its finalize resumes from
the previous checkpoint, add defer_async_checkpoint_finalize to the
single-controller and SFT guides, correct the PPO exemplar comment (the
critic handoff blocks on the finalize once per chunk), and name the remedy
in the SFT v1 rejection.

Signed-off-by: sniverty <[email protected]>
@snivertynv
snivertynv force-pushed the sniverty/sft-v2-defer-async-ckpt-finalize branch from 0a4103f to 5367d2b Compare October 27, 2026 11:35
@github-actions github-actions Bot added the Documentation Improvements or additions to documentation label Oct 27, 2026
@snivertynv

Copy link
Copy Markdown
Contributor Author

Thanks for the thorough review. All items are addressed in 5f93e1b, 5d1b356 and 5367d2b; replies are in each thread.

@snivertynv

Copy link
Copy Markdown
Contributor Author

/ok to test 8841e8e

saumishr
saumishr previously approved these changes Oct 27, 2026

@terrykong terrykong left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the quick fixes. The teardown publish in both paths (5f93e1b) covers the lost-checkpoint case I had staged, including a run that ends because its data runs out. I confirmed that with a test through run(). This review only adds what saumishr's review did not already cover:

  1. (C8) CI is red: the new SFT v2 tests never enter the loop. One-line fix.
  2. (C7) The pending save is cleared before waiting for the lock, so a cancel during that wait still loses it.
  3. (C3) With save_period: 1, rollout snapshots are skipped almost every time, not just in a short window. Reject the combination.
  4. (C5, C10) Test gaps: the config check's branches, and turning the flag on in the existing SingleController checkpoint functional test.
  5. (C9) A save is safe only because publishing comes after every other write into tmp_step_N/. Nothing states or checks that; one helper called last plus a test would.

Visual explainer: https://terrykong.github.io/gh-pages-poc/terryk/pr-4859-overlap-write.html
Visual explainer: https://terrykong.github.io/gh-pages-poc/terryk/pr-4859-lost-last-checkpoint.html
Visual explainer: https://terrykong.github.io/gh-pages-poc/terryk/pr-4859-save-order.html

Generated by Claude Code

Comment thread nemo_rl/algorithms/single_controller.py Resolved
Comment thread tests/unit/algorithms/test_sft_v2.py Resolved
Comment thread nemo_rl/algorithms/single_controller.py Resolved
…/pr4859-r2

# Conflicts:
#	examples/configs/grpo_math_1B_megatron_single_controller.yaml
#	examples/configs/ppo_math_1B_megatron_single_controller.yaml
#	nemo_rl/algorithms/single_controller_utils/config.py
The helper inherited total_steps=10 but set max_steps=3, so run() never
entered the loop and the injected failure never fired. Run exactly the step
after the pending save.

Signed-off-by: sniverty <[email protected]>
- Clear the pending save inside the checkpoint lock, right after
  begin_finalization, instead of before waiting for the lock. A cancel
  during that wait left a finalized tmp_step_N that teardown could no
  longer publish.
- Reject defer_async_checkpoint_finalize with rollout snapshots: a
  snapshot anchors on step_N, which a deferred finalize publishes only
  after training has moved past step N, so snapshots were skipped.
- Test every branch of the deferred-finalize config check, the accepted
  case, and a cancel while waiting for the lock.

Signed-off-by: sniverty <[email protected]>
…lled last

A save is safe only because publishing comes after every other write into
tmp_step_N/: the rename waits for the policy's async write and nothing else.
Route both ways of publishing (background rename, or the deferred pending
mark) through _publish_when_policy_write_done, call it last, and say so in
the single-controller and SFT v2 save paths. Test that the files on disk
when publishing starts are exactly the published step_N/ in both modes, and
that the PPO save publishes after the critic and policy saves.

Signed-off-by: sniverty <[email protected]>
… deferred finalize

Turn on async_rl.defer_async_checkpoint_finalize so the real non-blocking
finalize and the rename after a real async write run on a GPU in CI, and
check that run 2 published the deferred step_2 checkpoint.

Signed-off-by: sniverty <[email protected]>
@snivertynv

Copy link
Copy Markdown
Contributor Author

Thanks for the quick fixes. The teardown publish in both paths (5f93e1b) covers the lost-checkpoint case I had staged, including a run that ends because its data runs out. I confirmed that with a test through run(). This review only adds what saumishr's review did not already cover:

  1. (C8) CI is red: the new SFT v2 tests never enter the loop. One-line fix.
  2. (C7) The pending save is cleared before waiting for the lock, so a cancel during that wait still loses it.
  3. (C3) With save_period: 1, rollout snapshots are skipped almost every time, not just in a short window. Reject the combination.
  4. (C5, C10) Test gaps: the config check's branches, and turning the flag on in the existing SingleController checkpoint functional test.
  5. (C9) A save is safe only because publishing comes after every other write into tmp_step_N/. Nothing states or checks that; one helper called last plus a test would.

Visual explainer: https://terrykong.github.io/gh-pages-poc/terryk/pr-4859-overlap-write.html Visual explainer: https://terrykong.github.io/gh-pages-poc/terryk/pr-4859-lost-last-checkpoint.html Visual explainer: https://terrykong.github.io/gh-pages-poc/terryk/pr-4859-save-order.html

Generated by Claude Code

Thanks @terrykong. All items from your review are addressed in this PR: C8 in b99fdae; C7, C3 and C5 in 82bd876; C9 in 7d55821; C10 in 4c9bd06. I also merged current main. @saumishr, your approval was on 8841e8e; could you take another look at these commits?

@snivertynv

Copy link
Copy Markdown
Contributor Author

/ok to test 4c9bd06

@terrykong
terrykong enabled auto-merge (squash) October 28, 2026 21:58
@terrykong
terrykong merged commit 3921a3d into main Oct 29, 2026
298 of 303 checks passed
@terrykong
terrykong deleted the sniverty/sft-v2-defer-async-ckpt-finalize branch October 29, 2026 08:04
rrs45 added a commit that referenced this pull request Oct 29, 2026
…ing part

`defer_async_checkpoint_finalize` (#4859) moves the finalization handoff out of
`_save_checkpoint` to a later step boundary, so the span no longer wraps the
whole save in that mode. Says what it does wrap, and points at
`rl.checkpoint.finalize` for when the save became resumable.

Signed-off-by: Raj Singh <[email protected]>
rrs45 added a commit that referenced this pull request Oct 30, 2026
…ing part

`defer_async_checkpoint_finalize` (#4859) moves the finalization handoff out of
`_save_checkpoint` to a later step boundary, so the span no longer wraps the
whole save in that mode. Says what it does wrap, and points at
`rl.checkpoint.finalize` for when the save became resumable.

Signed-off-by: Raj Singh <[email protected]>
rrs45 added a commit that referenced this pull request Nov 1, 2026
…ing part

`defer_async_checkpoint_finalize` (#4859) moves the finalization handoff out of
`_save_checkpoint` to a later step boundary, so the span no longer wraps the
whole save in that mode. Says what it does wrap, and points at
`rl.checkpoint.finalize` for when the save became resumable.

Signed-off-by: Raj Singh <[email protected]>
rrs45 added a commit that referenced this pull request Nov 2, 2026
…ing part

`defer_async_checkpoint_finalize` (#4859) moves the finalization handoff out of
`_save_checkpoint` to a later step boundary, so the span no longer wraps the
whole save in that mode. Says what it does wrap, and points at
`rl.checkpoint.finalize` for when the save became resumable.

Signed-off-by: Raj Singh <[email protected]>
rrs45 added a commit that referenced this pull request Nov 2, 2026
…ing part

`defer_async_checkpoint_finalize` (#4859) moves the finalization handoff out of
`_save_checkpoint` to a later step boundary, so the span no longer wraps the
whole save in that mode. Says what it does wrap, and points at
`rl.checkpoint.finalize` for when the save became resumable.

Signed-off-by: Raj Singh <[email protected]>
rrs45 added a commit that referenced this pull request Nov 3, 2026
…ing part

`defer_async_checkpoint_finalize` (#4859) moves the finalization handoff out of
`_save_checkpoint` to a later step boundary, so the span no longer wraps the
whole save in that mode. Says what it does wrap, and points at
`rl.checkpoint.finalize` for when the save became resumable.

Signed-off-by: Raj Singh <[email protected]>
rrs45 added a commit that referenced this pull request Nov 3, 2026
…ing part

`defer_async_checkpoint_finalize` (#4859) moves the finalization handoff out of
`_save_checkpoint` to a later step boundary, so the span no longer wraps the
whole save in that mode. Says what it does wrap, and points at
`rl.checkpoint.finalize` for when the save became resumable.

Signed-off-by: Raj Singh <[email protected]>
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants