Open Ghelfi opened 3 weeks ago
Hi @Ghelfi , thanks a lot for the report, for the nice benchmarks and for the references to alternative implementations!
It'd be great if we could do the 'unwarp' part natively on the device as well. There doesn't seem to be any technical limitation. I probably won't be able to give it a go myself as I'm short on time lately, but I should be able to review a PR if you want to take a stab at it.
(For my own reference, there has been similar report sin the past: https://github.com/pytorch/vision/issues/6403, https://github.com/pytorch/vision/issues/7412)
🐛 Describe the bug
Description
torchvision.ops.boxes.batched_nms
on CUDA GPU slows down considerably when then number of bounding boxes involved increases.The slow down is associated with Device -> Host transfer, and is linked to the iterative part of the Non Maximum Suppression (NMS) algorithm. In a nutshell the IoU map is computed on the device, then the mask is copied to the CPU to perform the iterative unwrap, which result is copied back to the device (from here and below).
The mask size grows quadratically with the number of input bounding boxes and we see a large TX rate when running on 30_000+ boxes.
In comparison the OpenLabs mmcv solution does the same thing for the IoU map but runs a custom kernel to do the unwrap directly on the device. The implemented kernel is not very efficient compute wise but save the data transfer cost, which is the main bottleneck.
I benchmarked
torchvision
batched_nms againstmmcv
's onV100
andA100
GPUs. Both figures show the speed factor when comparing a solution totorchvision.ops.boxes._batched_nms_vanilla
(there is 2 nms in torchvision, selected based on the number of elements. Here ,torchvision.ops.boxes._batched_nms_vanilla
is used a base comparison and we comparetorchvision.ops.boxes._batched_nms_coordinate_trick
andmmcv
batched_nms). From 30k boxes and abovemmcv
NMS is x20+ faster.Is there a reason why we keep this GPU -> CPU transfer ? Could we improve the scalability by having a similar on-device additional kernel ?
Additional informations
torch.utils.benchmark.Timer
on 100 examples for each NMS.Versions