aclnnAvgPool2dBackward

📄 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: Supported

Function

  • Description: Backward propagation of 2D average pooling. Computes the gradient of the input for the forward propagation of 2D average pooling.

  • Formula: Assume that the input tensor of the 2D average pooling forward pass is XX, the output tensor is YY, the kernel size is k∗kk*k, and the stride is ss. Then the gradient of XX, ∂L∂X\frac{\partial L}{\partial X}, is calculated as follows:

    ∂L∂Xi,j=1k2∑n=0k−1∂L∂Y⌊i∗s+mk⌋,⌊j∗s+nk⌋\frac{\partial L}{\partial X_{i,j}}=\frac{1}{k^2}\sum_{n=0}^{k-1}\frac{\partial L}{\partial Y_{\lfloor\frac{i*s+m}{k}\rfloor,\lfloor\frac{j*s+n}{k}\rfloor}}

    The parameters are described as follows:

    • LL is the loss function, and ⌊⋅⌋\lfloor\cdot\rfloor indicates rounding up.
    • Xi,jX_{i,j} represents the ii-th row and jj-th column of the input feature map.
    • Y⌊i∗s+mk⌋,⌊j∗s+nk⌋Y_{\lfloor\frac{i*s+m}{k}\rfloor,\lfloor\frac{j*s+n}{k}\rfloor} represents the pixel value at the ⌊i∗s+mk⌋\lfloor\frac{i*s+m}{k}\rfloor-th row and ⌊j∗s+nk⌋\lfloor\frac{j*s+n}{k}\rfloor-th column of the output feature map.
    • kk represents the kernel size of the pooling window.
    • ss represents the stride size.
    • ∂L∂Xi,j\frac{\partial L}{\partial X_{i,j}} represents the partial derivative of the loss function LL with respect to the pixel value at row ii, column jj in the input feature map.
    • ∂L∂Y⌊i∗s+mk⌋,⌊j∗s+nk⌋\frac{\partial L}{\partial Y_{\lfloor\frac{i*s+m}{k}\rfloor,\lfloor\frac{j*s+n}{k}\rfloor}} represents the partial derivative of the loss function LL with respect to the pixel value at row ⌊i∗s+mk⌋\lfloor\frac{i*s+m}{k}\rfloor, column ⌊j∗s+nk⌋\lfloor\frac{j*s+n}{k}\rfloor in the output feature map.

Function Prototype

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

aclnnStatus aclnnAvgPool2dBackwardGetWorkspaceSize(
  const aclTensor   *gradOutput,
  const aclTensor   *self,
  const aclIntArray *kernelSize,
  const aclIntArray *stride,
  const aclIntArray *padding,
  bool               ceilMode,
  bool               countIncludePad,
  int64_t            divisorOverride,
  int8_t             cubeMathType,
  aclTensor         *gradInput,
  uint64_t          *workspaceSize,
  aclOpExecutor     **executor)
aclnnStatus aclnnAvgPool2dBackward(
  void          *workspace,
  uint64_t       workspaceSize,
  aclOpExecutor *executor,
  aclrtStream    stream)

aclnnAvgPool2dBackwardGetWorkspaceSize

  • Parameters:

    Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor
    gradOutput Input Input gradient. Empty tensors are not supported. FLOAT32, FLOAT16, BFLOAT16 NCHW, NCL 3-4 √
    self Input Input during forward propagation. Empty tensors are not supported. FLOAT32, FLOAT16, BFLOAT16 NCHW, NCL 3-4 √
    kernelSize Input Size of the pooling window, k in the formula. Length: 1 (kH=kW) or 2 (kH, kW), with values greater than 0 INT64 - - -
    stride Input Stride of the pooling operation, that is, strides in the formula. The length is 0 (stride=kernelSize), 1 (sH=sW), or 2 (sH, sW), and the value must be greater than 0. INT64 - - -
    padding Input Number of layers padded with zeros on the H and W dimensions of the input, which is paddings in the formula. Length of 1 (padH=padW) or 2 (padH, padW). The value must not be less than 0. INT64 - - -
    ceilMode Input Whether the shape of the derived output out is rounded up. - BOOL - - -
    countIncludePad Input Whether zeros added by padding are included when computing average pooling. - BOOL - - -
    divisorOverride Input Divisor used to calculate the average. Atlas inference products and Atlas training products: the supported range is [-255,255]. 0 does not affect whether padding participates in the average computation. INT64 - - -
    cubeMathType Input Specifies the computation logic of the Cube unit. - INT8 - - -
    gradInput Output Output tensor, that is, out in the formula. - FLOAT32, FLOAT16, BFLOAT16 NCHW, NCL 3-4 √
    workspaceSize Output Returns the workspace size to be allocated on the Device. - - - - -
    executor Output Returns the op executor that contains the operator computation process. - - - - -
    Atlas inference products, Atlas training products: The data types of parameters self and out do not support BFLOAT16.

    cubeMathType: The supported enumeration values are as follows:

    • 0: KEEP_DTYPE, which keeps the input data type for computation.
      • Atlas training products, Atlas inference products: This option is not supported when the input data type is FLOAT32.
    • 1: ALLOW_FP32_DOWN_PRECISION, which allows the input data to be computed at reduced precision.
      • Atlas training products, Atlas inference products: When the input data type is FLOAT32, it is converted to FLOAT16 for computation. No processing is performed for other input data types.
      • Atlas A2 products, Atlas A3 products: When the input data type is FLOAT32, it is converted to HFLOAT32 for computation. No processing is performed for other input data types.
      • Ascend 950PR&950DT products: In globalPooling mode, when the input data type is FLOAT32, it is converted to HFLOAT32 for computation. No processing is performed for other input data types.
    • 2: USE_FP16, supports downscaling the input precision to FLOAT16 for computation.
      • Atlas A2 products, Atlas A3 products: This option is not supported when the input data type is BFLOAT16.
      • Ascend 950PR&950DT products: In globalPooling mode, this option is not supported when the input data type is BFLOAT16.
    • 3: USE_HF32, supports reducing input precision to HFLOAT32 for computation.
      • Atlas training products, Atlas inference products: This option is not supported.
      • Atlas A2 products, Atlas A3 products: When the input data type is FLOAT32, the input is converted to HFLOAT32 for computation. This option is not supported for other input data types.
      • Ascend 950PR&950DT products: In globalPooling mode, when the input data type is FLOAT32, the input is converted to HFLOAT32 for computation. This option is not supported for other input data types.
  • 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 gradOutput, self, kernelSize, padding, or gradInput is a null pointer.
    ACLNN_ERR_PARAM_INVALID 161002 The data type/data format of gradOutput or gradInput is out of the supported range.
    The data type or data format of self is inconsistent with that of gradInput.
    The dimensions of gradOutput, self, kernelSize, stride, or gradInput are out of the supported range.
    A dimension value of gradOutput, self, kernelSize, stride, or gradInput is less than 0.
    cubeMathType is out of the supported range.
    The padding attribute exceeds 1/2 of kernelSize at the corresponding position. For example, paddingH=2, kernelSizeH=2, and paddingH>kernelSizeH*1/2.
    The divisorOverride passed in is out of the supported range, but this check is not performed for Ascend 950PR&950DT products.

aclnnAvgPool2dBackward

  • Parameters:

    Parameter Input/Output Description
    workspace Input Workspace memory address allocated on the device.
    workspaceSize Input Size of the workspace allocated on the device, obtained by calling the first-phase API aclnnAvgPool2dBackwardGetWorkspaceSize.
    executor Input Op executor, including the operator computation flow.
    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, Atlas inference products, Atlas training products: aclnnAvgPool2dBackward defaults to a non-deterministic implementation. Deterministic computation can be enabled through aclrtCtxSetSysParamOpt.
    • Ascend 950PR&950DT products: aclnnAvgPool2dBackward defaults to a deterministic implementation.
  • Atlas training products, Atlas inference products: The Cube unit does not support FLOAT32 computation. If the input is FLOAT32, set cubeMathType=1 (ALLOW_FP32_DOWN_PRECISION) to allow the API to cast the input to FLOAT16 for internal computation.

Example

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

#include <cstdio>
#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_avgpool2d_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, aclFormat Format = aclFormat::ACL_FORMAT_ND) {
  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 data to 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);

  // 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, Format, shape.data(), shape.size(), *deviceAddr);
  return 0;
}

int main() {
  // 1. (Boilerplate) Initialize the device/stream. For details, see the list of acl external APIs.
  // 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. Custom-construct the inputs and outputs based on the API.
  std::vector<int64_t> gradOutputShape = {1, 16, 1, 1};
  std::vector<int64_t> selfShape = {1, 16, 4, 4};
  std::vector<int64_t> kernelDims = {4, 4};
  std::vector<int64_t> strideDims = {1, 1};
  std::vector<int64_t> paddingDims = {0, 0};
  bool ceilMode = false;
  int64_t divisorOverride = 0;
  bool countIncludePad = true;
  int8_t cubeMathType = 1;
  std::vector<int64_t> gradInputShape = {1, 16, 4, 4};

  void* gradOutputDeviceAddr = nullptr;
  void* selfDeviceAddr = nullptr;
  void* gradInputDeviceAddr = nullptr;

  aclTensor* gradOutput = nullptr;
  aclTensor* self = nullptr;
  aclTensor* gradInput = nullptr;

  std::vector<float> gradOutputHostData(GetShapeSize(gradOutputShape) * 2, 1);
  std::vector<float> selfHostData(GetShapeSize(selfShape) * 2, 1);
  std::vector<float> gradInputHostData(GetShapeSize(gradInputShape) * 2, 1);

  // Create the gradOutput aclTensor.
  ret = CreateAclTensor(gradOutputHostData, gradOutputShape, &gradOutputDeviceAddr, aclDataType::ACL_FLOAT, &gradOutput, aclFormat::ACL_FORMAT_NCHW);
  CHECK_RET(ret == ACL_SUCCESS, return ret);

  // Create the self aclTensor.
  ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_FLOAT, &self, aclFormat::ACL_FORMAT_NCHW);
  CHECK_RET(ret == ACL_SUCCESS, return ret);

  // Create the gradInput aclTensor.
  ret = CreateAclTensor(gradInputHostData, gradInputShape, &gradInputDeviceAddr, aclDataType::ACL_FLOAT, &gradInput, aclFormat::ACL_FORMAT_NCHW);
  CHECK_RET(ret == ACL_SUCCESS, return ret);

  // Create the kernel aclIntArray.
  aclIntArray *kernelSize = aclCreateIntArray(kernelDims.data(), 2);

  // Create the stride aclIntArray.
  aclIntArray *stride = aclCreateIntArray(strideDims.data(), 2);

  // Create the paddings aclIntArray.
  aclIntArray *padding = aclCreateIntArray(paddingDims.data(), 2);

  // 3. Call the CANN operator library API. Change it to the actual API name.
  uint64_t workspaceSize = 0;
  aclOpExecutor* executor;
  // Call the first-phase API of aclnnAvgPool2dBackward.
  ret = aclnnAvgPool2dBackwardGetWorkspaceSize(gradOutput,
                                       self,
                                       kernelSize,
                                       stride,
                                       padding,
                                       ceilMode,
                                       countIncludePad,
                                       divisorOverride,
                                       cubeMathType,
                                       gradInput,
                                       &workspaceSize,
                                       &executor);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnAvgPool2dBackwardGetWorkspaceSize 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 aclnnAvgPool2dBackward.
  ret = aclnnAvgPool2dBackward(workspaceAddr, workspaceSize, executor, stream);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnAvgPool2dBackward failed. ERROR: %d\n", ret); return ret);

  // 4. (boilerplate) Synchronize and 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, copy the result on the device side to the host side, and modify the code according to the specific API definition.
  auto size = GetShapeSize(gradInputShape);
  std::vector<float> outData(size, 0);
  ret = aclrtMemcpy(outData.data(), outData.size() * sizeof(outData[0]), gradInputDeviceAddr,
                    size * sizeof(outData[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, outData[i]);
  }

  // 6. Release aclTensor and aclScalar, and modify the code according to the specific API definition.
  aclDestroyTensor(gradOutput);
  aclDestroyTensor(self);
  aclDestroyTensor(gradInput);
  aclDestroyIntArray(kernelSize);
  aclDestroyIntArray(stride);
  aclDestroyIntArray(padding);

  // 7. Release device resources, and modify the code according to the specific API definition.
  aclrtFree(gradOutputDeviceAddr);
  aclrtFree(selfDeviceAddr);
  aclrtFree(gradInputDeviceAddr);
  if (workspaceSize > 0) {
    aclrtFree(workspaceAddr);
  }
  aclrtDestroyStream(stream);
  aclrtResetDevice(deviceId);
  aclFinalize();

  return 0;
}