Repository navigation
perf: finalize async checkpoints at step boundaries - #4859
Conversation
|
/ok to test 0f9b387 |
|
/ok to test 0a4103f |
There was a problem hiding this comment.
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
blockingcarve-out is rank-uniform. Both inputs are config-derived, and_async_checkpoint_cuda_cache_activehas exactly two writers, both under the same predicate, both fanned out withoutrun_rank_0_only_axes. _pending_checkpointhas no asyncio race. Every mutation runs on the single train-pump task,_save_rollout_checkpointnever touches it, and there are no signal handlers insingle_controller.py.- The duplicated
_finish_pending_saveis the right duplication. Three of its five lines differ for forced reasons — sync vsasyncio.to_thread,(path, step)vspathbecause only the controller writeslatest_checkpoint_status.json, and the lock because_save_rollout_checkpointpublishes from its own task. - The raising
NotImplementedErrordefault is the right shape. The "safe" default ofreturn Truewould be actively wrong for Automodel, whose own docstring warns that renaming beforedcp.async_savecompletes yields a checkpoint missing its optimizer shards. - The
poll_async_savedocstring's NVRx claim is accurate, and understated if anything: at the pinnednvidia-resiliency-extthere 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.
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]>
0a4103f to
5367d2b
Compare
|
/ok to test 8841e8e |
terrykong
left a comment
There was a problem hiding this comment.
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:
- (C8) CI is red: the new SFT v2 tests never enter the loop. One-line fix.
- (C7) The pending save is cleared before waiting for the lock, so a cancel during that wait still loses it.
- (C3) With
save_period: 1, rollout snapshots are skipped almost every time, not just in a short window. Reject the combination. - (C5, C10) Test gaps: the config check's branches, and turning the flag on in the existing SingleController checkpoint functional test.
- (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
…/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]>
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? |
|
/ok to test 4c9bd06 |
…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]>
…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]>
…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]>
…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]>
…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]>
…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]>
…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]>
What does this PR do ?
SFT v2 and the SingleController finalize an async policy save from a
CheckpointManagerbackground thread that calls the blockingfinalize_async_saveon 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) andasync_rl.defer_async_checkpoint_finalize(SingleController), both requiringpolicy.megatron_cfg.checkpoint.async_saveThe save is recorded as pending and the training loop calls
Policy.poll_async_save()(NVRxmaybe_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
GRPO / PPO SingleController (benefits non-colocated generation)
Before your PR is "Ready for review"
Pre checks:
Additional Information
Perf impact recorded on a 32-node SFT v2 run:
.metadata+ barrier)