mindspore.mint.distributed.reduce_scatter

View Source On Gitee
mindspore.mint.distributed.reduce_scatter(output, input_list, op=ReduceOp.SUM, group=None, async_op=False)[source]

Reduces and scatters tensors from the specified communication group and returns the tensor which is reduced and scattered.

Note

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

Parameters
  • output (Tensor) – the output tensor.

  • input_list (list[Tensor]) – List of tensors to reduce and scatter.

  • op (str, optional) – Specifies an operation used for element-wise reductions, like SUM and MAX. Default: ReduceOp.SUM .

  • group (str, optional) – The communication group to work on. If None, which means "hccl_world_group" in Ascend. Default: None.

  • async_op (bool, optional) – Whether this operator should be an async operator. Default: False .

Returns

CommHandle, CommHandle is an async work handle, if async_op is set to True. CommHandle will be None, when async_op is False.

Raises
  • TypeError – If the type of output parameter is not Tensor, input_list is not Tensor List.

  • TypeError – If any of op and group is not a str. async_op is not bool or 'op' is invalid.

  • TypeError – If size of input_list is not equal to group size.

  • TypeError – If the type or shape of output not equal to the member of input_list.

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

Supported Platforms:

Ascend

Examples

Note

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

For Ascend 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.

>>> from mindspore import Tensor
>>> from mindspore.mint.distributed import init_process_group
>>> from mindspore.mint.distributed import reduce_scatter
>>> import numpy as np
>>>
>>> init_process_group()
>>> input_tensors = [Tensor(np.ones([4, 8]).astype(np.float32)), Tensor(np.ones([4, 8]).astype(np.float32))]
>>> output_tensor = Tensor(np.zeros([4, 8]).astype(np.float32))
>>> output = reduce_scatter(output_tensor ,input_tensors)
>>> print(output_tensor)
[[2. 2. 2. 2. 2. 2. 2. 2.]
 [2. 2. 2. 2. 2. 2. 2. 2.]
 [2. 2. 2. 2. 2. 2. 2. 2.]
 [2. 2. 2. 2. 2. 2. 2. 2.]]