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_cm1.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_cm1.comp` 663 lines ยท 30175 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_float16 : require 7 #extension GL_EXT_shader_explicit_arithmetic_types_int32 : require 8 9 #if defined(BFLOAT16) 10 #extension GL_EXT_bfloat16 : enable 11 #endif 12 13 #extension GL_KHR_shader_subgroup_basic : enable 14 #extension GL_KHR_shader_subgroup_arithmetic : enable 15 #extension GL_KHR_shader_subgroup_vote : enable 16 #extension GL_KHR_memory_scope_semantics : enable 17 #extension GL_KHR_cooperative_matrix : enable 18 19 #include "types.glsl" 20 #include "flash_attn_base.glsl" 21 #if !defined(BFLOAT16) 22 #include "flash_attn_dequant.glsl" 23 #endif 24 25 // These need to be supported N,M values for a MatBc x MatBr x 16 coopmatmuladd 26 const uint32_t MatBr = 16; 27 const uint32_t MatBc = 16; 28 29 const uint32_t rows_per_thread = Br / row_split; 30 const uint32_t cols_per_iter = gl_WorkGroupSize.x / row_split; 31 const uint32_t cols_per_thread = Bc / cols_per_iter; 32 33 34 layout (binding = 0) readonly buffer Q {float data_q[];}; 35 layout (binding = 0) readonly buffer QV4 {vec4 data_qv4[];}; 36 layout (binding = 1) readonly buffer K {FLOAT_TYPE data_k[];}; 37 layout (binding = 1) readonly buffer KV4 {FLOAT_TYPEV4 data_kv4[];}; 38 layout (binding = 2) readonly buffer V {FLOAT_TYPE data_v[];}; 39 layout (binding = 2) readonly buffer VV4 {FLOAT_TYPEV4 data_vv4[];}; 40 layout (binding = 3) readonly buffer M {float16_t data_m[];}; 41 42 shared float tmpsh[row_split]; 43 44 const uint32_t qstride = HSK_pad / 4 + 2; 45 shared FLOAT_TYPEV4 Qf[Br * qstride]; 46 47 const uint psh_stride = Br / 4 + 2; 48 shared FLOAT_TYPEV4 Psh[Bc * psh_stride]; 49 50 // Avoid padding for hsk==256 to make it fit in 48KB shmem. 51 const uint32_t sfshstride = (HSK <= 128) ? (Br / 4 + 2) : Br / 4; 52 shared ACC_TYPEV4 sfsh[Bc * sfshstride]; 53 54 const uint32_t D_pad = HSK_pad > HSV_pad ? HSK_pad : HSV_pad; 55 const uint32_t kvsh_stride = (SHMEM_STAGING != 0 ? D_pad : MatBr) / 4 + 2; 56 const uint v_cols = MatBc / 4 * row_split; // total cols, 4 vec4s per MatBc * number of subgroups 57 const uint vsh_stride = v_cols; 58 shared FLOAT_TYPEV4 kvsh[(kvsh_stride >= vsh_stride) ? (Bc * kvsh_stride) : (Bc * vsh_stride)]; 59 60 const uint32_t osh_stride = row_split * MatBr / 4; 61 shared O_TYPEV4 pvsh[MatBc * osh_stride]; 62 63 shared ACC_TYPE slope[Br]; 64 65 void main() { 66 #ifdef NEEDS_INIT_IQ_SHMEM 67 if (fa_type_needs_shmem(FaTypeK) || fa_type_needs_shmem(FaTypeV)) { 68 init_iq_shmem(gl_WorkGroupSize); 69 } 70 #endif 71 72 init_indices(); 73 74 const uint32_t tid = gl_LocalInvocationIndex; 75 76 const uint32_t threads_per_rowgroup = gl_WorkGroupSize.x / row_split; 77 const uint32_t d_per_thread = (HSV/4 + threads_per_rowgroup - 1) / threads_per_rowgroup; 78 const uint32_t row_tid = gl_LocalInvocationIndex / threads_per_rowgroup; 79 const uint32_t col_tid = gl_LocalInvocationIndex % threads_per_rowgroup; 80 81 #define tile_row(r) (row_tid * rows_per_thread + (r)) 82 83 // Zero-initialize shared memory for Q/K when HSK is not a multiple of 16 (HSK_pad > HSK). 84 if ((HSK % 16) != 0) { 85 [[unroll]] for (uint i = 0; i < Br * qstride; i += gl_WorkGroupSize.x) { 86 if (i + tid < Br * qstride) { 87 Qf[i + tid] = FLOAT_TYPEV4(0); 88 } 89 } 90 barrier(); 91 } 92 93 uint32_t q_offset = gqa_iq1*p.nb01 + (iq2*p.nb02+iq3*p.nb03) / 4; 94 95 [[unroll]] for (uint32_t idx = 0; idx < Br * HSK / 4; idx += gl_WorkGroupSize.x) { 96 uint32_t d = (idx + tid) % (HSK / 4); 97 uint32_t r = (idx + tid) / (HSK / 4); 98 if (r < Br && d < HSK / 4 && 99 i * Br + r < N) { 100 Qf[r * qstride + d] = FLOAT_TYPEV4(data_qv4[q_offset / 4 + (i * Br + r) * q_stride / 4 + d] * p.scale); 101 } 102 } 103 barrier(); 104 105 O_TYPEV4 Of[rows_per_thread][d_per_thread]; 106 [[unroll]] for (uint32_t r = 0; r < rows_per_thread; ++r) { 107 [[unroll]] for (uint32_t d = 0; d < d_per_thread; ++d) { 108 Of[r][d] = O_TYPEV4(0.0); 109 } 110 } 111 112 float Lf[rows_per_thread], Mf[rows_per_thread]; 113 114 // Use -FLT_MAX/2 rather than -inf to reduce the possibility of NaNs, e.g. when computing Mold-M. 115 const float NEG_FLT_MAX_OVER_2 = uintBitsToFloat(0xFEFFFFFF); 116 117 [[unroll]] for (uint32_t r = 0; r < rows_per_thread; ++r) { 118 Lf[r] = 0; 119 Mf[r] = NEG_FLT_MAX_OVER_2; 120 } 121 122 // ALiBi 123 if (p.max_bias > 0.0f) { 124 if (tid < Br) { 125 uint r = tid; 126 slope[r] = perElemOpComputeSlope(r, col_tid, ACC_TYPE(0), iq2); 127 } 128 } else { 129 if (tid < Br) { 130 uint r = tid; 131 slope[r] = ACC_TYPE(1.0); 132 } 133 } 134 135 const uint32_t mo_stride = CEIL_DIV(KV, 16 * Bc); 136 // mo_offset will point to the tile starting at row i*Br and col 0 137 uint32_t mo_offset = mo_stride * i; 138 139 // FaBlockBytesK/V == 2 for f16 (sizeof f16) and == 16 for f32 (vec4) and == ggml block size for quants. 140 uint32_t k_offset = (ik2*p.nb12 + ik3*p.nb13) / FaBlockBytesK; 141 uint32_t v_offset = (iv2*p.nb22 + iv3*p.nb23) / FaBlockBytesV; 142 uint32_t m_offset = gqa_iq1*KV; 143 if (p.nem2 != 1 || p.nem3 != 1) { 144 m_offset += ((iq3 % p.nem3) * p.nem2 + (iq2 % p.nem2)) * p.nem1 * KV; 145 mo_offset += ((iq3 % p.nem3) * p.nem2 + (iq2 % p.nem2)) * CEIL_DIV(p.nem1, Br) * mo_stride; 146 } 147 148 uint32_t mask_opt = 0; 149 uint32_t mask_opt_idx = ~0; 150 uint32_t mask_opt_bits = 0; 151 f16vec4 mask_cache[Bc * Br / 4 / WorkGroupSize]; 152 153 [[dont_unroll]] 154 for (uint32_t j = start_j; j < end_j; ++j) { 155 156 [[unroll]] for (uint32_t idx = 0; idx < mask_cache.length(); ++idx) { 157 mask_cache[idx] = f16vec4(0); 158 } 159 160 if (MASK_ENABLE) { 161 if (USE_MASK_OPT && mask_opt_idx != j / 16) { 162 mask_opt_idx = j / 16; 163 mask_opt = data_mask_opt[mo_offset + mask_opt_idx]; 164 } 165 mask_opt_bits = (mask_opt >> ((j % 16) * 2)) & 0x3; 166 if (mask_opt_bits == MASK_OPT_ALL_NEG_INF) { 167 // skip this block 168 continue; 169 } 170 // Only load if the block is not all zeros 171 if (mask_opt_bits != MASK_OPT_ALL_ZERO) { 172 bool nem1_bounds_check = !(p.gqa_ratio > 1) && (p.nem1 % Br) != 0; 173 174 float max_mask = NEG_FLT_MAX_OVER_2; 175 [[unroll]] for (uint32_t idx = 0; idx < Bc * Br / 4; idx += gl_WorkGroupSize.x) { 176 uint32_t c = (idx + tid) / (Br / 4); 177 uint32_t r = (idx + tid) % (Br / 4); 178 if (idx + tid < Bc * Br / 4 || idx + gl_WorkGroupSize.x <= Bc * Br / 4) { 179 uint32_t kcol; 180 bool kv_active = fa_kv_index(j * Bc + c, kcol); 181 if (kv_active) { 182 f16vec4 m; 183 if (USE_SPARSE) { 184 // sparse is gqa-gated (m_stride == 0): all four rows share the value 185 FLOAT_TYPE mv = FLOAT_TYPE(data_m[m_offset + kcol]); 186 m = f16vec4(mv); 187 max_mask = max(max_mask, float(mv)); 188 } else if (!nem1_bounds_check || i * Br + r * 4 + 3 < p.nem1) { 189 m = f16vec4(data_m[m_offset + (i * Br + r * 4 ) * m_stride + (j * Bc + c)], 190 data_m[m_offset + (i * Br + r * 4 + 1) * m_stride + (j * Bc + c)], 191 data_m[m_offset + (i * Br + r * 4 + 2) * m_stride + (j * Bc + c)], 192 data_m[m_offset + (i * Br + r * 4 + 3) * m_stride + (j * Bc + c)]); 193 max_mask = max(max(max(max(max_mask, float(m[0])), float(m[1])), float(m[2])), float(m[3])); 194 } else if (i * Br + r * 4 + 2 < p.nem1) { 195 m = f16vec4(data_m[m_offset + (i * Br + r * 4 ) * m_stride + (j * Bc + c)], 196 data_m[m_offset + (i * Br + r * 4 + 1) * m_stride + (j * Bc + c)], 197 data_m[m_offset + (i * Br + r * 4 + 2) * m_stride + (j * Bc + c)], 198 0.0); 199 max_mask = max(max(max(max_mask, float(m[0])), float(m[1])), float(m[2])); 200 } else if (i * Br + r * 4 + 1 < p.nem1) { 201 m = f16vec4(data_m[m_offset + (i * Br + r * 4 ) * m_stride + (j * Bc + c)], 202 data_m[m_offset + (i * Br + r * 4 + 1) * m_stride + (j * Bc + c)], 203 0.0, 204 0.0); 205 max_mask = max(max(max_mask, float(m[0])), float(m[1])); 206 } else if (i * Br + r * 4 < p.nem1) { 207 m = f16vec4(data_m[m_offset + (i * Br + r * 4 ) * m_stride + (j * Bc + c)], 208 0.0, 209 0.0, 210 0.0); 211 max_mask = max(max_mask, float(m[0])); 212 } else { 213 m = f16vec4(0.0); 214 } 215 mask_cache[idx / WorkGroupSize] = m; 216 } else if (USE_SPARSE) { 217 mask_cache[idx / WorkGroupSize] = f16vec4(NEG_FLT_MAX_OVER_2); 218 } 219 } 220 } 221 // skip the block if the mask is entirely -inf 222 bool all_less = subgroupAll(max_mask <= NEG_FLT_MAX_OVER_2); 223 barrier(); 224 if (gl_SubgroupInvocationID == 0) { 225 tmpsh[gl_SubgroupID] = all_less ? NEG_FLT_MAX_OVER_2 : 0.0f; 226 } 227 barrier(); 228 [[unroll]] for (uint s = 0; s < gl_NumSubgroups; ++s) { 229 max_mask = max(max_mask, tmpsh[s]); 230 } 231 if (max_mask <= NEG_FLT_MAX_OVER_2) { 232 continue; 233 } 234 } 235 } 236 237 if (SHMEM_STAGING != 0) { 238 [[unroll]] for (uint32_t idx = 0; idx < Bc * HSK_pad / 4; idx += gl_WorkGroupSize.x) { 239 uint32_t d = (idx + tid) % (HSK_pad / 4); 240 uint32_t c = (idx + tid) / (HSK_pad / 4); 241 if (idx + gl_WorkGroupSize.x <= Bc * HSK_pad / 4 || c < Bc) { 242 FLOAT_TYPEV4 K_Tf = FLOAT_TYPEV4(0); 243 uint32_t kcol; 244 bool kv_active = fa_kv_index(j * Bc + c, kcol); 245 if (kv_active && (HSK == HSK_pad || d < HSK / 4)) { 246 #if !defined(BFLOAT16) 247 if (USE_DECODE_K) { 248 uint coord = kcol * k_stride * BLOCK_SIZE_K + 4 * d; 249 uint ib = coord / BLOCK_SIZE_K; 250 uint iqs = (coord % BLOCK_SIZE_K); 251 K_Tf = dequantize4(ib, iqs, k_offset, BINDING_IDX_K); 252 } else 253 #endif 254 { 255 K_Tf = FLOAT_TYPEV4(data_kv4[k_offset / 4 + kcol * k_stride / 4 + d]); 256 } 257 } 258 259 kvsh[c * kvsh_stride + d] = K_Tf; 260 } 261 } 262 barrier(); 263 } 264 265 // K * Q^T -> S^T: Bc x HSK_pad * HSK_pad x Br -> Bc x Br 266 // Bc split across workgroup (four subgroups), loop over HSK in chunks of 16: 16 x 16 * 16 x 16 -> 16 x 16 267 // This is written transposed in order to allow for N being 8 if implementations need it 268 coopmat SfMat = coopmat(0); 269 coopmat KMat; 270 coopmat QMat; 271 272 [[unroll]] for (uint32_t d = 0; d < HSK_pad / 16; ++d) { 273 // If SHMEM_STAGING is set, a Bc * HSK_pad size tile of K is loaded to shmem 274 // If not, K is loaded directly from global memory if aligned, otherwise 275 // staged through a Bc * MatBr size staging buffer. 276 // If K is a quant type, then it is always staged for dequantization. 277 if (SHMEM_STAGING == 0) { 278 // For quants we always need to dequant into kvsh; for f16/bf16 we can load 279 // directly from global memory when alignment / bounds allow it. 280 const bool stage_k = USE_DECODE_K || KV_bounds_check || USE_SPARSE || d * 16 + 16 > HSK; 281 if (stage_k) { 282 barrier(); 283 [[unroll]] for (uint32_t idx = 0; idx < Bc * MatBr / 4; idx += gl_WorkGroupSize.x) { 284 uint32_t col_vec = (idx + tid) % (MatBr / 4); 285 uint32_t row = (idx + tid) / (MatBr / 4); 286 if (idx + tid < Bc * MatBr / 4) { 287 FLOAT_TYPEV4 K_Tf = FLOAT_TYPEV4(0); 288 uint32_t kcol; 289 bool kv_active = fa_kv_index(j * Bc + row, kcol); 290 if (kv_active && (HSK == HSK_pad || d * 16 + col_vec * 4 < HSK)) { 291 #if !defined(BFLOAT16) 292 if (USE_DECODE_K) { 293 uint coord = kcol * k_stride * BLOCK_SIZE_K + d * 16 + col_vec * 4; 294 uint ib = coord / BLOCK_SIZE_K; 295 uint iqs = (coord % BLOCK_SIZE_K); 296 K_Tf = dequantize4(ib, iqs, k_offset, BINDING_IDX_K); 297 } else 298 #endif 299 { 300 K_Tf = FLOAT_TYPEV4(data_kv4[k_offset / 4 + kcol * k_stride / 4 + d * 16 / 4 + col_vec]); 301 } 302 } 303 304 kvsh[row * kvsh_stride + col_vec] = K_Tf; 305 } 306 } 307 barrier(); 308 } 309 310 if (stage_k) { 311 uint coord = (gl_SubgroupID * MatBc) * kvsh_stride; 312 coopMatLoad(KMat, kvsh, coord, kvsh_stride, gl_CooperativeMatrixLayoutRowMajor); 313 } else { 314 const uint coord = k_offset / 4 + (j * Bc + gl_SubgroupID * MatBc) * k_stride / 4 + d * 16 / 4; 315 coopMatLoad(KMat, data_kv4, coord, k_stride / 4, gl_CooperativeMatrixLayoutRowMajor); 316 } 317 } else { 318 uint coord = (gl_SubgroupID * MatBc) * kvsh_stride + d * 16 / 4; 319 coopMatLoad(KMat, kvsh, coord, kvsh_stride, gl_CooperativeMatrixLayoutRowMajor); 320 } 321 322 coopMatLoad(QMat, Qf, d * 16 / 4, qstride, gl_CooperativeMatrixLayoutColumnMajor); 323 324 SfMat = coopMatMulAdd(KMat, QMat, SfMat); 325 } 326 327 uint coord = gl_SubgroupID * MatBc * sfshstride; 328 coopMatStore(SfMat, sfsh, coord, sfshstride, gl_CooperativeMatrixLayoutRowMajor); 329 barrier(); 330 331 if (LOGIT_SOFTCAP) { 332 [[unroll]] for (uint32_t idx = 0; idx < Bc * Br / 4; idx += gl_WorkGroupSize.x) { 333 uint32_t c = (idx + tid) / (Br / 4); 334 uint32_t r = (idx + tid) % (Br / 4); 335 if (idx + tid < Bc * Br / 4 || idx + gl_WorkGroupSize.x <= Bc * Br / 4) { 336 sfsh[c * sfshstride + r] = ACC_TYPEV4(p.logit_softcap * tanh(sfsh[c * sfshstride + r])); 337 } 338 } 339 barrier(); 340 } 341 342 if (MASK_ENABLE && mask_opt_bits != MASK_OPT_ALL_ZERO) { 343 [[unroll]] for (uint32_t idx = 0; idx < Bc * Br / 4; idx += gl_WorkGroupSize.x) { 344 uint32_t c = (idx + tid) / (Br / 4); 345 uint32_t r = (idx + tid) % (Br / 4); 346 if (idx + tid < Bc * Br / 4 || idx + gl_WorkGroupSize.x <= Bc * Br / 4) { 347 if (!KV_bounds_check || j * Bc + c < KV) { 348 // Mask nem1 bounds check is handled when loading masks 349 ACC_TYPEV4 masks = ACC_TYPEV4(mask_cache[idx / WorkGroupSize]); 350 ACC_TYPEV4 slopes = ACC_TYPEV4(slope[r * 4], slope[r * 4 + 1], slope[r * 4 + 2], slope[r * 4 + 3]); 351 sfsh[c * sfshstride + r] += slopes * masks; 352 } 353 } 354 } 355 barrier(); 356 } 357 358 float eMf[rows_per_thread]; 359 [[unroll]] for (uint32_t r = 0; r < rows_per_thread; ++r) { 360 const uint r_vec = tile_row(r) / 4; 361 const uint r_comp = tile_row(r) % 4; 362 363 float rowmaxf = NEG_FLT_MAX_OVER_2; 364 [[unroll]] for (uint32_t c = 0; c < cols_per_thread; ++c) { 365 if (KV_bounds_check && j * Bc + c * cols_per_iter + col_tid >= KV) { 366 continue; 367 } 368 rowmaxf = max(rowmaxf, float(sfsh[r_vec + (c * cols_per_iter + col_tid) * sfshstride][r_comp])); 369 } 370 rowmaxf += FATTN_KQ_MAX_OFFSET; 371 float Moldf = Mf[r]; 372 373 // Compute max across the row 374 rowmaxf = subgroupMax(rowmaxf); 375 376 // M = max(rowmax, Mold) 377 // P = e^(S - M) 378 // eM = e^(Mold - M) 379 Mf[r] = max(rowmaxf, Moldf); 380 eMf[r] = exp(Moldf - Mf[r]); 381 382 Lf[r] = eMf[r]*Lf[r]; 383 } 384 385 [[unroll]] for (uint32_t d0 = 0; d0 < HSV / 4; d0 += threads_per_rowgroup) { 386 const uint d_local = d0 / threads_per_rowgroup; 387 [[unroll]] for (uint32_t r = 0; r < rows_per_thread; ++r) { 388 Of[r][d_local] = O_TYPE(eMf[r]) * Of[r][d_local]; 389 } 390 } 391 392 // Calculate and store Pf in Psh 393 [[unroll]] for (uint32_t c = 0; c < cols_per_thread; ++c) { 394 const uint col = c * cols_per_iter + col_tid; 395 396 [[unroll]] for (uint32_t r = 0; r < rows_per_thread; r += 4) { 397 const uint row = tile_row(r); 398 if (KV_bounds_check && j * Bc + col >= KV) { 399 Psh[col * psh_stride + row / 4] = FLOAT_TYPEV4(0.0f); 400 } else { 401 const vec4 mfvec = vec4(Mf[r], Mf[r + 1], Mf[r + 2], Mf[r + 3]); 402 const FLOAT_TYPEV4 Pf = FLOAT_TYPEV4(exp(vec4(sfsh[row / 4 + col * sfshstride]) - mfvec)); 403 [[unroll]] for (uint32_t vec_idx = 0; vec_idx < 4; ++vec_idx) { 404 Lf[r + vec_idx] += Pf[vec_idx]; 405 } 406 Psh[col * psh_stride + row / 4] = Pf; 407 } 408 } 409 } 410 411 if (SHMEM_STAGING != 0) { 412 [[unroll]] for (uint32_t idx = 0; idx < Bc * HSV_pad / 4; idx += gl_WorkGroupSize.x) { 413 uint32_t d = (idx + tid) % (HSV_pad / 4); 414 uint32_t c = (idx + tid) / (HSV_pad / 4); 415 if (idx + gl_WorkGroupSize.x <= Bc * HSV_pad / 4 || c < Bc) { 416 FLOAT_TYPEV4 V_Tf = FLOAT_TYPEV4(0); 417 uint32_t v_row; 418 bool kv_active = fa_kv_index(j * Bc + c, v_row); 419 if (kv_active && (HSV == HSV_pad || d < HSV / 4)) { 420 #if !defined(BFLOAT16) 421 if (USE_DECODE_V) { 422 uint coord = v_row * v_stride * BLOCK_SIZE_V + 4 * d; 423 uint ib = coord / BLOCK_SIZE_V; 424 uint iqs = (coord % BLOCK_SIZE_V); 425 V_Tf = dequantize4(ib, iqs, v_offset, BINDING_IDX_V); 426 } else 427 #endif 428 { 429 V_Tf = FLOAT_TYPEV4(data_vv4[v_offset / 4 + v_row * v_stride / 4 + d]); 430 } 431 } 432 433 kvsh[c * kvsh_stride + d] = V_Tf; 434 } 435 } 436 } 437 barrier(); 438 439 const uint num_hsv_tiles = (HSV + MatBc * row_split - 1) / (MatBc * row_split); // round up 440 441 // Each subgroup handles HSV/4 columns 442 [[unroll]] for (uint32_t hsv_tile = 0; hsv_tile < num_hsv_tiles; ++hsv_tile) { 443 const uint hsv_offset = (hsv_tile * row_split + gl_SubgroupID) * 16; 444 445 coopmat PVMat = coopmat(0); 446 447 // Preload V tiles for [Bc, 16 * num subgroups] 448 const uint v_rows = Bc; 449 const uint v_total = v_rows * v_cols; 450 const uint v_loads_per_thread = v_total / gl_WorkGroupSize.x; 451 452 // If SHMEM_STAGING is set, a Bc * HSV_pad size tile of V is loaded to shmem. 453 // If not, V is loaded directly from global memory if aligned, otherwise 454 // staged through a Bc * MatBr size staging buffer. 455 // If V is a quant type, then it is always staged for dequantization. 456 if (SHMEM_STAGING == 0) { 457 // For quants we always preload via kvsh. For f16/bf16 we only preload when 458 // alignment / bounds force it (otherwise we coopMatLoad direct from data_vv4). 459 const bool stage_v = USE_DECODE_V || KV_bounds_check || USE_SPARSE; 460 if (stage_v) { 461 [[unroll]] for (uint32_t i = 0; i < v_loads_per_thread; ++i) { 462 const uint idx = i * gl_WorkGroupSize.x + tid; 463 const uint row = idx / v_cols; 464 const uint col = idx % v_cols; 465 466 uint32_t v_row; 467 bool kv_active = fa_kv_index(j * Bc + row, v_row); 468 const uint v_col = hsv_tile * MatBc * row_split + col * 4; 469 470 const uint coord = v_row * v_stride * BLOCK_SIZE_V + v_col; 471 const uint ib = coord / BLOCK_SIZE_V; 472 const uint iqs = coord % BLOCK_SIZE_V; 473 474 if (USE_SPARSE ? (kv_active && v_col < HSV) : (!KV_bounds_check || (v_row < KV && v_col < HSV))) { 475 #if !defined(BFLOAT16) 476 if (USE_DECODE_V) { 477 kvsh[row * vsh_stride + col] = dequantize4(ib, iqs, v_offset, BINDING_IDX_V); 478 } else 479 #endif 480 { 481 kvsh[row * vsh_stride + col] = data_vv4[(v_offset + v_row * v_stride + v_col) / 4]; 482 } 483 } else { 484 kvsh[row * vsh_stride + col] = FLOAT_TYPEV4(0.0f); 485 } 486 } 487 } 488 } 489 barrier(); 490 491 const uint o_offset = gl_SubgroupID * MatBr / 4; 492 493 if (hsv_offset < HSV_pad) { 494 [[unroll]] for (uint32_t bc_chunk = 0; bc_chunk < Bc / MatBc; ++bc_chunk) { 495 coopMatLoad(KMat, Psh, bc_chunk * MatBc * psh_stride, psh_stride, gl_CooperativeMatrixLayoutColumnMajor); 496 497 if (SHMEM_STAGING == 0) { 498 if (!USE_DECODE_V && !KV_bounds_check && !USE_SPARSE) { 499 // F16/BF16 values can be loaded directly from global memory 500 const uint v_tile_row = j * Bc + bc_chunk * MatBc; 501 const uint v_tile_offset = v_offset / 4 + v_tile_row * v_stride / 4 + hsv_offset / 4; 502 coopMatLoad(QMat, data_vv4, v_tile_offset, v_stride / 4, gl_CooperativeMatrixLayoutRowMajor); 503 } else { 504 const uint v_tile_offset = bc_chunk * MatBr * v_cols + gl_SubgroupID * (MatBc / 4); 505 coopMatLoad(QMat, kvsh, v_tile_offset, vsh_stride, gl_CooperativeMatrixLayoutRowMajor); 506 } 507 } else { 508 const uint v_tile_offset = bc_chunk * MatBc * kvsh_stride + (hsv_tile * row_split + gl_SubgroupID) * (MatBc / 4); 509 coopMatLoad(QMat, kvsh, v_tile_offset, kvsh_stride, gl_CooperativeMatrixLayoutRowMajor); 510 } 511 512 PVMat = coopMatMulAdd(KMat, QMat, PVMat); 513 } 514 515 // Store PVMat to pvsh and load into Of 516 coopMatStore(PVMat, pvsh, o_offset, osh_stride, gl_CooperativeMatrixLayoutRowMajor); 517 } 518 519 barrier(); 520 521 const uint hsv_per_tile = row_split * MatBc; 522 const uint hsv_base = hsv_tile * hsv_per_tile; 523 const uint d_values_per_tile = hsv_per_tile / 4; 524 525 const uint d_start = hsv_tile * d_values_per_tile; 526 const uint d_end = min(d_start + d_values_per_tile, HSV / 4); 527 528 [[unroll]] for (uint32_t r = 0; r < rows_per_thread; ++r) { 529 const uint row = tile_row(r); 530 531 [[unroll]] for (uint32_t d_local = 0; d_local < d_per_thread; ++d_local) { 532 const uint d = d_local * threads_per_rowgroup + col_tid; 533 const uint hsv_col = 4 * d; 534 535 if (hsv_col >= hsv_base && hsv_col < hsv_base + hsv_per_tile && hsv_col < HSV) { 536 const uint local_hsv = (hsv_col - hsv_base) / 4; 537 Of[r][d_local] += pvsh[row * osh_stride + local_hsv]; 538 } 539 } 540 } 541 } 542 543 barrier(); 544 } 545 546 [[unroll]] for (uint32_t r = 0; r < rows_per_thread; ++r) { 547 Lf[r] = subgroupAdd(Lf[r]); 548 } 549 550 // If there is split_k, then the split_k resolve shader does the final 551 // division by L. Store the intermediate O value and per-row m and L values. 552 if (p.k_num > 1) { 553 if (p.gqa_ratio > 1) { 554 // note: O and Q have swapped coord 1,2. 555 uint32_t o_offset = HSV * p.ne1 * (split_k_index + p.k_num * (gqa_iq1 + p.ne2 * iq3)) / 4; 556 557 [[unroll]] for (uint32_t r = 0; r < rows_per_thread; ++r) { 558 if (tile_row(r) < N) { 559 [[unroll]] for (uint32_t d0 = 0; d0 < HSV / 4; d0 += threads_per_rowgroup) { 560 const uint d = d0 + col_tid; 561 if (d >= HSV/4) break; 562 const uint d_local = d0 / threads_per_rowgroup; 563 gqaStore(tile_row(r), d, Of[r][d_local], o_offset, iq2, N); 564 } 565 } 566 } 567 568 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)); 569 [[unroll]] for (uint32_t r = 0; r < rows_per_thread; ++r) { 570 if (tile_row(r) < N) { 571 perElemOpStoreCol0(tile_row(r), 0u, ACC_TYPE(Lf[r]), o_offset, iq2, N); 572 perElemOpStoreCol0(tile_row(r), 0u, ACC_TYPE(Mf[r]), o_offset + p.ne1, iq2, N); 573 } 574 } 575 } else { 576 [[unroll]] for (uint32_t r = 0; r < rows_per_thread; ++r) { 577 const uint row = tile_row(r); 578 const uint global_row = i * Br + row; 579 580 if (global_row < N) { 581 uint32_t o_offset = HSV * p.ne1 * (split_k_index + p.k_num * (global_row + p.ne2 * iq3)) / 4; 582 583 [[unroll]] for (uint32_t d0 = 0; d0 < HSV / 4; d0 += threads_per_rowgroup) { 584 const uint d = d0 + col_tid; 585 if (d >= HSV/4) break; 586 data_ov4[o_offset + iq2 * HSV/4 + d] = D_TYPEV4(Of[r][d/threads_per_rowgroup]); 587 } 588 } 589 590 if (global_row < N && col_tid == 0) { 591 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)); 592 data_o[lm_offset + iq2] = D_TYPE(Lf[r]); 593 data_o[lm_offset + p.ne1 + iq2] = D_TYPE(Mf[r]); 594 } 595 } 596 } 597 598 return; 599 } 600 601 if ((p.mask_n_head_log2 & SINK_ENABLE_BIT) != 0) { 602 [[unroll]] for (uint32_t r = 0; r < rows_per_thread; ++r) { 603 float sink = perElemOpGetSink(tile_row(r), 0u, ACC_TYPE(0), iq2); 604 605 float ms = 1.0f; 606 float vs = 1.0f; 607 608 if (sink > Mf[r]) { 609 ms = exp(Mf[r] - sink); 610 611 [[unroll]] for (uint32_t d0 = 0; d0 < HSV / 4; d0 += threads_per_rowgroup) { 612 const uint d_local = d0 / threads_per_rowgroup; 613 Of[r][d_local] *= O_TYPE(ms); 614 } 615 } else { 616 vs = exp(sink - Mf[r]); 617 } 618 619 Lf[r] = Lf[r]*ms + vs; 620 } 621 } 622 623 float Lfrcp[rows_per_thread]; 624 [[unroll]] for (uint32_t r = 0; r < rows_per_thread; ++r) { 625 Lfrcp[r] = (Lf[r] == 0.0) ? 0.0 : (1.0 / Lf[r]); 626 } 627 628 [[unroll]] for (uint32_t d0 = 0; d0 < HSV / 4; d0 += threads_per_rowgroup) { 629 const uint d_local = d0 / threads_per_rowgroup; 630 [[unroll]] for (uint32_t r = 0; r < rows_per_thread; ++r) { 631 Of[r][d_local] *= O_TYPE(Lfrcp[r]); 632 #if defined(FLOAT_TYPE_MAX) 633 Of[r][d_local] = clamp(Of[r][d_local], -FLOAT_TYPE_MAX, FLOAT_TYPE_MAX); 634 #endif 635 } 636 } 637 638 uint32_t o_offset = (gqa_iq1*p.ne1*HSV + iq3*p.ne2*p.ne1*HSV) / 4; 639 640 if (p.gqa_ratio > 1) { 641 [[unroll]] for (uint32_t r = 0; r < rows_per_thread; ++r) { 642 if (tile_row(r) < N) { 643 [[unroll]] for (uint32_t d0 = 0; d0 < HSV / 4; d0 += threads_per_rowgroup) { 644 const uint d = d0 + col_tid; 645 if (d >= HSV / 4) break; 646 const uint d_local = d0 / threads_per_rowgroup; 647 gqaStore(tile_row(r), d, Of[r][d_local], o_offset, iq2, N); 648 } 649 } 650 } 651 } else { 652 [[unroll]] for (uint32_t r = 0; r < rows_per_thread; ++r) { 653 if (i * Br + tile_row(r) < N) { 654 [[unroll]] for (uint32_t d0 = 0; d0 < HSV / 4; d0 += threads_per_rowgroup) { 655 const uint d = d0 + col_tid; 656 if (d >= HSV / 4) break; 657 const uint d_local = d0 / threads_per_rowgroup; 658 data_ov4[o_offset + (iq2 * HSV + (i * Br + tile_row(r)) * p.ne1 * HSV) / 4 + d] = D_TYPEV4(Of[r][d_local]); 659 } 660 } 661 } 662 } 663 }