forked from 1ytic/warp-rna
-
Notifications
You must be signed in to change notification settings - Fork 1
/
core.cu
396 lines (291 loc) · 10.7 KB
/
core.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#include "core.h"
#include <stdio.h>
#include <assert.h>
#include <algorithm>
#include <cuda_runtime_api.h>
#include <device_atomic_functions.h>
#include <device_launch_parameters.h>
#define W 32
#define G 1024
#define B 256
__forceinline__ __device__ static int idx2(int n, int u, int U1) {
return n * U1 + u;
}
__forceinline__ __device__ static int idx3(int n, int t, int u, int T, int U) {
return n * (T * U) + t * U + u;
}
__forceinline__ __device__ static int idx4(int n, int t, int u, int v, int T, int U, int V) {
return n * (T * U * V) + t * (U * V) + u * V + v;
}
__forceinline__ __device__ static float log_sum_exp(float a, float b) {
float maximum, diff;
if (a > b) {
maximum = a;
diff = b-a;
} else {
maximum = b;
diff = a-b;
}
//if (diff > -42) {
maximum += log1pf(expf(diff));
//}
return maximum;
}
__device__
void kernel_warp_alphas(unsigned int *counts, volatile float *alphas, const int *labels, const float *log_probs,
const int *xn, const int *yn, int T, int S, int U, int V, int blank) {
unsigned int d = threadIdx.x;
unsigned int g = blockIdx.x;
unsigned int u = blockIdx.y + 1;
unsigned int n = blockIdx.z;
unsigned int p = g * W;
unsigned int t = p + d + 1;
assert (d < W);
assert (u <= U);
assert (gridDim.y == U);
assert (blockDim.x == W);
int actual_u = yn[n]+1;
int actual_s = xn[n]-actual_u+2;
if (t > actual_s || u > actual_u)
return;
unsigned int *lock = counts + n * U * 2 + blockIdx.y;
if (blockIdx.x == 0 && blockIdx.y == 0) {
alphas[idx3(n, 0, 0, S, U)] = 0;
}
if (blockIdx.x > 0) {
// Wait previous row
do {} while (atomicAdd(lock, 0) < g);
}
if (blockIdx.y > 0) {
// Wait previous column
do {} while (atomicAdd(lock-1, 0) <= g);
}
if (blockIdx.x == 0 && u < actual_u) {
// Compute initial row value
unsigned int l = labels[idx2(n, u-1, U-1)];
float a = alphas[idx3(n, 0, u-1, S, U)];
float b = log_probs[idx4(n, u-1, u-1, l, T, U, V)];
alphas[idx3(n, 0, u, S, U)] = a + b;
}
if (blockIdx.y == 0 && t < actual_s) {
// Compute initial column with local scan algorithm
float a;
float b = log_probs[idx4(n, t-1, 0, blank, T, U, V)];
#pragma unroll
for(unsigned int i = 1; i < W; i *= 2) {
a = __shfl_up_sync(0xffffffff, b, i);
if (i <= d) {
b += a;
}
}
a = alphas[idx3(n, p, 0, S, U)];
alphas[idx3(n, t, 0, S, U)] = a + b;
}
if (t < actual_s && u < actual_u) {
// Ready to compute alphas[t, u]
unsigned int l = labels[idx2(n, u-1, U-1)];
float bias = log_probs[idx4(n, t+u-1, u, blank, T, U, V)];
float skip = alphas[idx3(n, p, u, S, U)] + bias;
float emit = alphas[idx3(n, t, u-1, S, U)] + log_probs[idx4(n, t+u-1, u-1, l, T, U, V)];
float r = log_sum_exp(skip, emit);
float output = r;
for(unsigned int i = 1; i < W; i++) {
r = __shfl_up_sync(0xffffffff, r, 1);
if (i == d) {
r = log_sum_exp(r + bias, emit);
output = r;
}
}
alphas[idx3(n, t, u, S, U)] = output;
}
if (d == 0) {
// https://stackoverflow.com/a/5233737
__threadfence();
atomicAdd(lock, 1);
}
}
__device__
void kernel_warp_betas(unsigned int *counts, volatile float *betas, const int *labels, const float *log_probs,
const int *xn, const int *yn, int T, int S, int U, int V, int blank) {
unsigned int d = threadIdx.x;
unsigned int g = blockIdx.x;
unsigned int u = blockIdx.y + 1;
unsigned int n = blockIdx.z;
unsigned int p = g * W;
unsigned int t = p + d + 1;
assert (d < W);
assert (u <= U);
assert (gridDim.y == U);
assert (blockDim.x == W);
int actual_t = xn[n];
int actual_u = yn[n]+1;
int actual_s = actual_t-actual_u+2;
if (t > actual_s || u > actual_u)
return;
int S1 = actual_s - 1;
int U1 = actual_u - 1;
unsigned int *lock = counts + n * U * 2 + U + blockIdx.y;
if (blockIdx.x == 0 && blockIdx.y == 0) {
betas[idx3(n, S1, U1, S, U)] = 0;
}
if (blockIdx.x > 0) {
// Wait previous row
do {} while (atomicAdd(lock, 0) < g);
}
if (blockIdx.y > 0) {
// Wait previous column
do {} while (atomicAdd(lock-1, 0) <= g);
}
if (blockIdx.x == 0 && u < actual_u) {
// Compute last row value
unsigned int l = labels[idx2(n, U1-u, U-1)];
float a = betas[idx3(n, S1, U1-u+1, S, U)];
float b = log_probs[idx4(n, actual_t-u, U1-u, l, T, U, V)];
betas[idx3(n, S1, U1-u, S, U)] = a + b;
}
if (blockIdx.y == 0 && t < actual_s) {
// Compute last column with local scan algorithm
float a;
float b = log_probs[idx4(n, actual_t-t, U1, blank, T, U, V)];
#pragma unroll
for(unsigned int i = 1; i < W; i *= 2) {
a = __shfl_up_sync(0xffffffff, b, i);
if (i <= d) {
b += a;
}
}
a = betas[idx3(n, S1-p, U1, S, U)];
betas[idx3(n, S1-t, U1, S, U)] = a + b;
}
if (t < actual_s && u < actual_u) {
// Ready to compute betas[S1-t, U1-u]
unsigned int l = labels[idx2(n, U1-u, U-1)];
float bias = log_probs[idx4(n, actual_t-t-u, U1-u, blank, T, U, V)];
float skip = betas[idx3(n, S1-p, U1-u, S, U)] + bias;
float emit = betas[idx3(n, S1-t, U1-u+1, S, U)] + log_probs[idx4(n, actual_t-t-u, U1-u, l, T, U, V)];
float r = log_sum_exp(skip, emit);
float output = r;
for(unsigned int i = 1; i < W; i++) {
r = __shfl_up_sync(0xffffffff, r, 1);
if (i == d) {
r = log_sum_exp(r + bias, emit);
output = r;
}
}
betas[idx3(n, S1-t, U1-u, S, U)] = output;
}
if (d == 0) {
// https://stackoverflow.com/a/5233737
__threadfence();
atomicAdd(lock, 1);
}
}
__global__
void kernel_warp(unsigned int *counts, volatile float *alphas, volatile float *betas, const int *labels, const float *log_probs,
const int *xn, const int *yn, int T, int S, int U, int V, int blank) {
if (threadIdx.y == 0) {
kernel_warp_alphas(counts, alphas, labels, log_probs, xn, yn, T, S, U, V, blank);
}
else if (threadIdx.y == 1) {
kernel_warp_betas(counts, betas, labels, log_probs, xn, yn, T, S, U, V, blank);
}
}
__global__
void kernel_grads_blank(float *grads, const float *alphas, const float *betas, const float *log_probs,
const int *xn, const int *yn, int T, int S, int U, int V, int blank) {
unsigned int d = threadIdx.x;
unsigned int g = blockIdx.x;
unsigned int u = blockIdx.y;
unsigned int n = blockIdx.z;
unsigned int t = g * G + d;
assert (u < U);
assert (d < G);
assert (blockDim.x == G);
assert (gridDim.y == U);
int actual_u = yn[n]+1;
int actual_s = xn[n]-actual_u+2;
if (t >= actual_s-1 || u >= actual_u)
return;
float a = alphas[idx3(n, t, u, S, U)];
float b = betas[idx3(n, t+1, u, S, U)];
float log_like = betas[idx3(n, 0, 0, S, U)];
unsigned int index = idx4(n, t+u, u, blank, T, U, V);
grads[index] = -expf(a + b + log_probs[index] - log_like);
}
__global__
void kernel_grads_label(float *grads, const float *alphas, const float *betas,
const int *labels, const float *log_probs,
const int *xn, const int *yn, int T, int S, int U, int V) {
unsigned int d = threadIdx.x;
unsigned int g = blockIdx.x;
unsigned int u = blockIdx.y;
unsigned int n = blockIdx.z;
unsigned int t = g * G + d;
assert (u < U - 1);
assert (d < G);
assert (blockDim.x == G);
assert (gridDim.y == U - 1);
int actual_u = yn[n]+1;
int actual_s = xn[n]-actual_u+2;
if (t >= actual_s || u >= actual_u-1)
return;
float a = alphas[idx3(n, t, u, S, U)];
float b = betas[idx3(n, t, u+1, S, U)];
unsigned int l = labels[idx2(n, u, U-1)];
float log_like = betas[idx3(n, 0, 0, S, U)];
unsigned int index = idx4(n, t+u, u, l, T, U, V);
grads[index] = -expf(a + b + log_probs[index] - log_like);
}
__global__
void kernel_fill_costs(float *costs, float *grads, const float *alphas, const float *betas,
const int *xn, const int *yn, int N, int T, int S, int U, int V) {
unsigned int n = blockIdx.x * blockDim.x + threadIdx.x;
if (n >= N)
return;
int u = yn[n];
int t = xn[n]-u;
float a = alphas[idx3(n, t, u, S, U)];
float b = betas[idx3(n, 0, 0, S, U)];
float ratio = fabsf(a - b) / fabsf(fmaxf(a, b));
if (ratio > 0.001) {
printf("\nWARNING: sample %d [%d, %d] has a forward/backward mismatch %f / %f, erasing grads.\n",
n, t, u, a, b);
float *g = grads + idx4(n, 0, 0, 0, T, U, V);
for (int i = 0; i < T; ++i) {
for (int j = 0; j < U; ++j) {
for (int v = 0; v < V; ++v, ++g) {
*g = 0;
}
}
}
//b = (a + b) / 2.0f;
}
// -a seemed more stable than -b
costs[n] = -a;
}
rnaStatus_t run_warp_rna(cudaStream_t stream, unsigned int *counts, float *alphas, float *betas,
const int *labels, const float *log_probs, float *grads, float *costs,
const int *xn, const int *yn, int N, int T, int S, int U, int V, int blank) {
dim3 threads1(W, 2);
dim3 blocks1((S + W - 1) / W, U, N);
kernel_warp <<<blocks1, threads1, 0, stream>>> (counts, alphas, betas, labels, log_probs, xn, yn, T, S, U, V, blank);
if (cudaGetLastError() != cudaSuccess)
return RNA_STATUS_WARP_FAILED;
if (S > 1) {
dim3 blocks2((S - 1 + G - 1) / G, U, N);
kernel_grads_blank <<<blocks2, G, 0, stream>>> (grads, alphas, betas, log_probs, xn, yn, T, S, U, V, blank);
if (cudaGetLastError() != cudaSuccess)
return RNA_STATUS_GRADS_BLANK_FAILED;
}
if (U > 1) {
dim3 blocks3((S + G - 1) / G, U - 1, N);
kernel_grads_label <<<blocks3, G, 0, stream>>> (grads, alphas, betas, labels, log_probs, xn, yn, T, S, U, V);
if (cudaGetLastError() != cudaSuccess)
return RNA_STATUS_GRADS_LABEL_FAILED;
}
dim3 blocks4((N + B - 1) / B, 1, 1);
kernel_fill_costs <<<blocks4, B, 0, stream>>> (costs, grads, alphas, betas, xn, yn, N, T, S, U, V);
if (cudaGetLastError() != cudaSuccess)
return RNA_STATUS_COSTS_FAILED;
return RNA_STATUS_SUCCESS;
}