MCPcopy Create free account
hub / github.com/PaddlePaddle/Paddle / init_parallel_env

Function init_parallel_env

python/paddle/distributed/parallel.py:1003–1280  ·  view source on GitHub ↗

Initialize parallel training environment in dynamic graph mode. Note: Now initialize both `NCCL` and `GLOO` contexts for communication. Args: backend (string): A string represents the backend used by DataParallel, should be one of 'gloo'(for cpu), 'nccl'(f

(nccl_config: NCCLConfig | None = None)

Source from the content-addressed store, hash-verified

1001
1002
1003def init_parallel_env(nccl_config: NCCLConfig | None = None) -> Group:
1004 """
1005
1006 Initialize parallel training environment in dynamic graph mode.
1007
1008 Note:
1009 Now initialize both `NCCL` and `GLOO` contexts for communication.
1010
1011 Args:
1012 backend (string): A string represents the backend used by DataParallel,
1013 should be one of 'gloo'(for cpu), 'nccl'(for cuda), 'bkcl'(for xpu), 'auto'(auto detect).
1014 The auto detection prefer 'nccl', 'bkcl' than 'gloo'.
1015
1016 Returns:
1017 None
1018
1019 Examples:
1020 .. code-block:: pycon
1021
1022 >>> # doctest: +REQUIRES(env:GPU, env:DISTRIBUTED)
1023 >>> import paddle
1024 >>> import paddle.nn as nn
1025 >>> import paddle.optimizer as opt
1026 >>> import paddle.distributed as dist
1027
1028 >>> class LinearNet(nn.Layer):
1029 ... def __init__(self):
1030 ... super().__init__()
1031 ... self._linear1 = nn.Linear(10, 10)
1032 ... self._linear2 = nn.Linear(10, 1)
1033 ...
1034 ... def forward(self, x):
1035 ... return self._linear2(self._linear1(x))
1036
1037 >>> def train():
1038 ... # 1. initialize parallel environment
1039 ... dist.init_parallel_env()
1040 ... # 2. create data parallel layer & optimizer
1041 ... layer = LinearNet()
1042 ... dp_layer = paddle.DataParallel(layer)
1043 ... loss_fn = nn.MSELoss()
1044 ... adam = opt.Adam(learning_rate=0.001, parameters=dp_layer.parameters())
1045 ... # 3. run layer
1046 ... inputs = paddle.randn([10, 10], 'float32')
1047 ... outputs = dp_layer(inputs)
1048 ... labels = paddle.randn([10, 1], 'float32')
1049 ... loss = loss_fn(outputs, labels)
1050 ... loss.backward()
1051 ... adam.step()
1052 ... adam.clear_grad()
1053
1054 >>> if __name__ == '__main__':
1055 ... dist.spawn(train)
1056
1057 """
1058
1059 modified_flags = _get_modified_flags()
1060 _print_modified_flags(modified_flags)

Callers

nothing calls this directly

Calls 15

_set_expected_placeFunction · 0.90
_get_group_map_by_nameFunction · 0.90
_set_default_backendFunction · 0.90
_set_default_storeFunction · 0.90
_new_process_group_implFunction · 0.90
message2nccl_configFunction · 0.90
_set_group_map_by_nameFunction · 0.90
_set_group_mapFunction · 0.90
_set_group_map_backendFunction · 0.90
_add_new_groupFunction · 0.90
wait_server_readyFunction · 0.90
_get_modified_flagsFunction · 0.85

Tested by

no test coverage detected