aclnnAddRmsNorm

📄 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 commonly used normalization operation in large models. Compared with the LayerNorm operator, it removes the part of subtracting the mean. The AddRmsNorm operator fuses the Add operator before RMSNorm to reduce data movement.
  • Formula:xi=x1i+x2ix_i=x1_{i}+x2_{i} RmsNorm⁡(xi)=xiRms⁡(x)gammai, where Rms⁡(x)=1n∑i=1nxi2+epsilon\operatorname{RmsNorm}(x_i)=\frac{x_i}{\operatorname{Rms}(\mathbf{x})} gamma_i, \quad \text { where } \operatorname{Rms}(\mathbf{x})=\sqrt{\frac{1}{n} \sum_{i=1}^n x_i^2+epsilon}

Function Prototype

Each operator uses a two-phase API. First call the aclnnAddRmsNormGetWorkspaceSize API to obtain the workspace size required for the computation and the executor containing the operator computation process. Then call the aclnnAddRmsNorm API to execute the computation.

aclnnStatus aclnnAddRmsNormGetWorkspaceSize(
  const aclTensor *x1,
  const aclTensor *x2,
  const aclTensor *gamma,
  double           epsilon,
  aclTensor       *yOut,
  aclTensor       *rstdOut,
  aclTensor       *xOut,
  uint64_t        *workspaceSize,
  aclOpExecutor  **executor)
aclnnStatus aclnnAddRmsNorm(
  void          *workspace,
  uint64_t       workspaceSize,
  aclOpExecutor *executor,
  aclrtStream    stream)

aclnnAddRmsNormGetWorkspaceSize

  • Parameters

    Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor
    x1 (aclTensor*) Input First input for the Add computation. It corresponds to `x1` in the formula. Empty tensors are supported. FLOAT32, FLOAT16, BFLOAT16 ND 1-8 √
    x2 (aclTensor*) Input Second input for the Add computation. It corresponds to `x2` in the formula.
    • Supports empty tensors.
    • The shape and data type must be the same as those of `x1`.
    FLOAT32, FLOAT16, BFLOAT16 ND 1-8 √
    gamma (aclTensor*) Input Scaling factor (weight) of RmsNorm. Corresponds to `gamma` in the formula.
    • Supports empty tensors.
    • The data type must be the same as that of `x1`.
    FLOAT32, FLOAT16, BFLOAT16 ND 1-8 √
    epsilon (double) Input Value added to the denominator to ensure numerical stability. Corresponds to `epsilon` in the formula.
    • The value of epsilon must be greater than or equal to zero.
    • Recommended value: 1e-6.
    - - - -
    yOut (aclTensor*) Output Represents the final output. Corresponds to `RmsNorm(x)` in the formula.
    • Empty tensors are supported.
    • The shape and data type are the same as those of input `x1`.
    FLOAT32, FLOAT16, BFLOAT16 ND 1-8 √
    rstdOut (aclTensor*) Output Represents the reciprocal of the standard deviation after normalization. Corresponds to the reciprocal of `Rms(x)` in the formula.
    • Supports empty tensors.
    • The number of dimensions is the same as that of `x1`. Dimensions that do not require normalization are the same as the corresponding dimensions of `x1`, and dimensions that require normalization (the last few dimensions with the same count as `gamma`) are all 1. Examples of the relationship between the rstdOut shape, x1 shape, and gamma shape: if the x1 shape is (2,3,4,8) and the gamma shape is (8), the rstdOut shape is (2,3,4,1); if the x1 shape is (2,3,4,8) and the gamma shape is (4,8), the rstdOut shape is (2,3,1,1).
    • If nullptr is passed in, the final output of this parameter is invalid.
    FLOAT32 ND 1-8 √
    xOut(aclTensor*) Output Represents the result of the Add computation. Corresponds to `x` in the formula.
    • Supports empty tensors.
    • The shape and data type must be the same as those of input `x1`.
    • If `xOut` is nullptr, the final output of this parameter is invalid.
    FLOAT32, FLOAT16, BFLOAT16 ND 1-8 √
    workspaceSize (uint64_t*) Output Returns the workspace size to be requested on the Device side. - - - - -
    executor (aclOpExecutor**) Output Returns the op executor that contains the operator computation flow. - - - - -
    • Atlas inference products:
      • The data type of parameters x1, x2, gamma, yOut, and xOut does not support BFLOAT16.
      • The parameter rstdOut is invalid in the current product usage scenario.
  • 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 input x1, x2, gamma, or yOut is a null pointer.
    When the preset value of rstdOut is not nullptr, the preset value of xOut is nullptr.
    ACLNN_ERR_PARAM_INVALID 161002 The data type of the input or output is out of the supported range.
    The input and output parameters do not meet the constraints in Parameters.

aclnnAddRmsNorm

  • Parameters

    Parameter Input/Output Description
    workspace Input Workspace memory address allocated on the Device.
    workspaceSize Input Workspace size allocated on the Device, obtained by the first-phase API aclnnAddRmsNormGetWorkspaceSize.
    executor Input Executor of op, which contains the operator computation flow.
    stream Input Stream used to execute tasks.
  • Return Value

    aclnnStatus: Return status code. (For details, see aclnn Error Codes.)

Constraints

  • Boundary value scenarios

    • When the input is Inf, the output is Inf.
    • When the input is NaN, the output is NaN.
  • The supported combinations of inputs x1, x2, gamma, yOut, rstdOut, and xOut are as follows:

    • Atlas A2 products, Atlas A3 products, Ascend 950PR&950DT products:

| x1 | x2 | Gamma | yOut | rstdOut | xOut | | --------| --------| --------| --------| --------| :------ | | FLOAT32 | FLOAT32 | shape must be consistent with the last few dimensions of x1, where the last few dimensions are the dimensions of x1 to be normalized; FLOAT32 | FLOAT32 | Required | Required, FLOAT32 | | FLOAT16 | FLOAT16 | shape must be consistent with the last few dimensions of x1, where the last few dimensions are the dimensions of x1 to be normalized; FLOAT16 | FLOAT16 | Required | Required, FLOAT16 | | BFLOAT16 | BFLOAT16 | shape must be consistent with the last few dimensions of x1, where the last few dimensions are the dimensions of x1 to be normalized; BFLOAT16 | BFLOAT16 | Required | Required, BFLOAT16 | | FLOAT16 | FLOAT16 | shape is [1, the last dimension of x1]; FLOAT16 | FLOAT16 | Null pointer. | null pointer, FLOAT16 | | BFLOAT16 | BFLOAT16 | shape is [1, the last dimension of x1]; BFLOAT16 | BFLOAT16 | Null pointer. | null pointer, BFLOAT16 | | FLOAT16 | FLOAT16 | shape is [1, the last dimension of x1]; FLOAT16 | FLOAT16 | Null pointer. | Required, FLOAT16 | | BFLOAT16 | BFLOAT16 | shape is [1, the last dimension of x1]; BFLOAT16 | BFLOAT16 | Null pointer. | Required, BFLOAT16 |

  • Atlas inference products:

| x1 | x2 | Gamma | yOut | rstdOut | xOut | | --------| --------| --------| --------| --------| :------ | | FLOAT32 | FLOAT32 | shape must be consistent with the last few dimensions of x1, where the last few dimensions are the dimensions of x1 to be normalized; FLOAT32 | FLOAT32 | Required | Required, FLOAT32 | | FLOAT16 | FLOAT16 | shape must be consistent with the last few dimensions of x1, where the last few dimensions are the dimensions of x1 to be normalized; FLOAT16 | FLOAT16 | Required | Required, FLOAT16 | | FLOAT16 | FLOAT16 | shape is [1, the last dimension of x1]; FLOAT16 | FLOAT16 | Null pointer. | null pointer, FLOAT16 | | FLOAT16 | FLOAT16 | shape is [1, the last dimension of x1]; FLOAT16 | FLOAT16 | Null pointer. | Required, FLOAT16 |

  • Deterministic computation:
    • aclnnAddRmsNorm defaults to a deterministic implementation.

Example

The sample code is for reference only. For details about compiling and running, see Compile and Run Samples.

#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_add_rms_norm.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, 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 data from the host to 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);

    // Compute 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) Initialize device/stream. 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 required.
    CHECK_RET(ret == 0, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
    // 2. Construct inputs and outputs. Customize them based on the API.
    std::vector<int64_t> xShape = {2, 16};
    std::vector<int64_t> gammaShape = {16};
    std::vector<int64_t> yShape = {2, 16};
    std::vector<int64_t> rstdShape = {2, 1};
    void* x1DeviceAddr = nullptr;
    void* x2DeviceAddr = nullptr;
    void* gammaDeviceAddr = nullptr;
    void* yDeviceAddr = nullptr;
    void* rstdDeviceAddr = nullptr;
    void* xDeviceAddr = nullptr;
    aclTensor* x1 = nullptr;
    aclTensor* x2 = nullptr;
    aclTensor* gamma = nullptr;
    aclTensor* y = nullptr;
    aclTensor* rstd = nullptr;
    aclTensor* x = nullptr;
    std::vector<float> x1HostData = {0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7,
                                     0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7};
    std::vector<float> x2HostData = {0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7,
                                     0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7};
    std::vector<float> gammaHostData = {0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7};
    std::vector<float> yHostData = {0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7,
                                    0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7};
    std::vector<float> rstdHostData = {1, 2};
    std::vector<float> xHostData = {0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7,
                                    0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7};
    float epsilon = 1e-6;

    // Create x1 aclTensor.
    ret = CreateAclTensor(x1HostData, xShape, &x1DeviceAddr, aclDataType::ACL_FLOAT, &x1);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create x2 aclTensor.
    ret = CreateAclTensor(x2HostData, xShape, &x2DeviceAddr, aclDataType::ACL_FLOAT, &x2);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create gamma aclTensor.
    ret = CreateAclTensor(gammaHostData, gammaShape, &gammaDeviceAddr, aclDataType::ACL_FLOAT, &gamma);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create y aclTensor.
    ret = CreateAclTensor(yHostData, yShape, &yDeviceAddr, aclDataType::ACL_FLOAT, &y);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create rstd aclTensor.
    ret = CreateAclTensor(rstdHostData, rstdShape, &rstdDeviceAddr, aclDataType::ACL_FLOAT, &rstd);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create x aclTensor.
    ret = CreateAclTensor(xHostData, xShape, &xDeviceAddr, aclDataType::ACL_FLOAT, &x);
    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 aclnnAddRmsNorm.
    ret = aclnnAddRmsNormGetWorkspaceSize(x1, x2, gamma, epsilon, y, rstd, x, &workspaceSize, &executor);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnAddRmsNormGetWorkspaceSize 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 aclnnAddRmsNorm.
    ret = aclnnAddRmsNorm(workspaceAddr, workspaceSize, executor, stream);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnAddRmsNorm failed. ERROR: %d\n", ret); return ret);
    // 4. (boilerplate) Synchronize and wait for the task execution to end.
    ret = aclrtSynchronizeStream(stream);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);
    // 5. Get the output value and copy the result from device memory to host memory. Modify it based on the specific API definition.
    auto size = GetShapeSize(yShape);
    std::vector<float> resultData(size, 0);
    ret = aclrtMemcpy(
        resultData.data(), resultData.size() * sizeof(resultData[0]), yDeviceAddr, size * sizeof(float),
        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("y result[%ld] is: %f\n", i, resultData[i]);
    }

    // 6. Release aclTensor. Modify it based on the specific API definition.
    aclDestroyTensor(x1);
    aclDestroyTensor(x2);
    aclDestroyTensor(gamma);
    aclDestroyTensor(y);
    aclDestroyTensor(rstd);
    aclDestroyTensor(x);

    // 7. Release device resources. Modify it based on the specific API definition.
    aclrtFree(x1DeviceAddr);
    aclrtFree(x2DeviceAddr);
    aclrtFree(xDeviceAddr);
    aclrtFree(gammaDeviceAddr);
    aclrtFree(yDeviceAddr);
    aclrtFree(rstdDeviceAddr);
    if (workspaceSize > 0) {
        aclrtFree(workspaceAddr);
    }
    aclrtDestroyStream(stream);
    aclrtResetDevice(deviceId);
    aclFinalize();
    return 0;
}