`torchvision.ops.nms` CUDA kernel narrows `double iou_threshold` to `float` in `devIoU`, causing CPU/CUDA divergence on `float64` boxes

Open Beginner friendly
#9,777 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
84/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python, pytorch

Research direction

Read torchvision/csrc/ops/cuda/nms_kernel.cu around devIoU and compare its threshold parameter with the CPU comparison in torchvision/csrc/ops/cpu/nms_kernel.cpp. Reproduce the reported float64 CPU/CUDA mismatch using the supplied script, then verify CUDA matches the expected CPU results for thresholds 0.2, 0.3, and 0.7.

Written by the indexing model from the issue text.

Description

🐛 Describe the bug

In torchvision/csrc/ops/cuda/nms_kernel.cu, nms_kernel, launch_nms_kernel_impl, and nms_kernel_impl all pass double iou_threshold, and devIoU computes the intersection and union areas in acc_T (double for torch.float64 inputs). However, devIoU narrows its third parameter to const float threshold:

https://github.com/pytorch/vision/blob/4174d08e42fb04453a373a1208bb086913347adf/torchvision/csrc/ops/cuda/nms_kernel.cu#L40-L60

template <typename T>
__device__ inline bool devIoU(
    T const* const a,
    T const* const b,
    const float threshold) {          // <-- narrows `double iou_threshold` to `float`
  ...
  using acc_T = typename AccType<T>::type;
  acc_T interS = (acc_T)width * height;
  acc_T Sa = ((acc_T)a[2] - a[0]) * (a[3] - a[1]);
  acc_T Sb = ((acc_T)b[2] - b[0]) * (b[3] - b[1]);
  return (interS / (Sa + Sb - interS)) > threshold;
}

By contrast, the CPU implementation in torchvision/csrc/ops/cpu/nms_kernel.cpp#L88 compares directly against double iou_threshold (if (ovr > iou_threshold)).

Because common decimal thresholds such as 0.2, 0.3, and 0.7 are not exact in binary floating point, narrowing double to float shifts the threshold in either direction:

  • float(0.2) = 0.20000000298023224 > 0.2 → CUDA fails to suppress boxes with 0.2 < IoU <= float(0.2).
  • float(0.3) = 0.30000001192092896 > 0.3 → CUDA fails to suppress boxes with 0.3 < IoU <= float(0.3).
  • float(0.7) = 0.699999988079071 < 0.7 → CUDA falsely suppresses boxes with float(0.7) < IoU <= 0.7.

(Distinct from #7165, which was about float32 precision differences between box_iou and nms; here boxes and scores are torch.float64 and nms itself disagrees between CPU and CUDA.)

Minimal reproduction
import torch
from torchvision.ops import nms

scores = torch.tensor([0.9, 0.8], dtype=torch.float64)

for thr, target_iou in [(0.2, 0.2000000015), (0.3, 0.300000006), (0.7, 0.699999994)]:
    x = (1.0 - target_iou) / (1.0 + target_iou)
    boxes = torch.tensor([[0.0, 0.0, 1.0, 1.0], [x, 0.0, x + 1.0, 1.0]], dtype=torch.float64)
    iou = (1.0 - x) / (1.0 + x)
    keep_cpu = nms(boxes, scores, thr).tolist()
    keep_gpu = nms(boxes.cuda(), scores.cuda(), thr).cpu().tolist()
    print(f"thr={thr} iou={iou:.17g} | CPU={keep_cpu} CUDA={keep_gpu}")
Observed vs. expected output
thr=0.2 iou=0.20000000149999997 | CPU=[0]    CUDA=[0, 1]
thr=0.3 iou=0.30000000599999993 | CPU=[0]    CUDA=[0, 1]
thr=0.7 iou=0.6999999940000001  | CPU=[0, 1] CUDA=[0]

Expected: CUDA should match CPU ([0], [0], [0, 1]). Changing const float threshold to const double threshold in devIoU (torchvision/csrc/ops/cuda/nms_kernel.cu:44) aligns CUDA with nms_kernel_impl and cpu/nms_kernel.cpp.

Versions
  • PyTorch: 2.15.0+cu132
  • torchvision: 0.29.2+cu132 (also present on main at 4174d08e42fb04453a373a1208bb086913347adf)
  • Python: 3.12.3
  • OS: Linux (x86_64)
  • GPU: NVIDIA L40 (sm_89, 46068 MiB)
  • NVIDIA Driver: 580.178.04
Dominant language
Python
Stars
18k
Forks
7.3k
Avg merge
1h
Merged PRs (30d)
3

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from pytorch/vision

All issues in pytorch/vision

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.