aclnnGRU

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: Implements the computation of the Gated Recurrent Unit (GRU). Supports multi-layer stacking, bidirectional computation, and two input modes: fixed-length sequences and variable-length sequences (PackedSequence). In training mode, the intermediate results of each gate (r, z, n, hn, h) can be output for backward propagation.

  • Input and output shapes:

    • Fixed-length mode (batchSizes is nullptr): If the shape of the input tensor input is (T, B, I) (batchFirst=false) or (B, T, I) (batchFirst=true), the shape of the output tensor output is (T, B, D H) (batchFirst=false) or (B, T, D H) (batchFirst=true), and the shape of the final hidden state hy is (L * D, B, H).
    • Variable-length mode (batchSizes is not nullptr): the shape of the input tensor input is (sum(batch_size), I), the shape of the output tensor output is (sum(batch_size), D H), and the shape of the final hidden state hy is (L D, B, H).
  • Formula:

    rt=σ(Wirxt+bir+Whrh(t−1)+bhr)r_t = \sigma(W_{ir} x_t + b_{ir} + W_{hr} h_{(t-1)} + b_{hr}) zt=σ(Wizxt+biz+Whzh(t−1)+bhz)z_t = \sigma(W_{iz} x_t + b_{iz} + W_{hz} h_{(t-1)} + b_{hz}) nt=tanh⁡(Winxt+bin+rt⊙(Whnh(t−1)+bhn))n_t = \tanh(W_{in} x_t + b_{in} + r_t \odot (W_{hn} h_{(t-1)} + b_{hn})) ht=(1−zt)⊙nt+zt⊙h(t−1)h_t = (1 - z_t) \odot n_t + z_t \odot h_{(t-1)}

    Where:

    • σ\sigma is the sigmoid activation function.
    • ⊙\odot indicates element-wise multiplication (Hadamard product).
    • rtr_t, ztz_t, and ntn_t are the reset gate, update gate, and new gate, respectively.
    • hth_t is the hidden state at the current time step.
    • WirW_{ir}, WizW_{iz}, and WinW_{in} are the input weight matrices with shape [H,I][H, I] (stored in transposed form as [3H,I][3H, I]).
    • WhrW_{hr}, WhzW_{hz}, WhnW_{hn} are hidden state weight matrices, with shape [H,H][H, H] (stored in transposed form as [3H,H][3H, H]).
    • If hasBias=true, birb_{ir}, bizb_{iz}, binb_{in} and bhrb_{hr}, bhzb_{hz}, bhnb_{hn} are the input biases and hidden state biases respectively, both with shape [H][H] (stored as [3H][3H]).

Function Prototype

Each operator uses a two-phase API. Call "aclnnGRUGetWorkspaceSize" first to obtain the workspace size required for computation and the executor that contains the computation flow of the operator, and then call "aclnnGRU" to execute the computation.

aclnnStatus aclnnGRUGetWorkspaceSize(
  const aclTensor     *input,
  const aclTensorList *params,
  const aclTensor     *hx,
  const aclTensor     *batchSizes,
  bool                 hasBias,
  int64_t              numLayers,
  double               dropout,
  bool                 train,
  bool                 bidirection,
  bool                 batchFirst,
  aclTensor           *output,
  aclTensor           *hy,
  aclTensorList       *rOut,
  aclTensorList       *zOut,
  aclTensorList       *nOut,
  aclTensorList       *hnOut,
  aclTensorList       *hOut,
  uint64_t            *workspaceSize,
  aclOpExecutor      **executor)
aclnnStatus aclnnGRU(
  void          *workspace,
  uint64_t       workspaceSize,
  aclOpExecutor *executor,
  aclrtStream    stream)

aclnnGRUGetWorkspaceSize

  • Parameters

    Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor
    input (aclTensor*) Input Input sequence of GRU, corresponding to x in the formula.
    • If a valid batchSizes is passed in, the shape is [sum(batch_size), input_size];
    • If a null pointer batchSizes is passed in, the shape is [time_step, batch_size, input_size] or [batch_size, time_step, input_size] (when batchFirst=true).
    FLOAT32, FLOAT16 ND 2-3 √
    params (aclTensorList*) Input List of weight and bias tensors of each GRU layer, corresponding to w and b in the formula.
    • If bidirection is True, `D = 2`; otherwise, `D = 1`. If hasBias is True, `B = 2`; otherwise, `B = 1`. The list length is 2 * B * D * num_layers (2 indicates the number of weights W_ih and W_hh per layer per direction).
    • If both bidirection and hasBias are True, the layout is: [weight_ih_0, weight_hh_0, bias_ih_0, bias_hh_0, weight_ih_reverse_0, weight_hh_reverse_0, bias_ih_reverse_0, bias_hh_reverse_0].
    • If hasBias is False, there is no bias item; if bidirection is False, there is no reverse item.
    • For multiple layers, the weights are arranged layer by layer.
    • The data type is consistent with input.
    • weight_ih: [3*hidden_size, cur_input_size] (cur_input_size = input_size for the first layer, and D*hidden_size for non-first layers)
      weight_hh: [3*hidden_size, hidden_size]
      bias_ih: [3*hidden_size]
      bias_hh: [3*hidden_size]
    FLOAT32, FLOAT16 ND / √
    hx (aclTensor*) Optional input Initial hidden state of each GRU layer. Corresponds to h(t-1) at time 0.
    • Optional parameter. Passing a null pointer indicates whether this parameter is used. If not, the initial hidden state is a zero vector.
    • For multiple bidirectional layers, each tensor is arranged along dimension 0, first by direction and then by layer.
    • The data type is consistent with input.
    • [numLayers * D, batch_size, hidden_size]
    FLOAT32, FLOAT16 ND 3 √
    batchSizes (aclTensor*) Optional input Number of valid sequence batches at each time step in the variable-length GRU input sequence, corresponding to the PackedSequence mode.
    • Optional parameter. Passing a null pointer indicates the fixed-length mode.
    • Passing a valid batchSizes enables the PackedSequence mode. The shape of input must be 2D [sum(batch_size), input_size].
    INT64 ND 1 √
    hasBias (bool) Input Indicates whether bias b is present, corresponding to $b_{ir}$, $b_{iz}$, $b_{in}$, $b_{hr}$, $b_{hz}$, and $b_{hn}$ in the formula. If hasBias is True, the params list contains bias parameters. Otherwise, it does not. - - - -
    numLayers (int64_t) Input Indicates the number of stacked GRU layers, corresponding to $L$ in the formula. Value range: integers greater than or equal to 1. - - - -
    dropout (double) Input Indicates the dropout probability. dropout is not implemented in the current version. Passing any value does not affect the computation result. - - - -
    train (bool) Input Indicates whether training mode is used. If train is True, rOut, zOut, nOut, hnOut, and hOut need to be passed for gate output. If train is False, pass null pointers for the corresponding parameters. - - - -
    bidirection (bool) Input Indicates whether it is a bidirectional GRU. When bidirection is True, both the forward and backward directions are computed at each time step. The output dimension is 2*H. - - - -
    batchFirst (bool) Input Indicates whether the batch dimension of input and output is the first dimension. When batchFirst is True, the shapes of input and output are (B, T, I) and (B, T, D*H); when batchFirst is False, the shapes are (T, B, I) and (T, B, D*H). - - - -
    output (aclTensor*) Output Indicates the output of all time steps, corresponding to $h_t$ in the formula.
    • If valid batchSizes is passed, the shape is [sum(batch_size), hidden_size * D];
    • If a null pointer batchSizes is passed, the shape is [time_step, batch_size, hidden_size * D] or [batch_size, time_step, hidden_size * D] (when batchFirst=true).
    FLOAT32, FLOAT16 ND 2-3 √
    hy (aclTensor*) Output Indicates the final hidden state of all directions in the last layer, corresponding to $h_t$ at the last time step in the formula. Shape: [numLayers * D, batch_size, hidden_size]. FLOAT32, FLOAT16 ND 3 √
    rOut (aclTensorList*) Optional output Indicates the reset gate output in training mode, corresponding to $r_t$ in the formula.
    • Required in training mode. Pass null pointer in inference mode.
    • List length = dScale * numLayers.
    • Fixed-length mode: each shape in the list is [time_step, batch_size, hidden_size];
    • Variable-length mode: each shape in the list is [sum(batch_size), hidden_size].
    FLOAT32, FLOAT16 ND 2-3 √
    zOut (aclTensorList*) Optional output Indicates the update gate output in training mode, corresponding to $z_t$ in the formula.
    • Required in training mode. Pass null pointer in inference mode.
    • List length = dScale * numLayers.
    • Fixed-length mode: each shape in the list is [time_step, batch_size, hidden_size];
    • Variable-length mode: each shape in the list is [sum(batch_size), hidden_size].
    FLOAT32, FLOAT16 ND 2-3 √
    nOut (aclTensorList*) Optional output Indicates the new gate output in training mode, which is $n_t$ in the corresponding formula.
    • Required in training mode. Pass null pointer in inference mode.
    • List length = dScale * numLayers.
    • Fixed-length mode: each tensor in the list has the shape [time_step, batch_size, hidden_size];
    • Variable-length mode: each tensor in the list has the shape [sum(batch_size), hidden_size].
    FLOAT32, FLOAT16 ND 2-3 √
    hnOut (aclTensorList*) optional output. Indicates the hidden state-new gate output $W_{hn} h_{(t-1)} + b_{hn}$ in training mode.
    • Mandatory in training mode. Pass null pointer in inference mode.
    • List length = dScale * numLayers.
    • Fixed-length mode: each shape in the list is [time_step, batch_size, hidden_size];
    • Variable-length mode: each shape in the list is [sum(batch_size), hidden_size].
    FLOAT32, FLOAT16 ND 2-3 √
    hOut (aclTensorList*) optional output. Indicates the hidden state output in training mode, that is, $h_t$ of each time step.
    • Required in training mode; pass null pointer in inference mode.
    • List length = dScale * numLayers.
    • Fixed-length mode: each shape in the list is [time_step, batch_size, hidden_size];
    • Variable-length mode: each shape in the list is [sum(batch_size), hidden_size].
    FLOAT32, FLOAT16 ND 2-3 √
    workspaceSize (uint64_t*) Output Returns the size of the workspace to be requested on the Device. - - - - -
    executor (aclOpExecutor**) Output Returns the op executor containing the computation flow of the operator. - - - - -
  • Return Value

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

    The first-phase API performs input parameter validation and returns an error in the following scenarios:

    Error Code Error Code Description
    ACLNN_ERR_PARAM_NULLPTR 161001 The passed-in input, params, output, or hy is a null pointer, or rOut, zOut, nOut, hnOut, or hOut is a null pointer in training mode.
    ACLNN_ERR_PARAM_INVALID 161002 The data types of input, elements in params, hx, output, hy, or gate outputs in training mode are out of the supported range (only FLOAT32 and FLOAT16 are supported).
    The data types of input, elements in params, hx, output, hy, or gate outputs in training mode are inconsistent.
    The list length of params is incorrect (should be 2 * bScale * dScale * numLayers).
    In training mode, the list length of rOut, zOut, nOut, hnOut, or hOut is incorrect (should be dScale * numLayers).
    The number of dimensions of input is not 2D or 3D.
    The dimension of weights in params is not 2D, or the dimension of biases is not 1D.
    The dimension of hx is not 3D, or the dimensions of output and hy are not 3D (fixed-length mode).
    The shapes of weights or biases in params do not meet expectations: $W_{ih}$ should be $[3H, I]$ for the first layer and $[3H, D*H]$ for other layers, $W_{hh}$ should be $[3H, H]$, and biases should be $[3H]$.

aclnnGRU

  • Parameters

    Parameter Input/Output Description
    workspace Input Workspace memory address applied for on the device.
    workspaceSize Input Workspace size applied for on the device, obtained by the first-phase API aclnnGRUGetWorkspaceSize.
    executor Input Executor of the op, including the computation process of the operator.
    stream Input Stream for task execution.
  • Return Value

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

Constraints

  • Deterministic computation:

    • Atlas A3 products, Atlas A2 products: aclnnGRU 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_gru.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;
}

void PrintOutResult(const std::string& name, const std::vector<int64_t>& shape, void** deviceAddr)
{
    auto size = GetShapeSize(shape);
    std::vector<float> resultData(size, 0);
    auto ret = aclrtMemcpy(
        resultData.data(), resultData.size() * sizeof(resultData[0]), *deviceAddr, size * sizeof(resultData[0]),
        ACL_MEMCPY_DEVICE_TO_HOST);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy %s from device to host failed. ERROR: %d\n", name.c_str(), ret); return);
    LOG_PRINT("=== %s shape=[", name.c_str());
    for (size_t i = 0; i < shape.size(); i++) {
        LOG_PRINT("%ld%s", shape[i], (i + 1 < shape.size()) ? "," : "");
    }
    LOG_PRINT("] ===\n");
    for (int64_t i = 0; i < size; i++) {
        LOG_PRINT("  [%ld] = %f\n", i, resultData[i]);
    }
    LOG_PRINT("\n");
}

int Init(int32_t deviceId, aclrtStream* stream)
{
    // Boilerplate: AscendCL 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);
    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);
    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);

    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];
    }

    *tensor = aclCreateTensor(
        shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND, shape.data(), shape.size(),
        *deviceAddr);
    return 0;
}

template <typename T>
int CreateAclTensorList(
    const std::vector<std::vector<int64_t>>& shapes, void** deviceAddr, aclDataType dataType, aclTensorList** tensor,
    T initVal = 1)
{
    int size = shapes.size();
    aclTensor* tensors[size];
    for (int i = 0; i < size; i++) {
        std::vector<T> hostData(GetShapeSize(shapes[i]), initVal);
        int ret = CreateAclTensor<float>(hostData, shapes[i], deviceAddr + i, dataType, tensors + i);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
    }
    *tensor = aclCreateTensorList(tensors, size);
    return ACL_SUCCESS;
}

int main()
{
    // 1. (Boilerplate) Device/stream initialization.
    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 inputs and outputs.
    int64_t timeStep = 2;
    int64_t batchSize = 3;
    int64_t inputSize = 4;
    int64_t hiddenSize = 5;
    int64_t gateNum = 3;
    int64_t numLayers = 1;
    bool isbias = false;
    bool batchFirst = false;
    bool bidirection = false;
    bool isTraining = true;
    double dropout = 0.0;
    int64_t dScale = bidirection ? 2 : 1;
    int64_t ldScale = numLayers * dScale;

    std::vector<int64_t> inputShape = {timeStep, batchSize, inputSize};
    std::vector<int64_t> outputShape = {timeStep, batchSize, dScale * hiddenSize};
    std::vector<int64_t> hyShape = {ldScale, batchSize, hiddenSize};
    std::vector<int64_t> hxShape = {ldScale, batchSize, hiddenSize};
    std::vector<std::vector<int64_t>> paramsListShape = {};

    auto curLayerInputSize = inputSize;
    for (int i = 0; i < numLayers; i++) {
        for (int64_t j = 0; j < dScale; j++) {
            paramsListShape.push_back({hiddenSize * gateNum, curLayerInputSize});
            paramsListShape.push_back({hiddenSize * gateNum, hiddenSize});
            if (isbias) {
                paramsListShape.push_back({hiddenSize * gateNum});
                paramsListShape.push_back({hiddenSize * gateNum});
            }
        }
        curLayerInputSize = dScale * hiddenSize;
    }

    std::vector<std::vector<int64_t>> rOutListShape;
    std::vector<std::vector<int64_t>> zOutListShape;
    std::vector<std::vector<int64_t>> nOutListShape;
    std::vector<std::vector<int64_t>> hnOutListShape;
    std::vector<std::vector<int64_t>> hOutListShape;

    if (isTraining) {
        for (int64_t i = 0; i < ldScale; i++) {
            rOutListShape.push_back({timeStep, batchSize, hiddenSize});
            zOutListShape.push_back({timeStep, batchSize, hiddenSize});
            nOutListShape.push_back({timeStep, batchSize, hiddenSize});
            hnOutListShape.push_back({timeStep, batchSize, hiddenSize});
            hOutListShape.push_back({timeStep, batchSize, hiddenSize});
        }
    }

    void* inputDeviceAddr = nullptr;
    std::vector<void*> paramsListDeviceAddr(paramsListShape.size(), nullptr);
    void* outputDeviceAddr = nullptr;
    void* hyDeviceAddr = nullptr;
    void* hxDeviceAddr = nullptr;

    std::vector<void*> rOutDeviceAddr;
    std::vector<void*> zOutDeviceAddr;
    std::vector<void*> nOutDeviceAddr;
    std::vector<void*> hnOutDeviceAddr;
    std::vector<void*> hOutDeviceAddr;

    aclTensor* input = nullptr;
    aclTensorList* params = nullptr;
    aclTensor* output = nullptr;
    aclTensor* hy = nullptr;
    aclTensor* hx = nullptr;

    aclTensorList* rOut = nullptr;
    aclTensorList* zOut = nullptr;
    aclTensorList* nOut = nullptr;
    aclTensorList* hnOut = nullptr;
    aclTensorList* hOut = nullptr;

    std::vector<float> inputHostData(GetShapeSize(inputShape), 1.0);
    ret = CreateAclTensor<float>(inputHostData, inputShape, &inputDeviceAddr, aclDataType::ACL_FLOAT, &input);
    CHECK_RET(ret == ACL_SUCCESS, return ret);

    ret = CreateAclTensorList<float>(paramsListShape, paramsListDeviceAddr.data(), aclDataType::ACL_FLOAT, ¶ms, 1.0);
    CHECK_RET(ret == ACL_SUCCESS, return ret);

    std::vector<float> outputHostData(GetShapeSize(outputShape), 0.0);
    ret = CreateAclTensor<float>(outputHostData, outputShape, &outputDeviceAddr, aclDataType::ACL_FLOAT, &output);
    CHECK_RET(ret == ACL_SUCCESS, return ret);

    std::vector<float> hyHostData(GetShapeSize(hyShape), 0.0);
    ret = CreateAclTensor<float>(hyHostData, hyShape, &hyDeviceAddr, aclDataType::ACL_FLOAT, &hy);
    CHECK_RET(ret == ACL_SUCCESS, return ret);

    bool useHx = false;
    if (useHx) {
        std::vector<float> hxHostData(GetShapeSize(hxShape), 0.5);
        ret = CreateAclTensor<float>(hxHostData, hxShape, &hxDeviceAddr, aclDataType::ACL_FLOAT, &hx);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
    }

    if (isTraining) {
        rOutDeviceAddr.resize(ldScale, nullptr);
        zOutDeviceAddr.resize(ldScale, nullptr);
        nOutDeviceAddr.resize(ldScale, nullptr);
        hnOutDeviceAddr.resize(ldScale, nullptr);
        hOutDeviceAddr.resize(ldScale, nullptr);

        ret = CreateAclTensorList<float>(rOutListShape, rOutDeviceAddr.data(), aclDataType::ACL_FLOAT, &rOut, 0.0);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
        ret = CreateAclTensorList<float>(zOutListShape, zOutDeviceAddr.data(), aclDataType::ACL_FLOAT, &zOut, 0.0);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
        ret = CreateAclTensorList<float>(nOutListShape, nOutDeviceAddr.data(), aclDataType::ACL_FLOAT, &nOut, 0.0);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
        ret = CreateAclTensorList<float>(hnOutListShape, hnOutDeviceAddr.data(), aclDataType::ACL_FLOAT, &hnOut, 0.0);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
        ret = CreateAclTensorList<float>(hOutListShape, hOutDeviceAddr.data(), aclDataType::ACL_FLOAT, &hOut, 0.0);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
    }

    // 3. Call the first-phase API of aclnnGRU.
    uint64_t workspaceSize = 0;
    aclOpExecutor* executor = nullptr;
    ret = aclnnGRUGetWorkspaceSize(
        input, params, (useHx ? hx : nullptr), nullptr, isbias, numLayers, dropout, isTraining, bidirection, batchFirst, output, hy,
        isTraining ? rOut : nullptr,
        isTraining ? zOut : nullptr,
        isTraining ? nOut : nullptr,
        isTraining ? hnOut : nullptr,
        isTraining ? hOut : nullptr,
        &workspaceSize, &executor);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnGRUGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);

    // 4. 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);
    }

    // 5. Call the second-phase API of aclnnGRU.
    ret = aclnnGRU(workspaceAddr, workspaceSize, executor, stream);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnGRU failed. ERROR: %d\n", ret); return ret);

    // 6. (Boilerplate) Synchronize and wait for the task to complete.
    ret = aclrtSynchronizeStream(stream);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);

    // 7. Obtain the output values.
    PrintOutResult("output", outputShape, &outputDeviceAddr);
    PrintOutResult("hy", hyShape, &hyDeviceAddr);
    if (isTraining) {
        for (int64_t i = 0; i < ldScale; i++) {
            PrintOutResult("rOut[" + std::to_string(i) + "]", rOutListShape[i], &rOutDeviceAddr[i]);
            PrintOutResult("zOut[" + std::to_string(i) + "]", zOutListShape[i], &zOutDeviceAddr[i]);
            PrintOutResult("nOut[" + std::to_string(i) + "]", nOutListShape[i], &nOutDeviceAddr[i]);
            PrintOutResult("hnOut[" + std::to_string(i) + "]", hnOutListShape[i], &hnOutDeviceAddr[i]);
            PrintOutResult("hOut[" + std::to_string(i) + "]", hOutListShape[i], &hOutDeviceAddr[i]);
        }
    }

    // Release the resources.
    aclDestroyTensor(input);
    aclDestroyTensorList(params);
    aclDestroyTensor(output);
    aclDestroyTensor(hy);
    if (hx != nullptr) {
        aclDestroyTensor(hx);
    }
    if (isTraining) {
        aclDestroyTensorList(rOut);
        aclDestroyTensorList(zOut);
        aclDestroyTensorList(nOut);
        aclDestroyTensorList(hnOut);
        aclDestroyTensorList(hOut);
    }
    aclrtFree(inputDeviceAddr);
    for (size_t i = 0; i < paramsListShape.size(); i++) {
        aclrtFree(paramsListDeviceAddr[i]);
    }
    aclrtFree(outputDeviceAddr);
    aclrtFree(hyDeviceAddr);
    if (hxDeviceAddr != nullptr) {
        aclrtFree(hxDeviceAddr);
    }
    if (isTraining) {
        for (size_t i = 0; i < rOutDeviceAddr.size(); i++) {
            aclrtFree(rOutDeviceAddr[i]);
            aclrtFree(zOutDeviceAddr[i]);
            aclrtFree(nOutDeviceAddr[i]);
            aclrtFree(hnOutDeviceAddr[i]);
            aclrtFree(hOutDeviceAddr[i]);
        }
    }
    if (workspaceSize > 0) {
        aclrtFree(workspaceAddr);
    }
    aclrtDestroyStream(stream);
    aclrtResetDevice(deviceId);
    aclFinalize();

    return 0;
}