Skip to content

FSDP2: gather before clearing the post-forward mesh in native_fsdp2_unsharded - #15426

Merged
danielhanchen merged 2 commits into
mainfrom
dh/s7-rc-mesh-generate
Oct 26, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
dh/s7-rc-mesh-generate

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Problem

native_fsdp2_unsharded (the context that wraps generate, merges and the sentence-transformer encode path under native FSDP2) cleared each unit's post_forward_mesh_info and only then called unshard().

With an int reshard_after_forward (for example shard_model(model, FSDP2Config(reshard_after_forward = 2)) on a 4-rank mesh), a forward outside backward, such as an evaluation step, leaves the unit in SHARDED_POST_FORWARD on its 2-rank sub-mesh. FSDP2 picks the all-gather group for that state from post_forward_mesh_info, so entering the context after an evaluation forward failed on every rank:

AssertionError: Expected mesh_info to be FSDPMeshInfo, got <class 'NoneType'>

Entering the context cold worked, and so did bool settings. #14762 added int restore support, but its test ran on one rank, so the units never resharded onto a sub-mesh.

The env and Trainer paths only produce bool settings (UNSLOTH_FSDP2_RESHARD_AFTER_FORWARD is 1 / 0 / auto). An int only reaches this code through an explicit FSDP2Config(reshard_after_forward = N) or unit_reshard_after_forward.

Fix

Unshard every unit first, while the post-forward mesh is still set, and then turn post-forward resharding off for the forwards inside the context. Exit is unchanged: reshard fully, then restore the saved setting.

Before / after

New test tests/test_fsdp2_native_unsharded_int_mesh_gloo.py, 4 gloo CPU ranks, FSDP2Config(reshard_after_forward = 2). It checks outputs inside the context against one process, the restored setting, the resharded weights on exit, and a training step afterwards.

Test torch 2.10 base torch 2.10 head torch 2.13 base torch 2.13 head torch 2.14 base torch 2.14 head
test_int_mesh_unsharded_context[cold] pass pass pass pass pass pass
test_int_mesh_unsharded_context[after_eval_forward] fail pass fail pass fail pass

On torch 2.13 the existing native FSDP2 tests that use the context (unsharded_restore, qat_convert_generate, infer_embed_audio, vision, optim_reentry, units) pass with this change: 68 passed.

Not covered

  • torch 2.6: FSDP2 does not run on a CPU gloo mesh there (Stream.record_event is missing), so this test and the existing restore tests cannot run on that version. The change only reorders two existing steps (unshard() and the post-forward mesh write), both present on 2.6.
  • No CUDA / NCCL run of the new test in this change.

…nsharded

An int reshard_after_forward leaves a unit on its post-forward sub-mesh after an
evaluation forward; FSDP2 all-gathers such a unit over that mesh, so clearing it
first failed with 'Expected mesh_info to be FSDPMeshInfo'. Unshard first, then
keep the units gathered. Adds a 4-rank gloo test (cold + after eval forward).
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-26T19:23:23.745433Z 3406ae4 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.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: 3406ae4965

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

[skip ci]
@danielhanchen
danielhanchen merged commit dbe9846 into main Oct 26, 2026
@danielhanchen
danielhanchen deleted the dh/s7-rc-mesh-generate branch October 26, 2026 23:40
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