-
Notifications
You must be signed in to change notification settings - Fork 13
/
matmul_cuda.cpp
172 lines (149 loc) · 4.37 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#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,
torch::Tensor maxes,
int mode);
std::vector<torch::Tensor> matmul_cuda_backbackward(
torch::Tensor a,
torch::Tensor b,
torch::Tensor grad_output,
torch::Tensor part,
torch::Tensor maxes,
torch::Tensor grad_out_a,
int mode);
std::vector<torch::Tensor> banded_cuda_forward(
torch::Tensor a,
int a_lu,
int a_lb,
torch::Tensor b,
int b_lu,
int b_lb,
int mode);
std::vector<torch::Tensor> banded_cuda_backward(
torch::Tensor a,
int a_lu,
int a_lb,
torch::Tensor b,
int b_lu,
int b_lb,
torch::Tensor grad_output,
torch::Tensor part,
int mode);
std::vector<torch::Tensor> banded_cuda_backbackward(
torch::Tensor a,
int a_lu,
int a_lb,
torch::Tensor b,
int b_lu,
int b_lb,
torch::Tensor grad_output,
torch::Tensor part,
torch::Tensor maxes,
torch::Tensor grad_out_a,
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,
torch::Tensor maxes,
int mode) {
CHECK_INPUT(a);
CHECK_INPUT(b);
CHECK_INPUT(grad_output);
CHECK_INPUT(part);
CHECK_INPUT(maxes);
return matmul_cuda_backward(a, b, grad_output, part, maxes, mode);
}
std::vector<torch::Tensor> matmul_backbackward(
torch::Tensor a,
torch::Tensor b,
torch::Tensor grad_output,
torch::Tensor part,
torch::Tensor maxes,
torch::Tensor grad_out_a,
int mode) {
CHECK_INPUT(a);
CHECK_INPUT(b);
CHECK_INPUT(grad_output);
CHECK_INPUT(part);
CHECK_INPUT(maxes);
CHECK_INPUT(grad_out_a);
return matmul_cuda_backbackward(a, b, grad_output, part, maxes, grad_out_a, mode);
}
std::vector<torch::Tensor> banded_forward(
torch::Tensor a,
int a_lu,
int a_lb,
torch::Tensor b,
int b_lu,
int b_lb,
int mode) {
CHECK_INPUT(a);
CHECK_INPUT(b);
return banded_cuda_forward(a, a_lu, a_lb, b, b_lu, b_lb, mode);
}
std::vector<torch::Tensor> banded_backward(
torch::Tensor a,
int a_lu,
int a_lb,
torch::Tensor b,
int b_lu,
int b_lb,
torch::Tensor grad_output,
torch::Tensor part,
int mode) {
CHECK_INPUT(a);
CHECK_INPUT(b);
CHECK_INPUT(grad_output);
CHECK_INPUT(part);
return banded_cuda_backward(a, a_lu, a_lb,
b, b_lu, b_lb,
grad_output, part, mode);
}
std::vector<torch::Tensor> banded_backbackward(
torch::Tensor a,
int a_lu,
int a_lb,
torch::Tensor b,
int b_lu,
int b_lb,
torch::Tensor grad_output,
torch::Tensor part,
torch::Tensor maxes,
torch::Tensor grad_out_a,
int mode) {
return banded_cuda_backbackward(a, a_lu, a_lb,
b, b_lu, b_lb,
grad_output, part, maxes, grad_out_a, 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)");
m.def("backbackward", &matmul_backbackward, "Log-Matmul backbackward (CUDA)");
m.def("forward_band", &banded_forward, "Banded Log-Matmul forward (CUDA)");
m.def("backward_band", &banded_backward, "Banded Log-Matmul backward (CUDA)");
m.def("backbackward_band", &banded_backbackward, "Banded Log-Matmul backbackward (CUDA)");
}