Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/ggml/blob/master/src/ggml-metal/kernels/mul_mv_mma.metal # ggml [Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `ggml / src / ggml-metal / kernels / mul_mv_mma.metal` 627 lines ยท 27341 bytes Raw Blame History 1 #include "common.h" 2 #include "dequantize.h" 3 4 constant short FC_mul_mv_mma_nsg [[function_constant(FC_MUL_MV_MMA + 0)]]; 5 constant short FC_mul_mv_mma_ne12 [[function_constant(FC_MUL_MV_MMA + 1)]]; 6 constant short FC_mul_mv_mma_r2 [[function_constant(FC_MUL_MV_MMA + 2)]]; 7 constant short FC_mul_mv_mma_r3 [[function_constant(FC_MUL_MV_MMA + 3)]]; 8 constant int FC_mul_mv_mma_ne00 [[function_constant(FC_MUL_MV_MMA + 4)]]; 9 constant bool FC_mul_mv_mma_add [[function_constant(FC_MUL_MV_MMA + 5)]]; 10 11 // a lane of a few-row MMA tile: A fragment row fm, B fragment columns fn and fn + 1 12 struct mul_mv_mma_tile { 13 short fm; 14 short fn; 15 int i01; 16 int i11; 17 int i1m; 18 uint64_t offset0; 19 uint64_t offset1; 20 }; 21 22 // the A fragment row and the first B fragment column that lane l holds in an 8x8 simdgroup matrix 23 inline short mul_mv_mma_lane_fm(ushort l) { return ((l/4) & 4) + ((l/2) % 4); } 24 inline short mul_mv_mma_lane_fn(ushort l) { return ((l/4) & 2)*2 + (l % 2)*2; } 25 26 template 27 inline mul_mv_mma_tile mul_mv_mma_tile_init(constant ggml_metal_kargs_mul_mv_ext & args, uint3 tgpig, ushort tiisg) { 28 mul_mv_mma_tile tile; 29 tile.fm = mul_mv_mma_lane_fm(tiisg); 30 tile.fn = mul_mv_mma_lane_fn(tiisg); 31 tile.i01 = tgpig.x*(8*NT); 32 tile.i11 = tgpig.y*(8*RT); 33 tile.i1m = tgpig.z; 34 35 const int i12 = tile.i1m%FC_mul_mv_mma_ne12; 36 const int i13 = tile.i1m/FC_mul_mv_mma_ne12; 37 38 tile.offset0 = (i12/FC_mul_mv_mma_r2)*args.nb02 + (i13/FC_mul_mv_mma_r3)*args.nb03; 39 tile.offset1 = i12*args.nb12 + i13*args.nb13; 40 41 return tile; 42 } 43 44 // the src0 row of A fragment row fm in 8-row tile t, clamped to the last row 45 inline device const char * mul_mv_mma_src0_row( 46 thread const mul_mv_mma_tile & tile, constant ggml_metal_kargs_mul_mv_ext & args, device const char * src0, short t) { 47 const int r = min(tile.i01 + 8*t + tile.fm, args.ne01 - 1); 48 return src0 + tile.offset0 + (uint64_t) r*args.nb01; 49 } 50 51 // the src1 row of B fragment column fn + e in 8-row tile rt, clamped to the last row 52 inline device const float * mul_mv_mma_src1_row( 53 thread const mul_mv_mma_tile & tile, constant ggml_metal_kargs_mul_mv_ext & args, device const char * src1, short rt, short e) { 54 const int r = min(tile.i11 + 8*rt + tile.fn + e, args.ne11 - 1); 55 return (device const float *) (src1 + tile.offset1 + (uint64_t) r*args.nb11); 56 } 57 58 constexpr constant static ushort mma_f16_1024_bits = 0x6400; 59 constexpr constant static half mma_f16_1024 = 1024.0h; 60 61 // the halves 1024 + q for integers q < 1024: exact normal values, unlike the subnormal q*2^-24 that Metal may flush to zero 62 inline half2 mul_mv_mma_1024_plus(ushort2 q) { 63 return as_type(q | mma_f16_1024_bits); 64 } 65 66 // adds up the K slices of the NSG simdgroups for an 8*NT x 8*RT output tile and writes it, plus the residual in src2 if FC_mul_mv_mma_add 67 template 68 inline void mul_mv_mma_store( 69 thread float (&acc)[RT][NT][2], 70 constant ggml_metal_kargs_mul_mv_ext & args, 71 device const char * src2, 72 device char * dst, 73 threadgroup char * shmem, 74 thread const mul_mv_mma_tile & tile, ushort tiisg, ushort sgitg) { 75 const short NSG = FC_mul_mv_mma_nsg; 76 77 threadgroup float * red = (threadgroup float *) shmem; 78 79 FOR_UNROLL (short rt = 0; rt < RT; ++rt) { 80 FOR_UNROLL (short t = 0; t < NT; ++t) { 81 red[((sgitg*RT + rt)*NT + t)*64 + 2*tiisg + 0] = acc[rt][t][0]; 82 red[((sgitg*RT + rt)*NT + t)*64 + 2*tiisg + 1] = acc[rt][t][1]; 83 } 84 } 85 86 threadgroup_barrier(mem_flags::mem_threadgroup); 87 88 device float * dst_f32 = (device float *) dst + (uint64_t) tile.i1m*args.ne0*args.ne1; 89 device const float * res_f32 = (device const float *) src2 + (uint64_t) tile.i1m*args.ne0*args.ne1; 90 91 for (short idx = sgitg*32 + tiisg; idx < RT*NT*64; idx += NSG*32) { 92 float sum = 0.0f; 93 for (short sg = 0; sg < NSG; ++sg) { 94 sum += red[sg*(RT*NT*64) + idx]; 95 } 96 97 const short rt = idx/(NT*64); 98 const short t = (idx/64) % NT; 99 const short l = (idx % 64)/2; 100 const short e = idx % 2; 101 102 const int r0 = tile.i01 + 8*t + mul_mv_mma_lane_fm(l); 103 const int r1 = tile.i11 + 8*rt + mul_mv_mma_lane_fn(l) + e; 104 105 if (r0 < args.ne01 && r1 < args.ne11) { 106 const uint64_t i = (uint64_t) r1*args.ne0 + r0; 107 dst_f32[i] = FC_mul_mv_mma_add ? sum + res_f32[i] : sum; 108 } 109 } 110 } 111 112 // the per-type parts of kernel_mul_mv_mma_blk for 32-weight blocks. a src1 block splits into halves b0 and b1, 113 // and MMA step s uses half b1 when b1_step(s) and the .y value of a pair when y_step(s) 114 // q4_0: A lane (m, j) holds qs ushorts j and j + 1 (j even); a high nibble stays in place as 16*q, so b1 is divided by 16. 115 // B lane k = fm holds src1 values 2*k, 2*k + 1 (b0, low nibbles) and 2*k + 16, 2*k + 17 (b1, high nibbles) of a block 116 struct mul_mv_mma_q4_0 { 117 typedef block_q4_0 block; 118 typedef ushort2 quants; 119 120 // weights per block, and the float2 offset of b1 in a src1 block 121 enum { qk = QK4_0, b1 = QK4_0/4 }; 122 123 static short a_off(short fn) { return 1 + fn; } 124 static short b_off(short fm) { return 2*fm; } 125 126 static quants load(device const ushort * qs) { return ushort2(qs[0], qs[1]); } 127 static quants prep(quants q) { return q; } 128 static float2 prep_b1(float2 v) { 129 constexpr float hi_scale = 1.0f/16; 130 return v*hi_scale; 131 } 132 133 static bool b1_step(short s) { return s % 2 != 0; } 134 static bool y_step (short s) { return s >= 2; } 135 136 static half2 frag(quants q, short s) { 137 constexpr ushort lo_mask = 0x000F; 138 constexpr ushort hi_mask = 0x00F0; 139 constexpr half lo_zero = 8.0h; 140 constexpr half hi_zero = 16*lo_zero; 141 142 const ushort2 qq = s < 2 ? q : q >> 8; 143 return s % 2 == 0 ? mul_mv_mma_1024_plus(qq & lo_mask) - (mma_f16_1024 + lo_zero) : mul_mv_mma_1024_plus(qq & hi_mask) - (mma_f16_1024 + hi_zero); 144 } 145 }; 146 147 // q8_0: A lane (m, j) holds qs bytes 4*j .. 4*j + 7 (j even); flipping the sign bit of a quant byte gives the unsigned q + 128. 148 // B lane k = fm holds src1 values b, b + 1 (b0) and b + 4, b + 5 (b1) of a block, b = 8*(k/2) + 2*(k%2) 149 struct mul_mv_mma_q8_0 { 150 typedef block_q8_0 block; 151 typedef ushort4 quants; 152 153 // weights per block, and the float2 offset of b1 in a src1 block 154 enum { qk = QK8_0, b1 = 2 }; 155 156 static short a_off(short fn) { return 1 + 2*fn; } 157 static short b_off(short fm) { return 8*(fm/2) + 2*(fm%2); } 158 159 static quants load(device const ushort * qs) { return ushort4(qs[0], qs[1], qs[2], qs[3]); } 160 static quants prep(quants q) { 161 constexpr ushort sign_bits = 0x8080; 162 return q ^ ushort4(sign_bits); 163 } 164 static float2 prep_b1(float2 v) { return v; } 165 166 static bool b1_step(short s) { return s >= 2; } 167 static bool y_step (short s) { return s % 2 != 0; } 168 169 static half2 frag(quants q, short s) { 170 constexpr ushort byte_mask = 0x00FF; 171 constexpr half q_bias = mma_f16_1024 + 128.0h; 172 173 const ushort2 w = s < 2 ? q.xy : q.zw; 174 const ushort2 qq = s % 2 == 0 ? w : w >> 8; 175 return mul_mv_mma_1024_plus(qq & byte_mask) - q_bias; 176 } 177 }; 178 179 template 180 inline void load_mma_blk_a(device const ushort * const x[NT], int off, short fn, thread typename Q::quants * q, thread float * d) { 181 FOR_UNROLL (short t = 0; t < NT; ++t) { 182 device const ushort * qs = x[t] + off; 183 q[t] = Q::load(qs); 184 d[t] = as_type(*(qs - Q::a_off(fn))); 185 } 186 } 187 188 template 189 inline void load_mma_blk_b(device const float2 * const y[RT][2], int ib, thread float2 (*b0)[2], thread float2 (*b1)[2]) { 190 FOR_UNROLL (short rt = 0; rt < RT; ++rt) { 191 FOR_UNROLL (short e = 0; e < 2; ++e) { 192 b0[rt][e] = y[rt][e][ib*(Q::qk/2)]; 193 b1[rt][e] = y[rt][e][ib*(Q::qk/2) + Q::b1]; 194 } 195 } 196 } 197 198 // few-row mat-mat (2..16 src1 rows) on 8x8 simdgroup matrices for 32-weight block types: a threadgroup reads each weight once 199 // for 8*NT src0 rows x 8*RT src1 rows, and its NSG simdgroups split K 200 template 201 kernel void kernel_mul_mv_mma_blk( 202 constant ggml_metal_kargs_mul_mv_ext & args, 203 device const char * src0, 204 device const char * src1, 205 device char * dst, 206 device const char * src2, 207 threadgroup char * shmem [[threadgroup(0)]], 208 uint3 tgpig[[threadgroup_position_in_grid]], 209 ushort tiisg[[thread_index_in_simdgroup]], 210 ushort sgitg[[simdgroup_index_in_threadgroup]]) { 211 const short NSG = FC_mul_mv_mma_nsg; 212 213 const mul_mv_mma_tile tile = mul_mv_mma_tile_init(args, tgpig, tiisg); 214 215 device const ushort * x[NT]; 216 FOR_UNROLL (short t = 0; t < NT; ++t) { 217 x[t] = (device const ushort *) mul_mv_mma_src0_row(tile, args, src0, t) + Q::a_off(tile.fn); 218 } 219 220 device const float2 * y[RT][2]; 221 FOR_UNROLL (short rt = 0; rt < RT; ++rt) { 222 FOR_UNROLL (short e = 0; e < 2; ++e) { 223 y[rt][e] = (device const float2 *) (mul_mv_mma_src1_row(tile, args, src1, rt, e) + Q::b_off(tile.fm)); 224 } 225 } 226 227 float acc[RT][NT][2] = {}; 228 229 const int nb = FC_mul_mv_mma_ne00/Q::qk; 230 231 // a block is d, then the quants 232 constexpr short us_blk = sizeof(typename Q::block)/2; 233 234 typename Q::quants q[NT]; 235 float d[NT]; 236 float2 b0[RT][2]; 237 float2 b1[RT][2]; 238 239 const int ib0 = min((int) sgitg, nb - 1); 240 load_mma_blk_a(x, ib0*us_blk, tile.fn, q, d); 241 load_mma_blk_b(y, ib0, b0, b1); 242 243 for (int ib = sgitg; ib < nb; ib += NSG) { 244 typename Q::quants qc[NT]; 245 float dc[NT]; 246 float2 b0c[RT][2]; 247 float2 b1c[RT][2]; 248 249 FOR_UNROLL (short t = 0; t < NT; ++t) { 250 qc[t] = Q::prep(q[t]); 251 dc[t] = d[t]; 252 } 253 FOR_UNROLL (short rt = 0; rt < RT; ++rt) { 254 FOR_UNROLL (short e = 0; e < 2; ++e) { 255 b0c[rt][e] = b0[rt][e]; 256 b1c[rt][e] = Q::prep_b1(b1[rt][e]); 257 } 258 } 259 260 const int ibn = min(ib + NSG, nb - 1); 261 load_mma_blk_a(x, ibn*us_blk, tile.fn, q, d); 262 load_mma_blk_b(y, ibn, b0, b1); 263 264 simdgroup_float8x8 mp[RT][NT]; 265 FOR_UNROLL (short rt = 0; rt < RT; ++rt) { 266 FOR_UNROLL (short t = 0; t < NT; ++t) { 267 mp[rt][t] = make_filled_simdgroup_matrix(0.0f); 268 } 269 } 270 271 FOR_UNROLL (short s = 0; s < 4; ++s) { 272 simdgroup_float8x8 mb[RT]; 273 FOR_UNROLL (short rt = 0; rt < RT; ++rt) { 274 const float2 v0 = Q::b1_step(s) ? b1c[rt][0] : b0c[rt][0]; 275 const float2 v1 = Q::b1_step(s) ? b1c[rt][1] : b0c[rt][1]; 276 mb[rt].thread_elements()[0] = Q::y_step(s) ? v0.y : v0.x; 277 mb[rt].thread_elements()[1] = Q::y_step(s) ? v1.y : v1.x; 278 } 279 280 FOR_UNROLL (short t = 0; t < NT; ++t) { 281 const half2 h = Q::frag(qc[t], s); 282 283 simdgroup_half8x8 ma; 284 ma.thread_elements()[0] = h.x; 285 ma.thread_elements()[1] = h.y; 286 287 FOR_UNROLL (short rt = 0; rt < RT; ++rt) { 288 simdgroup_multiply_accumulate(mp[rt][t], ma, mb[rt], mp[rt][t]); 289 } 290 } 291 } 292 293 FOR_UNROLL (short t = 0; t < NT; ++t) { 294 FOR_UNROLL (short rt = 0; rt < RT; ++rt) { 295 acc[rt][t][0] = fma(dc[t], mp[rt][t].thread_elements()[0], acc[rt][t][0]); 296 acc[rt][t][1] = fma(dc[t], mp[rt][t].thread_elements()[1], acc[rt][t][1]); 297 } 298 } 299 } 300 301 mul_mv_mma_store(acc, args, src2, dst, shmem, tile, tiisg, sgitg); 302 } 303 304 typedef decltype(kernel_mul_mv_mma_blk<4, 1, mul_mv_mma_q4_0>) mul_mv_mma_t; 305 306 template [[host_name("kernel_mul_mv_mma_q4_0_f32_nt1_rt1")]] kernel mul_mv_mma_t kernel_mul_mv_mma_blk<1, 1, mul_mv_mma_q4_0>; 307 template [[host_name("kernel_mul_mv_mma_q4_0_f32_nt2_rt1")]] kernel mul_mv_mma_t kernel_mul_mv_mma_blk<2, 1, mul_mv_mma_q4_0>; 308 template [[host_name("kernel_mul_mv_mma_q4_0_f32_nt4_rt1")]] kernel mul_mv_mma_t kernel_mul_mv_mma_blk<4, 1, mul_mv_mma_q4_0>; 309 template [[host_name("kernel_mul_mv_mma_q4_0_f32_nt1_rt2")]] kernel mul_mv_mma_t kernel_mul_mv_mma_blk<1, 2, mul_mv_mma_q4_0>; 310 template [[host_name("kernel_mul_mv_mma_q4_0_f32_nt2_rt2")]] kernel mul_mv_mma_t kernel_mul_mv_mma_blk<2, 2, mul_mv_mma_q4_0>; 311 template [[host_name("kernel_mul_mv_mma_q4_0_f32_nt4_rt2")]] kernel mul_mv_mma_t kernel_mul_mv_mma_blk<4, 2, mul_mv_mma_q4_0>; 312 313 template [[host_name("kernel_mul_mv_mma_q8_0_f32_nt1_rt1")]] kernel mul_mv_mma_t kernel_mul_mv_mma_blk<1, 1, mul_mv_mma_q8_0>; 314 template [[host_name("kernel_mul_mv_mma_q8_0_f32_nt2_rt1")]] kernel mul_mv_mma_t kernel_mul_mv_mma_blk<2, 1, mul_mv_mma_q8_0>; 315 template [[host_name("kernel_mul_mv_mma_q8_0_f32_nt4_rt1")]] kernel mul_mv_mma_t kernel_mul_mv_mma_blk<4, 1, mul_mv_mma_q8_0>; 316 317 // q5_K scale and min of sub-block j from the 12 packed bytes, held as 3 words 318 inline float2 mul_mv_mma_q5_K_scale_min(thread const uint * w, short j) { 319 if (j < 4) { 320 return float2((w[0] >> 8*j) & 63, (w[1] >> 8*j) & 63); 321 } 322 const short k = 8*(j - 4); 323 return float2(((w[2] >> k) & 0xF) | (((w[0] >> (k + 6)) & 3) << 4), ((w[2] >> (k + 4)) & 0xF) | (((w[1] >> (k + 6)) & 3) << 4)); 324 } 325 326 // the A fragments of one qs/qh word: lo holds the 5-bit quants q of the low-nibble sub-block of a pair, hi holds 16*q of the high-nibble sub-block. 327 // step e0 takes bytes 0 and 2 of the word, step e1 takes bytes 1 and 3; hs holds the qh bits of the pair. 328 inline void mul_mv_mma_q5_K_frags(uint q, uint hs, thread half2 * lo, thread half2 * hi) { 329 const ushort2 qw = as_type(q); 330 const ushort2 hw = as_type(hs); 331 332 lo[0] = mul_mv_mma_1024_plus((qw & 0x000F) | ((hw << 4) & 0x0010)) - mma_f16_1024; 333 lo[1] = mul_mv_mma_1024_plus(((qw >> 8) & 0x000F) | ((hw >> 4) & 0x0010)) - mma_f16_1024; 334 hi[0] = mul_mv_mma_1024_plus((qw & 0x00F0) | ((hw << 7) & 0x0100)) - mma_f16_1024; 335 hi[1] = mul_mv_mma_1024_plus(((qw >> 8) & 0x00F0) | ((hw >> 1) & 0x0100)) - mma_f16_1024; 336 } 337 338 struct mul_mv_mma_q5_K_a { 339 uint2 q; 340 uint2 h; 341 uint sc[3]; 342 uint dm; 343 }; 344 345 template 346 inline void load_q5_K_mma_a(device const block_q5_K * const x[NT], int ip, short fn, thread mul_mv_mma_q5_K_a * a) { 347 constexpr short pairs = QK_K/64; 348 349 FOR_UNROLL (short t = 0; t < NT; ++t) { 350 device const block_q5_K * xb = x[t] + ip/pairs; 351 device const uint * sp = (device const uint *) xb->scales; 352 353 a[t].q = *((device const uint2 *) (xb->qs + 32*(ip%pairs)) + fn/2); 354 a[t].h = *((device const uint2 *) xb->qh + fn/2); 355 a[t].sc[0] = sp[0]; 356 a[t].sc[1] = sp[1]; 357 a[t].sc[2] = sp[2]; 358 a[t].dm = *((device const uint *) xb); 359 } 360 } 361 362 // xs[rt][e][h]: src1 values of sub-block h of the pair, for the steps (b, b + 1) and (b + 4, b + 5) 363 template 364 inline void load_q5_K_mma_b(device const float2 * const y[RT][2], int ip, thread float2 (*xs)[2][2][2]) { 365 FOR_UNROLL (short rt = 0; rt < RT; ++rt) { 366 FOR_UNROLL (short e = 0; e < 2; ++e) { 367 FOR_UNROLL (short h = 0; h < 2; ++h) { 368 xs[rt][e][h][0] = y[rt][e][ip*32 + h*16 + 0]; 369 xs[rt][e][h][1] = y[rt][e][ip*32 + h*16 + 2]; 370 } 371 } 372 } 373 } 374 375 // few-row mat-mat for q5_K over pairs of 32-weight sub-blocks, laid out like mul_mv_mma_q4_0: A lane (m, j) holds qs and qh bytes 4*j .. 4*j + 7 of a pair (j even). 376 // the 1/16 of the in-place high nibbles and the sub-block scales go into the accumulation; the mins are removed with the src1 sums. 377 template 378 kernel void kernel_mul_mv_mma_q5_K_f32( 379 constant ggml_metal_kargs_mul_mv_ext & args, 380 device const char * src0, 381 device const char * src1, 382 device char * dst, 383 device const char * src2, 384 threadgroup char * shmem [[threadgroup(0)]], 385 uint3 tgpig[[threadgroup_position_in_grid]], 386 ushort tiisg[[thread_index_in_simdgroup]], 387 ushort sgitg[[simdgroup_index_in_threadgroup]]) { 388 const short NSG = FC_mul_mv_mma_nsg; 389 390 constexpr float hi_scale = 1.0f/16; 391 constexpr short pairs = QK_K/64; 392 393 const mul_mv_mma_tile tile = mul_mv_mma_tile_init(args, tgpig, tiisg); 394 395 device const block_q5_K * x[NT]; 396 FOR_UNROLL (short t = 0; t < NT; ++t) { 397 x[t] = (device const block_q5_K *) mul_mv_mma_src0_row(tile, args, src0, t); 398 } 399 400 // B lane k = fm holds src1 values b, b + 1, b + 4, b + 5 of each sub-block, b = 8*(k/2) + 2*(k%2) 401 device const float2 * y[RT][2]; 402 FOR_UNROLL (short rt = 0; rt < RT; ++rt) { 403 FOR_UNROLL (short e = 0; e < 2; ++e) { 404 y[rt][e] = (device const float2 *) (mul_mv_mma_src1_row(tile, args, src1, rt, e) + 8*(tile.fm/2) + 2*(tile.fm%2)); 405 } 406 } 407 408 float acc[RT][NT][2] = {}; 409 410 const int np = FC_mul_mv_mma_ne00/64; 411 412 mul_mv_mma_q5_K_a an[NT]; 413 float2 xn[RT][2][2][2]; 414 415 const int ip0 = min((int) sgitg, np - 1); 416 load_q5_K_mma_a(x, ip0, tile.fn, an); 417 load_q5_K_mma_b(y, ip0, xn); 418 419 for (int ip = sgitg; ip < np; ip += NSG) { 420 const short p = ip%pairs; 421 422 mul_mv_mma_q5_K_a ac[NT]; 423 float2 xs[RT][2][2][2]; 424 FOR_UNROLL (short t = 0; t < NT; ++t) { 425 ac[t] = an[t]; 426 } 427 FOR_UNROLL (short rt = 0; rt < RT; ++rt) { 428 FOR_UNROLL (short e = 0; e < 2; ++e) { 429 FOR_UNROLL (short h = 0; h < 2; ++h) { 430 xs[rt][e][h][0] = xn[rt][e][h][0]; 431 xs[rt][e][h][1] = xn[rt][e][h][1]; 432 } 433 } 434 } 435 436 const int ipn = min(ip + NSG, np - 1); 437 load_q5_K_mma_a(x, ipn, tile.fn, an); 438 load_q5_K_mma_b(y, ipn, xn); 439 440 float c[RT][2][2]; 441 FOR_UNROLL (short rt = 0; rt < RT; ++rt) { 442 FOR_UNROLL (short e = 0; e < 2; ++e) { 443 FOR_UNROLL (short h = 0; h < 2; ++h) { 444 float u = xs[rt][e][h][0].x + xs[rt][e][h][0].y + xs[rt][e][h][1].x + xs[rt][e][h][1].y; 445 u += simd_shuffle_xor(u, 2); 446 u += simd_shuffle_xor(u, 4); 447 u += simd_shuffle_xor(u, 16); 448 c[rt][e][h] = u; 449 } 450 } 451 } 452 453 FOR_UNROLL (short t = 0; t < NT; ++t) { 454 const float2 dm = float2(as_type(ac[t].dm)); 455 const float2 sm0 = mul_mv_mma_q5_K_scale_min(ac[t].sc, 2*p + 0); 456 const float2 sm1 = mul_mv_mma_q5_K_scale_min(ac[t].sc, 2*p + 1); 457 458 half2 a[2][2][2]; 459 mul_mv_mma_q5_K_frags(ac[t].q.x, ac[t].h.x >> 2*p, a[0][0], a[1][0]); 460 mul_mv_mma_q5_K_frags(ac[t].q.y, ac[t].h.y >> 2*p, a[0][1], a[1][1]); 461 462 FOR_UNROLL (short hh = 0; hh < 2; ++hh) { 463 simdgroup_float8x8 mp[RT]; 464 FOR_UNROLL (short rt = 0; rt < RT; ++rt) { 465 mp[rt] = make_filled_simdgroup_matrix(0.0f); 466 } 467 468 FOR_UNROLL (short s = 0; s < 4; ++s) { 469 simdgroup_half8x8 ma; 470 ma.thread_elements()[0] = a[hh][s/2][s%2].x; 471 ma.thread_elements()[1] = a[hh][s/2][s%2].y; 472 473 FOR_UNROLL (short rt = 0; rt < RT; ++rt) { 474 simdgroup_float8x8 mb; 475 mb.thread_elements()[0] = s % 2 == 0 ? xs[rt][0][hh][s/2].x : xs[rt][0][hh][s/2].y; 476 mb.thread_elements()[1] = s % 2 == 0 ? xs[rt][1][hh][s/2].x : xs[rt][1][hh][s/2].y; 477 478 simdgroup_multiply_accumulate(mp[rt], ma, mb, mp[rt]); 479 } 480 } 481 482 const float2 smh = hh == 0 ? sm0 : sm1; 483 const float dsc = dm.x*smh.x*(hh == 0 ? 1.0f : hi_scale); 484 const float dmn = dm.y*smh.y; 485 486 FOR_UNROLL (short rt = 0; rt < RT; ++rt) { 487 acc[rt][t][0] = fma(dsc, mp[rt].thread_elements()[0], fma(-dmn, c[rt][0][hh], acc[rt][t][0])); 488 acc[rt][t][1] = fma(dsc, mp[rt].thread_elements()[1], fma(-dmn, c[rt][1][hh], acc[rt][t][1])); 489 } 490 } 491 } 492 } 493 494 mul_mv_mma_store(acc, args, src2, dst, shmem, tile, tiisg, sgitg); 495 } 496 497 template [[host_name("kernel_mul_mv_mma_q5_K_f32_nt1_rt1")]] kernel mul_mv_mma_t kernel_mul_mv_mma_q5_K_f32<1, 1>; 498 template [[host_name("kernel_mul_mv_mma_q5_K_f32_nt2_rt1")]] kernel mul_mv_mma_t kernel_mul_mv_mma_q5_K_f32<2, 1>; 499 template [[host_name("kernel_mul_mv_mma_q5_K_f32_nt4_rt1")]] kernel mul_mv_mma_t kernel_mul_mv_mma_q5_K_f32<4, 1>; 500 template [[host_name("kernel_mul_mv_mma_q5_K_f32_nt1_rt2")]] kernel mul_mv_mma_t kernel_mul_mv_mma_q5_K_f32<1, 2>; 501 template [[host_name("kernel_mul_mv_mma_q5_K_f32_nt2_rt2")]] kernel mul_mv_mma_t kernel_mul_mv_mma_q5_K_f32<2, 2>; 502 template [[host_name("kernel_mul_mv_mma_q5_K_f32_nt4_rt2")]] kernel mul_mv_mma_t kernel_mul_mv_mma_q5_K_f32<4, 2>; 503 504 // few-row mat-mat for any type with a 16-weight dequantizer: a lane dequantizes 16 consecutive weights of a 64-weight chunk once for all src1 rows. 505 // MMA step s at MMA-k index j reads chunk weight 16*(j/2) + 8*(j%2) + s. 506 template 507 kernel void kernel_mul_mv_mma_gen( 508 constant ggml_metal_kargs_mul_mv_ext & args, 509 device const char * src0, 510 device const char * src1, 511 device char * dst, 512 device const char * src2, 513 threadgroup char * shmem [[threadgroup(0)]], 514 uint3 tgpig[[threadgroup_position_in_grid]], 515 ushort tiisg[[thread_index_in_simdgroup]], 516 ushort sgitg[[simdgroup_index_in_threadgroup]]) { 517 const short NSG = FC_mul_mv_mma_nsg; 518 519 const mul_mv_mma_tile tile = mul_mv_mma_tile_init(args, tgpig, tiisg); 520 521 device const block_q * x[NT]; 522 FOR_UNROLL (short t = 0; t < NT; ++t) { 523 x[t] = (device const block_q *) mul_mv_mma_src0_row(tile, args, src0, t); 524 } 525 526 // B lane j = fm reads chunk values 16*(fm/2) + 8*(fm%2) .. +7 527 device const float4 * y[RT][2]; 528 FOR_UNROLL (short rt = 0; rt < RT; ++rt) { 529 FOR_UNROLL (short e = 0; e < 2; ++e) { 530 y[rt][e] = (device const float4 *) mul_mv_mma_src1_row(tile, args, src1, rt, e) + 4*(tile.fm/2) + 2*(tile.fm%2); 531 } 532 } 533 534 simdgroup_float8x8 mc[RT][NT]; 535 FOR_UNROLL (short rt = 0; rt < RT; ++rt) { 536 FOR_UNROLL (short t = 0; t < NT; ++t) { 537 mc[rt][t] = make_filled_simdgroup_matrix(0.0f); 538 } 539 } 540 541 const int nch = args.ne00/64; 542 543 for (int g = sgitg; g < nch; g += NSG) { 544 simdgroup_float8x8 mb[RT][8]; 545 FOR_UNROLL (short rt = 0; rt < RT; ++rt) { 546 const float4 a0 = y[rt][0][16*g + 0]; 547 const float4 a1 = y[rt][0][16*g + 1]; 548 const float4 b0 = y[rt][1][16*g + 0]; 549 const float4 b1 = y[rt][1][16*g + 1]; 550 FOR_UNROLL (short s = 0; s < 4; ++s) { 551 mb[rt][s ].thread_elements()[0] = a0[s]; 552 mb[rt][s ].thread_elements()[1] = b0[s]; 553 mb[rt][s + 4].thread_elements()[0] = a1[s]; 554 mb[rt][s + 4].thread_elements()[1] = b1[s]; 555 } 556 } 557 558 const int ci = 4*g + tile.fn/2; 559 560 FOR_UNROLL (short t = 0; t < NT; ++t) { 561 float4x4 w; 562 dequantize_func(x[t] + ci/nl, ci%nl, w); 563 564 FOR_UNROLL (short s = 0; s < 8; ++s) { 565 simdgroup_float8x8 ma; 566 ma.thread_elements()[0] = w[s/4 ][s%4]; 567 ma.thread_elements()[1] = w[s/4 + 2][s%4]; 568 569 FOR_UNROLL (short rt = 0; rt < RT; ++rt) { 570 simdgroup_multiply_accumulate(mc[rt][t], ma, mb[rt][s], mc[rt][t]); 571 } 572 } 573 } 574 } 575 576 float acc[RT][NT][2]; 577 FOR_UNROLL (short rt = 0; rt < RT; ++rt) { 578 FOR_UNROLL (short t = 0; t < NT; ++t) { 579 acc[rt][t][0] = mc[rt][t].thread_elements()[0]; 580 acc[rt][t][1] = mc[rt][t].thread_elements()[1]; 581 } 582 } 583 584 mul_mv_mma_store(acc, args, src2, dst, shmem, tile, tiisg, sgitg); 585 } 586 587 #define MUL_MV_MMA_GEN(tname, bq, nl, deq) \ 588 template [[host_name("kernel_mul_mv_mma_" tname "_f32_nt1_rt1")]] kernel mul_mv_mma_t kernel_mul_mv_mma_gen<1, 1, bq, nl, deq>; \ 589 template [[host_name("kernel_mul_mv_mma_" tname "_f32_nt2_rt1")]] kernel mul_mv_mma_t kernel_mul_mv_mma_gen<2, 1, bq, nl, deq>; \ 590 template [[host_name("kernel_mul_mv_mma_" tname "_f32_nt4_rt1")]] kernel mul_mv_mma_t kernel_mul_mv_mma_gen<4, 1, bq, nl, deq>; \ 591 template [[host_name("kernel_mul_mv_mma_" tname "_f32_nt1_rt2")]] kernel mul_mv_mma_t kernel_mul_mv_mma_gen<1, 2, bq, nl, deq>; \ 592 template [[host_name("kernel_mul_mv_mma_" tname "_f32_nt2_rt2")]] kernel mul_mv_mma_t kernel_mul_mv_mma_gen<2, 2, bq, nl, deq>; \ 593 template [[host_name("kernel_mul_mv_mma_" tname "_f32_nt4_rt2")]] kernel mul_mv_mma_t kernel_mul_mv_mma_gen<4, 2, bq, nl, deq>; 594 595 // q8_0 with 9..16 src1 rows: the per-block scaling above is slower than dequantizing to f32 596 template [[host_name("kernel_mul_mv_mma_q8_0_f32_nt1_rt2")]] kernel mul_mv_mma_t kernel_mul_mv_mma_gen<1, 2, block_q8_0, 2, dequantize_q8_0>; 597 template [[host_name("kernel_mul_mv_mma_q8_0_f32_nt2_rt2")]] kernel mul_mv_mma_t kernel_mul_mv_mma_gen<2, 2, block_q8_0, 2, dequantize_q8_0>; 598 template [[host_name("kernel_mul_mv_mma_q8_0_f32_nt4_rt2")]] kernel mul_mv_mma_t kernel_mul_mv_mma_gen<4, 2, block_q8_0, 2, dequantize_q8_0>; 599 600 MUL_MV_MMA_GEN("f32", float4x4, 1, dequantize_f32) 601 MUL_MV_MMA_GEN("f16", half4x4, 1, dequantize_f16) 602 MUL_MV_MMA_GEN("q4_1", block_q4_1, 2, dequantize_q4_1) 603 MUL_MV_MMA_GEN("q5_0", block_q5_0, 2, dequantize_q5_0) 604 MUL_MV_MMA_GEN("q5_1", block_q5_1, 2, dequantize_q5_1) 605 MUL_MV_MMA_GEN("q4_K", block_q4_K, QK_NL, dequantize_q4_K) 606 MUL_MV_MMA_GEN("q6_K", block_q6_K, QK_NL, dequantize_q6_K) 607 608 #if defined(GGML_METAL_HAS_BF16) 609 MUL_MV_MMA_GEN("bf16", bfloat4x4, 1, dequantize_bf16) 610 #endif 611 MUL_MV_MMA_GEN("q1_0", block_q1_0, 8, dequantize_q1_0) 612 MUL_MV_MMA_GEN("q2_0", block_q2_0, 4, dequantize_q2_0) 613 MUL_MV_MMA_GEN("mxfp4", block_mxfp4, 2, dequantize_mxfp4) 614 MUL_MV_MMA_GEN("q2_K", block_q2_K, QK_NL, dequantize_q2_K) 615 MUL_MV_MMA_GEN("q3_K", block_q3_K, QK_NL, dequantize_q3_K) 616 MUL_MV_MMA_GEN("iq2_xxs", block_iq2_xxs, QK_NL, dequantize_iq2_xxs) 617 MUL_MV_MMA_GEN("iq2_xs", block_iq2_xs, QK_NL, dequantize_iq2_xs) 618 MUL_MV_MMA_GEN("iq2_s", block_iq2_s, QK_NL, dequantize_iq2_s) 619 MUL_MV_MMA_GEN("iq3_xxs", block_iq3_xxs, QK_NL, dequantize_iq3_xxs) 620 MUL_MV_MMA_GEN("iq3_s", block_iq3_s, QK_NL, dequantize_iq3_s) 621 MUL_MV_MMA_GEN("iq1_s", block_iq1_s, QK_NL, dequantize_iq1_s) 622 MUL_MV_MMA_GEN("iq1_m", block_iq1_m, QK_NL, dequantize_iq1_m) 623 MUL_MV_MMA_GEN("iq4_nl", block_iq4_nl, 2, dequantize_iq4_nl) 624 MUL_MV_MMA_GEN("iq4_xs", block_iq4_xs, QK_NL, dequantize_iq4_xs) 625 MUL_MV_MMA_GEN("tq2_0", block_tq2_0, QK_NL, dequantize_tq2_0) 626 627 #undef MUL_MV_MMA_GEN