[Features] Add NMS Kernel support with Triton Implementation#8746
Open
Stonepia wants to merge 9 commits intopytorch:mainfrom
Open
[Features] Add NMS Kernel support with Triton Implementation#8746Stonepia wants to merge 9 commits intopytorch:mainfrom
Stonepia wants to merge 9 commits intopytorch:mainfrom
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/vision/8746
Note: Links to docs will display an error until the docs builds have been completed. This comment was automatically generated by Dr. CI and updates every 15 minutes. |
torchvision/ops/xpu/nms.py
Outdated
| picked.append(order[i]) | ||
| remove_box[i:] |= iou_keep_out_mask[i][i:] | ||
|
|
||
| return torch.as_tensor(picked) |
There was a problem hiding this comment.
should this also respect the device of the boxes? (remove_boxes is allocated on boxes.device, while the return value - always on CPU)
Author
There was a problem hiding this comment.
Thanks for the reminder~! Yes this should be on boxes.device. I will update it.
Author
6 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Motivation
This PR follows RFC #8679 which proposes to add torchvision custom op support with Triton kernels.
Implementing Method
The Triton kernel mapping basically follows the CUDA kernels. As is shown below, the native CUDA kernel will be mapped into the Triton kernel. Some logic could not be run in parallel, thus they will be implemented with Python as well as C++ Ops.
This PR contains the following parts:
torchvision/ops/triton/. This contains the common logic that could be implemented in Triton.torchvision/ops/xpu. This will do op registration and combine non-Triton ops with Triton kernels into one big op.Kernel Implementing Structure
The NMS kernel contains three parts, please see
torchvision/ops/xpu/nms.pyfor details. It wraps the three parts:argsortwhich are called using PyTorch ATen ops.torchvision/ops/triton/nms.py. It is a device-agnostic part, which could be shared across devices.Kernel Implementing Detail
box jif we have already chosenbox i. A naive implementation will have a matrix with[N, N]. However, as the performance consideration, it will try to combine the "bit mask" into 32-bits ints. Thus, the output will be[N, N//32].row i, this means we choose thebox i. As a result, some boxesjwill be excluded. That's what the post-process function does. To make it more device-agnostic, we choose to do this serialized process on the CPU.cc: @EikanWang