aclnnQuantMatmulAllReduceV5

📄 View Source Code

Applicable Products

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

Function

  • Description: Compatible with the functions supported by aclnnQuantMatmulAllReduce, aclnnQuantMatmulAllReduceV2, aclnnQuantMatmulAllReduceV3, and aclnnQuantMatmulAllReduceV4. Adds the communication engine parameter commMode to allow selection of a communication engine.
  • Formula: Same as aclnnQuantMatmulAllReduceV4. For details, see aclnnQuantMatmulAllReduceV4 Formula.

Function Prototype

Each operator uses a two-phase API. Call aclnnQuantMatmulAllReduceV5GetWorkspaceSize first to obtain the workspace size required for the computation and the executor that contains the computation process. Then call aclnnQuantMatmulAllReduceV5 to execute the computation.

aclnnStatus aclnnQuantMatmulAllReduceV5GetWorkspaceSize(
    const aclTensor *x1,
    const aclTensor *x2,
    const aclTensor *biasOptional,
    const aclTensor *x3Optional,
    const aclTensor *x1ScaleOptional,
    const aclTensor *x2ScaleOptional,
    const aclTensor *commQuantScale1Optional,
    const aclTensor *commQuantScale2Optional,
    const char      *group,
    const char      *reduceOp,
    const char      *commMode,
    int64_t          commTurn,
    int64_t          streamMode,
    int64_t          groupSize,
    int64_t          commQuantMode,
    const aclTensor *output,
    uint64_t        *workspaceSize,
    aclOpExecutor  **executor)
aclnnStatus aclnnQuantMatmulAllReduceV5(
    void              *workspace,
    uint64_t           workspaceSize,
    aclOpExecutor     *executor,
    const aclrtStream  stream)

aclnnQuantMatmulAllReduceV5GetWorkspaceSize

  • Parameters

    Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor
    x1 Input Left matrix of the MatMul computation, that is, x1 in the formula.
    • Only two-dimensional or three-dimensional input is supported in the current version.
    • The non-transposition use case is supported.
    INT8, FLOAT8_E4M3FN, FLOAT8_E5M2, HIFLOAT8, FLOAT4_E2M1 ND 2-3 ×
    x2 Input Right matrix of the MatMul computation, that is, x2 in the formula.
    • The Current Version supports only 2D input.
    • Supports the transpose/non-transpose use case.
    • In ND format, non-contiguous tensors with the last two axes transposed are supported; other non-contiguous tensors are not supported.
    INT8, FLOAT8_E4M3FN, FLOAT8_E5M2, HIFLOAT8, FLOAT4_E2M1. ND 2 ×
    biasOptional Input Bias offset, that is, biasOptional in the Formula.
    • The Current Version supports only 1D input.
    INT32, FLOAT32 ND 1 √
    x3Optional Input The add operation after the MatMul computation, which is x3Optional in the Formula.
    • In low-bit communication use cases, only BFLOAT16 output and non-empty input are supported. The data type and dimensions must be consistent with those of output.
    FLOAT16, BFLOAT16, FLOAT32 ND 2-3 √
    x1ScaleOptional Input pertoken dequantization coefficient after MatMul computation, that is, x1ScaleOptional in the Formula.
    • In the pertoken use case, the shape is (b*m) when x1 is (b, m, k), and (m) when x1 is (m, k).
    • In the perblock use case, the shape is [b, ceilDiv(m, 128), ceilDiv(k, 128)] when x1 is (b, m, k), and [ceilDiv(m, 128), ceilDiv(k, 128)] when x1 is (m, k).
    • When the data type is FLOAT8_E8M0, the shape is [m, ceilDiv(k, 64), 2]. When x1 is FLOAT4_E2M1, ceilDiv(k, 32) must be an even number.
    FLOAT32, FLOAT8_E8M0 ND 1-3 √
    x2ScaleOptional Input Dequantization coefficient after the MatMul computation, that is, x2Scale in the Formula.
    • Shape is (1) in the pertensor use case and (n)/(1, n) in the perchannel use case.
    • If the input is int8 and the output is BFLOAT16, directly pass x2ScaleOptional of the BFLOAT16 type to this API.
    • If the output is FLOAT16 and the input is INT8 and x1ScaleOptional is not empty, directly pass x2ScaleOptional of the FLOAT32 type to this API. If x1ScaleOptional is empty, call the aclnn API of the TransQuantParamV2 operator in advance to convert x2ScaleOptional to the INT64/UINT64 data type.
    • If the data type is FLOAT8_E8M0, only transposition is supported, and the shape is [n, ceilDiv(k, 64), 2].
    • If x2 is FLOAT4_E2M1, ensure that ceilDiv(k, 32) is an even number.
    • In the perblock use case, the shape of x2 is [ceilDiv(k, 128), ceilDiv(n, 128)]. If x2 is transposed, the shape of x2ScaleOptional is [ceilDiv(n, 128), ceilDiv(k, 128)].
    INT64, UINT64, FLOAT32, BFLOAT16, FLOAT8_E8M0 ND 1-3 √
    commQuantScale1Optional Input Perchannel quantization coefficient after the MatMul+Add computation, that is, commQuantScale1Optional in the Formula.
    • Supported only when the input is of the int8 type in the current version. For other use cases, pass in an empty value.
    • If x2 is (k, n), shape can be (n) or (1,n).
    BFLOAT16, FLOAT16 ND 1-2 √
    commQuantScale2Optional Input Perchannel quantization scale after AllGather computation, that is, commQuantScale2Optional in the Formula.
    • Supported only when the input is of the int8 type in the current version. For other use cases, pass in an empty value.
    • If x2 is (k, n), shape can be (n) or (1,n).
    BFLOAT16, FLOAT16 ND 1-2 √
    group Input Name of the communicator.
    • Obtained via the Hccl API "extern HcclResult HcclGetCommName(HcclComm comm, char* commName);", where commName is group.
    String - - -
    reduceOp Input Reduce operation type.
    • The current version supports only "sum".
    String - - -
    commMode Input Communication engine parameter. Specifies the communication engine. Input restrictions vary by chip model. For a brief introduction to the communication engine and its input constraints, see Constraints. String - - -
    commTurn Input Number of communication data splits, that is, total data volume / data volume per communication.
    • Only input 0 is supported in the current version.
    INT64 - - -
    streamMode Input Enumeration of the stream mode.
    • Only enum value 1 is supported in the current version.
    INT64 - - -
    groupSize Input Specifies the number of x1/x2 input values along a dimension that can be dequantized by one value of the x1Scale/x2Scale input along the corresponding dimension in dequantization.
    • The groupSize input consists of three values — groupSizeM, groupSizeN, and groupSizeK — corresponding to three dimensions. Each value occupies 16 bits. Formula: groupSize = groupSizeK | groupSizeN << 16 | groupSizeM << 32.
    • The perblock use case supports only groupSizeM, groupSizeN, groupSizeK = 128,128,128.
    • The MXFP use case supports only groupSizeM, groupSizeN, groupSizeK = 1,1,32.
    • Automatic parameter deduction is supported. If any parameter is 0, the operator automatically derives that parameter value. If all parameters are 0, the operator automatically derives all parameter values.
    INT64 - - -
    commQuantMode Input Flag for static and dynamic quantization.
    • The value is 0 or 1. 1 is supported only when x1 and x2 are FLOAT8_E4M3FN or FLOAT8_E5M2. When set to 1, the Pertile quantization Fp8 communication use case is applied.
    INT64 - - -
    Output Output Result of the MatMul computation and AllReduce communication, that is, the output in the Formula.
    • output has the same number of dimensions as x1.
    FLOAT16, BFLOAT16, FLOAT32 ND 2-3 √
    workspaceSize Output Returns the workspace size to be requested on the Device. - - - - -
    executor Output Returns the op executor, which contains the operator computation flow. - - - - -
  • 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 Either x1, x2, x2Scale, reduceOp, or output is a null pointer.
    ACLNN_ERR_PARAM_INVALID 161002 The data type of x1, x2, biasOptional, x1ScaleOptional, x2Scale, x3Optional, commQuantScale1Optional, commQuantScale2Optional, or output is not within the supported range.
    streamMode is not within the valid range.
    The shape of x1, x2, biasOptional, x1ScaleOptional, x2Scale, x3Optional, commQuantScale1Optional, commQuantScale2Optional, or output does not meet the constraint requirements.

aclnnQuantMatmulAllReduceV5

  • Parameters

    Parameter Input/Output Description
    workspace Input Workspace memory address allocated on the Device.
    workspaceSize Input Workspace size allocated on the Device, obtained by the first-phase API aclnnQuantMatmulAllReduceV5GetWorkspaceSize.
    executor Input Op executor that contains the operator computation process.
    stream Input Stream for executing tasks.
  • Return Value

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

Constraints

  • Communication Engine commMode support:

    • Atlas A2 products: Currently, specifying the communication engine is not supported. commMode only allows "ai_cpu", which uses the AICPU communication engine.
    • Ascend 950PR&950DT products: Currently, the supported communication engines are AICPU and CCU, and commMode allows "ai_cpu" or "ccu". CCU supports only UB-domain interconnection within a single machine, and AICPU supports UB-domain interconnection across machines.
    • Within the same communication link, only one type of communication engine can be selected.
    • A brief introduction to the AICPU and CCU communication engines:
      • AICPU: Does not occupy compute cores and provides high communication efficiency. However, it has high static communication overhead and is not suitable for small-data communication use cases. Suitable for large-data, high-bandwidth use cases.
      • CCU: Reduces memory access bandwidth and compute core usage, but is limited by on-chip resources and supports only a limited number of communicators. Suitable for high-bandwidth, low-latency communication use cases.
      • For more details about communication engines, see Communication Engine.
  • Deterministic computation:

    • Atlas A2 products: aclnnQuantMatmulAllReduceV5 defaults to a non-deterministic implementation. To enable deterministic computation, set the HCCL_DETERMINISTIC environment variable to true.
    • Ascend 950PR&950DT products: aclnnQuantMatmulAllReduceV5 defaults to a deterministic implementation.
  • MC2 is disabled in incremental scenarios and enabled in full scenarios.

  • The input x1 can be 2-dimensional or 3-dimensional, with its shape being (b, s, k) or (m, k). x2 must be 2-dimensional, with its shape being (k, n). The k axis must satisfy the mm operator input requirements, meaning the k axes must be equal.

  • The size of m must not exceed 2147483647. The last dimension size of x1 and x2 must not exceed 65535. The last dimension of x1 refers to k, and the last dimension of x2 refers to k when transposed or n when not transposed.

  • The passed x1, x2, x2Scale, or output must not be null pointers.

  • The data types and data formats of x1, x2, dequantScale, output, bias (when not null), and x3 (when not null) must fall within the supported ranges.

  • The passed commQuantScale1 and commQuantScale2 must be either both null pointers or both non-null pointers. If both commQuantScale1 and commQuantScale2 are non-null pointers, the shapes of the two quantization parameters must be consistent, their types must be consistent with the operator output type, and the inputs on each card must be consistent.

  • Only HCCS all-mesh networking is supported.

    • Atlas A2 products: supports 1, 2, 4, and 8 devices.
    • Ascend 950PR&950DT products: supports 1, 2, 4, 8, 16, 32, and 64 devices.
  • The general-purpose computation fused MC2 operators in a model support only the same communication domain.

  • INT8 and FP8 low-bit communication delivers performance gains only in communication-bound cases. Enabling INT8 or FP8 low-bit communication is not recommended in computation-bound cases, that is, do not input commQuantScale1 and commQuantScale2, and set commQuantMode to 0. (Note: INT8 low-bit communication means that the input is int8 with commQuantScale1Optional and commQuantScale2Optional enabled; FP8 low-bit communication means that the input is FLOAT8_E4M3FN/FLOAT8_E5M2 with commQuantMode set to 1.)

  • Null tensor support:

    • Empty tensors are not supported.
  • Constraints on groupSize:

    • The groupSize value is valid only when both x1Scale and x2Scale inputs are 2-dimensional or higher. In other scenarios, pass 0.
    • The input groupSize is internally decomposed into groupSizeM, groupSizeN, and groupSizeK using the following formula. When one or more of them are 0, groupSizeM, groupSizeN, and groupSizeK are reset based on the input shapes of x1, x2, x1Scale, and x2Scale for computation. Principle: If groupSizeM = 0, it indicates that the quantization group value in the m direction is inferred by the API. The inference formula is groupSizeM = m / scaleM (m must be divisible by scaleM), where m is consistent with the m in the x1 shape, and scaleM is consistent with the m in the x1Scale shape.
      $$ groupSize = groupSizeK | groupSizeN << 16 | groupSizeM << 32 $$

The input and output support the following data type combinations:

  • Atlas A2 products:
    x1 x2 biasOptional x3Optional x1ScaleOptional x2ScaleOptional commQuantScale1Optional commQuantScale2Optional output Restriction
    INT8 INT8 null, INT32 null, FLOAT16 null INT64, UINT64 null, FLOAT16 null, FLOAT16 FLOAT16 commQuantScale1Optional and commQuantScale2Optional are both empty or both non-empty.
    INT8 INT8 null, INT32 null, FLOAT16 FLOAT32 FLOAT32 null or FLOAT16 null or FLOAT16 FLOAT16 commQuantScale1Optional and commQuantScale2Optional are both null or both non-null.
    INT8 INT8 null or INT32 null or BFLOAT16 null, FLOAT32 BFLOAT16 null, BFLOAT16 null, BFLOAT16 BFLOAT16 commQuantScale1Optional and commQuantScale2Optional are both empty or both non-empty.
  • Ascend 950PR&950DT products:

    For int8 input, supports pertoken-perchannel quantization && pertensor-perchannel quantization.

    x1 x2 biasOptional x3Optional x1ScaleOptional x2ScaleOptional commQuantScale1Optional commQuantScale2Optional output Restriction
    INT8 INT8 null, INT32 null, FLOAT16 null INT64, UINT64 null, FLOAT16 null, FLOAT16 FLOAT16 commQuantScale1Optional and commQuantScale2Optional are both empty or both non-empty.
    INT8 INT8 null, INT32 null, FLOAT16 FLOAT32 FLOAT32 null, FLOAT16 null, FLOAT16 FLOAT16 commQuantScale1Optional and commQuantScale2Optional are both empty or both non-empty.
    INT8 INT8 null, INT32 null, BFLOAT16 null, FLOAT32 BFLOAT16 null, BFLOAT16 null, BFLOAT16 BFLOAT16 commQuantScale1Optional and commQuantScale2Optional are both empty or both non-empty.

    pertoken-perchannel quantization && pertoken-pertensor quantization && perblock-perblock quantization

    x1 x2 biasOptional x3Optional x1ScaleOptional x2ScaleOptional output Restriction
    FLOAT8_E4M3FN FLOAT8_E4M3FN null, FLOAT32 null, FLOAT16, BFLOAT16, FLOAT32 FLOAT32 FLOAT32 FLOAT16, BFLOAT16, FLOAT32 The B-B quantization scenario supports only null for biasOptional.
    FLOAT8_E5M2 FLOAT8_E5M2 null, FLOAT32 null, FLOAT16, BFLOAT16, FLOAT32 FLOAT32 FLOAT32 FLOAT16, BFLOAT16, FLOAT32 The B-B quantization scenario supports only null for biasOptional.
    HIFLOAT8 HIFLOAT8 null, FLOAT32 null, FLOAT16, BFLOAT16, FLOAT32 FLOAT32 FLOAT32 FLOAT16, BFLOAT16, FLOAT32 The B-B quantization scenario supports only null for biasOptional.

    perchannel quantization && pertensor quantization

    x1 x2 biasOptional x3Optional x1ScaleOptional x2ScaleOptional output Restriction
    FLOAT8_E4M3FN FLOAT8_E4M3FN null, FLOAT32 null, FLOAT16, BFLOAT16, FLOAT32 null UINT64 FLOAT16, BFLOAT16 -
    FLOAT8_E5M2 FLOAT8_E5M2 null, FLOAT32 null, FLOAT16, BFLOAT16, FLOAT32 null UINT64 FLOAT16, BFLOAT16 -
    HIFLOAT8 HIFLOAT8 FLOAT32 FLOAT16, BFLOAT16, FLOAT32 null UINT64 FLOAT16, BFLOAT16 -

    MXFP quantization

    x1 x2 biasOptional x3Optional x1ScaleOptional x2ScaleOptional output Restriction
    FLOAT4_E2M1 FLOAT4_E2M1 null, FLOAT32 null, FLOAT16, BFLOAT16, FLOAT32 FLOAT8_E8M0 FLOAT8_E8M0 FLOAT16, BFLOAT16, FLOAT32 -
    FLOAT8_E4M3FN FLOAT8_E4M3FN null, FLOAT32 null, FLOAT16, BFLOAT16, FLOAT32 FLOAT8_E8M0 FLOAT8_E8M0 FLOAT16, BFLOAT16, FLOAT32 -
    FLOAT8_E5M2 FLOAT8_E5M2 null, FLOAT32 null, FLOAT16, BFLOAT16, FLOAT32 FLOAT8_E8M0 FLOAT8_E8M0 FLOAT16, BFLOAT16, FLOAT32 -

Example

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

Note: This sample code calls some HCCL collective communication library APIs: HcclGetCommName, HcclCommInitAll, and HcclCommDestroy. See HCCL API (C).

  • Atlas A2 products, Ascend 950PR&950DT products:

    #include <iostream>
    #include <vector>
    #include <thread>
    #include "hccl/hccl.h"
    #include "aclnn/opdev/fp16_t.h"
    #include "aclnnop/aclnn_trans_matmul_weight.h"
    #include "aclnnop/aclnn_quant_matmul_all_reduce_v5.h"
    
    #define ACL_CHECK(ret)                                                                                     \
        do {                                                                                                   \
            auto retcode = ret;                                                                                \
            if (retcode != ACL_SUCCESS) {                                                                      \
                printf("[ERROR] acl interface return err %s:%d, retcode: %d \n", __FILE__, __LINE__, retcode); \
                return retcode;                                                                                \
            }                                                                                                  \
        } while (0)
    
    #define CHECK_RET(cond, return_expr) \
        do {                             \
            if (!(cond)) {               \
                return_expr;             \
            }                            \
        } while (0)
    
    #define LOG_PRINT(message, ...)         \
        do {                                \
            printf(message, ##__VA_ARGS__); \
        } while (0)
    
    constexpr int DEV_NUM = 2;
    
    int64_t GetShapeSize(const std::vector<int64_t> &shape)
    {
        int64_t shape_size = 1;
        for (auto i : shape) {
            shape_size *= i;
        }
        return shape_size;
    }
    
    struct Args {
        uint32_t rankId;
        HcclComm hcclComm;
        aclrtStream stream;
        aclrtContext context;
        std::string format;
    };
    
    template<typename T>
    int CreateWeightNzAclTensor(const std::vector<T> &hostData, const std::vector<int64_t> &shape, void **deviceAddr,
                                aclDataType dataType, aclTensor **tensor, Args &args) {
        auto size = GetShapeSize(shape) * sizeof(T);
        const aclIntArray *mat2Size = aclCreateIntArray(shape.data(), shape.size());
        auto ret = aclnnCalculateMatmulWeightSizeV2(mat2Size, ACL_INT8, &size);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnCalculateMatmulWeightSizeV2 failed. ERROR: %d\n", ret); return ret);
    
        // Call aclrtMalloc to allocate device memory.
        ret = aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMalloc failed. ERROR: %d\n", ret); return ret);
    
        // Call aclrtMemcpy to copy host-side data to device-side memory.
        ret = aclrtMemcpy(*deviceAddr, size, hostData.data(), size, ACL_MEMCPY_HOST_TO_DEVICE);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMemcpy failed. ERROR: %d\n", ret); return ret);
    
        // Calculate the strides of the contiguous tensor.
        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];
        }
        // Call aclCreateTensor to create an aclTensor.
        *tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND,
                                  shape.data(), shape.size(), *deviceAddr);
    
        uint64_t transWorkspaceSize;
        aclOpExecutor *executor;
        void *transWorkspaceAddr = nullptr;
        ret = aclnnTransMatmulWeightGetWorkspaceSize(*tensor, &transWorkspaceSize, &executor);
        CHECK_RET(ret == ACL_SUCCESS && transWorkspaceSize > 0,
                  printf("[ERROR] aclnnTransMatmulWeightGetWorkspaceSize failed. ret = %d \n", ret); return ret);
        ACL_CHECK(aclrtMalloc(&transWorkspaceAddr, transWorkspaceSize, ACL_MEM_MALLOC_HUGE_FIRST));
        ret = aclnnTransMatmulWeight(transWorkspaceAddr, transWorkspaceSize, executor, args.stream);
        CHECK_RET(ret == ACL_SUCCESS, printf("[ERROR] aclnnTransMatmulWeight failed. ret = %d \n", ret);return ret);
        ACL_CHECK(aclrtSynchronizeStreamWithTimeout(args.stream, 20000000));
    
        return 0;
    }
    
    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);
        // Call aclrtMalloc to allocate device-side memory.
        auto ret = aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMalloc failed. ERROR: %d\n", ret); return ret);
        // Call aclrtMemcpy to copy host-side data to device memory.
        ret = aclrtMemcpy(*deviceAddr, size, hostData.data(), size, ACL_MEMCPY_HOST_TO_DEVICE);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMemcpy failed. ERROR: %d\n", ret); return ret);
        // Compute the strides of the contiguous tensor.
        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];
        }
        // Call aclCreateTensor to create an aclTensor.
        *tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND,
                                  shape.data(), shape.size(), *deviceAddr);
        return 0;
    }
    
    int launchOneThreadQuantMatmulAllReduce(Args &args) {
        int ret;
        ret = aclrtSetCurrentContext(args.context);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSetCurrentContext failed. ERROR: %d\n", ret); return ret);
        char hcom_name[128];
        ret = HcclGetCommName(args.hcclComm, hcom_name);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("[ERROR] HcclGetCommName failed. ret = %d \n", ret); return -1);
        LOG_PRINT("[INFO] rank %d hcom: %s stream: %p, context : %p\n", args.rankId, hcom_name, args.stream,
                  args.context);
    
        std::vector<int64_t> x1Shape = {32, 64};
        std::vector<int64_t> x2Shape = {64, 128};
        std::vector<int64_t> biasShape = {128};
        std::vector<int64_t> dequantScaleShape = {128};
        std::vector<int64_t> x1ScaleShape = {32};
        std::vector<int64_t> commQuantScale1Shape = {128};
        std::vector<int64_t> commQuantScale2Shape = {128};
        std::vector<int64_t> x3Shape = {32, 128};
        std::vector<int64_t> outShape = {32, 128};
        void *x1DeviceAddr = nullptr;
        void *x2DeviceAddr = nullptr;
        void *biasDeviceAddr = nullptr;
        void *dequantScaleDeviceAddr = nullptr;
        void *x1ScaleDeviceAddr = nullptr;
        void *commQuantScale1DeviceAddr = nullptr;
        void *commQuantScale2DeviceAddr = nullptr;
        void *x3DeviceAddr = nullptr;
        void *outDeviceAddr = nullptr;
        aclTensor *x1 = nullptr;
        aclTensor *x2 = nullptr;
        aclTensor *bias = nullptr;
        aclTensor *x2ScaleOptional = nullptr;
        aclTensor *x1ScaleOptional = nullptr;
        aclTensor *commQuantScale1 = nullptr;
        aclTensor *commQuantScale2 = nullptr;
        aclTensor *x3Optional = nullptr;
        aclTensor *out = nullptr;
    
        int64_t commTurn = 0;
        int64_t streamMode = 1;
        uint64_t workspaceSize = 0;
        aclOpExecutor *executor;
        void *workspace = nullptr;
    
        long long x1ShapeSize = GetShapeSize(x1Shape);
        long long x2ShapeSize = GetShapeSize(x2Shape);
        long long biasShapeSize = GetShapeSize(biasShape);
        long long dequantScaleShapeSize = GetShapeSize(dequantScaleShape);
        long long x1ScaleShapeSize = GetShapeSize(x1ScaleShape);
        long long commQuantScale1ShapeSize = GetShapeSize(commQuantScale1Shape);
        long long commQuantScale2ShapeSize = GetShapeSize(commQuantScale2Shape);
        long long x3ShapeSize = GetShapeSize(x3Shape);
        long long outShapeSize = GetShapeSize(outShape);
    
        std::vector<int8_t> x1HostData(x1ShapeSize, 1);
        std::vector<int8_t> x2HostData(x2ShapeSize, 1);
        std::vector<int32_t> biasHostData(biasShapeSize, 1);
        std::vector<float> dequantScaleHostData(dequantScaleShapeSize, 1);
        std::vector<float> x1ScaleHostData(x1ScaleShapeSize, 1);
        std::vector<op::fp16_t> commQuantScale1HostData(commQuantScale1ShapeSize, 1);
        std::vector<op::fp16_t> commQuantScale2HostData(commQuantScale2ShapeSize, 1);
        std::vector<op::fp16_t> x3HostData(x3ShapeSize, 1);
        std::vector<op::fp16_t> outHostData(outShapeSize, 0);
        // Create a tensor.
        ret = CreateAclTensor(x1HostData, x1Shape, &x1DeviceAddr, aclDataType::ACL_INT8, &x1);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
        if (args.format == "NZ") {
            ret = CreateWeightNzAclTensor(x2HostData, x2Shape, &x2DeviceAddr, aclDataType::ACL_INT8, &x2, args);
        } else {
            ret = CreateAclTensor(x2HostData, x2Shape, &x2DeviceAddr, aclDataType::ACL_INT8, &x2);
        }
        CHECK_RET(ret == ACL_SUCCESS, return ret);
        ret = CreateAclTensor(biasHostData, biasShape, &biasDeviceAddr, aclDataType::ACL_INT32, &bias);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
        ret = CreateAclTensor(dequantScaleHostData, dequantScaleShape, &dequantScaleDeviceAddr,
                              aclDataType::ACL_FLOAT, &x2ScaleOptional);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
        ret = CreateAclTensor(x1ScaleHostData, x1ScaleShape, &x1ScaleDeviceAddr,
                              aclDataType::ACL_FLOAT, &x1ScaleOptional);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
        ret = CreateAclTensor(commQuantScale1HostData, commQuantScale1Shape, &commQuantScale1DeviceAddr,
                              aclDataType::ACL_FLOAT16, &commQuantScale1);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
        ret = CreateAclTensor(commQuantScale2HostData, commQuantScale2Shape, &commQuantScale2DeviceAddr,
                              aclDataType::ACL_FLOAT16, &commQuantScale2);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
        ret = CreateAclTensor(x3HostData, x3Shape, &x3DeviceAddr, aclDataType::ACL_FLOAT16, &x3Optional);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
        ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_FLOAT16, &out);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
        // Call the first-phase API.
        ret = aclnnQuantMatmulAllReduceV5GetWorkspaceSize(x1, x2, bias, x3Optional, x1ScaleOptional, x2ScaleOptional,
                                                          commQuantScale1, commQuantScale2, hcom_name,
                                                          "sum", "ai_cpu", commTurn, streamMode, 0, 0, out,
                                                          &workspaceSize, &executor);
        CHECK_RET(ret == ACL_SUCCESS,
                  LOG_PRINT("aclnnQuantMatmulAllReduceV5GetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
        // Allocate device memory based on the workspaceSize computed by the first-phase API.
        if (workspaceSize > 0) {
            ret = aclrtMalloc(&workspace, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST);
            CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret);
        }
        // Call the second-phase API.
        ret = aclnnQuantMatmulAllReduceV5(workspace, workspaceSize, executor, args.stream);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnQuantMatmulAllReduceV5 failed. ERROR: %d\n", ret); return ret);
        // (Boilerplate) Synchronize and wait for the task execution to complete.
        ret = aclrtSynchronizeStreamWithTimeout(args.stream, 10000);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);
        LOG_PRINT("device%d aclnnQuantMatmulAllReduceV5 execute success \n", args.rankId);
        // Release device resources. Modify according to the specific API definition.
        if (x1 != nullptr) {
            aclDestroyTensor(x1);
        }
        if (x2 != nullptr) {
            aclDestroyTensor(x2);
        }
        if (bias != nullptr) {
            aclDestroyTensor(bias);
        }
        if (x2ScaleOptional != nullptr) {
            aclDestroyTensor(x2ScaleOptional);
        }
        if (x1ScaleOptional != nullptr) {
            aclDestroyTensor(x1ScaleOptional);
        }
        if (commQuantScale1 != nullptr) {
            aclDestroyTensor(commQuantScale1);
        }
        if (commQuantScale2 != nullptr) {
            aclDestroyTensor(commQuantScale2);
        }
        if (x3Optional != nullptr) {
            aclDestroyTensor(x3Optional);
        }
        if (out != nullptr) {
            aclDestroyTensor(out);
        }
        if (x1DeviceAddr != nullptr) {
            aclrtFree(x1DeviceAddr);
        }
        if (x2DeviceAddr != nullptr) {
            aclrtFree(x2DeviceAddr);
        }
        if (biasDeviceAddr != nullptr) {
            aclrtFree(biasDeviceAddr);
        }
        if (dequantScaleDeviceAddr != nullptr) {
            aclrtFree(dequantScaleDeviceAddr);
        }
        if (x1ScaleDeviceAddr != nullptr) {
            aclrtFree(x1ScaleDeviceAddr);
        }
        if (commQuantScale1DeviceAddr != nullptr) {
            aclrtFree(commQuantScale1DeviceAddr);
        }
        if (commQuantScale2DeviceAddr != nullptr) {
            aclrtFree(commQuantScale2DeviceAddr);
        }
        if (x3DeviceAddr != nullptr) {
            aclrtFree(x3DeviceAddr);
        }
        if (outDeviceAddr != nullptr) {
            aclrtFree(outDeviceAddr);
        }
        if (workspaceSize > 0) {
            aclrtFree(workspace);
        }
        aclrtDestroyStream(args.stream);
        HcclCommDestroy(args.hcclComm);
        aclrtDestroyContext(args.context);
        aclrtResetDevice(args.rankId);
        return 0;
    }
    
    int main(int argc, char *argv[])
    {
        int ret;
        int32_t devices[DEV_NUM];
        for (int i = 0; i < DEV_NUM; i++) {
            devices[i] = i;
        }
        HcclComm comms[128];
        ret = aclInit(nullptr);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclInit failed. ERROR: %d\n", ret); return ret);
        // Initialize the communicator.
        for (int i = 0; i < DEV_NUM; i++) {
            ret = aclrtSetDevice(devices[i]);
            CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSetDevice failed. ERROR: %d\n", ret); return ret);
        }
        ret = HcclCommInitAll(DEV_NUM, devices, comms);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("HcclCommInitAll failed. ERROR: %d\n", ret); return ret);
        Args args[DEV_NUM];
        aclrtStream stream[DEV_NUM];
        aclrtContext context[DEV_NUM];
        for (uint32_t rankId = 0; rankId < DEV_NUM; rankId++) {
            ret = aclrtSetDevice(rankId);
            CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSetDevice failed. ERROR: %d\n", ret); return ret);
            ret = aclrtCreateContext(&context[rankId], rankId);
            CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtCreateContext failed. ERROR: %d\n", ret); return ret);
            ret = aclrtCreateStream(&stream[rankId]);
            CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtCreateStream failed. ERROR: %d\n", ret); return ret);
        }
        // Start multithreading.
        std::vector<std::unique_ptr<std::thread>> threads(DEV_NUM);
        for (uint32_t rankId = 0; rankId < DEV_NUM; 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::nothrow) std::thread(&launchOneThreadQuantMatmulAllReduce, std::ref(args[rankId])));
        }
        for (uint32_t rankId = 0; rankId < DEV_NUM; rankId++) {
            threads[rankId]->join();
        }
        aclFinalize();
        return 0;
    }