Repository navigation
FSDP2: gather before clearing the post-forward mesh in native_fsdp2_unsharded - #15426
Merged
Merged
Conversation
…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).
Member
Author
|
@codex review |
Codex Review SummaryThis 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. |
|
Codex Review: Didn't find any major issues. Delightful! 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". |
[skip ci]
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.
Problem
native_fsdp2_unsharded(the context that wrapsgenerate, merges and the sentence-transformer encode path under native FSDP2) cleared each unit'spost_forward_mesh_infoand only then calledunshard().With an int
reshard_after_forward(for exampleshard_model(model, FSDP2Config(reshard_after_forward = 2))on a 4-rank mesh), a forward outside backward, such as an evaluation step, leaves the unit inSHARDED_POST_FORWARDon its 2-rank sub-mesh. FSDP2 picks the all-gather group for that state frompost_forward_mesh_info, so entering the context after an evaluation forward failed on every rank: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_FORWARDis 1 / 0 / auto). An int only reaches this code through an explicitFSDP2Config(reshard_after_forward = N)orunit_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_int_mesh_unsharded_context[cold]test_int_mesh_unsharded_context[after_eval_forward]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
Stream.record_eventis 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.