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-metal/kernels/fa_common.metal # 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-metal / kernels / fa_common.metal` 710 lines ยท 28666 bytes Raw Blame History 1 constant bool FC_flash_attn_ext_has_mask [[function_constant(FC_FLASH_ATTN_EXT + 0)]]; 2 constant bool FC_flash_attn_ext_has_sinks [[function_constant(FC_FLASH_ATTN_EXT + 1)]]; 3 constant bool FC_flash_attn_ext_has_bias [[function_constant(FC_FLASH_ATTN_EXT + 2)]]; 4 constant bool FC_flash_attn_ext_has_scap [[function_constant(FC_FLASH_ATTN_EXT + 3)]]; 5 constant bool FC_flash_attn_ext_has_kvpad [[function_constant(FC_FLASH_ATTN_EXT + 4)]]; 6 7 constant bool FC_flash_attn_ext_bc_mask [[function_constant(FC_FLASH_ATTN_EXT + 10)]]; 8 9 //constant float FC_flash_attn_ext_scale [[function_constant(FC_FLASH_ATTN_EXT + 10)]]; 10 //constant float FC_flash_attn_ext_max_bias [[function_constant(FC_FLASH_ATTN_EXT + 11)]]; 11 //constant float FC_flash_attn_ext_logit_softcap [[function_constant(FC_FLASH_ATTN_EXT + 12)]]; 12 13 constant int32_t FC_flash_attn_ext_ns10 [[function_constant(FC_FLASH_ATTN_EXT + 20)]]; 14 constant int32_t FC_flash_attn_ext_ns20 [[function_constant(FC_FLASH_ATTN_EXT + 21)]]; 15 constant int32_t FC_flash_attn_ext_nsg [[function_constant(FC_FLASH_ATTN_EXT + 22)]]; 16 17 // ref: https://arxiv.org/pdf/2307.08691.pdf 18 template< 19 typename q_t, // query types in shared memory 20 typename q4_t, 21 typename q8x8_t, 22 typename k_t, // key types in shared memory 23 typename k4x4_t, 24 typename k8x8_t, 25 typename v_t, // value types in shared memory 26 typename v4x4_t, 27 typename v8x8_t, 28 typename qk_t, // Q*K types 29 typename qk8x8_t, 30 typename s_t, // soft-max types 31 typename s2_t, 32 typename s8x8_t, 33 typename o_t, // attention accumulation types 34 typename o4_t, 35 typename o8x8_t, 36 typename kd4x4_t, // key type in device memory 37 short nl_k, 38 void (*deq_k)(device const kd4x4_t *, short, thread k4x4_t &), 39 typename vd4x4_t, // value type in device memory 40 short nl_v, 41 void (*deq_v)(device const vd4x4_t *, short, thread v4x4_t &), 42 short DK, // K head size 43 short DV, // V head size 44 short Q, // queries per threadgroup 45 short C, // cache items per threadgroup 46 short NSG> // number of simd groups 47 void kernel_flash_attn_ext_impl( 48 constant ggml_metal_kargs_flash_attn_ext & args, 49 device const char * q, 50 device const char * k, 51 device const char * v, 52 device const char * mask, 53 device const char * sinks, 54 device const char * pad, 55 device const char * blk, 56 device char * dst, 57 threadgroup half * shmem_f16, 58 uint3 tgpig, 59 ushort tiisg, 60 ushort sgitg) { 61 const ushort iq3 = tgpig[2]; 62 const ushort iq2 = tgpig[1]; 63 const ushort iq1 = tgpig[0]*Q; 64 65 #define NS10 (FC_flash_attn_ext_ns10) 66 #define NS20 (FC_flash_attn_ext_ns20) 67 68 // note: I had some concerns that using this instead of the ugly macros above was affecting performance 69 // need to re-check carefully and if no regressions are observerd - remove the macros 70 // the concerns is that maybe using const variables requires extra registers? but not sure if the compiler 71 // is clever enough to avoid this. unfortunately, using constexpr is not possible with FC 72 //const short NS10 = FC_flash_attn_ext_ns10; 73 //const short NS20 = FC_flash_attn_ext_ns20; 74 75 constexpr short KV = 8; 76 77 constexpr short DK4 = DK/4; 78 constexpr short DK8 = DK/8; 79 constexpr short DK16 = DK/16; 80 constexpr short DV4 = DV/4; 81 //constexpr short DV8 = DV/8; 82 constexpr short DV16 = DV/16; 83 84 constexpr short PV = PAD2(DV, 64); 85 constexpr short PV4 = PV/4; 86 constexpr short PV8 = PV/8; 87 //constexpr short PV16 = PV/16; 88 89 constexpr short NW = N_SIMDWIDTH; 90 constexpr short NQ = Q/NSG; 91 constexpr short SH = 2*C; // shared memory per simdgroup (s_t == float) 92 93 constexpr short TS = 2*SH; 94 constexpr short T = DK + 2*PV; // shared memory size per query in (half) 95 96 threadgroup q_t * sq = (threadgroup q_t *) (shmem_f16 + 0*T); // holds the query data 97 threadgroup q4_t * sq4 = (threadgroup q4_t *) (shmem_f16 + 0*T); // same as above but in q4_t 98 threadgroup o_t * so = (threadgroup o_t *) (shmem_f16 + 0*T + Q*DK); // the result for all queries in 8x8 matrices (the O matrix from the paper) 99 threadgroup o4_t * so4 = (threadgroup o4_t *) (shmem_f16 + 0*T + Q*DK); 100 threadgroup s_t * ss = (threadgroup s_t *) (shmem_f16 + Q*T); // scratch buffer for attention, mask and diagonal matrix 101 threadgroup s2_t * ss2 = (threadgroup s2_t *) (shmem_f16 + Q*T); // same as above but in s2_t 102 103 threadgroup k_t * sk = (threadgroup k_t *) (shmem_f16 + sgitg*(4*16*KV) + Q*T + Q*TS); // scratch buffer to load K in shared memory 104 threadgroup k4x4_t * sk4x4 = (threadgroup k4x4_t *) (shmem_f16 + sgitg*(4*16*KV) + Q*T + Q*TS); // same as above but in k4x4_t 105 106 threadgroup v_t * sv = (threadgroup v_t *) (shmem_f16 + sgitg*(4*16*KV) + Q*T + Q*TS); // scratch buffer to load V in shared memory 107 threadgroup v4x4_t * sv4x4 = (threadgroup v4x4_t *) (shmem_f16 + sgitg*(4*16*KV) + Q*T + Q*TS); // same as above but in v4x4_t 108 109 // mask storage in shared mem 110 threadgroup half2 * sm2 = (threadgroup half2 *) (shmem_f16 + Q*T + 2*C); 111 112 // per-query mask pointers 113 device const half2 * pm2[NQ]; 114 115 FOR_UNROLL (short jj = 0; jj < NQ; ++jj) { 116 const short j = jj*NSG + sgitg; 117 118 pm2[jj] = (device const half2 *) ((device const char *) mask + (iq1 + j)*args.nb31 + (iq2%args.ne32)*args.nb32 + (iq3%args.ne33)*args.nb33); 119 } 120 121 { 122 const int32_t nblk1 = ((args.ne01 + Q - 1)/Q); 123 const int32_t nblk0 = ((args.ne11 + C - 1)/C); 124 125 blk += (((iq3%args.ne33)*args.ne32 + (iq2%args.ne32))*nblk1 + iq1/Q)*nblk0; 126 } 127 128 { 129 q += iq1*args.nb01 + iq2*args.nb02 + iq3*args.nb03; 130 131 const short ikv2 = iq2/(args.ne02/args.ne_12_2); 132 const short ikv3 = iq3/(args.ne03/args.ne_12_3); 133 134 k += ikv2*args.nb12 + ikv3*args.nb13; 135 v += ikv2*args.nb22 + ikv3*args.nb23; 136 } 137 138 // load heads from Q to shared memory 139 FOR_UNROLL (short jj = 0; jj < NQ; ++jj) { 140 const short j = jj*NSG + sgitg; 141 142 device const float4 * q4 = (device const float4 *) ((device const char *) q + j*args.nb01); 143 144 for (short i = tiisg; i < DK4; i += NW) { 145 if (iq1 + j < args.ne01) { 146 sq4[j*DK4 + i] = (q4_t) q4[i]; 147 } else { 148 sq4[j*DK4 + i] = 0; 149 } 150 } 151 } 152 153 // zero out 154 FOR_UNROLL (short jj = 0; jj < NQ; ++jj) { 155 const short j = jj*NSG + sgitg; 156 157 for (short i = tiisg; i < DV4; i += NW) { 158 so4[j*PV4 + i] = 0; 159 } 160 161 for (short i = tiisg; i < SH; i += NW) { 162 ss[j*SH + i] = 0.0f; 163 } 164 } 165 166 threadgroup_barrier(mem_flags::mem_threadgroup); 167 168 float S[NQ] = { [0 ... NQ-1] = 0.0f }; 169 170 { 171 float M[NQ] = { [0 ... NQ-1] = -FLT_MAX/2 }; 172 173 float slope = 1.0f; 174 175 // ALiBi 176 if (FC_flash_attn_ext_has_bias) { 177 const short h = iq2; 178 179 const float base = h < args.n_head_log2 ? args.m0 : args.m1; 180 const short exph = h < args.n_head_log2 ? h + 1 : 2*(h - args.n_head_log2) + 1; 181 182 slope = pow(base, exph); 183 } 184 185 // loop over the KV cache 186 // each simdgroup handles blocks of Q rows and C columns 187 for (int ic0 = 0; ; ++ic0) { 188 int ic = ic0*C; 189 if (ic >= args.ne11) { 190 break; 191 } 192 193 // the last partial chunk uses the pad buffer as source 194 if (FC_flash_attn_ext_has_kvpad && ic + C > args.ne11) { 195 k = pad; 196 v = k + args.nb11*C*args.ne_12_2*args.ne_12_3; 197 mask = v + args.nb21*C*args.ne_12_2*args.ne_12_3; 198 199 const short ikv2 = iq2/(args.ne02/args.ne_12_2); 200 const short ikv3 = iq3/(args.ne03/args.ne_12_3); 201 202 k += (ikv2 + ikv3*args.ne_12_2)*args.nb11*C; 203 v += (ikv2 + ikv3*args.ne_12_2)*args.nb21*C; 204 205 if (!FC_flash_attn_ext_has_mask) { 206 threadgroup half * sm = (threadgroup half *) (sm2); 207 208 FOR_UNROLL (short jj = 0; jj < NQ; ++jj) { 209 const short j = jj*NSG + sgitg; 210 211 for (short i = tiisg; i < C; i += NW) { 212 if (ic + i >= args.ne11) { 213 sm[2*j*SH + i] = -MAXHALF; 214 } 215 } 216 } 217 } else { 218 FOR_UNROLL (short jj = 0; jj < NQ; ++jj) { 219 const short j = jj*NSG + sgitg; 220 221 pm2[jj] = (device const half2 *) ((device const half *) mask + 222 (iq1 + j)*C + 223 (iq2%args.ne32)*(C*args.ne31) + 224 (iq3%args.ne33)*(C*args.ne31*args.ne32)); 225 } 226 } 227 228 ic = 0; 229 } 230 231 char blk_cur = 1; 232 233 // read the mask into shared mem 234 if (FC_flash_attn_ext_has_mask) { 235 blk_cur = blk[ic0]; 236 237 if (blk_cur == 0) { 238 FOR_UNROLL (short jj = 0; jj < NQ; ++jj) { 239 pm2[jj] += NW; 240 } 241 242 continue; 243 } 244 245 if (blk_cur == 1) { 246 FOR_UNROLL (short jj = 0; jj < NQ; ++jj) { 247 const short j = jj*NSG + sgitg; 248 249 if (FC_flash_attn_ext_bc_mask) { 250 sm2[j*SH + tiisg] = (iq1 + j) < args.ne31 ? pm2[jj][tiisg] : half2(-MAXHALF, -MAXHALF); 251 } else { 252 sm2[j*SH + tiisg] = pm2[jj][tiisg]; 253 } 254 255 pm2[jj] += NW; 256 } 257 } else if (blk_cur == 2) { 258 FOR_UNROLL (short jj = 0; jj < NQ; ++jj) { 259 pm2[jj] += NW; 260 } 261 } 262 263 #if 0 264 // note: old -INF block optimization - obsoleted by pre-computing non-masked blocks 265 266 threadgroup_barrier(mem_flags::mem_threadgroup); 267 268 // used to detect blocks full of -INF 269 // skip only when the entire threadgroup is masked 270 half2 smax2(-MAXHALF/2, -MAXHALF/2); 271 272 FOR_UNROLL (short j = 0; j < Q; ++j) { 273 smax2 = max(smax2, sm2[j*SH + tiisg]); 274 } 275 276 smax2 = simd_max(smax2); 277 278 if (max(smax2[0], smax2[1]) <= -MAXHALF/2) { 279 // this barrier is important 280 threadgroup_barrier(mem_flags::mem_threadgroup); 281 282 continue; 283 } 284 #endif 285 } 286 287 // Q*K^T 288 // this is compile-time check, so it does not have runtime overhead 289 if (is_same::value) { 290 // we can read directly from global memory 291 device const k_t * pk = (device const k_t *) (k + ic*args.nb11); 292 threadgroup const q_t * pq = sq; 293 threadgroup s_t * ps = ss; 294 295 pk += sgitg*(8*NS10); 296 ps += sgitg*(8*1); 297 298 static_assert((C/8) % NSG == 0, ""); 299 300 constexpr short NC = (C/8)/NSG; 301 302 FOR_UNROLL (short cc = 0; cc < NC; ++cc) { 303 qk8x8_t mqk = make_filled_simdgroup_matrix((qk_t) 0.0f); 304 305 if (DK % 16 != 0) { 306 k8x8_t mk; 307 q8x8_t mq; 308 309 FOR_UNROLL (short i = 0; i < DK8; ++i) { 310 simdgroup_barrier(mem_flags::mem_none); 311 312 simdgroup_load(mk, pk + 8*i, NS10, 0, true); 313 simdgroup_load(mq, pq + 8*i, DK); 314 315 simdgroup_barrier(mem_flags::mem_none); 316 317 simdgroup_multiply_accumulate(mqk, mq, mk, mqk); 318 } 319 } else { 320 k8x8_t mk[2]; 321 q8x8_t mq[2]; 322 323 // note: too much unroll can tank the performance for large heads 324 #pragma unroll (MIN(DK8/2, 4*NSG)) 325 for (short i = 0; i < DK8/2; ++i) { 326 simdgroup_barrier(mem_flags::mem_none); 327 328 simdgroup_load(mq[0], pq + 0*8 + 16*i, DK); 329 simdgroup_load(mq[1], pq + 1*8 + 16*i, DK); 330 331 simdgroup_load(mk[0], pk + 0*8 + 16*i, NS10, 0, true); 332 simdgroup_load(mk[1], pk + 1*8 + 16*i, NS10, 0, true); 333 334 simdgroup_barrier(mem_flags::mem_none); 335 336 simdgroup_multiply_accumulate(mqk, mq[0], mk[0], mqk); 337 simdgroup_multiply_accumulate(mqk, mq[1], mk[1], mqk); 338 } 339 } 340 341 simdgroup_store(mqk, ps, SH, 0, false); 342 343 pk += 8*(NSG*NS10); 344 ps += 8*(NSG); 345 } 346 } else { 347 // TODO: this is the quantized K cache branch - not optimized yet 348 for (short ccc = 0; ccc < (C/8)/NSG; ++ccc) { 349 const short cc = ccc*NSG + sgitg; 350 351 const short tx = tiisg%4; 352 const short ty = tiisg/4; 353 354 qk8x8_t mqk = make_filled_simdgroup_matrix((qk_t) 0.0f); 355 356 for (short ii = 0; ii < DK16; ii += 4) { 357 device const kd4x4_t * pk4x4 = (device const kd4x4_t *) (k + ((ic + 8*cc + ty)*args.nb11)); 358 359 if (DK16%4 == 0) { 360 // the head is evenly divisible by 4*16 = 64, so no need for bound checks 361 { 362 k4x4_t tmp; 363 deq_k(pk4x4 + (ii + tx)/nl_k, (ii + tx)%nl_k, tmp); 364 sk4x4[4*ty + tx] = tmp; 365 } 366 367 simdgroup_barrier(mem_flags::mem_threadgroup); 368 369 FOR_UNROLL (short k = 0; k < 4; ++k) { 370 k8x8_t mk; 371 q8x8_t mq; 372 373 simdgroup_load(mk, sk + 16*k + 0*8, 4*16, 0, true); // transpose 374 simdgroup_load(mq, sq + (2*(ii + k) + 0)*8, DK); 375 simdgroup_multiply_accumulate(mqk, mq, mk, mqk); 376 377 simdgroup_load(mk, sk + 16*k + 1*8, 4*16, 0, true); // transpose 378 simdgroup_load(mq, sq + (2*(ii + k) + 1)*8, DK); 379 simdgroup_multiply_accumulate(mqk, mq, mk, mqk); 380 } 381 } else { 382 if (ii + tx < DK16) { 383 k4x4_t tmp; 384 deq_k(pk4x4 + (ii + tx)/nl_k, (ii + tx)%nl_k, tmp); 385 sk4x4[4*ty + tx] = tmp; 386 } 387 388 simdgroup_barrier(mem_flags::mem_threadgroup); 389 390 for (short k = 0; k < 4 && ii + k < DK16; ++k) { 391 k8x8_t mk; 392 q8x8_t mq; 393 394 simdgroup_load(mk, sk + 16*k + 0*8, 4*16, 0, true); // transpose 395 simdgroup_load(mq, sq + (2*(ii + k) + 0)*8, DK); 396 simdgroup_multiply_accumulate(mqk, mq, mk, mqk); 397 398 simdgroup_load(mk, sk + 16*k + 1*8, 4*16, 0, true); // transpose 399 simdgroup_load(mq, sq + (2*(ii + k) + 1)*8, DK); 400 simdgroup_multiply_accumulate(mqk, mq, mk, mqk); 401 } 402 } 403 } 404 405 simdgroup_store(mqk, ss + 8*cc, SH, 0, false); 406 } 407 } 408 409 threadgroup_barrier(mem_flags::mem_threadgroup); 410 411 // online softmax 412 FOR_UNROLL (short jj = 0; jj < NQ; ++jj) { 413 const short j = jj*NSG + sgitg; 414 415 const float m = M[jj]; 416 417 // scale and apply the logitcap / mask 418 float2 s2 = ss2[j*SH/2 + tiisg]*args.scale; 419 420 if (FC_flash_attn_ext_has_scap) { 421 s2 = args.logit_softcap*precise::tanh(s2); 422 } 423 424 // mqk = mqk + slope*mask 425 if (blk_cur != 2) { 426 if (FC_flash_attn_ext_has_bias) { 427 s2 += s2_t(sm2[j*SH + tiisg])*slope; 428 } else { 429 s2 += s2_t(sm2[j*SH + tiisg]); 430 } 431 } 432 433 M[jj] = simd_max(max(M[jj], max(s2[0], s2[1]))); 434 435 const float ms = exp(m - M[jj]); 436 const float2 vs2 = exp(s2 - M[jj]); 437 438 S[jj] = S[jj]*ms + simd_sum(vs2[0] + vs2[1]); 439 440 // the P matrix from the paper (Q rows, C columns) 441 ss2[j*SH/2 + tiisg] = vs2; 442 443 if (DV4 % NW == 0) { 444 FOR_UNROLL (short ii = 0; ii < DV4/NW; ++ii) { 445 const short i = ii*NW + tiisg; 446 447 so4[j*PV4 + i] *= ms; 448 } 449 } else { 450 for (short i = tiisg; i < DV4; i += NW) { 451 so4[j*PV4 + i] *= ms; 452 } 453 } 454 } 455 456 threadgroup_barrier(mem_flags::mem_threadgroup); 457 458 // O = O + (Q*K^T)*V 459 { 460 // we can read directly from global memory 461 if (is_same::value) { 462 static_assert(PV8 % NSG == 0, ""); 463 464 constexpr short NO = PV8/NSG; 465 466 o8x8_t lo[NO]; 467 468 { 469 auto sot = so + 8*sgitg; 470 471 FOR_UNROLL (short ii = 0; ii < NO; ++ii) { 472 simdgroup_load(lo[ii], sot, PV, 0, false); 473 474 sot += 8*NSG; 475 } 476 } 477 478 { 479 device const v_t * pv = (device const v_t *) (v + ic*args.nb21); 480 481 pv += 8*sgitg; 482 483 if (DV <= 64) { 484 FOR_UNROLL (short cc = 0; cc < C/8; ++cc) { 485 s8x8_t vs; 486 simdgroup_load(vs, ss + 8*cc, SH, 0, false); 487 488 FOR_UNROLL (short ii = 0; ii < NO/2; ++ii) { 489 v8x8_t mv[2]; 490 491 simdgroup_load(mv[0], pv + 0*NSG + 16*ii*NSG, NS20, 0, false); 492 simdgroup_load(mv[1], pv + 8*NSG + 16*ii*NSG, NS20, 0, false); 493 494 simdgroup_multiply_accumulate(lo[2*ii + 0], vs, mv[0], lo[2*ii + 0]); 495 simdgroup_multiply_accumulate(lo[2*ii + 1], vs, mv[1], lo[2*ii + 1]); 496 } 497 498 pv += 8*NS20; 499 } 500 } else { 501 constexpr short NC = (C/8)/2; 502 503 FOR_UNROLL (short cc = 0; cc < NC; ++cc) { 504 s8x8_t vs[2]; 505 506 simdgroup_load(vs[0], ss + 16*cc + 0, SH, 0, false); 507 simdgroup_load(vs[1], ss + 16*cc + 8, SH, 0, false); 508 509 FOR_UNROLL (short ii = 0; ii < NO/2; ++ii) { 510 v8x8_t mv[4]; 511 512 simdgroup_load(mv[0], pv + 0*NSG + 16*ii*NSG + 0*8*NS20, NS20, 0, false); 513 simdgroup_load(mv[1], pv + 8*NSG + 16*ii*NSG + 0*8*NS20, NS20, 0, false); 514 simdgroup_load(mv[2], pv + 0*NSG + 16*ii*NSG + 1*8*NS20, NS20, 0, false); 515 simdgroup_load(mv[3], pv + 8*NSG + 16*ii*NSG + 1*8*NS20, NS20, 0, false); 516 517 simdgroup_multiply_accumulate(lo[2*ii + 0], vs[0], mv[0], lo[2*ii + 0]); 518 simdgroup_multiply_accumulate(lo[2*ii + 1], vs[0], mv[1], lo[2*ii + 1]); 519 simdgroup_multiply_accumulate(lo[2*ii + 0], vs[1], mv[2], lo[2*ii + 0]); 520 simdgroup_multiply_accumulate(lo[2*ii + 1], vs[1], mv[3], lo[2*ii + 1]); 521 } 522 523 pv += 2*8*NS20; 524 } 525 } 526 } 527 528 { 529 auto sot = so + 8*sgitg; 530 531 FOR_UNROLL (short ii = 0; ii < NO; ++ii) { 532 simdgroup_store(lo[ii], sot, PV, 0, false); 533 534 sot += 8*NSG; 535 } 536 } 537 } else { 538 // TODO: this is the quantized V cache branch - not optimized yet 539 540 const short tx = tiisg%4; 541 const short ty = tiisg/4; 542 543 for (short cc = 0; cc < C/8; ++cc) { 544 s8x8_t vs; 545 simdgroup_load(vs, ss + 8*cc, SH, 0, false); 546 547 for (short ii = 4*sgitg; ii < DV16; ii += 4*NSG) { 548 device const vd4x4_t * pv4x4 = (device const vd4x4_t *) (v + ((ic + 8*cc + ty)*args.nb21)); 549 550 if (DV16%4 == 0) { 551 // no need for bound checks 552 { 553 v4x4_t tmp; 554 deq_v(pv4x4 + (ii + tx)/nl_v, (ii + tx)%nl_v, tmp); 555 sv4x4[4*ty + tx] = tmp; 556 } 557 558 simdgroup_barrier(mem_flags::mem_threadgroup); 559 560 FOR_UNROLL (short k = 0; k < 4; ++k) { 561 v8x8_t mv[2]; 562 o8x8_t lo[2]; 563 564 simdgroup_load(mv[0], sv + 16*k + 0*8, 4*16, 0, false); 565 simdgroup_load(mv[1], sv + 16*k + 1*8, 4*16, 0, false); 566 simdgroup_load(lo[0], so + 8*(2*(ii + k) + 0), PV, 0, false); 567 simdgroup_load(lo[1], so + 8*(2*(ii + k) + 1), PV, 0, false); 568 569 simdgroup_multiply_accumulate(lo[0], vs, mv[0], lo[0]); 570 simdgroup_multiply_accumulate(lo[1], vs, mv[1], lo[1]); 571 572 simdgroup_store(lo[0], so + 8*(2*(ii + k) + 0), PV, 0, false); 573 simdgroup_store(lo[1], so + 8*(2*(ii + k) + 1), PV, 0, false); 574 } 575 } else { 576 if (ii + tx < DV16) { 577 v4x4_t tmp; 578 deq_v(pv4x4 + (ii + tx)/nl_v, (ii + tx)%nl_v, tmp); 579 sv4x4[4*ty + tx] = tmp; 580 } 581 582 simdgroup_barrier(mem_flags::mem_threadgroup); 583 584 for (short k = 0; k < 4 && ii + k < DV16; ++k) { 585 v8x8_t mv[2]; 586 o8x8_t lo[2]; 587 588 simdgroup_load(mv[0], sv + 16*k + 0*8, 4*16, 0, false); 589 simdgroup_load(mv[1], sv + 16*k + 1*8, 4*16, 0, false); 590 simdgroup_load(lo[0], so + 8*(2*(ii + k) + 0), PV, 0, false); 591 simdgroup_load(lo[1], so + 8*(2*(ii + k) + 1), PV, 0, false); 592 593 simdgroup_multiply_accumulate(lo[0], vs, mv[0], lo[0]); 594 simdgroup_multiply_accumulate(lo[1], vs, mv[1], lo[1]); 595 596 simdgroup_store(lo[0], so + 8*(2*(ii + k) + 0), PV, 0, false); 597 simdgroup_store(lo[1], so + 8*(2*(ii + k) + 1), PV, 0, false); 598 } 599 } 600 } 601 } 602 } 603 } 604 605 threadgroup_barrier(mem_flags::mem_threadgroup); 606 } 607 608 if (FC_flash_attn_ext_has_sinks) { 609 FOR_UNROLL (short jj = 0; jj < NQ; ++jj) { 610 const short j = jj*NSG + sgitg; 611 612 const float m = M[jj]; 613 const float s = tiisg == 0 ? ((device const float *) sinks)[iq2] : -FLT_MAX/2; 614 615 M[jj] = simd_max(max(M[jj], s)); 616 617 const float ms = exp(m - M[jj]); 618 const float vs = exp(s - M[jj]); 619 620 S[jj] = S[jj]*ms + simd_sum(vs); 621 622 for (short i = tiisg; i < DV4; i += NW) { 623 so4[j*PV4 + i] *= ms; 624 } 625 } 626 } 627 } 628 629 // store to global memory 630 for (short jj = 0; jj < NQ; ++jj) { 631 const short j = jj*NSG + sgitg; 632 if (iq1 + j >= args.ne01) { 633 break; 634 } 635 636 device float4 * dst4 = (device float4 *) dst + ((uint64_t)iq3*args.ne2*args.ne1 + iq2 + (uint64_t)(iq1 + j)*args.ne1)*DV4; 637 638 const float scale = S[jj] == 0.0 ? 0.0f : 1.0f/S[jj]; 639 640 if (DV4 % NW == 0) { 641 FOR_UNROLL (short ii = 0; ii < DV4/NW; ++ii) { 642 const short i = ii*NW + tiisg; 643 644 dst4[i] = (float4) so4[j*PV4 + i]*scale; 645 } 646 } else { 647 for (short i = tiisg; i < DV4; i += NW) { 648 dst4[i] = (float4) so4[j*PV4 + i]*scale; 649 } 650 } 651 } 652 653 #undef NS10 654 #undef NS20 655 } 656 657 template< 658 typename q_t, // query types in shared memory 659 typename q4_t, 660 typename q8x8_t, 661 typename k_t, // key types in shared memory 662 typename k4x4_t, 663 typename k8x8_t, 664 typename v_t, // value types in shared memory 665 typename v4x4_t, 666 typename v8x8_t, 667 typename qk_t, // Q*K types 668 typename qk8x8_t, 669 typename s_t, // soft-max types 670 typename s2_t, 671 typename s8x8_t, 672 typename o_t, // attention accumulation types 673 typename o4_t, 674 typename o8x8_t, 675 typename kd4x4_t, // key type in device memory 676 short nl_k, 677 void (*deq_k)(device const kd4x4_t *, short, thread k4x4_t &), 678 typename vd4x4_t, // value type in device memory 679 short nl_v, 680 void (*deq_v)(device const vd4x4_t *, short, thread v4x4_t &), 681 short DK, // K head size 682 short DV, // V head size 683 short Q = OP_FLASH_ATTN_EXT_NQPSG, // queries per threadgroup 684 short C = OP_FLASH_ATTN_EXT_NCPSG> // cache items per threadgroup 685 kernel void kernel_flash_attn_ext( 686 constant ggml_metal_kargs_flash_attn_ext & args, 687 device const char * q, 688 device const char * k, 689 device const char * v, 690 device const char * mask, 691 device const char * sinks, 692 device const char * pad, 693 device const char * blk, 694 device char * dst, 695 threadgroup half * shmem_f16 [[threadgroup(0)]], 696 uint3 tgpig[[threadgroup_position_in_grid]], 697 ushort tiisg[[thread_index_in_simdgroup]], 698 ushort sgitg[[simdgroup_index_in_threadgroup]]) { 699 #define FWD_TMPL q_t, q4_t, q8x8_t, k_t, k4x4_t, k8x8_t, v_t, v4x4_t, v8x8_t, qk_t, qk8x8_t, s_t, s2_t, s8x8_t, o_t, o4_t, o8x8_t, kd4x4_t, nl_k, deq_k, vd4x4_t, nl_v, deq_v, DK, DV, Q, C 700 #define FWD_ARGS args, q, k, v, mask, sinks, pad, blk, dst, shmem_f16, tgpig, tiisg, sgitg 701 switch (FC_flash_attn_ext_nsg) { 702 // note: disabled cases to reduce library load time 703 //case 1: kernel_flash_attn_ext_impl(FWD_ARGS); break; 704 //case 2: kernel_flash_attn_ext_impl(FWD_ARGS); break; 705 case 4: kernel_flash_attn_ext_impl(FWD_ARGS); break; 706 case 8: kernel_flash_attn_ext_impl(FWD_ARGS); break; 707 } 708 #undef FWD_TMPL 709 #undef FWD_ARGS 710 }