aclnnDeformableConv2dBackward

Applicable Products

  • Ascend 950PR&950DT products: Supported
  • Atlas A3 products: Not supported
  • Atlas A2 products: Not supported
  • Atlas 200I/500 A2 inference products: Not supported
  • Atlas inference products: Not supported
  • Atlas training products: Not supported

Function

  • Description: Implements backward propagation of deformable convolution and computes the gradient of the input, the weight, the bias, and the offset.

  • Formula:

    The relationship between the input input(N,Cin,Hin,WinN,C_{in},H_{in},W_{in}), the output out(N,Cout,Hout,WoutN,C_{out},H_{out},W_{out}), the convolution stride (stridestride), the kernel size (kernelSize,KH,KWkernelSize,K_H,K_W), the dilation (dilationdilation), and the padding (paddingpadding) is:

    Hout=⌊Hin+padding[0]+padding[1]−((KH−1)∗dilation[2]+1)stride[2]+1⌋H_{out}=\lfloor \frac{H_{in}+padding[0]+padding[1]-((K_H - 1) * dilation[2] + 1)}{stride[2]}+1 \rfloor Wout=⌊Win+padding[2]+padding[3]−((KW−1)∗dilation[3]+1)stride[3]+1⌋W_{out}=\lfloor \frac{W_{in}+padding[2]+padding[3]-((K_W - 1) * dilation[3] + 1)}{stride[3]}+1 \rfloor

    The backward propagation of deformable convolution requires computing the gradients of the input tensor xx of the forward convolution (input in the prototype), the convolution kernel weight tensor ww (weight in the prototype), the bias bb (bias in the prototype), and the offset oo (offset in the prototype).

    For deformable convolution, the sampling position of each output position is dynamically determined by the offset. Let the output position be (n,cout,i,j)(n, c_{out}, i, j) and the corresponding offset be Δpn,cout,i,j\Delta p_{n,c_{out},i,j}. The actual sampling position is pn,cout,i,j=(i⋅sH,j⋅sW)+Δpn,cout,i,jp_{n,c_{out},i,j} = (i \cdot s_H, j \cdot s_W) + \Delta p_{n,c_{out},i,j}, where sHs_H and sWs_W are the strides in the height and width directions, respectively.

    • For the gradient of xx, ∂L∂x\frac{\partial L}{\partial x} (corresponding to the gradInput parameter in the function prototype):

      ∂L∂xn,cin,h,w=∑cout=1Cout∑i=1Hout∑j=1Wout∂L∂yn,cout,i,j⋅wcout,cin,p,q⋅δ(pn,cout,i,j=(h,w))\frac{\partial L}{\partial x_{n, c_{in}, h, w}} = \sum_{c_{out}=1}^{C_{out}} \sum_{i=1}^{H_{out}} \sum_{j=1}^{W_{out}} \frac{\partial L}{\partial y_{n, c_{out}, i, j}} \cdot w_{c_{out}, c_{in}, p, q} \cdot \delta(p_{n,c_{out},i,j} = (h, w))

      where LL is the loss function, ∂L∂y\frac{\partial L}{\partial y} is the gradient of the output tensor yy with respect to LL (the gradOutput parameter in the prototype), and δ(⋅)\delta(\cdot) is the indicator function, which is 1 when the sampling position coincides with the input position (h,w)(h,w) and 0 otherwise. In the actual implementation, the gradient is distributed to neighboring pixel positions through bilinear interpolation.

    • For the gradient of ww, ∂L∂w\frac{\partial L}{\partial w} (corresponding to the gradWeight parameter in the function prototype):

      ∂L∂wcout,cin,p,q=∑n=1N∑i=1Hout∑j=1Woutxn,cin,pn,cout,i,j.h,pn,cout,i,j.w⋅∂L∂yn,cout,i,j\frac{\partial L}{\partial w_{c_{out}, c_{in}, p, q}} = \sum_{n=1}^{N} \sum_{i=1}^{H_{out}} \sum_{j=1}^{W_{out}} x_{n, c_{in}, p_{n,c_{out},i,j}.h, p_{n,c_{out},i,j}.w} \cdot \frac{\partial L}{\partial y_{n, c_{out}, i, j}}

      where pn,cout,i,j.hp_{n,c_{out},i,j}.h and pn,cout,i,j.wp_{n,c_{out},i,j}.w indicate the height and width coordinates of the sampling position respectively, obtained by sampling from the input tensor xx through bilinear interpolation.

    • For the gradient of bb, ∂L∂b\frac{\partial L}{\partial b} (corresponding to the gradBias parameter in the function prototype):

      ∂L∂bcout=∑n=1N∑i=1Hout∑j=1Wout∂L∂yn,cout,i,j\frac{\partial L}{\partial b_{c_{out}}} = \sum_{n=1}^{N} \sum_{i=1}^{H_{out}} \sum_{j=1}^{W_{out}} \frac{\partial L}{\partial y_{n, c_{out}, i, j}}
    • For the gradient of oo ∂L∂o\frac{\partial L}{\partial o} (corresponding to the gradOffset parameter in the prototype):

      ∂L∂on,cout,i,j=∑cin=1Cin∂L∂yn,cout,i,j⋅wcout,cin,p,q⋅∂xn,cin,pn,cout,i,j.h,pn,cout,i,j.w∂on,cout,i,j\frac{\partial L}{\partial o_{n, c_{out}, i, j}} = \sum_{c_{in}=1}^{C_{in}} \frac{\partial L}{\partial y_{n, c_{out}, i, j}} \cdot w_{c_{out}, c_{in}, p, q} \cdot \frac{\partial x_{n, c_{in}, p_{n,c_{out},i,j}.h, p_{n,c_{out},i,j}.w}}{\partial o_{n, c_{out}, i, j}}

      where the computation of the gradient of the offset involves the backward propagation of bilinear interpolation. The partial derivative of the input value with respect to the offset must be computed. This partial derivative reflects the impact of offset changes on the sampling point position.

Function Prototype

Each operator uses a two-phase API. You must first call the "aclnnDeformableConv2dBackwardGetWorkspaceSize" API to obtain the workspace size required for computation and the executor that contains the operator computation process. Then call the "aclnnDeformableConv2dBackward" API to perform the computation.

aclnnStatus aclnnDeformableConv2dBackwardGetWorkspaceSize(
  const aclTensor*   input,
  const aclTensor*   gradOutput,
  const aclTensor*   offsetOut,
  const aclTensor*   weight,
  const aclTensor*   offset,
  const aclIntArray* kernelSize,
  const aclIntArray* stride,
  const aclIntArray* padding,
  const aclIntArray* dilation,
  int64_t            groups,
  int64_t            deformableGroups,
  bool               modulated,
  aclTensor*         gradInput,
  aclTensor*         gradWeight,
  aclTensor*         gradOffset,
  aclTensor*         gradBias,
  uint64_t*          workspaceSize,
  aclOpExecutor**    executor)
aclnnStatus aclnnDeformableConv2dBackward(
  void*          workspace,
  uint64_t       workspaceSize,
  aclOpExecutor* executor,
  const aclrtStream    stream)

aclnnDeformableConv2dBackwardGetWorkspaceSize

  • Parameters:

    Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor
    Input Input Represents the input data of forward propagation.
    • Empty tensors are not supported.
    • Shape is [N, inC, inH, inW].
    FLOAT32, FLOAT16, BFLOAT16 NCHW 4 √
    gradOutput Input Represents the gradient of the forward propagation output.
    • Empty tensors are not supported.
    • The data type and data format must be the same as those of input.
    • The shape is [N, outC, outH, outW].
    Same as input NCHW 4 √
    offsetOut Input Represents the output sampling points of the deformable convolution in forward propagation.
    • Empty tensors are not supported.
    • The data type and data format must be the same as those of input.
    • The shape is [N, inC, outH * K_H, outW * K_W].
    Same as input NCHW 4 √
    weight Input Represents the convolution kernel weight in forward propagation.
    • Empty tensors are not supported.
    • The data type and data format must be the same as those of input.
    • The shape is [outC, inC/groups, K_H, K_W].
    Same as input NCHW 4 √
    offset Input Represents the x-y coordinate offsets and mask in forward propagation.
    • Empty tensors are not supported.
    • The data type and data format must be the same as those of input.
    • When modulated is True, the shape is [N, 3 * deformableGroups * K_H * K_W, outH, outW].
    Same as input NCHW 4 √
    kernelSize Input Specifies the convolution kernel size, corresponding to K_H and K_W in the formula. size is 2 (K_H, K_W). Each element must be greater than 0. K_H * K_W must not exceed 2048, and K_H * K_W * inC/groups must not exceed 65535. INT64 - - -
    stride Input Specifies the sliding window stride of each input dimension, corresponding to stride in the formula. size is 4 and each element is greater than zero. Dimensions are interpreted based on the data format of input. N and C must be set to 1. INT64 - - -
    padding Input Represents the number of pixels padded to each side of input (top, bottom, left, and right), corresponding to padding in the formula. size is 4. INT64 - - -
    dilation Input Represents the dilation coefficient of each dimension of input, corresponding to dilation in the formula. size is 4, all elements are greater than zero, and the dimension order is interpreted according to the data format of input. The N dimension and C dimension must be set to 1. INT64 - - -
    groups Input Specifies the number of groups in grouped convolution. inC and outC must both be divisible by groups, and groups must be greater than zero. INT64 - - -
    deformableGroups Input Represents the number of deformable group partitions. inC must be divisible by deformableGroups, and deformableGroups must be greater than zero. INT64 - - -
    modulated Input Indicates whether offset contains a mask. If true, offset contains a mask; if false, it does not. Only true is supported. BOOL - - -
    gradInput Output Gradient of the input data.
    • Empty tensors are not supported.
    • The data type and data format must be the same as those of input.
    • The shape is [N, inC, inH, inW].
    Same as input NCHW 4 √
    gradWeight Output Gradient of the convolution kernel weight.
    • Empty tensors are not supported.
    • The data type and data format must be the same as those of input.
    • The shape is [outC, inC/groups, K_H, K_W].
    Same as input NCHW 4 √
    gradOffset Output Gradient of the offset.
    • Empty tensor is not supported.
    • The data type and data format are the same as those of input.
    • If modulated is True, the shape is [N, 3 * deformableGroups * K_H * K_W, outH, outW]; if modulated is False, the shape is [N, 2 * deformableGroups * K_H * K_W, outH, outW].
    Same as input NCHW 4 √
    gradBias Output Gradient of bias.
    • Empty tensors are not supported.
    • shape: [outC].
    Same as input ND 1 √
    workspaceSize Output Returns the size of the workspace to be requested on the device. - - - - -
    executor Output Returns the op executor, which contains 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 input, gradOutput, offsetOut, weight, offset, gradInput, gradWeight, gradBias, or gradOffset is a null pointer.
    ACLNN_ERR_PARAM_INVALID 161002 The data type or data format of input, gradOutput, offsetOut, weight, offset, gradInput, gradWeight, gradBias, or gradOffset is beyond the supported range.
    The shape of input, gradOutput, offsetOut, weight, offset, gradInput, gradWeight, gradBias, or gradOffset is inconsistent with that described in Parameters.
    The size of kernelSize, stride, padding, or dilation is inconsistent with that described in Parameters.
    K_H*K_W exceeds 2048, or K_H*K_W*inC/groups exceeds 65535.
    The groups or deformableGroups parameter does not meet the Constraints.
    The modulated parameter is not true.
    ACLNN_ERR_INNER_NULLPTR 561103 API internal verification error, usually caused by the specifications of input data or attributes being out of the supported range.

aclnnDeformableConv2dBackward

  • Parameters:

    Parameter Input/Output Description
    workspace Input Address of the workspace memory allocated on the Device.
    workspaceSize Input Size of the workspace memory allocated on the Device, obtained by the first-phase API aclnnDeformableConv2dBackwardGetWorkspaceSize.
    executor Input Executor of the op, which contains the computation flow of the operator.
    stream Input Stream for task execution.
  • Return value

    aclnnStatus: return status code. For details, see aclnn Error Codes.

Constraints

  • Deterministic computation:
    • aclnnDeformableConv2dBackward 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_convolution_backward.h"

#define CHECK_RET(cond, return_expr) \
    do {                             \
        if (!(cond)) {               \
            return_expr;             \
        }                            \
    } while (0)

#define CHECK_FREE_RET(cond, return_expr)        \
    do {                                         \
        if (!(cond)) {                           \
            Finalize(deviceId, stream); \
            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)
{
    // 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 device memory.
    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 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];
    }

    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;
}

void Finalize(int32_t deviceId, aclrtStream stream)
{
    aclrtDestroyStream(stream);
    aclrtResetDevice(deviceId);
    aclFinalize();
}

int aclnnDeformableConv2dBackwardTest(int32_t deviceId, aclrtStream& stream)
{
    auto ret = Init(deviceId, &stream);
    // Handle the 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 according to the API.
    std::vector<int64_t> inputShape = {1, 6, 2, 4};
    std::vector<int64_t> gradOutputShape = {1, 4, 2, 4};
    std::vector<int64_t> offsetOutShape = {1, 6, 10, 20};
    std::vector<int64_t> weightShape = {4, 3, 5, 5};
    std::vector<int64_t> offsetShape = {1, 75, 2, 4};
    std::vector<int64_t> gradInputShape = {1, 6, 2, 4};
    std::vector<int64_t> gradWeightShape = {4, 3, 5, 5};
    std::vector<int64_t> gradBiasShape = {4};
    std::vector<int64_t> gradOffsetShape = {1, 75, 2, 4};
    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* inputDeviceAddr = nullptr;
    void* gradOutputDeviceAddr = nullptr;
    void* offsetOutDeviceAddr = nullptr;
    void* weightDeviceAddr = nullptr;
    void* offsetDeviceAddr = nullptr;
    void* gradInputDeviceAddr = nullptr;
    void* gradWeightDeviceAddr = nullptr;
    void* gradBiasDeviceAddr = nullptr;
    void* gradOffsetDeviceAddr = nullptr;
    aclTensor* input = nullptr;
    aclTensor* gradOutput = nullptr;
    aclTensor* offsetOut = nullptr;
    aclTensor* weight = nullptr;
    aclTensor* offset = nullptr;
    aclTensor* gradInput = nullptr;
    aclTensor* gradWeight = nullptr;
    aclTensor* gradBias = nullptr;
    aclTensor* gradOffset = nullptr;
    std::vector<float> inputHostData(1 * 6 * 2 * 4, 1);
    std::vector<float> gradOutputHostData(1 * 4 * 2 * 4, 1);
    std::vector<float> offsetOutHostData(1 * 6 * 10 * 20, 1);
    std::vector<float> weightHostData(4 * 3 * 5 * 5, 1);
    std::vector<float> offsetHostData(1 * 75 * 2 * 4, 1);
    std::vector<float> gradInputHostData(1 * 6 * 2 * 4, 0);
    std::vector<float> gradWeightHostData(4 * 3 * 5 * 5, 0);
    std::vector<float> gradBiasHostData(4, 0);
    std::vector<float> gradOffsetHostData(1 * 75 * 2 * 4, 0);
    // Create the input aclTensor.
    ret = CreateAclTensor(inputHostData, inputShape, &inputDeviceAddr, aclDataType::ACL_FLOAT, &input);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create the gradOutput aclTensor.
    ret = CreateAclTensor(
        gradOutputHostData, gradOutputShape, &gradOutputDeviceAddr, aclDataType::ACL_FLOAT, &gradOutput);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create the offsetOut aclTensor.
    ret = CreateAclTensor(offsetOutHostData, offsetOutShape, &offsetOutDeviceAddr, aclDataType::ACL_FLOAT, &offsetOut);
    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 offset aclTensor.
    ret = CreateAclTensor(offsetHostData, offsetShape, &offsetDeviceAddr, aclDataType::ACL_FLOAT, &offset);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create gradInput aclTensor.
    ret = CreateAclTensor(gradInputHostData, gradInputShape, &gradInputDeviceAddr, aclDataType::ACL_FLOAT, &gradInput);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create gradWeight aclTensor.
    ret = CreateAclTensor(
        gradWeightHostData, gradWeightShape, &gradWeightDeviceAddr, aclDataType::ACL_FLOAT, &gradWeight);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create gradBias aclTensor.
    ret = CreateAclTensor(gradBiasHostData, gradBiasShape, &gradBiasDeviceAddr, aclDataType::ACL_FLOAT, &gradBias);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create gradOffset aclTensor.
    ret = CreateAclTensor(
        gradOffsetHostData, gradOffsetShape, &gradOffsetDeviceAddr, aclDataType::ACL_FLOAT, &gradOffset);
    CHECK_RET(ret == ACL_SUCCESS, return ret);

    // Create kernelSize aclIntArray.
    const aclIntArray* kernelSizeArray = aclCreateIntArray(kernelSize.data(), kernelSize.size());
    CHECK_RET(kernelSizeArray != nullptr, return ret);
    // Create stride aclIntArray.
    const aclIntArray* strideArray = aclCreateIntArray(stride.data(), stride.size());
    CHECK_RET(strideArray != nullptr, return ret);
    // Create padding aclIntArray.
    const aclIntArray* paddingArray = aclCreateIntArray(padding.data(), padding.size());
    CHECK_RET(paddingArray != nullptr, return ret);
    // Create 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 specific API.
    uint64_t workspaceSize = 0;
    aclOpExecutor* executor;
    // Call the first-phase API of aclnnDeformableConv2dBackward.
    ret = aclnnDeformableConv2dBackwardGetWorkspaceSize(
        input, gradOutput, offsetOut, weight, offset, kernelSizeArray, strideArray, paddingArray, dilationArray, groups,
        deformableGroups, true, gradInput, gradWeight, gradOffset, gradBias, &workspaceSize, &executor);
    CHECK_RET(
        ret == ACL_SUCCESS, LOG_PRINT("aclnnDeformableConv2dBackwardGetWorkspaceSize 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 aclnnDeformableConv2dBackward.
    ret = aclnnDeformableConv2dBackward(workspaceAddr, workspaceSize, executor, stream);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnDeformableConv2dBackward failed. ERROR: %d\n", ret); return ret);
    // 4. (boilerplate) Synchronize and wait for the task execution to finish.
    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 on the device side to the host side. Modify it based on the specific API definition.
    auto size = GetShapeSize(gradInputShape);
    std::vector<float> resultData(size, 0);
    ret = aclrtMemcpy(
        resultData.data(), resultData.size() * sizeof(resultData[0]), gradInputDeviceAddr, 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]);
    }

    size = GetShapeSize(gradWeightShape);
    std::vector<float> gradWeightShapeData(size, 0);
    ret = aclrtMemcpy(
        gradWeightShapeData.data(), gradWeightShapeData.size() * sizeof(gradWeightShapeData[0]), gradWeightDeviceAddr,
        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, gradWeightShapeData[i]);
    }

    size = GetShapeSize(gradOffsetShape);
    std::vector<float> gradOffsetShapeData(size, 0);
    ret = aclrtMemcpy(
        gradOffsetShapeData.data(), gradOffsetShapeData.size() * sizeof(gradOffsetShapeData[0]), gradOffsetDeviceAddr,
        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, gradOffsetShapeData[i]);
    }

    size = GetShapeSize(gradBiasShape);
    std::vector<float> gradBiasShapeData(size, 0);
    ret = aclrtMemcpy(
        gradBiasShapeData.data(), gradBiasShapeData.size() * sizeof(gradBiasShapeData[0]), gradBiasDeviceAddr,
        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, gradBiasShapeData[i]);
    }

    // 6. Release aclTensor and aclScalar. Modify it based on the specific API definition.
    aclDestroyTensor(input);
    aclDestroyTensor(gradOutput);
    aclDestroyTensor(offsetOut);
    aclDestroyTensor(weight);
    aclDestroyTensor(offset);
    aclDestroyTensor(gradInput);
    aclDestroyTensor(gradWeight);
    aclDestroyTensor(gradBias);
    aclDestroyTensor(gradOffset);

    // 7. Release device resources. Modify it based on the specific API definition.
    aclrtFree(inputDeviceAddr);
    aclrtFree(gradOutputDeviceAddr);
    aclrtFree(offsetOutDeviceAddr);
    aclrtFree(weightDeviceAddr);
    aclrtFree(offsetDeviceAddr);
    aclrtFree(gradInputDeviceAddr);
    aclrtFree(gradWeightDeviceAddr);
    aclrtFree(gradBiasDeviceAddr);
    aclrtFree(gradOffsetDeviceAddr);
    if (workspaceSize > 0) {
        aclrtFree(workspaceAddr);
    }
    return ACL_SUCCESS;
}

int main()
{
    // 1. (boilerplate) Initialize the device/stream.
    // Set deviceId based on the actual device.
    int32_t deviceId = 0;
    aclrtStream stream;
    auto ret = aclnnDeformableConv2dBackwardTest(deviceId, stream);
    CHECK_FREE_RET(
        ret == ACL_SUCCESS, LOG_PRINT("aclnnDeformableConv2dBackwardTest failed. ERROR: %d\n", ret); return ret);

    Finalize(deviceId, stream);
    return 0;
}