Skip to content

Commit

Permalink
[minor] OSS doc fix - add the DDP wrap (#131)
Browse files Browse the repository at this point in the history
* wrapping the model in DDP in the tutorial

* typo
  • Loading branch information
blefaudeux committed Oct 9, 2020
1 parent bfd88ca commit 5220f89
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions docs/source/tutorials/oss.rst
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
Optimizer state sharding
========================

Using torch.nn.parallel.DistributedDataParallel leads to some wasted communications, but it is possible and makes OSS a drop in solution in your existing torch distributed code.
Using torch.nn.parallel.DistributedDataParallel leads to some wasted communications in the case of OSS, but it is possible and makes OSS a drop in solution in your existing torch distributed code.
Let's suppose that your trainer looks like

.. code-block:: python
import torch
from torch.nn.parallel import DistributedDataParallel as DDP
def train(
rank: int,
Expand All @@ -19,11 +21,12 @@ Let's suppose that your trainer looks like
# Problem statement
model = myAwesomeModel().to(rank)
model = DDP(model, device_ids=[rank])
dataloader = mySuperFastDataloader()
loss_ln = myVeryRelevantLoss()
# optimizer specific arguments e.g. LR, momentum, etc...
base_optimizer_arguments = { "lr": 1e-4}
base_optimizer_arguments = { "lr": 1e-4}
optimizer = torch.optim.SGD(
params=model.parameters(),
**base_optimizer_arguments)
Expand All @@ -50,6 +53,7 @@ Then sharding the optimizer state is merely a matter of wrapping your optimizer
import torch
from fairscale.optim.oss import OSS
from torch.nn.parallel import DistributedDataParallel as DDP
def train(
rank: int,
Expand All @@ -61,11 +65,12 @@ Then sharding the optimizer state is merely a matter of wrapping your optimizer
# Problem statement
model = myAwesomeModel().to(rank)
model = DDP(model, device_ids=[rank])
dataloader = mySuperFastDataloader()
loss_ln = myVeryRelevantLoss()
# optimizer specific arguments e.g. LR, momentum, etc...
base_optimizer_arguments = { "lr": 1e-4}
base_optimizer_arguments = { "lr": 1e-4}
# ** NEW ** Wrap a base optimizer into OSS
base_optimizer = torch.optim.SGD # any pytorch compliant optimizer
Expand Down Expand Up @@ -100,5 +105,5 @@ The above `train` function will then need to be run via a `multiprocessing.spawn
nprocs=WORLD_SIZE,
join=True
)
to see it in action, you can test it with the following script _`tutorial_oss.py <../../../examples/tutorial_oss.py>`_

0 comments on commit 5220f89

Please sign in to comment.