-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature] Support knn gpu op #360
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .knn import knn | ||
|
||
__all__ = ['knn'] |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import torch | ||
from torch.autograd import Function | ||
|
||
from . import knn_ext | ||
|
||
|
||
class KNN(Function): | ||
"""KNN (CUDA). | ||
|
||
Find k-nearest points. | ||
""" | ||
|
||
@staticmethod | ||
def forward(ctx, | ||
k: int, | ||
xyz: torch.Tensor, | ||
center_xyz: torch.Tensor, | ||
transposed: bool = False) -> torch.Tensor: | ||
"""forward. | ||
|
||
Args: | ||
k (int): number of nearest neighbors. | ||
xyz (Tensor): (B, N, 3) if transposed == False, else (B, 3, N). | ||
xyz coordinates of the features. | ||
center_xyz (Tensor): (B, npoint, 3) if transposed == False, | ||
else (B, 3, npoint). centers of the knn query. | ||
transposed (bool): whether the input tensors are transposed. | ||
defaults to False. | ||
|
||
Returns: | ||
Tensor: (B, k, npoint) tensor with the indicies of | ||
the features that form k-nearest neighbours. | ||
""" | ||
assert k > 0 | ||
|
||
B, npoint = center_xyz.shape[:2] | ||
N = xyz.shape[1] | ||
|
||
if not transposed: | ||
xyz = xyz.transpose(2, 1).contiguous() | ||
center_xyz = center_xyz.transpose(2, 1).contiguous() | ||
|
||
assert center_xyz.is_contiguous() | ||
assert xyz.is_contiguous() | ||
|
||
center_xyz_device = center_xyz.get_device() | ||
assert center_xyz_device == xyz.get_device(), \ | ||
'center_xyz and xyz should be put on the same device' | ||
if torch.cuda.current_device() != center_xyz_device: | ||
torch.cuda.set_device(center_xyz_device) | ||
|
||
idx = center_xyz.new_zeros((B, k, npoint)).long() | ||
|
||
for bi in range(B): | ||
knn_ext.knn_wrapper(xyz[bi], N, center_xyz[bi], npoint, idx[bi], k) | ||
|
||
ctx.mark_non_differentiable(idx) | ||
|
||
idx -= 1 | ||
|
||
return idx | ||
|
||
@staticmethod | ||
def backward(ctx, a=None): | ||
return None, None | ||
|
||
|
||
knn = KNN.apply |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Modified from https://github.com/unlimblue/KNN_CUDA | ||
|
||
#include <vector> | ||
#include <torch/extension.h> | ||
#include <ATen/cuda/CUDAContext.h> | ||
|
||
#define CHECK_CONTIGUOUS(x) AT_ASSERTM(x.is_contiguous(), #x " must be contiguous") | ||
#define CHECK_TYPE(x, t) AT_ASSERTM(x.dtype() == t, #x " must be " #t) | ||
#define CHECK_CUDA(x) AT_ASSERTM(x.device().type() == at::Device::Type::CUDA, #x " must be on CUDA") | ||
#define CHECK_INPUT(x, t) CHECK_CONTIGUOUS(x); CHECK_TYPE(x, t); CHECK_CUDA(x) | ||
|
||
|
||
void knn_kernels_launcher( | ||
const float* ref_dev, | ||
int ref_nb, | ||
const float* query_dev, | ||
int query_nb, | ||
int dim, | ||
int k, | ||
float* dist_dev, | ||
long* ind_dev, | ||
cudaStream_t stream | ||
); | ||
|
||
// std::vector<at::Tensor> knn_wrapper( | ||
void knn_wrapper( | ||
at::Tensor & ref, | ||
int ref_nb, | ||
at::Tensor & query, | ||
int query_nb, | ||
at::Tensor & ind, | ||
const int k | ||
) { | ||
|
||
CHECK_INPUT(ref, at::kFloat); | ||
CHECK_INPUT(query, at::kFloat); | ||
const float * ref_dev = ref.data_ptr<float>(); | ||
const float * query_dev = query.data_ptr<float>(); | ||
int dim = query.size(0); | ||
auto dist = at::empty({ref_nb, query_nb}, query.options().dtype(at::kFloat)); | ||
float * dist_dev = dist.data_ptr<float>(); | ||
long * ind_dev = ind.data_ptr<long>(); | ||
|
||
cudaStream_t stream = at::cuda::getCurrentCUDAStream(); | ||
|
||
knn_kernels_launcher( | ||
ref_dev, | ||
ref_nb, | ||
query_dev, | ||
query_nb, | ||
dim, | ||
k, | ||
dist_dev, | ||
ind_dev, | ||
stream | ||
); | ||
} | ||
|
||
|
||
PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) { | ||
m.def("knn_wrapper", &knn_wrapper, "knn_wrapper"); | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to ref the original implementation if necessary.