interplane.Get Omega
Read in your language

ggml

[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)

Brought by drakestapleton

branch master Branches Tags History

ggml / src / ggml-cuda / fattn-vec.cuh

613 lines · 25136 bytes Raw Blame History
1#include "common.cuh"
2#include "fattn-common.cuh"
3
4static int ggml_cuda_fattn_vec_get_nthreads_host(const int cc) {
5 return 128;
6 GGML_UNUSED(cc);
7}
8
9static constexpr __device__ int ggml_cuda_fattn_vec_get_nthreads_device() {
10 return 128;
11}
12
13// Currently llvm with the amdgcn target does not support unrolling loops
14// that contain a break that can not be resolved at compile time.
15#ifdef __clang__
16#pragma clang diagnostic push
17#pragma clang diagnostic ignored "-Wpass-failed"
18#endif // __clang__
19template<int D, int ncols, ggml_type type_K, ggml_type type_V, bool use_logit_softcap> // D == head size
20__launch_bounds__(ggml_cuda_fattn_vec_get_nthreads_device(), 1)
21static __global__ void flash_attn_ext_vec(
22 const char * Q_ptr,
23 const char * K_ptr,
24 const char * V_ptr,
25 const char * mask_ptr,
26 const char * sinks_ptr,
27 const int * KV_max_ptr,
28 const int * KV_rows_ptr,
29 float * dst_ptr,
30 float2 * dst_meta_ptr,
31 const float scale,
32 const float max_bias,
33 const float m0,
34 const float m1,
35 const uint32_t n_head_log2,
36 const float logit_softcap,
37 const int32_t ne00, const uint3 ne01, const int32_t ne02, const int32_t ne03,
38 const int32_t nb01, const int32_t nb02, const int32_t nb03,
39 const int32_t ne10, const int32_t ne11, const int32_t ne12, const int32_t ne13,
40 const int32_t nb11, const int32_t nb12, const int64_t nb13,
41 const int32_t nb21, const int32_t nb22, const int64_t nb23,
42 const int32_t ne31, const int32_t ne32, const int32_t ne33,
43 const int32_t nb31, const int32_t nb32, const int64_t nb33) {
44 ggml_cuda_pdl_lc();
45#ifdef FLASH_ATTN_AVAILABLE
46 const char * GGML_CUDA_RESTRICT Q = Q_ptr;
47 const char * GGML_CUDA_RESTRICT K = K_ptr;
48 const char * GGML_CUDA_RESTRICT V = V_ptr;
49 const char * GGML_CUDA_RESTRICT mask = mask_ptr;
50 const char * GGML_CUDA_RESTRICT sinks = sinks_ptr;
51 const int * GGML_CUDA_RESTRICT KV_max = KV_max_ptr;
52 float * GGML_CUDA_RESTRICT dst = dst_ptr;
53 float2 * GGML_CUDA_RESTRICT dst_meta = dst_meta_ptr;
54 GGML_UNUSED(KV_rows_ptr);
55
56 // Skip unused kernel variants for faster compilation:
57 if (use_logit_softcap && !(D == 128 || D == 256)) {
58 GGML_UNUSED_VARS(Q, K, V, mask, sinks, KV_max, dst, dst_meta, scale,
59 max_bias, m0, m1, n_head_log2, logit_softcap,
60 ne00, ne01, ne02, ne03,
61 nb01, nb02, nb03,
62 ne10, ne11, ne12, ne13,
63 nb11, nb12, nb13,
64 nb21, nb22, nb23,
65 ne31, ne32, ne33,
66 nb31, nb32, nb33);
67 NO_DEVICE_CODE;
68 return;
69 }
70
71 //In this kernel Q, K, V are matrices while i, j, k are matrix indices.
72
73 constexpr int cpy_nb = ggml_cuda_get_max_cpy_bytes();
74 constexpr int cpy_ne = cpy_nb / 4;
75
76#ifdef GGML_USE_HIP
77#ifdef RDNA
78 constexpr int nthreads_KQ_q = 2;
79#else
80 constexpr int nthreads_KQ_q = 4;
81#endif // RDNA
82 constexpr int nthreads_V_q = (D/4 < 32 ? D/4 : 32);
83#else
84 constexpr int nthreads_KQ_q = (D/4 < 32 ? D/4 : 32);
85 constexpr int nthreads_V_q = (D/4 < 32 ? D/4 : 32);
86#endif // GGML_USE_HIP
87
88 constexpr int nthreads = ggml_cuda_fattn_vec_get_nthreads_device();
89 constexpr int nthreads_KQ = (type_K == GGML_TYPE_F16 || type_K == GGML_TYPE_BF16) ? 128 / cpy_nb : nthreads_KQ_q;
90 constexpr int nthreads_V = (type_V == GGML_TYPE_F16 || type_V == GGML_TYPE_BF16) ? 128 / cpy_nb : nthreads_V_q;
91
92 static_assert(WARP_SIZE % nthreads_KQ == 0, "bad nthreads_K");
93 static_assert(WARP_SIZE % nthreads_V == 0, "bad nthreads_V");
94
95 constexpr int V_rows_per_thread = (type_V == GGML_TYPE_F16 || type_V == GGML_TYPE_BF16) ? 2*cpy_ne : 4;
96 constexpr int V_cols_per_iter = WARP_SIZE / nthreads_V;
97
98 constexpr vec_dot_KQ_t vec_dot_KQ = get_vec_dot_KQ<type_K, D, nthreads_KQ>();
99 constexpr bool Q_q8_1 = type_K != GGML_TYPE_F16 && type_K != GGML_TYPE_BF16;
100#ifdef V_DOT2_F32_F16_AVAILABLE
101 constexpr dequantize_V_t dequantize_V = get_dequantize_V<type_V, half, V_rows_per_thread>();
102#else
103 constexpr dequantize_V_t dequantize_V = get_dequantize_V<type_V, float, V_rows_per_thread>();
104#endif // V_DOT2_F32_F16_AVAILABLE
105
106 const int ic0 = blockIdx.x * ncols; // Index of the Q/QKV column to work on.
107
108 const int sequence = blockIdx.z / ne02;
109 const int head = blockIdx.z - sequence*ne02;
110 const int gqa_ratio = ne02 / ne12; // With grouped query attention there are > 1 Q matrices per K, V matrix.
111 Q += nb03*sequence + nb02* head + nb01*ic0;
112 K += nb13*sequence + nb12*(head / gqa_ratio);
113 V += nb23*sequence + nb22*(head / gqa_ratio);
114
115 const half * maskh = (const half *) (mask + nb33*(sequence % ne33) + nb31*ic0);
116
117 const float slope = get_alibi_slope(max_bias, head, n_head_log2, m0, m1);
118
119 static_assert(D % (2*WARP_SIZE) == 0, "D not divisible by 2*WARP_SIZE == 64.");
120 constexpr int nwarps = nthreads / WARP_SIZE;
121 const int tid = WARP_SIZE*threadIdx.y + threadIdx.x;
122 __builtin_assume(tid < nthreads);
123
124 constexpr int ne_KQ = ncols*D;
125 constexpr int ne_combine = nwarps*V_cols_per_iter*D;
126#ifdef V_DOT2_F32_F16_AVAILABLE
127 half2 VKQ[ncols][(D/2)/nthreads_V] = {{{0.0f, 0.0f}}};
128 __shared__ half KQ[ne_KQ > ne_combine ? ne_KQ : ne_combine];
129#else
130 float2 VKQ[ncols][(D/2)/nthreads_V] = {{{0.0f, 0.0f}}};
131 __shared__ float KQ[ne_KQ > ne_combine ? ne_KQ : ne_combine];
132#endif // V_DOT2_F32_F16_AVAILABLE
133
134 float KQ_max[ncols];
135 float KQ_sum[ncols];
136#pragma unroll
137 for (int j = 0; j < ncols; ++j) {
138 KQ_max[j] = -FLT_MAX/2.0f;
139 KQ_sum[j] = 0.0f;
140 }
141
142 // Convert Q to float2 (f16 K) or q8_1 (quantized K) and store in registers:
143#ifdef V_DOT2_F32_F16_AVAILABLE
144 half2 Q_reg[ncols][(D/2)/nthreads_KQ]; // Will be initialized completely.
145#else
146 __align__(16) float2 Q_reg[ncols][(D/2)/nthreads_KQ] = {{{0.0f, 0.0f}}}; // May be only partially initialized.
147#endif // V_DOT2_F32_F16_AVAILABLE
148 int Q_i32[ncols][1 > D/(sizeof(int)*nthreads_KQ) ? 1 : D/(sizeof(int)*nthreads_KQ)];
149 float2 Q_ds[ncols][1 > D/(sizeof(int)*nthreads_KQ) ? 1 : D/(sizeof(int)*nthreads_KQ)];
150
151 ggml_cuda_pdl_sync();
152 if constexpr (Q_q8_1) {
153#pragma unroll
154 for (int j0 = 0; j0 < ncols; j0 += nwarps) {
155 const int j = j0 + threadIdx.y;
156
157 if (j0 + nwarps > ncols && j >= ncols) {
158 break;
159 }
160
161 // Reuse KQ as temporary storage for converting Q to q8_1:
162 int * tmp_q_i32 = (int *) &KQ[j*D];
163 float2 * tmp_q_ds = (float2 *) (tmp_q_i32 + D/sizeof(int));
164
165 // Set memory to zero if out of bounds:
166 if (ncols > 1 && ic0 + j >= int(ne01.z)) {
167#pragma unroll
168 for (int i0 = 0; i0 < int(D/sizeof(int)); i0 += WARP_SIZE) {
169 const int i = i0 + threadIdx.x;
170
171 if (i0 + WARP_SIZE <= int(D/sizeof(int)) || i < int(D/sizeof(int))) {
172 tmp_q_i32[i] = 0;
173 }
174 }
175 if (threadIdx.x < D/QK8_1) {
176 tmp_q_ds[threadIdx.x] = make_float2(0.0f, 0.0f);
177 }
178 } else {
179 const float * Q_f = (const float *) (Q + j*nb01);
180 constexpr int nthreads_quantize = D/sizeof(int) < WARP_SIZE ? D/sizeof(int) : WARP_SIZE;
181#pragma unroll
182 for (int i0 = 0; i0 < int(D/sizeof(int)); i0 += nthreads_quantize) {
183 quantize_q8_1_to_shared<float2, nthreads_quantize>
184 (Q_f + i0*sizeof(int), scale, tmp_q_i32 + i0, tmp_q_ds + i0/QI8_1);
185 }
186 }
187 }
188
189 __syncthreads();
190
191#pragma unroll
192 for (int j = 0; j < ncols; ++j) {
193 int * tmp_q_i32 = (int *) &KQ[j*D];
194 float2 * tmp_q_ds = (float2 *) (tmp_q_i32 + D/sizeof(int));
195
196#pragma unroll
197 for (int i0 = 0; i0 < int(D/sizeof(int)); i0 += nthreads_KQ) {
198 const int i = i0 + (nthreads_KQ == WARP_SIZE ? threadIdx.x : threadIdx.x % nthreads_KQ);
199
200 Q_i32[j][i0/nthreads_KQ] = tmp_q_i32[i];
201 Q_ds[j][i0/nthreads_KQ] = tmp_q_ds[i/QI8_1];
202 }
203 }
204
205 __syncthreads();
206 } else {
207#ifdef V_DOT2_F32_F16_AVAILABLE
208 const half2 scale_h2 = make_half2(scale, scale);
209#pragma unroll
210 for (int j = 0; j < ncols; ++j) {
211 const float2 * Q_j = (const float2 *) (Q + j*nb01);
212#pragma unroll
213 for (int i0 = 0; i0 < D/2; i0 += nthreads_KQ*cpy_ne) {
214 const int i = i0 + (nthreads_KQ == WARP_SIZE ? threadIdx.x : threadIdx.x % nthreads_KQ)*cpy_ne;
215
216 __align__(16) float2 tmp[cpy_ne] = {{0.0f, 0.0f}};
217 if (ncols == 1 || ic0 + j < int(ne01.z)) {
218 ggml_cuda_memcpy_1<cpy_nb>(tmp, &Q_j[i]);
219 ggml_cuda_memcpy_1<cpy_nb>(tmp + cpy_ne/2, &Q_j[i + cpy_ne/2]);
220 }
221#pragma unroll
222 for (int i1 = 0; i1 < cpy_ne; ++i1) {
223 Q_reg[j][i0/nthreads_KQ + i1] = make_half2(tmp[i1].x, tmp[i1].y);
224 }
225 }
226#pragma unroll
227 for (int k = 0; k < (D/2)/nthreads_KQ; ++k) {
228 Q_reg[j][k] *= scale_h2;
229 }
230 }
231#else
232#pragma unroll
233 for (int j = 0; j < ncols; ++j) {
234 const float2 * Q_j = (const float2 *) (Q + j*nb01);
235#pragma unroll
236 for (int i0 = 0; i0 < D/2; i0 += nthreads_KQ*cpy_ne) {
237 const int i = i0 + (nthreads_KQ == WARP_SIZE ? threadIdx.x : threadIdx.x % nthreads_KQ)*cpy_ne;
238 if (ncols == 1 || ic0 + j < int(ne01.z)) {
239 ggml_cuda_memcpy_1<cpy_nb>(&Q_reg[j][i0/nthreads_KQ], &Q_j[i]);
240 ggml_cuda_memcpy_1<cpy_nb>(&Q_reg[j][i0/nthreads_KQ + cpy_ne/2], &Q_j[i + cpy_ne/2]);
241 }
242 }
243#pragma unroll
244 for (int k = 0; k < (D/2)/nthreads_KQ; ++k) {
245 Q_reg[j][k].x *= scale;
246 Q_reg[j][k].y *= scale;
247 }
248 }
249#endif // V_DOT2_F32_F16_AVAILABLE
250 }
251
252 const int k_VKQ_max = KV_max ? KV_max[sequence*gridDim.x + blockIdx.x] : ne11;
253 K += blockIdx.y*nthreads * nb11;
254 V += blockIdx.y*nthreads * nb21;
255 maskh += blockIdx.y*nthreads;
256 for (int k_VKQ_0 = blockIdx.y*nthreads; k_VKQ_0 < k_VKQ_max; k_VKQ_0 += gridDim.y*nthreads,
257 // Increment pointers after each loop:
258 K += gridDim.y*nthreads*nb11, V += gridDim.y*nthreads*nb21, maskh += gridDim.y*nthreads) {
259
260 // Calculate KQ tile and keep track of new maximum KQ values:
261 float KQ_reg[ncols]; // KQ in registers.
262
263 float KQ_max_new[ncols];
264#pragma unroll
265 for (int j = 0; j < ncols; ++j) {
266 KQ_max_new[j] = KQ_max[j];
267 }
268
269#pragma unroll
270 for (int i_KQ_0 = 0; i_KQ_0 < nthreads_KQ; ++i_KQ_0) {
271 const int i_KQ = threadIdx.y*WARP_SIZE + (nthreads_KQ == WARP_SIZE ? 0 : (threadIdx.x & ~(nthreads_KQ-1))) + i_KQ_0;
272
273#pragma unroll
274 for (int j = 0; j < ncols; ++j) {
275 float sum = vec_dot_KQ(K + i_KQ*nb11, Q_reg[j], Q_i32[j], Q_ds[j]);
276 sum = warp_reduce_sum<nthreads_KQ>(sum);
277
278 if (use_logit_softcap) {
279 sum = logit_softcap*tanhf(sum);
280 }
281
282 if (mask && (ncols == 1 || ic0 + j < int(ne01.z))) {
283 sum += slope*__half2float(maskh[j*ne11 + i_KQ]);
284 }
285
286 KQ_max_new[j] = fmaxf(KQ_max_new[j], sum + FATTN_KQ_MAX_OFFSET);
287
288 if ((nthreads_KQ == WARP_SIZE ? threadIdx.x : threadIdx.x % nthreads_KQ) == uint32_t(i_KQ_0)) {
289 KQ_reg[j] = sum;
290 }
291 }
292 }
293
294#pragma unroll
295 for (int j = 0; j < ncols; ++j) {
296#pragma unroll
297 for (int offset = nthreads_KQ; offset < WARP_SIZE; offset <<= 1) {
298 KQ_max_new[j] = fmaxf(KQ_max_new[j], __shfl_xor_sync(0xFFFFFFFF, KQ_max_new[j], offset, WARP_SIZE));
299 }
300 const float KQ_max_scale = expf(KQ_max[j] - KQ_max_new[j]);
301 KQ_max[j] = KQ_max_new[j];
302
303 KQ_reg[j] = expf(KQ_reg[j] - KQ_max[j]);
304 KQ_sum[j] = KQ_sum[j]*KQ_max_scale + KQ_reg[j];
305 KQ[j*nthreads + tid] = KQ_reg[j];
306
307#ifdef V_DOT2_F32_F16_AVAILABLE
308 const half2 KQ_max_scale_h2 = make_half2(KQ_max_scale, KQ_max_scale);
309#pragma unroll
310 for (int i_VKQ_0 = 0; i_VKQ_0 < D/2; i_VKQ_0 += nthreads_V) {
311 VKQ[j][i_VKQ_0/nthreads_V] *= KQ_max_scale_h2;
312 }
313#else
314#pragma unroll
315 for (int i_VKQ_0 = 0; i_VKQ_0 < D/2; i_VKQ_0 += nthreads_V) {
316 VKQ[j][i_VKQ_0/nthreads_V].x *= KQ_max_scale;
317 VKQ[j][i_VKQ_0/nthreads_V].y *= KQ_max_scale;
318 }
319#endif // V_DOT2_F32_F16_AVAILABLE
320 }
321
322 ggml_cuda_syncwarp(); // Needed for KQ synchronization.
323
324#pragma unroll
325 for (int k0 = 0; k0 < WARP_SIZE; k0 += V_cols_per_iter) {
326 const int k = threadIdx.y*WARP_SIZE + k0 + (nthreads_V == WARP_SIZE ? 0 : threadIdx.x / nthreads_V);
327
328#ifdef V_DOT2_F32_F16_AVAILABLE
329 half2 KQ_k[ncols];
330#pragma unroll
331 for (int j = 0; j < ncols; ++j) {
332 KQ_k[j] = __half2half2(KQ[j*nthreads + k]);
333 }
334#pragma unroll
335 for (int i_VKQ_0 = 0; i_VKQ_0 < D/2; i_VKQ_0 += nthreads_V*V_rows_per_thread/2) {
336 half2 tmp[V_rows_per_thread/2];
337 if constexpr (type_V == GGML_TYPE_BF16) {
338 float2 tmp_f[V_rows_per_thread/2];
339 dequantize_V(V + k*nb21, tmp_f,
340 2*i_VKQ_0 + (nthreads_V == WARP_SIZE ? threadIdx.x : threadIdx.x % nthreads_V)*V_rows_per_thread);
341#pragma unroll
342 for (int i_VKQ_1 = 0; i_VKQ_1 < V_rows_per_thread/2; ++i_VKQ_1) {
343 tmp[i_VKQ_1] = __float22half2_rn(tmp_f[i_VKQ_1]);
344 }
345 } else {
346 dequantize_V(V + k*nb21, tmp,
347 2*i_VKQ_0 + (nthreads_V == WARP_SIZE ? threadIdx.x : threadIdx.x % nthreads_V)*V_rows_per_thread);
348 }
349#pragma unroll
350 for (int i_VKQ_1 = 0; i_VKQ_1 < V_rows_per_thread/2; ++i_VKQ_1) {
351#pragma unroll
352 for (int j = 0; j < ncols; ++j) {
353 VKQ[j][i_VKQ_0/nthreads_V + i_VKQ_1] += tmp[i_VKQ_1]*KQ_k[j];
354 }
355 }
356 }
357#else
358 float KQ_k[ncols];
359#pragma unroll
360 for (int j = 0; j < ncols; ++j) {
361 KQ_k[j] = KQ[j*nthreads + k];
362 }
363#pragma unroll
364 for (int i_VKQ_0 = 0; i_VKQ_0 < D/2; i_VKQ_0 += nthreads_V*V_rows_per_thread/2) {
365 float2 tmp[V_rows_per_thread/2];
366 dequantize_V(V + k*nb21, tmp,
367 2*i_VKQ_0 + (nthreads_V == WARP_SIZE ? threadIdx.x : threadIdx.x % nthreads_V)*V_rows_per_thread);
368#pragma unroll
369 for (int i_VKQ_1 = 0; i_VKQ_1 < V_rows_per_thread/2; ++i_VKQ_1) {
370#pragma unroll
371 for (int j = 0; j < ncols; ++j) {
372 VKQ[j][i_VKQ_0/nthreads_V + i_VKQ_1].x += tmp[i_VKQ_1].x*KQ_k[j];
373 VKQ[j][i_VKQ_0/nthreads_V + i_VKQ_1].y += tmp[i_VKQ_1].y*KQ_k[j];
374 }
375 }
376 }
377#endif // V_DOT2_F32_F16_AVAILABLE
378 }
379
380 ggml_cuda_syncwarp(); // Needed for KQ synchronization.
381 }
382
383 if (sinks && blockIdx.y == 0) {
384 const float sink = ((const float *) sinks)[head];
385
386#pragma unroll
387 for (int j0 = 0; j0 < ncols; j0 += nwarps) {
388 const int j = j0 + threadIdx.y;
389
390 if (j0 + nwarps > ncols && j >= ncols) {
391 break;
392 }
393
394 const float kqmax_new_j = fmaxf(sink, KQ_max[j]);
395 const float KQ_max_scale = expf(KQ_max[j] - kqmax_new_j);
396 KQ_max[j] = kqmax_new_j;
397
398 KQ_sum[j] = KQ_sum[j]*KQ_max_scale + (threadIdx.x == 0 ? expf(sink - KQ_max[j]) : 0.0f);
399
400#ifdef V_DOT2_F32_F16_AVAILABLE
401 const half2 KQ_max_scale_h2 = make_half2(KQ_max_scale, KQ_max_scale);
402#pragma unroll
403 for (int i_VKQ_0 = 0; i_VKQ_0 < D/2; i_VKQ_0 += nthreads_V) {
404 VKQ[j][i_VKQ_0/nthreads_V] *= KQ_max_scale_h2;
405 }
406#else
407#pragma unroll
408 for (int i_VKQ_0 = 0; i_VKQ_0 < D/2; i_VKQ_0 += nthreads_V) {
409 VKQ[j][i_VKQ_0/nthreads_V].x *= KQ_max_scale;
410 VKQ[j][i_VKQ_0/nthreads_V].y *= KQ_max_scale;
411 }
412#endif // V_DOT2_F32_F16_AVAILABLE
413 }
414 }
415
416 __shared__ float KQ_max_shared[ncols][WARP_SIZE];
417 __shared__ float KQ_sum_shared[ncols][WARP_SIZE];
418#pragma unroll
419 for (int j = 0; j < ncols; ++j) {
420 if (threadIdx.y == 0) {
421 KQ_max_shared[j][threadIdx.x] = -FLT_MAX/2.0f;
422 KQ_sum_shared[j][threadIdx.x] = 0.0f;
423 }
424 }
425
426 __syncthreads();
427
428#pragma unroll
429 for (int j = 0; j < ncols; ++j) {
430 if (threadIdx.x == 0) {
431 KQ_max_shared[j][threadIdx.y] = KQ_max[j];
432 }
433 }
434 __syncthreads();
435
436#pragma unroll
437 for (int j_VKQ = 0; j_VKQ < ncols; ++j_VKQ) {
438 if (ncols > 1 && ic0 + j_VKQ >= int(ne01.z)) {
439 break;
440 }
441
442 float kqmax_new = KQ_max_shared[j_VKQ][threadIdx.x];
443 kqmax_new = warp_reduce_max(kqmax_new);
444 const float kqmax_scale = expf(KQ_max[j_VKQ] - kqmax_new);
445 KQ_max[j_VKQ] = kqmax_new;
446
447#ifdef V_DOT2_F32_F16_AVAILABLE
448 half2 * VKQ_tmp = (half2 *) KQ + threadIdx.y*(V_cols_per_iter*D/2)
449 + (nthreads_V == WARP_SIZE ? 0 : threadIdx.x / nthreads_V)*(D/2);
450
451 const half2 kqmax_scale_h2 = make_half2(kqmax_scale, kqmax_scale);
452#pragma unroll
453 for (int i_VKQ_0 = 0; i_VKQ_0 < D/2; i_VKQ_0 += nthreads_V) {
454 VKQ[j_VKQ][i_VKQ_0/nthreads_V] *= kqmax_scale_h2;
455 }
456#pragma unroll
457 for (int i_VKQ_0 = 0; i_VKQ_0 < D/2; i_VKQ_0 += nthreads_V*V_rows_per_thread/2) {
458 const int i_VKQ = i_VKQ_0 + (nthreads_V == WARP_SIZE ? threadIdx.x : threadIdx.x % nthreads_V)*(V_rows_per_thread/2);
459
460 ggml_cuda_memcpy_1<V_rows_per_thread*sizeof(half)>(VKQ_tmp + i_VKQ, &VKQ[j_VKQ][i_VKQ_0/nthreads_V]);
461 }
462#else
463 float2 * VKQ_tmp = (float2 *) KQ + threadIdx.y*(V_cols_per_iter*D/2)
464 + (nthreads_V == WARP_SIZE ? 0 : threadIdx.x / nthreads_V)*(D/2);
465
466#pragma unroll
467 for (int i_VKQ_0 = 0; i_VKQ_0 < D/2; i_VKQ_0 += nthreads_V) {
468 VKQ[j_VKQ][i_VKQ_0/nthreads_V].x *= kqmax_scale;
469 VKQ[j_VKQ][i_VKQ_0/nthreads_V].y *= kqmax_scale;
470 }
471#pragma unroll
472 for (int i_VKQ_0 = 0; i_VKQ_0 < D/2; i_VKQ_0 += nthreads_V*V_rows_per_thread/2) {
473 const int i_VKQ = i_VKQ_0 + (nthreads_V == WARP_SIZE ? threadIdx.x : threadIdx.x % nthreads_V)*(V_rows_per_thread/2);
474
475 ggml_cuda_memcpy_1<V_rows_per_thread/2*sizeof(float)>(VKQ_tmp + i_VKQ, &VKQ[j_VKQ][i_VKQ_0/nthreads_V]);
476 ggml_cuda_memcpy_1<V_rows_per_thread/2*sizeof(float)>(VKQ_tmp + i_VKQ + V_rows_per_thread/4, &VKQ[j_VKQ][i_VKQ_0/nthreads_V + V_rows_per_thread/4]);
477 }
478#endif // V_DOT2_F32_F16_AVAILABLE
479
480 KQ_sum[j_VKQ] *= kqmax_scale;
481 KQ_sum[j_VKQ] = warp_reduce_sum(KQ_sum[j_VKQ]);
482 if (threadIdx.x == 0) {
483 KQ_sum_shared[j_VKQ][threadIdx.y] = KQ_sum[j_VKQ];
484 }
485
486 __syncthreads();
487
488 if (nthreads <= D || tid < D) {
489 KQ_sum[j_VKQ] = KQ_sum_shared[j_VKQ][threadIdx.x];
490 KQ_sum[j_VKQ] = warp_reduce_sum(KQ_sum[j_VKQ]);
491
492#pragma unroll
493 for (int i0 = 0; i0 < D; i0 += nthreads) {
494 float dst_val = 0;
495#pragma unroll
496 for (int w = 0; w < nwarps; ++w) {
497#pragma unroll
498 for (int v = 0; v < V_cols_per_iter; ++v) {
499 dst_val += float(KQ[w*V_cols_per_iter*D + v*D + i0 + tid]);
500 }
501 }
502 if (gridDim.y == 1) {
503 dst_val /= KQ_sum[j_VKQ];
504 }
505 dst[(((sequence*int(ne01.z) + ic0 + j_VKQ)*ne02 + head)*gridDim.y + blockIdx.y)*D + i0 + tid] = dst_val;
506 }
507 }
508
509 if (j_VKQ < ncols-1) {
510 __syncthreads();
511 }
512
513 }
514
515 if (gridDim.y != 1 && tid < ncols && (ncols == 1 || ic0 + tid < int(ne01.z))) {
516 dst_meta[((sequence*int(ne01.z) + ic0 + tid)*ne02 + head)*gridDim.y + blockIdx.y] = make_float2(KQ_max[tid], KQ_sum[tid]);
517 }
518#else
519 GGML_UNUSED_VARS(Q_ptr, K_ptr, V_ptr, mask_ptr, sinks_ptr, KV_max_ptr, KV_rows_ptr, dst_ptr, dst_meta_ptr, scale,
520 max_bias, m0, m1, n_head_log2, logit_softcap,
521 ne00, ne01, ne02, ne03,
522 nb01, nb02, nb03,
523 ne10, ne11, ne12, ne13,
524 nb11, nb12, nb13,
525 nb21, nb22, nb23,
526 ne31, ne32, ne33,
527 nb31, nb32, nb33);
528 NO_DEVICE_CODE;
529#endif // FLASH_ATTN_AVAILABLE
530}
531#ifdef __clang__
532#pragma clang diagnostic pop
533#endif // __clang__
534
535template <int D, int cols_per_block, ggml_type type_K, ggml_type type_V, bool use_logit_softcap>
536void ggml_cuda_flash_attn_ext_vec_case_impl(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
537 const int cc = ggml_cuda_info().devices[ggml_cuda_get_device()].cc;
538
539 const int nthreads = ggml_cuda_fattn_vec_get_nthreads_host(cc);
540 const int nwarps = nthreads / WARP_SIZE;
541 fattn_kernel_t fattn_kernel = flash_attn_ext_vec<D, cols_per_block, type_K, type_V, use_logit_softcap>;
542 const bool need_f16_K = type_K == GGML_TYPE_F16;
543 const bool need_f16_V = type_V == GGML_TYPE_F16;
544 constexpr size_t nbytes_shared = 0;
545 launch_fattn<D, cols_per_block, 1>(ctx, dst, fattn_kernel, nwarps, nbytes_shared, D, need_f16_K, need_f16_V, false, false);
546}
547
548template <int D, ggml_type type_K, ggml_type type_V>
549void ggml_cuda_flash_attn_ext_vec_case(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
550 const ggml_tensor * KQV = dst;
551 const ggml_tensor * Q = dst->src[0];
552
553 float logit_softcap;
554 memcpy(&logit_softcap, (const float *) KQV->op_params + 2, sizeof(float));
555
556 if (Q->ne[1] == 1) {
557 constexpr int cols_per_block = 1;
558 if (logit_softcap == 0.0f) {
559 constexpr bool use_logit_softcap = false;
560 ggml_cuda_flash_attn_ext_vec_case_impl<D, cols_per_block, type_K, type_V, use_logit_softcap>(ctx, dst);
561 } else {
562 constexpr bool use_logit_softcap = true;
563 ggml_cuda_flash_attn_ext_vec_case_impl<D, cols_per_block, type_K, type_V, use_logit_softcap>(ctx, dst);
564 }
565 return;
566 }
567
568 constexpr int cols_per_block = 2;
569 if (logit_softcap == 0.0f) {
570 constexpr bool use_logit_softcap = false;
571 ggml_cuda_flash_attn_ext_vec_case_impl<D, cols_per_block, type_K, type_V, use_logit_softcap>(ctx, dst);
572 } else {
573 constexpr bool use_logit_softcap = true;
574 ggml_cuda_flash_attn_ext_vec_case_impl<D, cols_per_block, type_K, type_V, use_logit_softcap>(ctx, dst);
575 }
576}
577
578#define DECL_FATTN_VEC_CASE(D, type_K, type_V) \
579 template void ggml_cuda_flash_attn_ext_vec_case \
580 <D, type_K, type_V>(ggml_backend_cuda_context & ctx, ggml_tensor * dst) \
581
582#define EXTERN_DECL_FATTN_VEC_CASES(D, type_K) \
583 extern DECL_FATTN_VEC_CASE(D, type_K, GGML_TYPE_F16); \
584 extern DECL_FATTN_VEC_CASE(D, type_K, GGML_TYPE_Q4_0); \
585 extern DECL_FATTN_VEC_CASE(D, type_K, GGML_TYPE_Q4_1); \
586 extern DECL_FATTN_VEC_CASE(D, type_K, GGML_TYPE_Q5_0); \
587 extern DECL_FATTN_VEC_CASE(D, type_K, GGML_TYPE_Q5_1); \
588 extern DECL_FATTN_VEC_CASE(D, type_K, GGML_TYPE_Q8_0); \
589 extern DECL_FATTN_VEC_CASE(D, type_K, GGML_TYPE_BF16); \
590
591EXTERN_DECL_FATTN_VEC_CASES( 64, GGML_TYPE_F16)
592EXTERN_DECL_FATTN_VEC_CASES( 64, GGML_TYPE_Q4_0)
593EXTERN_DECL_FATTN_VEC_CASES( 64, GGML_TYPE_Q4_1)
594EXTERN_DECL_FATTN_VEC_CASES( 64, GGML_TYPE_Q5_0)
595EXTERN_DECL_FATTN_VEC_CASES( 64, GGML_TYPE_Q5_1)
596EXTERN_DECL_FATTN_VEC_CASES( 64, GGML_TYPE_Q8_0)
597EXTERN_DECL_FATTN_VEC_CASES( 64, GGML_TYPE_BF16)
598
599EXTERN_DECL_FATTN_VEC_CASES(128, GGML_TYPE_F16)
600EXTERN_DECL_FATTN_VEC_CASES(128, GGML_TYPE_Q4_0)
601EXTERN_DECL_FATTN_VEC_CASES(128, GGML_TYPE_Q4_1)
602EXTERN_DECL_FATTN_VEC_CASES(128, GGML_TYPE_Q5_0)
603EXTERN_DECL_FATTN_VEC_CASES(128, GGML_TYPE_Q5_1)
604EXTERN_DECL_FATTN_VEC_CASES(128, GGML_TYPE_Q8_0)
605EXTERN_DECL_FATTN_VEC_CASES(128, GGML_TYPE_BF16)
606
607EXTERN_DECL_FATTN_VEC_CASES(256, GGML_TYPE_F16)
608EXTERN_DECL_FATTN_VEC_CASES(256, GGML_TYPE_Q4_0)
609EXTERN_DECL_FATTN_VEC_CASES(256, GGML_TYPE_Q4_1)
610EXTERN_DECL_FATTN_VEC_CASES(256, GGML_TYPE_Q5_0)
611EXTERN_DECL_FATTN_VEC_CASES(256, GGML_TYPE_Q5_1)
612EXTERN_DECL_FATTN_VEC_CASES(256, GGML_TYPE_Q8_0)
613EXTERN_DECL_FATTN_VEC_CASES(256, GGML_TYPE_BF16)