aclnnIou
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 : Not supported
Function
- Description: For two sets of input rectangle boxes, compute the Intersection over Union (IoU) or Intersection over Foreground (IoF) to evaluate the overlap between the predicted box (bBox) and the ground-truth box (gtBox).
- Formula: Where Area_1 is the area of bBox, Area_2 is the area of gtBox, and Area_3 is the area of their overlapping region. For definitions of x and y, see the Parameters.
Function Prototype
Each operator uses a two-phase API. You must first call the "aclnnIouGetWorkspaceSize" API to obtain the workspace size required for the computation and the executor that contains the computation process of the operator. Then, call the "aclnnIou" API to perform the computation.
aclnnStatus aclnnIouGetWorkspaceSize(
const aclTensor* bBoxes,
const aclTensor* gtBoxes,
const char* mode,
float eps,
bool aligned,
aclTensor* overlap,
uint64_t* workspaceSize,
aclOpExecutor** executor)aclnnStatus aclnnIou(
void* workspace,
uint64_t workspaceSize,
aclOpExecutor* executor,
aclrtStream stream)aclnnIouGetWorkspaceSize
Parameters
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor bBoxes (aclTensor*) Input Predicted bounding boxes. - m in shape indicates the number of bounding boxes.
- 4 in shape indicates [x0, y0, x1, y1], where (x0, y0) and (x1, y1) indicate the upper-left corner and lower-right corner of a bounding box, respectively, and x1 > x0, y1 > y0 must be met.
FLOAT, FLOAT16, BFLOAT16 ND (m, 4) √ gtBoxes (aclTensor*) Input Ground truth bounding boxes. - n is the number of bounding boxes.
- 4 indicates [x2, y2, x3, y3], where (x2, y2) and (x3, y3) are the upper-left corner and lower-right corner of the rectangle, respectively. x3 > x2 and y3 > y2 must be met.
- The data type must be the same as that of bBoxes.
FLOAT, FLOAT16, BFLOAT16 ND (n, 4). √ mode (char*) Input Used to select the calculation mode "iou" or "iof". - "iou": calculates the intersection over union.
- "iof": calculates the foreground intersection ratio.
String - - - eps (float) Input Prevents division by zero. eps is added to both the length and the width when calculating the area. - FLOAT - - - aligned (bool) Input Identifies whether the two inputs have the same shape. - True: The shapes of bBoxes and gtBoxes are the same, both (m, 4). The output shape is (m, 1).
- False: The shapes of bBoxes and gtBoxes are different, (m, 4) and (n, 4) respectively. The output shape is (m, n).
BOOL - - - overlap (aclTensor*) Output IOU/IOF calculated based on the two inputs. The data type must be the same as that of bBoxes. FLOAT, FLOAT16, BFLOAT16 ND (m, n) or (m, 1) √ workspaceSize (uint64_t*) Output Returns the size of the workspace to be allocated on the Device side. - - - - - executor (aclOpExecutor**) Output Returns the op executor, including the operator computation process. - - - - - Atlas inference products : The data type BFLOAT16 is not supported.
Return Value
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 Value Error Code Description ACLNN_ERR_PARAM_NULLPTR 161001 The input bBoxes, gtBoxes, and output overlap are null pointers. ACLNN_ERR_PARAM_INVALID 161002 bBoxes, gtBoxes, or overlap is not two-dimensional. The data types of bBoxes, gtBoxes, and overlap are inconsistent. The data types and data formats of bBoxes, gtBoxes, and overlap are not within the supported range. The second dimension of bBoxes or gtBoxes is not 4. If aligned is true, the first dimensions of bBoxes and gtBoxes are different. If aligned is true, the second dimension of overlap is not 1. mode is not "iou" or "iof". eps is less than 0. ACLNN_ERR_INNER_NULLPTR 561103 Internal API verification error, usually caused by input data or attribute specifications out of the supported range. ACLNN_ERR_RUNTIME_ERROR 361001 An exception occurs when the API calls the NPU runtime API, for example, an unsupported SocVersion.
aclnnIou
Parameters
Parameter Input/Output Description workspace Input Workspace memory address allocated on the Device side. workspaceSize Input Workspace size allocated on the Device side, obtained by the first-phase API aclnnIouGetWorkspaceSize. executor Input Op executor, containing the operator computation flow. stream Input Stream for executing the task. Return Value
aclnnStatus: return status code. For details, see aclnn Return Codes.
Constraints
- Deterministic computation:
- aclnnIou defaults to a deterministic implementation.
Example
The sample code is as follows and 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/level2/aclnn_iou.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 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 memory on the device side.
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 data from the host side to the 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);
// 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 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. (Boilerplate) Initialize the device/stream. For details, see the acl API manual.
// Set 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 input and output. Customize the construction based on the API.
// input
std::vector<float> bBoxesHostData = {1.0, 1.0, 5.0, 3.0, 1.0, 1.0, 5.0, 3.0};
std::vector<float> gtBoxesHostData = {4.0, 2.0, 9.0, 5.0, 4.0, 2.0, 9.0, 5.0};
std::vector<float> overlapHostData = {0.045455, 0.045455};
std::vector<int64_t> bBoxesShape = {2, 4};
std::vector<int64_t> gtBoxesShape = {2, 4};
std::vector<int64_t> overlapShape = {2, 1};
void* bBoxesDeviceAddr = nullptr;
void* gtBoxesDeviceAddr = nullptr;
void* overlapDeviceAddr = nullptr;
aclTensor* bBoxes = nullptr;
aclTensor* gtBoxes = nullptr;
aclTensor* overlap = nullptr;
ret = CreateAclTensor(bBoxesHostData, bBoxesShape, &bBoxesDeviceAddr, aclDataType::ACL_FLOAT, &bBoxes);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(gtBoxesHostData, gtBoxesShape, >BoxesDeviceAddr, aclDataType::ACL_FLOAT, >Boxes);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(overlapHostData, overlapShape, &overlapDeviceAddr, aclDataType::ACL_FLOAT, &overlap);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// attr
const char* mode = "iou";
float eps = 0.0f;
bool aligned = true;
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// 3. Call the CANN operator library API. Change it to the specific API name.
ret = aclnnIouGetWorkspaceSize(bBoxes, gtBoxes, mode, eps, aligned, overlap, &workspaceSize, &executor);
CHECK_RET(
ret == ACL_SUCCESS,
LOG_PRINT("aclnnIouGetWorkspaceSize 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);
}
// aclnnIou
ret = aclnnIou(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS,
LOG_PRINT("aclnnIou 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, copy the result on the Device memory to the host side, and modify the code according to the specific API definition.
auto size = GetShapeSize(overlapShape);
std::vector<float> resultData(size, 0);
ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), overlapDeviceAddr,
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 aclTensor and aclScalar, and modify the code according to the specific API definition.
aclDestroyTensor(bBoxes);
aclDestroyTensor(gtBoxes);
aclDestroyTensor(overlap);
// 7. Release Device resources.
aclrtFree(bBoxesDeviceAddr);
aclrtFree(gtBoxesDeviceAddr);
aclrtFree(overlapDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}