okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
b2de2641f
main
1#include "common.h" 2#include "dequantize.h" 3 4constant bool FC_mul_mm_bc_inp [[function_constant(FC_MUL_MM + 0)]]; 5constant bool FC_mul_mm_bc_out [[function_constant(FC_MUL_MM + 1)]]; 6constant short FC_mul_mm_ne12 [[function_constant(FC_MUL_MM + 2)]]; 7constant short FC_mul_mm_ne13 [[function_constant(FC_MUL_MM + 3)]]; 8constant short FC_mul_mm_r2 [[function_constant(FC_MUL_MM + 4)]]; 9constant short FC_mul_mm_r3 [[function_constant(FC_MUL_MM + 5)]]; 10 11// each block_q contains 16*nl weights 12#ifdef GGML_METAL_HAS_TENSOR 13template< 14 typename SA, typename SA_4x4, typename SA_8x8, 15 typename SB, typename SB_2x4, typename SB_8x8, 16 typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread SA_4x4 &), 17 typename T0, typename T0_4x4, typename T1, typename T1_2x4> 18kernel void kernel_mul_mm( 19 constant ggml_metal_kargs_mul_mm & args, 20 device const char * srcA, 21 device const char * srcB, 22 device char * dst, 23 threadgroup char * shmem [[threadgroup(0)]], 24 uint3 tgpig [[threadgroup_position_in_grid]], 25 ushort tiitg [[thread_index_in_threadgroup]], 26 ushort sgitg [[simdgroup_index_in_threadgroup]]) { 27 (void) sgitg; 28 29 // Matrix dimensions: A(M,K) x B(K,N) -> C(M,N) 30 const int K = args.ne00; 31 const int M = args.ne0; 32 const int N = args.ne1; 33 34 // Batch dimension handling 35 const int im = tgpig.z; 36 const int i12 = im % FC_mul_mm_ne12; 37 const int i13 = im / FC_mul_mm_ne12; 38 39 // Batch offsets for srcA and srcB 40 const uint64_t offset0 = (i12/FC_mul_mm_r2)*args.nb02 + (i13/FC_mul_mm_r3)*args.nb03; 41 42 // Tile dimensions 43 constexpr int NRB = SZ_SIMDGROUP * N_MM_BLOCK_X * N_MM_SIMD_GROUP_X; 44 constexpr int NRA = SZ_SIMDGROUP * N_MM_BLOCK_Y * N_MM_SIMD_GROUP_Y; 45 46 // Tile offsets in output matrix 47 const int ra = tgpig.y * NRA; 48 const int rb = tgpig.x * NRB; 49 50 // Threadgroup memory for dequantized A tile only 51 threadgroup SA * sa = (threadgroup SA *)(shmem); 52 53 // Work-item count for A loading 54 constexpr int A_WORK_ITEMS = NRA * N_MM_NK; 55 constexpr int NUM_THREADS = N_SIMDWIDTH * N_MM_SIMD_GROUP_X * N_MM_SIMD_GROUP_Y; 56 57 // tA wraps threadgroup memory 58 auto tA = tensor(sa, dextents<int32_t, 2>(N_MM_NK_TOTAL, NRA)); 59 60 // tB wraps device memory directly 61 device T1 * ptrB = (device T1 *)(srcB + args.nb12*i12 + args.nb13*i13); 62 const int strideB = args.nb11 / sizeof(T1); 63 auto tB = tensor(ptrB, dextents<int32_t, 2>(K, N), array<int, 2>({1, strideB})); 64 65 // Configure matmul operation 66 // note: K is dynamic_extent (clamped to the valid range in PHASE 2), since a static 67 // N_MM_NK_TOTAL K tile would read src1 out of bounds when K % N_MM_NK_TOTAL != 0 68 // ref: https://github.com/ggml-org/llama.cpp/pull/27064 69 mpp::tensor_ops::matmul2d< 70 mpp::tensor_ops::matmul2d_descriptor( 71 NRB, NRA, static_cast<int>(dynamic_extent), false, true, true, 72 mpp::tensor_ops::matmul2d_descriptor::mode::multiply_accumulate), 73 execution_simdgroups<N_MM_SIMD_GROUP_X * N_MM_SIMD_GROUP_Y>> mm; 74 75 auto cT = mm.get_destination_cooperative_tensor<decltype(tB), decltype(tA), float>(); 76 77 // Accumulate partial results over K dimension 78 for (int loop_k = 0; loop_k < K; loop_k += N_MM_NK_TOTAL) { 79 // === PHASE 1: Dequantization of A into threadgroup memory === 80 for (int work = tiitg; work < A_WORK_ITEMS; work += NUM_THREADS) { 81 const int row = work / N_MM_NK; 82 const int k_chunk = work % N_MM_NK; 83 const int k_pos = loop_k + k_chunk * 16; 84 const short k_base = k_chunk * 16; 85 86 // Bounds check: skip device read if row is out of matrix bounds 87 if (ra + row < M) { 88 if (is_same<T0_4x4, block_q>::value && FC_mul_mm_bc_inp) { 89 // Element-wise reads when K is not aligned (nb01 not aligned for half4x4/float4x4). 90 // MSL spec Table 2.5: half4x4 requires 8-byte alignment. When K is odd, 91 // nb01 = K*2 is not 8-byte aligned, so odd-row pointers are misaligned. 92 // Mirrors the legacy kernel's existing guard. 93 device const T0 * row_ptr = (device const T0 *)(srcA + args.nb01 * (ra + row) + offset0); 94 95 FOR_UNROLL (short i = 0; i < 16; i++) { 96 sa[row * N_MM_NK_TOTAL + (k_base + i)] = (k_pos + i < K) ? (SA) row_ptr[k_pos + i] : (SA)0; 97 } 98 } else { 99 const int block_idx = k_pos / (16 * nl); 100 const short il = (k_pos / 16) % nl; 101 102 device const block_q * row_ptr = (device const block_q *)(srcA + args.nb01 * (ra + row) + offset0); 103 104 SA_4x4 temp_a; 105 dequantize_func(row_ptr + block_idx, il, temp_a); 106 107 FOR_UNROLL (short i = 0; i < 16; i++) { 108 // Zero-pad A for K positions beyond valid range (handles partial K iterations) 109 sa[row * N_MM_NK_TOTAL + (k_base + i)] = (k_pos + i < K) ? temp_a[i/4][i%4] : (SA)0; 110 } 111 } 112 } else { 113 // Zero-pad rows beyond matrix bounds 114 FOR_UNROLL (short i = 0; i < 16; i++) { 115 sa[row * N_MM_NK_TOTAL + (k_base + i)] = (SA)0; 116 } 117 } 118 } 119 120 threadgroup_barrier(mem_flags::mem_threadgroup); 121 122 // === PHASE 2: Tensor matmul === 123 // Clamp the K extent of both operand tensors to the remaining valid K range so 124 // the dynamic-K op never reads past the K extent of src1 (or the staged A tile). 125 const int kExt = min(N_MM_NK_TOTAL, K - loop_k); 126 127 auto tAv = tensor(sa, dextents<int32_t, 2>(kExt, NRA), array<int, 2>({1, N_MM_NK_TOTAL})); 128 auto tBv = tensor(ptrB + loop_k + rb * strideB, dextents<int32_t, 2>(kExt, N - rb), array<int, 2>({1, strideB})); 129 130 mm.run(tBv, tAv, cT); 131 132 threadgroup_barrier(mem_flags::mem_threadgroup); 133 } 134 135 // Store result tile to output matrix (with batch offset) 136 // cT.store handles bounds checking via tD's extents (M, N) 137 device float * dstBatch = (device float *)dst + im * N * M; 138 139 auto tD = tensor(dstBatch, dextents<int32_t, 2>(M, N), array<int, 2>({1, M})); 140 cT.store(tD.slice(ra, rb)); 141} 142 143#else 144 145template< 146 typename S0, typename S0_4x4, typename S0_8x8, 147 typename S1, typename S1_2x4, typename S1_8x8, 148 typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread S0_4x4 &), 149 typename T0, typename T0_4x4, typename T1, typename T1_2x4> 150kernel void kernel_mul_mm( 151 constant ggml_metal_kargs_mul_mm & args, 152 device const char * src0, 153 device const char * src1, 154 device char * dst, 155 threadgroup char * shmem [[threadgroup(0)]], 156 uint3 tgpig[[threadgroup_position_in_grid]], 157 ushort tiitg[[thread_index_in_threadgroup]], 158 ushort sgitg[[simdgroup_index_in_threadgroup]]) { 159 160 threadgroup S0 * sa = (threadgroup S0 *)(shmem); 161 threadgroup S1 * sb = (threadgroup S1 *)(shmem + 4096); 162 163 constexpr int NR0 = 64; 164 constexpr int NR1 = 32; 165 166 constexpr int NK = 32; 167 constexpr int NL0 = NK/16; 168 constexpr int NL1 = NK/8; 169 170 const int im = tgpig.z; 171 const int r0 = tgpig.y*NR0; 172 const int r1 = tgpig.x*NR1; 173 174 // if this block is of 64x32 shape or smaller 175 const short nr0 = (args.ne0 - r0 < NR0) ? (args.ne0 - r0) : NR0; 176 const short nr1 = (args.ne1 - r1 < NR1) ? (args.ne1 - r1) : NR1; 177 178 // a thread shouldn't load data outside of the matrix 179 const short lr0 = ((short)tiitg/NL0) < nr0 ? ((short)tiitg/NL0) : nr0 - 1; // 0 .. 63 180 const short lr1 = ((short)tiitg/NL1) < nr1 ? ((short)tiitg/NL1) : nr1 - 1; // 0 .. 31 181 182 const short il0 = (tiitg % NL0); 183 184 short il = il0; 185 186 const int i12 = im % FC_mul_mm_ne12; 187 const int i13 = im / FC_mul_mm_ne12; 188 189 const uint64_t offset0 = (i12/FC_mul_mm_r2)*args.nb02 + (i13/FC_mul_mm_r3)*args.nb03; 190 const short offset1 = il0/nl; 191 192 device const block_q * x = (device const block_q *)(src0 + args.nb01*(r0 + lr0) + offset0) + offset1; 193 194 const short iy = 8*(tiitg % NL1); 195 196 device const T1 * y = (device const T1 *)(src1 197 + args.nb13*i13 198 + args.nb12*i12 199 + args.nb11*(r1 + lr1) 200 + args.nb10*iy); 201 202 S0_8x8 ma[4]; 203 S1_8x8 mb[2]; 204 205 simdgroup_float8x8 mc[8]; 206 207 for (short i = 0; i < 8; i++){ 208 mc[i] = make_filled_simdgroup_matrix<float, 8>(0.f); 209 } 210 211 for (int loop_k = 0; loop_k < args.ne00; loop_k += NK) { 212 // load data and store to threadgroup memory 213 if (is_same<T0_4x4, block_q>::value && FC_mul_mm_bc_inp) { 214 threadgroup_barrier(mem_flags::mem_threadgroup); 215 216 // no need for dequantization 217 for (short i = 0; i < 16; i++) { 218 const short sx = 2*il0 + i/8; 219 const short sy = (tiitg/NL0)/8; 220 221 //const short lx = i%8; 222 //const short ly = (tiitg/NL0)%8; 223 const short lx = (tiitg/NL0)%8; 224 const short ly = i%8; 225 226 const short ib = 8*sx + sy; 227 228 *(sa + 64*ib + 8*ly + lx) = loop_k + 16*il + i < args.ne00 ? *((device T0 *) x + i) : 0; 229 } 230 } else { 231 S0_4x4 temp_a; 232 dequantize_func(x, il, temp_a); 233 234 threadgroup_barrier(mem_flags::mem_threadgroup); 235 236 FOR_UNROLL (short i = 0; i < 16; i++) { 237 const short sx = 2*il0 + i/8; 238 const short sy = (tiitg/NL0)/8; 239 240 //const short lx = i%8; 241 //const short ly = (tiitg/NL0)%8; 242 const short lx = (tiitg/NL0)%8; 243 const short ly = i%8; 244 245 const short ib = 8*sx + sy; 246 247 // NOTE: this is massively slower.. WTF? 248 //sa[64*ib + 8*ly + lx] = temp_a[i/4][i%4]; 249 250 *(sa + 64*ib + 8*ly + lx) = temp_a[i/4][i%4]; 251 } 252 } 253 254 if (FC_mul_mm_bc_inp) { 255 for (short i = 0; i < 8; ++i) { 256 const short sx = (tiitg%NL1); 257 const short sy = (tiitg/NL1)/8; 258 259 const short lx = i; 260 const short ly = (tiitg/NL1)%8; 261 //const short lx = (tiitg/NL1)%8; 262 //const short ly = i; 263 264 const short ib = 4*sx + sy; 265 266 *(sb + 64*ib + 8*ly + lx) = loop_k + iy + i < args.ne00 ? (S1) *((device T1 *) y + i) : 0; 267 } 268 } else { 269 const short sx = (tiitg%NL1); 270 const short sy = (tiitg/NL1)/8; 271 272 //const short dx = sx; 273 //const short dy = sy; 274 275 const short ly = (tiitg/NL1)%8; 276 277 const short ib = 4*sx + sy; 278 279 *(threadgroup S1_2x4 *)(sb + 64*ib + 8*ly) = (S1_2x4)(*((device T1_2x4 *) y)); 280 } 281 282 il = (il + 2 < nl) ? il + 2 : il % 2; 283 x = (il < 2) ? x + (2 + nl - 1)/nl : x; 284 285 y += NK; 286 287 threadgroup_barrier(mem_flags::mem_threadgroup); 288 289 // load matrices from threadgroup memory and conduct outer products 290 threadgroup const S0 * lsma = (sa + 4*64*(sgitg%2)); 291 threadgroup const S1 * lsmb = (sb + 2*64*(sgitg/2)); 292 293 FOR_UNROLL (short ik = 0; ik < NK/8; ik++) { 294 simdgroup_barrier(mem_flags::mem_none); 295 296 FOR_UNROLL (short i = 0; i < 4; i++) { 297 simdgroup_load(ma[i], lsma + 64*i, 8, 0, false); 298 } 299 300 simdgroup_barrier(mem_flags::mem_none); 301 302 FOR_UNROLL (short i = 0; i < 2; i++) { 303 simdgroup_load(mb[i], lsmb + 64*i, 8, 0, false); 304 } 305 306 simdgroup_barrier(mem_flags::mem_none); 307 308 FOR_UNROLL (short i = 0; i < 8; i++){ 309 simdgroup_multiply_accumulate(mc[i], mb[i/4], ma[i%4], mc[i]); 310 } 311 312 lsma += 8*64; 313 lsmb += 4*64; 314 } 315 } 316 317 if (!FC_mul_mm_bc_out || (r0 + NR0 <= args.ne0 && r1 + NR1 <= args.ne1)) { 318 // if no bounds checks on the output are needed, we can directly write to device memory 319 device float * C = (device float *) dst + 320 (r0 + 32*(sgitg & 1)) + \ 321 (r1 + 16*(sgitg >> 1)) * args.ne0 + im*args.ne1*args.ne0; 322 323 for (short i = 0; i < 8; i++) { 324 simdgroup_store(mc[i], C + 8*(i%4) + 8*args.ne0*(i/4), args.ne0, 0, false); 325 } 326 } else { 327 // block is smaller than 64x32, we should avoid writing data outside of the matrix 328 threadgroup_barrier(mem_flags::mem_threadgroup); 329 330 threadgroup float * temp_str = ((threadgroup float *) shmem) + 32*(sgitg&1) + (16*(sgitg >> 1))*NR0; 331 332 for (short i = 0; i < 8; i++) { 333 simdgroup_store(mc[i], temp_str + 8*(i%4) + 8*NR0*(i/4), NR0, 0, false); 334 } 335 336 threadgroup_barrier(mem_flags::mem_threadgroup); 337 338 if (sgitg == 0) { 339 for (int j = tiitg; j < nr1; j += NR1) { 340 device float * D = (device float *) dst + r0 + (r1 + j)*args.ne0 + im*args.ne1*args.ne0; 341 device float4 * D4 = (device float4 *) D; 342 343 threadgroup float * C = temp_str + (j*NR0); 344 threadgroup float4 * C4 = (threadgroup float4 *) C; 345 346 int i = 0; 347 for (; i < nr0/4; i++) { 348 *(D4 + i) = *(C4 + i); 349 } 350 351 i *= 4; 352 for (; i < nr0; i++) { 353 *(D + i) = *(C + i); 354 } 355 } 356 } 357 } 358} 359 360#endif // GGML_METAL_HAS_TENSOR 361 362template<short ne20> // n_expert_used 363kernel void kernel_mul_mm_id_map0( 364 constant ggml_metal_kargs_mul_mm_id_map0 & args, 365 device const char * src2, 366 device char * htpe, 367 device char * hids, 368 threadgroup char * shmem [[threadgroup(0)]], 369 ushort tpitg[[thread_position_in_threadgroup]], 370 ushort ntg[[threads_per_threadgroup]]) { 371 const short ide = tpitg; // expert id 372 373 uint32_t n_all = 0; 374 375 device int32_t * ids_i32 = (device int32_t *) hids + ide*args.ne21; 376 377 for (int i21 = 0; i21 < args.ne21; i21 += ntg) { // n_tokens 378 if (i21 + tpitg < args.ne21) { 379 device const int32_t * src2_i32 = (device const int32_t *) (src2 + (i21 + tpitg)*args.nb21); 380 381 threadgroup uint16_t * sids = (threadgroup uint16_t *) shmem + tpitg*ne20; 382 383 #pragma unroll(ne20) 384 for (short i20 = 0; i20 < ne20; i20++) { 385 sids[i20] = src2_i32[i20]; 386 } 387 } 388 389 threadgroup_barrier(mem_flags::mem_threadgroup); 390 391 for (short t = 0; t < ntg; t++) { 392 if (i21 + t >= args.ne21) { 393 break; 394 } 395 396 threadgroup const uint16_t * sids = (threadgroup const uint16_t *) shmem + t*ne20; 397 398 short sel = 0; 399 #pragma unroll(ne20) 400 for (short i20 = 0; i20 < ne20; i20++) { 401 sel += (sids[i20] == ide)*(i20 + 1); 402 } 403 404 ids_i32[n_all] = (i21 + t)*ne20 + sel - 1; 405 406 n_all += sel > 0; 407 } 408 409 threadgroup_barrier(mem_flags::mem_threadgroup); 410 } 411 412 device uint32_t * tpe_u32 = (device uint32_t *) (htpe); 413 tpe_u32[ide] = n_all; 414} 415 416kernel void kernel_mul_mm_id_amax_part_f32( 417 constant ggml_metal_kargs_mul_mm_id_amax & args, 418 device const char * src1, 419 device char * dst, 420 threadgroup char * shmem [[threadgroup(0)]], 421 uint tgpig[[threadgroup_position_in_grid]], 422 ushort tiitg[[thread_index_in_threadgroup]], 423 ushort tiisg[[thread_index_in_simdgroup]], 424 ushort sgitg[[simdgroup_index_in_threadgroup]], 425 ushort ntg[[threads_per_threadgroup]]) { 426 const int nrow = args.ne01*args.ne02; 427 428 float lmax = 0.0f; 429 430 for (int ir = tgpig; ir < nrow; ir += N_MM_NPART_AMAX) { 431 const int i01 = ir % args.ne01; 432 const int i02 = ir / args.ne01; 433 434 device const float * row = (device const float *) (src1 + i02*args.nb02 + i01*args.nb01); 435 436 for (int i00 = tiitg; i00 < args.ne00; i00 += ntg) { 437 lmax = max(lmax, fabs(row[i00])); 438 } 439 } 440 441 float amax = simd_max(lmax); 442 443 threadgroup float * shared_amax = (threadgroup float *) shmem; 444 445 if (ntg > N_SIMDWIDTH) { 446 if (sgitg == 0) { 447 shared_amax[tiisg] = 0.0f; 448 } 449 threadgroup_barrier(mem_flags::mem_threadgroup); 450 451 if (tiisg == 0) { 452 shared_amax[sgitg] = amax; 453 } 454 threadgroup_barrier(mem_flags::mem_threadgroup); 455 456 amax = shared_amax[tiisg]; 457 amax = simd_max(amax); 458 } 459 460 if (tiitg == 0) { 461 ((device float *) (dst + 8))[tgpig] = amax; 462 } 463} 464 465kernel void kernel_mul_mm_id_amax_f32( 466 device char * dst, 467 ushort tiitg[[thread_index_in_threadgroup]]) { 468 device const float * part = (device const float *) (dst + 8); 469 470 float amax = 0.0f; 471 472 for (int i = tiitg; i < N_MM_NPART_AMAX; i += N_SIMDWIDTH) { 473 amax = max(amax, part[i]); 474 } 475 476 amax = simd_max(amax); 477 478 if (tiitg == 0) { 479 // leave a comfortable margin below the f16 max of 65504 480 float scale = 1.0f; 481 482 // isfinite: src1 already inf/nan is not ours to fix - keep the 483 // scale at 1.0 instead of turning it into a different failure 484 if (isfinite(amax) && amax > 32768.0f) { 485 scale = exp2(ceil(log2(amax)) - 15.0f); 486 } 487 488 device float * d = (device float *) dst; 489 490 d[0] = 1.0f/scale; // exact: scale is a power of two 491 d[1] = scale; 492 } 493} 494 495typedef decltype(kernel_mul_mm_id_map0<1>) kernel_mul_mm_id_map0_t; 496 497template [[host_name("kernel_mul_mm_id_map0_ne20_1" )]] kernel kernel_mul_mm_id_map0_t kernel_mul_mm_id_map0<1>; 498template [[host_name("kernel_mul_mm_id_map0_ne20_2" )]] kernel kernel_mul_mm_id_map0_t kernel_mul_mm_id_map0<2>; 499template [[host_name("kernel_mul_mm_id_map0_ne20_4" )]] kernel kernel_mul_mm_id_map0_t kernel_mul_mm_id_map0<4>; 500template [[host_name("kernel_mul_mm_id_map0_ne20_5" )]] kernel kernel_mul_mm_id_map0_t kernel_mul_mm_id_map0<5>; 501template [[host_name("kernel_mul_mm_id_map0_ne20_6" )]] kernel kernel_mul_mm_id_map0_t kernel_mul_mm_id_map0<6>; 502template [[host_name("kernel_mul_mm_id_map0_ne20_8" )]] kernel kernel_mul_mm_id_map0_t kernel_mul_mm_id_map0<8>; 503template [[host_name("kernel_mul_mm_id_map0_ne20_10")]] kernel kernel_mul_mm_id_map0_t kernel_mul_mm_id_map0<10>; 504template [[host_name("kernel_mul_mm_id_map0_ne20_16")]] kernel kernel_mul_mm_id_map0_t kernel_mul_mm_id_map0<16>; 505template [[host_name("kernel_mul_mm_id_map0_ne20_22")]] kernel kernel_mul_mm_id_map0_t kernel_mul_mm_id_map0<22>; 506 507template<typename S0, typename S0_4x4, typename S0_8x8, typename S1, typename S1_2x4, typename S1_8x8, typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread S0_4x4 &), typename T0, typename T0_4x4, typename T1, typename T1_2x4> 508kernel void kernel_mul_mm_id( 509 constant ggml_metal_kargs_mul_mm_id & args, 510 device const char * src0, 511 device const char * src1, 512 device const char * htpe, 513 device const char * hids, 514 device char * dst, 515 device const char * amax, 516 threadgroup char * shmem [[threadgroup(0)]], 517 uint3 tgpig[[threadgroup_position_in_grid]], 518 ushort tiitg[[thread_index_in_threadgroup]], 519 ushort tiisg[[thread_index_in_simdgroup]], 520 ushort sgitg[[simdgroup_index_in_threadgroup]]) { 521 threadgroup S0 * sa = (threadgroup S0 *)(shmem); 522 threadgroup S1 * sb = (threadgroup S1 *)(shmem + 4096); 523 524#ifdef GGML_METAL_HAS_TENSOR 525 threadgroup float * sc = (threadgroup float *)(shmem); 526#endif 527 528 constexpr int NR0 = 64; 529 constexpr int NR1 = 32; 530 531 constexpr int NK = 32; 532 constexpr int NL0 = NK/16; 533 constexpr int NL1 = NK/8; 534 535 const int im = tgpig.z; // expert 536 const int r0 = tgpig.y*NR0; 537 const int r1 = tgpig.x*NR1; 538 539 device const uint32_t * tpe_u32 = (device const uint32_t *) (htpe); 540 device const int32_t * ids_i32 = (device const int32_t *) (hids); 541 542 const int32_t neh1 = tpe_u32[im]; 543 544 if (r1 >= neh1) { 545 return; 546 } 547 548 // if this block is of 64x32 shape or smaller 549 const short nr0 = (args.ne0 - r0 < NR0) ? (args.ne0 - r0) : NR0; 550 const short nr1 = ( neh1 - r1 < NR1) ? ( neh1 - r1) : NR1; 551 552 // a thread shouldn't load data outside of the matrix 553 const short lr0 = ((short)tiitg/NL0) < nr0 ? ((short)tiitg/NL0) : nr0 - 1; // 0 .. 63 554 const short lr1 = ((short)tiitg/NL1) < nr1 ? ((short)tiitg/NL1) : nr1 - 1; // 0 .. 31 555 556 const short il0 = (tiitg % NL0); 557 558 short il = il0; 559 560 const int id = ids_i32[im*args.ne21 + r1 + lr1]; 561 562 const short i11 = (id % args.ne20) % args.ne11; 563 const short i12 = (id / args.ne20); 564 const short i13 = 0; 565 566 const uint64_t offset0 = im*args.nb02 + i13*args.nb03; 567 const short offset1 = il0/nl; 568 569 device const block_q * x = (device const block_q *)(src0 + args.nb01*(r0 + lr0) + offset0) + offset1; 570 571 const short iy = 8*(tiitg % NL1); 572 573 device const T1 * y = (device const T1 *)(src1 574 + args.nb13*i13 575 + args.nb12*i12 576 + args.nb11*i11 577 + args.nb10*iy); 578 579 // skip the upper half of the token tile when the expert did not fill it 580 constexpr short NR1H = NR1/2; 581 582 const bool has_hi = nr1 > NR1H; 583 584 const short lb1 = (short) tiitg/NL1; // 0 .. NR1-1, this thread's row of the B tile 585 586 // power-of-two rescaling 587 const float s1_inv = ((device const float *) amax)[0]; 588 const float s1_scale = ((device const float *) amax)[1]; 589 590#ifndef GGML_METAL_HAS_TENSOR 591 S0_8x8 ma[4]; 592 S1_8x8 mb[2]; 593 594 simdgroup_float8x8 mc[8]; 595 596 for (short i = 0; i < 8; i++){ 597 mc[i] = make_filled_simdgroup_matrix<float, 8>(0.f); 598 } 599 600 // simdgroups 2,3 own rows NR1H..NR1-1 601 const bool sg_active = has_hi || sgitg < 2; 602#else 603 auto tA = tensor<threadgroup S0, dextents<int32_t, 2>, tensor_inline>(sa, dextents<int32_t, 2>(NK, NR0)); 604 605 // sb is [NR1][NK] row-major 606 auto tB0 = tensor<threadgroup S1, dextents<int32_t, 2>, tensor_inline>(sb, dextents<int32_t, 2>(NK, NR1H)); 607 auto tB1 = tensor<threadgroup S1, dextents<int32_t, 2>, tensor_inline>(sb + NR1H*NK, dextents<int32_t, 2>(NK, NR1H)); 608 609 mpp::tensor_ops::matmul2d< 610 mpp::tensor_ops::matmul2d_descriptor(NR1H, NR0, NK, false, true, false, mpp::tensor_ops::matmul2d_descriptor::mode::multiply_accumulate), 611 execution_simdgroups<4>> mm; 612 613 auto cT0 = mm.get_destination_cooperative_tensor<decltype(tA), decltype(tB0), float>(); 614 auto cT1 = mm.get_destination_cooperative_tensor<decltype(tA), decltype(tB1), float>(); 615#endif 616 617 for (int loop_k = 0; loop_k < args.ne00; loop_k += NK) { 618#ifndef GGML_METAL_HAS_TENSOR 619 // load data and store to threadgroup memory 620 if (is_same<T0_4x4, block_q>::value && FC_mul_mm_bc_inp) { 621 threadgroup_barrier(mem_flags::mem_threadgroup); 622 623 // no need for dequantization 624 for (short i = 0; i < 16; i++) { 625 const short sx = 2*il0 + i/8; 626 const short sy = (tiitg/NL0)/8; 627 628 //const short lx = i%8; 629 //const short ly = (tiitg/NL0)%8; 630 const short lx = (tiitg/NL0)%8; 631 const short ly = i%8; 632 633 const short ib = 8*sx + sy; 634 635 *(sa + 64*ib + 8*ly + lx) = loop_k + 16*il + i < args.ne00 ? (S0) *((device T0 *) x + i) : (S0) 0; 636 } 637 } else { 638 S0_4x4 temp_a; 639 dequantize_func(x, il, temp_a); 640 641 threadgroup_barrier(mem_flags::mem_threadgroup); 642 643 FOR_UNROLL (short i = 0; i < 16; i++) { 644 const short sx = 2*il0 + i/8; 645 const short sy = (tiitg/NL0)/8; 646 647 //const short lx = i%8; 648 //const short ly = (tiitg/NL0)%8; 649 const short lx = (tiitg/NL0)%8; 650 const short ly = i%8; 651 652 const short ib = 8*sx + sy; 653 654 // NOTE: this is massively slower.. WTF? 655 //sa[64*ib + 8*ly + lx] = temp_a[i/4][i%4]; 656 657 *(sa + 64*ib + 8*ly + lx) = temp_a[i/4][i%4]; 658 } 659 } 660 661 if (FC_mul_mm_bc_inp) { 662 for (short i = 0; i < 8; ++i) { 663 const short sx = (tiitg%NL1); 664 const short sy = (tiitg/NL1)/8; 665 666 const short lx = i; 667 const short ly = (tiitg/NL1)%8; 668 //const short lx = (tiitg/NL1)%8; 669 //const short ly = i; 670 671 const short ib = 4*sx + sy; 672 673 *(sb + 64*ib + 8*ly + lx) = loop_k + iy + i < args.ne00 ? (S1) (*((device T1 *) y + i) * (T1) s1_inv) : 0; 674 } 675 } else { 676 const short sx = (tiitg%NL1); 677 const short sy = (tiitg/NL1)/8; 678 679 //const short dx = sx; 680 //const short dy = sy; 681 682 const short ly = (tiitg/NL1)%8; 683 684 const short ib = 4*sx + sy; 685 686 *(threadgroup S1_2x4 *)(sb + 64*ib + 8*ly) = (S1_2x4)((*((device T1_2x4 *) y)) * (T1) s1_inv); 687 } 688#else 689 // load data and store to threadgroup memory 690 if (is_same<T0_4x4, block_q>::value && FC_mul_mm_bc_inp) { 691 threadgroup_barrier(mem_flags::mem_threadgroup); 692 693 // no need for dequantization 694 for (short i = 0; i < 16; i++) { 695 const short sx = 2*il0 + i/8; 696 const short sy = (tiitg/NL0)/8; 697 698 const short lx = i%8; 699 const short ly = (tiitg/NL0)%8; 700 //const short lx = (tiitg/NL0)%8; 701 //const short ly = i%8; 702 703 *(sa + NK*(8*sy + ly) + 8*sx + lx) = loop_k + 16*il + i < args.ne00 ? *((device T0 *) x + i) : 0; 704 } 705 } else { 706 S0_4x4 temp_a; 707 dequantize_func(x, il, temp_a); 708 709 threadgroup_barrier(mem_flags::mem_threadgroup); 710 711 FOR_UNROLL (short i = 0; i < 16; i++) { 712 const short sx = 2*il0 + i/8; 713 const short sy = (tiitg/NL0)/8; 714 715 const short lx = i%8; 716 const short ly = (tiitg/NL0)%8; 717 //const short lx = (tiitg/NL0)%8; 718 //const short ly = i%8; 719 720 *(sa + NK*(8*sy + ly) + 8*sx + lx) = temp_a[i/4][i%4]; 721 } 722 } 723 724 if (FC_mul_mm_bc_inp) { 725 for (short i = 0; i < 8; ++i) { 726 const short sx = (tiitg%NL1); 727 const short sy = (tiitg/NL1)/8; 728 729 const short lx = i; 730 const short ly = (tiitg/NL1)%8; 731 //const short lx = (tiitg/NL1)%8; 732 //const short ly = i; 733 734 *(sb + NK*(8*sy + ly) + 8*sx + lx) = loop_k + iy + i < args.ne00 ? (S1) (*((device T1 *) y + i) * (T1) s1_inv) : 0; 735 } 736 } else { 737 const short sx = (tiitg%NL1); 738 const short sy = (tiitg/NL1)/8; 739 740 //const short lx = i; 741 const short ly = (tiitg/NL1)%8; 742 //const short lx = (tiitg/NL1)%8; 743 //const short ly = i; 744 745 *(threadgroup S1_2x4 *)(sb + NK*(8*sy + ly) + 8*sx) = (S1_2x4)((*((device T1_2x4 *) y)) * (T1) s1_inv); 746 } 747#endif 748 749 il = (il + 2 < nl) ? il + 2 : il % 2; 750 x = (il < 2) ? x + (2 + nl - 1)/nl : x; 751 752 y += NK; 753 754 threadgroup_barrier(mem_flags::mem_threadgroup); 755 756#ifndef GGML_METAL_HAS_TENSOR 757 if (sg_active) { 758 // load matrices from threadgroup memory and conduct outer products 759 threadgroup const S0 * lsma = (sa + 4*64*(sgitg%2)); 760 threadgroup const S1 * lsmb = (sb + 2*64*(sgitg/2)); 761 762 FOR_UNROLL (short ik = 0; ik < NK/8; ik++) { 763 simdgroup_barrier(mem_flags::mem_none); 764 765 FOR_UNROLL (short i = 0; i < 4; i++) { 766 simdgroup_load(ma[i], lsma + 64*i, 8, 0, false); 767 } 768 769 simdgroup_barrier(mem_flags::mem_none); 770 771 FOR_UNROLL (short i = 0; i < 2; i++) { 772 simdgroup_load(mb[i], lsmb + 64*i, 8, 0, false); 773 } 774 775 simdgroup_barrier(mem_flags::mem_none); 776 777 FOR_UNROLL (short i = 0; i < 8; i++){ 778 simdgroup_multiply_accumulate(mc[i], mb[i/4], ma[i%4], mc[i]); 779 } 780 781 lsma += 8*64; 782 lsmb += 4*64; 783 } 784 } 785#else 786 auto sA = tA.slice(0, 0); 787 auto sB0 = tB0.slice(0, 0); 788 789 mm.run(sB0, sA, cT0); 790 791 if (has_hi) { 792 auto sB1 = tB1.slice(0, 0); 793 794 mm.run(sB1, sA, cT1); 795 } 796#endif 797 } 798 799 // block is smaller than 64x32, we should avoid writing data outside of the matrix 800 threadgroup_barrier(mem_flags::mem_threadgroup); 801 802#ifdef GGML_METAL_HAS_TENSOR 803 auto tC0 = tensor<threadgroup float, dextents<int32_t, 2>, tensor_inline>(sc, dextents<int32_t, 2>(NR0, NR1H)); 804 cT0.store(tC0); 805 806 if (has_hi) { 807 auto tC1 = tensor<threadgroup float, dextents<int32_t, 2>, tensor_inline>(sc + NR1H*NR0, dextents<int32_t, 2>(NR0, NR1H)); 808 cT1.store(tC1); 809 } 810#else 811 if (sg_active) { 812 threadgroup float * temp_str = ((threadgroup float *) shmem) + 32*(sgitg&1) + (16*(sgitg >> 1))*NR0; 813 814 for (short i = 0; i < 8; i++) { 815 simdgroup_store(mc[i], temp_str + 8*(i%4) + 8*NR0*(i/4), NR0, 0, false); 816 } 817 } 818#endif 819 820 threadgroup_barrier(mem_flags::mem_threadgroup); 821 822 for (short j = sgitg; j < nr1; j += 4) { 823 const int id = ids_i32[im*args.ne21 + r1 + j]; 824 825 const short ide = id % args.ne20; 826 const short idt = id / args.ne20; 827 828 device float * D = (device float *) dst + r0 + ide*args.ne0 + idt*args.ne1*args.ne0; 829 device float4 * D4 = (device float4 *) D; 830 831 threadgroup float * C = (threadgroup float *) shmem + j*NR0; 832 threadgroup float4 * C4 = (threadgroup float4 *) C; 833 834 int i = tiisg; 835 for (; i < nr0/4; i += 32) { 836 *(D4 + i) = *(C4 + i) * s1_scale; 837 } 838 839 i = (4*(nr0/4)) + tiisg; 840 for (; i < nr0; i += 32) { 841 *(D + i) = *(C + i) * s1_scale; 842 } 843 } 844} 845 846// 847// matrix-matrix multiplication 848// 849 850typedef decltype(kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, float4x4, 1, dequantize_f32, float, float4x4, float, float2x4>) mul_mm_t; 851 852template [[host_name("kernel_mul_mm_f32_f32")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, float4x4, 1, dequantize_f32, float, float4x4, float, float2x4>; 853template [[host_name("kernel_mul_mm_f16_f32")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, half4x4, 1, dequantize_f16, half, half4x4, float, float2x4>; 854#if defined(GGML_METAL_HAS_BF16) 855template [[host_name("kernel_mul_mm_bf16_f32")]] kernel mul_mm_t kernel_mul_mm<bfloat, bfloat4x4, simdgroup_bfloat8x8, bfloat, bfloat2x4, simdgroup_bfloat8x8, bfloat4x4, 1, dequantize_bf16, bfloat, bfloat4x4, float, float2x4>; 856#endif 857template [[host_name("kernel_mul_mm_q1_0_f32")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q1_0, 8, dequantize_q1_0, float, float4x4, float, float2x4>; 858template [[host_name("kernel_mul_mm_q2_0_f32")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q2_0, 4, dequantize_q2_0, float, float4x4, float, float2x4>; 859template [[host_name("kernel_mul_mm_q4_0_f32")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q4_0, 2, dequantize_q4_0, float, float4x4, float, float2x4>; 860template [[host_name("kernel_mul_mm_q4_1_f32")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q4_1, 2, dequantize_q4_1, float, float4x4, float, float2x4>; 861template [[host_name("kernel_mul_mm_q5_0_f32")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q5_0, 2, dequantize_q5_0, float, float4x4, float, float2x4>; 862template [[host_name("kernel_mul_mm_q5_1_f32")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q5_1, 2, dequantize_q5_1, float, float4x4, float, float2x4>; 863template [[host_name("kernel_mul_mm_q8_0_f32")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q8_0, 2, dequantize_q8_0, float, float4x4, float, float2x4>; 864template [[host_name("kernel_mul_mm_mxfp4_f32")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_mxfp4, 2, dequantize_mxfp4, float, float4x4, float, float2x4>; 865template [[host_name("kernel_mul_mm_q2_K_f32")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q2_K, QK_NL, dequantize_q2_K, float, float4x4, float, float2x4>; 866template [[host_name("kernel_mul_mm_q3_K_f32")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q3_K, QK_NL, dequantize_q3_K, float, float4x4, float, float2x4>; 867template [[host_name("kernel_mul_mm_q4_K_f32")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q4_K, QK_NL, dequantize_q4_K, float, float4x4, float, float2x4>; 868template [[host_name("kernel_mul_mm_q5_K_f32")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q5_K, QK_NL, dequantize_q5_K, float, float4x4, float, float2x4>; 869template [[host_name("kernel_mul_mm_q6_K_f32")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q6_K, QK_NL, dequantize_q6_K, float, float4x4, float, float2x4>; 870template [[host_name("kernel_mul_mm_iq2_xxs_f32")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq2_xxs, QK_NL, dequantize_iq2_xxs, float, float4x4, float, float2x4>; 871template [[host_name("kernel_mul_mm_iq2_xs_f32")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq2_xs, QK_NL, dequantize_iq2_xs, float, float4x4, float, float2x4>; 872template [[host_name("kernel_mul_mm_iq3_xxs_f32")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq3_xxs, QK_NL, dequantize_iq3_xxs, float, float4x4, float, float2x4>; 873template [[host_name("kernel_mul_mm_iq3_s_f32")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq3_s, QK_NL, dequantize_iq3_s, float, float4x4, float, float2x4>; 874template [[host_name("kernel_mul_mm_iq2_s_f32")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq2_s, QK_NL, dequantize_iq2_s, float, float4x4, float, float2x4>; 875template [[host_name("kernel_mul_mm_iq1_s_f32")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq1_s, QK_NL, dequantize_iq1_s, float, float4x4, float, float2x4>; 876template [[host_name("kernel_mul_mm_iq1_m_f32")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq1_m, QK_NL, dequantize_iq1_m, float, float4x4, float, float2x4>; 877template [[host_name("kernel_mul_mm_iq4_nl_f32")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq4_nl, 2, dequantize_iq4_nl, float, float4x4, float, float2x4>; 878template [[host_name("kernel_mul_mm_iq4_xs_f32")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq4_xs, QK_NL, dequantize_iq4_xs, float, float4x4, float, float2x4>; 879template [[host_name("kernel_mul_mm_tq2_0_f32")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_tq2_0, QK_NL, dequantize_tq2_0, float, float4x4, float, float2x4>; 880 881template [[host_name("kernel_mul_mm_f32_f16")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, float4x4, 1, dequantize_f32, float, float4x4, half, half2x4>; 882template [[host_name("kernel_mul_mm_f16_f16")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, half4x4, 1, dequantize_f16, half, half4x4, half, half2x4>; 883template [[host_name("kernel_mul_mm_q1_0_f16")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q1_0, 8, dequantize_q1_0, float, float4x4, half, half2x4>; 884template [[host_name("kernel_mul_mm_q2_0_f16")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q2_0, 4, dequantize_q2_0, float, float4x4, half, half2x4>; 885template [[host_name("kernel_mul_mm_q4_0_f16")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q4_0, 2, dequantize_q4_0, float, float4x4, half, half2x4>; 886template [[host_name("kernel_mul_mm_q4_1_f16")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q4_1, 2, dequantize_q4_1, float, float4x4, half, half2x4>; 887template [[host_name("kernel_mul_mm_q5_0_f16")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q5_0, 2, dequantize_q5_0, float, float4x4, half, half2x4>; 888template [[host_name("kernel_mul_mm_q5_1_f16")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q5_1, 2, dequantize_q5_1, float, float4x4, half, half2x4>; 889template [[host_name("kernel_mul_mm_q8_0_f16")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q8_0, 2, dequantize_q8_0, float, float4x4, half, half2x4>; 890template [[host_name("kernel_mul_mm_mxfp4_f16")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_mxfp4, 2, dequantize_mxfp4, float, float4x4, half, half2x4>; 891template [[host_name("kernel_mul_mm_q2_K_f16")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q2_K, QK_NL, dequantize_q2_K, float, float4x4, half, half2x4>; 892template [[host_name("kernel_mul_mm_q3_K_f16")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q3_K, QK_NL, dequantize_q3_K, float, float4x4, half, half2x4>; 893template [[host_name("kernel_mul_mm_q4_K_f16")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q4_K, QK_NL, dequantize_q4_K, float, float4x4, half, half2x4>; 894template [[host_name("kernel_mul_mm_q5_K_f16")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q5_K, QK_NL, dequantize_q5_K, float, float4x4, half, half2x4>; 895template [[host_name("kernel_mul_mm_q6_K_f16")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q6_K, QK_NL, dequantize_q6_K, float, float4x4, half, half2x4>; 896template [[host_name("kernel_mul_mm_iq2_xxs_f16")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq2_xxs, QK_NL, dequantize_iq2_xxs, float, float4x4, half, half2x4>; 897template [[host_name("kernel_mul_mm_iq2_xs_f16")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq2_xs, QK_NL, dequantize_iq2_xs, float, float4x4, half, half2x4>; 898template [[host_name("kernel_mul_mm_iq3_xxs_f16")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq3_xxs, QK_NL, dequantize_iq3_xxs, float, float4x4, half, half2x4>; 899template [[host_name("kernel_mul_mm_iq3_s_f16")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq3_s, QK_NL, dequantize_iq3_s, float, float4x4, half, half2x4>; 900template [[host_name("kernel_mul_mm_iq2_s_f16")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq2_s, QK_NL, dequantize_iq2_s, float, float4x4, half, half2x4>; 901template [[host_name("kernel_mul_mm_iq1_s_f16")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq1_s, QK_NL, dequantize_iq1_s, float, float4x4, half, half2x4>; 902template [[host_name("kernel_mul_mm_iq1_m_f16")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq1_m, QK_NL, dequantize_iq1_m, float, float4x4, half, half2x4>; 903template [[host_name("kernel_mul_mm_iq4_nl_f16")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq4_nl, 2, dequantize_iq4_nl, float, float4x4, half, half2x4>; 904template [[host_name("kernel_mul_mm_iq4_xs_f16")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq4_xs, QK_NL, dequantize_iq4_xs, float, float4x4, half, half2x4>; 905template [[host_name("kernel_mul_mm_tq2_0_f16")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_tq2_0, QK_NL, dequantize_tq2_0, float, float4x4, half, half2x4>; 906 907// 908// indirect matrix-matrix multiplication 909// 910 911typedef decltype(kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, float4x4, 1, dequantize_f32, float, float4x4, float, float2x4>) mul_mm_id; 912 913template [[host_name("kernel_mul_mm_id_f32_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, float4x4, 1, dequantize_f32, float, float4x4, float, float2x4>; 914template [[host_name("kernel_mul_mm_id_f16_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, half4x4, 1, dequantize_f16, half, half4x4, float, float2x4>; 915#if defined(GGML_METAL_HAS_BF16) 916template [[host_name("kernel_mul_mm_id_bf16_f32")]] kernel mul_mm_id kernel_mul_mm_id<bfloat, bfloat4x4, simdgroup_bfloat8x8, bfloat, bfloat2x4, simdgroup_bfloat8x8, bfloat4x4, 1, dequantize_bf16, bfloat, bfloat4x4, float, float2x4>; 917#endif 918template [[host_name("kernel_mul_mm_id_q1_0_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q1_0, 8, dequantize_q1_0, float, float4x4, float, float2x4>; 919template [[host_name("kernel_mul_mm_id_q2_0_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q2_0, 4, dequantize_q2_0, float, float4x4, float, float2x4>; 920template [[host_name("kernel_mul_mm_id_q4_0_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q4_0, 2, dequantize_q4_0, float, float4x4, float, float2x4>; 921template [[host_name("kernel_mul_mm_id_q4_1_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q4_1, 2, dequantize_q4_1, float, float4x4, float, float2x4>; 922template [[host_name("kernel_mul_mm_id_q5_0_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q5_0, 2, dequantize_q5_0, float, float4x4, float, float2x4>; 923template [[host_name("kernel_mul_mm_id_q5_1_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q5_1, 2, dequantize_q5_1, float, float4x4, float, float2x4>; 924template [[host_name("kernel_mul_mm_id_q8_0_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q8_0, 2, dequantize_q8_0, float, float4x4, float, float2x4>; 925template [[host_name("kernel_mul_mm_id_mxfp4_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_mxfp4, 2, dequantize_mxfp4, float, float4x4, float, float2x4>; 926template [[host_name("kernel_mul_mm_id_q2_K_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q2_K, QK_NL, dequantize_q2_K, float, float4x4, float, float2x4>; 927template [[host_name("kernel_mul_mm_id_q3_K_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q3_K, QK_NL, dequantize_q3_K, float, float4x4, float, float2x4>; 928template [[host_name("kernel_mul_mm_id_q4_K_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q4_K, QK_NL, dequantize_q4_K, float, float4x4, float, float2x4>; 929template [[host_name("kernel_mul_mm_id_q5_K_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q5_K, QK_NL, dequantize_q5_K, float, float4x4, float, float2x4>; 930template [[host_name("kernel_mul_mm_id_q6_K_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q6_K, QK_NL, dequantize_q6_K, float, float4x4, float, float2x4>; 931template [[host_name("kernel_mul_mm_id_iq2_xxs_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq2_xxs, QK_NL, dequantize_iq2_xxs, float, float4x4, float, float2x4>; 932template [[host_name("kernel_mul_mm_id_iq2_xs_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq2_xs, QK_NL, dequantize_iq2_xs, float, float4x4, float, float2x4>; 933template [[host_name("kernel_mul_mm_id_iq3_xxs_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq3_xxs, QK_NL, dequantize_iq3_xxs, float, float4x4, float, float2x4>; 934template [[host_name("kernel_mul_mm_id_iq3_s_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq3_s, QK_NL, dequantize_iq3_s, float, float4x4, float, float2x4>; 935template [[host_name("kernel_mul_mm_id_iq2_s_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq2_s, QK_NL, dequantize_iq2_s, float, float4x4, float, float2x4>; 936template [[host_name("kernel_mul_mm_id_iq1_s_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq1_s, QK_NL, dequantize_iq1_s, float, float4x4, float, float2x4>; 937template [[host_name("kernel_mul_mm_id_iq1_m_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq1_m, QK_NL, dequantize_iq1_m, float, float4x4, float, float2x4>; 938template [[host_name("kernel_mul_mm_id_iq4_nl_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq4_nl, 2, dequantize_iq4_nl, float, float4x4, float, float2x4>; 939template [[host_name("kernel_mul_mm_id_iq4_xs_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq4_xs, QK_NL, dequantize_iq4_xs, float, float4x4, float, float2x4>; 940template [[host_name("kernel_mul_mm_id_tq2_0_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_tq2_0, QK_NL, dequantize_tq2_0, float, float4x4, float, float2x4>; 941 942template [[host_name("kernel_mul_mm_id_f32_f16")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, float4x4, 1, dequantize_f32, float, float4x4, half, half2x4>; 943template [[host_name("kernel_mul_mm_id_f16_f16")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, half4x4, 1, dequantize_f16, half, half4x4, half, half2x4>; 944template [[host_name("kernel_mul_mm_id_q1_0_f16")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q1_0, 8, dequantize_q1_0, float, float4x4, half, half2x4>; 945template [[host_name("kernel_mul_mm_id_q2_0_f16")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q2_0, 4, dequantize_q2_0, float, float4x4, half, half2x4>; 946template [[host_name("kernel_mul_mm_id_q4_0_f16")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q4_0, 2, dequantize_q4_0, float, float4x4, half, half2x4>; 947template [[host_name("kernel_mul_mm_id_q4_1_f16")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q4_1, 2, dequantize_q4_1, float, float4x4, half, half2x4>; 948template [[host_name("kernel_mul_mm_id_q5_0_f16")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q5_0, 2, dequantize_q5_0, float, float4x4, half, half2x4>; 949template [[host_name("kernel_mul_mm_id_q5_1_f16")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q5_1, 2, dequantize_q5_1, float, float4x4, half, half2x4>; 950template [[host_name("kernel_mul_mm_id_q8_0_f16")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q8_0, 2, dequantize_q8_0, float, float4x4, half, half2x4>; 951template [[host_name("kernel_mul_mm_id_mxfp4_f16")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_mxfp4, 2, dequantize_mxfp4, float, float4x4, half, half2x4>; 952template [[host_name("kernel_mul_mm_id_q2_K_f16")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q2_K, QK_NL, dequantize_q2_K, float, float4x4, half, half2x4>; 953template [[host_name("kernel_mul_mm_id_q3_K_f16")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q3_K, QK_NL, dequantize_q3_K, float, float4x4, half, half2x4>; 954template [[host_name("kernel_mul_mm_id_q4_K_f16")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q4_K, QK_NL, dequantize_q4_K, float, float4x4, half, half2x4>; 955template [[host_name("kernel_mul_mm_id_q5_K_f16")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q5_K, QK_NL, dequantize_q5_K, float, float4x4, half, half2x4>; 956template [[host_name("kernel_mul_mm_id_q6_K_f16")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q6_K, QK_NL, dequantize_q6_K, float, float4x4, half, half2x4>; 957template [[host_name("kernel_mul_mm_id_iq2_xxs_f16")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq2_xxs, QK_NL, dequantize_iq2_xxs, float, float4x4, half, half2x4>; 958template [[host_name("kernel_mul_mm_id_iq2_xs_f16")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq2_xs, QK_NL, dequantize_iq2_xs, float, float4x4, half, half2x4>; 959template [[host_name("kernel_mul_mm_id_iq3_xxs_f16")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq3_xxs, QK_NL, dequantize_iq3_xxs, float, float4x4, half, half2x4>; 960template [[host_name("kernel_mul_mm_id_iq3_s_f16")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq3_s, QK_NL, dequantize_iq3_s, float, float4x4, half, half2x4>; 961template [[host_name("kernel_mul_mm_id_iq2_s_f16")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq2_s, QK_NL, dequantize_iq2_s, float, float4x4, half, half2x4>; 962template [[host_name("kernel_mul_mm_id_iq1_s_f16")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq1_s, QK_NL, dequantize_iq1_s, float, float4x4, half, half2x4>; 963template [[host_name("kernel_mul_mm_id_iq1_m_f16")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq1_m, QK_NL, dequantize_iq1_m, float, float4x4, half, half2x4>; 964template [[host_name("kernel_mul_mm_id_iq4_nl_f16")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq4_nl, 2, dequantize_iq4_nl, float, float4x4, half, half2x4>; 965template [[host_name("kernel_mul_mm_id_iq4_xs_f16")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq4_xs, QK_NL, dequantize_iq4_xs, float, float4x4, half, half2x4>; 966template [[host_name("kernel_mul_mm_id_tq2_0_f16")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_tq2_0, QK_NL, dequantize_tq2_0, float, float4x4, half, half2x4>;