aclnnAddRmsNormDynamicQuantV2
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: The RmsNorm operator is a commonly used normalization operation in large models. Compared with the LayerNorm operator, it removes the part of subtracting the mean. The DynamicQuant operator performs symmetric dynamic quantization on input tensors. The AddRmsNormDynamicQuant operator fuses the Add operator before RmsNorm and one or two DynamicQuant operators that receive the RmsNorm normalized output, reducing data movement operations. Compared with aclnnAddRmsNormDynamicQuant, aclnnAddRmsNormDynamicQuantV2 adds the bias term betaOptional parameter during the RmsNorm computation, i.e., the beta in the formula, and adds the output configuration item outputMaskOptional parameter, which configures whether to output the quantization result at the corresponding position.
Formula:
In the formula, row_max represents the maximum value of each row.
Function Prototype
Each operator uses a two-phase API. Call the aclnnAddRmsNormDynamicQuantV2GetWorkspaceSize API first to obtain the workspace size required for computation and the executor that contains the operator computation process, and then call the aclnnAddRmsNormDynamicQuantV2 API to perform the computation.
aclnnStatus aclnnAddRmsNormDynamicQuantV2GetWorkspaceSize(
const aclTensor *x1,
const aclTensor *x2,
const aclTensor *gamma,
const aclTensor *smoothScale1Optional,
const aclTensor *smoothScale2Optional,
const aclTensor *betaOptional,
double epsilon,
const aclBoolArray *outputMaskOptional,
aclTensor *y1Out,
aclTensor *y2Out,
aclTensor *xOut,
aclTensor *scale1Out,
aclTensor *scale2Out,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnAddRmsNormDynamicQuantV2(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnAddRmsNormDynamicQuantV2GetWorkspaceSize
Parameters
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor x1 (aclTensor*) Input Represents the source data tensor in the normalization process. Corresponds to `x1` in the corresponding formula. - Supports empty tensors.
- If the data type of output `y1Out` or `y2Out` is INT4, the last axis of `x1` must be divisible by 2.
- If the data type of output `y1Out` or `y2Out` is INT32, the last axis of `x1` must be 8 times the last axis of output `y1Out` or `y2Out`.
FLOAT16, BFLOAT16 ND 2-8 √ x2 (aclTensor*) Input Represents the source data tensor in the normalization process. Corresponds to `x2` in the corresponding formula. - Support empty tensors.
- The shape and data type need to remain consistent with `x1`.
FLOAT16, BFLOAT16 ND 2-8 √ gamma (aclTensor*) Input Weight tensor in the standardization process. It corresponds to `gamma` in the formula. - Support empty tensors.
- The data type needs to remain consistent with `x1`.
- The shape needs to be consistent with the last dimension of `x1`.
FLOAT16, BFLOAT16 ND 1 √ smoothScale1Optional (aclTensor*) Input Indicates the smoothScale tensor used to obtain `y1Out` during the quantization process. The `smoothScale1Optional` in the corresponding formula. - Supports empty tensors.
- Optional parameter. Supports passing in a null pointer.
- The shape and data type need to be consistent with `gamma`.
FLOAT16, BFLOAT16 ND 1 √ smoothScale2Optional (aclTensor*) Input Indicates the smoothScale tensor used to obtain `y2Out` during the quantization process. Corresponds to `smoothScale2Optional` in the formula. - Supports empty tensors.
- Optional parameter. Supports passing in a null pointer.
- The shape and data type need to remain consistent with `gamma`.
FLOAT16, BFLOAT16 ND 1 √ betaOptional (aclTensor*) Input Represents the bias term in the normalization process. It corresponds to `beta` in the corresponding formula. - Supports empty tensors.
- Optional parameter. Supports passing in a null pointer.
- The shape and data type need to remain consistent with `gamma`.
FLOAT16, BFLOAT16 ND 1 √ epsilon (double) Input Represents the value used to prevent division-by-zero errors, corresponding to `epsilon` in the corresponding formula. It is recommended to pass in a small positive number, such as 1e-6. - - - - outputMaskOptional (aclBoolArray*) Input - Mask of the output, corresponding to `outputMask` in the formula.
- For specific constraints, see Constraints.
Supports passing in a null pointer or an array of length 2. - - - - y1Out (aclTensor*) Output Indicates the quantization output tensor, which is `y1Out` in the corresponding formula. - Supports empty tensors.
- If `y1Out` is a valid output, the shape and data type need to remain consistent with the input `x1`; if `y1Out` is an invalid output, the shape is [1].
- See the Constraints section for details about specific constraints.
INT4, INT32, INT8, HIFLOAT8, FLOAT8_E5M2, FLOAT8_E4M3FN ND 2-8 √ y2Out (aclTensor*) Output Indicates the quantization output tensor, corresponding to `y2Out` in the formula. - Supports empty tensors.
- If `y2Out` is a valid output, its shape and data type need to remain consistent with those of `y1Out`; if `y2Out` is an invalid output, its shape is [1].
- For details about specific constraints, see the Constraints section.
INT4, INT32, INT8, HIFLOAT8, FLOAT8_E5M2, FLOAT8_E4M3FN ND 2-8 √ xOut (aclTensor*) Output Indicates the sum of x1 and x2, corresponding to `x` in the corresponding formula. - Supports empty tensors.
- The shape and data type need to be consistent with input `x1`/`x2`.
FLOAT16, BFLOAT16 ND 2-8 √ scale1Out (aclTensor*) Output Indicates the output of the first quantization path, corresponding to `scale1Out` in the corresponding formula. - Supports empty tensors.
- If this output is a valid output, the shape needs to be consistent with the shape of `x1` except the last dimension, or with the product of dimensions of `x1` except the last dimension; otherwise, the shape is [1].
- For specific constraints, see Constraints.
FLOAT32 ND 1-8 × scale2Out(aclTensor*) Output Output of the second quantization path, corresponding to `scale2Out` in the formula. - Supports empty tensors.
- If this output is a valid output, the shape needs to be consistent with the shape of `x1` except the last dimension, or with the product of dimensions of `x1` except the last dimension; otherwise, the shape is [1].
- For specific constraints, see Constraints.
FLOAT32 ND 1-8 × workspaceSize (uint64_t*) Output Returns the Device-allocated workspace size. - - - - - executor (aclOpExecutor**) Output Returns the op executor, which includes the calculation flow of the operator. - - - - - Return Value
aclnnStatus: return status code. For details, see aclnn Error Codes.
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 If the passed-in parameter is a mandatory input, output, or mandatory attribute and is a null pointer, 161001 is returned. ACLNN_ERR_PARAM_INVALID 161002 The data type of the input or output is out of the supported range. When outputMaskOptional is a null pointer, smoothScale2Optional is input, but smoothScale1Optional is not. When outputMaskOptional is not a null pointer, its length is not 2, or smoothScale1Optional or smoothScale2Optional is not empty but the corresponding position in outputMaskOptional is false. When both y1Out and y2Out are valid outputs, their data types are different. When the data type of y1Out or y2Out is DT_INT32, the last dimension of y1Out or y2Out is not 1/8 of the last dimension of x1. When the data type of y1Out or y2Out is not DT_INT32, the shape of y1Out or y2Out is not consistent with the shape of x1. ACLNN_ERR_INNER_TILING_ERROR 561002 The data type of the input or output is not within the supported range. The shape of the input or output does not meet the constraints.
aclnnAddRmsNormDynamicQuantV2
Parameters
Parameter Input/Output Description workspace Input The pointer of the Device-allocated workspace memory. workspaceSize Input The size of the Device-allocated workspace, obtained by the first-phase API aclnnAddRmsNormDynamicQuantV2GetWorkspaceSize. executor Input Op executor that includes calculation process of the operator. stream Input Stream for executing tasks. Return Value
aclnnStatus: Return status code. (For details, see aclnn return code)
Constraints
Data Format Description
It is recommended to use the ND format for all input and output tensors. Other data formats are converted to the ND format by the framework by default for processing.
Input Parameter Constraints
- When outputMaskOptional is not empty and smoothScale1Optional has a value, outputMaskOptional[0] must be True. When smoothScale2Optional has a value, outputMaskOptional[1] must be True.
- When outputMaskOptional is not empty, outputMaskOptional[0] and outputMaskOptional[1] cannot both be False.
- When outputMaskOptional is empty and smoothScale2Optional has a value, smoothScale1Optional must also have a value.
Output Constraints
- When outputMaskOptional[0] is True, y1Out and scale1Out are valid outputs; when outputMaskOptional[0] is False, y1Out and scale1Out are invalid outputs.
- When outputMaskOptional[1] is True, y2Out and scale2Out are valid outputs; when outputMaskOptional[1] is False, y2Out and scale2Out are invalid outputs.
- When outputMaskOptional is empty, y1Out and scale1Out are always valid outputs.
- When outputMaskOptional is empty, y2Out and scale2Out are valid outputs when both smoothScale1Optional and smoothScale2Optional are valid; otherwise, they are invalid outputs.
Supported Data Types by Product Model
Ascend 950PR&950DT products :x1/x2/gamma/smoothScale1/smoothScale2/betaData Typey1Out/y2OutData TypexOutData Typescale1Out/scale2OutData TypeFLOAT16 INT8 FLOAT16 FLOAT32 BFLOAT16 INT8 BFLOAT16 FLOAT32 FLOAT16 FLOAT8_E4M3FN FLOAT16 FLOAT32 BFLOAT16 FLOAT8_E4M3FN BFLOAT16 FLOAT32 FLOAT16 FLOAT8_E5M2 FLOAT16 FLOAT32 BFLOAT16 FLOAT8_E5M2 BFLOAT16 FLOAT32 FLOAT16 HIFLOAT8 FLOAT16 FLOAT32 BFLOAT16 HIFLOAT8 BFLOAT16 FLOAT32 FLOAT16 INT4 FLOAT16 FLOAT32 BFLOAT16 INT4 BFLOAT16 FLOAT32 FLOAT16 INT32 FLOAT16 FLOAT32 BFLOAT16 INT32 BFLOAT16 FLOAT32
Atlas A3 products ,Atlas A2 products :x1/x2/gamma/smoothScale1/smoothScale2/betaData Typey1Out/y2OutData TypexOutData Typescale1Out/scale2OutData TypeFLOAT16 INT8 FLOAT16 FLOAT32 BFLOAT16 INT8 BFLOAT16 FLOAT32 FLOAT16 INT4 FLOAT16 FLOAT32 BFLOAT16 INT4 BFLOAT16 FLOAT32
Deterministic computation:
- aclnnAddRmsNormDynamicQuantV2 defaults to a deterministic implementation.
Example
The following sample code is for reference only. For details about the compile and run process, see Compile and Run Samples.
#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_add_rms_norm_dynamic_quant_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 shape_size = 1;
for (auto i : shape) {
shape_size *= i;
}
return shape_size;
}
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); aclFinalize(); return ret);
ret = aclrtCreateStream(stream);
CHECK_RET(ret == ACL_SUCCESS,
LOG_PRINT("aclrtCreateStream failed. ERROR: %d\n", ret);
aclrtResetDevice(deviceId); aclFinalize(); return ret);
return 0;
}
template <typename T>
int CreateAclTensor(
const std::vector<T>& hostData, const std::vector<int64_t>& shape, void** deviceAddr, aclDataType dataType,
aclTensor** tensor)
{
auto size = GetShapeSize(shape) * sizeof(T);
// Call aclrtMalloc to allocate device memory.
auto ret = aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMalloc failed. ERROR: %d\n", ret); return ret);
// Call aclrtMemcpy to copy host data to 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);
// Calculate the strides of the contiguous tensor.
std::vector<int64_t> strides(shape.size(), 1);
for (int64_t i = shape.size() - 2; i >= 0; i--) {
strides[i] = shape[i + 1] * strides[i + 1];
}
// Call the aclCreateTensor API to create an aclTensor.
*tensor = aclCreateTensor(
shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND, shape.data(), shape.size(),
*deviceAddr);
return 0;
}
int main()
{
// 1. (Boilerplate) device/stream initialization. For details, see the acl API documentation.
// Set deviceId based on the actual device.
int32_t deviceId = 0;
aclrtStream stream;
auto ret = Init(deviceId, &stream);
// Handle check as needed.
CHECK_RET(ret == 0, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
// 2. Construct the input and output, which must be custom-built based on the API.
std::vector<int64_t> xShape = {2, 8};
std::vector<int64_t> gammaShape = {8};
std::vector<int64_t> betaShape = {8};
std::vector<int64_t> reduceShape = {2};
void* x1DeviceAddr = nullptr;
void* x2DeviceAddr = nullptr;
void* gammaDeviceAddr = nullptr;
void* betaDeviceAddr = nullptr;
void* smooth1DeviceAddr = nullptr;
void* smooth2DeviceAddr = nullptr;
void* y1DeviceAddr = nullptr;
void* y2DeviceAddr = nullptr;
void* xDeviceAddr = nullptr;
void* scale1DeviceAddr = nullptr;
void* scale2DeviceAddr = nullptr;
aclTensor* x1 = nullptr;
aclTensor* x2 = nullptr;
aclTensor* gamma = nullptr;
aclTensor* beta = nullptr;
aclTensor* smooth1 = nullptr;
aclTensor* smooth2 = nullptr;
aclTensor* y1 = nullptr;
aclTensor* y2 = nullptr;
aclTensor* x = nullptr;
aclTensor* scale1 = nullptr;
aclTensor* scale2 = nullptr;
int64_t xShapeSize = GetShapeSize(xShape);
int64_t gammaShapeSize = GetShapeSize(gammaShape);
int64_t betaShapeSize = GetShapeSize(betaShape);
int64_t reduceShapeSize = GetShapeSize(reduceShape);
std::vector<short> x1HostData(xShapeSize, 0x3800);
std::vector<short> x2HostData(xShapeSize, 0x3800);
std::vector<short> gammaHostData(gammaShapeSize, 0x3e00);
std::vector<short> betaHostData(betaShapeSize, 0x3e00);
std::vector<short> smooth1HostData(gammaShapeSize, 0x3e00);
std::vector<short> smooth2HostData(gammaShapeSize, 0x3e00);
std::vector<short> y1HostData(xShapeSize, 0);
std::vector<short> y2HostData(xShapeSize, 0);
std::vector<short> xHostData(xShapeSize, 0);
std::vector<short> scale1HostData(reduceShapeSize, 0);
std::vector<short> scale2HostData(reduceShapeSize, 0);
float epsilon = 1e-6;
// Create x1 aclTensor.
ret = CreateAclTensor(x1HostData, xShape, &x1DeviceAddr, aclDataType::ACL_FLOAT16, &x1);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create x2 aclTensor.
ret = CreateAclTensor(x2HostData, xShape, &x2DeviceAddr, aclDataType::ACL_FLOAT16, &x2);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create gamma aclTensor.
ret = CreateAclTensor(gammaHostData, gammaShape, &gammaDeviceAddr, aclDataType::ACL_FLOAT16, &gamma);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create beta aclTensor.
ret = CreateAclTensor(betaHostData, betaShape, &betaDeviceAddr, aclDataType::ACL_FLOAT16, &beta);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create smooth1 aclTensor.
ret = CreateAclTensor(smooth1HostData, gammaShape, &smooth1DeviceAddr, aclDataType::ACL_FLOAT16, &smooth1);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create smooth2 aclTensor.
ret = CreateAclTensor(smooth2HostData, gammaShape, &smooth2DeviceAddr, aclDataType::ACL_FLOAT16, &smooth2);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create y1 aclTensor.
ret = CreateAclTensor(y1HostData, xShape, &y1DeviceAddr, aclDataType::ACL_INT8, &y1);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create y2 aclTensor.
ret = CreateAclTensor(y2HostData, xShape, &y2DeviceAddr, aclDataType::ACL_INT8, &y2);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create x aclTensor.
ret = CreateAclTensor(xHostData, xShape, &xDeviceAddr, aclDataType::ACL_FLOAT16, &x);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create outScale1 aclTensor.
ret = CreateAclTensor(scale1HostData, reduceShape, &scale1DeviceAddr, aclDataType::ACL_FLOAT, &scale1);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the outScale2 aclTensor
ret = CreateAclTensor(scale2HostData, reduceShape, &scale2DeviceAddr, aclDataType::ACL_FLOAT, &scale2);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// 3. Call the CANN operator library API. Change it to the actual API.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Call the first-phase API of aclnnAddRmsNormDynamicQuantV2
ret = aclnnAddRmsNormDynamicQuantV2GetWorkspaceSize(
x1, x2, gamma, smooth1, smooth2, beta, epsilon, nullptr, y1, y2, x, scale1, scale2, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnAddRmsNormDynamicQuantV2GetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
// Allocate device memory based on the workspaceSize calculated 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 second-phase API of aclnnAddRmsNormDynamicQuantV2
ret = aclnnAddRmsNormDynamicQuantV2(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnAddRmsNormDynamicQuantV2 failed. ERROR: %d\n", ret); return ret);
// 4. (boilerplate) Synchronously wait for the task execution to complete.
ret = aclrtSynchronizeStream(stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);
// 5. Obtain the output value and copy the result from device memory to host memory. Modify it according to the specific API definition.
auto size = GetShapeSize(xShape);
std::vector<int8_t> y1Ret(size, 0);
ret = aclrtMemcpy(
y1Ret.data(), y1Ret.size() * sizeof(y1Ret[0]), y1DeviceAddr, size * sizeof(int8_t), ACL_MEMCPY_DEVICE_TO_HOST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret);
for (int64_t i = 0; i < size; i++) {
LOG_PRINT("result[%ld] is: %d\n", i, y1Ret[i]);
}
// 6. Release aclTensor and aclScalar. Modify it according to the specific API definition.
aclDestroyTensor(x1);
aclDestroyTensor(x2);
aclDestroyTensor(gamma);
aclDestroyTensor(beta);
aclDestroyTensor(smooth1);
aclDestroyTensor(smooth2);
aclDestroyTensor(y1);
aclDestroyTensor(y2);
aclDestroyTensor(x);
aclDestroyTensor(scale1);
aclDestroyTensor(scale2);
// 7. Release device resources. Modify it according to the specific API definition.
aclrtFree(x1DeviceAddr);
aclrtFree(x2DeviceAddr);
aclrtFree(gammaDeviceAddr);
aclrtFree(betaDeviceAddr);
aclrtFree(smooth1DeviceAddr);
aclrtFree(smooth2DeviceAddr);
aclrtFree(y1DeviceAddr);
aclrtFree(y2DeviceAddr);
aclrtFree(xDeviceAddr);
aclrtFree(scale1DeviceAddr);
aclrtFree(scale2DeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}