Skip to content

[release/2.14] Fix post-local SGD checkpoint reload race (#194614) - #4061

Merged
jeffdaily merged 1 commit into
release/2.14from
albmalamd/cherry-pick/2.14/194614-fix-post-localsgd-reload-race
Nov 23, 2026
Merged

jeffdaily merged 1 commit into
release/2.14from
albmalamd/cherry-pick/2.14/194614-fix-post-localsgd-reload-race

Conversation

@albmalamd

@albmalamd albmalamd commented Nov 23, 2026 •

Copy link
Copy Markdown

Summary

Fixes pytorch#84886.

There was a race in the post-local SGD checkpoint reload test. Rank 0 created the temporary checkpoint and all ranks synchronized after it was written. However, there was no synchronization after loading. Rank 0 could complete the test first, leave the temporary-file context, and delete the checkpoint before another rank opened it, causing FileNotFoundError.

Serialize the checkpoint into an in-memory buffer on rank 0 and broadcast the bytes through the process group. Each rank then loads from its own BytesIO buffer.

This removes the rank-0-owned file and its lifetime race instead of adding another barrier around temporary-file cleanup. It also removes the unnecessary shared-filesystem dependency while preserving coverage of serialization, device remapping, optimizer state restoration, and the missing-step fallback.

The same changes applied to test_ddp_hook_pickling_powerSGD and unskipped the test.

Reproduction

On main, add a 3s delay on non-zero ranks between the post-save dist.barrier() and torch.load in _test_post_localSGD_optimizer_step_reload:

            dist.barrier()
            if self.rank != 0:
                time.sleep(3)
            map_location = {"cuda:0": f"cuda:{self.rank:d}"}
            checkpoint = torch.load(chkpt_file, map_location=map_location)

And then execute the test:

BACKEND=nccl WORLD_SIZE=2 TEMP_DIR=/tmp/dist python test/distributed/test_distributed_spawn.py -v \
  TestDistBackendWithSpawn.test_post_localSGD_optimizer_step_reload

Without the changes, the test returns:

FileNotFoundError: [Errno 2] No such file or directory: '/tmp/tmptsfocehr'

Pull Request resolved: pytorch#194614
Approved by: https://github.com/jeffdaily

)

## Summary

Fixes pytorch#84886.

There was a race in the post-local SGD checkpoint reload test. Rank 0 created the temporary checkpoint and all ranks synchronized after it was written. However, there was no synchronization after loading. Rank 0 could complete the test first, leave the temporary-file context, and delete the checkpoint before another rank opened it, causing FileNotFoundError.

Serialize the checkpoint into an in-memory buffer on rank 0 and broadcast the bytes through the process group. Each rank then loads from its own BytesIO buffer.

This removes the rank-0-owned file and its lifetime race instead of adding another barrier around temporary-file cleanup. It also removes the unnecessary shared-filesystem dependency while preserving coverage of serialization, device remapping, optimizer state restoration, and the missing-step fallback.

The same changes applied to test_ddp_hook_pickling_powerSGD and unskipped the test.

## Reproduction
On `main`, add a 3s delay on non-zero ranks between the post-save `dist.barrier()` and `torch.load` in `_test_post_localSGD_optimizer_step_reload`:
```python
            dist.barrier()
            if self.rank != 0:
                time.sleep(3)
            map_location = {"cuda:0": f"cuda:{self.rank:d}"}
            checkpoint = torch.load(chkpt_file, map_location=map_location)
```
And then execute the test:
```
BACKEND=nccl WORLD_SIZE=2 TEMP_DIR=/tmp/dist python test/distributed/test_distributed_spawn.py -v \
  TestDistBackendWithSpawn.test_post_localSGD_optimizer_step_reload
```
Without the changes, the test returns:
```
FileNotFoundError: [Errno 2] No such file or directory: '/tmp/tmptsfocehr'
```
Pull Request resolved: pytorch#194614
Approved by: https://github.com/jeffdaily

(cherry picked from commit 7658834)
Co-authored-by: Cursor <[email protected]>
@jeffdaily
jeffdaily marked this pull request as ready for review November 23, 2026 22:27
@jeffdaily
jeffdaily merged commit 03e0481 into release/2.14 Nov 23, 2026
5 checks passed
@jeffdaily
jeffdaily deleted the albmalamd/cherry-pick/2.14/194614-fix-post-localsgd-reload-race branch November 23, 2026 22:28
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