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