`torchvision.ops.nms` CUDA kernel narrows `double iou_threshold` to `float` in `devIoU`, causing CPU/CUDA divergence on `float64` boxes
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
- Domain
- computer-vision
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:
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 with0.2 < IoU <= float(0.2).float(0.3) = 0.30000001192092896 > 0.3→ CUDA fails to suppress boxes with0.3 < IoU <= float(0.3).float(0.7) = 0.699999988079071 < 0.7→ CUDA falsely suppresses boxes withfloat(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
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from pytorch/vision
-
[detection] Faster R-CNN accepts negative box_detections_per_img and applies Python slicingPossibly taken @hparik11 claimed this 6 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
pytorch/vision#9761 · 2 comments ·
Maintainers usually reply within 1 day
-
sigmoid_focal_loss gives NaN gradients for confident predictions when gamma < 1Possibly taken @yuvraj-del claimed this 21 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
pytorch/vision#9743 · 2 comments ·
Maintainers usually reply within 1 day
-
vision_stable.cpp gets hipified into a duplicate copy under ROCm, causing "multiple definition of vision::cuda_version()"Possibly taken A pull request linked to this issue is open or already merged. Openmodule: rocm
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
Clip samplers crash on iteration when no video clips are availablePossibly taken @raunakpandey1 claimed this 29 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Maintainers usually reply within 1 day
-
complete_box_iou_loss / complete_box_iou: NaN gradients for zero-height boxes, NaN for point boxesPossibly taken @aounullah claimed this 21 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
pytorch/vision#9730 · 3 comments ·
Maintainers usually reply within 1 day
Similar issues
-
Difficulty 1/5 Under an hour Newbie friendliness 69/100
anderspitman/awesome-tunneling#330 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 85/100
521xueweihan/HelloGitHub#4676 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 67/100
Maintainers usually reply within 18 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
pandas-dev/pandas-stubs#2155 ·
Maintainers usually reply within 1 day
-
essreduce
Difficulty 2/5 1-3 hours Newbie friendliness 69/100
Maintainers usually reply within 1 day