aclnnRepeatInterleaveGrad

📄 View Source Code

Applicable Products

  • Ascend 950PR&950DT products: Supported
  • Atlas A3 products: Not supported
  • Atlas A2 products: Not supported
  • Atlas 200I/500 A2 inference products: Not supported
  • Atlas inference products: Not supported
  • Atlas training products: Not supported

Function

  • Description: backward propagation of the repeatInterleave operator, which performs ReduceSum on the axis dimension of the yGrad tensor according to repeats.

  • Example: Assume tensor yGrad is ([[a1, b1, c1, d1, e1, f1], [a2, b2, c2, d2, e2, f2]]), repeats is ([1, 2, 2, 1]), and axis is 1. Then the resulting tensor is tensor([[a1, b1 + c1, d1 + e1, f1], [a2, b2 + c2, d2 + e2, f2]]). This performs ReduceSum on the axis dimension of tensor yGrad according to repeats.

    Assume that the yGrad tensor is ([[a1, b1, c1, d1, e1, f1], [a2, b2, c2, d2, e2, f2]]), repeats is ([2]), and axis is 1. The final tensor is tensor([[a1 + b1, c1 + d1, e1 + f1], [a2 + b2, c2 + d2, e2 + f2]]). Perform ReduceSum on every two elements along the axis dimension of the yGrad tensor according to the value of repeats. Note: This use case is equivalent to repeats being (2).

Function Prototype

Each operator uses a two-phase API. Call the "aclnnRepeatInterleaveGradGetWorkspaceSize" API first to obtain the workspace size required for the computation and the executor that contains the operator computation flow. Then, call the "aclnnRepeatInterleaveGrad" API to perform the computation.

 aclnnStatus aclnnRepeatInterleaveGradGetWorkspaceSize(
   const aclTensor *yGrad,
   const aclTensor *repeats,
   int64_t          axis,
   const aclTensor *out,
   uint64_t        *workspaceSize,
   aclOpExecutor  **executor)
aclnnStatus aclnnRepeatInterleaveGrad(
  void          *workspace,
  uint64_t       workspaceSize,
  aclOpExecutor *executor,
  aclrtStream    stream)

aclnnRepeatInterleaveGradGetWorkspaceSize

  • Parameters:

    Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor
    yGrad Input Input tensor to be reduced by ReduceSum in the Function section. Empty tensors are supported. FLOAT16, BFLOAT16, FLOAT ND 0-8 √
    repeats Input Number of repetitions. repeats must be a 0D / 1D tensor. If repeats is a 1D tensor with a size of 1, repeats supports broadcasting. If repeats is a 1D tensor with a size greater than 1, the sum of the elements in repeats must equal the size of the axis dimension of yGrad. Empty tensors are not supported. INT32, INT64 ND √
    axis Input Dimension along which ReduceSum is performed. Value range of axis: [-n, n), where n is the number of dimensions of yGrad.
    out Output Output tensor of ReduceSum described in Function. For shape constraints, see Constraints. Same as yGrad ND -
    workspaceSize Output Returns the size of the workspace to be allocated on the Device. - - - - -
    executor Output Returns the op executor, which contains the operator computation process. - - - - -
    • Ascend 950PR&950DT products : The data type of yGrad supports FLOAT16, BFLOAT16, and FLOAT.
  • 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:

    Return Value Error Code Description
    ACLNN_ERR_PARAM_NULLPTR 161001 yGrad, repeats, or out is a null pointer.
    ACLNN_ERR_PARAM_INVALID 161002 The data types of yGrad and repeats are not in the supported range.
    The data types of yGrad and out are different.
    ACLNN_ERR_INNER_TILING_ERROR 561002 repeats is an empty tensor.

aclnnRepeatInterleaveGrad

  • Parameters:

    Parameter Input/Output Description
    workspace Input Address of the workspace memory allocated on the Device.
    workspaceSize Input Size of the workspace allocated on the Device, obtained by the first-phase API aclnnRepeatInterleaveGradGetWorkspaceSize.
    executor Input Op executor that contains the operator computation process.
    stream Input Stream for executing tasks.
  • Return value

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

Constraints

  • Deterministic computation: aclnnRepeatInterleaveGrad defaults to a deterministic implementation.

The following requirements must be met during computation:

  • If repeats is a 0D tensor or a 1D tensor with size 1, the element value of repeats must be a divisor of the dimension of yGrad on axis. If it is a 1D tensor with a size greater than 1, the sum of the elements in repeats must equal the dimension of yGrad along axis. The values in the repeats tensor must be natural numbers.
  • The shape size of out satisfies the shape size after ReduceSum along the axis of yGrad. For example, if the shape of yGrad is [64], repeats is [2], and axis is 0, the shape of out can be [32], [2, 16], [2, 4, 4], and so on, as long as the size of the shape of out is 32.

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_repeat_interleave_grad.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); 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 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);

  // Compute the strides of the contiguous tensor.
  std::vector<int64_t> strides(shape.size(), 1);
  for (int64_t i = shape.size() - 2; i >= 0; i--) {
    strides[i] = shape[i + 1] * strides[i + 1];
  }

  // Call 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) Device/stream initialization. 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);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);

  // 2. Construct input and output. Customize the construction according to the API.
  std::vector<int64_t> yGradShape = {4, 3};
  std::vector<int64_t> repeatsShape = {2};
  std::vector<int64_t> outShape = {2, 3};
  void* yGradDeviceAddr = nullptr;
  void* repeatsDeviceAddr = nullptr;
  void* outDeviceAddr = nullptr;
  aclTensor* yGrad = nullptr;
  aclTensor* repeats = nullptr;
  aclTensor* out = nullptr;
  int64_t axis = 0;
  std::vector<float> yGradHostData = {3, 4, 5, 3, 4, 5, -3, -4, -5, -3, -4, -5};
  std::vector<int64_t> repeatsHostData = {2, 2};
  std::vector<float> outHostData = {0, 0, 0, 0, 0, 0};

  // Create the yGrad aclTensor.
  ret = CreateAclTensor(yGradHostData, yGradShape, &yGradDeviceAddr, aclDataType::ACL_FLOAT, &yGrad);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  // Create the repeats aclTensor.
  ret = CreateAclTensor(repeatsHostData, repeatsShape, &repeatsDeviceAddr, aclDataType::ACL_INT64, &repeats);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  // Create the out aclTensor.
  ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_FLOAT, &out);
  CHECK_RET(ret == ACL_SUCCESS, return ret);

  // 3. Call the CANN operator library API. Replace it with the specific API name.
  uint64_t workspaceSize = 0;
  aclOpExecutor* executor;
  // Call the first-phase API of aclnnRepeatInterleaveGrad.
  ret = aclnnRepeatInterleaveGradGetWorkspaceSize(yGrad, repeats, axis, out, &workspaceSize, &executor);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnRepeatInterleaveGradGetWorkspaceSize 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 aclnnRepeatInterleaveGrad.
  ret = aclnnRepeatInterleaveGrad(workspaceAddr, workspaceSize, executor, stream);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnRepeatInterleaveGrad 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. Modify the code based on the specific API definition.
  auto size = GetShapeSize(outShape);
  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("result[%ld] is: %f\n", i, resultData[i]);
  }

  // 6. Release aclTensor and aclScalar. Modify the code based on the specific API definition.
  aclDestroyTensor(yGrad);
  aclDestroyTensor(repeats);
  aclDestroyTensor(out);

  // 7. Release device resources. Modify the code based on the specific API definition.
  aclrtFree(yGradDeviceAddr);
  aclrtFree(repeatsDeviceAddr);
  aclrtFree(outDeviceAddr);
  if (workspaceSize > 0) {
    aclrtFree(workspaceAddr);
  }
  aclrtDestroyStream(stream);
  aclrtResetDevice(deviceId);
  aclFinalize();
  return 0;
}