-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature](mlu-ops): add dcn_forward and dcn_backward_weight op
- Loading branch information
sangchengmeng
committed
Jan 16, 2024
1 parent
c20850e
commit c56aef1
Showing
16 changed files
with
2,721 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.