From 46eb9ec5d07ea344ed43056d007a7eb71dc3ee98 Mon Sep 17 00:00:00 2001 From: jayggh <35617559+jayggh@users.noreply.github.com> Date: Wed, 14 Dec 2022 15:09:46 +0800 Subject: [PATCH] [Feature] Add NPU adapter for Deformable RoIPool (#2481) * add npu adapter for deformroipool * cleancode * Update deform_roi_pool.cpp * Update deform_roi_pool.cpp * Update deform_roi_pool.cpp * Update deform_roi_pool.cpp * Update deform_roi_pool.cpp * Update deform_roi_pool.cpp * Update deform_roi_pool.cpp --- docs/en/understand_mmcv/ops.md | 2 +- docs/zh_cn/understand_mmcv/ops.md | 2 +- mmcv/ops/csrc/pytorch/npu/deform_roi_pool.cpp | 63 +++++++++++++++++++ tests/test_ops/test_deform_roi_pool.py | 8 ++- 4 files changed, 71 insertions(+), 4 deletions(-) create mode 100644 mmcv/ops/csrc/pytorch/npu/deform_roi_pool.cpp diff --git a/docs/en/understand_mmcv/ops.md b/docs/en/understand_mmcv/ops.md index cfc70e773..262711d21 100644 --- a/docs/en/understand_mmcv/ops.md +++ b/docs/en/understand_mmcv/ops.md @@ -19,7 +19,7 @@ We implement common ops used in detection, segmentation, etc. | CornerPool | | √ | | | | | Correlation | | √ | | | | | Deformable Convolution v1/v2 | √ | √ | | | | -| Deformable RoIPool | | √ | √ | | | +| Deformable RoIPool | | √ | √ | | √ | | DiffIoURotated | | √ | | | | | DynamicScatter | | √ | | | | | FurthestPointSample | | √ | | | | diff --git a/docs/zh_cn/understand_mmcv/ops.md b/docs/zh_cn/understand_mmcv/ops.md index 7fbba8768..a15392e18 100644 --- a/docs/zh_cn/understand_mmcv/ops.md +++ b/docs/zh_cn/understand_mmcv/ops.md @@ -19,7 +19,7 @@ MMCV 提供了检测、分割等任务中常用的算子 | CornerPool | | √ | | | | | Correlation | | √ | | | | | Deformable Convolution v1/v2 | √ | √ | | | | -| Deformable RoIPool | | √ | √ | | | +| Deformable RoIPool | | √ | √ | | √ | | DiffIoURotated | | √ | | | | | DynamicScatter | | √ | | | | | FurthestPointSample | | √ | | | | diff --git a/mmcv/ops/csrc/pytorch/npu/deform_roi_pool.cpp b/mmcv/ops/csrc/pytorch/npu/deform_roi_pool.cpp new file mode 100644 index 000000000..0e9f2ee7a --- /dev/null +++ b/mmcv/ops/csrc/pytorch/npu/deform_roi_pool.cpp @@ -0,0 +1,63 @@ +#include "pytorch_npu_helper.hpp" + +using namespace NPU_NAME_SPACE; +using namespace std; + +void deform_roi_pool_forward_impl(Tensor input, Tensor rois, Tensor offset, + Tensor output, int pooled_height, + int pooled_width, float spatial_scale, + int sampling_ratio, float gamma); + +void deform_roi_pool_backward_impl(Tensor grad_output, Tensor input, + Tensor rois, Tensor offset, + Tensor grad_input, Tensor grad_offset, + int pooled_height, int pooled_width, + float spatial_scale, int sampling_ratio, + float gamma); + +void deform_roi_pool_forward_npu(Tensor input, Tensor rois, Tensor offset, + Tensor output, int pooled_height, + int pooled_width, float spatial_scale, + int sampling_ratio, float gamma) { + c10::SmallVector output_sizes = {pooled_height, pooled_width}; + at::IntArrayRef output_size = at::IntArrayRef(output_sizes); + int64_t sampling_ratio_ = (int64_t)sampling_ratio; + OpCommand cmd; + cmd.Name("DeformableRoiPool") + .Input(input) + .Input(rois) + .Input(offset) + .Output(output) + .Attr("spatial_scale", spatial_scale) + .Attr("output_size", output_size) + .Attr("sampling_ratio", sampling_ratio_) + .Attr("gamma", gamma) + .Run(); +} + +void deform_roi_pool_backward_npu(Tensor grad_output, Tensor input, Tensor rois, + Tensor offset, Tensor grad_input, + Tensor grad_offset, int pooled_height, + int pooled_width, float spatial_scale, + int sampling_ratio, float gamma) { + c10::SmallVector output_sizes = {pooled_height, pooled_width}; + at::IntArrayRef output_size = at::IntArrayRef(output_sizes); + int64_t sampling_ratio_ = (int64_t)sampling_ratio; + OpCommand cmd; + cmd.Name("DeformableRoiPoolGrad") + .Input(grad_input) + .Input(input) + .Input(rois) + .Input(offset) + .Output(grad_output) + .Output(grad_offset) + .Attr("output_size", output_size) + .Attr("spatial_scale", spatial_scale) + .Attr("sample_ratio", sampling_ratio_) + .Attr("gamma", gamma) + .Run(); +} + +REGISTER_NPU_IMPL(deform_roi_pool_forward_impl, deform_roi_pool_forward_npu); + +REGISTER_NPU_IMPL(deform_roi_pool_backward_impl, deform_roi_pool_backward_npu); diff --git a/tests/test_ops/test_deform_roi_pool.py b/tests/test_ops/test_deform_roi_pool.py index 5c48e6f77..0e6d52fe9 100644 --- a/tests/test_ops/test_deform_roi_pool.py +++ b/tests/test_ops/test_deform_roi_pool.py @@ -5,7 +5,7 @@ import numpy as np import pytest import torch -from mmcv.utils import IS_CUDA_AVAILABLE, IS_MLU_AVAILABLE +from mmcv.utils import IS_CUDA_AVAILABLE, IS_MLU_AVAILABLE, IS_NPU_AVAILABLE _USING_PARROTS = True try: @@ -133,7 +133,11 @@ class TestDeformRoIPool: pytest.param( 'mlu', marks=pytest.mark.skipif( - not IS_MLU_AVAILABLE, reason='requires MLU support')) + not IS_MLU_AVAILABLE, reason='requires MLU support')), + pytest.param( + 'npu', + marks=pytest.mark.skipif( + not IS_NPU_AVAILABLE, reason='requires NPU support')) ]) @pytest.mark.parametrize('dtype', [ torch.float,