-
Notifications
You must be signed in to change notification settings - Fork 26.3k
[FSDP][1/N] _summon_full_params -> _unshard_params
#92236
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
Conversation
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
[ghstack-poisoned]
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/92236
Note: Links to docs will display an error until the docs builds have been completed. ⏳ No Failures, 1 PendingAs of commit d6668dc: This comment was automatically generated by Dr. CI and updates every 15 minutes. |
**Overview**
This PR stack will add support for unsharding FSDP's sharded parameters for `fully_shard`. This PR takes the first step by doing some internal refactoring.
- The existing API for wrapper FSDP is the static method `summon_full_params()`, which calls into the helper `_summon_full_params()`.
- This PR refactors:
- `summon_full_params()` core logic to `_unshard_params()`
- `_summon_full_params()` to `_unshard_params_recurse()`, which has a `recurse: bool` argument
- Previous `_unshard_params()` to `_unshard_fsdp_state_params()`, which applies to a single FSDP state
**Details**
- This PR introduces `_get_fsdp_states_with_modules()` and `_get_root_fsdp_states_with_modules()`, which additionally return the modules along with the FSDP states. The modules are needed for handling `FlatParameter` registration.
- We may be able to remove this if we clean up the `use_orig_params=True` vs. `False` code paths because for `True`, the `FlatParameter` is not registered, meaning that it does not need to be de-registered.
- Since `fully_shard` requires `use_orig_params=True`, we may not need `_get_fsdp_states_with_modules()` and `_get_root_fsdp_root_modules()`; however, I prefer to make the separation of FSDP state and module explicit for now for clarity.
**Follow-Ups**
- `writeback=True` and `rank0_only=True` raises an error. The previous explanation was:
> is not supported, as model parameter shapes will be different across ranks, and writing to them can lead to inconsistencies across ranks when the context is exited.
I am not exactly sure what the different model parameter shapes refers to. However, I believe that we can support `writeback=True` and `rank0_only=True` by broadcasting the `FlatParameter` from rank 0 in the `finally`, writing back, and freeing. This should not increase the peak memory since rank 0 already holds the unsharded `FlatParameter` in GPU memory before writing back and nonzero ranks do not have any other unsharded `FlatParameter`s in GPU memory.
[ghstack-poisoned]
**Overview**
This PR stack will add support for unsharding FSDP's sharded parameters for `fully_shard`. This PR takes the first step by doing some internal refactoring.
- The existing API for wrapper FSDP is the static method `summon_full_params()`, which calls into the helper `_summon_full_params()`.
- This PR refactors:
- `summon_full_params()` core logic to `_unshard_params()`
- `_summon_full_params()` to `_unshard_params_recurse()`, which has a `recurse: bool` argument
- Previous `_unshard_params()` to `_unshard_fsdp_state_params()`, which applies to a single FSDP state
**Details**
- This PR introduces `_get_fsdp_states_with_modules()` and `_get_root_fsdp_states_with_modules()`, which additionally return the modules along with the FSDP states. The modules are needed for handling `FlatParameter` registration.
- We may be able to remove this if we clean up the `use_orig_params=True` vs. `False` code paths because for `True`, the `FlatParameter` is not registered, meaning that it does not need to be de-registered.
- Since `fully_shard` requires `use_orig_params=True`, we may not need `_get_fsdp_states_with_modules()` and `_get_root_fsdp_root_modules()`; however, I prefer to make the separation of FSDP state and module explicit for now for clarity.
**Follow-Ups**
- `writeback=True` and `rank0_only=True` raises an error. The previous explanation was:
> is not supported, as model parameter shapes will be different across ranks, and writing to them can lead to inconsistencies across ranks when the context is exited.
I am not exactly sure what the different model parameter shapes refers to. However, I believe that we can support `writeback=True` and `rank0_only=True` by broadcasting the `FlatParameter` from rank 0 in the `finally`, writing back, and freeing. This should not increase the peak memory since rank 0 already holds the unsharded `FlatParameter` in GPU memory before writing back and nonzero ranks do not have any other unsharded `FlatParameter`s in GPU memory.
[ghstack-poisoned]
awgu
pushed a commit
to awgu/pytorch
that referenced
this pull request
Jan 17, 2023
ghstack-source-id: 82e26c4 Pull Request resolved: pytorch#92236
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.
Stack from ghstack:
fully_shardunit tests #92281 [FSDP][3/N] Splitfully_shardunit testssummon_full_paramsunit tests #92263 [FSDP][2/N] Refactorsummon_full_paramsunit tests_summon_full_params->_unshard_params#92236 [FSDP][1/N]_summon_full_params->_unshard_paramsdevice_id+ CPU offload test #92031 [FSDP][BE] Improvedevice_id+ CPU offload testprefixed_param_names->fqnsfor consolidation #92028 [FSDP][BE] Renameprefixed_param_names->fqnsfor consolidationOverview
This PR stack will add support for unsharding FSDP's sharded parameters for
fully_shard. This PR takes the first step by doing some internal refactoring.summon_full_params(), which calls into the helper_summon_full_params().summon_full_params()core logic to_unshard_params()_summon_full_params()to_unshard_params_recurse(), which has arecurse: boolargument_unshard_params()to_unshard_fsdp_state_params(), which applies to a single FSDP stateDetails
_get_fsdp_states_with_modules()and_get_root_fsdp_states_with_modules(), which additionally return the modules along with the FSDP states. The modules are needed for handlingFlatParameterregistration.use_orig_params=Truevs.Falsecode paths because forTrue, theFlatParameteris not registered, meaning that it does not need to be de-registered.fully_shardrequiresuse_orig_params=True, we may not need_get_fsdp_states_with_modules()and_get_root_fsdp_root_modules(); however, I prefer to make the separation of FSDP state and module explicit for now for clarity.Follow-Ups
writeback=Trueandrank0_only=Trueraises an error. The previous explanation was:I am not exactly sure what the different model parameter shapes refers to. However, I believe that we can support
writeback=Trueandrank0_only=Trueby broadcasting theFlatParameterfrom rank 0 in thefinally, writing back, and freeing. This should not increase the peak memory since rank 0 already holds the unshardedFlatParameterin GPU memory before writing back and nonzero ranks do not have any other unshardedFlatParameters in GPU memory.