spspmm.cpp 2.4 KB
Newer Older
1
#include <torch/extension.h>
rusty1s's avatar
rusty1s committed
2

rusty1s's avatar
rusty1s committed
3
#define CHECK_CUDA(x) AT_ASSERTM(x.type().is_cuda(), #x " must be CUDA tensor")
rusty1s's avatar
rusty1s committed
4

rusty1s's avatar
rusty1s committed
5
6
7
8
9
std::tuple<at::Tensor, at::Tensor, at::optional<at::Tensor>>
spspmm_cuda(at::Tensor rowptrA, at::Tensor colA,
            at::optional<at::Tensor> valueA, at::Tensor rowptrB,
            at::Tensor colB, at::optional<at::Tensor> valueB, int M, int N,
            int K);
rusty1s's avatar
to csr  
rusty1s committed
10

rusty1s's avatar
rusty1s committed
11
12
13
14
15
16
17
18
19
20
21
22
23
std::tuple<at::Tensor, at::Tensor, at::optional<at::Tensor>>
spspmm(at::Tensor rowptrA, at::Tensor colA, at::optional<at::Tensor> valueA,
       at::Tensor rowptrB, at::Tensor colB, at::optional<at::Tensor> valueB,
       int M, int N, int K) {
  CHECK_CUDA(rowptrA);
  CHECK_CUDA(colA);
  if (valueA.has_value())
    CHECK_CUDA(valueA.value());
  CHECK_CUDA(rowptrB);
  CHECK_CUDA(colB);
  if (valueB.has_value())
    CHECK_CUDA(valueB.value());
  return spspmm_cuda(rowptrA, colA, valueA, rowptrB, colB, valueB, M, N, K);
rusty1s's avatar
rusty1s committed
24
25
}

rusty1s's avatar
rusty1s committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// std::tuple<at::Tensor, at::Tensor>
// spspmm_cuda(at::Tensor indexA, at::Tensor valueA, at::Tensor indexB,
//             at::Tensor valueB, size_t m, size_t k, size_t n);
// at::Tensor spspmm_bw_cuda(at::Tensor index, at::Tensor indexA,
//                           at::Tensor valueA, at::Tensor indexB,
//                           at::Tensor valueB, size_t rowA_max, size_t
//                           rowB_max);

// std::tuple<at::Tensor, at::Tensor> spspmm(at::Tensor indexA, at::Tensor
// valueA,
//                                           at::Tensor indexB, at::Tensor
//                                           valueB, size_t m, size_t k, size_t
//                                           n) {
//   CHECK_CUDA(indexA);
//   CHECK_CUDA(valueA);
//   CHECK_CUDA(indexB);
//   CHECK_CUDA(valueB);
//   return spspmm_cuda(indexA, valueA, indexB, valueB, m, k, n);
// }

// at::Tensor spspmm_bw(at::Tensor index, at::Tensor indexA, at::Tensor valueA,
//                      at::Tensor indexB, at::Tensor valueB, size_t rowA_max,
//                      size_t rowB_max) {
//   CHECK_CUDA(index);
//   CHECK_CUDA(indexA);
//   CHECK_CUDA(valueA);
//   CHECK_CUDA(indexB);
//   CHECK_CUDA(valueB);
//   return spspmm_bw_cuda(index, indexA, valueA, indexB, valueB, rowA_max,
//                         rowB_max);
// }
rusty1s's avatar
rusty1s committed
57

rusty1s's avatar
rusty1s committed
58
59
PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
  m.def("spspmm", &spspmm, "Sparse-Sparse Matrix Multiplication (CUDA)");
rusty1s's avatar
rusty1s committed
60
61
  // m.def("spspmm_bw", &spspmm_bw,
  //       "Sparse-Sparse Matrix Multiplication Backward (CUDA)");
rusty1s's avatar
rusty1s committed
62
}