aclnnAvgPool2d

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: Performs a 2D average pooling operation on the input tensor with a window of kH∗kWkH * kW and a stride of sH∗sWsH * sW, where kk is kernelSize, representing the pooling window size, and ss is stride, representing the step size of the pooling operation.
  • Formula: The relationship between the input (N,C,H,WN,C,H,W), output (N,C,Hout,WoutN,C,H_{out},W_{out}), pooling stride (stridesstrides), and pooling window size (kH,kWkH,kW) is:Hout=⌊Hin+2∗paddings[0]−kHstrides[0]+1⌋H_{out}=\lfloor \frac{H_{in}+2*paddings[0]-kH}{strides[0]}+1 \rfloor Wout=⌊Win+2∗paddings[1]−kWstrides[1]+1⌋W_{out}=\lfloor \frac{W_{in}+2*paddings[1]-kW}{strides[1]}+1 \rfloor If ceilMode is true and the following condition is met:(Hout−1)∗stride[0]>=Hin+padding[0](H_{out} - 1) * stride[0] >= H_{in} + padding[0] then the shape of H_{out} needs to be reduced by 1. The same applies to W_{out}.out(Ni,Ci,h,w)=1kH∗kW∑m=0kH−1∑n=0kW−1input(Ni,Ci,strides[0]∗h+m,strides[1]∗w+n)out(N_i,C_i,h,w)=\frac{1}{kH*kW}\sum_{m=0}^{kH-1}\sum_{n=0}^{kW-1}input(N_i,C_i,strides[0]*h+m,strides[1]*w+n)

Function Prototype

Each operator uses a two-phase API. Call "aclnnAvgPool2dGetWorkspaceSize" first to obtain the workspace size required for the computation and the executor that contains the computation process of the operator. Then call "aclnnAvgPool2d" to perform the computation.

aclnnStatus aclnnAvgPool2dGetWorkspaceSize(
  const aclTensor   *self,
  const aclIntArray *kernelSize,
  const aclIntArray *strides,
  const aclIntArray *paddings,
  const bool         ceilMode,
  const bool         countIncludePad,
  const int64_t      divisorOverride,
  const int8_t       cubeMathType,
  aclTensor         *out,
  uint64_t          *workspaceSize,
  aclOpExecutor     **executor)
aclnnStatus aclnnAvgPool2d(
  void          *workspace,
  uint64_t       workspaceSize,
  aclOpExecutor *executor,
  aclrtStream    stream)

aclnnAvgPool2dGetWorkspaceSize

  • Parameters:

    Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor
    self Input Represents the input tensor for average pooling, i.e., input in the formula. Empty tensors are not supported. FLOAT32, FLOAT16, BFLOAT16 NCHW, NCL 3-4 √
    kernelSize Input Pooling window size, i.e., k in the formula. Length is 1 (kH=kW) or 2 (kH, kW), and the value is greater than 0. INT64 - - -
    strides Input Stride of the pooling operation, corresponding to strides in the formula. Length is 0 (sH=kH, sW=kW), or length is 1 (sH=sW) or 2 (sH, sW), and the value must be greater than 0. INT64 - - -
    paddings Input The number of 0-padding layers on the H and W dimensions of the input, that is, paddings in the formula. The length is 1 (padH=padW) or 2 (padH, padW), and the value cannot be less than 0. INT64 - - -
    ceilMode Input Whether the shape of the deduced output out is rounded up. - BOOL - - -
    countIncludePad Input Whether padded zeros are included in average pooling computation. - BOOL - - -
    divisorOverride Input Divisor used for averaging. If set to 0, the function is disabled. INT64 - - -
    cubeMathType Input Specifies the computation logic of the Cube unit. - INT8 - - -
    out Output The output tensor, out in the formula. - FLOAT32, FLOAT16, BFLOAT16 NCHW, NCL 3-4 √
    workspaceSize Output Returns the workspace size that needs to be applied on the Device side. - - - - -
    executor Output Returns the op executor, which contains the operator computation flow. - - - - -
    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, the computation is converted to FLOAT16. When the input is of other data types, no processing is performed.
      • Atlas A2 products, Atlas A3 products: When the input data type is FLOAT32, this option is not supported. When the input is of other data types, no processing is performed.
    • 2: USE_FP16, supports downscaling the input precision to FLOAT16 for computation.
      • Atlas A2 products, Atlas A3 products: When the input data type is BFLOAT16, this option is not supported.
    • 3: USE_HF32. This option is not supported.
  • 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 self, kernelSize, strides, paddings, or out is a null pointer.
    ACLNN_ERR_PARAM_INVALID 161002 The data type/data format of the passed-in self or out is not in the supported range.
    The data types/data formats of the passed-in self and out are inconsistent.
    A dimension value of the passed-in self, paddings, or out is less than 0.
    A dimension value of the passed-in kernelSize or strides is less than or equal to 0.
    The length of the passed-in kernelSize or paddings is less than 1.
    The output shape computed based on the average pooling semantics is inconsistent with the specified shape.
    The padding attribute exceeds 1/2 of the corresponding value in kernelSize. For example, paddingH=2, kernelSizeH=2, and paddingH>kernelSizeH*1/2.

aclnnAvgPool2d

  • Parameters:

    Parameter Input/Output Description
    workspace Input Memory address of the workspace applied on the side of the Device.
    workspaceSize Input Size of the workspace applied on the side of the Device, obtained by the first-phase API aclnnAvgPool2dGetWorkspaceSize.
    executor Input Operator executor containing the operator computation flow.
    stream Input Specifies the stream for executing tasks.
  • Return value

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

Constraints

  • Deterministic computation:
    • aclnnAvgPool2d defaults to a deterministic implementation.
  • Atlas training products: The Cube unit does not support FLOAT32 computation. When the input is FLOAT32, set cubeMathType=1(ALLOW_FP32_DOWN_PRECISION) to allow the API to internally cast to FLOAT16 for 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 <memory>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_avgpool2d.h"

#define CHECK_RET(cond, return_expr) \
  do {                               \
    if (!(cond)) {                   \
      return_expr;                   \
    }                                \
  } while (0)

#define CHECK_FREE_RET(cond, return_expr) \
  do {                                     \
      if (!(cond)) {                       \
          Finalize(deviceId, stream);      \
          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);

  // Compute the strides of a 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_NCHW,
                            shape.data(), shape.size(), *deviceAddr);
  return 0;
}

void Finalize(int32_t deviceId, aclrtStream& stream)
{
  aclrtDestroyStream(stream);
  aclrtResetDevice(deviceId);
  aclFinalize();
}

int aclnnAvgPool2dTest(int32_t deviceId, aclrtStream& stream) {
  auto ret = Init(deviceId, &stream);
  CHECK_FREE_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);

  // 2. Construct the input and output. Custom construction is required based on the API
  int64_t divisorOverride = 0;
  bool countIncludePad = true;
  bool ceilMode = false;
  int8_t cubeMathType = 2;

  std::vector<int64_t> selfShape = {1, 16, 4, 4};
  std::vector<int64_t> outShape = {1, 16, 1, 1};

  std::vector<int64_t> kernelDims = {4, 4};
  std::vector<int64_t> strideDims = {1, 1};
  std::vector<int64_t> paddingDims = {0, 0};

  void* selfDeviceAddr = nullptr;
  void* outDeviceAddr = nullptr;

  aclTensor* self = nullptr;
  aclTensor* out = nullptr;

  std::vector<float> selfHostData(256, 2);
  std::vector<float> outHostData(16, 0);

  // Create the self aclTensor
  ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_FLOAT, &self);
  std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor *)> selfTensorPtr(self, aclDestroyTensor);
  std::unique_ptr<void, aclError (*)(void *)> selfDeviceAddrPtr(selfDeviceAddr, aclrtFree);
  CHECK_FREE_RET(ret == ACL_SUCCESS, return ret);

  // Create the kernel aclIntArray
  aclIntArray *kernelSize = aclCreateIntArray(kernelDims.data(), 2);
  std::unique_ptr<aclIntArray, aclnnStatus (*)(const aclIntArray *)> kernelSizePtr(kernelSize, aclDestroyIntArray);
  CHECK_FREE_RET(kernelSize != nullptr, return ACL_ERROR_INTERNAL_ERROR);

  // Create the stride aclIntArray
  aclIntArray *strides = aclCreateIntArray(strideDims.data(), 2);
  std::unique_ptr<aclIntArray, aclnnStatus (*)(const aclIntArray *)> stridesPtr(strides, aclDestroyIntArray);
  CHECK_FREE_RET(strides != nullptr, return ACL_ERROR_INTERNAL_ERROR);

  // Create the paddings aclIntArray
  aclIntArray *paddings = aclCreateIntArray(paddingDims.data(), 2);
  std::unique_ptr<aclIntArray, aclnnStatus (*)(const aclIntArray *)> paddingsPtr(paddings, aclDestroyIntArray);
  CHECK_FREE_RET(paddings != nullptr, return ACL_ERROR_INTERNAL_ERROR);

  // Create the out aclTensor.
  ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_FLOAT, &out);
  std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor *)> outTensorPtr(out, aclDestroyTensor);
  std::unique_ptr<void, aclError (*)(void *)> outDeviceAddrPtr(outDeviceAddr, aclrtFree);
  CHECK_FREE_RET(ret == ACL_SUCCESS, return ret);

  // 3. Call the CANN operator library API. Replace with the specific API name.
  uint64_t workspaceSize = 0;
  aclOpExecutor* executor;
  // Call the first-phase API of aclnnAvgPool2d.
  ret = aclnnAvgPool2dGetWorkspaceSize(self,
                                       kernelSize,
                                       strides,
                                       paddings,
                                       ceilMode,
                                       countIncludePad,
                                       divisorOverride,
                                       cubeMathType,
                                       out,
                                       &workspaceSize,
                                       &executor);
  CHECK_FREE_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnAvgPool2dGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
  // Allocate device memory based on the workspaceSize calculated by the first-phase API.
  void* workspaceAddr = nullptr;
  std::unique_ptr<void, aclError (*)(void *)> workspaceAddrPtr(nullptr, aclrtFree);
  if (workspaceSize > 0) {
    ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST);
    CHECK_FREE_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret);
    workspaceAddrPtr.reset(workspaceAddr);
  }
  // Call the second-phase API of aclnnAvgPool2d.
  ret = aclnnAvgPool2d(workspaceAddr, workspaceSize, executor, stream);
  CHECK_FREE_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnAvgPool2d failed. ERROR: %d\n", ret); return ret);

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

  // 5. Obtain the output value and copy the result from the device side to the host side. Modify it based on the specific API definition.
  auto size = GetShapeSize(outShape);
  std::vector<float> outData(size, 0);
  ret = aclrtMemcpy(outData.data(), outData.size() * sizeof(outData[0]), outDeviceAddr,
                    size * sizeof(outData[0]), ACL_MEMCPY_DEVICE_TO_HOST);
  CHECK_FREE_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]);
  }
  return ACL_SUCCESS;
}

int main() {
  // 1. (Boilerplate) Initialize device/stream. For details, see the acl API manual.
  // Fill in deviceId based on the actual device.
  int32_t deviceId = 0;
  aclrtStream stream;
  auto ret = aclnnAvgPool2dTest(deviceId, stream);
  CHECK_FREE_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnAvgPool2dTest failed. ERROR: %d\n", ret); return ret);

  Finalize(deviceId, stream);
  return 0;
}