[FSDP2] Add additional_modules_to_shard for sharding custom modules - #4145
Open
yuxinyuan wants to merge 1 commit into
Open
[FSDP2] Add additional_modules_to_shard for sharding custom modules#4145yuxinyuan wants to merge 1 commit into
yuxinyuan wants to merge 1 commit into
Conversation
Opt-in FSDP2-only plugin option to explicitly shard extra modules into their own units on top of the auto wrap policy, with optional per-module reshard_after_forward/mp_policy overrides. Useful for non-standard models whose embeddings/norm aren't found by the built-in heuristic. Adds a set_additional_modules_to_shard setter and CPU unit tests.
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.
What does this PR do?
Adds an opt-in
additional_modules_to_shardoption toFullyShardedDataParallelPlugin(FSDP2) that lets users explicitly shard extra modules into their own FSDP2 units, on top of whatauto_wrap_policyselects.Motivation
FSDP2 preparation shards the modules matched by
auto_wrap_policy, then uses a transformers-specific heuristic to carve the input/output embeddings and the final norm into their own units. Any module that is neither matched by the policy nor discovered by that heuristic falls into the root FSDP2 unit, which is not resharded after forward.This is common for composite / multimodal / omni models that stitch together several transformer stacks. Take a Qwen3-TTS-style model as a concrete example: it contains a speaker encoder, a standard LM decoder, and a code predictor (another transformer). The top-level composite also doesn't expose a single
get_input_embeddings/get_output_embeddings/final-norm through the standardtransformersinterface, so the built-in heuristic can't carve those out.additional_modules_to_shardlets users close that gap explicitly — shard the non-standard modules into their own units, and optionally give specific submodules different sharding behavior (e.g. keep a precision-sensitive module in fp32, or skip resharding a custom tail)API
additional_modules_to_shardis aselector -> overridesmapping:fnmatchglob (e.g."model.norm","model.layers.*.mlp"), or atupleof them. Astrshards each matched module as its own unit; atuplegroups the referenced modules into a single unit (like the built-in norm+lm_head tail). Names are canonicalized before matching, so selectors written against the original module names still match aftertorch.compile/ activation checkpointing.dictoverriding the per-module sharding kwargs —reshard_after_forwardand/ormp_policy; unset keys inherit the model-wide values (offload_policyis intentionally not supported).Tests
Adds CPU unit tests mirroring the existing mocked
fsdp2_prepare_modeltests (fully_shardspied), covering: single/glob/grouped selection, per-module kwarg overrides, precedence over the auto wrap policy, name canonicalization fortorch.compile/activation checkpointing, group-vs-traversal ordering, config validation, the FSDP1 no-op, and the setter.Before submitting
Pull Request section?
to it if that's the case.
documentation guidelines, and
here are tips on formatting docstrings.
Who can review?
Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR. @SunMarc