Skip to content

Commit b17241f

Browse files
committed
polish doc: remove func use & update example
1 parent 1dd5881 commit b17241f

2 files changed

Lines changed: 94 additions & 66 deletions

File tree

python/paddle/distributed/spawn.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,6 @@ def spawn(func, args=(), nprocs=-1, join=True, daemon=False, **options):
236236
func (function): The target function is called by spawned process.
237237
This function need to be able to pickled, so it must be defined
238238
at the top level of a module.
239-
This function should be called as ``func(i, *args)``, ``i`` is
240-
the process index and ``args`` contains other arguments as tuple.
241239
args (tuple, optional): Arguments passed to ``func``.
242240
nprocs (int, optional): Number of processed to start. Default: -1.
243241
when nprocs is -1, the available device will be obtained from

python/paddle/fluid/dygraph/parallel.py

Lines changed: 94 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -349,38 +349,53 @@ def scale_loss(self, loss):
349349
Examples:
350350
.. code-block:: python
351351
352-
import numpy as np
353-
import paddle.fluid as fluid
354-
355-
place = fluid.CUDAPlace(fluid.dygraph.ParallelEnv().dev_id)
356-
with fluid.dygraph.guard(place):
357-
358-
# prepare the data parallel context
359-
strategy = fluid.dygraph.prepare_context()
360-
361-
linear = fluid.dygraph.Linear(1, 10, act="softmax")
362-
adam = fluid.optimizer.AdamOptimizer(
363-
learning_rate=0.001, parameter_list=linear.parameters())
364-
365-
# make the module become the data parallelism module
366-
linear = fluid.dygraph.DataParallel(linear, strategy)
367-
368-
x_data = np.random.random(size=[10, 1]).astype(np.float32)
369-
data = fluid.dygraph.to_variable(x_data)
370-
371-
hidden = linear(data)
372-
avg_loss = fluid.layers.mean(hidden)
373-
374-
# scale the loss according to the number of trainers.
375-
avg_loss = linear.scale_loss(avg_loss)
376-
377-
avg_loss.backward()
378-
379-
# collect the gradients of trainers.
380-
linear.apply_collective_grads()
381-
382-
adam.minimize(avg_loss)
383-
linear.clear_gradients()
352+
import paddle
353+
import paddle.nn as nn
354+
import paddle.optimizer as opt
355+
import paddle.distributed as dist
356+
357+
class LinearNet(nn.Layer):
358+
def __init__(self):
359+
super(LinearNet, self).__init__()
360+
self._linear1 = nn.Linear(10, 10)
361+
self._linear2 = nn.Linear(10, 1)
362+
363+
def forward(self, x):
364+
return self._linear2(self._linear1(x))
365+
366+
def train():
367+
# 1. enable dynamic mode
368+
paddle.disable_static()
369+
370+
# 2. initialize parallel environment
371+
dist.init_parallel_env()
372+
373+
# 3. create data parallel layer & optimizer
374+
layer = LinearNet()
375+
dp_layer = paddle.DataParallel(layer)
376+
377+
loss_fn = nn.MSELoss()
378+
adam = opt.Adam(
379+
learning_rate=0.001, parameters=dp_layer.parameters())
380+
381+
# 4. run layer
382+
inputs = paddle.randn([10, 10], 'float32')
383+
outputs = dp_layer(inputs)
384+
labels = paddle.randn([10, 1], 'float32')
385+
loss = loss_fn(outputs, labels)
386+
387+
loss = dp_layer.scale_loss(loss)
388+
loss.backward()
389+
dp_layer.apply_collective_grads()
390+
391+
adam.step()
392+
adam.clear_grad()
393+
394+
if __name__ == '__main__':
395+
# 1. start by ``paddle.distributed.spawn`` (default)
396+
dist.spawn(train, nprocs=2)
397+
# 2. start by ``paddle.distributed.launch``
398+
# train()
384399
"""
385400
if not self._is_data_parallel_mode():
386401
return loss
@@ -438,38 +453,53 @@ def apply_collective_grads(self):
438453
Examples:
439454
.. code-block:: python
440455
441-
import numpy as np
442-
import paddle.fluid as fluid
443-
444-
place = fluid.CUDAPlace(fluid.dygraph.ParallelEnv().dev_id)
445-
with fluid.dygraph.guard(place):
446-
447-
# prepare the data parallel context
448-
strategy = fluid.dygraph.prepare_context()
449-
450-
linear = fluid.dygraph.Linear(1, 10, act="softmax")
451-
adam = fluid.optimizer.AdamOptimizer(
452-
learning_rate=0.001, parameter_list=linear.parameters())
453-
454-
# make the module become the data parallelism module
455-
linear = fluid.dygraph.DataParallel(linear, strategy)
456-
457-
x_data = np.random.random(size=[10, 1]).astype(np.float32)
458-
data = fluid.dygraph.to_variable(x_data)
459-
460-
hidden = linear(data)
461-
avg_loss = fluid.layers.mean(hidden)
462-
463-
# scale the loss according to the number of trainers.
464-
avg_loss = linear.scale_loss(avg_loss)
465-
466-
avg_loss.backward()
467-
468-
# collect the gradients of trainers.
469-
linear.apply_collective_grads()
470-
471-
adam.minimize(avg_loss)
472-
linear.clear_gradients()
456+
import paddle
457+
import paddle.nn as nn
458+
import paddle.optimizer as opt
459+
import paddle.distributed as dist
460+
461+
class LinearNet(nn.Layer):
462+
def __init__(self):
463+
super(LinearNet, self).__init__()
464+
self._linear1 = nn.Linear(10, 10)
465+
self._linear2 = nn.Linear(10, 1)
466+
467+
def forward(self, x):
468+
return self._linear2(self._linear1(x))
469+
470+
def train():
471+
# 1. enable dynamic mode
472+
paddle.disable_static()
473+
474+
# 2. initialize parallel environment
475+
dist.init_parallel_env()
476+
477+
# 3. create data parallel layer & optimizer
478+
layer = LinearNet()
479+
dp_layer = paddle.DataParallel(layer)
480+
481+
loss_fn = nn.MSELoss()
482+
adam = opt.Adam(
483+
learning_rate=0.001, parameters=dp_layer.parameters())
484+
485+
# 4. run layer
486+
inputs = paddle.randn([10, 10], 'float32')
487+
outputs = dp_layer(inputs)
488+
labels = paddle.randn([10, 1], 'float32')
489+
loss = loss_fn(outputs, labels)
490+
491+
loss = dp_layer.scale_loss(loss)
492+
loss.backward()
493+
dp_layer.apply_collective_grads()
494+
495+
adam.step()
496+
adam.clear_grad()
497+
498+
if __name__ == '__main__':
499+
# 1. start by ``paddle.distributed.spawn`` (default)
500+
dist.spawn(train, nprocs=2)
501+
# 2. start by ``paddle.distributed.launch``
502+
# train()
473503
"""
474504
if not self._is_data_parallel_mode():
475505
return

0 commit comments

Comments
 (0)