Repository navigation
Native FSDP2 resume: memory-map the full optimizer.pt - #19019
Merged
Merged
Conversation
Every rank loads the whole unsharded optimizer.pt and keeps only its shards, so an eager torch.load costs each rank an anonymous copy of the full optimizer state (ranks x file size of host RAM). Load it with mmap=True: the pages are file-backed and shared through the page cache. A file object or a legacy non-zip file still loads eagerly.
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. |
Member
Author
|
@codex review |
|
Codex Review: Didn't find any major issues. Chef's kiss. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
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.
Summary
On a native FSDP2 resume every rank calls
torch.load(optimizer.pt, map_location="cpu")on the whole, unshardedoptimizer state and then keeps only its own shards. An eager load gives each rank its own anonymous copy of the full
file, so host RAM needs about (ranks per node) x (optimizer.pt size) at the moment of the resume. For full fine-tuning
with AdamW that is 8 bytes per parameter per rank.
This loads
optimizer.ptwithmmap=True. The tensors are file-backed and the ranks on one node share the pagesthrough the page cache, so the anonymous allocation goes away.
set_optimizer_state_dictstill copies each rank'sshards to the device exactly as before.
native_load_optimizerchanges. The DCP directory path (UNSLOTH_FSDP2_DCP_OPTIM=1), the scheduler load andthe model weight reader are untouched.
Measurements
Kaggle 2x T4, Qwen3-0.6B full fine-tuning, native FSDP2 sharded over 2 ranks,
adamw_torch(optimizer.pt 4.77 GB),torch 2.14.1, transformers 5.17.0. The window measured is the call to
native_load_optimizer, with RSS sampled every20 ms. Arms alternate: eager, mmap, eager, mmap.
give the same number (spread 0.00 GiB).
themselves, and the mmap runs fall inside that spread.
The same check on torch 2.15.0, this branch's tree against main with no injection, alternating main / head / main /
head. On the head,
torch.loadreceivedmmap=Trueforoptimizer.pton both ranks:Tests
tests/test_fsdp2_native_optim_mmap_load.py(new):native_load_optimizerpassesmmap=Truefor a path; themapped load equals the eager one; a legacy non-zip file and a file object load eagerly; other errors propagate.
4 of the 5 fail on main and all 5 pass here.
tests/test_native_resume_old_torch.pystill passes: torch < 2.6 is refused before any load.test_fsdp2_native_resume_gloo.py,test_fsdp2_native_dcp_resume_gloo.py,test_fsdp2_native_optim_unstepped_gloo.py) pass.