Skip to content

Commit 90d83c9

Browse files
authored
[NPU] 更新3个 box_iou 相关 NPU 算子适配层 (#3276)
* Update boxes_overlap_bev_npu.cpp 更换 boxes_overlap_bev_npu 的底层 aclnn 调用算子 * Update box_iou_rotated.py 更新 box_iou_rotated python 的 npu 相关适配层 * Update box_iou_rotated_npu.cpp Update box_iou_rotated_npu.cpp * Create diff_iou_rotated_npu.cpp diff_iou_rotated 算子添加 npu 适配 * Update box_iou_rotated_npu.cpp * Update diff_iou_rotated_npu.cpp * Update boxes_overlap_bev_npu.cpp * Update boxes_overlap_bev_npu.cpp * Update diff_iou_rotated_npu.cpp * Update boxes_overlap_bev_npu.cpp
1 parent f19d3e7 commit 90d83c9

File tree

4 files changed

+44
-44
lines changed

4 files changed

+44
-44
lines changed

mmcv/ops/box_iou_rotated.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,6 @@ def box_iou_rotated(bboxes1: torch.Tensor,
142142
flip_mat[-1] = -1
143143
bboxes1 = bboxes1 * flip_mat
144144
bboxes2 = bboxes2 * flip_mat
145-
if bboxes1.device.type == 'npu':
146-
scale_mat = bboxes1.new_ones(bboxes1.shape[-1])
147-
scale_mat[-1] = 1.0 / 0.01745329252
148-
bboxes1 = bboxes1 * scale_mat
149-
bboxes2 = bboxes2 * scale_mat
150145
bboxes1 = bboxes1.contiguous()
151146
bboxes2 = bboxes2.contiguous()
152147
ext_module.box_iou_rotated(

mmcv/ops/csrc/pytorch/npu/box_iou_rotated_npu.cpp

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,10 @@ void box_iou_rotated_impl(const Tensor boxes1, const Tensor boxes2, Tensor ious,
88

99
void box_iou_rotated_npu(const Tensor boxes1, const Tensor boxes2, Tensor ious,
1010
const int mode_flag, const bool aligned) {
11-
at::Tensor boxes = at::ones_like(boxes1);
12-
at::Tensor query_boxes = at::ones_like(boxes2);
13-
boxes = boxes1.transpose(0, 1).unsqueeze(0);
14-
query_boxes = boxes2.transpose(0, 1).unsqueeze(0);
15-
16-
bool is_trans = false;
17-
string modeStr = "iou";
18-
if (mode_flag == 1) {
19-
modeStr = "iof";
20-
}
21-
bool is_cross = true;
22-
if (aligned) {
23-
is_cross = false;
24-
}
25-
float v_threshold = 0;
26-
float e_threshold = 0;
27-
28-
OpCommand cmd;
29-
cmd.Name("RotatedIou")
30-
.Input(boxes)
31-
.Input(query_boxes)
32-
.Output(ious)
33-
.Attr("trans", is_trans)
34-
.Attr("mode", modeStr)
35-
.Attr("is_cross", is_cross)
36-
.Attr("v_threshold", v_threshold)
37-
.Attr("e_threshold", e_threshold)
38-
.Run();
39-
40-
if (is_cross) {
41-
ious = ious.view({boxes1.size(0), boxes2.size(0)});
42-
} else {
43-
ious = ious.view({boxes1.size(0), 1});
44-
}
11+
TORCH_CHECK(boxes1.size(1) == 5, "boxes1 must be 2D tensor (N, 5)");
12+
TORCH_CHECK(boxes1.size(1) == 5, "boxes1 must be 2D tensor (N, 5)");
13+
EXEC_NPU_CMD(aclnnBoxIou, boxes1, boxes2, mode_flag, aligned, ious);
14+
return;
4515
}
4616

4717
REGISTER_NPU_IMPL(box_iou_rotated_impl, box_iou_rotated_npu);

mmcv/ops/csrc/pytorch/npu/boxes_overlap_bev_npu.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
using namespace NPU_NAME_SPACE;
44
using namespace std;
55

6+
namespace {
7+
constexpr int32_t MODE_FLAG_OVERLAP = 0;
8+
constexpr int32_t FORMAT_FLAG_XYZWHDR = 3;
9+
}; // namespace
10+
611
void iou3d_boxes_overlap_bev_forward_impl(const int num_a, const Tensor boxes_a,
712
const int num_b, const Tensor boxes_b,
813
Tensor ans_overlap);
@@ -13,13 +18,13 @@ void iou3d_boxes_overlap_bev_forward_npu(const int num_a, const Tensor boxes_a,
1318
TORCH_CHECK(boxes_a.size(1) == 7, "boxes_a must be 2D tensor (N, 7)");
1419
TORCH_CHECK(boxes_b.size(1) == 7, "boxes_b must be 2D tensor (N, 7)");
1520

16-
auto format_flag = 3;
1721
auto clockwise = true;
18-
auto mode_flag = 0;
19-
auto aligned = false;
20-
auto margin = 1e-2;
22+
bool aligned = false;
23+
double margin = 1e-5;
24+
int32_t mode_flag = MODE_FLAG_OVERLAP;
25+
int32_t format_flag = FORMAT_FLAG_XYZWHDR;
2126

22-
EXEC_NPU_CMD(aclnnBoxesOverlapBev, boxes_a, boxes_b, format_flag, clockwise,
27+
EXEC_NPU_CMD(aclnnBoxesOverlapBevV1, boxes_a, boxes_b, format_flag, clockwise,
2328
mode_flag, aligned, margin, ans_overlap);
2429
return;
2530
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include "pytorch_npu_helper.hpp"
2+
using namespace NPU_NAME_SPACE;
3+
using namespace std;
4+
5+
Tensor diff_iou_rotated_sort_vertices_npu(Tensor vertices, Tensor mask,
6+
Tensor num_valid) {
7+
TORCH_CHECK(vertices.dim() == 4,
8+
"vertices must be a 4D Tensor, but got: ", vertices.dim());
9+
TORCH_CHECK(mask.dim() == 3,
10+
"mask must be a 3D Tensor, but got: ", mask.dim());
11+
TORCH_CHECK(num_valid.dim() == 2,
12+
"num_valid must be a 2D Tensor, but got: ", num_valid.dim());
13+
14+
uint32_t B = vertices.size(0);
15+
uint32_t N = vertices.size(1);
16+
17+
at::Tensor sortedIdx = at::empty({B, N, 9}, num_valid.options());
18+
at::Tensor mask_fp = mask.to(at::kFloat);
19+
20+
EXEC_NPU_CMD(aclnnDiffIouRotatedSortVertices, vertices, mask_fp, num_valid,
21+
sortedIdx);
22+
23+
return sortedIdx;
24+
}
25+
26+
Tensor diff_iou_rotated_sort_vertices_forward_impl(Tensor vertices, Tensor mask,
27+
Tensor num_valid);
28+
29+
REGISTER_NPU_IMPL(diff_iou_rotated_sort_vertices_forward_impl,
30+
diff_iou_rotated_sort_vertices_npu);

0 commit comments

Comments
 (0)