aclnnGridSampler2D

📄 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: Given an input tensor and a grid, computes the output using input values and pixel locations from grid.

  • Formula:

    The dimensions of input, grid, and output are as follows:

    input:(N,C,Hin,Win)grid:(N,Hout,Wout,2)output:(N,C,Hout,Wout)input: (N, C, H_{in}, W_{in})\\ grid: (N, H_{out}, W_{out}, 2)\\ output: (N, C, H_{out}, W_{out})

    input, grid, and output have the same N. C in input is the same as that in output. HoutH_{out} and WoutW_{out} in grid and output are the same. The size of the last dimension of grid is 2, indicating that the input pixel location is (x, y). The value ranges of x and y are normalized to [-1,1]. (-1, 1) indicates the coordinates of the upper left corner, and (1,1) indicates the coordinates of the lower right corner.

    • Out-of-bound coordinates are processed based on the padding mode.

      • paddingMode=0: Use 0 for out-of-bound locations.
      • paddingMode=1: Use border values for out-of-bound locations.
      • paddingMode=2: Use symmetric values of the border values for out-of-bound locations.
    • During input sampling, the processing varies according to interpolationMode.

      • interpolationMode=0: Use the weighted average value of the 4 coordinates around (x, y).
      • interpolationMode=1: Use the nearest coordinates to (x, y) in the input.
      • interpolationMode=2: Use the weighted average value of the 16 coordinates around (x, y).

Prototype

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

aclnnStatus aclnnGridSampler2DGetWorkspaceSize(
  const aclTensor *input,
  const aclTensor *grid,
  int64_t          interpolationMode,
  int64_t          paddingMode,
  bool             alignCorners,
  aclTensor       *out,
  uint64_t        *workspaceSize,
  aclOpExecutor  **executor)
aclnnStatus aclnnGridSampler2D(
  void          *workspace,
  uint64_t       workspaceSize,
  aclOpExecutor *executor,
  aclrtStream    stream)

aclnnGridSampler2DGetWorkspaceSize

  • Parameters:

    Name Input/Output Description Precaution Data Type Data Format Dimension (Shape) Non-contiguous Tensor
    input Input Input tensor for interpolation calculation, corresponding to `input` in the formula.
    • Empty tensors are supported.
    • The shape can be (N, C, Hin, Win). H × W is less than the maximum value of INT32. The last two dimensions of the `input` shape cannot be 0.
    FLOAT32, FLOAT16, DOUBLE ND 4
    grid Input Sampling network, corresponding to `grid` in the formula.
    • Empty tensors are supported.
    • The data type is the same as that of the input parameter `input`.
    • The shape can be (N, Hout, Wout, 2). N is the same as that of the shape in the input parameter `input`.
    FLOAT32, FLOAT16, DOUBLE ND 4
    interpolationMode Input Interpolation mode, corresponding to `interpolationMode` in the formula description. The value can be 0 (bilinear), 1 (nearest), or 2 (bicubic). INT64 - - -
    paddingMode Input Padding mode. When the values of (x, y) exceed the sampling range of the input feature map, a specific value is returned. This corresponds to `paddingMode` in the formula description. The value can be 0 (zeros), 1 (border), or 2 (reflection). INT64 - - -
    alignCorners Input Alignment mode between the feature map coordinates and feature values. If this parameter is set to true, the feature values are located in the pixel center. If this parameter is set to false, the feature values are located at the corner points of the corner pixels. If true, the feature values are located in the pixel center. If false, the feature values are located at the corner points of the corner pixels. BOOL - - -
    out Output Final output of the interpolation calculation, corresponding to `output` in the formula.
    • Empty tensors are supported.
    • The data type is the same as that of input.
    • The shape can be (N, C, Hout, Wout). N and C must be the same as those in the shape of input. Hout and Wout must be the same as those in the shape of grid.
    FLOAT32, FLOAT16, DOUBLE ND 4
    workspaceSize Output Size of the workspace required to be allocated on the device. - - - - -
    executor Output Operator executor, containing the operator computation process. - - - - -
    • Atlas training products:

      The input parameter interpolationMode does not support interpolation mode 2 (bicubic interpolation).

    • Atlas A2 training products/Atlas A2 inference products and Atlas A3 training products/Atlas A3 inference products:

      The input parameter interpolationMode supports interpolation mode 2 (bicubic interpolation) only when the input data type is FLOAT32 or FLOAT16.

  • 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 input, grid, or out is a null pointer.
    ACLNN_ERR_PARAM_INVALID 161002 The data type of input, grid, or out is not supported or their data types are different.
    The value of interpolationMode or paddingMode is not supported.
    When interpolationMode is set to bicubic, the data type of input, grid, or out is not FLOAT32 or FLOAT16.
    The dimensions of input, grid, and out do not match.
    The last two dimensions of input are empty.

aclnnGridSampler2D

  • 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 aclnnGridSampler2DGetWorkspaceSize.
    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 data formats of input, grid, and out can only be (N, C, H, W). If other data formats are input, the (N, C, H, W) format is used by default.
  • The (H axis × W axis) of the input is less than the maximum value of INT32.
  • If the input value of grid multiplied by the image length or width is greater than a 24-bit binary number (16777216), the sampling point and the precision may be inaccurate.
  • If grid contains a large amount of data beyond the range of [-1,1], lots of duplicate values will occur in the calculation result when zeros or border padding is used.
  • Atlas A2 training products/Atlas A2 inference products and Atlas A3 training products/Atlas A3 inference products:
    • If grid contains data beyond the range of [-1, 1], when bicubic interpolation is used, errors may occur in the calculation of data in a small value range, and the precision may be inaccurate.
    • When bilinear or bicubic interpolation is used, the workspace memory is required for the FLOAT16 data type.
  • Atlas training products: When bilinear interpolation is used, the workspace memory is required for the FLOAT16 data type.
  • Deterministic computing:
    • aclnnGridSampler2D 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_grid_sampler2d.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 acl failed. ERROR: %d\n", ret); return ret);

    // 2. Construct the input and output based on the API.
    int64_t interpolationMode = 0;
    int64_t paddingMode = 0;
    bool alignCorners = false;
    std::vector<int64_t> inputShape = {1, 1, 5, 8};
    std::vector<int64_t> gridShape = {1, 3, 3, 2};
    std::vector<int64_t> outShape = {1, 1, 3, 3};
    void* inputDeviceAddr = nullptr;
    void* gridDeviceAddr = nullptr;
    void* outDeviceAddr = nullptr;
    aclTensor* input = nullptr;
    aclTensor* grid = nullptr;
    aclTensor* out = nullptr;

    std::vector<float> inputHostData = {1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
                                        21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40};
    std::vector<float> gridHostData = {-1, -1, 0, -1, 1, -1, -1, 0, 0, 0, 1, 0, -1, 1, 0, 1, 1, 1};
    std::vector<float> outHostData = {0, 0, 0, 0, 0, 0, 0, 0, 0};

    // Create an input aclTensor.
    ret = CreateAclTensor(inputHostData, inputShape, &inputDeviceAddr, aclDataType::ACL_FLOAT, &input);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create a grid aclTensor.
    ret = CreateAclTensor(gridHostData, gridShape, &gridDeviceAddr, aclDataType::ACL_FLOAT, &grid);
    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);

    // 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 aclnnGridSampler2D.
    ret = aclnnGridSampler2DGetWorkspaceSize(
        input, grid, interpolationMode, paddingMode, alignCorners, out, &workspaceSize, &executor);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnGridSampler2DGetWorkspaceSize 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 aclnnGridSampler2D.
    ret = aclnnGridSampler2D(workspaceAddr, workspaceSize, executor, stream);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnGridSampler2D 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 resultData from device to host failed. ERROR: %d\n", ret);
              return ret);
    for (int64_t i = 0; i < size; i++) {
        LOG_PRINT("resultData[%ld] is: %f\n", i, resultData[i]);
    }

    // 6. Release aclTensor. Modify the configuration based on the API definition.
    aclDestroyTensor(input);
    aclDestroyTensor(grid);
    aclDestroyTensor(out);

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

    return 0;
}