Skip to content

Conversation

@awgu
Copy link
Collaborator

@awgu awgu commented Jan 16, 2023

Stack from ghstack:

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 FlatParameters in GPU memory.

@pytorch-bot
Copy link

pytorch-bot bot commented Jan 16, 2023

🔗 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 Pending

As of commit d6668dc:
💚 Looks good so far! There are no failures yet. 💚

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 Jan 16, 2023
awgu pushed a commit that referenced this pull request Jan 16, 2023
ghstack-source-id: 4f6b984
Pull Request resolved: #92236
**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
@awgu awgu closed this Jan 17, 2023
@facebook-github-bot facebook-github-bot deleted the gh/awgu/297/head branch June 8, 2023 15:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants