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_f16.cl

2775 lines · 110856 bytes Raw Blame History
1#pragma OPENCL EXTENSION cl_khr_fp16 : enable
2
3#ifdef cl_intel_subgroups
4#pragma OPENCL EXTENSION cl_intel_subgroups : enable
5#else
6#pragma OPENCL EXTENSION cl_khr_subgroups : enable
7#endif
8
9#ifdef cl_qcom_reqd_sub_group_size
10#pragma OPENCL EXTENSION cl_qcom_reqd_sub_group_size : enable
11#define REQD_SUBGROUP_SIZE_64 __attribute__((qcom_reqd_sub_group_size("half")))
12#else
13#define REQD_SUBGROUP_SIZE_64
14#endif
15
16// subgroup size for q1 kernels
17#ifndef FA_SG
18#define FA_SG 64
19#endif
20#ifdef cl_intel_required_subgroup_size
21#pragma OPENCL EXTENSION cl_intel_required_subgroup_size : enable
22#define REQD_FA_SG __attribute__((intel_reqd_sub_group_size(FA_SG)))
23#else
24#define REQD_FA_SG
25#endif
26
27#ifdef cl_khr_subgroup_shuffle
28#pragma OPENCL EXTENSION cl_khr_subgroup_shuffle : enable
29#define HAS_SUBGROUP_SHUFFLE 1
30#elif defined(cl_qcom_subgroup_shuffle)
31#pragma OPENCL EXTENSION cl_qcom_subgroup_shuffle : enable
32#define HAS_SUBGROUP_SHUFFLE 1
33// Adreno compilers that expose only cl_qcom_subgroup_shuffle do not declare the KHR
34// name, so calling it is an implicit declaration and the program fails to build.
35// Route it to the qcom builtin.
36#define sub_group_shuffle_xor(val, mask) qcom_sub_group_shuffle_xor((val), (mask), CLK_SUB_GROUP_SHUFFLE_WIDTH_WAVE_SIZE_QCOM, 0.0f)
37#endif
38
39#define ACC_TYPE float
40#define ACC_TYPE4 float4
41#define Q_DATA_TYPE4 float4
42#define KV_DATA_TYPE4 half4
43#define O_DATA_TYPE4 float4
44#define MASK_DATA_TYPE half
45#define CONVERT_Q_ACC4(x) (x)
46#define CONVERT_KV_ACC4(x) convert_float4(x)
47#define CONVERT_O_DATA4(x) (x)
48
49#define DK_VEC (DK/4)
50#define DV_VEC (DV/4)
51
52#ifndef FA_PARTIAL_FLOATS
53#define FA_PARTIAL_FLOATS (2 + DV)
54#endif
55#define Q1_WG_SIZE FA_SG
56
57// The kernels are built with -cl-finite-math-only. On some older Adreno GPUs,
58// infinite operand can cause undefined behavior and miscompilation for exp.
59// Therefore, a large negative value is used instead.
60#define FA_M_INIT (-3.0e38f)
61
62// Drop full unroll at DK>=192 — Adreno compiler host-memory budget.
63#if DK >= 192
64#define FA_UNROLL
65#else
66#define FA_UNROLL _Pragma("unroll")
67#endif
68
69// N_SPLIT>1 splits DK/DV across threads to cut per-thread register use.
70#ifndef N_SPLIT
71#define N_SPLIT 1
72#endif
73
74#define SPLIT_DK_VEC (DK_VEC / N_SPLIT)
75#define SPLIT_DV_VEC (DV_VEC / N_SPLIT)
76
77#if N_SPLIT > 1
78#define WG_SIZE (BLOCK_M * N_SPLIT)
79#else
80#define WG_SIZE (BLOCK_M)
81#endif
82
83inline float get_alibi_slope(
84 const float max_bias, const uint h, const uint n_head_log2, const float m0, const float m1
85) {
86 if (max_bias <= 0.0f) {
87 return 1.0f;
88 }
89 const float base = h < n_head_log2 ? m0 : m1;
90 const int exph = h < n_head_log2 ? h + 1 : 2*(h - n_head_log2) + 1;
91
92 return pow(base, exph);
93}
94
95// Adreno compiler crashes when attempting to compile the entire program for DK=512,
96// FA_DECODE_ONLY allows bypass the encoding kernel.
97#if !defined(FA_DECODE_ONLY) && !defined(FA_MQ_ONLY)
98#ifndef FA_TILE_NAME
99#define FA_TILE_NAME flash_attn_f32_f16
100#endif
101__kernel void FA_TILE_NAME(
102 const global void * q_void, ulong q_offset,
103#ifdef FA_K_IMG
104 __read_only image1d_buffer_t k_img, ulong k_offset_unused,
105#else
106 const global void * k_void, ulong k_offset,
107#endif
108 const global void * v_void, ulong v_offset,
109 global void * o_void, ulong o_offset,
110 const float scale,
111 const int n_q,
112 const int n_kv,
113 const int is_causal,
114 const int n_head,
115 const ulong q_nb1, const ulong q_nb2, const ulong q_nb3,
116 const ulong k_nb1, const ulong k_nb2, const ulong k_nb3,
117 const ulong v_nb1, const ulong v_nb2, const ulong v_nb3,
118 const ulong o_nb1, const ulong o_nb2, const ulong o_nb3,
119 const float max_bias,
120 const float m0,
121 const float m1,
122 const int n_head_log2,
123 const float logit_softcap,
124 const int n_head_kv,
125 const global void* mask_void,
126 const ulong mask_offset,
127 const ulong mask_nb1,
128 const ulong mask_nb2,
129 const ulong mask_nb3,
130 const int mask_ne2,
131 const int mask_ne3,
132 const global void* sinks_void,
133 const ulong sinks_offset,
134 const global void * k_pad_void,
135 const global void * v_pad_void,
136 const global void * mask_pad_void,
137 const global char * blk,
138 const int n_kv_blocks,
139 const ulong mask_pad_nb1,
140 const ulong mask_pad_nb2,
141 const ulong mask_pad_nb3
142) {
143 const int tid = get_local_id(0);
144 const int block_q_idx = get_group_id(0);
145 const int head_batch_idx = get_global_id(1);
146
147#if N_SPLIT > 1
148 const int q_lane = tid / N_SPLIT;
149 const int split_idx = tid % N_SPLIT;
150#else
151 const int q_lane = tid;
152 const int split_idx = 0;
153#endif
154
155 const int my_query_row = block_q_idx * BLOCK_M + q_lane;
156 const int query_valid = my_query_row < n_q;
157
158 const int batch_idx = head_batch_idx / n_head;
159 const int head_idx = head_batch_idx % n_head;
160
161 const int gqa_ratio = n_head / n_head_kv;
162 const int head_kv_idx = head_idx / gqa_ratio;
163 const int mask_head_idx = mask_void != NULL ? head_idx % mask_ne2 : 0;
164 const int mask_batch_idx = mask_void != NULL ? batch_idx % mask_ne3 : 0;
165
166 const global char* q_base = (const global char*)q_void + q_offset;
167#ifndef FA_K_IMG
168 const global char* k_base = (const global char*)k_void + k_offset;
169#endif
170 const global char* v_base = (const global char*)v_void + v_offset;
171 global char* o_base = (global char*)o_void + o_offset;
172
173 const global char* mask_base = NULL;
174 if (mask_void != NULL) {
175 mask_base = (const global char*)mask_void + mask_offset + mask_batch_idx * mask_nb3 + mask_head_idx * mask_nb2;
176 }
177 const global char* mask_pad_base = NULL;
178 if (mask_pad_void != NULL) {
179 mask_pad_base = (const global char*)mask_pad_void + mask_batch_idx * mask_pad_nb3 + mask_head_idx * mask_pad_nb2;
180 }
181 const global char* blk_base = NULL;
182 if (blk != NULL) {
183 const int n_q_blocks = (n_q + BLOCK_M - 1) / BLOCK_M;
184 blk_base = blk + (((mask_batch_idx * mask_ne2) + mask_head_idx) * n_q_blocks + block_q_idx) * n_kv_blocks;
185 }
186
187 ACC_TYPE4 q_priv[SPLIT_DK_VEC];
188 const int dk_off = split_idx * SPLIT_DK_VEC;
189 if (query_valid) {
190 const ulong q_row_offset = batch_idx * q_nb3 + head_idx * q_nb2 + my_query_row * q_nb1;
191 const global Q_DATA_TYPE4* q_ptr = (const global Q_DATA_TYPE4*)(q_base + q_row_offset);
192 FA_UNROLL
193 for (int i = 0; i < SPLIT_DK_VEC; ++i) {
194 q_priv[i] = CONVERT_Q_ACC4(q_ptr[dk_off + i]);
195 }
196 } else {
197 FA_UNROLL
198 for (int i = 0; i < SPLIT_DK_VEC; ++i) {
199 q_priv[i] = (ACC_TYPE4)(0.0f);
200 }
201 }
202
203 ACC_TYPE4 o_acc[SPLIT_DV_VEC];
204 FA_UNROLL
205 for (int i = 0; i < SPLIT_DV_VEC; ++i) {
206 o_acc[i] = (ACC_TYPE4)(0.0f);
207 }
208
209 ACC_TYPE m_i = FA_M_INIT;
210 ACC_TYPE l_i = 0.0f;
211
212 float slope = get_alibi_slope(max_bias, head_idx, n_head_log2, m0, m1);
213
214#ifdef FA_K_LDS_T
215 // K tile transposed: [dk vec][kv row] instead of [kv row][dk vec].
216 //
217 // The QK loop walks 2 or 4 KV rows at a time against the same dk element. Row-major
218 // those are DK_VEC half4s apart, so each is its own 64-bit local read. Transposed they
219 // are adjacent, so a pair is one 128-bit read -- half the LDS issues for the same bytes,
220 // no extra registers, arithmetic untouched.
221 //
222 // This kernel looked like it should be FMA-bound (a half4 mad does ~4 ALU ops per LDS
223 // read, unlike the 1:1 of the dp4a loop), but it is NOT: a wrong-math probe that kept
224 // every FMA and removed the LDS reads ran it 38.6% faster (18.92 -> 11.62 ms/op).
225 // Explicitly 16-byte aligned: FA_LK_PAIR below reads two adjacent half4 as one float4,
226 // and the element type only obliges the compiler to align this array to 8. The indices
227 // are even so the offset is a multiple of 16, but the base has to be too, and relying
228 // on the compiler to over-align it is relying on luck.
229 __local KV_DATA_TYPE4 l_k[DK_VEC][BLOCK_N] __attribute__((aligned(16)));
230#define FA_LK(ROW, C) l_k[C][ROW]
231 // Two adjacent KV rows as one 128-bit local read (half4 pair == 16 B). j is even and
232 // BLOCK_N is even, so &l_k[c][j] is 16 B past a 16 B-aligned base.
233#define FA_LK_PAIR(C, J) as_half8(*(__local const float4 *)(&l_k[C][J]))
234#else
235 __local KV_DATA_TYPE4 l_k[BLOCK_N][DK_VEC];
236#define FA_LK(ROW, C) l_k[ROW][C]
237#endif
238 __local KV_DATA_TYPE4 l_v[BLOCK_N][DV_VEC];
239
240#if N_SPLIT > 1 && !defined(HAS_SUBGROUP_SHUFFLE)
241 __local ACC_TYPE local_partial[BLOCK_N][WG_SIZE];
242 __local ACC_TYPE local_p[BLOCK_M][BLOCK_N];
243 __local ACC_TYPE local_softmax_scale[BLOCK_M];
244 __local ACC_TYPE local_l_inv[BLOCK_M];
245#endif
246
247 for (int k_start = 0; k_start < n_kv; k_start += BLOCK_N) {
248 char blk_cur = 1;
249 if (blk_base != NULL) {
250 blk_cur = blk_base[k_start / BLOCK_N];
251 if (blk_cur == 0) continue;
252 }
253
254 const int use_kv_pad = k_pad_void != NULL && k_start + BLOCK_N > n_kv;
255 const int k_tile_start = use_kv_pad ? 0 : k_start;
256 const ulong k_tile_nb2 = use_kv_pad ? (ulong) BLOCK_N * k_nb1 : k_nb2;
257 const ulong k_tile_nb3 = use_kv_pad ? (ulong) n_head_kv * k_tile_nb2 : k_nb3;
258 const ulong v_tile_nb2 = use_kv_pad ? (ulong) BLOCK_N * v_nb1 : v_nb2;
259 const ulong v_tile_nb3 = use_kv_pad ? (ulong) n_head_kv * v_tile_nb2 : v_nb3;
260#ifdef FA_K_IMG
261 // K via texture cache for the bulk (aligned) tiles; the ragged last
262 // tile (use_kv_pad) still reads the f32-strided pad buffer from global.
263 const global char* k_tile_base = use_kv_pad ? (const global char*) k_pad_void : (const global char*) 0;
264 const int k_pitch_px_row = (int)(k_nb1 >> 3);
265 const int k_pitch_px_head = (int)(k_nb2 >> 3);
266 const int k_pitch_px_batch = (int)(k_nb3 >> 3);
267#else
268 const global char* k_tile_base = use_kv_pad ? (const global char*) k_pad_void : k_base;
269#endif
270 const global char* v_tile_base = use_kv_pad ? (const global char*) v_pad_void : v_base;
271
272 for (int i = tid; i < BLOCK_N * DK_VEC; i += WG_SIZE) {
273 const int row = i / DK_VEC;
274 const int col = i % DK_VEC;
275 const int k_row_idx = k_tile_start + row;
276 if (use_kv_pad || k_row_idx < n_kv) {
277#ifdef FA_K_IMG
278 if (use_kv_pad) {
279 const ulong k_row_offset = batch_idx * k_tile_nb3 + head_kv_idx * k_tile_nb2 + k_row_idx * k_nb1;
280 FA_LK(row, col) = ((__global KV_DATA_TYPE4*)(k_tile_base + k_row_offset))[col];
281 } else {
282 const int k_row_px = batch_idx * k_pitch_px_batch + head_kv_idx * k_pitch_px_head + k_row_idx * k_pitch_px_row;
283 FA_LK(row, col) = read_imageh(k_img, k_row_px + col);
284 }
285#else
286 const ulong k_row_offset = batch_idx * k_tile_nb3 + head_kv_idx * k_tile_nb2 + k_row_idx * k_nb1;
287 FA_LK(row, col) = ((__global KV_DATA_TYPE4*)(k_tile_base + k_row_offset))[col];
288#endif
289 } else {
290 FA_LK(row, col) = (KV_DATA_TYPE4)(0.0h);
291 }
292 }
293 for (int i = tid; i < BLOCK_N * DV_VEC; i += WG_SIZE) {
294 const int row = i / DV_VEC;
295 const int col = i % DV_VEC;
296 const int v_row_idx = k_tile_start + row;
297 if (use_kv_pad || v_row_idx < n_kv) {
298 const ulong v_row_offset = batch_idx * v_tile_nb3 + head_kv_idx * v_tile_nb2 + v_row_idx * v_nb1;
299 l_v[row][col] = ((__global KV_DATA_TYPE4*)(v_tile_base + v_row_offset))[col];
300 } else {
301 l_v[row][col] = (KV_DATA_TYPE4)(0.0h);
302 }
303 }
304 barrier(CLK_LOCAL_MEM_FENCE);
305
306#if N_SPLIT > 1 && defined(HAS_SUBGROUP_SHUFFLE)
307 {
308 const int dv_off = split_idx * SPLIT_DV_VEC;
309 for (int j = 0; j < BLOCK_N; j += 2) {
310 const int k_row0 = k_start + j;
311 const int k_row1 = k_start + j + 1;
312
313 ACC_TYPE partial0 = 0.0f;
314 ACC_TYPE partial1 = 0.0f;
315 FA_UNROLL
316 for (int k = 0; k < SPLIT_DK_VEC; k++) {
317 const ACC_TYPE4 qk = q_priv[k];
318#if defined(FA_K_LDS_T)
319 // 2 KV rows adjacent in the transposed tile: one 128-bit local read.
320 const half8 kk = FA_LK_PAIR(dk_off + k, j);
321 ACC_TYPE4 dot0 = qk * CONVERT_KV_ACC4(kk.lo);
322 ACC_TYPE4 dot1 = qk * CONVERT_KV_ACC4(kk.hi);
323#else
324 ACC_TYPE4 dot0 = qk * CONVERT_KV_ACC4(l_k[j ][dk_off + k]);
325 ACC_TYPE4 dot1 = qk * CONVERT_KV_ACC4(l_k[j+1][dk_off + k]);
326#endif
327 partial0 += dot0.s0 + dot0.s1 + dot0.s2 + dot0.s3;
328 partial1 += dot1.s0 + dot1.s1 + dot1.s2 + dot1.s3;
329 }
330
331 FA_UNROLL
332 for (int step = 1; step < N_SPLIT; step <<= 1) {
333 partial0 += sub_group_shuffle_xor(partial0, step);
334 partial1 += sub_group_shuffle_xor(partial1, step);
335 }
336
337 ACC_TYPE score0 = partial0 * scale;
338 ACC_TYPE score1 = partial1 * scale;
339 if (logit_softcap > 0.0f) {
340 score0 = logit_softcap * tanh(score0 / logit_softcap);
341 score1 = logit_softcap * tanh(score1 / logit_softcap);
342 }
343
344 if (!query_valid) { score0 = FA_M_INIT; score1 = FA_M_INIT; }
345 if (is_causal) {
346 if (k_row0 > (n_kv - n_q + my_query_row)) score0 = FA_M_INIT;
347 if (k_row1 > (n_kv - n_q + my_query_row)) score1 = FA_M_INIT;
348 }
349 if (k_row0 >= n_kv) score0 = FA_M_INIT;
350 if (k_row1 >= n_kv) score1 = FA_M_INIT;
351
352 if (query_valid && mask_base != NULL && blk_cur != 2) {
353 if (use_kv_pad && mask_pad_base != NULL) {
354 const global MASK_DATA_TYPE* mask_ptr =
355 (const global MASK_DATA_TYPE*)(mask_pad_base + my_query_row * mask_pad_nb1);
356 score0 += slope * (ACC_TYPE)mask_ptr[j];
357 score1 += slope * (ACC_TYPE)mask_ptr[j + 1];
358 } else {
359 const global MASK_DATA_TYPE* mask_ptr =
360 (const global MASK_DATA_TYPE*)(mask_base + my_query_row * mask_nb1);
361 if (k_row0 < n_kv) score0 += slope * (ACC_TYPE)mask_ptr[k_row0];
362 if (k_row1 < n_kv) score1 += slope * (ACC_TYPE)mask_ptr[k_row1];
363 }
364 }
365
366 const ACC_TYPE m_new = max(m_i, max(score0, score1));
367 // Whole tile masked (m_new == FA_M_INIT): force the exp() args
368 // far negative so the tile contributes 0, not exp(0)=1.
369 const ACC_TYPE m_exp = (m_new == FA_M_INIT) ? 0.0f : m_new;
370 const ACC_TYPE sp = native_exp(m_i - m_exp);
371 const ACC_TYPE p0 = native_exp(score0 - m_exp);
372 const ACC_TYPE p1 = native_exp(score1 - m_exp);
373
374 FA_UNROLL
375 for (int i = 0; i < SPLIT_DV_VEC; ++i) {
376 o_acc[i] = o_acc[i] * sp
377 + p0 * CONVERT_KV_ACC4(l_v[j ][dv_off + i])
378 + p1 * CONVERT_KV_ACC4(l_v[j+1][dv_off + i]);
379 }
380 l_i = l_i * sp + p0 + p1;
381 m_i = m_new;
382 }
383 }
384#elif N_SPLIT > 1
385 // N_SPLIT>1 fallback (no shuffle): 3-phase local-memory reduction.
386 // Phase 1 — partial dots for all BLOCK_N tokens.
387 for (int j = 0; j < BLOCK_N; ++j) {
388 ACC_TYPE4 dot_acc = (ACC_TYPE4)(0.0f);
389 FA_UNROLL
390 for (int k = 0; k < SPLIT_DK_VEC; k++) {
391 dot_acc = mad(q_priv[k], CONVERT_KV_ACC4(FA_LK(j, dk_off + k)), dot_acc);
392 }
393 local_partial[j][tid] =
394 dot_acc.s0 + dot_acc.s1 + dot_acc.s2 + dot_acc.s3;
395 }
396 barrier(CLK_LOCAL_MEM_FENCE); // 1 barrier: partial dots visible
397
398 // Phase 2 — split_idx==0 reduces partial sums and computes block softmax.
399 if (split_idx == 0) {
400 if (query_valid) {
401 ACC_TYPE m_new = m_i;
402 for (int j = 0; j < BLOCK_N; ++j) {
403 const int k_row = k_start + j;
404 ACC_TYPE score = 0.0f;
405 FA_UNROLL
406 for (int s = 0; s < N_SPLIT; s++) {
407 score += local_partial[j][q_lane * N_SPLIT + s];
408 }
409 score *= scale;
410 if (logit_softcap > 0.0f) {
411 score = logit_softcap * tanh(score / logit_softcap);
412 }
413
414 if (is_causal && k_row > (n_kv - n_q + my_query_row)) score = FA_M_INIT;
415 if (k_row >= n_kv) score = FA_M_INIT;
416
417 if (mask_base != NULL && blk_cur != 2) {
418 if (use_kv_pad && mask_pad_base != NULL) {
419 const global MASK_DATA_TYPE* mask_ptr =
420 (const global MASK_DATA_TYPE*)(mask_pad_base + my_query_row * mask_pad_nb1);
421 score += slope * (ACC_TYPE)mask_ptr[j];
422 } else {
423 const global MASK_DATA_TYPE* mask_ptr =
424 (const global MASK_DATA_TYPE*)(mask_base + my_query_row * mask_nb1);
425 if (k_row < n_kv) score += slope * (ACC_TYPE)mask_ptr[k_row];
426 }
427 }
428
429 m_new = max(m_new, score);
430 local_p[q_lane][j] = score;
431 }
432
433 const ACC_TYPE m_exp = (m_new == FA_M_INIT) ? 0.0f : m_new;
434 const ACC_TYPE sp = native_exp(m_i - m_exp);
435 ACC_TYPE l_new = l_i * sp;
436 for (int j = 0; j < BLOCK_N; ++j) {
437 const ACC_TYPE p = native_exp(local_p[q_lane][j] - m_exp);
438 local_p[q_lane][j] = p;
439 l_new += p;
440 }
441 local_softmax_scale[q_lane] = sp;
442 l_i = l_new;
443 m_i = m_new;
444 } else {
445 local_softmax_scale[q_lane] = 1.0f;
446 for (int j = 0; j < BLOCK_N; ++j) local_p[q_lane][j] = 0.0f;
447 }
448 }
449 barrier(CLK_LOCAL_MEM_FENCE);
450
451 // Phase 3 — V accumulate using broadcast probabilities.
452 {
453 const ACC_TYPE sp_block = local_softmax_scale[q_lane];
454 const int dv_off = split_idx * SPLIT_DV_VEC;
455 FA_UNROLL
456 for (int i = 0; i < SPLIT_DV_VEC; ++i) {
457 o_acc[i] *= sp_block;
458 }
459 for (int j = 0; j < BLOCK_N; ++j) {
460 const ACC_TYPE p = local_p[q_lane][j];
461 FA_UNROLL
462 for (int i = 0; i < SPLIT_DV_VEC; ++i) {
463 o_acc[i] = mad(p, CONVERT_KV_ACC4(l_v[j][dv_off + i]), o_acc[i]);
464 }
465 }
466 }
467#else
468 // N_SPLIT==1: j+=4 unroll. Requires BLOCK_N % 4 == 0.
469 if (query_valid) {
470 for (int j = 0; j < BLOCK_N; j += 4) {
471 const int k_row0 = k_start + j;
472 const int k_row1 = k_start + j + 1;
473 const int k_row2 = k_start + j + 2;
474 const int k_row3 = k_start + j + 3;
475
476 ACC_TYPE4 dot_acc0 = (ACC_TYPE4)(0.0f);
477 ACC_TYPE4 dot_acc1 = (ACC_TYPE4)(0.0f);
478 ACC_TYPE4 dot_acc2 = (ACC_TYPE4)(0.0f);
479 ACC_TYPE4 dot_acc3 = (ACC_TYPE4)(0.0f);
480 FA_UNROLL
481 for (int k = 0; k < DK_VEC; k++) {
482 const ACC_TYPE4 qk = q_priv[k];
483#if defined(FA_K_LDS_T)
484 // 4 KV rows adjacent in the transposed tile: two 128-bit local reads
485 // instead of four 64-bit ones.
486 const half8 kk01 = FA_LK_PAIR(k, j);
487 const half8 kk23 = FA_LK_PAIR(k, j + 2);
488 dot_acc0 = mad(qk, CONVERT_KV_ACC4(kk01.lo), dot_acc0);
489 dot_acc1 = mad(qk, CONVERT_KV_ACC4(kk01.hi), dot_acc1);
490 dot_acc2 = mad(qk, CONVERT_KV_ACC4(kk23.lo), dot_acc2);
491 dot_acc3 = mad(qk, CONVERT_KV_ACC4(kk23.hi), dot_acc3);
492#else
493 dot_acc0 = mad(qk, CONVERT_KV_ACC4(l_k[j][k]), dot_acc0);
494 dot_acc1 = mad(qk, CONVERT_KV_ACC4(l_k[j+1][k]), dot_acc1);
495 dot_acc2 = mad(qk, CONVERT_KV_ACC4(l_k[j+2][k]), dot_acc2);
496 dot_acc3 = mad(qk, CONVERT_KV_ACC4(l_k[j+3][k]), dot_acc3);
497#endif
498 }
499 ACC_TYPE s0 = (dot_acc0.s0 + dot_acc0.s1 + dot_acc0.s2 + dot_acc0.s3) * scale;
500 ACC_TYPE s1 = (dot_acc1.s0 + dot_acc1.s1 + dot_acc1.s2 + dot_acc1.s3) * scale;
501 ACC_TYPE s2 = (dot_acc2.s0 + dot_acc2.s1 + dot_acc2.s2 + dot_acc2.s3) * scale;
502 ACC_TYPE s3 = (dot_acc3.s0 + dot_acc3.s1 + dot_acc3.s2 + dot_acc3.s3) * scale;
503 if (logit_softcap > 0.0f) {
504 s0 = logit_softcap * tanh(s0 / logit_softcap);
505 s1 = logit_softcap * tanh(s1 / logit_softcap);
506 s2 = logit_softcap * tanh(s2 / logit_softcap);
507 s3 = logit_softcap * tanh(s3 / logit_softcap);
508 }
509
510 if (is_causal) {
511 const int causal_limit = n_kv - n_q + my_query_row;
512 if (k_row0 > causal_limit) s0 = FA_M_INIT;
513 if (k_row1 > causal_limit) s1 = FA_M_INIT;
514 if (k_row2 > causal_limit) s2 = FA_M_INIT;
515 if (k_row3 > causal_limit) s3 = FA_M_INIT;
516 }
517 if (k_row0 >= n_kv) s0 = FA_M_INIT;
518 if (k_row1 >= n_kv) s1 = FA_M_INIT;
519 if (k_row2 >= n_kv) s2 = FA_M_INIT;
520 if (k_row3 >= n_kv) s3 = FA_M_INIT;
521
522 if (mask_base != NULL && blk_cur != 2) {
523 if (use_kv_pad && mask_pad_base != NULL) {
524 const global MASK_DATA_TYPE* mask_ptr = (const global MASK_DATA_TYPE*)(mask_pad_base + my_query_row * mask_pad_nb1);
525 s0 += slope * (ACC_TYPE)mask_ptr[j];
526 s1 += slope * (ACC_TYPE)mask_ptr[j + 1];
527 s2 += slope * (ACC_TYPE)mask_ptr[j + 2];
528 s3 += slope * (ACC_TYPE)mask_ptr[j + 3];
529 } else {
530 const global MASK_DATA_TYPE* mask_ptr = (const global MASK_DATA_TYPE*)(mask_base + my_query_row * mask_nb1);
531 if (k_row0 < n_kv) s0 += slope * (ACC_TYPE)mask_ptr[k_row0];
532 if (k_row1 < n_kv) s1 += slope * (ACC_TYPE)mask_ptr[k_row1];
533 if (k_row2 < n_kv) s2 += slope * (ACC_TYPE)mask_ptr[k_row2];
534 if (k_row3 < n_kv) s3 += slope * (ACC_TYPE)mask_ptr[k_row3];
535 }
536 }
537
538 const ACC_TYPE m_new = max(m_i, max(max(s0, s1), max(s2, s3)));
539 // Whole tile masked (m_new == FA_M_INIT): force the exp() args
540 // far negative so the tile contributes 0, not exp(0)=1.
541 const ACC_TYPE m_exp = (m_new == FA_M_INIT) ? 0.0f : m_new;
542 const ACC_TYPE scale_prev = native_exp(m_i - m_exp);
543 const ACC_TYPE p0 = native_exp(s0 - m_exp);
544 const ACC_TYPE p1 = native_exp(s1 - m_exp);
545 const ACC_TYPE p2 = native_exp(s2 - m_exp);
546 const ACC_TYPE p3 = native_exp(s3 - m_exp);
547
548 FA_UNROLL
549 for (int i = 0; i < DV_VEC; ++i) {
550 o_acc[i] = mad(p3, CONVERT_KV_ACC4(l_v[j+3][i]),
551 mad(p2, CONVERT_KV_ACC4(l_v[j+2][i]),
552 mad(p1, CONVERT_KV_ACC4(l_v[j+1][i]),
553 mad(p0, CONVERT_KV_ACC4(l_v[j][i]),
554 o_acc[i] * scale_prev))));
555 }
556 l_i = l_i * scale_prev + p0 + p1 + p2 + p3;
557 m_i = m_new;
558 }
559 }
560#endif
561 // End of tile: every thread must finish reading l_k/l_v before the
562 // next iteration's load overwrites them (WAR hazard on local memory).
563 barrier(CLK_LOCAL_MEM_FENCE);
564 }
565
566 // Write output.
567#if N_SPLIT > 1 && defined(HAS_SUBGROUP_SHUFFLE)
568 if (query_valid) {
569 ACC_TYPE sinks_sp = 1.0f;
570 if (sinks_void != NULL) {
571 const global ACC_TYPE* sinks_ptr = (const global ACC_TYPE*)((const global char*)sinks_void + sinks_offset);
572 const ACC_TYPE m_sink = sinks_ptr[head_idx];
573 const ACC_TYPE m_final = max(m_i, m_sink);
574 sinks_sp = exp(m_i - m_final);
575 l_i = l_i * sinks_sp + exp(m_sink - m_final);
576 m_i = m_final;
577 }
578 const ACC_TYPE l_inv = (l_i > 0.0f) ? (1.0f / l_i) : 0.0f;
579 const int dv_off = split_idx * SPLIT_DV_VEC;
580 const ulong o_row_offset = batch_idx * o_nb3 + my_query_row * o_nb2 + head_idx * o_nb1;
581 global O_DATA_TYPE4 *o_row = (global O_DATA_TYPE4 *)(o_base + o_row_offset);
582 if (l_inv > 0.0f) {
583 FA_UNROLL
584 for (int i = 0; i < SPLIT_DV_VEC; ++i) {
585 o_row[dv_off + i] = CONVERT_O_DATA4(o_acc[i] * sinks_sp * l_inv);
586 }
587 } else {
588 FA_UNROLL
589 for (int i = 0; i < SPLIT_DV_VEC; ++i) {
590 o_row[dv_off + i] = (O_DATA_TYPE4)(0.0f);
591 }
592 }
593 }
594#elif N_SPLIT > 1
595 if (split_idx == 0) {
596 ACC_TYPE sinks_sp = 1.0f;
597 if (query_valid && sinks_void != NULL) {
598 const global ACC_TYPE* sinks_ptr = (const global ACC_TYPE*)((const global char*)sinks_void + sinks_offset);
599 const ACC_TYPE m_sink = sinks_ptr[head_idx];
600 const ACC_TYPE m_final = max(m_i, m_sink);
601 sinks_sp = exp(m_i - m_final);
602 l_i = l_i * sinks_sp + exp(m_sink - m_final);
603 m_i = m_final;
604 }
605 local_softmax_scale[q_lane] = sinks_sp;
606 local_l_inv[q_lane] = (query_valid && l_i > 0.0f) ? (1.0f / l_i) : 0.0f;
607 }
608 barrier(CLK_LOCAL_MEM_FENCE);
609
610 if (query_valid) {
611 const ACC_TYPE sinks_sp = local_softmax_scale[q_lane];
612 const ACC_TYPE l_inv = local_l_inv[q_lane];
613 const int dv_off = split_idx * SPLIT_DV_VEC;
614 const ulong o_row_offset = batch_idx * o_nb3 + my_query_row * o_nb2 + head_idx * o_nb1;
615 global O_DATA_TYPE4 *o_row = (global O_DATA_TYPE4 *)(o_base + o_row_offset);
616 if (l_inv > 0.0f) {
617 FA_UNROLL
618 for (int i = 0; i < SPLIT_DV_VEC; ++i) {
619 o_row[dv_off + i] = CONVERT_O_DATA4(o_acc[i] * sinks_sp * l_inv);
620 }
621 } else {
622 FA_UNROLL
623 for (int i = 0; i < SPLIT_DV_VEC; ++i) {
624 o_row[dv_off + i] = (O_DATA_TYPE4)(0.0f);
625 }
626 }
627 }
628#else
629 if (query_valid) {
630 if (sinks_void != NULL) {
631 const global ACC_TYPE* sinks_ptr = (const global ACC_TYPE*)((const global char*)sinks_void + sinks_offset);
632 const ACC_TYPE m_sink = sinks_ptr[head_idx];
633 const ACC_TYPE m_final = max(m_i, m_sink);
634
635 const ACC_TYPE scale_o = exp(m_i - m_final);
636 FA_UNROLL
637 for (int i = 0; i < DV_VEC; ++i) {
638 o_acc[i] *= scale_o;
639 }
640
641 l_i = l_i * exp(m_i - m_final) + exp(m_sink - m_final);
642 }
643
644 const ulong o_row_offset = batch_idx * o_nb3 + my_query_row * o_nb2 + head_idx * o_nb1;
645 global O_DATA_TYPE4 *o_row = (global O_DATA_TYPE4 *)(o_base + o_row_offset);
646 if (l_i > 0.0f) {
647 const ACC_TYPE l_inv = 1.0f / l_i;
648 FA_UNROLL
649 for (int i = 0; i < DV_VEC; ++i) {
650 o_row[i] = CONVERT_O_DATA4(o_acc[i] * l_inv);
651 }
652 } else {
653 FA_UNROLL
654 for (int i = 0; i < DV_VEC; ++i) {
655 o_row[i] = (O_DATA_TYPE4)(0.0f);
656 }
657 }
658 }
659#endif
660}
661#endif // !FA_DECODE_ONLY
662
663// allow bypassing decode kernels to avoid compiler crash for DK=512 on Adreno GPUs
664#ifndef FA_PREFILL_ONLY
665#if !defined(FA_MQ_ONLY) && !defined(FA_VEC_ONLY)
666REQD_FA_SG
667__kernel void flash_attn_f32_f16_q1(
668 const global void * q_void, ulong q_offset,
669 const global void * k_void, ulong k_offset,
670 const global void * v_void, ulong v_offset,
671 global void * o_void, ulong o_offset,
672 const float scale,
673 const int n_q,
674 const int n_kv,
675 const int is_causal,
676 const int n_head,
677 const ulong q_nb1, const ulong q_nb2, const ulong q_nb3,
678 const ulong k_nb1, const ulong k_nb2, const ulong k_nb3,
679 const ulong v_nb1, const ulong v_nb2, const ulong v_nb3,
680 const ulong o_nb1, const ulong o_nb2, const ulong o_nb3,
681 const float max_bias,
682 const float m0,
683 const float m1,
684 const int n_head_log2,
685 const float logit_softcap,
686 const int n_head_kv,
687 const global void* mask_void,
688 const ulong mask_offset,
689 const ulong mask_nb1,
690 const ulong mask_nb2,
691 const ulong mask_nb3,
692 const int mask_ne2,
693 const int mask_ne3,
694 const global void* sinks_void,
695 const ulong sinks_offset
696) {
697 const int tid = get_local_id(0);
698 const int head_batch_idx = get_global_id(1);
699
700 const int batch_idx = head_batch_idx / n_head;
701 const int head_idx = head_batch_idx % n_head;
702
703 const int gqa_ratio = n_head / n_head_kv;
704 const int head_kv_idx = head_idx / gqa_ratio;
705
706 const global char* q_base = (const global char*)q_void + q_offset;
707#ifndef FA_K_IMG
708 const global char* k_base = (const global char*)k_void + k_offset;
709#endif
710 const global char* v_base = (const global char*)v_void + v_offset;
711 global char* o_base = (global char*)o_void + o_offset;
712
713 const global char* mask_base = NULL;
714 if (mask_void != NULL) {
715 const int mask_head_idx = head_idx % mask_ne2;
716 const int mask_batch_idx = batch_idx % mask_ne3;
717 mask_base = (const global char*)mask_void + mask_offset + mask_batch_idx * mask_nb3 + mask_head_idx * mask_nb2;
718 }
719
720 // Q is uniform across WG threads (n_q=1). Share via local memory to
721 // avoid per-thread q_priv[DK_VEC] dynamic-indexed private array that
722 // spills to DDR on Adreno.
723 __local ACC_TYPE4 q_shared[DK_VEC];
724 const ulong q_row_offset = batch_idx * q_nb3 + head_idx * q_nb2;
725 const global Q_DATA_TYPE4* q_ptr = (const global Q_DATA_TYPE4*)(q_base + q_row_offset);
726 for (int i = tid; i < DK_VEC; i += Q1_WG_SIZE) {
727 q_shared[i] = CONVERT_Q_ACC4(q_ptr[i]);
728 }
729 sub_group_barrier(CLK_LOCAL_MEM_FENCE);
730
731 float slope = get_alibi_slope(max_bias, head_idx, n_head_log2, m0, m1);
732
733 const global ACC_TYPE* sinks_ptr = NULL;
734 if (sinks_void != NULL) {
735 sinks_ptr = (const global ACC_TYPE*)((const global char*)sinks_void + sinks_offset);
736 }
737
738 ACC_TYPE m_i = (sinks_ptr != NULL) ? sinks_ptr[head_idx] : FA_M_INIT;
739 for (int k_idx = tid; k_idx < n_kv; k_idx += Q1_WG_SIZE) {
740 const ulong k_row_offset = batch_idx * k_nb3 + head_kv_idx * k_nb2 + k_idx * k_nb1;
741 const global KV_DATA_TYPE4* k_ptr = (const global KV_DATA_TYPE4*)(k_base + k_row_offset);
742 ACC_TYPE4 dot_acc = (ACC_TYPE4)(0.0f);
743 FA_UNROLL
744 for (int k = 0; k < DK_VEC; k++) {
745 dot_acc = mad(q_shared[k], CONVERT_KV_ACC4(k_ptr[k]), dot_acc);
746 }
747 ACC_TYPE score = (dot_acc.s0 + dot_acc.s1 + dot_acc.s2 + dot_acc.s3) * 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 m_i = max(m_i, score);
756 }
757
758 const ACC_TYPE m_final = sub_group_reduce_max(m_i);
759
760 ACC_TYPE4 o_acc[DV_VEC];
761 FA_UNROLL
762 for (int i = 0; i < DV_VEC; ++i) o_acc[i] = (ACC_TYPE4)(0.0f);
763 ACC_TYPE l_i = 0.0f;
764
765 for (int k_idx = tid; k_idx < n_kv; k_idx += Q1_WG_SIZE) {
766 const ulong k_row_offset = batch_idx * k_nb3 + head_kv_idx * k_nb2 + k_idx * k_nb1;
767 const ulong v_row_offset = batch_idx * v_nb3 + head_kv_idx * v_nb2 + k_idx * v_nb1;
768 const global KV_DATA_TYPE4* k_ptr = (const global KV_DATA_TYPE4*)(k_base + k_row_offset);
769 const global KV_DATA_TYPE4* v_ptr = (const global KV_DATA_TYPE4*)(v_base + v_row_offset);
770 ACC_TYPE4 dot_acc = (ACC_TYPE4)(0.0f);
771 FA_UNROLL
772 for (int k = 0; k < DK_VEC; k++) {
773 dot_acc = mad(q_shared[k], CONVERT_KV_ACC4(k_ptr[k]), dot_acc);
774 }
775 ACC_TYPE score = (dot_acc.s0 + dot_acc.s1 + dot_acc.s2 + dot_acc.s3) * scale;
776 if (logit_softcap > 0.0f) {
777 score = logit_softcap * tanh(score / logit_softcap);
778 }
779 if (mask_base != NULL) {
780 const global MASK_DATA_TYPE* mask_ptr = (const global MASK_DATA_TYPE*)(mask_base);
781 score += slope * (ACC_TYPE)mask_ptr[k_idx];
782 }
783 const ACC_TYPE p = exp(score - m_final);
784 l_i += p;
785 FA_UNROLL
786 for (int i = 0; i < DV_VEC; i++) {
787 o_acc[i] = mad(p, CONVERT_KV_ACC4(v_ptr[i]), o_acc[i]);
788 }
789 }
790
791 __local ACC_TYPE4 local_o_comp[Q1_WG_SIZE];
792 const ACC_TYPE l_red = sub_group_reduce_add(l_i);
793
794 const ulong o_row_offset = batch_idx * o_nb3 + head_idx * o_nb1;
795 global O_DATA_TYPE4 *o_row = (global O_DATA_TYPE4 *)(o_base + o_row_offset);
796 ACC_TYPE l_final = l_red;
797
798 if (sinks_ptr != NULL) {
799 l_final += exp(sinks_ptr[head_idx] - m_final);
800 }
801
802 if (l_final > 0.0f) {
803 const ACC_TYPE l_inv = 1.0f / l_final;
804 for (int i = 0; i < DV_VEC; i++) {
805 local_o_comp[tid] = o_acc[i];
806 sub_group_barrier(CLK_LOCAL_MEM_FENCE);
807 FA_UNROLL
808 for (int s = Q1_WG_SIZE / 2; s > 0; s >>= 1) {
809 if (tid < s) local_o_comp[tid] += local_o_comp[tid + s];
810 sub_group_barrier(CLK_LOCAL_MEM_FENCE);
811 }
812 if (tid == 0) {
813 o_row[i] = CONVERT_O_DATA4(local_o_comp[0] * l_inv);
814 }
815 }
816 } else if (tid == 0) {
817 FA_UNROLL
818 for (int i = 0; i < DV_VEC; ++i) o_row[i] = (O_DATA_TYPE4)(0.0f);
819 }
820}
821
822#endif // !FA_MQ_ONLY (q1)
823// decode variant for large DV (e.g. Gemma-4 DK=DV=512 global layers).
824#define VEC_NSG 4
825#define VEC_WG_SIZE (Q1_WG_SIZE * VEC_NSG)
826#define Q1V_DV_PER_THREAD ((DV_VEC + Q1_WG_SIZE - 1) / Q1_WG_SIZE)
827
828// allow bypassing the kernel to avoid compiler crash for DK=512 on Adreno GPUs
829#if !defined(FA_DECODE_MINIMAL) && !defined(FA_MQ_ONLY)
830REQD_SUBGROUP_SIZE_64
831__kernel void flash_attn_f32_f16_q1_vec(
832 const global void * q_void, ulong q_offset,
833 const global void * k_void, ulong k_offset,
834 const global void * v_void, ulong v_offset,
835 global void * o_void, ulong o_offset,
836 const float scale,
837 const int n_q,
838 const int n_kv,
839 const int is_causal,
840 const int n_head,
841 const ulong q_nb1, const ulong q_nb2, const ulong q_nb3,
842 const ulong k_nb1, const ulong k_nb2, const ulong k_nb3,
843 const ulong v_nb1, const ulong v_nb2, const ulong v_nb3,
844 const ulong o_nb1, const ulong o_nb2, const ulong o_nb3,
845 const float max_bias,
846 const float m0,
847 const float m1,
848 const int n_head_log2,
849 const float logit_softcap,
850 const int n_head_kv,
851 const global void* mask_void,
852 const ulong mask_offset,
853 const ulong mask_nb1,
854 const ulong mask_nb2,
855 const ulong mask_nb3,
856 const int mask_ne2,
857 const int mask_ne3,
858 const global void* sinks_void,
859 const ulong sinks_offset
860) {
861 const int tid = get_local_id(0);
862 const int sgid = tid / Q1_WG_SIZE; // subgroup index (0..VEC_NSG-1)
863 const int tid_sg = tid % Q1_WG_SIZE; // lane within subgroup
864 const int head_batch_idx = get_global_id(1);
865
866 const int batch_idx = head_batch_idx / n_head;
867 const int head_idx = head_batch_idx % n_head;
868
869 const int gqa_ratio = n_head / n_head_kv;
870 const int head_kv_idx = head_idx / gqa_ratio;
871
872 const global char * q_base = (const global char *) q_void + q_offset;
873 const global char * k_base = (const global char *) k_void + k_offset;
874 const global char * v_base = (const global char *) v_void + v_offset;
875 global char * o_base = (global char *) o_void + o_offset;
876
877 const global char * mask_base = NULL;
878 if (mask_void != NULL) {
879 const int mask_head_idx = head_idx % mask_ne2;
880 const int mask_batch_idx = batch_idx % mask_ne3;
881 mask_base = (const global char *) mask_void + mask_offset +
882 mask_batch_idx * mask_nb3 + mask_head_idx * mask_nb2;
883 }
884
885 // Q is uniform across the WG — stage in __local once. All WG threads load.
886 __local ACC_TYPE4 q_shared[DK_VEC];
887 {
888 const ulong q_row_offset = batch_idx * q_nb3 + head_idx * q_nb2;
889 const global Q_DATA_TYPE4 * q_ptr = (const global Q_DATA_TYPE4 *) (q_base + q_row_offset);
890 for (int i = tid; i < DK_VEC; i += VEC_WG_SIZE) {
891 q_shared[i] = CONVERT_Q_ACC4(q_ptr[i]);
892 }
893 }
894 barrier(CLK_LOCAL_MEM_FENCE);
895
896 const float slope = get_alibi_slope(max_bias, head_idx, n_head_log2, m0, m1);
897
898 const global ACC_TYPE * sinks_ptr = NULL;
899 if (sinks_void != NULL) {
900 sinks_ptr = (const global ACC_TYPE *) ((const global char *) sinks_void + sinks_offset);
901 }
902
903 // per-thread DV slice within its subgroup
904 // DV=512 -> 2x float4 = 32 bytes; DV=256 -> 1x float4 - no spill
905 ACC_TYPE4 o_acc[Q1V_DV_PER_THREAD];
906 #pragma unroll
907 for (int i = 0; i < Q1V_DV_PER_THREAD; ++i) o_acc[i] = (ACC_TYPE4)(0.0f);
908
909 // each subgroup independently runs the FA-2 online softmax over its slice of n_kv.
910 // sinks are not folded into per-subgroup m_i — they're added once in
911 // the cross-subgroup merge to avoid double-counting.
912 ACC_TYPE m_i = FA_M_INIT;
913 ACC_TYPE l_i = 0.0f;
914
915 const int kv_per_sg = (n_kv + VEC_NSG - 1) / VEC_NSG;
916 const int kv_start = sgid * kv_per_sg;
917 const int kv_end = min(n_kv, kv_start + kv_per_sg);
918
919 for (int k_idx = kv_start; k_idx < kv_end; ++k_idx) {
920 const ulong k_row_off = batch_idx * k_nb3 + head_kv_idx * k_nb2 + k_idx * k_nb1;
921 const ulong v_row_off = batch_idx * v_nb3 + head_kv_idx * v_nb2 + k_idx * v_nb1;
922 const global KV_DATA_TYPE4 * k_ptr = (const global KV_DATA_TYPE4 *) (k_base + k_row_off);
923 const global KV_DATA_TYPE4 * v_ptr = (const global KV_DATA_TYPE4 *) (v_base + v_row_off);
924
925 // Q*K^T: each thread accumulates its DK slice; subgroup-reduce the partial.
926 ACC_TYPE4 dot4 = (ACC_TYPE4)(0.0f);
927 for (int k = tid_sg; k < DK_VEC; k += Q1_WG_SIZE) {
928 dot4 = mad(q_shared[k], CONVERT_KV_ACC4(k_ptr[k]), dot4);
929 }
930 ACC_TYPE dot_partial = dot4.s0 + dot4.s1 + dot4.s2 + dot4.s3;
931 ACC_TYPE score = sub_group_reduce_add(dot_partial) * scale;
932 if (logit_softcap > 0.0f) {
933 score = logit_softcap * tanh(score / logit_softcap);
934 }
935
936 if (mask_base != NULL) {
937 const global MASK_DATA_TYPE * mask_ptr = (const global MASK_DATA_TYPE *) mask_base;
938 score += slope * (ACC_TYPE) mask_ptr[k_idx];
939 }
940
941 // FA-2 online update. All threads in the subgroup see the same score,
942 // so m_i and l_i evolve identically across lanes within the subgroup.
943 const ACC_TYPE m_new = max(m_i, score);
944 const ACC_TYPE scale_prev = native_exp(m_i - m_new);
945 const ACC_TYPE p = native_exp(score - m_new);
946
947 int idx = 0;
948 for (int dv_idx = tid_sg; dv_idx < DV_VEC; dv_idx += Q1_WG_SIZE, ++idx) {
949 o_acc[idx] = mad(p, CONVERT_KV_ACC4(v_ptr[dv_idx]), o_acc[idx] * scale_prev);
950 }
951 l_i = l_i * scale_prev + p;
952 m_i = m_new;
953 }
954
955 // Cross-subgroup merge via __local. Each subgroup publishes (m_i, l_i)
956 // and its o_acc slice; subgroup 0 then folds them into the final norm
957 // and writes the row.
958 __local ACC_TYPE sg_m[VEC_NSG];
959 __local ACC_TYPE sg_l[VEC_NSG];
960 __local ACC_TYPE4 sg_o[VEC_NSG][DV_VEC];
961
962 if (tid_sg == 0) {
963 sg_m[sgid] = m_i;
964 sg_l[sgid] = l_i;
965 }
966 {
967 int idx = 0;
968 for (int dv_idx = tid_sg; dv_idx < DV_VEC; dv_idx += Q1_WG_SIZE, ++idx) {
969 sg_o[sgid][dv_idx] = o_acc[idx];
970 }
971 }
972 barrier(CLK_LOCAL_MEM_FENCE);
973
974 if (sgid == 0) {
975 // m_final = max over all subgroups' m_i, plus the sink (if any).
976 ACC_TYPE m_final = sg_m[0];
977 #pragma unroll
978 for (int s = 1; s < VEC_NSG; ++s) {
979 m_final = max(m_final, sg_m[s]);
980 }
981 if (sinks_ptr != NULL) {
982 m_final = max(m_final, sinks_ptr[head_idx]);
983 }
984
985 ACC_TYPE l_final = 0.0f;
986 #pragma unroll
987 for (int s = 0; s < VEC_NSG; ++s) {
988 l_final += sg_l[s] * native_exp(sg_m[s] - m_final);
989 }
990 if (sinks_ptr != NULL) {
991 l_final += native_exp(sinks_ptr[head_idx] - m_final);
992 }
993 const ACC_TYPE l_inv = (l_final > 0.0f) ? (1.0f / l_final) : 0.0f;
994
995 const ulong o_row_offset = batch_idx * o_nb3 + head_idx * o_nb1;
996 global O_DATA_TYPE4 * o_row = (global O_DATA_TYPE4 *) (o_base + o_row_offset);
997
998 // Each thread in subgroup 0 writes its DV slice, folding all subgroups'
999 // contributions with the rescale factor.
1000 int idx = 0;
1001 for (int dv_idx = tid_sg; dv_idx < DV_VEC; dv_idx += Q1_WG_SIZE, ++idx) {
1002 ACC_TYPE4 o_merged = (ACC_TYPE4)(0.0f);
1003 #pragma unroll
1004 for (int s = 0; s < VEC_NSG; ++s) {
1005 const ACC_TYPE alpha = native_exp(sg_m[s] - m_final);
1006 o_merged = mad((ACC_TYPE4)(alpha), sg_o[s][dv_idx], o_merged);
1007 }
1008 o_row[dv_idx] = CONVERT_O_DATA4(o_merged * l_inv);
1009 }
1010 }
1011}
1012
1013#endif // !FA_DECODE_MINIMAL
1014
1015#ifndef FA_DECODE_ONLY
1016
1017// flash_attn_f32_f16_q1_local_tile
1018// one WG per (q_idx, q_head)
1019
1020#define LT_KC 32
1021#define LT_WG 128
1022
1023#ifndef FA_MQ_ONLY // q1_local_tile excluded from the MQ-only (g8) program
1024REQD_SUBGROUP_SIZE_64
1025__kernel void flash_attn_f32_f16_q1_local_tile(
1026 const global void * q_void, ulong q_offset,
1027 const global void * k_void, ulong k_offset,
1028 const global void * v_void, ulong v_offset,
1029 global void * o_void, ulong o_offset,
1030 const float scale,
1031 const int n_q,
1032 const int n_kv,
1033 const int is_causal,
1034 const int n_head,
1035 const ulong q_nb1, const ulong q_nb2, const ulong q_nb3,
1036 const ulong k_nb1, const ulong k_nb2, const ulong k_nb3,
1037 const ulong v_nb1, const ulong v_nb2, const ulong v_nb3,
1038 const ulong o_nb1, const ulong o_nb2, const ulong o_nb3,
1039 const float max_bias,
1040 const float m0,
1041 const float m1,
1042 const int n_head_log2,
1043 const float logit_softcap,
1044 const int n_head_kv,
1045 const global void * mask_void,
1046 const ulong mask_offset,
1047 const ulong mask_nb1,
1048 const ulong mask_nb2,
1049 const ulong mask_nb3,
1050 const int mask_ne2,
1051 const int mask_ne3,
1052 const global void * sinks_void,
1053 const ulong sinks_offset
1054) {
1055 const int q_idx = get_global_id(0) / LT_WG;
1056 const int head_idx = get_global_id(1);
1057 const int batch_idx = get_global_id(2);
1058 const int tid = get_local_id(0);
1059
1060 const int gqa_ratio = n_head_kv > 0 ? (n_head / n_head_kv) : 1;
1061 const int head_kv_idx = head_idx / gqa_ratio;
1062
1063 const float slope = get_alibi_slope(max_bias, head_idx, n_head_log2, m0, m1);
1064
1065 __local half k_tile[LT_KC * DK]; // 32*128*2 = 8 KB at DK=128
1066 __local half v_tile[LT_KC * DV]; // 8 KB
1067 __local float red[LT_WG]; // 512 B reduction scratch
1068 __local float score_shared; // broadcast score (each K-step)
1069
1070 // Each thread owns one float of Q at index `tid` (assumes LT_WG == DK).
1071 const global char * q_row_base = (const global char *) q_void + q_offset +
1072 batch_idx * q_nb3 + head_idx * q_nb2 + (ulong) q_idx * q_nb1;
1073 float q_val = ((const global float *) q_row_base)[tid];
1074
1075 const global char * mask_base = NULL;
1076 if (mask_void != NULL) {
1077 const int mask_head_idx = head_idx % mask_ne2;
1078 const int mask_batch_idx = batch_idx % mask_ne3;
1079 mask_base = (const global char *) mask_void + mask_offset +
1080 mask_batch_idx * mask_nb3 + mask_head_idx * mask_nb2 +
1081 (ulong) q_idx * mask_nb1;
1082 }
1083
1084 float o_val = 0.0f;
1085 float m_i = FA_M_INIT;
1086 float l_i = 0.0f;
1087
1088 for (int kb = 0; kb < n_kv; kb += LT_KC) {
1089 const int tile_len = min(LT_KC, n_kv - kb);
1090
1091 // Stage K and V tiles into __local.
1092 for (int i = tid; i < tile_len * DK; i += LT_WG) {
1093 const int j = i / DK;
1094 const int d = i % DK;
1095 const int kv_idx = kb + j;
1096 const global char * k_row = (const global char *) k_void + k_offset +
1097 batch_idx * k_nb3 + head_kv_idx * k_nb2 +
1098 (ulong) kv_idx * k_nb1;
1099 const global char * v_row = (const global char *) v_void + v_offset +
1100 batch_idx * v_nb3 + head_kv_idx * v_nb2 +
1101 (ulong) kv_idx * v_nb1;
1102 k_tile[j * DK + d] = ((const global half *) k_row)[d];
1103 v_tile[j * DV + d] = ((const global half *) v_row)[d];
1104 }
1105 barrier(CLK_LOCAL_MEM_FENCE);
1106
1107 for (int j = 0; j < tile_len; ++j) {
1108 const int kv_idx = kb + j;
1109
1110 // Q·K dot via __local tree-reduce.
1111 red[tid] = q_val * convert_float(k_tile[j * DK + tid]);
1112 barrier(CLK_LOCAL_MEM_FENCE);
1113 for (int stride = LT_WG >> 1; stride > 0; stride >>= 1) {
1114 if (tid < stride) {
1115 red[tid] += red[tid + stride];
1116 }
1117 barrier(CLK_LOCAL_MEM_FENCE);
1118 }
1119
1120 if (tid == 0) {
1121 float s = red[0] * scale;
1122 if (logit_softcap > 0.0f) {
1123 s = logit_softcap * tanh(s / logit_softcap);
1124 }
1125 if (mask_base != NULL) {
1126 const global MASK_DATA_TYPE * mask_ptr = (const global MASK_DATA_TYPE *) mask_base;
1127 s += slope * (float) mask_ptr[kv_idx];
1128 }
1129 score_shared = s;
1130 }
1131 barrier(CLK_LOCAL_MEM_FENCE);
1132
1133 const float s = score_shared;
1134 const float m_new = fmax(m_i, s);
1135 const float alpha = native_exp(m_i - m_new);
1136 const float beta = native_exp(s - m_new);
1137
1138 o_val = o_val * alpha + beta * convert_float(v_tile[j * DV + tid]);
1139 l_i = l_i * alpha + beta;
1140 m_i = m_new;
1141 }
1142 barrier(CLK_LOCAL_MEM_FENCE);
1143 }
1144
1145 // Fold attention sinks into the running (m, l, o), if present.
1146 if (sinks_void != NULL) {
1147 const global float * sinks_ptr =
1148 (const global float *) ((const global char *) sinks_void + sinks_offset);
1149 const float m_sink = sinks_ptr[head_idx];
1150 const float m_new = fmax(m_i, m_sink);
1151 const float alpha = native_exp(m_i - m_new);
1152 const float beta = native_exp(m_sink - m_new);
1153 o_val = o_val * alpha;
1154 l_i = l_i * alpha + beta;
1155 m_i = m_new;
1156 }
1157
1158 const float l_inv = (l_i > 0.0f) ? (1.0f / l_i) : 0.0f;
1159 global float * o_row = (global float *) ((global char *) o_void + o_offset +
1160 batch_idx * o_nb3 + head_idx * o_nb1 +
1161 (ulong) q_idx * o_nb2);
1162 o_row[tid] = o_val * l_inv;
1163}
1164
1165// flash_attn_f32_f16_q1_local_mq_split
1166
1167#define LMQ_WG 64
1168#define LMQ_KC 32
1169#define LMQ_DPL 2 // DK / LMQ_WG at DK=128
1170
1171#endif // !FA_MQ_ONLY (q1_local_tile)
1172#ifndef MQ_GQA
1173#define MQ_GQA 4
1174#endif
1175
1176#ifndef FA_PARTIAL_FLOATS
1177#define FA_PARTIAL_FLOATS (2 + DV)
1178#endif
1179
1180#ifndef FA_MQ_ONLY // q1_local_mq_split excluded from the MQ-only (g8) program
1181REQD_SUBGROUP_SIZE_64
1182__kernel void flash_attn_f32_f16_q1_local_mq_split(
1183 const global void * q_void, ulong q_offset,
1184 const global void * k_void, ulong k_offset,
1185 const global void * v_void, ulong v_offset,
1186 const float scale,
1187 const int n_q,
1188 const int n_kv,
1189 const int n_head,
1190 const ulong q_nb1, const ulong q_nb2, const ulong q_nb3,
1191 const ulong k_nb1, const ulong k_nb2, const ulong k_nb3,
1192 const ulong v_nb1, const ulong v_nb2, const ulong v_nb3,
1193 const float max_bias,
1194 const float m0,
1195 const float m1,
1196 const int n_head_log2,
1197 const float logit_softcap,
1198 const int n_head_kv,
1199 const global void * mask_void,
1200 const ulong mask_offset,
1201 const ulong mask_nb1,
1202 const ulong mask_nb2,
1203 const ulong mask_nb3,
1204 const int mask_ne2,
1205 const int mask_ne3,
1206 global float * partial_void,
1207 const int n_splits,
1208 const int kv_per_split
1209) {
1210 const int tid = get_local_id(0); // 0..LMQ_WG-1
1211 const int kvhead_batch_idx = get_global_id(1);
1212 const int split_q_idx = get_global_id(2);
1213 const int split_idx = split_q_idx % n_splits;
1214 const int q_idx = split_q_idx / n_splits;
1215
1216 const int batch_idx = kvhead_batch_idx / n_head_kv;
1217 const int head_kv_idx = kvhead_batch_idx % n_head_kv;
1218
1219 const int kv_start = split_idx * kv_per_split;
1220 const int kv_end = min(kv_start + kv_per_split, n_kv);
1221
1222 const ulong record_stride = (ulong) FA_PARTIAL_FLOATS;
1223
1224 if (kv_start >= kv_end) {
1225 // Empty split — write sentinel for each Q-head so merge treats it as 0.
1226 if (tid == 0) {
1227 #pragma unroll
1228 for (int h = 0; h < MQ_GQA; ++h) {
1229 const int head_idx = head_kv_idx * MQ_GQA + h;
1230 const ulong rec_idx = ((((ulong) batch_idx * n_head + head_idx) * n_q + q_idx)
1231 * n_splits + split_idx);
1232 global float * rec = partial_void + rec_idx * record_stride;
1233 rec[0] = FA_M_INIT;
1234 rec[1] = 0.0f;
1235 }
1236 }
1237 return;
1238 }
1239
1240 const global char * q_base = (const global char *) q_void + q_offset;
1241 const global char * k_base = (const global char *) k_void + k_offset;
1242 const global char * v_base = (const global char *) v_void + v_offset;
1243
1244 // Stage MQ_GQA Q rows in __local (MQ_GQA × DK floats).
1245 __local float q_shared[MQ_GQA * DK];
1246 for (int i = tid; i < MQ_GQA * DK; i += LMQ_WG) {
1247 const int h = i / DK;
1248 const int d = i % DK;
1249 const int head_idx = head_kv_idx * MQ_GQA + h;
1250 const ulong q_row_off = batch_idx * q_nb3 + head_idx * q_nb2 + (ulong) q_idx * q_nb1;
1251 const global float * q_ptr = (const global float *) (q_base + q_row_off);
1252 q_shared[h * DK + d] = q_ptr[d];
1253 }
1254
1255 // K/V tile staging buffers (16 KB combined at DK=DV=128 KC=32).
1256 __local half k_tile[LMQ_KC * DK];
1257 __local half v_tile[LMQ_KC * DV];
1258
1259 // Per-h state held in private registers.
1260 float o_acc[MQ_GQA][LMQ_DPL];
1261 float m_i[MQ_GQA];
1262 float l_i[MQ_GQA];
1263 float slope[MQ_GQA];
1264 #pragma unroll
1265 for (int h = 0; h < MQ_GQA; ++h) {
1266 m_i[h] = FA_M_INIT;
1267 l_i[h] = 0.0f;
1268 slope[h] = get_alibi_slope(max_bias, head_kv_idx * MQ_GQA + h, n_head_log2, m0, m1);
1269 #pragma unroll
1270 for (int p = 0; p < LMQ_DPL; ++p) o_acc[h][p] = 0.0f;
1271 }
1272
1273 // Per-h mask pointers.
1274 const global char * mask_base[MQ_GQA];
1275 if (mask_void != NULL) {
1276 const int mask_batch_idx = batch_idx % mask_ne3;
1277 const global char * mask_base_b = (const global char *) mask_void + mask_offset +
1278 mask_batch_idx * mask_nb3 +
1279 (ulong) q_idx * mask_nb1;
1280 #pragma unroll
1281 for (int h = 0; h < MQ_GQA; ++h) {
1282 const int head_idx = head_kv_idx * MQ_GQA + h;
1283 const int mask_head_idx = head_idx % mask_ne2;
1284 mask_base[h] = mask_base_b + mask_head_idx * mask_nb2;
1285 }
1286 } else {
1287 #pragma unroll
1288 for (int h = 0; h < MQ_GQA; ++h) mask_base[h] = NULL;
1289 }
1290
1291 barrier(CLK_LOCAL_MEM_FENCE); // Ensure Q staged before first dot.
1292
1293 for (int kb = kv_start; kb < kv_end; kb += LMQ_KC) {
1294 const int tile_len = min((int) LMQ_KC, kv_end - kb);
1295
1296 // Cooperative load K + V tile.
1297 for (int i = tid; i < tile_len * DK; i += LMQ_WG) {
1298 const int j = i / DK;
1299 const int d = i % DK;
1300 const int kv_idx = kb + j;
1301 const global char * k_row = k_base + batch_idx * k_nb3 + head_kv_idx * k_nb2 + (ulong) kv_idx * k_nb1;
1302 const global char * v_row = v_base + batch_idx * v_nb3 + head_kv_idx * v_nb2 + (ulong) kv_idx * v_nb1;
1303 k_tile[j * DK + d] = ((const global half *) k_row)[d];
1304 v_tile[j * DV + d] = ((const global half *) v_row)[d];
1305 }
1306 barrier(CLK_LOCAL_MEM_FENCE);
1307
1308 // Process each cache row in the tile.
1309 for (int j = 0; j < tile_len; ++j) {
1310 const int kv_idx = kb + j;
1311
1312 // Dot product per h: lane owns LMQ_DPL D-elements at (tid*LMQ_DPL..).
1313 float score[MQ_GQA];
1314 #pragma unroll
1315 for (int h = 0; h < MQ_GQA; ++h) {
1316 float contrib = 0.0f;
1317 #pragma unroll
1318 for (int p = 0; p < LMQ_DPL; ++p) {
1319 const int d = tid * LMQ_DPL + p;
1320 contrib += q_shared[h * DK + d] * (float) k_tile[j * DK + d];
1321 }
1322 float s = sub_group_reduce_add(contrib) * scale;
1323 if (logit_softcap > 0.0f) {
1324 s = logit_softcap * tanh(s / logit_softcap);
1325 }
1326 if (mask_base[h] != NULL) {
1327 const global MASK_DATA_TYPE * mask_ptr = (const global MASK_DATA_TYPE *) mask_base[h];
1328 s += slope[h] * (float) mask_ptr[kv_idx];
1329 }
1330 score[h] = s;
1331 }
1332
1333 // Online softmax update + V accumulation per h.
1334 float p_h[MQ_GQA];
1335 float sp_h[MQ_GQA];
1336 #pragma unroll
1337 for (int h = 0; h < MQ_GQA; ++h) {
1338 const float m_new = fmax(m_i[h], score[h]);
1339 sp_h[h] = native_exp(m_i[h] - m_new);
1340 p_h[h] = native_exp(score[h] - m_new);
1341 l_i[h] = l_i[h] * sp_h[h] + p_h[h];
1342 m_i[h] = m_new;
1343 }
1344
1345 #pragma unroll
1346 for (int p = 0; p < LMQ_DPL; ++p) {
1347 const int d = tid * LMQ_DPL + p;
1348 const float v_val = (float) v_tile[j * DV + d];
1349 #pragma unroll
1350 for (int h = 0; h < MQ_GQA; ++h) {
1351 o_acc[h][p] = o_acc[h][p] * sp_h[h] + p_h[h] * v_val;
1352 }
1353 }
1354 }
1355 barrier(CLK_LOCAL_MEM_FENCE); // Before next tile load overwrites k/v_tile.
1356 }
1357
1358 // write partial records: one per (h, split)
1359 #pragma unroll
1360 for (int h = 0; h < MQ_GQA; ++h) {
1361 const int head_idx = head_kv_idx * MQ_GQA + h;
1362 const ulong rec_idx = ((((ulong) batch_idx * n_head + head_idx) * n_q + q_idx)
1363 * n_splits + split_idx);
1364 global float * rec = partial_void + rec_idx * record_stride;
1365 global float * rec_o = rec + 2;
1366
1367 if (tid == 0) {
1368 rec[0] = m_i[h];
1369 rec[1] = l_i[h];
1370 }
1371 #pragma unroll
1372 for (int p = 0; p < LMQ_DPL; ++p) {
1373 const int d = tid * LMQ_DPL + p;
1374 rec_o[d] = o_acc[h][p];
1375 }
1376 }
1377}
1378
1379#endif // !FA_MQ_ONLY (q1_local_mq_split)
1380#ifndef MQ_NSG
1381#define MQ_NSG 4
1382#endif
1383#define MQ_WG_SIZE (Q1_WG_SIZE * MQ_NSG)
1384
1385#ifndef FA_MQ_SPLIT_ONLY
1386REQD_SUBGROUP_SIZE_64
1387__kernel void flash_attn_f32_f16_q1_vec_mq(
1388 const global void * q_void, ulong q_offset,
1389 const global void * k_void, ulong k_offset,
1390 const global void * v_void, ulong v_offset,
1391 global void * o_void, ulong o_offset,
1392 const float scale,
1393 const int n_q,
1394 const int n_kv,
1395 const int is_causal,
1396 const int n_head,
1397 const ulong q_nb1, const ulong q_nb2, const ulong q_nb3,
1398 const ulong k_nb1, const ulong k_nb2, const ulong k_nb3,
1399 const ulong v_nb1, const ulong v_nb2, const ulong v_nb3,
1400 const ulong o_nb1, const ulong o_nb2, const ulong o_nb3,
1401 const float max_bias,
1402 const float m0,
1403 const float m1,
1404 const int n_head_log2,
1405 const float logit_softcap,
1406 const int n_head_kv,
1407 const global void* mask_void,
1408 const ulong mask_offset,
1409 const ulong mask_nb1,
1410 const ulong mask_nb2,
1411 const ulong mask_nb3,
1412 const int mask_ne2,
1413 const int mask_ne3,
1414 const global void* sinks_void,
1415 const ulong sinks_offset
1416) {
1417 const int tid = get_local_id(0);
1418 const int sgid = tid / Q1_WG_SIZE; // subgroup 0..MQ_NSG-1
1419 const int tid_sg = tid % Q1_WG_SIZE; // lane 0..63
1420 const int kvhead_batch_idx = get_global_id(1);
1421
1422 const int batch_idx = kvhead_batch_idx / n_head_kv;
1423 const int head_kv_idx = kvhead_batch_idx % n_head_kv;
1424
1425 const global char * q_base = (const global char *) q_void + q_offset;
1426 const global char * k_base = (const global char *) k_void + k_offset;
1427 const global char * v_base = (const global char *) v_void + v_offset;
1428 global char * o_base = (global char *) o_void + o_offset;
1429
1430 __local ACC_TYPE4 q_shared[MQ_GQA * DK_VEC];
1431 for (int i = tid; i < MQ_GQA * DK_VEC; i += MQ_WG_SIZE) {
1432 const int h = i / DK_VEC;
1433 const int k = i % DK_VEC;
1434 const int head_idx = head_kv_idx * MQ_GQA + h;
1435 const ulong q_row_offset = batch_idx * q_nb3 + head_idx * q_nb2;
1436 const global Q_DATA_TYPE4 * q_ptr = (const global Q_DATA_TYPE4 *) (q_base + q_row_offset);
1437 q_shared[h * DK_VEC + k] = CONVERT_Q_ACC4(q_ptr[k]);
1438 }
1439 barrier(CLK_LOCAL_MEM_FENCE);
1440
1441 // per-h ALiBi slope
1442 float slope[MQ_GQA];
1443 #pragma unroll
1444 for (int h = 0; h < MQ_GQA; ++h) {
1445 slope[h] = get_alibi_slope(max_bias, head_kv_idx * MQ_GQA + h, n_head_log2, m0, m1);
1446 }
1447
1448 // per-h mask row pointer
1449 const global char * mask_base[MQ_GQA];
1450 if (mask_void != NULL) {
1451 const int mask_batch_idx = batch_idx % mask_ne3;
1452 const global char * mask_base_b = (const global char *) mask_void + mask_offset +
1453 mask_batch_idx * mask_nb3;
1454 #pragma unroll
1455 for (int h = 0; h < MQ_GQA; ++h) {
1456 const int head_idx = head_kv_idx * MQ_GQA + h;
1457 const int mask_head_idx = head_idx % mask_ne2;
1458 mask_base[h] = mask_base_b + mask_head_idx * mask_nb2;
1459 }
1460 } else {
1461 #pragma unroll
1462 for (int h = 0; h < MQ_GQA; ++h) mask_base[h] = NULL;
1463 }
1464
1465 const global ACC_TYPE * sinks_ptr = NULL;
1466 if (sinks_void != NULL) {
1467 sinks_ptr = (const global ACC_TYPE *) ((const global char *) sinks_void + sinks_offset);
1468 }
1469
1470 // per-thread per-h DV slice.
1471 ACC_TYPE4 o_acc[MQ_GQA][Q1V_DV_PER_THREAD];
1472 ACC_TYPE m_i[MQ_GQA];
1473 ACC_TYPE l_i[MQ_GQA];
1474 #pragma unroll
1475 for (int h = 0; h < MQ_GQA; ++h) {
1476 m_i[h] = FA_M_INIT;
1477 l_i[h] = 0.0f;
1478 #pragma unroll
1479 for (int i = 0; i < Q1V_DV_PER_THREAD; ++i) o_acc[h][i] = (ACC_TYPE4)(0.0f);
1480 }
1481
1482 // each subgroup independently sweeps its slice of n_kv.
1483 const int kv_per_sg = (n_kv + MQ_NSG - 1) / MQ_NSG;
1484 const int kv_start = sgid * kv_per_sg;
1485 const int kv_end = min(n_kv, kv_start + kv_per_sg);
1486
1487 for (int k_idx = kv_start; k_idx < kv_end; ++k_idx) {
1488 const ulong k_row_off = batch_idx * k_nb3 + head_kv_idx * k_nb2 + k_idx * k_nb1;
1489 const ulong v_row_off = batch_idx * v_nb3 + head_kv_idx * v_nb2 + k_idx * v_nb1;
1490 const global KV_DATA_TYPE4 * k_ptr = (const global KV_DATA_TYPE4 *) (k_base + k_row_off);
1491 const global KV_DATA_TYPE4 * v_ptr = (const global KV_DATA_TYPE4 *) (v_base + v_row_off);
1492
1493 // Q*K^T: load each K stride once, dot against all MQ_GQA Q rows.
1494 ACC_TYPE4 dot4[MQ_GQA];
1495 #pragma unroll
1496 for (int h = 0; h < MQ_GQA; ++h) dot4[h] = (ACC_TYPE4)(0.0f);
1497 for (int k = tid_sg; k < DK_VEC; k += Q1_WG_SIZE) {
1498 const ACC_TYPE4 k_vec = CONVERT_KV_ACC4(k_ptr[k]);
1499 #pragma unroll
1500 for (int h = 0; h < MQ_GQA; ++h) {
1501 dot4[h] = mad(q_shared[h * DK_VEC + k], k_vec, dot4[h]);
1502 }
1503 }
1504
1505 ACC_TYPE score[MQ_GQA];
1506 #pragma unroll
1507 for (int h = 0; h < MQ_GQA; ++h) {
1508 const ACC_TYPE dot_partial = dot4[h].s0 + dot4[h].s1 + dot4[h].s2 + dot4[h].s3;
1509 ACC_TYPE s = sub_group_reduce_add(dot_partial) * scale;
1510 if (logit_softcap > 0.0f) {
1511 s = logit_softcap * tanh(s / logit_softcap);
1512 }
1513 if (mask_base[h] != NULL) {
1514 const global MASK_DATA_TYPE * mask_ptr = (const global MASK_DATA_TYPE *) mask_base[h];
1515 s += slope[h] * (ACC_TYPE) mask_ptr[k_idx];
1516 }
1517 score[h] = s;
1518 }
1519
1520 // FA-2 online softmax update — V load amortized across MQ_GQA heads.
1521 // p, scale_prev are computed per h; the V vector is loaded once
1522 // per dv stride and reused MQ_GQA times.
1523 ACC_TYPE p_h[MQ_GQA];
1524 ACC_TYPE sp_h[MQ_GQA];
1525 #pragma unroll
1526 for (int h = 0; h < MQ_GQA; ++h) {
1527 const ACC_TYPE m_new = max(m_i[h], score[h]);
1528 sp_h[h] = native_exp(m_i[h] - m_new);
1529 p_h[h] = native_exp(score[h] - m_new);
1530 l_i[h] = l_i[h] * sp_h[h] + p_h[h];
1531 m_i[h] = m_new;
1532 }
1533
1534 int idx = 0;
1535 for (int dv_idx = tid_sg; dv_idx < DV_VEC; dv_idx += Q1_WG_SIZE, ++idx) {
1536 const ACC_TYPE4 v_vec = CONVERT_KV_ACC4(v_ptr[dv_idx]);
1537 #pragma unroll
1538 for (int h = 0; h < MQ_GQA; ++h) {
1539 o_acc[h][idx] = mad(p_h[h], v_vec, o_acc[h][idx] * sp_h[h]);
1540 }
1541 }
1542 }
1543
1544 // cross subgroup merge
1545 __local ACC_TYPE sg_m[MQ_GQA][MQ_NSG];
1546 __local ACC_TYPE sg_l[MQ_GQA][MQ_NSG];
1547 __local ACC_TYPE4 sg_o[MQ_NSG][DV_VEC];
1548
1549 if (tid_sg == 0) {
1550 #pragma unroll
1551 for (int h = 0; h < MQ_GQA; ++h) {
1552 sg_m[h][sgid] = m_i[h];
1553 sg_l[h][sgid] = l_i[h];
1554 }
1555 }
1556
1557 #pragma unroll
1558 for (int h = 0; h < MQ_GQA; ++h) {
1559 // each subgroup publishes its o_acc slice for head h.
1560 {
1561 int idx = 0;
1562 for (int dv_idx = tid_sg; dv_idx < DV_VEC; dv_idx += Q1_WG_SIZE, ++idx) {
1563 sg_o[sgid][dv_idx] = o_acc[h][idx];
1564 }
1565 }
1566 barrier(CLK_LOCAL_MEM_FENCE);
1567
1568 if (sgid == 0) {
1569 const int head_idx = head_kv_idx * MQ_GQA + h;
1570
1571 ACC_TYPE m_final = sg_m[h][0];
1572 #pragma unroll
1573 for (int s = 1; s < MQ_NSG; ++s) {
1574 m_final = max(m_final, sg_m[h][s]);
1575 }
1576 if (sinks_ptr != NULL) {
1577 m_final = max(m_final, sinks_ptr[head_idx]);
1578 }
1579
1580 ACC_TYPE l_final = 0.0f;
1581 #pragma unroll
1582 for (int s = 0; s < MQ_NSG; ++s) {
1583 l_final += sg_l[h][s] * native_exp(sg_m[h][s] - m_final);
1584 }
1585 if (sinks_ptr != NULL) {
1586 l_final += native_exp(sinks_ptr[head_idx] - m_final);
1587 }
1588 const ACC_TYPE l_inv = (l_final > 0.0f) ? (1.0f / l_final) : 0.0f;
1589
1590 const ulong o_row_offset = batch_idx * o_nb3 + head_idx * o_nb1;
1591 global O_DATA_TYPE4 * o_row = (global O_DATA_TYPE4 *) (o_base + o_row_offset);
1592 for (int dv_idx = tid_sg; dv_idx < DV_VEC; dv_idx += Q1_WG_SIZE) {
1593 ACC_TYPE4 o_merged = (ACC_TYPE4)(0.0f);
1594 #pragma unroll
1595 for (int s = 0; s < MQ_NSG; ++s) {
1596 const ACC_TYPE alpha = native_exp(sg_m[h][s] - m_final);
1597 o_merged = mad((ACC_TYPE4)(alpha), sg_o[s][dv_idx], o_merged);
1598 }
1599 o_row[dv_idx] = CONVERT_O_DATA4(o_merged * l_inv);
1600 }
1601 }
1602 // Barrier guards next h's overwrite of sg_o.
1603 barrier(CLK_LOCAL_MEM_FENCE);
1604 }
1605}
1606
1607#endif // !FA_MQ_SPLIT_ONLY
1608
1609#ifndef MQ_NSG_SPLIT
1610#define MQ_NSG_SPLIT 4
1611#endif
1612#define MQ_SPLIT_WG_SIZE (Q1_WG_SIZE * MQ_NSG_SPLIT)
1613
1614#ifndef FA_PARTIAL_FLOATS
1615#define FA_PARTIAL_FLOATS (2 + DV)
1616#endif
1617
1618#ifndef FA_HEAD_SUB
1619#define FA_HEAD_SUB 1
1620#endif
1621
1622REQD_SUBGROUP_SIZE_64
1623__kernel void flash_attn_f32_f16_q1_vec_mq_split(
1624 const global void * q_void, ulong q_offset,
1625 const global void * k_void, ulong k_offset,
1626 const global void * v_void, ulong v_offset,
1627 const float scale,
1628 const int n_q,
1629 const int n_kv,
1630 const int n_head,
1631 const ulong q_nb1, const ulong q_nb2, const ulong q_nb3,
1632 const ulong k_nb1, const ulong k_nb2, const ulong k_nb3,
1633 const ulong v_nb1, const ulong v_nb2, const ulong v_nb3,
1634 const float max_bias,
1635 const float m0,
1636 const float m1,
1637 const int n_head_log2,
1638 const float logit_softcap,
1639 const int n_head_kv,
1640 const global void * mask_void,
1641 const ulong mask_offset,
1642 const ulong mask_nb1,
1643 const ulong mask_nb2,
1644 const ulong mask_nb3,
1645 const int mask_ne2,
1646 const int mask_ne3,
1647 global float * partial_void,
1648 const int n_splits,
1649 const int kv_per_split
1650) {
1651 const int tid = get_local_id(0);
1652 const int sgid = tid / Q1_WG_SIZE;
1653 const int tid_sg = tid % Q1_WG_SIZE;
1654 const int kvhead_batch_idx = get_global_id(1);
1655 const int split_q_idx = get_global_id(2);
1656 const int split_idx = split_q_idx % n_splits;
1657 const int q_idx = split_q_idx / n_splits;
1658
1659 const int hgroups = n_head_kv * FA_HEAD_SUB;
1660 const int batch_idx = kvhead_batch_idx / hgroups;
1661 const int hg = kvhead_batch_idx % hgroups;
1662 const int head_kv_idx = hg / FA_HEAD_SUB;
1663 const int head_sub = hg % FA_HEAD_SUB;
1664#define FA_MQS_HEAD_IDX(h) (head_kv_idx * (MQ_GQA * FA_HEAD_SUB) + head_sub * MQ_GQA + (h))
1665
1666 const int kv_start = split_idx * kv_per_split;
1667 const int kv_end = min(kv_start + kv_per_split, n_kv);
1668
1669 const ulong record_stride = (ulong) FA_PARTIAL_FLOATS;
1670
1671 if (kv_start >= kv_end) {
1672 // write sentinel for each of the MQ_GQA Q-heads so the
1673 // merge pass treats this slot as dropped
1674 if (tid == 0) {
1675 #pragma unroll
1676 for (int h = 0; h < MQ_GQA; ++h) {
1677 const int head_idx = FA_MQS_HEAD_IDX(h);
1678 const ulong rec_idx = ((((ulong) batch_idx * n_head + head_idx) * n_q + q_idx)
1679 * n_splits + split_idx);
1680 global float * rec = partial_void + rec_idx * record_stride;
1681 rec[0] = FA_M_INIT;
1682 rec[1] = 0.0f;
1683 }
1684 }
1685 return;
1686 }
1687
1688 const global char * q_base = (const global char *) q_void + q_offset;
1689 const global char * k_base = (const global char *) k_void + k_offset;
1690 const global char * v_base = (const global char *) v_void + v_offset;
1691
1692#ifdef FA_Q1_Q_REG
1693 ACC_TYPE4 q_reg[MQ_GQA];
1694 #pragma unroll
1695 for (int h = 0; h < MQ_GQA; ++h) {
1696 const int head_idx = FA_MQS_HEAD_IDX(h);
1697 const ulong q_row_offset = batch_idx * q_nb3 + head_idx * q_nb2 + (ulong) q_idx * q_nb1;
1698 const global Q_DATA_TYPE4 * q_ptr = (const global Q_DATA_TYPE4 *) (q_base + q_row_offset);
1699 q_reg[h] = (tid_sg < DK_VEC) ? CONVERT_Q_ACC4(q_ptr[tid_sg]) : (ACC_TYPE4)(0.0f);
1700 }
1701#else
1702 // stage MQ_GQA Q rows in __local once (uniform across WG)
1703 __local ACC_TYPE4 q_shared[MQ_GQA * DK_VEC];
1704 for (int i = tid; i < MQ_GQA * DK_VEC; i += MQ_SPLIT_WG_SIZE) {
1705 const int h = i / DK_VEC;
1706 const int k = i % DK_VEC;
1707 const int head_idx = FA_MQS_HEAD_IDX(h);
1708 const ulong q_row_offset = batch_idx * q_nb3 + head_idx * q_nb2 + (ulong) q_idx * q_nb1;
1709 const global Q_DATA_TYPE4 * q_ptr = (const global Q_DATA_TYPE4 *) (q_base + q_row_offset);
1710 q_shared[h * DK_VEC + k] = CONVERT_Q_ACC4(q_ptr[k]);
1711 }
1712 barrier(CLK_LOCAL_MEM_FENCE);
1713#endif
1714
1715 float slope[MQ_GQA];
1716 #pragma unroll
1717 for (int h = 0; h < MQ_GQA; ++h) {
1718 slope[h] = get_alibi_slope(max_bias, FA_MQS_HEAD_IDX(h), n_head_log2, m0, m1);
1719 }
1720
1721 const global char * mask_base[MQ_GQA];
1722 if (mask_void != NULL) {
1723 const int mask_batch_idx = batch_idx % mask_ne3;
1724 const global char * mask_base_b = (const global char *) mask_void + mask_offset +
1725 mask_batch_idx * mask_nb3 +
1726 (ulong) q_idx * mask_nb1;
1727 #pragma unroll
1728 for (int h = 0; h < MQ_GQA; ++h) {
1729 const int head_idx = FA_MQS_HEAD_IDX(h);
1730 const int mask_head_idx = head_idx % mask_ne2;
1731 mask_base[h] = mask_base_b + mask_head_idx * mask_nb2;
1732 }
1733 } else {
1734 #pragma unroll
1735 for (int h = 0; h < MQ_GQA; ++h) mask_base[h] = NULL;
1736 }
1737
1738 ACC_TYPE4 o_acc[MQ_GQA][Q1V_DV_PER_THREAD];
1739 ACC_TYPE m_i[MQ_GQA];
1740 ACC_TYPE l_i[MQ_GQA];
1741 #pragma unroll
1742 for (int h = 0; h < MQ_GQA; ++h) {
1743 m_i[h] = FA_M_INIT;
1744 l_i[h] = 0.0f;
1745 #pragma unroll
1746 for (int i = 0; i < Q1V_DV_PER_THREAD; ++i) o_acc[h][i] = (ACC_TYPE4)(0.0f);
1747 }
1748
1749 // each subgroup independently sweeps its slice of the split's kv range.
1750 const int kv_len = kv_end - kv_start;
1751 const int kv_per_sg = (kv_len + MQ_NSG_SPLIT - 1) / MQ_NSG_SPLIT;
1752 const int kv_lo = kv_start + sgid * kv_per_sg;
1753 const int kv_hi = min(kv_end, kv_lo + kv_per_sg);
1754
1755 for (int k_idx = kv_lo; k_idx < kv_hi; ++k_idx) {
1756 const ulong k_row_off = batch_idx * k_nb3 + head_kv_idx * k_nb2 + k_idx * k_nb1;
1757 const ulong v_row_off = batch_idx * v_nb3 + head_kv_idx * v_nb2 + k_idx * v_nb1;
1758 const global KV_DATA_TYPE4 * k_ptr = (const global KV_DATA_TYPE4 *) (k_base + k_row_off);
1759 const global KV_DATA_TYPE4 * v_ptr = (const global KV_DATA_TYPE4 *) (v_base + v_row_off);
1760
1761 ACC_TYPE4 dot4[MQ_GQA];
1762 #pragma unroll
1763 for (int h = 0; h < MQ_GQA; ++h) dot4[h] = (ACC_TYPE4)(0.0f);
1764#ifdef FA_Q1_Q_REG
1765 if (tid_sg < DK_VEC) {
1766 const ACC_TYPE4 k_vec = CONVERT_KV_ACC4(k_ptr[tid_sg]);
1767 #pragma unroll
1768 for (int h = 0; h < MQ_GQA; ++h) {
1769 dot4[h] = mad(q_reg[h], k_vec, dot4[h]);
1770 }
1771 }
1772#else
1773 for (int k = tid_sg; k < DK_VEC; k += Q1_WG_SIZE) {
1774 const ACC_TYPE4 k_vec = CONVERT_KV_ACC4(k_ptr[k]);
1775 #pragma unroll
1776 for (int h = 0; h < MQ_GQA; ++h) {
1777 dot4[h] = mad(q_shared[h * DK_VEC + k], k_vec, dot4[h]);
1778 }
1779 }
1780#endif
1781
1782 ACC_TYPE score[MQ_GQA];
1783 #pragma unroll
1784 for (int h = 0; h < MQ_GQA; ++h) {
1785 const ACC_TYPE dot_partial = dot4[h].s0 + dot4[h].s1 + dot4[h].s2 + dot4[h].s3;
1786 ACC_TYPE s = sub_group_reduce_add(dot_partial) * scale;
1787 if (logit_softcap > 0.0f) {
1788 s = logit_softcap * tanh(s / logit_softcap);
1789 }
1790 if (mask_base[h] != NULL) {
1791 const global MASK_DATA_TYPE * mask_ptr = (const global MASK_DATA_TYPE *) mask_base[h];
1792 s += slope[h] * (ACC_TYPE) mask_ptr[k_idx];
1793 }
1794 score[h] = s;
1795 }
1796
1797 ACC_TYPE p_h[MQ_GQA];
1798 ACC_TYPE sp_h[MQ_GQA];
1799 #pragma unroll
1800 for (int h = 0; h < MQ_GQA; ++h) {
1801 const ACC_TYPE m_new = max(m_i[h], score[h]);
1802 sp_h[h] = native_exp(m_i[h] - m_new);
1803 p_h[h] = native_exp(score[h] - m_new);
1804 l_i[h] = l_i[h] * sp_h[h] + p_h[h];
1805 m_i[h] = m_new;
1806 }
1807
1808 int idx = 0;
1809 for (int dv_idx = tid_sg; dv_idx < DV_VEC; dv_idx += Q1_WG_SIZE, ++idx) {
1810 const ACC_TYPE4 v_vec = CONVERT_KV_ACC4(v_ptr[dv_idx]);
1811 #pragma unroll
1812 for (int h = 0; h < MQ_GQA; ++h) {
1813 o_acc[h][idx] = mad(p_h[h], v_vec, o_acc[h][idx] * sp_h[h]);
1814 }
1815 }
1816 }
1817
1818 // per-h cross-subgroup merge
1819 __local ACC_TYPE sg_m[MQ_GQA][MQ_NSG_SPLIT];
1820 __local ACC_TYPE sg_l[MQ_GQA][MQ_NSG_SPLIT];
1821 __local ACC_TYPE4 sg_o[MQ_NSG_SPLIT][DV_VEC];
1822
1823 if (tid_sg == 0) {
1824 #pragma unroll
1825 for (int h = 0; h < MQ_GQA; ++h) {
1826 sg_m[h][sgid] = m_i[h];
1827 sg_l[h][sgid] = l_i[h];
1828 }
1829 }
1830
1831 #pragma unroll
1832 for (int h = 0; h < MQ_GQA; ++h) {
1833 {
1834 int idx = 0;
1835 for (int dv_idx = tid_sg; dv_idx < DV_VEC; dv_idx += Q1_WG_SIZE, ++idx) {
1836 sg_o[sgid][dv_idx] = o_acc[h][idx];
1837 }
1838 }
1839 barrier(CLK_LOCAL_MEM_FENCE);
1840
1841 if (sgid == 0) {
1842 const int head_idx = FA_MQS_HEAD_IDX(h);
1843
1844 // fold per-subgroup (m, l) into split-level (m_c, l_c)
1845 ACC_TYPE m_c = sg_m[h][0];
1846 #pragma unroll
1847 for (int s = 1; s < MQ_NSG_SPLIT; ++s) {
1848 m_c = max(m_c, sg_m[h][s]);
1849 }
1850 ACC_TYPE l_c = 0.0f;
1851 #pragma unroll
1852 for (int s = 0; s < MQ_NSG_SPLIT; ++s) {
1853 l_c += sg_l[h][s] * native_exp(sg_m[h][s] - m_c);
1854 }
1855
1856 const ulong rec_idx = ((((ulong) batch_idx * n_head + head_idx) * n_q + q_idx)
1857 * n_splits + split_idx);
1858 global float * rec = partial_void + rec_idx * record_stride;
1859 global float4 * rec_o = (global float4 *) (rec + 2);
1860
1861 if (tid_sg == 0) {
1862 rec[0] = (float) m_c;
1863 rec[1] = (float) l_c;
1864 }
1865 // each thread writes its DV slice of the merged O.
1866 for (int dv_idx = tid_sg; dv_idx < DV_VEC; dv_idx += Q1_WG_SIZE) {
1867 ACC_TYPE4 o_merged = (ACC_TYPE4)(0.0f);
1868 #pragma unroll
1869 for (int s = 0; s < MQ_NSG_SPLIT; ++s) {
1870 const ACC_TYPE alpha = native_exp(sg_m[h][s] - m_c);
1871 o_merged = mad((ACC_TYPE4)(alpha), sg_o[s][dv_idx], o_merged);
1872 }
1873 rec_o[dv_idx] = o_merged;
1874 }
1875 }
1876 barrier(CLK_LOCAL_MEM_FENCE);
1877 }
1878}
1879
1880#undef FA_MQS_HEAD_IDX
1881
1882#ifndef FA_MQ_SPLIT_ONLY
1883// Cluster-parallel variant of _q1_vec_mq_split
1884//
1885// Tthe baseline keeps one 256B K row in flight per subgroup (32 lanes cooperate
1886// on one position, serialized by the reduce+exp chain). This kernel
1887// takes q1_split's memory-level parallelism at MQ's read-once traffic:
1888// - the 64-lane subgroup is split into FA_CL_NCL clusters of FA_CL_C lanes;
1889// - each cluster owns its own KV position stream (positions strided by
1890// FA_CL_NCL) with private per-cluster online-softmax state, hence FA_CL_NCL
1891// independent K rows in flight per subgroup, no cross-cluster serial chain;
1892// - within a cluster, lanes split DK for the dot (cluster-reduce via
1893// sub_group_shuffle_xor, steps < FA_CL_C stay inside the cluster) and
1894// split DV for o_acc (each lane owns dv indices {lic + FA_CL_C*i} — the
1895// same slice for every position, so accumulation is lane-local);
1896// - merge stage 1 folds the FA_CL_NCL cluster partials with cross-cluster
1897// shuffles (distances >= FA_CL_C); stage 2 is the baseline cross-subgroup
1898// LDS merge (o published by cluster 0's lanes, layout-identical to the
1899// baseline's sg_o).
1900// The KV sweep runs a UNIFORM trip count (max over clusters) with a clamped
1901// row address + FA_M_INIT score on the tail — keeps every shuffle convergent
1902// (p = exp(FA_M_INIT - m) underflows to 0, so clamped-row reads are inert).
1903// Register cost vs baseline: o_acc grows from DV_VEC/64 to DV_VEC/FA_CL_C
1904// float4 per lane per head — FA_CL_C=8 / MQ_GQA=4 => 16 float4 (256B).
1905
1906#ifdef HAS_SUBGROUP_SHUFFLE // cluster reduce/merge needs shuffles; absent -> kernel dropped, dispatch falls back
1907
1908#ifndef FA_CL_C
1909#define FA_CL_C 8
1910#endif
1911
1912// The lane striping requires DK/DV to divide evenly across the cluster;
1913// otherwise (e.g. DK=40 with FA_CL_C=16 -> zero-size arrays) compile the
1914// kernel out — host soft-create falls back silently.
1915#if (DK_VEC % FA_CL_C) == 0 && (DV_VEC % FA_CL_C) == 0
1916#define FA_CL_NCL (Q1_WG_SIZE / FA_CL_C) // clusters (position streams) per subgroup
1917#define FA_CL_DK (DK_VEC / FA_CL_C) // half4s of K per lane per row
1918#define FA_CL_DV (DV_VEC / FA_CL_C) // float4s of o_acc per lane per head
1919
1920// explicit "half" sub-group attribute routes this fp16-heavy kernel to a slow
1921// codegen path on the X1 compiler. X2 keeps the pin: its driver miscompile
1922// without it.
1923#ifdef FA_C8_NO_SG_PIN
1924#define FA_C8_SG_ATTR
1925#else
1926// REQD_FA_SG pins the HW subgroup on Intel (intel_reqd_sub_group_size(FA_SG),
1927// host passes -D FA_SG=32); empty on Adreno. REQD_SUBGROUP_SIZE_64 pins 64 on
1928// Adreno; empty on Intel.
1929#define FA_C8_SG_ATTR REQD_FA_SG REQD_SUBGROUP_SIZE_64
1930#endif
1931
1932FA_C8_SG_ATTR
1933__kernel void flash_attn_f32_f16_q1_vec_mq_split_c8(
1934 const global void * q_void, ulong q_offset,
1935 const global void * k_void, ulong k_offset,
1936 const global void * v_void, ulong v_offset,
1937 const float scale,
1938 const int n_q,
1939 const int n_kv,
1940 const int n_head,
1941 const ulong q_nb1, const ulong q_nb2, const ulong q_nb3,
1942 const ulong k_nb1, const ulong k_nb2, const ulong k_nb3,
1943 const ulong v_nb1, const ulong v_nb2, const ulong v_nb3,
1944 const float max_bias,
1945 const float m0,
1946 const float m1,
1947 const int n_head_log2,
1948 const float logit_softcap,
1949 const int n_head_kv,
1950 const global void * mask_void,
1951 const ulong mask_offset,
1952 const ulong mask_nb1,
1953 const ulong mask_nb2,
1954 const ulong mask_nb3,
1955 const int mask_ne2,
1956 const int mask_ne3,
1957 global float * partial_void,
1958 const int n_splits,
1959 const int kv_per_split
1960) {
1961 const int tid = get_local_id(0);
1962 const int sgid = tid / Q1_WG_SIZE;
1963 const int tid_sg = tid % Q1_WG_SIZE;
1964 const int cl = tid_sg / FA_CL_C; // cluster id
1965 const int lic = tid_sg % FA_CL_C; // lane in cluster
1966 const int kvhead_batch_idx = get_global_id(1);
1967 const int split_q_idx = get_global_id(2);
1968 const int split_idx = split_q_idx % n_splits;
1969 const int q_idx = split_q_idx / n_splits;
1970
1971 const int hgroups = n_head_kv * FA_HEAD_SUB;
1972 const int batch_idx = kvhead_batch_idx / hgroups;
1973 const int hg = kvhead_batch_idx % hgroups;
1974 const int head_kv_idx = hg / FA_HEAD_SUB;
1975 const int head_sub = hg % FA_HEAD_SUB;
1976#define FA_HEAD_IDX(h) (head_kv_idx * (MQ_GQA * FA_HEAD_SUB) + head_sub * MQ_GQA + (h))
1977
1978 const int kv_start = split_idx * kv_per_split;
1979 const int kv_end = min(kv_start + kv_per_split, n_kv);
1980
1981 const ulong record_stride = (ulong) FA_PARTIAL_FLOATS;
1982
1983 if (kv_start >= kv_end) {
1984 if (tid == 0) {
1985 #pragma unroll
1986 for (int h = 0; h < MQ_GQA; ++h) {
1987 const int head_idx = FA_HEAD_IDX(h);
1988 const ulong rec_idx = ((((ulong) batch_idx * n_head + head_idx) * n_q + q_idx)
1989 * n_splits + split_idx);
1990 global float * rec = partial_void + rec_idx * record_stride;
1991 rec[0] = FA_M_INIT;
1992 rec[1] = 0.0f;
1993 }
1994 }
1995 return;
1996 }
1997
1998 const global char * q_base = (const global char *) q_void + q_offset;
1999 const global char * k_base = (const global char *) k_void + k_offset;
2000 const global char * v_base = (const global char *) v_void + v_offset;
2001
2002 // Stage MQ_GQA Q rows in __local once (uniform across WG).
2003 __local ACC_TYPE4 q_shared[MQ_GQA * DK_VEC];
2004 for (int i = tid; i < MQ_GQA * DK_VEC; i += MQ_SPLIT_WG_SIZE) {
2005 const int h = i / DK_VEC;
2006 const int k = i % DK_VEC;
2007 const int head_idx = FA_HEAD_IDX(h);
2008 const ulong q_row_offset = batch_idx * q_nb3 + head_idx * q_nb2 + (ulong) q_idx * q_nb1;
2009 const global Q_DATA_TYPE4 * q_ptr = (const global Q_DATA_TYPE4 *) (q_base + q_row_offset);
2010 q_shared[h * DK_VEC + k] = CONVERT_Q_ACC4(q_ptr[k]);
2011 }
2012 barrier(CLK_LOCAL_MEM_FENCE);
2013
2014 float slope[MQ_GQA];
2015 #pragma unroll
2016 for (int h = 0; h < MQ_GQA; ++h) {
2017 slope[h] = get_alibi_slope(max_bias, FA_HEAD_IDX(h), n_head_log2, m0, m1);
2018 }
2019
2020#ifdef FA_CL_MASK_BCAST
2021 const global char * mask_base_b = NULL;
2022 if (mask_void != NULL) {
2023 mask_base_b = (const global char *) mask_void + mask_offset +
2024 (batch_idx % mask_ne3) * mask_nb3 + (ulong) q_idx * mask_nb1;
2025 }
2026 const int mask_bcast = mask_base_b != NULL && mask_ne2 == 1;
2027#else
2028 const global char * mask_base[MQ_GQA];
2029 if (mask_void != NULL) {
2030 const int mask_batch_idx = batch_idx % mask_ne3;
2031 const global char * mask_base_b = (const global char *) mask_void + mask_offset +
2032 mask_batch_idx * mask_nb3 +
2033 (ulong) q_idx * mask_nb1;
2034 #pragma unroll
2035 for (int h = 0; h < MQ_GQA; ++h) {
2036 const int head_idx = FA_HEAD_IDX(h);
2037 const int mask_head_idx = head_idx % mask_ne2;
2038 mask_base[h] = mask_base_b + mask_head_idx * mask_nb2;
2039 }
2040 } else {
2041 #pragma unroll
2042 for (int h = 0; h < MQ_GQA; ++h) mask_base[h] = NULL;
2043 }
2044#endif
2045
2046 // Per-CLUSTER online-softmax state (uniform across the cluster's lanes);
2047 // o_acc holds this lane's DV slice {lic + FA_CL_C*i}.
2048 ACC_TYPE4 o_acc[MQ_GQA][FA_CL_DV];
2049 ACC_TYPE m_i[MQ_GQA];
2050 ACC_TYPE l_i[MQ_GQA];
2051 #pragma unroll
2052 for (int h = 0; h < MQ_GQA; ++h) {
2053 m_i[h] = FA_M_INIT;
2054 l_i[h] = 0.0f;
2055 #pragma unroll
2056 for (int i = 0; i < FA_CL_DV; ++i) o_acc[h][i] = (ACC_TYPE4)(0.0f);
2057 }
2058
2059 const int kv_len = kv_end - kv_start;
2060 const int kv_per_sg = (kv_len + MQ_NSG_SPLIT - 1) / MQ_NSG_SPLIT;
2061 const int kv_lo = kv_start + sgid * kv_per_sg;
2062 const int kv_hi = min(kv_end, kv_lo + kv_per_sg);
2063
2064 // Uniform trip count across the subgroup: every cluster runs n_iter
2065 // iterations; tail positions clamp the row address and drop the score to
2066 // FA_M_INIT so shuffles stay convergent and the contribution is exactly 0.
2067 const int n_iter = (kv_hi - kv_lo + FA_CL_NCL - 1) / FA_CL_NCL;
2068 const ulong kv_row_base = batch_idx * k_nb3 + head_kv_idx * k_nb2;
2069 const ulong v_row_base = batch_idx * v_nb3 + head_kv_idx * v_nb2;
2070
2071 for (int it = 0; it < n_iter; ++it) {
2072 const int k_idx = kv_lo + cl + it * FA_CL_NCL;
2073 const int valid = k_idx < kv_hi;
2074 const int k_safe = valid ? k_idx : (kv_hi - 1);
2075
2076 const global KV_DATA_TYPE4 * k_ptr = (const global KV_DATA_TYPE4 *) (k_base + kv_row_base + (ulong) k_safe * k_nb1);
2077 const global KV_DATA_TYPE4 * v_ptr = (const global KV_DATA_TYPE4 *) (v_base + v_row_base + (ulong) k_safe * v_nb1);
2078
2079#if defined(FA_CL_MHRED) && MQ_GQA == 4 && FA_CL_C == 16 && FA_CL_DK == 1 && FA_CL_DV == 1
2080 ACC_TYPE mask_val = 0.0f;
2081 if (mask_bcast) {
2082 mask_val = (ACC_TYPE) ((const global MASK_DATA_TYPE *) mask_base_b)[k_safe];
2083 }
2084 const ACC_TYPE4 k_vec_1 = CONVERT_KV_ACC4(k_ptr[lic]);
2085 const ACC_TYPE4 v_vec_1 = CONVERT_KV_ACC4(v_ptr[lic]);
2086
2087 // Reduce four heads with eight shuffles and keep each head's summation order.
2088 const int mh_b0 = lic & 1;
2089 const int mh_b1 = lic & 2;
2090
2091 ACC_TYPE mh_p[MQ_GQA];
2092 #pragma unroll
2093 for (int h = 0; h < MQ_GQA; ++h) {
2094 const ACC_TYPE4 d4 = mad(q_shared[h * DK_VEC + lic], k_vec_1, (ACC_TYPE4)(0.0f));
2095 mh_p[h] = d4.s0 + d4.s1 + d4.s2 + d4.s3;
2096 }
2097
2098 ACC_TYPE mh_r2[2];
2099 #pragma unroll
2100 for (int j = 0; j < 2; ++j) {
2101 const ACC_TYPE keep = mh_b0 ? mh_p[j + 2] : mh_p[j];
2102 const ACC_TYPE send = mh_b0 ? mh_p[j] : mh_p[j + 2];
2103 mh_r2[j] = keep + sub_group_shuffle_xor(send, 1);
2104 }
2105 ACC_TYPE mh_r1 = (mh_b1 ? mh_r2[1] : mh_r2[0]) +
2106 sub_group_shuffle_xor(mh_b1 ? mh_r2[0] : mh_r2[1], 2);
2107 mh_r1 += sub_group_shuffle_xor(mh_r1, 4);
2108 mh_r1 += sub_group_shuffle_xor(mh_r1, 8);
2109
2110 ACC_TYPE mh_e2[2];
2111 {
2112 const ACC_TYPE other = sub_group_shuffle_xor(mh_r1, 2);
2113 mh_e2[0] = mh_b1 ? other : mh_r1;
2114 mh_e2[1] = mh_b1 ? mh_r1 : other;
2115 }
2116 ACC_TYPE mh_s[MQ_GQA];
2117 #pragma unroll
2118 for (int j = 0; j < 2; ++j) {
2119 const ACC_TYPE other = sub_group_shuffle_xor(mh_e2[j], 1);
2120 mh_s[j] = mh_b0 ? other : mh_e2[j];
2121 mh_s[j + 2] = mh_b0 ? mh_e2[j] : other;
2122 }
2123
2124 #pragma unroll
2125 for (int h = 0; h < MQ_GQA; ++h) {
2126 ACC_TYPE s = mh_s[h] * scale;
2127 if (logit_softcap > 0.0f) {
2128 s = logit_softcap * tanh(s / logit_softcap);
2129 }
2130 if (mask_bcast) {
2131 s += slope[h] * mask_val;
2132 } else if (mask_base_b != NULL) {
2133 const int mask_head_idx = FA_HEAD_IDX(h) % mask_ne2;
2134 const global MASK_DATA_TYPE * mask_ptr = (const global MASK_DATA_TYPE *) (mask_base_b + mask_head_idx * mask_nb2);
2135 s += slope[h] * (ACC_TYPE) mask_ptr[k_safe];
2136 }
2137 const ACC_TYPE sc = valid ? s : FA_M_INIT;
2138 const ACC_TYPE m_new = max(m_i[h], sc);
2139 const ACC_TYPE sp = native_exp(m_i[h] - m_new);
2140 const ACC_TYPE p = native_exp(sc - m_new);
2141 l_i[h] = l_i[h] * sp + p;
2142 m_i[h] = m_new;
2143 o_acc[h][0] = mad(p, v_vec_1, o_acc[h][0] * sp);
2144 }
2145#else
2146 // Dot: this lane covers DK elements {lic + FA_CL_C*i} of the cluster's row.
2147 ACC_TYPE4 dot4[MQ_GQA];
2148 #pragma unroll
2149 for (int h = 0; h < MQ_GQA; ++h) dot4[h] = (ACC_TYPE4)(0.0f);
2150 #pragma unroll
2151 for (int i = 0; i < FA_CL_DK; ++i) {
2152 const int kk = lic + FA_CL_C * i;
2153 const ACC_TYPE4 k_vec = CONVERT_KV_ACC4(k_ptr[kk]);
2154 #pragma unroll
2155 for (int h = 0; h < MQ_GQA; ++h) {
2156 dot4[h] = mad(q_shared[h * DK_VEC + kk], k_vec, dot4[h]);
2157 }
2158 }
2159
2160 // Cluster-reduce (xor steps < FA_CL_C stay inside the cluster) + score.
2161 ACC_TYPE score[MQ_GQA];
2162 #pragma unroll
2163 for (int h = 0; h < MQ_GQA; ++h) {
2164 ACC_TYPE s = dot4[h].s0 + dot4[h].s1 + dot4[h].s2 + dot4[h].s3;
2165 #pragma unroll
2166 for (int step = 1; step < FA_CL_C; step <<= 1) {
2167 s += sub_group_shuffle_xor(s, step);
2168 }
2169 s *= scale;
2170 if (logit_softcap > 0.0f) {
2171 s = logit_softcap * tanh(s / logit_softcap);
2172 }
2173 if (mask_base[h] != NULL) {
2174 const global MASK_DATA_TYPE * mask_ptr = (const global MASK_DATA_TYPE *) mask_base[h];
2175 s += slope[h] * (ACC_TYPE) mask_ptr[k_safe];
2176 }
2177 score[h] = valid ? s : FA_M_INIT;
2178 }
2179
2180 // Per-cluster online update — identical math to the baseline, but the
2181 // serial chain is per cluster (depth n_iter, not kv_per_sg).
2182 ACC_TYPE p_h[MQ_GQA];
2183 ACC_TYPE sp_h[MQ_GQA];
2184 #pragma unroll
2185 for (int h = 0; h < MQ_GQA; ++h) {
2186 const ACC_TYPE m_new = max(m_i[h], score[h]);
2187 sp_h[h] = native_exp(m_i[h] - m_new);
2188 p_h[h] = native_exp(score[h] - m_new);
2189 l_i[h] = l_i[h] * sp_h[h] + p_h[h];
2190 m_i[h] = m_new;
2191 }
2192
2193 // V accumulate on this lane's DV slice (p = 0 on tail -> inert).
2194 #pragma unroll
2195 for (int i = 0; i < FA_CL_DV; ++i) {
2196 const ACC_TYPE4 v_vec = CONVERT_KV_ACC4(v_ptr[lic + FA_CL_C * i]);
2197 #pragma unroll
2198 for (int h = 0; h < MQ_GQA; ++h) {
2199 o_acc[h][i] = mad(p_h[h], v_vec, o_acc[h][i] * sp_h[h]);
2200 }
2201 }
2202#endif
2203 }
2204
2205 // Merge stage 1: fold the FA_CL_NCL cluster partials inside the subgroup.
2206 // Lanes with equal lic across clusters hold the SAME dv slice, so a
2207 // cross-cluster xor-reduce (distances FA_CL_C..Q1_WG_SIZE/2) sums o
2208 // slice-wise; m/l fold the same way. All shuffles are subgroup-convergent.
2209 #pragma unroll
2210 for (int h = 0; h < MQ_GQA; ++h) {
2211 ACC_TYPE m_c = m_i[h];
2212 #pragma unroll
2213 for (int step = FA_CL_C; step < Q1_WG_SIZE; step <<= 1) {
2214 m_c = max(m_c, sub_group_shuffle_xor(m_c, step));
2215 }
2216 const ACC_TYPE alpha = native_exp(m_i[h] - m_c);
2217 ACC_TYPE l_c = l_i[h] * alpha;
2218 #pragma unroll
2219 for (int step = FA_CL_C; step < Q1_WG_SIZE; step <<= 1) {
2220 l_c += sub_group_shuffle_xor(l_c, step);
2221 }
2222 #pragma unroll
2223 for (int i = 0; i < FA_CL_DV; ++i) {
2224 ACC_TYPE4 o = o_acc[h][i] * alpha;
2225 #pragma unroll
2226 for (int step = FA_CL_C; step < Q1_WG_SIZE; step <<= 1) {
2227 o.s0 += sub_group_shuffle_xor(o.s0, step);
2228 o.s1 += sub_group_shuffle_xor(o.s1, step);
2229 o.s2 += sub_group_shuffle_xor(o.s2, step);
2230 o.s3 += sub_group_shuffle_xor(o.s3, step);
2231 }
2232 o_acc[h][i] = o;
2233 }
2234 m_i[h] = m_c;
2235 l_i[h] = l_c;
2236 }
2237
2238 // Merge stage 2: baseline cross-subgroup LDS merge. Cluster 0's lanes hold
2239 // the subgroup's merged o (dv indices {lic + FA_CL_C*i}) — same sg_o layout
2240 // and fold loop as q1_vec_mq_split.
2241 __local ACC_TYPE sg_m[MQ_GQA][MQ_NSG_SPLIT];
2242 __local ACC_TYPE sg_l[MQ_GQA][MQ_NSG_SPLIT];
2243 __local ACC_TYPE4 sg_o[MQ_NSG_SPLIT][DV_VEC];
2244
2245 if (tid_sg == 0) {
2246 #pragma unroll
2247 for (int h = 0; h < MQ_GQA; ++h) {
2248 sg_m[h][sgid] = m_i[h];
2249 sg_l[h][sgid] = l_i[h];
2250 }
2251 }
2252
2253 #pragma unroll
2254 for (int h = 0; h < MQ_GQA; ++h) {
2255 if (cl == 0) {
2256 #pragma unroll
2257 for (int i = 0; i < FA_CL_DV; ++i) {
2258 sg_o[sgid][lic + FA_CL_C * i] = o_acc[h][i];
2259 }
2260 }
2261 barrier(CLK_LOCAL_MEM_FENCE);
2262
2263 if (sgid == 0) {
2264 const int head_idx = FA_HEAD_IDX(h);
2265
2266 ACC_TYPE m_c = sg_m[h][0];
2267 #pragma unroll
2268 for (int s = 1; s < MQ_NSG_SPLIT; ++s) {
2269 m_c = max(m_c, sg_m[h][s]);
2270 }
2271 ACC_TYPE l_c = 0.0f;
2272 #pragma unroll
2273 for (int s = 0; s < MQ_NSG_SPLIT; ++s) {
2274 l_c += sg_l[h][s] * native_exp(sg_m[h][s] - m_c);
2275 }
2276
2277 const ulong rec_idx = ((((ulong) batch_idx * n_head + head_idx) * n_q + q_idx)
2278 * n_splits + split_idx);
2279 global float * rec = partial_void + rec_idx * record_stride;
2280 global float4 * rec_o = (global float4 *) (rec + 2);
2281
2282 if (tid_sg == 0) {
2283 rec[0] = (float) m_c;
2284 rec[1] = (float) l_c;
2285 }
2286 for (int dv_idx = tid_sg; dv_idx < DV_VEC; dv_idx += Q1_WG_SIZE) {
2287 ACC_TYPE4 o_merged = (ACC_TYPE4)(0.0f);
2288 #pragma unroll
2289 for (int s = 0; s < MQ_NSG_SPLIT; ++s) {
2290 const ACC_TYPE alpha = native_exp(sg_m[h][s] - m_c);
2291 o_merged = mad((ACC_TYPE4)(alpha), sg_o[s][dv_idx], o_merged);
2292 }
2293 rec_o[dv_idx] = o_merged;
2294 }
2295 }
2296 barrier(CLK_LOCAL_MEM_FENCE);
2297 }
2298}
2299
2300#undef FA_HEAD_IDX
2301
2302#endif // DK_VEC/DV_VEC divisible by FA_CL_C
2303#endif // HAS_SUBGROUP_SHUFFLE (q1_vec_mq_split_c8)
2304
2305REQD_SUBGROUP_SIZE_64
2306__kernel void flash_attn_f32_f16_q1_vec_mq_split_k_img(
2307 const global void * q_void, ulong q_offset,
2308 __read_only image1d_buffer_t k_img,
2309 const global void * v_void, ulong v_offset,
2310 const float scale,
2311 const int n_q,
2312 const int n_kv,
2313 const int n_head,
2314 const ulong q_nb1, const ulong q_nb2, const ulong q_nb3,
2315 const ulong k_nb1, const ulong k_nb2, const ulong k_nb3,
2316 const ulong v_nb1, const ulong v_nb2, const ulong v_nb3,
2317 const float max_bias,
2318 const float m0,
2319 const float m1,
2320 const int n_head_log2,
2321 const float logit_softcap,
2322 const int n_head_kv,
2323 const global void * mask_void,
2324 const ulong mask_offset,
2325 const ulong mask_nb1,
2326 const ulong mask_nb2,
2327 const ulong mask_nb3,
2328 const int mask_ne2,
2329 const int mask_ne3,
2330 global float * partial_void,
2331 const int n_splits,
2332 const int kv_per_split
2333) {
2334 const int tid = get_local_id(0);
2335 const int sgid = tid / Q1_WG_SIZE;
2336 const int tid_sg = tid % Q1_WG_SIZE;
2337 const int kvhead_batch_idx = get_global_id(1);
2338 const int split_q_idx = get_global_id(2);
2339 const int split_idx = split_q_idx % n_splits;
2340 const int q_idx = split_q_idx / n_splits;
2341
2342 const int batch_idx = kvhead_batch_idx / n_head_kv;
2343 const int head_kv_idx = kvhead_batch_idx % n_head_kv;
2344
2345 const int kv_start = split_idx * kv_per_split;
2346 const int kv_end = min(kv_start + kv_per_split, n_kv);
2347
2348 const ulong record_stride = (ulong) FA_PARTIAL_FLOATS;
2349
2350 if (kv_start >= kv_end) {
2351 if (tid == 0) {
2352 #pragma unroll
2353 for (int h = 0; h < MQ_GQA; ++h) {
2354 const int head_idx = head_kv_idx * MQ_GQA + h;
2355 const ulong rec_idx = ((((ulong) batch_idx * n_head + head_idx) * n_q + q_idx)
2356 * n_splits + split_idx);
2357 global float * rec = partial_void + rec_idx * record_stride;
2358 rec[0] = FA_M_INIT;
2359 rec[1] = 0.0f;
2360 }
2361 }
2362 return;
2363 }
2364
2365 const global char * q_base = (const global char *) q_void + q_offset;
2366 const global char * v_base = (const global char *) v_void + v_offset;
2367
2368 __local ACC_TYPE4 q_shared[MQ_GQA * DK_VEC];
2369 for (int i = tid; i < MQ_GQA * DK_VEC; i += MQ_SPLIT_WG_SIZE) {
2370 const int h = i / DK_VEC;
2371 const int k = i % DK_VEC;
2372 const int head_idx = head_kv_idx * MQ_GQA + h;
2373 const ulong q_row_offset = batch_idx * q_nb3 + head_idx * q_nb2 + (ulong) q_idx * q_nb1;
2374 const global Q_DATA_TYPE4 * q_ptr = (const global Q_DATA_TYPE4 *) (q_base + q_row_offset);
2375 q_shared[h * DK_VEC + k] = CONVERT_Q_ACC4(q_ptr[k]);
2376 }
2377 barrier(CLK_LOCAL_MEM_FENCE);
2378
2379 float slope[MQ_GQA];
2380 #pragma unroll
2381 for (int h = 0; h < MQ_GQA; ++h) {
2382 slope[h] = get_alibi_slope(max_bias, head_kv_idx * MQ_GQA + h, n_head_log2, m0, m1);
2383 }
2384
2385 const global char * mask_base[MQ_GQA];
2386 if (mask_void != NULL) {
2387 const int mask_batch_idx = batch_idx % mask_ne3;
2388 const global char * mask_base_b = (const global char *) mask_void + mask_offset +
2389 mask_batch_idx * mask_nb3 +
2390 (ulong) q_idx * mask_nb1;
2391 #pragma unroll
2392 for (int h = 0; h < MQ_GQA; ++h) {
2393 const int head_idx = head_kv_idx * MQ_GQA + h;
2394 const int mask_head_idx = head_idx % mask_ne2;
2395 mask_base[h] = mask_base_b + mask_head_idx * mask_nb2;
2396 }
2397 } else {
2398 #pragma unroll
2399 for (int h = 0; h < MQ_GQA; ++h) mask_base[h] = NULL;
2400 }
2401
2402 ACC_TYPE4 o_acc[MQ_GQA][Q1V_DV_PER_THREAD];
2403 ACC_TYPE m_i[MQ_GQA];
2404 ACC_TYPE l_i[MQ_GQA];
2405 #pragma unroll
2406 for (int h = 0; h < MQ_GQA; ++h) {
2407 m_i[h] = FA_M_INIT;
2408 l_i[h] = 0.0f;
2409 #pragma unroll
2410 for (int i = 0; i < Q1V_DV_PER_THREAD; ++i) o_acc[h][i] = (ACC_TYPE4)(0.0f);
2411 }
2412
2413 // K pitches in pixel units, pixel = 1 half4 = 8 B -> byte_stride >> 3.
2414 const int pitch_px_row = (int)(k_nb1 >> 3);
2415 const int pitch_px_head = (int)(k_nb2 >> 3);
2416 const int pitch_px_batch = (int)(k_nb3 >> 3);
2417
2418 const int kv_len = kv_end - kv_start;
2419 const int kv_per_sg = (kv_len + MQ_NSG_SPLIT - 1) / MQ_NSG_SPLIT;
2420 const int kv_lo = kv_start + sgid * kv_per_sg;
2421 const int kv_hi = min(kv_end, kv_lo + kv_per_sg);
2422
2423 for (int k_idx = kv_lo; k_idx < kv_hi; ++k_idx) {
2424 const int k_row_px = batch_idx * pitch_px_batch +
2425 head_kv_idx * pitch_px_head +
2426 k_idx * pitch_px_row;
2427
2428 const ulong v_row_off = batch_idx * v_nb3 + head_kv_idx * v_nb2 + k_idx * v_nb1;
2429 const global KV_DATA_TYPE4 * v_ptr = (const global KV_DATA_TYPE4 *) (v_base + v_row_off);
2430
2431 ACC_TYPE4 dot4[MQ_GQA];
2432 #pragma unroll
2433 for (int h = 0; h < MQ_GQA; ++h) dot4[h] = (ACC_TYPE4)(0.0f);
2434 for (int k = tid_sg; k < DK_VEC; k += Q1_WG_SIZE) {
2435 const half4 k_h4 = read_imageh(k_img, k_row_px + k);
2436 const ACC_TYPE4 k_vec = CONVERT_KV_ACC4(k_h4);
2437 #pragma unroll
2438 for (int h = 0; h < MQ_GQA; ++h) {
2439 dot4[h] = mad(q_shared[h * DK_VEC + k], k_vec, dot4[h]);
2440 }
2441 }
2442
2443 ACC_TYPE score[MQ_GQA];
2444 #pragma unroll
2445 for (int h = 0; h < MQ_GQA; ++h) {
2446 const ACC_TYPE dot_partial = dot4[h].s0 + dot4[h].s1 + dot4[h].s2 + dot4[h].s3;
2447 ACC_TYPE s = sub_group_reduce_add(dot_partial) * scale;
2448 if (logit_softcap > 0.0f) {
2449 s = logit_softcap * tanh(s / logit_softcap);
2450 }
2451 if (mask_base[h] != NULL) {
2452 const global MASK_DATA_TYPE * mask_ptr = (const global MASK_DATA_TYPE *) mask_base[h];
2453 s += slope[h] * (ACC_TYPE) mask_ptr[k_idx];
2454 }
2455 score[h] = s;
2456 }
2457
2458 ACC_TYPE p_h[MQ_GQA];
2459 ACC_TYPE sp_h[MQ_GQA];
2460 #pragma unroll
2461 for (int h = 0; h < MQ_GQA; ++h) {
2462 const ACC_TYPE m_new = max(m_i[h], score[h]);
2463 sp_h[h] = native_exp(m_i[h] - m_new);
2464 p_h[h] = native_exp(score[h] - m_new);
2465 l_i[h] = l_i[h] * sp_h[h] + p_h[h];
2466 m_i[h] = m_new;
2467 }
2468
2469 int idx = 0;
2470 for (int dv_idx = tid_sg; dv_idx < DV_VEC; dv_idx += Q1_WG_SIZE, ++idx) {
2471 const ACC_TYPE4 v_vec = CONVERT_KV_ACC4(v_ptr[dv_idx]);
2472 #pragma unroll
2473 for (int h = 0; h < MQ_GQA; ++h) {
2474 o_acc[h][idx] = mad(p_h[h], v_vec, o_acc[h][idx] * sp_h[h]);
2475 }
2476 }
2477 }
2478
2479 __local ACC_TYPE sg_m[MQ_GQA][MQ_NSG_SPLIT];
2480 __local ACC_TYPE sg_l[MQ_GQA][MQ_NSG_SPLIT];
2481 __local ACC_TYPE4 sg_o[MQ_NSG_SPLIT][DV_VEC];
2482
2483 if (tid_sg == 0) {
2484 #pragma unroll
2485 for (int h = 0; h < MQ_GQA; ++h) {
2486 sg_m[h][sgid] = m_i[h];
2487 sg_l[h][sgid] = l_i[h];
2488 }
2489 }
2490
2491 #pragma unroll
2492 for (int h = 0; h < MQ_GQA; ++h) {
2493 {
2494 int idx = 0;
2495 for (int dv_idx = tid_sg; dv_idx < DV_VEC; dv_idx += Q1_WG_SIZE, ++idx) {
2496 sg_o[sgid][dv_idx] = o_acc[h][idx];
2497 }
2498 }
2499 barrier(CLK_LOCAL_MEM_FENCE);
2500
2501 if (sgid == 0) {
2502 const int head_idx = head_kv_idx * MQ_GQA + h;
2503
2504 ACC_TYPE m_c = sg_m[h][0];
2505 #pragma unroll
2506 for (int s = 1; s < MQ_NSG_SPLIT; ++s) {
2507 m_c = max(m_c, sg_m[h][s]);
2508 }
2509 ACC_TYPE l_c = 0.0f;
2510 #pragma unroll
2511 for (int s = 0; s < MQ_NSG_SPLIT; ++s) {
2512 l_c += sg_l[h][s] * native_exp(sg_m[h][s] - m_c);
2513 }
2514
2515 const ulong rec_idx = ((((ulong) batch_idx * n_head + head_idx) * n_q + q_idx)
2516 * n_splits + split_idx);
2517 global float * rec = partial_void + rec_idx * record_stride;
2518 global float4 * rec_o = (global float4 *) (rec + 2);
2519
2520 if (tid_sg == 0) {
2521 rec[0] = (float) m_c;
2522 rec[1] = (float) l_c;
2523 }
2524 for (int dv_idx = tid_sg; dv_idx < DV_VEC; dv_idx += Q1_WG_SIZE) {
2525 ACC_TYPE4 o_merged = (ACC_TYPE4)(0.0f);
2526 #pragma unroll
2527 for (int s = 0; s < MQ_NSG_SPLIT; ++s) {
2528 const ACC_TYPE alpha = native_exp(sg_m[h][s] - m_c);
2529 o_merged = mad((ACC_TYPE4)(alpha), sg_o[s][dv_idx], o_merged);
2530 }
2531 rec_o[dv_idx] = o_merged;
2532 }
2533 }
2534 barrier(CLK_LOCAL_MEM_FENCE);
2535 }
2536}
2537#endif // !FA_MQ_SPLIT_ONLY
2538#endif // !FA_DECODE_ONLY
2539
2540#ifndef FA_MQ_ONLY // q1_split + merge excluded from the MQ-only (g8) program
2541#ifndef FA_VEC_ONLY
2542__kernel void flash_attn_f32_f16_q1_split(
2543 const global void * q_void, ulong q_offset,
2544 const global void * k_void, ulong k_offset,
2545 const global void * v_void, ulong v_offset,
2546 const float scale,
2547 const int n_q,
2548 const int n_kv,
2549 const int n_head,
2550 const ulong q_nb1, const ulong q_nb2, const ulong q_nb3,
2551 const ulong k_nb1, const ulong k_nb2, const ulong k_nb3,
2552 const ulong v_nb1, const ulong v_nb2, const ulong v_nb3,
2553 const float max_bias,
2554 const float m0,
2555 const float m1,
2556 const int n_head_log2,
2557 const float logit_softcap,
2558 const int n_head_kv,
2559 const global void * mask_void,
2560 const ulong mask_offset,
2561 const ulong mask_nb1,
2562 const ulong mask_nb2,
2563 const ulong mask_nb3,
2564 const int mask_ne2,
2565 const int mask_ne3,
2566 global float * partial_void,
2567 const int n_splits,
2568 const int kv_per_split
2569) {
2570 const int tid = get_local_id(0);
2571 const int head_batch_idx = get_global_id(1);
2572 const int split_q_idx = get_global_id(2);
2573 const int split_idx = split_q_idx % n_splits;
2574 const int q_idx = split_q_idx / n_splits;
2575 const int batch_idx = head_batch_idx / n_head;
2576 const int head_idx = head_batch_idx % n_head;
2577 const int gqa_ratio = n_head / n_head_kv;
2578 const int head_kv_idx = head_idx / gqa_ratio;
2579
2580 const int kv_start = split_idx * kv_per_split;
2581 const int kv_end = min(kv_start + kv_per_split, n_kv);
2582
2583 const ulong record_stride = (ulong) FA_PARTIAL_FLOATS;
2584 const ulong record_idx = ((((ulong) batch_idx * n_head + head_idx) * n_q + q_idx)
2585 * n_splits + split_idx);
2586 global float * rec = partial_void + record_idx * record_stride;
2587 global float4 * rec_o = (global float4 *) (rec + 2);
2588
2589 if (kv_start >= kv_end) {
2590 // Empty split: leave sentinel partial for merge.
2591 if (tid == 0) {
2592 rec[0] = FA_M_INIT;
2593 rec[1] = 0.0f;
2594 }
2595 return;
2596 }
2597
2598 const global char * q_base = (const global char *) q_void + q_offset;
2599 const global char * k_base = (const global char *) k_void + k_offset;
2600 const global char * v_base = (const global char *) v_void + v_offset;
2601
2602 const global char * mask_base = NULL;
2603 if (mask_void != NULL) {
2604 const int mask_head_idx = head_idx % mask_ne2;
2605 const int mask_batch_idx = batch_idx % mask_ne3;
2606 mask_base = (const global char *) mask_void + mask_offset +
2607 mask_batch_idx * mask_nb3 + mask_head_idx * mask_nb2 +
2608 (ulong) q_idx * mask_nb1;
2609 }
2610
2611 // share Q via local memory (n_q=1 per split -> uniform across WG).
2612 __local ACC_TYPE4 q_shared[DK_VEC];
2613 const ulong q_row_offset = batch_idx * q_nb3 + head_idx * q_nb2 + (ulong) q_idx * q_nb1;
2614 const global Q_DATA_TYPE4 * q_ptr = (const global Q_DATA_TYPE4 *) (q_base + q_row_offset);
2615 for (int i = tid; i < DK_VEC; i += Q1_WG_SIZE) {
2616 q_shared[i] = CONVERT_Q_ACC4(q_ptr[i]);
2617 }
2618 sub_group_barrier(CLK_LOCAL_MEM_FENCE);
2619
2620 const float slope = get_alibi_slope(max_bias, head_idx, n_head_log2, m0, m1);
2621
2622 // pass 1a — split-local max.
2623 ACC_TYPE m_i = FA_M_INIT;
2624 for (int k_idx = kv_start + tid; k_idx < kv_end; k_idx += Q1_WG_SIZE) {
2625 const ulong k_row_offset = batch_idx * k_nb3 + head_kv_idx * k_nb2 + k_idx * k_nb1;
2626 const global KV_DATA_TYPE4 * k_ptr = (const global KV_DATA_TYPE4 *) (k_base + k_row_offset);
2627 ACC_TYPE4 dot_acc = (ACC_TYPE4)(0.0f);
2628 #pragma unroll
2629 for (int k = 0; k < DK_VEC; ++k) {
2630 dot_acc = mad(q_shared[k], CONVERT_KV_ACC4(k_ptr[k]), dot_acc);
2631 }
2632 ACC_TYPE score = (dot_acc.s0 + dot_acc.s1 + dot_acc.s2 + dot_acc.s3) * scale;
2633 if (logit_softcap > 0.0f) {
2634 score = logit_softcap * tanh(score / logit_softcap);
2635 }
2636 if (mask_base != NULL) {
2637 const global MASK_DATA_TYPE * mask_ptr = (const global MASK_DATA_TYPE *) (mask_base);
2638 score += slope * (ACC_TYPE) mask_ptr[k_idx];
2639 }
2640 m_i = max(m_i, score);
2641 }
2642
2643 const ACC_TYPE m_c = sub_group_reduce_max(m_i);
2644
2645 // pass 1b — softmax-weighted V accumulate.
2646 ACC_TYPE4 o_acc[DV_VEC];
2647 #pragma unroll
2648 for (int i = 0; i < DV_VEC; ++i) o_acc[i] = (ACC_TYPE4)(0.0f);
2649 ACC_TYPE l_i = 0.0f;
2650
2651 for (int k_idx = kv_start + tid; k_idx < kv_end; k_idx += Q1_WG_SIZE) {
2652 const ulong k_row_offset = batch_idx * k_nb3 + head_kv_idx * k_nb2 + k_idx * k_nb1;
2653 const ulong v_row_offset = batch_idx * v_nb3 + head_kv_idx * v_nb2 + k_idx * v_nb1;
2654 const global KV_DATA_TYPE4 * k_ptr = (const global KV_DATA_TYPE4 *) (k_base + k_row_offset);
2655 const global KV_DATA_TYPE4 * v_ptr = (const global KV_DATA_TYPE4 *) (v_base + v_row_offset);
2656 ACC_TYPE4 dot_acc = (ACC_TYPE4)(0.0f);
2657 #pragma unroll
2658 for (int k = 0; k < DK_VEC; ++k) {
2659 dot_acc = mad(q_shared[k], CONVERT_KV_ACC4(k_ptr[k]), dot_acc);
2660 }
2661 ACC_TYPE score = (dot_acc.s0 + dot_acc.s1 + dot_acc.s2 + dot_acc.s3) * scale;
2662 if (logit_softcap > 0.0f) {
2663 score = logit_softcap * tanh(score / logit_softcap);
2664 }
2665 if (mask_base != NULL) {
2666 const global MASK_DATA_TYPE * mask_ptr = (const global MASK_DATA_TYPE *) (mask_base);
2667 score += slope * (ACC_TYPE) mask_ptr[k_idx];
2668 }
2669 const ACC_TYPE p = exp(score - m_c);
2670 l_i += p;
2671 #pragma unroll
2672 for (int i = 0; i < DV_VEC; ++i) {
2673 o_acc[i] = mad(p, CONVERT_KV_ACC4(v_ptr[i]), o_acc[i]);
2674 }
2675 }
2676
2677 __local ACC_TYPE4 local_o[Q1_WG_SIZE];
2678 const ACC_TYPE l_c = sub_group_reduce_add(l_i);
2679
2680 if (tid == 0) {
2681 rec[0] = (float) m_c;
2682 rec[1] = (float) l_c;
2683 }
2684 for (int i = 0; i < DV_VEC; ++i) {
2685 local_o[tid] = o_acc[i];
2686 sub_group_barrier(CLK_LOCAL_MEM_FENCE);
2687 #pragma unroll
2688 for (int s = Q1_WG_SIZE / 2; s > 0; s >>= 1) {
2689 if (tid < s) local_o[tid] += local_o[tid + s];
2690 sub_group_barrier(CLK_LOCAL_MEM_FENCE);
2691 }
2692 if (tid == 0) {
2693 rec_o[i] = local_o[0];
2694 }
2695 }
2696}
2697
2698#endif // !FA_VEC_ONLY
2699
2700// FD Pass 2: merge per-split partials into final O
2701// empty splits drop via exp(-INF)=0.
2702__kernel void flash_attn_f32_merge(
2703 const global float * partial_void,
2704 global void * o_void,
2705 const ulong o_offset,
2706 const int n_head,
2707 const int n_splits,
2708 const ulong o_nb1, const ulong o_nb2, const ulong o_nb3,
2709 const global void * sinks_void,
2710 const ulong sinks_offset,
2711 const int n_q
2712) {
2713 const int lane = get_local_id(0); // 0..DV_VEC-1
2714 const int head_batch_idx = get_global_id(1);
2715 const int q_idx = get_global_id(2);
2716 const int batch_idx = head_batch_idx / n_head;
2717 const int head_idx = head_batch_idx % n_head;
2718
2719 const ulong record_stride = (ulong) FA_PARTIAL_FLOATS;
2720 const ulong record_idx_0 = (((ulong) batch_idx * n_head + head_idx) * n_q + q_idx) * n_splits;
2721 const global float * rec0 = partial_void + record_idx_0 * record_stride;
2722
2723 __local ACC_TYPE m_final_shared;
2724 __local ACC_TYPE l_final_shared;
2725 if (lane == 0) {
2726 ACC_TYPE m = FA_M_INIT;
2727 for (int c = 0; c < n_splits; ++c) {
2728 const ACC_TYPE m_c = rec0[c * record_stride + 0];
2729 m = max(m, m_c);
2730 }
2731 ACC_TYPE m_sink = 0.0f;
2732 bool has_sink = false;
2733 if (sinks_void != NULL) {
2734 const global ACC_TYPE * sinks_ptr =
2735 (const global ACC_TYPE *) ((const global char *) sinks_void + sinks_offset);
2736 m_sink = sinks_ptr[head_idx];
2737 has_sink = true;
2738 m = max(m, m_sink);
2739 }
2740 ACC_TYPE l = 0.0f;
2741 for (int c = 0; c < n_splits; ++c) {
2742 const ACC_TYPE m_c = rec0[c * record_stride + 0];
2743 const ACC_TYPE l_c = rec0[c * record_stride + 1];
2744 if (m_c > FA_M_INIT) {
2745 l += l_c * exp(m_c - m);
2746 }
2747 }
2748 if (has_sink) {
2749 l += exp(m_sink - m);
2750 }
2751 m_final_shared = m;
2752 l_final_shared = l;
2753 }
2754 barrier(CLK_LOCAL_MEM_FENCE);
2755 const ACC_TYPE m_final = m_final_shared;
2756 const ACC_TYPE l_final = l_final_shared;
2757 const ACC_TYPE l_inv = (l_final > 0.0f) ? (1.0f / l_final) : 0.0f;
2758
2759 ACC_TYPE4 o = (ACC_TYPE4)(0.0f);
2760 for (int c = 0; c < n_splits; ++c) {
2761 const global float * rec_c = rec0 + c * record_stride;
2762 const ACC_TYPE m_c = rec_c[0];
2763 if (m_c <= FA_M_INIT) continue;
2764 const global float4 * rec_oc = (const global float4 *) (rec_c + 2);
2765 const ACC_TYPE scale_c = exp(m_c - m_final);
2766 o = mad((ACC_TYPE4)(scale_c), rec_oc[lane], o);
2767 }
2768 o = o * l_inv;
2769
2770 const ulong o_row_offset = (ulong) batch_idx * o_nb3 + (ulong) q_idx * o_nb2 + (ulong) head_idx * o_nb1;
2771 global O_DATA_TYPE4 * o_row = (global O_DATA_TYPE4 *) ((global char *) o_void + o_offset + o_row_offset);
2772 o_row[lane] = CONVERT_O_DATA4(o);
2773}
2774#endif // !FA_MQ_ONLY (q1_split + merge)
2775#endif // !FA_PREFILL_ONLY (decode kernels)