-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflash_attention.cpp
More file actions
441 lines (385 loc) · 14.6 KB
/
Copy pathflash_attention.cpp
File metadata and controls
441 lines (385 loc) · 14.6 KB
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#include "flash_attention.h"
#include <algorithm>
#include <cmath>
#include <limits>
#include <vector>
constexpr float someRandVal = 42.0f;
/* Algorithm 0 Standard Attention Implementation
Q, K, V - N x d
S - N x N
P - N x N
O - N x d
N - sequence length
d - head dimension
*/
void attention_forward_naive(const float *Q, const float *K, const float *V,
float *S, float *P, float *O, int N, int d) {
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
float Sij = 0;
for (int k = 0; k < d; ++k)
Sij += Q[i * d + k] * K[j * d + k];
S[i * N + j] = Sij;
}
}
for (int i = 0; i < N; ++i) {
float Si_max = -std::numeric_limits<float>::max();
for (int j = 0; j < N; ++j)
Si_max = std::max(Si_max, S[i * N + j]);
float Si_norm = 0;
for (int j = 0; j < N; ++j) {
float Pij = std::exp(S[i * N + j] - Si_max);
P[i * N + j] = Pij;
Si_norm += Pij;
}
for (int j = 0; j < N; ++j)
P[i * N + j] /= Si_norm;
}
for (int i = 0; i < N; ++i) {
for (int j = 0; j < d; ++j) {
float Oij = 0;
for (int k = 0; k < N; ++k)
Oij += P[i * N + k] * V[k * d + j];
O[i * d + j] = Oij;
}
}
}
// Algorithm 1 FlashAttention
void attention_forward_tiled(const float *Q, const float *K, const float *V,
float *O, int N, int d, int Br, int Bc) {
for (int i = 0; i < N; ++i)
for (int j = 0; j < d; ++j)
O[i * d + j] = 0;
std::vector<float> l(N, 0);
std::vector<float> m(N, -std::numeric_limits<float>::max());
const int Tr = (N + Br - 1) / Br;
const int Tc = (N + Bc - 1) / Bc;
for (int j = 0; j < Tc; ++j) {
std::vector<float> Kj(Bc * d, someRandVal); // SRAM
std::vector<float> Vj(Bc * d, someRandVal); // SRAM
for (int row = 0; row < Bc && Bc * j + row < N; ++row) {
for (int col = 0; col < d; ++col) {
Kj[row * d + col] = K[(Bc * j + row) * d + col];
Vj[row * d + col] = V[(Bc * j + row) * d + col];
}
}
for (int i = 0; i < Tr; ++i) {
std::vector<float> Qi(Br * d, someRandVal); // SRAM
std::vector<float> Oi(Br * d, someRandVal); // SRAM
std::vector<float> li(Br, someRandVal);
std::vector<float> mi(Br, someRandVal);
for (int row = 0; row < Br && Br * i + row < N; ++row) {
for (int col = 0; col < d; ++col) {
Qi[row * d + col] = Q[(Br * i + row) * d + col];
Oi[row * d + col] = O[(Br * i + row) * d + col];
}
li[row] = l[Br * i + row];
mi[row] = m[Br * i + row];
}
std::vector<float> Sij(Br * Bc, someRandVal); // SRAM
for (int row = 0; row < Br && Br * i + row < N; ++row) {
for (int col = 0; col < Bc && Bc * j + col < N; ++col) {
float tmp = 0;
for (int k = 0; k < d; ++k)
tmp += Qi[row * d + k] * Kj[col * d + k];
Sij[row * Bc + col] = tmp;
}
}
for (int row = 0; row < Br && Br * i + row < N; ++row) {
float mij = -std::numeric_limits<float>::max();
for (int col = 0; col < Bc && Bc * j + col < N; ++col)
mij = std::max(mij, Sij[row * Bc + col]);
for (int col = 0; col < Bc && Bc * j + col < N; ++col)
Sij[row * Bc + col] = std::exp(Sij[row * Bc + col] - mij);
float lij = 0;
for (int col = 0; col < Bc && Bc * j + col < N; ++col)
lij += Sij[row * Bc + col];
const float mi_new = std::max(mi[row], mij);
const float li_new =
std::exp(mi[row] - mi_new) * li[row] + std::exp(mij - mi_new) * lij;
const float inv_li_new = 1 / li_new;
const float o_factor = li[row] * std::exp(mi[row] - mi_new);
const float pv_factor = std::exp(mij - mi_new);
for (int col = 0; col < d; ++col) {
float pv = 0;
for (int k = 0; k < Bc && Bc * j + k < N; ++k)
pv += Sij[row * Bc + k] * Vj[k * d + col];
O[(Br * i + row) * d + col] =
inv_li_new * (o_factor * Oi[row * d + col] + pv_factor * pv);
}
l[Br * i + row] = li_new;
m[Br * i + row] = mi_new;
}
}
}
}
// Algorithm 2 FlashAttention Forward Pass
// TODO: Add `dropout`
template <typename Mask>
void attention_forward_tiled(const float *Q, const float *K, const float *V,
float *O, float *l, float *m, int N, int d, int Br,
int Bc, float scale, Mask mask) {
for (int i = 0; i < N; ++i)
for (int j = 0; j < d; ++j)
O[i * d + j] = 0;
for (int i = 0; i < N; ++i)
l[i] = 0;
for (int i = 0; i < N; ++i)
m[i] = -std::numeric_limits<float>::max();
const int Tr = (N + Br - 1) / Br;
const int Tc = (N + Bc - 1) / Bc;
for (int j = 0; j < Tc; ++j) {
constexpr float someRandVal = 42.0f;
std::vector<float> Kj(Bc * d, someRandVal); // SRAM
std::vector<float> Vj(Bc * d, someRandVal); // SRAM
for (int row = 0; row < Bc && Bc * j + row < N; ++row) {
for (int col = 0; col < d; ++col) {
Kj[row * d + col] = K[(Bc * j + row) * d + col];
Vj[row * d + col] = V[(Bc * j + row) * d + col];
}
}
for (int i = 0; i < Tr; ++i) {
std::vector<float> Qi(Br * d, someRandVal); // SRAM
std::vector<float> Oi(Br * d, someRandVal); // SRAM
std::vector<float> li(Br, someRandVal);
std::vector<float> mi(Br, someRandVal);
for (int row = 0; row < Br && Br * i + row < N; ++row) {
for (int col = 0; col < d; ++col) {
Qi[row * d + col] = Q[(Br * i + row) * d + col];
Oi[row * d + col] = O[(Br * i + row) * d + col];
}
li[row] = l[Br * i + row];
mi[row] = m[Br * i + row];
}
std::vector<float> Sij(Br * Bc, someRandVal); // SRAM
for (int row = 0; row < Br && Br * i + row < N; ++row) {
for (int col = 0; col < Bc && Bc * j + col < N; ++col) {
if (mask(Br * i + row, Bc * j + col)) {
Sij[row * Bc + col] = -std::numeric_limits<float>::max();
continue;
}
float tmp = 0;
for (int k = 0; k < d; ++k)
tmp += Qi[row * d + k] * Kj[col * d + k];
Sij[row * Bc + col] = scale * tmp;
}
}
for (int row = 0; row < Br && Br * i + row < N; ++row) {
float mij = -std::numeric_limits<float>::max();
for (int col = 0; col < Bc && Bc * j + col < N; ++col)
mij = std::max(mij, Sij[row * Bc + col]);
for (int col = 0; col < Bc && Bc * j + col < N; ++col)
Sij[row * Bc + col] = std::exp(Sij[row * Bc + col] - mij);
float lij = 0;
for (int col = 0; col < Bc && Bc * j + col < N; ++col)
lij += Sij[row * Bc + col];
const float mi_new = std::max(mi[row], mij);
const float li_new =
std::exp(mi[row] - mi_new) * li[row] + std::exp(mij - mi_new) * lij;
const float inv_li_new = 1 / li_new;
const float o_factor = li[row] * std::exp(mi[row] - mi_new);
const float pv_factor = std::exp(mij - mi_new);
for (int col = 0; col < d; ++col) {
float pv = 0;
for (int k = 0; k < Bc && Bc * j + k < N; ++k)
pv += Sij[row * Bc + k] * Vj[k * d + col];
O[(Br * i + row) * d + col] =
inv_li_new * (o_factor * Oi[row * d + col] + pv_factor * pv);
}
l[Br * i + row] = li_new;
m[Br * i + row] = mi_new;
}
}
}
}
#define INSTANTIATE_ATTENTION_FORWARD_TILED(MASK) \
template void attention_forward_tiled<MASK>( \
const float *, const float *, const float *, float *, float *, float *, \
int, int, int, int, float, MASK);
INSTANTIATE_ATTENTION_FORWARD_TILED(masks::None)
INSTANTIATE_ATTENTION_FORWARD_TILED(masks::Causal)
INSTANTIATE_ATTENTION_FORWARD_TILED(masks::KeyPadding)
INSTANTIATE_ATTENTION_FORWARD_TILED(masks::Window)
/* Algorithm 3 Standard Attention Backward Pass
Q, K, V, dO - N x d
P - N x N
dQ, dK, dV - N x d
*/
void attention_backward_naive(const float *Q, const float *K, const float *V,
const float *dO, const float *P, float *dQ,
float *dK, float *dV, int N, int d) {
for (int i = 0; i < N; ++i) {
for (int j = 0; j < d; ++j) {
float dVij = 0;
for (int k = 0; k < N; ++k)
dVij += P[k * N + i] * dO[k * d + j];
dV[i * d + j] = dVij;
}
}
std::vector<float> dP(N * N, someRandVal);
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
float dPij = 0;
for (int k = 0; k < d; ++k)
dPij += dO[i * d + k] * V[j * d + k];
dP[i * N + j] = dPij;
}
}
std::vector<float> dS(N * N, someRandVal);
for (int i = 0; i < N; ++i) {
float PdP = 0;
for (int j = 0; j < N; ++j)
PdP += P[i * N + j] * dP[i * N + j];
for (int j = 0; j < N; ++j)
dS[i * N + j] = P[i * N + j] * (dP[i * N + j] - PdP);
}
for (int i = 0; i < N; ++i) {
for (int j = 0; j < d; ++j) {
float dQij = 0;
for (int k = 0; k < N; ++k)
dQij += dS[i * N + k] * K[k * d + j];
dQ[i * d + j] = dQij;
}
}
for (int i = 0; i < N; ++i) {
for (int j = 0; j < d; ++j) {
float dKij = 0;
for (int k = 0; k < N; ++k)
dKij += dS[k * N + i] * Q[k * d + j];
dK[i * d + j] = dKij;
}
}
}
template <typename Mask>
void attention_backward_tiled(const float *Q, const float *K, const float *V,
const float *O, const float *dO, const float *l,
const float *m, float *dQ, float *dK, float *dV,
int N, int d, int Br, int Bc, float scale,
Mask mask) {
const int Tr = (N + Br - 1) / Br;
const int Tc = (N + Bc - 1) / Bc;
for (int i = 0; i < N; ++i) {
for (int j = 0; j < d; ++j) {
dQ[i * d + j] = 0;
dK[i * d + j] = 0;
dV[i * d + j] = 0;
}
}
for (int j = 0; j < Tc; ++j) {
std::vector<float> Kj(Bc * d, someRandVal);
std::vector<float> Vj(Bc * d, someRandVal);
for (int row = 0, grow = Bc * j; row < Bc && grow < N; ++row, ++grow) {
for (int col = 0; col < d; ++col) {
Kj[row * d + col] = K[grow * d + col];
Vj[row * d + col] = V[grow * d + col];
}
}
std::vector<float> dKj(Bc * d, someRandVal);
std::vector<float> dVj(Bc * d, someRandVal);
for (int row = 0; row < Bc; ++row) {
for (int col = 0; col < d; ++col) {
dKj[row * d + col] = 0;
dVj[row * d + col] = 0;
}
}
for (int i = 0; i < Tr; ++i) {
std::vector<float> Qi(Br * d, someRandVal);
std::vector<float> Oi(Br * d, someRandVal);
std::vector<float> dQi(Br * d, someRandVal);
std::vector<float> dOi(Br * d, someRandVal);
std::vector<float> li(Br, someRandVal);
std::vector<float> mi(Br, someRandVal);
for (int row = 0, grow = Br * i; row < Br && grow < N; ++row, ++grow) {
for (int col = 0; col < d; ++col) {
Qi[row * d + col] = Q[grow * d + col];
Oi[row * d + col] = O[grow * d + col];
dQi[row * d + col] = dQ[grow * d + col];
dOi[row * d + col] = dO[grow * d + col];
}
li[row] = l[grow];
mi[row] = m[grow];
}
std::vector<float> Sij(Br * Bc, someRandVal);
for (int row = 0, grow = Br * i; row < Br && grow < N; ++row, ++grow) {
const float inv_li = 1 / li[row];
const float mi_val = mi[row];
for (int col = 0, gcol = Bc * j; col < Bc && gcol < N; ++col, ++gcol) {
float Sij_val;
if (mask(grow, gcol))
Sij_val = -std::numeric_limits<float>::max();
else {
Sij_val = 0;
for (int k = 0; k < d; ++k)
Sij_val += Qi[row * d + k] * Kj[col * d + k];
Sij_val *= scale;
}
const float Pij_val = inv_li * std::exp(Sij_val - mi_val);
Sij[row * Bc + col] = Pij_val;
}
}
for (int row = 0, grow = Bc * j; row < Bc && grow < N; ++row, ++grow) {
for (int col = 0; col < d; ++col) {
float tmp = 0;
for (int k = 0; k < Br && Br * i + k < N; ++k)
tmp += Sij[k * Bc + row] * dOi[k * d + col];
dVj[row * d + col] += tmp;
}
}
std::vector<float> dPij(Br * Bc, someRandVal);
for (int row = 0, grow = Br * i; row < Br && grow < N; ++row, ++grow) {
for (int col = 0, gcol = Bc * j; col < Bc && gcol < N; ++col, ++gcol) {
float tmp = 0;
for (int k = 0; k < d; ++k)
tmp += dOi[row * d + k] * Vj[col * d + k];
dPij[row * Bc + col] = tmp;
}
}
std::vector<float> Di(Br, someRandVal);
for (int row = 0, grow = Br * i; row < Br && grow < N; ++row, ++grow) {
float tmp = 0;
for (int col = 0; col < d; ++col)
tmp += dOi[row * d + col] * Oi[row * d + col];
Di[row] = tmp;
}
std::vector<float> dSij(Br * Bc, someRandVal);
for (int row = 0, grow = Br * i; row < Br && grow < N; ++row, ++grow) {
const float di_val = Di[row];
for (int col = 0, gcol = Bc * j; col < Bc && gcol < N; ++col, ++gcol) {
dSij[row * Bc + col] =
Sij[row * Bc + col] * (dPij[row * Bc + col] - di_val);
}
}
for (int row = 0, grow = Br * i; row < Br && grow < N; ++row, ++grow) {
for (int col = 0; col < d; ++col) {
float tmp = 0;
for (int k = 0; k < Bc && Bc * j + k < N; ++k)
tmp += dSij[row * Bc + k] * Kj[k * d + col];
dQ[grow * d + col] = dQi[row * d + col] + tmp * scale;
}
}
for (int row = 0, grow = Bc * j; row < Bc && grow < N; ++row, ++grow) {
for (int col = 0; col < d; ++col) {
float tmp = 0;
for (int k = 0; k < Br && Br * i + k < N; ++k)
tmp += dSij[k * Bc + row] * Qi[k * d + col];
dKj[row * d + col] += tmp * scale;
}
}
}
for (int row = 0, grow = Bc * j; row < Bc && grow < N; ++row, ++grow) {
for (int col = 0; col < d; ++col) {
dK[grow * d + col] = dKj[row * d + col];
dV[grow * d + col] = dVj[row * d + col];
}
}
}
}
#define INSTANTIATE_ATTENTION_BACKWARD_TILED(MASK) \
template void attention_backward_tiled<MASK>( \
const float *, const float *, const float *, const float *, \
const float *, const float *, const float *, float *, float *, float *, \
int, int, int, int, float, MASK);
INSTANTIATE_ATTENTION_BACKWARD_TILED(masks::None)
INSTANTIATE_ATTENTION_BACKWARD_TILED(masks::Causal)
INSTANTIATE_ATTENTION_BACKWARD_TILED(masks::KeyPadding)
INSTANTIATE_ATTENTION_BACKWARD_TILED(masks::Window)