mindspore.ops.communication.all_gather_into_tensor

查看源文件
mindspore.ops.communication.all_gather_into_tensor(output_tensor, input_tensor, group=None, async_op=False)[源代码]

汇聚指定的通信组中的tensor,并返回汇聚后的tensor。

说明

集合中所有进程的tensor必须具有相同的shape和格式。

参数:
  • output_tensor (Tensor) - 输出待汇聚操作的tensor。如果组中的device数量为N,则输出tensor的shape为 \((N*x_1, x_2, ..., x_R)\) 。如果函数以非in-place模式运行,此参数无效。

  • input_tensor (Tensor) - 输入待汇聚操作的tensor。tensor的shape为 \((x_1, x_2, ..., x_R)\)

  • group (str,可选) - 通信组名称。默认值: None ,即Ascend平台表示为 "hccl_world_group"

  • async_op (bool, 可选) - 本算子是否是异步算子。默认值: False

返回:
  • 若函数以in-place模式运行,返回CommHandle。

  • 若函数以非in-place模式运行,返回Tuple(Tensor, CommHandle)。第一个元素存储输出结果,第二个元素是CommHandle。

其中,当 async_opTrue ,则CommHandle是一个异步工作句柄;当 async_opFalse ,则CommHandle将返回 None

异常:
  • TypeError - output_tensorinput_tensor 输入的数据类型不为Tensor, group 不是str, async_op 不是bool。

  • RuntimeError - 如果目标设备无效,或后端无效,或分布式初始化失败。

支持平台:

Ascend

样例:

说明

运行以下样例之前,需要配置好通信环境变量。

针对Ascend设备,推荐使用msrun启动方式,无第三方以及配置文件依赖。详见 msrun启动

该样例需要在2卡环境下运行。

>>> import numpy as np
>>> import mindspore as ms
>>> from mindspore import ops
>>> from mindspore.ops.communication import init_process_group
>>> from mindspore.ops.communication import all_gather_into_tensor
>>> from mindspore import Tensor
>>>
>>> ms.set_device(device_target="Ascend")
>>> init_process_group()
>>> input_tensor = Tensor(np.ones([2, 8]).astype(np.float32))
>>> out_tensor = Tensor(np.zeros([4, 8]).astype(np.float32))
>>> output = all_gather_into_tensor(out_tensor, input_tensor)
>>> print(out_tensor)
[[1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1.]]