aclnnUpsampleBilinear2d

📄 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 2D bilinear upsampling to an input signal composed of several input channels.

    • For input shape: If the input shape is (N, C, H, W), then the output shape is (N, C, outputSize[0], outputSize[1]).
    • For center alignment selection: Pixels are generally regarded as a grid of points. When alignCorners is set to True, the pixel is regarded as the point in the upper left corner of the grid. The corner pixel of the output is center-aligned with the corner pixel of the original image, and the points in the same direction are equally spaced. When alignCorners is set to False, pixels are regarded as points on the cross lines of the grid. The corner pixel of the output is still the corner pixel of the original image, but the points in the same direction are not equally spaced.
  • Formula:

    • Core algorithm logic:
      1. Scale the destination image to the same size as the source image.
      2. Calculate points of the scaled destination image and points of adjacent source images.
      3. Calculate the weights from the adjacent points to the target points, and multiply and accumulate the weights to obtain the target point values.
    • Calculation logic: An image can be scaled by corner alignment (that is, based on the center point of the pixels in the upper left corner of the source image) or edge alignment (that is, based on the vertex in the upper left corner and two edges of the source image). The two modes differ in the scaling factor and coordinates. Then:scaleH={(self.dim[2]1)/(outputSize[0]1)alignCorners=true1/scalesHalignCorners=false&scalesH>0self.dim[2]/outputSize[0]alignCorners=falsescaleH =\begin{cases} (self.dim[2]-1) / (outputSize[0]-1) & alignCorners=true \\ 1 / scalesH & alignCorners=false\&scalesH>0\\ self.dim[2] / outputSize[0] & alignCorners=false \end{cases} scaleW={(self.dim[3]1)/(outputSize[1]1)alignCorners=true1/scalesWalignCorners=false&scalesW>0self.dim[3]/outputSize[1]alignCorners=falsescaleW =\begin{cases} (self.dim[3]-1) / (outputSize[1]-1) & alignCorners=true \\ 1 / scalesW & alignCorners=false\&scalesW>0\\ self.dim[3] / outputSize[1] & alignCorners=false \end{cases} Therefore, for a point p (x, y) in a direction of the output, a point mapped back to the source image is denoted as q (x', y'). Then:x={xscaleHalignCorners=trueMAX(0,(x+0.5)scaleH0.5)alignCorners=falsex' =\begin{cases} x * scaleH & alignCorners=true \\ MAX(0,{(x+0.5)*scaleH-0.5}) & alignCorners=false \end{cases} y={yscaleWalignCorners=trueMAX(0,(y+0.5)scaleW0.5)alignCorners=falsey' =\begin{cases} y * scaleW & alignCorners=true \\ MAX(0,{(y+0.5)*scaleW-0.5}) & alignCorners=false \end{cases}
      • Denoted:

        x0=int(x),x1=int(x)+1,lambda0=x1x,lambda1=1lambda0x_{0} =int(x'),x_{1} =int(x')+1, lambda_{0} = x_{1}-x', lambda_{1} = 1-lambda_{0} y0=int(y),y1=int(y)+1,lambdb0=y1y,lambdb1=1lambdb0y_{0} =int(y'),y_{1} =int(y')+1, lambdb_{0} = y_{1}-y', lambdb_{1} = 1-lambdb_{0}
      • Then:

        V(px,y)=V(px0,y0)lambda0lambdb0+V(px0,y1)lambda0lambdb1+V(px1,y0)lambda1lambdb0+V(px1,y1)lambda1lambdb1{V(p_{x, y})} = {V(p_{x0, y0})} * {lambda_{0}} * {lambdb_{0}} + {V(p_{x0, y1})} * {lambda_{0}} * {lambdb_{1}} + {V(p_{x1, y0})} * {lambda_{1}} * {lambdb_{0}} + {V(p_{x1, y1})} * {lambda_{1}} * {lambdb_{1}}

Prototype

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

aclnnStatus aclnnUpsampleBilinear2dGetWorkspaceSize(
  const aclTensor   *self,
  const aclIntArray *outputSize,
  const bool         alignCorners,
  const double       scalesH,
  const double       scalesW,
  aclTensor         *out,
  uint64_t          *workspaceSize,
  aclOpExecutor    **executor)
aclnnStatus aclnnUpsampleBilinear2d(
  void          *workspace,
  uint64_t       workspaceSize,
  aclOpExecutor *executor,
  aclrtStream    stream)

aclnnUpsampleBilinear2dGetWorkspaceSize

  • Parameters:

    Name Input/Output Description Precaution Data Type Data Format Dimension (Shape) Non-contiguous Tensor
    self Input Input tensor for upsampling, corresponding to `self` in the formula.
    • Empty tensors are not supported.
    • When the data type is DOUBLE, only the NHWC format is supported.
    FLOAT32, BFLOAT16, FLOAT16, DOUBLE NCHW and NHWC 4
    outputSize Input Spatial size of the output, that is, the spatial size of `out` in the H and W dimensions. It corresponds to `outputSize` in the formula. The size is 2. INT64 - - -
    alignCorners Input Whether to align corner pixels, corresponding to `alignCorners` in the formula. If True, the input and output tensors are aligned by the center points of their corner pixels, preserving the values at the corner pixels. If False, the input and output tensors are aligned by the corner points of their corner pixels, and the interpolation uses edge value padding for out-of-boundary values. BOOL - - -
    scalesH Input Multiplier for spatial size in the height dimension, corresponding to `scalesH` in the formula. The value cannot be negative. DOUBLE - - -
    scalesW Input Multiplier for spatial size in the width dimension, corresponding to `scalesW` in the formula. The value cannot be negative. DOUBLE - - -
    out Output Output tensor after sampling, corresponding to `out` in the formula.
    • Empty tensors are not supported.
    • The data type and format must be the same as those of `self`.
    • When its data type is DOUBLE, only the NHWC format is supported.
    • The shape can only be 4D, and the N and C axes must be the same as those of the input self shape. If the value of outputSize is valid, the values of the H and W axes are the same as those of the corresponding axes of outputSize. If the value of outputSize is invalid (for details, see Constraints), the values of the H and W axes are the same as those of the corresponding axes of outputSize after calculation.
    FLOAT32, BFLOAT16, FLOAT16, DOUBLE NCHW and NHWC 4
    workspaceSize Output Size of the workspace required to be allocated on the device. - - - - -
    executor Output Operator executor, containing the operator computation process. - - - - -
    • Atlas inference products and Atlas training products:

      The data types of self and out do not support BFLOAT16.

  • 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 Internal API verification error, usually caused by unsupported input data or attribute specifications.
    ACLNN_ERR_PARAM_INVALID 161002 The data type of self is not supported.
    The dimension sizes of the N or C axes of self and out are different.
    The data format of self or out is not supported.
    The value of scalesH or scalesW is negative.

aclnnUpsampleBilinear2d

  • 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 aclnnUpsampleBilinear2dGetWorkspaceSize.
    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

  • If alignCorners is set to True, either outputSize or scalesH/scalesW must be used.
    • 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 self and outputSize are used, and scales=(self1)/(outputSize1)scales = (self – 1)/(outputSize – 1).
  • If alignCorners is set to False:
    • If the value of scalesH or scalesW is equal to 0, the value of 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=(self/outputSize)scales = (self/outputSize).
    • If the values of both scalesH and scalesW are greater than 0, the values of scalesH, scalesW, and outputSize are used.
  • Deterministic computing:
    • aclnnUpsampleBilinear2d 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_bilinear_2d.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 CreateAclNchTensor(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_NCHW,
        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 acl failed. ERROR: %d\n", ret); return ret);
    // 2. Construct the input and output based on the API.
    std::vector<int64_t> selfShape = {1, 1, 4, 2};
    std::vector<int64_t> outShape = {1, 1, 8, 4};
    void *selfDeviceAddr = nullptr;
    void *outDeviceAddr = nullptr;
    aclTensor *self = nullptr;
    aclTensor *out = nullptr;
    std::vector<float> selfHostData = {0, 1, 2, 3, 4, 5, 6, 7};
    std::vector<float> outHostData(32, 0);
    std::vector<int64_t> outputSize = {8, 4};
    bool alignCorners = true;
    double scalesH = 0.5;
    double scalesW = 0.5;
    // Create a self aclTensor.
    ret = CreateAclNchTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_FLOAT, &self);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create a self aclIntArray.
    auto outputSizeArray = aclCreateIntArray(outputSize.data(), 2);
    // Create an out aclTensor.
    ret = CreateAclNchTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_FLOAT, &out);
    CHECK_RET(ret == ACL_SUCCESS, 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 aclnnUpsampleBilinear2d.
    ret = aclnnUpsampleBilinear2dGetWorkspaceSize(
        self, outputSizeArray, alignCorners, scalesH, scalesW, out, &workspaceSize, &executor);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnUpsampleBilinear2dGetWorkspaceSize 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 aclnnUpsampleBilinear2d.
    ret = aclnnUpsampleBilinear2d(workspaceAddr, workspaceSize, executor, stream);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnUpsampleBilinear2d 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.
    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. Modify the configuration based on the API definition.
    aclDestroyTensor(self);
    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;
}