Skip to content

Commit 7b763e2

Browse files
author
Svetlana Karslioglu
authored
Merge branch 'main' into patch-1
2 parents ba7782c + f1b27b4 commit 7b763e2

10 files changed

+1578
-23
lines changed

.jenkins/validate_tutorials_built.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@
2222
"beginner_source/former_torchies/tensor_tutorial_old",
2323
"beginner_source/examples_autograd/polynomial_autograd",
2424
"beginner_source/examples_autograd/polynomial_custom_function",
25-
"beginner_source/t5_tutorial", # re-enable after this is fixed: https://github.com/pytorch/text/issues/1756
25+
"beginner_source/t5_tutorial", # re-enable after this is fixed: https://github.com/pytorch/text/issues/1756
2626
"intermediate_source/parametrizations",
2727
"intermediate_source/mnist_train_nas", # used by ax_multiobjective_nas_tutorial.py
2828
"intermediate_source/fx_conv_bn_fuser",
29+
"intermediate_source/_torch_export_nightly_tutorial", # does not work on release
2930
"advanced_source/super_resolution_with_onnxruntime",
3031
"advanced_source/ddp_pipeline", # requires 4 gpus
3132
"prototype_source/fx_graph_mode_ptq_dynamic",

conf.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ def reset_seeds(gallery_conf, fname):
106106
'first_notebook_cell': ("# For tips on running notebooks in Google Colab, see\n"
107107
"# https://pytorch.org/tutorials/beginner/colab\n"
108108
"%matplotlib inline"),
109-
'reset_modules': (reset_seeds)
109+
'reset_modules': (reset_seeds),
110+
'ignore_pattern': r'_torch_export_nightly_tutorial.py'
110111
}
111112

112113
if os.getenv('GALLERY_PATTERN'):

en-wordlist.txt

+5
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ CPUs
5252
CPython
5353
CUDA
5454
Caffe
55+
callable's
5556
Captum
5657
Captum's
5758
CartPole
@@ -72,6 +73,8 @@ DQN
7273
DataPipe
7374
DataPipes
7475
DataLoaders
76+
Decompositions
77+
decompositions
7578
DeepMind
7679
DeiT
7780
DenseNet
@@ -160,6 +163,8 @@ ONNX Runtime
160163
ONNX Script
161164
OpenAI
162165
OpenMP
166+
Opset
167+
opset
163168
Ornstein
164169
OU
165170
PIL

intermediate_source/FSDP_tutorial.rst

+18-19
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Getting Started with Fully Sharded Data Parallel(FSDP)
88

99
Training AI models at a large scale is a challenging task that requires a lot of compute power and resources.
1010
It also comes with considerable engineering complexity to handle the training of these very large models.
11-
`Pytorch FSDP <https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/>`__, released in PyTorch 1.11 makes this easier.
11+
`PyTorch FSDP <https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/>`__, released in PyTorch 1.11 makes this easier.
1212

1313
In this tutorial, we show how to use `FSDP APIs <https://pytorch.org/docs/1.11/fsdp.html>`__, for simple MNIST models that can be extended to other larger models such as `HuggingFace BERT models <https://huggingface.co/blog/zero-deepspeed-fairscale>`__,
1414
`GPT 3 models up to 1T parameters <https://pytorch.medium.com/training-a-1-trillion-parameter-model-with-pytorch-fully-sharded-data-parallel-on-aws-3ac13aa96cff>`__ . The sample DDP MNIST code has been borrowed from `here <https://github.com/yqhu/mnist_examples>`__.
@@ -18,7 +18,7 @@ How FSDP works
1818
--------------
1919
In `DistributedDataParallel <https://pytorch.org/docs/stable/generated/torch.nn.parallel.DistributedDataParallel.html>`__, (DDP) training, each process/ worker owns a replica of the model and processes a batch of data, finally it uses all-reduce to sum up gradients over different workers. In DDP the model weights and optimizer states are replicated across all workers. FSDP is a type of data parallelism that shards model parameters, optimizer states and gradients across DDP ranks.
2020

21-
FSDP GPU memory footprint would be smaller than DDP across all workers. This makes the training of some very large models feasible and helps to fit larger models or batch sizes for our training job. This would come with the cost of increased communication volume. The communication overhead is reduced by internal optimizations like communication and computation overlapping.
21+
When training with FSDP, the GPU memory footprint is smaller than when training with DDP across all workers. This makes the training of some very large models feasible by allowing larger models or batch sizes to fit on device. This comes with the cost of increased communication volume. The communication overhead is reduced by internal optimizations like overlapping communication and computation.
2222

2323
.. figure:: /_static/img/distributed/fsdp_workflow.png
2424
:width: 100%
@@ -27,7 +27,7 @@ FSDP GPU memory footprint would be smaller than DDP across all workers. This mak
2727

2828
FSDP Workflow
2929

30-
At high level FSDP works as follow:
30+
At a high level FSDP works as follow:
3131

3232
*In constructor*
3333

@@ -48,11 +48,11 @@ At high level FSDP works as follow:
4848

4949
How to use FSDP
5050
--------------
51-
Here we use a toy model to run training on MNIST dataset for demonstration purposes. Similarly the APIs and logic can be applied to larger models for training.
51+
Here we use a toy model to run training on the MNIST dataset for demonstration purposes. The APIs and logic can be applied to training larger models as well.
5252

5353
*Setup*
5454

55-
1.1 Install Pytorch along with Torchvision
55+
1.1 Install PyTorch along with Torchvision
5656

5757
.. code-block:: bash
5858
@@ -139,7 +139,7 @@ We add the following code snippets to a python script “FSDP_mnist.py”.
139139
output = F.log_softmax(x, dim=1)
140140
return output
141141
142-
2.2 define a train function
142+
2.2 Define a train function
143143

144144
.. code-block:: python
145145
@@ -189,7 +189,7 @@ We add the following code snippets to a python script “FSDP_mnist.py”.
189189
190190
2.4 Define a distributed train function that wraps the model in FSDP
191191

192-
**Note: to save the FSDP model, we need to call the state_dict on each rank then on Rank 0 save the overall states. This is only available in Pytorch nightlies, current Pytorch release is 1.11 at the moment.**
192+
**Note: to save the FSDP model, we need to call the state_dict on each rank then on Rank 0 save the overall states.**
193193

194194
.. code-block:: python
195195
@@ -250,7 +250,6 @@ We add the following code snippets to a python script “FSDP_mnist.py”.
250250
if args.save_model:
251251
# use a barrier to make sure training is done on all ranks
252252
dist.barrier()
253-
# state_dict for FSDP model is only available on Nightlies for now
254253
states = model.state_dict()
255254
if rank == 0:
256255
torch.save(states, "mnist_cnn.pt")
@@ -259,7 +258,7 @@ We add the following code snippets to a python script “FSDP_mnist.py”.
259258
260259
261260
262-
2.5 Finally parsing the arguments and setting the main function
261+
2.5 Finally parse the arguments and set the main function
263262

264263
.. code-block:: python
265264
@@ -319,7 +318,7 @@ Alternatively, we will look at adding the fsdp_auto_wrap_policy next and will di
319318
)
320319
)
321320
322-
Following is the peak memory usage from FSDP MNIST training on g4dn.12.xlarge AWS EC2 instance with 4 gpus captured from Pytorch Profiler.
321+
The following is the peak memory usage from FSDP MNIST training on g4dn.12.xlarge AWS EC2 instance with 4 GPUs captured from PyTorch Profiler.
323322

324323

325324
.. figure:: /_static/img/distributed/FSDP_memory.gif
@@ -329,7 +328,7 @@ Following is the peak memory usage from FSDP MNIST training on g4dn.12.xlarge AW
329328

330329
FSDP Peak Memory Usage
331330

332-
*Applying fsdp_auto_wrap_policy* in FSDP otherwise, FSDP will put the entire model in one FSDP unit, which will reduce computation efficiency and memory efficiency.
331+
Applying *fsdp_auto_wrap_policy* in FSDP otherwise, FSDP will put the entire model in one FSDP unit, which will reduce computation efficiency and memory efficiency.
333332
The way it works is that, suppose your model contains 100 Linear layers. If you do FSDP(model), there will only be one FSDP unit which wraps the entire model.
334333
In that case, the allgather would collect the full parameters for all 100 linear layers, and hence won't save CUDA memory for parameter sharding.
335334
Also, there is only one blocking allgather call for the all 100 linear layers, there will not be communication and computation overlapping between layers.
@@ -354,7 +353,7 @@ Finding an optimal auto wrap policy is challenging, PyTorch will add auto tuning
354353
model = FSDP(model,
355354
fsdp_auto_wrap_policy=my_auto_wrap_policy)
356355
357-
Applying the FSDP_auto_wrap_policy, the model would be as follows:
356+
Applying the fsdp_auto_wrap_policy, the model would be as follows:
358357

359358
.. code-block:: bash
360359
@@ -381,7 +380,7 @@ Applying the FSDP_auto_wrap_policy, the model would be as follows:
381380
382381
CUDA event elapsed time on training loop 41.89130859375sec
383382
384-
Following is the peak memory usage from FSDP with auto_wrap policy of MNIST training on g4dn.12.xlarge AWS EC2 instance with 4 gpus captured from Pytorch Profiler.
383+
The following is the peak memory usage from FSDP with auto_wrap policy of MNIST training on a g4dn.12.xlarge AWS EC2 instance with 4 GPUs captured from PyTorch Profiler.
385384
It can be observed that the peak memory usage on each device is smaller compared to FSDP without auto wrap policy applied, from ~75 MB to 66 MB.
386385
387386
.. figure:: /_static/img/distributed/FSDP_autowrap.gif
@@ -391,11 +390,11 @@ It can be observed that the peak memory usage on each device is smaller compared
391390
392391
FSDP Peak Memory Usage using Auto_wrap policy
393392
394-
*CPU Off-loading*: In case the model is very large that even with FSDP wouldn't fit into gpus, then CPU offload can be helpful here.
393+
*CPU Off-loading*: In case the model is very large that even with FSDP wouldn't fit into GPUs, then CPU offload can be helpful here.
395394
396395
Currently, only parameter and gradient CPU offload is supported. It can be enabled via passing in cpu_offload=CPUOffload(offload_params=True).
397396
398-
Note that this currently implicitly enables gradient offloading to CPU in order for params and grads to be on the same device to work with the optimizer. This API is subject to change. Default is None in which case there will be no offloading.
397+
Note that this currently implicitly enables gradient offloading to CPU in order for params and grads to be on the same device to work with the optimizer. This API is subject to change. The default is None in which case there will be no offloading.
399398
400399
Using this feature may slow down the training considerably, due to frequent copying of tensors from host to device, but it could help improve memory efficiency and train larger scale models.
401400
@@ -409,7 +408,7 @@ In 2.4 we just add it to the FSDP wrapper
409408
cpu_offload=CPUOffload(offload_params=True))
410409
411410
412-
Compare it with DDP, if in 2.4 we just normally wrap the model in ddp, saving the changes in “DDP_mnist.py”.
411+
Compare it with DDP, if in 2.4 we just normally wrap the model in DPP, saving the changes in “DDP_mnist.py”.
413412
414413
.. code-block:: python
415414
@@ -423,7 +422,7 @@ Compare it with DDP, if in 2.4 we just normally wrap the model in ddp, saving th
423422
424423
CUDA event elapsed time on training loop 39.77766015625sec
425424
426-
Following is the peak memory usage from DDP MNIST training on g4dn.12.xlarge AWS EC2 instance with 4 gpus captured from Pytorch profiler.
425+
The following is the peak memory usage from DDP MNIST training on g4dn.12.xlarge AWS EC2 instance with 4 GPUs captured from PyTorch profiler.
427426
428427
.. figure:: /_static/img/distributed/DDP_memory.gif
429428
:width: 100%
@@ -434,8 +433,8 @@ Following is the peak memory usage from DDP MNIST training on g4dn.12.xlarge AWS
434433
435434
436435
Considering the toy example and tiny MNIST model we defined here, we can observe the difference between peak memory usage of DDP and FSDP.
437-
In DDP each process holds a replica of the model, so the memory footprint is higher compared to FSDP that shards the model parameter, optimizer states and gradients over DDP ranks.
436+
In DDP each process holds a replica of the model, so the memory footprint is higher compared to FSDP which shards the model parameters, optimizer states and gradients over DDP ranks.
438437
The peak memory usage using FSDP with auto_wrap policy is the lowest followed by FSDP and DDP.
439438
440-
Also, looking at timings, considering the small model and running the training on a single machine, FSDP with/out auto_wrap policy performed almost as fast as DDP.
439+
Also, looking at timings, considering the small model and running the training on a single machine, FSDP with and without auto_wrap policy performed almost as fast as DDP.
441440
This example does not represent most of the real applications, for detailed analysis and comparison between DDP and FSDP please refer to this `blog post <https://pytorch.medium.com/6c8da2be180d>`__ .

0 commit comments

Comments
 (0)