aclnnDeformableConv2d
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 : Not supported
Atlas training products : Not supported
Function
Description: Implements the convolution function, supporting 2D convolution, as well as deformable convolution and grouped convolution.
Formula:
Assume that the shape of input is [N, inC, inH, inW] and the shape of out is [N, outC, outH, outW]. outH and outW are calculated based on the existing parameters:
Standard convolution computes the sampling point indices:
Based on the input offset, perform deformable convolution and calculate the offset subscripts:
Use bilinear interpolation to calculate the value of the offset point:
Perform convolution computation to obtain the final output:
Function Prototype
Each operator uses a two-phase API. Call "aclnnDeformableConv2dGetWorkspaceSize" first to obtain the workspace size required for the computation and the executor containing the computation process of the operator, and then call "aclnnDeformableConv2d" to perform the computation.
aclnnStatus aclnnDeformableConv2dGetWorkspaceSize(
const aclTensor* x,
const aclTensor* weight,
const aclTensor* offset,
const aclTensor* biasOptional,
const aclIntArray* kernelSize,
const aclIntArray* stride,
const aclIntArray* padding,
const aclIntArray* dilation,
int64_t groups,
int64_t deformableGroups,
bool modulated,
aclTensor* out,
aclTensor* deformOutOptional,
uint64_t* workspaceSize,
aclOpExecutor** executor)aclnnStatus aclnnDeformableConv2d(
void* workspace,
uint64_t workspaceSize,
aclOpExecutor* executor,
aclrtStream stream)aclnnDeformableConv2dGetWorkspaceSize
Parameters:
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor x Input Original input data. It corresponds to `input` in the formula. - Empty tensors are not supported.
- The shape is [N, inC, inH, inW], where inH * inW cannot exceed 2147483647.
FLOAT32, FLOAT16, BFLOAT16 ND, NCHW 4 √ weight Input 4D tensor of learnable filters. It corresponds to `weight` in the formula. - Empty tensors are not supported.
- Data type and data format are the same as those of input parameter `x`.
- The shape is [outC, inC/groups, K_H, K_W].
FLOAT32, FLOAT16, BFLOAT16 ND, NCHW 4 √ offset Input Represents a 4D tensor of x-y coordinate offsets and masks. Corresponds to `offset` in the formula. - Empty tensors are not supported.
- Data type and data format are the same as those of input parameter `x`.
- When `modulated` is True, the shape is [N, 3 * deformableGroups * K_H * K_W, outH, outW]; when `modulated` is False, the shape is [N, 2 * deformableGroups * K_H * K_W, outH, outW].
FLOAT32, FLOAT16, BFLOAT16 ND, NCHW 4 √ biasOptional Input Optional input parameter, indicating a 1D tensor of the additional bias of the filter output, corresponding to `bias` in the corresponding formula. - Empty tensors are not supported.
- The data type is the same as that of input parameter `x`.
- Set to a null pointer if not required; the shape is [outC] if present.
FLOAT32, FLOAT16, BFLOAT16 ND 1 √ kernelSize Input Kernel size, corresponding to `K_H` and `K_W` in the formula. The size is 2 (K_H, K_W), and each element must be greater than 0. K_H * K_W cannot exceed 2048, and K_H * K_W * inC/groups cannot exceed 65535. aclIntArray - - - stride Input Represents the sliding window stride of each input dimension, corresponding to `stride` in the formula. The size is 4 and all elements are greater than 0. The dimension order is interpreted according to the data format of `x`. The N and C dimensions must be set to 1. aclIntArray - - - padding Input Represents the number of pixels to be added to each side (top, bottom, left, right) of the input, corresponding to `padding` in the formula. size is 4. aclIntArray - - - dilation Input Represents the dilation coefficient of each input dimension, corresponding to `dilation` in the formula. size is 4, each element is greater than 0, and the dimension order is interpreted according to the data format of x. N and C dimensions must be set to 1. aclIntArray - - - groups Input Indicates the number of groups connecting input channels to output channels. inC and outC must be divisible by groups, and groups must be greater than zero. INT64 - - - deformableGroups Input Specifies the number of deformable groups. inC must be divisible by deformableGroups, and deformableGroups must be greater than zero. INT64 - - - modulated Input Reserved parameter indicating whether offset contains a mask. If true, `offset` contains a mask; if false, it does not. Currently, only true is supported. BOOL - - - out Output Represents the output data. Corresponds to `out` in the formula. - Empty tensors are not supported.
- The data type and data format must be the same as those of `x`.
- The shape is [N, outC, outH, outW].
FLOAT32, FLOAT16, BFLOAT16 ND, NCHW 4 √ deformOutOptional Output Optional output, indicating the sampling points of deformable convolution. It corresponds to `deformOut` in the corresponding formula. - Empty tensors are not supported.
- The data type and data format must be the same as those of `x`.
- The shape is [N, inC, outH * K_H, outW * K_W].
FLOAT32, FLOAT16, BFLOAT16 ND, NCHW 4 √ workspaceSize Output Returns the size of the workspace to be applied for on the Device. - - - - - executor Output Returns the op executor containing the operator computation flow. - - - - - Return value:
aclnnStatus: return status code. For details, see aclnn Error Codes.
The first-phase API performs input parameter validation and returns an error in the following scenarios:
Error Code Error Code Description ACLNN_ERR_PARAM_NULLPTR 161001 x, weight, offset, and out are null pointers. ACLNN_ERR_PARAM_INVALID 161002 The data type or data format of x, weight, offset, and out is out of the supported range. If deformOutOptional is not a null pointer, its data type or data format is out of the supported range. When biasOptional is not a null pointer, the data type or data format is out of the supported range. The shapes of x, weight, offset, biasOptional, out, and deformOutOptional are inconsistent with the parameter descriptions. The sizes of kernelSize, stride, padding, and dilation are inconsistent with the parameter descriptions. K_H*K_W exceeds 2048, or K_H*K_W*inC/groups exceeds 65535.
aclnnDeformableConv2d
Parameters:
Parameter Input/Output Description workspace Input Workspace memory address allocated on the device. workspaceSize Input Workspace size allocated on the device, obtained by calling the first-phase API aclnnDeformableConv2dGetWorkspaceSize. executor Input Op executor, containing the operator computation flow. stream Input Stream for task execution. Return value
aclnnStatus: return status code. For details, see aclnn Error Codes.
Constraints
- Deterministic computation:
- aclnnDeformableConv2d defaults to a deterministic implementation.
| Parameter | ||
|---|---|---|
| input, weight, offset, out, deformOutOptional | The supported data formats are ND and NCHW. | The supported data format is NCHW. |
Example
The sample code is as follows for reference only. For details about how to compile and run, see Compile and Run Samples.
#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_deformable_conv2d.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: 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
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 to 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);
// 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];
}
auto format = shape.size() == 1 ? ACL_FORMAT_ND : ACL_FORMAT_NCHW;
// Call aclCreateTensor to create an aclTensor
*tensor = aclCreateTensor(
shape.data(), shape.size(), dataType, strides.data(), 0, format, shape.data(), shape.size(), *deviceAddr);
return 0;
}
int main()
{
// 1. (boilerplate) Initialize 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);
// Handle check as needed.
CHECK_RET(ret == 0, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
// 2. Construct the input and output. Customize the construction based on the API.
std::vector<int64_t> xShape = {1, 6, 2, 4};
std::vector<int64_t> weightShape = {4, 3, 5, 5};
std::vector<int64_t> offsetShape = {1, 75, 2, 4};
std::vector<int64_t> biasShape = {4};
std::vector<int64_t> outShape = {1, 4, 2, 4};
std::vector<int64_t> deformOutShape = {1, 6, 10, 20};
std::vector<int64_t> kernelSize = {5, 5};
std::vector<int64_t> stride = {1, 1, 1, 1};
std::vector<int64_t> padding = {2, 2, 2, 2};
std::vector<int64_t> dilation = {1, 1, 1, 1};
int64_t groups = 2;
int64_t deformableGroups = 1;
void* xDeviceAddr = nullptr;
void* weightDeviceAddr = nullptr;
void* offsetDeviceAddr = nullptr;
void* biasDeviceAddr = nullptr;
void* outDeviceAddr = nullptr;
void* deformOutDeviceAddr = nullptr;
aclTensor* x = nullptr;
aclTensor* weight = nullptr;
aclTensor* offset = nullptr;
aclTensor* bias = nullptr;
aclTensor* out = nullptr;
aclTensor* deformOut = nullptr;
std::vector<float> xHostData(1 * 6 * 2 * 4, 1);
std::vector<float> weightHostData(4 * 3 * 5 * 5, 1);
std::vector<float> offsetHostData(1 * 75 * 2 * 4, 1);
std::vector<float> biasHostData(4, 0);
std::vector<float> outHostData(1 * 4 * 2 * 4, 0);
std::vector<float> deformOutHostData(1 * 6 * 10 * 20, 0);
// Create the x aclTensor.
ret = CreateAclTensor(xHostData, xShape, &xDeviceAddr, aclDataType::ACL_FLOAT, &x);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the weight aclTensor.
ret = CreateAclTensor(weightHostData, weightShape, &weightDeviceAddr, aclDataType::ACL_FLOAT, &weight);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the offset aclTensor.
ret = CreateAclTensor(offsetHostData, offsetShape, &offsetDeviceAddr, aclDataType::ACL_FLOAT, &offset);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the bias aclTensor.
ret = CreateAclTensor(biasHostData, biasShape, &biasDeviceAddr, aclDataType::ACL_FLOAT, &bias);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the out aclTensor.
ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_FLOAT, &out);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the deformOut aclTensor.
ret = CreateAclTensor(deformOutHostData, deformOutShape, &deformOutDeviceAddr, aclDataType::ACL_FLOAT, &deformOut);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the kernelSize aclIntArray.
const aclIntArray* kernelSizeArray = aclCreateIntArray(kernelSize.data(), kernelSize.size());
CHECK_RET(kernelSizeArray != nullptr, return ret);
// Create the stride aclIntArray.
const aclIntArray* strideArray = aclCreateIntArray(stride.data(), stride.size());
CHECK_RET(strideArray != nullptr, return ret);
// Create the padding aclIntArray.
const aclIntArray* paddingArray = aclCreateIntArray(padding.data(), padding.size());
CHECK_RET(paddingArray != nullptr, return ret);
// Create the dilation aclIntArray.
const aclIntArray* dilationArray = aclCreateIntArray(dilation.data(), dilation.size());
CHECK_RET(dilationArray != nullptr, return ret);
// 3. Call the CANN operator library API (replace it with the actual API).
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Call the first-phase API of aclnnDeformableConv2d.
ret = aclnnDeformableConv2dGetWorkspaceSize(
x, weight, offset, bias, kernelSizeArray, strideArray, paddingArray, dilationArray, groups, deformableGroups,
true, out, deformOut, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnDeformableConv2dGetWorkspaceSize 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 aclnnDeformableConv2d.
ret = aclnnDeformableConv2d(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnDeformableConv2d failed. ERROR: %d\n", ret); return ret);
// 4. (Boilerplate) Wait for 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 values and copy the result from device memory to host memory (modify it 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(float),
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 aclScalar. Modify it based on the specific API definition.
aclDestroyTensor(x);
aclDestroyTensor(weight);
aclDestroyTensor(offset);
aclDestroyTensor(bias);
aclDestroyTensor(out);
aclDestroyTensor(deformOut);
// 7. Release device resources. Modify it based on the specific API definition.
aclrtFree(xDeviceAddr);
aclrtFree(weightDeviceAddr);
aclrtFree(offsetDeviceAddr);
aclrtFree(biasDeviceAddr);
aclrtFree(outDeviceAddr);
aclrtFree(deformOutDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}