-
Notifications
You must be signed in to change notification settings - Fork 4.2k
FSDP2 tutorial: how FSDP2 works #3354
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
Open
weifengpy
wants to merge
1
commit into
gh/weifengpy/1/base
Choose a base branch
from
gh/weifengpy/1/head
base: gh/weifengpy/1/base
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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,55 @@ | ||
Getting Started with Fully Sharded Data Parallel(FSDP) | ||
====================================================== | ||
|
||
**Author**: `Wei Feng <https://github.com/weifengpy>`__, `Will Constable <https://github.com/wconstab>`__, `Yifan Mao <https://github.com/mori360>`__ | ||
|
||
.. note:: | ||
|edit| View and edit this tutorial in `github <https://github.com/pytorch/tutorials/blob/main/intermediate_source/FSDP2_tutorial.rst>`__. | ||
|
||
How FSDP2 works | ||
-------------- | ||
In `DistributedDataParallel <https://pytorch.org/docs/stable/generated/torch.nn.parallel.DistributedDataParallel.html>`__ (DDP) training, each rank owns a model replica and processes a batch of data, finally it uses all-reduce to sync gradients across ranks. | ||
|
||
Comparing with DDP, FSDP reduces GPU memory footprint by sharding model parameters, gradients, and optimizer states. It makes it feasible to train models that cannot fit on a single GPU. As shown below in the picture, | ||
* Outside of forward and backward, parameters stay fully sharded. | ||
* Before forward and backward, all-gather to unshard parameters for computation. | ||
* Inside backward, reduce-scatter to get fully sharded gradients. | ||
* Optimizer updates sharded parameters according to sharded gradients, resulting in sharded optimizer states. | ||
|
||
.. figure:: /_static/img/distributed/fsdp_workflow.png | ||
:width: 100% | ||
:align: center | ||
:alt: FSDP workflow | ||
|
||
FSDP Workflow | ||
|
||
|
||
FSDP can be considered as decomposing DDP all-reduce into reduce-scatter and all-gather. | ||
|
||
.. figure:: /_static/img/distributed/fsdp_sharding.png | ||
:width: 100% | ||
:align: center | ||
:alt: FSDP allreduce | ||
|
||
FSDP Allreduce | ||
|
||
Comparing with FSDP1, FSDP2 has following advantages: | ||
* Representing sharded parameters as DTensors sharded on dim-i, allowing for easy manipulation of individual parameters, communication-free sharded state dicts, and a simpler meta-device initialization flow. | ||
* Improving memory management system that achieves lower and deterministic GPU memory by avoiding recordStream and does so without any CPU synchronization. | ||
* Offers an extension point to customize the all-gather, e.g. for fp8 all-gather for fp8 linears. | ||
* Mixing frozen and non-frozen parameters can in the the same communication group without using extra memory. | ||
|
||
How to use FSDP2 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this still a work in progress? |
||
--------------- | ||
Model Initialization: nested wrapping, dim-0 sharding, AC | ||
|
||
Loading State Dict | ||
|
||
Forward and Backward | ||
|
||
Gradient Clipping and Scaler, and Optimizer with DTensor | ||
|
||
Saving State Dict | ||
|
||
FSDP1-to-FSDP2 Migration Guide | ||
--------------- |
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.
please add to index.rst.