Repository navigation
Synchronize folder creation; Single latest file writer - #1486
Merged
Merged
Conversation
tjruwase
requested review from
RezaYazdaniAminabadi,
ShadenSmith,
awan-10,
cli99,
conglongli,
eltonzheng,
minjiaz,
niumanar and
samyam
as code owners
October 26, 2021 18:50
jeffra
approved these changes
Oct 26, 2021
jeffra
reviewed
Oct 26, 2021
yh0903
pushed a commit
to yh0903/DeepSpeed
that referenced
this pull request
Nov 24, 2026
…speedai#9071) fixes deepspeedai#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 deepspeedai#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 deepspeedai#1486 (single latest writer, merged 2021) and deepspeedai#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. --------- Signed-off-by: Dundy Pasupuleti <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Mitigate checkpoint save failures in distributed filesystems by (1) synchronizing after folder creation, and (2) having a single writer of latest file.