---
title: AllToAllVOperation
description: "| 硬件型号 | 是否支持 |"
url: https://www.hiascend.com/document/detail/zh/canncommercial/latest/API/ascendtb/ascendtb_01_0136.html
sourcePath: /source/zh/canncommercial/900/API/ascendtb/ascendtb_01_0136.html
indexId: 58f9d55bfe197188eebfcf6c2bdaaef57b1a0fe82d8d3227ea0c5e6f49f9ccd764
---
# AllToAllVOperation

#### 产品支持情况

| 硬件型号 | 是否支持 |
| --- | --- |
| Atlas 350 加速卡 | x |
| Atlas A3 推理系列产品 / Atlas A3 训练系列产品 | √ |
| Atlas A2 训练系列产品 / Atlas A2 推理系列产品 | √ |
| Atlas 训练系列产品 | x |
| Atlas 推理系列产品 | x |
| Atlas 200I/500 A2 推理产品 | x |


#### 功能说明

向通信域内所有通信卡发送数据（数据量可以通过参数定制），并从所有通信卡接收数据（数据量可以通过参数定制）。

- 示例
  图1 算子输入输出示例

- 计算过程python示意
  1 2 3 4 5 6 golden_result = [] for j in range(rankSize): tensor_list = intensors[j].reshape(-1).tolist() # intensors[j]代表第j张卡上的intensor golden_result += tensor_list[sdispls[rank]:sdispls[rank]+sendCounts[rank]] size = sum(recvCounts) golden_out_tensor = torch.tensor(golden_result).reshape(1,size)


#### 算子上下文

图2 通信INT8量化中AllToAll的位置

模型传输一个int8的量化输入tensor数据X，首先使用AllToAll进行通信发送到各个节点上（这里使用int8进行通信，提升了通信速度）。

然后使用reduce的sum操作对x进行求和，将int8数据反量化为float16, 最后使用AllGather进行通信将计算结果传输到各个节点上。


#### 使用场景

用于将数据发送到各个节点上，多对多。对All-Gather的扩展，但不同的节点向某一节点收集到的数据是不同的。

图3 AllToAll和AllGather使用对比

应用于模型并行，模型并行里的矩阵转置，数据并行到模型并行的矩阵转置。


#### 定义

```
struct AllToAllVParam {
    int rank = 0;
    int rankSize = 0;
    int rankRoot = 0;
    std::vector<int64_t> sendCounts;
    std::vector<int64_t> sdispls;
    std::vector<int64_t> recvCounts;
    std::vector<int64_t> rdispls;
    std::string backend = "hccl";
    HcclComm hcclComm = nullptr;
    CommMode commMode = COMM_MULTI_PROCESS;
    std::string rankTableFile;
    std::string commDomain;
    uint8_t rsv[64] = {0};
};
```


#### 参数列表

| 成员名称 | 类型 | 默认值 | 描述 |
| --- | --- | --- | --- |
| rank | int | 0 | 当前卡所属通信编号。 |
| rankSize | int | 0 | 通信的卡的数量。 |
| rankRoot | int | 0 | 主通信编号。 |
| sendCounts | std::vector< int64\_t > | [] | 表示发送数据量的数组。 例如：若发送的数据类型为float32，sendCounts[i] = n 表示本rank发给rank i n个float32数据。 |
| sdispls | std::vector< int64\_t > | [] | 表示发送偏移量的数组。 sdispls[i] = n表示本rank从相对于输入起始位置的偏移量为n的位置开始发送数据给rank i。 |
| recvCounts | std::vector< int64\_t > | [] | 表示接收数据量的数组。 例如：若发送的数据类型为float32，recvCounts[i] = n 表示本rank从rank i收到n个float32数据。 |
| rdispls | std::vector< int64\_t > | [] | 表示接收偏移量的数组。 rdispls[i] = n表示本rank从相对于输出起始位置的偏移量为n的位置开始接收rank i的数据。 |
| backend | std::string | hccl | 通信计算类型，仅支持hccl。 |
| hcclComm | HcclComm | nullptr | HCCL通信域指针。 默认为空，加速库为用户创建；若用户想要自己管理通信域，则需要传入该通信域指针，加速库使用传入的通信域指针来执行通信算子。 |
| commMode | CommMode | COMM\_MULTI\_PROCESS | 通信模式，CommMode类型枚举值。hccl多线程只支持外部传入通信域方式。 |
| rankTableFile | std::string | \- | 集群信息的配置文件路径，适用单机以及多机通信场景，当前仅支持hccl后端场景。 若单机配置了ranktable，则以ranktable来初始化通信域。 配置请参见《HCCL集合通信库》的“rank table配置资源信息(https://www.hiascend.com/document/detail/zh/canncommercial/900/API/hcclug/hcclug\_000065.html)”。 |
| commDomain | std::string | \- | 通信Device组用通信域名标识，多通信域时使用。当backend为lccl时，commMode为多进程时，commDomain需要设置为0\-65535的数字。commMode为多线程时，不支持确定性计算，"LCCL\_DETERMINISTIC"需要为0或者false。 LCCL在多进程/多线程多通信域并发场景下，"LCCL\_PARALLEL"需要设置为1或者true。多通信域并行功能使用结束后，"LCCL\_PARALLEL"需要设置为0或者false，否则会导致基础场景性能下降。 支持设置通信域的内存大小，用于性能优化，配置方式通信域：通信域大小（MB）。如“0:400”，当同一个通信域中多个算子配置冲突时，以第一个执行到的该通信域算子配置为准。 限制：默认值为200，当前配置大小不支持小于200。 |
| rsv[64] | uint8\_t | {0} | 预留参数。 |


#### 输入

| 参数 | 维度 | 数据类型 | 格式 | 描述 |
| --- | --- | --- | --- | --- |
| x | [dim\_0, dim\_1, ..., dim\_n] | "hccl"：float16/float/int8/int16/int32/int64/bf16 | ND | 输入tensor。 |


#### 输出

| 参数 | 维度 | 数据类型 | 格式 | 描述 |
| --- | --- | --- | --- | --- |
| output | [1,recvCountsSum] recvCountsSum为参数recvCounts的所有元素之和 | "hccl": float16/float/int8/int16/int32/int64/bf16 数据类型和输入相同 | ND | 输出tensor，最后一维的shape为参数recvCounts的所有元素之和。 |


#### 约束说明

- 数组参数sendCounts，recvCounts，sdispls，rdispls的长度等于rankSize，且数组中元素值都大于0。
- 各个通信卡的输入tensor维度相等。
- sendCounts，recvCounts数组元素之和不能溢出int64，且recvCounts的数组和要大于0。对于rdispls中的任一元素，recvCounts[i] + rdispls[i] 不能大于recvCountsSum（即output的最后一维），对于sdispls中的任一元素，sendCounts[i] + sdispls[i] 不能大于输入tensor的数据量。如输入tensor shape为[3, 4, 5]，数据量为3*4*5 = 60，在进行AllToAllV时会被视为shape为[60]的tensor进行计算。
- rank、rankSize、rankRoot需满足以下条件。

  - 0 ≤ rank < rankSize
  - 0 ≤ rankRoot < rankSize

- 多用户使用时需要使用ATB_SHARE_MEMORY_NAME_SUFFIX环境变量（请参见  Transformer加速库环境变量说明
）进行共享内存的区分，以进行初始化信息同步。
- 当使用加速库的通信算子异常退出时，需要清空残留数据，避免影响之后的使用，命令参考如下：

```
rm -rf /dev/shm/sem.lccl*
rm -rf /dev/shm/sem.hccl*
ipcrm -a
```
