-
Notifications
You must be signed in to change notification settings - Fork 21
/
attn.cpp
265 lines (218 loc) · 8.21 KB
/
attn.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
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
#include "attn.h"
#include "utils.h"
#include <omp.h>
#include <iostream>
struct attn_fwd_params {
size_t bs;
size_t head_num;
// TODO: GQA support
size_t q_seqlen;
size_t head_dim;
size_t k_seqlen;
size_t kv_head_num;
size_t stride_q_bs;
size_t stride_q_head_num;
size_t stride_q_seqlen;
size_t stride_q_head_dim;
size_t stride_kv_bs;
size_t stride_kv_head_num;
size_t stride_kv_seqlen;
size_t stride_kv_head_dim;
void *q_ptr;
void *k_ptr;
void *v_ptr;
void *o_ptr;
bool is_causal;
float softmax_scale;
};
template <typename Attn_traits> void run_naive_attn(attn_fwd_params ¶ms, typename Attn_traits::elem_type* attn_score, size_t stride_score_l1) {
/*
q k v.shape (bs, head_num, seqlen, head_dim)
attn_score.shape = (seqlen, seqlen), compute one by one
*/
using elem_type = typename Attn_traits::elem_type;
for (int bid = 0; bid < params.bs; bid++) {
for (int hid = 0; hid < params.head_num; hid++) {
#pragma omp parallel for
for (int i = 0; i < params.q_seqlen; i++) {
elem_type* q = static_cast<elem_type*>(params.q_ptr) + bid * params.stride_q_bs + hid * params.stride_q_head_num + i * params.stride_q_seqlen;
float maxval = -INFINITY;
int kv_len = params.k_seqlen;
if (params.is_causal) {
kv_len = i + 1 + (params.k_seqlen - params.q_seqlen);
}
// qk dot product
for (int j = 0; j < kv_len; j++) {
elem_type* k = static_cast<elem_type*>(params.k_ptr) + bid * params.stride_kv_bs + hid * params.stride_kv_head_num + j * params.stride_kv_seqlen;
elem_type val = 0.0f;
for (int dim = 0; dim < params.head_dim; dim++) {
val += q[dim] * k[dim];
}
val *= params.softmax_scale;
if (val > maxval) {
maxval = val;
}
// set score[i, j]
attn_score[i * stride_score_l1 + j] = val;
}
// NOTE: softmax
float score_sum = 0.0f;
for (int j = 0; j < kv_len; j++) {
auto exp = expf(attn_score[i * stride_score_l1 + j] - maxval);
score_sum += exp;
attn_score[i * stride_score_l1 + j] = exp;
}
for (int j = 0; j < kv_len; j++) {
attn_score[i * stride_score_l1 + j] /= score_sum;
}
// NOTE: compute qk @ v
// (seqlen, seqlen) @ (seqlen, head_dim)
elem_type* out = static_cast<elem_type*>(params.o_ptr) + bid * params.stride_q_bs + hid * params.stride_q_head_num + i * params.stride_q_seqlen;
// init accumulators
for (int dim = 0; dim < params.head_dim; dim++) {
out[dim] = 0.0f;
}
for (int j = 0; j < kv_len; j++) {
elem_type* v = static_cast<elem_type*>(params.v_ptr) + bid * params.stride_kv_bs + hid * params.stride_kv_head_num + j * params.stride_kv_seqlen;
for (int dim = 0; dim < params.head_dim; dim++) {
out[dim] += attn_score[i * stride_score_l1 + j] * v[dim];
}
}
}
}
}
}
template <typename Attn_traits> void run_flash_attn(attn_fwd_params ¶ms) {
/*
q k v.shape (bs, head_num, seqlen, head_dim)
attn_score.shape = (seqlen, seqlen), compute one by one
*/
using elem_type = typename Attn_traits::elem_type;
#pragma omp parallel for collapse(3)
for (int bid = 0; bid < params.bs; bid++) {
for (int hid = 0; hid < params.head_num; hid++) {
for (int i = 0; i < params.q_seqlen; i++) {
elem_type* q = static_cast<elem_type*>(params.q_ptr) + bid * params.stride_q_bs + hid * params.stride_q_head_num + i * params.stride_q_seqlen;
// init accumulators with zero allocate
elem_type* out = static_cast<elem_type*>(params.o_ptr) + bid * params.stride_q_bs + hid * params.stride_q_head_num + i * params.stride_q_seqlen;
// history max
float maxval = -INFINITY;
// div delay till the end (only div once)
float score_sum = 0.0f;
// qk dot product
// NOTE: and online softmax
int kv_len = params.k_seqlen;
if (params.is_causal) {
kv_len = i + 1 + (params.k_seqlen - params.q_seqlen);
}
for (int j = 0; j < kv_len; j++) {
float local_maxval = -INFINITY;
elem_type* k = static_cast<elem_type*>(params.k_ptr) + bid * params.stride_kv_bs + hid * params.stride_kv_head_num + j * params.stride_kv_seqlen;
// TODO: val need should be higher precision
elem_type val = 0.0f;
// q @ k
for (int dim = 0; dim < params.head_dim; dim++) {
val += q[dim] * k[dim];
}
val *= params.softmax_scale;
// local_maxval always the real max
local_maxval = std::max(maxval, val);
// TODO: skip scale if no update?
// TODO: exp2f?
auto exp = expf(val - local_maxval);
auto scale = expf(maxval - local_maxval);
// rescale score sum
score_sum *= scale;
score_sum += exp;
// NOTE: online softmax rescale, update
// and compute qk @ v: (seqlen, seqlen) @ (seqlen, head_dim)
elem_type* v = static_cast<elem_type*>(params.v_ptr) + bid * params.stride_kv_bs + hid * params.stride_kv_head_num + j * params.stride_kv_seqlen;
for (int dim = 0; dim < params.head_dim; dim++) {
// rescale score
out[dim] *= scale;
out[dim] += exp * v[dim];
}
// update max
maxval = local_maxval;
}
// TODO: online rescale or delay till the end?
for (int dim = 0; dim < params.head_dim; dim++) {
out[dim] /= score_sum;
}
}
}
}
}
void set_params_fprop(attn_fwd_params ¶ms,
// device pointers
const torch::Tensor q,
const torch::Tensor k,
const torch::Tensor v,
torch::Tensor out,
bool is_causal,
float softmax_scale) {
params.bs = q.size(0);
params.head_num = q.size(1);
params.kv_head_num = k.size(1);
params.q_seqlen = q.size(2);
params.k_seqlen = k.size(2);
params.head_dim = q.size(3);
params.stride_q_bs = q.stride(0);
params.stride_q_head_num = q.stride(1);
params.stride_q_seqlen = q.stride(2);
params.stride_q_head_dim = q.stride(3);
params.stride_kv_bs = k.stride(0);
params.stride_kv_head_num = k.stride(1);
params.stride_kv_seqlen = k.stride(2);
params.stride_kv_head_dim = k.stride(3);
params.q_ptr = q.data_ptr();
params.k_ptr = k.data_ptr();
params.v_ptr = v.data_ptr();
params.o_ptr = out.data_ptr();
params.is_causal = is_causal;
params.softmax_scale = softmax_scale;
}
torch::Tensor naive_attn(torch::Tensor q, torch::Tensor k,
torch::Tensor v, bool is_causal = false, float softmax_scale=1) {
TORCH_CHECK(q.device().is_cpu(), "q must be on CPU");
TORCH_CHECK(k.device().is_cpu(), "k must be on CPU");
TORCH_CHECK(v.device().is_cpu(), "v must be on CPU");
// batch size
int bs = q.size(0);
// head number
int head = q.size(1);
// seqlen
int seqlen = q.size(2);
int kv_seqlen = k.size(2);
// dim
int dim = q.size(3);
auto attn_score = torch::empty({seqlen, kv_seqlen}, q.options());
auto out = torch::zeros_like(q);
attn_fwd_params params;
set_params_fprop(params, q, k, v, out,
is_causal, softmax_scale);
// TODO: hard code float
run_naive_attn<Naive_fwd_traits<float>>(params, (float*)attn_score.data_ptr(), attn_score.stride(0));
return out;
}
torch::Tensor flash_attn(torch::Tensor q, torch::Tensor k,
torch::Tensor v, bool is_causal = false, float softmax_scale=1) {
TORCH_CHECK(q.device().is_cpu(), "q must be on CPU");
TORCH_CHECK(k.device().is_cpu(), "k must be on CPU");
TORCH_CHECK(v.device().is_cpu(), "v must be on CPU");
// batch size
int bs = q.size(0);
// head number
int head = q.size(1);
// seqlen
int seqlen = q.size(2);
// dim
int dim = q.size(3);
auto out = torch::zeros_like(q);
attn_fwd_params params;
set_params_fprop(params, q, k, v, out,
is_causal, softmax_scale);
// TODO: hard code float
run_flash_attn<Naive_fwd_traits<float>>(params);
return out;
}