aclnnAdaLayerNormBackward

📄 View Source Code

Applicable Products

  • Ascend 950PR&950DT products: Not 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 backward propagation of aclnnAdaLayerNormV2. It computes the gradients of the input tensors for updating model parameters during backward propagation.

  • Formula:

    Forward:

    out=LayerNorm(input)×(1+scale)+shiftout = LayerNorm(input)\times(1+scale)+shift

    LayerNorm formula:

    LayerNorm(input)=input−E(input)Var(input)+epsilon∗weightOptional+biasOptionalLayerNorm(input) = {{input-E(input)}\over\sqrt {Var(input)+epsilon}} * weightOptional + biasOptional

    In the preceding formula, E(input) indicates the mean of the input, and Var(input) indicates the variance of the input.

    Backward:

    z=(input−mean)×rstdz = (input - mean) \times rstd dy_g=gradOut×weightOptional×(1+scale)dy\_g = gradOut \times weightOptional \times (1 + scale) temp1=1/N×∑reduce_axis_2(gradOut×weightOptional×(1+scale))temp_1 = 1/N \times \sum_{reduce\_axis\_2} (gradOut \times weightOptional \times (1 + scale)) temp2=1/N×(input−mean)×rstd×∑reduce_axis_2(gradOut×weightOptional×(1+scale)×(input−mean)×rstd)temp_2 = 1/N \times (input - mean) \times rstd \times \sum_{reduce\_axis\_2}(gradOut \times weightOptional \times (1 + scale) \times (input - mean) \times rstd) gradInputOut=(gradOut×weightOptional×(1+scale)−(temp1+temp2))×rstdgradInputOut = (gradOut \times weightOptional \times (1 + scale) - (temp_1 + temp_2)) \times rstd gradScaleOut=∑reduce_axis_1(gradOut×((input−mean)×rstd×weightOptional+biasOptional))gradScaleOut = \sum_{reduce\_axis\_1}(gradOut \times ((input - mean) \times rstd \times weightOptional + biasOptional)) gradShiftOut=∑reduce_axis_1gradOutgradShiftOut = \sum_{reduce\_axis\_1}gradOut gradWeightOut=∑reduce_axis_0,1gradOut×(1+scale)×(input−mean)×rstdgradWeightOut = \sum_{reduce\_axis\_0,1}gradOut \times (1 + scale) \times (input - mean) \times rstd gradBiasOut=∑reduce_axis_0,1gradOut×(1+scale)gradBiasOut = \sum_{reduce\_axis\_0,1}gradOut \times (1 + scale)

    Where N is the dimension of the axis for normalization computation, that is, the size of the normalized axis dimension.

Function Prototype

Each operator uses a two-phase API. Call the "aclnnAdaLayerNormBackwardGetWorkspaceSize" API first to obtain the workspace size required for the computation and the executor containing the computation process, and then call the "aclnnAdaLayerNormBackward" API to execute the computation.

aclnnStatus aclnnAdaLayerNormBackwardGetWorkspaceSize(
    const aclTensor*    gradOut,
    const aclTensor*    input,
    const aclIntArray*  normalizedShape,
    const aclTensor*    rstd,
    const aclTensor*    mean,
    const aclTensor*    scale,
    const aclTensor*    shift,
    const aclTensor*    weightOptional,
    const aclTensor*    biasOptional,
    aclTensor*          gradInputOut,
    aclTensor*          gradScaleOut,
    aclTensor*          gradShiftOut,
    aclTensor*          gradWeightOut,
    aclTensor*          gradBiasOut,
    uint64_t*           workspaceSize,
    aclOpExecutor**     executor)
aclnnStatus aclnnAdaLayerNormBackward(
  void*          workspace,
  uint64_t       workspaceSize,
  aclOpExecutor* executor,
  aclrtStream    stream)

aclnnAdaLayerNormBackwardGetWorkspaceSize

  • Parameters

    Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor
    gradOut (aclTensor*) Input Indicates the gradient tensor for backward computation, corresponding to `gradOut` in the formula.
    • Null tensors are not supported.
    • Same data type as input `input`.
    • Shape: [B…, S, H], where B supports 0 to 6 dimensions.
    FLOAT32, FLOAT16, BFLOAT16 ND 2-8 √
    input (aclTensor*) Input Indicates the first input for forward computation, corresponding to `input` in the formula.
    • Does not support null tensors.
    • Has the same data type as input `gradOut`.
    • The shape is equal to the shape of `gradOut`, which is [B…, S, H], where B supports 0 to 6 dimensions.
    FLOAT32, FLOAT16, BFLOAT16 ND 2-8 √
    normalizedShape (aclIntArray*) Input Indicates the dimensions on which the norm computation is performed, corresponding to `reduce_axis_2` in the formula.
    • In the formula, `reduce_axis_0` corresponds to dimension B and `reduce_axis_1` corresponds to dimension S. The norm computation is not performed on dimensions B and S.
    • Here, the value is fixed to an array of length 1, and the element of the array is fixed to [H]. It does not support null.
    INT64 - - -
    mean(aclTensor*) Input The second output of the forward computation. Indicates the mean of `input`, corresponding to `mean` in the formula.
    • Does not support null tensors.
    • The data type is the same as that of input `gradOut`, and the bit width is not lower than that of input `input`.
    • The shape is equal to the shape of `rstd`, which is [B..., S, 1]. The last dimension is fixed to 1, and the other dimensions are consistent with `input`.
    FLOAT32, FLOAT16, BFLOAT16 ND 2-8 √
    rstd (aclTensor*) Input The third output of the forward computation, indicating the reciprocal of the standard deviation of `input`, corresponding to `rstd` in the formula.
    • Does not support null tensors.
    • The data type is the same as that of input `gradOut`, and its bit width is not lower than the data type bit width of input `input`.
    • The shape is equal to the shape of `mean`, which is [B..., S, 1]. The last dimension is fixed to 1, and other dimensions are consistent with `input`.
    FLOAT32, FLOAT16, BFLOAT16 ND 2-8 √
    scale (aclTensor*) Input Indicates the adaptive scaling parameter. Corresponds to `scale` in the formula.
    • Does not support null tensors.
    • The data type must be the same as that of `gradOut`.
    • shape is [B…, H] or [B…, 1, H], where B supports 0 to 6 dimensions. The dimension count and size must be consistent with B in `gradOut`, and H must be consistent with the H dimension in `gradOut`.
    FLOAT32, FLOAT16, BFLOAT16 ND 1-8 √
    shift (aclTensor*) Input Indicates the adaptive offset parameter.
    • Does not support null tensor.
    • The data type must be the same as that of input `gradOut`.
    • The shape is [B…, H] or [B…, 1, H], where B supports 0 to 6 dimensions. The number and size of dimensions must be consistent with B in `gradOut`, and H must be consistent with the H dimension in `gradOut`.
    FLOAT32, FLOAT16, BFLOAT16 ND 1-8 √
    weightOptional (aclTensor*) Input Optional input parameter, which indicates the normalization scaling parameter. It corresponds to `weightOptional` in the formula.
    • Does not support null tensor.
    • If the data type is not FLOAT32, the data type of this parameter must be the same as that of `input`.
    • The shape is [H], and H must be consistent with the H dimension in `gradOut`.
    FLOAT32, FLOAT16, BFLOAT16 ND 1 √
    biasOptional (aclTensor*) Input Optional input parameter, indicating the normalization bias parameter. It corresponds to `biasOptional` in the formula.
    • Does not support null Tensor.
    • The data type must be the same as that of the optional parameter `weightOptional`.
    • The shape is [H], where H is consistent with the H dimension in `gradOut`.
    FLOAT32, FLOAT16, BFLOAT16 ND 1 √
    gradInputOut (aclTensor*) Output Optional output that indicates the backward propagation output gradient, corresponding to `gradInputOut` in the formula.
    • Null Tensor is not supported.
    • When the pointer `gradInputOut` is not null, the gradient computation result of `input` is output, and its data type is the same as that of input `input`.
    • The shape is equal to the shape of `input`, which is [B…, S, H].
    FLOAT32, FLOAT16, BFLOAT16 ND 2-8 √
    gradScaleOut (aclTensor*) Output Optional output, indicates the gradient of the adaptive scaling coefficient in backward propagation, corresponding to `gradScaleOut` in the formula.
    • Null tensors are not supported.
    • When the pointer `gradScaleOut` is not null, outputs the gradient computation result of `scale`, which has the same data type as input `input`.
    • The shape is equal to the shape of `scale`, which is [B..., H] or [B..., 1, H].
    FLOAT32, FLOAT16, BFLOAT16 ND 1-8 √
    gradShiftOut (aclTensor*) Output Optional output, indicates the gradient of the adaptive offset coefficient in backward propagation, corresponding to `gradShiftOut` in the formula.
    • Does not support null tensors.
    • When the pointer `gradShiftOut` is not null, outputs the gradient computation result of `shift`, and its data type is the same as that of input `input`.
    • The shape is equal to the shape of `shift`, which is [B..., H] or [B..., 1, H].
    FLOAT32, FLOAT16, BFLOAT16 ND 1-8 √
    gradWeightOut (aclTensor*) Output Optional output that indicates the gradient of the normalization scale coefficient in backward propagation, corresponding to `gradWeightOut` in the formula.
    • Does not support null tensors.
    • When the pointer `gradWeightOut` is not null, outputs the gradient computation result of `weightOptional`, and its data type is the same as that of input `weightOptional`.
    • The shape is equal to the shape of `weightOptional`, which is [H].
    FLOAT32, FLOAT16, BFLOAT16 ND 1 √
    gradBiasOut (aclTensor*) Output Optional output. Indicates the gradient of the normalization bias coefficient for backward propagation, corresponding to `gradBiasOut` in the formula.
    • Does not support null tensor.
    • When the pointer `gradBiasOut` is not null, outputs the gradient computation result of `biasOptional`, and its data type is the same as that of input `weightOptional`.
    • The shape is equal to the shape of `gradWeightOut`, that is, [H].
    FLOAT32, FLOAT16, BFLOAT16 ND 1 √
    workspaceSize (uint64_t*) Output Returns the workspace size to be applied on the Device side. - - - - -
    executor (aclOpExecutor**) Output Returns the op executor, which contains the operator computation flow. - - - - -
  • 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 gradOut, input, normalizedShape, mean, rstd, scale, or shift is a null pointer.
    ACLNN_ERR_PARAM_INVALID 161002 The data type of gradOut, weightOptional (when non-null), or biasOptional (when non-null) is not supported.
    The data type of input, mean, rstd, scale, or shift is not the same as that of gradOut.
    The data type of weightOptional (when non-null) is not the same as that of biasOptional (when non-null).
    weightOptional (when non-null) and biasOptional (when non-null) do not meet the constraint (when the data type is not FLOAT32, the data type is the same as input).
    The data type of gradInputOut (when non-null), gradScaleOut (when non-null), gradShiftOut (when non-null), gradWeightOut (when non-null), or gradBiasOut (when non-null) is inconsistent with that in Parameters.
    The number of dimensions of input is less than 2.
    The length of the normalizedShape array is not 1.
    The corresponding dimensions are not equal when the shape array of input is right-aligned with the normalizedShape array.
    The shape of gradOut is not equal to that of input.
    The product of the shape of mean is not equal to the product of the dimensions of input from axis 0 to axis len(input) - len(normalizedShape).
    The product of the shape of rstd is not equal to the product of the dimensions of input from axis 0 to axis len(input) - len(normalizedShape).
    The shape of scale is not equal to [B…, 1, H] or [B…, H].
    The shape of shift is not equal to [B…, 1, H] or [B…, H].
    The shape of gradScaleOut (when non-null) is not equal to that of scale.
    The shape of gradShiftOut (when non-null) is not equal to that of shift.
    weightOptional is non-null and its shape array is not equal to the normalizedShape array.
    biasOptional is non-null and its shape array is not equal to the normalizedShape array.
    The shape of gradOut, input, mean, rstd, scale, shift, weightOptional (when non-null), biasOptional (when non-null), gradInputOut (when non-null), gradScaleOut (when non-null), gradShiftOut (when non-null), gradWeightOut (when non-null), and gradBiasOut (when non-null) has more than 8 dimensions.

aclnnAdaLayerNormBackward

  • Parameters

    Parameter Input/Output Description
    workspace Input Workspace memory address applied on the Device side.
    workspaceSize Input Size of the workspace applied on the Device side, obtained by the first-phase API aclnnAdaLayerNormBackwardGetWorkspaceSize.
    executor Input Op executor that contains the operator computation flow.
    stream Input Stream for task execution.
  • Return Value

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

Constraints

  • Mixed precision combinations:

    • The following parameters of the AdaLayerNormBackward operator must have the same data type:

      GroupingSpecific Parameters Included
      Group AgradOut, input, rstd, mean, scale, shift, gradInputOut, gradScaleOut, gradShiftOut
      Group BweightOptional, biasOptional, gradWeightOut, gradBiasOut
    • The fixed data type combinations of the two parameter groups above are as follows (the left and right data types strictly correspond):

      First Group Of Parameters (Group A)Second Group Of Parameters (Group B)
      FLOAT32FLOAT32
      FLOAT16FLOAT16
      FLOAT16FLOAT32
      BFLOAT16BFLOAT16
      BFLOAT16FLOAT32
  • Deterministic computation:

    • aclnnAdaLayerNormBackward defaults to a deterministic implementation.

Example

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

#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_ada_layer_norm_backward.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 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 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 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. For details, see the acl API manual.
    // Set deviceId based on the actual device.
    int32_t deviceId = 0;
    aclrtStream stream;
    auto ret = Init(deviceId, &stream);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);

    // 2. Construct the input and output. You need to custom-construct them according to the API definition.
    std::vector<int64_t> xShape = {2, 2, 2};
    std::vector<int64_t> meanShape = {2, 2, 1};
    std::vector<int64_t> scaleShape = {2, 2};
    std::vector<int64_t> shiftShape = {2, 2};
    std::vector<int64_t> normShape = {2};
    void* dyDeviceAddr = nullptr;
    void* xDeviceAddr = nullptr;
    void* rstdDeviceAddr = nullptr;
    void* meanDeviceAddr = nullptr;
    void* scaleDeviceAddr = nullptr;
    void* shiftDeviceAddr = nullptr;
    void* weightDeviceAddr = nullptr;
    void* biasDeviceAddr = nullptr;
    void* outDeviceAddr = nullptr;
    void* dsDeviceAddr = nullptr;
    void* dtDeviceAddr = nullptr;
    void* dwDeviceAddr = nullptr;
    void* dbDeviceAddr = nullptr;
    aclTensor* dy = nullptr;
    aclTensor* x = nullptr;
    aclIntArray* norm = nullptr;
    aclTensor* rstd = nullptr;
    aclTensor* mean = nullptr;
    aclTensor* scale = nullptr;
    aclTensor* shift = nullptr;
    aclTensor* weight = nullptr;
    aclTensor* bias = nullptr;
    aclTensor* out = nullptr;
    aclTensor* ds = nullptr;
    aclTensor* dt = nullptr;
    aclTensor* dw = nullptr;
    aclTensor* db = nullptr;
    std::vector<float> dyHostData = {2, 3, 4, 5, 2, 3, 4, 5};
    std::vector<float> xHostData = {2, 3, 4, 5, 2, 3, 4, 5};
    std::vector<int64_t> normData = {2};
    std::vector<float> meanHostData = {2, 3, 4, 5};
    std::vector<float> rstdHostData = {4, 5, 6, 7};
    std::vector<float> scaleHostData = {1, 1, 2, 2};
    std::vector<float> shiftHostData = {2, 2, 1, 1};
    std::vector<float> weightHostData = {1, 1};
    std::vector<float> biasHostData = {0, 0};
    std::vector<float> outHostData(8, 0);
    std::vector<float> dsHostData(4, 0);
    std::vector<float> dtHostData(4, 0);
    std::vector<float> dwHostData(2, 0);
    std::vector<float> dbHostData(2, 0);

    // Create the dy aclTensor.
    ret = CreateAclTensor(dyHostData, xShape, &dyDeviceAddr, aclDataType::ACL_FLOAT, &dy);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create the x aclTensor.
    ret = CreateAclTensor(xHostData, xShape, &xDeviceAddr, aclDataType::ACL_FLOAT, &x);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create the normalizedShape aclIntArray.
    norm = aclCreateIntArray(normData.data(), 1);
    CHECK_RET(norm != nullptr, return ret);
    // Create the rstd aclTensor.
    ret = CreateAclTensor(rstdHostData, meanShape, &rstdDeviceAddr, aclDataType::ACL_FLOAT, &rstd);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create the mean aclTensor.
    ret = CreateAclTensor(meanHostData, meanShape, &meanDeviceAddr, aclDataType::ACL_FLOAT, &mean);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create the scale aclTensor.
    ret = CreateAclTensor(scaleHostData, scaleShape, &scaleDeviceAddr, aclDataType::ACL_FLOAT, &scale);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create the shift aclTensor.
    ret = CreateAclTensor(shiftHostData, shiftShape, &shiftDeviceAddr, aclDataType::ACL_FLOAT, &shift);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create the weight aclTensor.
    ret = CreateAclTensor(weightHostData, normShape, &weightDeviceAddr, aclDataType::ACL_FLOAT, &weight);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create the bias aclTensor.
    ret = CreateAclTensor(biasHostData, normShape, &biasDeviceAddr, aclDataType::ACL_FLOAT, &bias);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create the out aclTensor.
    ret = CreateAclTensor(outHostData, xShape, &outDeviceAddr, aclDataType::ACL_FLOAT, &out);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create the ds aclTensor.
    ret = CreateAclTensor(dsHostData, scaleShape, &dsDeviceAddr, aclDataType::ACL_FLOAT, &ds);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create the dt aclTensor.
    ret = CreateAclTensor(dtHostData, shiftShape, &dtDeviceAddr, aclDataType::ACL_FLOAT, &dt);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create the dw aclTensor.
    ret = CreateAclTensor(dwHostData, normShape, &dwDeviceAddr, aclDataType::ACL_FLOAT, &dw);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create db aclTensor.
    ret = CreateAclTensor(dbHostData, normShape, &dbDeviceAddr, aclDataType::ACL_FLOAT, &db);
    CHECK_RET(ret == ACL_SUCCESS, return ret);

    // 3. Call the CANN operator library API. Replace it with the actual API name.
    uint64_t workspaceSize = 0;
    aclOpExecutor* executor;
    // Call the first-phase API of aclnnAdaLayerNormBackward.
    ret = aclnnAdaLayerNormBackwardGetWorkspaceSize(
        dy, x, norm, rstd, mean, scale, shift, weight, bias, out, ds, dt, dw, db, &workspaceSize, &executor);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnAdaLayerNormBackwardGetWorkspaceSize 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 aclnnAdaLayerNormBackward.
    ret = aclnnAdaLayerNormBackward(workspaceAddr, workspaceSize, executor, stream);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnAdaLayerNormBackward failed. ERROR: %d\n", ret); return ret);

    // 4. (Boilerplate) Wait synchronously until the task is 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 the host. Modify it based on the specific API definition.
    auto size = GetShapeSize(xShape);
    std::vector<float> resultData(size, 0);
    ret = aclrtMemcpy(
        resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr, size * sizeof(resultData[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);
    for (int64_t i = 0; i < size; i++) {
        LOG_PRINT("out result[%ld] is: %f\n", i, resultData[i]);
    }

    auto size1 = GetShapeSize(scaleShape);
    std::vector<float> resultData1(size1, 0);
    ret = aclrtMemcpy(
        resultData1.data(), resultData1.size() * sizeof(resultData1[0]), dsDeviceAddr, size1 * sizeof(resultData1[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);
    for (int64_t i = 0; i < size1; i++) {
        LOG_PRINT("ds result[%ld] is: %f\n", i, resultData1[i]);
    }

    auto size2 = GetShapeSize(shiftShape);
    std::vector<float> resultData2(size2, 0);
    ret = aclrtMemcpy(
        resultData2.data(), resultData2.size() * sizeof(resultData2[0]), dtDeviceAddr, size2 * sizeof(resultData2[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);
    for (int64_t i = 0; i < size2; i++) {
        LOG_PRINT("dt result[%ld] is: %f\n", i, resultData2[i]);
    }

    auto size3 = GetShapeSize(normShape);
    std::vector<float> resultData3(size3, 0);
    ret = aclrtMemcpy(
        resultData3.data(), resultData3.size() * sizeof(resultData3[0]), dwDeviceAddr, size3 * sizeof(resultData3[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);
    for (int64_t i = 0; i < size3; i++) {
        LOG_PRINT("dw result[%ld] is: %f\n", i, resultData3[i]);
    }

    auto size4 = GetShapeSize(normShape);
    std::vector<float> resultData4(size4, 0);
    ret = aclrtMemcpy(
        resultData4.data(), resultData4.size() * sizeof(resultData4[0]), dbDeviceAddr, size4 * sizeof(resultData4[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);
    for (int64_t i = 0; i < size4; i++) {
        LOG_PRINT("db result[%ld] is: %f\n", i, resultData4[i]);
    }

    // 6. Release aclTensor, aclIntArray, and aclBoolArray. Modify it based on the specific API definition.
    aclDestroyTensor(dy);
    aclDestroyTensor(x);
    aclDestroyIntArray(norm);
    aclDestroyTensor(mean);
    aclDestroyTensor(rstd);
    aclDestroyTensor(scale);
    aclDestroyTensor(shift);
    aclDestroyTensor(weight);
    aclDestroyTensor(bias);
    aclDestroyTensor(out);
    aclDestroyTensor(ds);
    aclDestroyTensor(dt);
    aclDestroyTensor(dw);
    aclDestroyTensor(db);

    // 7. Release device resources.
    aclrtFree(dyDeviceAddr);
    aclrtFree(xDeviceAddr);
    aclrtFree(meanDeviceAddr);
    aclrtFree(rstdDeviceAddr);
    aclrtFree(scaleDeviceAddr);
    aclrtFree(shiftDeviceAddr);
    aclrtFree(weightDeviceAddr);
    aclrtFree(biasDeviceAddr);
    aclrtFree(outDeviceAddr);
    aclrtFree(dsDeviceAddr);
    aclrtFree(dtDeviceAddr);
    aclrtFree(dwDeviceAddr);
    aclrtFree(dbDeviceAddr);
    if (workspaceSize > 0) {
        aclrtFree(workspaceAddr);
    }
    aclrtDestroyStream(stream);
    aclrtResetDevice(deviceId);
    aclFinalize();

    return 0;
}