mindspore.ops.AllGather
- class mindspore.ops.AllGather(group=GlobalComm.WORLD_COMM_GROUP)[源代码]
- 在指定的通信组中汇聚Tensor。 - 说明 - 集合中所有进程的Tensor必须具有相同的shape和格式。 
- 目前仅支持图模式且需要在Cell下调用。 
 - 参数:
- group (str) - 工作的通信组,默认值: - GlobalComm.WORLD_COMM_GROUP(即Ascend平台为- "hccl_world_group",GPU平台为- "nccl_world_group")。
 
- 输入:
- input_x (Tensor) - AllGather的输入,shape为 \((x_1, x_2, ..., x_R)\) 的Tensor。 
 
- 输出:
- Tensor,如果组中的device数量为N,则输出的shape为 \((N, x_1, x_2, ..., x_R)\) 。 
- 异常:
- TypeError - group 不是str。 
- ValueError - 调用进程的rank id大于本通信组的rank大小。 
 
- 支持平台:
- Ascend- GPU
 - 样例: - 说明 - 运行以下样例之前,需要配置好通信环境变量。 - 针对Ascend设备,用户需要准备rank表,设置rank_id和device_id,详见 rank table启动 。 - 针对GPU设备,用户需要准备host文件和mpi,详见 mpirun启动 。 - 针对CPU设备,用户需要编写动态组网启动脚本,详见 动态组网启动 。 - 该样例需要在2卡环境下运行。 - >>> import numpy as np >>> import mindspore as ms >>> import mindspore.ops as ops >>> import mindspore.nn as nn >>> from mindspore.communication import init >>> from mindspore import Tensor >>> >>> ms.set_context(mode=ms.GRAPH_MODE) >>> init() >>> class Net(nn.Cell): ... def __init__(self): ... super(Net, self).__init__() ... self.allgather = ops.AllGather() ... ... def construct(self, x): ... return self.allgather(x) ... >>> input_x = Tensor(np.ones([2, 8]).astype(np.float32)) >>> net = Net() >>> output = net(input_x) >>> print(output) [[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.]] - 教程样例: