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 15, 2024
1 parent fe373fc commit c0229c5
Show file tree
Hide file tree
Showing 20 changed files with 2,826 additions and 42 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 | ||
8 changes: 8 additions & 0 deletions docs/user_guide/9_operators/index.rst
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -1014,3 +1014,11 @@ mluOpConcat
mluOpDCNBackwardData
---------------------------------
该算子用于求取可变形卷积算子关于input、offset、mask的反向梯度。

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

mluOpDCNBackwardWeight
-----------------------------
求取可变形卷积算子关于filter和bias的反向梯度。
5 changes: 5 additions & 0 deletions kernel_depends.toml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ masked_col2im_forward = ["fill", "transpose"]
masked_im2col_forward = ["fill", "transpose"]
mutual_information_backward = ["fill"]
batch_matmul_bcast = ["matmul"]
dcn_forward = ["dcn_common"]
dcn_backward_weight = ["dcn_common"]
dcn_backward_data = ["dcn_common"]

[gtest]

Expand Down Expand Up @@ -72,3 +75,5 @@ deform_roi_pool_forward = ["deform_roi_pool"]
deform_roi_pool_backward = ["deform_roi_pool"]
carafe_forward = ["carafe"]
carafe_backward = ["carafe"]
dcn_forward = ["transpose"]
dcn_backward_weight = ["transpose"]
41 changes: 1 addition & 40 deletions kernels/dcn_backward_data/dcn_backward_data.cpp
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -20,49 +20,10 @@
* 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"
#include "kernels/dcn_common/dcn_common.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,
"[mluOpDcnBackwardData] 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(DCNBPDATA_API, dcn_desc != NULL);
CHECK_FUNC_RETURN(cnnlDestroyDCNDescriptor(dcn_desc), CNNL_STATUS_SUCCESS,
"[mluOpDcnBackwardData] 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(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,
"[mluOpDcnBackwardData] Internal error accured in mluOpSetDCNDescriptor.",
MLUOP_STATUS_INTERNAL_ERROR);
return MLUOP_STATUS_SUCCESS;
}

mluOpStatus_t MLUOP_WIN_API mluOpGetDCNBakcwardDataWorkspaceSize(
mluOpHandle_t handle, const mluOpDCNDescriptor_t dcn_desc,
const mluOpTensorDescriptor_t input_desc,
Expand Down
107 changes: 107 additions & 0 deletions kernels/dcn_backward_weight/dcn_backward_weight.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*************************************************************************
* 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.
*************************************************************************/

#include "kernels/dcn_common/dcn_common.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;
}
60 changes: 60 additions & 0 deletions kernels/dcn_common/dcn_common.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*************************************************************************
* 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.
*************************************************************************/
#include "dcn_common.h"


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;
}
45 changes: 45 additions & 0 deletions kernels/dcn_common/dcn_common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*************************************************************************
* 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_DCN_H_
#define KERNELS_DCN_DCN_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);

mluOpStatus_t MLUOP_WIN_API
mluOpDestroyDCNDescriptor(mluOpDCNDescriptor_t dcn_desc);

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

#endif // KERNELS_DCN_DCN_H_
60 changes: 60 additions & 0 deletions kernels/dcn_forward/dcn_common.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*************************************************************************
* 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.
*************************************************************************/
#include "dcn_common.h"


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

0 comments on commit c0229c5

Please sign in to comment.