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_aux.metal
| 1 | #include "common.h" |
| 2 | #include "dequantize.h" |
| 3 | |
| 4 | // dequantize a quantized KV cache tensor to contiguous F16 before running the F16 flash attention kernels |
| 5 | // - one thread per block; dispatched separately for K and V |
| 6 | // - ref: https://github.com/ggml-org/llama.cpp/pull/27390 |
| 7 | template < |
| 8 | typename block_t, |
| 9 | short QK, |
| 10 | void (*deq_t4x4)(device const block_t *, short, thread float4x4 &)> |
| 11 | kernel void kernel_flash_attn_ext_kv_f16( |
| 12 | constant ggml_metal_kargs_flash_attn_ext_kv_f16 & args, |
| 13 | device const char * x, |
| 14 | device half * x_dst, |
| 15 | uint gid [[thread_position_in_grid]]) { |
| 16 | if (gid >= (uint) args.nblocks) { |
| 17 | return; |
| 18 | } |
| 19 | |
| 20 | const uint nb = args.ne0/QK; |
| 21 | const uint i0 = gid%nb; |
| 22 | uint ib = gid/nb; |
| 23 | const uint i1 = ib%args.ne1; |
| 24 | ib /= args.ne1; |
| 25 | const uint i2 = ib%args.ne2; |
| 26 | const uint i3 = ib/args.ne2; |
| 27 | |
| 28 | const uint64_t offs = i0*args.nb0 + i1*args.nb1 + i2*args.nb2 + i3*args.nb3; |
| 29 | |
| 30 | device const block_t * src = (device const block_t *) (x + offs); |
| 31 | device half4 * dst = (device half4 *) x_dst + (QK/4)*gid; |
| 32 | |
| 33 | for (short i = 0; i < QK/16; ++i) { |
| 34 | float4x4 reg; |
| 35 | deq_t4x4(src, i, reg); |
| 36 | dst[4*i + 0] = (half4) reg[0]; |
| 37 | dst[4*i + 1] = (half4) reg[1]; |
| 38 | dst[4*i + 2] = (half4) reg[2]; |
| 39 | dst[4*i + 3] = (half4) reg[3]; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | typedef decltype(kernel_flash_attn_ext_kv_f16<block_q8_0, 32, dequantize_q8_0>) kernel_flash_attn_ext_kv_f16_t; |
| 44 | |
| 45 | template [[host_name("kernel_flash_attn_ext_kv_q4_0_f16")]] kernel kernel_flash_attn_ext_kv_f16_t kernel_flash_attn_ext_kv_f16<block_q4_0, 32, dequantize_q4_0>; |
| 46 | template [[host_name("kernel_flash_attn_ext_kv_q4_1_f16")]] kernel kernel_flash_attn_ext_kv_f16_t kernel_flash_attn_ext_kv_f16<block_q4_1, 32, dequantize_q4_1>; |
| 47 | template [[host_name("kernel_flash_attn_ext_kv_q5_0_f16")]] kernel kernel_flash_attn_ext_kv_f16_t kernel_flash_attn_ext_kv_f16<block_q5_0, 32, dequantize_q5_0>; |
| 48 | template [[host_name("kernel_flash_attn_ext_kv_q5_1_f16")]] kernel kernel_flash_attn_ext_kv_f16_t kernel_flash_attn_ext_kv_f16<block_q5_1, 32, dequantize_q5_1>; |
| 49 | template [[host_name("kernel_flash_attn_ext_kv_q8_0_f16")]] kernel kernel_flash_attn_ext_kv_f16_t kernel_flash_attn_ext_kv_f16<block_q8_0, 32, dequantize_q8_0>; |
| 50 | |
| 51 | // gather the K/V cache rows referenced by the kv_rows lists into contiguous F16 buffers |
| 52 | // - one threadgroup per (row, stream); the attention is then run as a standard dense op over the |
| 53 | // gathered rows, which avoids the per-tile K/V gather in the main kernels |
| 54 | // - negative rows are padding: filled with zeros, their mask entries are -INF so they contribute nothing |
| 55 | kernel void kernel_flash_attn_ext_rows( |
| 56 | constant ggml_metal_kargs_flash_attn_ext_rows & args, |
| 57 | device const char * k_src, |
| 58 | device const char * v_src, |
| 59 | device const int * rows, |
| 60 | device char * dst, |
| 61 | uint3 tgpig[[threadgroup_position_in_grid]], |
| 62 | ushort tiitg[[thread_index_in_threadgroup]], |
| 63 | ushort3 ntg[[threads_per_threadgroup]]) { |
| 64 | const int64_t i1 = tgpig[0]; // row index |
| 65 | const int64_t i3 = tgpig[1]; // stream |
| 66 | |
| 67 | const int64_t row = rows[i1 + i3*args.ne11]; |
| 68 | |
| 69 | // gathered layout: [DK, n_rows, n_head_kv, n_streams] and [DV, n_rows, n_head_kv, n_streams] F16 |
| 70 | device half4 * k_dst = (device half4 *) (dst + i3*args.ne_12_2*args.ne11*args.ne10*sizeof(half)); |
| 71 | device half4 * v_dst = (device half4 *) (dst + args.v_off + i3*args.ne_12_2*args.ne11*args.ne20*sizeof(half)); |
| 72 | |
| 73 | // note: the K/V cache is shared by all streams (ne13 = ne23 = 1), the row list selects the rows |
| 74 | // each thread copies up to 4 half4 values (32 bytes) to keep the memory transactions large |
| 75 | |
| 76 | // K: all heads of this row |
| 77 | { |
| 78 | const int64_t per_head = args.ne10/4; |
| 79 | |
| 80 | for (int64_t i = 4*tiitg; i < args.ne_12_2*per_head; i += 4*ntg.x) { |
| 81 | const int64_t i2 = i/per_head; |
| 82 | const int64_t i0 = i%per_head; |
| 83 | const int n = (int) min((int64_t) 4, per_head - i0); |
| 84 | |
| 85 | device half4 * dst4 = k_dst + (i2*args.ne11 + i1)*per_head + i0; |
| 86 | |
| 87 | if (row >= 0) { |
| 88 | device const half4 * src4 = (device const half4 *) (k_src + row*args.nb11 + i2*args.nb12) + i0; |
| 89 | for (int j = 0; j < n; ++j) { |
| 90 | dst4[j] = src4[j]; |
| 91 | } |
| 92 | } else { |
| 93 | for (int j = 0; j < n; ++j) { |
| 94 | dst4[j] = half4(0.0h); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | // V: all heads of this row |
| 101 | { |
| 102 | const int64_t per_head = args.ne20/4; |
| 103 | |
| 104 | for (int64_t i = 4*tiitg; i < args.ne_12_2*per_head; i += 4*ntg.x) { |
| 105 | const int64_t i2 = i/per_head; |
| 106 | const int64_t i0 = i%per_head; |
| 107 | const int n = (int) min((int64_t) 4, per_head - i0); |
| 108 | |
| 109 | device half4 * dst4 = v_dst + (i2*args.ne11 + i1)*per_head + i0; |
| 110 | |
| 111 | if (row >= 0) { |
| 112 | device const half4 * src4 = (device const half4 *) (v_src + row*args.nb21 + i2*args.nb22) + i0; |
| 113 | for (int j = 0; j < n; ++j) { |
| 114 | dst4[j] = src4[j]; |
| 115 | } |
| 116 | } else { |
| 117 | for (int j = 0; j < n; ++j) { |
| 118 | dst4[j] = half4(0.0h); |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | constant bool FC_flash_attn_ext_pad_has_mask [[function_constant(FC_FLASH_ATTN_EXT_PAD + 0)]]; |
| 126 | |
| 127 | constant int32_t FC_flash_attn_ext_pad_ncpsg [[function_constant(FC_FLASH_ATTN_EXT_PAD + 25)]]; |
| 128 | |
| 129 | // pad the last chunk of C elements of k and v into a an extra pad buffer |
| 130 | kernel void kernel_flash_attn_ext_pad( |
| 131 | constant ggml_metal_kargs_flash_attn_ext_pad & args, |
| 132 | device const char * k, |
| 133 | device const char * v, |
| 134 | device const char * mask, |
| 135 | device char * dst, |
| 136 | uint3 tgpig[[threadgroup_position_in_grid]], |
| 137 | ushort tiitg[[thread_index_in_threadgroup]], |
| 138 | ushort3 ntg[[threads_per_threadgroup]]) { |
| 139 | const int32_t C = FC_flash_attn_ext_pad_ncpsg; |
| 140 | |
| 141 | device char * k_pad = dst; |
| 142 | device char * v_pad = k_pad + args.nb11*C*args.ne_12_2*args.ne_12_3; |
| 143 | device char * mask_pad = v_pad + args.nb21*C*args.ne_12_2*args.ne_12_3; |
| 144 | |
| 145 | const int32_t icp = args.ne11 % C; |
| 146 | const int32_t ic0 = args.ne11 - icp; |
| 147 | |
| 148 | const int32_t i1 = tgpig[0]; |
| 149 | const int32_t i2 = tgpig[1]; |
| 150 | const int32_t i3 = tgpig[2]; |
| 151 | |
| 152 | if (i2 < args.ne_12_2 && i3 < args.ne_12_3) { |
| 153 | device const char * k_src = k + args.nb11*(ic0 + i1) + args.nb12*i2 + args.nb13*i3; |
| 154 | device const char * v_src = v + args.nb21*(ic0 + i1) + args.nb22*i2 + args.nb23*i3; |
| 155 | |
| 156 | device char * k_dst = k_pad + args.nb11*i1 + args.nb11*C*i2 + args.nb11*C*args.ne_12_2*i3; |
| 157 | device char * v_dst = v_pad + args.nb21*i1 + args.nb21*C*i2 + args.nb21*C*args.ne_12_2*i3; |
| 158 | |
| 159 | if (i1 >= icp) { |
| 160 | // here it is not important the exact value that will be used as we rely on masking out the scores in the attention |
| 161 | for (uint64_t i = tiitg; i < args.nb11; i += ntg.x) { |
| 162 | k_dst[i] = 0; |
| 163 | } |
| 164 | for (uint64_t i = tiitg; i < args.nb21; i += ntg.x) { |
| 165 | v_dst[i] = 0; |
| 166 | } |
| 167 | } else { |
| 168 | for (uint64_t i = tiitg; i < args.nb11; i += ntg.x) { |
| 169 | k_dst[i] = k_src[i]; |
| 170 | } |
| 171 | for (uint64_t i = tiitg; i < args.nb21; i += ntg.x) { |
| 172 | v_dst[i] = v_src[i]; |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | if (FC_flash_attn_ext_pad_has_mask) { |
| 178 | if (i2 < args.ne32 && i3 < args.ne33) { |
| 179 | for (int ib = i1; ib < args.ne31; ib += C) { |
| 180 | device const half * mask_src = (device const half *)(mask + args.nb31*ib + args.nb32*i2 + args.nb33*i3) + ic0; |
| 181 | device half * mask_dst = (device half *)(mask_pad) + C*ib + C*args.ne31*i2 + C*args.ne31*args.ne32*i3; |
| 182 | |
| 183 | for (int i = tiitg; i < C; i += ntg.x) { |
| 184 | if (i >= icp) { |
| 185 | mask_dst[i] = -MAXHALF; |
| 186 | } else { |
| 187 | mask_dst[i] = mask_src[i]; |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | constant int32_t FC_flash_attn_ext_blk_nqptg [[function_constant(FC_FLASH_ATTN_EXT_BLK + 24)]]; |
| 196 | constant int32_t FC_flash_attn_ext_blk_ncpsg [[function_constant(FC_FLASH_ATTN_EXT_BLK + 25)]]; |
| 197 | |
| 198 | // scan the blocks of the mask that are not masked |
| 199 | // 0 - masked (i.e. full of -INF, skip) |
| 200 | // 1 - not masked (i.e. at least one element of the mask is not -INF) |
| 201 | // 2 - all zero |
| 202 | kernel void kernel_flash_attn_ext_blk( |
| 203 | constant ggml_metal_kargs_flash_attn_ext_blk & args, |
| 204 | device const char * mask, |
| 205 | device char * dst, |
| 206 | uint3 tgpig[[threadgroup_position_in_grid]], |
| 207 | ushort tiisg[[thread_index_in_simdgroup]]) { |
| 208 | // block size C x Q |
| 209 | const int32_t Q = FC_flash_attn_ext_blk_nqptg; |
| 210 | const int32_t C = FC_flash_attn_ext_blk_ncpsg; |
| 211 | |
| 212 | constexpr short NW = N_SIMDWIDTH; |
| 213 | |
| 214 | const int32_t i3 = tgpig[2]/args.ne32; |
| 215 | const int32_t i2 = tgpig[2]%args.ne32; |
| 216 | const int32_t i1 = tgpig[1]; |
| 217 | const int32_t i0 = tgpig[0]; |
| 218 | |
| 219 | char res = i0*C + C > args.ne30 || i1*Q + Q > args.ne31 ? 1 : 0; |
| 220 | |
| 221 | device const half * mask_src = (device const half *) (mask + (i1*Q)*args.nb31 + i2*args.nb32 + i3*args.nb33) + i0*C + tiisg; |
| 222 | |
| 223 | // detailed check of the elements of the block |
| 224 | if ((C > NW || Q > 1) && res == 0) { |
| 225 | half mmin = MAXHALF; |
| 226 | half mmax = -MAXHALF; |
| 227 | |
| 228 | FOR_UNROLL (short j = 0; j < Q; ++j) { |
| 229 | FOR_UNROLL (short ii = 0; ii < C/NW; ++ii) { |
| 230 | mmin = min(mmin, mask_src[ii*NW]); |
| 231 | mmax = max(mmax, mask_src[ii*NW]); |
| 232 | } |
| 233 | |
| 234 | mask_src += args.nb31/2; |
| 235 | } |
| 236 | |
| 237 | mmin = simd_min(mmin); |
| 238 | mmax = simd_max(mmax); |
| 239 | |
| 240 | if (mmax > -MAXHALF) { |
| 241 | if (mmin == 0.0 && mmax == 0.0) { |
| 242 | res = 2; |
| 243 | } else { |
| 244 | res = 1; |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | const int32_t nblk1 = ((args.ne01 + Q - 1)/Q); |
| 250 | const int32_t nblk0 = ((args.ne30 + C - 1)/C); |
| 251 | |
| 252 | if (tiisg == 0) { |
| 253 | dst[((i3*args.ne32 + i2)*nblk1 + i1)*nblk0 + i0] = res; |
| 254 | } |
| 255 | } |
| 256 | // compress the finite entries of each KQ mask row into a list of KV indices (ascending order), |
| 257 | // padded with -1 up to n_kv_max_padded (a multiple of OP_FLASH_ATTN_EXT_VEC_NCPSG) |
| 258 | // one threadgroup per mask row; the mask remains the single source of truth for the values |
| 259 | kernel void kernel_flash_attn_ext_vec_idx( |
| 260 | constant ggml_metal_kargs_flash_attn_ext_vec_idx & args, |
| 261 | device const half * mask, |
| 262 | device int * idx, |
| 263 | uint3 tgpig[[threadgroup_position_in_grid]], |
| 264 | ushort tiitg[[thread_index_in_threadgroup]], |
| 265 | ushort3 ntg[[threads_per_threadgroup]]) { |
| 266 | constexpr short NW = N_SIMDWIDTH; |
| 267 | constexpr short NLOCAL = 32; // max finite positions kept in registers per thread |
| 268 | |
| 269 | const int i1 = tgpig[0]; |
| 270 | const int i2 = tgpig[1]; |
| 271 | const int i3 = tgpig[2]; |
| 272 | |
| 273 | device const half * pm = (device const half *) ((device const char *) mask + i1*args.nb31 + i2*args.nb32 + i3*args.nb33); |
| 274 | device int * pidx = idx + (((int64_t)i3*args.ne32 + i2)*args.ne31 + i1)*args.n_kv_max_padded; |
| 275 | |
| 276 | const int n = args.ne30; |
| 277 | const int q = n/ntg.x; |
| 278 | const int r = n%ntg.x; |
| 279 | |
| 280 | // each thread handles a contiguous slice of the mask row |
| 281 | const int r0 = q*tiitg + min((int) tiitg, r); |
| 282 | const int r1 = r0 + q + (tiitg < r ? 1 : 0); |
| 283 | |
| 284 | // count the finite entries in the slice and keep their positions in registers (single mask read) |
| 285 | int cnt = 0; // total finite entries in the slice |
| 286 | int nloc = 0; // finite entries kept in registers |
| 287 | int local[NLOCAL]; |
| 288 | for (int i = r0; i < r1; ++i) { |
| 289 | if (isfinite((float) pm[i])) { |
| 290 | if (nloc < NLOCAL) { |
| 291 | local[nloc] = i; |
| 292 | nloc++; |
| 293 | } |
| 294 | cnt++; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | const short sgitg = tiitg/NW; |
| 299 | const short tiisg = tiitg%NW; |
| 300 | |
| 301 | threadgroup int tcount[8]; |
| 302 | |
| 303 | // simd_sum is a collective: all lanes must evaluate it |
| 304 | const int sg_sum = simd_sum(cnt); |
| 305 | if (tiisg == 0) { |
| 306 | tcount[sgitg] = sg_sum; |
| 307 | } |
| 308 | |
| 309 | threadgroup_barrier(mem_flags::mem_threadgroup); |
| 310 | |
| 311 | int total = 0; |
| 312 | for (short s = 0; s < ntg.x/NW; ++s) { |
| 313 | total += tcount[s]; |
| 314 | } |
| 315 | |
| 316 | // base offset of this thread's slice in the output list (exclusive scan within the simdgroup) |
| 317 | int sg_base = 0; |
| 318 | for (short s = 0; s < sgitg; ++s) { |
| 319 | sg_base += tcount[s]; |
| 320 | } |
| 321 | |
| 322 | // exclusive prefix scan of the per-thread counts within the simdgroup |
| 323 | int incl = cnt; |
| 324 | for (int d = 1; d < NW; d <<= 1) { |
| 325 | const int v = simd_shuffle_up(incl, d); |
| 326 | if (tiisg >= d) { |
| 327 | incl += v; |
| 328 | } |
| 329 | } |
| 330 | const int base = sg_base + (incl - cnt); |
| 331 | |
| 332 | // write the finite positions in order; if the hint is violated, keep only the first n_kv_max entries |
| 333 | int j = 0; |
| 334 | for (; j < nloc && base + j < args.n_kv_max; ++j) { |
| 335 | pidx[base + j] = local[j]; |
| 336 | } |
| 337 | |
| 338 | // a dense mask may have more than NLOCAL finite entries in a slice; re-read the mask to write the rest |
| 339 | if (cnt > nloc && base + nloc < args.n_kv_max) { |
| 340 | int j2 = 0; |
| 341 | for (int i = r0; i < r1; ++i) { |
| 342 | if (isfinite((float) pm[i])) { |
| 343 | if (j2 >= nloc) { |
| 344 | pidx[base + j2] = i; |
| 345 | } |
| 346 | j2++; |
| 347 | if (base + j2 >= args.n_kv_max) { |
| 348 | break; |
| 349 | } |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | // pad the tail of the list with -1 |
| 355 | const int count = min(total, args.n_kv_max); |
| 356 | for (int i = count + tiitg; i < args.n_kv_max_padded; i += ntg.x) { |
| 357 | pidx[i] = -1; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | constant int32_t FC_flash_attn_ext_vec_reduce_DV [[function_constant(FC_FLASH_ATTN_EXT_VEC_REDUCE + 0)]]; |
| 362 | constant int32_t FC_flash_attn_ext_vec_reduce_NWG [[function_constant(FC_FLASH_ATTN_EXT_VEC_REDUCE + 1)]]; |
| 363 | |
| 364 | kernel void kernel_flash_attn_ext_vec_reduce( |
| 365 | constant ggml_metal_kargs_flash_attn_ext_vec_reduce & args, |
| 366 | device const char * htmp, |
| 367 | device char * dst, |
| 368 | uint tgpig[[threadgroup_position_in_grid]], |
| 369 | ushort tiisg[[thread_index_in_simdgroup]], |
| 370 | ushort sgitg[[simdgroup_index_in_threadgroup]]) { |
| 371 | #define NWG (FC_flash_attn_ext_vec_reduce_NWG) |
| 372 | #define DV (FC_flash_attn_ext_vec_reduce_DV) |
| 373 | |
| 374 | const uint64_t rid = tgpig; |
| 375 | |
| 376 | const short iwg = tiisg; |
| 377 | |
| 378 | device const float * ss = (device const float *) htmp + (uint64_t)args.nrows*DV*NWG; |
| 379 | |
| 380 | float S = ss[rid*(2*NWG) + 2*iwg + 0]; |
| 381 | float M = ss[rid*(2*NWG) + 2*iwg + 1]; |
| 382 | |
| 383 | const float m = simd_max(M); |
| 384 | const float ms = exp(M - m); |
| 385 | |
| 386 | S = simd_sum(S*ms); |
| 387 | S = S == 0.0f ? 0.0f : 1.0f/S; |
| 388 | |
| 389 | const short DV4 = DV/4; |
| 390 | |
| 391 | device const float4 * htmp4 = (device const float4 *) htmp + rid*DV4*NWG; |
| 392 | device float4 * dst4 = (device float4 *) dst + rid*DV4; |
| 393 | |
| 394 | for (short i = sgitg; i < DV4; i += NWG) { |
| 395 | const float4 v = simd_sum(htmp4[i*NWG + iwg]*ms); |
| 396 | |
| 397 | if (iwg == 0) { |
| 398 | dst4[i] = v*S; |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | #undef NWG |
| 403 | #undef DV |
| 404 | } |
| 405 | |
| 406 | constant short FC_lightning_indexer_nh [[function_constant(FC_LIGHTNING_INDEXER + 0)]]; |
| 407 | |
| 408 | template< |
| 409 | typename kd4x4_t, |
| 410 | short nl_k, |
| 411 | void (*deq_k)(device const kd4x4_t *, short, thread half4x4 &)> |
| 412 | kernel void kernel_lightning_indexer( |
| 413 | constant ggml_metal_kargs_lightning_indexer & args, |
| 414 | device const char * q, |
| 415 | device const char * k, |
| 416 | device const char * w, |
| 417 | device const char * m, |
| 418 | device char * dst, |
| 419 | uint3 tgpig[[threadgroup_position_in_grid]], |
| 420 | ushort tiitg[[thread_index_in_threadgroup]], |
| 421 | ushort tiisg[[thread_index_in_simdgroup]], |
| 422 | ushort sgitg[[simdgroup_index_in_threadgroup]]) { |
| 423 | constexpr short DK = OP_LIGHTNING_INDEXER_DK; |
| 424 | const short NH = FC_lightning_indexer_nh; |
| 425 | constexpr short NHPTG = OP_LIGHTNING_INDEXER_NHPTG; |
| 426 | constexpr short NKPSG = OP_LIGHTNING_INDEXER_NKPSG; |
| 427 | constexpr short NSG = OP_LIGHTNING_INDEXER_NSG; |
| 428 | constexpr short NBPTG = OP_LIGHTNING_INDEXER_NBPTG; |
| 429 | |
| 430 | constexpr short DK4 = DK/4; |
| 431 | constexpr short DK8 = DK/8; |
| 432 | constexpr short DK16 = DK/16; |
| 433 | |
| 434 | constexpr short NK = NKPSG*NSG; // keys per threadgroup |
| 435 | constexpr short NTG = 32*NSG; // threads per threadgroup |
| 436 | |
| 437 | const int i_stream = tgpig.z; |
| 438 | const int i_kv_0 = tgpig.x*NK; // first key of this threadgroup |
| 439 | const int i_kv = i_kv_0 + sgitg*NKPSG; // first key of this simdgroup |
| 440 | |
| 441 | threadgroup half sk[NK * DK16 * 16]; |
| 442 | threadgroup half4x4 * sk4x4 = (threadgroup half4x4 *) sk; |
| 443 | |
| 444 | for (short i = tiitg; i < NK*DK16; i += NTG) { |
| 445 | const short ik = i/DK16; |
| 446 | const short i16 = i%DK16; |
| 447 | |
| 448 | half4x4 tmp; |
| 449 | |
| 450 | if (i_kv_0 + ik < args.n_kv) { |
| 451 | device const kd4x4_t * kr = (device const kd4x4_t *) (k + (i_kv_0 + ik)*args.nbk2 + i_stream*args.nbk3); |
| 452 | |
| 453 | deq_k(kr + i16/nl_k, i16%nl_k, tmp); |
| 454 | } else { |
| 455 | FOR_UNROLL (short j = 0; j < 4; ++j) { |
| 456 | tmp[j] = half4(0.0h); |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | sk4x4[i] = tmp; |
| 461 | } |
| 462 | |
| 463 | threadgroup_barrier(mem_flags::mem_threadgroup); |
| 464 | |
| 465 | // K tile of this simdgroup, transposed to [DK, NKPSG] |
| 466 | simdgroup_half8x8 mk[DK8]; |
| 467 | |
| 468 | FOR_UNROLL (short i = 0; i < DK8; ++i) { |
| 469 | simdgroup_load(mk[i], sk + sgitg*NKPSG*DK + 8*i, DK, 0, true); |
| 470 | } |
| 471 | |
| 472 | threadgroup half4 sq4[NHPTG*DK4]; |
| 473 | threadgroup half * sq = (threadgroup half *) sq4; |
| 474 | |
| 475 | threadgroup float sw [NHPTG]; |
| 476 | threadgroup float sqk[NSG*NHPTG*NKPSG]; |
| 477 | |
| 478 | const int i_batch_0 = tgpig.y*NBPTG; |
| 479 | const int n_batch = min((int) NBPTG, args.n_batch - i_batch_0); |
| 480 | |
| 481 | for (short ib = 0; ib < n_batch; ++ib) { |
| 482 | const int i_batch = i_batch_0 + ib; |
| 483 | |
| 484 | device const char * pq = q + i_batch*args.nbq2 + i_stream*args.nbq3; |
| 485 | device const char * pw = w + i_batch*args.nbw1 + i_stream*args.nbw3; |
| 486 | |
| 487 | float score = 0.0f; |
| 488 | |
| 489 | FOR_UNROLL (short i_head = 0; i_head < NH; i_head += NHPTG) { |
| 490 | // stage the Q tile [DK, NHPTG] and the (prescaled) head weights, heads past NH are zero |
| 491 | for (short i = tiitg; i < NHPTG*DK4; i += NTG) { |
| 492 | const short ih = i/DK4; |
| 493 | const short i4 = i%DK4; |
| 494 | |
| 495 | if (i_head + ih < NH) { |
| 496 | device const float4 * q4 = (device const float4 *) (pq + (i_head + ih)*args.nbq1); |
| 497 | |
| 498 | sq4[ih*DK4 + i4] = half4(q4[i4]); |
| 499 | } else { |
| 500 | sq4[ih*DK4 + i4] = half4(0.0h); |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | if (tiitg < NHPTG) { |
| 505 | sw[tiitg] = i_head + tiitg < NH ? ((device const float *) pw)[i_head + tiitg] : 0.0f; |
| 506 | } |
| 507 | |
| 508 | threadgroup_barrier(mem_flags::mem_threadgroup); |
| 509 | |
| 510 | simdgroup_float8x8 mqk = make_filled_simdgroup_matrix<float, 8>(0.0f); |
| 511 | |
| 512 | FOR_UNROLL (short i = 0; i < DK8; ++i) { |
| 513 | simdgroup_half8x8 mq; |
| 514 | |
| 515 | simdgroup_load(mq, sq + 8*i, DK, 0, false); |
| 516 | simdgroup_multiply_accumulate(mqk, mq, mk[i], mqk); |
| 517 | } |
| 518 | |
| 519 | threadgroup float * pqk = sqk + sgitg*NHPTG*NKPSG; |
| 520 | |
| 521 | simdgroup_store(mqk, pqk, NKPSG, 0, false); |
| 522 | simdgroup_barrier(mem_flags::mem_threadgroup); |
| 523 | |
| 524 | // one lane per key: ReLU, apply the head weight and accumulate over the head tile |
| 525 | if (tiisg < NKPSG) { |
| 526 | FOR_UNROLL (short ih = 0; ih < NHPTG; ++ih) { |
| 527 | score += max(pqk[ih*NKPSG + tiisg], 0.0f)*sw[ih]; |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | threadgroup_barrier(mem_flags::mem_threadgroup); |
| 532 | } |
| 533 | |
| 534 | if (tiisg < NKPSG) { |
| 535 | const int ik = i_kv + tiisg; |
| 536 | if (ik < args.n_kv) { |
| 537 | device const half * pm = (device const half *) (m + i_batch*args.nbm1 + (i_stream % args.mask_ne3)*args.nbm3); |
| 538 | device float * pd = (device float *) (dst + i_batch*args.nb1 + i_stream*args.nb3); |
| 539 | |
| 540 | pd[ik] = score + (float) pm[ik]; |
| 541 | } |
| 542 | } |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | typedef decltype(kernel_lightning_indexer<half4x4, 1, dequantize_f16>) kernel_lightning_indexer_t; |
| 547 | |
| 548 | template [[host_name("kernel_lightning_indexer_f32")]] kernel kernel_lightning_indexer_t kernel_lightning_indexer<float4x4, 1, dequantize_f32>; |
| 549 | template [[host_name("kernel_lightning_indexer_f16")]] kernel kernel_lightning_indexer_t kernel_lightning_indexer<half4x4, 1, dequantize_f16>; |
| 550 | |
| 551 | #if defined(GGML_METAL_HAS_BF16) |
| 552 | template [[host_name("kernel_lightning_indexer_bf16")]] kernel kernel_lightning_indexer_t kernel_lightning_indexer<bfloat4x4, 1, dequantize_bf16>; |
| 553 | #endif |
| 554 | |
| 555 | template [[host_name("kernel_lightning_indexer_q4_0")]] kernel kernel_lightning_indexer_t kernel_lightning_indexer<block_q4_0, 2, dequantize_q4_0>; |
| 556 | template [[host_name("kernel_lightning_indexer_q4_1")]] kernel kernel_lightning_indexer_t kernel_lightning_indexer<block_q4_1, 2, dequantize_q4_1>; |
| 557 | template [[host_name("kernel_lightning_indexer_q5_0")]] kernel kernel_lightning_indexer_t kernel_lightning_indexer<block_q5_0, 2, dequantize_q5_0>; |
| 558 | template [[host_name("kernel_lightning_indexer_q5_1")]] kernel kernel_lightning_indexer_t kernel_lightning_indexer<block_q5_1, 2, dequantize_q5_1>; |
| 559 | template [[host_name("kernel_lightning_indexer_q8_0")]] kernel kernel_lightning_indexer_t kernel_lightning_indexer<block_q8_0, 2, dequantize_q8_0>; |