mindspore.ops.communication.gather
- mindspore.ops.communication.gather(tensor, gather_list, dst=0, group=None, async_op=False)[源代码]
从指定通信组中收集张量。操作将根据维度0从进程收集张量。
说明
只有进程 dst (全局rank)会保留收集的张量。其他进程将保留一个没有数学意义的张量列表。
集合中所有进程的tensor必须具有相同的shape和格式。
当前仅支持PyNative模式,不支持Graph模式。
- 参数:
tensor (Tensor) - 要收集的张量。
gather_list (list[Tensor]) - 用于收集数据的相同大小的张量列表。
dst (int, 可选) - 指定接收张量的进程的rank(全局rank)。只有进程 dst 会接收收集的张量。默认值:
0。group (str,可选) - 通信组名称。默认值:
None,即Ascend平台表示为"hccl_world_group"。async_op (bool, 可选) - 本算子是否是异步算子。默认值:
False。
- 返回:
CommHandle。若 async_op 是
True,CommHandle是一个异步工作句柄。若 async_op 是False,CommHandle将返回None。- 异常:
TypeError - 如果 tensor 的类型不是Tensor,或 gather_list 不是Tensor列表。
TypeError - 如果 dst 不是整数,group 不是字符串或 async_op 不是bool。
TypeError - 如果 gather_list 的大小不等于组大小。
TypeError - 如果 tensor 的类型或shape不等于 gather_list 的成员。
RuntimeError - 如果目标设备无效,或者后端无效,或者分布式初始化失败。
- 支持平台:
AscendCPU
样例:
>>> import numpy as np >>> import mindspore as ms >>> import mindspore.nn as nn >>> from mindspore.ops.communication import init_process_group, gather >>> from mindspore import Tensor >>> # Launch 2 processes. >>> init_process_group() >>> input = Tensor(np.arange(4).reshape([2, 2]).astype(np.float32)) >>> outputs = [Tensor(np.zeros([2, 2]).astype(np.float32)),Tensor(np.zeros([2, 2]).astype(np.float32))] >>> gather(input, outputs, dst=0) >>> print(outputs) # rank_0 [Tensor(shape=[2, 2], dtype=Float32, value= [[ 0.00000000e+00, 1.00000000e+00], [ 2.00000000e+00, 3.00000000e+00]]), Tensor(shape=[2, 2], dtype=Float32, value= [[ 0.00000000e+00, 1.00000000e+00], [ 2.00000000e+00, 3.00000000e+00]])] [Tensor(shape=[2, 2], dtype=Float32, value=[[ 0.00000000e+00, 1.00000000e+00], [ 2.00000000e+00, 3.00000000e+00]]), Tensor(shape=[2, 2], dtype=Float32, value= [[ 0.00000000e+00, 1.00000000e+00], [ 2.00000000e+00, 3.00000000e+00]])] # rank_1 [Tensor(shape=[2, 2], dtype=Float32, value=[[ 0.00000000e+00, 0.00000000e+00], [ 0.00000000e+00, 0.00000000e+00]]), Tensor(shape=[2, 2], dtype=Float32, value= [[ 0.00000000e+00, 0.00000000e+00], [ 0.00000000e+00, 0.00000000e+00]])] [Tensor(shape=[2, 2], dtype=Float32, value= [[ 0.00000000e+00, 0.00000000e+00], [ 0.00000000e+00, 0.00000000e+00]]), Tensor(shape=[2, 2], dtype=Float32, value= [[ 0.00000000e+00, 0.00000000e+00], [ 0.00000000e+00, 0.00000000e+00]])]