-
Notifications
You must be signed in to change notification settings - Fork 0
/
matmul_cuda.cpp
52 lines (42 loc) · 1.33 KB
/
matmul_cuda.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
#include <torch/extension.h>
#include <vector>
// CUDA forward declarations
std::vector<torch::Tensor> matmul_cuda_forward(
torch::Tensor a,
torch::Tensor b,
int mode);
std::vector<torch::Tensor> matmul_cuda_backward(
torch::Tensor a,
torch::Tensor b,
torch::Tensor grad_output,
torch::Tensor part,
int mode);
// C++ interface
// NOTE: AT_ASSERT has become AT_CHECK on master after 0.4.
#define CHECK_CUDA(x) TORCH_CHECK(x.type().is_cuda(), #x " must be a CUDA tensor")
#define CHECK_CONTIGUOUS(x) TORCH_CHECK(x.is_contiguous(), #x " must be contiguous")
#define CHECK_INPUT(x) CHECK_CUDA(x); CHECK_CONTIGUOUS(x)
std::vector<torch::Tensor> matmul_forward(
torch::Tensor a,
torch::Tensor b,
int mode) {
CHECK_INPUT(a);
CHECK_INPUT(b);
return matmul_cuda_forward(a, b, mode);
}
std::vector<torch::Tensor> matmul_backward(
torch::Tensor a,
torch::Tensor b,
torch::Tensor grad_output,
torch::Tensor part,
int mode) {
CHECK_INPUT(a);
CHECK_INPUT(b);
CHECK_INPUT(grad_output);
CHECK_INPUT(part);
return matmul_cuda_backward(a, b, grad_output, part, mode);
}
PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
m.def("forward", &matmul_forward, "Log-Matmul forward (CUDA)");
m.def("backward", &matmul_backward, "Log-Matmul backward (CUDA)");
}