Repository navigation
write the latest checkpoint tag only after every rank has saved - #9071
Conversation
rank 0 wrote `latest` as soon as its own files were saved and only then hit the barrier, so a job killed while another rank was still writing its zero shard left `latest` naming an incomplete tag and every restart failed. `latest` was also truncated and rewritten in place, so a kill between the open and the write left it empty. add a barrier before rank 0 writes `latest` (as before deepspeedai#2202) in both the regular and the decoupled commit paths, and write it through a temporary file plus os.replace so readers only ever see the old or the new tag. fixes deepspeedai#9067 Signed-off-by: Dundy Pasupuleti <[email protected]>
|
@sfc-gh-truwase @tohtana could you take a look when you have a chance; this puts the barrier back before rank 0 writes |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 5a638f29fe
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| time.sleep(2) | ||
| return save_zero_checkpoint(*args, **kwargs) | ||
|
|
||
| model._save_zero_checkpoint = slow_save_zero_checkpoint |
There was a problem hiding this comment.
Inject the delay through the checkpoint writer's public contract
This slow-rank test monkey-patches an internal method of the engine being tested. AGENTS.md explicitly requires mocks to represent a collaborator's documented contract, “never for internals of the module under test.” A correct refactor that inlines or renames _save_zero_checkpoint would therefore fail this test despite preserving the public checkpoint-ordering guarantee. Inject the delay through the checkpoint writer's CheckpointEngine.save contract instead, leaving the engine implementation unmodified.
AGENTS.md reference: AGENTS.md:L80-L80
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
good call; 6be87c9 drops the patch on the engine internal and instead wraps checkpoint_engine.save(state_dict, path) on rank 1, sleeping only for that rank's optimizer shard, so the test only leans on the writer contract. ran it on cpu with 2 ranks: passes with the fix and fails on the old engine.py (latest shows up before rank 1's shard).
…test Signed-off-by: Dundy Pasupuleti <[email protected]>
|
I ran the two scripts from #9067 against this PR's head (6be87c9), with 0.20.0 as a control: 2 CPU ranks over gloo, ZeRO stage 1, torch 2.15.0+cpu,
I did not test GPUs or the decoupled checkpoint engine. Output on this PRThis comment was prepared with an AI coding agent. I ran the reproduction on the versions listed above and checked the results. |
|
thanks @asonnino for running both scripts against the head; that matches what i saw on cpu with gloo, and like you i haven't run it on gpus or with the decoupled checkpoint engine. the leftover |
|
No need from my side: the leftover file is harmless, since the loader never reads it, and keeping this PR to the barrier and the atomic write seems easier to review. A sweep could go in a follow-up if the maintainers want one. |
tohtana
left a comment
There was a problem hiding this comment.
Thank you @dundysm! I tested the decoupled engine with two CPU ranks over Gloo. On the second save, latest stayed on the previous checkpoint while rank 1's writer was delayed. Looks good to me.
I'm addressing the shutdown case, where destroy() immediately follows the final save_checkpoint(), in #9155.
57001c1
fixes #9067
what
save_checkpointhad rank 0 write<save_dir>/latestas soon as its own files were on disk, then calldist.barrier(). if the job died while another rank was still writing its zero shard,latestnamed a tag missing that shard and every restart throughload_checkpoint(save_dir)failed until someone editedlatestby hand.latestwas also truncated and rewritten in place, so a kill between theopenand thewriteleft it empty.this change:
dist.barrier()aftercheckpoint_engine.commit()and before rank 0 writeslatest, solatestonly points at a tag once every rank's files for it are written (this is the ordering from before Refactor dist tests: Checkpointing #2202, which moved the barrier after the write). the existing barrier after the write stays, sosave_checkpointstill returns on every rank only afterlatestexists.latestthrough a temporary file (latest.tmp.<pid>),fsync, andos.replace, so a reader sees either the previous tag or the new one, never an empty or partial file._commit_decoupled_checkpoint, which had the same write-then-barrier order (the decoupled engine'scommitonly waits for its own writer process, and only barriers on the first call to measure the folder size).sibling paths checked
TorchCheckpointEngine,FastCheckpointEngine): fixed.save_checkpoint, covered.deepspeed/checkpoint/ds_to_universal.pyalso writeslatest_universalin place, but it is an offline single-process converter with no cross-rank ordering issue; left unchanged to keep this pr focused.why this is not a duplicate
searched open and closed prs for
9067,latest checkpoint barrier,os.replace latest,save_checkpointandlatest in:title; nothing touches this write. the closest are #1486 (single latest writer, merged 2021) and #8501 (zero-3 tag selection on load, a different path).tests
new test
tests/unit/checkpoint/test_latest_checkpoint.py::TestLatestWrittenAfterAllRanks::test_latest_waits_for_slow_rank: two ranks, zero stage 1, rank 1 delays its zero shard save by 2 s, and a watcher thread on rank 0 records whether rank 1's shard exists at the momentlatestfirst names the tag. it also checks that nolatest.*temporary file is left behind.latestnamestag1whilezero_pp_rank_1_mp_rank_00_optim_states.ptdoes not exist yet.hardware and software: cpu only, 8 vcpu intel xeon, 2 ranks over gloo via the cpu accelerator, torch 2.14.1+cpu, python 3.12, deepspeed master at 988b2e3. no gpu was used. the box has no c++ compiler, so the shm comm and fused adam ops could not be built; the test uses
torch_adamand i ran with the shm comm op marked incompatible locally.yapf0.43.0 andflake85.0.4 are clean on both changed files.compatibility matrix