aclnnUpsampleBicubic2dAA

📄 View source code

Supported Products

ProductSupported
Atlas A3 training products/Atlas A3 inference products
Atlas A2 training products/Atlas A2 inference products
Atlas 200I/500 A2 inference products×
Atlas inference products×
Atlas training products×

Function

  • API description: Applies the bicubic interpolation with anti-aliasing to upsample the input signal composed of several input channels. If the shape of the input tensor x is (N, C, H, W), then the shape of the output tensor out is (N, C, outputSize[0], outputSize[1]).

  • Formula: For a two-dimensional interpolation point (N,C,h,w)(N, C, h, w), the interpolation out(N,C,h,w)out(N, C, h, w) may be represented as:

    out(N,C,h,w)=i=0kWj=0kHW(i,j)f(hi,wj){out(N, C, h, w)}=\sum_{i=0}^{kW}\sum_{j=0}^{kH}{W(i, j)}*{f(h_i, w_j)} scaleH={(x.dim(2)1)/(outputSize[0]1)alignCorners=true1/scalesHalignCorners=false&scalesH>0x.dim(2)/outputSize[0]otherwisescaleH =\begin{cases} (x.dim(2)-1) / (outputSize[0]-1) & alignCorners=true \\ 1 / scalesH & alignCorners=false\&scalesH>0\\ x.dim(2) / outputSize[0] & otherwise \end{cases} scaleW={(x.dim(3)1)/(outputSize[1]1)alignCorners=true1/scalesWalignCorners=false&scalesW>0x.dim(3)/outputSize[1]otherwisescaleW =\begin{cases} (x.dim(3)-1) / (outputSize[1]-1) & alignCorners=true \\ 1 / scalesW & alignCorners=false\&scalesW>0\\ x.dim(3) / outputSize[1] & otherwise \end{cases}

    Where,

    • i and j are index variables of W(i,j)W(i, j).
    • If scaleH1scaleH ≥ 1, then kH=1/scaleHkH = 1/scaleH; otherwise, kH=4kH = 4.
    • If scaleW1scaleW ≥ 1, then kW=1/scaleWkW = 1/scaleW; otherwise, kW=4kW = 4.
    • hi=h+ih_i = |h| + i
    • wj=w+jw_j = |w| + j
    • f(hi,wj)f(h_i, w_j) is the pixel value of the original image in (hi,wj)(h_i, w_j).
    • W(i,j)W(i, j) is the weight of the bicubic anti-aliasing interpolation, which is defined as follows:W(d)={(a+2)d3(a+3)d2+1d1ad35ad2+8ad4a1<d<20otherwiseW(d) =\begin{cases} (a+2)|d|^3-(a+3)|d|^2+1 & |d|\leq1 \\ a|d|^3-5a|d|^2+8a|d|-4a & 1<|d|<2 \\ 0 & otherwise \end{cases} Where,
      • Anti-aliasing scenario: a=0.5a = –0.5.
      • d=(h,w)(hi,wj)d = |(h, w) - (h_i, w_j)|

Prototype

Each operator has two-phase API calls. First, aclnnUpsampleBicubic2dAAGetWorkspaceSize is called to obtain the workspace size required for computation and the executor that contains the operator computation process. Then, aclnnUpsampleBicubic2dAA is called to perform computation.

aclnnStatus aclnnUpsampleBicubic2dAAGetWorkspaceSize(
  const aclTensor*   x,
  const aclIntArray* outputSize,
  const bool         alignCorners,
  const double       scalesH,
  const double       scalesW,
  aclTensor*         out,
  uint64_t*          workspaceSize,
  aclOpExecutor**    executor)
aclnnStatus aclnnUpsampleBicubic2dAA(
  void          *workspace,
  uint64_t       workspaceSize,
  aclOpExecutor *executor,
  aclrtStream    stream)

aclnnUpsampleBicubic2dAAGetWorkspaceSize

  • Parameters:

    Name Input/Output Description Precaution Data Type Data Format Dimension (Shape) Non-contiguous Tensor
    x Input Input tensor for upsampling, corresponding to `x` in the formula.
    • Empty tensors are supported.
    • The data type must be the same as that of the output parameter `out`.
    • The ND format is processed as NCHW format by default.
    FLOAT16, FLOAT32, BFLOAT16 NCHW and ND 4
    outputSize Input Spatial size of `out` in the H and W dimensions. It corresponds to `outputSize` in the formula. The size is 2, and each element is greater than 0. INT64 - - -
    alignCorners Input Whether to align corner pixels, corresponding to `alignCorners` in the formula. If alignCorners is True, the corner pixels of the input and output tensors are aligned. Otherwise, the corner pixels are not aligned. BOOL - - -
    scalesH Input Multiplier for spatial size in the height dimension, corresponding to `scalesH` in the formula. - DOUBLE - - -
    scalesW Input Multiplier for spatial size in the width dimension, corresponding to `scalesW` in the formula. - DOUBLE - - -
    out Output Output tensor after sampling, corresponding to `out` in the formula.
    • Empty tensors are supported.
    • The data type is the same as that of the input parameter `x`.
    FLOAT16, FLOAT32, BFLOAT16 NCHW and ND 4
    workspaceSize Output Size of the workspace required to be allocated on the device. - - - - -
    executor Output Operator executor, containing the operator computation process. - - - - -
  • Returns:

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

    The first-phase API implements input parameter verification. The following errors may be thrown.

    Return Error Code Description
    ACLNN_ERR_PARAM_NULLPTR 161001 The passed x, outputSize, or out is a null pointer.
    ACLNN_ERR_PARAM_INVALID 161002 The data type of x or out is not supported.
    The data types of x and out are inconsistent.
    The shape of x is not 4D.

aclnnUpsampleBicubic2dAA

  • Parameters:

    Name Input/Output Description
    workspace Input Address of the workspace to be allocated on the device.
    workspaceSize Input Size of the workspace to be allocated on the device, which is obtained by calling the first-phase API aclnnUpsampleBicubic2dAAGetWorkspaceSize.
    executor Input Operator executor, containing the operator computation process.
    stream Input Stream for executing the task.
  • Returns:

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

Constraints

  • The shape constraints of x and out are as follows:
    • The value of each dimension is less than or equal to 2^20.

    • The N and C axes of out must be the same as those of self. The C, H, and W axes must be greater than 0.

    • The memory usage must be less than 60 GB. The memory size can be calculated according to the following formula:

      (x_Hx_W+out_Hout_W+x_Hout_W)NCsizeof(float)<60102410241024(x\_H * x\_W + out\_H * out\_W + x\_H * out\_W) * N * C * sizeof(float) < 60 * 1024 * 1024 * 1024

      Where,

      • N indicates the N axis of the input and output.
      • C indicates the C axis of the input and output.
    • N * C * x_H < 2^31

  • The downscaling factor for input data must be less than or equal to 50. That is, the "height H of input shape/outputSize[0]" and "width W of input shape/outputSize[1]" must be less than or equal to 50.
  • The number of elements contained in outputSize must be 2, and the H and W axes of outputSize must be the same as those of the output shape.
  • Either the H and W axes of the outputSize parameter or the scalesH and scalesW parameters can be used.
    • When alignCorners is set to True:
      • If the value of the corresponding axis of outputSize is less than or equal to 1, the value of the corresponding axis of scales is 0.
      • In other cases, the values of the corresponding axes in x and outputSize are used, and scales=(x1)/(outputSize1)scales = (x – 1)/(outputSize – 1).
    • If alignCorners is set to False:
      • If the value of scalesH or scalesW is less than or equal to 0, the value of the corresponding axis in outputSize is used.
        • If the value of the corresponding axis of outputSize is 0, the corresponding value of scales is 0.
        • If the value of the corresponding axis of outputSize is not 0, scales=(x/outputSize)scales = (x/outputSize).
      • If the value of scalesH or scalesW is greater than 0, the value of scalesH or scalesW is used. That is, the value of the corresponding axis of outputSize is floor(x_HscalesH)floor(x\_H * scalesH) or floor(x_WscalesW)floor(x\_W * scalesW).
  • Deterministic computing:
    • aclnnUpsampleBicubic2dAA defaults to a deterministic implementation.

Example

The following example is for reference only. For details, see Compilation and Running Sample.

#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_upsample_bicubic2d_aa.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) {
  // (Fixed writing) Initialize resources.
  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 the data on the host to the memory on the device.
  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 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. (Fixed writing) Initialize the device and stream. For details, see the ACL API manual.
  // Set the device ID in use.
  int32_t deviceId = 0;
  aclrtStream stream;
  auto ret = Init(deviceId, &stream);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Init failed. ERROR: %d\n", ret); return ret);

  // 2. Construct the input and output based on the API.
  std::vector<int64_t> selfShape = {1, 1, 3, 3};
  std::vector<int64_t> outShape = {1, 1, 5, 5};
  void* selfDeviceAddr = nullptr;
  void* outDeviceAddr = nullptr;
  aclTensor* x = nullptr;
  aclTensor* out = nullptr;
  std::vector<float> selfHostData = {1, 2, 3, 4, 5, 6, 7, 8, 9};
  std::vector<float> outHostData = {25, 0};
  std::vector<int64_t> outputSizeData = {5, 5};
  bool alignCorners = false;
  double scalesH = 0.0;
  double scalesW = 0.0;
  // Create a self aclTensor.
  ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_FLOAT, &x);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  // Create an out aclTensor.
  ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_FLOAT, &out);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  // Create an outputSize aclIntArray.
  const aclIntArray *outputSize = aclCreateIntArray(outputSizeData.data(), outputSizeData.size());
  CHECK_RET(outputSize != nullptr, return ret);
  
  // 3. Call the CANN operator library API, which needs to be replaced with the actual API.
  uint64_t workspaceSize = 0;
  aclOpExecutor* executor;
  // Call the first-phase API of aclnnUpsampleBicubic2dAA.
  ret = aclnnUpsampleBicubic2dAAGetWorkspaceSize(x, outputSize, alignCorners, scalesH, scalesW, out, &workspaceSize, &executor);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnUpsampleBicubic2dAAGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
  // Allocate device memory based on the computed workspaceSize.
  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 aclnnUpsampleBicubic2dAA.
  ret = aclnnUpsampleBicubic2dAA(workspaceAddr, workspaceSize, executor, stream);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnUpsampleBicubic2dAA failed. ERROR: %d\n", ret); return ret);

  // 4. (Fixed writing) Wait until the task execution is 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 the device memory to the host. Modify the configuration based on the 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 aclIntArray. Modify the configuration based on the API definition.
  aclDestroyTensor(x);
  aclDestroyIntArray(outputSize);
  aclDestroyTensor(out);

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