Skip to content

Native FSDP2 resume: memory-map the full optimizer.pt - #19019

Merged
danielhanchen merged 1 commit into
mainfrom
r6-fsdp2-mmap-optimizer-resume
Nov 23, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
r6-fsdp2-mmap-optimizer-resume

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

On a native FSDP2 resume every rank calls torch.load(optimizer.pt, map_location="cpu") on the whole, unsharded
optimizer 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.pt with mmap=True. The tensors are file-backed and the ranks on one node share the pages
through the page cache, so the anonymous allocation goes away. set_optimizer_state_dict still copies each rank's
shards to the device exactly as before.

  • A file object, or a file torch cannot map (legacy non-zip serialization), loads eagerly as before.
  • Other load errors still propagate (the fallback only matches torch's own "mmap can only be used" error).
  • Only native_load_optimizer changes. The DCP directory path (UNSLOTH_FSDP2_DCP_OPTIM=1), the scheduler load and
    the 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 every
20 ms. Arms alternate: eager, mmap, eager, mmap.

arm RssAnon peak increase per rank host MemAvailable drop (both ranks) load time resumed losses (steps 4, 5)
eager #1 +4.57 GiB 8.8 GiB 23.5 s (cold cache) 11.260438, 11.153262
mmap #1 +0.13 GiB 0.3 GiB 4.8 s 11.260438, 11.153244
eager #2 +4.57 GiB 8.8 GiB 7.6 s 11.260438, 11.153244
mmap #2 +0.13 GiB 0.1 GiB 4.8 s 11.260438, 11.153270
  • The anonymous memory a rank needs for the resume drops from 4.57 to 0.13 GiB (-97%). Repeated runs of each arm
    give the same number (spread 0.00 GiB).
  • Device peak during the load is unchanged (5.571 GiB in every arm).
  • Losses: step 4 is bitwise identical in all four arms. Step 5 varies by up to 2.6e-5 between the two eager runs
    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.load received mmap=True for optimizer.pt on both ranks:

arm RssAnon increase per rank host MemAvailable drop load time losses (steps 4, 5)
main +4.57 GiB 9.14 GiB 23.5 s (cold) 11.280999, 11.154003
this branch +0.13 GiB 0.28 GiB 5.0 s 11.280999, 11.153976
main +4.57 GiB 8.68 GiB 7.8 s 11.280999, 11.154009
this branch +0.13 GiB 0.15 GiB 4.9 s 11.280999, 11.154009

Tests

  • tests/test_fsdp2_native_optim_mmap_load.py (new): native_load_optimizer passes mmap=True for a path; the
    mapped 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.py still passes: torch < 2.6 is refused before any load.
  • The gloo resume tests (test_fsdp2_native_resume_gloo.py, test_fsdp2_native_dcp_resume_gloo.py,
    test_fsdp2_native_optim_unstepped_gloo.py) pass.

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

chatgpt-codex-connector Bot commented Nov 23, 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-23T07:21:56.430925Z d0a551c Manual request
ℹ️ 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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: d0a551c435

ℹ️ 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".

@danielhanchen
danielhanchen merged commit cbbfff4 into main Nov 23, 2026
@danielhanchen
danielhanchen deleted the r6-fsdp2-mmap-optimizer-resume branch November 23, 2026 10:24
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.

1 participant