aclnnRmsNormQuantV3
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: The RmsNorm operator is a normalization operation commonly used in large models. Compared with the LayerNorm operator, it removes the mean subtraction. The RmsNormQuantV3 operator fuses the RmsNorm operator and the Quantize operator after RmsNorm, reducing data movement operations. It also adds the Rstd output based on the RmsNormQuantV2 operator.
- Formula:
When divMode is true:
When divMode is false:
Function Prototype
Each operator uses a two-phase API. First call the "aclnnRmsNormQuantV3GetWorkspaceSize" API to obtain the workspace size required for the computation and the executor that contains the operator computation flow, and then call the "aclnnRmsNormQuantV3" API to perform the computation.
aclnnStatus aclnnRmsNormQuantV3GetWorkspaceSize(
const aclTensor *x,
const aclTensor *gamma,
const aclTensor *scale,
const aclTensor *offsetOptional,
const aclTensor *betaOptional,
double epsilon,
bool divMode,
bool outputRstd,
aclTensor *y,
aclTensor *rstd,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnRmsNormQuantV3(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnRmsNormQuantV3GetWorkspaceSize
Parameters
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor x (aclTensor*) Input Source data tensor in the normalization process. Corresponds to `x` in the formula. - Empty tensors are not supported.
- For details, see Constraints.
FLOAT32, FLOAT16, BFLOAT16 ND 1-8 √ gamma (aclTensor*) Input Weight tensor in the normalization process. Corresponds to `gamma` in the formula. - Empty tensors are not supported.
- The data type must be consistent with that of `x`.
- If shape is 1D, it must be the same as the last dimension of `x`.
- If shape is 2D, the first dimension must be 1, and the second dimension must be the same as the last dimension of `x`.
FLOAT32, FLOAT16, BFLOAT16 ND 1-2 √ scale (aclTensor*) Input Scale tensor used to obtain y during quantization, corresponding to `scale` in the formula. - Empty tensors are not supported.
- 1D. The shape size is 1 or the same as the last dimension of x.
- For the data type constraint, see Constraints.
- The value of this parameter cannot be 0.
FLOAT32, FLOAT16, BFLOAT16 ND 1 √ offsetOptional (aclTensor*) Optional input Offset tensor used to obtain y during quantization, corresponding to `offset` in the formula. - An empty tensor is not supported.
- Optional parameter. A null pointer can be passed in.
- The shape must be the same as that of `scale`.
- For data type constraints, see Constraints.
FLOAT32, FLOAT16, BFLOAT16, INT8 ND 1 √ betaOptional(aclTensor*) Optional input Indicates the offset tensor in the normalization process. Corresponds to `beta` in the formula. - Empty tensors are not supported.
- Optional parameter. Supports passing a null pointer.
- The data type must be the same as that of `x`.
- If the shape is 1D, it must be the same as the last dimension of `x`.
- If the shape is 2D, the first dimension must be 1, and the second dimension must be the same as the last dimension of `x`.
FLOAT32, FLOAT16, BFLOAT16 ND 1-2 √ epsilon(double) Input The input `epsilon` in the formula, used to prevent division-by-zero errors. The data type is DOUBLE. A small positive value is recommended. - - - - divMode (bool) Input The parameter in the formula that determines whether the quantization formula uses division. The data type is bool. Supports True and False. - - - - outputRstd (bool) Input Specifies whether to output valid rstdOut. - Supports True and False.
- When outputRstd is False, rstdOut is an invalid output.
- - - - y (aclTensor*) Output Final quantized output tensor, corresponding to `y` in the formula. - Empty tensors are not supported.
- The shape must be the same as that of input `x`.
INT8, INT32, INT4, FLOAT8, HIF8 ND 1-8 √ rstd (aclTensor*) Output Represents the reciprocal of the standard deviation after normalization. It is the reciprocal of Rms(x) in the corresponding formula. - When outputRstd is True, empty tensors are not supported. The shape must be consistent with the leading dimensions of x. The leading dimensions refer to the dimensions of x minus 1, indicating the dimensions that do not require normalization. The -1 axis of rstdOut is 1.
- When outputRstd is False, the final output of this parameter is invalid. A null pointer or an empty tensor can be passed as a placeholder.
FLOAT32 ND 1-8 √ workspaceSize (uint64_t*) Output Returns the size of the workspace to be allocated on the Device. - - - - - executor (aclOpExecutor**) Output Returns the op executor containing the operator computation process. - - - - - 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 The passed-in x, gamma, scale, and y are null pointers. When outputRstd is True, the passed-in rstd is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 When the output yDtype is int32, the size of the last dimension of y does not equal 1/8 of the size of the last dimension of x, and the last dimension of x is not divisible by 8. gamma does not have 1-2 dimensions, beta (if present) does not have 1-2 dimensions, and scale does not have 1 dimension. ACLNN_ERR_INNER_NULLPTR 561103 The number of dimensions and data type of the input or output parameters are out of range. The data type combination of input or output parameters is not within the Constraints. The shapes of input x and output y are not exactly the same. When output yDtype is int4, the size of the last dimension of x is not even. The last dimensions of gamma and beta (if present) are not the same, or the length of the last dimension is not equal to the size of the last dimension of x, or their data types are not the same. The number of dimensions of rstdOut is different from that of x, or the shape of rstdOut is inconsistent with the leading dimensions of the shape of input x (the number of leading dimensions is the number of dimensions of x minus 1), or the last dimension of rstdOut is not 1. The shapes of scale and offsetOptional (if present) are not exactly the same, or the axis length is not equal to 1 or the size of the last dimension of x, or the data types do not meet the following Constraints.
aclnnRmsNormQuantV3
Parameters
Parameter Input/Output Description workspace Input Memory address of the workspace requested on the Device. workspaceSize Input Size of the workspace requested on the Device, obtained by the first-phase API aclnnRmsNormQuantV3GetWorkspaceSize. executor Input Op executor, which contains the operator computation process. stream Input Specifies the stream for executing the task. Return Value
aclnnStatus: Return status code. (For details, see Error Codes.)
Constraints
Ascend 950PR&950DT products : When the data type ofyis INT4, the last dimension ofx,gamma, andbetamust be an even number.
Ascend 950PR&950DT products : When the data type ofyis INT32, the last dimension ofymust be 1/8 of the last dimension ofx.
Supported data types for each product model:
Ascend 950PR&950DT products :
| X Data Type | Gamma Data Type | Scale Data Type | offsetOptional Data Type | betaOptional Data Type | Epsilon Data Type | y Data Type | Rstd Data Type | | --------- | ------------- | ------------- | -------------- |------------- | --------- |--------- |--------- | | FLOAT16 | FLOAT16 | FLOAT16 | INT8 |FLOAT16 | DOUBLE |INT8, INT4, FLOAT8_E4M3FN, FLOAT8_E5M2, HIFLOAT8 | FLOAT32 | | BFLOAT16 | BFLOAT16 | BFLOAT16 | INT8 | BFLOAT16 |DOUBLE |INT8, INT4, FLOAT8_E4M3FN, FLOAT8_E5M2, HIFLOAT8 | FLOAT32 | | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT16 |DOUBLE |INT8, INT4, FLOAT8_E4M3FN, FLOAT8_E5M2, HIFLOAT8 | FLOAT32 | | BFLOAT16 | BFLOAT16 | BFLOAT16 | BFLOAT16 |BFLOAT16 | DOUBLE |INT8, INT4, FLOAT8_E4M3FN, FLOAT8_E5M2, HIFLOAT8 | FLOAT32 | | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 |FLOAT32 | DOUBLE |INT8, INT4, FLOAT8_E4M3FN, FLOAT8_E5M2, HIFLOAT8 | FLOAT32 | | FLOAT16 | FLOAT16 | FLOAT32 | INT32 |FLOAT16 |DOUBLE |INT8, INT4, FLOAT8_E4M3FN, FLOAT8_E5M2, HIFLOAT8 | FLOAT32 | | BFLOAT16 | BFLOAT16 | FLOAT32 | INT32 |BFLOAT16 | DOUBLE |INT8, INT4, FLOAT8_E4M3FN, FLOAT8_E5M2, HIFLOAT8 | FLOAT32 | | FLOAT16 | FLOAT16 | FLOAT32 | FLOAT32 | FLOAT16 |DOUBLE |INT8, INT4, FLOAT8_E4M3FN, FLOAT8_E5M2, HIFLOAT8 | FLOAT32 | | BFLOAT16 | BFLOAT16 | FLOAT32 | FLOAT32 | BFLOAT16 |DOUBLE |INT8, INT4, FLOAT8_E4M3FN, FLOAT8_E5M2, HIFLOAT8 | FLOAT32 |
- Deterministic computation:
- aclnnRmsNormQuantV3 defaults to a deterministic implementation.
Example
The following sample code is for reference only. For the compilation and execution procedure, see Compile and Run Samples.
#include <iostream>
#include <vector>
#include <cmath>
#include <cstring>
#include <memory>
#include "acl/acl.h"
#include "aclnnop/aclnn_rms_norm_quant_v3.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)
{
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;
}
// Check whether the current hardware is supported.
bool CheckHardwareSupport()
{
const char* socName = aclrtGetSocName();
if (socName == nullptr) {
LOG_PRINT("Warning: Cannot get SOC name, skip hardware check\n");
return true;
}
LOG_PRINT("Current SOC: %s\n", socName);
// This operator supports only Ascend950.
if (strstr(socName, "Ascend950") != nullptr || strstr(socName, "ascend950") != nullptr) {
return true;
}
LOG_PRINT("Warning: This operator only supports Ascend950, current SOC '%s' is not supported. Skip test.\n",
socName);
return false;
}
void Finalize(int32_t deviceId, aclrtStream stream)
{
(void)aclrtDestroyStream(stream);
(void)aclrtResetDevice(deviceId);
(void)aclFinalize();
}
template <typename T>
int CreateAclTensor(
const std::vector<T>& hostData, const std::vector<int64_t>& shape, void** deviceAddr, aclDataType dataType,
aclTensor** tensor)
{
auto size = GetShapeSize(shape) * sizeof(T);
// Call aclrtMalloc to allocate device-side memory.
auto ret = aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMalloc failed. ERROR: %d\n", ret); return ret);
// Call aclrtMemcpy to copy host-side data to device-side memory.
ret = aclrtMemcpy(*deviceAddr, size, hostData.data(), size, ACL_MEMCPY_HOST_TO_DEVICE);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMemcpy failed. ERROR: %d\n", ret); return ret);
// Calculate the strides of the contiguous tensor.
std::vector<int64_t> strides(shape.size(), 1);
for (int64_t i = shape.size() - 2; i >= 0; i--) {
strides[i] = shape[i + 1] * strides[i + 1];
}
// Call aclCreateTensor to create an aclTensor.
*tensor = aclCreateTensor(
shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND, shape.data(), shape.size(),
*deviceAddr);
return 0;
}
int main()
{
// 1. (boilerplate) device/stream initialization
int32_t deviceId = 0;
aclrtStream stream;
auto ret = Init(deviceId, &stream);
CHECK_RET(ret == 0, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
// Check hardware support
if (!CheckHardwareSupport()) {
LOG_PRINT("\n=== Test SKIPPED (hardware not supported) ===\n");
Finalize(deviceId, stream);
return 0;
}
// 2. Construct input and output
// x: [2, 64] FP16, gamma: [64] FP16, scale: [1] FP16, offset: [1] INT8, beta: [64] FP16
// y: [2, 64] INT8, rstd: [2, 1] FLOAT32
std::vector<int64_t> x_shape = {2, 64};
std::vector<int64_t> gamma_shape = {64};
std::vector<int64_t> scale_shape = {1};
std::vector<int64_t> offset_shape = {1};
std::vector<int64_t> beta_shape = {64};
std::vector<int64_t> y_shape = {2, 64};
std::vector<int64_t> rstd_shape = {2, 1};
void* x_device_addr = nullptr;
void* gamma_device_addr = nullptr;
void* scale_device_addr = nullptr;
void* offset_device_addr = nullptr;
void* beta_device_addr = nullptr;
void* y_device_addr = nullptr;
void* rstd_device_addr = nullptr;
void* workspace_addr = nullptr;
aclTensor* x = nullptr;
aclTensor* gamma = nullptr;
aclTensor* scale = nullptr;
aclTensor* offset = nullptr;
aclTensor* beta = nullptr;
aclTensor* y = nullptr;
aclTensor* rstd = nullptr;
// Initialize input data
// x: FP16 0.5 = 0x3800
std::vector<uint16_t> x_host_data(GetShapeSize(x_shape), 0x3800);
// gamma: FP16 1.5 = 0x3e00
std::vector<uint16_t> gamma_host_data(GetShapeSize(gamma_shape), 0x3e00);
// scale: FP16 1.0 = 0x3C00
std::vector<uint16_t> scale_host_data(GetShapeSize(scale_shape), 0x3C00);
// offset: INT8 0
std::vector<int8_t> offset_host_data(GetShapeSize(offset_shape), 0);
// beta: FP16 0.0 = 0x0000
std::vector<uint16_t> beta_host_data(GetShapeSize(beta_shape), 0);
// Initialize output
std::vector<int8_t> y_host_data(GetShapeSize(y_shape), 0);
std::vector<float> rstd_host_data(GetShapeSize(rstd_shape), 0.0f);
double epsilon = 1e-6;
bool divMode = true;
bool outputRstd = true;
LOG_PRINT("Input x shape: [2, 64], dtype: FP16\n");
LOG_PRINT("Gamma shape: [64], dtype: FP16\n");
LOG_PRINT("Scale shape: [1], dtype: FP16\n");
LOG_PRINT("Offset shape: [1], dtype: INT8\n");
LOG_PRINT("Beta shape: [64], dtype: FP16\n");
LOG_PRINT("Output y shape: [2, 64], dtype: INT8\n");
LOG_PRINT("Output rstd shape: [2, 1], dtype: FLOAT32\n");
LOG_PRINT("epsilon: %e, divMode: %s, outputRstd: %s\n",
epsilon, divMode ? "true" : "false", outputRstd ? "true" : "false");
// Create x aclTensor (FP16)
ret = CreateAclTensor(x_host_data, x_shape, &x_device_addr, aclDataType::ACL_FLOAT16, &x);
std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor*)> xTensorPtr(x, aclDestroyTensor);
std::unique_ptr<void, aclError (*)(void*)> xDeviceAddrPtr(x_device_addr, aclrtFree);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("CreateAclTensor x failed. ERROR: %d\n", ret);
Finalize(deviceId, stream); return ret);
// Create gamma aclTensor (FP16)
ret = CreateAclTensor(gamma_host_data, gamma_shape, &gamma_device_addr, aclDataType::ACL_FLOAT16, &gamma);
std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor*)> gammaTensorPtr(gamma, aclDestroyTensor);
std::unique_ptr<void, aclError (*)(void*)> gammaDeviceAddrPtr(gamma_device_addr, aclrtFree);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("CreateAclTensor gamma failed. ERROR: %d\n", ret);
Finalize(deviceId, stream); return ret);
// Create scale aclTensor (FP16)
ret = CreateAclTensor(scale_host_data, scale_shape, &scale_device_addr, aclDataType::ACL_FLOAT16, &scale);
std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor*)> scaleTensorPtr(scale, aclDestroyTensor);
std::unique_ptr<void, aclError (*)(void*)> scaleDeviceAddrPtr(scale_device_addr, aclrtFree);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("CreateAclTensor scale failed. ERROR: %d\n", ret);
Finalize(deviceId, stream); return ret);
// Create offset aclTensor (INT8)
ret = CreateAclTensor(offset_host_data, offset_shape, &offset_device_addr, aclDataType::ACL_INT8, &offset);
std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor*)> offsetTensorPtr(offset, aclDestroyTensor);
std::unique_ptr<void, aclError (*)(void*)> offsetDeviceAddrPtr(offset_device_addr, aclrtFree);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("CreateAclTensor offset failed. ERROR: %d\n", ret);
Finalize(deviceId, stream); return ret);
// Create beta aclTensor (FP16)
ret = CreateAclTensor(beta_host_data, beta_shape, &beta_device_addr, aclDataType::ACL_FLOAT16, &beta);
std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor*)> betaTensorPtr(beta, aclDestroyTensor);
std::unique_ptr<void, aclError (*)(void*)> betaDeviceAddrPtr(beta_device_addr, aclrtFree);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("CreateAclTensor beta failed. ERROR: %d\n", ret);
Finalize(deviceId, stream); return ret);
// Create y aclTensor (INT8 output)
ret = CreateAclTensor(y_host_data, y_shape, &y_device_addr, aclDataType::ACL_INT8, &y);
std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor*)> yTensorPtr(y, aclDestroyTensor);
std::unique_ptr<void, aclError (*)(void*)> yDeviceAddrPtr(y_device_addr, aclrtFree);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("CreateAclTensor y failed. ERROR: %d\n", ret);
Finalize(deviceId, stream); return ret);
// Create the rstd aclTensor (FLOAT32 output).
ret = CreateAclTensor(rstd_host_data, rstd_shape, &rstd_device_addr, aclDataType::ACL_FLOAT, &rstd);
std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor*)> rstdTensorPtr(rstd, aclDestroyTensor);
std::unique_ptr<void, aclError (*)(void*)> rstdDeviceAddrPtr(rstd_device_addr, aclrtFree);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("CreateAclTensor rstd failed. ERROR: %d\n", ret);
Finalize(deviceId, stream); return ret);
// 3. Call the CANN operator library API.
uint64_t workspace_size = 0;
aclOpExecutor* executor = nullptr;
// Call the first-phase API of aclnnRmsNormQuantV3.
LOG_PRINT("Calling aclnnRmsNormQuantV3GetWorkspaceSize...\n");
ret = aclnnRmsNormQuantV3GetWorkspaceSize(
x, gamma, scale, offset, beta, epsilon, divMode, outputRstd,
y, rstd, &workspace_size, &executor);
CHECK_RET(ret == ACL_SUCCESS,
LOG_PRINT("aclnnRmsNormQuantV3GetWorkspaceSize failed. ERROR: %d\n", ret);
Finalize(deviceId, stream); return ret);
LOG_PRINT("Workspace size: %lu bytes (%.2f KB)\n", workspace_size, workspace_size / 1024.0);
// Allocate device memory based on the workspaceSize calculated by the first-phase API.
std::unique_ptr<void, aclError (*)(void*)> workspaceAddrPtr(nullptr, aclrtFree);
if (workspace_size > 0) {
ret = aclrtMalloc(&workspace_addr, workspace_size, ACL_MEM_MALLOC_HUGE_FIRST);
CHECK_RET(ret == ACL_SUCCESS,
LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret);
Finalize(deviceId, stream); return ret);
workspaceAddrPtr.reset(workspace_addr);
}
// Call the second-phase API of aclnnRmsNormQuantV3.
LOG_PRINT("Calling aclnnRmsNormQuantV3...\n");
ret = aclnnRmsNormQuantV3(workspaceAddrPtr.get(), workspace_size, executor, stream);
CHECK_RET(ret == ACL_SUCCESS,
LOG_PRINT("aclnnRmsNormQuantV3 failed. ERROR: %d\n", ret);
Finalize(deviceId, stream); return ret);
// 4. Synchronize and wait for the task to complete.
ret = aclrtSynchronizeStream(stream);
CHECK_RET(ret == ACL_SUCCESS,
LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret);
Finalize(deviceId, stream); return ret);
// 5. Obtain the output values and copy the results from the device side to the host side.
{
// Copy the y result (INT8).
auto size = GetShapeSize(y_shape);
std::vector<int8_t> y_result(size, 0);
ret = aclrtMemcpy(y_result.data(), y_result.size() * sizeof(y_result[0]),
yDeviceAddrPtr.get(), size * sizeof(int8_t), ACL_MEMCPY_DEVICE_TO_HOST);
CHECK_RET(ret == ACL_SUCCESS,
LOG_PRINT("copy y from device to host failed. ERROR: %d\n", ret);
Finalize(deviceId, stream); return ret);
LOG_PRINT("Output y (first 10 values):\n");
for (int64_t i = 0; i < std::min(size, (int64_t)10); i++) {
LOG_PRINT(" y[%ld] = %d\n", i, y_result[i]);
}
// Copy the rstd result (FLOAT32).
size = GetShapeSize(rstd_shape);
std::vector<float> rstd_result(size, 0.0f);
ret = aclrtMemcpy(rstd_result.data(), rstd_result.size() * sizeof(rstd_result[0]),
rstdDeviceAddrPtr.get(), size * sizeof(float), ACL_MEMCPY_DEVICE_TO_HOST);
CHECK_RET(ret == ACL_SUCCESS,
LOG_PRINT("copy rstd from device to host failed. ERROR: %d\n", ret);
Finalize(deviceId, stream); return ret);
LOG_PRINT("Output rstd values:\n");
for (int64_t i = 0; i < size; i++) {
LOG_PRINT(" rstd[%ld] = %f\n", i, rstd_result[i]);
}
}
LOG_PRINT("\n=== RmsNormQuantV3 Test PASSED ===\n");
// 6. Resources are automatically released.
Finalize(deviceId, stream);
return 0;
}