[object Object]

[object Object][object Object]undefined
[object Object]
  • Description: Fuses [object Object], [object Object], [object Object], and [object Object]. For details, see the formulas. This API is the weightNZ specialization version of .

  • Formulas:

    [object Object]
    • Definition

      • indicates matrix multiplication.
      • indicates element-wise multiplication.
      • x\left \lfloor x\right \rceil indicates rounding [object Object] to the nearest integer.
      • Z8={xZ128x127}\mathbb{Z_8} = \{ x \in \mathbb{Z} | −128≤x≤127 \}
      • Z32={xZ2147483648x2147483647}\mathbb{Z_{32}} = \{ x \in \mathbb{Z} | -2147483648≤x≤2147483647 \}
    • Input

      • XZ8M×KX∈\mathbb{Z_8}^{M \times K}: activation matrix (left matrix), where MM indicates the total number of tokens and KK indicates the feature dimension.
      • WZ8E×K×NW∈\mathbb{Z_8}^{E \times K \times N}: grouped weight matrix (right matrix), where [object Object] indicates the number of experts, [object Object] indicates the feature dimension, and [object Object] indicates the output dimension.
      • w_scaleRE×Nw\_scale∈\mathbb{R}^{E \times N}: per-channel scale factor for the grouped weight matrix (right matrix), where [object Object] indicates the number of experts and [object Object] indicates the output dimension.
      • x_scaleRMx\_scale∈\mathbb{R}^{M}: per-token scale factor for the activation matrix (left matrix), where [object Object] indicates the total number of tokens.
      • groupListNEgroupList∈\mathbb{N}^{E}: Group index list of cumsum.
    • Output

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

        1. Determine the tokens of the current group based on [object Object], where i[0,Len(groupList)]i \in [0,Len(groupList)].
        [object Object]
        1. Perform the following computation based on the input parameters determined by grouping:

        Ci=(XiWi)x_scaleiBroadcastw_scaleiBroadcastC_{i} = (X_{i}\cdot W_{i} )\odot x\_scale_{i\,\text{Broadcast}} \odot w\_scale_{i\,\text{Broadcast}}

        Ci,act,gatei=split(Ci)C_{i,act}, gate_{i} = split(C_{i})

        Si=Swish(Ci,act)gateiS_{i}=Swish(C_{i,act})\odot gate_{i}    where Swish(x)=x1+exSwish(x)=\frac{x}{1+e^{-x}}

        1. Quantize the output.

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

        Qi=SiQ_scaleiQ_{i} = \left\lfloor \frac{S_{i}}{Q\_scale_{i}} \right\rceil

    [object Object][object Object]
    • Definition
      • indicates matrix multiplication.
      • indicates element-wise multiplication.
      • x\left \lfloor x\right \rceil indicates rounding [object Object] to the nearest integer.
      • Z8={xZ128x127}\mathbb{Z_8} = \{ x \in \mathbb{Z} | −128≤x≤127 \}
      • Z4={xZ8x7}\mathbb{Z_4} = \{ x \in \mathbb{Z} | −8≤x≤7 \}
      • Z32={xZ2147483648x2147483647}\mathbb{Z_{32}} = \{ x \in \mathbb{Z} | -2147483648≤x≤2147483647 \}
    • Input
      • XZ8M×KX∈\mathbb{Z_8}^{M \times K}: activation matrix (left matrix), where MM indicates the total number of tokens and KK indicates the feature dimension.
      • WZ4E×K×NW∈\mathbb{Z_4}^{E \times K \times N}: grouped weight matrix (right matrix), where [object Object] indicates the number of experts, [object Object] indicates the feature dimension, and [object Object] indicates the output dimension.
      • weightAssistMatrixRE×NweightAssistMatrix∈\mathbb{R}^{E \times N}: Auxiliary matrix used for matrix multiplication. For details about how to generate the auxiliary matrix, see the following description.
      • w_scaleRE×K_group_num×Nw\_scale∈\mathbb{R}^{E \times K\_group\_num \times N}: per-channel scale factor for the grouped weight matrix (right matrix), where [object Object] indicates the number of experts, [object Object] indicates the number of groups along the K-axis, and [object Object] indicates the output dimension.
      • x_scaleRMx\_scale∈\mathbb{R}^{M}: per-token scale factor for the activation matrix (left matrix), where [object Object] indicates the total number of tokens.
      • groupListNEgroupList∈\mathbb{N}^{E}: Group index list of cumsum.
    • Output
      • QZ8M×N/2Q∈\mathbb{Z_8}^{M \times N / 2}: quantized output matrix.
      • Q_scaleRMQ\_scale∈\mathbb{R}^{M}: quantization scale factor.
    • Computation process
        1. Determine the tokens of the current group based on [object Object], where i[0,Len(groupList)]i \in [0,Len(groupList)].
        • The grouping logic is the same as that of A8W8.
        1. The computation process of generating the auxiliary matrix (weightAssistMatrix) is as follows. (Note that the computation of weightAssistMatrix is performed offline and is not completed inside the operator.)
        • For per-channel quantization (w_scalew\_scale is 2D):

          weightAssistMatrixi=8×w_scale×Σk=0K1weight[:,k,:]weightAssistMatrix_{i} = 8 × w\_scale × Σ_{k=0}^{K-1} weight[:,k,:]

        • For per-group quantization (w_scalew\_scale is 3D):

          weightAssistMatrixi=8×Σk=0K1(weight[:,k,:]×w_scale[:,k/num_per_group,:])weightAssistMatrix_{i} = 8 × Σ_{k=0}^{K-1} (weight[:,k,:] × w\_scale[:, ⌊k/num\_per\_group⌋, :])

          Note: num_per_group=K//K_group_numnum\_per\_group = K // K\_group\_num

        1. Perform the following computation based on the input parameters determined by grouping:
        • 3.1. Convert the left matrix Z8\mathbb{Z_8} into two Z4\mathbb{Z_4} components that represent the high and low bits. X_high_4bitsi=Xi16X\_high\_4bits_{i} = \lfloor \frac{X_{i}}{16} \rfloor X_low_4bitsi=Xi&0x0f8X\_low\_4bits_{i} = X_{i} \& 0x0f - 8

        • 3.2. Enable per-channel or per-group quantization during matrix multiplication. Per-channel:

          C_highi=(X_high_4bitsiWi)w_scaleiC\_high_{i} = (X\_high\_4bits_{i} \cdot W_{i}) \odot w\_scale_{i}

          C_lowi=(X_low_4bitsiWi)w_scaleiC\_low_{i} = (X\_low\_4bits_{i} \cdot W_{i}) \odot w\_scale_{i}

          Per-group:

          C_highi=Σk=0K1((X_high_4bitsi[:,knum_per_group:(k+1)num_per_group]Wi[knum_per_group:(k+1)num_per_group,:])w_scalei[k,:])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_lowi=Σk=0K1((X_low_4bitsi[:,knum_per_group:(k+1)num_per_group]Wi[knum_per_group:(k+1)num_per_group,:])w_scalei[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 matrix multiplication results of the high and low bits into the overall result.

          Ci=(C_highi16+C_lowi+weightAssistMatrixi)x_scaleiC_{i} = (C\_high_{i} * 16 + C\_low_{i} + weightAssistMatrix_{i}) \odot x\_scale_{i}

          Ci,act,gatei=split(Ci)C_{i,act}, gate_{i} = split(C_{i})

          Si=Swish(Ci,act)gateiS_{i}=Swish(C_{i,act})\odot gate_{i}    where Swish(x)=x1+exSwish(x)=\frac{x}{1+e^{-x}}

        1. Quantize the output.

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

        Qi=SiQ_scaleiQ_{i} = \left\lfloor \frac{S_{i}}{Q\_scale_{i}} \right\rceil

    [object Object]
[object Object]

Each operator has calls. First, [object Object] is called to obtain the workspace size required for computation and the executor that contains the operator computation process. Then, [object Object] is called to perform computation.

[object Object]
[object Object]
[object Object]
  • Parameters

    [object Object]
  • Return

    [object Object]: status code. For details, see .

    The first-phase API implements input parameter validation. The following errors may be thrown:

    [object Object]
[object Object]
  • Parameters

    [object Object]
  • Return

    [object Object]: status code. For details, see .

[object Object]
  • Deterministic computation:

    • [object Object] defaults to a deterministic implementation.
  • A8W8 scenario (A: activation matrix (left matrix); W: weight matrix (right matrix); 8: INT8)

      1. The length of the last axis of [object Object] cannot be greater than or equal to 65536.
      1. The length of the N-axis cannot exceed 10240.
  • A8W4 scenario (A: activation matrix (left matrix); W: weight matrix (right matrix); 4: INT4)

      1. The length of the last axis of [object Object] cannot be greater than or equal to 20000.
      1. The length of the N-axis cannot exceed 10240.
[object Object]

The following example is for reference only. For details, see .

[object Object]