aclnnAddLayerNorm

📄 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: Implements the AddLayerNorm operation.
  • Formula:
    $$ x = x1 + x2 + biasOptional $$
    $$ rstd = {{1}\over\sqrt {Var(x)+epsilon}} $$
    $$ y = (x-E(x)) * rstd * gamma + beta $$
    Where E(x) represents the mean and Var(x) represents the variance, both of which must be computed internally by the operator.

Function Prototype

Each operator uses a two-phase API. First call the aclnnAddLayerNormGetWorkspaceSize API to obtain the workspace size required for the computation and the executor that contains the operator computation process. Then call the aclnnAddLayerNorm API to execute the computation.

aclnnStatus aclnnAddLayerNormGetWorkspaceSize(
  const aclTensor *x1,
  const aclTensor *x2,
  const aclTensor *gamma,
  const aclTensor *beta,
  const aclTensor *biasOptional,
  double           epsilon,
  bool             additionalOutput,
  const aclTensor *yOut,
  const aclTensor *meanOut,
  const aclTensor *rstdOut,
  const aclTensor *xOut,
  uint64_t        *workspaceSize,
  aclOpExecutor  **executor)
aclnnStatus aclnnAddLayerNorm(
  void          *workspace,
  uint64_t       workspaceSize,
  aclOpExecutor *executor,
  aclrtStream    stream)

aclnnAddLayerNormGetWorkspaceSize

  • Parameters

    Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor
    x1 (aclTensor*) Input Indicates the input of the addition computation in AddLayerNorm. The operator computes x1 + x2 + biasOptional and performs layer normalization on the result. Corresponds to `x1` in the formula.
    • Empty tensors are supported.
    • A dimension value of 0 in the input is not supported.
    FLOAT32, FLOAT16, BFLOAT16 ND 1-8 √
    x2 (aclTensor*) Input Indicates the input of the addition computation in AddLayerNorm. The operator computes x1 + x2 + biasOptional and performs layer normalization on the result. Corresponds to `x2` in the formula.
    • Supports empty tensors.
    • The shape is consistent with that of `x1`.
    FLOAT32, FLOAT16, BFLOAT16 ND 1-8 √
    gamma (aclTensor*) Input Indicates the gamma parameter in layer normalization. Corresponds to `gamma` in the corresponding formula.
    • Supports empty tensors.
    • The dimension values of the shape are the same as the dimension values to be normalized in `x1`.
    FLOAT32, FLOAT16, BFLOAT16 ND 1-8 √
    beta (aclTensor*) Input Indicates the beta parameter in layer normalization. Corresponds to `beta` in the formula.
    • Supports empty Tensor.
    • The shape dimension value is the same as the dimension value to be normalized in `x1`.
    FLOAT32, FLOAT16, BFLOAT16 ND 1-8 √
    biasOptional (aclTensor*) Input Optional input parameter, indicates the input of the addition computation in AddLayerNorm. The addition of x1 + x2 + biasOptional is computed within the operator, and layer normalization is performed on the computation result. Corresponds to `biasOptional` in the formula.
    • Supports empty Tensor.
    • The shape can be consistent with `gamma`/`beta` or `x1`/`x2`.
    FLOAT32, FLOAT16, BFLOAT16 ND 1-8 √
    epsilon (double) Input Indicates the value added to the denominator to ensure numerical stability. Corresponds to `epsilon` in the formula.
    • Recommended value: 1e-5.
    - - - -
    additionalOutput (bool) Input Indicates whether to enable the output of x=x1+x2+biasOptional. - - - - -
    yOut (aclTensor*) Output Indicates the output result of LayerNorm. Corresponds to `y` in the formula.
    • Supports empty tensors.
    • The shape must be consistent with input `x1`.
    FLOAT32, FLOAT16, BFLOAT16 ND 1-8 ×
    meanOut (aclTensor*) Output Indicates the mean of the (x1 + x2 + biasOptional) result output during the LayerNorm computation, corresponding to `E(x)` in the formula.
    • Supports empty tensors.
    • The shape must satisfy the broadcast relationship with `x1` (the leading dimensions are the same as the leading dimensions of `x1`, the trailing dimensions are 1, and the total number of dimensions is the same as that of `x1`; the leading dimensions refer to the dimensions of `x1` minus the dimensions of gamma, indicating the dimensions that do not require normalization).
    • When the input `x1` is an empty tensor, `meanOut` must also be an empty tensor.
    FLOAT32 ND 1-8 ×
    rstdOut (aclTensor*) Output Indicates the `rstd` result output in the LayerNorm calculation. Corresponds to `rstd` in the formula.
    • Supports empty tensors.
    • The shape must satisfy the broadcast relationship with `x1` (the leading dimensions are the same as the leading dimensions of `x1`, the trailing dimensions are 1, and the total dimensions are the same as the dimensions of `x1`. The leading dimensions refer to the dimensions of `x1` minus the dimensions of gamma, indicating the dimensions that do not need normalization).
    • When the input `x1` is an empty tensor, `rstdOut` must also be an empty tensor.
    FLOAT32 ND 1-8 ×
    xOut (aclTensor*) Output Indicates the result output `x` of the Add operation. This corresponds to `x` in the corresponding formula.
    • Supports empty tensors.
    • The shape must be consistent with input `x1`.
    FLOAT32, FLOAT16, BFLOAT16 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, which contains the operator computation process. - - - - -
    • Atlas inference products:
      • The data types of the parameters x1, x2, beta, gamma, biasOptional, yOut, and xOut do not support BFLOAT16.
      • The parameters meanOut and rstdOut are 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 If a required input, output, or required attribute is a null pointer, 161001 is returned.
    ACLNN_ERR_PARAM_INVALID 161002 The data type of the input or output is not within the supported range.
    ACLNN_ERR_INNER_TILING_ERROR 561002 Failed to obtain the shapes of (x1, x2, gamma, beta, yOut, meanOut, rstdOut, xOut) in the tiling phase.
    The number of dimensions in the shapes of (x1, gamma) is greater than 8 or less than 0.
    The dimensions of (x1, x2, yOut, meanOut, rstdOut, xOut) are inconsistent.
    The number of dimensions of x1 is less than that of gamma.
    Any dimension of (x1, gamma, meanOut) equals 0.
    The shapes of (x1, x2, yOut, xOut) are not completely the same.
    The shapes of (gamma, beta) are not completely the same.
    The shapes of (meanOut, rstdOut) are not completely the same.
    The dimensions of gamma are not the same as the dimensions of x to be normalized, or the dimensions of meanOut are not the same as the dimensions of x that do not need normalization, or the dimensions of meanOut that need normalization are not 1.

aclnnAddLayerNorm

  • Parameters

    Parameter Input/Output Description
    workspace Input Device-allocated workspace memory address.
    workspaceSize Input Device-allocated workspace size, obtained by the first-phase API aclnnAddLayerNormGetWorkspaceSize.
    executor Input Op executor that contains the operator computation flow.
    stream Input Specifies the stream for task execution.
  • Return Value

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

Constraints

  • Functional Dimensions

    • Supported data types

      • Atlas inference products: x1, x2, beta, gamma, and biasOptional support FLOAT32 and FLOAT16.
      • Atlas A2 products and Atlas A3 products: x1, x2, beta, gamma, and biasOptional support FLOAT32, FLOAT16, and BFLOAT16.
      • rstdOut and meanOut support: FLOAT32.
    • Data format support: ND.

    • Atlas inference products: The trailing axis length of the five inputs x1, x2, beta, gamma, and biasOptional must be greater than or equal to 32 Bytes.
  • Unsupported Type Description

    • DOUBLE: DOUBLE is not supported.
  • Boundary Value Scenario Description

    • When the input is Inf, the output is Inf.
    • When the input is NaN, the output is NaN.
  • Supported Data Types by Product

    • Atlas A2 products and Atlas A3 products:

| x1 Data Type | x2 Data Type | Gamma Data Type | Beta Data Type | biasOptional Data Type | yOut Data Type | meanOut Data Type | rstdOut Data Type | xOut Data Type | | -------- | -------- | ------------- | ------------- | ----------- | --------- | --------- | --------- | :-------- | | FLOAT32 | FLOAT16 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | | FLOAT32 | BFLOAT16 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | | FLOAT16 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | | BFLOAT16 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | | FLOAT16 | FLOAT16 | FLOAT32 | FLOAT32 | FLOAT16 | FLOAT16 | FLOAT32 | FLOAT32 | FLOAT16 | | BFLOAT16 | BFLOAT16 | FLOAT32 | FLOAT32 | BFLOAT16 | BFLOAT16 | FLOAT32 | FLOAT32 | BFLOAT16 | | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT32 | FLOAT32 | FLOAT16 | | BFLOAT16 | BFLOAT16 | BFLOAT16 | BFLOAT16 | BFLOAT16 | BFLOAT16 | FLOAT32 | FLOAT32 | BFLOAT16 | | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 |

  • Atlas inference products:

| x1 Data Type | x2 Data Type | Gamma Data Type | Beta Data Type | biasOptional Data Type | yOut Data Type | meanOut Data Type | rstdOut Data Type | xOut Data Type | | -------- | -------- | ------------- | ------------- | ----------- | --------- | --------- | --------- | :-------- | | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | FLOAT32 | | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT16 | FLOAT32 | FLOAT32 | FLOAT16 |

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

Example

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

#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_add_layer_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 shapeSize = 1;
    for (auto i : shape) {
        shapeSize *= i;
    }
    return shapeSize;
}

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-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) 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);
    // Process check as needed.
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);

    // 2.
    // Construct inputs and outputs. Custom construction is required based on the API. This example calls the use case without the optional biasOptional input and the use case with the biasOptional input once each.
    float eps = 1e-6;
    bool additionalOutput = true;

    std::vector<int64_t> x1Shape = {1, 2, 8};
    std::vector<int64_t> x2Shape = {1, 2, 8};
    std::vector<int64_t> gammaShape = {8};
    std::vector<int64_t> betaShape = {8};
    std::vector<int64_t> biasOptionalShape = {8};

    std::vector<int64_t> outputYShape = {1, 2, 8};
    std::vector<int64_t> outputMeanShape = {1, 2, 1};
    std::vector<int64_t> outputRstdShape = {1, 2, 1};
    std::vector<int64_t> outputXShape = {1, 2, 8};

    void* x1DeviceAddr = nullptr;
    void* x2DeviceAddr = nullptr;
    void* betaDeviceAddr = nullptr;
    void* gammaDeviceAddr = nullptr;
    void* biasOptionalDeviceAddr = nullptr;

    // Device address of the output for the use case without biasOptional
    void* outputYDeviceAddr = nullptr;
    void* outputMeanDeviceAddr = nullptr;
    void* outputRstdDeviceAddr = nullptr;
    void* outputXDeviceAddr = nullptr;

    // Device address of the output for the use case with biasOptional
    void* outputYDeviceAddrbiasOptional = nullptr;
    void* outputMeanDeviceAddrbiasOptional = nullptr;
    void* outputRstdDeviceAddrbiasOptional = nullptr;
    void* outputXDeviceAddrbiasOptional = nullptr;

    aclTensor* x1 = nullptr;
    aclTensor* x2 = nullptr;
    aclTensor* beta = nullptr;
    aclTensor* gamma = nullptr;
    aclTensor* biasOptional = nullptr;

    // aclTensor for the use case without biasOptional
    aclTensor* outputY = nullptr;
    aclTensor* outputMean = nullptr;
    aclTensor* outputRstd = nullptr;
    aclTensor* outputX = nullptr;

    // aclTensor for the use case with biasOptional
    aclTensor* outputYbiasOptional = nullptr;
    aclTensor* outputMeanbiasOptional = nullptr;
    aclTensor* outputRstdbiasOptional = nullptr;
    aclTensor* outputXbiasOptional = nullptr;

    std::vector<float> x1HostData = {1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2};
    std::vector<float> x2HostData = {4, 4, 4, 4, 4, 4, 4, 4, -3, -3, -3, -3, -3, -3, -3, -3};
    std::vector<float> gammaHostData = {2, 2, 2, 2, 2, 2, 2, 2};
    std::vector<float> betaHostData = {0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1};
    std::vector<float> biasOptionalHostData = {0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5};

    // For HostData without biasOptional
    std::vector<float> outputYHostData(1 * 2 * 8);
    std::vector<float> outputMeanHostData(2);
    std::vector<float> outputRstdHostData(2);
    std::vector<float> outputXHostData(1 * 2 * 8);

    // For HostData with biasOptional
    std::vector<float> outputYHostDatabiasOptional(1 * 2 * 8);
    std::vector<float> outputMeanHostDatabiasOptional(2);
    std::vector<float> outputRstdHostDatabiasOptional(2);
    std::vector<float> outputXHostDatabiasOptional(1 * 2 * 8);

    // Create the self aclTensor.
    ret = CreateAclTensor(x1HostData, x1Shape, &x1DeviceAddr, aclDataType::ACL_FLOAT, &x1);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    ret = CreateAclTensor(x2HostData, x2Shape, &x2DeviceAddr, aclDataType::ACL_FLOAT, &x2);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    ret = CreateAclTensor(betaHostData, betaShape, &betaDeviceAddr, aclDataType::ACL_FLOAT, &beta);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    ret = CreateAclTensor(gammaHostData, gammaShape, &gammaDeviceAddr, aclDataType::ACL_FLOAT, &gamma);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    ret = CreateAclTensor(
        biasOptionalHostData, biasOptionalShape, &biasOptionalDeviceAddr, aclDataType::ACL_FLOAT, &biasOptional);
    CHECK_RET(ret == ACL_SUCCESS, return ret);

    // Create the aclTensor without biasOptional.
    ret = CreateAclTensor(outputYHostData, outputYShape, &outputYDeviceAddr, aclDataType::ACL_FLOAT, &outputY);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    ret = CreateAclTensor(
        outputMeanHostData, outputMeanShape, &outputMeanDeviceAddr, aclDataType::ACL_FLOAT, &outputMean);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    ret = CreateAclTensor(
        outputRstdHostData, outputRstdShape, &outputRstdDeviceAddr, aclDataType::ACL_FLOAT, &outputRstd);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    ret = CreateAclTensor(outputXHostData, outputXShape, &outputXDeviceAddr, aclDataType::ACL_FLOAT, &outputX);
    CHECK_RET(ret == ACL_SUCCESS, return ret);

    // Create the aclTensor with biasOptional.
    ret = CreateAclTensor(
        outputYHostDatabiasOptional, outputYShape, &outputYDeviceAddrbiasOptional, aclDataType::ACL_FLOAT,
        &outputYbiasOptional);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    ret = CreateAclTensor(
        outputMeanHostDatabiasOptional, outputMeanShape, &outputMeanDeviceAddrbiasOptional, aclDataType::ACL_FLOAT,
        &outputMeanbiasOptional);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    ret = CreateAclTensor(
        outputRstdHostDatabiasOptional, outputRstdShape, &outputRstdDeviceAddrbiasOptional, aclDataType::ACL_FLOAT,
        &outputRstdbiasOptional);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    ret = CreateAclTensor(
        outputXHostDatabiasOptional, outputXShape, &outputXDeviceAddrbiasOptional, aclDataType::ACL_FLOAT,
        &outputXbiasOptional);
    CHECK_RET(ret == ACL_SUCCESS, return ret);

    // Example of calling the aclnnAddLayerNorm API, including one call with biasOptional and one call without biasOptional.
    // 3. Call the CANN operator library API. Replace the API name with the actual one.

    // 3.1 Example without the optional input biasOptional
    // Call the first-phase API of aclnnAddLayerNorm.
    uint64_t workspaceSize = 0;
    aclOpExecutor* executor;
    LOG_PRINT("\nUse aclnnAddLayerNorm Non-biasOptional Port.");
    // For the biasOptional parameter, simply pass in nullptr.
    ret = aclnnAddLayerNormGetWorkspaceSize(
        x1, x2, gamma, beta, nullptr, eps, additionalOutput, outputY, outputMean, outputRstd, outputX, &workspaceSize,
        &executor);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnAddLayerNormGetWorkspaceSize 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 aclnnAddLayerNorm.
    ret = aclnnAddLayerNorm(workspaceAddr, workspaceSize, executor, stream);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnAddLayerNorm failed. ERROR: %d\n", ret); return ret);

    // 3.2 Example with the optional input biasOptional
    // Call the first-phase API of aclnnAddLayerNorm.
    uint64_t workspaceSizebiasOptional = 0;
    aclOpExecutor* executorbiasOptional;
    LOG_PRINT("\nUse aclnnAddLayerNorm biasOptional Port.");
    // Pass in biasOptional as normal.
    ret = aclnnAddLayerNormGetWorkspaceSize(
        x1, x2, gamma, beta, biasOptional, eps, additionalOutput, outputYbiasOptional, outputMeanbiasOptional,
        outputRstdbiasOptional, outputXbiasOptional, &workspaceSizebiasOptional, &executorbiasOptional);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnAddLayerNormGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
    // Allocate device memory based on the workspaceSize calculated by the first-phase API.
    void* workspaceAddrbiasOptional = nullptr;
    if (workspaceSizebiasOptional > 0) {
        ret = aclrtMalloc(&workspaceAddrbiasOptional, workspaceSizebiasOptional, 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 aclnnAddLayerNorm.
    ret = aclnnAddLayerNorm(workspaceAddrbiasOptional, workspaceSizebiasOptional, executorbiasOptional, stream);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnAddLayerNorm failed. ERROR: %d\n", ret); return ret);

    // 4. (Boilerplate) Synchronize and wait for 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 result from device memory to host memory. Modify this part based on the specific API definition.

    // 5.1 Copy the output without biasOptional.
    auto outputYSize = GetShapeSize(outputYShape);
    std::vector<float> resultDataY(outputYSize, 0);
    ret = aclrtMemcpy(
        resultDataY.data(), resultDataY.size() * sizeof(resultDataY[0]), outputYDeviceAddr,
        outputYSize * sizeof(resultDataY[0]), 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);
    LOG_PRINT("==== AddLayerNorm non-biasOptional: y output");
    for (int64_t i = 0; i < outputYSize; i++) {
        LOG_PRINT("result[%ld] is: %f\n", i, resultDataY[i]);
    }

    auto outputMeanSize = GetShapeSize(outputMeanShape);
    std::vector<float> resultDataMean(outputMeanSize, 0);
    ret = aclrtMemcpy(
        resultDataMean.data(), resultDataMean.size() * sizeof(resultDataMean[0]), outputMeanDeviceAddr,
        outputMeanSize * sizeof(resultDataMean[0]), 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);
    LOG_PRINT("==== AddLayerNorm non-biasOptional: mean output");
    for (int64_t i = 0; i < outputMeanSize; i++) {
        LOG_PRINT("result[%ld] is: %f\n", i, resultDataMean[i]);
    }

    auto outputRstdSize = GetShapeSize(outputRstdShape);
    std::vector<float> resultDataRstd(outputRstdSize, 0);
    ret = aclrtMemcpy(
        resultDataRstd.data(), resultDataRstd.size() * sizeof(resultDataRstd[0]), outputRstdDeviceAddr,
        outputRstdSize * sizeof(resultDataRstd[0]), 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);
    LOG_PRINT("==== AddLayerNorm non-biasOptional: rstd output");
    for (int64_t i = 0; i < outputRstdSize; i++) {
        LOG_PRINT("result[%ld] is: %f\n", i, resultDataRstd[i]);
    }

    auto outputXSize = GetShapeSize(outputXShape);
    std::vector<float> resultDataX(outputXSize, 0);
    ret = aclrtMemcpy(
        resultDataX.data(), resultDataX.size() * sizeof(resultDataX[0]), outputXDeviceAddr,
        outputXSize * sizeof(resultDataX[0]), 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);
    LOG_PRINT("==== AddLayerNorm non-biasOptional: x output");
    for (int64_t i = 0; i < outputXSize; i++) {
        LOG_PRINT("result[%ld] is: %f\n", i, resultDataX[i]);
    }

    // 5.2 Copy out the output containing biasOptional.
    auto outputYSizebiasOptional = GetShapeSize(outputYShape);
    std::vector<float> resultDataYbiasOptional(outputYSizebiasOptional, 0);
    ret = aclrtMemcpy(
        resultDataYbiasOptional.data(), resultDataYbiasOptional.size() * sizeof(resultDataYbiasOptional[0]),
        outputYDeviceAddrbiasOptional, outputYSizebiasOptional * sizeof(resultDataYbiasOptional[0]),
        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);
    LOG_PRINT("==== AddLayerNorm biasOptional: y output");
    for (int64_t i = 0; i < outputYSizebiasOptional; i++) {
        LOG_PRINT("result[%ld] is: %f\n", i, resultDataYbiasOptional[i]);
    }

    auto outputMeanSizebiasOptional = GetShapeSize(outputMeanShape);
    std::vector<float> resultDataMeanbiasOptional(outputMeanSizebiasOptional, 0);
    ret = aclrtMemcpy(
        resultDataMeanbiasOptional.data(), resultDataMeanbiasOptional.size() * sizeof(resultDataMeanbiasOptional[0]),
        outputMeanDeviceAddrbiasOptional, outputMeanSizebiasOptional * sizeof(resultDataMeanbiasOptional[0]),
        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);
    LOG_PRINT("==== AddLayerNorm biasOptional: mean output");
    for (int64_t i = 0; i < outputMeanSizebiasOptional; i++) {
        LOG_PRINT("result[%ld] is: %f\n", i, resultDataMeanbiasOptional[i]);
    }

    auto outputRstdSizebiasOptional = GetShapeSize(outputRstdShape);
    std::vector<float> resultDataRstdbiasOptional(outputRstdSizebiasOptional, 0);
    ret = aclrtMemcpy(
        resultDataRstdbiasOptional.data(), resultDataRstdbiasOptional.size() * sizeof(resultDataRstdbiasOptional[0]),
        outputRstdDeviceAddrbiasOptional, outputRstdSizebiasOptional * sizeof(resultDataRstdbiasOptional[0]),
        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);
    LOG_PRINT("==== AddLayerNorm biasOptional: rstd output");
    for (int64_t i = 0; i < outputRstdSizebiasOptional; i++) {
        LOG_PRINT("result[%ld] is: %f\n", i, resultDataRstdbiasOptional[i]);
    }

    auto outputXSizebiasOptional = GetShapeSize(outputXShape);
    std::vector<float> resultDataXbiasOptional(outputXSizebiasOptional, 0);
    ret = aclrtMemcpy(
        resultDataXbiasOptional.data(), resultDataXbiasOptional.size() * sizeof(resultDataXbiasOptional[0]),
        outputXDeviceAddrbiasOptional, outputXSizebiasOptional * sizeof(resultDataXbiasOptional[0]),
        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);
    LOG_PRINT("==== AddLayerNorm biasOptional: x output");
    for (int64_t i = 0; i < outputXSizebiasOptional; i++) {
        LOG_PRINT("result[%ld] is: %f\n", i, resultDataXbiasOptional[i]);
    }

    // 6. Release aclTensor and aclScalar. Modify based on the specific API definition.
    aclDestroyTensor(x1);
    aclDestroyTensor(x2);
    aclDestroyTensor(beta);
    aclDestroyTensor(gamma);
    aclDestroyTensor(biasOptional);

    aclDestroyTensor(outputY);
    aclDestroyTensor(outputMean);
    aclDestroyTensor(outputRstd);
    aclDestroyTensor(outputX);

    aclDestroyTensor(outputYbiasOptional);
    aclDestroyTensor(outputMeanbiasOptional);
    aclDestroyTensor(outputRstdbiasOptional);
    aclDestroyTensor(outputXbiasOptional);

    // 7. Release device resources. Modify based on the specific API definition.
    aclrtFree(x1DeviceAddr);
    aclrtFree(x2DeviceAddr);
    aclrtFree(gammaDeviceAddr);
    aclrtFree(betaDeviceAddr);
    aclrtFree(biasOptionalDeviceAddr);

    aclrtFree(outputYDeviceAddr);
    aclrtFree(outputMeanDeviceAddr);
    aclrtFree(outputRstdDeviceAddr);
    aclrtFree(outputXDeviceAddr);

    aclrtFree(outputYDeviceAddrbiasOptional);
    aclrtFree(outputMeanDeviceAddrbiasOptional);
    aclrtFree(outputRstdDeviceAddrbiasOptional);
    aclrtFree(outputXDeviceAddrbiasOptional);

    if (workspaceSize > 0) {
        aclrtFree(workspaceAddr);
    }

    if (workspaceSizebiasOptional > 0) {
        aclrtFree(workspaceAddrbiasOptional);
    }

    aclrtDestroyStream(stream);
    aclrtResetDevice(deviceId);
    aclFinalize();
    return 0;
}