From a3849646729bf35506b1486ca8903b4b4d14f275 Mon Sep 17 00:00:00 2001 From: momo609 <963372609@qq.com> Date: Fri, 3 Mar 2023 19:18:10 +0800 Subject: [PATCH 1/4] add roi_algin op --- mmcv/ops/csrc/pytorch/npu/roi_align_npu.cpp | 65 +++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 mmcv/ops/csrc/pytorch/npu/roi_align_npu.cpp diff --git a/mmcv/ops/csrc/pytorch/npu/roi_align_npu.cpp b/mmcv/ops/csrc/pytorch/npu/roi_align_npu.cpp new file mode 100644 index 0000000000..7b090850fa --- /dev/null +++ b/mmcv/ops/csrc/pytorch/npu/roi_align_npu.cpp @@ -0,0 +1,65 @@ +#include "pytorch_npu_helper.hpp" + +using namespace NPU_NAME_SPACE; +using namespace std; + +void roi_align_forward_npu(Tensor input, Tensor rois, Tensor output, + Tensor argmax_y, Tensor argmax_x, int aligned_height, + int aligned_width, float spatial_scale, + int sampling_ratio, int pool_mode, bool aligned) { + int64_t aligned_height_64 = aligned_height; + int64_t aligned_width_64 = aligned_width; + int64_t sampling_ratio_64 = sampling_ratio; + int64_t roi_end_mode = 0; + OpCommand cmd; + cmd.Name("ROIAlign") + .Input(input) + .Input(rois) + .Output(output) + .Attr("spatial_scale", spatial_scale) + .Attr("pooled_height", aligned_height_64) + .Attr("pooled_width", aligned_width_64) + .Attr("sample_num", sampling_ratio_64) + .Attr("roi_end_mode", roi_end_mode) + .Run(); +} + +void roi_align_backward_npu(Tensor grad_output, Tensor rois, Tensor argmax_y, + Tensor argmax_x, Tensor grad_input, + int aligned_height, int aligned_width, + float spatial_scale, int sampling_ratio, + int pool_mode, bool aligned) { + int64_t aligned_height_64 = aligned_height; + int64_t aligned_width_64 = aligned_width; + int64_t sampling_ratio_64 = sampling_ratio; + int64_t roi_end_mode = 0; + c10::SmallVector xdiff_shape = + at_npu::native::array_to_small_vector(grad_input.sizes()); + OpCommand cmd; + cmd.Name("ROIAlignGrad") + .Input(grad_output) + .Input(rois) + .Output(grad_input) + .Attr("xdiff_shape", xdiff_shape) + .Attr("pooled_width", aligned_width_64) + .Attr("pooled_height", aligned_height_64) + .Attr("spatial_scale", spatial_scale) + .Attr("sample_num", sampling_ratio_64) + .Attr("roi_end_mode", roi_end_mode) + .Run(); +} + +void roi_align_forward_impl(Tensor input, Tensor rois, Tensor output, + Tensor argmax_y, Tensor argmax_x, + int aligned_height, int aligned_width, + float spatial_scale, int sampling_ratio, + int pool_mode, bool aligned); + +void roi_align_backward_impl(Tensor grad_output, Tensor rois, Tensor argmax_y, + Tensor argmax_x, Tensor grad_input, + int aligned_height, int aligned_width, + float spatial_scale, int sampling_ratio, + int pool_mode, bool aligned); + +REGISTER_NPU_IMPL(roi_align_forward_impl, roi_align_forward_npu); +REGISTER_NPU_IMPL(roi_align_backward_impl, roi_align_backward_npu); From b6834bd42857c101a43ee04054111dd31252da32 Mon Sep 17 00:00:00 2001 From: momo609 <963372609.qq.com> Date: Mon, 20 Mar 2023 09:43:32 +0800 Subject: [PATCH 2/4] add unit test --- tests/test_ops/test_roi_align.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/tests/test_ops/test_roi_align.py b/tests/test_ops/test_roi_align.py index 6caf5c5356..f8b1f3e9a2 100644 --- a/tests/test_ops/test_roi_align.py +++ b/tests/test_ops/test_roi_align.py @@ -3,7 +3,7 @@ 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: @@ -102,7 +102,11 @@ def _test_roialign_allclose(device, dtype): 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, @@ -111,6 +115,11 @@ def _test_roialign_allclose(device, dtype): marks=pytest.mark.skipif( IS_MLU_AVAILABLE, reason='MLU does not support for 64-bit floating point')), + pytest.param( + torch.double, + marks=pytest.mark.skipif( + IS_NPU_AVAILABLE, + reason='NPU does not support for 64-bit floating point')), torch.half ]) def test_roialign(device, dtype): From 01be0e665ae494e3ee2a8e13d08d05dc17a518f4 Mon Sep 17 00:00:00 2001 From: momo609 <963372609.qq.com> Date: Mon, 20 Mar 2023 15:09:35 +0800 Subject: [PATCH 3/4] update ops.md --- docs/en/understand_mmcv/ops.md | 2 +- docs/zh_cn/understand_mmcv/ops.md | 2 +- mmcv/ops/csrc/pytorch/npu/roi_align_npu.cpp | 3 +++ tests/test_ops/test_roi_align.py | 9 ++------- 4 files changed, 7 insertions(+), 9 deletions(-) diff --git a/docs/en/understand_mmcv/ops.md b/docs/en/understand_mmcv/ops.md index ac9b36a5be..6c5f760bbe 100644 --- a/docs/en/understand_mmcv/ops.md +++ b/docs/en/understand_mmcv/ops.md @@ -46,7 +46,7 @@ We implement common ops used in detection, segmentation, etc. | RoIPool | | √ | √ | | √ | | RoIAlignRotated | √ | √ | √ | | | | RiRoIAlignRotated | | √ | | | | -| RoIAlign | √ | √ | √ | | | +| RoIAlign | √ | √ | √ | | √ | | RoIAwarePool3d | | √ | √ | | | | SAConv2d | | √ | | | | | SigmoidFocalLoss | | √ | √ | | √ | diff --git a/docs/zh_cn/understand_mmcv/ops.md b/docs/zh_cn/understand_mmcv/ops.md index 70179d05d2..0fd0873a32 100644 --- a/docs/zh_cn/understand_mmcv/ops.md +++ b/docs/zh_cn/understand_mmcv/ops.md @@ -46,7 +46,7 @@ MMCV 提供了检测、分割等任务中常用的算子 | RoIPool | | √ | √ | | √ | | RoIAlignRotated | √ | √ | √ | | | | RiRoIAlignRotated | | √ | | | | -| RoIAlign | √ | √ | √ | | | +| RoIAlign | √ | √ | √ | | √ | | RoIAwarePool3d | | √ | √ | | | | SAConv2d | | √ | | | | | SigmoidFocalLoss | | √ | √ | | √ | diff --git a/mmcv/ops/csrc/pytorch/npu/roi_align_npu.cpp b/mmcv/ops/csrc/pytorch/npu/roi_align_npu.cpp index 7b090850fa..31335557e4 100644 --- a/mmcv/ops/csrc/pytorch/npu/roi_align_npu.cpp +++ b/mmcv/ops/csrc/pytorch/npu/roi_align_npu.cpp @@ -7,6 +7,9 @@ void roi_align_forward_npu(Tensor input, Tensor rois, Tensor output, Tensor argmax_y, Tensor argmax_x, int aligned_height, int aligned_width, float spatial_scale, int sampling_ratio, int pool_mode, bool aligned) { + if (!aligned) { + LOG(WARNING) << "The [aligned] attr in roi_align op is false"; + } int64_t aligned_height_64 = aligned_height; int64_t aligned_width_64 = aligned_width; int64_t sampling_ratio_64 = sampling_ratio; diff --git a/tests/test_ops/test_roi_align.py b/tests/test_ops/test_roi_align.py index f8b1f3e9a2..14b43e5abe 100644 --- a/tests/test_ops/test_roi_align.py +++ b/tests/test_ops/test_roi_align.py @@ -113,13 +113,8 @@ def _test_roialign_allclose(device, dtype): pytest.param( torch.double, marks=pytest.mark.skipif( - IS_MLU_AVAILABLE, - reason='MLU does not support for 64-bit floating point')), - pytest.param( - torch.double, - marks=pytest.mark.skipif( - IS_NPU_AVAILABLE, - reason='NPU does not support for 64-bit floating point')), + IS_MLU_AVAILABLE or IS_NPU_AVAILABLE, + reason='MLU and NPU does not support for 64-bit floating point')), torch.half ]) def test_roialign(device, dtype): From c3ef4c0794896731bba3cd8b00e1cfb2ad4ba8dc Mon Sep 17 00:00:00 2001 From: Zaida Zhou <58739961+zhouzaida@users.noreply.github.com> Date: Mon, 20 Mar 2023 19:07:50 +0800 Subject: [PATCH 4/4] fix typo --- tests/test_ops/test_roi_align.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_ops/test_roi_align.py b/tests/test_ops/test_roi_align.py index 14b43e5abe..9609f37c93 100644 --- a/tests/test_ops/test_roi_align.py +++ b/tests/test_ops/test_roi_align.py @@ -114,7 +114,7 @@ def _test_roialign_allclose(device, dtype): torch.double, marks=pytest.mark.skipif( IS_MLU_AVAILABLE or IS_NPU_AVAILABLE, - reason='MLU and NPU does not support for 64-bit floating point')), + reason='MLU and NPU do not support for 64-bit floating point')), torch.half ]) def test_roialign(device, dtype):