Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Support nms rotated op using diopi #2993

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions mmcv/ops/csrc/pytorch/nms_rotated.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@
// modified from
// https://github.com/facebookresearch/detectron2/blob/master/detectron2/layers/csrc/nms_rotated/nms_rotated.h
#include "pytorch_cpp_helper.hpp"
#include "pytorch_device_registry.hpp"
#ifdef MMCV_WITH_DIOPI
#include <diopi/diopirt.h>
#include <diopi/functions.h>
#include <diopi/functions_mmcv.h>

#include "csrc_dipu/base/basedef.h"
#include "csrc_dipu/diopirt/diopirt_impl.h"

using dipu::diopi_helper::toDiopiScalar;
using dipu::diopi_helper::toDiopiTensorHandle;
#endif

Tensor nms_rotated_cpu(const Tensor dets, const Tensor scores,
const float iou_threshold);
Expand All @@ -22,12 +34,55 @@ Tensor nms_rotated_mlu(const Tensor dets, const Tensor scores,
const float iou_threshold);
#endif

#ifdef MMCV_WITH_DIOPI
Tensor nms_rotated_diopi(const Tensor dets, const Tensor scores,
const Tensor order, const Tensor dets_sorted,
const Tensor labels, const float iou_threshold,
const int multi_label) {
auto dets_p = toDiopiTensorHandle(dets);
diopiDevice_t device;
diopiGetTensorDevice(dets_p, &device);
if (device == diopi_host) {
return nms_rotated_cpu(dets.contiguous(), scores.contiguous(),
iou_threshold);
}
diopiContext ctx(dipu::getCurrentDIPUStream().rawstream());
diopiContextHandle_t ch = &ctx;
Tensor out;
auto out_p = toDiopiTensorHandle(out);
diopiTensorHandle_t *out_handle = &out_p;
auto scores_p = toDiopiTensorHandle(scores);
auto order_p = toDiopiTensorHandle(order);
auto dets_sorted_p = toDiopiTensorHandle(dets_sorted);
auto labels_p = toDiopiTensorHandle(labels);
bool is_mock_cuda = dets.device().type() == dipu::DIPU_DEVICE_TYPE;
if (is_mock_cuda &&
reinterpret_cast<void *>(diopiNmsRotatedMmcv) != nullptr) {
auto ret = diopiNmsRotatedMmcv(ch, out_handle, dets_p, scores_p, order_p,
dets_sorted_p, labels_p, iou_threshold,
static_cast<bool>(multi_label));
if (ret == diopiSuccess) {
auto tensorhandle = reinterpret_cast<Tensor *>(*out_handle);
return *tensorhandle;
}
}
LOG(WARNING) << "Fallback to cpu: mmcv ext op nms_rotated";
auto dets_cpu = dets.cpu();
auto scores_cpu = scores.cpu();
return nms_rotated_cpu(dets_cpu, scores_cpu, iou_threshold);
}
#endif

// Interface for Python
// inline is needed to prevent multiple function definitions when this header is
// included by different cpps
Tensor nms_rotated(const Tensor dets, const Tensor scores, const Tensor order,
const Tensor dets_sorted, const Tensor labels,
const float iou_threshold, const int multi_label) {
#ifdef MMCV_WITH_DIOPI
return nms_rotated_diopi(dets, scores, order, dets_sorted, labels,
iou_threshold, multi_label);
#endif
assert(dets.device().is_cuda() == scores.device().is_cuda());
if (dets.device().is_cuda()) {
#ifdef MMCV_WITH_CUDA
Expand Down