aclnnGroupedMatmulActivationQuantWeightNz
Applicable Products
Ascend 950PR&950DT products : Supported
Atlas A3 products : Not supported
Atlas A2 products : Not supported
Atlas 200I/500 A2 inference products : Not supported
Atlas inference products : Not supported
Atlas training products : Not supported
Function
Description: Fuses GroupedMatmul, activation, and quant. For details, see Formula. This API is a WeightNz-specific version. The caller must pass weight in FRACTAL_NZ format, which the API parses accordingly. The current version supports only the MX quantization scenario, and the activation function supports only gelu_tanh.
Formula:
MX quantization scenario: Definition:
⋅ denotes matrix multiplication.
⊙ indicates per-element multiplication.
The mathematical semantics of the gelu_tanh activation function is as follows:
$$ \text{GELU}_{\text{tanh}}(x) \approx 0.5x \left( 1 + \tanh\left( \sqrt{\frac{2}{\pi}} \left( x + 0.044715 x^3 \right) \right) \right) $$The current underlying kernel implementation uses the gelu_sigmoid function to approximate gelu_tanh, defined as:
$$ \operatorname{GeluTanh}(x) = \frac{x}{1 + \exp\left(-1.595769121 \times \left(x + 0.044715 \times x^3\right)\right)} $$*$E$ indicates the number of experts, $M$ indicates the total number of tokens, $K$ indicates the input feature dimension, and $N$ indicates the output feature dimension.
$blocksize$ denotes the storage block size of the MX quantization result. Currently, only 64 is supported, corresponding to 2 MX quantization groups, each containing 32 elements.
Inputs:
- $X$: activation matrix.
- $groupList$: Grouping index list, interpreted as cumsum or count based on groupListType.
- $weight$: Grouped weight matrix.
- $weightScale$: quantization factor of the weight matrix.
- $bias$: must be empty in the MX scenario. Supports nullptr, an empty tensorlist, or an empty tensorlist with a length of 1 and an element shape of (0).
- $xScaleOptional$: quantization factor of the left matrix, corresponding to $xScale$ in the formula.
Outputs:
- $Y$: output matrix after activation and quantization. Its data type is specified by the output tensor y.
- $YScale$: output quantization factor.
Computation Process
1.Determine the token range of the current group based on groupList[i], where $i \in [0, Len(groupList))$.
2.Perform GroupedMatmul and dequantization computation based on the input parameters determined by grouping. The intermediate GroupedMatmul result defaults to FLOAT32:
$C_i = (X_i \cdot weight_i) \odot xScale_{i\ Broadcast} \odot weightScale_{i\ Broadcast}$
3.Perform gelu_tanh activation:
$S_i = GeluTanh(C_i)$
Note: The underlying kernel implementation uses the gelu_sigmoid function to approximately compute gelu_tanh. For the approximate formula, see the definition.
Perform MX quantization on the activation result. The target data type DType is specified by the data type of output Tensor y:
Case 1: When scaleAlg is 0, the OCP implementation is used. The activation result $S_i$ is grouped along the N axis by $group\_size=32$. Each group of $group\_size$ elements $\{V_j\}_{j=1}^{group\_size}$ is dynamically quantized to $\{YScale, \{P_j\}_{j=1}^{group\_size}\}$.
$$ shared\_exp = floor(log_2(max_j(|V_j|))) - emax $$$$ YScale = 2 ^ {shared\_exp} $$$$ P_j = cast\_to\_dst\_type(V_j / YScale, roundMode), \space j \space from \space 1 \space to \space group\_size $$The quantized $P_j$ values are placed at the positions of the corresponding $V_j$ to form output $Y$. The $YScale$ values of the quantization groups along the N axis form output $YScale$.
- $emax$: Exponent of the largest normal number for the corresponding data type.
| DataType | Emax | | :---: | :---: | | FLOAT4_E2M1 | 2 | | FLOAT4_E1M2 | 0 | | FLOAT8_E4M3FN | 8 | | FLOAT8_E5M2 | 15 |
- Use case 2: when scaleAlg is 1, the cuBLAS implementation is used, and only FP8 types are involved. Group the activation result $S_i$ along the N axis by the storage $blocksize=64$, and compute a block scaling factor $S_{fp32}^b$ for each block independently. Then map all elements in the block to the target FP8 type using the same $S_{fp32}^b$. If the last block contains fewer than $blocksize$ elements, treat the missing values as 0 and process the block as a complete one.
Find the maximum absolute value of the elements in the block:
$$
Amax(D_{fp32}^b)=max(\{|d_j|\}_{j=1}^{k})
$$
Map FP32 to the range representable by the target data type, where $Amax(DType)$ is the maximum value representable by the target precision:
$$
S_{fp32}^b = \frac{Amax(D_{fp32}^b)}{Amax(DType)}
$$
Convert the block scaling factor $S_{fp32}^b$ to the scaling value $S_{ue8m0}^b$ representable in FP8 format. First, extract the unbiased exponent $E_{int}^b$ and the mantissa $M_{fixp}^b$ from $S_{fp32}^b$. Then round up the exponent to avoid quantization overflow:
$
E_{int}^b = \begin{cases}
E_{int}^b + 1, & \text{ if} S_{fp32}^b \text{ is a normal number, and} E_{int}^b < 254 \text{ and} M_{fixp}^b > 0 \\
E_{int}^b + 1, & \text{ if} S_{fp32}^b \text{ is a subnormal number, and} M_{fixp}^b > 0.5 \\
E_{int}^b, & \text{ otherwise}
\end{cases}
$
$$
S_{ue8m0}^b = 2 ^ {E_{int}^b}
$$
$$
R_{fp32}^b = \frac{1}{fp32(S_{ue8m0}^b)}
$$
Quantize each element in the block:
$$
d^j = DType(d_{fp32}^j \cdot R_{fp32}^b), \space j \space from \space 1 \space to \space blocksize
$$
The final quantized output is $\left(S^b, [d^j]_{j=1}^{k}\right)$, where $S^b$ denotes the block scaling factor $S_{ue8m0}^b$, and $[d^j]_{j=1}^{k}$ denotes the quantized data in the block.
- Scenario 3: when scaleAlg is 2, only the FLOAT4_E2M1 output is involved, and `dstTypeMax` supports 0.0 or [6.0, 12.0]. Group the activation result $S_i$ along the N axis by $group\_size=32$:
When `dstTypeMax` is 0.0, 6.0, or 7.0, determine whether to carry the shared exponent based on the exponent and high-order mantissa bits of the maximum absolute value in the block. Specifically, 0.0 and 6.0 use one high-order mantissa bit for the determination, and 7.0 uses two:
$
shared\_exp = \begin{cases}
ceil(log_2(max_j(|V_j|))) - emax, & \text{ if the high-order mantissa bits meet the carry condition and the mantissa is not all zeros} \\
floor(log_2(max_j(|V_j|))) - emax, & \text{ otherwise}
\end{cases}
$
where the $emax$ of FLOAT4_E2M1 is 2. Then, compute $YScale=2^{shared\_exp}$ and perform the following:
$$
P_j = cast\_to\_FLOAT4\_E2M1(V_j / YScale, roundMode)
$$
When `dstTypeMax` is any other value in [6.0, 12.0], first compute $scale=max_j(|V_j|)/dstTypeMax$, round the result up to a power of 2 representable in E8M0 as $YScale$, and obtain $P_j$ using the preceding formula.
The quantized $P_j$ values form the output $Y$. Each 32-element group corresponds to one $YScale$ of the FLOAT8_E8M0 type.Function Prototype
Each operator uses a two-phase API. Call the "aclnnGroupedMatmulActivationQuantWeightNzGetWorkspaceSize" API first to obtain the workspace size required for computation and the executor that contains the operator computation process, and then call the "aclnnGroupedMatmulActivationQuantWeightNz" API to perform the computation.
aclnnStatus aclnnGroupedMatmulActivationQuantWeightNzGetWorkspaceSize(
const aclTensor *x,
const aclTensor *groupList,
const aclTensorList *weight,
const aclTensorList *weightScale,
const aclTensorList *bias,
const aclTensor *xScaleOptional,
const char *activationType,
int64_t groupListType,
const aclIntArray *tuningConfig,
const char *quantMode,
const char *roundMode,
int64_t scaleAlg,
float dstTypeMax,
aclTensor *y,
aclTensor *yScale,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnGroupedMatmulActivationQuantWeightNz(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnGroupedMatmulActivationQuantWeightNzGetWorkspaceSize
Parameters
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor x (const aclTensor *) Input Indicates the left matrix, corresponding to X in the formula. Mandatory parameter. FLOAT8_E4M3FN, FLOAT8_E5M2, FLOAT4_E2M1, FLOAT4_E1M2 ND 2 √ groupList (const aclTensor *) Input Indicates the grouping information, corresponding to groupList in the formula. - Mandatory parameter. Provides data in different formats based on groupListType. 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 values must not be greater than the first dimension of the tensor in x.
- When groupListType is 0, elements in groupList must be non-negative and monotonically non-decreasing, and the last element must not be greater than M. When groupListType is 1, elements must be non-negative and the sum must not be greater than M.
- The first phase of GetWorkspaceSize only validates the pointer, dtype, and shape of groupList, and does not read the element values of the device-side tensor; the caller must ensure the preceding value range constraints.
- Values in groupList constrain the valid part of the output data, and the part not specified by groupList does not participate in the update.
INT64 ND 1 √ weight (const aclTensorList *) Input Indicates the weight matrix, which is weight in the corresponding formula. The length of tensorList currently only supports 1. The caller must pass in weight in FRACTAL_NZ format, and the API parses this parameter in that format. viewShape must be 3-dimensional, and storageShape must be 5-dimensional. FLOAT8_E4M3FN, FLOAT4_E2M1, FLOAT4_E1M2 FRACTAL_NZ 3 (storageShape is 5) × weightScale (const aclTensorList *) Input Indicates the quantization factor of the weight matrix, corresponding to weightScale in the formula. The tensorList length currently only supports 1. FLOAT8_E8M0 ND 4 √ bias (const aclTensorList *) Optional input Indicates the bias. Must be empty in the MX scenario. Supports nullptr, an empty tensorlist, or an empty tensorlist with a length of 1 and an element shape of (0). - - - - xScaleOptional (const aclTensor *) Input Indicates the quantization factor of the left matrix, corresponding to xScale in the formula. Mandatory in the current MX quantization scenario. Cannot be nullptr. FLOAT8_E8M0 ND 3 √ activationType (const char *) Input Indicates the activation function type. Mandatory attribute. Currently only supports "gelu_tanh". - - - - groupListType (int64_t) Input Indicates the interpretation mode of groupList. Supports values 0 and 1. When groupListType is 0, groupList must be a non-negative monotonically non-decreasing sequence. When groupListType is 1, groupList must be a non-negative sequence. - - - - tuningConfig (const aclIntArray *) Optional input Indicates tuning parameters. Reserved parameter. Supports passing nullptr. Currently unused. - - - - quantMode (const char *) Optional input Indicates the quantization mode. Supports passing nullptr or an empty string. Currently only supports "mx". When nullptr or an empty string is passed, if the data type of x is a supported FLOAT8 or FLOAT4 type and the data type of xScaleOptional is FLOAT8_E8M0, it is deduced as "mx". If a value other than "mx" is explicitly passed, an error is reported. - - - - roundMode (const char *) Optional input Indicates the quantization rounding mode. Supports passing nullptr or an empty string. If nullptr or an empty string is passed, it is processed as "rint". Currently only "rint" is supported. - - - - scaleAlg (int64_t) Input Indicates the algorithm for computing the quantization factor. Supports 0, 1, and 2: 0 indicates the OCP implementation; 1 indicates the cuBLAS implementation and only supports FLOAT8 output; 2 indicates the FLOAT4 dynamic range implementation and only supports FLOAT4_E2M1 output. FLOAT4_E1M2 output only supports 0. - - - - dstTypeMax (float) Input Indicates the value of maxType, corresponding to Amax(DType) in the formula. Supports 0.0 or [6.0, 12.0] when scaleAlg is 2; 0.0 indicates using the maximum value 6.0 of FLOAT4_E2M1. Other scaleAlg values only support 0.0. - - - - y (aclTensor *) Output Indicates the output matrix after activation and quantization. Mandatory parameter. FLOAT4 output only supports the FLOAT4 input scenario. FLOAT8_E4M3FN, FLOAT8_E5M2, FLOAT4_E2M1, FLOAT4_E1M2 ND 2 - yScale (aclTensor *) Output Indicates the output quantization factor. Mandatory parameter. FLOAT8_E8M0 ND 3 - workspaceSize (uint64_t *) Output Returns the size of the workspace that needs to be side-applied on the Device. - - - - - executor (aclOpExecutor **) Output Returns the op executor, which contains the operator computation flow. - - - - - Ascend 950PR&950DT products :- x supports non-transposed input only, and weight supports non-transposed and transposed input. The API deduces whether weight is transposed based on the shapes and transposition relationship of weight and weightScale.
- The transpose attribute of weightScale must be consistent with that of weight.
- Empty tensor handling rules:
- weight and weightScale are mandatory inputs. Empty tensorlist is not supported, and elements in the tensorlist cannot be nullptr.
- Empty tensors with M of 0 or N of 0 are supported: x and xScaleOptional support M of 0, and weight and weightScale support N of 0. In this case, K of 0 is allowed. The first-phase API returns ACLNN_SUCCESS, and workspaceSize is 0. When both M and N are not 0, K of 0 is not supported.
- Supports the empty tensor use case where groupList is empty: when groupList is empty and output y or yScale is empty, the first-phase API returns ACLNN_SUCCESS and workspaceSize is 0.
Return Value
aclnnStatus: Return status code. For details, see aclnn return code.
The first-phase API performs input parameter validation and returns an error in the following scenarios:
Error Code Error code Description ACLNN_ERR_PARAM_NULLPTR 161001 A mandatory parameter is a null pointer. For example, x, groupList, xScaleOptional, weight, weightScale, activationType, y, yScale, workspaceSize, or executor is null. ACLNN_ERR_PARAM_INVALID 161002 activationType is not "gelu_tanh". bias is not empty. The input data type or data format is not supported. The shape dimensions do not match. The attribute value is out of the supported range.
aclnnGroupedMatmulActivationQuantWeightNz
Parameters
Parameter Input/Output Description workspace Input The workspace address side-applied on the Device. workspaceSize Input The workspace size side-applied on the Device, obtained by the first segment connection aclnnGroupedMatmulActivationQuantWeightNzGetWorkspaceSize. executor Input Op executor, which contains the operator computation flow. stream Input acl stream. Return Value
aclnnStatus: return status code. For details, see aclnn Return Codes.
Constraints
- Deterministic computation:
- aclnnGroupedMatmulActivationQuantWeightNz defaults to a deterministic implementation.
- In non-empty tensor use cases, the first dimension of groupList is in the range 1-1024.
- 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 Activation function x weight weightScale xScaleOptional y yScale MXFP8 gelu_tanh FLOAT8_E4M3FN, FLOAT8_E5M2 FLOAT8_E4M3FN FLOAT8_E8M0 FLOAT8_E8M0 FLOAT8_E4M3FN, FLOAT8_E5M2 FLOAT8_E8M0 MXFP4 gelu_tanh FLOAT4_E2M1, FLOAT4_E1M2 FLOAT4_E2M1, FLOAT4_E1M2 FLOAT8_E8M0 FLOAT8_E8M0 FLOAT8_E4M3FN, FLOAT8_E5M2, FLOAT4_E2M1, FLOAT4_E1M2 FLOAT8_E8M0 The shape constraints must meet the following table:
MX quantization scenario activation function x weight (storageShape) weightScale xScale output outputScale MXFP8 gelu_tanh (M, K) - Non-transposed shape in the form of {(E, ceil(N / 32), ceil(K / 16), 16, 32)}.
- Transposed shape in the form of {(E, ceil(K / 32), ceil(N / 16), 16, 32)}.
- Non-transposed shape in the form of {(E, ceil(K / 64), N, 2)}.
- Transposed shape in the form of {(E, N, ceil(K / 64), 2)}.
(M, ceil(K / 64), 2) (M, N) (M, ceil(N / 64), 2) MXFP4 gelu_tanh (M, K) - Non-transposed shape in the form of {(E, ceil(N / 64), ceil(K / 16), 16, 64)}.
- Transposed shape in the form of {(E, ceil(K / 64), ceil(N / 16), 16, 64)}.
- Non-transposed shape in the form of {(E, ceil(K / 64), N, 2)}.
- Transposed shape in the form of {(E, N, ceil(K / 64), 2)}.
(M, ceil(K / 64), 2) (M, N) (M, ceil(N / 64), 2) In the table, the last dimension of the shapes of xScale, weightScale, and outputScale is 2, indicating that each 64-element storage block contains 2 MX quantization groups, and each group covers 32 elements.
In non-empty use cases, N must be a multiple of 64 and at least 64.
In non-empty use cases, K must be greater than 0. In MXFP4 use cases, x and weight must both be FLOAT4, and each can be FLOAT4_E2M1 or FLOAT4_E1M2. K must be an even number and cannot be 2.
Example
The sample code for calling the API is as follows, for reference only. For details about the compilation and execution process, see Compile and Run Samples.
/**
* Copyright (c) 2026 Huawei Technologies Co., Ltd.
* This program is free software, you can redistribute it and/or modify it under the terms and conditions of
* CANN Open Software License Agreement Version 2.0 (the "License").
* Please refer to the License for details. You may not use this file except in compliance with the License.
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
* See LICENSE in the root of the software repository for the full text of the License.
*/
#include <cstdio>
#include <cstdint>
#include <memory>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_grouped_matmul_activation_quant_weight_nz.h"
/*!
* \file test_aclnn_grouped_matmul_activation_quant_weight_nz.cpp
* \brief GroupedMatmulActivationQuant WeightNz MXFP8 aclnn example for Ascend 950PR&950DT products.
*/
#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 dim : shape) {
shapeSize *= dim;
}
return shapeSize;
}
template <typename T1, typename T2>
auto CeilDiv(T1 a, T2 b) -> T1
{
if (b == 0) {
return a;
}
return (a + b - 1) / b;
}
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 ACL_SUCCESS;
}
void Finalize(int32_t deviceId, aclrtStream stream)
{
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
}
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 = static_cast<int64_t>(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 ACL_SUCCESS;
}
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 **tensorList)
{
int size = static_cast<int>(shapes.size());
std::vector<aclTensor *> tensors(size, nullptr);
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);
}
*tensorList = aclCreateTensorList(tensors.data(), size);
return ACL_SUCCESS;
}
template <typename T>
int CreateAclTensorListWithStorageShape(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>> &strides, void **deviceAddr, aclDataType dataType, aclFormat formatType,
aclTensorList **tensorList)
{
int size = static_cast<int>(storageShapes.size());
std::vector<aclTensor *> tensors(size, nullptr);
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, strides[i].data(), 0,
formatType, storageShapes[i].data(), storageShapes[i].size(), *(deviceAddr + i));
}
*tensorList = aclCreateTensorList(tensors.data(), size);
return ACL_SUCCESS;
}
int RunGroupedMatmulActivationQuantWeightNz(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 = 2;
int64_t m = 4;
int64_t k = 64;
int64_t n = 128;
int64_t kScaleBlock = CeilDiv(k, 64);
int64_t nScaleBlock = CeilDiv(n, 64);
std::vector<int64_t> xShape = {m, k};
std::vector<int64_t> groupListShape = {e};
std::vector<std::vector<int64_t>> weightViewShapes = {{e, k, n}};
std::vector<std::vector<int64_t>> weightStorageShapes = {{e, CeilDiv(n, 32), CeilDiv(k, 16), 16, 32}};
std::vector<std::vector<int64_t>> weightStrides = {{k * n, n, 1}};
std::vector<std::vector<int64_t>> weightScaleShapes = {{e, kScaleBlock, n, 2}};
std::vector<int64_t> xScaleShape = {m, kScaleBlock, 2};
std::vector<int64_t> yShape = {m, n};
std::vector<int64_t> yScaleShape = {m, nScaleBlock, 2};
std::vector<uint8_t> xHostData(GetShapeSize(xShape), 1);
std::vector<int64_t> groupListHostData = {2, 2};
std::vector<uint8_t> weightHostData(GetShapeSize(weightStorageShapes[0]), 1);
std::vector<uint8_t> weightScaleHostData(GetShapeSize(weightScaleShapes[0]), 1);
std::vector<uint8_t> xScaleHostData(GetShapeSize(xScaleShape), 1);
std::vector<uint8_t> yHostData(GetShapeSize(yShape), 0);
std::vector<uint8_t> yScaleHostData(GetShapeSize(yScaleShape), 0);
void *xDeviceAddr = nullptr;
void *groupListDeviceAddr = nullptr;
void *weightDeviceAddr = nullptr;
void *weightScaleDeviceAddr = nullptr;
void *xScaleDeviceAddr = nullptr;
void *yDeviceAddr = nullptr;
void *yScaleDeviceAddr = nullptr;
aclTensor *x = nullptr;
aclTensor *groupList = nullptr;
aclTensorList *weight = nullptr;
aclTensorList *weightScale = nullptr;
aclTensorList *bias = nullptr;
aclTensor *xScale = nullptr;
aclTensor *y = nullptr;
aclTensor *yScale = nullptr;
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);
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);
std::vector<std::vector<uint8_t>> weightHostDataList = {weightHostData};
ret = CreateAclTensorListWithStorageShape<uint8_t>(weightHostDataList, weightViewShapes, weightStorageShapes,
weightStrides, &weightDeviceAddr, aclDataType::ACL_FLOAT8_E4M3FN, aclFormat::ACL_FORMAT_FRACTAL_NZ, &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 = CreateAclTensorList<uint8_t>(weightScaleHostDataList, weightScaleShapes, &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<uint8_t>(yHostData, yShape, &yDeviceAddr, aclDataType::ACL_FLOAT8_E4M3FN,
aclFormat::ACL_FORMAT_ND, &y);
std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor *)> yTensorPtr(y, aclDestroyTensor);
std::unique_ptr<void, aclError (*)(void *)> yDeviceAddrPtr(yDeviceAddr, aclrtFree);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor<uint8_t>(yScaleHostData, yScaleShape, &yScaleDeviceAddr, aclDataType::ACL_FLOAT8_E8M0,
aclFormat::ACL_FORMAT_ND, &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);
const char *activationType = "gelu_tanh";
int64_t groupListType = 1;
aclIntArray *tuningConfig = nullptr;
const char *quantMode = "mx";
const char *roundMode = "rint";
int64_t scaleAlg = 0;
float dstTypeMax = 0.0F;
uint64_t workspaceSize = 0;
aclOpExecutor *executor = nullptr;
void *workspaceAddr = nullptr;
ret = aclnnGroupedMatmulActivationQuantWeightNzGetWorkspaceSize(x, groupList, weight, weightScale, bias, xScale,
activationType, groupListType, tuningConfig, quantMode, roundMode, scaleAlg, dstTypeMax, y, yScale,
&workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS,
LOG_PRINT("aclnnGroupedMatmulActivationQuantWeightNzGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
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);
}
ret = aclnnGroupedMatmulActivationQuantWeightNz(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS,
LOG_PRINT("aclnnGroupedMatmulActivationQuantWeightNz 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);
LOG_PRINT("y shape is: [%ld, %ld]\n", yShape[0], yShape[1]);
LOG_PRINT("yScale shape is: [%ld, %ld, %ld]\n", yScaleShape[0], yScaleShape[1], yScaleShape[2]);
LOG_PRINT("GroupedMatmulActivationQuantWeightNz example execute success.\n");
return ACL_SUCCESS;
}
int main()
{
int32_t deviceId = 0;
aclrtStream stream = nullptr;
auto ret = RunGroupedMatmulActivationQuantWeightNz(deviceId, stream);
CHECK_FREE_RET(ret == ACL_SUCCESS,
LOG_PRINT("RunGroupedMatmulActivationQuantWeightNz failed. ERROR: %d\n", ret); return ret);
Finalize(deviceId, stream);
return ACL_SUCCESS;
}