init_parallel_env

paddle.distributed. init_parallel_env ( ) [源代码]

初始化动态图模式下的并行训练环境。

注解

目前同时初始化 NCCLGLOO 上下文用于通信。

返回

代码示例

  1. import paddle
  2. import paddle.nn as nn
  3. import paddle.optimizer as opt
  4. import paddle.distributed as dist
  5. class LinearNet(nn.Layer):
  6. def __init__(self):
  7. super(LinearNet, self).__init__()
  8. self._linear1 = nn.Linear(10, 10)
  9. self._linear2 = nn.Linear(10, 1)
  10. def forward(self, x):
  11. return self._linear2(self._linear1(x))
  12. def train():
  13. # 1. initialize parallel environment
  14. dist.init_parallel_env()
  15. # 2. create data parallel layer & optimizer
  16. layer = LinearNet()
  17. dp_layer = paddle.DataParallel(layer)
  18. loss_fn = nn.MSELoss()
  19. adam = opt.Adam(
  20. learning_rate=0.001, parameters=dp_layer.parameters())
  21. # 3. run layer
  22. inputs = paddle.randn([10, 10], 'float32')
  23. outputs = dp_layer(inputs)
  24. labels = paddle.randn([10, 1], 'float32')
  25. loss = loss_fn(outputs, labels)
  26. loss.backward()
  27. adam.step()
  28. adam.clear_grad()
  29. if __name__ == '__main__':
  30. dist.spawn(train)