-
Notifications
You must be signed in to change notification settings - Fork 0
/
matmul_cuda_kernel.cu
371 lines (324 loc) · 13.7 KB
/
matmul_cuda_kernel.cu
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#include <torch/extension.h>
#include <cuda.h>
#include <cuda_runtime.h>
#include <vector>
#include <iostream>
#include <curand.h>
#include <curand_kernel.h>
namespace {
template <typename scalar_t>
__global__ void matmul_cuda_forward_kernel(
const torch::PackedTensorAccessor32<scalar_t,3,torch::RestrictPtrTraits> a,
const torch::PackedTensorAccessor32<scalar_t,3,torch::RestrictPtrTraits> b,
torch::PackedTensorAccessor32<scalar_t,3,torch::RestrictPtrTraits> out,
const int in_size,
const int a_size,
const int b_size
) {
const int n = blockIdx.z;
const int row = threadIdx.x + blockIdx.x * blockDim.x;
const int col = threadIdx.y + blockIdx.y * blockDim.y;
scalar_t val = 0.0;
scalar_t m = -1e9;
if (row < a_size && col < b_size) {
for (int i = 0; i < in_size; ++i) {
scalar_t v = a[n][row][i] + b[n][i][col];
if (v > m) {
m = v;
}
}
for (int i = 0; i < in_size; ++i) {
scalar_t v = a[n][row][i] + b[n][i][col];
val += exp(v - m);
}
out[n][row][col] = log(val) + m;
}
}
template <typename scalar_t>
__global__ void max_cuda_forward_kernel(
const torch::PackedTensorAccessor32<scalar_t,3,torch::RestrictPtrTraits> a,
const torch::PackedTensorAccessor32<scalar_t,3,torch::RestrictPtrTraits> b,
torch::PackedTensorAccessor32<scalar_t,3,torch::RestrictPtrTraits> out,
torch::PackedTensorAccessor32<int,3,torch::RestrictPtrTraits> indices,
const int in_size,
const int a_size,
const int b_size
) {
const int n = blockIdx.z;
const int row = threadIdx.x + blockIdx.x * blockDim.x;
const int col = threadIdx.y + blockIdx.y * blockDim.y;
scalar_t val = 0.0;
scalar_t m = -1e9;
int ind = -1;
if (row < a_size && col < b_size) {
for (int i = 0; i < in_size; ++i) {
scalar_t v = a[n][row][i] + b[n][i][col];
if (v > m) {
m = v;
ind = i;
}
}
out[n][row][col] = m;
indices[n][row][col] = ind;
}
}
template <typename scalar_t>
__global__ void sample_cuda_forward_kernel(
const torch::PackedTensorAccessor32<scalar_t,3,torch::RestrictPtrTraits> a,
const torch::PackedTensorAccessor32<scalar_t,3,torch::RestrictPtrTraits> b,
const torch::PackedTensorAccessor32<scalar_t,3,torch::RestrictPtrTraits> rand,
torch::PackedTensorAccessor32<scalar_t,3,torch::RestrictPtrTraits> out,
torch::PackedTensorAccessor32<int,3,torch::RestrictPtrTraits> indices,
const int in_size,
const int a_size,
const int b_size
) {
const int n = blockIdx.z;
const int row = threadIdx.x + blockIdx.x * blockDim.x;
const int col = threadIdx.y + blockIdx.y * blockDim.y;
scalar_t val = 0.0;
scalar_t m = -1e9;
int ind = -1;
if (row < a_size && col < b_size) {
for (int i = 0; i < in_size; ++i) {
scalar_t v = a[n][row][i] + b[n][i][col];
if (v > m) {
m = v;
}
}
for (int i = 0; i < in_size; ++i) {
scalar_t v = a[n][row][i] + b[n][i][col];
val += exp(v - m);
}
out[n][row][col] = log(val) + m;
scalar_t total = 0.0;
auto r = rand[n][row][col];
for (int i = 0; i < in_size; ++i) {
scalar_t v = a[n][row][i] + b[n][i][col] - out[n][row][col];
if (total < r && total + exp(v) > r ){
indices[n][row][col] = i;
break;
}
total += exp(v);
}
}
}
// BACKWARD
// LOGSUM
template <typename scalar_t>
__global__ void matmul_cuda_backward_kernel_A(
torch::PackedTensorAccessor32<scalar_t,3,torch::RestrictPtrTraits> grad_a,
const torch::PackedTensorAccessor32<scalar_t,3,torch::RestrictPtrTraits> a,
const torch::PackedTensorAccessor32<scalar_t,3,torch::RestrictPtrTraits> b,
const torch::PackedTensorAccessor32<scalar_t,3,torch::RestrictPtrTraits> part,
const torch::PackedTensorAccessor32<scalar_t,3,torch::RestrictPtrTraits> grad_output,
const int in_size,
const int a_size,
const int b_size
) {
const int n = blockIdx.z;
const int row = threadIdx.x + blockIdx.x * blockDim.x;
const int col = threadIdx.y + blockIdx.y * blockDim.y;
if (row < a_size && col < in_size) {
scalar_t val = 0.0;
for (int k = 0; k < b_size; ++k) {
scalar_t v = a[n][row][col] + b[n][col][k] - part[n][row][k];
val += exp(v) * grad_output[n][row][k];
}
grad_a[n][row][col] = val;
}
}
template <typename scalar_t>
__global__ void matmul_cuda_backward_kernel_B(
torch::PackedTensorAccessor32<scalar_t,3,torch::RestrictPtrTraits> grad_b,
const torch::PackedTensorAccessor32<scalar_t,3,torch::RestrictPtrTraits> a,
const torch::PackedTensorAccessor32<scalar_t,3,torch::RestrictPtrTraits> b,
const torch::PackedTensorAccessor32<scalar_t,3,torch::RestrictPtrTraits> part,
const torch::PackedTensorAccessor32<scalar_t,3,torch::RestrictPtrTraits> grad_output,
const int in_size,
const int a_size,
const int b_size
) {
const int n = blockIdx.z;
const int row = threadIdx.x + blockIdx.x * blockDim.x;
const int col = threadIdx.y + blockIdx.y * blockDim.y;
if (row < in_size && col < b_size) {
scalar_t val = 0.0;
for (int k = 0; k < a_size; ++k) {
scalar_t v = a[n][k][row] + b[n][row][col] - part[n][k][col];
val += exp(v) * grad_output[n][k][col];
}
grad_b[n][row][col] = val;
}
}
// MAX
template <typename scalar_t>
__global__ void max_cuda_backward_kernel_A(
torch::PackedTensorAccessor32<scalar_t,3,torch::RestrictPtrTraits> grad_a,
const torch::PackedTensorAccessor32<scalar_t,3,torch::RestrictPtrTraits> a,
const torch::PackedTensorAccessor32<scalar_t,3,torch::RestrictPtrTraits> b,
const torch::PackedTensorAccessor32<scalar_t,3,torch::RestrictPtrTraits> part,
const torch::PackedTensorAccessor32<scalar_t,3,torch::RestrictPtrTraits> grad_output,
const int in_size,
const int a_size,
const int b_size
) {
const int n = blockIdx.z;
const int row = threadIdx.x + blockIdx.x * blockDim.x;
const int col = threadIdx.y + blockIdx.y * blockDim.y;
if (row < a_size && col < in_size) {
scalar_t val = 0.0;
for (int k = 0; k < b_size; ++k) {
scalar_t v = (col == part[n][row][k]) ? 1 : 0;
val += v * grad_output[n][row][k];
}
grad_a[n][row][col] = val;
}
}
template <typename scalar_t>
__global__ void max_cuda_backward_kernel_B(
torch::PackedTensorAccessor32<scalar_t,3,torch::RestrictPtrTraits> grad_b,
const torch::PackedTensorAccessor32<scalar_t,3,torch::RestrictPtrTraits> a,
const torch::PackedTensorAccessor32<scalar_t,3,torch::RestrictPtrTraits> b,
const torch::PackedTensorAccessor32<scalar_t,3,torch::RestrictPtrTraits> part,
const torch::PackedTensorAccessor32<scalar_t,3,torch::RestrictPtrTraits> grad_output,
const int in_size,
const int a_size,
const int b_size
) {
const int n = blockIdx.z;
const int row = threadIdx.x + blockIdx.x * blockDim.x;
const int col = threadIdx.y + blockIdx.y * blockDim.y;
if (row < in_size && col < b_size) {
scalar_t val = 0.0;
for (int k = 0; k < a_size; ++k) {
scalar_t v = (row == part[n][k][col]) ? 1 : 0;
val += v * grad_output[n][k][col];
}
grad_b[n][row][col] = val;
}
}
} // namespace
std::vector<torch::Tensor> matmul_cuda_forward(
torch::Tensor a,
torch::Tensor b,
int mode) {
const int batch_size = a.size(0);
const int a_size = a.size(1);
const int b_size = b.size(2);
auto options = torch::TensorOptions()
.dtype(a.dtype())
.device(torch::kCUDA, a.device().index());
auto out = torch::zeros({batch_size, a_size, b_size}, options);
const int in_size = a.size(2);
const int threads = 32;
const dim3 threads_per_block(threads, threads, 1);
const dim3 blocks(a_size / threads + 1,
b_size / threads + 1,
batch_size);
// Dispatch
if (mode == 0) {
AT_DISPATCH_FLOATING_TYPES(a.type(), "matmul_forward_cuda", ([&] {
matmul_cuda_forward_kernel<scalar_t><<<blocks, threads_per_block>>>(
a.packed_accessor32<scalar_t,3,torch::RestrictPtrTraits>(),
b.packed_accessor32<scalar_t,3,torch::RestrictPtrTraits>(),
out.packed_accessor32<scalar_t,3,torch::RestrictPtrTraits>(),
in_size, a_size, b_size);
} ) );
return {out};
} else if (mode == 1) {
auto options2 = torch::TensorOptions()
.dtype(torch::kInt)
.device(torch::kCUDA, a.device().index());
auto indices = torch::zeros({batch_size, a_size, b_size}, options2);
AT_DISPATCH_FLOATING_TYPES(a.type(), "matmul_forward_cuda", ([&] {
max_cuda_forward_kernel<scalar_t><<<blocks, threads_per_block>>>(
a.packed_accessor32<scalar_t,3,torch::RestrictPtrTraits>(),
b.packed_accessor32<scalar_t,3,torch::RestrictPtrTraits>(),
out.packed_accessor32<scalar_t,3,torch::RestrictPtrTraits>(),
indices.packed_accessor32<int,3,torch::RestrictPtrTraits>(),
in_size, a_size, b_size);
} ) );
return {out, indices};
} else if (mode == 2) {
auto options2 = torch::TensorOptions()
.dtype(torch::kInt)
.device(torch::kCUDA, a.device().index());
auto indices = torch::zeros({batch_size, a_size, b_size}, options2);
auto rand = torch::rand({batch_size, a_size, b_size}, options);
AT_DISPATCH_FLOATING_TYPES(a.type(), "matmul_forward_cuda", ([&] {
sample_cuda_forward_kernel<scalar_t><<<blocks, threads_per_block>>>(
a.packed_accessor32<scalar_t,3,torch::RestrictPtrTraits>(),
b.packed_accessor32<scalar_t,3,torch::RestrictPtrTraits>(),
rand.packed_accessor32<scalar_t,3,torch::RestrictPtrTraits>(),
out.packed_accessor32<scalar_t,3,torch::RestrictPtrTraits>(),
indices.packed_accessor32<int,3,torch::RestrictPtrTraits>(),
in_size, a_size, b_size);
} ) );
return {out, indices};
}
}
std::vector<torch::Tensor> matmul_cuda_backward(
torch::Tensor a,
torch::Tensor b,
torch::Tensor grad_out,
torch::Tensor part,
int mode) {
const auto batch_size = a.size(0);
const auto in_size = a.size(2);
const int a_size = a.size(1);
const int b_size = b.size(2);
const int threads = 32;
const dim3 blocks(a_size / threads + 1,
in_size / threads + 1,
batch_size);
const dim3 threads_per_block(threads, threads, 1);
auto grad_a = torch::zeros_like(a);
auto grad_b = torch::zeros_like(b);
auto grad_bp = grad_b.packed_accessor32<float,3,torch::RestrictPtrTraits>();
const int threads2 = 32;
const dim3 blocks2(in_size / threads2 + 1,
b_size / threads2 + 1,
batch_size);
if (mode == 0) {
AT_DISPATCH_FLOATING_TYPES(a.type(), "matmul_forward_cuda", ([&] {
matmul_cuda_backward_kernel_A<scalar_t><<<blocks, threads_per_block>>>(
grad_a.packed_accessor32<scalar_t,3,torch::RestrictPtrTraits>(),
a.packed_accessor32<scalar_t,3,torch::RestrictPtrTraits>(),
b.packed_accessor32<scalar_t,3,torch::RestrictPtrTraits>(),
part.packed_accessor32<scalar_t,3,torch::RestrictPtrTraits>(),
grad_out.packed_accessor32<scalar_t,3,torch::RestrictPtrTraits>(),
in_size, a_size, b_size
);
}));
AT_DISPATCH_FLOATING_TYPES(a.type(), "matmul_forward_cuda", ([&] {
matmul_cuda_backward_kernel_B<scalar_t><<<blocks2, threads_per_block>>>(
grad_b.packed_accessor32<scalar_t,3,torch::RestrictPtrTraits>(),
a.packed_accessor32<scalar_t,3,torch::RestrictPtrTraits>(),
b.packed_accessor32<scalar_t,3,torch::RestrictPtrTraits>(),
part.packed_accessor32<scalar_t,3,torch::RestrictPtrTraits>(),
grad_out.packed_accessor32<scalar_t,3,torch::RestrictPtrTraits>(),
in_size, a_size, b_size);
}));
} else if (mode == 1 or mode == 2) {
AT_DISPATCH_FLOATING_TYPES(a.type(), "matmul_forward_cuda", ([&] {
max_cuda_backward_kernel_A<scalar_t><<<blocks, threads_per_block>>>(
grad_a.packed_accessor32<scalar_t,3,torch::RestrictPtrTraits>(),
a.packed_accessor32<scalar_t,3,torch::RestrictPtrTraits>(),
b.packed_accessor32<scalar_t,3,torch::RestrictPtrTraits>(),
part.packed_accessor32<scalar_t,3,torch::RestrictPtrTraits>(),
grad_out.packed_accessor32<scalar_t,3,torch::RestrictPtrTraits>(),
in_size, a_size, b_size);
}));
AT_DISPATCH_FLOATING_TYPES(a.type(), "matmul_forward_cuda", ([&] {
max_cuda_backward_kernel_B<scalar_t><<<blocks2, threads_per_block>>>(
grad_b.packed_accessor32<scalar_t,3,torch::RestrictPtrTraits>(),
a.packed_accessor32<scalar_t,3,torch::RestrictPtrTraits>(),
b.packed_accessor32<scalar_t,3,torch::RestrictPtrTraits>(),
part.packed_accessor32<scalar_t,3,torch::RestrictPtrTraits>(),
grad_out.packed_accessor32<scalar_t,3,torch::RestrictPtrTraits>(),
in_size, a_size, b_size);
}));
}
return {grad_a, grad_b};
}