Skip to content

Synchronize folder creation; Single latest file writer - #1486

Merged
tjruwase merged 5 commits into
masterfrom
olruwase/single_checkpoint_lastest
Oct 27, 2021
Merged

tjruwase merged 5 commits into
masterfrom
olruwase/single_checkpoint_lastest

Conversation

@tjruwase

Copy link
Copy Markdown
Contributor

Mitigate checkpoint save failures in distributed filesystems by (1) synchronizing after folder creation, and (2) having a single writer of latest file.

Comment thread deepspeed/runtime/engine.py Resolved
@tjruwase
tjruwase merged commit 99bd592 into master Oct 27, 2021
@mrwyattii
mrwyattii deleted the olruwase/single_checkpoint_lastest branch July 7, 2023 02:40
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]>
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.

2 participants