aclnnQuantMatmulV5

📄 View Source Code

Applicable Products

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

Function

  • Description: Performs quantized matrix multiplication computation.

    • Atlas A2 products and Atlas A3 products:

      This API is compatible with aclnnQuantMatmulV3 and aclnnQuantMatmulV4 and performs quantized matrix multiplication computation. x1 supports 2-6 dimensions, and x2 supports 2-6 dimensions in ND format and 4-8 dimensions in NZ format. For the dimension limits in specific quantization scenarios, see Constraints. The similar API is aclnnMm (matrix multiplication that supports only 2D tensors as input).

    • Ascend 950PR&950DT products:

      This API is compatible with aclnnQuantMatmulV3 and aclnnQuantMatmulV4 and adds support for new features such as the G-B, B-B, T-CG, and MX quantization modes. The x1 and x2 inputs additionally support data types such as FLOAT8_E4M3FN, FLOAT8_E5M2, HIFLOAT8, and FLOAT4_E2M1. It performs matrix multiplication computation upon completing quantization, supporting a minimum input dimension of 2 and a maximum of 6. Similar APIs include aclnnMm (which supports only matrix multiplication with 2D tensors as inputs) and aclnnBatchMatMul (which supports only 3D matrix multiplication, where the first dimension is the Batch dimension).

  • Formula:

    • Atlas A2 products, Atlas A3 products:

      Supports the K-C && K-T, T-C && T-T, G-B, and K-G quantization modes. For the input and output data type combinations corresponding to different quantization modes, see Constraints.

      K-G quantization mode
      • x1 is INT8, x2 is INT32, x1Scale is FLOAT32, x2Scale is UINT64/INT64, and yOffset is FLOAT32:

        out=((x1@(x2∗x2Scale))+yOffset)∗x1Scaleout = ((x1 @ (x2*x2Scale)) + yOffset) * x1Scale
      • x1 and x2 are INT4, x1Scale and x2Scale are FLOAT32, x2Offset is FLOAT16, and out is FLOAT16/BFLOAT16 (pertoken-pergroup asymmetric quantization):

        out=x1Scale∗x2Scale∗(x1@x2−x1@x2Offset)out = x1Scale * x2Scale * (x1 @ x2 - x1 @ x2Offset)
      K-C && K-T quantization mode
      • x1Scale, no bias:

        out=x1@x2∗x2Scale∗x1Scaleout = x1@x2 * x2Scale * x1Scale
      • x1Scale, bias INT32 (no offset in this use case):

        out=(x1@x2+bias)∗x2Scale∗x1Scaleout = (x1@x2 + bias) * x2Scale * x1Scale
      • x1Scale and bias BFLOAT16/FLOAT16/FLOAT32 (no offset in this use case):

        out=x1@x2∗x2Scale∗x1Scale+biasout = x1@x2 * x2Scale * x1Scale + bias
      T-C && T-T quantization mode
      • Without x1Scale and bias:

        out=x1@x2∗x2Scale+x2Offsetout = x1@x2 * x2Scale + x2Offset
      • bias INT32:

        out=(x1@x2+bias)∗x2Scale+x2Offsetout = (x1@x2 + bias) * x2Scale + x2Offset
      • bias BFLOAT16/FLOAT32 (no offset in this scenario):

        out=x1@x2∗x2Scale+biasout = x1@x2 * x2Scale + bias
      G-B quantization mode
      • x1, x2 are INT8, x1Scale, x2Scale are FLOAT32, bias is FLOAT32, out is FLOAT16/BFLOAT16 (pergroup-perblock quantization):out=(x1@x2)∗x1Scale∗x2Scale+biasout = (x1 @ x2) * x1Scale * x2Scale + bias
    • Atlas inference products:

      Supports the K-C quantization mode. For input and output data type combinations corresponding to different quantization modes, see Constraints.

      K-C Quantization Mode
      • x1Scale, no bias:

        out=x1@x2∗x2Scale∗x1Scaleout = x1@x2 * x2Scale * x1Scale
      • x1Scale, bias INT32 (no offset in this use case):

        out=(x1@x2+bias)∗x2Scale∗x1Scaleout = (x1@x2 + bias) * x2Scale * x1Scale
    • Ascend 950PR&950DT products:

      Supports the T-C && T-T, K-C && K-T, G-B, B-B, MX, T-CG, and K-G quantization modes. For input and output data type combinations corresponding to different quantization modes, see Constraints.

      K-G quantization mode
      • x1 and x2 are INT4, x1Scale and x2Scale are FLOAT32, x2Offset is FLOAT16, and out is FLOAT16/BFLOAT16 (pertoken-pergroup asymmetric quantization):out=x1Scale∗x2Scale∗(x1@x2−x1@x2Offset)out = x1Scale * x2Scale * (x1 @ x2 - x1 @ x2Offset)
      T-C && T-T quantization mode
      • x1 and x2 are INT8, no x1Scale, x2Scale is INT64/UINT64, optional parameter x2Offset is FLOAT32, and optional parameter bias is INT32:

        out=(x1@x2+bias)∗x2Scale+x2Offsetout = (x1@x2 + bias) * x2Scale + x2Offset
      • x1 and x2 are INT8, without x1Scale, x2Scale is INT64/UINT64, and the optional parameter bias is INT32; Or x1 and x2 are of data type FLOAT8_E4M3FN/FLOAT8_E5M2/HIFLOAT8, x1Scale is absent, x2Scale is of data type INT64/UINT64, and the optional parameter bias is of data type FLOAT32; Or x1 and x2 are of data type INT4, x1Scale is absent, x2Scale is of data type INT64/UINT64, and the optional parameter bias is of data type INT32:

        out=(x1@x2+bias)∗x2Scaleout = (x1@x2 + bias) * x2Scale
      • x1 and x2 are INT8, without x1Scale, x2Scale is BFLOAT16/FLOAT32, and the optional parameter bias is BFLOAT16/FLOAT32:

        out=x1@x2∗x2Scale+biasout = x1@x2 * x2Scale + bias
      • x1 and x2 are FLOAT8_E4M3FN/FLOAT8_E5M2/HIFLOAT8, x1Scale is FLOAT32, x2Scale is FLOAT32, and the optional parameter bias is FLOAT32:

        out=x1@x2∗x2Scale∗x1Scale+biasout = x1@x2 * x2Scale * x1Scale + bias
      K-C && K-T quantization mode
      • x1 and x2 are INT8, x1Scale is FLOAT32, x2Scale is BFLOAT16/FLOAT32, and the optional parameter bias is INT32; Or x1 and x2 are INT4, x1Scale is FLOAT32, x2Scale is BFLOAT16/FLOAT32, and the optional parameter bias is INT32:

        out=(x1@x2+bias)∗x2Scale∗x1Scaleout = (x1@x2 + bias) * x2Scale * x1Scale
      • x1 and x2 are INT8, x1Scale is FLOAT32, x2Scale is BFLOAT16/FLOAT32, and the optional parameter bias is BFLOAT16/FLOAT32; Or x1 and x2 are INT8, x1Scale is FLOAT32, x2Scale is FLOAT32, and the optional parameter bias is FLOAT16/FLOAT32; Or x1 and x2 are INT4, x1Scale is FLOAT32, x2Scale is BFLOAT16/FLOAT32, and the optional parameter bias is BFLOAT16/FLOAT32; Or x1 and x2 are INT4, x1Scale is FLOAT32, x2Scale is FLOAT32, and the optional parameter bias is FLOAT16/FLOAT32; Or x1 and x2 are FLOAT8_E4M3FN/FLOAT8_E5M2/HIFLOAT8, x1Scale is FLOAT32, x2Scale is FLOAT32, and the optional parameter bias is FLOAT32:

        out=x1@x2∗x2Scale∗x1Scale+biasout = x1@x2 * x2Scale * x1Scale + bias
      G-B && B-B && MX quantization mode out[m,n]=∑j=0kLoops−1((∑k=0gsK−1(x1Slice∗x2Slice))∗(x1Scale[m/gsM,j]∗x2Scale[j,n/gsN]))+bias[n]out[m,n] = \sum_{j=0}^{kLoops-1} ((\sum_{k=0}^{gsK-1} (x1Slice * x2Slice))* (x1Scale[m/gsM, j] * x2Scale[j, n/gsN]))+bias[n]

      where gsM, gsN, and gsK denote groupSizeM, groupSizeN, and groupSizeK, respectively; x1Slice denotes the vector of length groupSizeK in row m of x1, and x2Slice denotes the vector of length groupSizeK in column n of x2; both are sliced starting at j*groupSizeK along the K axis, where j is in the range [0, kLoops), kLoops = ceil(K / groupSizeK), and K is the length of the K axis; the last slice can be shorter than groupSizeK. bias is included only in the MX quantization mode. For the G-B, B-B, and MX quantization modes, the supported value combinations of [groupSizeM, groupSizeN, groupSizeK] are only [1, 128, 128], [128, 128, 128], and [1, 1, 32], respectively.

      T-CG quantization mode out=(x1@(x2∗x2Scale))∗yScaleout = (x1@(x2 * x2Scale)) * yScale

Function Prototype

Each operator uses a two-phase API. You must first call the "aclnnQuantMatmulV5GetWorkspaceSize" API to obtain the workspace size required for the calculation and the executor that contains the operator calculation flow, and then call the "aclnnQuantMatmulV5" API to perform the calculation.

aclnnStatus aclnnQuantMatmulV5GetWorkspaceSize(
  const aclTensor *x1,
  const aclTensor *x2,
  const aclTensor *x1Scale,
  const aclTensor *x2Scale,
  const aclTensor *yScale,
  const aclTensor *x1Offset,
  const aclTensor *x2Offset,
  const aclTensor *yOffset,
  const aclTensor *bias,
  bool             transposeX1,
  bool             transposeX2,
  int64_t          groupSize,
  aclTensor       *out,
  uint64_t        *workspaceSize,
  aclOpExecutor   **executor)
aclnnStatus aclnnQuantMatmulV5(
  void          *workspace,
  uint64_t       workspaceSize,
  aclOpExecutor *executor,
  aclrtStream    stream)

aclnnQuantMatmulV5GetWorkspaceSize

  • Parameters

    Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor
    x1(aclTensor*) Input Input x1 in the formula. INT4, INT8, INT321, FLOAT8_E4M3FN2, FLOAT8_E5M22, HIFLOAT82, FLOAT4_E2M12 ND 2-6 ✓
    x2(aclTensor*) Input Input x2 in the formula.
    • For the empty tensor support status, see Empty Tensor Support Status.
    • In NZ format, shapes with 4-8 dimensions are supported.
    • In ND format, non-contiguous tensors with the last two axes transposed are supported, while other non-contiguous tensors are not supported.
    INT4, INT8, INT321, FLOAT8_E4M3FN2, FLOAT8_E5M22, HIFLOAT82, FLOAT4_E2M12 ND, NZ 2-6 (ND)
    4-8 (NZ)
    ✓
    x1Scale(aclTensor*) Optional input The input x1Scale in the formula.
    • Empty tensor not supported.
    FLOAT32, FLOAT8_E8M02 ND 1-7 ✓
    x2Scale(aclTensor*) Input Represents the quantization parameter, the input x2Scale in the formula.
    • Empty tensors are not supported.
    UINT64, INT64, FLOAT32, BFLOAT16, FLOAT8_E8M02, FLOAT162 ND 1-7 ✓
    yScale(aclTensor*) Optional input Dequantization scale parameter for output y.
    • Empty tensors are not supported.
    • If this parameter is not used, pass in nullptr.
    UINT642, INT642 ND 2 ×
    x1Offset(aclTensor*) Optional input Input x1Offset in the formula.
    • Reserved parameter, not supported in the current version. Need to pass in nullptr.
    - - - -
    x2Offset(aclTensor*) Optional input Input x2Offset in the formula.
    • Empty tensors are not supported.
    FLOAT16, FLOAT32 ND 1-2 -
    yOffset(aclTensor*) Optional input Input yOffset in the formula. - FLOAT32 ND 1 -
    bias(aclTensor*) Optional input Input bias in the formula.
    • Empty tensors are not supported.
    INT32, FLOAT32, BFLOAT16, FLOAT16 ND 1-3 ×
    transposeX1(bool) Input Indicates whether the input shape of x1 is transposed. - BOOL - - -
    transposeX2(bool) Input Indicates whether the input shape of x2 is transposed. - BOOL - - -
    groupSize(int64_t) Optional input Specifies the quantization group sizes along the m, n, and k dimensions. Composed of groupSizeM, groupSizeN, and groupSizeK in three directions. Each value occupies 16 bits, taking up the low 48 bits of the int64_t type groupSize (the value of the high 16 bits in groupSize is invalid). For the formula, see below the table. INT64 - - -
    out(aclTensor*) Output Output out in the formula. FLOAT16, INT8, BFLOAT16, INT32, FLOAT32 ND 2-6. For details, see Constraints. -
    workspaceSize(uint64_t) Output Returns the size of the workspace to be applied on the Device side. - - - - -
    executor(aclOpExecutor*) Output Returns the op executor, which contains the calculation sub-computation flow. - - - - -
    • Note: Optional inputs refer to optional quantization parameters, which support passing nullptr.
    • Ascend 950PR&950DT products :

      • The superscript "1" in the data type column of the table above indicates a data type not supported by this series.
      • The input parameters x1 and x2 do not support the INT32 type.
      • x2 supports only the ND format.
    • Atlas A2 products and Atlas A3 products:

      • The superscript "2" in the data type column of the table above indicates a data type not supported by this series.
    • Formula: groupSize=groupSizeK∣groupSizeN<<16∣groupSizeM<<32groupSize = groupSizeK | groupSizeN << 16 | groupSizeM << 32
  • 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 x1, x2, x2Scale, or out is a null pointer.
    ACLNN_ERR_PARAM_INVALID 161002 x1, x2, or out does not meet the constraints in Empty Tensor Support Status, or unsupported empty tensors are passed in for bias, x1Scale, x2Scale, and x2Offset.
    The data type and data format of x1, x2, bias, x1Scale, x2Scale, x2Offset, or out are not within the supported ranges.
    The shape of x1, x2, bias, x1Scale, x2Scale, x2Offset, or out does not meet the verification conditions.
    The passed groupSize does not meet the verification conditions, or when the passed groupSize is 0, groupSize cannot be inferred from the shape relationships of x1, x2, x1Scale, and x2Scale.

aclnnQuantMatmulV5

  • Parameters

    Parameter Input/Output Description
    workspace Input The workspace memory address applied on the Device side.
    workspaceSize Input The workspace size applied on the Device side, obtained by the first-phase API aclnnQuantMatmulV5GetWorkspaceSize.
    executor Input Op executor, which contains the operator computation flow.
    stream Input Stream used to execute tasks.
  • Return Value

    aclnnStatus: return status code, see details in aclnn return code.

Constraints

  • Deterministic computation:
    • aclnnQuantMatmulV5 defaults to a deterministic implementation.
Atlas inference products
  • General Constraints:

    • In the current version, yScale, x1Offset, x2Offset, and yOffset are not supported. Pass nullptr for these parameters.
    K-C quantization scenario constraints:
    • The input and output support the following data type combinations:

| x1 | x2 | x1Scale | x2Scale | x1Offset | x2Offset | yScale | yOffset | Bias | Out | | -----------| ------------ | ----------- | ----------- | ----------- |----------- | ------- | -----------| ------------ | -------------------| | INT8 | INT8 | FLOAT32 | FLOAT32 | null | null | null | null | null/INT32 | FLOAT16 |

  • x1 constraints:
    • The last dimension of x1 cannot exceed 65535, and transposeX1 supports only false.
  • Constraints on x2:
    • The last dimension size of x2 cannot exceed 65535, and transposeX2 only supports true.
    • Each dimension represents: (batch, k1, n1, n0, k0), where batch may not exist, k0 = 32, and n0 = 16. k in the x1 shape and k1 in the x2 shape must satisfy the following relationship: ceil(k / 32) = k1.
    • x2 in ND format needs to be processed by aclnnTransMatmulWeight to obtain the AI processor affinity data layout format.
  • x1Scale constraint: The data format supports ND. The shape is one-dimensional (t,), where t = m and m is the same as the m of x1.
  • x2Scale constraint: The data format supports ND. The shape is one-dimensional (t,), where t = n and n is the same as the n of x2.
  • bias constraint: The data format supports ND. The shape can be one-dimensional (n,) or three-dimensional (batch, 1, n), where n is the same as the n of x2.
Atlas A2 products, Atlas A3 products
  • General Constraints:

    • When transposeX1 is false, the dimensions of X1 are (batch, m, k). When transposeX1 is true, the dimensions of X1 are (batch, k, m), where batch may not exist.
    • The constraints on transposeX2 are as follows:
      • In ND format, when it is false, the dimensions of X2 are (batch, k, n); when it is true, the dimensions are (batch, n, k). batch may not exist, where k is the same as k in the x1 shape.
      • In NZ format:
        • When it is true, the dimensions of X2 are (batch, k1, n1, n0, k0). batch may not exist, where k0 = 32, n0 = 16, and k in the x1 shape and k1 in the x2 shape must satisfy the following relationship: ceil(k / 32) = k1.
        • When it is false, the dimensions of X2 are (batch, n1, k1, k0, n0). batch may not exist, where k0 = 16, n0 = 32, and k in the x1 shape and k1 in the x2 shape must satisfy the following relationship: ceil(k / 16) = k1.
        • The aclnnCalculateMatmulWeightSizeV2 API and aclnnTransMatmulWeight API can be used to convert the input format from ND to the NZ format.
    • When the original input type of x2Scale does not meet the combination in the quantization scenario constraints, call the aclnnTransQuantParamV2 API in advance to convert the scale to INT64 or UINT64 data type.
    • yScale is not supported in the current version. Pass nullptr.
    • The shape of out supports 2-6 dimensions: (batch, m, n), where batch may not exist. The supported data types are FLOAT16, INT8, BFLOAT16, and INT32.
    • When x1 and x2 are INT8, out is INT32, and bias is INT32 or nullptr, each scale does not actually participate in the computation. The formula is as follows:
      • bias INT32

        out=x1@x2+biasout = x1@x2 + bias
      • Without bias

        out=x1@x2out = x1@x2
    G-B quantization scenario constraints:
    • The input and output support the following data type combinations:

| x1 | x2 | x1Scale | x2Scale | x2Offset | yScale | Bias | yOffset | Out | | ------------------------- | ------------------------- | ----------- | ----------- | ----------- | ------- | ------------ | -----------| -------------------------------------- | | INT8 | INT8 | FLOAT32 | FLOAT32 | null | null | FLOAT32 | null | BFLOAT16 |

  • Relationship between x1 shape, x2 shape, x1Scale shape, x2Scale shape, bias shape, and groupSize:

| Quantization Type | x1 Shape | x2 Shape | x1Scale Shape | x2Scale Shape | Bias Shape | [gsM, gsN, gsK] | | ----- | ------ | ------ | ----------- | ----------- | ----------- | ----------- | | G-B quantization | (m, k) |(n, k)|(m, ceil(k / 128))|(ceil(n / 128),ceil(k / 128))| (n, ) | [1, 128, 128]|

  • Note: In the table above, gsM, gsK, and gsN represent groupSizeM, groupSizeK, and groupSizeN, respectively.
  • Constraints on x1: Currently, k must be aligned to 128 and be a multiple of 4 * 128, and transposeX1 must be false.
  • Constraints on x2: Currently, n must be aligned to 256, k must be aligned to 128 and be a multiple of 4 * 128, and transposeX2 must be true.
T-C && T-T && K-C && K-T quantization scenario constraints:
  • The input and output support the following data type combinations:

| x1 | x2 | x1Scale | x2Scale | x2Offset | yScale | Bias | yOffset | Out | | ------------------------- | ------------------------- | ----------- | ----------- | ----------- | ------- | ------------ | -----------| -------------------------------------- | | INT8 | INT32 | FLOAT32 | UINT64/INT64 | null | null | null | FLOAT32 | FLOAT16/BFLOAT16 | | INT8 | INT8 | null | UINT64/INT64 | null | null | null/INT32 | null | FLOAT16 | | INT8 | INT8 | null | UINT64/INT64 | null/FLOAT32| null | null/INT32 | null | INT8 | | INT8 | INT8 | null/FLOAT32| FLOAT32/BFLOAT16| null | null | null/INT32/BFLOAT16/FLOAT32 | null | BFLOAT16 | | INT8 | INT8 | FLOAT32 | FLOAT32 | null | null | null/INT32/FLOAT16/FLOAT32 | null | FLOAT16 | | INT4/INT32 | INT4/INT32 | null | UINT64/INT64 | null | null | null/INT32 | null | FLOAT16 | | INT8 | INT8 | null | FLOAT32/BFLOAT16| null | null | null/INT32 | null | INT32 | | INT8 | INT8 | FLOAT32 | FLOAT32 | null | null | FLOAT32 | null | BFLOAT16 | | INT4/INT32 | INT4/INT32 | FLOAT32 | FLOAT32/BFLOAT16| null | null | null/INT32/BFLOAT16/FLOAT32 | null | BFLOAT16 | | INT4/INT32 | INT4/INT32 | FLOAT32 | FLOAT32 | null | null | null/INT32/FLOAT16/FLOAT32 | null | FLOAT16 |

  • x1 constraints:
    • When the data type is INT4, transposeX1 must be false. The dimension is (m, k), and k must be an even number.
    • When the data type is INT32, transposeX1 must be false. Each INT32 value stores 8 INT4 values, and the dimension is (m, ceil(k / 8)). k must be a multiple of 8.
    • When the data type is INT8 and the data type of x2 is INT32, transposeX1 must be false. The dimension is (m, k), and k must be an even number.
  • Constraints on x2:
    • When the data type is INT4:
      • Currently, only 2D ND format is supported.
      • When transposeX2 is true, the dimension is (n, k), and k must be an even number.
      • When transposeX2 is false, the dimension is (k, n), and n must be an even number.
    • When the data type is INT32, each INT32 value stores eight INT4 values.
      • Currently, only 2D ND format is supported.
      • When transposeX2 is true, the dimension is (n, ceil(k / 8)), and k must be a multiple of 8.
      • When transposeX2 is false, the dimension is (k, ceil(n / 8)), and n must be a multiple of 8.
      • You can use the aclnnConvertWeightToINT4Pack API to complete the data format conversion of x2 from INT32 (1 INT4 stored in bits 0-3 of 1 INT32) to INT32 (8 INT4s stored in 1 INT32) or INT4 (1 INT4 represented by 1 INT4). For details, see aclnnConvertWeightToINT4Pack API.
  • Constraints on x1Scale: The data format is ND. The shape is 1D (t,), where t = m, and m is the same as m of x1.
  • Constraints on x2Scale: The data format is ND. The shape is 1D (t,), where t = 1 or n, and n is the same as n of x2. When the x1 data type is INT8 and the x2 data type is INT32, t can only be n.
  • Constraints on x2Offset: The data format is ND. The shape is 1D (t,), where t = 1 or n, and n is the same as n of x2.
  • bias constraint:
    • The data format is ND. The shape is 1D (n,) or 3D (batch, 1, n), where n is the same as n of x2.
    • When x1 and x2 are INT32 or INT4, the shape of bias can only be 1D (n,).
    • When the shape of out is 2D, 4D, 5D, or 6D, the shape of bias can only be 1D (n,).
  • Constraints on yOffset: The shape is 1D (n). It is an auxiliary result computed offline during the calculation process, and the value must be 8*x2*x2Scale and is accumulated along the first dimension.
K-G quantization scenario constraints:
  • The input and output support the following data type combinations:

| x1 | x2 | x1Scale | x2Scale | x2Offset | yScale | Bias | yOffset | Out | | ------------------------- | ------------------------- | ----------- | ----------- | ----------- | ------- | ------------ | -----------| -------------------------------------- | | INT8 | INT32 | FLOAT32 | UINT64/INT64 | null | null | null | FLOAT32 | FLOAT16/BFLOAT16 | | INT4 | INT4 | FLOAT32 | FLOAT32 | FLOAT16 | null | null | null | BFLOAT16 |

  • Relationship between x1, x2, x1Scale, x2Scale, and groupSize values:

| Quantization Type | x1 Data Type | x2 Data Type | x1Scale Data Type | x2Scale Data Type | x1 Shape | x2 Shape | x1Scale Shape | x2Scale Shape | x2Offset Shape | yOffset Shape | [gsM, gsN, gsK] | | ----- | ------------------------- | ------------------------- | -------------- | ------------- | -------- | ------- | ------------ | ------ |------------ | ------------ | ------------ | | K-G quantization | INT8 |INT32 |FLOAT32 |UINT64/INT64 |(m, k) |(k, ceil(n / 8))|(m, 1)|(ceil(k / 256), n)|null| (n) | [0, 0, 256]| | K-G quantization | INT4 |INT4 |FLOAT32 |FLOAT32 |(m, k)|(n, k)|(m, 1)|(ceil(k / 256), n)|(ceil(k / 256), n)| null | [0, 0, 256]|

  • x1 constraints:
    • When the data type is INT8, k must be aligned to 256 and less than 29576. transposeX1 is false.
    • When the data type is INT4, k must be aligned to 1024. transposeX1 is false.
  • Constraints on x2:
    • When the data type is INT32, k must be aligned to 256. transposeX2 is false.
    • When the data type is INT4, k must be aligned to 1024, and n must be aligned to 256. transposeX2 must be set to true.
  • Constraints on x2Scale:
    • When the data type is UINT64/INT64, since TransQuantParamV2 supports only 1D, view x2Scale as 1D (k / groupSize * n), call the aclnn API of the TransQuantParamV2 operator to convert x2Scale to the UINT64/INT64 data type, and then view the output as 2D (k / groupSize, n). groupSize is 256.
    • When x1 and x2 are INT4, the shape of x2Scale is (ceil(k / 256), n).
Ascend 950PR&950DT products
  • General Constraints:

    • int4 input is supported only when transposeX1 is false.

    • When transposeX1 is false, the shape of x1 is (batch, m, k). When transposeX1 is true, the shape of x1 is (batch, k, m). Here, batch represents the first 0 to 4 dimensions, where 0 dimension indicates that batch does not exist.

    • When transposeX2 is false, the shape of x2 is (batch, k, n). When transposeX2 is true, the shape of x2 is (batch, n, k). Here, batch represents the first 0 to 4 dimensions, and 0 dimension means batch does not exist. k is consistent with k in the shape of x1.

    • When the original input type of x2Scale does not meet the combination in the quantization scenario constraints, call the aclnnTransQuantParamV2 API in advance to convert the scale to INT64 or UINT64 data type.

    • yScale is supported only when x1 is FLOAT8_E4M3FN and x2 is FLOAT4_E2M1. It is not supported in other scenarios. The shape is 2D (1, n), where n is consistent with n of x2.

    • x2Offset is supported only when the data types of x1 and x2 are INT8 and the data type of out is INT8. For other input types, nullptr must be passed. The shape is 1D (t, ), where t = 1 or n, and n is consistent with n of x2.

    • yOffset is a reserved parameter and is not supported in the current version. Pass nullptr or an empty tensor.

    • bias-related constraints:

      • Optional parameter. Supports passing nullptr.
      • When the shape of out is 2-dimensional, the shape of bias supports 1-dimensional (n,) or 2-dimensional (1, n).
      • When the shape of out is 4-, 5-, or 6-dimensional, the shape of bias supports 1-dimensional (n,).
      • When the shape of out is 3 dimensions, the shape of bias supports 1D (n,) or 3D (batch, 1, n).
    • Constraints on groupSize:

      • Takes effect only in the MX, G-B, B-B, K-G, and T-CG quantization modes.
      • 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<<32groupSize = groupSizeK | groupSizeN << 16 | groupSizeM << 32
    • The shape of out supports 2 to 6 dimensions, (batch, m, n). batch may be absent. Broadcasting of the batch dimensions of x1 and x2 is supported. The output batch is consistent with the batch after broadcasting, m is consistent with the m of x1, and n is consistent with the n of x2.

    • When x1 and x2 are INT8, out is INT32, and bias is INT32 or nullptr, each scale does not actually participate in the computation. The formula is as follows:

      • bias INT32

        out=x1@x2+biasout = x1@x2 + bias
      • Without bias

        out=x1@x2out = x1@x2
    Constraints for T-C quantization && T-T quantization use cases:
    • The input and output support the following data type combinations:

| x1 | x2 | x1Scale | x2Scale | x2Offset | yScale | Bias | Out | | ------------------------- | ------------------------- | ----------- | ----------- | -------- | -------| ------- | -------------------------------------- | | INT8 | INT8 | null | UINT64/INT64 | null | null | null/INT32 | FLOAT16/BFLOAT16 | | INT8 | INT8 | null | UINT64/INT64 | null/FLOAT32 | null | null/INT32 | INT8 | | INT8 | INT8 | null | FLOAT32/BFLOAT16 | null | null | null/INT32/FLOAT32/BFLOAT16 | BFLOAT16 | | INT8 | INT8 | null | FLOAT32/BFLOAT16 | null | null | null/INT32 | INT32 | | FLOAT8_E4M3FN/FLOAT8_E5M2 | FLOAT8_E4M3FN/FLOAT8_E5M2 | null | UINT64/INT64 | null | null | null/FLOAT32 | FLOAT16/BFLOAT16/FLOAT32 | | HIFLOAT8 | HIFLOAT8 | null | UINT64/INT64 | null | null | null/FLOAT32 | FLOAT16/BFLOAT16/FLOAT32 | | FLOAT8_E4M3FN/FLOAT8_E5M2 | FLOAT8_E4M3FN/FLOAT8_E5M2 | FLOAT32 | FLOAT32 | null | null | null/FLOAT32 | FLOAT16/BFLOAT16/FLOAT32 | | HIFLOAT8 | HIFLOAT8 | FLOAT32 | FLOAT32 | null | null | null/FLOAT32 | FLOAT16/BFLOAT16/FLOAT32 | | INT4/INT32 | INT4/INT32 | null | UINT64/INT64 | null | null | null/INT32 | FLOAT16 |

  • x1 constraints:
    • When the data type is INT4, transposeX1 is false. The dimension is (m, k), and k must be even.
    • When the data type is INT32, transposeX1 is false. Each INT32 value stores 8 INT4 values. The corresponding dimension is (m, ceil(k / 8)), and k must be a multiple of 8.
    • When the data type is INT8 and the x2 data type is INT32, transposeX1 is false. The dimension is (m, k), and k must be even.
  • Constraints on x2:
    • When the data type is INT4:
      • Currently, only 2D ND format is supported.
      • When transposeX2 is true, the dimension is (n, k), and k must be an even number.
      • When transposeX2 is false, the dimension is (k, n), and n must be an even number.
    • When the data type is INT32, each INT32 value stores eight INT4 values.
      • Currently, only 2D ND format is supported.
      • When transposeX2 is true, the dimension is (n, ceil(k / 8)), and k must be a multiple of 8.
      • When transposeX2 is false, the dimension is (k, ceil(n / 8)), and n must be a multiple of 8.
  • In the T-T quantization scenario, the shape of x1Scale is (1,) or nullptr, and the shape of x2Scale is (1,).
  • In the T-C quantization scenario, the shape of x1Scale is (1,) or nullptr, and the shape of x2Scale is (n,), where n is consistent with the n of x2.
  • When the data type of x1/x2 is FLOAT8_E4M3FN/FLOAT8_E5M2/HIFLOAT8, static quantization and dynamic quantization are distinguished. For static quantization, the x2Scale data type is UINT64/INT64; for dynamic quantization, the x2Scale data type is FLOAT32. When the x1/x2 data type is INT8, INT4, or INT32, dynamic T-C or dynamic T-T quantization is not supported.
  • In static quantization use cases, when x1/x2 is INT4 or INT32, x1 supports 2-6 dimensions and x2 supports only 2 dimensions.
  • Batch consistency: To satisfy batch consistency in the T-T quantization and T-C quantization scenarios, when comparing inputs with different M values, if x1Scale is not nullptr, the input value of x1Scale must not dynamically change with the input value of x1 and must remain unchanged.
Constraints on K-C quantization and K-T quantization scenarios:
  • The input and output support the following data type combinations:

| x1 | x2 | x1Scale | x2Scale | x2Offset | yScale | Bias | Out | | ------------------------- | ------------------------- | ----------- | ----------- | -------- | -------| ------- | -------------------------------------- | | INT8 | INT8 | FLOAT32| FLOAT32/BFLOAT16 | null | null | null/INT32/FLOAT32/BFLOAT16 | BFLOAT16 | | INT8 | INT8 | FLOAT32 | FLOAT32 | null | null | null/INT32/FLOAT32/FLOAT16 | FLOAT16 | | INT4 | INT4 | FLOAT32| FLOAT32/BFLOAT16 | null | null | null/INT32/FLOAT32/BFLOAT16 | BFLOAT16 | | INT4 | INT4 | FLOAT32 | FLOAT32 | null | null | null/INT32/FLOAT32/FLOAT16 | FLOAT16 | | FLOAT8_E4M3FN/FLOAT8_E5M2 | FLOAT8_E4M3FN/FLOAT8_E5M2 | FLOAT32 | FLOAT32 | null | null | null/FLOAT32 | FLOAT16/BFLOAT16/FLOAT32 | | HIFLOAT8 | HIFLOAT8 | FLOAT32 | FLOAT32 | null | null | null/FLOAT32 | FLOAT16/BFLOAT16/FLOAT32 |

- In the K-C quantization scenario, the shape of x1Scale is (m,) and the shape of x2Scale is (n,), where m is the same as the m of x1, and n is the same as the n of x2.
- In the K-T quantization scenario, the shape of x1Scale is (m,), and the shape of x2Scale is (1,), where m matches the m of x1.
Constraints on G-B quantization and B-B quantization scenarios:
  • The input and output support the following data type combinations:

| x1 | x2 | x1Scale | x2Scale | x2Offset | yScale | Bias | Out | | ------------------------- | ------------------------- | ----------- | ----------- | -------- | -------| ------- | -------------------------------------- | | FLOAT8_E4M3FN/FLOAT8_E5M2 | FLOAT8_E4M3FN/FLOAT8_E5M2 | FLOAT32 | FLOAT32 | null | null | null | FLOAT16/BFLOAT16/ FLOAT32 | | HIFLOAT8 | HIFLOAT8 | FLOAT32 | FLOAT32 | null | null | null | FLOAT16/BFLOAT16/ FLOAT32 | | INT8 |INT8 | FLOAT32 | FLOAT32 | null | null | FLOAT32 |BFLOAT16 |

  • Relationship between x1, x2, x1Scale, x2Scale, and groupSize values:

| Quantization Type | x1 Shape | x2 Shape | x1Scale Shape | x2Scale Shape | yScale Shape | [gsM, gsN, gsK] | groupSize | |-------|--------|--------|-------------|-------------|------------|---|---| |B-B quantization|

  • Non-transposed: (batch, m, k)
  • Transposed: (batch, k, m)
  • |
  • Non-transposed: (batch, k, n)
  • Transposed: (batch, n, k)
  • |
  • Non-transposed: (batch, ceil(m / 128), ceil(k / 128))
  • Transposed: (batch, ceil(k / 128), ceil(m / 128))
  • |
  • Non-transposed: (batch, ceil(k / 128), ceil(n / 128))
  • Transposed: (batch, ceil(n / 128), ceil(k / 128))
  • |null|[128, 128, 128]|549764202624| |G-B quantization|
  • Non-transposed: (batch, m, k)
  • Transposed: (batch, k, m)
  • |
  • Non-transposed: (batch, k, n)
  • Transposed: (batch, n, k)
  • |
  • Non-transposed: (batch, m, ceil(k / 128))
  • Transposed: (batch, ceil(k / 128), m)
  • |
  • Non-transposed: (batch, ceil(k / 128), ceil(n / 128))
  • Transposed: (batch, ceil(n / 128), ceil(k / 128))
  • |null|[1, 128, 128]|4303356032|

    • Note: In the table above, gsM, gsK, and gsN denote groupSizeM, groupSizeK, and groupSizeN respectively. Dimensions with gsM, gsK, or gsN set to 0 are automatically inferred. The table above lists the case where automatic inference is not used.
    • In G-B and B-B quantization scenarios, the transpose attributes of x1 and x1Scale must be the same, and the transpose attributes of x2 and x2Scale must be the same (when a shape axis contains 1 and it is not a dynamic graph NZ scenario, the transpose attributes of x and scale can be different).
    • In G-B quantization scenarios, bias is supported only for INT8 input. Bias is not supported in other scenarios.
    • In B-B quantization scenarios, INT8 input is not supported, and bias is not supported.
    • Batch consistency: The B-B quantization scenario does not support batch consistency. Even if the batch consistency switch is enabled, the output cannot be guaranteed to meet batch consistency requirements.
    MX quantization use case constraints:
    • The input and output support the following data type combinations:

    | Quantization Type | x1 | x2 | x1Scale | x2Scale | x2Offset | yScale | Bias | Out | |-------| ------------------------- | ------------------------- | ----------- | ----------- | -------- | -------| ------- | -------------------------------------- | |MX full quantization| FLOAT8_E4M3FN/FLOAT8_E5M2 | FLOAT8_E4M3FN/FLOAT8_E5M2 | FLOAT8_E8M0 | FLOAT8_E8M0 | null | null | null/FLOAT32 | FLOAT16/BFLOAT16/FLOAT32 | |MX full quantization| FLOAT4_E2M1 | FLOAT4_E2M1 | FLOAT8_E8M0 | FLOAT8_E8M0 | null | null | null/FLOAT32 | FLOAT16/BFLOAT16/FLOAT32 | |MX pseudo quantization| FLOAT8_E4M3FN | FLOAT4_E2M1 | FLOAT8_E8M0 | FLOAT8_E8M0 | null | null | null/BFLOAT16/FLOAT16 | BFLOAT16/FLOAT16 |

    • Relationship among the x1 data type, x2 data type, x1, x2, x1Scale, x2Scale, and groupSize:

    | Quantization Type | x1 Data Type | x2 Data Type | x1 Shape | x2 Shape | x1Scale Shape | x2Scale Shape | Bias Shape | yScale Shape | [gsM, gsN, gsK] | groupSize | |-------|--------|--------|--------|--------|-------------|-------------|------------|---------------------------------------|--|--| |MX full quantization|FLOAT8_E4M3FN/FLOAT8_E5M2|FLOAT8_E4M3FN/FLOAT8_E5M2|

  • Non-transposed: (batch, m, k)
  • Transposed: (batch, k, m)
  • |
  • Non-transposed: (batch, k, n)
  • Transposed: (batch, n, k)
  • |
  • Non-transposed: (batch, m, ceil(k / 64), 2)
  • Transposed: (batch, ceil(k / 64), m, 2)
  • |
  • non-transpose: (batch, ceil(k / 64), n, 2)
  • transpose: (batch, n, ceil(k / 64), 2)
  • |(n,) or (batch, 1, n)|null|[1, 1, 32]|4295032864| |MX full quantization|FLOAT4_E2M1|FLOAT4_E2M1|
  • Non-transposed: (batch, m, k)
  • Transposed: (batch, k, m)
  • |
  • Non-transposed: (batch, k, n)
  • Transposed: (batch, n, k)
  • |
  • Non-transposed: (batch, m, ceil(k / 64), 2)
  • Transposed: (batch, ceil(k / 64), m, 2)
  • |
  • non-transpose: (batch, ceil(k / 64), n, 2)
  • transpose: (batch, n, ceil(k / 64), 2)
  • |(n,) or (batch, 1, n)|null|[1, 1, 32]|4295032864| |MX pseudo quantization|FLOAT8_E4M3FN|FLOAT4_E2M1|(m, k)|(n, k)|(m, ceil(k / 64), 2)|(n, ceil(k / 64), 2)|(1, n)|null|[0, 0, 32]/[1, 1, 32]|32/4295032864|

    • Note: In the table above, gsM, gsK, and gsN denote groupSizeM, groupSizeK, and groupSizeN respectively. Dimensions with gsM, gsK, or gsN set to 0 are automatically inferred. The table above lists the case where automatic inference is not used.
    • In MX full quantization scenarios, when the x2 data type is FLOAT8_E4M3FN/FLOAT8_E5M2, the transpose attributes of x1 and x1Scale must be the same, and the transpose attributes of x2 and x2Scale must be the same (when a shape axis contains 1 and it is not a dynamic graph NZ scenario, the transpose attributes of x and scale can be different).
    • In the MX full quantization scenario, when the x2 data type is FLOAT4_E2M1, the inner axis of x1 and x2 must be even, and k must be greater than 2.
    • In MX full quantization scenarios, only the last three axes of x1Scale and x2Scale support non-contiguous tensors.
    • In MX pseudo quantization scenarios, when the x2 data type is FLOAT4_E2M1, the batch axis is not supported, and k must be a multiple of 8.
      • Constraints on x1: Only non-transpose is supported. Only the ND data format is supported.
      • Constraints on x2: only transpose is supported. The data format supports ND and NZ. If x2 is in NZ format, n must be a multiple of 8.
    • In MX pseudo quantization scenarios, bias is an optional parameter. The data type can be BFLOAT16 or FLOAT16 and must be the same as the output type. The supported data format is ND. The supported shape is 2-dimensional, in the format of (1, n). Pass nullptr if this parameter is not required.
    T-CG quantization scenario constraints:
    • The input and output support the following data type combinations:

    | x1 | x2 | x1Scale | x2Scale | x2Offset | yScale | Bias | Out | | ------------------------- | ------------------------- | ----------- | ----------- | -------- | -------| ------- | -------------------------------------- | | FLOAT8_E4M3FN | FLOAT4_E2M1 | null | BFLOAT16/FLOAT16 | null | INT64/UINT64 | null | BFLOAT16/FLOAT16 |

    • Relationship between x1, x2, x1Scale, x2Scale, and groupSize values:

    | Quantization Type | x1 Shape | x2 Shape | x1Scale Shape | x2Scale Shape | yScale Shape | [gsM, gsN, gsK] | groupSize | |-------|--------|--------|-------------|-------------|------------|---------------------------------------|--| |T-CG quantization|(m, k)|(n, k)|null|(n, ceil(k / 32))|(1, n)|[0, 0, 32]/[1, 1, 32]|32/4295032864|

    • In T-CG quantization mode, the yScale data type supports INT64 and UINT64, the data format supports ND, and the shape supports 2 dimensions, expressed as (1, n). When the original input type does not meet the data type combinations in the constraints and restrictions, call the aclnn API of the TransQuantParamV2 operator in advance to convert it to the UINT64 data type. When the input data type is INT64, INT64 is internally treated as UINT64.
    • In T-CG quantization mode, bias is a reserved parameter and is not supported in the current version. Pass nullptr.
    • In T-CG quantization mode, k must be a multiple of 32.
      • Constraints on x1: Only non-transpose is supported. Only the ND data format is supported.
      • Constraints on x2: Only transpose is supported when x2 is in ND format; only non-transpose is supported when x2 is in NZ format.
    • In T-CG quantization mode, the supported value combinations of [groupSizeM, groupSizeN, groupSizeK] are [0, 0, 32] and [1, 1, 32], and the corresponding groupSize values are 32 and 4295032864 respectively.
    • In T-CG quantization mode, out and x2Scale must have the same data type.
    K-G quantization scenario constraints:
    • The input and output support the following data type combinations:

    | x1 | x2 | x1Scale | x2Scale | x2Offset | yScale | Bias | yOffset | Out | | ------------------------- | ------------------------- | ----------- | ----------- | ----------- | ------- | ------------ | -----------| -------------------------------------- | | INT4 | INT4 | FLOAT32 | FLOAT32 | FLOAT16 | null | null | null | BFLOAT16/FLOAT16 |

    • Relationship between x1, x2, x1Scale, x2Scale, and groupSize values:

    | Quantization Type | x1 Data Type | x2 Data Type | x1Scale Data Type | x2Scale Data Type | x1 Shape | x2 Shape | x1Scale Shape | x2Scale Shape | x2offset | yOffset Shape | [gsM, gsN, gsK] | | ----- | ------------------------- | ------------------------- | -------------- | ------------- | -------- | ------- | ------------ | ------------ | ------------| ------------ | ------------ | | K-G quantization | INT4 |INT4 |FLOAT32 |FLOAT32 |(m, k)|(n, k)|(m, 1)|(ceil(k / 256), n)|(ceil(k / 256), n)| null | [0, 0, 256]|

    • x1 constraints:
      • When the data type is INT4, k must be aligned to 1024. transposeX1 is false.
    • Constraints on x2:
      • When the data type is INT4, k must be aligned to 1024, and n must be aligned to 256. transposeX2 must be true.
    • Constraints on x2Scale:
      • When x1 and x2 are INT4, the shape of x2Scale is (ceil(k / 256), n).

    Empty tensor support status

    The following descriptions apply to x1, x2, and out:

    • Ascend 950PR&950DT products: In the full quantization use case, x1 with m=0 or x2 with n=0 is supported, in which case out is an empty tensor. Other empty tensor use cases are not supported.
    • Other products: Empty tensors are not supported.

    For other parameters, the empty tensor restrictions are subject to the descriptions of each parameter and the corresponding quantization use case. Passing nullptr to an optional parameter indicates that the parameter is not used, which is different from passing an empty tensor.

    Example

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

    • Ascend 950PR&950DT products:

    x1 is FLOAT8_E4M3FN, x2 is FLOAT4_E2M1, x2Scale is BFLOAT16, and yScale is UINT64.

    #include <iostream>
    #include <memory>
    #include <vector>
    
    #include "acl/acl.h"
    #include "aclnnop/aclnn_quant_matmul_v5.h"
    #include "aclnnop/aclnn_trans_quant_param_v2.h"
    
    #define CHECK_RET(cond, return_expr) \
        do {                             \
            if (!(cond)) {               \
                return_expr;             \
            }                            \
        } while (0)
    
    #define CHECK_FREE_RET(cond, return_expr) \
        do {                                  \
            if (!(cond)) {                    \
                Finalize(deviceId, stream);   \
                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 shapeSize = 1;
        for (auto i : shape) {
            shapeSize *= i;
        }
        return shapeSize;
    }
    
    int Init(int32_t deviceId, aclrtStream* stream)
    {
        // Boilerplate: resource initialization.
        auto ret = aclInit(nullptr);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclInit failed. ERROR: %d\n", ret); return ret);
        ret = aclrtSetDevice(deviceId);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSetDevice failed. ERROR: %d\n", ret); return ret);
        ret = aclrtCreateStream(stream);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtCreateStream failed. ERROR: %d\n", ret); return ret);
        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 memory on the device.
        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 data from the host to the device.
        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 the aclCreateTensor API 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;
    }
    
    void Finalize(int32_t deviceId, aclrtStream stream)
    {
        aclrtDestroyStream(stream);
        aclrtResetDevice(deviceId);
        aclFinalize();
    }
    
    // Convert the uint16_t representation of bfloat16 to the float representation.
    float Bf16ToFloat(uint16_t h)
    {
        uint32_t sign = (h & 0x8000U) ? 0x80000000U : 0x00000000U; // sign bit
        uint32_t exponent = (h >> 7) & 0x00FFU;                    // exponent bits
        uint32_t mantissa = h & 0x007FU;                           // mantissa bits
        // The exponent offset remains unchanged.
        // Shift the mantissa left by 23 - 7 and fill the remaining bits with 0s.
        uint32_t fBits = sign | (exponent << 23) | (mantissa << (23 - 7));
        // Cast to float.
        return *reinterpret_cast<float*>(&fBits);
    }
    
    int AclnnQuantMatmulV5Test(int32_t deviceId, aclrtStream& stream)
    {
        auto ret = Init(deviceId, &stream);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
    
        // 2. Construct the inputs and outputs. Customize the construction based on the API.
        std::vector<int64_t> x1Shape = {5, 64};
        std::vector<int64_t> x2Shape = {8, 64};
        std::vector<int64_t> x2ScaleShape = {8, 2};
        std::vector<int64_t> yScaleShape = {1, 8};
        std::vector<int64_t> outShape = {5, 8};
        void* x1DeviceAddr = nullptr;
        void* x2DeviceAddr = nullptr;
        void* x2ScaleDeviceAddr = nullptr;
        void* yScaleDeviceAddr = nullptr;
        void* quantParamDeviceAddr = nullptr;
        void* outDeviceAddr = nullptr;
        aclTensor* x1 = nullptr;
        aclTensor* x2 = nullptr;
        aclTensor* x2Scale = nullptr;
        aclTensor* yScale = nullptr;
        aclTensor* yOffset = nullptr;
        aclTensor* quantParam = nullptr;
        aclTensor* bias = nullptr;
        aclTensor* out = nullptr;
        std::vector<uint8_t> x1HostData(5 * 64, 0b00111000);                 // 0b00111000 is 1.0 in fp8_e4m3fn.
        std::vector<uint8_t> x2HostData(8 * 64 / 2, 0b0010 + (0b0010 << 4)); // 0b0010 is 1.0 in fp4_e2m1, where a uint8 represents 2 fp4 values.
        std::vector<uint16_t> x2ScaleHostData(8 * 2, 0b0011111110000000); // 1.0 in bf16.
        std::vector<float> yScaleHostData(8, 1.0);                        // 1.0 in fp32
        std::vector<uint64_t> quantParamHostData(8, 0);
        std::vector<uint16_t> outHostData(40, 0); // actually bfloat16
        // Create the x1 aclTensor.
        ret = CreateAclTensor(x1HostData, x1Shape, &x1DeviceAddr, aclDataType::ACL_FLOAT8_E4M3FN, &x1);
        std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor*)> x1TensorPtr(x1, aclDestroyTensor);
        std::unique_ptr<void, aclError (*)(void*)> x1DeviceAddrPtr(x1DeviceAddr, aclrtFree);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
        // Create the x2 aclTensor.
        ret = CreateAclTensor(x2HostData, x2Shape, &x2DeviceAddr, aclDataType::ACL_FLOAT4_E2M1, &x2);
        std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor*)> x2TensorPtr(x2, aclDestroyTensor);
        std::unique_ptr<void, aclError (*)(void*)> x2DeviceAddrPtr(x2DeviceAddr, aclrtFree);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
        // Create the x2Scale aclTensor.
        ret = CreateAclTensor(x2ScaleHostData, x2ScaleShape, &x2ScaleDeviceAddr, aclDataType::ACL_BF16, &x2Scale);
        std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor*)> x2ScaleTensorPtr(x2Scale, aclDestroyTensor);
        std::unique_ptr<void, aclError (*)(void*)> x2ScaleDeviceAddrPtr(x2ScaleDeviceAddr, aclrtFree);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
        // Create the yScale aclTensor.
        ret = CreateAclTensor(yScaleHostData, yScaleShape, &yScaleDeviceAddr, aclDataType::ACL_FLOAT, &yScale);
        std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor*)> yScaleTensorPtr(yScale, aclDestroyTensor);
        std::unique_ptr<void, aclError (*)(void*)> yScaleDeviceAddrPtr(yScaleDeviceAddr, aclrtFree);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
        // Create the quantParam aclTensor.
        ret = CreateAclTensor(quantParamHostData, yScaleShape, &quantParamDeviceAddr, aclDataType::ACL_UINT64, &quantParam);
        std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor*)> quantParamTensorPtr(quantParam, aclDestroyTensor);
        std::unique_ptr<void, aclError (*)(void*)> quantParamDeviceAddrPtr(quantParamDeviceAddr, aclrtFree);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
        // Create the out aclTensor.
        ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_BF16, &out);
        std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor*)> outTensorPtr(out, aclDestroyTensor);
        std::unique_ptr<void, aclError (*)(void*)> outDeviceAddrPtr(outDeviceAddr, aclrtFree);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
        bool transposeX1 = false;
        bool transposeX2 = true;
        int64_t groupSize = 32;
    
        // 3. Call the CANN operator library API. Replace it with the actual API name.
        uint64_t workspaceSize = 0;
        aclOpExecutor* executor = nullptr;
    
        // Call the first-phase API of aclnnTransQuantParamV2.
        ret = aclnnTransQuantParamV2GetWorkspaceSize(yScale, yOffset, quantParam, &workspaceSize, &executor);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnTransQuantParamV2GetWorkspaceSize failed. ERROR: %d\n", ret);
                  return ret);
        // Allocate device memory based on the workspaceSize calculated by the first-phase API
        void* workspaceQuantParamAddr = nullptr;
        std::unique_ptr<void, aclError (*)(void*)> workspaceQuantParamAddrPtr(nullptr, aclrtFree);
        if (workspaceSize > 0) {
            ret = aclrtMalloc(&workspaceQuantParamAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST);
            CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret);
            workspaceQuantParamAddrPtr.reset(workspaceQuantParamAddr);
        }
        // Call the second-phase API of aclnnTransQuantParamV2
        ret = aclnnTransQuantParamV2(workspaceQuantParamAddr, workspaceSize, executor, stream);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnTransQuantParamV2 failed. ERROR: %d\n", ret); return ret);
    
        workspaceSize = 0;
        executor = nullptr;
        // Call the first-phase API of aclnnQuantMatmulV5
        ret = aclnnQuantMatmulV5GetWorkspaceSize(
            x1, x2, nullptr, x2Scale, quantParam, nullptr, nullptr, nullptr, bias, transposeX1, transposeX2, groupSize, out,
            &workspaceSize, &executor);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnQuantMatmulV5GetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
        // Allocate device memory based on the workspaceSize calculated by the first-phase API
        void* workspaceAddr = nullptr;
        std::unique_ptr<void, aclError (*)(void*)> workspaceAddrPtr(nullptr, aclrtFree);
        if (workspaceSize > 0) {
            ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST);
            CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret);
            workspaceAddrPtr.reset(workspaceAddr);
        }
        // Call the second-phase API of aclnnQuantMatmulV5
        ret = aclnnQuantMatmulV5(workspaceAddr, workspaceSize, executor, stream);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnQuantMatmulV5 failed. ERROR: %d\n", ret); return ret);
    
        // 4. (boilerplate) Synchronously wait for the task to finish.
        ret = aclrtSynchronizeStream(stream);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);
    
        // 5. Obtain the output value, copy the result from device memory to host memory, and modify it according to the API definition.
        auto size = GetShapeSize(outShape);
        std::vector<uint16_t> resultData(
            size, 0); // fp16 data cannot be printed directly in C. Read it as uint16 and convert it to fp16 through binary.
        ret = aclrtMemcpy(
            resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr, size * sizeof(resultData[0]),
            ACL_MEMCPY_DEVICE_TO_HOST);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret);
        for (int64_t i = 0; i < size; i++) {
            LOG_PRINT("result[%ld] is: %.1f\n", i, Bf16ToFloat(resultData[i]));
        }
        return ACL_SUCCESS;
    }
    
    int main()
    {
        // 1. (boilerplate) Initialize device/stream. For details, see the acl API manual.
        // Set deviceId based on the actual device.
        int32_t deviceId = 0;
        aclrtStream stream;
        auto ret = AclnnQuantMatmulV5Test(deviceId, stream);
        CHECK_FREE_RET(ret == ACL_SUCCESS, LOG_PRINT("AclnnQuantMatmulV5Test failed. ERROR: %d\n", ret); return ret);
    
        Finalize(deviceId, stream);
        return 0;
    }
    • Ascend 950PR&950DT products:

    x1 is INT8, x2 is INT8, x1Scale is FLOAT32, x2Scale is FLOAT32, and bias is INT32.

    #include <iostream>
    #include <memory>
    #include <vector>
    
    #include "acl/acl.h"
    #include "aclnnop/aclnn_quant_matmul_v5.h"
    
    #define CHECK_RET(cond, return_expr) \
        do {                             \
            if (!(cond)) {               \
                return_expr;             \
            }                            \
        } while (0)
    
    #define CHECK_FREE_RET(cond, return_expr) \
        do {                                  \
            if (!(cond)) {                    \
                Finalize(deviceId, stream);   \
                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 shapeSize = 1;
        for (auto i : shape) {
            shapeSize *= i;
        }
        return shapeSize;
    }
    
    int Init(int32_t deviceId, aclrtStream *stream)
    {
        // Boilerplate for resource initialization
        auto ret = aclInit(nullptr);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclInit failed. ERROR: %d\n", ret); return ret);
        ret = aclrtSetDevice(deviceId);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSetDevice failed. ERROR: %d\n", ret); return ret);
        ret = aclrtCreateStream(stream);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtCreateStream failed. ERROR: %d\n", ret); return ret);
        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 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);
    
        // 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 the aclCreateTensor API 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;
    }
    
    void Finalize(int32_t deviceId, aclrtStream stream)
    {
        aclrtDestroyStream(stream);
        aclrtResetDevice(deviceId);
        aclFinalize();
    }
    
    int aclnnQuantMatmulV5Test(int32_t deviceId, aclrtStream &stream)
    {
        auto ret = Init(deviceId, &stream);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
    
        // 2. Construct the inputs and outputs. Custom construction is required based on the API.
        std::vector<int64_t> x1Shape = {5, 16};
        std::vector<int64_t> x2Shape = {16, 8};
        std::vector<int64_t> biasShape = {8};
        std::vector<int64_t> x1ScaleShape = {5};
        std::vector<int64_t> x2ScaleShape = {8};
        std::vector<int64_t> outShape = {5, 8};
        void *x1DeviceAddr = nullptr;
        void *x2DeviceAddr = nullptr;
        void *x2ScaleDeviceAddr = nullptr;
        void *x2OffsetDeviceAddr = nullptr;
        void *x1ScaleDeviceAddr = nullptr;
        void *biasDeviceAddr = nullptr;
        void *outDeviceAddr = nullptr;
        aclTensor *x1 = nullptr;
        aclTensor *x2 = nullptr;
        aclTensor *bias = nullptr;
        aclTensor *x2Scale = nullptr;
        aclTensor *x2Offset = nullptr;
        aclTensor *x1Scale = nullptr;
        aclTensor *out = nullptr;
        std::vector<int8_t> x1HostData(80, 1);
        std::vector<int8_t> x2HostData(128, 1);
        std::vector<int32_t> biasHostData(8, 1);
        std::vector<float> x2ScaleHostData(8, 1);
        std::vector<float> x1ScaleHostData(5, 1);
        std::vector<uint16_t> outHostData(40, 1);  // Actually in float16 half-precision mode
        // Create the x1 aclTensor
        ret = CreateAclTensor(x1HostData, x1Shape, &x1DeviceAddr, aclDataType::ACL_INT8, &x1);
        std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor *)> x1TensorPtr(x1, aclDestroyTensor);
        std::unique_ptr<void, aclError (*)(void *)> x1DeviceAddrPtr(x1DeviceAddr, aclrtFree);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
        // Create the x2 aclTensor
        ret = CreateAclTensor(x2HostData, x2Shape, &x2DeviceAddr, aclDataType::ACL_INT8, &x2);
        std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor *)> x2TensorPtr(x2, aclDestroyTensor);
        std::unique_ptr<void, aclError (*)(void *)> x2DeviceAddrPtr(x2DeviceAddr, aclrtFree);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
        // Create the x1Scale aclTensor.
        ret = CreateAclTensor(x1ScaleHostData, x1ScaleShape, &x1ScaleDeviceAddr,
                              aclDataType::ACL_FLOAT, &x1Scale);
        std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor *)> x1ScaleTensorPtr(x1Scale,
                                                                                              aclDestroyTensor);
        std::unique_ptr<void, aclError (*)(void *)> x1ScaleDeviceAddrPtr(x1ScaleDeviceAddr, aclrtFree);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
        // Create the x2Scale aclTensor.
        ret = CreateAclTensor(x2ScaleHostData, x2ScaleShape, &x2ScaleDeviceAddr, aclDataType::ACL_FLOAT, &x2Scale);
        std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor *)> scaleTensorPtr(x2Scale, aclDestroyTensor);
        std::unique_ptr<void, aclError (*)(void *)> x2ScaleDeviceAddrPtr(x2ScaleDeviceAddr, aclrtFree);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
        // Create the bias aclTensor.
        ret = CreateAclTensor(biasHostData, biasShape, &biasDeviceAddr, aclDataType::ACL_INT32, &bias);
        std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor *)> biasTensorPtr(bias, aclDestroyTensor);
        std::unique_ptr<void, aclError (*)(void *)> biasDeviceAddrPtr(biasDeviceAddr, aclrtFree);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
        // Create the out aclTensor.
        ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_FLOAT16, &out);
        std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor *)> outTensorPtr(out, aclDestroyTensor);
        std::unique_ptr<void, aclError (*)(void *)> outDeviceAddrPtr(outDeviceAddr, aclrtFree);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
        bool transposeX1 = false;
        bool transposeX2 = false;
    
        // 3. Call the CANN operator library API, which must be changed to the specific API name.
        uint64_t workspaceSize = 0;
        aclOpExecutor *executor = nullptr;
        // Call the first-phase API of aclnnQuantMatmulV5.
        ret = aclnnQuantMatmulV5GetWorkspaceSize(x1, x2, x1Scale, x2Scale, nullptr, nullptr, nullptr, nullptr, bias,
                                                 transposeX1, transposeX2, 0, out, &workspaceSize, &executor);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnQuantMatmulV5GetWorkspaceSize failed. ERROR: %d\n", ret);
                  return ret);
        // Allocate device memory based on the workspaceSize calculated by the first-phase API.
        void *workspaceAddr = nullptr;
        std::unique_ptr<void, aclError (*)(void *)> workspaceAddrPtr(nullptr, aclrtFree);
        if (workspaceSize > 0) {
            ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST);
            CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret);
            workspaceAddrPtr.reset(workspaceAddr);
        }
        // Call the second-phase API of aclnnQuantMatmulV5.
        ret = aclnnQuantMatmulV5(workspaceAddr, workspaceSize, executor, stream);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnQuantMatmulV5 failed. ERROR: %d\n", ret); return ret);
    
        // 4. (Boilerplate) Synchronously wait for the task execution to complete.
        ret = aclrtSynchronizeStream(stream);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);
    
        // 5. Obtain the output value and copy the result from the device to the host. Modify it according to the specific API definition.
        auto size = GetShapeSize(outShape);
        std::vector<uint16_t> resultData(
            size, 0);  // fp16 data cannot be printed directly in C. Read it as uint16 and manually convert it to fp16 in binary.
        ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr,
                          size * sizeof(resultData[0]), ACL_MEMCPY_DEVICE_TO_HOST);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret);
                  return ret);
        for (int64_t i = 0; i < size; i++) {
            LOG_PRINT("result[%ld] is: %u\n", i, resultData[i]);
        }
        return ACL_SUCCESS;
    }
    
    int main()
    {
        // 1. (Boilerplate) Initialize device/stream. For details, see the acl API documentation.
        // Set deviceId based on the actual device.
        int32_t deviceId = 0;
        aclrtStream stream;
        auto ret = aclnnQuantMatmulV5Test(deviceId, stream);
        CHECK_FREE_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnQuantMatmulV5Test failed. ERROR: %d\n", ret); return ret);
    
        Finalize(deviceId, stream);
        return 0;
    }
    • Ascend 950PR&950DT products:

    x1 and x2 are FLOAT8_E4M3FN, x1Scale is FLOAT32, x2Scale is FLOAT32, no x2Offset, and bias is FLOAT32.

    #include <iostream>
    #include <memory>
    #include <vector>
    
    #include "acl/acl.h"
    #include "aclnnop/aclnn_quant_matmul_v5.h"
    
    #define CHECK_RET(cond, return_expr) \
        do {                             \
            if (!(cond)) {               \
                return_expr;             \
            }                            \
        } while (0)
    
    #define CHECK_FREE_RET(cond, return_expr) \
        do {                                  \
            if (!(cond)) {                    \
                Finalize(deviceId, stream);   \
                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 shapeSize = 1;
        for (auto i : shape) {
            shapeSize *= i;
        }
        return shapeSize;
    }
    
    int Init(int32_t deviceId, aclrtStream *stream)
    {
        // Boilerplate for resource initialization.
        auto ret = aclInit(nullptr);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclInit failed. ERROR: %d\n", ret); return ret);
        ret = aclrtSetDevice(deviceId);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSetDevice failed. ERROR: %d\n", ret); return ret);
        ret = aclrtCreateStream(stream);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtCreateStream failed. ERROR: %d\n", ret); return ret);
        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-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);
        return 0;
    }
    
    void Finalize(int32_t deviceId, aclrtStream stream)
    {
        aclrtDestroyStream(stream);
        aclrtResetDevice(deviceId);
        aclFinalize();
    }
    
    int aclnnQuantMatmulV5Test(int32_t deviceId, aclrtStream &stream)
    {
        auto ret = Init(deviceId, &stream);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
    
        // 2. Construct the input and output. Custom construction is required based on the API.
        std::vector<int64_t> x1Shape = {16, 16};
        std::vector<int64_t> x2Shape = {16, 16};
        std::vector<int64_t> biasShape = {16};
        std::vector<int64_t> x1ScaleShape = {1};
        std::vector<int64_t> x2ScaleShape = {1};
        std::vector<int64_t> outShape = {16, 16};
        void *x1DeviceAddr = nullptr;
        void *x2DeviceAddr = nullptr;
        void *x2ScaleDeviceAddr = nullptr;
        void *x2OffsetDeviceAddr = nullptr;
        void *x1ScaleDeviceAddr = nullptr;
        void *biasDeviceAddr = nullptr;
        void *outDeviceAddr = nullptr;
        aclTensor *x1 = nullptr;
        aclTensor *x2 = nullptr;
        aclTensor *bias = nullptr;
        aclTensor *x2Scale = nullptr;
        aclTensor *x2Offset = nullptr;
        aclTensor *x1Scale = nullptr;
        aclTensor *out = nullptr;
        std::vector<int8_t> x1HostData(256, 1);
        std::vector<int8_t> x2HostData(256, 1);
        std::vector<float> biasHostData(16, 1);
        std::vector<float> x2ScaleHostData(1, 1);
        std::vector<float> x1ScaleHostData(1, 1);
        std::vector<uint16_t> outHostData(256, 1);  // Actually float16 half precision
    
        // Create x1 aclTensor.
        ret = CreateAclTensor(x1HostData, x1Shape, &x1DeviceAddr, aclDataType::ACL_FLOAT8_E4M3FN, &x1);
        std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor *)> x1TensorPtr(x1, aclDestroyTensor);
        std::unique_ptr<void, aclError (*)(void *)> x1DeviceAddrPtr(x1DeviceAddr, aclrtFree);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
        // Create x2 aclTensor.
        ret = CreateAclTensor(x2HostData, x2Shape, &x2DeviceAddr, aclDataType::ACL_FLOAT8_E4M3FN, &x2);
        std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor *)> x2TensorPtr(x2, aclDestroyTensor);
        std::unique_ptr<void, aclError (*)(void *)> x2DeviceAddrPtr(x2DeviceAddr, aclrtFree);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
        // Create x2Scale aclTensor.
        ret = CreateAclTensor(x2ScaleHostData, x2ScaleShape, &x2ScaleDeviceAddr, aclDataType::ACL_FLOAT, &x2Scale);
        std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor *)> scaleTensorPtr(x2Scale, aclDestroyTensor);
        std::unique_ptr<void, aclError (*)(void *)> x2ScaleDeviceAddrPtr(x2ScaleDeviceAddr, aclrtFree);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
        // Create x1Scale aclTensor.
        ret = CreateAclTensor(x1ScaleHostData, x1ScaleShape, &x1ScaleDeviceAddr,
                              aclDataType::ACL_FLOAT, &x1Scale);
        std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor *)> x1ScaleTensorPtr(x1Scale,
                                                                                              aclDestroyTensor);
        std::unique_ptr<void, aclError (*)(void *)> x1ScaleDeviceAddrPtr(x1ScaleDeviceAddr, aclrtFree);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
        // Create bias aclTensor.
        ret = CreateAclTensor(biasHostData, biasShape, &biasDeviceAddr, aclDataType::ACL_FLOAT, &bias);
        std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor *)> biasTensorPtr(bias, aclDestroyTensor);
        std::unique_ptr<void, aclError (*)(void *)> biasDeviceAddrPtr(biasDeviceAddr, aclrtFree);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
        // Create out aclTensor.
        ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_FLOAT16, &out);
        std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor *)> outTensorPtr(out, aclDestroyTensor);
        std::unique_ptr<void, aclError (*)(void *)> outDeviceAddrPtr(outDeviceAddr, aclrtFree);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
        bool transposeX1 = false;
        bool transposeX2 = false;
    
        // 3. Call the CANN operator library API. Change it to the specific API name.
        uint64_t workspaceSize = 0;
        aclOpExecutor *executor = nullptr;
        // Call the first-phase API of aclnnQuantMatmulV5.
        ret = aclnnQuantMatmulV5GetWorkspaceSize(x1, x2, x1Scale, x2Scale, nullptr, nullptr, x2Offset, nullptr, bias,
                                                 transposeX1, transposeX2, 0, out, &workspaceSize, &executor);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnQuantMatmulV5GetWorkspaceSize failed. ERROR: %d\n", ret);
                  return ret);
        // Allocate device memory based on the workspaceSize computed by the first-phase API.
        void *workspaceAddr = nullptr;
        std::unique_ptr<void, aclError (*)(void *)> workspaceAddrPtr(nullptr, aclrtFree);
        if (workspaceSize > 0) {
            ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST);
            CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret);
            workspaceAddrPtr.reset(workspaceAddr);
        }
        // Call the second-phase API aclnnQuantMatmulV5
        ret = aclnnQuantMatmulV5(workspaceAddr, workspaceSize, executor, stream);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnQuantMatmulV5 failed. ERROR: %d\n", ret); return ret);
    
        // 4. (boilerplate) Synchronously wait for the task execution to complete.
        ret = aclrtSynchronizeStream(stream);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);
    
        // 5. Obtain the output value and copy the result from device memory to host memory. Modify it according to the specific API definition.
        auto size = GetShapeSize(outShape);
        std::vector<uint16_t> resultData(
            size, 0);  // fp16 data cannot be printed directly in C. Read it as uint16 and convert the binary data to fp16 by yourself.
        ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr,
                          size * sizeof(resultData[0]), ACL_MEMCPY_DEVICE_TO_HOST);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret);
                  return ret);
        for (int64_t i = 0; i < size; i++) {
            LOG_PRINT("result[%ld] is: %u\n", i, resultData[i]);
        }
        return ACL_SUCCESS;
    }
    
    int main()
    {
        // 1. (boilerplate) Initialize the device/stream. For details, see the acl API manual.
        // Set deviceId based on the actual device.
        int32_t deviceId = 0;
        aclrtStream stream;
        auto ret = aclnnQuantMatmulV5Test(deviceId, stream);
        CHECK_FREE_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnQuantMatmulV5Test failed. ERROR: %d\n", ret); return ret);
    
        Finalize(deviceId, stream);
        return 0;
    }
    • Atlas A2 products, Atlas A3 products:

    x1 is INT8, x2 is INT32, x1Scale is FLOAT32, and x2Scale is UINT64.

    #include <iostream>
    #include <memory>
    #include <vector>
    
    #include "acl/acl.h"
    #include "aclnnop/aclnn_quant_matmul_v5.h"
    
    #define CHECK_RET(cond, return_expr) \
        do {                             \
            if (!(cond)) {               \
                return_expr;             \
            }                            \
        } while (0)
    
    #define CHECK_FREE_RET(cond, return_expr) \
        do {                                  \
            if (!(cond)) {                    \
                Finalize(deviceId, stream);   \
                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 shapeSize = 1;
        for (auto i : shape) {
            shapeSize *= i;
        }
        return shapeSize;
    }
    
    int Init(int32_t deviceId, aclrtStream *stream)
    {
        // Boilerplate: initialize resources.
        auto ret = aclInit(nullptr);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclInit failed. ERROR: %d\n", ret); return ret);
        ret = aclrtSetDevice(deviceId);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSetDevice failed. ERROR: %d\n", ret); return ret);
        ret = aclrtCreateStream(stream);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtCreateStream failed. ERROR: %d\n", ret); return ret);
        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 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 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);
    
        // 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 the aclCreateTensor API 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;
    }
    
    void Finalize(int32_t deviceId, aclrtStream stream)
    {
        aclrtDestroyStream(stream);
        aclrtResetDevice(deviceId);
        aclFinalize();
    }
    
    int aclnnQuantMatmulV5Test(int32_t deviceId, aclrtStream &stream)
    {
        auto ret = Init(deviceId, &stream);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
    
        // 2. Construct the inputs and outputs, which need to be customized based on the API.
        std::vector<int64_t> x1Shape = {1, 8192};     // (m,k)
        std::vector<int64_t> x2Shape = {8192, 128};  // (k,n)
        std::vector<int64_t> yoffsetShape = {1024};
    
        std::vector<int64_t> x1ScaleShape = {1,1};
        std::vector<int64_t> x2ScaleShape = {32, 1024}; // x2ScaleShape = [KShape / groupsize, N]
        std::vector<int64_t> outShape = {1, 1024};
    
        void *x1DeviceAddr = nullptr;
        void *x2DeviceAddr = nullptr;
        void *x2ScaleDeviceAddr = nullptr;
        void *x1ScaleDeviceAddr = nullptr;
        void *yoffsetDeviceAddr = nullptr;
        void *outDeviceAddr = nullptr;
        aclTensor *x1 = nullptr;
        aclTensor *x2 = nullptr;
        aclTensor *yoffset = nullptr;
        aclTensor *x2Scale = nullptr;
        aclTensor *x2Offset = nullptr;
        aclTensor *x1Scale = nullptr;
        aclTensor *out = nullptr;
        std::vector<int8_t> x1HostData(GetShapeSize(x1Shape), 1);
        std::vector<int32_t> x2HostData(GetShapeSize(x2Shape), 1);
        std::vector<int32_t> yoffsetHostData(GetShapeSize(yoffsetShape), 1);
        std::vector<int32_t> x1ScaleHostData(GetShapeSize(x1ScaleShape), 1);
        float tmp = 1;
        uint64_t x2ScaleValue = static_cast<uint64_t>(*reinterpret_cast<int32_t*>(&tmp));
        std::vector<int64_t> x2ScaleHostData(GetShapeSize(x2ScaleShape), x2ScaleValue);
        std::vector<uint16_t> outHostData(GetShapeSize(outShape), 1);  // Actually in float16 half-precision mode.
    
        // Create the x1 aclTensor.
        ret = CreateAclTensor(x1HostData, x1Shape, &x1DeviceAddr, aclDataType::ACL_INT8, &x1);
        std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor *)> x1TensorPtr(x1, aclDestroyTensor);
        std::unique_ptr<void, aclError (*)(void *)> x1DeviceAddrPtr(x1DeviceAddr, aclrtFree);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
        // Create the x2 aclTensor.
        ret = CreateAclTensor(x2HostData, x2Shape, &x2DeviceAddr, aclDataType::ACL_INT32, &x2);
        std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor *)> x2TensorPtr(x2, aclDestroyTensor);
        std::unique_ptr<void, aclError (*)(void *)> x2DeviceAddrPtr(x2DeviceAddr, aclrtFree);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
        // Create the x1Scale aclTensor.
        ret = CreateAclTensor(x1ScaleHostData, x1ScaleShape, &x1ScaleDeviceAddr, aclDataType::ACL_FLOAT, &x1Scale);
        std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor *)> x1ScaleTensorPtr(x1Scale, aclDestroyTensor);
        std::unique_ptr<void, aclError (*)(void *)> x1ScaleDeviceAddrPtr(x1ScaleDeviceAddr, aclrtFree);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
        // Create the x2Scale aclTensor.
        ret = CreateAclTensor(x2ScaleHostData, x2ScaleShape, &x2ScaleDeviceAddr, aclDataType::ACL_UINT64, &x2Scale);
        std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor *)> scaleTensorPtr(x2Scale, aclDestroyTensor);
        std::unique_ptr<void, aclError (*)(void *)> x2ScaleDeviceAddrPtr(x2ScaleDeviceAddr, aclrtFree);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
        // Create the yoffset aclTensor.
        ret = CreateAclTensor(yoffsetHostData, yoffsetShape, &yoffsetDeviceAddr, aclDataType::ACL_FLOAT, &yoffset);
        std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor *)> yoffsetTensorPtr(yoffset, aclDestroyTensor);
        std::unique_ptr<void, aclError (*)(void *)> yoffsetDeviceAddrPtr(yoffsetDeviceAddr, aclrtFree);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
        // Create the out aclTensor.
        ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_FLOAT16, &out);
        std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor *)> outTensorPtr(out, aclDestroyTensor);
        std::unique_ptr<void, aclError (*)(void *)> outDeviceAddrPtr(outDeviceAddr, aclrtFree);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
        bool transposeX1 = false;
        bool transposeX2 = false;
        int64_t groupSize = 256;
    
        // 3. Call the CANN operator library API. Replace it with the specific API name.
        uint64_t workspaceSize = 0;
        aclOpExecutor *executor = nullptr;
    
        ret = aclnnQuantMatmulV5GetWorkspaceSize(x1, x2, x1Scale, x2Scale, nullptr, nullptr, nullptr, yoffset, nullptr,
                                                transposeX1, transposeX2, groupSize, out, &workspaceSize, &executor);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnQuantMatmulV5GetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
        // Allocate device memory based on the workspaceSize computed by the first-phase API.
        void *workspaceAddr = nullptr;
        std::unique_ptr<void, aclError (*)(void *)> workspaceAddrPtr(nullptr, aclrtFree);
        if (workspaceSize > 0) {
            ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST);
            CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret);
            workspaceAddrPtr.reset(workspaceAddr);
        }
        // Call the second-phase API of aclnnQuantMatmulV5.
        ret = aclnnQuantMatmulV5(workspaceAddr, workspaceSize, executor, stream);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnQuantMatmulV5 failed. ERROR: %d\n", ret); return ret);
    
        // 4. (boilerplate) Synchronously wait for the task execution to complete.
        ret = aclrtSynchronizeStream(stream);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);
    
        // 5. Obtain the output value and copy the result from the device side to the host side. Change it according to the specific API definition.
        auto size = GetShapeSize(outShape);
        std::vector<uint16_t> resultData(size, 0);  // fp16 data cannot be printed directly in C. Read it as uint16 and convert it to fp16 manually based on the binary.
        ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr,
                        size * sizeof(resultData[0]), ACL_MEMCPY_DEVICE_TO_HOST);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret);
                return ret);
        for (int64_t i = 0; i < size; i++) {
            LOG_PRINT("result[%ld] is: %u\n", i, resultData[i]);
        }
        return ACL_SUCCESS;
    }
    
    int main()
    {
        // 1. (boilerplate) Initialize device/stream. For details, see the acl API guide.
        // Set deviceId based on the actual device.
        int32_t deviceId = 1;
        aclrtStream stream;
        auto ret = aclnnQuantMatmulV5Test(deviceId, stream);
        CHECK_FREE_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnQuantMatmulV5Test failed. ERROR: %d\n", ret); return ret);
        Finalize(deviceId, stream);
        return 0;
    }