-
Notifications
You must be signed in to change notification settings - Fork 0
/
packed_attention_kernel.cu
481 lines (444 loc) · 20.4 KB
/
packed_attention_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
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
#include <torch/extension.h>
#include <ATen/cuda/CUDAContext.h>
#include <cuda.h>
#include <cuda_runtime.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include "ops.h"
#include "util.cuh"
template <typename scalar_t>
__global__ void packed_attention_kernel(
const int max_context_len,
const int dim,
const float scale,
scalar_t* __restrict__ Q, // [length, num_heads, dim]
scalar_t* __restrict__ K, // [length, num_heads, dim]
scalar_t* __restrict__ V, // [length, num_heads, dim]
int* __restrict__ offsets, // [length]
scalar_t* __restrict__ S,
scalar_t* __restrict__ P,
scalar_t* __restrict__ O
) {
const int thread_id = threadIdx.x;
const int block_dim = blockDim.x;
const int block_id = blockIdx.x;
const int head_id = blockIdx.y;
const int batch_size = gridDim.x;
const int num_heads = gridDim.y;
const int beg_idx = (block_id == 0)? 0 : offsets[block_id - 1];
const int end_idx = offsets[block_id];
const int size = end_idx - beg_idx;
// S, P have shape [batch_size, num_heads, max_context_len, max_context_len]
// Q, K have shape [length, num_heads, dim]
for(int i = thread_id; i < size; i += block_dim) {
const int shifted_i = i + beg_idx;
for(int j = 0; j < size; ++j) {
const int shifted_j = j + beg_idx;
int S_idx = (num_heads * max_context_len * max_context_len) * block_id + \
(max_context_len * max_context_len) * head_id + \
(max_context_len) * i + j;
if (i >= j)
{
for(int k = 0; k < dim; ++k) {
int Q_idx = (dim * num_heads) * shifted_i + dim * head_id + k;
int K_idx = (dim * num_heads) * shifted_j + dim * head_id + k;
S[S_idx] += Q[Q_idx] * K[K_idx] * scale;
}
} else {
S[S_idx] = -10000.0;
}
}
}
float val_sum;
float val_max;
const int idx_beg = (num_heads * max_context_len * max_context_len) * block_id + \
(max_context_len * max_context_len) * head_id;
// batch_size, num_heads, max_context_len, max_context_len
for (int i = 0; i < size; ++i) {
val_max = 0;
val_sum = 1e-9;
for(int j = thread_id; j < size; j += block_dim) {
float val = S[idx_beg + max_context_len * i + j];
val_max = max(val, val_max);
}
val_max = blockReduceMax<float>(val_max);
for(int j = thread_id; j < size; j += block_dim) {
float exp_val = exp(S[idx_beg + max_context_len * i + j] - val_max);
val_sum += exp_val;
}
__syncthreads();
val_sum = blockReduceSum<float>(val_sum);
for(int j = thread_id; j < size; j += block_dim) {
float exp_val = exp(S[idx_beg + max_context_len * i + j] - val_max);
P[idx_beg + max_context_len * i + j] = (exp_val / val_sum);
}
}
for(int i = thread_id; i < size; i += block_dim) {
const int shifted_i = beg_idx + i;
for(int j = 0; j < size; ++j) {
const int shifted_j = beg_idx + j;
for(int k = 0; k < dim; ++k) {
int P_idx = (num_heads * max_context_len * max_context_len) * block_id + \
(max_context_len * max_context_len) * head_id + \
(max_context_len) * i + j;
int V_idx = (num_heads * dim) * shifted_j + dim * head_id + k;
int O_idx = (num_heads * dim) * shifted_i + dim * head_id + k;
O[O_idx] += P[P_idx] * V[V_idx];
}
}
}
}
template <typename scalar_t>
__global__ void kv_single_query_attention_kernel(
const int max_context_len,
const int dim,
const float scale,
scalar_t* __restrict__ Q, // [length, num_heads, dim]
scalar_t* __restrict__ K, // [length, num_heads, dim]
scalar_t* __restrict__ V, // [length, num_heads, dim]
scalar_t* __restrict__ K_cache, // [cache_size, num_heads, dim]
scalar_t* __restrict__ V_cache, // [cache_size, num_heads, dim]
int* __restrict__ cache_indices, // [length]
int* __restrict__ offsets, // [batch_size]
scalar_t* __restrict__ S, // [batch_size, num_heads, max_context_len + 1]
scalar_t* __restrict__ P, // [batch_size, num_heads, max_context_len + 1]
scalar_t* __restrict__ O // [length, num_heads, dim]
) {
const int thread_id = threadIdx.x;
const int block_dim = blockDim.x;
const int block_id = blockIdx.x;
const int head_id = blockIdx.y;
const int batch_size = gridDim.x;
const int num_heads = gridDim.y;
const int beg_idx = (block_id == 0) ? 0 : offsets[block_id - 1];
const int end_idx = offsets[block_id];
const int size = end_idx - beg_idx;
// S[i] = K_cache[i][j] * Q[j];
// S has shape [batch_size, num_heads, max_context_len + 1]
for(int i = thread_id; i < size; i += block_dim) {
int S_idx = ((1 + max_context_len) * num_heads) * block_id + \
(1 + max_context_len) * head_id + i;
for(int j = 0;j < dim; ++j) {
int K_cache_idx = (dim) * head_id + (num_heads * dim) * cache_indices[beg_idx + i] + j;
int Q_idx = (num_heads * dim) * block_id + dim * head_id + j;
S[S_idx] += K_cache[K_cache_idx] * Q[Q_idx] * scale;
}
}
double tmp = 0.0;
for(int i = thread_id; i < dim; i += block_dim) {
int Q_idx = (num_heads * dim) * block_id + dim * head_id + i;
int K_idx = Q_idx;
tmp += (double)Q[Q_idx] * (double)K[K_idx] * scale;
}
// S shape [batch_size, num_heads, max_context_len + 1]
__syncthreads();
S[(num_heads * (1 + max_context_len)) * block_id + (max_context_len + 1) * head_id + size] = blockReduceSum<double>(tmp);
double val_max = 0;
for(int i = thread_id; i < size + 1; i += block_dim) {
int idx = ((1 + max_context_len) * num_heads) * block_id + (1 + max_context_len) * head_id + i;
val_max = max(val_max, S[idx]);
}
__syncthreads();
val_max = blockReduceMax<double>(val_max);
double exp_sum = 1e-6;
for(int i = thread_id; i < size + 1; i += block_dim) {
int idx = ((1 + max_context_len) * num_heads) * block_id + (1 + max_context_len) * head_id + i;
double exp_val = exp(S[idx] - val_max);
exp_sum += exp_val;
}
__syncthreads();
exp_sum = blockReduceSum<double>(exp_sum);
for(int i = thread_id; i < size + 1; i += block_dim) {
int idx = ((1 + max_context_len) * num_heads) * block_id + (1 + max_context_len) * head_id + i;
double exp_val = exp(S[idx] - val_max);
P[idx] = exp_val / (1e-6 + exp_sum);
}
__syncthreads();
for (int j = thread_id; j < dim; j += block_dim) {
for (int i = 0; i < size; ++i) {
int O_idx = (num_heads * dim) * block_id + (dim) * head_id + j;
int P_idx = (num_heads * (1 + max_context_len)) * block_id + \
((1 + max_context_len)) * head_id + i;
int V_idx = dim * head_id + (num_heads * dim) * cache_indices[beg_idx + i] + j;
O[O_idx] += P[P_idx] * V_cache[V_idx];
}
}
for (int j = thread_id; j < dim; j += block_dim) {
int O_idx = (num_heads * dim) * block_id + (dim) * head_id + j;
int P_idx = (num_heads * (1 + max_context_len)) * block_id + \
((1 + max_context_len)) * head_id + size;
int V_idx = (num_heads * dim) * block_id + (dim) * head_id + j;
O[O_idx] += P[P_idx] * V[V_idx];
}
}
template <typename scalar_t>
__global__ void kv_multi_query_attention_kernel(
const int max_context_len,
const int dim,
const float scale,
scalar_t* __restrict__ Q, // [length, num_queries, num_heads, dim]
scalar_t* __restrict__ K, // [length, num_queries, num_heads, dim]
scalar_t* __restrict__ V, // [length, num_queries, num_heads, dim]
scalar_t* __restrict__ K_cache, // [cache_size, num_heads, dim]
scalar_t* __restrict__ V_cache, // [cache_size, num_heads, dim]
int* __restrict__ cache_indices, // [length]
int* __restrict__ offsets, // [batch_size]
scalar_t* __restrict__ S, // [batch_size, num_heads, num_queries, max_context_len + num_queries]
scalar_t* __restrict__ P, // [batch_size, num_heads, num_queries, max_context_len + num_queries]
scalar_t* __restrict__ O // [length, num_queries, num_heads, dim]
) {
const int thread_id = threadIdx.x;
const int block_dim = blockDim.x;
const int block_id = blockIdx.x;
const int head_id = blockIdx.y;
const int query_id = blockIdx.z;
const int batch_size = gridDim.x;
const int num_heads = gridDim.y;
const int num_queries = gridDim.z;
const int beg_idx = (block_id == 0) ? 0 : offsets[block_id - 1];
const int end_idx = offsets[block_id];
const int size = end_idx - beg_idx;
for(int i = thread_id; i < size; i += block_dim) {
int S_idx = ((num_queries + max_context_len) * num_queries * num_heads) * block_id + \
(num_queries + max_context_len) * num_queries * head_id + \
(num_queries + max_context_len) * query_id + i;
for(int j = 0;j < dim; ++j) {
int K_cache_idx = (dim) * head_id + (num_heads * dim) * cache_indices[beg_idx + i] + j;
int Q_idx = (num_queries * dim * num_heads) * block_id + \
(num_heads * dim) * query_id + \
dim * head_id + j;
S[S_idx] += K_cache[K_cache_idx] * Q[Q_idx] * scale;
}
}
for (int prev_query_id = 0; prev_query_id <= query_id; ++prev_query_id) {
float tmp = 0.0;
for(int i = thread_id; i < dim; i += block_dim) {
int Q_idx = (num_queries * dim * num_heads) * block_id + \
(num_heads * dim) * query_id + \
dim * head_id + i;
int K_idx = (num_queries * dim * num_heads) * block_id + \
(num_heads * dim) * prev_query_id + \
dim * head_id + i;
tmp += Q[Q_idx] * K[K_idx] * scale;
}
__syncthreads();
int S_idx = ((num_queries + max_context_len) * num_queries * num_heads) * block_id + \
(num_queries + max_context_len) * num_queries * head_id + \
(num_queries + max_context_len) * query_id + \
(prev_query_id + size);
S[S_idx] = blockReduceSum<float>(tmp);
}
for(int mask_query_id = query_id + 1; mask_query_id < num_queries; ++mask_query_id) {
int S_idx = ((num_queries + max_context_len) * num_queries * num_heads) * block_id + \
(num_queries + max_context_len) * num_queries * head_id + \
(num_queries + max_context_len) * query_id + \
(mask_query_id + size);
S[S_idx] = -10000.0;
}
__syncthreads();
double val_max = 0;
for(int i = thread_id; i < size + num_queries; i += block_dim) {
int idx = ((num_queries + max_context_len) * num_queries * num_heads) * block_id + \
(num_queries + max_context_len) * num_queries * head_id + \
(num_queries + max_context_len) * query_id + i;
val_max = max(val_max, S[idx]);
}
__syncthreads();
val_max = blockReduceMax<double>(val_max);
double exp_sum = 1e-6;
for(int i = thread_id; i < size + num_queries; i += block_dim) {
int idx = ((num_queries + max_context_len) * num_queries * num_heads) * block_id + \
(num_queries + max_context_len) * num_queries * head_id + \
(num_queries + max_context_len) * query_id + i;
double exp_val = exp(S[idx] - val_max);
exp_sum += exp_val;
}
exp_sum = blockReduceSum<double>(exp_sum);
__syncthreads();
for(int i = thread_id; i < size + num_queries; i += block_dim) {
int idx = ((num_queries + max_context_len) * num_queries * num_heads) * block_id + \
(num_queries + max_context_len) * num_queries * head_id + \
(num_queries + max_context_len) * query_id + i;
double exp_val = exp(S[idx] - val_max);
P[idx] = exp_val / (1e-6 + exp_sum);
}
__syncthreads();
for (int j = thread_id; j < dim; j += block_dim) {
for (int i = 0; i < size; ++i) {
int O_idx = (num_queries * num_heads * dim) * block_id + \
(num_heads * dim) * query_id + \
dim * head_id + j;
int P_idx = ((num_queries + max_context_len) * num_queries * num_heads) * block_id + \
(num_queries + max_context_len) * num_queries * head_id + \
(num_queries + max_context_len) * query_id + i;
int V_idx = dim * head_id + (num_heads * dim) * cache_indices[beg_idx + i] + j;
O[O_idx] += P[P_idx] * V_cache[V_idx];
}
}
// V, // [length, num_queries, num_heads, dim]
// S, // [batch_size, num_heads, num_queries, max_context_len + num_queries]
// P, // [batch_size, num_heads, num_queries, max_context_len + num_queries]
// O // [length, num_queries, num_heads, dim]
for (int j = thread_id; j < dim; j += block_dim) {
for (int prev_query_id = 0; prev_query_id <= query_id; ++prev_query_id) {
int O_idx = (num_queries * num_heads * dim) * block_id + \
(num_heads * dim) * query_id + \
dim * head_id + j;
int P_idx = ((num_queries + max_context_len) * num_queries * num_heads) * block_id + \
(num_queries + max_context_len) * num_queries * head_id + \
(num_queries + max_context_len) * query_id + \
(prev_query_id + size);
int V_idx = (num_queries * num_heads * dim) * block_id + \
(num_heads * dim) * prev_query_id + \
dim * head_id + j;
O[O_idx] += P[P_idx] * V[V_idx];
}
}
}
std::vector<torch::Tensor> packed_attention(
torch::Tensor &Q, // [length, dim]
torch::Tensor &K, // [length, dim]
torch::Tensor &V, // [length, dim]
torch::Tensor &offsets, // [length]
int num_heads
) {
// always perform diagonal masking
CHECK_INPUT(Q); CHECK_INPUT(K); CHECK_INPUT(V);
auto batch_size = offsets.size(0);
auto dim = Q.size(1);
assert(dim % num_heads == 0);
auto options = torch::TensorOptions().dtype(Q.scalar_type()).device(torch::kCUDA);
int max_context_len = offsets[0].item<int>();
for (int i = 1; i < batch_size; ++i) {
max_context_len = max(max_context_len, (offsets[i] - offsets[i - 1]).item<int>());
}
auto S = torch::zeros({batch_size, num_heads, max_context_len, max_context_len}, options);
auto P = torch::zeros({batch_size, num_heads, max_context_len, max_context_len}, options);
auto O = torch::zeros_like(V);
const int threads = std::min(max_context_len, 1024);
const dim3 blocks(batch_size, num_heads);
const cudaStream_t stream = at::cuda::getCurrentCUDAStream();
float scale = 1.0 / std::sqrt(float(dim) / num_heads);
AT_DISPATCH_FLOATING_TYPES_AND_HALF(
Q.scalar_type(),
"packed_attention_kernel",
([&] {
packed_attention_kernel<<<blocks, threads, 0, stream>>>(
max_context_len,
dim / num_heads,
scale,
Q.data_ptr<scalar_t>(),
K.data_ptr<scalar_t>(),
V.data_ptr<scalar_t>(),
offsets.data_ptr<int>(),
S.data_ptr<scalar_t>(),
P.data_ptr<scalar_t>(),
O.data_ptr<scalar_t>()
);
})
);
return {S, P, O};
}
std::vector<torch::Tensor> kv_single_query_attention(
torch::Tensor &Q, // [batch_size, dim]
torch::Tensor &K, // [batch_size, dim]
torch::Tensor &V, // [batch_size, dim]
torch::Tensor &K_cache, // [num tokens, num_heads, dim]
torch::Tensor &V_cache, // [num tokens, num_heads, dim]
torch::Tensor &cache_indices, // [num total working indices]
torch::Tensor &offsets, // [batch_size]
int num_heads
) {
CHECK_INPUT(Q); CHECK_INPUT(K); CHECK_INPUT(V);
CHECK_INPUT(K_cache); CHECK_INPUT(V_cache);
auto batch_size = Q.size(0);
auto dim = Q.size(1);
assert(dim % num_heads == 0);
auto options = torch::TensorOptions().dtype(Q.scalar_type()).device(torch::kCUDA);
auto max_context_len = offsets[0].item<int>();
for(int i = 1; i < batch_size; ++i) {
max_context_len = max(max_context_len, (offsets[i] - offsets[i - 1]).item<int>());
}
auto S = torch::zeros({batch_size, num_heads, max_context_len + 1}, options);
auto P = torch::zeros({batch_size, num_heads, max_context_len + 1}, options);
auto O = torch::zeros_like(Q);
const int threads = std::min((int) (max_context_len + 1), 1024);
const dim3 blocks(batch_size, num_heads);
const cudaStream_t stream = at::cuda::getCurrentCUDAStream();
float scale = 1.0 / std::sqrt(float(dim) / num_heads);
AT_DISPATCH_FLOATING_TYPES_AND_HALF(
Q.scalar_type(),
"kv_single_query_attention_kernel",
([&] {
kv_single_query_attention_kernel<<<blocks, threads, max_context_len, stream>>>(
max_context_len,
dim / num_heads,
scale,
Q.data_ptr<scalar_t>(),
K.data_ptr<scalar_t>(),
V.data_ptr<scalar_t>(),
K_cache.data_ptr<scalar_t>(),
V_cache.data_ptr<scalar_t>(),
cache_indices.data_ptr<int>(),
offsets.data_ptr<int>(),
S.data_ptr<scalar_t>(),
P.data_ptr<scalar_t>(),
O.data_ptr<scalar_t>()
);
})
);
return {S, P, O};
}
std::vector<torch::Tensor> kv_multi_query_attention(
torch::Tensor &Q, // [batch_size, num_queries, dim]
torch::Tensor &K, // [batch_size, num_queries, dim]
torch::Tensor &V, // [batch_size, num_queries, dim]
torch::Tensor &K_cache, // [num tokens, num_heads, dim]
torch::Tensor &V_cache, // [num tokens, num_heads, dim]
torch::Tensor &cache_indices, // [num total working indices]
torch::Tensor &offsets, // [batch_size]
int num_heads
) {
CHECK_INPUT(Q); CHECK_INPUT(K); CHECK_INPUT(V);
CHECK_INPUT(K_cache); CHECK_INPUT(V_cache);
auto batch_size = Q.size(0);
auto num_queries = Q.size(1);
auto dim = Q.size(2);
assert(dim % num_heads == 0);
auto options = torch::TensorOptions().dtype(Q.scalar_type()).device(torch::kCUDA);
auto max_context_len = offsets[0].item<int>();
for(int i = 1; i < batch_size; ++i) {
max_context_len = max(max_context_len, (offsets[i] - offsets[i - 1]).item<int>());
}
auto S = torch::zeros({batch_size, num_heads, num_queries, max_context_len + num_queries}, options);
auto P = torch::zeros({batch_size, num_heads, num_queries, max_context_len + num_queries}, options);
auto O = torch::zeros_like(Q);
const int threads = std::min((int) (max_context_len + 1), 1024);
const dim3 blocks(batch_size, num_heads, num_queries);
const cudaStream_t stream = at::cuda::getCurrentCUDAStream();
float scale = 1.0 / std::sqrt(float(dim) / num_heads);
AT_DISPATCH_FLOATING_TYPES_AND_HALF(
Q.scalar_type(),
"kv_multi_query_attention_kernel",
([&] {
kv_multi_query_attention_kernel<<<blocks, threads, max_context_len, stream>>>(
max_context_len,
dim / num_heads,
scale,
Q.data_ptr<scalar_t>(),
K.data_ptr<scalar_t>(),
V.data_ptr<scalar_t>(),
K_cache.data_ptr<scalar_t>(),
V_cache.data_ptr<scalar_t>(),
cache_indices.data_ptr<int>(),
offsets.data_ptr<int>(),
S.data_ptr<scalar_t>(),
P.data_ptr<scalar_t>(),
O.data_ptr<scalar_t>()
);
})
);
return {S, P, O};
}