aclnnRoiAlignV2
Applicable Products
Ascend 950PR&950DT products : Not 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
RoiAlign is a pooling layer used for feature maps with non-uniform input sizes, and outputs feature maps of fixed size. aclnnRoiAlign aligns with the ONNX opset 10 operator prototype, while aclnnRoiAlignV2 aligns with the torchvision operator prototype. aclnnRoiAlignV2 uses boxes to replace the rois and batch_indices of aclnnRoiAlign, adds the aligned input parameter, and removes the mode input parameter, defaulting to the mode="avg" scenario.
Function Prototype
Each operator uses a two-phase API. You must first call the "aclnnRoiAlignV2GetWorkspaceSize" API to obtain the workspace size required for computation and the executor containing the computation process of the operator, and then call the "aclnnRoiAlignV2" API to perform the computation.
aclnnStatus aclnnRoiAlignV2GetWorkspaceSize(
const aclTensor* self,
const aclTensor* boxes,
int64_t pooledHeight,
int64_t pooledWidth,
float spatialScale,
int64_t samplingRatio,
bool aligned,
aclTensor* out,
uint64_t* workspaceSize,
aclOpExecutor** executor)aclnnStatus aclnnRoiAlignV2(
void* workspace,
uint64_t workspaceSize,
aclOpExecutor* executor,
aclrtStream stream)aclnnRoiAlignV2GetWorkspaceSize
Parameters
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor self (aclTensor*) Input Input image feature maps. The data type must be consistent with that of boxes and out. FLOAT, FLOAT16 NCHW 4D, in (B, C, inputHeight, inputWidth)
Indicates that the input tensor contains B images in a batch, and each image contains C feature maps of the size inputHeight * inputWidth.
A value of 0 is not supported for B, inputHeight, or inputWidth.β boxes (aclTensor*) Input Coordinates of the region-of-interest (RoI) boxes. Must be consistent with the data types of self and out. FLOAT, FLOAT16 ND 2D, (K, 5)
5 indicates box information (image_id, x1, y1, x2, y2)
K must be consistent with the 0th dimension of out.
image_id is in the range [0, B), rounded down to obtain the image ID. B is the size of the 0th dimension of self. The coordinates satisfy 0 <= x1 <= x2 <= inputWidth/spatialScale and 0 <= y1 <= y2 <= inputHeight/spatialScale.β pooledHeight (int64_t) Input Height of the output image after pooling. - INT64 - - - pooledWidth (int64_t) Input Width of the output image after pooling. - INT64 - - - spatialScale (float) Input Multiplicative spatial scale factor. Converts ROI coordinates from their input spatial scale to the scale used for pooling, that is, the spatial scale of input feature map X relative to the input image. Must be greater than 0. FLOAT32 - - - samplingRatio (int64_t) Input Used to compute the sampling frequency of each output element in the H and W directions. Must be greater than or equal to 0. INT64 - - - aligned (bool) Input If false, the implementation is consistent with the aclnnRoiAlign version.
If true, the box coordinates are offset by -0.5 pixel to better align the indexes of adjacent pixels.- BOOL - - - out (aclTensor*) Output Pooled output. Must be consistent with the data types of self and boxes. FLOAT, FLOAT16 NCHW 4D, in the format of (K, C, pooledHeight, pooledWidth)
Indicates that the output tensor contains K elements per batch, and each element contains C feature maps of the size pooledHeight * pooledWidth.β workspaceSize (uint64_t*) Output Returns the workspace size to be allocated on the device. - - - - - executor (aclOpExecutor**) Output Returns the op executor, which includes the operator computation process. - - - - - Error Codes:
aclnnStatus: return status code. For details, see aclnn Return Codes.
The first-phase API performs input parameter validation and returns an error in the following scenarios:
Return Values Error Code Description ACLNN_ERR_PARAM_NULLPTR 161001 self, boxes, and out are null pointers. ACLNN_ERR_PARAM_INVALID 161002 The data types and data formats of self, boxes, and out are out of the supported range. The shapes of self, boxes, and out do not meet the constraints. spatialScale must be greater than 0, and samplingRatio must be greater than or equal to 0.
aclnnRoiAlignV2
Parameters
Parameter Input/Output Description workspace Input Workspace memory address allocated on the device. workspaceSize Input Workspace size allocated on the device, obtained by the first-phase API aclnnRoiAlignV2GetWorkspaceSize. executor Input Operator executor containing the operator computation flow. stream Input Stream for task execution. Error Codes:
aclnnStatus: return status code. For details, see aclnn Return Codes.
Constraints
- Deterministic computation:
- aclnnRoiAlignV2 defaults to a deterministic implementation.
Example
The following sample code is for reference only. For details about compilation and execution, see Compile and Run Samples.
#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_roi_align_v2.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 shape_size = 1;
for (auto i : shape) {
shape_size *= i;
}
return shape_size;
}
int Init(int32_t deviceId, aclrtStream* stream) {
// Boilerplate. 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 host data to the 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);
// Calculate 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, aclFormat::ACL_FORMAT_ND,
shape.data(), shape.size(), *deviceAddr);
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 host data to the 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);
// Calculate 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, aclFormat::ACL_FORMAT_NCHW,
shape.data(), shape.size(), *deviceAddr);
return 0;
}
int main() {
// 1. (Boilerplate) Initialize the 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 = Init(deviceId, &stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
// 2. Construct the inputs and outputs. Customize the construction based on the API.
std::vector<int64_t> selfShape = {1, 1, 6, 6};
std::vector<int64_t> boxesShape = {1, 5};
std::vector<int64_t> outShape = {1, 1, 3, 3};
void* selfDeviceAddr = nullptr;
void* boxesDeviceAddr = nullptr;
void* outDeviceAddr = nullptr;
aclTensor* self = nullptr;
aclTensor* boxes = nullptr;
aclTensor* out = nullptr;
std::vector<float> selfHostData = {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};
std::vector<float> boxesHostData = {0.0, -2.0, -2.0, 22.0, 22.0};
std::vector<float> outHostData = {4.5, 6.5, 8.5, 16.5, 18.5, 20.5, 28.5, 30.5, 32.5};
// Create the self aclTensor.
ret = CreateAclNchTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_FLOAT, &self);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the boxes aclTensor.
ret = CreateAclTensor(boxesHostData, boxesShape, &boxesDeviceAddr, aclDataType::ACL_FLOAT, &boxes);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the out aclTensor.
ret = CreateAclNchTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_FLOAT, &out);
CHECK_RET(ret == ACL_SUCCESS, return ret);
int64_t pooledHeight = 3;
int64_t pooledWidth = 3;
int64_t samplingRatio = 2;
float spatialScale = 0.25f;
bool aligned = false;
// 3. Call the CANN operator library API.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Call the first-phase API of aclnnRoiAlignV2.
ret = aclnnRoiAlignV2GetWorkspaceSize(self, boxes, pooledHeight, pooledWidth, spatialScale,
samplingRatio, aligned, out, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnRoiAlignV2GetWorkspaceSize 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 aclnnRoiAlignV2.
ret = aclnnRoiAlignV2(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnRoiAlignV2 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 and copy the result from device memory to host memory. Modify the code based on the specific 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("result[%ld] is: %f\n", i, resultData[i]);
}
// 6. Release the aclTensor. Modify the code based on the specific API definition.
aclDestroyTensor(self);
aclDestroyTensor(boxes);
aclDestroyTensor(out);
// 7. Release device resources. Modify the code based on the specific API definition.
aclrtFree(selfDeviceAddr);
aclrtFree(boxesDeviceAddr);
aclrtFree(outDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}