Repository navigation
Conversation
- 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.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 4 potential issues.
❌ 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) |
There was a problem hiding this comment.
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)
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 |
There was a problem hiding this comment.
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)
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() |
There was a problem hiding this comment.
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)
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)) |
There was a problem hiding this comment.
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)
Reviewed by Cursor Bugbot for commit b3d581b. Configure here.


Summary
ckpt.async_save, the checkpoint directory, the model/tokenizer files,mapping.jsonand 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.jsondepends 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.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.clean_exitwaits 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 optionaltimeoutand keeps the save pending if it times out. SIGTERM/SIGKILL still end the trainer immediately; a resume then skips the incomplete step.docs/training.md.Test plan
uvx ruff check/uvx ruff format --checkon changed filespytest 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_writenow callsclose()to join the deleter)StatefulDataLoaderandkeep_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=...)raisesTimeoutErrorand keeps the save pending, and the exit callback waits for the in-flight write, which then completes with.metadataand is registeredNote
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.jsonis 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_exitthroughclean_exitexit 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.mddescribe 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.