mindspore.communication.comm_func.all_reduce

View Source On Gitee
mindspore.communication.comm_func.all_reduce(tensor, op=ReduceOp.SUM, group=GlobalComm.WORLD_COMM_GROUP)[source]

Reduce tensors across all devices in such a way that all deviceswill get the same final result, returns the tensor which is all reduced.

Note

The tensors must have the same shape and format in all processes of the collection.

Parameters
  • tensor (Tensor) – The input tensor to be all reduced. The shape of tensor is \((x_1, x_2, ..., x_R)\).

  • op (str, optional) – Specifies an operation used for element-wise reductions, like sum, prod, max, and min. On the CPU, only 'sum' is supported. Default: ReduceOp.SUM .

  • group (str, optional) – The communication group to work on. Default: GlobalComm.WORLD_COMM_GROUP , which means "hccl_world_group" in Ascend, and "nccl_world_group" in GPU.

Returns

Tensor, has the same shape of the input, i.e., \((x_1, x_2, ..., x_R)\). The contents depend on the specified operation.

Raises
  • TypeError – If the type of the first input parameter is not Tensor, or any of op and group is not a str.

  • RuntimeError – If device target is invalid, or backend is invalid, or distributed initialization fails.

Supported Platforms:

Ascend GPU CPU

Examples

Note

Before running the following examples, you need to configure the communication environment variables.

For Ascend/GPU/CPU devices, it is recommended to use the msrun startup method without any third-party or configuration file dependencies. Please see the msrun start up for more details.

This example should be run with 2 devices.

>>> import numpy as np
>>> from mindspore.communication import init
>>> from mindspore.communication.comm_func import all_reduce
>>> from mindspore import Tensor
>>>
>>> init()
>>> input_tensor = Tensor(np.ones([2, 8]).astype(np.float32))
>>> output = all_reduce(input_tensor)
>>> print(output)
[[2. 2. 2. 2. 2. 2. 2. 2.]
 [2. 2. 2. 2. 2. 2. 2. 2.]]