-
Notifications
You must be signed in to change notification settings - Fork 25.7k
[FSDP][BE] Move dynamo annotation to separate file #89890
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
81b582b
[FSDP][BE] Move dynamo annotation to separate file
966c071
Update on "[FSDP][BE] Move dynamo annotation to separate file"
1a411a1
Update on "[FSDP][BE] Move dynamo annotation to separate file"
d7fdf90
Update on "[FSDP][BE] Move dynamo annotation to separate file"
07c3cd8
Update on "[FSDP][BE] Move dynamo annotation to separate file"
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| from typing import Set | ||
|
|
||
| import torch.nn as nn | ||
|
|
||
|
|
||
| def _annotate_modules_for_dynamo( | ||
| module: nn.Module, | ||
| ignored_modules: Set[nn.Module], | ||
| use_orig_params: bool, | ||
| ): | ||
| """ | ||
| Annotates the submodules in ``module`` 's tree, except those in | ||
| ``ignored_modules``, indicating that the submodules are FSDP-managed and | ||
| saving the ``use_orig_params`` setting passed to the FSDP constructor. | ||
| """ | ||
| for submodule in module.modules(): | ||
| if submodule not in ignored_modules: | ||
| """[note: Dynamo treats FSDP wrapped modules as UnspecializedNNModule] | ||
|
|
||
| Dynamo doesn't get to see this instance (FullyShardedDataParallel) during tracing, since | ||
| it skips tracing all the torch.distributed.fsdp code. | ||
| - Why? Running the FSDP code eagerly avoids lots of issues trying to trace complex hooks, and also | ||
| gets us graph-breaks on FSDP module boundaries which we want anyway for comm ops. | ||
| - However, we _also_ want dynamo to treat the wrapped module inside FSDP 'unspecially' (*), | ||
| and we need a way to indicate to dynamo which modules are wrapped by FSDP. | ||
|
|
||
| (*) UnspecializedNNModules in dynamo are traced-through without any assumptions, and with thorough | ||
| guards. NNModules otherwise are 'specialized', meaning there is less overhead due to assuming | ||
| their code is well-behaved. | ||
|
|
||
| One particular issue with specialized NNModules for FSDP is that the | ||
| views created for orig_params are captured into the compiled graph on the first iteration, and while | ||
| they are always going to point to the correct flatparameter and give correct results, their order | ||
| of creation influences the order of backward execution, preventing overlap of comm and computation | ||
| during backward. We need to _use_ the new parameter views created on each forward iteration, in | ||
| order for backward to interleave hooks with compute per layer. UnspecializedNNModule lets us achieve | ||
| this by capturing the module code more 'functionally' and passing parameters in as inputs each time. | ||
| """ | ||
| submodule._is_fsdp_managed_module = True # type: ignore[assignment] | ||
|
|
||
| # Dynamo only supports FSDP with use_orig_params=True. | ||
| # This is hacky, but I could not think of another way to add an assertion to dynamo | ||
| # for this, since Dynamo skips all the FSDP code frames and thus can't inspect the | ||
| # FSDP module directly | ||
| submodule._fsdp_use_orig_params = use_orig_params # type: ignore[assignment] | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All of the comments below are copied directly.