knn.cpp 721 Bytes
Newer Older
1
#include <torch/extension.h>
rusty1s's avatar
rusty1s committed
2
3
4
5
6

#define CHECK_CUDA(x) AT_ASSERTM(x.type().is_cuda(), #x " must be CUDA tensor")
#define IS_CONTIGUOUS(x) AT_ASSERTM(x.is_contiguous(), #x " is not contiguous");

at::Tensor knn_cuda(at::Tensor x, at::Tensor y, size_t k, at::Tensor batch_x,
7
                    at::Tensor batch_y, bool cosine);
rusty1s's avatar
rusty1s committed
8
9

at::Tensor knn(at::Tensor x, at::Tensor y, size_t k, at::Tensor batch_x,
10
               at::Tensor batch_y, bool cosine) {
rusty1s's avatar
rusty1s committed
11
12
13
14
15
16
  CHECK_CUDA(x);
  IS_CONTIGUOUS(x);
  CHECK_CUDA(y);
  IS_CONTIGUOUS(y);
  CHECK_CUDA(batch_x);
  CHECK_CUDA(batch_y);
17
  return knn_cuda(x, y, k, batch_x, batch_y, cosine);
rusty1s's avatar
rusty1s committed
18
19
20
21
22
}

PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
  m.def("knn", &knn, "k-Nearest Neighbor (CUDA)");
}