|
| 1 | +#include <ATen/ATen.h> |
| 2 | +#include <ATen/native/quantized/affine_quantizer.h> |
| 3 | +#include <torch/library.h> |
| 4 | + |
| 5 | +namespace vision { |
| 6 | +namespace ops { |
| 7 | + |
| 8 | +namespace { |
| 9 | + |
| 10 | +template <typename scalar_t> |
| 11 | +at::Tensor qnms_kernel_impl( |
| 12 | + const at::Tensor& dets, |
| 13 | + const at::Tensor& scores, |
| 14 | + double iou_threshold) { |
| 15 | + TORCH_CHECK(!dets.is_cuda(), "dets must be a CPU tensor"); |
| 16 | + TORCH_CHECK(!scores.is_cuda(), "scores must be a CPU tensor"); |
| 17 | + TORCH_CHECK( |
| 18 | + dets.scalar_type() == scores.scalar_type(), |
| 19 | + "dets should have the same type as scores"); |
| 20 | + |
| 21 | + if (dets.numel() == 0) |
| 22 | + return at::empty({0}, dets.options().dtype(at::kLong)); |
| 23 | + |
| 24 | + const auto ndets = dets.size(0); |
| 25 | + |
| 26 | + auto x1_t = dets.select(1, 0).contiguous(); |
| 27 | + auto y1_t = dets.select(1, 1).contiguous(); |
| 28 | + auto x2_t = dets.select(1, 2).contiguous(); |
| 29 | + auto y2_t = dets.select(1, 3).contiguous(); |
| 30 | + auto order_t = std::get<1>(scores.sort(0, /* descending=*/true)); |
| 31 | + at::Tensor suppressed_t = at::zeros({ndets}, dets.options().dtype(at::kByte)); |
| 32 | + at::Tensor keep_t = at::zeros({ndets}, dets.options().dtype(at::kLong)); |
| 33 | + at::Tensor areas_t = at::zeros({ndets}, dets.options().dtype(at::kFloat)); |
| 34 | + |
| 35 | + auto suppressed = suppressed_t.data_ptr<uint8_t>(); |
| 36 | + auto keep = keep_t.data_ptr<int64_t>(); |
| 37 | + auto order = order_t.data_ptr<int64_t>(); |
| 38 | + auto x1 = x1_t.data_ptr<scalar_t>(); |
| 39 | + auto y1 = y1_t.data_ptr<scalar_t>(); |
| 40 | + auto x2 = x2_t.data_ptr<scalar_t>(); |
| 41 | + auto y2 = y2_t.data_ptr<scalar_t>(); |
| 42 | + auto areas = areas_t.data_ptr<float>(); |
| 43 | + |
| 44 | + for (int64_t i = 0; i < ndets; i++) { |
| 45 | + // Note 1: To get the exact area we'd need to multiply by scale**2, but this |
| 46 | + // would get canceled out in the computation of ovr below. So we leave that |
| 47 | + // out. |
| 48 | + // Note 2: degenerate boxes (x2 < x1 or y2 < y1) may underflow, although |
| 49 | + // integral promotion rules will likely prevent it (see |
| 50 | + // https://stackoverflow.com/questions/32959564/subtraction-of-two-unsigned-gives-signed |
| 51 | + // for more details). |
| 52 | + areas[i] = (x2[i].val_ - x1[i].val_) * (y2[i].val_ - y1[i].val_); |
| 53 | + } |
| 54 | + |
| 55 | + int64_t num_to_keep = 0; |
| 56 | + |
| 57 | + for (int64_t _i = 0; _i < ndets; _i++) { |
| 58 | + auto i = order[_i]; |
| 59 | + if (suppressed[i] == 1) |
| 60 | + continue; |
| 61 | + keep[num_to_keep++] = i; |
| 62 | + |
| 63 | + // We explicitely cast coordinates to float so that the code can be |
| 64 | + // vectorized. |
| 65 | + float ix1val = x1[i].val_; |
| 66 | + float iy1val = y1[i].val_; |
| 67 | + float ix2val = x2[i].val_; |
| 68 | + float iy2val = y2[i].val_; |
| 69 | + float iarea = areas[i]; |
| 70 | + |
| 71 | + for (int64_t _j = _i + 1; _j < ndets; _j++) { |
| 72 | + auto j = order[_j]; |
| 73 | + if (suppressed[j] == 1) |
| 74 | + continue; |
| 75 | + float xx1 = std::max(ix1val, (float)x1[j].val_); |
| 76 | + float yy1 = std::max(iy1val, (float)y1[j].val_); |
| 77 | + float xx2 = std::min(ix2val, (float)x2[j].val_); |
| 78 | + float yy2 = std::min(iy2val, (float)y2[j].val_); |
| 79 | + |
| 80 | + auto w = std::max(0.f, xx2 - xx1); // * scale (gets canceled below) |
| 81 | + auto h = std::max(0.f, yy2 - yy1); // * scale (gets canceled below) |
| 82 | + auto inter = w * h; |
| 83 | + auto ovr = inter / (iarea + areas[j] - inter); |
| 84 | + if (ovr > iou_threshold) |
| 85 | + suppressed[j] = 1; |
| 86 | + } |
| 87 | + } |
| 88 | + return keep_t.narrow(/*dim=*/0, /*start=*/0, /*length=*/num_to_keep); |
| 89 | +} |
| 90 | + |
| 91 | +at::Tensor qnms_kernel( |
| 92 | + const at::Tensor& dets, |
| 93 | + const at::Tensor& scores, |
| 94 | + double iou_threshold) { |
| 95 | + TORCH_CHECK( |
| 96 | + dets.dim() == 2, "boxes should be a 2d tensor, got ", dets.dim(), "D"); |
| 97 | + TORCH_CHECK( |
| 98 | + dets.size(1) == 4, |
| 99 | + "boxes should have 4 elements in dimension 1, got ", |
| 100 | + dets.size(1)); |
| 101 | + TORCH_CHECK( |
| 102 | + scores.dim() == 1, |
| 103 | + "scores should be a 1d tensor, got ", |
| 104 | + scores.dim(), |
| 105 | + "D"); |
| 106 | + TORCH_CHECK( |
| 107 | + dets.size(0) == scores.size(0), |
| 108 | + "boxes and scores should have same number of elements in ", |
| 109 | + "dimension 0, got ", |
| 110 | + dets.size(0), |
| 111 | + " and ", |
| 112 | + scores.size(0)); |
| 113 | + |
| 114 | + auto result = at::empty({0}); |
| 115 | + |
| 116 | + AT_DISPATCH_QINT_TYPES(dets.scalar_type(), "qnms_kernel", [&] { |
| 117 | + result = qnms_kernel_impl<scalar_t>(dets, scores, iou_threshold); |
| 118 | + }); |
| 119 | + return result; |
| 120 | +} |
| 121 | + |
| 122 | +} // namespace |
| 123 | + |
| 124 | +TORCH_LIBRARY_IMPL(torchvision, QuantizedCPU, m) { |
| 125 | + m.impl(TORCH_SELECTIVE_NAME("torchvision::nms"), TORCH_FN(qnms_kernel)); |
| 126 | +} |
| 127 | + |
| 128 | +} // namespace ops |
| 129 | +} // namespace vision |
0 commit comments