aclnnQuantGroupedMatMulAlltoAllv

📄 View Source Code

Applicable Products

  • Ascend 950DT products: Supported
  • Atlas A3 products: Not supported
  • Atlas A2 products: Not supported
  • Atlas 200I/500 A2 inference products: Not supported
  • Atlas inference products: Not supported
  • Atlas training products: Not supported

Function

  • Description:

    • Completes the fusion of quantized routing expert GroupedMatMul, Unpermute, and AlltoAllv, and fuses in parallel with shared expert MatMul. Computation is performed before communication. Supports pertensor-pertensor and mx quantization modes.
  • Formula:

    • Routed experts:

      $$ gmmY = (gmmX @ gmmWeight) * gmmXScale * gmmWeightScale \\ unpermuteOut = Unpermute(gmmY) \\ y = AlltoAllv(unpermuteOut) $$
    • Shared expert:

      $$ mmY = (mmX @ mmWeight) * mmXScaleOptional * mmWeightScaleOptional $$

Function Prototype

This operator consists of a two-phase API. Call the aclnnQuantGroupedMatMulAlltoAllvGetWorkspaceSize API first to obtain the input parameters, calculate the required workspace size based on the computation process, and obtain the executor that contains the operator computation process. Then, call the aclnnQuantGroupedMatMulAlltoAllv API to perform the computation.

aclnnStatus aclnnQuantGroupedMatMulAlltoAllvGetWorkspaceSize(
    const aclTensor*   gmmX,
    const aclTensor*   gmmWeight,
    const aclTensor*   gmmXScale,
    const aclTensor*   gmmWeightScale,
    const aclTensor*   sendCountsTensorOptional,
    const aclTensor*   recvCountsTensorOptional,
    const aclTensor*   mmXOptional,
    const aclTensor*   mmWeightOptional,
    const aclTensor*   mmXScaleOptional,
    const aclTensor*   mmWeightScaleOptional,
    const aclTensor*   commQuantScaleOptional,
    int64_t            gmmXQuantMode,
    int64_t            gmmWeightQuantMode,
    int64_t            mmXQuantMode,
    int64_t            mmWeightQuantMode,
    int64_t            commQuantMode,
    int64_t            commQuantDtypeOptional,
    int64_t            groupSize,
    const char*        group,
    int64_t            epWorldSize,
    const aclIntArray* sendCounts,
    const aclIntArray* recvCounts,
    bool               transGmmWeight,
    bool               transMmWeight,
    const aclTensor*   y,
    const aclTensor*   mmYOptional,
    uint64_t*          workspaceSize,
    aclOpExecutor**    executor)
aclnnStatus aclnnQuantGroupedMatMulAlltoAllv(
    void*           workspace,
    uint64_t        workspaceSize,
    aclOpExecutor*  executor,
    aclrtStream     stream)

aclnnQuantGroupedMatMulAlltoAllvGetWorkspaceSize

  • Parameters

    Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor
    gmmX Input The input gmmX in the formula. Shape (A, H1). HIFLOAT8, FLOAT8_E4M3FN, FLOAT8_E5M2, FLOAT4_E2M1 ND 2 x
    gmmWeight Input The input gmmWeight in the formula. shape (e, H1, N1). e is the number of experts deployed for each card, H1 is the hidden size, and N1 is the intermediate dimension of the routing expert FFN. HIFLOAT8, FLOAT8_E4M3FN, FLOAT8_E5M2, FLOAT4_E2M1 ND 3 x
    gmmXScale Input Quantization coefficient of gmmX.
  • pertensor quantization: shape (1).
  • mx quantization: shape (A, ceil(H1/64), 2)
  • FLOAT32, FLOAT8_E8M0 ND
  • pertensor quantization: 1
  • mx quantization: 3
  • x
    gmmWeightScale Input Quantization coefficient of gmmWeight.
  • pertensor quantization: shape (1).
  • mx quantization: shape (e, N1, ceil(H1/64), 2), or (e, ceil(H1/64), N1, 2) if weight is transposed.
  • FLOAT32, FLOAT8_E8M0 ND
  • pertensor quantization: 1
  • mx quantization: 3
  • x
    sendCountsTensorOptional Input Send count used by AlltoAllv. Currently only supports empty. shape (e * ep, ). e is the number of deployed experts for each card, and ep is the ep domain size. INT64 ND 1 x
    recvCountsTensorOptional Input Recv count used by AlltoAllv. Currently only supports empty. Shape (e * ep, ). e is the number of experts deployed on each card, and ep is the size of the ep domain. INT64 ND 1 x
    mmXOptional Input Input mmX in the formula. shape (BS, H2). BS indicates batch sequence size, and H2 indicates hidden size. HIFLOAT8, FLOAT8_E4M3FN, FLOAT8_E5M2, FLOAT4_E2M1 ND 2 x
    mmWeightOptional Input Input mmWeight in the formula. shape (H2, N2). H2 indicates hidden size, and N2 indicates the intermediate layer dimension of the shared expert FFN. HIFLOAT8, FLOAT8_E4M3FN, FLOAT8_E5M2, FLOAT4_E2M1 ND 2 x
    mmXScaleOptional Input Quantization coefficient of mmX.
  • pertensor quantization: shape (1).
  • mx quantization: shape (BS, ceil(H2/64), 2)
  • FLOAT32, FLOAT8_E8M0 ND
  • pertensor quantization: 1
  • mx quantization: 3
  • x
    mmWeightScaleOptional Input Quantization coefficient of mmWeight.
  • pertensor quantization: shape (1).
  • mx quantization: shape (N2, ceil(H2/64), 2), or shape (ceil(H2/64), N2, 2) when weight is transposed.
  • FLOAT32, FLOAT8_E8M0 ND
  • pertensor quantization: 1
  • mx quantization: 3
  • x
    commQuantScaleOptional Input Low-bit communication quantization coefficient. Reserved parameter. Currently only supports empty. FLOAT32 ND 1 x
    gmmXQuantMode Input Quantization mode of gmmX. The quantization mode must be passed in. Currently supports 1 (pertensor quantization) and 6 (mx quantization). INT64 - 1 x
    gmmWeightQuantMode Input Quantization mode of gmmWeight. The quantization mode must be passed in. Currently supports 1 (pertensor quantization) and 6 (mx quantization). INT64 - 1 x
    mmXQuantMode Input Quantization mode of mmX. If mmX is not empty, the quantization mode must be passed in. Currently, 1 (pertensor quantization) and 6 (mx quantization) are supported. INT64 - 1 x
    mmWeightQuantMode Input Quantization mode of mmWeight. If mmWeight is not empty, the quantization mode must be passed in. Currently supports 1 (pertensor quantization) and 6 (mx quantization). INT64 - 1 x
    commQuantMode Input Low-bit communication quantization mode. The low-bit feature is currently reserved. 0 must be passed in, indicating no quantization. INT64 - 1 x
    commQuantDtypeOptional Input Data type of low-bit communication. Currently reserved for the low-bit feature. -1 must be passed in. INT64 - 1 x
    groupSize Input PerGroup quantization group size. Specifies the quantization group sizes in three directions for the Matmul computation. This is a reserved parameter. The value is valid only when the quantization Scale input has 2 dimensions and above. In other use cases, 0 must be passed in. The groupSize input consists of three values: groupSizeM, groupSizeN, and groupSizeK, which correspond to the three directions. Each value occupies 16 bits. Together they occupy the low 48 bits of the int64_t groupSize (the high 16 bits are invalid). The formula is as follows: groupSize = groupSizeK | groupSizeN << 16 | groupSizeM << 32. INT64 - - -
    group Input Communicator ID. String length must be greater than 0 and less than 128. char* - - -
    epWorldSize Input Size of the communicator. Supports 2/4/8/16/32/64/128/256. INT64 - - -
    sendCounts Input AlltoAllv send count. Indicates the number of tokens sent from other ranks to each expert on the current rank. Supported dimensions: e * ep. Expanded in one dimension according to sendCounts[fromRank][expertId]. For example, when e=3, the order is e0,e1,e2,e0,e1,e2, ... aclIntArray* (element type INT64) ND - -
    recvCounts Input AlltoAllv recv count. Indicates the number of tokens that the current device needs to receive after AlltoAllv. The supported dimension is e * ep. It is flattened into one dimension by recvCounts[fromRank][expertId]. For example, if e=3, the order is e0,e1,e2,e0,e1,e2, ... aclIntArray* (element type INT64) ND - -
    transGmmWeight Input Whether the right matrix of gmm is transposed. Must be passed in. No default value. BOOL ND - -
    transMmWeight Input Whether the right matrix of mm is transposed. Must be passed in. No default value. BOOL ND - -
    y Output Output of the GroupedMatMul computation. Empty tensors are not supported. Shape (BSK, N1). FLOAT16, BFLOAT16 ND 2 x
    mmYOptional Output Output of the matmul computation. Shape (bs, N2). FLOAT16, BFLOAT16 ND 2 x
    workspaceSize Output Returns the workspace size to be applied on the Device side. - UINT64 ND - -
    executor Output Returns the op executor, which contains the operator computation process. - aclOpExecutor* ND - -

    The enumeration values of gmmXQuantMode, gmmWeightQuantMode, mmXQuantMode, mmWeightQuantMode, and commQuantMode correspond to the quantization mode as follows:

    • 0: non-quantized
    • 1: pertensor
    • 2: perchannel
    • 3: pertoken
    • 4: pergroup
    • 5: perblock
    • 6: mx quantization
    • 7: pertoken dynamic quantization
  • Return Value

    aclnnStatus: return status code. For details, see aclnn Return Codes.

    The first-phase API performs input parameter validation and returns an error in the following scenarios:

    Return Value Error Code Description
    ACLNN_ERR_PARAM_NULLPTR 161001 A required input or output parameter Tensor is a null pointer.
    ACLNN_ERR_PARAM_INVALID 161002 The data type of input or output is not in the supported range.

aclnnQuantGroupedMatMulAlltoAllv

  • Parameters

    Parameter Input/Output Description
    workspace Input Workspace memory address applied on the Device side.
    workspaceSize Input Size of the workspace applied on the Device side, obtained by the first-phase API aclnnQuantGroupedMatMulAlltoAllvGetWorkspaceSize.
    executor Input Op executor that contains the operator computation flow.
    stream Input Specifies the Stream for executing tasks.
  • Return Value

    aclnnStatus: return status code. For details, see aclnn Return Codes.

Constraints

  • Deterministic computation:

    • aclnnQuantGroupedMatMulAlltoAllv defaults to a deterministic implementation.
  • Communication engine constraints:

    • Ascend 950DT products: Support AI_CPU communication.
  • The maximum product of e * epWorldSize is 256, where e (the number of experts per card) supports a maximum of 32, and epWorldSize supports 2/4/8/16/32/64/128/256;

  • The shape of gmmX is (A, H1), where A is the sum of sendCounts, and the value range of H1 is (0, 65536);

  • The shape of gmmWeight is (e, H1, N1), and the value range of N1 is (0, 65536);

  • The shape of y is (BSK, N1), where K in the first dimension has a range of [2, 8], and BSK is the sum of recvCounts;

  • mmX is the left matrix of the shared expert, with the shape (BS, H2). The value range of H2 is (0, 12288];

  • mmWeight is the right matrix of the shared expert, with the shape (H2, N2). The value range of N2 is (0, 65536);

  • sendCounts indicates the number of tokens sent to other devices, and the array size is e * epWorldSize;

  • recvCounts indicates the number of tokens received from other devices, and the array size is e * epWorldSize;

  • The quantization Scale, Mode, and other parameters of routed experts and shared experts are all mandatory;

  • low-bit communication Mode is a mandatory parameter; DType and Scale are optional and must be provided when Mode is non-zero;

  • Variables used in shape in the parameter description:

    • BSK: The number of tokens received by the current card, which is the cumulative sum of the recvCounts parameter. Value range: (0, 52428800].
    • H1: Indicates the hidden size of the routed expert. Value range: (0, 65536).
    • H2: Indicates the hidden size of the shared expert. Value range: (0, 12288].
    • e: indicates the number of experts on a single device. 0<e<=32, and e * epWorldSize supports a maximum of 256.
    • N1: indicates the intermediate layer dimension of the routing expert FFN. Value range: (0, 65536).
    • N2: indicates the intermediate layer dimension of the shared expert FFN. Value range: (0, 65536).
    • BS: batch sequence size.
    • K: indicates the number of TopK experts selected. K ranges from [2, 8].
    • A: the number of tokens sent by the current card, which is the cumulative sum of the sendCounts parameter.
    • The sum of the A parameters of all devices in the ep communicator equals the sum of the BSK parameters on all devices.
    • For mx quantization, when gmmX and gmmWeight are FLOAT4_E2M1, H1 and H2 must be even numbers and cannot be 2. In addition, when transGmmWeight and transMmWeight are false, N1 and N2 must be even numbers.
    • The transpose states of gmmWeight and gmmWeightScale must be consistent: both transposed or both not transposed. The transpose states of mmWeight and mmWeightScale must also be consistent.
    • groupSize:
      • The value of groupSize is valid only when the gmmXScale/gmmWeightScale/mmXScale/mmWeightScale inputs are all 2 dimensions and above. In other use cases, 0 must be passed in.
      • The groupSize value supports formula deduction: the input groupSize is internally decomposed into groupSizeM, groupSizeN, and groupSizeK according to the following formula. If one or more of them is 0, the groupSizeM, groupSizeN, and groupSizeK used for computation are reset based on the shapes of the gmmX/gmmWeight/mmX/mmWeight/gmmXScale/gmmWeightScale/mmXScale/mmWeightScale inputs. Setting principle: If groupSizeM=0, the quantization group value in the m direction is determined by interface deduction, and the deduction formula is groupSizeM = m / scaleM (m must be divisible by scaleM), where m is consistent with m in the gmmX/mmX shape and scaleM is consistent with m in the gmmXScale/mmXScale shape; if groupSizeK=0, the quantization group value in the k direction is determined by interface deduction, and the deduction formula is groupSizeK = k / scaleK (k must be divisible by scaleK), where k is consistent with k in the gmmX/mmX shape and scaleK is consistent with k in the gmmXScale/mmXScale shape; if groupSizeN=0, the quantization group value in the n direction is determined by interface deduction, and the deduction formula is groupSizeN = n / scaleN (n must be divisible by scaleN), where n is consistent with n in the gmmWeight/mmWeight shape and scaleN is consistent with n in the gmmWeightScale/mmWeightScale shape.
        $$ groupSize = groupSizeK | groupSizeN << 16 | groupSizeM << 32 $$
      • If the reset condition is met, when the gmmXScale/gmmWeightScale/mmXScale/mmWeightScale inputs are 2 dimensions and above and their data types are all FLOAT8_E8M0, the [groupSizeM, groupSizeN, groupSizeK] value combination is derived as [1, 1, 32], and the corresponding groupSize value is 4295032864.
  • Quantization parameter constraints:

    • The current version supports pertensor quantization and mx quantization.
  • Type constraints

    • Pertensor quantization

      gmmXgmmWeightgmmXScalegmmWeightScalemmXScalemmWeightScaley
      HIFLOAT8HIFLOAT8FLOAT32FLOAT32FLOAT32FLOAT32FLOAT16/BFLOAT16
    • mx quantization

      gmmXgmmWeightgmmXScalegmmWeightScalemmXScalemmWeightScaley
      FLOAT8_E4M3FNFLOAT8_E4M3FNFLOAT8_E8M0FLOAT8_E8M0FLOAT8_E8M0FLOAT8_E8M0FLOAT16/BFLOAT16
      FLOAT8_E4M3FNFLOAT8_E5M2FLOAT8_E8M0FLOAT8_E8M0FLOAT8_E8M0FLOAT8_E8M0FLOAT16/BFLOAT16
      FLOAT8_E5M2FLOAT8_E4M3FNFLOAT8_E8M0FLOAT8_E8M0FLOAT8_E8M0FLOAT8_E8M0FLOAT16/BFLOAT16
      FLOAT8_E5M2FLOAT8_E5M2FLOAT8_E8M0FLOAT8_E8M0FLOAT8_E8M0FLOAT8_E8M0FLOAT16/BFLOAT16
      FLOAT4_E2M1FLOAT4_E2M1FLOAT8_E8M0FLOAT8_E8M0FLOAT8_E8M0FLOAT8_E8M0FLOAT16/BFLOAT16
    • The mmX type and gmmX type must be consistent, the mmWeight type and gmmWeight type must be consistent, and the mmY type and y type must be consistent.

Example

The following is sample code for reference only. For details about compilation and execution, see Compile and Run Samples.

NOTE: The quantization API only supports Ascend 950DT products. The following examples are based on this products. The sample code uses 2 devices. Modify EP_WORLD_SIZE based on the actual number of devices.

  • Ascend 950DT products:
#include <thread>
#include <iostream>
#include <string>
#include <vector>
#include "acl/acl.h"
#include "hccl/hccl.h"
#include "aclnnop/aclnn_quant_grouped_mat_mul_allto_allv.h"

#define CHECK_RET(cond, return_expr) \
    do {                             \
        if (!(cond)) {               \
            return_expr;             \
        }                            \
    } while (0)

#define LOG_PRINT(message, ...)          \
    do {                                 \
        printf(message, ##__VA_ARGS__);  \
    } while (0)

int64_t GetShapeSize(const std::vector<int64_t> &shape)
{
    int64_t shape_size = 1;
    for (auto i : shape) {
        shape_size *= i;
    }
    return shape_size;
}

template <typename T>
int CreateAclTensor(const std::vector<T> &hostData, const std::vector<int64_t> &shape, void **deviceAddr,
    aclDataType dataType, aclTensor **tensor)
{
    auto size = GetShapeSize(shape) * sizeof(T);
    auto ret = aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("[ERROR] aclrtMalloc failed. ret: %d\n", ret); return ret);
    ret = aclrtMemcpy(*deviceAddr, size, hostData.data(), size, ACL_MEMCPY_HOST_TO_DEVICE);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("[ERROR] aclrtMemcpy failed. ret: %d\n", ret); return ret);
    std::vector<int64_t> strides(shape.size(), 1);
    for (int64_t i = shape.size() - 2; i >= 0; i--) {
        strides[i] = shape[i + 1] * strides[i + 1];
    }
    *tensor = aclCreateTensor(shape.data(),
        shape.size(),
        dataType,
        strides.data(),
        0,
        aclFormat::ACL_FORMAT_ND,
        shape.data(),
        shape.size(),
        *deviceAddr);
    return 0;
}

struct Args {
    int rankId;
    HcclComm hcclComm;
    aclrtStream stream;
    aclrtContext context;
};

// Basic shape information
constexpr int64_t EP_WORLD_SIZE = 2;
constexpr int64_t BS = 4096;
constexpr int64_t K = 2;
constexpr int64_t H = 7168;
constexpr int64_t e = 4;
constexpr int64_t N1 = 4096;
constexpr int64_t N2 = 4096;
constexpr int64_t A = BS * K;

std::vector<int16_t> pGmmYData(BS *K *N1, 0);
std::vector<int16_t> pmmYData(BS *N2, 0);

int LaunchOneThreadAlltoAllvGmm(Args &args)
{
    int ret = aclrtSetCurrentContext(args.context);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("[ERROR] aclrtSetCurrentContext failed. ret: %d\n", ret); return ret);
    char hcomName[128] = {0};
    ret = HcclGetCommName(args.hcclComm, hcomName);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("[ERROR] HcclGetCommName failed. ret: %d\n", ret); return -1);

    std::vector<int64_t> gmmXShape = {A, H};
    std::vector<int64_t> gmmWShape = {e, H, N1};
    std::vector<int64_t> gmmXScaleShape = {1};
    std::vector<int64_t> gmmWScaleShape = {1};
    std::vector<int64_t> yShape = {BS * K, N1};
    std::vector<int64_t> mmXShape = {BS, H};
    std::vector<int64_t> mmWShape = {H, N2};
    std::vector<int64_t> mmXScaleShape = {1};
    std::vector<int64_t> mmWScaleShape = {1};
    std::vector<int64_t> mmYShape = {BS, N2};

    std::vector<int64_t> sendCountsShape = {EP_WORLD_SIZE * e};
    std::vector<int64_t> recvCountsShape = {EP_WORLD_SIZE * e};

    std::vector<int64_t> sendCountsList(EP_WORLD_SIZE * e, A / (EP_WORLD_SIZE * e));
    std::vector<int64_t> recvCountsList(EP_WORLD_SIZE * e, A / (EP_WORLD_SIZE * e));

    void *gmmXDeviceAddr = nullptr;
    void *gmmWDeviceAddr = nullptr;
    void *gmmXScaleDeviceAddr = nullptr;
    void *gmmWScaleDeviceAddr = nullptr;
    void *yDeviceAddr = nullptr;
    void *mmXDeviceAddr = nullptr;
    void *mmWDeviceAddr = nullptr;
    void *mmXScaleDeviceAddr = nullptr;
    void *mmWScaleDeviceAddr = nullptr;
    void *mmYDeviceAddr = nullptr;

    aclTensor *gmmX = nullptr;
    aclTensor *gmmW = nullptr;
    aclTensor *gmmXScale = nullptr;
    aclTensor *gmmWScale = nullptr;
    aclTensor *y = nullptr;

    aclTensor *mmX = nullptr;
    aclTensor *mmW = nullptr;
    aclTensor *mmXScale = nullptr;
    aclTensor *mmWScale = nullptr;
    aclTensor *mmY = nullptr;

    aclTensor *sendCountsTensor = nullptr;
    aclTensor *recvCountsTensor = nullptr;
    aclTensor *commQuantScaleOptional = nullptr;

    int64_t gmmXQuantMode = 1;
    int64_t gmmWQuantMode = 1;
    int64_t mmXQuantMode = 1;
    int64_t mmWQuantMode = 1;
    int64_t commQuantMode = 0;
    int64_t commQuantDtypeOptional = -1;
    int64_t groupSize = 0;

    uint64_t workspaceSize = 0;
    aclOpExecutor *executor = nullptr;
    void *workspaceAddr = nullptr;

    long long gmmXShapeSize = GetShapeSize(gmmXShape);
    long long gmmWShapeSize = GetShapeSize(gmmWShape);
    long long gmmXScaleShapeSize = GetShapeSize(gmmXScaleShape);
    long long gmmWScaleShapeSize = GetShapeSize(gmmWScaleShape);
    long long yShapeSize = GetShapeSize(yShape);

    long long mmXShapeSize = GetShapeSize(mmXShape);
    long long mmWShapeSize = GetShapeSize(mmWShape);
    long long mmXScaleShapeSize = GetShapeSize(mmXScaleShape);
    long long mmWScaleShapeSize = GetShapeSize(mmWScaleShape);
    long long mmYShapeSize = GetShapeSize(mmYShape);

    std::vector<uint8_t> gmmXHostData(gmmXShapeSize, (args.rankId + 1) * 1024);  // HIFLOAT8
    std::vector<uint8_t> gmmWHostData(gmmWShapeSize, (args.rankId + 1) * 512);
    std::vector<float> gmmXScaleHostData(gmmXScaleShapeSize, 1);
    std::vector<float> gmmWScaleHostData(gmmWScaleShapeSize, 1);
    std::vector<int16_t> yHostData(yShapeSize, 65535);

    std::vector<uint8_t> mmXHostData(mmXShapeSize, (args.rankId + 1) * 1024);  // HIFLOAT8
    std::vector<uint8_t> mmWHostData(mmWShapeSize, (args.rankId + 1) * 512);
    std::vector<float> mmXScaleHostData(mmXScaleShapeSize, 1);
    std::vector<float> mmWScaleHostData(mmWScaleShapeSize, 1);
    std::vector<int16_t> mmYHostData(mmYShapeSize, 0);

    ret = CreateAclTensor(gmmXHostData, gmmXShape, &gmmXDeviceAddr, aclDataType::ACL_HIFLOAT8, &gmmX);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    ret = CreateAclTensor(gmmWHostData, gmmWShape, &gmmWDeviceAddr, aclDataType::ACL_HIFLOAT8, &gmmW);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    ret = CreateAclTensor(gmmXScaleHostData, gmmXScaleShape, &gmmXScaleDeviceAddr, aclDataType::ACL_FLOAT, &gmmXScale);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    ret = CreateAclTensor(gmmWScaleHostData, gmmWScaleShape, &gmmWScaleDeviceAddr, aclDataType::ACL_FLOAT, &gmmWScale);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    ret = CreateAclTensor(yHostData, yShape, &yDeviceAddr, aclDataType::ACL_FLOAT16, &y);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    ret = CreateAclTensor(mmXHostData, mmXShape, &mmXDeviceAddr, aclDataType::ACL_HIFLOAT8, &mmX);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    ret = CreateAclTensor(mmWHostData, mmWShape, &mmWDeviceAddr, aclDataType::ACL_HIFLOAT8, &mmW);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    ret = CreateAclTensor(mmXScaleHostData, mmXScaleShape, &mmXScaleDeviceAddr, aclDataType::ACL_FLOAT, &mmXScale);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    ret = CreateAclTensor(mmWScaleHostData, mmWScaleShape, &mmWScaleDeviceAddr, aclDataType::ACL_FLOAT, &mmWScale);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    ret = CreateAclTensor(mmYHostData, mmYShape, &mmYDeviceAddr, aclDataType::ACL_FLOAT16, &mmY);
    CHECK_RET(ret == ACL_SUCCESS, return ret);

    aclIntArray *sendCounts = aclCreateIntArray(sendCountsList.data(), sendCountsList.size());
    aclIntArray *recvCounts = aclCreateIntArray(recvCountsList.data(), recvCountsList.size());

    // Call the first-phase API.
    ret = aclnnQuantGroupedMatMulAlltoAllvGetWorkspaceSize(
        gmmX,
        gmmW,
        gmmXScale,
        gmmWScale,
        sendCountsTensor,
        recvCountsTensor,
        mmX,
        mmW,
        mmXScale,
        mmWScale,
        commQuantScaleOptional,
        gmmXQuantMode,
        gmmWQuantMode,
        mmXQuantMode,
        mmWQuantMode,
        commQuantMode,
        commQuantDtypeOptional,
        groupSize,
        hcomName,
        EP_WORLD_SIZE,
        sendCounts,
        recvCounts,
        false,
        false,
        y,
        mmY,
        &workspaceSize,
        &executor);
    CHECK_RET(
        ret == ACL_SUCCESS, LOG_PRINT("[ERROR] aclnnQuantGroupedMatMulAlltoAllvGetWorkspaceSize failed. ret = %d \n", ret);
        return ret);

    if (workspaceSize > 0) {
        ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("[ERROR] aclrtMalloc workspace failed. ret = %d \n", ret); return ret);
    }
    // Call the second-phase API.
    ret = aclnnQuantGroupedMatMulAlltoAllv(workspaceAddr, workspaceSize, executor, args.stream);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("[ERROR] aclnnQuantGroupedMatMulAlltoAllv failed. ret = %d \n", ret);
            return ret);
    //(Boilerplate) Wait for the task execution to complete.
    ret = aclrtSynchronizeStreamWithTimeout(args.stream, 10000000);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("[ERROR] aclrtSynchronizeStreamWithTimeout failed. ret = %d \n", ret);
            return ret);
    LOG_PRINT("[INFO] device_%d aclnnQuantGroupedMatMulAlltoAllv execute successfully.\n", args.rankId);
    // Release device resources. Modify this part according to the definition of the specific API.
    if (args.rankId == 0) {
        size_t size = A * N1 * sizeof(int16_t);
        aclrtMemcpy(pGmmYData.data(), size, yDeviceAddr, size, ACL_MEMCPY_DEVICE_TO_HOST);
    }
    if (gmmX != nullptr) {
        aclDestroyTensor(gmmX);
    }
    if (gmmW != nullptr) {
        aclDestroyTensor(gmmW);
    }
    if (gmmXScale != nullptr) {
        aclDestroyTensor(gmmXScale);
    }
    if (gmmWScale != nullptr) {
        aclDestroyTensor(gmmWScale);
    }
    if (y != nullptr) {
        aclDestroyTensor(y);
    }
    if (mmX != nullptr) {
        aclDestroyTensor(mmX);
    }
    if (mmW != nullptr) {
        aclDestroyTensor(mmW);
    }
    if (mmXScale != nullptr) {
        aclDestroyTensor(mmXScale);
    }
    if (mmWScale != nullptr) {
        aclDestroyTensor(mmWScale);
    }
    if (mmY != nullptr) {
        aclDestroyTensor(mmY);
    }
    if (gmmXDeviceAddr != nullptr) {
        aclrtFree(gmmXDeviceAddr);
    }
    if (gmmWDeviceAddr != nullptr) {
        aclrtFree(gmmWDeviceAddr);
    }
    if (gmmXScaleDeviceAddr != nullptr) {
        aclrtFree(gmmXScaleDeviceAddr);
    }
    if (gmmWScaleDeviceAddr != nullptr) {
        aclrtFree(gmmWScaleDeviceAddr);
    }
    if (yDeviceAddr != nullptr) {
        aclrtFree(yDeviceAddr);
    }
    if (mmXDeviceAddr != nullptr) {
        aclrtFree(mmXDeviceAddr);
    }
    if (mmWDeviceAddr != nullptr) {
        aclrtFree(mmWDeviceAddr);
    }
    if (mmXScaleDeviceAddr != nullptr) {
        aclrtFree(mmXScaleDeviceAddr);
    }
    if (mmWScaleDeviceAddr != nullptr) {
        aclrtFree(mmWScaleDeviceAddr);
    }
    if (mmYDeviceAddr != nullptr) {
        aclrtFree(mmYDeviceAddr);
    }
    if (workspaceSize > 0) {
        aclrtFree(workspaceAddr);
    }
    HcclCommDestroy(args.hcclComm);
    aclrtDestroyStream(args.stream);
    aclrtDestroyContext(args.context);
    aclrtResetDevice(args.rankId);
    return 0;
}

int main(int argc, char *argv[])
{
    int ret = aclInit(nullptr);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("[ERROR] aclInit failed. ret = %d \n", ret); return ret);
    aclrtStream stream[EP_WORLD_SIZE];
    aclrtContext context[EP_WORLD_SIZE];
    for (uint32_t rankId = 0; rankId < EP_WORLD_SIZE; rankId++) {
        ret = aclrtSetDevice(rankId);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("[ERROR] aclrtSetDevice failed. ret = %d \n", ret); return ret);
        ret = aclrtCreateContext(&context[rankId], rankId);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("[ERROR] aclrtCreateContext failed. ret = %d \n", ret); return ret);
        ret = aclrtCreateStream(&stream[rankId]);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("[ERROR] aclrtCreateStream failed. ret = %d \n", ret); return ret);
    }

    int32_t devices[EP_WORLD_SIZE];
    for (int i = 0; i < EP_WORLD_SIZE; i++) {
        devices[i] = i;
    }
    //Initialize the communicator.
    HcclComm comms[EP_WORLD_SIZE];
    ret = HcclCommInitAll(EP_WORLD_SIZE, devices, comms);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("[ERROR] HcclCommInitAll failed. ret = %d \n", ret); return ret);

    Args args[EP_WORLD_SIZE];
    // Start multiple threads.
    std::vector<std::unique_ptr<std::thread>> threads(EP_WORLD_SIZE);
    for (uint32_t rankId = 0; rankId < EP_WORLD_SIZE; rankId++) {
        args[rankId].rankId = rankId;
        args[rankId].hcclComm = comms[rankId];
        args[rankId].stream = stream[rankId];
        args[rankId].context = context[rankId];
        threads[rankId].reset(new std::thread(&LaunchOneThreadAlltoAllvGmm, std::ref(args[rankId])));
    }
    for (uint32_t rankId = 0; rankId < EP_WORLD_SIZE; rankId++) {
        threads[rankId]->join();
    }
    aclFinalize();
    return 0;
}