Skip to content
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

[FSDP][8/N] Replace _FSDPPolicy.policy with _Policy._run_policy #104969

Closed
wants to merge 10 commits into from

Conversation

awgu
Copy link
Contributor

@awgu awgu commented Jul 11, 2023

Stack from ghstack (oldest at bottom):

This does some code organization improvement.

  • It renames _FSDPPolicy to _Policy to show that it is not only for FSDP but for any module-level API.
  • It formalizes the contract that such a policy should return something like target_module_to_kwargs: Dict[nn.Module, Dict[str, Any]] that maps each module to wrap to its kwargs. It does so by requiring a _run_policy abstract method (this time private since users do not need to care about it). Then, our auto wrapping can just call _run_policy() to generate the dict and do any validation or post-processing.

This PR is technically BC-breaking because it removes the public ModuleWrapPolicy.policy. However, I do not think anyone was using that anyway, so this is a pretty safe breakage.

@pytorch-bot
Copy link

pytorch-bot bot commented Jul 11, 2023

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/104969

Note: Links to docs will display an error until the docs builds have been completed.

✅ 1 Unrelated Failure

As of commit ad2e8ce:

UNSTABLE - The following job failed but was likely due to flakiness present on trunk and has been marked as unstable:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@pytorch-bot pytorch-bot bot added the release notes: distributed (fsdp) release notes category label Jul 11, 2023
awgu added a commit that referenced this pull request Jul 11, 2023
ghstack-source-id: c41660b85e42e6de90e78d12b859a9961ddd703e
Pull Request resolved: #104969
awgu added a commit that referenced this pull request Jul 11, 2023
ghstack-source-id: 53a5622bc1a3603308b3d266e5840a730b6247ae
Pull Request resolved: #104969
@awgu awgu added the suppress-bc-linter Suppresses the failures of API backward-compatibility linter (Lint/bc_linter) label Jul 11, 2023
awgu added a commit that referenced this pull request Jul 11, 2023
ghstack-source-id: 783d09ccc43c84621f91e801159db877b508b541
Pull Request resolved: #104969
@awgu awgu changed the title [FSDP][8/N] Replace _FSDPPolicy.policy with run_policy [FSDP][8/N] Replace _FSDPPolicy.policy with _run_policy Jul 11, 2023
@awgu awgu added the topic: not user facing topic category label Jul 11, 2023
@awgu awgu marked this pull request as ready for review July 11, 2023 16:04
awgu added a commit to awgu/pytorch that referenced this pull request Jul 11, 2023
ghstack-source-id: a5fcf40d6e6d5f7db475bf0b60d3cfa3a42ee3d3
Pull Request resolved: pytorch#104969
This is a code organization improvement. It formalizes the contract that an FSDP policy should return something like `target_module_to_kwargs: Dict[nn.Module, Dict[str, Any]]` that maps each module to wrap to its FSDP kwargs. It does so by requiring a `_run_policy` abstract method (this time private since users do not need to care about it). Then, our auto wrapping can just call `_run_policy()` to generate the dict and do any validation or post-processing.

This PR is technically BC-breaking because it removes the public `ModuleWrapPolicy.policy`. However, I do not think anyone was using that anyway, so this is a pretty safe breakage.



[ghstack-poisoned]
@awgu awgu requested a review from yhcharles as a code owner July 11, 2023 19:52
awgu added a commit to awgu/pytorch that referenced this pull request Jul 11, 2023
ghstack-source-id: 86c9da70764481465d83b24d69224561d1f83fc3
Pull Request resolved: pytorch#104969
This is a code organization improvement. 
- It renames `_FSDPPolicy` to `_WrapPolicy` to show that it is not only for FSDP but for any module-level API.
- It formalizes the contract that such a policy should return something like `target_module_to_kwargs: Dict[nn.Module, Dict[str, Any]]` that maps each module to wrap to its kwargs. It does so by requiring a `_run_policy` abstract method (this time private since users do not need to care about it). Then, our auto wrapping can just call `_run_policy()` to generate the dict and do any validation or post-processing.

This PR is technically BC-breaking because it removes the public `ModuleWrapPolicy.policy`. However, I do not think anyone was using that anyway, so this is a pretty safe breakage.



[ghstack-poisoned]
awgu added a commit to awgu/pytorch that referenced this pull request Jul 11, 2023
ghstack-source-id: 8e8b698c212c8c5e8269641ec5c19c0498488878
Pull Request resolved: pytorch#104969
awgu added a commit to awgu/pytorch that referenced this pull request Jul 11, 2023
ghstack-source-id: 8e8b698c212c8c5e8269641ec5c19c0498488878
Pull Request resolved: pytorch#104969
This does some code organization improvement. 
- It renames `_FSDPPolicy` to `_WrapPolicy` to show that it is not only for FSDP but for any module-level API.
- It formalizes the contract that such a policy should return something like `target_module_to_kwargs: Dict[nn.Module, Dict[str, Any]]` that maps each module to wrap to its kwargs. It does so by requiring a `_run_policy` abstract method (this time private since users do not need to care about it). Then, our auto wrapping can just call `_run_policy()` to generate the dict and do any validation or post-processing.

This PR is technically BC-breaking because it removes the public `ModuleWrapPolicy.policy`. However, I do not think anyone was using that anyway, so this is a pretty safe breakage.



[ghstack-poisoned]
continue
elif isinstance(module, module_classes):
# Shallow copy to avoid coupling changes across modules
target_module_to_kwargs[module] = copy.copy(root_kwargs)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need to copy the args? Do we expect root_kwargs to change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We must copy the kwargs or else if a function overrides it (e.g. the mixed precision overrides), then it will override it for every target module. We have to avoid them sharing a reference to the same kwarg dict.

awgu added a commit to awgu/pytorch that referenced this pull request Jul 19, 2023
ghstack-source-id: 3c5d71e48d07274d6ed3e3f791d300427849d2da
Pull Request resolved: pytorch#104969
awgu added a commit to awgu/pytorch that referenced this pull request Jul 21, 2023
ghstack-source-id: 3c5d71e48d07274d6ed3e3f791d300427849d2da
Pull Request resolved: pytorch#104969
awgu added a commit to awgu/pytorch that referenced this pull request Jul 26, 2023
ghstack-source-id: 3c5d71e48d07274d6ed3e3f791d300427849d2da
Pull Request resolved: pytorch#104969
awgu added a commit to awgu/pytorch that referenced this pull request Jul 31, 2023
ghstack-source-id: 3c5d71e48d07274d6ed3e3f791d300427849d2da
Pull Request resolved: pytorch#104969
@awgu awgu changed the title [FSDP][8/N] Replace _FSDPPolicy.policy with _run_policy [FSDP][8/N] Replace _FSDPPolicy.policy with _Policy._run_policy Aug 2, 2023
…n_policy`"


This does some code organization improvement. 
- It renames `_FSDPPolicy` to `_Policy` to show that it is not only for FSDP but for any module-level API.
- It formalizes the contract that such a policy should return something like `target_module_to_kwargs: Dict[nn.Module, Dict[str, Any]]` that maps each module to wrap to its kwargs. It does so by requiring a `_run_policy` abstract method (this time private since users do not need to care about it). Then, our auto wrapping can just call `_run_policy()` to generate the dict and do any validation or post-processing.

This PR is technically BC-breaking because it removes the public `ModuleWrapPolicy.policy`. However, I do not think anyone was using that anyway, so this is a pretty safe breakage.



[ghstack-poisoned]
…n_policy`"


This does some code organization improvement. 
- It renames `_FSDPPolicy` to `_Policy` to show that it is not only for FSDP but for any module-level API.
- It formalizes the contract that such a policy should return something like `target_module_to_kwargs: Dict[nn.Module, Dict[str, Any]]` that maps each module to wrap to its kwargs. It does so by requiring a `_run_policy` abstract method (this time private since users do not need to care about it). Then, our auto wrapping can just call `_run_policy()` to generate the dict and do any validation or post-processing.

This PR is technically BC-breaking because it removes the public `ModuleWrapPolicy.policy`. However, I do not think anyone was using that anyway, so this is a pretty safe breakage.



[ghstack-poisoned]
awgu added a commit to awgu/pytorch that referenced this pull request Aug 2, 2023
ghstack-source-id: f0778e5f19edfb0a16e282f9cb133829badb591d
Pull Request resolved: pytorch#104969
…n_policy`"


This does some code organization improvement. 
- It renames `_FSDPPolicy` to `_Policy` to show that it is not only for FSDP but for any module-level API.
- It formalizes the contract that such a policy should return something like `target_module_to_kwargs: Dict[nn.Module, Dict[str, Any]]` that maps each module to wrap to its kwargs. It does so by requiring a `_run_policy` abstract method (this time private since users do not need to care about it). Then, our auto wrapping can just call `_run_policy()` to generate the dict and do any validation or post-processing.

This PR is technically BC-breaking because it removes the public `ModuleWrapPolicy.policy`. However, I do not think anyone was using that anyway, so this is a pretty safe breakage.



[ghstack-poisoned]
@awgu awgu added the ciflow/trunk Trigger trunk jobs on your pull request label Aug 3, 2023
@awgu
Copy link
Contributor Author

awgu commented Aug 3, 2023

@awgu
Copy link
Contributor Author

awgu commented Aug 3, 2023

@pytorchbot merge -f "unrelated failure"

@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged immediately since you used the force (-f) flag, bypassing any CI checks (ETA: 1-5 minutes). Please use -f as last resort and instead consider -i/--ignore-current to continue the merge ignoring current failures. This will allow currently pending tests to finish and report signal before the merge.

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

pytorchmergebot pushed a commit that referenced this pull request Aug 3, 2023
This PR adds a new `CustomPolicy` that acts like the existing `lambda_auto_wrap_policy` except it (1) leverages the new auto wrapping infrastructure and (2) allows overriding FSDP kwargs for particular instances. (1) gives it access to the validation checks (like for frozen parameters), and (2) makes it as expressive as manual wrapping. This should allow us to effectively deprecate manual wrapping if desired.

The API is as follows:
```
def lambda_fn(module: nn.Module) -> Union[bool, Dict[str, Any]]:
    ...
policy = CustomPolicy(lambda_fn)
```
The `lambda_fn` can return:
- `False` or `{}` to indicate no wrapping
- `True` to indicate wrapping while inheriting the root's FSDP kwargs
- Non-empty `dict` to indicate wrapping while overriding the specified FSDP kwargs and inheriting the rest from the root

---

After this PR, the follow-up work items for auto wrapping are:
1. Add shared parameter validation
2. (Longer-term / exploratory) Add a policy that provides a reasonable auto wrapping with "minimal" user input

Pull Request resolved: #104986
Approved by: https://github.com/ezyang
ghstack dependencies: #104427, #104967, #104999, #104969
@facebook-github-bot facebook-github-bot deleted the gh/awgu/419/head branch August 6, 2023 14:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ciflow/trunk Trigger trunk jobs on your pull request Merged release notes: distributed (fsdp) release notes category suppress-bc-linter Suppresses the failures of API backward-compatibility linter (Lint/bc_linter) topic: not user facing topic category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants