Skip to content

perf(ckpt): move async checkpoint side work off the training thread - #4893

Open
MarioSieg wants to merge 4 commits into
mainfrom
mario/ckpt-async-side-work
Open

MarioSieg wants to merge 4 commits into
mainfrom
mario/ckpt-async-side-work

Conversation

@MarioSieg

@MarioSieg MarioSieg commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Side files in the writer thread. With ckpt.async_save, the checkpoint directory, the model/tokenizer files, mapping.json and each rank's dataloader state (a copy taken at save time) are now written by the background writer thread, before the sharded state, instead of on the training thread with a barrier on the DCP group. Synchronous saves write the same files in the same order as before.
  • Mapping built once. The prime -> HF mapping.json depends only on the model's structure, so it is built on the first save and reused, instead of walking the model state dict on every save.
  • Background deletion. Without a remote, the master deletes past checkpoints (keep_last / keep_interval) in a background thread; removing a large checkpoint can take minutes on a parallel FS. close() waits for pending deletions. With a remote, deletions still go through the sync process as before.
  • Exit during a write. A pending async write is no longer dropped when the trainer fails or is interrupted with Ctrl-C: an exit callback in clean_exit waits up to 10 minutes for it (bounded, since a lost peer would otherwise hold the write's gloo collectives until the DCP timeout), logging rather than raising a failed or unfinished write. wait() takes an optional timeout and keeps the save pending if it times out. SIGTERM/SIGKILL still end the trainer immediately; a resume then skips the incomplete step.
  • Expected win: removes the per-save barrier, mkdir and model/tokenizer/mapping/dataloader file writes from the training step with async saves, and checkpoint deletion from every save step. No config changes; docs updated in docs/training.md.

Test plan

  • uvx ruff check / uvx ruff format --check on changed files
  • pytest tests/unit/train/test_ckpt.py tests/unit/train/test_ckpt_remote.py tests/unit/train/test_checkpoint_writer.py tests/unit/train/test_checkpoint_model_files.py (CPU): 26 passed (test_keep_last_ignores_a_step_cut_off_mid_write now calls close() to join the deleter)
  • CPU script (1 gloo rank, async save with the CUDA stager stubbed out): four async saves with a StatefulDataLoader and keep_last=1; checked the side files are written from the writer thread, the dataloader state on disk matches the state at save time, old steps are deleted in the background, wait(timeout=...) raises TimeoutError and keeps the save pending, and the exit callback waits for the in-flight write, which then completes with .metadata and is registered

Note

Medium Risk
Touches distributed checkpoint I/O, process shutdown ordering, and async thread lifetimes; failures are mostly performance or incomplete checkpoints rather than silent weight corruption, but multi-rank exit paths are subtle.

Overview
Async checkpoint saves no longer do heavy I/O on the training thread. Side artifacts (model/tokenizer files, mapping.json, per-rank dataloader state from a snapshot at save time) are written via shared _write_files, including inside the background writer before DCP shards. Synchronous saves use the same path and ordering.

mapping.json is computed once on the first save and reused instead of rebuilding from the model on every step.

Checkpoint retention cleanup on disk-only runs deletes old steps on the master in a background thread; close() joins that work. Remote sync deletion behavior is unchanged.

Graceful shutdown registers wait_before_exit through clean_exit exit callbacks so Ctrl-C or training failure can wait up to 10 minutes for an in-flight async write (wait(timeout=...) leaves the save pending on timeout). SIGTERM/SIGKILL still exit immediately.

Docs in docs/training.md describe background deletion and exit-during-write behavior.

Reviewed by Cursor Bugbot for commit b3d581b. Bugbot is set up for automated code reviews on this repo. Configure here.

- Write the model files, mapping.json and the dataloader state, and create
  the checkpoint directory, from the async writer thread (synchronous saves
  write them as before); build the HF mapping once per process.
- Delete past checkpoints in a background thread on the master rank when
  there is no remote; close() waits for the deletions.
- On a failure or Ctrl-C, wait up to 10 minutes for a pending async write
  before exiting, so its step is not lost.
@MarioSieg
MarioSieg marked this pull request as ready for review December 3, 2026 19:44

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 4 potential issues.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit b3d581b. Configure here.

remove_remote_step(self.remote, step)
# Another timeline's files of the step would pass for this save's in the remote
if self.world.is_master and step in self.stale_remote_steps:
remove_remote_step(self.remote, step)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Async save skips main-thread barrier

Medium Severity

With async_save, the main-thread barrier that used to follow remove_remote_step is gone. The master still deletes a stale remote step before save_to_path, while other ranks return immediately and enter the next training collective. A slow remote delete can then exceed the process-group timeout and fail the job.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit b3d581b. Configure here.

finally:
# Kept while the write runs, so a wait that times out or is interrupted (Ctrl-C) leaves it pending
if pending.write.done():
self.pending = None

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Timed-out wait drops finished save

Medium Severity

wait clears self.pending whenever the write future is already done, then only calls _register if result did not raise. If the write finishes in the window after a timeout is decided, TimeoutError still propagates, the save is no longer pending, and the finished checkpoint is never registered or uploaded.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit b3d581b. Configure here.

# sys.exit raises SystemExit so the finally block still runs.
sys.exit(1)
finally:
_run_exit_callbacks()

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Ctrl-C shutdown can hang again

Medium Severity

On Ctrl-C, clean_exit runs the checkpoint exit callback and then still calls destroy_process_group. If wait_before_exit hits its 10-minute timeout, the writer thread is still inside the gloo collective, and destroying the process group waits on that collective until the DCP timeout.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit b3d581b. Configure here.

try_rmtree(get_step_path(self.ckpt_dir, step))
if self.deleter is None:
self.deleter = ThreadPoolExecutor(max_workers=1)
self.deleter.submit(try_rmtree, get_step_path(self.ckpt_dir, step))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Exit abandons in-progress checkpoint deletes

Medium Severity

Past checkpoints are removed on a daemon thread, and only close joins that thread. Ctrl-C, a training exception, or SIGTERM kills rmtree mid-directory. is_ckpt_step_complete treats a step directory with no trainer/ as complete, so a half-deleted checkpoint can be resumed as the latest step.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit b3d581b. Configure here.

This branch has not been deployed

No deployments
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.

1 participant