ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-metal / kernels / fa_vec_common.metal
| 1 | constant bool FC_flash_attn_ext_vec_has_mask [[function_constant(FC_FLASH_ATTN_EXT_VEC + 0)]]; |
| 2 | constant bool FC_flash_attn_ext_vec_has_sinks [[function_constant(FC_FLASH_ATTN_EXT_VEC + 1)]]; |
| 3 | constant bool FC_flash_attn_ext_vec_has_bias [[function_constant(FC_FLASH_ATTN_EXT_VEC + 2)]]; |
| 4 | constant bool FC_flash_attn_ext_vec_has_scap [[function_constant(FC_FLASH_ATTN_EXT_VEC + 3)]]; |
| 5 | constant bool FC_flash_attn_ext_vec_has_kvpad [[function_constant(FC_FLASH_ATTN_EXT_VEC + 4)]]; |
| 6 | |
| 7 | //constant float FC_flash_attn_ext_vec_scale [[function_constant(FC_FLASH_ATTN_EXT_VEC + 10)]]; |
| 8 | //constant float FC_flash_attn_ext_vec_max_bias [[function_constant(FC_FLASH_ATTN_EXT_VEC + 11)]]; |
| 9 | //constant float FC_flash_attn_ext_vec_logit_softcap [[function_constant(FC_FLASH_ATTN_EXT_VEC + 12)]]; |
| 10 | |
| 11 | constant int32_t FC_flash_attn_ext_vec_ns10 [[function_constant(FC_FLASH_ATTN_EXT_VEC + 20)]]; |
| 12 | constant int32_t FC_flash_attn_ext_vec_ns20 [[function_constant(FC_FLASH_ATTN_EXT_VEC + 21)]]; |
| 13 | constant int32_t FC_flash_attn_ext_vec_nsg [[function_constant(FC_FLASH_ATTN_EXT_VEC + 22)]]; |
| 14 | constant int32_t FC_flash_attn_ext_vec_nwg [[function_constant(FC_FLASH_ATTN_EXT_VEC + 23)]]; |
| 15 | constant bool FC_flash_attn_ext_vec_has_sparse [[function_constant(FC_FLASH_ATTN_EXT_VEC + 5)]]; |
| 16 | constant bool FC_flash_attn_ext_vec_has_rows [[function_constant(FC_FLASH_ATTN_EXT_VEC + 6)]]; |
| 17 | template< |
| 18 | typename q4_t, // query types in shared memory |
| 19 | typename k4_t, // key types in shared memory |
| 20 | typename v4_t, // value types in shared memory |
| 21 | typename qk_t, // Q*K types |
| 22 | typename s_t, // soft-max types |
| 23 | typename s4_t, |
| 24 | typename o4_t, // attention accumulation types |
| 25 | typename kd4_t, // key type in device memory |
| 26 | short nl_k, |
| 27 | void (*deq_k_t4)(device const kd4_t *, short, thread k4_t &), |
| 28 | typename vd4_t, // value type in device memory |
| 29 | short nl_v, |
| 30 | void (*deq_v_t4)(device const vd4_t *, short, thread v4_t &), |
| 31 | short DK, // K head size |
| 32 | short DV, // V head size |
| 33 | short NE = 4, // head elements per thread |
| 34 | short Q = OP_FLASH_ATTN_EXT_VEC_NQPSG, // queries per threadgroup |
| 35 | short C = OP_FLASH_ATTN_EXT_VEC_NCPSG> // cache items per threadgroup |
| 36 | |
| 37 | kernel void kernel_flash_attn_ext_vec( |
| 38 | constant ggml_metal_kargs_flash_attn_ext_vec & args, |
| 39 | device const char * q, |
| 40 | device const char * k, |
| 41 | device const char * v, |
| 42 | device const char * mask, |
| 43 | device const char * sinks, |
| 44 | device const char * pad, |
| 45 | device char * dst, |
| 46 | device const char * idx, |
| 47 | threadgroup half * shmem_f16 [[threadgroup(0)]], |
| 48 | uint3 tgpig[[threadgroup_position_in_grid]], |
| 49 | ushort tiisg[[thread_index_in_simdgroup]], |
| 50 | ushort sgitg[[simdgroup_index_in_threadgroup]]) { |
| 51 | static_assert(DK % 32 == 0, "DK must be divisible by 32"); |
| 52 | static_assert(DV % 32 == 0, "DV must be divisible by 32"); |
| 53 | |
| 54 | #define NWG (FC_flash_attn_ext_vec_nwg) |
| 55 | #define NSG (FC_flash_attn_ext_vec_nsg) |
| 56 | |
| 57 | #define NS10 (FC_flash_attn_ext_vec_ns10) |
| 58 | #define NS20 (FC_flash_attn_ext_vec_ns20) |
| 59 | |
| 60 | const short iwg = tgpig[2]%NWG; |
| 61 | |
| 62 | const ushort iq3 = tgpig[2]/NWG; |
| 63 | const ushort iq2 = tgpig[1]; |
| 64 | const ushort iq1 = tgpig[0]; |
| 65 | |
| 66 | constexpr short DK4 = DK/4; |
| 67 | constexpr short DV4 = DV/4; |
| 68 | |
| 69 | constexpr short PK = PAD2(DK, 128); |
| 70 | constexpr short PK4 = PK/4; |
| 71 | |
| 72 | constexpr short PV = PAD2(DV, 128); |
| 73 | constexpr short PV4 = PV/4; |
| 74 | |
| 75 | constexpr short NW = N_SIMDWIDTH; |
| 76 | constexpr short NL = NW/NE; // note: this can be adjusted to support different head sizes and simdgroup work loads |
| 77 | constexpr short SH = 4*Q*C; // shared memory per simdgroup |
| 78 | |
| 79 | const int SMEM_Q = Q*NSG*PK; |
| 80 | const int SMEM_S = NSG*SH; |
| 81 | const int SMEM_O = 2*NSG*Q*PV; |
| 82 | const int SMEM = SMEM_Q + SMEM_S + SMEM_O; |
| 83 | |
| 84 | static_assert(DK4 % NL == 0, "DK4 must be divisible by NL"); |
| 85 | static_assert(DV4 % NL == 0, "DV4 must be divisible by NL"); |
| 86 | |
| 87 | threadgroup q4_t * sq4 = (threadgroup q4_t *) shmem_f16; // holds the query data |
| 88 | threadgroup s_t * ss = (threadgroup s_t *) (shmem_f16 + SMEM_Q + sgitg*SH); // scratch buffer for attention |
| 89 | threadgroup s4_t * ss4 = (threadgroup s4_t *) (shmem_f16 + SMEM_Q + sgitg*SH); // same as above but in s4_t |
| 90 | threadgroup half * sm = (threadgroup half *) (shmem_f16 + SMEM_Q + sgitg*SH + 2*Q*C); // scratch buffer for mask |
| 91 | threadgroup o4_t * so4 = (threadgroup o4_t *) (shmem_f16 + SMEM_Q + SMEM_S + 2*sgitg*Q*PV); // scratch buffer for the results |
| 92 | |
| 93 | // sparse indices for the current block |
| 94 | // note: with kv_rows, the buffer holds the row list of the current slice instead |
| 95 | threadgroup int * spidx = (FC_flash_attn_ext_vec_has_sparse || FC_flash_attn_ext_vec_has_rows) |
| 96 | ? (threadgroup int *) (shmem_f16 + SMEM) + sgitg*C |
| 97 | : nullptr; |
| 98 | |
| 99 | // store the result for all queries in shared memory (the O matrix from the paper) |
| 100 | so4 += tiisg; |
| 101 | |
| 102 | { |
| 103 | q += iq1*Q*args.nb01 + iq2*args.nb02 + iq3*args.nb03; |
| 104 | |
| 105 | const short ikv2 = iq2/(args.ne02/args.ne_12_2); |
| 106 | const short ikv3 = iq3/(args.ne03/args.ne_12_3); |
| 107 | |
| 108 | k += ikv2*args.nb12 + ikv3*args.nb13; |
| 109 | v += ikv2*args.nb22 + ikv3*args.nb23; |
| 110 | } |
| 111 | |
| 112 | // load Q query rows to shared memory |
| 113 | { |
| 114 | for (short qq = 0; qq < Q; ++qq) { |
| 115 | const int iq1_q = iq1*Q + qq; |
| 116 | device const float4 * q4 = (device const float4 *) ((device const char *) q + qq*args.nb01); |
| 117 | if (iq1_q < args.ne01) { |
| 118 | for (short i = tiisg; i < PK4; i += NW) { |
| 119 | if (i < DK4) { |
| 120 | sq4[qq*PK4 + i] = (q4_t) q4[i]; |
| 121 | } else { |
| 122 | sq4[qq*PK4 + i] = (q4_t) 0.0f; |
| 123 | } |
| 124 | } |
| 125 | } else { |
| 126 | for (short i = tiisg; i < PK4; i += NW) { |
| 127 | sq4[qq*PK4 + i] = (q4_t) 0.0f; |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | // zero out so |
| 134 | for (short qq = 0; qq < Q; ++qq) { |
| 135 | for (short i = 0; i < DV4/NL; ++i) { |
| 136 | so4[qq*DV4 + i*NL] = (o4_t) 0.0f; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | // zero out shared memory SH |
| 141 | for (short i = tiisg; i < SH/4; i += NW) { |
| 142 | ss4[i] = (s4_t) 0.0f; |
| 143 | } |
| 144 | |
| 145 | threadgroup_barrier(mem_flags::mem_threadgroup); |
| 146 | |
| 147 | { |
| 148 | float S[Q]; |
| 149 | float M[Q]; |
| 150 | FOR_UNROLL (short qq = 0; qq < Q; ++qq) { |
| 151 | S[qq] = 0.0f; |
| 152 | M[qq] = -FLT_MAX/2; |
| 153 | } |
| 154 | |
| 155 | // thread indices inside the simdgroup |
| 156 | const short tx = tiisg%NL; |
| 157 | const short ty = tiisg/NL; |
| 158 | |
| 159 | // pointer to the mask |
| 160 | device const half * pm_base = (device const half *) (mask + iq1*Q*args.nb31 + (iq2%args.ne32)*args.nb32 + (iq3%args.ne33)*args.nb33); |
| 161 | |
| 162 | // sparse indices: the list of finite mask entries per query row |
| 163 | // the sparse path requires Q == 1 (enforced by the host) |
| 164 | device const int * pidx = nullptr; |
| 165 | if (FC_flash_attn_ext_vec_has_sparse) { |
| 166 | pidx = (device const int *) idx + |
| 167 | ((int64_t)(iq3%args.ne33)*args.ne32 + (iq2%args.ne32))*args.ne31*args.n_kv_max_padded + (iq1%args.ne31)*args.n_kv_max_padded; |
| 168 | } |
| 169 | |
| 170 | // K/V row list of the current slice (ggml_flash_attn_ext_rows) |
| 171 | // with kv_rows, the loop below runs over the mask columns (args.ne11) and the K/V cache rows |
| 172 | // are read indirectly through the list. negative entries are padding, their mask is -INF |
| 173 | device const int * rows_list = FC_flash_attn_ext_vec_has_rows ? |
| 174 | (device const int *) idx + (int64_t)(iq3%args.ne33)*args.ne11 : nullptr; |
| 175 | |
| 176 | float slope = 1.0f; |
| 177 | |
| 178 | // ALiBi |
| 179 | if (FC_flash_attn_ext_vec_has_bias) { |
| 180 | const short h = iq2; |
| 181 | |
| 182 | const float base = h < args.n_head_log2 ? args.m0 : args.m1; |
| 183 | const short exph = h < args.n_head_log2 ? h + 1 : 2*(h - args.n_head_log2) + 1; |
| 184 | |
| 185 | slope = pow(base, exph); |
| 186 | } |
| 187 | |
| 188 | // loop over the KV cache |
| 189 | // each simdgroup handles blocks of Q rows and C columns |
| 190 | for (int ic0 = iwg*NSG + sgitg; ; ic0 += NWG*NSG) { |
| 191 | int ic = ic0*C; |
| 192 | if (ic >= args.ne11) { |
| 193 | break; |
| 194 | } |
| 195 | |
| 196 | device const half * pm[Q]; |
| 197 | FOR_UNROLL (short qq = 0; qq < Q; ++qq) { |
| 198 | // padded query rows clamp to row 0 of the mask to avoid OOB; their scores |
| 199 | // are forced to -inf below, so the values never affect the result. |
| 200 | pm[qq] = pm_base + ((iq1*Q + qq) < args.ne01 ? qq*(args.nb31/sizeof(half)) : -iq1*Q*(args.nb31/sizeof(half))); |
| 201 | } |
| 202 | |
| 203 | // the last partial chunk uses the pad buffer as source |
| 204 | if (FC_flash_attn_ext_vec_has_kvpad && ic + C > args.ne11) { |
| 205 | k = pad; |
| 206 | v = k + args.nb11*C*args.ne_12_2*args.ne_12_3; |
| 207 | mask = v + args.nb21*C*args.ne_12_2*args.ne_12_3; |
| 208 | |
| 209 | const short ikv2 = iq2/(args.ne02/args.ne_12_2); |
| 210 | const short ikv3 = iq3/(args.ne03/args.ne_12_3); |
| 211 | |
| 212 | k += (ikv2 + ikv3*args.ne_12_2)*args.nb11*C; |
| 213 | v += (ikv2 + ikv3*args.ne_12_2)*args.nb21*C; |
| 214 | |
| 215 | if (!FC_flash_attn_ext_vec_has_mask) { |
| 216 | FOR_UNROLL (short qq = 0; qq < Q; ++qq) { |
| 217 | if (ic + tiisg >= args.ne11) { |
| 218 | sm[qq*C + tiisg] = -MAXHALF; |
| 219 | } |
| 220 | } |
| 221 | } else { |
| 222 | FOR_UNROLL (short qq = 0; qq < Q; ++qq) { |
| 223 | pm[qq] = (device const half *) (mask) + |
| 224 | (iq1*Q + qq)*C + |
| 225 | (iq2%args.ne32)*(C*args.ne31) + |
| 226 | (iq3%args.ne33)*(C*args.ne31*args.ne32); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | ic = 0; |
| 231 | } |
| 232 | |
| 233 | // load the KV row indices for the current block into shared memory |
| 234 | if (FC_flash_attn_ext_vec_has_sparse || FC_flash_attn_ext_vec_has_rows) { |
| 235 | FOR_UNROLL (short ii = 0; ii < C/NW; ++ii) { |
| 236 | const short i = ii*NW + tiisg; |
| 237 | |
| 238 | spidx[i] = FC_flash_attn_ext_vec_has_rows ? rows_list[ic + i] : pidx[ic + i]; |
| 239 | } |
| 240 | simdgroup_barrier(mem_flags::mem_threadgroup); |
| 241 | } |
| 242 | |
| 243 | if (FC_flash_attn_ext_vec_has_mask) { |
| 244 | if (FC_flash_attn_ext_vec_has_sparse) { |
| 245 | FOR_UNROLL (short qq = 0; qq < Q; ++qq) { |
| 246 | const int i11 = spidx[tiisg]; |
| 247 | if ((iq1*Q + qq) < args.ne01 && i11 >= 0) { |
| 248 | sm[qq*C + tiisg] = pm[qq][i11]; |
| 249 | } else { |
| 250 | sm[qq*C + tiisg] = -MAXHALF; |
| 251 | } |
| 252 | } |
| 253 | } else if (FC_flash_attn_ext_vec_has_rows) { |
| 254 | // the mask is compact: column ic maps to cache row spidx[tiisg] |
| 255 | FOR_UNROLL (short qq = 0; qq < Q; ++qq) { |
| 256 | if ((iq1*Q + qq) < args.ne01) { |
| 257 | sm[qq*C + tiisg] = pm[qq][ic + tiisg]; |
| 258 | } else { |
| 259 | sm[qq*C + tiisg] = -MAXHALF; |
| 260 | } |
| 261 | } |
| 262 | } else { |
| 263 | FOR_UNROLL (short qq = 0; qq < Q; ++qq) { |
| 264 | if ((iq1*Q + qq) < args.ne01) { |
| 265 | sm[qq*C + tiisg] = pm[qq][ic + tiisg]; |
| 266 | } else { |
| 267 | sm[qq*C + tiisg] = -MAXHALF; |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | } else { |
| 272 | FOR_UNROLL (short qq = 0; qq < Q; ++qq) { |
| 273 | if ((iq1*Q + qq) >= args.ne01) { |
| 274 | sm[qq*C + tiisg] = -MAXHALF; |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | // skip -INF mask |
| 280 | { |
| 281 | bool any_finite = false; |
| 282 | FOR_UNROLL (short qq = 0; qq < Q; ++qq) { |
| 283 | if (simd_max(sm[qq*C + tiisg]) > -MAXHALF) { |
| 284 | any_finite = true; |
| 285 | } |
| 286 | } |
| 287 | if (!any_finite) { |
| 288 | continue; |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | // Q*K^T |
| 293 | { |
| 294 | device const k4_t * pk4 = nullptr; |
| 295 | |
| 296 | if (!FC_flash_attn_ext_vec_has_sparse && !FC_flash_attn_ext_vec_has_rows) { |
| 297 | pk4 = (device const k4_t *) (k + ic*args.nb11); |
| 298 | |
| 299 | pk4 += ty*NS10/4 + tx; |
| 300 | } |
| 301 | |
| 302 | qk_t mqk[Q][C/NE]; |
| 303 | FOR_UNROLL (short qq = 0; qq < Q; ++qq) { |
| 304 | FOR_UNROLL (short cc = 0; cc < C/NE; ++cc) { |
| 305 | mqk[qq][cc] = 0.0f; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | // each simdgroup processes Q queries and NE (NW/NL) cache elements |
| 310 | FOR_UNROLL (short cc = 0; cc < C/NE; ++cc) { |
| 311 | if (FC_flash_attn_ext_vec_has_sparse || FC_flash_attn_ext_vec_has_rows) { |
| 312 | // the KV rows are gathered from the index list; -1 entries are padding |
| 313 | const int i11 = spidx[NE*cc + ty]; |
| 314 | if (i11 >= 0) { |
| 315 | if (is_same<kd4_t, k4_t>::value) { |
| 316 | device const k4_t * pk4s = (device const k4_t *) (k + i11*args.nb11) + tx; |
| 317 | FOR_UNROLL (short ii = 0; ii < DK4/NL; ++ii) { |
| 318 | const k4_t k_elem = pk4s[ii*NL]; |
| 319 | FOR_UNROLL (short qq = 0; qq < Q; ++qq) { |
| 320 | mqk[qq][cc] += dot((float4) k_elem, (float4) sq4[qq*PK4 + ii*NL + tx]); |
| 321 | } |
| 322 | } |
| 323 | } else { |
| 324 | device const kd4_t * pk = (device const kd4_t *) (k + i11*args.nb11); |
| 325 | |
| 326 | k4_t mk; |
| 327 | |
| 328 | FOR_UNROLL (short ii = 0; ii < DK4/NL; ++ii) { |
| 329 | const short i = ii*NL + tx; |
| 330 | |
| 331 | deq_k_t4(pk + i/nl_k, i%nl_k, mk); |
| 332 | |
| 333 | FOR_UNROLL (short qq = 0; qq < Q; ++qq) { |
| 334 | mqk[qq][cc] += dot((float4) mk, (float4) sq4[qq*PK4 + i]); |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | } else if (is_same<kd4_t, k4_t>::value) { |
| 340 | FOR_UNROLL (short ii = 0; ii < DK4/NL; ++ii) { |
| 341 | const k4_t k_elem = pk4[cc*NE*NS10/4 + ii*NL]; |
| 342 | FOR_UNROLL (short qq = 0; qq < Q; ++qq) { |
| 343 | mqk[qq][cc] += dot((float4) k_elem, (float4) sq4[qq*PK4 + ii*NL + tx]); |
| 344 | } |
| 345 | } |
| 346 | } else { |
| 347 | device const kd4_t * pk = (device const kd4_t *) (k + ((ic + NE*cc + ty)*args.nb11)); |
| 348 | |
| 349 | k4_t mk; |
| 350 | |
| 351 | FOR_UNROLL (short ii = 0; ii < DK4/NL; ++ii) { |
| 352 | const short i = ii*NL + tx; |
| 353 | |
| 354 | deq_k_t4(pk + i/nl_k, i%nl_k, mk); |
| 355 | |
| 356 | FOR_UNROLL (short qq = 0; qq < Q; ++qq) { |
| 357 | mqk[qq][cc] += dot((float4) mk, (float4) sq4[qq*PK4 + i]); |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | FOR_UNROLL (short qq = 0; qq < Q; ++qq) { |
| 363 | if (NE == 1) { |
| 364 | mqk[qq][cc] = simd_sum(mqk[qq][cc]); |
| 365 | } else { |
| 366 | // simdgroup reduce (NE = 4) |
| 367 | // [ 0 .. 7] -> [ 0] |
| 368 | // [ 8 .. 15] -> [ 8] |
| 369 | // [16 .. 23] -> [16] |
| 370 | // [24 .. 31] -> [24] |
| 371 | if (NE <= 1) { |
| 372 | mqk[qq][cc] += simd_shuffle_down(mqk[qq][cc], 16); |
| 373 | } |
| 374 | if (NE <= 2) { |
| 375 | mqk[qq][cc] += simd_shuffle_down(mqk[qq][cc], 8); |
| 376 | } |
| 377 | if (NE <= 4) { |
| 378 | mqk[qq][cc] += simd_shuffle_down(mqk[qq][cc], 4); |
| 379 | } |
| 380 | if (NE <= 8) { |
| 381 | mqk[qq][cc] += simd_shuffle_down(mqk[qq][cc], 2); |
| 382 | } |
| 383 | if (NE <= 16) { |
| 384 | mqk[qq][cc] += simd_shuffle_down(mqk[qq][cc], 1); |
| 385 | } |
| 386 | |
| 387 | // broadcast |
| 388 | mqk[qq][cc] = simd_shuffle(mqk[qq][cc], NL*ty); |
| 389 | } |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | FOR_UNROLL (short qq = 0; qq < Q; ++qq) { |
| 394 | if (FC_flash_attn_ext_vec_has_mask && |
| 395 | !FC_flash_attn_ext_vec_has_scap && |
| 396 | !FC_flash_attn_ext_vec_has_bias) { |
| 397 | ss[qq*C + NE*tx + ty] = fma(mqk[qq][tx], args.scale, (qk_t) sm[qq*C + NE*tx + ty]); |
| 398 | } else { |
| 399 | mqk[qq][tx] *= args.scale; |
| 400 | |
| 401 | if (FC_flash_attn_ext_vec_has_scap) { |
| 402 | mqk[qq][tx] = args.logit_softcap*precise::tanh(mqk[qq][tx]); |
| 403 | } |
| 404 | |
| 405 | if (FC_flash_attn_ext_vec_has_bias) { |
| 406 | mqk[qq][tx] += (qk_t) sm[qq*C + NE*tx + ty]*slope; |
| 407 | } else { |
| 408 | mqk[qq][tx] += (qk_t) sm[qq*C + NE*tx + ty]; |
| 409 | } |
| 410 | |
| 411 | ss[qq*C + NE*tx + ty] = mqk[qq][tx]; |
| 412 | } |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | simdgroup_barrier(mem_flags::mem_threadgroup); |
| 417 | |
| 418 | // online softmax |
| 419 | { |
| 420 | FOR_UNROLL (short qq = 0; qq < Q; ++qq) { |
| 421 | const float m = M[qq]; |
| 422 | const float s = ss[qq*C + tiisg]; |
| 423 | |
| 424 | M[qq] = simd_max(max(M[qq], s)); |
| 425 | |
| 426 | const float ms = exp(m - M[qq]); |
| 427 | const float vs = exp(s - M[qq]); |
| 428 | |
| 429 | S[qq] = S[qq]*ms + simd_sum(vs); |
| 430 | |
| 431 | // the P matrix from the paper (Q rows, C columns) |
| 432 | ss[qq*C + tiisg] = vs; |
| 433 | |
| 434 | // O = diag(ms)*O |
| 435 | if ((DV4/NL % NW == 0) || ty == 0) { |
| 436 | FOR_UNROLL (short ii = 0; ii < DV4/NL; ++ii) { |
| 437 | so4[qq*DV4 + ii*NL] *= ms; |
| 438 | } |
| 439 | } |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | simdgroup_barrier(mem_flags::mem_threadgroup); |
| 444 | |
| 445 | // O = O + (Q*K^T)*V |
| 446 | { |
| 447 | o4_t lo[Q][DV4/NL]; |
| 448 | FOR_UNROLL (short qq = 0; qq < Q; ++qq) { |
| 449 | FOR_UNROLL (short ii = 0; ii < DV4/NL; ++ii) { |
| 450 | lo[qq][ii] = 0.0f; |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | if (FC_flash_attn_ext_vec_has_sparse || FC_flash_attn_ext_vec_has_rows) { |
| 455 | FOR_UNROLL (short cc = 0; cc < C/NE; ++cc) { |
| 456 | // the KV rows are gathered from the index list; -1 entries are padding |
| 457 | const int i11 = spidx[NE*cc + ty]; |
| 458 | if (i11 >= 0) { |
| 459 | if (is_same<vd4_t, v4_t>::value) { |
| 460 | device const v4_t * pv4 = (device const v4_t *) (v + i11*args.nb21); |
| 461 | |
| 462 | pv4 += tx; |
| 463 | |
| 464 | FOR_UNROLL (short ii = 0; ii < DV4/NL; ++ii) { |
| 465 | const v4_t v_elem = pv4[ii*NL]; |
| 466 | FOR_UNROLL (short qq = 0; qq < Q; ++qq) { |
| 467 | lo[qq][ii] += o4_t(float4(v_elem)*float4(ss[qq*C + cc*NE + ty])); |
| 468 | } |
| 469 | } |
| 470 | } else { |
| 471 | device const vd4_t * pv4 = (device const vd4_t *) (v + i11*args.nb21); |
| 472 | |
| 473 | FOR_UNROLL (short ii = 0; ii < DV4/NL; ++ii) { |
| 474 | const short i = ii*NL + tx; |
| 475 | |
| 476 | v4_t mv; |
| 477 | |
| 478 | deq_v_t4(pv4 + i/nl_v, i%nl_v, mv); |
| 479 | |
| 480 | FOR_UNROLL (short qq = 0; qq < Q; ++qq) { |
| 481 | lo[qq][ii] += o4_t(float4(mv)*float4(ss[qq*C + cc*NE + ty])); |
| 482 | } |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | } |
| 487 | } else if (is_same<vd4_t, v4_t>::value) { |
| 488 | device const v4_t * pv4 = (device const v4_t *) (v + ic*args.nb21); |
| 489 | |
| 490 | pv4 += ty*NS20/4 + tx; |
| 491 | |
| 492 | FOR_UNROLL (short cc = 0; cc < C/NE; ++cc) { |
| 493 | FOR_UNROLL (short ii = 0; ii < DV4/NL; ++ii) { |
| 494 | const v4_t v_elem = pv4[cc*NE*NS20/4 + ii*NL]; |
| 495 | FOR_UNROLL (short qq = 0; qq < Q; ++qq) { |
| 496 | lo[qq][ii] += o4_t(float4(v_elem)*float4(ss[qq*C + cc*NE + ty])); |
| 497 | } |
| 498 | } |
| 499 | } |
| 500 | } else { |
| 501 | FOR_UNROLL (short cc = 0; cc < C/NE; ++cc) { |
| 502 | device const vd4_t * pv4 = (device const vd4_t *) (v + ((ic + NE*cc + ty)*args.nb21)); |
| 503 | |
| 504 | FOR_UNROLL (short ii = 0; ii < DV4/NL; ++ii) { |
| 505 | const short i = ii*NL + tx; |
| 506 | |
| 507 | v4_t mv; |
| 508 | deq_v_t4(pv4 + i/nl_v, i%nl_v, mv); |
| 509 | |
| 510 | FOR_UNROLL (short qq = 0; qq < Q; ++qq) { |
| 511 | lo[qq][ii] += o4_t(float4(mv)*float4(ss[qq*C + NE*cc + ty])); |
| 512 | } |
| 513 | } |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | FOR_UNROLL (short qq = 0; qq < Q; ++qq) { |
| 518 | FOR_UNROLL (short ii = 0; ii < DV4/NL; ++ii) { |
| 519 | if (NE > 1) { |
| 520 | lo[qq][ii][0] += simd_shuffle_down(lo[qq][ii][0], 16); |
| 521 | lo[qq][ii][1] += simd_shuffle_down(lo[qq][ii][1], 16); |
| 522 | lo[qq][ii][2] += simd_shuffle_down(lo[qq][ii][2], 16); |
| 523 | lo[qq][ii][3] += simd_shuffle_down(lo[qq][ii][3], 16); |
| 524 | } |
| 525 | |
| 526 | if (NE > 2) { |
| 527 | lo[qq][ii][0] += simd_shuffle_down(lo[qq][ii][0], 8); |
| 528 | lo[qq][ii][1] += simd_shuffle_down(lo[qq][ii][1], 8); |
| 529 | lo[qq][ii][2] += simd_shuffle_down(lo[qq][ii][2], 8); |
| 530 | lo[qq][ii][3] += simd_shuffle_down(lo[qq][ii][3], 8); |
| 531 | } |
| 532 | |
| 533 | if (NE > 4) { |
| 534 | lo[qq][ii][0] += simd_shuffle_down(lo[qq][ii][0], 4); |
| 535 | lo[qq][ii][1] += simd_shuffle_down(lo[qq][ii][1], 4); |
| 536 | lo[qq][ii][2] += simd_shuffle_down(lo[qq][ii][2], 4); |
| 537 | lo[qq][ii][3] += simd_shuffle_down(lo[qq][ii][3], 4); |
| 538 | } |
| 539 | |
| 540 | if (NE > 8) { |
| 541 | lo[qq][ii][0] += simd_shuffle_down(lo[qq][ii][0], 2); |
| 542 | lo[qq][ii][1] += simd_shuffle_down(lo[qq][ii][1], 2); |
| 543 | lo[qq][ii][2] += simd_shuffle_down(lo[qq][ii][2], 2); |
| 544 | lo[qq][ii][3] += simd_shuffle_down(lo[qq][ii][3], 2); |
| 545 | } |
| 546 | |
| 547 | if (NE > 16) { |
| 548 | lo[qq][ii][0] += simd_shuffle_down(lo[qq][ii][0], 1); |
| 549 | lo[qq][ii][1] += simd_shuffle_down(lo[qq][ii][1], 1); |
| 550 | lo[qq][ii][2] += simd_shuffle_down(lo[qq][ii][2], 1); |
| 551 | lo[qq][ii][3] += simd_shuffle_down(lo[qq][ii][3], 1); |
| 552 | } |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | if ((DV4/NL % NW == 0) || ty == 0) { |
| 557 | FOR_UNROLL (short qq = 0; qq < Q; ++qq) { |
| 558 | FOR_UNROLL (short ii = 0; ii < DV4/NL; ++ii) { |
| 559 | so4[qq*DV4 + ii*NL] += lo[qq][ii]; |
| 560 | } |
| 561 | } |
| 562 | } |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | if (FC_flash_attn_ext_vec_has_sinks && sgitg == 0 && iwg == 0) { |
| 567 | FOR_UNROLL (short qq = 0; qq < Q; ++qq) { |
| 568 | const float m = M[qq]; |
| 569 | const float s = tiisg == 0 ? ((device const float *) sinks)[iq2] : -FLT_MAX/2; |
| 570 | |
| 571 | M[qq] = simd_max(max(M[qq], s)); |
| 572 | |
| 573 | const float ms = exp(m - M[qq]); |
| 574 | const float vs = exp(s - M[qq]); |
| 575 | |
| 576 | S[qq] = S[qq]*ms + simd_sum(vs); |
| 577 | |
| 578 | if ((DV4/NL % NW == 0) || ty == 0) { |
| 579 | FOR_UNROLL (short ii = 0; ii < DV4/NL; ++ii) { |
| 580 | so4[qq*DV4 + ii*NL] *= ms; |
| 581 | } |
| 582 | } |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | // these are needed for reducing the results from the simdgroups (reuse the ss buffer) |
| 587 | if (tiisg == 0) { |
| 588 | FOR_UNROLL (short qq = 0; qq < Q; ++qq) { |
| 589 | ss[2*qq + 0] = (s_t) S[qq]; |
| 590 | ss[2*qq + 1] = (s_t) M[qq]; |
| 591 | } |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | so4 -= tiisg; |
| 596 | |
| 597 | threadgroup_barrier(mem_flags::mem_threadgroup); |
| 598 | |
| 599 | // parallel reduce |
| 600 | for (short r = NSG/2; r > 0; r >>= 1) { |
| 601 | if (sgitg < r) { |
| 602 | FOR_UNROLL (short qq = 0; qq < Q; ++qq) { |
| 603 | const float S0 = ss[ 2*qq + 0]; |
| 604 | const float S1 = ss[r*(SH/2) + 2*qq + 0]; |
| 605 | |
| 606 | const float M0 = ss[ 2*qq + 1]; |
| 607 | const float M1 = ss[r*(SH/2) + 2*qq + 1]; |
| 608 | |
| 609 | const float Mx = max(M0, M1); |
| 610 | |
| 611 | const float ms0 = exp(M0 - Mx); |
| 612 | const float ms1 = exp(M1 - Mx); |
| 613 | |
| 614 | const float Sx = S0*ms0 + S1*ms1; |
| 615 | |
| 616 | if (tiisg == 0) { |
| 617 | ss[2*qq + 0] = Sx; |
| 618 | ss[2*qq + 1] = Mx; |
| 619 | } |
| 620 | |
| 621 | // O_0 = diag(ms0)*O_0 + diag(ms1)*O_1 |
| 622 | for (short i = tiisg; i < DV4; i += NW) { |
| 623 | so4[qq*DV4 + i] = so4[qq*DV4 + i]*ms0 + so4[qq*DV4 + i + r*Q*PV4]*ms1; |
| 624 | } |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | threadgroup_barrier(mem_flags::mem_threadgroup); |
| 629 | } |
| 630 | |
| 631 | // final rescale with 1/S and store to global memory |
| 632 | if (sgitg == 0) { |
| 633 | const int64_t nrows = args.ne3*args.ne2*args.ne1; |
| 634 | |
| 635 | device float4 * dst4 = (device float4 *) dst; |
| 636 | device float * dst1 = (device float *) dst + nrows*DV*NWG; // the S and M are stored after the results |
| 637 | |
| 638 | FOR_UNROLL (short qq = 0; qq < Q; ++qq) { |
| 639 | const int iq1_q = iq1*Q + qq; |
| 640 | if (iq1_q >= args.ne01) { |
| 641 | continue; |
| 642 | } |
| 643 | |
| 644 | const int64_t rid = iq3*args.ne2*args.ne1 + iq2 + iq1_q*args.ne1; |
| 645 | |
| 646 | const float Sval = NWG == 1 ? (ss[2*qq + 0] == 0.0f ? 0.0f : 1.0f/ss[2*qq + 0]) : 1.0f; |
| 647 | |
| 648 | // interleave the workgroup data |
| 649 | for (short i = tiisg; i < DV4; i += NW) { |
| 650 | dst4[rid*DV4*NWG + NWG*i + iwg] = (float4) so4[qq*DV4 + i]*Sval; |
| 651 | } |
| 652 | |
| 653 | // store S and M |
| 654 | if (NWG > 1) { |
| 655 | if (tiisg == 0) { |
| 656 | dst1[rid*(2*NWG) + 2*iwg + 0] = ss[2*qq + 0]; |
| 657 | dst1[rid*(2*NWG) + 2*iwg + 1] = ss[2*qq + 1]; |
| 658 | } |
| 659 | } |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | #undef NWG |
| 664 | #undef NSG |
| 665 | #undef NS10 |
| 666 | #undef NS20 |
| 667 | } |