Skip to content

Commit

Permalink
[Feature](mlu-ops): add dcn_forward and dcn_backward_weight op
Browse files Browse the repository at this point in the history
  • Loading branch information
sangchengmeng committed Jan 16, 2024
1 parent c20850e commit c56aef1
Show file tree
Hide file tree
Showing 16 changed files with 2,721 additions and 50 deletions.
4 changes: 3 additions & 1 deletion docs/MLU-OPS-OpList.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,6 @@ MLU Binary Op算子结构: 
| voxel_pooling_forward || |
| voxelization || |
| yolo_box || |
| dcn_backward_data | ||
| dcn_backward_data | ||
| dcn_forward | ||
| dcn_backward_weight | ||
24 changes: 18 additions & 6 deletions docs/user_guide/9_operators/index.rst
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,24 @@ mluOpCopy
-----------------------------
该算子主要在语音网络中使用,对数据块进行 device 到 device 的拷贝。

.. _dcn_backward_data:

mluOpDCNBackwardData
---------------------------------
该算子用于求取可变形卷积算子关于input、offset、mask的反向梯度。

.. _dcn_backward_weight:

mluOpDCNBackwardWeight
-----------------------------
求取可变形卷积算子关于filter和bias的反向梯度。

.. _dcn_forward:

mluOpDCNForward
-----------------------------
可变形卷积。通过额外的offset和mask来增强滤波器对空间的几何表达能力,并且该卷积可以任意替代之前卷及网络里面的任意常规卷积层。

.. _deform_roi_pool_backward:

mluOpDeformRoiPoolBackward
Expand Down Expand Up @@ -1008,9 +1026,3 @@ mluOpConcat
- ``N`` 为每个input和output的维度数。
- ``sum(axis_1, ..., axis_m)`` 表示对待拼接维度求和,output的拼接维度大小为所有input拼接维度的总和。
- 除拼接维度外,其余维度的大小需要相等。

.. _dcn_backward_data:

mluOpDCNBackwardData
---------------------------------
该算子用于求取可变形卷积算子关于input、offset、mask的反向梯度。
2 changes: 2 additions & 0 deletions kernel_depends.toml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,5 @@ deform_roi_pool_forward = ["deform_roi_pool"]
deform_roi_pool_backward = ["deform_roi_pool"]
carafe_forward = ["carafe"]
carafe_backward = ["carafe"]
dcn_backward_weight = ["dcn_forward"]
dcn_backward_data = ["dcn_forward"]
40 changes: 2 additions & 38 deletions kernels/dcn_backward_data/dcn_backward_data.cpp
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*************************************************************************
* Copyright (C) [2022] by Cambricon, Inc.
* Copyright (C) [2024] by Cambricon, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
Expand All @@ -26,43 +26,7 @@

#include "kernels/utils/cnnl_helper.h"

#define DCNBPDATA_API "mluOpDcnBackwardData"

mluOpStatus_t MLUOP_WIN_API
mluOpCreateDCNDescriptor(mluOpDCNDescriptor_t *dcn_desc) {
PARAM_CHECK(DCNBPDATA_API, dcn_desc != NULL);
CHECK_FUNC_RETURN(cnnlCreateDCNDescriptor(dcn_desc), CNNL_STATUS_SUCCESS,
"[mluOpCreateDCNDescriptor] Internal error accured in "
"cnnlCreateDCNDescriptor.",
MLUOP_STATUS_INTERNAL_ERROR);
return MLUOP_STATUS_SUCCESS;
}

mluOpStatus_t MLUOP_WIN_API
mluOpDestroyDCNDescriptor(mluOpDCNDescriptor_t dcn_desc) {
PARAM_CHECK(DCNBPDATA_API, dcn_desc != NULL);
CHECK_FUNC_RETURN(cnnlDestroyDCNDescriptor(dcn_desc), CNNL_STATUS_SUCCESS,
"[mluOpDestroyDCNDescriptor] Internal error accured in "
"cnnlDestroyDCNDescriptor.",
MLUOP_STATUS_INTERNAL_ERROR);
return MLUOP_STATUS_SUCCESS;
}

mluOpStatus_t MLUOP_WIN_API mluOpSetDCNDescriptor(
mluOpDCNDescriptor_t dcn_desc, int dimNb, const int pad[],
const int stride[], const int dilation[], int deformable_group,
int conv_group, int im2col_step, const mluOpDataType_t compute_type) {
PARAM_CHECK(DCNBPDATA_API, dcn_desc != NULL);
CHECK_FUNC_RETURN(
cnnlSetDCNDescriptor(dcn_desc, dimNb, pad, stride, dilation,
deformable_group, conv_group, im2col_step,
cnnlDataType_t(compute_type)),
CNNL_STATUS_SUCCESS,
"[mluOpSetDCNDescriptor] Internal error accured in "
"cnnlSetDCNDescriptor.",
MLUOP_STATUS_INTERNAL_ERROR);
return MLUOP_STATUS_SUCCESS;
}
#define DCNBPDATA_API "mluOpDCNBackwardData"

mluOpStatus_t MLUOP_WIN_API mluOpGetDCNBakcwardDataWorkspaceSize(
mluOpHandle_t handle, const mluOpDCNDescriptor_t dcn_desc,
Expand Down
109 changes: 109 additions & 0 deletions kernels/dcn_backward_weight/dcn_backward_weight.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*************************************************************************
* Copyright (C) [2024] by Cambricon, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*************************************************************************/
#include <limits.h>
#include <math.h>
#include <vector>

#include "kernels/utils/cnnl_helper.h"

#define DCNBACKWARDWEIGHT_API "mluOpDCNBackwardWeight"

mluOpStatus_t MLUOP_WIN_API mluOpGetDCNBackwardWeightWorkspaceSize(
mluOpHandle_t handle, const mluOpDCNDescriptor_t dcn_desc,
const mluOpTensorDescriptor_t input_desc,
const mluOpTensorDescriptor_t offset_desc,
const mluOpTensorDescriptor_t mask_desc,
const mluOpTensorDescriptor_t grad_output_desc,
const mluOpTensorDescriptor_t grad_filter_desc,
const mluOpTensorDescriptor_t grad_bias_desc, size_t *size) {
PARAM_CHECK("mluOpDCNBackwardWeight", handle != NULL);
PARAM_CHECK("mluOpDCNBackwardWeight", dcn_desc != NULL);
DEFINE_CREATE_AND_SET_CNNL_HANDLE(handle, _handle);
DEFINE_CREATE_AND_SET_CNNL_TENSOR_DESCRIPTOR(input_desc, _input_desc);
DEFINE_CREATE_AND_SET_CNNL_TENSOR_DESCRIPTOR(offset_desc, _offset_desc);
DEFINE_CREATE_AND_SET_CNNL_TENSOR_DESCRIPTOR(mask_desc, _mask_desc);
DEFINE_CREATE_AND_SET_CNNL_TENSOR_DESCRIPTOR(grad_output_desc,
_grad_output_desc);
DEFINE_CREATE_AND_SET_CNNL_TENSOR_DESCRIPTOR(grad_filter_desc,
_grad_filter_desc);
DEFINE_CREATE_AND_SET_CNNL_TENSOR_DESCRIPTOR(grad_bias_desc, _grad_bias_desc);
CHECK_FUNC_RETURN(
cnnlGetDCNBackwardWeightWorkspaceSize(
_handle, dcn_desc, _input_desc, _offset_desc, _mask_desc,
_grad_output_desc, _grad_filter_desc, _grad_bias_desc, size),
CNNL_STATUS_SUCCESS,
"[mluOpDCNBackwardWeight] Internal error accured in "
"mluOpGetDCNBackwardWeightWorkspaceSize.", // NOLINT
MLUOP_STATUS_INTERNAL_ERROR);
DESTROY_CNNL_TENSOR_DESCRIPTOR(_input_desc);
DESTROY_CNNL_TENSOR_DESCRIPTOR(_offset_desc);
DESTROY_CNNL_TENSOR_DESCRIPTOR(_mask_desc);
DESTROY_CNNL_TENSOR_DESCRIPTOR(_grad_output_desc);
DESTROY_CNNL_TENSOR_DESCRIPTOR(_grad_filter_desc);
DESTROY_CNNL_TENSOR_DESCRIPTOR(_grad_bias_desc);
DESTROY_CNNL_HANDLE(_handle);
return MLUOP_STATUS_SUCCESS;
}

mluOpStatus_t MLUOP_WIN_API mluOpDCNBackwardWeight(
mluOpHandle_t handle, const mluOpDCNDescriptor_t dcn_desc,
const mluOpTensorDescriptor_t input_desc, const void *input,
const mluOpTensorDescriptor_t offset_desc, const void *offset,
const mluOpTensorDescriptor_t mask_desc, const void *mask,
const mluOpTensorDescriptor_t grad_output_desc, const void *grad_output,
void *workspace, const size_t workspace_size,
const mluOpTensorDescriptor_t grad_filter_desc, void *grad_filter,
const mluOpTensorDescriptor_t grad_bias_desc, void *grad_bias) {
PARAM_CHECK(DCNBACKWARDWEIGHT_API, handle != NULL);
if (workspace_size > 0) {
PARAM_CHECK(DCNBACKWARDWEIGHT_API, workspace != NULL);
}
DEFINE_CREATE_AND_SET_CNNL_HANDLE(handle, cnnl_handle);
DEFINE_CREATE_AND_SET_CNNL_TENSOR_DESCRIPTOR(input_desc, cnnl_input_desc);
DEFINE_CREATE_AND_SET_CNNL_TENSOR_DESCRIPTOR(offset_desc, cnnl_offset_desc);
DEFINE_CREATE_AND_SET_CNNL_TENSOR_DESCRIPTOR(mask_desc, cnnl_mask_desc);
DEFINE_CREATE_AND_SET_CNNL_TENSOR_DESCRIPTOR(grad_output_desc,
cnnl_grad_output_desc);
DEFINE_CREATE_AND_SET_CNNL_TENSOR_DESCRIPTOR(grad_filter_desc,
cnnl_grad_filter_desc);
DEFINE_CREATE_AND_SET_CNNL_TENSOR_DESCRIPTOR(grad_bias_desc,
cnnl_grad_bias_desc);
CHECK_FUNC_RETURN(
cnnlDCNBackwardWeight(cnnl_handle, dcn_desc, cnnl_input_desc, input,
cnnl_offset_desc, offset, cnnl_mask_desc, mask,
cnnl_grad_output_desc, grad_output, workspace,
workspace_size, cnnl_grad_filter_desc, grad_filter,
cnnl_grad_bias_desc, grad_bias),
CNNL_STATUS_SUCCESS,
"[mluOpDcnBackwardWeight] Internal error accured in "
"mluOpDcnBackwardWeight.",
MLUOP_STATUS_INTERNAL_ERROR);
DESTROY_CNNL_TENSOR_DESCRIPTOR(cnnl_input_desc);
DESTROY_CNNL_TENSOR_DESCRIPTOR(cnnl_offset_desc);
DESTROY_CNNL_TENSOR_DESCRIPTOR(cnnl_mask_desc);
DESTROY_CNNL_TENSOR_DESCRIPTOR(cnnl_grad_output_desc);
DESTROY_CNNL_TENSOR_DESCRIPTOR(cnnl_grad_filter_desc);
DESTROY_CNNL_TENSOR_DESCRIPTOR(cnnl_grad_bias_desc);
DESTROY_CNNL_HANDLE(cnnl_handle);
return MLUOP_STATUS_SUCCESS;
}
69 changes: 69 additions & 0 deletions kernels/dcn_forward/dcn_common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*************************************************************************
* Copyright (C) [2022] by Cambricon, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*************************************************************************/
#ifndef KERNELS_DCN_COMMON_DCN_COMMON_H
#define KERNELS_DCN_COMMON_DCN_COMMON_H
#include <limits.h>
#include <math.h>
#include <vector>

#include "kernels/utils/cnnl_helper.h"

#define DCN_API "mluOpDCN"

mluOpStatus_t MLUOP_WIN_API
mluOpCreateDCNDescriptor(mluOpDCNDescriptor_t *dcn_desc) {
PARAM_CHECK(DCN_API, dcn_desc != NULL);
CHECK_FUNC_RETURN(cnnlCreateDCNDescriptor(dcn_desc), CNNL_STATUS_SUCCESS,
"[mluOpDcn] Internal error accured in "
"mluOpCreateDCNDescriptor.",
MLUOP_STATUS_INTERNAL_ERROR);
return MLUOP_STATUS_SUCCESS;
}

mluOpStatus_t MLUOP_WIN_API
mluOpDestroyDCNDescriptor(mluOpDCNDescriptor_t dcn_desc) {
PARAM_CHECK(DCN_API, dcn_desc != NULL);
CHECK_FUNC_RETURN(cnnlDestroyDCNDescriptor(dcn_desc), CNNL_STATUS_SUCCESS,
"[mluOpDcn] Internal error accured in "
"mluOpDestroyDCNDescriptor.",
MLUOP_STATUS_INTERNAL_ERROR);
return MLUOP_STATUS_SUCCESS;
}

mluOpStatus_t MLUOP_WIN_API mluOpSetDCNDescriptor(
mluOpDCNDescriptor_t dcn_desc, int dimNb, const int pad[],
const int stride[], const int dilation[], int deformable_group,
int conv_group, int im2col_step, const mluOpDataType_t compute_type) {
PARAM_CHECK(DCN_API, dcn_desc != NULL);
CHECK_FUNC_RETURN(
cnnlSetDCNDescriptor(dcn_desc, dimNb, pad, stride, dilation,
deformable_group, conv_group, im2col_step,
cnnlDataType_t(compute_type)),
CNNL_STATUS_SUCCESS,
"[mluOpDcn] Internal error accured in "
"mluOpSetDCNDescriptor.",
MLUOP_STATUS_INTERNAL_ERROR);
return MLUOP_STATUS_SUCCESS;
}

#endif // KERNELS_DCN_COMMON_DCN_COMMON_H
103 changes: 103 additions & 0 deletions kernels/dcn_forward/dcn_forward.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*************************************************************************
* Copyright (C) [2024] by Cambricon, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*************************************************************************/
#include "kernels/dcn_forward/dcn_common.h"

#define DCNFORWARD_API "mluOpDCNForward"

mluOpStatus_t MLUOP_WIN_API mluOpGetDCNForwardWorkspaceSize(
mluOpHandle_t handle, const mluOpDCNDescriptor_t dcn_desc,
const mluOpTensorDescriptor_t input_desc,
const mluOpTensorDescriptor_t offset_desc,
const mluOpTensorDescriptor_t mask_desc,
const mluOpTensorDescriptor_t filter_desc,
const mluOpTensorDescriptor_t bias_desc,
const mluOpTensorDescriptor_t output_desc, size_t *size) {
PARAM_CHECK("mluOpDCNForward", handle != NULL);
PARAM_CHECK("mluOpDCNForward", dcn_desc != NULL);
PARAM_CHECK("mluOpDCNForward", input_desc != NULL);
PARAM_CHECK("mluOpDCNForward", offset_desc != NULL);
PARAM_CHECK("mluOpDCNForward", filter_desc != NULL);
PARAM_CHECK("mluOpDCNForward", output_desc != NULL);
PARAM_CHECK("mluOpDCNForward", size != NULL);
DEFINE_CREATE_AND_SET_CNNL_HANDLE(handle, cnnl_handle);
DEFINE_CREATE_AND_SET_CNNL_TENSOR_DESCRIPTOR(input_desc, cnnl_input_desc);
DEFINE_CREATE_AND_SET_CNNL_TENSOR_DESCRIPTOR(offset_desc, cnnl_offset_desc);
DEFINE_CREATE_AND_SET_CNNL_TENSOR_DESCRIPTOR(mask_desc, cnnl_mask_desc);
DEFINE_CREATE_AND_SET_CNNL_TENSOR_DESCRIPTOR(filter_desc, cnnl_filter_desc);
DEFINE_CREATE_AND_SET_CNNL_TENSOR_DESCRIPTOR(bias_desc, cnnl_bias_desc);
DEFINE_CREATE_AND_SET_CNNL_TENSOR_DESCRIPTOR(output_desc, cnnl_output_desc);
CHECK_FUNC_RETURN(cnnlGetDCNForwardWorkspaceSize(
cnnl_handle, dcn_desc, cnnl_input_desc,
cnnl_offset_desc, cnnl_mask_desc, cnnl_filter_desc,
cnnl_bias_desc, cnnl_output_desc, size),
CNNL_STATUS_SUCCESS,
"[mluOpDCNForward] Internal error accured in "
"mluOpGetDCNForwardWorkspaceSize.", // NOLINT
MLUOP_STATUS_INTERNAL_ERROR);
DESTROY_CNNL_TENSOR_DESCRIPTOR(cnnl_input_desc);
DESTROY_CNNL_TENSOR_DESCRIPTOR(cnnl_offset_desc);
DESTROY_CNNL_TENSOR_DESCRIPTOR(cnnl_mask_desc);
DESTROY_CNNL_TENSOR_DESCRIPTOR(cnnl_filter_desc);
DESTROY_CNNL_TENSOR_DESCRIPTOR(cnnl_bias_desc);
DESTROY_CNNL_TENSOR_DESCRIPTOR(cnnl_output_desc);
DESTROY_CNNL_HANDLE(cnnl_handle);
return MLUOP_STATUS_SUCCESS;
}

mluOpStatus_t MLUOP_WIN_API
mluOpDCNForward(mluOpHandle_t handle, const mluOpDCNDescriptor_t dcn_desc,
const mluOpTensorDescriptor_t input_desc, const void *input,
const mluOpTensorDescriptor_t offset_desc, const void *offset,
const mluOpTensorDescriptor_t mask_desc, const void *mask,
const mluOpTensorDescriptor_t filter_desc, const void *filter,
const mluOpTensorDescriptor_t bias_desc, const void *bias,
void *workspace, size_t workspace_size,
const mluOpTensorDescriptor_t output_desc, void *output) {
PARAM_CHECK(DCNFORWARD_API, handle != NULL);
if (workspace_size > 0) {
PARAM_CHECK(DCNFORWARD_API, workspace != NULL);
}
DEFINE_CREATE_AND_SET_CNNL_HANDLE(handle, cnnl_handle);
DEFINE_CREATE_AND_SET_CNNL_TENSOR_DESCRIPTOR(input_desc, cnnl_input_desc);
DEFINE_CREATE_AND_SET_CNNL_TENSOR_DESCRIPTOR(offset_desc, cnnl_offset_desc);
DEFINE_CREATE_AND_SET_CNNL_TENSOR_DESCRIPTOR(mask_desc, cnnl_mask_desc);
DEFINE_CREATE_AND_SET_CNNL_TENSOR_DESCRIPTOR(filter_desc, cnnl_filter_desc);
DEFINE_CREATE_AND_SET_CNNL_TENSOR_DESCRIPTOR(bias_desc, cnnl_bias_desc);
DEFINE_CREATE_AND_SET_CNNL_TENSOR_DESCRIPTOR(output_desc, cnnl_output_desc);
CHECK_FUNC_RETURN(
cnnlDCNForward(cnnl_handle, dcn_desc, cnnl_input_desc, input,
cnnl_offset_desc, offset, cnnl_mask_desc, mask,
cnnl_filter_desc, filter, cnnl_bias_desc, bias, workspace,
workspace_size, cnnl_output_desc, output),
CNNL_STATUS_SUCCESS,
"[mluOpDcnForward] Internal error accured in mluOpDcnForward.",
MLUOP_STATUS_INTERNAL_ERROR);
DESTROY_CNNL_TENSOR_DESCRIPTOR(cnnl_input_desc);
DESTROY_CNNL_TENSOR_DESCRIPTOR(cnnl_offset_desc);
DESTROY_CNNL_TENSOR_DESCRIPTOR(cnnl_mask_desc);
DESTROY_CNNL_TENSOR_DESCRIPTOR(cnnl_filter_desc);
DESTROY_CNNL_TENSOR_DESCRIPTOR(cnnl_bias_desc);
DESTROY_CNNL_TENSOR_DESCRIPTOR(cnnl_output_desc);
DESTROY_CNNL_HANDLE(cnnl_handle);
return MLUOP_STATUS_SUCCESS;
}
Loading

0 comments on commit c56aef1

Please sign in to comment.