forked from harvardnlp/genbmm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
matmul_cuda.cpp
221 lines (187 loc) · 5.66 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include <torch/extension.h>
#include <vector>
// CUDA forward declarations
std::vector<torch::Tensor> inside_cuda_forward(
torch::Tensor a,
int diag);
std::vector<torch::Tensor> inside_cuda_backward(
torch::Tensor a,
torch::Tensor grad_output,
torch::Tensor part,
int diag);
std::vector<torch::Tensor> inside_rule_cuda_forward(
torch::Tensor a,
torch::Tensor rule,
int diag);
std::vector<torch::Tensor> inside_rule_cuda_backward(
torch::Tensor a,
torch::Tensor rule,
torch::Tensor grad_output,
torch::Tensor part,
int diag);
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> inside_forward(
torch::Tensor a,
int diag) {
CHECK_INPUT(a);
return inside_cuda_forward(a, diag);
}
std::vector<torch::Tensor> inside_backward(
torch::Tensor a,
torch::Tensor grad_output,
torch::Tensor part,
int diag) {
CHECK_INPUT(a);
CHECK_INPUT(grad_output);
CHECK_INPUT(part);
return inside_cuda_backward(a, grad_output, part, diag);
}
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("forward_inside", &inside_forward, "Log-Matmul-Inside forward (CUDA)");
m.def("backward_inside", &inside_backward, "Log-Matmul-Inside backward (CUDA)");
m.def("forward_rule_inside", &inside_rule_cuda_forward, "Log-Matmul-Inside-Rule forward (CUDA)");
m.def("backward_rule_inside", &inside_rule_cuda_backward, "Log-Matmul-Inside-Rule backward (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)");
}