Skip to content

write the latest checkpoint tag only after every rank has saved - #9071

Merged
tohtana merged 2 commits into
deepspeedai:masterfrom
dundysm:fix-latest-after-all-ranks
Nov 24, 2026
Merged

tohtana merged 2 commits into
deepspeedai:masterfrom
dundysm:fix-latest-after-all-ranks

Conversation

@dundysm

@dundysm dundysm commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

fixes #9067

what

save_checkpoint had rank 0 write <save_dir>/latest as soon as its own files were on disk, then call dist.barrier(). if the job died while another rank was still writing its zero shard, latest named a tag missing that shard and every restart through load_checkpoint(save_dir) failed until someone edited latest by hand. latest was also truncated and rewritten in place, so a kill between the open and the write left it empty.

this change:

  • adds a dist.barrier() after checkpoint_engine.commit() and before rank 0 writes latest, so latest only 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, so save_checkpoint still returns on every rank only after latest exists.
  • writes latest through a temporary file (latest.tmp.<pid>), fsync, and os.replace, so a reader sees either the previous tag or the new one, never an empty or partial file.
  • applies the same change to _commit_decoupled_checkpoint, which had the same write-then-barrier order (the decoupled engine's commit only waits for its own writer process, and only barriers on the first call to measure the folder size).

sibling paths checked

  • regular commit path (TorchCheckpointEngine, FastCheckpointEngine): fixed.
  • decoupled commit path: fixed, same helper.
  • pipeline engine: inherits save_checkpoint, covered.
  • deepspeed/checkpoint/ds_to_universal.py also writes latest_universal in 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_checkpoint and latest 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 moment latest first names the tag. it also checks that no latest.* temporary file is left behind.

DS_ACCELERATOR=cpu LOCAL_SIZE=2 pytest -q tests/unit/checkpoint/test_latest_checkpoint.py -k slow_rank
  • with this change: passes (3/3 runs).
  • on master without the engine change: fails, latest names tag1 while zero_pp_rank_1_mp_rank_00_optim_states.pt does 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_adam and i ran with the shm comm op marked incompatible locally. yapf 0.43.0 and flake8 5.0.4 are clean on both changed files.

compatibility matrix

  • validated: zero stage 1, fp32, cpu, 2 ranks, default torch checkpoint engine.
  • not validated on device: zero 2/3, offload, fp16/bf16, gpu/nccl, and the fast/decoupled checkpoint engines (they need the aio op). the change is only an extra collective barrier on all ranks plus how rank 0 writes one small text file, so it does not depend on zero stage or precision; the extra barrier adds one synchronization per save.

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]>
@dundysm
dundysm marked this pull request as ready for review November 12, 2026 21:45
@dundysm
dundysm requested a review from tjruwase as a code owner November 12, 2026 21:45
@dundysm

dundysm commented Nov 12, 2026

Copy link
Copy Markdown
Contributor Author

@sfc-gh-truwase @tohtana could you take a look when you have a chance; this puts the barrier back before rank 0 writes latest and writes it atomically, fixes #9067

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-12T21:51:05.711274Z 5a638f2 Draft marked ready
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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).

@auto-pr-labeler-with-jev auto-pr-labeler-with-jev Bot added the skip-gpu-test Requires positive evidence that every changed behavior can be validated without a GPU. label Nov 14, 2026
@asonnino

Copy link
Copy Markdown

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, DS_ACCELERATOR=cpu. The PR fixes both cases:

  • deepspeed_latest_before_shards.py (rank 1 delays its ZeRO shard, and the job is killed once latest names global_step2): on 0.20.0 latest names global_step2 before rank 1's shard exists, and the restart fails with ZeRORuntimeException (exit codes [1, 1]). With this PR, global_step2/ holds both shards by then, and the restart loads it ([0, 0]).
  • deepspeed_latest_killpoints.py (the job is killed at five points around the write of latest, 2 trials each): on 0.20.0, a kill after the open or after the write leaves latest empty, and 2 of 2 restarts fail. With this PR, 0 of 2 fail at every point: a kill before the os.replace leaves latest at global_step1, and a kill after it leaves global_step2. A kill between the open and the os.replace also leaves one latest.tmp.<pid> file, which the restart ignores.

I did not test GPUs or the decoupled checkpoint engine.

Output on this PR
$ python deepspeed_latest_before_shards.py
job killed once `latest` named global_step2; global_step2/ holds ['mp_rank_00_model_states.pt', 'zero_pp_rank_0_mp_rank_00_optim_states.pt', 'zero_pp_rank_1_mp_rank_00_optim_states.pt']
run 2 exit codes: [-9, -9]
rank 0: loaded /tmp/tmpr729c3u_/global_step2/mp_rank_00_model_states.pt
rank 1: loaded /tmp/tmpr729c3u_/global_step2/mp_rank_00_model_states.pt
run 3 exit codes: [0, 0]

$ python deepspeed_latest_killpoints.py 2 before_open after_open after_write after_close after_replace
run 1 exit code: 0
before_open trial 0: run 2 exit -9; latest='global_step1'; global_step2/ has 2/2 non-empty ZeRO shards; run 3 exit 0: loaded global_step1
before_open trial 1: run 2 exit -9; latest='global_step1'; global_step2/ has 2/2 non-empty ZeRO shards; run 3 exit 0: loaded global_step1
before_open: 0/2 restarts failed
after_open trial 0: run 2 exit -9; latest="global_step1' (+1 leftover latest.tmp.* file) '"; global_step2/ has 2/2 non-empty ZeRO shards; run 3 exit 0: loaded global_step1
after_open trial 1: run 2 exit -9; latest="global_step1' (+1 leftover latest.tmp.* file) '"; global_step2/ has 2/2 non-empty ZeRO shards; run 3 exit 0: loaded global_step1
after_open: 0/2 restarts failed
after_write trial 0: run 2 exit -9; latest="global_step1' (+1 leftover latest.tmp.* file) '"; global_step2/ has 2/2 non-empty ZeRO shards; run 3 exit 0: loaded global_step1
after_write trial 1: run 2 exit -9; latest="global_step1' (+1 leftover latest.tmp.* file) '"; global_step2/ has 2/2 non-empty ZeRO shards; run 3 exit 0: loaded global_step1
after_write: 0/2 restarts failed
after_close trial 0: run 2 exit -9; latest="global_step1' (+1 leftover latest.tmp.* file) '"; global_step2/ has 2/2 non-empty ZeRO shards; run 3 exit 0: loaded global_step1
after_close trial 1: run 2 exit -9; latest="global_step1' (+1 leftover latest.tmp.* file) '"; global_step2/ has 2/2 non-empty ZeRO shards; run 3 exit 0: loaded global_step1
after_close: 0/2 restarts failed
after_replace trial 0: run 2 exit -9; latest='global_step2'; global_step2/ has 2/2 non-empty ZeRO shards; run 3 exit 0: loaded global_step2
after_replace trial 1: run 2 exit -9; latest='global_step2'; global_step2/ has 2/2 non-empty ZeRO shards; run 3 exit 0: loaded global_step2
after_replace: 0/2 restarts failed

This comment was prepared with an AI coding agent. I ran the reproduction on the versions listed above and checked the results.

@dundysm

dundysm commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

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 latest.tmp.<pid> only shows up when the process dies between the open and the os.replace, and the loader never reads it; if you'd rather have the next save sweep stale ones, i can add that here. @delock could you take a look when you get a chance? this restores the barrier before rank 0 writes latest and makes the write atomic, for #9067.

@asonnino

Copy link
Copy Markdown

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 tohtana 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.

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.

@tohtana
tohtana enabled auto-merge November 23, 2026 23:54
@tohtana
tohtana added this pull request to the merge queue Nov 24, 2026
Merged via the queue into deepspeedai:master with commit 57001c1 Nov 24, 2026
18 of 19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skip-gpu-test Requires positive evidence that every changed behavior can be validated without a GPU.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] save_checkpoint writes latest before other ranks finish their shards

4 participants