aclnnAddRmsNormQuantV2

📄 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: 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 part. The AddRmsNormQuant operator fuses the Add operator before RmsNorm with the Quantize operator after RmsNorm, reducing data movement operations. Compared with AddRmsNormQuant, the AddRmsNormQuantV2 operator adds the bias term betaOptional parameter and the RmsNorm normalization result output rmsNormOut.

  • Formula:

    xi=x1i+x2ix_i={x1}_i+{x2}_i yi=1Rms⁡(x)∗xi∗gi+betai, where Rms⁡(x)=1n∑i=1nxi2+epsy_i=\frac{1}{\operatorname{Rms}(\mathbf{x})} * x_i * g_i + beta_i, \quad \text { where } \operatorname{Rms}(\mathbf{x})=\sqrt{\frac{1}{n} \sum_{i=1}^n x_i^2+eps} rmsNormOuti=1Rms⁡(x)∗xi∗girmsNormOut_i=\frac{1}{\operatorname{Rms}(\mathbf{x})} * x_i * g_i
    • When divMode is true:

      y1Out=round((y/scales1)+zero_points1)y1Out=round((y/scales1)+zero\_points1) y2Out=round((y/scales2)+zero_points2)y2Out=round((y/scales2)+zero\_points2)
    • When divMode is false:

      y1Out=round((y∗scales1)+zero_points1)y1Out=round((y*scales1)+zero\_points1) y2Out=round((y∗scales2)+zero_points2)y2Out=round((y*scales2)+zero\_points2)

    NOTE: rmsNormOut is the RmsNorm normalization result (excluding beta). betaOptional and rmsNormOut are independent of each other and can be used in any combination.

Function Prototype

Each operator uses a two-phase API. You must first call the "aclnnAddRmsNormQuantV2GetWorkspaceSize" API to obtain the workspace size required for the computation and the executor containing the operator computation process, and then call the "aclnnAddRmsNormQuantV2" API to perform the computation.

aclnnStatus aclnnAddRmsNormQuantV2GetWorkspaceSize(
  const aclTensor *x1,
  const aclTensor *x2,
  const aclTensor *gamma,
  const aclTensor *scales1,
  const aclTensor *scales2Optional,
  const aclTensor *zeroPoints1Optional,
  const aclTensor *zeroPoints2Optional,
  const aclTensor *betaOptional,
  int64_t          axis,
  double           epsilon,
  bool             divMode,
  aclTensor       *y1Out,
  aclTensor       *y2Out,
  aclTensor       *xOut,
  aclTensor       *rmsNormOut,
  uint64_t        *workspaceSize,
  aclOpExecutor  **executor)
aclnnStatus aclnnAddRmsNormQuantV2(
  void          *workspace,
  uint64_t       workspaceSize,
  aclOpExecutor *executor,
  aclrtStream    stream)

aclnnAddRmsNormQuantV2GetWorkspaceSize

  • Parameters

    Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor
    x1 (const aclTensor*) Input Source data tensor in the normalization process. Corresponds to `x1` in the formula. Supports null Tensor. FLOAT32, FLOAT16, BFLOAT16 ND 1-8 √
    x2 (const aclTensor*) Input Source data tensor in the normalization process. Corresponds to `x2` in the formula.
    • Supports empty tensors.
    • The shape and data type must remain consistent with `x1`.
    FLOAT32, FLOAT16, BFLOAT16 ND 1-8 √
    gamma (const aclTensor*) Input Weight tensor in the normalization process. Corresponds to `g` in the formula.
    • Supports empty tensors.
    • The shape must remain consistent with the dimensions of `x1` requiring norm (layer normalization), and the data type must remain consistent with `x1`.
    FLOAT32, FLOAT16, BFLOAT16 ND 1-8 √
    scales1 (const aclTensor*) Input Indicates the scales tensor used to obtain y1Out during quantization, corresponding to `scales1` in the formula.
    • Supports null tensor.
    • The shape must remain consistent with `gamma`.
    FLOAT32, FLOAT16, BFLOAT16 ND 1-8 √
    scales2Optional (const aclTensor*) Input Indicates the scales tensor used to obtain y2Out during quantization. Corresponds to `scales2` in the formula.
    • Supports null Tensor.
    • Optional parameter, supports passing in a null pointer. The shape and data type must remain consistent with those of `scales1`. For the combined constraints with `zeroPoints2Optional`, see Constraints.
    • When the value of parameter `divMode` is True, the value of this parameter cannot be 0.
    FLOAT32, FLOAT16, BFLOAT16 ND 1-8 √
    zeroPoints1Optional (const aclTensor*) Input Indicates the offset tensor used to obtain y1Out during quantization. Corresponds to `zero_points1` in the formula.
    • Supports null Tensor.
    • Optional parameter, supports passing in null pointer. shape remains consistent with `scales1`.
    INT32, FLOAT32, FLOAT16, BFLOAT16 ND 1-8 √
    zeroPoints2Optional (const aclTensor*) Input Indicates the offset tensor used to obtain y2Out during quantization. Corresponds to `zero_points2` in the formula.
    • Supports null tensor.
    • Optional parameter, supports passing in a null pointer. The shape must remain consistent with `scales1`, and the data type must remain consistent with `zeroPoints1Optional`. For combination constraints with `scales2Optional`, see Constraints.
    INT32, FLOAT32, FLOAT16, BFLOAT16 ND 1-8 √
    betaOptional (const aclTensor*) Input Represents the bias term in the normalization process. Corresponds to `beta` in the formula.
    • Supports null tensor.
    • Optional parameter, supports passing in a null pointer.
    • The shape and data type must remain consistent with `gamma`.
    FLOAT32, FLOAT16, BFLOAT16 ND 1-8 √
    axis (int64_t) Input Indicates the elementwise axis to be quantized. Other axes are broadcast. The specified axis cannot exceed the number of dimensions of input `x1`. Only -1 is supported currently, and other values do not take effect. - - - - -
    epsilon (double) Input Input eps in the formula, used to prevent division-by-zero errors. The data type is double. A small positive value is recommended. - - - - -
    divMode (bool) Input Parameter that determines whether division is used in the quantization formula. The data type is bool. Supports True and False. - - - -
    y1Out (aclTensor*) Output Quantized output tensor, corresponding to `y1Out` in the formula.
    • Empty tensors are supported.
    • The shape is consistent with that of inputs `x1`/`x2`.
    INT8, HIFLOAT8, FLOAT8_E5M2, FLOAT8_E4M3FN ND 1-8 √
    y2Out (aclTensor*) Output Indicates the quantization output tensor, corresponding to `y2Out` in the formula.
    • Empty tensors are supported.
    • The shape and data type remain consistent with `y1Out`.
    INT8, HIFLOAT8, FLOAT8_E5M2, FLOAT8_E4M3FN ND 1-8 √
    xOut (aclTensor*) Output Represents the sum of x1 and x2, corresponding to `x` in the corresponding formula.
    • Supports null tensor.
    • The shape and data type remain consistent with `x1`.
    FLOAT32, FLOAT16, BFLOAT16 ND 1-8 √
    rmsNormOut (aclTensor*) Output Represents the result after RmsNorm (excluding beta), corresponding to `rmsNormOut` in the formula.
    • Supports empty tensors.
    • Optional output; supports passing in a null pointer. If a null pointer is passed in, rmsNormOut is not output.
    • Shape and data type remain consistent with `x1`.
    FLOAT32, FLOAT16, BFLOAT16 ND 1-8 √
    workspaceSize (uint64_t*) Output Returns the size of the workspace to be allocated on the Device side. - - - - -
    executor (aclOpExecutor**) Output Returns the op executor containing the computation flow of the operator. - - - - -
    • Atlas inference products:
      • Data types:
        • Input parameters x1, x2, and gamma, and output parameters xOut and rmsNormOut only support FLOAT16.
        • The input parameters scales1 and scales2Optional only support FLOAT32.
        • The optional parameters zeroPoints1Optional and zeroPoints2Optional only support INT32.
        • Optional parameter betaOptional only supports FLOAT16.
        • The output parameters y1Out and y2Out only support INT8.
      • The input parameter divMode only supports True in the native V2 path. When xOut is non-null, rmsNormOut is null, and other parameters meet the V1 fallback path constraints, the API supports passing in False. It falls back to the V1 path and executes in multiplication mode.
    • Atlas A2 products, Atlas A3 products:
      • Data types:
        • The input parameters x1, x2, and gamma and output parameters xOut and rmsNormOut only support FLOAT16 and BFLOAT16.
        • The input parameters scales1 and scales2Optional only support FLOAT32 and BFLOAT16.
        • The optional parameters zeroPoints1Optional and zeroPoints2Optional only support INT32 and BFLOAT16.
        • The optional parameter betaOptional only supports FLOAT16 and BFLOAT16.
        • The output parameters y1Out and y2Out only support INT8.
      • The input parameter divMode only supports True in the native V2 path. When xOut is non-null, rmsNormOut is null, and other parameters meet the V1 fallback path constraints, the API supports passing in False. It falls back to the V1 path and executes in multiplication mode.
  • 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 input parameter is a required input, output, or required attribute and is a null pointer.
    ACLNN_ERR_PARAM_INVALID 161002
    The data type of the input or output is not within the supported range.

aclnnAddRmsNormQuantV2

  • Parameters

    Parameter Input/Output Description
    workspace Input Workspace memory address applied for on the Device side.
    workspaceSize Input Size of the workspace applied for on the Device side, obtained by calling the first-phase API aclnnAddRmsNormQuantV2GetWorkspaceSize.
    executor Input Op executor, containing the operator computation process.
    stream Input Stream for executing tasks.
  • Return Value

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

Constraints

  • The size of each dimension in the shape of parameters x1, x2, gamma, scales1, scales2Optional, zeroPoints1Optional, zeroPoints2Optional, betaOptional, y1Out, y2Out, xOut, and rmsNormOut must not exceed the maximum INT32 value of 2147483647.
  • Ascend 950PR&950DT products: support passing in a null pointer for scales2Optional while zeroPoints2Optional is non-null. In this case, scales2 of the second quantization is processed as 1; that is, regardless of whether divMode is True or False, y2Out=round(y+zero_points2)y2Out=round(y+zero\_points2) always holds, and the shape of y2Out remains consistent with that of x1.
  • Atlas A3 products, Atlas A2 products, and Atlas inference products: passing in only zeroPoints2Optional without scales2Optional is not supported; when zeroPoints2Optional is non-null, scales2Optional must also be non-null.
  • Atlas inference products: the norm axis length of x1, x2, y1Out, y2Out, xOut, and rmsNormOut, as well as the length of gamma, scales1, scales2Optional, zeroPoints1Optional, zeroPoints2Optional, and betaOptional, must be greater than or equal to 32 Bytes.
  • betaOptional and rmsNormOut are independent of each other:

    • When rmsNormOut is output, betaOptional can be present or absent.
    • When rmsNormOut is not output (a null pointer is passed in), betaOptional can still be present or absent.
    • The computation result of rmsNormOut is 1Rms⁡(x)∗xi∗gi\frac{1}{\operatorname{Rms}(\mathbf{x})} * x_i * g_i, excluding the beta computation.
  • 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.

  • Supported data types for each product model:

    • Atlas A2 products, Atlas A3 products:

| x1 Data Type | x2 Data Type | Gamma Data Type | scales1 Data Type | scales2Optional Data Type | zeroPoints1Optional Data Type | zeroPoints2Optional Data Type | betaOptional Data Type | y1Out Data Type | y2Out Data Type | xOut Data Type | rmsNormOut Data Type | | - | - | - | - | - | - | - | - | - | - | - | - | | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT32 | FLOAT32 | INT32 | INT32 | FLOAT16 | INT8 | INT8 | FLOAT16 | FLOAT16 | | BFLOAT16 | BFLOAT16 | BFLOAT16 | BFLOAT16 | BFLOAT16 | BFLOAT16 | BFLOAT16 | BFLOAT16 | INT8 | INT8 | BFLOAT16 | BFLOAT16 |

  • Atlas inference products:

| x1 Data Type | x2 Data Type | Gamma Data Type | scales1 Data Type | scales2Optional Data Type | zeroPoints1Optional Data Type | zeroPoints2Optional Data Type | betaOptional Data Type | y1Out Data Type | y2Out Data Type | xOut Data Type | rmsNormOut Data Type | | - | - | - | - | - | - | - | - | - | - | - | - | | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT32 | FLOAT32 | INT32 | INT32 | FLOAT16 | INT8 | INT8 | FLOAT16 | FLOAT16 |

  • Ascend 950PR&950DT products:

| x1 Data Type | x2 Data Type | Gamma Data Type | scales1 Data Type | scales2Optional Data Type | zeroPoints1Optional Data Type | zeroPoints2Optional Data Type | betaOptional Data Type | y1Out Data Type | y2Out Data Type | xOut Data Type | rmsNormOut Data Type | | - | - | - | - | - | - | - | - | - | - | - | - | | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT32 | FLOAT32 | INT32 | INT32 | FLOAT16 | INT8 | INT8 | FLOAT16 | FLOAT16 | | BFLOAT16 | BFLOAT16 | BFLOAT16 | FLOAT32 | FLOAT32 | INT32 | INT32 | BFLOAT16 | INT8 | INT8 | BFLOAT16 | BFLOAT16 | | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | INT8 | INT8 | FLOAT32 | FLOAT32 | | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT16 | INT8 | INT8 | FLOAT16 | FLOAT16 | | BFLOAT16 | BFLOAT16 | BFLOAT16 | BFLOAT16 | BFLOAT16 | BFLOAT16 | BFLOAT16 | BFLOAT16 | INT8 | INT8 | BFLOAT16 | BFLOAT16 | | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT16 | INT8 | INT8 | FLOAT16 | FLOAT16 | | BFLOAT16 | BFLOAT16 | BFLOAT16 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | BFLOAT16 | INT8 | INT8 | BFLOAT16 | BFLOAT16 | | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT32 | FLOAT32 | INT32 | INT32 | FLOAT16 | HIFLOAT8 | HIFLOAT8 | FLOAT16 | FLOAT16 | | BFLOAT16 | BFLOAT16 | BFLOAT16 | FLOAT32 | FLOAT32 | INT32 | INT32 | BFLOAT16 | HIFLOAT8 | HIFLOAT8 | BFLOAT16 | BFLOAT16 | | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | HIFLOAT8 | HIFLOAT8 | FLOAT32 | FLOAT32 | | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT16 | HIFLOAT8 | HIFLOAT8 | FLOAT16 | FLOAT16 | | BFLOAT16 | BFLOAT16 | BFLOAT16 | BFLOAT16 | BFLOAT16 | BFLOAT16 | BFLOAT16 | BFLOAT16 | HIFLOAT8 | HIFLOAT8 | BFLOAT16 | BFLOAT16 | | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT16 | HIFLOAT8 | HIFLOAT8 | FLOAT16 | FLOAT16 | | BFLOAT16 | BFLOAT16 | BFLOAT16 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | BFLOAT16 | HIFLOAT8 | HIFLOAT8 | BFLOAT16 | BFLOAT16 | | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT32 | FLOAT32 | INT32 | INT32 | FLOAT16 | FLOAT8_E5M2 | FLOAT8_E5M2 | FLOAT16 | FLOAT16 | | BFLOAT16 | BFLOAT16 | BFLOAT16 | FLOAT32 | FLOAT32 | INT32 | INT32 | BFLOAT16 | FLOAT8_E5M2 | FLOAT8_E5M2 | BFLOAT16 | BFLOAT16 | | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT8_E5M2 | FLOAT8_E5M2 | FLOAT32 | FLOAT32 | | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT8_E5M2 | FLOAT8_E5M2 | FLOAT16 | FLOAT16 | | BFLOAT16 | BFLOAT16 | BFLOAT16 | BFLOAT16 | BFLOAT16 | BFLOAT16 | BFLOAT16 | BFLOAT16 | FLOAT8_E5M2 | FLOAT8_E5M2 | BFLOAT16 | BFLOAT16 | | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT16 | FLOAT8_E5M2 | FLOAT8_E5M2 | FLOAT16 | FLOAT16 | | BFLOAT16 | BFLOAT16 | BFLOAT16 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | BFLOAT16 | FLOAT8_E5M2 | FLOAT8_E5M2 | BFLOAT16 | BFLOAT16 | | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT32 | FLOAT32 | INT32 | INT32 | FLOAT16 | FLOAT8_E4M3FN | FLOAT8_E4M3FN | FLOAT16 | FLOAT16 | | BFLOAT16 | BFLOAT16 | BFLOAT16 | FLOAT32 | FLOAT32 | INT32 | INT32 | BFLOAT16 | FLOAT8_E4M3FN | FLOAT8_E4M3FN | BFLOAT16 | BFLOAT16 | | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT8_E4M3FN | FLOAT8_E4M3FN | FLOAT32 | FLOAT32 | | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT8_E4M3FN | FLOAT8_E4M3FN | FLOAT16 | FLOAT16 | | BFLOAT16 | BFLOAT16 | BFLOAT16 | BFLOAT16 | BFLOAT16 | BFLOAT16 | BFLOAT16 | BFLOAT16 | FLOAT8_E4M3FN | FLOAT8_E4M3FN | BFLOAT16 | BFLOAT16 | | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT16 | FLOAT8_E4M3FN | FLOAT8_E4M3FN | FLOAT16 | FLOAT16 | | BFLOAT16 | BFLOAT16 | BFLOAT16 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | BFLOAT16 | FLOAT8_E4M3FN | FLOAT8_E4M3FN | BFLOAT16 | BFLOAT16 |

The data types of `x1`, `x2`, `gamma`, non-null `betaOptional`, `xOut`, and non-null `rmsNormOut` must be the same; the data types of `scales1` and non-null `scales2Optional` must be the same; when `zeroPoints1Optional` and `zeroPoints2Optional` are both non-null, their data types must be the same; the data types of `y1Out` and `y2Out` must be the same. The data type combinations of quantization parameters must follow those in the table.
  • Deterministic computation:
    • aclnnAddRmsNormQuantV2 defaults to a deterministic implementation.

Example

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

#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_add_rms_norm_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); 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,
    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-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);

    // Compute strides for 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) Initialize the device/stream. Refer to the acl API manual.
    // Fill in the deviceId based on the actual device.
    int32_t deviceId = 0;
    aclrtStream stream;
    auto ret = Init(deviceId, &stream);
    // Handle the check as needed.
    CHECK_RET(ret == 0, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
    // 2. Construct the input and output. Customize the construction based on the API.
    std::vector<int64_t> xShape = {64, 32};
    std::vector<int64_t> gammaShape = {32};
    std::vector<int64_t> yShape = {64, 32};
    long long xShapeSize = GetShapeSize(xShape);
    long long gammaShapeSize = GetShapeSize(gammaShape);
    void* x1DeviceAddr = nullptr;
    void* x2DeviceAddr = nullptr;
    void* gammaDeviceAddr = nullptr;
    void* betaDeviceAddr = nullptr;
    void* scales1DeviceAddr = nullptr;
    void* zeroPoints1DeviceAddr = nullptr;
    void* y1DeviceAddr = nullptr;
    void* y2DeviceAddr = nullptr;
    void* xDeviceAddr = nullptr;
    void* rmsNormOutDeviceAddr = nullptr;
    aclTensor* x1 = nullptr;
    aclTensor* x2 = nullptr;
    aclTensor* gamma = nullptr;
    aclTensor* beta = nullptr;
    aclTensor* scales1 = nullptr;
    aclTensor* zeroPoints1 = nullptr;
    aclTensor* y1 = nullptr;
    aclTensor* y2 = nullptr;
    aclTensor* x = nullptr;
    aclTensor* rmsNormOut = nullptr;
    std::vector<int16_t> x1HostData(xShapeSize, 0);
    std::vector<int16_t> x2HostData(xShapeSize, 0);
    std::vector<int16_t> gammaHostData(gammaShapeSize, 0);
    std::vector<int16_t> betaHostData(gammaShapeSize, 0);
    std::vector<float> scales1HostData(gammaShapeSize, 1);
    std::vector<int32_t> zeroPoints1HostData(gammaShapeSize, 100);
    std::vector<int8_t> y1HostData(xShapeSize, 0);
    std::vector<int8_t> y2HostData(xShapeSize, 0);
    std::vector<int16_t> xHostData(xShapeSize, 0);
    std::vector<int16_t> rmsNormOutHostData(xShapeSize, 0);
    float epsilon = 1e-6;
    int64_t axis = -1;
    bool divMode = true;
    // Create the x1 aclTensor.
    ret = CreateAclTensor(x1HostData, xShape, &x1DeviceAddr, aclDataType::ACL_FLOAT16, &x1);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create the x2 aclTensor.
    ret = CreateAclTensor(x2HostData, xShape, &x2DeviceAddr, aclDataType::ACL_FLOAT16, &x2);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create the gamma aclTensor.
    ret = CreateAclTensor(gammaHostData, gammaShape, &gammaDeviceAddr, aclDataType::ACL_FLOAT16, &gamma);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create the beta aclTensor.
    ret = CreateAclTensor(betaHostData, gammaShape, &betaDeviceAddr, aclDataType::ACL_FLOAT16, &beta);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create the scales1 aclTensor.
    ret = CreateAclTensor(scales1HostData, gammaShape, &scales1DeviceAddr, aclDataType::ACL_FLOAT, &scales1);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create the zeroPoints1 aclTensor.
    ret =
        CreateAclTensor(zeroPoints1HostData, gammaShape, &zeroPoints1DeviceAddr, aclDataType::ACL_INT32, &zeroPoints1);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create the y1 aclTensor.
    ret = CreateAclTensor(y1HostData, yShape, &y1DeviceAddr, aclDataType::ACL_INT8, &y1);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create the y2 aclTensor.
    ret = CreateAclTensor(y2HostData, yShape, &y2DeviceAddr, aclDataType::ACL_INT8, &y2);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create the x aclTensor.
    ret = CreateAclTensor(xHostData, xShape, &xDeviceAddr, aclDataType::ACL_FLOAT16, &x);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create the rmsNormOut aclTensor.
    ret = CreateAclTensor(rmsNormOutHostData, xShape, &rmsNormOutDeviceAddr, aclDataType::ACL_FLOAT16, &rmsNormOut);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // 3. Call the CANN operator library API. Replace it with the specific API.
    uint64_t workspaceSize = 0;
    aclOpExecutor* executor;
    // Call the first-phase API of aclnnAddRmsNormQuantV2.
    ret = aclnnAddRmsNormQuantV2GetWorkspaceSize(
        x1, x2, gamma, scales1, nullptr, zeroPoints1, nullptr, beta, axis, epsilon, divMode, y1, y2, x, rmsNormOut,
        &workspaceSize, &executor);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnAddRmsNormQuantV2GetWorkspaceSize 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 aclnnAddRmsNormQuantV2
    ret = aclnnAddRmsNormQuantV2(workspaceAddr, workspaceSize, executor, stream);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnAddRmsNormQuantV2 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 values and copy the results from device memory to host memory. Modify as required based on the specific API definition.
    auto size = GetShapeSize(yShape);
    std::vector<int8_t> resultData(size, 0);
    ret = aclrtMemcpy(
        resultData.data(), resultData.size() * sizeof(resultData[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, resultData[i]);
    }

    // 6. Release aclTensor and aclScalar. Modify as required based on the specific API definition.
    aclDestroyTensor(x1);
    aclDestroyTensor(x2);
    aclDestroyTensor(gamma);
    aclDestroyTensor(beta);
    aclDestroyTensor(scales1);
    aclDestroyTensor(zeroPoints1);
    aclDestroyTensor(y1);
    aclDestroyTensor(y2);
    aclDestroyTensor(x);
    aclDestroyTensor(rmsNormOut);

    // 7. Release device resources. Modify as required based on the specific API definition.
    aclrtFree(x1DeviceAddr);
    aclrtFree(x2DeviceAddr);
    aclrtFree(gammaDeviceAddr);
    aclrtFree(betaDeviceAddr);
    aclrtFree(scales1DeviceAddr);
    aclrtFree(zeroPoints1DeviceAddr);
    aclrtFree(y1DeviceAddr);
    aclrtFree(y2DeviceAddr);
    aclrtFree(xDeviceAddr);
    aclrtFree(rmsNormOutDeviceAddr);

    if (workspaceSize > 0) {
        aclrtFree(workspaceAddr);
    }
    aclrtDestroyStream(stream);
    aclrtResetDevice(deviceId);
    aclFinalize();
    return 0;
}