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-opencl / kernels / flash_attn_f32_q4_0.cl

2011 lines · 81596 bytes Raw Blame History
1#pragma OPENCL EXTENSION cl_khr_fp16 : enable
2#ifdef cl_khr_integer_dot_product
3#pragma OPENCL EXTENSION cl_khr_integer_dot_product : enable
4#define FA_HAVE_INT_DOT 1
5#endif
6
7#ifdef cl_khr_subgroup_shuffle
8#pragma OPENCL EXTENSION cl_khr_subgroup_shuffle : enable
9#define HAS_SUBGROUP_SHUFFLE 1
10#elif defined(cl_qcom_subgroup_shuffle)
11#pragma OPENCL EXTENSION cl_qcom_subgroup_shuffle : enable
12#define HAS_SUBGROUP_SHUFFLE 1
13// Adreno compilers that expose only cl_qcom_subgroup_shuffle do not declare the KHR
14// name, so calling it is an implicit declaration and the program fails to build.
15// Route it to the qcom builtin.
16#define sub_group_shuffle_xor(val, mask) qcom_sub_group_shuffle_xor((val), (mask), CLK_SUB_GROUP_SHUFFLE_WIDTH_WAVE_SIZE_QCOM, 0.0f)
17#endif
18
19// Flash attention: Q=f32, K=q4_0, V=q4_0.
20// Block = half d + uchar qs[16]; qs[j] low/high nibble -> elem j / j+16.
21// Dequant: val[i] = d * (nibble_i - 8). dp4a path runs on raw 0..15 nibbles
22// and applies the -8*sum(q) correction once per block (needs Q q_sum).
23
24#define ACC_TYPE float
25#define ACC_TYPE4 float4
26#define Q_DATA_TYPE4 float4
27#define O_DATA_TYPE4 float4
28#define MASK_DATA_TYPE half
29#define CONVERT_Q_ACC4(x) (x)
30#define CONVERT_O_DATA4(x) (x)
31
32#define DK_VEC (DK/4)
33#define DV_VEC (DV/4)
34
35#ifndef FA_SG
36#define FA_SG 64
37#endif
38#define Q1_WG_SIZE FA_SG
39
40// The kernels are built with -cl-finite-math-only. On some older Adreno GPUs,
41// infinite operand can cause undefined behavior and miscompilation for exp.
42// Therefore, a large negative value is used instead.
43#define FA_M_INIT (-3.0e38f)
44
45#define QK4_0 32
46#define Q4_0_BLOCK_SIZE 18
47
48#define DK_Q4_BLOCKS (DK / QK4_0)
49#define DV_Q4_BLOCKS (DV / QK4_0)
50
51inline float dot_q4_0_f32(const global char * block_ptr, ACC_TYPE4 * q_slice) {
52 float d = vload_half(0, (const global half *)block_ptr);
53 const global uchar * qs = (const global uchar *)(block_ptr + 2);
54
55 float sum = 0.0f;
56 // Low nibbles -> elems 0..15.
57 #pragma unroll
58 for (int g = 0; g < 4; ++g) {
59 float4 nv = (float4)((float)(int)(qs[g*4 + 0] & 0x0F) - 8.0f,
60 (float)(int)(qs[g*4 + 1] & 0x0F) - 8.0f,
61 (float)(int)(qs[g*4 + 2] & 0x0F) - 8.0f,
62 (float)(int)(qs[g*4 + 3] & 0x0F) - 8.0f);
63 sum += dot(q_slice[g], nv);
64 }
65 // High nibbles -> elems 16..31.
66 #pragma unroll
67 for (int g = 0; g < 4; ++g) {
68 float4 nv = (float4)((float)(int)(qs[g*4 + 0] >> 4) - 8.0f,
69 (float)(int)(qs[g*4 + 1] >> 4) - 8.0f,
70 (float)(int)(qs[g*4 + 2] >> 4) - 8.0f,
71 (float)(int)(qs[g*4 + 3] >> 4) - 8.0f);
72 sum += dot(q_slice[4 + g], nv);
73 }
74 return sum * d;
75}
76
77#ifdef FA_HAVE_INT_DOT
78inline uint pack_i8x4(char a, char b, char c, char d) {
79 return ((uint)(uchar)a) |
80 ((uint)(uchar)b) << 8 |
81 ((uint)(uchar)c) << 16 |
82 ((uint)(uchar)d) << 24;
83}
84
85// Returns (qd, q_sum); q_sum feeds the -8*sum(q) bias correction.
86typedef struct {
87 float qd;
88 int q_sum;
89} q4_q_block_info;
90
91inline q4_q_block_info quant_q_block_int8_packed_q4(const ACC_TYPE4 * q_block,
92 uint * out_packed) {
93 float amax = 0.0f;
94 #pragma unroll
95 for (int i = 0; i < 8; ++i) {
96 float4 av = fabs(q_block[i]);
97 amax = fmax(amax, fmax(fmax(av.s0, av.s1), fmax(av.s2, av.s3)));
98 }
99 float qd = amax / 127.0f;
100 float qid = (amax > 0.0f) ? 127.0f / amax : 0.0f;
101
102 int q_sum = 0;
103 #pragma unroll
104 for (int i = 0; i < 8; ++i) {
105 float4 v = q_block[i] * qid;
106 char a = (char)((int)round(v.s0));
107 char b = (char)((int)round(v.s1));
108 char c = (char)((int)round(v.s2));
109 char d = (char)((int)round(v.s3));
110 out_packed[i] = pack_i8x4(a, b, c, d);
111 q_sum += (int)a + (int)b + (int)c + (int)d;
112 }
113 q4_q_block_info info = { qd, q_sum };
114 return info;
115}
116
117// k_packed[0..3] = low nibbles (Q elems 0..15), k_packed[4..7] = high (16..31).
118inline void pack_q4_0_nibbles(const global uchar * qs, uint * k_packed) {
119 #pragma unroll
120 for (int g = 0; g < 4; ++g) {
121 uchar b0 = qs[g*4 + 0];
122 uchar b1 = qs[g*4 + 1];
123 uchar b2 = qs[g*4 + 2];
124 uchar b3 = qs[g*4 + 3];
125 k_packed[g] =
126 ((uint)(b0 & 0x0F)) |
127 ((uint)(b1 & 0x0F)) << 8 |
128 ((uint)(b2 & 0x0F)) << 16 |
129 ((uint)(b3 & 0x0F)) << 24;
130 k_packed[4 + g] =
131 ((uint)(b0 >> 4)) |
132 ((uint)(b1 >> 4)) << 8 |
133 ((uint)(b2 >> 4)) << 16 |
134 ((uint)(b3 >> 4)) << 24;
135 }
136}
137
138inline float dot_q4_0_int(const global char * k_block_ptr,
139 const uint * q_packed,
140 float q_d,
141 int q_sum) {
142 float kd = vload_half(0, (const global half *)k_block_ptr);
143 const global uchar * k_qs = (const global uchar *)(k_block_ptr + 2);
144
145 uint k_packed[8];
146 pack_q4_0_nibbles(k_qs, k_packed);
147
148 int sum = 0;
149 #pragma unroll
150 for (int i = 0; i < 8; ++i) {
151 sum = dot_acc_sat_4x8packed_ss_int(q_packed[i], k_packed[i], sum);
152 }
153 // Correct raw-nibble sum: (nibble - 8) bias -> subtract 8 * q_sum.
154 return (float)(sum - 8 * q_sum) * q_d * kd;
155}
156#endif // FA_HAVE_INT_DOT
157
158inline void dequant_q4_0_f32(const global char * block_ptr, ACC_TYPE4 * out) {
159 float d = vload_half(0, (const global half *)block_ptr);
160 const global uchar * qs = (const global uchar *)(block_ptr + 2);
161
162 #pragma unroll
163 for (int g = 0; g < 4; ++g) {
164 out[g] = d * (float4)((float)(int)(qs[g*4 + 0] & 0x0F) - 8.0f,
165 (float)(int)(qs[g*4 + 1] & 0x0F) - 8.0f,
166 (float)(int)(qs[g*4 + 2] & 0x0F) - 8.0f,
167 (float)(int)(qs[g*4 + 3] & 0x0F) - 8.0f);
168 }
169 #pragma unroll
170 for (int g = 0; g < 4; ++g) {
171 out[4 + g] = d * (float4)((float)(int)(qs[g*4 + 0] >> 4) - 8.0f,
172 (float)(int)(qs[g*4 + 1] >> 4) - 8.0f,
173 (float)(int)(qs[g*4 + 2] >> 4) - 8.0f,
174 (float)(int)(qs[g*4 + 3] >> 4) - 8.0f);
175 }
176}
177
178// max_bias<=0 returns 1.0 so score += 1.0 * mask[k] stays a no-op multiplier.
179inline float get_alibi_slope(float max_bias, int head_idx, int n_head_log2, float m0, float m1) {
180 if (max_bias <= 0.0f) return 1.0f;
181 float base = (head_idx < n_head_log2) ? m0 : m1;
182 int exph = (head_idx < n_head_log2) ? (head_idx + 1) : (2*(head_idx - n_head_log2) + 1);
183 return pow(base, (float)exph);
184}
185
186// q1 decode: one query row per WG, threads sweep KV positions.
187__kernel void flash_attn_f32_q4_0_q1(
188 const global void * q_void, ulong q_offset,
189 const global void * k_void, ulong k_offset,
190 const global void * v_void, ulong v_offset,
191 global void * o_void, ulong o_offset,
192 const float scale,
193 const int n_q,
194 const int n_kv,
195 const int is_causal,
196 const int n_head,
197 const ulong q_nb1, const ulong q_nb2, const ulong q_nb3,
198 const ulong k_nb1, const ulong k_nb2, const ulong k_nb3,
199 const ulong v_nb1, const ulong v_nb2, const ulong v_nb3,
200 const ulong o_nb1, const ulong o_nb2, const ulong o_nb3,
201 const float max_bias,
202 const float m0,
203 const float m1,
204 const int n_head_log2,
205 const float logit_softcap,
206 const int n_head_kv,
207 const global void* mask_void,
208 const ulong mask_offset,
209 const ulong mask_nb1,
210 const ulong mask_nb2,
211 const ulong mask_nb3,
212 const int mask_ne2,
213 const int mask_ne3,
214 const global void* sinks_void,
215 const ulong sinks_offset
216) {
217 const int tid = get_local_id(0);
218 const int head_batch_idx = get_global_id(1);
219
220 const int batch_idx = head_batch_idx / n_head;
221 const int head_idx = head_batch_idx % n_head;
222
223 const int gqa_ratio = n_head / n_head_kv;
224 const int head_kv_idx = head_idx / gqa_ratio;
225
226 const global char* q_base = (const global char*)q_void + q_offset;
227 const global char* k_base = (const global char*)k_void + k_offset;
228 const global char* v_base = (const global char*)v_void + v_offset;
229 global char* o_base = (global char*)o_void + o_offset;
230
231 const global char* mask_base = NULL;
232 if (mask_void != NULL) {
233 const int mask_head_idx = head_idx % mask_ne2;
234 const int mask_batch_idx = batch_idx % mask_ne3;
235 mask_base = (const global char*)mask_void + mask_offset + mask_batch_idx * mask_nb3 + mask_head_idx * mask_nb2;
236 }
237
238 ACC_TYPE4 q_priv[DK_VEC];
239 const ulong q_row_offset = batch_idx * q_nb3 + head_idx * q_nb2;
240 const global Q_DATA_TYPE4* q_ptr = (const global Q_DATA_TYPE4*)(q_base + q_row_offset);
241 #pragma unroll
242 for (int i = 0; i < DK_VEC; ++i) {
243 q_priv[i] = CONVERT_Q_ACC4(q_ptr[i]);
244 }
245
246#ifdef FA_HAVE_INT_DOT
247 // Quantise Q once per thread: 8 uints + qd + q_sum per block.
248 uint q_packed[DK_Q4_BLOCKS * 8];
249 float q_d_scale[DK_Q4_BLOCKS];
250 int q_sum_arr[DK_Q4_BLOCKS];
251 #pragma unroll
252 for (int b = 0; b < DK_Q4_BLOCKS; ++b) {
253 q4_q_block_info info = quant_q_block_int8_packed_q4(&q_priv[b * 8], &q_packed[b * 8]);
254 q_d_scale[b] = info.qd;
255 q_sum_arr[b] = info.q_sum;
256 }
257#endif
258
259 float slope = get_alibi_slope(max_bias, head_idx, n_head_log2, m0, m1);
260
261 const global ACC_TYPE* sinks_ptr = NULL;
262 if (sinks_void != NULL) {
263 sinks_ptr = (const global ACC_TYPE*)((const global char*)sinks_void + sinks_offset);
264 }
265
266 // One-pass online softmax (FA-2): single sweep over kv positions,
267 // updating per-thread (m_i, l_i, o_acc) per K. Eliminates the second
268 // K read of the original two-pass implementation.
269 ACC_TYPE m_i = (sinks_ptr != NULL) ? sinks_ptr[head_idx] : FA_M_INIT;
270 ACC_TYPE l_i = 0.0f;
271 ACC_TYPE4 o_acc[DV_VEC];
272 #pragma unroll
273 for (int i = 0; i < DV_VEC; ++i) o_acc[i] = (ACC_TYPE4)(0.0f);
274
275 for (int k_idx = tid; k_idx < n_kv; k_idx += Q1_WG_SIZE) {
276 const global char* k_row = k_base + batch_idx * k_nb3 + head_kv_idx * k_nb2 + k_idx * k_nb1;
277 const global char* v_row = v_base + batch_idx * v_nb3 + head_kv_idx * v_nb2 + k_idx * v_nb1;
278
279 ACC_TYPE score = 0.0f;
280 #pragma unroll
281 for (int b = 0; b < DK_Q4_BLOCKS; b++) {
282#ifdef FA_HAVE_INT_DOT
283 score += dot_q4_0_int(k_row + b * Q4_0_BLOCK_SIZE,
284 &q_packed[b * 8], q_d_scale[b], q_sum_arr[b]);
285#else
286 score += dot_q4_0_f32(k_row + b * Q4_0_BLOCK_SIZE, &q_priv[b * 8]);
287#endif
288 }
289 score *= scale;
290 if (logit_softcap > 0.0f) {
291 score = logit_softcap * tanh(score / logit_softcap);
292 }
293
294 if (mask_base != NULL) {
295 const global MASK_DATA_TYPE* mask_ptr = (const global MASK_DATA_TYPE*)(mask_base);
296 score += slope * (ACC_TYPE)mask_ptr[k_idx];
297 }
298
299 // Online softmax step.
300 const ACC_TYPE m_new = max(m_i, score);
301 const ACC_TYPE alpha = exp(m_i - m_new);
302 const ACC_TYPE p = exp(score - m_new);
303
304 l_i = alpha * l_i + p;
305 #pragma unroll
306 for (int i = 0; i < DV_VEC; ++i) o_acc[i] *= alpha;
307
308 #pragma unroll
309 for (int b = 0; b < DV_Q4_BLOCKS; b++) {
310 ACC_TYPE4 v_dequant[8];
311 dequant_q4_0_f32(v_row + b * Q4_0_BLOCK_SIZE, v_dequant);
312 #pragma unroll
313 for (int i = 0; i < 8; i++) {
314 o_acc[b * 8 + i] = mad(p, v_dequant[i], o_acc[b * 8 + i]);
315 }
316 }
317
318 m_i = m_new;
319 }
320
321 // Cross-thread reduce: max(m_i) -> m_final, rescale per-thread l_i and
322 // o_acc by alpha = exp(m_i_thread - m_final) before sum-reduce.
323 __local ACC_TYPE local_m[Q1_WG_SIZE];
324 local_m[tid] = m_i;
325 barrier(CLK_LOCAL_MEM_FENCE);
326 #pragma unroll
327 for (int s = Q1_WG_SIZE / 2; s > 0; s >>= 1) {
328 if (tid < s) local_m[tid] = max(local_m[tid], local_m[tid + s]);
329 barrier(CLK_LOCAL_MEM_FENCE);
330 }
331 const ACC_TYPE m_final = local_m[0];
332
333 const ACC_TYPE alpha_final = exp(m_i - m_final);
334 l_i *= alpha_final;
335 #pragma unroll
336 for (int i = 0; i < DV_VEC; ++i) o_acc[i] *= alpha_final;
337
338 __local ACC_TYPE local_l[Q1_WG_SIZE];
339 __local ACC_TYPE4 local_o_comp[Q1_WG_SIZE];
340 local_l[tid] = l_i;
341 barrier(CLK_LOCAL_MEM_FENCE);
342 #pragma unroll
343 for (int s = Q1_WG_SIZE / 2; s > 0; s >>= 1) {
344 if (tid < s) local_l[tid] += local_l[tid + s];
345 barrier(CLK_LOCAL_MEM_FENCE);
346 }
347
348 const ulong o_row_offset = batch_idx * o_nb3 + head_idx * o_nb1;
349 global O_DATA_TYPE4 *o_row = (global O_DATA_TYPE4 *)(o_base + o_row_offset);
350 ACC_TYPE l_final = local_l[0];
351
352 if (sinks_ptr != NULL) {
353 l_final += exp(sinks_ptr[head_idx] - m_final);
354 }
355
356 if (l_final > 0.0f) {
357 const ACC_TYPE l_inv = 1.0f / l_final;
358 for (int i = 0; i < DV_VEC; i++) {
359 local_o_comp[tid] = o_acc[i];
360 barrier(CLK_LOCAL_MEM_FENCE);
361 #pragma unroll
362 for (int s = Q1_WG_SIZE / 2; s > 0; s >>= 1) {
363 if (tid < s) local_o_comp[tid] += local_o_comp[tid + s];
364 barrier(CLK_LOCAL_MEM_FENCE);
365 }
366 if (tid == 0) {
367 o_row[i] = CONVERT_O_DATA4(local_o_comp[0] * l_inv);
368 }
369 }
370 } else if (tid == 0) {
371 #pragma unroll
372 for (int i = 0; i < DV_VEC; ++i) o_row[i] = (O_DATA_TYPE4)(0.0f);
373 }
374}
375
376#ifdef cl_intel_subgroups
377#pragma OPENCL EXTENSION cl_intel_subgroups : enable
378#else
379#pragma OPENCL EXTENSION cl_khr_subgroups : enable
380#endif
381
382#ifdef cl_qcom_reqd_sub_group_size
383#pragma OPENCL EXTENSION cl_qcom_reqd_sub_group_size : enable
384#define REQD_SUBGROUP_SIZE_64 __attribute__((qcom_reqd_sub_group_size("half")))
385#else
386#define REQD_SUBGROUP_SIZE_64
387#endif
388
389#define VEC_NSG 4
390#define VEC_WG_SIZE (Q1_WG_SIZE * VEC_NSG)
391#define Q1V_DV_PER_THREAD ((DV_VEC + Q1_WG_SIZE - 1) / Q1_WG_SIZE)
392
393// Dequant one float4 lane (0..7) from a q4_0 block.
394// Lanes 0..3 → low nibbles of qs[0..15], lanes 4..7 → high nibbles.
395inline float4 dequant_q4_0_lane(const global char * block_ptr, int lane) {
396 const float d = vload_half(0, (const global half *)block_ptr);
397 const global uchar * qs = (const global uchar *)(block_ptr + 2);
398 const int g = lane & 3;
399 const int shift = (lane < 4) ? 0 : 4;
400 return d * (float4)((float)((qs[g*4+0] >> shift) & 0x0F) - 8.0f,
401 (float)((qs[g*4+1] >> shift) & 0x0F) - 8.0f,
402 (float)((qs[g*4+2] >> shift) & 0x0F) - 8.0f,
403 (float)((qs[g*4+3] >> shift) & 0x0F) - 8.0f);
404}
405
406REQD_SUBGROUP_SIZE_64
407__kernel void flash_attn_f32_q4_0_q1_vec(
408 const global void * q_void, ulong q_offset,
409 const global void * k_void, ulong k_offset,
410 const global void * v_void, ulong v_offset,
411 global void * o_void, ulong o_offset,
412 const float scale,
413 const int n_q,
414 const int n_kv,
415 const int is_causal,
416 const int n_head,
417 const ulong q_nb1, const ulong q_nb2, const ulong q_nb3,
418 const ulong k_nb1, const ulong k_nb2, const ulong k_nb3,
419 const ulong v_nb1, const ulong v_nb2, const ulong v_nb3,
420 const ulong o_nb1, const ulong o_nb2, const ulong o_nb3,
421 const float max_bias,
422 const float m0,
423 const float m1,
424 const int n_head_log2,
425 const float logit_softcap,
426 const int n_head_kv,
427 const global void* mask_void,
428 const ulong mask_offset,
429 const ulong mask_nb1,
430 const ulong mask_nb2,
431 const ulong mask_nb3,
432 const int mask_ne2,
433 const int mask_ne3,
434 const global void* sinks_void,
435 const ulong sinks_offset
436) {
437 const int tid = get_local_id(0);
438 const int sgid = tid / Q1_WG_SIZE;
439 const int tid_sg = tid % Q1_WG_SIZE;
440 const int head_batch_idx = get_global_id(1);
441
442 const int batch_idx = head_batch_idx / n_head;
443 const int head_idx = head_batch_idx % n_head;
444
445 const int gqa_ratio = n_head / n_head_kv;
446 const int head_kv_idx = head_idx / gqa_ratio;
447
448 const global char * q_base = (const global char *) q_void + q_offset;
449 const global char * k_base = (const global char *) k_void + k_offset;
450 const global char * v_base = (const global char *) v_void + v_offset;
451 global char * o_base = (global char *) o_void + o_offset;
452
453 const global char * mask_base = NULL;
454 if (mask_void != NULL) {
455 const int mask_head_idx = head_idx % mask_ne2;
456 const int mask_batch_idx = batch_idx % mask_ne3;
457 mask_base = (const global char *) mask_void + mask_offset +
458 mask_batch_idx * mask_nb3 + mask_head_idx * mask_nb2;
459 }
460
461 __local ACC_TYPE4 q_shared[DK_VEC];
462 {
463 const ulong q_row_offset = batch_idx * q_nb3 + head_idx * q_nb2;
464 const global Q_DATA_TYPE4 * q_ptr = (const global Q_DATA_TYPE4 *) (q_base + q_row_offset);
465 for (int i = tid; i < DK_VEC; i += VEC_WG_SIZE) {
466 q_shared[i] = CONVERT_Q_ACC4(q_ptr[i]);
467 }
468 }
469 barrier(CLK_LOCAL_MEM_FENCE);
470
471#ifdef FA_HAVE_INT_DOT
472 // quantize Q to int8-packed uints + per-block (qd, q_sum) once per WG for dp4a
473 // one thread per Q block, remaining threads idle this step
474 __local uint q_packed_shared[DK_Q4_BLOCKS * 8];
475 __local float q_d_shared[DK_Q4_BLOCKS];
476 __local int q_sum_shared[DK_Q4_BLOCKS];
477 if (tid < DK_Q4_BLOCKS) {
478 ACC_TYPE4 q_block[8];
479 #pragma unroll
480 for (int i = 0; i < 8; ++i) q_block[i] = q_shared[tid * 8 + i];
481 uint packed[8];
482 q4_q_block_info info = quant_q_block_int8_packed_q4(q_block, packed);
483 #pragma unroll
484 for (int i = 0; i < 8; ++i) q_packed_shared[tid * 8 + i] = packed[i];
485 q_d_shared[tid] = info.qd;
486 q_sum_shared[tid] = info.q_sum;
487 }
488 barrier(CLK_LOCAL_MEM_FENCE);
489#endif
490
491 const float slope = get_alibi_slope(max_bias, head_idx, n_head_log2, m0, m1);
492
493 const global ACC_TYPE * sinks_ptr = NULL;
494 if (sinks_void != NULL) {
495 sinks_ptr = (const global ACC_TYPE *) ((const global char *) sinks_void + sinks_offset);
496 }
497
498 ACC_TYPE4 o_acc[Q1V_DV_PER_THREAD];
499 #pragma unroll
500 for (int i = 0; i < Q1V_DV_PER_THREAD; ++i) o_acc[i] = (ACC_TYPE4)(0.0f);
501
502 ACC_TYPE m_i = FA_M_INIT;
503 ACC_TYPE l_i = 0.0f;
504
505 const int kv_per_sg = (n_kv + VEC_NSG - 1) / VEC_NSG;
506 const int kv_start = sgid * kv_per_sg;
507 const int kv_end = min(n_kv, kv_start + kv_per_sg);
508
509 for (int k_idx = kv_start; k_idx < kv_end; ++k_idx) {
510 const global char * k_row = k_base + batch_idx * k_nb3 + head_kv_idx * k_nb2 + k_idx * k_nb1;
511 const global char * v_row = v_base + batch_idx * v_nb3 + head_kv_idx * v_nb2 + k_idx * v_nb1;
512
513#ifdef FA_HAVE_INT_DOT
514 // per-lane dp4a: each lane packs 4 raw q4_0 nibbles into a uint,
515 // then dot_acc_sat_4x8packed_ss_int against the matching uint.
516 ACC_TYPE lane_contrib = 0.0f;
517 for (int qk = tid_sg; qk < DK_VEC; qk += Q1_WG_SIZE) {
518 const int block_idx = qk / 8;
519 const int lane_in_block = qk % 8;
520 const int g = lane_in_block & 3;
521 const int shift = (lane_in_block < 4) ? 0 : 4;
522 const global char * k_block = k_row + block_idx * Q4_0_BLOCK_SIZE;
523 const float kd = vload_half(0, (const global half *)k_block);
524 const global uchar * k_qs = (const global uchar *)(k_block + 2);
525 const uchar b0 = k_qs[g*4 + 0];
526 const uchar b1 = k_qs[g*4 + 1];
527 const uchar b2 = k_qs[g*4 + 2];
528 const uchar b3 = k_qs[g*4 + 3];
529 const uint k_packed = ((uint)((b0 >> shift) & 0x0F)) |
530 ((uint)((b1 >> shift) & 0x0F)) << 8 |
531 ((uint)((b2 >> shift) & 0x0F)) << 16 |
532 ((uint)((b3 >> shift) & 0x0F)) << 24;
533 const uint q_packed_lane = q_packed_shared[block_idx * 8 + lane_in_block];
534 const int raw_dot = dot_acc_sat_4x8packed_ss_int(q_packed_lane, k_packed, 0);
535 const float qd = q_d_shared[block_idx];
536 const float block_scale = qd * kd;
537 float contrib = (float)raw_dot * block_scale;
538 if (lane_in_block == 0) {
539 // block bias correction is per-block
540 const int q_sum_b = q_sum_shared[block_idx];
541 contrib -= 8.0f * block_scale * (float)q_sum_b;
542 }
543 lane_contrib += contrib;
544 }
545 ACC_TYPE score = sub_group_reduce_add(lane_contrib) * scale;
546#else
547 ACC_TYPE4 dot4 = (ACC_TYPE4)(0.0f);
548 for (int qk = tid_sg; qk < DK_VEC; qk += Q1_WG_SIZE) {
549 const int block_idx = qk / 8;
550 const int lane = qk % 8;
551 const float4 k_v = dequant_q4_0_lane(k_row + block_idx * Q4_0_BLOCK_SIZE, lane);
552 dot4 = mad(q_shared[qk], k_v, dot4);
553 }
554 ACC_TYPE dot_partial = dot4.s0 + dot4.s1 + dot4.s2 + dot4.s3;
555 ACC_TYPE score = sub_group_reduce_add(dot_partial) * scale;
556#endif
557 if (logit_softcap > 0.0f) {
558 score = logit_softcap * tanh(score / logit_softcap);
559 }
560
561 if (mask_base != NULL) {
562 const global MASK_DATA_TYPE * mask_ptr = (const global MASK_DATA_TYPE *) mask_base;
563 score += slope * (ACC_TYPE) mask_ptr[k_idx];
564 }
565
566 const ACC_TYPE m_new = max(m_i, score);
567 const ACC_TYPE scale_prev = native_exp(m_i - m_new);
568 const ACC_TYPE p = native_exp(score - m_new);
569
570 int idx = 0;
571 for (int dv = tid_sg; dv < DV_VEC; dv += Q1_WG_SIZE, ++idx) {
572 const int block_idx = dv / 8;
573 const int lane = dv % 8;
574 const float4 v_v = dequant_q4_0_lane(v_row + block_idx * Q4_0_BLOCK_SIZE, lane);
575 o_acc[idx] = mad(p, v_v, o_acc[idx] * scale_prev);
576 }
577 l_i = l_i * scale_prev + p;
578 m_i = m_new;
579 }
580
581 __local ACC_TYPE sg_m[VEC_NSG];
582 __local ACC_TYPE sg_l[VEC_NSG];
583 __local ACC_TYPE4 sg_o[VEC_NSG][DV_VEC];
584
585 if (tid_sg == 0) {
586 sg_m[sgid] = m_i;
587 sg_l[sgid] = l_i;
588 }
589 {
590 int idx = 0;
591 for (int dv = tid_sg; dv < DV_VEC; dv += Q1_WG_SIZE, ++idx) {
592 sg_o[sgid][dv] = o_acc[idx];
593 }
594 }
595 barrier(CLK_LOCAL_MEM_FENCE);
596
597 if (sgid == 0) {
598 ACC_TYPE m_final = sg_m[0];
599 #pragma unroll
600 for (int s = 1; s < VEC_NSG; ++s) {
601 m_final = max(m_final, sg_m[s]);
602 }
603 if (sinks_ptr != NULL) {
604 m_final = max(m_final, sinks_ptr[head_idx]);
605 }
606
607 ACC_TYPE l_final = 0.0f;
608 #pragma unroll
609 for (int s = 0; s < VEC_NSG; ++s) {
610 l_final += sg_l[s] * native_exp(sg_m[s] - m_final);
611 }
612 if (sinks_ptr != NULL) {
613 l_final += native_exp(sinks_ptr[head_idx] - m_final);
614 }
615 const ACC_TYPE l_inv = (l_final > 0.0f) ? (1.0f / l_final) : 0.0f;
616
617 const ulong o_row_offset = batch_idx * o_nb3 + head_idx * o_nb1;
618 global O_DATA_TYPE4 * o_row = (global O_DATA_TYPE4 *) (o_base + o_row_offset);
619
620 int idx = 0;
621 for (int dv = tid_sg; dv < DV_VEC; dv += Q1_WG_SIZE, ++idx) {
622 ACC_TYPE4 o_merged = (ACC_TYPE4)(0.0f);
623 #pragma unroll
624 for (int s = 0; s < VEC_NSG; ++s) {
625 const ACC_TYPE alpha = native_exp(sg_m[s] - m_final);
626 o_merged = mad((ACC_TYPE4)(alpha), sg_o[s][dv], o_merged);
627 }
628 o_row[dv] = CONVERT_O_DATA4(o_merged * l_inv);
629 }
630 }
631}
632
633// Flash-decoding split pass for q4_0 KV. Merge kernel is type-agnostic and
634// shared with the f16/q8_0 FA kernels.
635#define FA_PARTIAL_FLOATS (2 + DV)
636
637__kernel void flash_attn_f32_q4_0_q1_split(
638 const global void * q_void, ulong q_offset,
639 const global void * k_void, ulong k_offset,
640 const global void * v_void, ulong v_offset,
641 const float scale,
642 const int n_q,
643 const int n_kv,
644 const int n_head,
645 const ulong q_nb1, const ulong q_nb2, const ulong q_nb3,
646 const ulong k_nb1, const ulong k_nb2, const ulong k_nb3,
647 const ulong v_nb1, const ulong v_nb2, const ulong v_nb3,
648 const float max_bias,
649 const float m0,
650 const float m1,
651 const int n_head_log2,
652 const float logit_softcap,
653 const int n_head_kv,
654 const global void * mask_void,
655 const ulong mask_offset,
656 const ulong mask_nb1,
657 const ulong mask_nb2,
658 const ulong mask_nb3,
659 const int mask_ne2,
660 const int mask_ne3,
661 global float * partial_void,
662 const int n_splits,
663 const int kv_per_split
664) {
665 const int tid = get_local_id(0);
666 const int head_batch_idx = get_global_id(1);
667 const int split_q_idx = get_global_id(2);
668 const int split_idx = split_q_idx % n_splits;
669 const int q_idx = split_q_idx / n_splits;
670 const int batch_idx = head_batch_idx / n_head;
671 const int head_idx = head_batch_idx % n_head;
672 const int gqa_ratio = n_head / n_head_kv;
673 const int head_kv_idx = head_idx / gqa_ratio;
674
675 const int kv_start = split_idx * kv_per_split;
676 const int kv_end = min(kv_start + kv_per_split, n_kv);
677
678 const ulong record_stride = (ulong) FA_PARTIAL_FLOATS;
679 const ulong record_idx = ((((ulong) batch_idx * n_head + head_idx) * n_q + q_idx)
680 * n_splits + split_idx);
681 global float * rec = partial_void + record_idx * record_stride;
682 global float4 * rec_o = (global float4 *) (rec + 2);
683
684 if (kv_start >= kv_end) {
685 if (tid == 0) {
686 rec[0] = FA_M_INIT;
687 rec[1] = 0.0f;
688 }
689 return;
690 }
691
692 const global char * q_base = (const global char *) q_void + q_offset;
693 const global char * k_base = (const global char *) k_void + k_offset;
694 const global char * v_base = (const global char *) v_void + v_offset;
695
696 const global char * mask_base = NULL;
697 if (mask_void != NULL) {
698 const int mask_head_idx = head_idx % mask_ne2;
699 const int mask_batch_idx = batch_idx % mask_ne3;
700 mask_base = (const global char *) mask_void + mask_offset +
701 mask_batch_idx * mask_nb3 + mask_head_idx * mask_nb2 +
702 (ulong) q_idx * mask_nb1;
703 }
704
705 ACC_TYPE4 q_priv[DK_VEC];
706 const ulong q_row_offset = batch_idx * q_nb3 + head_idx * q_nb2 + (ulong) q_idx * q_nb1;
707 const global Q_DATA_TYPE4 * q_ptr = (const global Q_DATA_TYPE4 *) (q_base + q_row_offset);
708 #pragma unroll
709 for (int i = 0; i < DK_VEC; ++i) {
710 q_priv[i] = CONVERT_Q_ACC4(q_ptr[i]);
711 }
712
713#ifdef FA_HAVE_INT_DOT
714 uint q_packed[DK_Q4_BLOCKS * 8];
715 float q_d_scale[DK_Q4_BLOCKS];
716 int q_sum_arr[DK_Q4_BLOCKS];
717 #pragma unroll
718 for (int b = 0; b < DK_Q4_BLOCKS; ++b) {
719 q4_q_block_info info = quant_q_block_int8_packed_q4(&q_priv[b * 8], &q_packed[b * 8]);
720 q_d_scale[b] = info.qd;
721 q_sum_arr[b] = info.q_sum;
722 }
723#endif
724
725 const float slope = get_alibi_slope(max_bias, head_idx, n_head_log2, m0, m1);
726
727 // One-pass online softmax (FA-2): single sweep over the split's K range.
728 ACC_TYPE m_i = FA_M_INIT;
729 ACC_TYPE l_i = 0.0f;
730 ACC_TYPE4 o_acc[DV_VEC];
731 #pragma unroll
732 for (int i = 0; i < DV_VEC; ++i) o_acc[i] = (ACC_TYPE4)(0.0f);
733
734 for (int k_idx = kv_start + tid; k_idx < kv_end; k_idx += Q1_WG_SIZE) {
735 const global char * k_row = k_base + batch_idx * k_nb3 + head_kv_idx * k_nb2 + k_idx * k_nb1;
736 const global char * v_row = v_base + batch_idx * v_nb3 + head_kv_idx * v_nb2 + k_idx * v_nb1;
737 ACC_TYPE score = 0.0f;
738 #pragma unroll
739 for (int b = 0; b < DK_Q4_BLOCKS; ++b) {
740#ifdef FA_HAVE_INT_DOT
741 score += dot_q4_0_int(k_row + b * Q4_0_BLOCK_SIZE,
742 &q_packed[b * 8], q_d_scale[b], q_sum_arr[b]);
743#else
744 score += dot_q4_0_f32(k_row + b * Q4_0_BLOCK_SIZE, &q_priv[b * 8]);
745#endif
746 }
747 score *= scale;
748 if (logit_softcap > 0.0f) {
749 score = logit_softcap * tanh(score / logit_softcap);
750 }
751 if (mask_base != NULL) {
752 const global MASK_DATA_TYPE * mask_ptr = (const global MASK_DATA_TYPE *) (mask_base);
753 score += slope * (ACC_TYPE) mask_ptr[k_idx];
754 }
755
756 // Online softmax step.
757 const ACC_TYPE m_new = max(m_i, score);
758 const ACC_TYPE alpha = exp(m_i - m_new);
759 const ACC_TYPE p = exp(score - m_new);
760
761 l_i = alpha * l_i + p;
762 #pragma unroll
763 for (int i = 0; i < DV_VEC; ++i) o_acc[i] *= alpha;
764
765 #pragma unroll
766 for (int b = 0; b < DV_Q4_BLOCKS; ++b) {
767 ACC_TYPE4 v_dequant[8];
768 dequant_q4_0_f32(v_row + b * Q4_0_BLOCK_SIZE, v_dequant);
769 #pragma unroll
770 for (int i = 0; i < 8; ++i) {
771 o_acc[b * 8 + i] = mad(p, v_dequant[i], o_acc[b * 8 + i]);
772 }
773 }
774
775 m_i = m_new;
776 }
777
778 // Cross-thread reduce: max(m_i) -> m_c, rescale per-thread l_i and o_acc
779 // by alpha = exp(m_i_thread - m_c) before sum-reduce.
780 __local ACC_TYPE local_m[Q1_WG_SIZE];
781 local_m[tid] = m_i;
782 barrier(CLK_LOCAL_MEM_FENCE);
783 #pragma unroll
784 for (int s = Q1_WG_SIZE / 2; s > 0; s >>= 1) {
785 if (tid < s) local_m[tid] = max(local_m[tid], local_m[tid + s]);
786 barrier(CLK_LOCAL_MEM_FENCE);
787 }
788 const ACC_TYPE m_c = local_m[0];
789
790 const ACC_TYPE alpha_final = exp(m_i - m_c);
791 l_i *= alpha_final;
792 #pragma unroll
793 for (int i = 0; i < DV_VEC; ++i) o_acc[i] *= alpha_final;
794
795 __local ACC_TYPE local_l[Q1_WG_SIZE];
796 __local ACC_TYPE4 local_o[Q1_WG_SIZE];
797 local_l[tid] = l_i;
798 barrier(CLK_LOCAL_MEM_FENCE);
799 #pragma unroll
800 for (int s = Q1_WG_SIZE / 2; s > 0; s >>= 1) {
801 if (tid < s) local_l[tid] += local_l[tid + s];
802 barrier(CLK_LOCAL_MEM_FENCE);
803 }
804 const ACC_TYPE l_c = local_l[0];
805
806 if (tid == 0) {
807 rec[0] = (float) m_c;
808 rec[1] = (float) l_c;
809 }
810 for (int i = 0; i < DV_VEC; ++i) {
811 local_o[tid] = o_acc[i];
812 barrier(CLK_LOCAL_MEM_FENCE);
813 #pragma unroll
814 for (int s = Q1_WG_SIZE / 2; s > 0; s >>= 1) {
815 if (tid < s) local_o[tid] += local_o[tid + s];
816 barrier(CLK_LOCAL_MEM_FENCE);
817 }
818 if (tid == 0) {
819 rec_o[i] = local_o[0];
820 }
821 }
822}
823
824// Prefill: q4_0 K/V, n_q > 1. BLOCK_M × BLOCK_N tiling.
825// K in local as packed nibbles + per-block scale; V dequant -> half in local.
826// Requires DK % QK4_0 == 0 and DV % QK4_0 == 0.
827#define KV_DATA_TYPE4 half4
828#define CONVERT_KV_ACC4(x) convert_float4(x)
829
830#define DK_Q4_BLOCKS_PREFILL (DK / QK4_0)
831#define DV_Q4_BLOCKS_PREFILL (DV / QK4_0)
832
833// N_SPLIT>1 splits DK/DV across N_SPLIT threads per query row; needs
834// sub_group_shuffle_xor and DK_Q4_BLOCKS_PREFILL % N_SPLIT == 0.
835#ifndef N_SPLIT
836#define N_SPLIT 1
837#endif
838
839#if N_SPLIT > 1
840#define SPLIT_DK_VEC (DK_VEC / N_SPLIT)
841#define SPLIT_DV_VEC (DV_VEC / N_SPLIT)
842#define SPLIT_DK_Q4_BLOCKS (DK_Q4_BLOCKS_PREFILL / N_SPLIT)
843#define WG_SIZE (BLOCK_M * N_SPLIT)
844#else
845#define SPLIT_DK_VEC DK_VEC
846#define SPLIT_DV_VEC DV_VEC
847#define SPLIT_DK_Q4_BLOCKS DK_Q4_BLOCKS_PREFILL
848#define WG_SIZE BLOCK_M
849#endif
850
851#ifndef MQ_GQA
852#define MQ_GQA 4
853#endif
854#ifndef MQ_NSG_SPLIT
855#define MQ_NSG_SPLIT 4
856#endif
857#define MQ_SPLIT_WG_SIZE_Q4 (Q1_WG_SIZE * MQ_NSG_SPLIT)
858
859REQD_SUBGROUP_SIZE_64
860__kernel void flash_attn_f32_q4_0_q1_vec_mq_split(
861 const global void * q_void, ulong q_offset,
862 const global void * k_void, ulong k_offset,
863 const global void * v_void, ulong v_offset,
864 const float scale,
865 const int n_q,
866 const int n_kv,
867 const int n_head,
868 const ulong q_nb1, const ulong q_nb2, const ulong q_nb3,
869 const ulong k_nb1, const ulong k_nb2, const ulong k_nb3,
870 const ulong v_nb1, const ulong v_nb2, const ulong v_nb3,
871 const float max_bias,
872 const float m0,
873 const float m1,
874 const int n_head_log2,
875 const float logit_softcap,
876 const int n_head_kv,
877 const global void * mask_void,
878 const ulong mask_offset,
879 const ulong mask_nb1,
880 const ulong mask_nb2,
881 const ulong mask_nb3,
882 const int mask_ne2,
883 const int mask_ne3,
884 global float * partial_void,
885 const int n_splits,
886 const int kv_per_split
887) {
888 const int tid = get_local_id(0);
889 const int sgid = tid / Q1_WG_SIZE;
890 const int tid_sg = tid % Q1_WG_SIZE;
891 const int kvhead_batch_idx = get_global_id(1);
892 const int split_q_idx = get_global_id(2);
893 const int split_idx = split_q_idx % n_splits;
894 const int q_idx = split_q_idx / n_splits;
895
896 const int batch_idx = kvhead_batch_idx / n_head_kv;
897 const int head_kv_idx = kvhead_batch_idx % n_head_kv;
898
899 const int kv_start = split_idx * kv_per_split;
900 const int kv_end = min(kv_start + kv_per_split, n_kv);
901
902 const ulong record_stride = (ulong) FA_PARTIAL_FLOATS;
903
904 if (kv_start >= kv_end) {
905 if (tid == 0) {
906 #pragma unroll
907 for (int h = 0; h < MQ_GQA; ++h) {
908 const int head_idx = head_kv_idx * MQ_GQA + h;
909 const ulong rec_idx = ((((ulong) batch_idx * n_head + head_idx) * n_q + q_idx)
910 * n_splits + split_idx);
911 global float * rec = partial_void + rec_idx * record_stride;
912 rec[0] = FA_M_INIT;
913 rec[1] = 0.0f;
914 }
915 }
916 return;
917 }
918
919 const global char * q_base = (const global char *) q_void + q_offset;
920 const global char * k_base = (const global char *) k_void + k_offset;
921 const global char * v_base = (const global char *) v_void + v_offset;
922
923 __local ACC_TYPE4 q_shared[MQ_GQA * DK_VEC];
924 for (int i = tid; i < MQ_GQA * DK_VEC; i += MQ_SPLIT_WG_SIZE_Q4) {
925 const int h = i / DK_VEC;
926 const int k = i % DK_VEC;
927 const int head_idx = head_kv_idx * MQ_GQA + h;
928 const ulong q_row_offset = batch_idx * q_nb3 + head_idx * q_nb2 + (ulong) q_idx * q_nb1;
929 const global Q_DATA_TYPE4 * q_ptr = (const global Q_DATA_TYPE4 *) (q_base + q_row_offset);
930 q_shared[h * DK_VEC + k] = CONVERT_Q_ACC4(q_ptr[k]);
931 }
932 barrier(CLK_LOCAL_MEM_FENCE);
933
934#ifdef FA_HAVE_INT_DOT
935 __local uint q_packed_shared[MQ_GQA * DK_Q4_BLOCKS * 8];
936 __local float q_d_shared[MQ_GQA * DK_Q4_BLOCKS];
937 __local int q_sum_shared[MQ_GQA * DK_Q4_BLOCKS];
938 {
939 const int active = MQ_GQA * DK_Q4_BLOCKS;
940 if (tid < active) {
941 const int h = tid / DK_Q4_BLOCKS;
942 const int block_id = tid % DK_Q4_BLOCKS;
943 ACC_TYPE4 q_block[8];
944 #pragma unroll
945 for (int i = 0; i < 8; ++i) q_block[i] = q_shared[h * DK_VEC + block_id * 8 + i];
946 uint packed[8];
947 q4_q_block_info info = quant_q_block_int8_packed_q4(q_block, packed);
948 #pragma unroll
949 for (int i = 0; i < 8; ++i) q_packed_shared[(h * DK_Q4_BLOCKS + block_id) * 8 + i] = packed[i];
950 q_d_shared[h * DK_Q4_BLOCKS + block_id] = info.qd;
951 q_sum_shared[h * DK_Q4_BLOCKS + block_id] = info.q_sum;
952 }
953 }
954 barrier(CLK_LOCAL_MEM_FENCE);
955#endif
956
957 float slope[MQ_GQA];
958 #pragma unroll
959 for (int h = 0; h < MQ_GQA; ++h) {
960 slope[h] = get_alibi_slope(max_bias, head_kv_idx * MQ_GQA + h, n_head_log2, m0, m1);
961 }
962
963 const global char * mask_base[MQ_GQA];
964 if (mask_void != NULL) {
965 const int mask_batch_idx = batch_idx % mask_ne3;
966 const global char * mask_base_b = (const global char *) mask_void + mask_offset +
967 mask_batch_idx * mask_nb3 +
968 (ulong) q_idx * mask_nb1;
969 #pragma unroll
970 for (int h = 0; h < MQ_GQA; ++h) {
971 const int head_idx = head_kv_idx * MQ_GQA + h;
972 const int mask_head_idx = head_idx % mask_ne2;
973 mask_base[h] = mask_base_b + mask_head_idx * mask_nb2;
974 }
975 } else {
976 #pragma unroll
977 for (int h = 0; h < MQ_GQA; ++h) mask_base[h] = NULL;
978 }
979
980 ACC_TYPE4 o_acc[MQ_GQA][Q1V_DV_PER_THREAD];
981 ACC_TYPE m_i[MQ_GQA];
982 ACC_TYPE l_i[MQ_GQA];
983 #pragma unroll
984 for (int h = 0; h < MQ_GQA; ++h) {
985 m_i[h] = FA_M_INIT;
986 l_i[h] = 0.0f;
987 #pragma unroll
988 for (int i = 0; i < Q1V_DV_PER_THREAD; ++i) o_acc[h][i] = (ACC_TYPE4)(0.0f);
989 }
990
991 const int kv_len = kv_end - kv_start;
992 const int kv_per_sg = (kv_len + MQ_NSG_SPLIT - 1) / MQ_NSG_SPLIT;
993 const int kv_lo = kv_start + sgid * kv_per_sg;
994 const int kv_hi = min(kv_end, kv_lo + kv_per_sg);
995
996 for (int k_idx = kv_lo; k_idx < kv_hi; ++k_idx) {
997 const global char * k_row = k_base + batch_idx * k_nb3 + head_kv_idx * k_nb2 + k_idx * k_nb1;
998 const global char * v_row = v_base + batch_idx * v_nb3 + head_kv_idx * v_nb2 + k_idx * v_nb1;
999
1000#ifdef FA_HAVE_INT_DOT
1001 ACC_TYPE lane_contrib[MQ_GQA];
1002 #pragma unroll
1003 for (int h = 0; h < MQ_GQA; ++h) lane_contrib[h] = 0.0f;
1004
1005 for (int qk = tid_sg; qk < DK_VEC; qk += Q1_WG_SIZE) {
1006 const int block_idx = qk / 8;
1007 const int lane_in_block = qk % 8;
1008 const int g = lane_in_block & 3;
1009 const int shift = (lane_in_block < 4) ? 0 : 4;
1010 const global char * k_block = k_row + block_idx * Q4_0_BLOCK_SIZE;
1011 const float kd = vload_half(0, (const global half *)k_block);
1012 const global uchar * k_qs = (const global uchar *)(k_block + 2);
1013 const uchar b0 = k_qs[g*4 + 0];
1014 const uchar b1 = k_qs[g*4 + 1];
1015 const uchar b2 = k_qs[g*4 + 2];
1016 const uchar b3 = k_qs[g*4 + 3];
1017 const uint k_packed = ((uint)((b0 >> shift) & 0x0F)) |
1018 ((uint)((b1 >> shift) & 0x0F)) << 8 |
1019 ((uint)((b2 >> shift) & 0x0F)) << 16 |
1020 ((uint)((b3 >> shift) & 0x0F)) << 24;
1021 #pragma unroll
1022 for (int h = 0; h < MQ_GQA; ++h) {
1023 const uint q_packed_lane = q_packed_shared[(h * DK_Q4_BLOCKS + block_idx) * 8 + lane_in_block];
1024 const int raw_dot = dot_acc_sat_4x8packed_ss_int(q_packed_lane, k_packed, 0);
1025 const float qd = q_d_shared[h * DK_Q4_BLOCKS + block_idx];
1026 const float block_scale = qd * kd;
1027 float contrib = (float) raw_dot * block_scale;
1028 if (lane_in_block == 0) {
1029 const int q_sum_b = q_sum_shared[h * DK_Q4_BLOCKS + block_idx];
1030 contrib -= 8.0f * block_scale * (float) q_sum_b;
1031 }
1032 lane_contrib[h] += contrib;
1033 }
1034 }
1035
1036 ACC_TYPE score[MQ_GQA];
1037 #pragma unroll
1038 for (int h = 0; h < MQ_GQA; ++h) {
1039 ACC_TYPE s = sub_group_reduce_add(lane_contrib[h]) * scale;
1040 if (logit_softcap > 0.0f) {
1041 s = logit_softcap * tanh(s / logit_softcap);
1042 }
1043 if (mask_base[h] != NULL) {
1044 const global MASK_DATA_TYPE * mask_ptr = (const global MASK_DATA_TYPE *) mask_base[h];
1045 s += slope[h] * (ACC_TYPE) mask_ptr[k_idx];
1046 }
1047 score[h] = s;
1048 }
1049#else
1050 // fallback float-dequant K dot
1051 ACC_TYPE4 dot4[MQ_GQA];
1052 #pragma unroll
1053 for (int h = 0; h < MQ_GQA; ++h) dot4[h] = (ACC_TYPE4)(0.0f);
1054
1055 for (int qk = tid_sg; qk < DK_VEC; qk += Q1_WG_SIZE) {
1056 const int block_idx = qk / 8;
1057 const int lane = qk % 8;
1058 const float4 k_v = dequant_q4_0_lane(k_row + block_idx * Q4_0_BLOCK_SIZE, lane);
1059 #pragma unroll
1060 for (int h = 0; h < MQ_GQA; ++h) {
1061 dot4[h] = mad(q_shared[h * DK_VEC + qk], k_v, dot4[h]);
1062 }
1063 }
1064
1065 ACC_TYPE score[MQ_GQA];
1066 #pragma unroll
1067 for (int h = 0; h < MQ_GQA; ++h) {
1068 const ACC_TYPE dot_partial = dot4[h].s0 + dot4[h].s1 + dot4[h].s2 + dot4[h].s3;
1069 ACC_TYPE s = sub_group_reduce_add(dot_partial) * scale;
1070 if (logit_softcap > 0.0f) {
1071 s = logit_softcap * tanh(s / logit_softcap);
1072 }
1073 if (mask_base[h] != NULL) {
1074 const global MASK_DATA_TYPE * mask_ptr = (const global MASK_DATA_TYPE *) mask_base[h];
1075 s += slope[h] * (ACC_TYPE) mask_ptr[k_idx];
1076 }
1077 score[h] = s;
1078 }
1079#endif
1080
1081 ACC_TYPE p_h[MQ_GQA];
1082 ACC_TYPE sp_h[MQ_GQA];
1083 #pragma unroll
1084 for (int h = 0; h < MQ_GQA; ++h) {
1085 const ACC_TYPE m_new = max(m_i[h], score[h]);
1086 sp_h[h] = native_exp(m_i[h] - m_new);
1087 p_h[h] = native_exp(score[h] - m_new);
1088 l_i[h] = l_i[h] * sp_h[h] + p_h[h];
1089 m_i[h] = m_new;
1090 }
1091
1092 int idx = 0;
1093 for (int dv = tid_sg; dv < DV_VEC; dv += Q1_WG_SIZE, ++idx) {
1094 const int block_idx = dv / 8;
1095 const int lane = dv % 8;
1096 const float4 v_v = dequant_q4_0_lane(v_row + block_idx * Q4_0_BLOCK_SIZE, lane);
1097 #pragma unroll
1098 for (int h = 0; h < MQ_GQA; ++h) {
1099 o_acc[h][idx] = mad(p_h[h], v_v, o_acc[h][idx] * sp_h[h]);
1100 }
1101 }
1102 }
1103
1104 // per-h cross-subgroup merge
1105 __local ACC_TYPE sg_m[MQ_GQA][MQ_NSG_SPLIT];
1106 __local ACC_TYPE sg_l[MQ_GQA][MQ_NSG_SPLIT];
1107 __local ACC_TYPE4 sg_o[MQ_NSG_SPLIT][DV_VEC];
1108
1109 if (tid_sg == 0) {
1110 #pragma unroll
1111 for (int h = 0; h < MQ_GQA; ++h) {
1112 sg_m[h][sgid] = m_i[h];
1113 sg_l[h][sgid] = l_i[h];
1114 }
1115 }
1116
1117 #pragma unroll
1118 for (int h = 0; h < MQ_GQA; ++h) {
1119 {
1120 int idx = 0;
1121 for (int dv_idx = tid_sg; dv_idx < DV_VEC; dv_idx += Q1_WG_SIZE, ++idx) {
1122 sg_o[sgid][dv_idx] = o_acc[h][idx];
1123 }
1124 }
1125 barrier(CLK_LOCAL_MEM_FENCE);
1126
1127 if (sgid == 0) {
1128 const int head_idx = head_kv_idx * MQ_GQA + h;
1129
1130 ACC_TYPE m_c = sg_m[h][0];
1131 #pragma unroll
1132 for (int s = 1; s < MQ_NSG_SPLIT; ++s) {
1133 m_c = max(m_c, sg_m[h][s]);
1134 }
1135 ACC_TYPE l_c = 0.0f;
1136 #pragma unroll
1137 for (int s = 0; s < MQ_NSG_SPLIT; ++s) {
1138 l_c += sg_l[h][s] * native_exp(sg_m[h][s] - m_c);
1139 }
1140
1141 const ulong rec_idx = ((((ulong) batch_idx * n_head + head_idx) * n_q + q_idx)
1142 * n_splits + split_idx);
1143 global float * rec = partial_void + rec_idx * record_stride;
1144 global float4 * rec_o = (global float4 *) (rec + 2);
1145
1146 if (tid_sg == 0) {
1147 rec[0] = (float) m_c;
1148 rec[1] = (float) l_c;
1149 }
1150 for (int dv_idx = tid_sg; dv_idx < DV_VEC; dv_idx += Q1_WG_SIZE) {
1151 ACC_TYPE4 o_merged = (ACC_TYPE4)(0.0f);
1152 #pragma unroll
1153 for (int s = 0; s < MQ_NSG_SPLIT; ++s) {
1154 const ACC_TYPE alpha = native_exp(sg_m[h][s] - m_c);
1155 o_merged = mad((ACC_TYPE4)(alpha), sg_o[s][dv_idx], o_merged);
1156 }
1157 rec_o[dv_idx] = o_merged;
1158 }
1159 }
1160 barrier(CLK_LOCAL_MEM_FENCE);
1161 }
1162}
1163
1164// flash_attn_f32_q4_0_q1_vec_mq_split_c8 — cluster-parallel variant of the MQ
1165// split, port of flash_attn_f32_f16_q1_vec_mq_split_c8
1166// Requires dp4a + subgroup shuffles
1167
1168#if defined(FA_HAVE_INT_DOT) && defined(HAS_SUBGROUP_SHUFFLE)
1169
1170#ifndef FA_CL_C
1171#define FA_CL_C 8
1172#endif
1173
1174// Lane striping requires DK/DV to divide across the cluster (see f16 c8).
1175#if (DK_VEC % FA_CL_C) == 0 && (DV_VEC % FA_CL_C) == 0
1176#define FA_CL_NCL (Q1_WG_SIZE / FA_CL_C) // clusters (position streams) per subgroup
1177#define FA_CL_DKQ (DK_VEC / FA_CL_C) // K quartets per lane per row
1178#define FA_CL_DVQ (DV_VEC / FA_CL_C) // V quartets (o_acc float4s) per lane per head
1179
1180#ifdef FA_C8_NO_SG_PIN
1181#define FA_C8_SG_ATTR_Q4
1182#else
1183#define FA_C8_SG_ATTR_Q4 REQD_SUBGROUP_SIZE_64
1184#endif
1185
1186FA_C8_SG_ATTR_Q4
1187__kernel void flash_attn_f32_q4_0_q1_vec_mq_split_c8(
1188 const global void * q_void, ulong q_offset,
1189 const global void * k_void, ulong k_offset,
1190 const global void * v_void, ulong v_offset,
1191 const float scale,
1192 const int n_q,
1193 const int n_kv,
1194 const int n_head,
1195 const ulong q_nb1, const ulong q_nb2, const ulong q_nb3,
1196 const ulong k_nb1, const ulong k_nb2, const ulong k_nb3,
1197 const ulong v_nb1, const ulong v_nb2, const ulong v_nb3,
1198 const float max_bias,
1199 const float m0,
1200 const float m1,
1201 const int n_head_log2,
1202 const float logit_softcap,
1203 const int n_head_kv,
1204 const global void * mask_void,
1205 const ulong mask_offset,
1206 const ulong mask_nb1,
1207 const ulong mask_nb2,
1208 const ulong mask_nb3,
1209 const int mask_ne2,
1210 const int mask_ne3,
1211 global float * partial_void,
1212 const int n_splits,
1213 const int kv_per_split
1214) {
1215 const int tid = get_local_id(0);
1216 const int sgid = tid / Q1_WG_SIZE;
1217 const int tid_sg = tid % Q1_WG_SIZE;
1218 const int cl = tid_sg / FA_CL_C; // cluster id
1219 const int lic = tid_sg % FA_CL_C; // lane in cluster
1220 const int kvhead_batch_idx = get_global_id(1);
1221 const int split_q_idx = get_global_id(2);
1222 const int split_idx = split_q_idx % n_splits;
1223 const int q_idx = split_q_idx / n_splits;
1224
1225 const int batch_idx = kvhead_batch_idx / n_head_kv;
1226 const int head_kv_idx = kvhead_batch_idx % n_head_kv;
1227
1228 const int kv_start = split_idx * kv_per_split;
1229 const int kv_end = min(kv_start + kv_per_split, n_kv);
1230
1231 const ulong record_stride = (ulong) FA_PARTIAL_FLOATS;
1232
1233 if (kv_start >= kv_end) {
1234 if (tid == 0) {
1235 #pragma unroll
1236 for (int h = 0; h < MQ_GQA; ++h) {
1237 const int head_idx = head_kv_idx * MQ_GQA + h;
1238 const ulong rec_idx = ((((ulong) batch_idx * n_head + head_idx) * n_q + q_idx)
1239 * n_splits + split_idx);
1240 global float * rec = partial_void + rec_idx * record_stride;
1241 rec[0] = FA_M_INIT;
1242 rec[1] = 0.0f;
1243 }
1244 }
1245 return;
1246 }
1247
1248 const global char * q_base = (const global char *) q_void + q_offset;
1249 const global char * k_base = (const global char *) k_void + k_offset;
1250 const global char * v_base = (const global char *) v_void + v_offset;
1251
1252 // Stage MQ_GQA Q rows in __local as float4 (source for the quantize pass).
1253 __local ACC_TYPE4 q_shared[MQ_GQA * DK_VEC];
1254 for (int i = tid; i < MQ_GQA * DK_VEC; i += MQ_SPLIT_WG_SIZE_Q4) {
1255 const int h = i / DK_VEC;
1256 const int k = i % DK_VEC;
1257 const int head_idx = head_kv_idx * MQ_GQA + h;
1258 const ulong q_row_offset = batch_idx * q_nb3 + head_idx * q_nb2 + (ulong) q_idx * q_nb1;
1259 const global Q_DATA_TYPE4 * q_ptr = (const global Q_DATA_TYPE4 *) (q_base + q_row_offset);
1260 q_shared[h * DK_VEC + k] = CONVERT_Q_ACC4(q_ptr[k]);
1261 }
1262 barrier(CLK_LOCAL_MEM_FENCE);
1263
1264 // Per-(h, block) int8-packed Q + (qd, q_sum), quantized once per WG.
1265 __local uint q_packed_shared[MQ_GQA * DK_Q4_BLOCKS * 8];
1266 __local float q_d_shared[MQ_GQA * DK_Q4_BLOCKS];
1267 __local int q_sum_shared[MQ_GQA * DK_Q4_BLOCKS];
1268 {
1269 const int active = MQ_GQA * DK_Q4_BLOCKS;
1270 if (tid < active) {
1271 const int h = tid / DK_Q4_BLOCKS;
1272 const int block_id = tid % DK_Q4_BLOCKS;
1273 ACC_TYPE4 q_block[8];
1274 #pragma unroll
1275 for (int i = 0; i < 8; ++i) q_block[i] = q_shared[h * DK_VEC + block_id * 8 + i];
1276 uint packed[8];
1277 q4_q_block_info info = quant_q_block_int8_packed_q4(q_block, packed);
1278 #pragma unroll
1279 for (int i = 0; i < 8; ++i) q_packed_shared[(h * DK_Q4_BLOCKS + block_id) * 8 + i] = packed[i];
1280 q_d_shared[h * DK_Q4_BLOCKS + block_id] = info.qd;
1281 q_sum_shared[h * DK_Q4_BLOCKS + block_id] = info.q_sum;
1282 }
1283 }
1284 barrier(CLK_LOCAL_MEM_FENCE);
1285
1286 float slope[MQ_GQA];
1287 #pragma unroll
1288 for (int h = 0; h < MQ_GQA; ++h) {
1289 slope[h] = get_alibi_slope(max_bias, head_kv_idx * MQ_GQA + h, n_head_log2, m0, m1);
1290 }
1291
1292 const global char * mask_base[MQ_GQA];
1293 if (mask_void != NULL) {
1294 const int mask_batch_idx = batch_idx % mask_ne3;
1295 const global char * mask_base_b = (const global char *) mask_void + mask_offset +
1296 mask_batch_idx * mask_nb3 +
1297 (ulong) q_idx * mask_nb1;
1298 #pragma unroll
1299 for (int h = 0; h < MQ_GQA; ++h) {
1300 const int head_idx = head_kv_idx * MQ_GQA + h;
1301 const int mask_head_idx = head_idx % mask_ne2;
1302 mask_base[h] = mask_base_b + mask_head_idx * mask_nb2;
1303 }
1304 } else {
1305 #pragma unroll
1306 for (int h = 0; h < MQ_GQA; ++h) mask_base[h] = NULL;
1307 }
1308
1309 // Per-CLUSTER online state; o_acc holds this lane's V quartets {lic + FA_CL_C*i}.
1310 ACC_TYPE4 o_acc[MQ_GQA][FA_CL_DVQ];
1311 ACC_TYPE m_i[MQ_GQA];
1312 ACC_TYPE l_i[MQ_GQA];
1313 #pragma unroll
1314 for (int h = 0; h < MQ_GQA; ++h) {
1315 m_i[h] = FA_M_INIT;
1316 l_i[h] = 0.0f;
1317 #pragma unroll
1318 for (int i = 0; i < FA_CL_DVQ; ++i) o_acc[h][i] = (ACC_TYPE4)(0.0f);
1319 }
1320
1321 const int kv_len = kv_end - kv_start;
1322 const int kv_per_sg = (kv_len + MQ_NSG_SPLIT - 1) / MQ_NSG_SPLIT;
1323 const int kv_lo = kv_start + sgid * kv_per_sg;
1324 const int kv_hi = min(kv_end, kv_lo + kv_per_sg);
1325
1326 // Uniform trip count; tail clamps the row address and drops the score to
1327 // FA_M_INIT (p underflows to 0) so shuffles stay convergent.
1328 const int n_iter = (kv_hi - kv_lo + FA_CL_NCL - 1) / FA_CL_NCL;
1329 const ulong k_row_base = batch_idx * k_nb3 + head_kv_idx * k_nb2;
1330 const ulong v_row_base = batch_idx * v_nb3 + head_kv_idx * v_nb2;
1331
1332 for (int it = 0; it < n_iter; ++it) {
1333 const int k_idx = kv_lo + cl + it * FA_CL_NCL;
1334 const int valid = k_idx < kv_hi;
1335 const int k_safe = valid ? k_idx : (kv_hi - 1);
1336
1337 const global char * k_row = k_base + k_row_base + (ulong) k_safe * k_nb1;
1338 const global char * v_row = v_base + v_row_base + (ulong) k_safe * v_nb1;
1339
1340 // dp4a K dot over this lane's quartets of the cluster's row.
1341 ACC_TYPE lane_contrib[MQ_GQA];
1342 #pragma unroll
1343 for (int h = 0; h < MQ_GQA; ++h) lane_contrib[h] = 0.0f;
1344
1345 #pragma unroll
1346 for (int i = 0; i < FA_CL_DKQ; ++i) {
1347 const int qk = lic + FA_CL_C * i;
1348 const int block_idx = qk / 8;
1349 const int lane_in_block = qk % 8;
1350 const int g = lane_in_block & 3;
1351 const int shift = (lane_in_block < 4) ? 0 : 4;
1352 const global char * k_block = k_row + block_idx * Q4_0_BLOCK_SIZE;
1353 const float kd = vload_half(0, (const global half *)k_block);
1354 const global uchar * k_qs = (const global uchar *)(k_block + 2);
1355 const uchar b0 = k_qs[g*4 + 0];
1356 const uchar b1 = k_qs[g*4 + 1];
1357 const uchar b2 = k_qs[g*4 + 2];
1358 const uchar b3 = k_qs[g*4 + 3];
1359 const uint k_packed = ((uint)((b0 >> shift) & 0x0F)) |
1360 ((uint)((b1 >> shift) & 0x0F)) << 8 |
1361 ((uint)((b2 >> shift) & 0x0F)) << 16 |
1362 ((uint)((b3 >> shift) & 0x0F)) << 24;
1363 #pragma unroll
1364 for (int h = 0; h < MQ_GQA; ++h) {
1365 const uint q_packed_lane = q_packed_shared[(h * DK_Q4_BLOCKS + block_idx) * 8 + lane_in_block];
1366 const int raw_dot = dot_acc_sat_4x8packed_ss_int(q_packed_lane, k_packed, 0);
1367 const float qd = q_d_shared[h * DK_Q4_BLOCKS + block_idx];
1368 const float block_scale = qd * kd;
1369 float contrib = (float) raw_dot * block_scale;
1370 if (lane_in_block == 0) {
1371 const int q_sum_b = q_sum_shared[h * DK_Q4_BLOCKS + block_idx];
1372 contrib -= 8.0f * block_scale * (float) q_sum_b;
1373 }
1374 lane_contrib[h] += contrib;
1375 }
1376 }
1377
1378 // Cluster-reduce + score.
1379 ACC_TYPE score[MQ_GQA];
1380 #pragma unroll
1381 for (int h = 0; h < MQ_GQA; ++h) {
1382 ACC_TYPE s = lane_contrib[h];
1383 #pragma unroll
1384 for (int step = 1; step < FA_CL_C; step <<= 1) {
1385 s += sub_group_shuffle_xor(s, step);
1386 }
1387 s *= scale;
1388 if (logit_softcap > 0.0f) {
1389 s = logit_softcap * tanh(s / logit_softcap);
1390 }
1391 if (mask_base[h] != NULL) {
1392 const global MASK_DATA_TYPE * mask_ptr = (const global MASK_DATA_TYPE *) mask_base[h];
1393 s += slope[h] * (ACC_TYPE) mask_ptr[k_safe];
1394 }
1395 score[h] = valid ? s : FA_M_INIT;
1396 }
1397
1398 // Per-cluster online update (serial chain depth n_iter, not kv_per_sg).
1399 ACC_TYPE p_h[MQ_GQA];
1400 ACC_TYPE sp_h[MQ_GQA];
1401 #pragma unroll
1402 for (int h = 0; h < MQ_GQA; ++h) {
1403 const ACC_TYPE m_new = max(m_i[h], score[h]);
1404 sp_h[h] = native_exp(m_i[h] - m_new);
1405 p_h[h] = native_exp(score[h] - m_new);
1406 l_i[h] = l_i[h] * sp_h[h] + p_h[h];
1407 m_i[h] = m_new;
1408 }
1409
1410 // V accumulate on this lane's quartets (p = 0 on tail -> inert).
1411 #pragma unroll
1412 for (int i = 0; i < FA_CL_DVQ; ++i) {
1413 const int dv = lic + FA_CL_C * i;
1414 const float4 v_v = dequant_q4_0_lane(v_row + (dv / 8) * Q4_0_BLOCK_SIZE, dv % 8);
1415 #pragma unroll
1416 for (int h = 0; h < MQ_GQA; ++h) {
1417 o_acc[h][i] = mad(p_h[h], v_v, o_acc[h][i] * sp_h[h]);
1418 }
1419 }
1420 }
1421
1422 // Merge stage 1: fold cluster partials inside the subgroup via shuffles.
1423 // Lanes with equal lic across clusters hold the SAME dv slice.
1424 #pragma unroll
1425 for (int h = 0; h < MQ_GQA; ++h) {
1426 ACC_TYPE m_c = m_i[h];
1427 #pragma unroll
1428 for (int step = FA_CL_C; step < Q1_WG_SIZE; step <<= 1) {
1429 m_c = max(m_c, sub_group_shuffle_xor(m_c, step));
1430 }
1431 const ACC_TYPE alpha = native_exp(m_i[h] - m_c);
1432 ACC_TYPE l_c = l_i[h] * alpha;
1433 #pragma unroll
1434 for (int step = FA_CL_C; step < Q1_WG_SIZE; step <<= 1) {
1435 l_c += sub_group_shuffle_xor(l_c, step);
1436 }
1437 #pragma unroll
1438 for (int i = 0; i < FA_CL_DVQ; ++i) {
1439 ACC_TYPE4 o = o_acc[h][i] * alpha;
1440 #pragma unroll
1441 for (int step = FA_CL_C; step < Q1_WG_SIZE; step <<= 1) {
1442 o.s0 += sub_group_shuffle_xor(o.s0, step);
1443 o.s1 += sub_group_shuffle_xor(o.s1, step);
1444 o.s2 += sub_group_shuffle_xor(o.s2, step);
1445 o.s3 += sub_group_shuffle_xor(o.s3, step);
1446 }
1447 o_acc[h][i] = o;
1448 }
1449 m_i[h] = m_c;
1450 l_i[h] = l_c;
1451 }
1452
1453 // Merge stage 2: baseline cross-subgroup LDS merge (o published by
1454 // cluster 0's lanes; layout identical to the baseline sg_o).
1455 __local ACC_TYPE sg_m[MQ_GQA][MQ_NSG_SPLIT];
1456 __local ACC_TYPE sg_l[MQ_GQA][MQ_NSG_SPLIT];
1457 __local ACC_TYPE4 sg_o[MQ_NSG_SPLIT][DV_VEC];
1458
1459 if (tid_sg == 0) {
1460 #pragma unroll
1461 for (int h = 0; h < MQ_GQA; ++h) {
1462 sg_m[h][sgid] = m_i[h];
1463 sg_l[h][sgid] = l_i[h];
1464 }
1465 }
1466
1467 #pragma unroll
1468 for (int h = 0; h < MQ_GQA; ++h) {
1469 if (cl == 0) {
1470 #pragma unroll
1471 for (int i = 0; i < FA_CL_DVQ; ++i) {
1472 sg_o[sgid][lic + FA_CL_C * i] = o_acc[h][i];
1473 }
1474 }
1475 barrier(CLK_LOCAL_MEM_FENCE);
1476
1477 if (sgid == 0) {
1478 const int head_idx = head_kv_idx * MQ_GQA + h;
1479
1480 ACC_TYPE m_c = sg_m[h][0];
1481 #pragma unroll
1482 for (int s = 1; s < MQ_NSG_SPLIT; ++s) {
1483 m_c = max(m_c, sg_m[h][s]);
1484 }
1485 ACC_TYPE l_c = 0.0f;
1486 #pragma unroll
1487 for (int s = 0; s < MQ_NSG_SPLIT; ++s) {
1488 l_c += sg_l[h][s] * native_exp(sg_m[h][s] - m_c);
1489 }
1490
1491 const ulong rec_idx = ((((ulong) batch_idx * n_head + head_idx) * n_q + q_idx)
1492 * n_splits + split_idx);
1493 global float * rec = partial_void + rec_idx * record_stride;
1494 global float4 * rec_o = (global float4 *) (rec + 2);
1495
1496 if (tid_sg == 0) {
1497 rec[0] = (float) m_c;
1498 rec[1] = (float) l_c;
1499 }
1500 for (int dv_idx = tid_sg; dv_idx < DV_VEC; dv_idx += Q1_WG_SIZE) {
1501 ACC_TYPE4 o_merged = (ACC_TYPE4)(0.0f);
1502 #pragma unroll
1503 for (int s = 0; s < MQ_NSG_SPLIT; ++s) {
1504 const ACC_TYPE alpha = native_exp(sg_m[h][s] - m_c);
1505 o_merged = mad((ACC_TYPE4)(alpha), sg_o[s][dv_idx], o_merged);
1506 }
1507 rec_o[dv_idx] = o_merged;
1508 }
1509 }
1510 barrier(CLK_LOCAL_MEM_FENCE);
1511 }
1512}
1513
1514#endif // DK_VEC/DV_VEC divisible by FA_CL_C
1515#endif // FA_HAVE_INT_DOT && HAS_SUBGROUP_SHUFFLE (q1_vec_mq_split_c8)
1516
1517__kernel void flash_attn_f32_q4_0(
1518 const global void * q_void, ulong q_offset,
1519 const global void * k_void, ulong k_offset,
1520 const global void * v_void, ulong v_offset,
1521 global void * o_void, ulong o_offset,
1522 const float scale,
1523 const int n_q,
1524 const int n_kv,
1525 const int is_causal,
1526 const int n_head,
1527 const ulong q_nb1, const ulong q_nb2, const ulong q_nb3,
1528 const ulong k_nb1, const ulong k_nb2, const ulong k_nb3,
1529 const ulong v_nb1, const ulong v_nb2, const ulong v_nb3,
1530 const ulong o_nb1, const ulong o_nb2, const ulong o_nb3,
1531 const float max_bias,
1532 const float m0,
1533 const float m1,
1534 const int n_head_log2,
1535 const float logit_softcap,
1536 const int n_head_kv,
1537 const global void* mask_void,
1538 const ulong mask_offset,
1539 const ulong mask_nb1,
1540 const ulong mask_nb2,
1541 const ulong mask_nb3,
1542 const int mask_ne2,
1543 const int mask_ne3,
1544 const global void* sinks_void,
1545 const ulong sinks_offset,
1546 // blk: per-(qblock,kvblock) class from flash_attn_blk_f16
1547 // (0=masked, 1=mixed, 2=unmasked). NULL disables the prepass opt.
1548 const global void * blk_void
1549) {
1550 const int tid = get_local_id(0);
1551 const int block_q_idx = get_group_id(0);
1552 const int head_batch_idx = get_global_id(1);
1553
1554#if N_SPLIT > 1
1555 const int q_lane = tid / N_SPLIT;
1556 const int split_idx = tid % N_SPLIT;
1557#else
1558 const int q_lane = tid;
1559 const int split_idx = 0;
1560#endif
1561 const int my_query_row = block_q_idx * BLOCK_M + q_lane;
1562 const int query_valid = my_query_row < n_q;
1563
1564 const int batch_idx = head_batch_idx / n_head;
1565 const int head_idx = head_batch_idx % n_head;
1566
1567 const int gqa_ratio = n_head / n_head_kv;
1568 const int head_kv_idx = head_idx / gqa_ratio;
1569 const int mask_head_idx = mask_void != NULL ? head_idx % mask_ne2 : 0;
1570 const int mask_batch_idx = mask_void != NULL ? batch_idx % mask_ne3 : 0;
1571
1572 const global char * q_base = (const global char *) q_void + q_offset;
1573 const global char * k_base = (const global char *) k_void + k_offset;
1574 const global char * v_base = (const global char *) v_void + v_offset;
1575 global char * o_base = (global char *) o_void + o_offset;
1576
1577 const global char * mask_base = NULL;
1578 if (mask_void != NULL) {
1579 mask_base = (const global char *) mask_void + mask_offset +
1580 mask_batch_idx * mask_nb3 + mask_head_idx * mask_nb2;
1581 }
1582
1583 // BLK_PREPASS_BM may differ from this kernel's BLOCK_M; scale q-block idx.
1584 #ifndef BLK_PREPASS_BM
1585 #define BLK_PREPASS_BM BLOCK_M
1586 #endif
1587 const global char * blk_base = NULL;
1588 int n_kv_blocks = 0;
1589 if (blk_void != NULL) {
1590 n_kv_blocks = (n_kv + BLOCK_N - 1) / BLOCK_N;
1591 const int n_q_blocks_prepass = (n_q + BLK_PREPASS_BM - 1) / BLK_PREPASS_BM;
1592 const int prepass_q_block = (block_q_idx * BLOCK_M) / BLK_PREPASS_BM;
1593 blk_base = (const global char *) blk_void +
1594 (((mask_batch_idx * mask_ne2) + mask_head_idx) * n_q_blocks_prepass + prepass_q_block) * n_kv_blocks;
1595 }
1596
1597 const int dk_off_vec = split_idx * SPLIT_DK_VEC;
1598 ACC_TYPE4 q_priv[SPLIT_DK_VEC];
1599 if (query_valid) {
1600 const ulong q_row_offset = batch_idx * q_nb3 + head_idx * q_nb2 + my_query_row * q_nb1;
1601 const global float4 * q_ptr = (const global float4 *) (q_base + q_row_offset);
1602 #pragma unroll
1603 for (int i = 0; i < SPLIT_DK_VEC; ++i) {
1604 q_priv[i] = q_ptr[dk_off_vec + i];
1605 }
1606 } else {
1607 #pragma unroll
1608 for (int i = 0; i < SPLIT_DK_VEC; ++i) q_priv[i] = (ACC_TYPE4)(0.0f);
1609 }
1610
1611#ifdef FA_HAVE_INT_DOT
1612 uint q_packed_pf[SPLIT_DK_Q4_BLOCKS * 8];
1613 float q_d_pf[SPLIT_DK_Q4_BLOCKS];
1614 int q_sum_pf[SPLIT_DK_Q4_BLOCKS];
1615 #pragma unroll
1616 for (int b = 0; b < SPLIT_DK_Q4_BLOCKS; ++b) {
1617 q4_q_block_info info = quant_q_block_int8_packed_q4(&q_priv[b * 8], &q_packed_pf[b * 8]);
1618 q_d_pf[b] = info.qd;
1619 q_sum_pf[b] = info.q_sum;
1620 }
1621#endif
1622
1623 const int dv_off_vec = split_idx * SPLIT_DV_VEC;
1624 ACC_TYPE4 o_acc[SPLIT_DV_VEC];
1625 #pragma unroll
1626 for (int i = 0; i < SPLIT_DV_VEC; ++i) o_acc[i] = (ACC_TYPE4)(0.0f);
1627
1628 ACC_TYPE m_i = FA_M_INIT;
1629 ACC_TYPE l_i = 0.0f;
1630
1631 float slope = get_alibi_slope(max_bias, head_idx, n_head_log2, m0, m1);
1632
1633#ifdef FA_HAVE_INT_DOT
1634// Accessors so the staging code is layout-agnostic.
1635#ifdef FA_K_LDS_T
1636#define FA_K_PACKED(ROW, IDX) l_k_packed[IDX][ROW]
1637#define FA_K_SCALE(ROW, BLK) l_k_scale[BLK][ROW]
1638#else
1639#define FA_K_PACKED(ROW, IDX) l_k_packed[ROW][IDX]
1640#define FA_K_SCALE(ROW, BLK) l_k_scale[ROW][BLK]
1641#endif
1642
1643#ifdef FA_K_LDS_T
1644 // K tile transposed: the 4 KV rows the QK loop walks together become adjacent, so each
1645 // (block, group) step is ONE 128-bit local read instead of four 32-bit ones. The QK
1646 // loop is LDS-read-issue-bound.
1647 __local uint l_k_packed[DK_Q4_BLOCKS_PREFILL * 8][BLOCK_N];
1648 __local float l_k_scale [DK_Q4_BLOCKS_PREFILL][BLOCK_N];
1649#else
1650 __local uint l_k_packed[BLOCK_N][DK_Q4_BLOCKS_PREFILL * 8];
1651 __local float l_k_scale [BLOCK_N][DK_Q4_BLOCKS_PREFILL];
1652#endif
1653#else
1654 __local half4 l_k[BLOCK_N][DK_VEC];
1655#endif
1656
1657 __local half4 l_v[BLOCK_N][DV_VEC];
1658
1659 for (int k_start = 0; k_start < n_kv; k_start += BLOCK_N) {
1660 // Skip fully-masked KV tiles (uniform branch across WG).
1661 char blk_cur = 1;
1662 if (blk_base != NULL) {
1663 blk_cur = blk_base[k_start / BLOCK_N];
1664 if (blk_cur == 0) continue;
1665 }
1666
1667 {
1668#ifdef FA_HAVE_INT_DOT
1669 const int k_blocks_per_row = DK_Q4_BLOCKS_PREFILL;
1670 const int n_blocks_total = BLOCK_N * k_blocks_per_row;
1671 for (int i = tid; i < n_blocks_total; i += WG_SIZE) {
1672 const int row = i / k_blocks_per_row;
1673 const int blk = i % k_blocks_per_row;
1674 const int k_row_idx = k_start + row;
1675 if (k_row_idx < n_kv) {
1676 const ulong k_row_off = batch_idx * k_nb3 + head_kv_idx * k_nb2 + k_row_idx * k_nb1;
1677 const global char * blk_ptr = k_base + k_row_off + blk * Q4_0_BLOCK_SIZE;
1678 const float df = (float) vload_half(0, (const global half *) blk_ptr);
1679 const global uchar * qs = (const global uchar *)(blk_ptr + 2);
1680 FA_K_SCALE(row, blk) = df;
1681 uint k_packed[8];
1682 pack_q4_0_nibbles(qs, k_packed);
1683 #pragma unroll
1684 for (int j = 0; j < 8; ++j) {
1685 FA_K_PACKED(row, blk * 8 + j) = k_packed[j];
1686 }
1687 } else {
1688 FA_K_SCALE(row, blk) = 0.0f;
1689 #pragma unroll
1690 for (int j = 0; j < 8; ++j) FA_K_PACKED(row, blk * 8 + j) = 0u;
1691 }
1692 }
1693#else
1694 // Fallback: dequant q4_0 -> half in local memory.
1695 const int k_blocks_per_row = DK_Q4_BLOCKS_PREFILL;
1696 const int n_blocks_total = BLOCK_N * k_blocks_per_row;
1697 for (int i = tid; i < n_blocks_total; i += WG_SIZE) {
1698 const int row = i / k_blocks_per_row;
1699 const int blk = i % k_blocks_per_row;
1700 const int k_row_idx = k_start + row;
1701 if (k_row_idx < n_kv) {
1702 const ulong k_row_off = batch_idx * k_nb3 + head_kv_idx * k_nb2 + k_row_idx * k_nb1;
1703 const global char * blk_ptr = k_base + k_row_off + blk * Q4_0_BLOCK_SIZE;
1704 const float df = (float) vload_half(0, (const global half *) blk_ptr);
1705 const global uchar * qs = (const global uchar *)(blk_ptr + 2);
1706 #pragma unroll
1707 for (int g = 0; g < 4; ++g) {
1708 float4 vlo = df * (float4)((float)(int)(qs[g*4 + 0] & 0x0F) - 8.0f,
1709 (float)(int)(qs[g*4 + 1] & 0x0F) - 8.0f,
1710 (float)(int)(qs[g*4 + 2] & 0x0F) - 8.0f,
1711 (float)(int)(qs[g*4 + 3] & 0x0F) - 8.0f);
1712 float4 vhi = df * (float4)((float)(int)(qs[g*4 + 0] >> 4) - 8.0f,
1713 (float)(int)(qs[g*4 + 1] >> 4) - 8.0f,
1714 (float)(int)(qs[g*4 + 2] >> 4) - 8.0f,
1715 (float)(int)(qs[g*4 + 3] >> 4) - 8.0f);
1716 l_k[row][blk * 8 + g ] = (half4)((half)vlo.s0, (half)vlo.s1, (half)vlo.s2, (half)vlo.s3);
1717 l_k[row][blk * 8 + 4 + g] = (half4)((half)vhi.s0, (half)vhi.s1, (half)vhi.s2, (half)vhi.s3);
1718 }
1719 } else {
1720 #pragma unroll
1721 for (int j = 0; j < 8; ++j) l_k[row][blk * 8 + j] = (half4)(0.0h);
1722 }
1723 }
1724#endif
1725 }
1726 // V tile load — dequant V -> half in local memory.
1727 {
1728 const int v_blocks_per_row = DV_Q4_BLOCKS_PREFILL;
1729 const int n_blocks_total = BLOCK_N * v_blocks_per_row;
1730 for (int i = tid; i < n_blocks_total; i += WG_SIZE) {
1731 const int row = i / v_blocks_per_row;
1732 const int blk = i % v_blocks_per_row;
1733 const int v_row_idx = k_start + row;
1734 if (v_row_idx < n_kv) {
1735 const ulong v_row_off = batch_idx * v_nb3 + head_kv_idx * v_nb2 + v_row_idx * v_nb1;
1736 const global char * blk_ptr = v_base + v_row_off + blk * Q4_0_BLOCK_SIZE;
1737 const float df = (float) vload_half(0, (const global half *) blk_ptr);
1738 const global uchar * qs = (const global uchar *)(blk_ptr + 2);
1739 #pragma unroll
1740 for (int g = 0; g < 4; ++g) {
1741 float4 vlo = df * (float4)((float)(int)(qs[g*4 + 0] & 0x0F) - 8.0f,
1742 (float)(int)(qs[g*4 + 1] & 0x0F) - 8.0f,
1743 (float)(int)(qs[g*4 + 2] & 0x0F) - 8.0f,
1744 (float)(int)(qs[g*4 + 3] & 0x0F) - 8.0f);
1745 float4 vhi = df * (float4)((float)(int)(qs[g*4 + 0] >> 4) - 8.0f,
1746 (float)(int)(qs[g*4 + 1] >> 4) - 8.0f,
1747 (float)(int)(qs[g*4 + 2] >> 4) - 8.0f,
1748 (float)(int)(qs[g*4 + 3] >> 4) - 8.0f);
1749 l_v[row][blk * 8 + g ] = (half4)((half)vlo.s0, (half)vlo.s1, (half)vlo.s2, (half)vlo.s3);
1750 l_v[row][blk * 8 + 4 + g] = (half4)((half)vhi.s0, (half)vhi.s1, (half)vhi.s2, (half)vhi.s3);
1751 }
1752 } else {
1753 #pragma unroll
1754 for (int j = 0; j < 8; ++j) l_v[row][blk * 8 + j] = (half4)(0.0h);
1755 }
1756 }
1757 }
1758 barrier(CLK_LOCAL_MEM_FENCE);
1759
1760 // QK dot + online softmax. N_SPLIT>1 reduces per-thread partials via shuffle_xor.
1761#if N_SPLIT > 1
1762 {
1763#else
1764 if (query_valid) {
1765#endif
1766 const int k_blk_base = split_idx * SPLIT_DK_Q4_BLOCKS;
1767 for (int j = 0; j < BLOCK_N; j += 4) {
1768 const int k_row0 = k_start + j;
1769 const int k_row1 = k_start + j + 1;
1770 const int k_row2 = k_start + j + 2;
1771 const int k_row3 = k_start + j + 3;
1772
1773 ACC_TYPE s0, s1, s2, s3;
1774#ifdef FA_HAVE_INT_DOT
1775 s0 = 0.0f; s1 = 0.0f; s2 = 0.0f; s3 = 0.0f;
1776 #pragma unroll
1777 for (int b_local = 0; b_local < SPLIT_DK_Q4_BLOCKS; ++b_local) {
1778 const int b = k_blk_base + b_local;
1779 int sum0 = 0, sum1 = 0, sum2 = 0, sum3 = 0;
1780#ifdef FA_K_LDS_T
1781 // 4 KV rows are adjacent in the transposed tile: one 128-bit local
1782 // read per (block, group) instead of four 32-bit ones.
1783 #pragma unroll
1784 for (int g = 0; g < 8; ++g) {
1785 const uint qp = q_packed_pf[b_local * 8 + g];
1786 const uint4 kq4 = vload4(0, &l_k_packed[b * 8 + g][j]);
1787 sum0 = dot_acc_sat_4x8packed_ss_int(qp, kq4.s0, sum0);
1788 sum1 = dot_acc_sat_4x8packed_ss_int(qp, kq4.s1, sum1);
1789 sum2 = dot_acc_sat_4x8packed_ss_int(qp, kq4.s2, sum2);
1790 sum3 = dot_acc_sat_4x8packed_ss_int(qp, kq4.s3, sum3);
1791 }
1792#else
1793 #pragma unroll
1794 for (int g = 0; g < 8; ++g) {
1795 const uint qp = q_packed_pf[b_local * 8 + g];
1796 sum0 = dot_acc_sat_4x8packed_ss_int(qp, l_k_packed[j ][b * 8 + g], sum0);
1797 sum1 = dot_acc_sat_4x8packed_ss_int(qp, l_k_packed[j+1][b * 8 + g], sum1);
1798 sum2 = dot_acc_sat_4x8packed_ss_int(qp, l_k_packed[j+2][b * 8 + g], sum2);
1799 sum3 = dot_acc_sat_4x8packed_ss_int(qp, l_k_packed[j+3][b * 8 + g], sum3);
1800 }
1801#endif
1802 const float qd = q_d_pf[b_local];
1803 const int q_sum = q_sum_pf[b_local];
1804#ifdef FA_K_LDS_T
1805 const float4 ks4 = vload4(0, &l_k_scale[b][j]);
1806 s0 += (float)(sum0 - 8 * q_sum) * qd * ks4.s0;
1807 s1 += (float)(sum1 - 8 * q_sum) * qd * ks4.s1;
1808 s2 += (float)(sum2 - 8 * q_sum) * qd * ks4.s2;
1809 s3 += (float)(sum3 - 8 * q_sum) * qd * ks4.s3;
1810#else
1811 s0 += (float)(sum0 - 8 * q_sum) * qd * l_k_scale[j ][b];
1812 s1 += (float)(sum1 - 8 * q_sum) * qd * l_k_scale[j+1][b];
1813 s2 += (float)(sum2 - 8 * q_sum) * qd * l_k_scale[j+2][b];
1814 s3 += (float)(sum3 - 8 * q_sum) * qd * l_k_scale[j+3][b];
1815#endif
1816 }
1817#else
1818 ACC_TYPE4 dot_acc0 = (ACC_TYPE4)(0.0f);
1819 ACC_TYPE4 dot_acc1 = (ACC_TYPE4)(0.0f);
1820 ACC_TYPE4 dot_acc2 = (ACC_TYPE4)(0.0f);
1821 ACC_TYPE4 dot_acc3 = (ACC_TYPE4)(0.0f);
1822 #pragma unroll
1823 for (int k = 0; k < SPLIT_DK_VEC; ++k) {
1824 const ACC_TYPE4 qk = q_priv[k];
1825 const int k_abs = dk_off_vec + k;
1826 dot_acc0 = mad(qk, CONVERT_KV_ACC4(l_k[j ][k_abs]), dot_acc0);
1827 dot_acc1 = mad(qk, CONVERT_KV_ACC4(l_k[j+1][k_abs]), dot_acc1);
1828 dot_acc2 = mad(qk, CONVERT_KV_ACC4(l_k[j+2][k_abs]), dot_acc2);
1829 dot_acc3 = mad(qk, CONVERT_KV_ACC4(l_k[j+3][k_abs]), dot_acc3);
1830 }
1831 s0 = dot_acc0.s0 + dot_acc0.s1 + dot_acc0.s2 + dot_acc0.s3;
1832 s1 = dot_acc1.s0 + dot_acc1.s1 + dot_acc1.s2 + dot_acc1.s3;
1833 s2 = dot_acc2.s0 + dot_acc2.s1 + dot_acc2.s2 + dot_acc2.s3;
1834 s3 = dot_acc3.s0 + dot_acc3.s1 + dot_acc3.s2 + dot_acc3.s3;
1835#endif
1836
1837#if N_SPLIT > 1
1838 // Power-of-2 N_SPLIT: shuffle_xor butterfly. N_SPLIT=3 (DK=96):
1839 // explicit 3-lane shuffle.
1840 #if (N_SPLIT & (N_SPLIT - 1)) == 0
1841 #pragma unroll
1842 for (int step = 1; step < N_SPLIT; step <<= 1) {
1843 s0 += sub_group_shuffle_xor(s0, step);
1844 s1 += sub_group_shuffle_xor(s1, step);
1845 s2 += sub_group_shuffle_xor(s2, step);
1846 s3 += sub_group_shuffle_xor(s3, step);
1847 }
1848 #else
1849 const uint tri_base = (get_sub_group_local_id() / N_SPLIT) * N_SPLIT;
1850 s0 = sub_group_shuffle(s0, tri_base + 0) + sub_group_shuffle(s0, tri_base + 1) + sub_group_shuffle(s0, tri_base + 2);
1851 s1 = sub_group_shuffle(s1, tri_base + 0) + sub_group_shuffle(s1, tri_base + 1) + sub_group_shuffle(s1, tri_base + 2);
1852 s2 = sub_group_shuffle(s2, tri_base + 0) + sub_group_shuffle(s2, tri_base + 1) + sub_group_shuffle(s2, tri_base + 2);
1853 s3 = sub_group_shuffle(s3, tri_base + 0) + sub_group_shuffle(s3, tri_base + 1) + sub_group_shuffle(s3, tri_base + 2);
1854 #endif
1855#endif
1856 s0 *= scale; s1 *= scale; s2 *= scale; s3 *= scale;
1857 if (logit_softcap > 0.0f) {
1858 s0 = logit_softcap * tanh(s0 / logit_softcap);
1859 s1 = logit_softcap * tanh(s1 / logit_softcap);
1860 s2 = logit_softcap * tanh(s2 / logit_softcap);
1861 s3 = logit_softcap * tanh(s3 / logit_softcap);
1862 }
1863 if (!query_valid) { s0 = FA_M_INIT; s1 = FA_M_INIT; s2 = FA_M_INIT; s3 = FA_M_INIT; }
1864
1865 if (is_causal) {
1866 const int causal_limit = n_kv - n_q + my_query_row;
1867 if (k_row0 > causal_limit) s0 = FA_M_INIT;
1868 if (k_row1 > causal_limit) s1 = FA_M_INIT;
1869 if (k_row2 > causal_limit) s2 = FA_M_INIT;
1870 if (k_row3 > causal_limit) s3 = FA_M_INIT;
1871 }
1872 if (k_row0 >= n_kv) s0 = FA_M_INIT;
1873 if (k_row1 >= n_kv) s1 = FA_M_INIT;
1874 if (k_row2 >= n_kv) s2 = FA_M_INIT;
1875 if (k_row3 >= n_kv) s3 = FA_M_INIT;
1876
1877 if (query_valid && mask_base != NULL && blk_cur != 2) {
1878 const global MASK_DATA_TYPE * mask_ptr =
1879 (const global MASK_DATA_TYPE *) (mask_base + my_query_row * mask_nb1);
1880 if (k_row0 < n_kv) s0 += slope * (ACC_TYPE) mask_ptr[k_row0];
1881 if (k_row1 < n_kv) s1 += slope * (ACC_TYPE) mask_ptr[k_row1];
1882 if (k_row2 < n_kv) s2 += slope * (ACC_TYPE) mask_ptr[k_row2];
1883 if (k_row3 < n_kv) s3 += slope * (ACC_TYPE) mask_ptr[k_row3];
1884 }
1885
1886 const ACC_TYPE m_new = max(m_i, max(max(s0, s1), max(s2, s3)));
1887 // Whole tile masked (m_new == FA_M_INIT): force the exp() args
1888 // far negative so the tile contributes 0, not exp(0)=1.
1889 const ACC_TYPE m_exp = (m_new == FA_M_INIT) ? 0.0f : m_new;
1890 const ACC_TYPE scale_prev = native_exp(m_i - m_exp);
1891 const ACC_TYPE p0 = native_exp(s0 - m_exp);
1892 const ACC_TYPE p1 = native_exp(s1 - m_exp);
1893 const ACC_TYPE p2 = native_exp(s2 - m_exp);
1894 const ACC_TYPE p3 = native_exp(s3 - m_exp);
1895
1896 #pragma unroll
1897 for (int i = 0; i < SPLIT_DV_VEC; ++i) {
1898 const int i_abs = dv_off_vec + i;
1899 o_acc[i] = mad(p3, CONVERT_KV_ACC4(l_v[j+3][i_abs]),
1900 mad(p2, CONVERT_KV_ACC4(l_v[j+2][i_abs]),
1901 mad(p1, CONVERT_KV_ACC4(l_v[j+1][i_abs]),
1902 mad(p0, CONVERT_KV_ACC4(l_v[j ][i_abs]),
1903 o_acc[i] * scale_prev))));
1904 }
1905 l_i = l_i * scale_prev + p0 + p1 + p2 + p3;
1906 m_i = m_new;
1907 }
1908 }
1909 barrier(CLK_LOCAL_MEM_FENCE);
1910 }
1911
1912 // Write output.
1913 if (query_valid) {
1914 if (sinks_void != NULL) {
1915 const global ACC_TYPE * sinks_ptr =
1916 (const global ACC_TYPE *) ((const global char *) sinks_void + sinks_offset);
1917 const ACC_TYPE m_sink = sinks_ptr[head_idx];
1918 const ACC_TYPE m_final = max(m_i, m_sink);
1919 const ACC_TYPE scale_o = exp(m_i - m_final);
1920 #pragma unroll
1921 for (int i = 0; i < SPLIT_DV_VEC; ++i) o_acc[i] *= scale_o;
1922 l_i = l_i * scale_o + exp(m_sink - m_final);
1923 m_i = m_final;
1924 }
1925 const ACC_TYPE l_inv = (l_i > 0.0f) ? (1.0f / l_i) : 0.0f;
1926 const ulong o_row_offset = batch_idx * o_nb3 + my_query_row * o_nb2 + head_idx * o_nb1;
1927 global float4 * o_row = (global float4 *) (o_base + o_row_offset);
1928 if (l_inv > 0.0f) {
1929 #pragma unroll
1930 for (int i = 0; i < SPLIT_DV_VEC; ++i) o_row[dv_off_vec + i] = o_acc[i] * l_inv;
1931 } else {
1932 #pragma unroll
1933 for (int i = 0; i < SPLIT_DV_VEC; ++i) o_row[dv_off_vec + i] = (float4)(0.0f);
1934 }
1935 }
1936}
1937
1938// FD Pass 2: merge split partials. Identical across q4_0/q8_0/f16; each FA
1939// source owns a copy since kernels compile per-source-program.
1940__kernel void flash_attn_f32_merge(
1941 const global float * partial_void,
1942 global void * o_void,
1943 const ulong o_offset,
1944 const int n_head,
1945 const int n_splits,
1946 const ulong o_nb1, const ulong o_nb2, const ulong o_nb3,
1947 const global void * sinks_void,
1948 const ulong sinks_offset,
1949 const int n_q
1950) {
1951 const int lane = get_local_id(0);
1952 const int head_batch_idx = get_global_id(1);
1953 const int q_idx = get_global_id(2);
1954 const int batch_idx = head_batch_idx / n_head;
1955 const int head_idx = head_batch_idx % n_head;
1956
1957 const ulong record_stride = (ulong) FA_PARTIAL_FLOATS;
1958 const ulong record_idx_0 = (((ulong) batch_idx * n_head + head_idx) * n_q + q_idx) * n_splits;
1959 const global float * rec0 = partial_void + record_idx_0 * record_stride;
1960
1961 __local ACC_TYPE m_final_shared;
1962 __local ACC_TYPE l_final_shared;
1963 if (lane == 0) {
1964 ACC_TYPE m = FA_M_INIT;
1965 for (int c = 0; c < n_splits; ++c) {
1966 const ACC_TYPE m_c = rec0[c * record_stride + 0];
1967 m = max(m, m_c);
1968 }
1969 ACC_TYPE m_sink = 0.0f;
1970 bool has_sink = false;
1971 if (sinks_void != NULL) {
1972 const global ACC_TYPE * sinks_ptr =
1973 (const global ACC_TYPE *) ((const global char *) sinks_void + sinks_offset);
1974 m_sink = sinks_ptr[head_idx];
1975 has_sink = true;
1976 m = max(m, m_sink);
1977 }
1978 ACC_TYPE l = 0.0f;
1979 for (int c = 0; c < n_splits; ++c) {
1980 const ACC_TYPE m_c = rec0[c * record_stride + 0];
1981 const ACC_TYPE l_c = rec0[c * record_stride + 1];
1982 if (m_c > FA_M_INIT) {
1983 l += l_c * exp(m_c - m);
1984 }
1985 }
1986 if (has_sink) {
1987 l += exp(m_sink - m);
1988 }
1989 m_final_shared = m;
1990 l_final_shared = l;
1991 }
1992 barrier(CLK_LOCAL_MEM_FENCE);
1993 const ACC_TYPE m_final = m_final_shared;
1994 const ACC_TYPE l_final = l_final_shared;
1995 const ACC_TYPE l_inv = (l_final > 0.0f) ? (1.0f / l_final) : 0.0f;
1996
1997 ACC_TYPE4 o = (ACC_TYPE4)(0.0f);
1998 for (int c = 0; c < n_splits; ++c) {
1999 const global float * rec_c = rec0 + c * record_stride;
2000 const ACC_TYPE m_c = rec_c[0];
2001 if (m_c <= FA_M_INIT) continue;
2002 const global float4 * rec_oc = (const global float4 *) (rec_c + 2);
2003 const ACC_TYPE scale_c = exp(m_c - m_final);
2004 o = mad((ACC_TYPE4)(scale_c), rec_oc[lane], o);
2005 }
2006 o = o * l_inv;
2007
2008 const ulong o_row_offset = (ulong) batch_idx * o_nb3 + (ulong) q_idx * o_nb2 + (ulong) head_idx * o_nb1;
2009 global O_DATA_TYPE4 * o_row = (global O_DATA_TYPE4 *) ((global char *) o_void + o_offset + o_row_offset);
2010 o_row[lane] = CONVERT_O_DATA4(o);
2011}