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-vulkan/vulkan-shaders/flash_attn.comp # 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-vulkan / vulkan-shaders / flash_attn.comp` 767 lines ยท 31558 bytes Raw Blame History 1 #version 450 2 3 #extension GL_EXT_control_flow_attributes : enable 4 #extension GL_EXT_shader_16bit_storage : require 5 6 #extension GL_EXT_shader_explicit_arithmetic_types_int32 : require 7 8 #ifdef FLOAT16 9 #extension GL_EXT_shader_explicit_arithmetic_types_float16 : require 10 #extension GL_EXT_shader_subgroup_extended_types_float16 : require 11 #endif 12 13 #ifdef MMQ 14 #extension GL_EXT_integer_dot_product : require 15 #extension GL_KHR_shader_subgroup_clustered : require 16 17 #include "mul_mmq_shmem_types.glsl" 18 #endif 19 20 #extension GL_KHR_shader_subgroup_shuffle : enable 21 #extension GL_KHR_shader_subgroup_vote : enable 22 23 #include "types.glsl" 24 #include "dot_product_funcs.glsl" 25 #include "flash_attn_base.glsl" 26 #include "flash_attn_dequant.glsl" 27 28 const uint32_t HSK_per_thread = HSK / D_split; 29 const uint32_t HSV_per_thread = HSV / D_split; 30 31 const uint32_t rows_per_thread = Br / row_split; 32 const uint32_t cols_per_iter = WorkGroupSize / D_split / row_split; 33 const uint32_t cols_per_thread = Bc / cols_per_iter; 34 const uint32_t num_subgroups = SubGroupSize == 0 ? 0 : WorkGroupSize / SubGroupSize; 35 36 37 layout (binding = 0) readonly buffer Q {float data_q[];}; 38 layout (binding = 0) readonly buffer QV4 {vec4 data_qv4[];}; 39 layout (binding = 1) readonly buffer K {float16_t data_k[];}; 40 layout (binding = 1) readonly buffer KV4 {f16vec4 data_kv4[];}; 41 layout (binding = 2) readonly buffer V {float16_t data_v[];}; 42 layout (binding = 2) readonly buffer VV4 {f16vec4 data_vv4[];}; 43 layout (binding = 3) readonly buffer M {float16_t data_m[];}; 44 45 // If SubGroupSize is set to 0 then only use shmem reductions 46 const uint32_t tmpsh_size = (SubGroupSize > 0) ? (row_split == 1 ? num_subgroups * D_split : num_subgroups) : WorkGroupSize; 47 shared float tmpsh[tmpsh_size]; 48 shared FLOAT_TYPEV4 tmpshv4[tmpsh_size]; 49 50 const uint32_t masksh_stride = Br + 1; 51 shared FLOAT_TYPE masksh[Bc * masksh_stride]; 52 53 #ifndef MMQ 54 const uint32_t qf_stride = HSK / 4 + 1; 55 shared FLOAT_TYPEV4 Qf[Br * qf_stride]; 56 #else 57 58 const uint32_t qf_stride = HSK / 32; 59 shared block_b_cache Qf[Br * qf_stride]; 60 #endif 61 62 #ifndef MMQ 63 const uint32_t D = HSK > HSV ? HSK : HSV; 64 #else 65 const uint32_t D = HSV; 66 #endif 67 const uint32_t kvsh_stride = D / 4 + 1; 68 shared FLOAT_TYPEV4 kvsh[SHMEM_STAGING != 0 ? Bc * kvsh_stride : 1]; 69 70 #ifdef MMQ 71 72 shared block_a_cache kblocksh[SHMEM_STAGING != 0 ? Bc * qf_stride : 1]; 73 #endif 74 75 shared vec4 occupancy_limiter[LIMIT_OCCUPANCY_SHMEM > 0 ? LIMIT_OCCUPANCY_SHMEM : 1]; 76 77 #ifdef MMQ 78 #include "flash_attn_mmq_funcs.glsl" 79 #endif 80 81 void main() { 82 #ifdef NEEDS_INIT_IQ_SHMEM 83 if (fa_type_needs_shmem(FaTypeK) || fa_type_needs_shmem(FaTypeV)) { 84 init_iq_shmem(gl_WorkGroupSize); 85 } 86 #endif 87 88 init_indices(); 89 90 const uint32_t tid = gl_LocalInvocationIndex; 91 const uint32_t threads_per_rowgroup = gl_WorkGroupSize.x / row_split; 92 const uint32_t row_tid = gl_LocalInvocationIndex / threads_per_rowgroup; 93 const uint32_t rowgroup_tid = gl_LocalInvocationIndex % threads_per_rowgroup; 94 const uint32_t d_tid = gl_LocalInvocationIndex % D_split; 95 const uint32_t col_tid = (gl_LocalInvocationIndex % threads_per_rowgroup) / D_split; 96 97 if (LIMIT_OCCUPANCY_SHMEM > 0) { 98 // This just exists to avoid the occupancy_limiter array getting optimized out 99 occupancy_limiter[tid] = vec4(tid); 100 101 barrier(); 102 103 if (occupancy_limiter[tid] == vec4(99999.0)) { 104 data_ov4[0] = D_TYPEV4(occupancy_limiter[tid]); 105 } 106 } 107 108 #define tile_row(r) (row_tid * rows_per_thread + (r)) 109 110 uint32_t q_offset = gqa_iq1*p.nb01 + (iq2*p.nb02 + iq3*p.nb03) / 4; 111 112 [[unroll]] for (uint32_t idx = 0; idx < Br * HSK / 4; idx += gl_WorkGroupSize.x) { 113 uint32_t d = (idx + tid) % (HSK / 4); 114 uint32_t r = (idx + tid) / (HSK / 4); 115 const bool is_in_bounds = r < Br && d < HSK / 4 && i * Br + r < N; 116 #ifndef MMQ 117 if (is_in_bounds) { 118 Qf[r * qf_stride + d] = FLOAT_TYPEV4(data_qv4[q_offset / 4 + (i * Br + r) * q_stride / 4 + d] * p.scale); 119 } 120 #else 121 const uint buf_ib = r * qf_stride + d / 8; 122 const uint buf_iqs = d % 8; 123 124 vec4 vals = is_in_bounds ? data_qv4[q_offset / 4 + (i * Br + r) * q_stride / 4 + d] * p.scale : vec4(0.0f); 125 const vec4 abs_vals = abs(vals); 126 127 const float thread_max = max(max(abs_vals.x, abs_vals.y), max(abs_vals.z, abs_vals.w)); 128 const float amax = subgroupClusteredMax(thread_max, 8); 129 const float qd = amax / 127.0f; 130 const float qd_inv = qd != 0.0f ? 1.0f / qd : 0.0f; 131 vals = round(vals * qd_inv); 132 133 const float thread_sum = vals.x + vals.y + vals.z + vals.w; 134 // Q8_0 K does not use the row sum. 135 const float sum = FaTypeK == GGML_TYPE_Q8_0 ? 0.0f : subgroupClusteredAdd(thread_sum, 8); 136 137 if (r < Br) { 138 Qf[buf_ib].qs[buf_iqs] = pack32(i8vec4(vals)); 139 140 if (buf_iqs == 0) { 141 Qf[buf_ib].ds = FaTypeK == GGML_TYPE_Q8_0 ? FLOAT_TYPEV2(qd, 0.0f) : FLOAT_TYPEV2(qd, sum * qd); 142 } 143 } 144 #endif 145 } 146 barrier(); 147 148 FLOAT_TYPEV4 Of[rows_per_thread][HSV_per_thread / 4]; 149 [[unroll]] for (uint32_t d = 0; d < HSV_per_thread / 4; ++d) { 150 [[unroll]] for (uint32_t r = 0; r < rows_per_thread; ++r) { 151 Of[r][d] = FLOAT_TYPEV4(0.0); 152 } 153 } 154 155 float Lf[rows_per_thread], Mf[rows_per_thread]; 156 157 // Use -FLT_MAX/2 rather than -inf to reduce the possibility of NaNs, e.g. when computing Mold-M. 158 const float NEG_FLT_MAX_OVER_2 = uintBitsToFloat(0xFEFFFFFF); 159 160 [[unroll]] for (uint32_t r = 0; r < rows_per_thread; ++r) { 161 Lf[r] = 0; 162 Mf[r] = NEG_FLT_MAX_OVER_2; 163 } 164 165 ACC_TYPE slope[rows_per_thread]; 166 [[unroll]] for (uint32_t r = 0; r < rows_per_thread; ++r) { 167 slope[r] = ACC_TYPE(1.0); 168 } 169 170 // ALiBi 171 if (p.max_bias > 0.0f) { 172 [[unroll]] for (uint32_t r = 0; r < rows_per_thread; ++r) { 173 slope[r] = perElemOpComputeSlope(tile_row(r), col_tid, ACC_TYPE(0), iq2); 174 } 175 } 176 177 const uint32_t mo_stride = CEIL_DIV(KV, 16 * Bc); 178 // mo_offset will point to the tile starting at row i*Br and col 0 179 uint32_t mo_offset = mo_stride * i; 180 181 // FaBlockBytesK/V == 2 for f16, 16 for f32, ggml block byte size for quants. 182 uint32_t k_offset = (ik2*p.nb12 + ik3*p.nb13) / FaBlockBytesK; 183 uint32_t v_offset = (iv2*p.nb22 + iv3*p.nb23) / FaBlockBytesV; 184 uint32_t m_offset = gqa_iq1*KV; 185 if (p.nem2 != 1 || p.nem3 != 1) { 186 m_offset += ((iq3 % p.nem3) * p.nem2 + (iq2 % p.nem2)) * p.nem1 * KV; 187 mo_offset += ((iq3 % p.nem3) * p.nem2 + (iq2 % p.nem2)) * CEIL_DIV(p.nem1, Br) * mo_stride; 188 } 189 190 uint32_t mask_opt = 0; 191 uint32_t mask_opt_idx = ~0; 192 uint32_t mask_opt_bits = 0; 193 194 [[dont_unroll]] 195 for (uint32_t j = start_j; j < end_j; ++j) { 196 if (MASK_ENABLE) { 197 if (USE_MASK_OPT && mask_opt_idx != j / 16) { 198 mask_opt_idx = j / 16; 199 mask_opt = data_mask_opt[mo_offset + mask_opt_idx]; 200 } 201 mask_opt_bits = (mask_opt >> ((j % 16) * 2)) & 0x3; 202 if (mask_opt_bits == MASK_OPT_ALL_NEG_INF) { 203 // skip this block 204 continue; 205 } 206 // Only load if the block is not all zeros 207 if (mask_opt_bits != MASK_OPT_ALL_ZERO) { 208 bool nem1_bounds_check = !(p.gqa_ratio > 1) && (p.nem1 % Br) != 0; 209 210 float max_mask = NEG_FLT_MAX_OVER_2; 211 barrier(); 212 [[unroll]] for (uint32_t idx = 0; idx < Bc * Br; idx += gl_WorkGroupSize.x) { 213 uint32_t c = (idx + tid) % Bc; 214 uint32_t r = (idx + tid) / Bc; 215 if (idx + tid < Bc * Br) { 216 uint32_t kcol; 217 bool kv_active = fa_kv_index(j * Bc + c, kcol); 218 if (kv_active && (!nem1_bounds_check || i * Br + r < p.nem1)) { 219 FLOAT_TYPE m = FLOAT_TYPE(data_m[m_offset + (i * Br + r) * m_stride + kcol]); 220 masksh[c * masksh_stride + r] = m; 221 max_mask = max(max_mask, float(m)); 222 } else { 223 masksh[c * masksh_stride + r] = USE_SPARSE ? FLOAT_TYPE(NEG_FLT_MAX_OVER_2) : FLOAT_TYPE(0); 224 } 225 } 226 } 227 // skip the block if the mask is entirely -inf 228 bool all_less = subgroupAll(max_mask <= NEG_FLT_MAX_OVER_2); 229 barrier(); 230 if (gl_SubgroupInvocationID == 0) { 231 tmpsh[gl_SubgroupID] = all_less ? NEG_FLT_MAX_OVER_2 : 0.0f; 232 } 233 barrier(); 234 [[unroll]] for (uint s = 0; s < gl_NumSubgroups; ++s) { 235 max_mask = max(max_mask, tmpsh[s]); 236 } 237 if (max_mask <= NEG_FLT_MAX_OVER_2) { 238 continue; 239 } 240 } 241 } 242 243 ACC_TYPE Sf[rows_per_thread][cols_per_thread]; 244 [[unroll]] for (uint32_t r = 0; r < rows_per_thread; ++r) { 245 [[unroll]] for (uint32_t c = 0; c < cols_per_thread; ++c) { 246 Sf[r][c] = ACC_TYPE(0.0); 247 } 248 } 249 250 if (SHMEM_STAGING != 0) { 251 barrier(); 252 #ifndef MMQ 253 [[unroll]] for (uint32_t idx = 0; idx < Bc * HSK / 4; idx += gl_WorkGroupSize.x) { 254 uint32_t d = (idx + tid) % (HSK / 4); 255 uint32_t c = (idx + tid) / (HSK / 4); 256 if (idx + gl_WorkGroupSize.x <= Bc * HSK / 4 || c < Bc) { 257 FLOAT_TYPEV4 K_Tf = FLOAT_TYPEV4(0); 258 uint32_t kcol; 259 if (fa_kv_index(j * Bc + c, kcol)) { 260 if (USE_DECODE_K) { 261 uint coord = kcol * k_stride * BLOCK_SIZE_K + 4 * d; 262 uint ib = coord / BLOCK_SIZE_K; 263 uint iqs = (coord % BLOCK_SIZE_K); 264 K_Tf = dequantize4(ib, iqs, k_offset, BINDING_IDX_K); 265 } else { 266 K_Tf = FLOAT_TYPEV4(data_kv4[k_offset / 4 + kcol * k_stride / 4 + d]); 267 } 268 } 269 270 kvsh[c * kvsh_stride + d] = K_Tf; 271 } 272 } 273 #else // MMQ 274 const uint ints_per_block = 8u / fa_quant_r_mmq(FaTypeK); 275 const uint quant_iters = Bc * HSK / 32 * ints_per_block; 276 [[unroll]] for (uint32_t idx = 0; idx < quant_iters; idx += gl_WorkGroupSize.x) { 277 const uint32_t iqs = (idx + tid) % ints_per_block; 278 const uint32_t ib = (idx + tid) / ints_per_block; 279 const uint32_t c = ib / (HSK / 32); 280 const uint32_t block = ib % (HSK / 32); 281 if (idx + gl_WorkGroupSize.x <= quant_iters || c < Bc) { 282 const uint buf_ib = c * qf_stride + block; 283 uint32_t kcol; 284 if (fa_kv_index(j * Bc + c, kcol)) { 285 const uint global_ib = kcol * k_stride + block; 286 k_block_to_shmem(buf_ib, global_ib, iqs, k_offset); 287 } else { 288 k_block_to_shmem_zero(buf_ib, iqs); 289 } 290 } 291 } 292 #endif // MMQ 293 barrier(); 294 } 295 296 #ifndef MMQ 297 // More d iterations means Q register caching becomes relevant 298 // Few iterations means the additional registers needed are worse than the speed-up from caching 299 if (HSK_per_thread / 4 > 4) { 300 [[unroll]] for (uint32_t d = 0; d < HSK_per_thread / 4; ++d) { 301 FLOAT_TYPEV4 Q_cache[rows_per_thread]; 302 [[unroll]] for (uint32_t r = 0; r < rows_per_thread; ++r) { 303 Q_cache[r] = Qf[tile_row(r) * qf_stride + d * D_split + d_tid]; 304 } 305 306 [[unroll]] for (uint32_t c = 0; c < cols_per_thread; ++c) { 307 uint32_t kcol; 308 bool kv_active = fa_kv_index(j * Bc + c * cols_per_iter + col_tid, kcol); 309 if (!kv_active) { 310 continue; 311 } 312 313 FLOAT_TYPEV4 K_Tf; 314 if (SHMEM_STAGING != 0) { 315 K_Tf = kvsh[(c * cols_per_iter + col_tid) * kvsh_stride + (d * D_split + d_tid)]; 316 } else if (USE_DECODE_K) { 317 uint coord = kcol * k_stride * BLOCK_SIZE_K + 4 * (d * D_split + d_tid); 318 uint ib = coord / BLOCK_SIZE_K; 319 uint iqs = (coord % BLOCK_SIZE_K); 320 K_Tf = dequantize4(ib, iqs, k_offset, BINDING_IDX_K); 321 } else { 322 K_Tf = FLOAT_TYPEV4(data_kv4[k_offset / 4 + kcol * k_stride / 4 + d * D_split + d_tid]); 323 } 324 [[unroll]] for (uint32_t r = 0; r < rows_per_thread; ++r) { 325 Sf[r][c] = dot_product(Q_cache[r], K_Tf, Sf[r][c]); 326 } 327 } 328 } 329 } else { 330 [[unroll]] for (uint32_t c = 0; c < cols_per_thread; ++c) { 331 uint32_t kcol; 332 bool kv_active = fa_kv_index(j * Bc + c * cols_per_iter + col_tid, kcol); 333 if (!kv_active) { 334 continue; 335 } 336 337 [[unroll]] for (uint32_t d = 0; d < HSK_per_thread / 4; ++d) { 338 FLOAT_TYPEV4 K_Tf; 339 if (SHMEM_STAGING != 0) { 340 K_Tf = kvsh[(c * cols_per_iter + col_tid) * kvsh_stride + (d * D_split + d_tid)]; 341 } else if (USE_DECODE_K) { 342 uint coord = kcol * k_stride * BLOCK_SIZE_K + 4 * (d * D_split + d_tid); 343 uint ib = coord / BLOCK_SIZE_K; 344 uint iqs = (coord % BLOCK_SIZE_K); 345 K_Tf = dequantize4(ib, iqs, k_offset, BINDING_IDX_K); 346 } else { 347 K_Tf = FLOAT_TYPEV4(data_kv4[k_offset / 4 + kcol * k_stride / 4 + d * D_split + d_tid]); 348 } 349 [[unroll]] for (uint32_t r = 0; r < rows_per_thread; ++r) { 350 Sf[r][c] = dot_product(Qf[tile_row(r) * qf_stride + d * D_split + d_tid], K_Tf, Sf[r][c]); 351 } 352 } 353 } 354 } 355 #else // MMQ 356 const uint hsk4 = HSK_per_thread / 4; 357 const uint d_per_step = (hsk4 % 8 == 0) ? 8 : 358 (hsk4 % 4 == 0) ? 4 : 359 (hsk4 % 2 == 0) ? 2 : 1; 360 361 [[unroll]] for (uint32_t c = 0; c < cols_per_thread; ++c) { 362 uint32_t kcol; 363 if (!fa_kv_index(j * Bc + c * cols_per_iter + col_tid, kcol)) { 364 continue; 365 } 366 367 [[unroll]] for (uint32_t d_block = 0; d_block < HSK_per_thread / 4; d_block += d_per_step) { 368 int32_t k_quants[d_per_step]; 369 ACC_TYPEV2 k_dm; 370 371 // Q4_*/Q5_* take the block-8 fast path when one step covers a full 372 // block; Q8_0 always goes through the per-int get_k_qs* helpers 373 // (its qs is byte-packed, not nibble-packed). 374 const bool block8_fast = (d_per_step == 8) && (FaTypeK != GGML_TYPE_Q8_0); 375 376 if (SHMEM_STAGING != 0) { 377 const uint k_block_idx = (d_tid * (HSK_per_thread / 4) + d_block) / 8; 378 const uint buf_ib = (c * cols_per_iter + col_tid) * qf_stride + k_block_idx; 379 k_dm = ACC_TYPEV2(kblocksh[buf_ib].dm); 380 381 if (block8_fast) { 382 const bool has_qh = (FaTypeK == GGML_TYPE_Q5_0) || (FaTypeK == GGML_TYPE_Q5_1); 383 [[unroll]] for (uint32_t d = 0; d < 4; d++) { 384 uint vui = kblocksh[buf_ib].qs[d]; 385 k_quants[d ] = int32_t( vui & 0x0F0F0F0F); 386 k_quants[d + 4] = int32_t((vui >> 4) & 0x0F0F0F0F); 387 if (has_qh) { 388 uint qh_lo = (kblocksh[buf_ib].qh >> (d * 4)) & 0xF; 389 uint qh_hi = (kblocksh[buf_ib].qh >> (d * 4 + 16)) & 0xF; 390 k_quants[d ] |= int32_t((qh_lo * 0x02040810u) & 0x10101010u); 391 k_quants[d + 4] |= int32_t((qh_hi * 0x02040810u) & 0x10101010u); 392 } 393 } 394 } else { 395 [[unroll]] for (uint32_t d = 0; d < d_per_step; d++) { 396 k_quants[d] = get_k_qs_shmem(buf_ib, (d_tid * (HSK_per_thread / 4) + d_block) % 8 + d); 397 } 398 } 399 } else { 400 const uint coord = kcol * k_stride * BLOCK_SIZE_K + 4 * (d_tid * (HSK_per_thread / 4) + d_block); 401 const uint ib = coord / BLOCK_SIZE_K; 402 const uint iqs = (coord % BLOCK_SIZE_K); 403 404 k_dm = ACC_TYPEV2(get_k_scale(ib, k_offset)); 405 406 if (block8_fast) { 407 fa_k_qs_block8 blk = get_k_qs_block8(ib, k_offset); 408 [[unroll]] for (uint32_t d = 0; d < 8; d++) { 409 k_quants[d] = blk.qs[d]; 410 } 411 } else { 412 [[unroll]] for (uint32_t d = 0; d < d_per_step; d++) { 413 k_quants[d] = get_k_qs(ib, iqs + d * 4, k_offset); 414 } 415 } 416 } 417 418 [[unroll]] for (uint32_t r = 0; r < rows_per_thread; ++r) { 419 const uint qib = tile_row(r) * qf_stride + (d_tid * (HSK_per_thread / 4) + d_block) / 8; 420 const uint qiqs = (d_tid * (HSK_per_thread / 4) + d_block) % 8; 421 422 int32_t acc = 0; 423 [[unroll]] for (uint32_t d = 0; d < d_per_step; d++) { 424 acc += dotPacked4x8EXT(Qf[qib].qs[qiqs + d], k_quants[d]); 425 } 426 427 Sf[r][c] += ACC_TYPE(acc) * ACC_TYPE(Qf[qib].ds.x) * k_dm.x; 428 if ((d_tid * (HSK_per_thread / 4) + d_block) % 8 == 0) { 429 Sf[r][c] += k_dot_correction(qib, k_dm); 430 } 431 } 432 } 433 } 434 #endif // MMQ 435 436 [[unroll]] for (uint32_t c = 0; c < cols_per_thread; ++c) { 437 // Compute sum across the D_split 438 [[unroll]] for (uint s = D_split / 2; s > 0; s >>= 1) { 439 [[unroll]] for (uint32_t r = 0; r < rows_per_thread; ++r) { 440 Sf[r][c] += subgroupShuffleXor(Sf[r][c], s); 441 } 442 } 443 } 444 445 if (LOGIT_SOFTCAP) { 446 [[unroll]] for (uint32_t r = 0; r < rows_per_thread; ++r) { 447 [[unroll]] for (uint32_t c = 0; c < cols_per_thread; ++c) { 448 Sf[r][c] = ACC_TYPE(p.logit_softcap * tanh(Sf[r][c])); 449 } 450 } 451 } 452 453 if (MASK_ENABLE && mask_opt_bits != MASK_OPT_ALL_ZERO) { 454 [[unroll]] for (uint32_t c = 0; c < cols_per_thread; ++c) { 455 [[unroll]] for (uint32_t r = 0; r < rows_per_thread; ++r) { 456 FLOAT_TYPE mvf = masksh[(c * cols_per_iter + col_tid) * masksh_stride + tile_row(r)]; 457 458 Sf[r][c] += slope[r]*mvf; 459 } 460 } 461 } 462 463 float eMf[rows_per_thread]; 464 [[unroll]] for (uint32_t r = 0; r < rows_per_thread; ++r) { 465 float rowmaxf = NEG_FLT_MAX_OVER_2; 466 [[unroll]] for (uint32_t c = 0; c < cols_per_thread; ++c) { 467 if (KV_bounds_check && j * Bc + c * cols_per_iter + col_tid >= KV) { 468 continue; 469 } 470 rowmaxf = max(rowmaxf, float(Sf[r][c])); 471 } 472 rowmaxf += FATTN_KQ_MAX_OFFSET; 473 float Moldf = Mf[r]; 474 475 // M = max(rowmax, Mold) 476 // P = e^(S - M) 477 // eM = e^(Mold - M) 478 Mf[r] = max(rowmaxf, Moldf); 479 eMf[r] = exp(Moldf - Mf[r]); 480 Lf[r] = eMf[r]*Lf[r]; 481 } 482 483 [[unroll]] for (uint32_t d = 0; d < HSV_per_thread / 4; ++d) { 484 [[unroll]] for (uint32_t r = 0; r < rows_per_thread; ++r) { 485 Of[r][d] = FLOAT_TYPE(eMf[r]) * Of[r][d]; 486 } 487 } 488 489 if (SHMEM_STAGING != 0) { 490 barrier(); 491 [[unroll]] for (uint32_t idx = 0; idx < Bc * HSV / 4; idx += gl_WorkGroupSize.x) { 492 uint32_t d = (idx + tid) % (HSV / 4); 493 uint32_t c = (idx + tid) / (HSV / 4); 494 if (idx + gl_WorkGroupSize.x <= Bc * HSV / 4 || c < Bc) { 495 FLOAT_TYPEV4 V_Tf = FLOAT_TYPEV4(0); 496 uint32_t vcol; 497 if (fa_kv_index(j * Bc + c, vcol)) { 498 if (USE_DECODE_V) { 499 uint coord = vcol * v_stride * BLOCK_SIZE_V + 4 * d; 500 uint ib = coord / BLOCK_SIZE_V; 501 uint iqs = (coord % BLOCK_SIZE_V); 502 V_Tf = dequantize4(ib, iqs, v_offset, BINDING_IDX_V); 503 } else { 504 V_Tf = FLOAT_TYPEV4(data_vv4[v_offset / 4 + vcol * v_stride / 4 + d]); 505 } 506 } 507 508 kvsh[c * kvsh_stride + d] = V_Tf; 509 } 510 } 511 barrier(); 512 } 513 514 [[unroll]] for (uint32_t c = 0; c < cols_per_thread; ++c) { 515 uint32_t vcol; 516 bool kv_active = fa_kv_index(j * Bc + c * cols_per_iter + col_tid, vcol); 517 if (!kv_active) { 518 continue; 519 } 520 521 FLOAT_TYPE Pf[rows_per_thread]; 522 [[unroll]] for (uint32_t r = 0; r < rows_per_thread; ++r) { 523 Pf[r] = FLOAT_TYPE(exp(float(Sf[r][c]) - Mf[r])); 524 Lf[r] += Pf[r]; 525 } 526 527 [[unroll]] for (uint32_t d = 0; d < HSV_per_thread / 4; ++d) { 528 FLOAT_TYPEV4 Vf; 529 if (SHMEM_STAGING != 0) { 530 Vf = kvsh[(c * cols_per_iter + col_tid) * kvsh_stride + (d * D_split + d_tid)]; 531 } else if (USE_DECODE_V) { 532 uint coord = vcol * v_stride * BLOCK_SIZE_V + 4 * (d * D_split + d_tid); 533 uint ib = coord / BLOCK_SIZE_V; 534 uint iqs = (coord % BLOCK_SIZE_V); 535 Vf = dequantize4(ib, iqs, v_offset, BINDING_IDX_V); 536 } else { 537 Vf = FLOAT_TYPEV4(data_vv4[v_offset / 4 + vcol * v_stride / 4 + d * D_split + d_tid]); 538 } 539 [[unroll]] for (uint32_t r = 0; r < rows_per_thread; ++r) { 540 Of[r][d] += FLOAT_TYPEV4(Pf[r] * Vf); 541 } 542 } 543 } 544 } 545 546 // prevent race on tmpsh 547 barrier(); 548 549 // reduce across threads 550 551 [[unroll]] for (uint32_t r = 0; r < rows_per_thread; ++r) { 552 float rowmaxf = Mf[r]; 553 554 // Compute max across the row 555 if (SubGroupSize > 0) { 556 [[unroll]] for (uint s = D_split; s < SubGroupSize; s *= 2) { 557 rowmaxf = max(rowmaxf, subgroupShuffleXor(rowmaxf, s)); 558 } 559 if (row_split == 1) { 560 // Reduce inside workgroup with shmem 561 barrier(); 562 if (gl_SubgroupInvocationID == d_tid) { 563 tmpsh[gl_SubgroupID * D_split + d_tid] = rowmaxf; 564 } 565 barrier(); 566 rowmaxf = tmpsh[d_tid]; 567 [[unroll]] for (uint32_t s = 1; s < num_subgroups; ++s) { 568 rowmaxf = max(rowmaxf, tmpsh[s * D_split + d_tid]); 569 } 570 } 571 } else { 572 barrier(); 573 tmpsh[tid] = rowmaxf; 574 barrier(); 575 [[unroll]] for (int s = int(threads_per_rowgroup) / 2; s >= D_split; s >>= 1) { 576 if (rowgroup_tid < s) { 577 tmpsh[tid] = max(tmpsh[tid], tmpsh[tid ^ s]); 578 } 579 barrier(); 580 } 581 rowmaxf = tmpsh[row_tid * threads_per_rowgroup + d_tid]; 582 } 583 584 float Moldf = Mf[r]; 585 586 // M = max(rowmax, Mold) 587 // eM = e^(Mold - M) 588 Mf[r] = max(rowmaxf, Moldf); 589 float eMf = exp(Moldf - Mf[r]); 590 591 Lf[r] = eMf*Lf[r]; 592 593 // Compute sum across the row 594 if (SubGroupSize > 0) { 595 [[unroll]] for (uint s = D_split; s < SubGroupSize; s *= 2) { 596 Lf[r] += subgroupShuffleXor(Lf[r], s); 597 } 598 if (row_split == 1) { 599 barrier(); 600 if (gl_SubgroupInvocationID == d_tid) { 601 tmpsh[gl_SubgroupID * D_split + d_tid] = Lf[r]; 602 } 603 barrier(); 604 Lf[r] = tmpsh[d_tid]; 605 [[unroll]] for (uint32_t s = 1; s < num_subgroups; ++s) { 606 Lf[r] += tmpsh[s * D_split + d_tid]; 607 } 608 } 609 } else { 610 barrier(); 611 tmpsh[tid] = Lf[r]; 612 barrier(); 613 [[unroll]] for (int s = int(threads_per_rowgroup) / 2; s >= D_split; s >>= 1) { 614 if (rowgroup_tid < s) { 615 tmpsh[tid] = tmpsh[tid] + tmpsh[tid ^ s]; 616 } 617 barrier(); 618 } 619 Lf[r] = tmpsh[row_tid * threads_per_rowgroup + d_tid]; 620 } 621 622 [[unroll]] for (uint32_t d = 0; d < HSV_per_thread / 4; ++d) { 623 Of[r][d] = FLOAT_TYPE(eMf) * Of[r][d]; 624 625 if (SubGroupSize > 0) { 626 [[unroll]] for (uint s = D_split; s < SubGroupSize; s *= 2) { 627 if (!OLD_AMD_WINDOWS) { 628 Of[r][d] += subgroupShuffleXor(Of[r][d], s); 629 } else { 630 // Something about f16vec4 subgroupShuffleXor is broken on AMD Windows RDNA2 and below. 631 // Shuffle full vec4 as workaround. 632 // See https://github.com/ggml-org/llama.cpp/issues/19881#issuecomment-`3958643697` 633 Of[r][d] += FLOAT_TYPEV4(subgroupShuffleXor(vec4(Of[r][d]), s)); 634 } 635 } 636 if (row_split == 1) { 637 barrier(); 638 if (gl_SubgroupInvocationID == d_tid) { 639 tmpshv4[gl_SubgroupID * D_split + d_tid] = Of[r][d]; 640 } 641 barrier(); 642 Of[r][d] = tmpshv4[d_tid]; 643 [[unroll]] for (uint32_t s = 1; s < num_subgroups; ++s) { 644 Of[r][d] += tmpshv4[s * D_split + d_tid]; 645 } 646 } 647 } else { 648 barrier(); 649 tmpshv4[tid] = Of[r][d]; 650 barrier(); 651 [[unroll]] for (int s = int(threads_per_rowgroup) / 2; s >= D_split; s >>= 1) { 652 if (rowgroup_tid < s) { 653 Of[r][d] += tmpshv4[tid ^ s]; 654 tmpshv4[tid] = Of[r][d]; 655 } 656 barrier(); 657 } 658 Of[r][d] = tmpshv4[row_tid * threads_per_rowgroup + d_tid]; 659 } 660 } 661 } 662 663 664 // If there is split_k, then the split_k resolve shader does the final 665 // division by L. Store the intermediate O value and per-row m and L values. 666 if (p.k_num > 1) { 667 if (p.gqa_ratio > 1) { 668 // note: O and Q have swapped coord 1,2. 669 uint32_t o_offset = HSV * p.ne1 * (split_k_index + p.k_num * (gqa_iq1 + p.ne2 * iq3)) / 4; 670 671 [[unroll]] for (uint32_t r = 0; r < rows_per_thread; ++r) { 672 const uint row = tile_row(r); 673 if (row < N) { 674 [[unroll]] for (uint32_t d = 0; d < HSV_per_thread / 4; ++d) { 675 gqaStore(row, d * D_split + d_tid, Of[r][d], o_offset, iq2, N); 676 } 677 } 678 } 679 680 o_offset = HSV * p.ne1 * p.k_num * p.ne2 * p.ne3 + p.ne1 * 2 * (split_k_index + p.k_num * (gqa_iq1 + p.ne2 * iq3)); 681 [[unroll]] for (uint32_t r = 0; r < rows_per_thread; ++r) { 682 const uint row = tile_row(r); 683 if (row < N) { 684 perElemOpStoreCol0(row, 0u, ACC_TYPE(Lf[r]), o_offset, iq2, N); 685 perElemOpStoreCol0(row, 0u, ACC_TYPE(Mf[r]), o_offset + p.ne1, iq2, N); 686 } 687 } 688 } else { 689 [[unroll]] for (uint32_t r = 0; r < rows_per_thread; ++r) { 690 const uint row = tile_row(r); 691 const uint global_row = i * Br + row; 692 693 if (global_row < N) { 694 uint32_t o_offset = HSV * p.ne1 * (split_k_index + p.k_num * (global_row + p.ne2 * iq3)) / 4; 695 696 [[unroll]] for (uint32_t d = 0; d < HSV_per_thread / 4; ++d) { 697 data_ov4[o_offset + iq2 * HSV/4 + d * D_split + d_tid] = D_TYPEV4(Of[r][d]); 698 } 699 } 700 701 if (global_row < N && d_tid == 0 && col_tid == 0) { 702 uint32_t lm_offset = HSV * p.ne1 * p.k_num * p.ne2 * p.ne3 + p.ne1 * 2 * (split_k_index + p.k_num * (global_row + p.ne2 * iq3)); 703 data_o[lm_offset + iq2] = D_TYPE(Lf[r]); 704 data_o[lm_offset + p.ne1 + iq2] = D_TYPE(Mf[r]); 705 } 706 } 707 } 708 return; 709 } 710 711 if ((p.mask_n_head_log2 & SINK_ENABLE_BIT) != 0) { 712 [[unroll]] for (uint32_t r = 0; r < rows_per_thread; ++r) { 713 float sink = perElemOpGetSink(tile_row(r), 0u, ACC_TYPE(0), iq2); 714 715 float ms = 1.0f; 716 float vs = 1.0f; 717 718 if (sink > Mf[r]) { 719 ms = exp(Mf[r] - sink); 720 721 [[unroll]] for (uint32_t d = 0; d < HSV_per_thread / 4; ++d) { 722 Of[r][d] *= FLOAT_TYPE(ms); 723 } 724 } else { 725 vs = exp(sink - Mf[r]); 726 } 727 728 Lf[r] = Lf[r]*ms + vs; 729 } 730 } 731 732 float Lfrcp[rows_per_thread]; 733 [[unroll]] for (uint32_t r = 0; r < rows_per_thread; ++r) { 734 Lfrcp[r] = (Lf[r] == 0.0) ? 0.0 : (1.0 / Lf[r]); 735 } 736 737 [[unroll]] for (uint32_t d = 0; d < HSV_per_thread / 4; ++d) { 738 [[unroll]] for (uint32_t r = 0; r < rows_per_thread; ++r) { 739 Of[r][d] *= FLOAT_TYPE(Lfrcp[r]); 740 #if defined(FLOAT_TYPE_MAX) 741 Of[r][d] = clamp(Of[r][d], -FLOAT_TYPE_MAX, FLOAT_TYPE_MAX); 742 #endif 743 } 744 } 745 746 uint32_t o_offset = (gqa_iq1*p.ne1*HSV + iq3*p.ne2*p.ne1*HSV) / 4; 747 748 if (p.gqa_ratio > 1) { 749 [[unroll]] for (uint32_t r = 0; r < rows_per_thread; ++r) { 750 const uint row = tile_row(r); 751 if (row < N) { 752 [[unroll]] for (uint32_t d = 0; d < HSV_per_thread / 4; ++d) { 753 gqaStore(row, d * D_split + d_tid, Of[r][d], o_offset, iq2, N); 754 } 755 } 756 } 757 } else { 758 [[unroll]] for (uint32_t r = 0; r < rows_per_thread; ++r) { 759 const uint row = tile_row(r); 760 if (i * Br + row < N) { 761 [[unroll]] for (uint32_t d = 0; d < HSV_per_thread / 4; ++d) { 762 data_ov4[o_offset + (iq2 * HSV + (i * Br + row) * p.ne1 * HSV) / 4 + d * D_split + d_tid] = D_TYPEV4(Of[r][d]); 763 } 764 } 765 } 766 } 767 }