From cf8ffc2beadadda8f6b686c3cee7258c6b06d3c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=83=A1=E5=AE=8F=E7=AC=8B?= Date: Tue, 16 Jul 2024 10:40:19 +0800 Subject: [PATCH 1/2] add npu roipoint_pool3d_forward --- .../pytorch/npu/roipoint_pool3d_forward.cpp | 38 +++++++++++++++++++ tests/test_ops/test_roipoint_pool3d.py | 11 ++++-- 2 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 mmcv/ops/csrc/pytorch/npu/roipoint_pool3d_forward.cpp diff --git a/mmcv/ops/csrc/pytorch/npu/roipoint_pool3d_forward.cpp b/mmcv/ops/csrc/pytorch/npu/roipoint_pool3d_forward.cpp new file mode 100644 index 0000000000..48d5b4789e --- /dev/null +++ b/mmcv/ops/csrc/pytorch/npu/roipoint_pool3d_forward.cpp @@ -0,0 +1,38 @@ +#include "pytorch_npu_helper.hpp" + +using namespace NPU_NAME_SPACE; +using namespace std; + +void roipoint_pool3d_forward_impl_npu(int batch_size, int pts_num, + int boxes_num, int feature_in_len, + int sampled_pts_num, const Tensor xyz, + const Tensor boxes3d, + const Tensor pts_feature, + Tensor pooled_features, + Tensor pooled_empty_flag) { + auto points_trans = xyz.transpose(1, 2).contiguous(); + auto point_features_trans = pts_feature.transpose(1, 2).contiguous(); + c10::SmallVector features_trans_size = { + xyz.size(0), boxes3d.size(1), xyz.size(2) + pts_feature.size(2), + sampled_pts_num}; + at::Tensor pooled_features_trans = + at::empty(features_trans_size, xyz.options()); + c10::SmallVector empty_flag_size = {boxes3d.size(0), + boxes3d.size(1)}; + EXEC_NPU_CMD(aclnnRoipointPool3dForward, points_trans, point_features_trans, + boxes3d, sampled_pts_num, pooled_features_trans, + pooled_empty_flag); + auto pooled_features_cache = + pooled_features_trans.transpose(2, 3).contiguous(); + pooled_features.copy_(pooled_features_cache); +} + +void roipoint_pool3d_forward_impl(int batch_size, int pts_num, int boxes_num, + int feature_in_len, int sampled_pts_num, + const Tensor xyz, const Tensor boxes3d, + const Tensor pts_feature, + Tensor pooled_features, + Tensor pooled_empty_flag); + +REGISTER_NPU_IMPL(roipoint_pool3d_forward_impl, + roipoint_pool3d_forward_impl_npu); diff --git a/tests/test_ops/test_roipoint_pool3d.py b/tests/test_ops/test_roipoint_pool3d.py index 391a0bf3a4..f0ad5586a9 100644 --- a/tests/test_ops/test_roipoint_pool3d.py +++ b/tests/test_ops/test_roipoint_pool3d.py @@ -3,7 +3,7 @@ import torch from mmcv.ops import RoIPointPool3d -from mmcv.utils import IS_CUDA_AVAILABLE, IS_MLU_AVAILABLE +from mmcv.utils import IS_CUDA_AVAILABLE, IS_MLU_AVAILABLE, IS_NPU_AVAILABLE @pytest.mark.parametrize('device', [ @@ -14,14 +14,19 @@ 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, torch.half, pytest.param( torch.double, marks=pytest.mark.skipif( - IS_MLU_AVAILABLE, reason='MLU does not support for double')) + IS_MLU_AVAILABLE or IS_NPU_AVAILABLE, + reason='MLU and NPU does not support for double')) ]) def test_roipoint(device, dtype): points = torch.tensor( From 47dff6ff31ef664823e7aca440133c63af14cf26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=83=A1=E5=AE=8F=E7=AC=8B?= Date: Tue, 16 Jul 2024 19:10:56 +0800 Subject: [PATCH 2/2] nms_rotated npu add new attr is_angle --- mmcv/ops/csrc/pytorch/npu/nms_rotated_npu.cpp | 1 + mmcv/ops/nms.py | 4 ---- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/mmcv/ops/csrc/pytorch/npu/nms_rotated_npu.cpp b/mmcv/ops/csrc/pytorch/npu/nms_rotated_npu.cpp index b7bdd90b23..0f2318cfbe 100644 --- a/mmcv/ops/csrc/pytorch/npu/nms_rotated_npu.cpp +++ b/mmcv/ops/csrc/pytorch/npu/nms_rotated_npu.cpp @@ -26,6 +26,7 @@ Tensor nms_rotated_npu(const Tensor dets, const Tensor scores, .Output(selectedBox) .Output(selectedIndex) .Attr("iou_threshold", (float)iou_threshold) + .Attr("is_angle", false) .Run(); selectedIndex = selectedIndex.to(at::kLong); return selectedIndex; diff --git a/mmcv/ops/nms.py b/mmcv/ops/nms.py index 5115a95f62..5513c4049f 100644 --- a/mmcv/ops/nms.py +++ b/mmcv/ops/nms.py @@ -460,10 +460,6 @@ def nms_rotated(dets: Tensor, input_labels = labels if dets.device.type in ('npu', 'mlu'): order = scores.new_empty(0, dtype=torch.long) - if dets.device.type == 'npu': - coefficient = 57.29578 # 180 / PI - for i in range(dets.size()[0]): - dets_cw[i][4] *= coefficient # radians to angle keep_inds = ext_module.nms_rotated(dets_cw, scores, order, dets_cw, input_labels, iou_threshold, multi_label)