aclnnGroupedMatmulSwigluQuantWeightNzV2

📄 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: Not supported
  • Atlas training products: Not supported

Function

  • Description: Fuses GroupedMatmul, dequant, swiglu, and quant. For details, see the formula. This is a weightNZ-specific version of the aclnnGroupedMatmulSwigluQuantV2 API. The difference between this API and aclnnGroupedMatmulSwigluQuantV2 is that the weight parameter is forcibly treated as FRACTAL_NZ format in this scenario.

  • Formula:

    • Atlas A3 products, Atlas A2 products:
      Quantization scenario A8W8 (A refers to the activation matrix, W refers to the weight matrix, and 8 refers to the INT8 data type):
      • Definition:

        • ⋅ denotes matrix multiplication.
        • ⊙ indicates per-element multiplication.
        • $\left \lfloor x\right \rceil$ indicates rounding x to the nearest integer.
        • $\mathbb{Z_8} = \{ x \in \mathbb{Z} | −128≤x≤127 \}$
        • $\mathbb{Z_{32}} = \{ x \in \mathbb{Z} | -2147483648≤x≤2147483647 \}$
      • Inputs:

        • $X∈\mathbb{Z_8}^{M \times K}$: activation matrix (left matrix), where M is the total number of tokens and K is the feature dimension.
        • $W∈\mathbb{Z_8}^{E \times K \times N}$: grouped weight matrix (right matrix), where E is the number of experts, K is the feature dimension, and N is the output dimension.
        • $w\_scale∈\mathbb{R}^{E \times N}$: Per-channel scale factor of the grouped weight matrix (right matrix), where E is the number of experts and N is the output dimension.
        • $x\_scale∈\mathbb{R}^{M}$: Per-token scale factor of the activation matrix (left matrix), where M is the total number of tokens.
        • $grouplist∈\mathbb{N}^{E}$: group index list of cumsum or count.
      • Outputs:

        • $Q∈\mathbb{Z_8}^{M \times N / 2}$: quantized output matrix.
        • $Q\_scale∈\mathbb{R}^{M}$: quantization scale factor.
      • Computation Process

        • Determine the current group's tokens according to groupList[i], $i \in [0,Len(groupList)]$.

          Example: Assume groupList=[3,4,4,6] and groupListType=cumsum, or groupList=[3,1,0,2] and groupListType=count.

          Note: The two different grouping methods above actually produce the same grouping result.

          The 0th right matrix W[0,:,:] corresponds to the tokens x[0:3] at index positions [0,3) (3-0=3 tokens in total), and corresponds to x_scale[0:3], w_scale[0], bias[0], offset[0], Q[0:3], Q_scale[0:3], and Q_offset[0:3];

          The 1st right matrix W[1,:,:] corresponds to the tokens x[3:4] at index positions [3,4) (4-3=1 token in total), and corresponds to x_scale[3:4], w_scale[1], bias[1], offset[1], Q[3:4], Q_scale[3:4], and Q_offset[3:4];

          The 2nd right matrix W[2,:,:] corresponds to the tokens x[4:4] at index positions [4,4) (4-4=0 tokens in total), and corresponds to x_scale[4:4], w_scale[2], bias[2], offset[2], Q[4:4], Q_scale[4:4], and Q_offset[4:4];

          The 3rd right matrix W[3,:,:] corresponds to the tokens x[4:6] at index positions [4,6) (6-4=2 tokens in total), and corresponds to x_scale[4:6], w_scale[3], bias[3], offset[3], Q[4:6], Q_scale[4:6], and Q_offset[4:6];

          Note: The parts not specified in grouplist will not be updated. For example, when groupList=[12,14,18], GroupListType=cumsum, and the shape of X is [30,:].

          Then the shape of the first output Q is [30,:], where the part Q[18:,:] is not updated or initialized, and the data is the original data of the device memory space upon allocation.

          Similarly, the shape of the second output Q_scale is [30], where Q_scale[18:] is not updated or initialized, and the data is the original data of the device memory space upon allocation.

          That is, Q[:grouplist[-1],:] and Q_scale[:grouplist[-1]] of the output are the valid data.

        • Perform the following computation based on the input parameters determined by the group:

          $C_{i} = (X_{i}\cdot W_{i} )\odot x\_scale_{i\ Broadcast} \odot w\_scale_{i\ Broadcast}$

          $C_{i,act}, gate_{i} = split(C_{i})$

          $S_{i}=Swish(C_{i,act})\odot gate_{i}$   where $Swish(x)=\frac{x}{1+e^{-x}}$

        • Quantize the output result

          $Q\_scale_{i} = \frac{max(|S_{i}|)}{127}$

          $Q_{i} = \left\lfloor \frac{S_{i}}{Q\_scale_{i}} \right\rceil$

MSD scenario A8W4 (A refers to the activation matrix, W refers to the weight matrix, 8 refers to the INT8 data type, and 4 refers to the INT4 data type):
  • Definition:
    • ⋅ denotes matrix multiplication.
    • ⊙ indicates per-element multiplication.
    • $\left \lfloor x\right \rceil$ indicates rounding x to the nearest integer.
    • $\mathbb{Z_8} = \{ x \in \mathbb{Z} | −128≤x≤127 \}$
    • $\mathbb{Z_4} = \{ x \in \mathbb{Z} | −8≤x≤7 \}$
    • $\mathbb{Z_{32}} = \{ x \in \mathbb{Z} | -2147483648≤x≤2147483647 \}$
  • Inputs:
    • $X∈\mathbb{Z_8}^{M \times K}$: activation matrix (left matrix), where M is the total number of tokens and K is the feature dimension.
    • $W∈\mathbb{Z_4}^{E \times K \times N}$: Grouped weight matrix (right matrix), where E is the number of experts, K is the feature dimension, and N is the output dimension.
    • $weightAssistMatrix∈\mathbb{R}^{E \times N}$: Auxiliary matrix used in matrix multiplication (see the computation process for generating the auxiliary matrix below).
    • $w\_scale∈\mathbb{R}^{E \times K\_group\_num \times N}$: Per-channel scale factor of the grouped weight matrix (right matrix), where E is the number of experts, K_group_num is the number of groups along the K axis, and N is the output dimension.
    • $x\_scale∈\mathbb{R}^{M}$: Per-token scale factor of the activation matrix (left matrix), where M is the total number of tokens.
    • $grouplist∈\mathbb{N}^{E}$: group index list of cumsum or count.
  • Output:
    • $Q∈\mathbb{Z_8}^{M \times N / 2}$: quantized output matrix.
    • $Q\_scale∈\mathbb{R}^{M}$: quantization scale factor.
  • Computation Process
    • Determine the tokens of the current group based on groupList[i], where $i \in [0,Len(groupList)]$.

      • The grouping logic is the same as that of A8W8.
    • Computation process for generating the auxiliary matrix (weightAssistMatrix) (note that the weightAssistMatrix is partially computed offline and provided as input, not completed inside the operator):

      • For per-channel quantization ($w\_scale$ is 2-dimensional):

        $weightAssistMatrix_{i} = 8 × weightScale × Σ_{k=0}^{K-1} weight[:,k,:]$

      • For per-group quantization ($w\_scale$ is 3-dimensional):

        $weightAssistMatrix_{i} = 8 × Σ_{k=0}^{K-1} (weight[:,k,:] × weightScale[:, ⌊k/num\_per\_group⌋, :])$

        NOTE: $num\_per\_group = K // K\_group\_num$

    • Perform the following computation based on the inputs determined by grouping:

      • 3.1. Convert the left matrix $\mathbb{Z_8}$ into high-bit and low-bit parts $\mathbb{Z_4}$ $X\_high\_4bits_{i} = \lfloor \frac{X_{i}}{16} \rfloor$ $X\_low\_4bits_{i} = X_{i} \& 0x0f - 8$

      • 3.2. Enable per-channel or per-group quantization when performing matrix multiplication per-channel:

        $C\_high_{i} = (X\_high\_4bits_{i} \cdot W_{i}) \odot w\_scale_{i}$

        $C\_low_{i} = (X\_low\_4bits_{i} \cdot W_{i}) \odot w\_scale_{i}$

        per-group:

        $C\_high_{i} = \\ Σ_{k=0}^{K-1}((X\_high\_4bits_{i}[:, k * num\_per\_group : (k+1) * num\_per\_group] \cdot W_{i}[k * num\_per\_group : (k+1) * num\_per\_group, :]) \odot w\_scale_{i}[k, :] )$

        $C\_low_{i} = \\ Σ_{k=0}^{K-1}((X\_low\_4bits_{i}[:, k * num\_per\_group : (k+1) * num\_per\_group] \cdot W_{i}[k * num\_per\_group : (k+1) * num\_per\_group, :]) \odot w\_scale_{i}[k, :] )$

      • 3.3. Restore the high-bit and low-bit matrix multiplication results to the overall result.

        $C_{i} = (C\_high_{i} * 16 + C\_low_{i} + weightAssistMatrix_{i}) \odot x\_scale_{i}$

        $C_{i,act}, gate_{i} = split(C_{i})$

        $S_{i}=Swish(C_{i,act})\odot gate_{i}$   where $Swish(x)=\frac{x}{1+e^{-x}}$

    • Quantize the output result

      $Q\_scale_{i} = \frac{max(|S_{i}|)}{127}$

      $Q_{i} = \left\lfloor \frac{S_{i}}{Q\_scale_{i}} \right\rceil$

Quantization scenario A4W4 (A refers to the activation matrix, W refers to the weight matrix, and 4 refers to the INT4 data type):
  • Definition:

    • ⋅ denotes matrix multiplication.
    • ⊙ indicates per-element multiplication.
    • $\left \lfloor x\right \rceil$ indicates rounding x to the nearest integer.
    • $\mathbb{Z_4} = \{ x \in \mathbb{Z} | −8≤x≤7 \}$
    • $\mathbb{Z_8} = \{ x \in \mathbb{Z} | −128≤x≤127 \}$
    • $\mathbb{Z_{32}} = \{ x \in \mathbb{Z} | -2147483648≤x≤2147483647 \}$
  • Input:

    • $X∈\mathbb{Z_4}^{M \times K}$: Activation matrix (left matrix), where M is the total number of tokens and K is the feature dimension.
    • $W∈\mathbb{Z_4}^{E \times K \times N}$: Grouped weight matrix (right matrix), where E is the number of experts, K is the feature dimension, and N is the output dimension.
    • $w\_scale∈\mathbb{R}^{E \times N}$: Per-channel scale factor of the grouped weight matrix (right matrix), where E is the number of experts and N is the output dimension.
    • $x\_scale∈\mathbb{R}^{M}$: Per-token scale factor of the activation matrix (left matrix), where M is the total number of tokens.
    • $smoothScale∈\mathbb{R}^{E \times N/2}(per-channel) or \mathbb{R}^{E}(per-tensor)$: smooth scale factor, where E is the number of experts and N is the output dimension.
    • $grouplist∈\mathbb{N}^{E}$: group index list of cumsum or count.
  • Output:

    • $Q∈\mathbb{Z_8}^{M \times N / 2}$: quantized output matrix.
    • $Q\_scale∈\mathbb{R}^{M}$: quantization scale factor.
  • Computation Process

    • Determine the current group's tokens according to groupList[i], $i \in [0,Len(groupList)]$.

      • The grouping logic is the same as that of A8W8.
    • Perform the following computation based on the input parameters determined by the group:

      $C_{i} = (X_{i}\cdot W_{i} )\odot x\_scale_{i\ Broadcast} \odot w\_scale_{i\ Broadcast}$

      $C_{i,act}, gate_{i} = split(C_{i})$

      $S_{i}=Swish(C_{i,act})\odot gate_{i}$   where $Swish(x)=\frac{x}{1+e^{-x}}$

      $S_{i} = S_{i} \odot smoothScale_{i\ Broadcast}$

    • Quantize the output result

      $Q\_scale_{i} = \frac{max(|S_{i}|)}{127}$

      $Q_{i} = \left\lfloor \frac{S_{i}}{Q\_scale_{i}} \right\rceil$

  • Ascend 950PR&950DT products:
    A8W8 Pertoken quantization:
    • Definition:

      • ⋅ denotes matrix multiplication.
      • ⊙ indicates per-element multiplication.
    • Input:

      • $X∈\mathbb{Z_8}^{M \times K}$: activation matrix (left matrix), where M is the total number of tokens and K is the feature dimension.
      • $W∈\mathbb{Z_8}^{E \times K \times N}$: grouped weight matrix (right matrix), where E is the number of experts, K is the feature dimension, and N is the output dimension.
      • $w\_scale∈\mathbb{R}^{E \times N}$: Per-channel scale factor of the grouped weight matrix (right matrix), where E is the number of experts and N is the output dimension.
      • $x\_scale∈\mathbb{R}^{M}$: Per-token scaling factor of the activation matrix (left matrix). M is the total number of tokens, and K is the feature dimension.
      • $grouplist∈\mathbb{N}^{E}$: group index list of cumsum or count.
    • Output:

      • $Q∈\mathbb{Z_8}^{M \times N / 2}$: quantized output matrix.
      • $Q\_scale∈\mathbb{R}^{M}$: quantization scale factor.
    • Computation Process

      • Determine the tokens of the current group according to groupList[i], $i \in [0,Len(groupList)]$

        • Perform the following computation based on the input parameters determined by the group:

          $C_{i} = (X_{i}\cdot W_{i} )\odot xScale_{i} \odot wScale_{i}$

          $C_{i,act}, gate_{i} = split(C_{i})$

          $S_{i}=Swish(C_{i,act})\odot gate_{i}$, where $Swish(x)=\frac{x}{1+e^{-x}}$

          where $xScale_{i}$ is the quantization factor corresponding to the token

        • Quantize the output result

          $Q\_scale_{i} = \frac{max(|S_{i}|)}{max(type)}$

          $Q_{i} = \lfloor \frac{S_{i}}{Q\_scale_{i}} \rceil$

    MX quantization scenario:
    • Definition:

      • ⋅ denotes matrix multiplication.
      • ⊙ indicates per-element multiplication.
    • Input:

      • $X∈\mathbb{Z_8}^{M \times K}$: activation matrix (left matrix), where M is the total number of tokens and K is the feature dimension.
      • $W∈\mathbb{Z_8}^{E \times K \times N}$: grouped weight matrix (right matrix), where E is the number of experts, K is the feature dimension, and N is the output dimension.
      • $w\_scale∈\mathbb{R}^{E \times ceil(K / 64) \times N \times 2}$: per-channel scaling factor of the grouped weight matrix (right matrix), where E is the number of experts, K is the feature dimension, and N is the output dimension.
      • $x\_scale∈\mathbb{R}^{M \times ceil(K / 64) \times 2}$: per-token scaling factor of the activation matrix (left matrix), where M is the total number of tokens and K is the feature dimension.
      • $grouplist∈\mathbb{N}^{E}$: group index list of cumsum or count.
    • Output:

      • $Q∈\mathbb{F}_{8E4M3FN}^{M \times N / 2}$ or $Q∈\mathbb{F}_{4E1M2}^{M \times N / 2}$ or $Q∈\mathbb{F}_{4E2M1}^{M \times N / 2}$: quantized output matrix. Supported data types: FLOAT8_E4M3FN, FLOAT4_E1M2, FLOAT4_E2M1.
      • $Q\_scale∈\mathbb{R}^{M \times ceil((N / 2) / 64) \times 2}$: Quantization scale factor.
    • Computation Process

      • Determine the tokens of the current group according to groupList[i], $i \in [0,Len(groupList)]$

      • Perform the following computation based on the input parameters determined by the group:

        $C_{i} = (X_{i}\cdot W_{i} )\odot xScale_{i\ Broadcast} \odot wScale_{i\ Broadcast}$

        $C_{i,act}, gate_{i} = split(C_{i})$

        $S_{i}=Swish(C_{i,act})\odot gate_{i}$, where $Swish(x)=\frac{x}{1+e^{-x}}$

      • Quantize the output result

        $shared\_exp = \left\lfloor \log_2(max_i(|S_i|)) \right\rceil - emax$

        $QScale = 2 ^ {shared\_exp}$

        $Q_i = quantize\_to\_element\_format(S_i/Qscale), \space i\space from\space 1\space to\space blocksize$

        • $emax$: The exponent of the maximum normal number of the corresponding data type.

          DataTypeemax
          FLOAT8_E4M3FN8
          FLOAT4_E1M21
          FLOAT4_E2M12
        • $blocksize$: The number of elements quantized each time. Only 32 is supported.

Function Prototype

Each operator uses a two-phase API. Call the "aclnnGroupedMatmulSwigluQuantWeightNzV2GetWorkspaceSize" API first to obtain the workspace size required for the computation and the executor that includes the computation process of the operator, and then call the "aclnnGroupedMatmulSwigluQuantWeightNzV2" API to execute the computation.

aclnnStatus aclnnGroupedMatmulSwigluQuantWeightNzV2GetWorkspaceSize(
    const aclTensor     *x,
    const aclTensorList *weight,
    const aclTensorList *weightScale,
    const aclTensorList *weightAssistMatrix,
    const aclTensor     *bias,
    const aclTensor     *xScale,
    const aclTensor     *smoothScale,
    const aclTensor     *groupList,
    int64_t              dequantMode,
    int64_t              dequantDtype,
    int64_t              quantMode,
    int64_t              groupListType,
    const aclIntArray   *tuningConfigOptional,
    aclTensor           *output,
    aclTensor           *outputScale,
    uint64_t            *workspaceSize,
    aclOpExecutor       **executor)
aclnnStatus aclnnGroupedMatmulSwigluQuantWeightNzV2(
    void          *workspace,
    uint64_t       workspaceSize,
    aclOpExecutor *executor,
    aclrtStream    stream)

aclnnGroupedMatmulSwigluQuantWeightNzV2GetWorkspaceSize

  • Parameters

    Parameter Input/Output Description Instruction Data Type Data Format Dim(shape) Non-contiguous Tensor
    x Input Represents the left matrix, corresponding to X in the formula. - INT8, INT42, INT322, FLOAT8_E4M3FN1, FLOAT4_E2M11, FLOAT4_E1M21, HIFLOAT81 ND 2 √
    weight Input Represents the weight matrix, corresponding to W in the formula. The weight of this API is forced to be treated as the FRACTAL_NZ format. INT8, INT42, INT322, FLOAT8_E4M3FN1, FLOAT4_E2M11, FLOAT4_E1M21, HIFLOAT81 FRACTAL_NZ 4, 5 √
    weightScale Input Represents the quantization factor of the right matrix, wScale in the formula. The first axis length must be equal to the first axis dimension of weight, and the trailing axis length must be the same as the trailing axis of weight restored to the ND format. UINT642, FLOAT, FLOAT16, BFLOAT16, FLOAT8_E8M01 ND 1, 2, 3, 4 √
    weightAssistMatrix Optional input Represents the auxiliary matrix for computing the matrix multiplication, which is weightAssistMatrix in the formula.
    • Takes effect only in the A8W4 scenario. Pass a null pointer in other scenarios.
    • The first axis length must be equal to the first axis dimension of weight, and the trailing axis length must be the same as the trailing axis of weight restored to the ND format.
    FLOAT ND 1, 2 √
    bias Optional input Represents the offset value of the matrix multiplication computation. Reserved input, not supported currently. A null pointer must be passed. - - - -
    xScale Input Represents the quantization factor of the left matrix, xScale in the formula. - FLOAT, FLOAT8_E8M01 ND 1, 3 √
    smoothScale Optional input Represents the smooth scaling factor.
    • Optional in the A4W4 quantization scenario; a null pointer must be passed in other scenarios.
    • In the A4W4 quantization scenario, the first axis length must be equal to the first axis dimension of weight.
    • In the A4W4 quantization scenario, a null pointer or two shapes are supported: (E, N / 2) or (E,).
    FLOAT ND 1, 2 √
    groupList Input Represents the distribution of matmul sizes for the input and output along the group axis. Corresponds to grouplist in the formula. The specific meaning is determined by groupListType.
    • The length must be equal to the first axis dimension of weight.
    • When groupListType is 0, the last value must not be greater than the first dimension of the tensor in x. When groupListType is 1, the sum of the values must not be greater than the first dimension of the tensor in x.
    • The values in groupList constrain the valid part of the output data. The part not specified by groupList will not participate in the update.
    INT64 ND 1 √
    dequantMode Input Represents the dequantization computation type, used to determine the dequantization method for the activation matrix and the weight matrix.
    • 0 indicates per-token for the activation matrix and per-channel for the weight matrix.
    • 1 indicates per-token for the activation matrix and per-group for the weight matrix.
    • 2 indicates MX quantization.
    INT64 - - -
    dequantDtype Input Represents the data type of the intermediate GroupedMatmul result.
    • 0 indicates FLOAT.
    • 1 indicates FLOAT16.
    • 27 indicates BFLOAT16.
    • 28 indicates UNDEFINED.
    INT64 - - -
    quantMode Input Represents the quantization computation type, used to determine the quantization mode of the swiglu result.
    • 0 indicates per-token.
    • 2 indicates MX quantization.
    INT64 - - -
    groupListType Input Represents the interpretation mode of the grouping, used to determine the semantics of groupList.
    • 0 indicates the cumsum mode, and each element in groupList represents the cumulative length of the current group.
    • 1 indicates the count mode, and each element in groupList represents the number of elements contained in the group.
    INT64 - - -
    tuningConfigOptional Optional input Used by the operator to estimate the M/E size and select different operator templates to meet performance requirements of different use cases. Array. The first number passed in indicates the expected number of tokens processed by each expert, used for tiling optimization. Enabled for A4W4 right matrix NZ input. Pass a null pointer for other inputs. INT64 - - -
    Output Output Represents the quantized output, which is Q in the formula. - INT8, FLOAT8_E4M3FN1, FLOAT4_E1M21, FLOAT4_E2M11 ND 2 √
    outputScale Output Represents the quantization factor of the output, QScale in the formula. - FLOAT, FLOAT8_E8M01 ND 1, 3 √
    workspaceSize Output Returns the side-requested workspace size on the Device side. - - - - -
    executor Output Returns the op executor, which includes the computation flow of the operator. - - - - -
    • Atlas A3 products, Atlas A2 products:
      • weight is forcibly treated as FRACTAL_NZ format.
      • The superscript "1" in the data type column of the table above indicates data types not supported by this series.
      • weight supports transposition in the A4W4 scenario, while other inputs support only the non-transposed format. INT32 is used for adaptation in A8W4 and A4W4 scenarios, where one INT32 is interpreted as eight INT4 data values in practice. The A8W8 scenario does not support the ND data format.
      • The dequantMode parameter is supported: values 0 and 1 are supported in A8W4 and A4W4 scenarios, while only value 0 is supported in the A8W8 scenario.
      • The dequantDtype and quantMode parameters are not supported.
      • x and weight do not support empty tensors.
      • When weight is input in NZ transpose format, only single-tensor mode is supported.
      • weight, weightScale, and weightAssistMatrix support single-tensor scenarios (tensorlist length is 1) and multi-tensor scenarios (tensorlist length is greater than 1).
    • Ascend 950PR&950DT products:
      • weight is forcibly treated as FRACTAL_NZ format.
      • The superscript "2" in the data type column of the table above indicates data types not supported by this series.
      • Supports the dequantMode parameter: supported values are 0 for the A8W8 Pertoken quantization scenario and 2 for the MX quantization scenario.
      • The dequantDtype parameter is supported: the A8W8 Pertoken quantization scenario supports values 0, 1, and 27, and the MX quantization scenario supports value 0.
      • The quantMode parameter is supported: the A8W8 Pertoken quantization scenario supports value 0, and the MX quantization scenario supports value 2.
      • Only the same value for dequantMode and quantMode is supported.
      • x and xScale support empty tensors with M equal to 0.
      • weight and weightScale support empty tensors with N equal to 0.
      • In MXFP4 and MXFP8 scenarios, weight and weightScale support both the single-Tensor scenario (the tensorlist length must be 1) and the multi-Tensor scenario (the tensorlist length is greater than or equal to 1). In the multi-Tensor scenario, the tensorlist length must equal E, and E can be 1. The shapes of weight and weightScale must be flattened along the E dimension. For example, {(E, K, N)} must be changed to {E x (K, N)}.
      • In the MXA8W4 scenario, weight and weightScale support the multi-Tensor scenario (the tensorlist length is greater than or equal to 1).
      • The MXFP4 scenario supports weight in NZ format. x and weight can be FLOAT4_E2M1 or FLOAT4_E1M2 (cross combinations are supported), and output can be FLOAT8_E4M3FN, FLOAT4_E1M2, or FLOAT4_E2M1.
      • In the A8W8 Pertoken quantization scenario, weight and weightScale support only the single-Tensor scenario, that is, the tensorlist length is 1.
  • Return Value

    aclnnStatus: Return status code. For details, see aclnnReturnCode.

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

    Return Values Error Codes Description
    ACLNN_ERR_PARAM_NULLPTR 161001 Parameters x, weight, weightScale, xScale, groupList, output, and outputScale are null pointers.
    ACLNN_ERR_PARAM_INVALID 161002 The dimensions of the input x, weight, weightScale, xScale, groupList, output, or outputScale do not satisfy the constraint.
    The shapes of the input x, weight, weightScale, xScale, groupList, output, or outputScale do not satisfy the constraints.
    The formats of the input x, weight, weightScale, xScale, groupList, output, or outputScale do not satisfy the constraints.
    In use cases that do not support multiple tensors, the tensor list length of the input weight or weightScale is not 1; or in multiple-tensor use cases, the tensor list lengths of weight and weightScale are inconsistent.
    The input x and xScale are empty tensors, and the input weight and weightScale are empty tensor lists.
    The number of elements in groupList is greater than the length of the first axis of weight.
    The input dequantMode, quantMode, or dequantDtype does not satisfy the constraints.
    The input bias, weightAssistMatrix, smoothScale, or tuningConfigOptional does not satisfy the constraints.

aclnnGroupedMatmulSwigluQuantWeightNzV2

  • Parameters

    ParameterInput/OutputDescription
    workspaceInputThe side-requested workspace memory address on the Device.
    workspaceSizeInputThe side-requested workspace size on the Device, obtained by the first-phase API aclnnGroupedMatmulSwigluQuantWeightNzV2GetWorkspaceSize.
    executorInputOp executor, which includes the computation flow of the operator.
    streamInputSpecifies the Stream for task execution.
  • Return Value

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

Constraints

  • Deterministic computation:
    • aclnnGroupedMatmulSwigluQuantWeightNzV2 defaults to a deterministic implementation.
  • Atlas A3 products, Atlas A2 products:
    • The following constraint conditions must be met in A8W8/A8W4/A4W4 quantization scenarios:
      • The data types must meet the requirements in the following table:

        Quantization scenario x weight weightScale weightAssistMatrix xScale smoothScale output outputScale
        A8W8 INT8 INT8 FLOAT, FLOAT16, BFLOAT16 nullptr FLOAT nullptr INT8 FLOAT
        A8W4 INT8 INT4, INT32 UINT64 FLOAT FLOAT nullptr INT8 FLOAT
        A4W4 INT4, INT32 INT4, INT32 UINT64 nullptr FLOAT nullptr/FLOAT INT8 FLOAT
      • The shape constraints must meet the following table:

        Quantization scenario x weight weightScale weightAssistMatrix xScale smoothScale output outputScale
        A8W8 (M, K) The shape in NZ format is {(E, N / 32, K / 16, 16, 32)}. {(E, N)} nullptr (M,) nullptr (M, N / 2) (M,)
        A8W4 (M, K)
        • For ND format, the shape is {(E, K, N)}.
        • For NZ format with INT4, the shape is {(E, N / 64, K / 16, 16, 64)}.
        • For NZ format with INT32, the shape is {(E, N / 64, K / 16, 16, 8)}.
        • For the per-channel scenario, the shape is {(E, N)}.
        • For the per-group scenario, the shape is {(E, K_group_num, N)}.
        {(E, N)} (M,) nullptr (M, N / 2) (M,)
        A4W4 (M, K)
        • The ND format shape is {(E, K, N)}.
        • A4W4 supports non-transposed and transposed NZ.
        • For NZ non-transposed format with INT4, the shape is {(E, N / 64, K / 16, 16, 64)}.
        • For NZ non-transposed format with INT32, the shape is {(E, N / 64, K / 16, 16, 8)}.
        • For NZ transposed format with INT4, the original shape is {(E, K / 64, N / 16, 16, 64)}, and the tensor is passed in after calling transpose(-1,-2).
        • For NZ transposed format with INT32, the original shape is {(E, K / 64, N / 16, 16, 8)}, and the tensor is passed in after calling transpose(-1,-2).
        • For NZ transposed input, align K/K_group_num of per-group to 64.
        • For the per-channel scenario, the shape is {(E, N)}.
        • For the per-group scenario, the shape is {(E, K_group_num, N)}.
        nullptr (M,)
        • nullptr
        • (E,)
        • (E, N / 2)
        (M, N / 2) (M,)
      • In the A8W8 scenario, the N axis length cannot exceed 10240, and the last axis length of x cannot be greater than or equal to 65536.

      • In the A4W4 scenario, the N axis length cannot exceed 10240, and the last axis length of x cannot be greater than or equal to 20000.

      • In the A4W4 scenario, the N-axis length cannot exceed 10240, and the last axis length of x cannot be greater than or equal to 20000.

      • In the multi-tensor scenario, that is, when the tensorlist length is greater than 1, the shapes of weight, weightScale, and weightAssistMatrix need to be flattened along the E dimension. For example, {(E, K, N)} needs to become {E instances of (K, N)}.

  • Ascend 950PR&950DT products:
    • The first dimension of groupList supports a maximum of 1024, i.e., a maximum of 1024 groups.
    • The A8W8 Pertoken quantization scenario must meet the following constraints:
      • x, weight, and output support the following data type combinations; the data types of xScale and outputScale are both FLOAT:
        • INT8: x, weight, and output are all INT8; weightScale supports FLOAT, FLOAT16, or BFLOAT16.
        • HIFLOAT8: x, weight, and output are all HIFLOAT8; weightScale supports FLOAT or BFLOAT16.
        • FP8: x and weight support FLOAT8_E4M3FN; weightScale supports FLOAT or BFLOAT16; output supports FLOAT8_E4M3FN and FLOAT_E5M2.
      • The shape of x is (M, K). weight supports non-transposed and transposed input, with the entry view shape (E, K, N) for both: non-transposed input uses a contiguous stride, with FRACTAL_NZ storage shape (E, ceil(N/32), ceil(K/16), 16, 32); transposed input uses the stride (K * N, 1, K), with FRACTAL_NZ storage shape (E, ceil(K/32), ceil(N/16), 16, 32).
      • x supports only non-transposed input. When weight is transposed input, the internal transposeWeight attribute of the operator is true, and the ZN template is used to process FRACTAL_NZ data.
      • The shape of weightScale is (E, N), the shapes of xScale and outputScale are (M,), and the shape of output is (M, N/2).
      • Both dequantMode and quantMode must be 0; dequantDtype supports 0, 1, and 27.
      • N must be greater than 0 and an integer multiple of 64.
      • The tensorlist lengths of weight and weightScale must both be 1.
    • The following constraints must be met in the MX quantization scenario:
      • The data types must meet the requirements in the following table:

        MX quantization scenario x weight weightScale xScale output outputScale
        MXFP8 FLOAT8_E4M3FN FLOAT8_E4M3FN FLOAT8_E8M0 FLOAT8_E8M0 FLOAT8_E4M3FN FLOAT8_E8M0
        MXFP4 FLOAT4_E2M1, FLOAT4_E1M2 FLOAT4_E2M1, FLOAT4_E1M2 FLOAT8_E8M0 FLOAT8_E8M0 FLOAT4_E2M1, FLOAT8_E4M3FN, FLOAT4_E1M2 FLOAT8_E8M0
        MXA8W4 FLOAT8_E4M3FN FLOAT4_E2M1 FLOAT8_E8M0 FLOAT8_E8M0 FLOAT8_E4M3FN FLOAT8_E8M0
      • The shape constraints must meet the following table:

        MX quantization scenario x weight weightScale xScale output outputScale
        MXFP8 (M, K)
        • Non-transposed shape: {(E, ceil(N / 32), ceil(K / 16), 16, 32)}.
        • Multi-tensor non-transposed shape: {E tensors of (ceil(N / 32), ceil(K / 16), 16, 32)}, viewShape: {E tensors of (K, N)}.
        • Transposed shape: {(E, ceil(K / 32), ceil(N / 16), 16, 32)}.
        • Non-transposed shape: {(E, ceil(K / 64), N, 2)}.
        • Multi-tensor non-transposed shape: {E × (ceil(K / 64), N, 2)}.
        • Transposed shape: {(E, N, ceil(K / 64), 2)}.
        (M, ceil(K / 64), 2) (M, N / 2) (M, ceil((N / 2) / 64), 2)
        MXFP4 (M, K)
        • Non-transposed shape: {(E, ceil(N / 64), ceil(K / 16), 16, 64)}.
        • Multi-tensor non-transposed shape: {E × (ceil(N / 64), ceil(K / 16), 16, 64)}; viewShape: {E × (K, N)}.
        • Transposed shape: {(E, ceil(K / 64), ceil(N / 16), 16, 64)}.
        • Non-transposed shape: {(E, ceil(K / 64), N, 2)}.
        • Multi-tensor non-transposed shape: {E × (ceil(K / 64), N, 2)}.
        • Transposed shape: {(E, N, ceil(K / 64), 2)}.
        (M, ceil(K / 64), 2) (M, N / 2) (M, ceil((N / 2) / 64), 2)
        MXA8W4 (M, K)
        • The transposed shape is in the form of {(E, K/32, N/16, 16, 32)}.
        • The transposed shape is in the form of {(E, N, ceil(K / 64), 2)}.
        (M, ceil(K / 64), 2) (M, N / 2) (M, ceil((N / 2) / 64), 2)
        MXA8W4 (multiple tensors) (M, K)
        • The transposed view shape is in the form of {E × (N, K)}, and the storage shape is in the form of {E × (N/32, K/16, 16, 32)}.
        • The transposed view shape is in the form of {E × (N, ceil(K / 64), 2)}.
        (M, ceil(K / 64), 2) (M, N / 2) (M, ceil((N / 2) / 64), 2)
      • The transpose attribute of weightScale must be consistent with that of weight.

      • In the MX quantization scenario, when the input is of the fp8 data type, N must be 64-aligned; when the input is of the fp4 data type, N must be 128-aligned.

      • For MXFP4 and MXFP8 multi-Tensor use cases, the tensorlist length of weight and weightScale must be equal to E.

      • For MXFP4, K must be greater than 2.

      • For MXFP4, the inner axes of both the left and right matrices must be even.

      • For MXFP4, the last two axes of weight must not be 1.

      • In the MXA8W4 scenario, the following constraints must be met:

        • K % 32 == 0, indicating that the K axis must be aligned to 32.
        • K >= 32.
        • N >= 128.
        • Only transposed weight is supported. The user needs to construct weight data in transposed NZ format and transpose it to the non-transposed state before the call.
        • In the multi-tensor case (tensorlist length is greater than or equal to 1), the number of tensors in weight and weightScale must be the same.
        • In the multi-tensor case, K and N of each tensor in weight must be equal.
        • In the multi-tensor case, the view shape of each tensor in weight is 2D, and the view shape of each tensor in weightScale is 3D; in the single-tensor case, the view shape of each tensor in weight is 3D, and the view shape of each tensor in weightScale is 4D.
        • In the multi-tensor case, the shape of each weightScale tensor must match that of the corresponding weight tensor.
        • In the multi-tensor case, the length of groupList must be equal to the number of tensors in weight/weightScale.

Example

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

  • Atlas A3 products and Atlas A2 products:

    #include <iostream>
    #include <vector>
    #include "acl/acl.h"
    #include "aclnnop/aclnn_grouped_matmul_swiglu_quant_weight_nz_v2.h"
    
    #define CHECK_RET(cond, return_expr)                                                                                   \
        do {                                                                                                               \
            if (!(cond)) {                                                                                                 \
                return_expr;                                                                                               \
            }                                                                                                              \
        } while (0)
    
    #define LOG_PRINT(message, ...)                                                                                        \
        do {                                                                                                               \
            printf(message, ##__VA_ARGS__);                                                                                \
        } while (0)
    
    int64_t GetShapeSize(const std::vector<int64_t>& shape) {
        int64_t 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, aclFormat formatType, 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 host data to the 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, formatType,
                                shape.data(), shape.size(), *deviceAddr);
        return 0;
    }
    
    template <typename T>
    int CreateAclTensorList(const std::vector<T> &hostData, const std::vector<std::vector<int64_t>> &shapes,
                            void **deviceAddr, aclDataType dataType, aclFormat formatType, aclTensorList **tensor) {
        int size = shapes.size();
        aclTensor* tensors[size];
        for (int i = 0; i < size; i++) {
            int ret = CreateAclTensor<T>(hostData, shapes[i], deviceAddr + i, dataType, formatType, tensors + i);
            CHECK_RET(ret == ACL_SUCCESS, return ret);
        }
        *tensor = aclCreateTensorList(tensors, size);
        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 = Init(deviceId, &stream);
        // Handle the check as required.
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
    
        // 2. Construct inputs and outputs as required by the API.
        int64_t E = 4;
        int64_t M = 8192;
        int64_t N = 4096;
        int64_t K = 7168;
        std::vector<int64_t> xShape = {M, K};
        std::vector<std::vector<int64_t>> weightShape = {{E, N / 32 , K / 16, 16, 32}};
        std::vector<std::vector<int64_t>> weightScaleShape = {{E, N}};
        std::vector<int64_t> xScaleShape = {M};
        std::vector<int64_t> groupListShape = {E};
        std::vector<int64_t> outputShape = {M, N / 2};
        std::vector<int64_t> outputScaleShape = {M};
    
        void* xDeviceAddr = nullptr;
        void* weightDeviceAddr[1];
        void* weightScaleDeviceAddr[1];
        void* xScaleDeviceAddr = nullptr;
        void* groupListDeviceAddr = nullptr;
        void* outputDeviceAddr = nullptr;
        void* outputScaleDeviceAddr = nullptr;
    
        aclTensor* x = nullptr;
        aclTensorList* weight = nullptr;
        aclTensorList* weightScale = nullptr;
        aclTensor* xScale = nullptr;
        aclTensor* groupList = nullptr;
        aclTensor* output = nullptr;
        aclTensor* outputScale = nullptr;
    
        std::vector<int8_t> xHostData(M * K, 1);
        std::vector<int8_t> weightHostData(E * N * K, 1);
        std::vector<float> weightScaleHostData(E * N, 0.5f);
        std::vector<float> xScaleHostData(M, 0.0314f);
        std::vector<int64_t> groupListHostData = {1, 2, 2, 3};
        std::vector<int8_t> outputHostData(M * N / 2, 0);
        std::vector<float> outputScaleHostData(M, 0);
    
        // Create an x aclTensor.
        ret = CreateAclTensor(xHostData, xShape, &xDeviceAddr, aclDataType::ACL_INT8, aclFormat::ACL_FORMAT_ND, &x);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
        // Create a weight aclTensorList.
        ret = CreateAclTensorList(weightHostData, weightShape, weightDeviceAddr, aclDataType::ACL_INT8, aclFormat::ACL_FORMAT_FRACTAL_NZ, &weight);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
        // Create weightScale aclTensorList.
        ret = CreateAclTensorList(weightScaleHostData, weightScaleShape, weightScaleDeviceAddr, aclDataType::ACL_FLOAT,  aclFormat::ACL_FORMAT_ND, &weightScale);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
        // Create xScale aclTensor.
        ret = CreateAclTensor(xScaleHostData, xScaleShape, &xScaleDeviceAddr, aclDataType::ACL_FLOAT, aclFormat::ACL_FORMAT_ND, &xScale);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
        // Create groupList aclTensor.
        ret = CreateAclTensor(groupListHostData, groupListShape, &groupListDeviceAddr, aclDataType::ACL_INT64, aclFormat::ACL_FORMAT_ND, &groupList);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
        // Create output aclTensor.
        ret = CreateAclTensor(outputHostData, outputShape, &outputDeviceAddr, aclDataType::ACL_INT8, aclFormat::ACL_FORMAT_ND, &output);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
        // Create outputScale aclTensor.
        ret = CreateAclTensor(outputScaleHostData, outputScaleShape, &outputScaleDeviceAddr, aclDataType::ACL_FLOAT, aclFormat::ACL_FORMAT_ND, &outputScale);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
    
        // New V2 parameters.
        aclTensorList* weightAssistMatrix = nullptr;
        aclTensor* bias = nullptr;
        aclTensor* smoothScale = nullptr;
        int64_t dequantMode = 0;
        int64_t dequantDtype = 28;
        int64_t quantMode = 0;
        int64_t groupListType = 0;
    
        std::vector<int64_t> tuningConfigData;
        aclIntArray* tuningConfig = aclCreateIntArray(tuningConfigData.data(), 0);
    
        uint64_t workspaceSize = 0;
        aclOpExecutor* executor;
    
        // 3. Call the CANN operator library API.
        // Call the aclnnGroupedMatmulSwigluQuantWeightNzV2 first-phase API.
        ret = aclnnGroupedMatmulSwigluQuantWeightNzV2GetWorkspaceSize(
            x, weight, weightScale, weightAssistMatrix, bias, xScale, smoothScale, groupList, dequantMode, dequantDtype,
            quantMode, groupListType, tuningConfig, output, outputScale, &workspaceSize, &executor);
        CHECK_RET(ret == ACL_SUCCESS,
        LOG_PRINT("aclnnGroupedMatmulSwigluQuantWeightNzV2GetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
        // Allocate device memory based on the workspaceSize computed by the first-phase API.
        void* workspaceAddr = nullptr;
        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);
        }
        // Call the aclnnGroupedMatmulSwigluQuantWeightNzV2 second-phase API.
        ret = aclnnGroupedMatmulSwigluQuantWeightNzV2(workspaceAddr, workspaceSize, executor, stream);
        CHECK_RET(ret == ACL_SUCCESS,
        LOG_PRINT("aclnnGroupedMatmulSwigluQuantWeightNzV2 failed. ERROR: %d\n", ret); return ret);
    
        // 4. (Boilerplate) Synchronize and 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, copy the result from the Device side to the Host side, and modify them according to the specific API definition.
        auto size = 10;
        std::vector<int8_t> out1Data(size, 0);
        ret = aclrtMemcpy(out1Data.data(), out1Data.size() * sizeof(out1Data[0]), outputDeviceAddr,
                            size * sizeof(out1Data[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 j = 0; j < size; j++) {
            LOG_PRINT("result[%ld] is: %d\n", j, out1Data[j]);
        }
        std::vector<float> out2Data(size, 0);
        ret = aclrtMemcpy(out2Data.data(), out2Data.size() * sizeof(out2Data[0]), outputScaleDeviceAddr,
                            size * sizeof(out2Data[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 j = 0; j < size; j++) {
            LOG_PRINT("result[%ld] is: %f\n", j, out2Data[j]);
        }
        // 6. Release aclTensor, aclTensorList, and aclScalar, and modify them according to the specific API definition.
        aclDestroyTensor(x);
        aclDestroyTensorList(weight);
        aclDestroyTensorList(weightScale);
        aclDestroyTensor(xScale);
        aclDestroyTensor(groupList);
        aclDestroyTensor(output);
        aclDestroyTensor(outputScale);
    
        aclDestroyIntArray(tuningConfig);
    
        // 7. Release device resources, and modify them according to the specific API definition.
        aclrtFree(xDeviceAddr);
        for (int64_t i = 0; i < 1; i++) {
            aclrtFree(weightDeviceAddr[i]);
            aclrtFree(weightScaleDeviceAddr[i]);
        }
        aclrtFree(xScaleDeviceAddr);
        aclrtFree(groupListDeviceAddr);
        aclrtFree(outputDeviceAddr);
        aclrtFree(outputScaleDeviceAddr);
        if (workspaceSize > 0) {
            aclrtFree(workspaceAddr);
        }
        aclrtDestroyStream(stream);
        aclrtResetDevice(deviceId);
        aclFinalize();
        return 0;
    }
  • Ascend 950PR&950DT products:

    #include <iostream>
    #include <memory>
    #include <vector>
    
    #include "acl/acl.h"
    #include "aclnnop/aclnn_grouped_matmul_swiglu_quant_weight_nz_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)
    {
        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, aclFormat formatType, aclTensor **tensor)
    {
        auto size = GetShapeSize(shape) * sizeof(T);
        auto ret = aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMalloc failed. ERROR: %d\n", ret); return ret);
    
        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);
    
        std::vector<int64_t> strides(shape.size(), 1);
        for (int64_t i = shape.size() - 2; i >= 0; i--) {
            strides[i] = shape[i + 1] * strides[i + 1];
        }
    
        *tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, formatType,
                                  shape.data(), shape.size(), *deviceAddr);
        return 0;
    }
    
    template <typename T>
    int CreateAclTensorList(const std::vector<std::vector<T>> &hostData,
                            const std::vector<std::vector<int64_t>> &shapes,
                            void **deviceAddr, aclDataType dataType, aclFormat formatType, aclTensorList **tensor)
    {
        int size = shapes.size();
        aclTensor *tensors[size];
        for (int i = 0; i < size; i++) {
            int ret = CreateAclTensor<T>(hostData[i], shapes[i], deviceAddr + i, dataType, formatType, tensors + i);
            CHECK_RET(ret == ACL_SUCCESS, return ret);
        }
        *tensor = aclCreateTensorList(tensors, size);
        return ACL_SUCCESS;
    }
    
    template <typename T>
    int CreateAclTensorListNZTranspose(const std::vector<std::vector<T>> &hostData,
                                       const std::vector<std::vector<int64_t>> &viewShapes,
                                       const std::vector<std::vector<int64_t>> &storageShapes,
                                       const std::vector<std::vector<int64_t>> &transposeStrides,
                                       void **deviceAddr, aclDataType dataType, aclTensorList **tensor)
    {
        int size = storageShapes.size();
        aclTensor *tensors[size];
        for (int i = 0; i < size; i++) {
            auto storageSize = GetShapeSize(storageShapes[i]) * sizeof(T);
            auto ret = aclrtMalloc(deviceAddr + i, storageSize, ACL_MEM_MALLOC_HUGE_FIRST);
            CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMalloc failed. ERROR: %d\n", ret); return ret);
    
            ret = aclrtMemcpy(*(deviceAddr + i), storageSize, hostData[i].data(), storageSize, ACL_MEMCPY_HOST_TO_DEVICE);
            CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMemcpy failed. ERROR: %d\n", ret); return ret);
    
            tensors[i] = aclCreateTensor(viewShapes[i].data(), viewShapes[i].size(), dataType,
                                         transposeStrides[i].data(), 0, aclFormat::ACL_FORMAT_FRACTAL_NZ,
                                         storageShapes[i].data(), storageShapes[i].size(), *(deviceAddr + i));
        }
        *tensor = aclCreateTensorList(tensors, size);
        return ACL_SUCCESS;
    }
    
    template <typename T>
    int CreateAclTensorListTranspose(const std::vector<std::vector<T>> &hostData,
                                     const std::vector<std::vector<int64_t>> &viewShapes,
                                     const std::vector<std::vector<int64_t>> &transposeStrides,
                                     void **deviceAddr, aclDataType dataType, aclFormat formatType, aclTensorList **tensor)
    {
        int size = viewShapes.size();
        aclTensor *tensors[size];
        for (int i = 0; i < size; i++) {
            auto storageSize = GetShapeSize(viewShapes[i]) * sizeof(T);
            auto ret = aclrtMalloc(deviceAddr + i, storageSize, ACL_MEM_MALLOC_HUGE_FIRST);
            CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMalloc failed. ERROR: %d\n", ret); return ret);
    
            ret = aclrtMemcpy(*(deviceAddr + i), storageSize, hostData[i].data(), storageSize, ACL_MEMCPY_HOST_TO_DEVICE);
            CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMemcpy failed. ERROR: %d\n", ret); return ret);
    
            tensors[i] = aclCreateTensor(viewShapes[i].data(), viewShapes[i].size(), dataType,
                                         transposeStrides[i].data(), 0, formatType, viewShapes[i].data(),
                                         viewShapes[i].size(), *(deviceAddr + i));
        }
        *tensor = aclCreateTensorList(tensors, size);
        return ACL_SUCCESS;
    }
    
    template <typename T1, typename T2>
    auto CeilDiv(T1 a, T2 b) -> T1
    {
        if (b == 0) {
            return a;
        }
        return (a + b - 1) / b;
    }
    
    void Finalize(int32_t deviceId, aclrtStream stream)
    {
        aclrtDestroyStream(stream);
        aclrtResetDevice(deviceId);
        aclFinalize();
    }
    
    int aclnnGroupedMatmulSwigluQuantWeightNzV2MxA8W4Test(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);
    
        int64_t E = 8;
        int64_t M = 2048;
        int64_t N = 4096;
        int64_t K = 4096;
    
        int64_t kScaleBlock = CeilDiv(K, 64);
    
        std::vector<int64_t> xShape = {M, K};
        std::vector<std::vector<int64_t>> weightViewShapes = {{E, K, N}};
        std::vector<std::vector<int64_t>> weightStorageShapes = {{E, K / 32, N / 16, 16, 32}};
        std::vector<std::vector<int64_t>> weightTransposeStrides = {{K * N, 1, K}};
        std::vector<std::vector<int64_t>> weightScaleViewShapes = {{E, kScaleBlock, N, 2}};
        std::vector<std::vector<int64_t>> weightScaleTransposeStrides = {{kScaleBlock * N * 2, 2, kScaleBlock * 2, 1}};
        std::vector<int64_t> xScaleShape = {M, CeilDiv(K, 64), 2};
        std::vector<int64_t> groupListShape = {E};
        std::vector<int64_t> outputShape = {M, N / 2};
        std::vector<int64_t> outputScaleShape = {M, CeilDiv((N / 2), 64), 2};
    
        void *xDeviceAddr = nullptr;
        void *weightDeviceAddr = nullptr;
        void *weightScaleDeviceAddr = nullptr;
        void *xScaleDeviceAddr = nullptr;
        void *groupListDeviceAddr = nullptr;
        void *outputDeviceAddr = nullptr;
        void *outputScaleDeviceAddr = nullptr;
    
        aclTensor *x = nullptr;
        aclTensorList *weight = nullptr;
        aclTensorList *weightScale = nullptr;
        aclTensor *xScale = nullptr;
        aclTensor *groupList = nullptr;
        aclTensor *output = nullptr;
        aclTensor *outputScale = nullptr;
    
        std::vector<uint8_t> xHostData(M * K, 1);
        std::vector<uint8_t> weightHostData(E * (K / 32) * (N / 16) * 16 * 32, 1);
        std::vector<uint8_t> weightScaleHostData(E * kScaleBlock * N * 2, 1);
        std::vector<uint8_t> xScaleHostData(M * CeilDiv(K, 64) * 2, 1);
        std::vector<int64_t> groupListHostData(E, 256);
        std::vector<uint8_t> outputHostData(M * N / 2, 1);
        std::vector<uint8_t> outputScaleHostData(M * CeilDiv((N / 2), 64) * 2, 1);
    
        aclIntArray *tuningConfig = nullptr;
    
        int64_t quantMode = 2;
        int64_t dequantMode = 2;
        int64_t dequantDtype = 0;
        int64_t groupListType = 1;
    
        ret = CreateAclTensor<uint8_t>(xHostData, xShape, &xDeviceAddr, aclDataType::ACL_FLOAT8_E4M3FN,
                                       aclFormat::ACL_FORMAT_ND, &x);
        std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor *)> xTensorPtr(x, aclDestroyTensor);
        std::unique_ptr<void, aclError (*)(void *)> xDeviceAddrPtr(xDeviceAddr, aclrtFree);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
    
        std::vector<std::vector<uint8_t>> weightHostDataList = {weightHostData};
        ret = CreateAclTensorListNZTranspose<uint8_t>(weightHostDataList, weightViewShapes, weightStorageShapes,
                                                      weightTransposeStrides, &weightDeviceAddr,
                                                      aclDataType::ACL_FLOAT4_E2M1, &weight);
        std::unique_ptr<aclTensorList, aclnnStatus (*)(const aclTensorList *)> weightTensorListPtr(weight,
                                                                                                   aclDestroyTensorList);
        std::unique_ptr<void, aclError (*)(void *)> weightDeviceAddrPtr(weightDeviceAddr, aclrtFree);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
    
        std::vector<std::vector<uint8_t>> weightScaleHostDataList = {weightScaleHostData};
        ret = CreateAclTensorListTranspose<uint8_t>(weightScaleHostDataList, weightScaleViewShapes,
                                                    weightScaleTransposeStrides, &weightScaleDeviceAddr,
                                                    aclDataType::ACL_FLOAT8_E8M0, aclFormat::ACL_FORMAT_ND, &weightScale);
        std::unique_ptr<aclTensorList, aclnnStatus (*)(const aclTensorList *)> weightScaleTensorListPtr(
            weightScale, aclDestroyTensorList);
        std::unique_ptr<void, aclError (*)(void *)> weightScaleDeviceAddrPtr(weightScaleDeviceAddr, aclrtFree);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
    
        ret = CreateAclTensor<uint8_t>(xScaleHostData, xScaleShape, &xScaleDeviceAddr, aclDataType::ACL_FLOAT8_E8M0,
                                       aclFormat::ACL_FORMAT_ND, &xScale);
        std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor *)> xScaleTensorPtr(xScale, aclDestroyTensor);
        std::unique_ptr<void, aclError (*)(void *)> xScaleDeviceAddrPtr(xScaleDeviceAddr, aclrtFree);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
    
        ret = CreateAclTensor<int64_t>(groupListHostData, groupListShape, &groupListDeviceAddr, aclDataType::ACL_INT64,
                                       aclFormat::ACL_FORMAT_ND, &groupList);
        std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor *)> groupListTensorPtr(groupList, aclDestroyTensor);
        std::unique_ptr<void, aclError (*)(void *)> groupListDeviceAddrPtr(groupListDeviceAddr, aclrtFree);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
    
        ret = CreateAclTensor<uint8_t>(outputHostData, outputShape, &outputDeviceAddr, aclDataType::ACL_FLOAT8_E4M3FN,
                                       aclFormat::ACL_FORMAT_ND, &output);
        std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor *)> outputTensorPtr(output, aclDestroyTensor);
        std::unique_ptr<void, aclError (*)(void *)> outputDeviceAddrPtr(outputDeviceAddr, aclrtFree);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
    
        ret = CreateAclTensor<uint8_t>(outputScaleHostData, outputScaleShape, &outputScaleDeviceAddr,
                                       aclDataType::ACL_FLOAT8_E8M0, aclFormat::ACL_FORMAT_ND, &outputScale);
        std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor *)> outputScaleTensorPtr(outputScale, aclDestroyTensor);
        std::unique_ptr<void, aclError (*)(void *)> outputScaleDeviceAddrPtr(outputScaleDeviceAddr, aclrtFree);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
    
        uint64_t workspaceSize = 0;
        aclOpExecutor *executor;
        void *workspaceAddr = nullptr;
    
        ret = aclnnGroupedMatmulSwigluQuantWeightNzV2GetWorkspaceSize(
            x, weight, weightScale, nullptr, nullptr, xScale, nullptr, groupList, dequantMode, dequantDtype, quantMode,
            groupListType, tuningConfig, output, outputScale, &workspaceSize, &executor);
        CHECK_RET(ret == ACL_SUCCESS,
                  LOG_PRINT("aclnnGroupedMatmulSwigluQuantWeightNzV2GetWorkspaceSize failed. ERROR: %d\n", ret);
                  return ret);
        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);
        }
        ret = aclnnGroupedMatmulSwigluQuantWeightNzV2(workspaceAddr, workspaceSize, executor, stream);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnGroupedMatmulSwigluQuantWeightNzV2 failed. ERROR: %d\n", ret);
                  return ret);
    
        ret = aclrtSynchronizeStream(stream);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);
    
        auto size = GetShapeSize(outputShape);
        std::vector<uint8_t> outputData(size, 0);
        ret = aclrtMemcpy(outputData.data(), size * sizeof(outputData[0]), outputDeviceAddr, size * sizeof(outputData[0]),
                          ACL_MEMCPY_DEVICE_TO_HOST);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy outputData from device to host failed. ERROR: %d\n", ret);
                  return ret);
        for (int64_t j = 0; j < size; j++) {
            LOG_PRINT("result[%ld] is: %d\n", j, outputData[j]);
        }
    
        size = GetShapeSize(outputScaleShape);
        std::vector<uint8_t> outputScaleData(size, 0);
        ret = aclrtMemcpy(outputScaleData.data(), size * sizeof(outputScaleData[0]), outputScaleDeviceAddr,
                          size * sizeof(outputScaleData[0]), ACL_MEMCPY_DEVICE_TO_HOST);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy outputScaleData from device to host failed. ERROR: %d\n", ret);
                  return ret);
        for (int64_t j = 0; j < size; j++) {
            LOG_PRINT("result[%ld] is: %d\n", j, outputScaleData[j]);
        }
    
        return ACL_SUCCESS;
    }
    
    int main()
    {
        int32_t deviceId = 0;
        aclrtStream stream;
        auto ret = aclnnGroupedMatmulSwigluQuantWeightNzV2MxA8W4Test(deviceId, stream);
        CHECK_FREE_RET(ret == ACL_SUCCESS,
                       LOG_PRINT("aclnnGroupedMatmulSwigluQuantWeightNzV2MxA8W4Test failed. ERROR: %d\n", ret);
                       return ret);
    
        Finalize(deviceId, stream);
        return 0;
    }