aclnnThnnFusedLstmCellBackward

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: Performs backward propagation for the remaining computation after matmul in the four gates of an LSTMCell, computing the gradients of the pre-activation values of the four gates in the forward output, the input cx, and the bias b.
  • Formula:

Variable Definitions

  • Input gradients: δht\delta h_t (gradHy), δct\delta c_t (gradC)
  • Forward cache: i,f,g,oi, f, g, o (activation values of each gate storage), ct−1c_{t-1} (cx), ctc_t (cy)
  • Output gradients: δai,δaf,δag,δao\delta a_i, \delta a_f, \delta a_g, \delta a_o (stored in gradGatesOut), δct−1\delta c_{t-1} (gradCxOut)

Phase 1: Intermediate gradients and state backpropagation

First, compute the contribution of the hidden state to the cell state, and aggregate to obtain the total gradient of the cell at the current time step grad_ctotal\text{grad\_}c_{total}:

gcx=tanh⁡(ct)grad_ctotal=δht⋅o⋅(1−gcx2)+δctδct−1=grad_ctotal⋅f\begin{aligned} gcx &= \tanh(c_t) \\ \text{grad\_}c_{total} &= \delta h_t \cdot o \cdot (1 - gcx^2) + \delta c_t \\ \delta c_{t-1} &= \text{grad\_}c_{total} \cdot f \end{aligned}

Second phase: gate component gradients (Pre-activation)

Based on the code logic, the gradients δa\delta a of each gate before entering the activation function are calculated as follows:

δao=(δht⋅gcx)⋅o⋅(1−o)δai=(grad_ctotal⋅g)⋅i⋅(1−i)δaf=(grad_ctotal⋅ct−1)⋅f⋅(1−f)δag=(grad_ctotal⋅i)⋅(1−g2)\begin{aligned} \delta a_o &= (\delta h_t \cdot gcx) \cdot o \cdot (1 - o) \\ \delta a_i &= (\text{grad\_}c_{total} \cdot g) \cdot i \cdot (1 - i) \\ \delta a_f &= (\text{grad\_}c_{total} \cdot c_{t-1}) \cdot f \cdot (1 - f) \\ \delta a_g &= (\text{grad\_}c_{total} \cdot i) \cdot (1 - g^2) \end{aligned}

Stage 3: Parameter gradient (db)

1. Bias gradient (db): Sum over the Batch dimension (NN):

δb=∑n=1N[δaiδafδagδao]n\delta b = \sum_{n=1}^{N} \begin{bmatrix} \delta a_i \\ \delta a_f \\ \delta a_g \\ \delta a_o \end{bmatrix}_n

Function Prototype

Each operator uses a two-phase API. Call the "aclnnThnnFusedLstmCellBackwardGetWorkspaceSize" API first to obtain the workspace size required for computation and the executor containing the computation process of the operator, and then call the "aclnnThnnFusedLstmCellBackward" API to perform the computation.

aclnnStatus aclnnThnnFusedLstmCellBackwardGetWorkspaceSize(
  const aclTensor     *gradHyOptional,
  const aclTensor     *gradCOptional,
  const aclTensor     *cx,
  const aclTensor     *cy,
  const aclTensor     *storage,
  bool                hasBias,
  aclTensor           *gradGatesOut,
  aclTensor           *gradCxOut,
  aclTensor           *gradBiasOut,
  uint64_t            *workspaceSize,
  aclOpExecutor       **executor)
aclnnStatus aclnnThnnFusedLstmCellBackward(
  void            *workspace,
  uint64_t        workspaceSize,
  aclOpExecutor   *executor,
  aclrtStream     stream)

aclnnThnnFusedLstmCellBackwardGetWorkspaceSize

  • Parameters:

    Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor
    gradHyOptional Optional input Indicates the gradient of the hidden state in the LSTMCell forward output. - FLOAT32, FLOAT16 ND [batch, hidden_size] √
    gradCOptional Optional input Indicates the gradient of the cell state in the LSTMCell forward output. The data type must be consistent with that of gradHy. FLOAT32, FLOAT16 ND [batch, hidden_size] √
    cx Input Indicates the input cell state of LSTMCell forward propagation. The data type must be consistent with that of gradHy. FLOAT32, FLOAT16 ND [batch, hidden_size] √
    cy Input Indicates the cell state of the LSTMCell forward output. Data type: consistent with gradHy. FLOAT32, FLOAT16 ND [batch, hidden_size] √
    storage Input Indicates the activation values of the four gates in the LSTMCell forward output. Data type: consistent with gradHy. FLOAT32, FLOAT16 ND [batch, 4 * hidden_size] √
    hasBias Input Indicates whether the bias gradient needs to be computed. - BOOL - - -
    gradGatesOut Output Indicates the gradient of the pre-activation values of the four gates in the forward pass of LSTMCell. Data type: consistent with gradHy. FLOAT32, FLOAT16 ND [batch, 4 * hidden_size] √
    gradCxOut Output Indicates the gradient of the input cell state in LSTMCell forward propagation. Data type: consistent with gradHy. FLOAT32, FLOAT16 ND [batch, hidden_size] √
    gradBiasOut Output Indicates the gradient of the input bias in the LSTM forward propagation. Data type consistent with gradHy. FLOAT32, FLOAT16 ND [4 * hidden_size] √
    workspaceSize Output Returns the size of the workspace to be applied for on the Device. Host-side output parameter. UINT64 - - -
    executor Output Returns the op executor that contains the operator computation flow. Host-side output parameter. - - - -
  • Return value

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

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

    Error Code Error code Description
    ACLNN_ERR_PARAM_NULLPTR 161001 If the input parameter is an aclTensor that is a non-optional input, and it is a null pointer.
    ACLNN_ERR_PARAM_INVALID 161002 If the input parameter is an aclTensor and its data type is not within the supported range.
    If the input parameter type is aclTensor and the data types are inconsistent.
    If the input parameter type is aclTensor and the shape does not meet the corresponding shape requirements.

aclnnThnnFusedLstmCellBackward

  • Parameters:

    Parameter Input/Output Description
    workspace Input Address of the workspace memory allocated on the Device.
    workspaceSize Input Size of the workspace memory allocated on the Device, obtained by calling the first-phase API aclnnThnnFusedLstmCellBackwardGetWorkspaceSize.
    executor Input opExecutor that contains the operator computation process.
    stream Input Stream for task execution.
  • Return value

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

Constraints

  • Deterministic computation:
    • aclnnThnnFusedLstmCellBackward defaults to a deterministic implementation.
  • Boundary value scenario description:
    • If the input is Inf, the output is NaN.
    • When the input is NaN, the output is NaN.

Example

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

#include <iostream>
#include <cmath>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_thnn_fused_lstm_cell_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: 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);
  // Call aclrtMalloc to allocate memory on the device.
  auto ret = aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMalloc failed. ERROR: %d\n", ret); return ret);
  // Call aclrtMemcpy to copy data from the host to the 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);

  // 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 the device/stream. For details, see the AscendCL API list.
  // 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 inputs and outputs.
  // Define variables.
  int64_t n = 1;
  int64_t hiddenSize = 8;

  // Shape definition.
  std::vector<int64_t> bShape = {hiddenSize * 4};
  std::vector<int64_t> dhShape = {n, hiddenSize};
  std::vector<int64_t> gatesShape = {n, 4 * hiddenSize};

  // Device address pointer
  void* dhyDeviceAddr = nullptr;
  void* dcDeviceAddr = nullptr;
  void* cxDeviceAddr = nullptr;
  void* cyDeviceAddr = nullptr;
  void* storageDeviceAddr = nullptr;

  // Device address pointer for backward propagation outputs
  void* dgatesDeviceAddr = nullptr;
  void* dcPrevDeviceAddr = nullptr;
  void* dbDeviceAddr = nullptr;

  // ACL Tensor pointer
  aclTensor* dhy = nullptr;
  aclTensor* dc = nullptr;
  aclTensor* cx = nullptr;
  aclTensor* cy = nullptr;
  aclTensor* storage = nullptr;

  // ACL Tensor pointer for backward propagation outputs
  aclTensor* dgates = nullptr;
  aclTensor* dcPrev = nullptr;
  aclTensor* db = nullptr;

  std::vector<float> dhyHostData(n * hiddenSize, 1.0f);         // 1 * 8 = 8 ones
  std::vector<float> dcHostData(n * hiddenSize, 1.0f);          // 1 * 8 = 8 ones
  std::vector<float> cxHostData(n * hiddenSize, 1.0f);          // 1 * 8 = 8 ones
  std::vector<float> cyHostData(n * hiddenSize, 1.0f);          // 1 * 8 = 8 ones
  std::vector<float> storageHostData(n * hiddenSize * 4, 1.0f); // 1 * 8 * 4 = 32 ones

  // Host data for backward propagation outputs (initialized to 0)
  std::vector<float> dgatesHostData(n * hiddenSize * 4, 0.0f);
  std::vector<float> dcPrevHostData(n * hiddenSize, 0.0f);
  std::vector<float> dbHostData(hiddenSize * 4, 0.0f);

  // Create the dhy aclTensor.
  ret = CreateAclTensor(dhyHostData, dhShape, &dhyDeviceAddr, aclDataType::ACL_FLOAT, &dhy);
  CHECK_RET(ret == ACL_SUCCESS, return ret);

  // Create the dc aclTensor.
  ret = CreateAclTensor(dcHostData, dhShape, &dcDeviceAddr, aclDataType::ACL_FLOAT, &dc);
  CHECK_RET(ret == ACL_SUCCESS, return ret);

  // Create the cx aclTensor.
  ret = CreateAclTensor(cxHostData, dhShape, &cxDeviceAddr, aclDataType::ACL_FLOAT, &cx);
  CHECK_RET(ret == ACL_SUCCESS, return ret);

  // Create the cy aclTensor.
  ret = CreateAclTensor(cyHostData, dhShape, &cyDeviceAddr, aclDataType::ACL_FLOAT, &cy);
  CHECK_RET(ret == ACL_SUCCESS, return ret);

  // Create the storage aclTensor.
  ret = CreateAclTensor(storageHostData, gatesShape, &storageDeviceAddr, aclDataType::ACL_FLOAT, &storage);
  CHECK_RET(ret == ACL_SUCCESS, return ret);

  // Create the backward propagation output tensors.
  // Create the dgates aclTensor.
  ret = CreateAclTensor(dgatesHostData, gatesShape, &dgatesDeviceAddr, aclDataType::ACL_FLOAT, &dgates);
  CHECK_RET(ret == ACL_SUCCESS, return ret);

  // Create the dcPrev aclTensor.
  ret = CreateAclTensor(dcPrevHostData, dhShape, &dcPrevDeviceAddr, aclDataType::ACL_FLOAT, &dcPrev);
  CHECK_RET(ret == ACL_SUCCESS, return ret);

  // Create the db aclTensor.
  ret = CreateAclTensor(dbHostData, bShape, &dbDeviceAddr, aclDataType::ACL_FLOAT, &db);
  CHECK_RET(ret == ACL_SUCCESS, return ret);

  // 3. Call the aclnnThnnFusedLstmCellBackward API.
  uint64_t workspaceSize = 0;
  aclOpExecutor* executor;
  // Call the first-phase API of aclnnThnnFusedLstmCellBackward.
  ret = aclnnThnnFusedLstmCellBackwardGetWorkspaceSize(dhy, dc, cx, cy, storage, true, dgates, dcPrev, db,
    &workspaceSize, &executor);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnThnnFusedLstmCellBackwardGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
  // Allocate device memory based on the workspaceSize computed 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 aclnnThnnFusedLstmCellBackward.
  ret = aclnnThnnFusedLstmCellBackward(workspaceAddr, workspaceSize, executor, stream);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnThnnFusedLstmCellBackward failed. ERROR: %d\n", ret); return ret);

  // 4. (boilerplate) Wait for the task execution to complete.
  ret = aclrtSynchronizeStream(stream);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);

  // 5. Obtain the output value and copy the result from device memory to host memory.
  // Print the result.
  auto dgatesSize = GetShapeSize(gatesShape);
  std::vector<float> resultDgatesData(dgatesSize, 0);
  ret = aclrtMemcpy(resultDgatesData.data(), resultDgatesData.size() * sizeof(resultDgatesData[0]), dgatesDeviceAddr,
                    dgatesSize * sizeof(resultDgatesData[0]), ACL_MEMCPY_DEVICE_TO_HOST);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy dgates result from device to host failed. ERROR: %d\n", ret); return ret);
  for (int64_t i = 0; i < dgatesSize; i++) {
    LOG_PRINT("result dgates[%ld] is: %f\n", i, resultDgatesData[i]);
  }

  auto dbSize = GetShapeSize(bShape);
  std::vector<float> resultDbData(dbSize, 0);
  ret = aclrtMemcpy(resultDbData.data(), resultDbData.size() * sizeof(resultDbData[0]), dbDeviceAddr,
                    dbSize * sizeof(resultDbData[0]), ACL_MEMCPY_DEVICE_TO_HOST);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy db result from device to host failed. ERROR: %d\n", ret); return ret);
  for (int64_t i = 0; i < dbSize; i++) {
    LOG_PRINT("result db[%ld] is: %f\n", i, resultDbData[i]);
  }

  auto dcPrevSize = GetShapeSize(dhShape);
  std::vector<float> resultDcPrevData(dcPrevSize, 0);
  ret = aclrtMemcpy(resultDcPrevData.data(), resultDcPrevData.size() * sizeof(resultDcPrevData[0]), dcPrevDeviceAddr,
                    dcPrevSize * sizeof(resultDcPrevData[0]), ACL_MEMCPY_DEVICE_TO_HOST);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy dcPrev result from device to host failed. ERROR: %d\n", ret); return ret);
  for (int64_t i = 0; i < dcPrevSize; i++) {
    LOG_PRINT("result dcPrev[%ld] is: %f\n", i, resultDcPrevData[i]);
  }
  // Release the aclTensor.
  aclDestroyTensor(dhy);
  aclDestroyTensor(dc);
  aclDestroyTensor(cx);
  aclDestroyTensor(cy);
  aclDestroyTensor(storage);
  aclDestroyTensor(dgates);
  aclDestroyTensor(dcPrev);
  aclDestroyTensor(db);

  // Release Device resources.
  aclrtFree(dhyDeviceAddr);
  aclrtFree(dcDeviceAddr);
  aclrtFree(cxDeviceAddr);
  aclrtFree(cyDeviceAddr);
  aclrtFree(storageDeviceAddr);
  aclrtFree(dgatesDeviceAddr);
  aclrtFree(dcPrevDeviceAddr);
  aclrtFree(dbDeviceAddr);
  if (workspaceSize > 0) {
    aclrtFree(workspaceAddr);
  }
  aclrtDestroyStream(stream);
  aclrtResetDevice(deviceId);
  aclFinalize();

  return 0;
}