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.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.metal` 3430 lines · 134241 bytes Raw Blame History 1 #include "common.h" 2 #include "dequantize.h" 3 // Q1_0 dot product: dot = d * (2 * Σ(yl[i] where bit=1) - sumy) 4 inline float block_q_n_dot_y(device const block_q1_0 * qb_curr, float sumy, thread float * yl, int il) { 5 device const uint8_t * qs = qb_curr->qs + il / 8; 6 const uint8_t b0 = qs[0]; 7 const uint8_t b1 = qs[1]; 8 9 float acc = 0.0f; 10 11 acc += select(0.0f, yl[ 0], bool(b0 & 0x01)); 12 acc += select(0.0f, yl[ 1], bool(b0 & 0x02)); 13 acc += select(0.0f, yl[ 2], bool(b0 & 0x04)); 14 acc += select(0.0f, yl[ 3], bool(b0 & 0x08)); 15 acc += select(0.0f, yl[ 4], bool(b0 & 0x10)); 16 acc += select(0.0f, yl[ 5], bool(b0 & 0x20)); 17 acc += select(0.0f, yl[ 6], bool(b0 & 0x40)); 18 acc += select(0.0f, yl[ 7], bool(b0 & 0x80)); 19 20 acc += select(0.0f, yl[ 8], bool(b1 & 0x01)); 21 acc += select(0.0f, yl[ 9], bool(b1 & 0x02)); 22 acc += select(0.0f, yl[10], bool(b1 & 0x04)); 23 acc += select(0.0f, yl[11], bool(b1 & 0x08)); 24 acc += select(0.0f, yl[12], bool(b1 & 0x10)); 25 acc += select(0.0f, yl[13], bool(b1 & 0x20)); 26 acc += select(0.0f, yl[14], bool(b1 & 0x40)); 27 acc += select(0.0f, yl[15], bool(b1 & 0x80)); 28 29 return qb_curr->d * (2.0f * acc - sumy); 30 } 31 32 // Q2_0 dot: d * (sum_lo(y) + 2*sum_hi(y) - sumy) via per-bit conditional adds 33 inline float block_q_n_dot_y(device const block_q2_0 * qb_curr, float sumy, thread float * yl, int il) { 34 device const uint8_t * qs = qb_curr->qs + (il / 4); 35 const uint8_t b0 = qs[0]; 36 const uint8_t b1 = qs[1]; 37 const uint8_t b2 = qs[2]; 38 const uint8_t b3 = qs[3]; 39 40 // Accumulate where low bit is set (bits 0,2,4,6 of each byte) 41 float acc_lo = 0.0f; 42 acc_lo += select(0.0f, yl[ 0], bool(b0 & 0x01)); 43 acc_lo += select(0.0f, yl[ 1], bool(b0 & 0x04)); 44 acc_lo += select(0.0f, yl[ 2], bool(b0 & 0x10)); 45 acc_lo += select(0.0f, yl[ 3], bool(b0 & 0x40)); 46 acc_lo += select(0.0f, yl[ 4], bool(b1 & 0x01)); 47 acc_lo += select(0.0f, yl[ 5], bool(b1 & 0x04)); 48 acc_lo += select(0.0f, yl[ 6], bool(b1 & 0x10)); 49 acc_lo += select(0.0f, yl[ 7], bool(b1 & 0x40)); 50 acc_lo += select(0.0f, yl[ 8], bool(b2 & 0x01)); 51 acc_lo += select(0.0f, yl[ 9], bool(b2 & 0x04)); 52 acc_lo += select(0.0f, yl[10], bool(b2 & 0x10)); 53 acc_lo += select(0.0f, yl[11], bool(b2 & 0x40)); 54 acc_lo += select(0.0f, yl[12], bool(b3 & 0x01)); 55 acc_lo += select(0.0f, yl[13], bool(b3 & 0x04)); 56 acc_lo += select(0.0f, yl[14], bool(b3 & 0x10)); 57 acc_lo += select(0.0f, yl[15], bool(b3 & 0x40)); 58 59 // Accumulate where high bit is set (bits 1,3,5,7 of each byte) 60 float acc_hi = 0.0f; 61 acc_hi += select(0.0f, yl[ 0], bool(b0 & 0x02)); 62 acc_hi += select(0.0f, yl[ 1], bool(b0 & 0x08)); 63 acc_hi += select(0.0f, yl[ 2], bool(b0 & 0x20)); 64 acc_hi += select(0.0f, yl[ 3], bool(b0 & 0x80)); 65 acc_hi += select(0.0f, yl[ 4], bool(b1 & 0x02)); 66 acc_hi += select(0.0f, yl[ 5], bool(b1 & 0x08)); 67 acc_hi += select(0.0f, yl[ 6], bool(b1 & 0x20)); 68 acc_hi += select(0.0f, yl[ 7], bool(b1 & 0x80)); 69 acc_hi += select(0.0f, yl[ 8], bool(b2 & 0x02)); 70 acc_hi += select(0.0f, yl[ 9], bool(b2 & 0x08)); 71 acc_hi += select(0.0f, yl[10], bool(b2 & 0x20)); 72 acc_hi += select(0.0f, yl[11], bool(b2 & 0x80)); 73 acc_hi += select(0.0f, yl[12], bool(b3 & 0x02)); 74 acc_hi += select(0.0f, yl[13], bool(b3 & 0x08)); 75 acc_hi += select(0.0f, yl[14], bool(b3 & 0x20)); 76 acc_hi += select(0.0f, yl[15], bool(b3 & 0x80)); 77 78 return qb_curr->d * (acc_lo + 2.0f * acc_hi - sumy); 79 } 80 81 // function for calculate inner product between half a q4_0 block and 16 floats (yl), sumy is SUM(yl[i]) 82 // il indicates where the q4 quants begin (0 or QK4_0/4) 83 // we assume that the yl's have been multiplied with the appropriate scale factor 84 // that corresponds to the missing bit shifts (1, 1/16, 1/256, 1/4096) 85 inline float block_q_n_dot_y(device const block_q4_0 * qb_curr, float sumy, thread float * yl, int il) { 86 float d = qb_curr->d; 87 88 float acc[4] = { 0.0f, 0.0f, 0.0f, 0.0f }; 89 90 device const uint16_t * qs = ((device const uint16_t *) qb_curr + 1 + il/2); 91 92 for (int i = 0; i < 8; i += 2) { 93 acc[0] += yl[i + 0] * (qs[i / 2] & 0x000F); 94 acc[1] += yl[i + 1] * (qs[i / 2] & 0x0F00); 95 acc[2] += yl[i + 8] * (qs[i / 2] & 0x00F0); 96 acc[3] += yl[i + 9] * (qs[i / 2] & 0xF000); 97 } 98 99 return d * (sumy * -8.f + acc[0] + acc[1] + acc[2] + acc[3]); 100 } 101 102 // function for calculate inner product between half a q4_1 block and 16 floats (yl), sumy is SUM(yl[i]) 103 // il indicates where the q4 quants begin (0 or QK4_0/4) 104 // we assume that the yl's have been multiplied with the appropriate scale factor 105 // that corresponds to the missing bit shifts (1, 1/16, 1/256, 1/4096) 106 inline float block_q_n_dot_y(device const block_q4_1 * qb_curr, float sumy, thread float * yl, int il) { 107 float d = qb_curr->d; 108 float m = qb_curr->m; 109 110 float acc[4] = { 0.0f, 0.0f, 0.0f, 0.0f }; 111 112 device const uint16_t * qs = ((device const uint16_t *) qb_curr + 2 + il/2); 113 114 for (int i = 0; i < 8; i+=2) { 115 acc[0] += yl[i + 0] * (qs[i / 2] & 0x000F); 116 acc[1] += yl[i + 1] * (qs[i / 2] & 0x0F00); 117 acc[2] += yl[i + 8] * (qs[i / 2] & 0x00F0); 118 acc[3] += yl[i + 9] * (qs[i / 2] & 0xF000); 119 } 120 121 return d * (acc[0] + acc[1] + acc[2] + acc[3]) + sumy * m; 122 } 123 124 // function for calculate inner product between half a q5_0 block and 16 floats (yl), sumy is SUM(yl[i]) 125 // il indicates where the q5 quants begin (0 or QK5_0/4) 126 // we assume that the yl's have been multiplied with the appropriate scale factor 127 // that corresponds to the missing bit shifts (1, 1/16, 1/256, 1/4096) 128 inline float block_q_n_dot_y(device const block_q5_0 * qb_curr, float sumy, thread float * yl, int il) { 129 float d = qb_curr->d; 130 131 float acc[4] = { 0.0f, 0.0f, 0.0f, 0.0f }; 132 133 device const uint16_t * qs = ((device const uint16_t *)qb_curr + 3 + il/2); 134 const uint32_t qh = *((device const uint32_t *)qb_curr->qh); 135 136 for (int i = 0; i < 8; i+=2) { 137 acc[0] += yl[i + 0] * ((qs[i / 2] & 0x000F) | ((qh >> (i+0+il ) << 4 ) & 0x00010)); 138 acc[1] += yl[i + 1] * ((qs[i / 2] & 0x0F00) | ((qh >> (i+1+il ) << 12) & 0x01000)); 139 acc[2] += yl[i + 8] * ((qs[i / 2] & 0x00F0) | ((qh >> (i+0+il+QK5_0/2) << 8 ) & 0x00100)); 140 acc[3] += yl[i + 9] * ((qs[i / 2] & 0xF000) | ((qh >> (i+1+il+QK5_0/2) << 16) & 0x10000)); 141 } 142 143 return d * (sumy * -16.f + acc[0] + acc[1] + acc[2] + acc[3]); 144 } 145 146 // function for calculate inner product between half a q5_1 block and 16 floats (yl), sumy is SUM(yl[i]) 147 // il indicates where the q5 quants begin (0 or QK5_1/4) 148 // we assume that the yl's have been multiplied with the appropriate scale factor 149 // that corresponds to the missing bit shifts (1, 1/16, 1/256, 1/4096) 150 inline float block_q_n_dot_y(device const block_q5_1 * qb_curr, float sumy, thread float * yl, int il) { 151 float d = qb_curr->d; 152 float m = qb_curr->m; 153 154 float acc[4] = { 0.0f, 0.0f, 0.0f, 0.0f }; 155 156 device const uint16_t * qs = ((device const uint16_t *)qb_curr + 4 + il/2); 157 const uint32_t qh = *((device const uint32_t *)qb_curr->qh); 158 159 for (int i = 0; i < 8; i+=2) { 160 acc[0] += yl[i + 0] * ((qs[i / 2] & 0x000F) | ((qh >> (i+0+il ) << 4 ) & 0x00010)); 161 acc[1] += yl[i + 1] * ((qs[i / 2] & 0x0F00) | ((qh >> (i+1+il ) << 12) & 0x01000)); 162 acc[2] += yl[i + 8] * ((qs[i / 2] & 0x00F0) | ((qh >> (i+0+il+QK5_0/2) << 8 ) & 0x00100)); 163 acc[3] += yl[i + 9] * ((qs[i / 2] & 0xF000) | ((qh >> (i+1+il+QK5_0/2) << 16) & 0x10000)); 164 } 165 166 return d * (acc[0] + acc[1] + acc[2] + acc[3]) + sumy * m; 167 } 168 169 template 170 static inline void helper_mv_reduce_and_write( 171 device float * dst_f32, 172 float sumf[NR0], 173 const int r0, 174 const int ne01, 175 ushort tiisg, 176 ushort sgitg, 177 threadgroup char * shmem) { 178 constexpr short NW = N_SIMDWIDTH; 179 180 threadgroup float * shmem_f32[NR0]; 181 182 for (short row = 0; row < NR0; ++row) { 183 shmem_f32[row] = (threadgroup float *) shmem + NW*row; 184 185 if (sgitg == 0) { 186 shmem_f32[row][tiisg] = 0.0f; 187 } 188 189 sumf[row] = simd_sum(sumf[row]); 190 } 191 192 threadgroup_barrier(mem_flags::mem_threadgroup); 193 194 for (short row = 0; row < NR0; ++row) { 195 if (tiisg == 0) { 196 shmem_f32[row][sgitg] = sumf[row]; 197 } 198 } 199 200 threadgroup_barrier(mem_flags::mem_threadgroup); 201 202 for (short row = 0; row < NR0 && r0 + row < ne01; ++row) { 203 float tot = simd_sum(shmem_f32[row][tiisg]); 204 205 if (tiisg == 0 && sgitg == 0) { 206 dst_f32[r0 + row] = tot; 207 } 208 } 209 } 210 211 constant short FC_mul_mv_nsg [[function_constant(FC_MUL_MV + 0)]]; 212 constant short FC_mul_mv_nxpsg [[function_constant(FC_MUL_MV + 1)]]; 213 constant short FC_mul_mv_ne12 [[function_constant(FC_MUL_MV + 2)]]; 214 constant short FC_mul_mv_r2 [[function_constant(FC_MUL_MV + 3)]]; 215 constant short FC_mul_mv_r3 [[function_constant(FC_MUL_MV + 4)]]; 216 constant bool FC_mul_mv_split [[function_constant(FC_MUL_MV + 5)]]; 217 218 template 219 void mul_vec_q_n_f32_impl( 220 args_t args, 221 device const char * src0, 222 device const char * src1, 223 device char * dst, 224 threadgroup char * shmem, 225 uint3 tgpig, 226 ushort tiisg, 227 ushort sgitg) { 228 const short NSG = FC_mul_mv_nsg; 229 230 constexpr short NW = N_SIMDWIDTH; 231 constexpr short NQ = 16; 232 233 const int nb = args.ne00/QK4_0; 234 235 const int r0 = (tgpig.x*NSG + sgitg)*NR0; 236 //const int r0 = tgpig.x*NR0; 237 const int r1 = tgpig.y; 238 const int im = tgpig.z; 239 240 const uint i12 = im%FC_mul_mv_ne12; 241 const uint i13 = im/FC_mul_mv_ne12; 242 243 //const uint64_t offset0 = r0*args.nb01 + (i12/FC_mul_mv_r2)*args.nb02 + (i13/FC_mul_mv_r3)*args.nb03; 244 const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13; 245 246 //device const block_q_type * x = (device const block_q_type *) (src0 + offset0); 247 device const float * y = (device const float *) (src1 + offset1); 248 249 // pointers to src0 rows 250 device const block_q_type * ax[NR0]; 251 FOR_UNROLL (int row = 0; row < NR0; ++row) { 252 const uint64_t offset0 = (r0 + row)*args.nb01 + (i12/FC_mul_mv_r2)*args.nb02 + (i13/FC_mul_mv_r3)*args.nb03; 253 254 ax[row] = (device const block_q_type *) ((device char *) src0 + offset0); 255 } 256 257 float sumf[NR0] = {0.f}; 258 259 const short ix = (tiisg/(NW/NQ)); 260 const short il = (tiisg%(NW/NQ))*8; 261 262 //const int ib0 = sgitg*NQ + ix; 263 const int ib0 = ix; 264 265 float yl[16]; // src1 vector cache 266 267 //device const float * yb = y + ix*QK4_0 + il; 268 device const float * yb = y + ib0*QK4_0 + il; 269 270 // each thread in a SIMD group deals with half a block. 271 //for (int ib = ib0; ib < nb; ib += NSG*NQ) { 272 for (int ib = ib0; ib < nb; ib += NQ) { 273 float sumy[2] = { 0.f, 0.f }; 274 275 FOR_UNROLL (short i = 0; i < 8; i += 2) { 276 sumy[0] += yb[i + 0] + yb[i + 1]; 277 yl[i + 0] = yb[i + 0]; 278 yl[i + 1] = yb[i + 1]/256.f; 279 280 sumy[1] += yb[i + 16] + yb[i + 17]; 281 yl[i + 8] = yb[i + 16]/16.f; 282 yl[i + 9] = yb[i + 17]/4096.f; 283 } 284 285 FOR_UNROLL (short row = 0; row < NR0; row++) { 286 sumf[row] += block_q_n_dot_y(ax[row] + ib, sumy[0] + sumy[1], yl, il); 287 } 288 289 yb += QK4_0 * 16; 290 //yb += NSG*NQ*QK4_0; 291 } 292 293 device float * dst_f32 = (device float *) dst + im*args.ne0*args.ne1 + r1*args.ne0; 294 295 //helper_mv_reduce_and_write(dst_f32, sumf, r0, args.ne01, tiisg, sgitg, shmem); 296 297 for (int row = 0; row < NR0; ++row) { 298 const float tot = simd_sum(sumf[row]); 299 300 if (tiisg == 0 && r0 + row < args.ne01) { 301 dst_f32[r0 + row] = tot; 302 } 303 } 304 } 305 306 template 307 void kernel_mul_mv_q1_0_f32_impl( 308 args_t args, 309 device const char * src0, 310 device const char * src1, 311 device char * dst, 312 threadgroup char * shmem, 313 uint3 tgpig, 314 ushort tiisg, 315 ushort sgitg) { 316 const short NSG = FC_mul_mv_nsg; 317 318 const int nb = args.ne00/QK1_0; 319 320 const int r0 = tgpig.x; 321 const int r1 = tgpig.y; 322 const int im = tgpig.z; 323 324 const int first_row = (r0 * NSG + sgitg) * nr0; 325 326 const uint i12 = im%FC_mul_mv_ne12; 327 const uint i13 = im/FC_mul_mv_ne12; 328 329 const uint64_t offset1 = r1*args.nb11 + (i12)*args.nb12 + (i13)*args.nb13; 330 331 device const float * y = (device const float *) (src1 + offset1); 332 333 device const block_q1_0 * ax[nr0]; 334 for (int row = 0; row < nr0; ++row) { 335 const uint64_t offset0 = (first_row + row)*args.nb01 + (i12/FC_mul_mv_r2)*args.nb02 + (i13/FC_mul_mv_r3)*args.nb03; 336 ax[row] = (device const block_q1_0 *) ((device char *) src0 + offset0); 337 } 338 339 float yl[16]; 340 float sumf[nr0] = {0.f}; 341 342 const short ix = (tiisg/8); 343 const short il = (tiisg%8)*16; 344 345 device const float * yb = y + ix*QK1_0 + il; 346 347 for (int ib = ix; ib < nb; ib += N_SIMDWIDTH/8) { 348 float sumy = 0.f; 349 350 FOR_UNROLL (short i = 0; i < 16; i++) { 351 yl[i] = yb[i]; 352 sumy += yb[i]; 353 } 354 355 FOR_UNROLL (short row = 0; row < nr0; row++) { 356 sumf[row] += block_q_n_dot_y(ax[row] + ib, sumy, yl, il); 357 } 358 359 yb += QK1_0 * (N_SIMDWIDTH/8); 360 } 361 362 device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0; 363 364 for (int row = 0; row < nr0; ++row) { 365 const float tot = simd_sum(sumf[row]); 366 367 if (tiisg == 0 && first_row + row < args.ne01) { 368 dst_f32[first_row + row] = tot; 369 } 370 } 371 } 372 373 [[host_name("kernel_mul_mv_q1_0_f32")]] 374 kernel void kernel_mul_mv_q1_0_f32( 375 constant ggml_metal_kargs_mul_mv & args, 376 device const char * src0, 377 device const char * src1, 378 device char * dst, 379 uint3 tgpig[[threadgroup_position_in_grid]], 380 ushort tiisg[[thread_index_in_simdgroup]], 381 ushort sgitg[[simdgroup_index_in_threadgroup]]) { 382 kernel_mul_mv_q1_0_f32_impl(args, src0, src1, dst, nullptr, tgpig, tiisg, sgitg); 383 } 384 385 template 386 void kernel_mul_mv_q2_0_f32_impl( 387 args_t args, 388 device const char * src0, 389 device const char * src1, 390 device char * dst, 391 threadgroup char * shmem, 392 uint3 tgpig, 393 ushort tiisg, 394 ushort sgitg) { 395 const short NSG = FC_mul_mv_nsg; 396 397 const int nb = args.ne00/QK2_0; 398 399 const int r0 = tgpig.x; 400 const int r1 = tgpig.y; 401 const int im = tgpig.z; 402 403 const int first_row = (r0 * NSG + sgitg) * nr0; 404 405 const uint i12 = im%FC_mul_mv_ne12; 406 const uint i13 = im/FC_mul_mv_ne12; 407 408 const uint64_t offset1 = r1*args.nb11 + (i12)*args.nb12 + (i13)*args.nb13; 409 410 device const float * y = (device const float *) (src1 + offset1); 411 412 device const block_q2_0 * ax[nr0]; 413 for (int row = 0; row < nr0; ++row) { 414 const uint64_t offset0 = (first_row + row)*args.nb01 + (i12/FC_mul_mv_r2)*args.nb02 + (i13/FC_mul_mv_r3)*args.nb03; 415 ax[row] = (device const block_q2_0 *) ((device char *) src0 + offset0); 416 } 417 418 float yl[16]; 419 float sumf[nr0] = {0.f}; 420 421 // group 64: 4 sub-blocks of 16 weights per Q2_0 block 422 const short ix = (tiisg/4); 423 const short il = (tiisg%4)*16; 424 425 device const float * yb = y + ix*QK2_0 + il; 426 427 for (int ib = ix; ib < nb; ib += N_SIMDWIDTH/4) { 428 float sumy = 0.f; 429 430 FOR_UNROLL (short i = 0; i < 16; i++) { 431 yl[i] = yb[i]; 432 sumy += yb[i]; 433 } 434 435 FOR_UNROLL (short row = 0; row < nr0; row++) { 436 sumf[row] += block_q_n_dot_y(ax[row] + ib, sumy, yl, il); 437 } 438 439 yb += QK2_0 * (N_SIMDWIDTH/4); 440 } 441 442 device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0; 443 444 for (int row = 0; row < nr0; ++row) { 445 const float tot = simd_sum(sumf[row]); 446 447 if (tiisg == 0 && first_row + row < args.ne01) { 448 dst_f32[first_row + row] = tot; 449 } 450 } 451 } 452 453 [[host_name("kernel_mul_mv_q2_0_f32")]] 454 kernel void kernel_mul_mv_q2_0_f32( 455 constant ggml_metal_kargs_mul_mv & args, 456 device const char * src0, 457 device const char * src1, 458 device char * dst, 459 uint3 tgpig[[threadgroup_position_in_grid]], 460 ushort tiisg[[thread_index_in_simdgroup]], 461 ushort sgitg[[simdgroup_index_in_threadgroup]]) { 462 kernel_mul_mv_q2_0_f32_impl(args, src0, src1, dst, nullptr, tgpig, tiisg, sgitg); 463 } 464 465 kernel void kernel_mul_mv_q4_0_f32( 466 constant ggml_metal_kargs_mul_mv & args, 467 device const char * src0, 468 device const char * src1, 469 device char * dst, 470 threadgroup char * shmem [[threadgroup(0)]], 471 uint3 tgpig[[threadgroup_position_in_grid]], 472 ushort tiisg[[thread_index_in_simdgroup]], 473 ushort sgitg[[simdgroup_index_in_threadgroup]]) { 474 mul_vec_q_n_f32_impl(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg); 475 } 476 477 kernel void kernel_mul_mv_q4_1_f32( 478 constant ggml_metal_kargs_mul_mv & args, 479 device const char * src0, 480 device const char * src1, 481 device char * dst, 482 threadgroup char * shmem [[threadgroup(0)]], 483 uint3 tgpig[[threadgroup_position_in_grid]], 484 ushort tiisg[[thread_index_in_simdgroup]], 485 ushort sgitg[[simdgroup_index_in_threadgroup]]) { 486 mul_vec_q_n_f32_impl(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg); 487 } 488 489 kernel void kernel_mul_mv_q5_0_f32( 490 constant ggml_metal_kargs_mul_mv & args, 491 device const char * src0, 492 device const char * src1, 493 device char * dst, 494 threadgroup char * shmem [[threadgroup(0)]], 495 uint3 tgpig[[threadgroup_position_in_grid]], 496 ushort tiisg[[thread_index_in_simdgroup]], 497 ushort sgitg[[simdgroup_index_in_threadgroup]]) { 498 mul_vec_q_n_f32_impl(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg); 499 } 500 501 kernel void kernel_mul_mv_q5_1_f32( 502 constant ggml_metal_kargs_mul_mv & args, 503 device const char * src0, 504 device const char * src1, 505 device char * dst, 506 threadgroup char * shmem [[threadgroup(0)]], 507 uint3 tgpig[[threadgroup_position_in_grid]], 508 ushort tiisg[[thread_index_in_simdgroup]], 509 ushort sgitg[[simdgroup_index_in_threadgroup]]) { 510 mul_vec_q_n_f32_impl(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg); 511 } 512 513 template 514 void kernel_mul_mv_q8_0_f32_impl( 515 args_t args, 516 device const char * src0, 517 device const char * src1, 518 device char * dst, 519 threadgroup char * shmem, 520 uint3 tgpig, 521 ushort tiisg, 522 ushort sgitg) { 523 const short NSG = FC_mul_mv_nsg; 524 525 constexpr short NW = N_SIMDWIDTH; 526 constexpr short NQ = 8; 527 528 const int nb = args.ne00/QK8_0; 529 530 const int r0 = tgpig.x*NR0; 531 const int r1 = tgpig.y; 532 const int im = tgpig.z; 533 534 const uint i12 = im%FC_mul_mv_ne12; 535 const uint i13 = im/FC_mul_mv_ne12; 536 537 //const uint64_t offset0 = r0*args.nb01 + (i12/FC_mul_mv_r2)*args.nb02 + (i13/FC_mul_mv_r3)*args.nb03; 538 const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13; 539 540 //device const block_q8_0 * x = (device const block_q8_0 *) (src0 + offset0); 541 device const float * y = (device const float *) (src1 + offset1); 542 543 // pointers to src0 rows 544 device const block_q8_0 * ax[NR0]; 545 FOR_UNROLL (short row = 0; row < NR0; ++row) { 546 const uint64_t offset0 = (r0 + row)*args.nb01 + (i12/FC_mul_mv_r2)*args.nb02 + (i13/FC_mul_mv_r3)*args.nb03; 547 548 ax[row] = (device const block_q8_0 *) ((device char *) src0 + offset0); 549 } 550 551 float sumf[NR0] = { 0.f }; 552 553 const short ix = tiisg/(NW/NQ); 554 const short il = tiisg%(NW/NQ); 555 556 const int ib0 = sgitg*NQ + ix; 557 558 float yl[NQ]; 559 560 device const float * yb = y + ib0*QK8_0 + il*NQ; 561 562 // each thread in a SIMD group deals with NQ quants at a time 563 for (int ib = ib0; ib < nb; ib += NSG*NQ) { 564 for (short i = 0; i < NQ; ++i) { 565 yl[i] = yb[i]; 566 } 567 568 for (short row = 0; row < NR0; row++) { 569 device const int8_t * qs = ax[row][ib].qs + il*NQ; 570 571 float sumq = 0.f; 572 FOR_UNROLL (short i = 0; i < NQ; ++i) { 573 sumq += qs[i] * yl[i]; 574 } 575 576 sumf[row] += sumq*ax[row][ib].d; 577 } 578 579 yb += NSG*NQ*QK8_0; 580 } 581 582 device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0; 583 584 helper_mv_reduce_and_write(dst_f32, sumf, r0, args.ne01, tiisg, sgitg, shmem); 585 } 586 587 [[host_name("kernel_mul_mv_q8_0_f32")]] 588 kernel void kernel_mul_mv_q8_0_f32( 589 constant ggml_metal_kargs_mul_mv & args, 590 device const char * src0, 591 device const char * src1, 592 device char * dst, 593 threadgroup char * shmem [[threadgroup(0)]], 594 uint3 tgpig[[threadgroup_position_in_grid]], 595 ushort tiisg[[thread_index_in_simdgroup]], 596 ushort sgitg[[simdgroup_index_in_threadgroup]]) { 597 kernel_mul_mv_q8_0_f32_impl(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg); 598 } 599 600 // mat-vec kernel processing in chunks of float4 601 // chpb - chunks per quantization block 602 template 603 void kernel_mul_mv_ext_q4_f32_impl( 604 constant ggml_metal_kargs_mul_mv_ext & args, 605 device const char * src0, 606 device const char * src1, 607 device char * dst, 608 uint3 tgpig[[threadgroup_position_in_grid]], 609 ushort tiisg[[thread_index_in_simdgroup]], 610 ushort sgitg[[simdgroup_index_in_threadgroup]]) { 611 const short NSG = FC_mul_mv_nsg; 612 const short nxpsg = FC_mul_mv_nxpsg; 613 614 const short chpt = 4; // chunks per thread 615 616 //const short nxpsg = (32); 617 const short nypsg = (32/nxpsg); 618 619 const short tx = tiisg%nxpsg; 620 const short ty = tiisg/nxpsg; 621 622 const int i01 = tgpig.x*(nypsg*NSG) + nypsg*sgitg + ty; 623 const int i11 = tgpig.y*r1ptg; 624 const int i1m = tgpig.z; 625 626 const int i12 = i1m%FC_mul_mv_ne12; 627 const int i13 = i1m/FC_mul_mv_ne12; 628 629 const uint64_t offset0 = i01*args.nb01 + (i12/FC_mul_mv_r2)*args.nb02 + (i13/FC_mul_mv_r3)*args.nb03; 630 const uint64_t offset1 = i11*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13; 631 632 device const q_t * xq = (i01 < args.ne01) ? (device const q_t *) (src0 + offset0) + tx/chpb : (device const q_t *) src0; 633 634 device const float4 * y4[r1ptg]; 635 636 for (int ir1 = 0; ir1 < r1ptg; ++ir1) { 637 y4[ir1] = (i11 + ir1 < args.ne11) ? (device const float4 *) (src1 + offset1 + ir1*args.nb11) + tx : (device const float4 *) src1; 638 } 639 640 float sumf[r1ptg] = { [ 0 ... r1ptg - 1 ] = 0.0f }; 641 642 short cch = tx%chpb; // current chunk index 643 644 for (int ich = tx; 4*ich < args.ne00; ich += chpt*nxpsg) { 645 float4 lx[chpt]; 646 647 #pragma unroll(chpt) 648 for (short ch = 0; ch < chpt; ++ch) { 649 deq_t4(xq, cch, lx[ch]); 650 651 cch += nxpsg; 652 if (cch >= chpb) { 653 xq += cch/chpb; 654 cch %= chpb; 655 } 656 } 657 658 #pragma unroll(chpt) 659 for (short ch = 0; ch < chpt; ++ch) { 660 #pragma unroll(r1ptg) 661 for (short ir1 = 0; ir1 < r1ptg; ++ir1) { 662 sumf[ir1] += dot(lx[ch], y4[ir1][ch*nxpsg]); 663 } 664 } 665 666 #pragma unroll(r1ptg) 667 for (short ir1 = 0; ir1 < r1ptg; ++ir1) { 668 y4[ir1] += chpt*nxpsg; 669 } 670 } 671 672 // reduce only the threads in each row 673 for (short ir1 = 0; ir1 < r1ptg; ++ir1) { 674 if (nxpsg >= 32) { 675 sumf[ir1] += simd_shuffle_down(sumf[ir1], 16); 676 } 677 if (nxpsg >= 16) { 678 sumf[ir1] += simd_shuffle_down(sumf[ir1], 8); 679 } 680 if (nxpsg >= 8) { 681 sumf[ir1] += simd_shuffle_down(sumf[ir1], 4); 682 } 683 if (nxpsg >= 4) { 684 sumf[ir1] += simd_shuffle_down(sumf[ir1], 2); 685 } 686 if (nxpsg >= 2) { 687 sumf[ir1] += simd_shuffle_down(sumf[ir1], 1); 688 } 689 690 //sumf[ir1] = simd_sum(sumf[ir1]); 691 } 692 693 if (tx == 0) { 694 for (short ir1 = 0; ir1 < r1ptg && i11 + ir1 < args.ne11; ++ir1) { 695 device float * dst_f32 = (device float *) dst + (uint64_t)i1m*args.ne0*args.ne1 + (uint64_t)(i11 + ir1)*args.ne0; 696 697 if (i01 < args.ne01) { 698 dst_f32[i01] = sumf[ir1]; 699 } 700 } 701 } 702 } 703 704 // mat-vec kernel processing in chunks of float4x4 705 template 706 void kernel_mul_mv_ext_q4x4_f32_impl( 707 constant ggml_metal_kargs_mul_mv_ext & args, 708 device const char * src0, 709 device const char * src1, 710 device char * dst, 711 uint3 tgpig[[threadgroup_position_in_grid]], 712 ushort tiisg[[thread_index_in_simdgroup]], 713 ushort sgitg[[simdgroup_index_in_threadgroup]]) { 714 const short NSG = FC_mul_mv_nsg; 715 const short nxpsg = FC_mul_mv_nxpsg; 716 717 const short chpt = 1; 718 719 //const short nxpsg = (32); 720 const short nypsg = (32/nxpsg); 721 722 const short tx = tiisg%nxpsg; 723 const short ty = tiisg/nxpsg; 724 725 const int i01 = tgpig.x*(nypsg*NSG) + nypsg*sgitg + ty; 726 const int i11 = tgpig.y*r1ptg; 727 const int i1m = tgpig.z; 728 729 const int i12 = i1m%FC_mul_mv_ne12; 730 const int i13 = i1m/FC_mul_mv_ne12; 731 732 const uint64_t offset0 = i01*args.nb01 + (i12/FC_mul_mv_r2)*args.nb02 + (i13/FC_mul_mv_r3)*args.nb03; 733 const uint64_t offset1 = i11*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13; 734 735 device const q_t * xq = (i01 < args.ne01) ? (device const q_t *) (src0 + offset0) + tx/chpb : (device const q_t *) src0; 736 737 device const float4x4 * y4x4[r1ptg]; 738 739 for (int ir1 = 0; ir1 < r1ptg; ++ir1) { 740 y4x4[ir1] = (i11 + ir1 < args.ne11) ? (device const float4x4 *) (src1 + offset1 + ir1*args.nb11) + tx : (device const float4x4 *) src1; 741 } 742 743 float sumf[r1ptg] = { [ 0 ... r1ptg - 1 ] = 0.0f }; 744 745 short cch = tx%chpb; 746 747 for (int ich = tx; 16*ich < args.ne00; ich += chpt*nxpsg) { 748 float4x4 lx[chpt]; 749 750 #pragma unroll(chpt) 751 for (short ch = 0; ch < chpt; ++ch) { 752 deq_t4x4(xq, cch, lx[ch]); 753 754 cch += nxpsg; 755 if (cch >= chpb) { 756 xq += cch/chpb; 757 cch %= chpb; 758 } 759 } 760 761 #pragma unroll(chpt) 762 for (short ch = 0; ch < chpt; ++ch) { 763 #pragma unroll(r1ptg) 764 for (short ir1 = 0; ir1 < r1ptg; ++ir1) { 765 sumf[ir1] += 766 dot(lx[ch][0], y4x4[ir1][ch*nxpsg][0]) + 767 dot(lx[ch][1], y4x4[ir1][ch*nxpsg][1]) + 768 dot(lx[ch][2], y4x4[ir1][ch*nxpsg][2]) + 769 dot(lx[ch][3], y4x4[ir1][ch*nxpsg][3]); 770 771 } 772 } 773 774 #pragma unroll(r1ptg) 775 for (short ir1 = 0; ir1 < r1ptg; ++ir1) { 776 y4x4[ir1] += chpt*nxpsg; 777 } 778 } 779 780 for (short ir1 = 0; ir1 < r1ptg; ++ir1) { 781 if (nxpsg >= 32) { 782 sumf[ir1] += simd_shuffle_down(sumf[ir1], 16); 783 } 784 if (nxpsg >= 16) { 785 sumf[ir1] += simd_shuffle_down(sumf[ir1], 8); 786 } 787 if (nxpsg >= 8) { 788 sumf[ir1] += simd_shuffle_down(sumf[ir1], 4); 789 } 790 if (nxpsg >= 4) { 791 sumf[ir1] += simd_shuffle_down(sumf[ir1], 2); 792 } 793 if (nxpsg >= 2) { 794 sumf[ir1] += simd_shuffle_down(sumf[ir1], 1); 795 } 796 797 //sumf[ir1] = simd_sum(sumf[ir1]); 798 } 799 800 if (tx == 0) { 801 for (short ir1 = 0; ir1 < r1ptg && i11 + ir1 < args.ne11; ++ir1) { 802 device float * dst_f32 = (device float *) dst + (uint64_t)i1m*args.ne0*args.ne1 + (uint64_t)(i11 + ir1)*args.ne0; 803 804 if (i01 < args.ne01) { 805 dst_f32[i01] = sumf[ir1]; 806 } 807 } 808 } 809 } 810 811 // dispatchers needed for compile-time nxpsg 812 // epb - elements per quantization block 813 template 814 kernel void kernel_mul_mv_ext_q4_f32_disp( 815 constant ggml_metal_kargs_mul_mv_ext & args, 816 device const char * src0, 817 device const char * src1, 818 device char * dst, 819 uint3 tgpig[[threadgroup_position_in_grid]], 820 ushort tiisg[[thread_index_in_simdgroup]], 821 ushort sgitg[[simdgroup_index_in_threadgroup]]) { 822 kernel_mul_mv_ext_q4_f32_impl(args, src0, src1, dst, tgpig, tiisg, sgitg); 823 } 824 825 template 826 kernel void kernel_mul_mv_ext_q4x4_f32_disp( 827 constant ggml_metal_kargs_mul_mv_ext & args, 828 device const char * src0, 829 device const char * src1, 830 device char * dst, 831 uint3 tgpig[[threadgroup_position_in_grid]], 832 ushort tiisg[[thread_index_in_simdgroup]], 833 ushort sgitg[[simdgroup_index_in_threadgroup]]) { 834 kernel_mul_mv_ext_q4x4_f32_impl(args, src0, src1, dst, tgpig, tiisg, sgitg); 835 } 836 837 typedef decltype(kernel_mul_mv_ext_q4_f32_disp <2, block_q8_0, 32, dequantize_q8_0_t4>) mul_mv_ext_q4_f32_t; 838 typedef decltype(kernel_mul_mv_ext_q4x4_f32_disp<2, block_q4_K, 256, dequantize_q4_K>) mul_mv_ext_q4x4_f32_t; 839 840 template [[host_name("kernel_mul_mv_ext_f32_f32_r1_2")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<2, float4, 4, dequantize_f32_t4>; 841 template [[host_name("kernel_mul_mv_ext_f32_f32_r1_3")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<3, float4, 4, dequantize_f32_t4>; 842 template [[host_name("kernel_mul_mv_ext_f32_f32_r1_4")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<4, float4, 4, dequantize_f32_t4>; 843 template [[host_name("kernel_mul_mv_ext_f32_f32_r1_5")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<5, float4, 4, dequantize_f32_t4>; 844 845 template [[host_name("kernel_mul_mv_ext_f16_f32_r1_2")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<2, half4, 4, dequantize_f16_t4>; 846 template [[host_name("kernel_mul_mv_ext_f16_f32_r1_3")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<3, half4, 4, dequantize_f16_t4>; 847 template [[host_name("kernel_mul_mv_ext_f16_f32_r1_4")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<4, half4, 4, dequantize_f16_t4>; 848 template [[host_name("kernel_mul_mv_ext_f16_f32_r1_5")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<5, half4, 4, dequantize_f16_t4>; 849 850 #if defined(GGML_METAL_HAS_BF16) 851 template [[host_name("kernel_mul_mv_ext_bf16_f32_r1_2")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<2, bfloat4, 4, dequantize_bf16_t4>; 852 template [[host_name("kernel_mul_mv_ext_bf16_f32_r1_3")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<3, bfloat4, 4, dequantize_bf16_t4>; 853 template [[host_name("kernel_mul_mv_ext_bf16_f32_r1_4")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<4, bfloat4, 4, dequantize_bf16_t4>; 854 template [[host_name("kernel_mul_mv_ext_bf16_f32_r1_5")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<5, bfloat4, 4, dequantize_bf16_t4>; 855 #endif 856 857 template [[host_name("kernel_mul_mv_ext_q1_0_f32_r1_2")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<2, block_q1_0, 128, dequantize_q1_0_t4>; 858 template [[host_name("kernel_mul_mv_ext_q1_0_f32_r1_3")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<3, block_q1_0, 128, dequantize_q1_0_t4>; 859 template [[host_name("kernel_mul_mv_ext_q1_0_f32_r1_4")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<4, block_q1_0, 128, dequantize_q1_0_t4>; 860 template [[host_name("kernel_mul_mv_ext_q1_0_f32_r1_5")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<5, block_q1_0, 128, dequantize_q1_0_t4>; 861 862 template [[host_name("kernel_mul_mv_ext_q2_0_f32_r1_2")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<2, block_q2_0, 64, dequantize_q2_0_t4>; 863 template [[host_name("kernel_mul_mv_ext_q2_0_f32_r1_3")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<3, block_q2_0, 64, dequantize_q2_0_t4>; 864 template [[host_name("kernel_mul_mv_ext_q2_0_f32_r1_4")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<4, block_q2_0, 64, dequantize_q2_0_t4>; 865 template [[host_name("kernel_mul_mv_ext_q2_0_f32_r1_5")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<5, block_q2_0, 64, dequantize_q2_0_t4>; 866 867 template [[host_name("kernel_mul_mv_ext_q4_0_f32_r1_2")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<2, block_q4_0, 32, dequantize_q4_0_t4>; 868 template [[host_name("kernel_mul_mv_ext_q4_0_f32_r1_3")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<3, block_q4_0, 32, dequantize_q4_0_t4>; 869 template [[host_name("kernel_mul_mv_ext_q4_0_f32_r1_4")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<4, block_q4_0, 32, dequantize_q4_0_t4>; 870 template [[host_name("kernel_mul_mv_ext_q4_0_f32_r1_5")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<5, block_q4_0, 32, dequantize_q4_0_t4>; 871 872 template [[host_name("kernel_mul_mv_ext_q4_1_f32_r1_2")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<2, block_q4_1, 32, dequantize_q4_1_t4>; 873 template [[host_name("kernel_mul_mv_ext_q4_1_f32_r1_3")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<3, block_q4_1, 32, dequantize_q4_1_t4>; 874 template [[host_name("kernel_mul_mv_ext_q4_1_f32_r1_4")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<4, block_q4_1, 32, dequantize_q4_1_t4>; 875 template [[host_name("kernel_mul_mv_ext_q4_1_f32_r1_5")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<5, block_q4_1, 32, dequantize_q4_1_t4>; 876 877 template [[host_name("kernel_mul_mv_ext_q5_0_f32_r1_2")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<2, block_q5_0, 32, dequantize_q5_0_t4>; 878 template [[host_name("kernel_mul_mv_ext_q5_0_f32_r1_3")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<3, block_q5_0, 32, dequantize_q5_0_t4>; 879 template [[host_name("kernel_mul_mv_ext_q5_0_f32_r1_4")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<4, block_q5_0, 32, dequantize_q5_0_t4>; 880 template [[host_name("kernel_mul_mv_ext_q5_0_f32_r1_5")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<5, block_q5_0, 32, dequantize_q5_0_t4>; 881 882 template [[host_name("kernel_mul_mv_ext_q5_1_f32_r1_2")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<2, block_q5_1, 32, dequantize_q5_1_t4>; 883 template [[host_name("kernel_mul_mv_ext_q5_1_f32_r1_3")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<3, block_q5_1, 32, dequantize_q5_1_t4>; 884 template [[host_name("kernel_mul_mv_ext_q5_1_f32_r1_4")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<4, block_q5_1, 32, dequantize_q5_1_t4>; 885 template [[host_name("kernel_mul_mv_ext_q5_1_f32_r1_5")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<5, block_q5_1, 32, dequantize_q5_1_t4>; 886 887 template [[host_name("kernel_mul_mv_ext_q8_0_f32_r1_2")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<2, block_q8_0, 32, dequantize_q8_0_t4>; 888 template [[host_name("kernel_mul_mv_ext_q8_0_f32_r1_3")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<3, block_q8_0, 32, dequantize_q8_0_t4>; 889 template [[host_name("kernel_mul_mv_ext_q8_0_f32_r1_4")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<4, block_q8_0, 32, dequantize_q8_0_t4>; 890 template [[host_name("kernel_mul_mv_ext_q8_0_f32_r1_5")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<5, block_q8_0, 32, dequantize_q8_0_t4>; 891 892 template [[host_name("kernel_mul_mv_ext_mxfp4_f32_r1_2")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<2, block_mxfp4, 32, dequantize_mxfp4_t4>; 893 template [[host_name("kernel_mul_mv_ext_mxfp4_f32_r1_3")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<3, block_mxfp4, 32, dequantize_mxfp4_t4>; 894 template [[host_name("kernel_mul_mv_ext_mxfp4_f32_r1_4")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<4, block_mxfp4, 32, dequantize_mxfp4_t4>; 895 template [[host_name("kernel_mul_mv_ext_mxfp4_f32_r1_5")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<5, block_mxfp4, 32, dequantize_mxfp4_t4>; 896 897 template [[host_name("kernel_mul_mv_ext_iq4_nl_f32_r1_2")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<2, block_iq4_nl, 32, dequantize_iq4_nl_t4>; 898 template [[host_name("kernel_mul_mv_ext_iq4_nl_f32_r1_3")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<3, block_iq4_nl, 32, dequantize_iq4_nl_t4>; 899 template [[host_name("kernel_mul_mv_ext_iq4_nl_f32_r1_4")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<4, block_iq4_nl, 32, dequantize_iq4_nl_t4>; 900 template [[host_name("kernel_mul_mv_ext_iq4_nl_f32_r1_5")]] kernel mul_mv_ext_q4_f32_t kernel_mul_mv_ext_q4_f32_disp<5, block_iq4_nl, 32, dequantize_iq4_nl_t4>; 901 902 template [[host_name("kernel_mul_mv_ext_q4_K_f32_r1_2")]] kernel mul_mv_ext_q4x4_f32_t kernel_mul_mv_ext_q4x4_f32_disp<2, block_q4_K, 256, dequantize_q4_K>; 903 template [[host_name("kernel_mul_mv_ext_q4_K_f32_r1_3")]] kernel mul_mv_ext_q4x4_f32_t kernel_mul_mv_ext_q4x4_f32_disp<3, block_q4_K, 256, dequantize_q4_K>; 904 template [[host_name("kernel_mul_mv_ext_q4_K_f32_r1_4")]] kernel mul_mv_ext_q4x4_f32_t kernel_mul_mv_ext_q4x4_f32_disp<4, block_q4_K, 256, dequantize_q4_K>; 905 template [[host_name("kernel_mul_mv_ext_q4_K_f32_r1_5")]] kernel mul_mv_ext_q4x4_f32_t kernel_mul_mv_ext_q4x4_f32_disp<5, block_q4_K, 256, dequantize_q4_K>; 906 907 template [[host_name("kernel_mul_mv_ext_q5_K_f32_r1_2")]] kernel mul_mv_ext_q4x4_f32_t kernel_mul_mv_ext_q4x4_f32_disp<2, block_q5_K, 256, dequantize_q5_K>; 908 template [[host_name("kernel_mul_mv_ext_q5_K_f32_r1_3")]] kernel mul_mv_ext_q4x4_f32_t kernel_mul_mv_ext_q4x4_f32_disp<3, block_q5_K, 256, dequantize_q5_K>; 909 template [[host_name("kernel_mul_mv_ext_q5_K_f32_r1_4")]] kernel mul_mv_ext_q4x4_f32_t kernel_mul_mv_ext_q4x4_f32_disp<4, block_q5_K, 256, dequantize_q5_K>; 910 template [[host_name("kernel_mul_mv_ext_q5_K_f32_r1_5")]] kernel mul_mv_ext_q4x4_f32_t kernel_mul_mv_ext_q4x4_f32_disp<5, block_q5_K, 256, dequantize_q5_K>; 911 912 template [[host_name("kernel_mul_mv_ext_q6_K_f32_r1_2")]] kernel mul_mv_ext_q4x4_f32_t kernel_mul_mv_ext_q4x4_f32_disp<2, block_q6_K, 256, dequantize_q6_K>; 913 template [[host_name("kernel_mul_mv_ext_q6_K_f32_r1_3")]] kernel mul_mv_ext_q4x4_f32_t kernel_mul_mv_ext_q4x4_f32_disp<3, block_q6_K, 256, dequantize_q6_K>; 914 template [[host_name("kernel_mul_mv_ext_q6_K_f32_r1_4")]] kernel mul_mv_ext_q4x4_f32_t kernel_mul_mv_ext_q4x4_f32_disp<4, block_q6_K, 256, dequantize_q6_K>; 915 template [[host_name("kernel_mul_mv_ext_q6_K_f32_r1_5")]] kernel mul_mv_ext_q4x4_f32_t kernel_mul_mv_ext_q4x4_f32_disp<5, block_q6_K, 256, dequantize_q6_K>; 916 917 template [[host_name("kernel_mul_mv_ext_q2_K_f32_r1_2")]] kernel mul_mv_ext_q4x4_f32_t kernel_mul_mv_ext_q4x4_f32_disp<2, block_q2_K, 256, dequantize_q2_K>; 918 template [[host_name("kernel_mul_mv_ext_q2_K_f32_r1_3")]] kernel mul_mv_ext_q4x4_f32_t kernel_mul_mv_ext_q4x4_f32_disp<3, block_q2_K, 256, dequantize_q2_K>; 919 template [[host_name("kernel_mul_mv_ext_q2_K_f32_r1_4")]] kernel mul_mv_ext_q4x4_f32_t kernel_mul_mv_ext_q4x4_f32_disp<4, block_q2_K, 256, dequantize_q2_K>; 920 template [[host_name("kernel_mul_mv_ext_q2_K_f32_r1_5")]] kernel mul_mv_ext_q4x4_f32_t kernel_mul_mv_ext_q4x4_f32_disp<5, block_q2_K, 256, dequantize_q2_K>; 921 922 template [[host_name("kernel_mul_mv_ext_q3_K_f32_r1_2")]] kernel mul_mv_ext_q4x4_f32_t kernel_mul_mv_ext_q4x4_f32_disp<2, block_q3_K, 256, dequantize_q3_K>; 923 template [[host_name("kernel_mul_mv_ext_q3_K_f32_r1_3")]] kernel mul_mv_ext_q4x4_f32_t kernel_mul_mv_ext_q4x4_f32_disp<3, block_q3_K, 256, dequantize_q3_K>; 924 template [[host_name("kernel_mul_mv_ext_q3_K_f32_r1_4")]] kernel mul_mv_ext_q4x4_f32_t kernel_mul_mv_ext_q4x4_f32_disp<4, block_q3_K, 256, dequantize_q3_K>; 925 template [[host_name("kernel_mul_mv_ext_q3_K_f32_r1_5")]] kernel mul_mv_ext_q4x4_f32_t kernel_mul_mv_ext_q4x4_f32_disp<5, block_q3_K, 256, dequantize_q3_K>; 926 927 template 928 void kernel_mul_mv_t_t_impl( 929 args_t args, 930 device const char * src0, 931 device const char * src1, 932 device char * dst, 933 threadgroup char * shmem, 934 uint3 tgpig, 935 ushort tiisg, 936 ushort sgitg) { 937 const short NSG = FC_mul_mv_nsg; 938 939 constexpr short NW = N_SIMDWIDTH; 940 constexpr short NB = 32; 941 constexpr short NF = 8; 942 943 const int nb = args.ne00/NB; 944 945 const int r0 = tgpig.x*NR0; 946 const int r1 = tgpig.y; 947 const int im = tgpig.z; 948 949 const uint i12 = im%FC_mul_mv_ne12; 950 const uint i13 = im/FC_mul_mv_ne12; 951 952 //const uint64_t offset0 = r0*args.nb01 + (i12/FC_mul_mv_r2)*args.nb02 + (i13/FC_mul_mv_r3)*args.nb03; 953 const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13; 954 955 //device const T0 * x = (device const T0 *) (src0 + offset0); 956 device const T1 * y = (device const T1 *) (src1 + offset1); 957 958 // pointers to src0 rows 959 device const T0 * ax [NR0]; 960 FOR_UNROLL (short row = 0; row < NR0; ++row) { 961 const uint64_t offset0 = (r0 + row)*args.nb01 + (i12/FC_mul_mv_r2)*args.nb02 + (i13/FC_mul_mv_r3)*args.nb03; 962 963 ax[row] = (device const T0 *) ((device char *) src0 + offset0); 964 } 965 966 float sumf[NR0] = { 0.f }; 967 968 const short ix = tiisg/(NW/NF); 969 const short il = tiisg%(NW/NF); 970 971 const int ib0 = sgitg*NF + ix; 972 973 T1 yl[NF]; 974 975 device const T1 * yb = y + (ib0*NB + il*NF); 976 977 for (int ib = ib0; ib < nb; ib += NSG*NF) { 978 for (short i = 0; i < NF; ++i) { 979 yl[i] = yb[i]; 980 } 981 982 for (short row = 0; row < NR0; row++) { 983 device const T0 * xb = ax[row] + (ib*NB + il*NF); 984 985 float sumq = 0.f; 986 FOR_UNROLL (short i = 0; i < NF; ++i) { 987 sumq += xb[i] * yl[i]; 988 } 989 990 sumf[row] += sumq; 991 } 992 993 yb += NSG*NF*NW; 994 } 995 996 for (int i = nb*NB + sgitg*NW + tiisg; i < args.ne00; i += NW*NSG) { 997 for (short row = 0; row < NR0; row++) { 998 sumf[row] += ax[row][i] * y[i]; 999 } 1000 } 1001 1002 device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0; 1003 1004 helper_mv_reduce_and_write(dst_f32, sumf, r0, args.ne01, tiisg, sgitg, shmem); 1005 } 1006 1007 template 1008 void kernel_mul_mv_t_t_disp( 1009 args_t args, 1010 device const char * src0, 1011 device const char * src1, 1012 device char * dst, 1013 threadgroup char * shmem, 1014 uint3 tgpig, 1015 ushort tiisg, 1016 ushort sgitg) { 1017 switch (args.nr0) { 1018 //case 1: kernel_mul_mv_t_t_impl(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg); break; 1019 case 2: kernel_mul_mv_t_t_impl(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg); break; 1020 //case 3: kernel_mul_mv_t_t_impl(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg); break; 1021 //case 4: kernel_mul_mv_t_t_impl(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg); break; 1022 } 1023 } 1024 1025 template 1026 kernel void kernel_mul_mv_t_t( 1027 constant ggml_metal_kargs_mul_mv & args, 1028 device const char * src0, 1029 device const char * src1, 1030 device char * dst, 1031 threadgroup char * shmem [[threadgroup(0)]], 1032 uint3 tgpig[[threadgroup_position_in_grid]], 1033 ushort tiisg[[thread_index_in_simdgroup]], 1034 ushort sgitg[[simdgroup_index_in_threadgroup]]) { 1035 kernel_mul_mv_t_t_disp(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg); 1036 } 1037 1038 typedef decltype(kernel_mul_mv_t_t) mul_mv_t_t; 1039 1040 template [[host_name("kernel_mul_mv_f32_f32")]] kernel mul_mv_t_t kernel_mul_mv_t_t; 1041 template [[host_name("kernel_mul_mv_f16_f32")]] kernel mul_mv_t_t kernel_mul_mv_t_t; 1042 template [[host_name("kernel_mul_mv_f16_f16")]] kernel mul_mv_t_t kernel_mul_mv_t_t; 1043 #if defined(GGML_METAL_HAS_BF16) 1044 template [[host_name("kernel_mul_mv_bf16_f32")]] kernel mul_mv_t_t kernel_mul_mv_t_t; 1045 template [[host_name("kernel_mul_mv_bf16_bf16")]] kernel mul_mv_t_t kernel_mul_mv_t_t; 1046 template [[host_name("kernel_mul_mv_f32_bf16")]] kernel mul_mv_t_t kernel_mul_mv_t_t; 1047 #endif 1048 1049 template 1050 void kernel_mul_mv_t_t_4_impl( 1051 args_t args, 1052 device const char * src0, 1053 device const char * src1, 1054 device char * dst, 1055 threadgroup char * shmem, 1056 uint3 tgpig, 1057 ushort tiisg, 1058 ushort sgitg) { 1059 const short NSG = FC_mul_mv_nsg; 1060 1061 constexpr short NW = N_SIMDWIDTH; 1062 constexpr short NB = 32; 1063 constexpr short NF = 16; 1064 constexpr short NF4 = NF/4; 1065 1066 const int nb = args.ne00/NB; 1067 1068 const int r0 = tgpig.x*NR0; 1069 const int r1 = tgpig.y; 1070 const int im = tgpig.z; 1071 1072 const uint i12 = im%FC_mul_mv_ne12; 1073 const uint i13 = im/FC_mul_mv_ne12; 1074 1075 //const uint64_t offset0 = r0*args.nb01 + (i12/FC_mul_mv_r2)*args.nb02 + (i13/FC_mul_mv_r3)*args.nb03; 1076 const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13; 1077 1078 device const T1 * y = (device const T1 *) (src1 + offset1); 1079 device const T14 * y4 = (device const T14 *) (src1 + offset1); 1080 1081 // pointers to src0 rows 1082 device const T0 * ax [NR0]; 1083 device const T04 * ax4[NR0]; 1084 FOR_UNROLL (short row = 0; row < NR0; ++row) { 1085 const uint64_t offset0 = (r0 + row)*args.nb01 + (i12/FC_mul_mv_r2)*args.nb02 + (i13/FC_mul_mv_r3)*args.nb03; 1086 1087 ax [row] = (device const T0 *) ((device char *) src0 + offset0); 1088 ax4[row] = (device const T04 *) ((device char *) src0 + offset0); 1089 } 1090 1091 float sumf[NR0] = { 0.f }; 1092 1093 const short ix = tiisg/(NW/NF); 1094 const short il = tiisg%(NW/NF); 1095 1096 const int ib0 = sgitg*NF + ix; 1097 1098 T14 yl4[NF4]; 1099 1100 device const T14 * yb4 = y4 + (ib0*NB + il*NF)/4; 1101 1102 for (int ib = ib0; ib < nb; ib += NSG*NF) { 1103 for (short i = 0; i < NF4; ++i) { 1104 yl4[i] = yb4[i]; 1105 } 1106 1107 for (short row = 0; row < NR0; row++) { 1108 device const T04 * xb4 = ax4[row] + (ib*NB + il*NF)/4; 1109 1110 float sumq = 0.f; 1111 FOR_UNROLL (short i = 0; i < NF4; ++i) { 1112 sumq += dot(float4(xb4[i]), float4(yl4[i])); 1113 } 1114 1115 sumf[row] += sumq; 1116 } 1117 1118 yb4 += NSG*NF*NW/4; 1119 } 1120 1121 for (int i = nb*NB + sgitg*NW + tiisg; i < args.ne00; i += NW*NSG) { 1122 for (short row = 0; row < NR0; row++) { 1123 sumf[row] += ax[row][i] * y[i]; 1124 } 1125 } 1126 1127 device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0; 1128 1129 helper_mv_reduce_and_write(dst_f32, sumf, r0, args.ne01, tiisg, sgitg, shmem); 1130 } 1131 1132 template 1133 void kernel_mul_mv_t_t_4_disp( 1134 args_t args, 1135 device const char * src0, 1136 device const char * src1, 1137 device char * dst, 1138 threadgroup char * shmem, 1139 uint3 tgpig, 1140 ushort tiisg, 1141 ushort sgitg) { 1142 switch (args.nr0) { 1143 //case 1: kernel_mul_mv_t_t_4_impl(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg); break; 1144 case 2: kernel_mul_mv_t_t_4_impl(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg); break; 1145 //case 3: kernel_mul_mv_t_t_4_impl(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg); break; 1146 //case 4: kernel_mul_mv_t_t_4_impl(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg); break; 1147 }; 1148 } 1149 1150 template 1151 kernel void kernel_mul_mv_t_t_4( 1152 constant ggml_metal_kargs_mul_mv & args, 1153 device const char * src0, 1154 device const char * src1, 1155 device char * dst, 1156 threadgroup char * shmem [[threadgroup(0)]], 1157 uint3 tgpig[[threadgroup_position_in_grid]], 1158 ushort tiisg[[thread_index_in_simdgroup]], 1159 ushort sgitg[[simdgroup_index_in_threadgroup]]) { 1160 kernel_mul_mv_t_t_4_disp(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg); 1161 } 1162 1163 typedef decltype(kernel_mul_mv_t_t_4) mul_mv_t_t_4; 1164 1165 template [[host_name("kernel_mul_mv_f32_f32_4")]] kernel mul_mv_t_t_4 kernel_mul_mv_t_t_4; 1166 template [[host_name("kernel_mul_mv_f16_f32_4")]] kernel mul_mv_t_t_4 kernel_mul_mv_t_t_4; 1167 template [[host_name("kernel_mul_mv_f16_f16_4")]] kernel mul_mv_t_t_4 kernel_mul_mv_t_t_4; 1168 #if defined(GGML_METAL_HAS_BF16) 1169 template [[host_name("kernel_mul_mv_bf16_f32_4")]] kernel mul_mv_t_t_4 kernel_mul_mv_t_t_4; 1170 template [[host_name("kernel_mul_mv_bf16_bf16_4")]] kernel mul_mv_t_t_4 kernel_mul_mv_t_t_4; 1171 template [[host_name("kernel_mul_mv_f32_bf16_4")]] kernel mul_mv_t_t_4 kernel_mul_mv_t_t_4; 1172 #endif 1173 1174 template 1175 void kernel_mul_mv_t_t_short_impl( 1176 args_t args, 1177 device const char * src0, 1178 device const char * src1, 1179 device char * dst, 1180 uint3 tgpig, 1181 ushort tiisg) { 1182 const int r0 = tgpig.x*32 + tiisg; 1183 const int r1 = tgpig.y; 1184 const int im = tgpig.z; 1185 1186 if (r0 >= args.ne01) { 1187 return; 1188 } 1189 1190 const uint i12 = im%FC_mul_mv_ne12; 1191 const uint i13 = im/FC_mul_mv_ne12; 1192 1193 const uint64_t offset0 = r0*args.nb01 + (i12/FC_mul_mv_r2)*args.nb02 + (i13/FC_mul_mv_r3)*args.nb03; 1194 1195 device const T0 * x = (device const T0 *) (src0 + offset0); 1196 1197 device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1; 1198 1199 const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13; 1200 1201 device const T1 * y = (device const T1 *) (src1 + offset1); 1202 1203 float res = 0.0f; 1204 1205 for (int i = 0; i < args.ne00; ++i) { 1206 res += (float) x[i] * (float) y[i]; 1207 } 1208 1209 dst_f32[(uint64_t)r1*args.ne0 + r0] = res; 1210 } 1211 1212 template 1213 kernel void kernel_mul_mv_t_t_short( 1214 constant ggml_metal_kargs_mul_mv & args, 1215 device const char * src0, 1216 device const char * src1, 1217 device char * dst, 1218 uint3 tgpig[[threadgroup_position_in_grid]], 1219 ushort tiisg[[thread_index_in_simdgroup]]) { 1220 kernel_mul_mv_t_t_short_impl( 1221 args, 1222 src0, 1223 src1, 1224 dst, 1225 tgpig, 1226 tiisg); 1227 } 1228 1229 typedef decltype(kernel_mul_mv_t_t_short) mul_mv_t_t_short_t; 1230 1231 template [[host_name("kernel_mul_mv_f32_f32_short")]] kernel mul_mv_t_t_short_t kernel_mul_mv_t_t_short; 1232 template [[host_name("kernel_mul_mv_f16_f32_short")]] kernel mul_mv_t_t_short_t kernel_mul_mv_t_t_short; 1233 template [[host_name("kernel_mul_mv_f16_f16_short")]] kernel mul_mv_t_t_short_t kernel_mul_mv_t_t_short; 1234 #if defined(GGML_METAL_HAS_BF16) 1235 template [[host_name("kernel_mul_mv_bf16_f32_short")]] kernel mul_mv_t_t_short_t kernel_mul_mv_t_t_short; 1236 template [[host_name("kernel_mul_mv_bf16_bf16_short")]] kernel mul_mv_t_t_short_t kernel_mul_mv_t_t_short; 1237 template [[host_name("kernel_mul_mv_f32_bf16_short")]] kernel mul_mv_t_t_short_t kernel_mul_mv_t_t_short; 1238 #endif 1239 1240 template 1241 void kernel_mul_mv_q2_K_f32_impl( 1242 args_t args, 1243 device const char * src0, 1244 device const char * src1, 1245 device char * dst, 1246 threadgroup char * shmem, 1247 uint3 tgpig, 1248 ushort tiisg, 1249 ushort sgitg) { 1250 const short NSG = FC_mul_mv_nsg; 1251 1252 const int nb = args.ne00/QK_K; 1253 1254 const int r0 = tgpig.x; 1255 const int r1 = tgpig.y; 1256 const int im = tgpig.z; 1257 1258 const int first_row = (r0 * NSG + sgitg) * nr0; 1259 1260 const uint i12 = im%FC_mul_mv_ne12; 1261 const uint i13 = im/FC_mul_mv_ne12; 1262 1263 const uint64_t offset0 = first_row*args.nb01 + (i12/FC_mul_mv_r2)*args.nb02 + (i13/FC_mul_mv_r3)*args.nb03; 1264 const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13; 1265 1266 device const block_q2_K * x = (device const block_q2_K *) (src0 + offset0); 1267 device const float * y = (device const float *) (src1 + offset1); 1268 1269 float yl[32]; 1270 float sumf[nr0]={0.f}; 1271 1272 const short ix = tiisg/8; // 0...3 1273 const short it = tiisg%8; // 0...7 1274 const short iq = it/4; // 0 or 1 1275 const short ir = it%4; // 0...3 1276 const short is = (8*ir)/16;// 0 or 1 1277 1278 device const float * y4 = y + ix * QK_K + 128 * iq + 8 * ir; 1279 1280 for (int ib = ix; ib < nb; ib += 4) { 1281 float4 sumy = {0.f, 0.f, 0.f, 0.f}; 1282 for (short i = 0; i < 8; ++i) { 1283 yl[i+ 0] = y4[i+ 0]; sumy[0] += yl[i+ 0]; 1284 yl[i+ 8] = y4[i+32]; sumy[1] += yl[i+ 8]; 1285 yl[i+16] = y4[i+64]; sumy[2] += yl[i+16]; 1286 yl[i+24] = y4[i+96]; sumy[3] += yl[i+24]; 1287 } 1288 1289 device const uint8_t * sc = (device const uint8_t *)x[ib].scales + 8*iq + is; 1290 device const uint16_t * qs = (device const uint16_t *)x[ib].qs + 16 * iq + 4 * ir; 1291 device const half * dh = &x[ib].d; 1292 1293 for (short row = 0; row < nr0; row++) { 1294 float4 acc1 = {0.f, 0.f, 0.f, 0.f}; 1295 float4 acc2 = {0.f, 0.f, 0.f, 0.f}; 1296 for (int i = 0; i < 8; i += 2) { 1297 acc1[0] += yl[i+ 0] * (qs[i/2] & 0x0003); 1298 acc2[0] += yl[i+ 1] * (qs[i/2] & 0x0300); 1299 acc1[1] += yl[i+ 8] * (qs[i/2] & 0x000c); 1300 acc2[1] += yl[i+ 9] * (qs[i/2] & 0x0c00); 1301 acc1[2] += yl[i+16] * (qs[i/2] & 0x0030); 1302 acc2[2] += yl[i+17] * (qs[i/2] & 0x3000); 1303 acc1[3] += yl[i+24] * (qs[i/2] & 0x00c0); 1304 acc2[3] += yl[i+25] * (qs[i/2] & 0xc000); 1305 } 1306 float dall = dh[0]; 1307 float dmin = dh[1] * 1.f/16.f; 1308 sumf[row] += dall * ((acc1[0] + 1.f/256.f * acc2[0]) * (sc[0] & 0xF) * 1.f/ 1.f + 1309 (acc1[1] + 1.f/256.f * acc2[1]) * (sc[2] & 0xF) * 1.f/ 4.f + 1310 (acc1[2] + 1.f/256.f * acc2[2]) * (sc[4] & 0xF) * 1.f/16.f + 1311 (acc1[3] + 1.f/256.f * acc2[3]) * (sc[6] & 0xF) * 1.f/64.f) - 1312 dmin * (sumy[0] * (sc[0] & 0xF0) + sumy[1] * (sc[2] & 0xF0) + sumy[2] * (sc[4] & 0xF0) + sumy[3] * (sc[6] & 0xF0)); 1313 1314 qs += args.nb01/2; 1315 sc += args.nb01; 1316 dh += args.nb01/2; 1317 } 1318 1319 y4 += 4 * QK_K; 1320 } 1321 1322 device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0; 1323 1324 for (int row = 0; row < nr0 && first_row + row < args.ne0; ++row) { 1325 float sum_all = simd_sum(sumf[row]); 1326 if (tiisg == 0) { 1327 dst_f32[first_row + row] = sum_all; 1328 } 1329 } 1330 } 1331 1332 [[host_name("kernel_mul_mv_q2_K_f32")]] 1333 kernel void kernel_mul_mv_q2_K_f32( 1334 constant ggml_metal_kargs_mul_mv & args, 1335 device const char * src0, 1336 device const char * src1, 1337 device char * dst, 1338 uint3 tgpig[[threadgroup_position_in_grid]], 1339 ushort tiisg[[thread_index_in_simdgroup]], 1340 ushort sgitg[[simdgroup_index_in_threadgroup]]) { 1341 1342 kernel_mul_mv_q2_K_f32_impl(args, src0, src1, dst, nullptr, tgpig, tiisg, sgitg); 1343 } 1344 1345 template 1346 void kernel_mul_mv_q3_K_f32_impl( 1347 args_t args, 1348 device const char * src0, 1349 device const char * src1, 1350 device char * dst, 1351 threadgroup char * shmem, 1352 uint3 tgpig, 1353 ushort tiisg, 1354 ushort sgitg) { 1355 const short NSG = FC_mul_mv_nsg; 1356 1357 const int nb = args.ne00/QK_K; 1358 1359 const int r0 = tgpig.x; 1360 const int r1 = tgpig.y; 1361 const int im = tgpig.z; 1362 1363 const int first_row = (r0 * NSG + sgitg) * nr0; 1364 1365 const uint i12 = im%FC_mul_mv_ne12; 1366 const uint i13 = im/FC_mul_mv_ne12; 1367 1368 const uint64_t offset0 = first_row*args.nb01 + (i12/FC_mul_mv_r2)*args.nb02 + (i13/FC_mul_mv_r3)*args.nb03; 1369 const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13; 1370 1371 device const block_q3_K * x = (device const block_q3_K *) (src0 + offset0); 1372 device const float * yy = (device const float *) (src1 + offset1); 1373 1374 float yl[32]; 1375 1376 //const uint16_t kmask1 = 0x3030; 1377 //const uint16_t kmask2 = 0x0f0f; 1378 1379 const short tid = tiisg/4; 1380 const short ix = tiisg%4; 1381 const short ip = tid/4; // 0 or 1 1382 const short il = 2*((tid%4)/2); // 0 or 2 1383 const short ir = tid%2; 1384 const short l0 = 8*ir; 1385 1386 // One would think that the Metal compiler would figure out that ip and il can only have 1387 // 4 possible states, and optimize accordingly. Well, no. It needs help, and we do it 1388 // with these two tales. 1389 // 1390 // Possible masks for the high bit 1391 const ushort4 mm[4] = {{0x0001, 0x0100, 0x0002, 0x0200}, // ip = 0, il = 0 1392 {0x0004, 0x0400, 0x0008, 0x0800}, // ip = 0, il = 2 1393 {0x0010, 0x1000, 0x0020, 0x2000}, // ip = 1, il = 0 1394 {0x0040, 0x4000, 0x0080, 0x8000}}; // ip = 1, il = 2 1395 1396 // Possible masks for the low 2 bits 1397 const int4 qm[2] = {{0x0003, 0x0300, 0x000c, 0x0c00}, {0x0030, 0x3000, 0x00c0, 0xc000}}; 1398 1399 const ushort4 hm = mm[2*ip + il/2]; 1400 1401 const short shift = 2*il; 1402 1403 const float v1 = il == 0 ? 4.f : 64.f; 1404 const float v2 = 4.f * v1; 1405 1406 const uint16_t s_shift1 = 4*ip; 1407 const uint16_t s_shift2 = s_shift1 + il; 1408 1409 const short q_offset = 32*ip + l0; 1410 const short y_offset = 128*ip + 32*il + l0; 1411 1412 device const float * y1 = yy + ix*QK_K + y_offset; 1413 1414 uint32_t scales32, aux32; 1415 thread uint16_t * scales16 = (thread uint16_t *)&scales32; 1416 thread const int8_t * scales = (thread const int8_t *)&scales32; 1417 1418 float sumf1[nr0] = {0.f}; 1419 float sumf2[nr0] = {0.f}; 1420 1421 for (int i = ix; i < nb; i += 4) { 1422 for (short l = 0; l < 8; ++l) { 1423 yl[l+ 0] = y1[l+ 0]; 1424 yl[l+ 8] = y1[l+16]; 1425 yl[l+16] = y1[l+32]; 1426 yl[l+24] = y1[l+48]; 1427 } 1428 1429 device const uint16_t * q = (device const uint16_t *)(x[i].qs + q_offset); 1430 device const uint16_t * h = (device const uint16_t *)(x[i].hmask + l0); 1431 device const uint16_t * a = (device const uint16_t *)(x[i].scales); 1432 device const half * dh = &x[i].d; 1433 1434 for (short row = 0; row < nr0; ++row) { 1435 const float d_all = (float)dh[0]; 1436 1437 scales16[0] = a[4]; 1438 scales16[1] = a[5]; 1439 aux32 = ((scales32 >> s_shift2) << 4) & 0x30303030; 1440 scales16[0] = a[il+0]; 1441 scales16[1] = a[il+1]; 1442 scales32 = ((scales32 >> s_shift1) & 0x0f0f0f0f) | aux32; 1443 1444 float s1 = 0, s2 = 0, s3 = 0, s4 = 0, s5 = 0, s6 = 0; 1445 for (short l = 0; l < 8; l += 2) { 1446 const int32_t qs = q[l/2]; 1447 s1 += yl[l+0] * (qs & qm[il/2][0]); 1448 s2 += yl[l+1] * (qs & qm[il/2][1]); 1449 s3 += ((h[l/2] & hm[0]) ? 0.f : yl[l+0]) + ((h[l/2] & hm[1]) ? 0.f : yl[l+1]); 1450 s4 += yl[l+16] * (qs & qm[il/2][2]); 1451 s5 += yl[l+17] * (qs & qm[il/2][3]); 1452 s6 += ((h[l/2] & hm[2]) ? 0.f : yl[l+16]) + ((h[l/2] & hm[3]) ? 0.f : yl[l+17]); 1453 } 1454 float d1 = d_all * (s1 + 1.f/256.f * s2 - s3*v1); 1455 float d2 = d_all * (s4 + 1.f/256.f * s5 - s6*v2); 1456 sumf1[row] += d1 * (scales[0] - 32); 1457 sumf2[row] += d2 * (scales[2] - 32); 1458 1459 s1 = s2 = s3 = s4 = s5 = s6 = 0; 1460 for (short l = 0; l < 8; l += 2) { 1461 const int32_t qs = q[l/2+8]; 1462 s1 += yl[l+8] * (qs & qm[il/2][0]); 1463 s2 += yl[l+9] * (qs & qm[il/2][1]); 1464 s3 += ((h[l/2+8] & hm[0]) ? 0.f : yl[l+8]) + ((h[l/2+8] & hm[1]) ? 0.f : yl[l+9]); 1465 s4 += yl[l+24] * (qs & qm[il/2][2]); 1466 s5 += yl[l+25] * (qs & qm[il/2][3]); 1467 s6 += ((h[l/2+8] & hm[2]) ? 0.f : yl[l+24]) + ((h[l/2+8] & hm[3]) ? 0.f : yl[l+25]); 1468 } 1469 d1 = d_all * (s1 + 1.f/256.f * s2 - s3*v1); 1470 d2 = d_all * (s4 + 1.f/256.f * s5 - s6*v2); 1471 sumf1[row] += d1 * (scales[1] - 32); 1472 sumf2[row] += d2 * (scales[3] - 32); 1473 1474 q += args.nb01/2; 1475 h += args.nb01/2; 1476 a += args.nb01/2; 1477 dh += args.nb01/2; 1478 } 1479 1480 y1 += 4 * QK_K; 1481 } 1482 1483 for (int row = 0; row < nr0; ++row) { 1484 const float sumf = (sumf1[row] + 0.25f * sumf2[row]) / (1 << shift); 1485 sumf1[row] = simd_sum(sumf); 1486 } 1487 1488 device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0; 1489 1490 if (tiisg == 0) { 1491 for (int row = 0; row < nr0 && first_row + row < args.ne0; ++row) { 1492 dst_f32[first_row + row] = sumf1[row]; 1493 } 1494 } 1495 } 1496 1497 [[host_name("kernel_mul_mv_q3_K_f32")]] 1498 kernel void kernel_mul_mv_q3_K_f32( 1499 constant ggml_metal_kargs_mul_mv & args, 1500 device const char * src0, 1501 device const char * src1, 1502 device char * dst, 1503 uint3 tgpig[[threadgroup_position_in_grid]], 1504 ushort tiisg[[thread_index_in_simdgroup]], 1505 ushort sgitg[[simdgroup_index_in_threadgroup]]) { 1506 1507 kernel_mul_mv_q3_K_f32_impl(args, src0, src1, dst, nullptr, tgpig, tiisg, sgitg); 1508 } 1509 1510 template 1511 void kernel_mul_mv_q4_K_f32_impl( 1512 args_t args, 1513 device const char * src0, 1514 device const char * src1, 1515 device char * dst, 1516 threadgroup char * shmem, 1517 uint3 tgpig, 1518 ushort tiisg, 1519 ushort sgitg) { 1520 const short NSG = FC_mul_mv_nsg; 1521 1522 constexpr uint16_t kmask1 = 0x3f3f; 1523 constexpr uint16_t kmask2 = 0x0f0f; 1524 constexpr uint16_t kmask3 = 0xc0c0; 1525 1526 const short ix = tiisg/8; // 0...3 1527 const short it = tiisg%8; // 0...7 1528 const short iq = it/4; // 0 or 1 1529 const short ir = it%4; // 0...3 1530 1531 const int nb = args.ne00/QK_K; 1532 1533 const int r0 = tgpig.x; 1534 const int r1 = tgpig.y; 1535 const int im = tgpig.z; 1536 1537 const int first_row = (r0 * NSG + sgitg) * nr0; 1538 1539 const uint i12 = im%FC_mul_mv_ne12; 1540 const uint i13 = im/FC_mul_mv_ne12; 1541 1542 const uint64_t offset0 = first_row*args.nb01 + (i12/FC_mul_mv_r2)*args.nb02 + (i13/FC_mul_mv_r3)*args.nb03; 1543 const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13; 1544 1545 device const block_q4_K * x = (device const block_q4_K *) (src0 + offset0); 1546 device const float * y = (device const float *) (src1 + offset1); 1547 1548 float yl[16]; 1549 float yh[16]; 1550 1551 float sumf[nr0]={0.f}; 1552 1553 device const float * y4 = y + ix * QK_K + 64 * iq + 8 * ir; 1554 1555 uint16_t sc16[4]; 1556 thread const uint8_t * sc8 = (thread const uint8_t *)sc16; 1557 1558 for (int ib = ix; ib < nb; ib += 4) { 1559 float4 sumy = {0.f, 0.f, 0.f, 0.f}; 1560 1561 for (short i = 0; i < 8; ++i) { 1562 yl[i+0] = y4[i+ 0]; sumy[0] += yl[i+0]; 1563 yl[i+8] = y4[i+ 32]; sumy[1] += yl[i+8]; 1564 yh[i+0] = y4[i+128]; sumy[2] += yh[i+0]; 1565 yh[i+8] = y4[i+160]; sumy[3] += yh[i+8]; 1566 } 1567 1568 device const uint16_t * sc = (device const uint16_t *)x[ib].scales + iq; 1569 device const uint16_t * q1 = (device const uint16_t *)x[ib].qs + 16 * iq + 4 * ir; 1570 device const half * dh = &x[ib].d; 1571 1572 for (short row = 0; row < nr0; row++) { 1573 sc16[0] = sc[0] & kmask1; 1574 sc16[1] = sc[2] & kmask1; 1575 sc16[2] = ((sc[4] >> 0) & kmask2) | ((sc[0] & kmask3) >> 2); 1576 sc16[3] = ((sc[4] >> 4) & kmask2) | ((sc[2] & kmask3) >> 2); 1577 1578 device const uint16_t * q2 = q1 + 32; 1579 1580 float4 acc1 = {0.f, 0.f, 0.f, 0.f}; 1581 float4 acc2 = {0.f, 0.f, 0.f, 0.f}; 1582 1583 FOR_UNROLL (short i = 0; i < 4; ++i) { 1584 acc1[0] += yl[2*i + 0] * (q1[i] & 0x000F); 1585 acc1[1] += yl[2*i + 1] * (q1[i] & 0x0F00); 1586 acc1[2] += yl[2*i + 8] * (q1[i] & 0x00F0); 1587 acc1[3] += yl[2*i + 9] * (q1[i] & 0xF000); 1588 acc2[0] += yh[2*i + 0] * (q2[i] & 0x000F); 1589 acc2[1] += yh[2*i + 1] * (q2[i] & 0x0F00); 1590 acc2[2] += yh[2*i + 8] * (q2[i] & 0x00F0); 1591 acc2[3] += yh[2*i + 9] * (q2[i] & 0xF000); 1592 } 1593 1594 sumf[row] += dh[0] * ((acc1[0] + 1.f/256.f * acc1[1]) * sc8[0] + 1595 (acc1[2] + 1.f/256.f * acc1[3]) * sc8[1] * 1.f/16.f + 1596 (acc2[0] + 1.f/256.f * acc2[1]) * sc8[4] + 1597 (acc2[2] + 1.f/256.f * acc2[3]) * sc8[5] * 1.f/16.f) - 1598 dh[1] * (sumy[0] * sc8[2] + sumy[1] * sc8[3] + sumy[2] * sc8[6] + sumy[3] * sc8[7]); 1599 1600 q1 += args.nb01/2; 1601 sc += args.nb01/2; 1602 dh += args.nb01/2; 1603 } 1604 1605 y4 += 4 * QK_K; 1606 } 1607 1608 device float * dst_f32 = (device float *) dst + (int64_t)im*args.ne0*args.ne1 + (int64_t)r1*args.ne0; 1609 1610 for (int row = 0; row < nr0 && first_row + row < args.ne0; ++row) { 1611 float sum_all = simd_sum(sumf[row]); 1612 if (tiisg == 0) { 1613 dst_f32[first_row + row] = sum_all; 1614 } 1615 } 1616 } 1617 1618 [[host_name("kernel_mul_mv_q4_K_f32")]] 1619 kernel void kernel_mul_mv_q4_K_f32( 1620 constant ggml_metal_kargs_mul_mv & args, 1621 device const char * src0, 1622 device const char * src1, 1623 device char * dst, 1624 uint3 tgpig[[threadgroup_position_in_grid]], 1625 ushort tiisg[[thread_index_in_simdgroup]], 1626 ushort sgitg[[simdgroup_index_in_threadgroup]]) { 1627 1628 kernel_mul_mv_q4_K_f32_impl(args, src0, src1, dst, nullptr, tgpig, tiisg, sgitg); 1629 } 1630 1631 template 1632 void kernel_mul_mv_q5_K_f32_impl( 1633 args_t args, 1634 device const char * src0, 1635 device const char * src1, 1636 device char * dst, 1637 threadgroup char * shmem, 1638 uint3 tgpig, 1639 ushort tiisg, 1640 ushort sgitg) { 1641 const short NSG = FC_mul_mv_nsg; 1642 1643 const int nb = args.ne00/QK_K; 1644 1645 const int r0 = tgpig.x; 1646 const int r1 = tgpig.y; 1647 const int im = tgpig.z; 1648 1649 const int first_row = (r0 * NSG + sgitg) * nr0; 1650 1651 const uint i12 = im%FC_mul_mv_ne12; 1652 const uint i13 = im/FC_mul_mv_ne12; 1653 1654 const uint64_t offset0 = first_row*args.nb01 + (i12/FC_mul_mv_r2)*args.nb02 + (i13/FC_mul_mv_r3)*args.nb03; 1655 const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13; 1656 1657 device const block_q5_K * x = (device const block_q5_K *) (src0 + offset0); 1658 device const float * yy = (device const float *) (src1 + offset1); 1659 1660 float sumf[nr0]={0.f}; 1661 1662 float yl[16], yh[16]; 1663 1664 constexpr uint16_t kmask1 = 0x3f3f; 1665 constexpr uint16_t kmask2 = 0x0f0f; 1666 constexpr uint16_t kmask3 = 0xc0c0; 1667 1668 const short tid = tiisg/4; 1669 const short ix = tiisg%4; 1670 const short iq = tid/4; 1671 const short ir = tid%4; 1672 1673 const short l0 = 8*ir; 1674 const short q_offset = 32*iq + l0; 1675 const short y_offset = 64*iq + l0; 1676 1677 const uint8_t hm1 = 1u << (2*iq); 1678 const uint8_t hm2 = hm1 << 1; 1679 const uint8_t hm3 = hm1 << 4; 1680 const uint8_t hm4 = hm2 << 4; 1681 1682 uint16_t sc16[4]; 1683 thread const uint8_t * sc8 = (thread const uint8_t *)sc16; 1684 1685 device const float * y1 = yy + ix*QK_K + y_offset; 1686 1687 for (int i = ix; i < nb; i += 4) { 1688 device const uint8_t * q1 = x[i].qs + q_offset; 1689 device const uint8_t * qh = x[i].qh + l0; 1690 device const half * dh = &x[i].d; 1691 device const uint16_t * a = (device const uint16_t *)x[i].scales + iq; 1692 1693 device const float * y2 = y1 + 128; 1694 float4 sumy = {0.f, 0.f, 0.f, 0.f}; 1695 for (short l = 0; l < 8; ++l) { 1696 yl[l+0] = y1[l+ 0]; sumy[0] += yl[l+0]; 1697 yl[l+8] = y1[l+32]; sumy[1] += yl[l+8]; 1698 yh[l+0] = y2[l+ 0]; sumy[2] += yh[l+0]; 1699 yh[l+8] = y2[l+32]; sumy[3] += yh[l+8]; 1700 } 1701 1702 for (short row = 0; row < nr0; ++row) { 1703 device const uint8_t * q2 = q1 + 64; 1704 1705 sc16[0] = a[0] & kmask1; 1706 sc16[1] = a[2] & kmask1; 1707 sc16[2] = ((a[4] >> 0) & kmask2) | ((a[0] & kmask3) >> 2); 1708 sc16[3] = ((a[4] >> 4) & kmask2) | ((a[2] & kmask3) >> 2); 1709 1710 float4 acc1 = {0.f}; 1711 float4 acc2 = {0.f}; 1712 FOR_UNROLL (short l = 0; l < 8; ++l) { 1713 uint8_t h = qh[l]; 1714 acc1[0] += yl[l+0] * (q1[l] & 0x0F); 1715 acc1[1] += yl[l+8] * (q1[l] & 0xF0); 1716 acc1[2] += yh[l+0] * (q2[l] & 0x0F); 1717 acc1[3] += yh[l+8] * (q2[l] & 0xF0); 1718 acc2[0] += h & hm1 ? yl[l+0] : 0.f; 1719 acc2[1] += h & hm2 ? yl[l+8] : 0.f; 1720 acc2[2] += h & hm3 ? yh[l+0] : 0.f; 1721 acc2[3] += h & hm4 ? yh[l+8] : 0.f; 1722 } 1723 1724 sumf[row] += dh[0] * (sc8[0] * (acc1[0] + 16.f*acc2[0]) + 1725 sc8[1] * (acc1[1]/16.f + 16.f*acc2[1]) + 1726 sc8[4] * (acc1[2] + 16.f*acc2[2]) + 1727 sc8[5] * (acc1[3]/16.f + 16.f*acc2[3])) - 1728 dh[1] * (sumy[0] * sc8[2] + sumy[1] * sc8[3] + sumy[2] * sc8[6] + sumy[3] * sc8[7]); 1729 1730 q1 += args.nb01; 1731 qh += args.nb01; 1732 dh += args.nb01/2; 1733 a += args.nb01/2; 1734 } 1735 1736 y1 += 4 * QK_K; 1737 } 1738 1739 device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0; 1740 1741 for (int row = 0; row < nr0 && first_row + row < args.ne0; ++row) { 1742 const float tot = simd_sum(sumf[row]); 1743 if (tiisg == 0) { 1744 dst_f32[first_row + row] = tot; 1745 } 1746 } 1747 } 1748 1749 [[host_name("kernel_mul_mv_q5_K_f32")]] 1750 kernel void kernel_mul_mv_q5_K_f32( 1751 constant ggml_metal_kargs_mul_mv & args, 1752 device const char * src0, 1753 device const char * src1, 1754 device char * dst, 1755 uint3 tgpig[[threadgroup_position_in_grid]], 1756 ushort tiisg[[thread_index_in_simdgroup]], 1757 ushort sgitg[[simdgroup_index_in_threadgroup]]) { 1758 1759 kernel_mul_mv_q5_K_f32_impl(args, src0, src1, dst, nullptr, tgpig, tiisg, sgitg); 1760 } 1761 1762 template 1763 void kernel_mul_mv_q6_K_f32_impl( 1764 args_t args, 1765 device const char * src0, 1766 device const char * src1, 1767 device char * dst, 1768 threadgroup char * shmem, 1769 uint3 tgpig, 1770 ushort tiisg, 1771 ushort sgitg) { 1772 const short NSG = FC_mul_mv_nsg; 1773 1774 constexpr uint8_t kmask1 = 0x03; 1775 constexpr uint8_t kmask2 = 0x0C; 1776 constexpr uint8_t kmask3 = 0x30; 1777 constexpr uint8_t kmask4 = 0xC0; 1778 1779 const int nb = args.ne00/QK_K; 1780 1781 const int r0 = tgpig.x; 1782 const int r1 = tgpig.y; 1783 const int im = tgpig.z; 1784 1785 const int first_row = (r0 * NSG + sgitg) * nr0; 1786 1787 const uint i12 = im%FC_mul_mv_ne12; 1788 const uint i13 = im/FC_mul_mv_ne12; 1789 1790 const uint64_t offset0 = first_row*args.nb01 + (i12/FC_mul_mv_r2)*args.nb02 + (i13/FC_mul_mv_r3)*args.nb03; 1791 const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13; 1792 1793 device const block_q6_K * x = (device const block_q6_K *) (src0 + offset0); 1794 device const float * yy = (device const float *) (src1 + offset1); 1795 1796 float sumf[nr0] = { 0.f }; 1797 1798 float yl[16]; 1799 1800 const short tid = tiisg/2; 1801 const short ix = tiisg%2; 1802 const short ip = tid/8; // 0 or 1 1803 const short il = tid%8; 1804 const short l0 = 4*il; 1805 const short is = 8*ip + l0/16; 1806 1807 const short y_offset = 128*ip + l0; 1808 const short q_offset_l = 64*ip + l0; 1809 const short q_offset_h = 32*ip + l0; 1810 1811 for (int i = ix; i < nb; i += 2) { 1812 device const uint8_t * q1 = x[i].ql + q_offset_l; 1813 device const uint8_t * q2 = q1 + 32; 1814 device const uint8_t * qh = x[i].qh + q_offset_h; 1815 device const int8_t * sc = x[i].scales + is; 1816 device const half * dh = &x[i].d; 1817 1818 device const float * y = yy + i * QK_K + y_offset; 1819 1820 for (short l = 0; l < 4; ++l) { 1821 yl[4*l + 0] = y[l + 0]; 1822 yl[4*l + 1] = y[l + 32]; 1823 yl[4*l + 2] = y[l + 64]; 1824 yl[4*l + 3] = y[l + 96]; 1825 } 1826 1827 for (short row = 0; row < nr0; ++row) { 1828 float4 sums = {0.f, 0.f, 0.f, 0.f}; 1829 1830 FOR_UNROLL (short l = 0; l < 4; ++l) { 1831 sums[0] += yl[4*l + 0] * ((int8_t)((q1[l] & 0xF) | ((qh[l] & kmask1) << 4)) - 32); 1832 sums[1] += yl[4*l + 1] * ((int8_t)((q2[l] & 0xF) | ((qh[l] & kmask2) << 2)) - 32); 1833 sums[2] += yl[4*l + 2] * ((int8_t)((q1[l] >> 4) | ((qh[l] & kmask3) << 0)) - 32); 1834 sums[3] += yl[4*l + 3] * ((int8_t)((q2[l] >> 4) | ((qh[l] & kmask4) >> 2)) - 32); 1835 } 1836 1837 sumf[row] += dh[0] * (sums[0] * sc[0] + sums[1] * sc[2] + sums[2] * sc[4] + sums[3] * sc[6]); 1838 1839 q1 += args.nb01; 1840 q2 += args.nb01; 1841 qh += args.nb01; 1842 sc += args.nb01; 1843 dh += args.nb01/2; 1844 } 1845 } 1846 1847 device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0; 1848 1849 for (int row = 0; row < nr0 && first_row + row < args.ne0; ++row) { 1850 float sum_all = simd_sum(sumf[row]); 1851 if (tiisg == 0) { 1852 dst_f32[first_row + row] = sum_all; 1853 } 1854 } 1855 } 1856 1857 [[host_name("kernel_mul_mv_q6_K_f32")]] 1858 kernel void kernel_mul_mv_q6_K_f32( 1859 constant ggml_metal_kargs_mul_mv & args, 1860 device const char * src0, 1861 device const char * src1, 1862 device char * dst, 1863 uint3 tgpig[[threadgroup_position_in_grid]], 1864 ushort tiisg[[thread_index_in_simdgroup]], 1865 ushort sgitg[[simdgroup_index_in_threadgroup]]) { 1866 1867 kernel_mul_mv_q6_K_f32_impl(args, src0, src1, dst, nullptr, tgpig, tiisg, sgitg); 1868 } 1869 1870 // ======================= "True" 2-bit 1871 1872 template 1873 void kernel_mul_mv_iq2_xxs_f32_impl( 1874 args_t args, 1875 device const char * src0, 1876 device const char * src1, 1877 device char * dst, 1878 threadgroup char * shmem, 1879 uint3 tgpig, 1880 ushort tiisg, 1881 ushort sgitg) { 1882 const short NSG = FC_mul_mv_nsg; 1883 1884 const int nb = args.ne00/QK_K; 1885 1886 const int r0 = tgpig.x; 1887 const int r1 = tgpig.y; 1888 const int im = tgpig.z; 1889 1890 const int first_row = (r0 * NSG + sgitg) * nr0; 1891 1892 const uint i12 = im%FC_mul_mv_ne12; 1893 const uint i13 = im/FC_mul_mv_ne12; 1894 1895 const int nb32 = nb * (QK_K / 32); 1896 1897 const short ntx = FC_mul_mv_split ? nb32 : 32; 1898 const short nrep = 32 / ntx; 1899 1900 const short ix = tiisg % ntx; 1901 const short irep = tiisg / ntx; 1902 1903 const short row0 = (nr0 * irep ) / nrep; 1904 const short row1 = (nr0 * (irep + 1)) / nrep; 1905 1906 const uint64_t offset0 = (first_row + row0)*args.nb01 + (i12/FC_mul_mv_r2)*args.nb02 + (i13/FC_mul_mv_r3)*args.nb03; 1907 const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13; 1908 1909 device const block_iq2_xxs * x = (device const block_iq2_xxs *) (src0 + offset0); 1910 device const float * y = (device const float *) (src1 + offset1); 1911 1912 float yl[32]; 1913 float sumf[nr0]={0.f}; 1914 1915 threadgroup uint64_t * svalues = (threadgroup uint64_t *)(shmem); 1916 threadgroup uint8_t * ssigns = (threadgroup uint8_t *)(svalues + 256); 1917 { 1918 int nval = 4; 1919 int pos = (32*sgitg + tiisg)*nval; 1920 for (int i = 0; i < nval; ++i) svalues[pos + i] = iq2xxs_grid[pos + i]; 1921 nval = 2; 1922 pos = (32*sgitg + tiisg)*nval; 1923 for (int i = 0; i < nval; ++i) ssigns[pos+i] = ksigns_iq2xs[pos+i]; 1924 threadgroup_barrier(mem_flags::mem_threadgroup); 1925 } 1926 1927 device const float * y4 = y + 32 * ix; 1928 1929 for (int ib32 = ix; ib32 < nb32; ib32 += ntx) { 1930 for (short i = 0; i < 32; ++i) { 1931 yl[i] = y4[i]; 1932 } 1933 1934 const int ibl = ib32 / (QK_K / 32); 1935 const int ib = ib32 % (QK_K / 32); 1936 1937 device const block_iq2_xxs * xr = x + ibl; 1938 device const uint16_t * q2 = xr->qs + 4 * ib; 1939 device const half * dh = &xr->d; 1940 1941 for (short row = row0; row < row1; row++) { 1942 const float db = dh[0]; 1943 device const uint8_t * aux8 = (device const uint8_t *)q2; 1944 const uint32_t aux32 = q2[2] | (q2[3] << 16); 1945 const float d = db * (0.5f + (aux32 >> 28)); 1946 1947 float sum = 0; 1948 for (short l = 0; l < 4; ++l) { 1949 const threadgroup uint8_t * grid = (const threadgroup uint8_t *)(svalues + aux8[l]); 1950 const uint8_t signs = ssigns[(aux32 >> 7*l) & 127]; 1951 for (short j = 0; j < 8; ++j) { 1952 sum += yl[8*l + j] * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f); 1953 } 1954 } 1955 sumf[row] += d * sum; 1956 1957 dh += args.nb01/2; 1958 q2 += args.nb01/2; 1959 } 1960 1961 y4 += 32 * ntx; 1962 } 1963 1964 device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0; 1965 1966 for (int row = 0; row < nr0 && first_row + row < args.ne0; ++row) { 1967 float sum_all = simd_sum(sumf[row]); 1968 if (tiisg == 0) { 1969 dst_f32[first_row + row] = sum_all * 0.25f; 1970 } 1971 } 1972 } 1973 1974 template 1975 void kernel_mul_mv_iq2_xxs_f32_disp( 1976 args_t args, 1977 device const char * src0, 1978 device const char * src1, 1979 device char * dst, 1980 threadgroup char * shmem, 1981 uint3 tgpig, 1982 ushort tiisg, 1983 ushort sgitg) { 1984 if (FC_mul_mv_split) { 1985 kernel_mul_mv_iq2_xxs_f32_impl(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg); 1986 } else { 1987 kernel_mul_mv_iq2_xxs_f32_impl(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg); 1988 } 1989 } 1990 1991 [[host_name("kernel_mul_mv_iq2_xxs_f32")]] 1992 kernel void kernel_mul_mv_iq2_xxs_f32( 1993 constant ggml_metal_kargs_mul_mv & args, 1994 device const char * src0, 1995 device const char * src1, 1996 device char * dst, 1997 threadgroup char * shmem [[threadgroup(0)]], 1998 uint3 tgpig[[threadgroup_position_in_grid]], 1999 ushort tiisg[[thread_index_in_simdgroup]], 2000 ushort sgitg[[simdgroup_index_in_threadgroup]]) { 2001 kernel_mul_mv_iq2_xxs_f32_disp(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg); 2002 } 2003 2004 template 2005 void kernel_mul_mv_iq2_xs_f32_impl( 2006 args_t args, 2007 device const char * src0, 2008 device const char * src1, 2009 device char * dst, 2010 threadgroup char * shmem, 2011 uint3 tgpig, 2012 ushort tiisg, 2013 ushort sgitg) { 2014 const short NSG = FC_mul_mv_nsg; 2015 2016 const int nb = args.ne00/QK_K; 2017 2018 const int r0 = tgpig.x; 2019 const int r1 = tgpig.y; 2020 const int im = tgpig.z; 2021 2022 const int first_row = (r0 * NSG + sgitg) * nr0; 2023 2024 const uint i12 = im%FC_mul_mv_ne12; 2025 const uint i13 = im/FC_mul_mv_ne12; 2026 2027 const int nb32 = nb * (QK_K / 32); 2028 2029 const short ntx = FC_mul_mv_split ? nb32 : 32; 2030 const short nrep = 32 / ntx; 2031 2032 const short ix = tiisg % ntx; 2033 const short irep = tiisg / ntx; 2034 2035 const short row0 = (nr0 * irep ) / nrep; 2036 const short row1 = (nr0 * (irep + 1)) / nrep; 2037 2038 const uint64_t offset0 = (first_row + row0)*args.nb01 + (i12/FC_mul_mv_r2)*args.nb02 + (i13/FC_mul_mv_r3)*args.nb03; 2039 const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13; 2040 2041 device const block_iq2_xs * x = (device const block_iq2_xs *) (src0 + offset0); 2042 device const float * y = (device const float *) (src1 + offset1); 2043 2044 float yl[32]; 2045 float sumf[nr0]={0.f}; 2046 2047 threadgroup uint64_t * svalues = (threadgroup uint64_t *)(shmem); 2048 threadgroup uint8_t * ssigns = (threadgroup uint8_t *)(svalues + 512); 2049 { 2050 int nval = 8; 2051 int pos = (32*sgitg + tiisg)*nval; 2052 for (int i = 0; i < nval; ++i) svalues[pos + i] = iq2xs_grid[pos + i]; 2053 nval = 2; 2054 pos = (32*sgitg + tiisg)*nval; 2055 for (int i = 0; i < nval; ++i) ssigns[pos+i] = ksigns_iq2xs[pos+i]; 2056 threadgroup_barrier(mem_flags::mem_threadgroup); 2057 } 2058 2059 device const float * y4 = y + 32 * ix; 2060 2061 for (int ib32 = ix; ib32 < nb32; ib32 += ntx) { 2062 for (short i = 0; i < 32; ++i) { 2063 yl[i] = y4[i]; 2064 } 2065 2066 const int ibl = ib32 / (QK_K / 32); 2067 const int ib = ib32 % (QK_K / 32); 2068 2069 device const block_iq2_xs * xr = x + ibl; 2070 device const uint16_t * q2 = xr->qs + 4 * ib; 2071 device const uint8_t * sc = xr->scales + ib; 2072 device const half * dh = &xr->d; 2073 2074 for (short row = row0; row < row1; row++) { 2075 const float db = dh[0]; 2076 const uint8_t ls1 = sc[0] & 0xf; 2077 const uint8_t ls2 = sc[0] >> 4; 2078 const float d1 = db * (0.5f + ls1); 2079 const float d2 = db * (0.5f + ls2); 2080 2081 float sum1 = 0, sum2 = 0; 2082 for (short l = 0; l < 2; ++l) { 2083 const threadgroup uint8_t * grid = (const threadgroup uint8_t *)(svalues + (q2[l] & 511)); 2084 const uint8_t signs = ssigns[(q2[l] >> 9)]; 2085 for (short j = 0; j < 8; ++j) { 2086 sum1 += yl[8*l + j] * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f); 2087 } 2088 } 2089 for (short l = 2; l < 4; ++l) { 2090 const threadgroup uint8_t * grid = (const threadgroup uint8_t *)(svalues + (q2[l] & 511)); 2091 const uint8_t signs = ssigns[(q2[l] >> 9)]; 2092 for (short j = 0; j < 8; ++j) { 2093 sum2 += yl[8*l + j] * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f); 2094 } 2095 } 2096 sumf[row] += d1 * sum1 + d2 * sum2; 2097 2098 dh += args.nb01/2; 2099 q2 += args.nb01/2; 2100 sc += args.nb01; 2101 } 2102 2103 y4 += 32 * ntx; 2104 } 2105 2106 device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0; 2107 2108 for (int row = 0; row < nr0 && first_row + row < args.ne0; ++row) { 2109 float sum_all = simd_sum(sumf[row]); 2110 if (tiisg == 0) { 2111 dst_f32[first_row + row] = sum_all * 0.25f; 2112 } 2113 } 2114 } 2115 2116 template 2117 void kernel_mul_mv_iq2_xs_f32_disp( 2118 args_t args, 2119 device const char * src0, 2120 device const char * src1, 2121 device char * dst, 2122 threadgroup char * shmem, 2123 uint3 tgpig, 2124 ushort tiisg, 2125 ushort sgitg) { 2126 if (FC_mul_mv_split) { 2127 kernel_mul_mv_iq2_xs_f32_impl(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg); 2128 } else { 2129 kernel_mul_mv_iq2_xs_f32_impl(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg); 2130 } 2131 } 2132 2133 [[host_name("kernel_mul_mv_iq2_xs_f32")]] 2134 kernel void kernel_mul_mv_iq2_xs_f32( 2135 constant ggml_metal_kargs_mul_mv & args, 2136 device const char * src0, 2137 device const char * src1, 2138 device char * dst, 2139 threadgroup char * shmem [[threadgroup(0)]], 2140 uint3 tgpig[[threadgroup_position_in_grid]], 2141 ushort tiisg[[thread_index_in_simdgroup]], 2142 ushort sgitg[[simdgroup_index_in_threadgroup]]) { 2143 2144 kernel_mul_mv_iq2_xs_f32_disp(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg); 2145 } 2146 2147 // FC_mul_mv_split: for nb32 < 32 (nb32 divides 32), 32/nb32 threads share each chunk and each takes a slice of the rows 2148 template 2149 void kernel_mul_mv_iq3_xxs_f32_impl( 2150 args_t args, 2151 device const char * src0, 2152 device const char * src1, 2153 device char * dst, 2154 threadgroup char * shmem, 2155 uint3 tgpig, 2156 ushort tiisg, 2157 ushort sgitg) { 2158 const short NSG = FC_mul_mv_nsg; 2159 2160 const int nb = args.ne00/QK_K; 2161 2162 const int r0 = tgpig.x; 2163 const int r1 = tgpig.y; 2164 const int im = tgpig.z; 2165 2166 const int first_row = (r0 * NSG + sgitg) * nr0; 2167 2168 const uint i12 = im%FC_mul_mv_ne12; 2169 const uint i13 = im/FC_mul_mv_ne12; 2170 2171 const int nb32 = nb * (QK_K / 32); 2172 2173 const short ntx = FC_mul_mv_split ? nb32 : 32; 2174 const short nrep = 32 / ntx; 2175 2176 const short ix = tiisg % ntx; 2177 const short irep = tiisg / ntx; 2178 2179 const short row0 = (nr0 * irep ) / nrep; 2180 const short row1 = (nr0 * (irep + 1)) / nrep; 2181 2182 const uint64_t offset0 = (first_row + row0)*args.nb01 + (i12/FC_mul_mv_r2)*args.nb02 + (i13/FC_mul_mv_r3)*args.nb03; 2183 const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13; 2184 2185 device const block_iq3_xxs * x = (device const block_iq3_xxs *) (src0 + offset0); 2186 device const float * y = (device const float *) (src1 + offset1); 2187 2188 float yl[32]; 2189 float sumf[nr0]={0.f}; 2190 2191 threadgroup uint32_t * svalues = (threadgroup uint32_t *)(shmem); 2192 threadgroup uint8_t * ssigns = (threadgroup uint8_t *)(svalues + 256); 2193 { 2194 int nval = 4; 2195 int pos = (32*sgitg + tiisg)*nval; 2196 for (int i = 0; i < nval; ++i) svalues[pos + i] = iq3xxs_grid[pos + i]; 2197 nval = 2; 2198 pos = (32*sgitg + tiisg)*nval; 2199 for (int i = 0; i < nval; ++i) ssigns[pos+i] = ksigns_iq2xs[pos+i]; 2200 threadgroup_barrier(mem_flags::mem_threadgroup); 2201 } 2202 2203 device const float * y4 = y + 32 * ix; 2204 2205 for (int ib32 = ix; ib32 < nb32; ib32 += ntx) { 2206 for (short i = 0; i < 32; ++i) { 2207 yl[i] = y4[i]; 2208 } 2209 2210 const int ibl = ib32 / (QK_K / 32); 2211 const int ib = ib32 % (QK_K / 32); 2212 2213 device const block_iq3_xxs * xr = x + ibl; 2214 device const uint8_t * q3 = xr->qs + 8 * ib; 2215 device const uint16_t * gas = (device const uint16_t *)(xr->qs + QK_K/4) + 2 * ib; 2216 device const half * dh = &xr->d; 2217 2218 for (short row = row0; row < row1; row++) { 2219 const float db = dh[0]; 2220 const uint32_t aux32 = gas[0] | (gas[1] << 16); 2221 const float d = db * (0.5f + (aux32 >> 28)); 2222 2223 float2 sum = {0}; 2224 for (short l = 0; l < 4; ++l) { 2225 const threadgroup uint8_t * grid1 = (const threadgroup uint8_t *)(svalues + q3[2*l+0]); 2226 const threadgroup uint8_t * grid2 = (const threadgroup uint8_t *)(svalues + q3[2*l+1]); 2227 const uint8_t signs = ssigns[(aux32 >> 7*l) & 127]; 2228 for (short j = 0; j < 4; ++j) { 2229 sum[0] += yl[8*l + j + 0] * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f); 2230 sum[1] += yl[8*l + j + 4] * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f); 2231 } 2232 } 2233 sumf[row] += d * (sum[0] + sum[1]); 2234 2235 dh += args.nb01/2; 2236 q3 += args.nb01; 2237 gas += args.nb01/2; 2238 } 2239 2240 y4 += 32 * ntx; 2241 } 2242 2243 device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0; 2244 2245 for (int row = 0; row < nr0 && first_row + row < args.ne0; ++row) { 2246 float sum_all = simd_sum(sumf[row]); 2247 if (tiisg == 0) { 2248 dst_f32[first_row + row] = sum_all * 0.5f; 2249 } 2250 } 2251 } 2252 2253 template 2254 void kernel_mul_mv_iq3_xxs_f32_disp( 2255 args_t args, 2256 device const char * src0, 2257 device const char * src1, 2258 device char * dst, 2259 threadgroup char * shmem, 2260 uint3 tgpig, 2261 ushort tiisg, 2262 ushort sgitg) { 2263 if (FC_mul_mv_split) { 2264 kernel_mul_mv_iq3_xxs_f32_impl(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg); 2265 } else { 2266 kernel_mul_mv_iq3_xxs_f32_impl(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg); 2267 } 2268 } 2269 2270 [[host_name("kernel_mul_mv_iq3_xxs_f32")]] 2271 kernel void kernel_mul_mv_iq3_xxs_f32( 2272 constant ggml_metal_kargs_mul_mv & args, 2273 device const char * src0, 2274 device const char * src1, 2275 device char * dst, 2276 threadgroup char * shmem [[threadgroup(0)]], 2277 uint3 tgpig[[threadgroup_position_in_grid]], 2278 ushort tiisg[[thread_index_in_simdgroup]], 2279 ushort sgitg[[simdgroup_index_in_threadgroup]]) { 2280 2281 kernel_mul_mv_iq3_xxs_f32_disp(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg); 2282 } 2283 2284 template 2285 void kernel_mul_mv_iq3_s_f32_impl( 2286 args_t args, 2287 device const char * src0, 2288 device const char * src1, 2289 device char * dst, 2290 threadgroup char * shmem, 2291 uint3 tgpig, 2292 ushort tiisg, 2293 ushort sgitg) { 2294 const short NSG = FC_mul_mv_nsg; 2295 2296 const int nb = args.ne00/QK_K; 2297 2298 const int r0 = tgpig.x; 2299 const int r1 = tgpig.y; 2300 const int im = tgpig.z; 2301 2302 const int first_row = (r0 * NSG + sgitg) * nr0; 2303 2304 const uint i12 = im%FC_mul_mv_ne12; 2305 const uint i13 = im/FC_mul_mv_ne12; 2306 2307 const int nb32 = nb * (QK_K / 32); 2308 2309 const short ntx = FC_mul_mv_split ? nb32 : 32; 2310 const short nrep = 32 / ntx; 2311 2312 const short ix = tiisg % ntx; 2313 const short irep = tiisg / ntx; 2314 2315 const short row0 = (nr0 * irep ) / nrep; 2316 const short row1 = (nr0 * (irep + 1)) / nrep; 2317 2318 const uint64_t offset0 = (first_row + row0)*args.nb01 + (i12/FC_mul_mv_r2)*args.nb02 + (i13/FC_mul_mv_r3)*args.nb03; 2319 const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13; 2320 2321 device const block_iq3_s * x = (device const block_iq3_s *) (src0 + offset0); 2322 device const float * y = (device const float *) (src1 + offset1); 2323 2324 float yl[32]; 2325 float sumf[nr0]={0.f}; 2326 2327 threadgroup uint32_t * svalues = (threadgroup uint32_t *) shmem; 2328 { 2329 int nval = 8; 2330 int pos = (32*sgitg + tiisg)*nval; 2331 for (int i = 0; i < nval; ++i) svalues[pos + i] = iq3s_grid[pos + i]; 2332 threadgroup_barrier(mem_flags::mem_threadgroup); 2333 } 2334 2335 device const float * y4 = y + 32 * ix; 2336 2337 for (int ib32 = ix; ib32 < nb32; ib32 += ntx) { 2338 for (short i = 0; i < 32; ++i) { 2339 yl[i] = y4[i]; 2340 } 2341 2342 const int ibl = ib32 / (QK_K / 32); 2343 const int ib = ib32 % (QK_K / 32); 2344 2345 device const block_iq3_s * xr = x + ibl; 2346 device const uint8_t * qs = xr->qs + 8 * ib; 2347 device const uint8_t * qh = xr->qh + ib; 2348 device const uint8_t * sc = xr->scales + (ib/2); 2349 device const uint8_t * signs = xr->signs + 4 * ib; 2350 device const half * dh = &xr->d; 2351 2352 for (short row = row0; row < row1; row++) { 2353 const float db = dh[0]; 2354 const float d = db * (1 + 2*((sc[0] >> 4*(ib%2)) & 0xf)); 2355 2356 float2 sum = {0}; 2357 for (short l = 0; l < 4; ++l) { 2358 const threadgroup uint32_t * table1 = qh[0] & kmask_iq2xs[2*l+0] ? svalues + 256 : svalues; 2359 const threadgroup uint32_t * table2 = qh[0] & kmask_iq2xs[2*l+1] ? svalues + 256 : svalues; 2360 const threadgroup uint8_t * grid1 = (const threadgroup uint8_t *)(table1 + qs[2*l+0]); 2361 const threadgroup uint8_t * grid2 = (const threadgroup uint8_t *)(table2 + qs[2*l+1]); 2362 for (short j = 0; j < 4; ++j) { 2363 sum[0] += yl[8*l + j + 0] * grid1[j] * select(1, -1, signs[l] & kmask_iq2xs[j+0]); 2364 sum[1] += yl[8*l + j + 4] * grid2[j] * select(1, -1, signs[l] & kmask_iq2xs[j+4]); 2365 } 2366 } 2367 sumf[row] += d * (sum[0] + sum[1]); 2368 2369 dh += args.nb01/2; 2370 qs += args.nb01; 2371 qh += args.nb01; 2372 sc += args.nb01; 2373 signs += args.nb01; 2374 } 2375 2376 y4 += 32 * ntx; 2377 } 2378 2379 device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0; 2380 2381 for (int row = 0; row < nr0 && first_row + row < args.ne0; ++row) { 2382 float sum_all = simd_sum(sumf[row]); 2383 if (tiisg == 0) { 2384 dst_f32[first_row + row] = sum_all; 2385 } 2386 } 2387 } 2388 2389 template 2390 void kernel_mul_mv_iq3_s_f32_disp( 2391 args_t args, 2392 device const char * src0, 2393 device const char * src1, 2394 device char * dst, 2395 threadgroup char * shmem, 2396 uint3 tgpig, 2397 ushort tiisg, 2398 ushort sgitg) { 2399 if (FC_mul_mv_split) { 2400 kernel_mul_mv_iq3_s_f32_impl(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg); 2401 } else { 2402 kernel_mul_mv_iq3_s_f32_impl(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg); 2403 } 2404 } 2405 2406 [[host_name("kernel_mul_mv_iq3_s_f32")]] 2407 kernel void kernel_mul_mv_iq3_s_f32( 2408 constant ggml_metal_kargs_mul_mv & args, 2409 device const char * src0, 2410 device const char * src1, 2411 device char * dst, 2412 threadgroup char * shmem [[threadgroup(0)]], 2413 uint3 tgpig[[threadgroup_position_in_grid]], 2414 ushort tiisg[[thread_index_in_simdgroup]], 2415 ushort sgitg[[simdgroup_index_in_threadgroup]]) { 2416 2417 kernel_mul_mv_iq3_s_f32_disp(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg); 2418 } 2419 2420 template 2421 void kernel_mul_mv_iq2_s_f32_impl( 2422 args_t args, 2423 device const char * src0, 2424 device const char * src1, 2425 device char * dst, 2426 threadgroup char * shmem, 2427 uint3 tgpig, 2428 ushort tiisg, 2429 ushort sgitg) { 2430 const short NSG = FC_mul_mv_nsg; 2431 2432 const int nb = args.ne00/QK_K; 2433 2434 const int r0 = tgpig.x; 2435 const int r1 = tgpig.y; 2436 const int im = tgpig.z; 2437 2438 const int first_row = (r0 * NSG + sgitg) * nr0; 2439 2440 const uint i12 = im%FC_mul_mv_ne12; 2441 const uint i13 = im/FC_mul_mv_ne12; 2442 2443 const int nb32 = nb * (QK_K / 32); 2444 2445 const short ntx = FC_mul_mv_split ? nb32 : 32; 2446 const short nrep = 32 / ntx; 2447 2448 const short ix = tiisg % ntx; 2449 const short irep = tiisg / ntx; 2450 2451 const short row0 = (nr0 * irep ) / nrep; 2452 const short row1 = (nr0 * (irep + 1)) / nrep; 2453 2454 const uint64_t offset0 = (first_row + row0)*args.nb01 + (i12/FC_mul_mv_r2)*args.nb02 + (i13/FC_mul_mv_r3)*args.nb03; 2455 const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13; 2456 2457 device const block_iq2_s * x = (device const block_iq2_s *) (src0 + offset0); 2458 device const float * y = (device const float *) (src1 + offset1); 2459 2460 float yl[32]; 2461 float sumf[nr0]={0.f}; 2462 2463 //threadgroup uint64_t * svalues = (threadgroup uint64_t *) shmem; 2464 //{ 2465 // int nval = 32; 2466 // int pos = (32*sgitg + tiisg)*nval; 2467 // for (int i = 0; i < nval; ++i) svalues[pos + i] = iq2s_grid[pos + i]; 2468 // threadgroup_barrier(mem_flags::mem_threadgroup); 2469 //} 2470 2471 device const float * y4 = y + 32 * ix; 2472 2473 for (int ib32 = ix; ib32 < nb32; ib32 += ntx) { 2474 for (short i = 0; i < 32; ++i) { 2475 yl[i] = y4[i]; 2476 } 2477 2478 const int ibl = ib32 / (QK_K / 32); 2479 const int ib = ib32 % (QK_K / 32); 2480 2481 device const block_iq2_s * xr = x + ibl; 2482 device const uint8_t * qs = xr->qs + 4 * ib; 2483 device const uint8_t * qh = xr->qh + ib; 2484 device const uint8_t * sc = xr->scales + ib; 2485 device const uint8_t * signs = qs + QK_K/8; 2486 device const half * dh = &xr->d; 2487 2488 for (short row = row0; row < row1; row++) { 2489 const float db = dh[0]; 2490 const float d1 = db * (0.5f + (sc[0] & 0xf)); 2491 const float d2 = db * (0.5f + (sc[0] >> 4)); 2492 2493 float2 sum = {0}; 2494 for (short l = 0; l < 2; ++l) { 2495 //const threadgroup uint8_t * grid1 = (const threadgroup uint8_t *)(svalues + (qs[l+0] | ((qh[0] << (8-2*l)) & 0x300))); 2496 //const threadgroup uint8_t * grid2 = (const threadgroup uint8_t *)(svalues + (qs[l+2] | ((qh[0] << (4-2*l)) & 0x300))); 2497 constant uint8_t * grid1 = (constant uint8_t *)(iq2s_grid + (qs[l+0] | ((qh[0] << (8-2*l)) & 0x300))); 2498 constant uint8_t * grid2 = (constant uint8_t *)(iq2s_grid + (qs[l+2] | ((qh[0] << (4-2*l)) & 0x300))); 2499 for (short j = 0; j < 8; ++j) { 2500 sum[0] += yl[8*l + j + 0] * grid1[j] * select(1, -1, signs[l+0] & kmask_iq2xs[j]); 2501 sum[1] += yl[8*l + j + 16] * grid2[j] * select(1, -1, signs[l+2] & kmask_iq2xs[j]); 2502 } 2503 } 2504 sumf[row] += d1 * sum[0] + d2 * sum[1]; 2505 2506 dh += args.nb01/2; 2507 qs += args.nb01; 2508 qh += args.nb01; 2509 sc += args.nb01; 2510 signs += args.nb01; 2511 } 2512 2513 y4 += 32 * ntx; 2514 } 2515 2516 device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0; 2517 2518 for (int row = 0; row < nr0 && first_row + row < args.ne0; ++row) { 2519 float sum_all = simd_sum(sumf[row]); 2520 if (tiisg == 0) { 2521 dst_f32[first_row + row] = sum_all * 0.25f; 2522 } 2523 } 2524 } 2525 2526 template 2527 void kernel_mul_mv_iq2_s_f32_disp( 2528 args_t args, 2529 device const char * src0, 2530 device const char * src1, 2531 device char * dst, 2532 threadgroup char * shmem, 2533 uint3 tgpig, 2534 ushort tiisg, 2535 ushort sgitg) { 2536 if (FC_mul_mv_split) { 2537 kernel_mul_mv_iq2_s_f32_impl(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg); 2538 } else { 2539 kernel_mul_mv_iq2_s_f32_impl(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg); 2540 } 2541 } 2542 2543 [[host_name("kernel_mul_mv_iq2_s_f32")]] 2544 kernel void kernel_mul_mv_iq2_s_f32( 2545 constant ggml_metal_kargs_mul_mv & args, 2546 device const char * src0, 2547 device const char * src1, 2548 device char * dst, 2549 threadgroup char * shmem [[threadgroup(0)]], 2550 uint3 tgpig[[threadgroup_position_in_grid]], 2551 ushort tiisg[[thread_index_in_simdgroup]], 2552 ushort sgitg[[simdgroup_index_in_threadgroup]]) { 2553 2554 kernel_mul_mv_iq2_s_f32_disp(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg); 2555 } 2556 2557 template 2558 void kernel_mul_mv_iq1_s_f32_impl( 2559 args_t args, 2560 device const char * src0, 2561 device const char * src1, 2562 device char * dst, 2563 threadgroup char * shmem, 2564 uint3 tgpig, 2565 ushort tiisg, 2566 ushort sgitg) { 2567 const short NSG = FC_mul_mv_nsg; 2568 2569 const int nb = args.ne00/QK_K; 2570 2571 const int r0 = tgpig.x; 2572 const int r1 = tgpig.y; 2573 const int im = tgpig.z; 2574 2575 const int first_row = (r0 * NSG + sgitg) * nr0; 2576 2577 const uint i12 = im%FC_mul_mv_ne12; 2578 const uint i13 = im/FC_mul_mv_ne12; 2579 2580 const int nb32 = nb * (QK_K / 32); 2581 2582 const short ntx = FC_mul_mv_split ? nb32 : 32; 2583 const short nrep = 32 / ntx; 2584 2585 const short ix = tiisg % ntx; 2586 const short irep = tiisg / ntx; 2587 2588 const short row0 = (nr0 * irep ) / nrep; 2589 const short row1 = (nr0 * (irep + 1)) / nrep; 2590 2591 const uint64_t offset0 = (first_row + row0)*args.nb01 + (i12/FC_mul_mv_r2)*args.nb02 + (i13/FC_mul_mv_r3)*args.nb03; 2592 const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13; 2593 2594 device const block_iq1_s * x = (device const block_iq1_s *) (src0 + offset0); 2595 device const float * y = (device const float *) (src1 + offset1); 2596 2597 float yl[32]; 2598 float sumf[nr0]={0.f}; 2599 2600 device const float * y4 = y + 32 * ix; 2601 2602 for (int ib32 = ix; ib32 < nb32; ib32 += ntx) { 2603 float sumy = 0; 2604 for (short i = 0; i < 32; ++i) { 2605 yl[i] = y4[i]; 2606 sumy += yl[i]; 2607 } 2608 2609 const int ibl = ib32 / (QK_K / 32); 2610 const int ib = ib32 % (QK_K / 32); 2611 2612 device const block_iq1_s * xr = x + ibl; 2613 device const uint8_t * qs = xr->qs + 4 * ib; 2614 device const uint16_t * qh = xr->qh + ib; 2615 device const half * dh = &xr->d; 2616 2617 for (short row = row0; row < row1; row++) { 2618 constant uint8_t * grid1 = (constant uint8_t *)(iq1s_grid_gpu + (qs[0] | ((qh[0] << 8) & 0x700))); 2619 constant uint8_t * grid2 = (constant uint8_t *)(iq1s_grid_gpu + (qs[1] | ((qh[0] << 5) & 0x700))); 2620 constant uint8_t * grid3 = (constant uint8_t *)(iq1s_grid_gpu + (qs[2] | ((qh[0] << 2) & 0x700))); 2621 constant uint8_t * grid4 = (constant uint8_t *)(iq1s_grid_gpu + (qs[3] | ((qh[0] >> 1) & 0x700))); 2622 2623 float sum = 0; 2624 for (short j = 0; j < 4; ++j) { 2625 sum += yl[j+ 0] * (grid1[j] & 0xf) + yl[j+ 4] * (grid1[j] >> 4) 2626 + yl[j+ 8] * (grid2[j] & 0xf) + yl[j+12] * (grid2[j] >> 4) 2627 + yl[j+16] * (grid3[j] & 0xf) + yl[j+20] * (grid3[j] >> 4) 2628 + yl[j+24] * (grid4[j] & 0xf) + yl[j+28] * (grid4[j] >> 4); 2629 } 2630 sumf[row] += (float)dh[0] * (sum + sumy * (qh[0] & 0x8000 ? -1 - IQ1S_DELTA : -1 + IQ1S_DELTA)) * (2*((qh[0] >> 12) & 7) + 1); 2631 2632 dh += args.nb01/2; 2633 qs += args.nb01; 2634 qh += args.nb01/2; 2635 } 2636 2637 y4 += 32 * ntx; 2638 } 2639 2640 device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0; 2641 2642 for (int row = 0; row < nr0 && first_row + row < args.ne0; ++row) { 2643 float sum_all = simd_sum(sumf[row]); 2644 if (tiisg == 0) { 2645 dst_f32[first_row + row] = sum_all; 2646 } 2647 } 2648 } 2649 2650 template 2651 void kernel_mul_mv_iq1_s_f32_disp( 2652 args_t args, 2653 device const char * src0, 2654 device const char * src1, 2655 device char * dst, 2656 threadgroup char * shmem, 2657 uint3 tgpig, 2658 ushort tiisg, 2659 ushort sgitg) { 2660 if (FC_mul_mv_split) { 2661 kernel_mul_mv_iq1_s_f32_impl(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg); 2662 } else { 2663 kernel_mul_mv_iq1_s_f32_impl(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg); 2664 } 2665 } 2666 2667 [[host_name("kernel_mul_mv_iq1_s_f32")]] 2668 kernel void kernel_mul_mv_iq1_s_f32( 2669 constant ggml_metal_kargs_mul_mv & args, 2670 device const char * src0, 2671 device const char * src1, 2672 device char * dst, 2673 uint3 tgpig[[threadgroup_position_in_grid]], 2674 ushort tiisg[[thread_index_in_simdgroup]], 2675 ushort sgitg[[simdgroup_index_in_threadgroup]]) { 2676 2677 kernel_mul_mv_iq1_s_f32_disp(args, src0, src1, dst, nullptr, tgpig, tiisg, sgitg); 2678 } 2679 2680 template 2681 void kernel_mul_mv_iq1_m_f32_impl( 2682 args_t args, 2683 device const char * src0, 2684 device const char * src1, 2685 device char * dst, 2686 threadgroup char * shmem, 2687 uint3 tgpig, 2688 ushort tiisg, 2689 ushort sgitg) { 2690 const short NSG = FC_mul_mv_nsg; 2691 2692 const int nb = args.ne00/QK_K; 2693 2694 const int r0 = tgpig.x; 2695 const int r1 = tgpig.y; 2696 const int im = tgpig.z; 2697 2698 const int first_row = (r0 * NSG + sgitg) * nr0; 2699 2700 const uint i12 = im%FC_mul_mv_ne12; 2701 const uint i13 = im/FC_mul_mv_ne12; 2702 2703 const int nb32 = nb * (QK_K / 32); 2704 2705 const short ntx = FC_mul_mv_split ? nb32 : 32; 2706 const short nrep = 32 / ntx; 2707 2708 const short ix = tiisg % ntx; 2709 const short irep = tiisg / ntx; 2710 2711 const short row0 = (nr0 * irep ) / nrep; 2712 const short row1 = (nr0 * (irep + 1)) / nrep; 2713 2714 const uint64_t offset0 = (first_row + row0)*args.nb01 + (i12/FC_mul_mv_r2)*args.nb02 + (i13/FC_mul_mv_r3)*args.nb03; 2715 const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13; 2716 2717 device const block_iq1_m * x = (device const block_iq1_m *) (src0 + offset0); 2718 device const float * y = (device const float *) (src1 + offset1); 2719 2720 float yl[32]; 2721 float sumf[nr0]={0.f}; 2722 2723 device const float * y4 = y + 32 * ix; 2724 2725 iq1m_scale_t scale; 2726 2727 for (int ib32 = ix; ib32 < nb32; ib32 += ntx) { 2728 float4 sumy = {0.f}; 2729 for (short i = 0; i < 8; ++i) { 2730 yl[i+ 0] = y4[i+ 0]; sumy[0] += yl[i+ 0]; 2731 yl[i+ 8] = y4[i+ 8]; sumy[1] += yl[i+ 8]; 2732 yl[i+16] = y4[i+16]; sumy[2] += yl[i+16]; 2733 yl[i+24] = y4[i+24]; sumy[3] += yl[i+24]; 2734 } 2735 2736 const int ibl = ib32 / (QK_K / 32); 2737 const int ib = ib32 % (QK_K / 32); 2738 2739 device const block_iq1_m * xr = x + ibl; 2740 device const uint8_t * qs = xr->qs + 4 * ib; 2741 device const uint8_t * qh = xr->qh + 2 * ib; 2742 device const uint16_t * sc = (device const uint16_t *)xr->scales; 2743 2744 for (short row = row0; row < row1; row++) { 2745 scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000); 2746 2747 constant uint8_t * grid1 = (constant uint8_t *)(iq1s_grid_gpu + (qs[0] | ((qh[0] << 8) & 0x700))); 2748 constant uint8_t * grid2 = (constant uint8_t *)(iq1s_grid_gpu + (qs[1] | ((qh[0] << 4) & 0x700))); 2749 constant uint8_t * grid3 = (constant uint8_t *)(iq1s_grid_gpu + (qs[2] | ((qh[1] << 8) & 0x700))); 2750 constant uint8_t * grid4 = (constant uint8_t *)(iq1s_grid_gpu + (qs[3] | ((qh[1] << 4) & 0x700))); 2751 2752 float2 sum = {0.f}; 2753 for (short j = 0; j < 4; ++j) { 2754 sum[0] += yl[j+ 0] * (grid1[j] & 0xf) + yl[j+ 4] * (grid1[j] >> 4) 2755 + yl[j+ 8] * (grid2[j] & 0xf) + yl[j+12] * (grid2[j] >> 4); 2756 sum[1] += yl[j+16] * (grid3[j] & 0xf) + yl[j+20] * (grid3[j] >> 4) 2757 + yl[j+24] * (grid4[j] & 0xf) + yl[j+28] * (grid4[j] >> 4); 2758 } 2759 const float delta1 = sumy[0] * (qh[0] & 0x08 ? -1 - IQ1M_DELTA : -1 + IQ1M_DELTA) + sumy[1] * (qh[0] & 0x80 ? -1 - IQ1M_DELTA : -1 + IQ1M_DELTA); 2760 const float delta2 = sumy[2] * (qh[1] & 0x08 ? -1 - IQ1M_DELTA : -1 + IQ1M_DELTA) + sumy[3] * (qh[1] & 0x80 ? -1 - IQ1M_DELTA : -1 + IQ1M_DELTA); 2761 2762 sumf[row] += (float)scale.f16 * ((sum[0] + delta1) * (2*((sc[ib/2] >> (6*(ib%2)+0)) & 7) + 1) + 2763 (sum[1] + delta2) * (2*((sc[ib/2] >> (6*(ib%2)+3)) & 7) + 1)); 2764 2765 sc += args.nb01/2; 2766 qs += args.nb01; 2767 qh += args.nb01; 2768 } 2769 2770 y4 += 32 * ntx; 2771 } 2772 2773 device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0; 2774 2775 for (int row = 0; row < nr0 && first_row + row < args.ne0; ++row) { 2776 float sum_all = simd_sum(sumf[row]); 2777 if (tiisg == 0) { 2778 dst_f32[first_row + row] = sum_all; 2779 } 2780 } 2781 } 2782 2783 template 2784 void kernel_mul_mv_iq1_m_f32_disp( 2785 args_t args, 2786 device const char * src0, 2787 device const char * src1, 2788 device char * dst, 2789 threadgroup char * shmem, 2790 uint3 tgpig, 2791 ushort tiisg, 2792 ushort sgitg) { 2793 if (FC_mul_mv_split) { 2794 kernel_mul_mv_iq1_m_f32_impl(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg); 2795 } else { 2796 kernel_mul_mv_iq1_m_f32_impl(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg); 2797 } 2798 } 2799 2800 [[host_name("kernel_mul_mv_iq1_m_f32")]] 2801 kernel void kernel_mul_mv_iq1_m_f32( 2802 constant ggml_metal_kargs_mul_mv & args, 2803 device const char * src0, 2804 device const char * src1, 2805 device char * dst, 2806 uint3 tgpig[[threadgroup_position_in_grid]], 2807 ushort tiisg[[thread_index_in_simdgroup]], 2808 ushort sgitg[[simdgroup_index_in_threadgroup]]) { 2809 2810 kernel_mul_mv_iq1_m_f32_disp(args, src0, src1, dst, nullptr, tgpig, tiisg, sgitg); 2811 } 2812 2813 template 2814 void kernel_mul_mv_iq4_nl_f32_impl( 2815 args_t args, 2816 device const char * src0, 2817 device const char * src1, 2818 device char * dst, 2819 threadgroup char * shmem, 2820 uint3 tgpig, 2821 ushort tiisg, 2822 ushort sgitg) { 2823 const short NSG = FC_mul_mv_nsg; 2824 2825 threadgroup float * shmem_f32 = (threadgroup float *) shmem; 2826 2827 const int r0 = tgpig.x; 2828 const int r1 = tgpig.y; 2829 const int im = tgpig.z; 2830 2831 const int first_row = (r0 * NSG + sgitg) * NR0; 2832 2833 const uint i12 = im%FC_mul_mv_ne12; 2834 const uint i13 = im/FC_mul_mv_ne12; 2835 2836 const uint64_t offset0 = first_row*args.nb01 + (i12/FC_mul_mv_r2)*args.nb02 + (i13/FC_mul_mv_r3)*args.nb03; 2837 const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13; 2838 2839 device const block_iq4_nl * x = (device const block_iq4_nl *) (src0 + offset0); 2840 device const float * y = (device const float *) (src1 + offset1); 2841 2842 const int nb = args.ne00/QK4_NL; 2843 const int ns01 = args.nb01/args.nb00; 2844 2845 const short ix = tiisg/2; // 0...15 2846 const short it = tiisg%2; // 0 or 1 2847 2848 shmem_f32[tiisg] = kvalues_iq4nl_f[tiisg%16]; 2849 threadgroup_barrier(mem_flags::mem_threadgroup); 2850 2851 float4 yl[4]; 2852 float sumf[NR0]={0.f}; 2853 2854 device const float * yb = y + ix*QK4_NL + it*8; 2855 2856 uint32_t aux32[2]; 2857 thread const uint8_t * q8 = (thread const uint8_t *)aux32; 2858 2859 float4 qf1, qf2; 2860 2861 // [TAG_MUL_MV_WEIRD] 2862 for (int ib = ix; ib < nb && ib < ns01; ib += 16) { 2863 device const float4 * y4 = (device const float4 *)yb; 2864 yl[0] = y4[0]; 2865 yl[1] = y4[4]; 2866 yl[2] = y4[1]; 2867 yl[3] = y4[5]; 2868 2869 for (short row = 0; row < NR0; row++) { 2870 device const block_iq4_nl & xb = x[row*ns01 + ib]; 2871 device const uint16_t * q4 = (device const uint16_t *)(xb.qs + 8*it); 2872 2873 float4 acc1 = {0.f}, acc2 = {0.f}; 2874 2875 aux32[0] = q4[0] | (q4[1] << 16); 2876 aux32[1] = (aux32[0] >> 4) & 0x0f0f0f0f; 2877 aux32[0] &= 0x0f0f0f0f; 2878 qf1 = {shmem_f32[q8[0]], shmem_f32[q8[1]], shmem_f32[q8[2]], shmem_f32[q8[3]]}; 2879 qf2 = {shmem_f32[q8[4]], shmem_f32[q8[5]], shmem_f32[q8[6]], shmem_f32[q8[7]]}; 2880 acc1 += yl[0] * qf1; 2881 acc2 += yl[1] * qf2; 2882 2883 aux32[0] = q4[2] | (q4[3] << 16); 2884 aux32[1] = (aux32[0] >> 4) & 0x0f0f0f0f; 2885 aux32[0] &= 0x0f0f0f0f; 2886 qf1 = {shmem_f32[q8[0]], shmem_f32[q8[1]], shmem_f32[q8[2]], shmem_f32[q8[3]]}; 2887 qf2 = {shmem_f32[q8[4]], shmem_f32[q8[5]], shmem_f32[q8[6]], shmem_f32[q8[7]]}; 2888 acc1 += yl[2] * qf1; 2889 acc2 += yl[3] * qf2; 2890 2891 acc1 += acc2; 2892 2893 sumf[row] += (float)xb.d * (acc1[0] + acc1[1] + acc1[2] + acc1[3]); 2894 } 2895 2896 yb += 16 * QK4_NL; 2897 } 2898 2899 device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0; 2900 2901 for (int row = 0; row < NR0 && first_row + row < args.ne0; ++row) { 2902 float sum_all = simd_sum(sumf[row]); 2903 if (tiisg == 0) { 2904 dst_f32[first_row + row] = sum_all; 2905 } 2906 } 2907 } 2908 2909 [[host_name("kernel_mul_mv_iq4_nl_f32")]] 2910 kernel void kernel_mul_mv_iq4_nl_f32( 2911 constant ggml_metal_kargs_mul_mv & args, 2912 device const char * src0, 2913 device const char * src1, 2914 device char * dst, 2915 threadgroup char * shmem [[threadgroup(0)]], 2916 uint3 tgpig[[threadgroup_position_in_grid]], 2917 ushort tiisg[[thread_index_in_simdgroup]], 2918 ushort sgitg[[simdgroup_index_in_threadgroup]]) { 2919 2920 kernel_mul_mv_iq4_nl_f32_impl(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg); 2921 } 2922 2923 template 2924 void kernel_mul_mv_iq4_xs_f32_impl( 2925 args_t args, 2926 device const char * src0, 2927 device const char * src1, 2928 device char * dst, 2929 threadgroup char * shmem, 2930 uint3 tgpig, 2931 ushort tiisg, 2932 ushort sgitg) { 2933 const short NSG = FC_mul_mv_nsg; 2934 2935 threadgroup float * shmem_f32 = (threadgroup float *) shmem; 2936 2937 const int r0 = tgpig.x; 2938 const int r1 = tgpig.y; 2939 const int im = tgpig.z; 2940 const int first_row = (r0 * NSG + sgitg) * NR0; 2941 2942 const uint i12 = im%FC_mul_mv_ne12; 2943 const uint i13 = im/FC_mul_mv_ne12; 2944 2945 const uint64_t offset0 = first_row*args.nb01 + (i12/FC_mul_mv_r2)*args.nb02 + (i13/FC_mul_mv_r3)*args.nb03; 2946 const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13; 2947 2948 device const block_iq4_xs * x = (device const block_iq4_xs *) (src0 + offset0); 2949 device const float * y = (device const float *) (src1 + offset1); 2950 2951 const int nb = args.ne00/QK_K; 2952 const int ns01 = args.nb01/args.nb00; 2953 2954 const short ix = tiisg/16; // 0 or 1 2955 const short it = tiisg%16; // 0...15 2956 const short ib = it/2; 2957 const short il = it%2; 2958 2959 shmem_f32[tiisg] = kvalues_iq4nl_f[tiisg%16]; 2960 threadgroup_barrier(mem_flags::mem_threadgroup); 2961 2962 float4 yl[4]; 2963 float sumf[NR0]={0.f}; 2964 2965 device const float * yb = y + ix * QK_K + ib * 32 + il * 8; 2966 2967 uint32_t aux32[2]; 2968 thread const uint8_t * q8 = (thread const uint8_t *)aux32; 2969 2970 float4 qf1, qf2; 2971 2972 // [TAG_MUL_MV_WEIRD] 2973 for (int ibl = ix; ibl < nb && ibl < ns01; ibl += 2) { 2974 device const float4 * y4 = (device const float4 *)yb; 2975 yl[0] = y4[0]; 2976 yl[1] = y4[4]; 2977 yl[2] = y4[1]; 2978 yl[3] = y4[5]; 2979 2980 for (short row = 0; row < NR0; ++row) { 2981 device const block_iq4_xs & xb = x[row*ns01 + ibl]; 2982 device const uint32_t * q4 = (device const uint32_t *)(xb.qs + 16*ib + 8*il); 2983 2984 float4 acc1 = {0.f}, acc2 = {0.f}; 2985 2986 aux32[0] = (q4[0] ) & 0x0f0f0f0f; 2987 aux32[1] = (q4[0] >> 4) & 0x0f0f0f0f; 2988 qf1 = {shmem_f32[q8[0]], shmem_f32[q8[1]], shmem_f32[q8[2]], shmem_f32[q8[3]]}; 2989 qf2 = {shmem_f32[q8[4]], shmem_f32[q8[5]], shmem_f32[q8[6]], shmem_f32[q8[7]]}; 2990 acc1 += yl[0] * qf1; 2991 acc2 += yl[1] * qf2; 2992 2993 aux32[0] = (q4[1] ) & 0x0f0f0f0f; 2994 aux32[1] = (q4[1] >> 4) & 0x0f0f0f0f; 2995 qf1 = {shmem_f32[q8[0]], shmem_f32[q8[1]], shmem_f32[q8[2]], shmem_f32[q8[3]]}; 2996 qf2 = {shmem_f32[q8[4]], shmem_f32[q8[5]], shmem_f32[q8[6]], shmem_f32[q8[7]]}; 2997 acc1 += yl[2] * qf1; 2998 acc2 += yl[3] * qf2; 2999 3000 acc1 += acc2; 3001 3002 const int ls = (((xb.scales_l[ib/2] >> 4*(ib%2)) & 0xf) | (((xb.scales_h >> 2*ib) & 3) << 4)) - 32; 3003 sumf[row] += (float)xb.d * ls * (acc1[0] + acc1[1] + acc1[2] + acc1[3]); 3004 } 3005 3006 yb += 2 * QK_K; 3007 } 3008 3009 device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0; 3010 3011 for (int row = 0; row < NR0 && first_row + row < args.ne0; ++row) { 3012 float sum_all = simd_sum(sumf[row]); 3013 if (tiisg == 0) { 3014 dst_f32[first_row + row] = sum_all; 3015 } 3016 } 3017 } 3018 3019 [[host_name("kernel_mul_mv_iq4_xs_f32")]] 3020 kernel void kernel_mul_mv_iq4_xs_f32( 3021 constant ggml_metal_kargs_mul_mv & args, 3022 device const char * src0, 3023 device const char * src1, 3024 device char * dst, 3025 threadgroup char * shmem [[threadgroup(0)]], 3026 uint3 tgpig[[threadgroup_position_in_grid]], 3027 ushort tiisg[[thread_index_in_simdgroup]], 3028 ushort sgitg[[simdgroup_index_in_threadgroup]]) { 3029 3030 kernel_mul_mv_iq4_xs_f32_impl(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg); 3031 } 3032 3033 // decode the E2M1 value in the low nibble of n, scaled by 2^-14, without a lookup table: the 3 magnitude bits 3034 // land in the fp16 exponent/mantissa (e=0 gives the 0.5 subnormal). same decode as MLX's fp4_e2m1 3035 static inline half mxfp4_dq1(uint32_t n) { 3036 const half c = as_type(ushort((n & 7) << 9)); 3037 return (n & 8) ? -c : c; 3038 } 3039 3040 // low / high nibbles of the 4 bytes of w, in byte order, scaled by 2^-14 3041 static inline float4 mxfp4_lo4(uint32_t w) { 3042 return float4(mxfp4_dq1(w), mxfp4_dq1(w >> 8), mxfp4_dq1(w >> 16), mxfp4_dq1(w >> 24)); 3043 } 3044 3045 static inline float4 mxfp4_hi4(uint32_t w) { 3046 return float4(mxfp4_dq1(w >> 4), mxfp4_dq1(w >> 12), mxfp4_dq1(w >> 20), mxfp4_dq1(w >> 28)); 3047 } 3048 3049 template 3050 void kernel_mul_mv_mxfp4_f32_impl( 3051 args_t args, 3052 device const char * src0, 3053 device const char * src1, 3054 device char * dst, 3055 threadgroup char * shmem, 3056 uint3 tgpig, 3057 ushort tiisg, 3058 ushort sgitg) { 3059 const short NSG = FC_mul_mv_nsg; 3060 3061 const int r0 = tgpig.x; 3062 const int r1 = tgpig.y; 3063 const int im = tgpig.z; 3064 3065 const int first_row = (r0 * NSG + sgitg) * NR0; 3066 3067 const uint i12 = im%FC_mul_mv_ne12; 3068 const uint i13 = im/FC_mul_mv_ne12; 3069 3070 const uint64_t offset0 = first_row*args.nb01 + (i12/FC_mul_mv_r2)*args.nb02 + (i13/FC_mul_mv_r3)*args.nb03; 3071 const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13; 3072 3073 device const block_mxfp4 * x = (device const block_mxfp4 *) (src0 + offset0); 3074 device const float * y = (device const float *) (src1 + offset1); 3075 3076 const int nb = args.ne00/QK_MXFP4; 3077 const int ns01 = args.nb01/args.nb00; // this can be larger than nb for permuted src0 tensors 3078 3079 const short ix = tiisg/2; // 0...15 3080 const short it = tiisg%2; // 0 or 1 3081 3082 float4 yl[4]; 3083 float sumf[NR0]={0.f}; 3084 3085 device const float * yb = y + ix*QK_MXFP4 + it*8; 3086 3087 // note: just the check `ib < nb` is enough, but adding the redundant `&& ib < ns01` check makes the kernel a bit faster 3088 // no idea why that is - needs some deeper investigation [TAG_MUL_MV_WEIRD] 3089 for (int ib = ix; ib < nb && ib < ns01; ib += 16) { 3090 device const float4 * y4 = (device const float4 *) yb; 3091 3092 yl[0] = y4[0]; 3093 yl[1] = y4[4]; 3094 yl[2] = y4[1]; 3095 yl[3] = y4[5]; 3096 3097 FOR_UNROLL (short row = 0; row < NR0; row++) { 3098 device const block_mxfp4 & xb = x[row*ns01 + ib]; 3099 device const uint8_t * q2 = (device const uint8_t *)(xb.qs + 8*it); 3100 3101 // blocks are 17 bytes, so the 8 quant bytes are only byte aligned: read the aligned words that cover them 3102 // and funnel-shift. the third word is read only when needed so no word past the block end is touched 3103 device const uint32_t * pw = (device const uint32_t *)((ulong) q2 & ~3ul); 3104 const uint sh = ((uint)(ulong) q2 & 3u)*8; 3105 const uint32_t a = pw[0]; 3106 const uint32_t b = pw[1]; 3107 const uint32_t c = pw[sh ? 2 : 1]; 3108 const uint32_t w0 = (a >> sh) | ((b << 1) << (31 - sh)); 3109 const uint32_t w1 = (b >> sh) | ((c << 1) << (31 - sh)); 3110 3111 float4 acc1 = yl[0]*mxfp4_lo4(w0); 3112 float4 acc2 = yl[1]*mxfp4_hi4(w0); 3113 float4 acc3 = yl[2]*mxfp4_lo4(w1); 3114 float4 acc4 = yl[3]*mxfp4_hi4(w1); 3115 3116 acc1 = (acc1 + acc3) + (acc2 + acc4); 3117 3118 // 16384 undoes the 2^-14 of the decode 3119 sumf[row] += (e8m0_to_fp32(xb.e)*16384.0f) * ((acc1[0] + acc1[1]) + (acc1[2] + acc1[3])); 3120 } 3121 3122 yb += 16 * QK_MXFP4; 3123 } 3124 3125 device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0; 3126 3127 for (int row = 0; row < NR0 && first_row + row < args.ne0; ++row) { 3128 float sum_all = simd_sum(sumf[row]); 3129 if (tiisg == 0) { 3130 dst_f32[first_row + row] = sum_all; 3131 } 3132 } 3133 } 3134 3135 [[host_name("kernel_mul_mv_mxfp4_f32")]] 3136 kernel void kernel_mul_mv_mxfp4_f32( 3137 constant ggml_metal_kargs_mul_mv & args, 3138 device const char * src0, 3139 device const char * src1, 3140 device char * dst, 3141 threadgroup char * shmem [[threadgroup(0)]], 3142 uint3 tgpig[[threadgroup_position_in_grid]], 3143 ushort tiisg[[thread_index_in_simdgroup]], 3144 ushort sgitg[[simdgroup_index_in_threadgroup]]) { 3145 3146 kernel_mul_mv_mxfp4_f32_impl(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg); 3147 } 3148 3149 template 3150 void kernel_mul_mv_tq2_0_f32_impl( 3151 args_t args, 3152 device const char * src0, 3153 device const char * src1, 3154 device char * dst, 3155 threadgroup char * shmem, 3156 uint3 tgpig, 3157 ushort tiisg, 3158 ushort sgitg) { 3159 const short NSG = FC_mul_mv_nsg; 3160 3161 const int nb = args.ne00/QK_K; 3162 3163 const int r0 = tgpig.x; 3164 const int r1 = tgpig.y; 3165 const int im = tgpig.z; 3166 3167 const int first_row = (r0 * NSG + sgitg) * nr0; 3168 3169 const uint i12 = im%FC_mul_mv_ne12; 3170 const uint i13 = im/FC_mul_mv_ne12; 3171 3172 const uint64_t offset1 = r1*args.nb11 + (i12 )*args.nb12 + (i13 )*args.nb13; 3173 3174 device const float * y = (device const float *) (src1 + offset1); 3175 3176 device const block_tq2_0 * ax[nr0]; 3177 for (int row = 0; row < nr0; ++row) { 3178 const uint64_t offset0 = (first_row + row)*args.nb01 + (i12/FC_mul_mv_r2)*args.nb02 + (i13/FC_mul_mv_r3)*args.nb03; 3179 ax[row] = (device const block_tq2_0 *) ((device char *) src0 + offset0); 3180 } 3181 3182 float sumf[nr0] = {0.f}; 3183 3184 // 8 threads per block, NBLOCK blocks per pass, 2 halves per block per pass 3185 constexpr short NBLOCK = 4; 3186 3187 constexpr short NB = N_SIMDWIDTH/NBLOCK; // threads per block 3188 3189 const short blk = tiisg / NB; // 0..NBLOCK-1, block handled by this thread 3190 const short htg = tiisg % NB; // 0..NB-1, thread within block (0..7) 3191 3192 // byte and y base offsets within the block (32 elements per thread, 4 per byte) 3193 device const float4 * yb4 = (device const float4 *)(y + 4*htg + blk*QK_K); 3194 3195 // hoisted per-byte coefficients (from y) and total y-sum, shared across rows 3196 // ref: https://github.com/ggml-org/llama.cpp/pull/26980 3197 float4 coef[4]; 3198 3199 for (int ib = blk; ib < nb; ib += NBLOCK) { 3200 FOR_UNROLL (short h0 = 0; h0 < 2; ++h0) { 3201 const float4 y0 = yb4[ 0 + 32*h0]; 3202 const float4 y1 = yb4[ 8 + 32*h0]; 3203 const float4 y2 = yb4[16 + 32*h0]; 3204 const float4 y3 = yb4[24 + 32*h0]; 3205 3206 float sumy = 0.f; 3207 FOR_UNROLL (short j = 0; j < 4; ++j) { 3208 coef[j] = float4( 3209 y0[j], 3210 y1[j] - 4.0f*y0[j], 3211 y2[j] - 4.0f*y1[j], 3212 y3[j] - 4.0f*y2[j]); 3213 3214 sumy += (y0[j] + y1[j]) + (y2[j] + y3[j]); 3215 } 3216 3217 FOR_UNROLL (short row = 0; row < nr0; ++row) { 3218 device const block_tq2_0 & xb = ax[row][ib]; 3219 device const uchar * qs = xb.qs + 4*htg + 32*h0; 3220 3221 float sum = -sumy; 3222 FOR_UNROLL (short j = 0; j < 4; ++j) { 3223 // express the 2-bit field shifts (v>>2, v>>4, v>>6) as float floor ops 3224 const float v = (float)qs[j]; 3225 3226 const float f0 = v; 3227 const float f1 = floor(v*0.25f); // v>>2 3228 const float f2 = floor(v*0.0625); // v>>4 3229 const float f3 = floor(v*0.015625); // v>>6 3230 3231 sum += coef[j][0]*f0 + coef[j][1]*f1 + coef[j][2]*f2 + coef[j][3]*f3; 3232 } 3233 3234 sumf[row] += xb.d * sum; 3235 } 3236 } 3237 3238 yb4 += QK_K * NBLOCK / 4; 3239 } 3240 3241 device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0; 3242 3243 for (int row = 0; row < nr0; ++row) { 3244 const float tot = simd_sum(sumf[row]); 3245 if (tiisg == 0 && first_row + row < args.ne01) { 3246 dst_f32[first_row + row] = tot; 3247 } 3248 } 3249 } 3250 3251 [[host_name("kernel_mul_mv_tq2_0_f32")]] 3252 kernel void kernel_mul_mv_tq2_0_f32( 3253 constant ggml_metal_kargs_mul_mv & args, 3254 device const char * src0, 3255 device const char * src1, 3256 device char * dst, 3257 uint3 tgpig[[threadgroup_position_in_grid]], 3258 ushort tiisg[[thread_index_in_simdgroup]], 3259 ushort sgitg[[simdgroup_index_in_threadgroup]]) { 3260 3261 kernel_mul_mv_tq2_0_f32_impl(args, src0, src1, dst, nullptr, tgpig, tiisg, sgitg); 3262 } 3263 3264 // 3265 // matrix-vector multiplication 3266 // 3267 3268 typedef void (kernel_mul_mv_disp_t)( 3269 ggml_metal_kargs_mul_mv args, 3270 device const char * src0, 3271 device const char * src1, 3272 device char * dst, 3273 uint3 tgpig, 3274 ushort tiisg); 3275 3276 typedef void (kernel_mul_mv2_disp_t)( 3277 ggml_metal_kargs_mul_mv args, 3278 device const char * src0, 3279 device const char * src1, 3280 device char * dst, 3281 threadgroup char * shmem, 3282 uint3 tgpig, 3283 ushort tiisg, 3284 ushort sgitg); 3285 3286 template 3287 void mmv_fn( 3288 ggml_metal_kargs_mul_mv args, 3289 device const char * src0, 3290 device const char * src1, 3291 device char * dst, 3292 threadgroup char * shmem, 3293 uint3 tgpig, 3294 ushort tiitg, 3295 ushort tiisg, 3296 ushort sgitg) { 3297 disp_fn(args, src0, src1, dst, tgpig, tiisg); 3298 } 3299 3300 template 3301 void mmv_fn( 3302 ggml_metal_kargs_mul_mv args, 3303 device const char * src0, 3304 device const char * src1, 3305 device char * dst, 3306 threadgroup char * shmem, 3307 uint3 tgpig, 3308 ushort tiitg, 3309 ushort tiisg, 3310 ushort sgitg) { 3311 disp_fn(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg); 3312 } 3313 3314 typedef decltype(mmv_fn>) mul_mv_disp_fn_t; 3315 3316 template 3317 kernel void kernel_mul_mv_id( 3318 constant ggml_metal_kargs_mul_mv_id & args, 3319 device const char * src0s, 3320 device const char * src1, 3321 device char * dst, 3322 device const char * ids, 3323 threadgroup char * shmem [[threadgroup(0)]], 3324 uint3 tgpig[[threadgroup_position_in_grid]], 3325 ushort tiitg[[thread_index_in_threadgroup]], 3326 ushort tiisg[[thread_index_in_simdgroup]], 3327 ushort sgitg[[simdgroup_index_in_threadgroup]], 3328 ushort3 ntg [[threads_per_threadgroup]]) { 3329 const int iid1 = tgpig.z/args.nei0; 3330 const int idx = tgpig.z%args.nei0; 3331 3332 tgpig.z = 0; 3333 3334 const int32_t i02 = ((device const int32_t *) (ids + iid1*args.nbi1))[idx]; 3335 3336 const int64_t i11 = idx % args.ne11; 3337 const int64_t i12 = iid1; 3338 3339 const int64_t i1 = idx; 3340 const int64_t i2 = i12; 3341 3342 device char * dst_cur = dst + (i1*args.ne0 + i2*args.ne1*args.ne0)*sizeof(float); 3343 3344 if (i02 == -1) { 3345 device float * dst_f32 = (device float *) dst_cur; 3346 const ushort nth = ntg.x*ntg.y*ntg.z; 3347 for (int i0 = tiitg; i0 < args.ne0; i0 += nth) { 3348 dst_f32[i0] = 0.0f; 3349 } 3350 return; 3351 } 3352 3353 device const char * src0_cur = src0s + i02*args.nb02; 3354 device const char * src1_cur = src1 + i11*args.nb11 + i12*args.nb12; 3355 3356 ggml_metal_kargs_mul_mv args0 = { 3357 /*.ne00 =*/ args.ne00, 3358 /*.ne01 =*/ args.ne01, 3359 /*.ne02 =*/ 1, // args.ne02, 3360 /*.nb00 =*/ args.nb00, 3361 /*.nb01 =*/ args.nb01, 3362 /*.nb02 =*/ args.nb02, 3363 /*.nb03 =*/ args.nb02, // args.ne02 == 1 3364 /*.ne10 =*/ args.ne10, 3365 /*.ne11 =*/ 1, // args.ne11, 3366 /*.ne12 =*/ 1, // args.ne12, 3367 /*.nb10 =*/ args.nb10, 3368 /*.nb11 =*/ args.nb11, 3369 /*.nb12 =*/ args.nb12, 3370 /*.nb13 =*/ args.nb12, // ne12 == 1 3371 /*.ne0 =*/ args.ne0, 3372 /*.ne1 =*/ 1, // args.ne1, 3373 /*.nr0 =*/ args.nr0, 3374 /*.r2 =*/ 1, 3375 /*.r3 =*/ 1, 3376 }; 3377 3378 disp_fn( 3379 args0, 3380 /* src0 */ src0_cur, 3381 /* src1 */ src1_cur, 3382 /* dst */ dst_cur, 3383 shmem, 3384 tgpig, 3385 tiitg, 3386 tiisg, 3387 sgitg); 3388 } 3389 3390 typedef decltype(kernel_mul_mv_id>>) kernel_mul_mv_id_t; 3391 3392 typedef decltype(kernel_mul_mv_id>>) kernel_mul_mv_id_4_t; 3393 3394 template [[host_name("kernel_mul_mv_id_f32_f32")]] kernel kernel_mul_mv_id_t kernel_mul_mv_id>>; 3395 template [[host_name("kernel_mul_mv_id_f16_f32")]] kernel kernel_mul_mv_id_t kernel_mul_mv_id>>; 3396 #if defined(GGML_METAL_HAS_BF16) 3397 template [[host_name("kernel_mul_mv_id_bf16_f32")]] kernel kernel_mul_mv_id_t kernel_mul_mv_id>>; 3398 #endif 3399 template [[host_name("kernel_mul_mv_id_f32_f32_4")]] kernel kernel_mul_mv_id_4_t kernel_mul_mv_id>>; 3400 template [[host_name("kernel_mul_mv_id_f16_f32_4")]] kernel kernel_mul_mv_id_4_t kernel_mul_mv_id>>; 3401 #if defined(GGML_METAL_HAS_BF16) 3402 template [[host_name("kernel_mul_mv_id_bf16_f32_4")]] kernel kernel_mul_mv_id_4_t kernel_mul_mv_id>>; 3403 #endif 3404 3405 template [[host_name("kernel_mul_mv_id_q8_0_f32")]] kernel kernel_mul_mv_id_t kernel_mul_mv_id>>; 3406 3407 template [[host_name("kernel_mul_mv_id_q1_0_f32")]] kernel kernel_mul_mv_id_t kernel_mul_mv_id>>; 3408 template [[host_name("kernel_mul_mv_id_q2_0_f32")]] kernel kernel_mul_mv_id_t kernel_mul_mv_id>>; 3409 template [[host_name("kernel_mul_mv_id_q4_0_f32")]] kernel kernel_mul_mv_id_t kernel_mul_mv_id>>; 3410 template [[host_name("kernel_mul_mv_id_q4_1_f32")]] kernel kernel_mul_mv_id_t kernel_mul_mv_id>>; 3411 template [[host_name("kernel_mul_mv_id_q5_0_f32")]] kernel kernel_mul_mv_id_t kernel_mul_mv_id>>; 3412 template [[host_name("kernel_mul_mv_id_q5_1_f32")]] kernel kernel_mul_mv_id_t kernel_mul_mv_id>>; 3413 3414 template [[host_name("kernel_mul_mv_id_mxfp4_f32")]] kernel kernel_mul_mv_id_t kernel_mul_mv_id>>; 3415 3416 template [[host_name("kernel_mul_mv_id_q2_K_f32")]] kernel kernel_mul_mv_id_t kernel_mul_mv_id>>; 3417 template [[host_name("kernel_mul_mv_id_q3_K_f32")]] kernel kernel_mul_mv_id_t kernel_mul_mv_id>>; 3418 template [[host_name("kernel_mul_mv_id_q4_K_f32")]] kernel kernel_mul_mv_id_t kernel_mul_mv_id>>; 3419 template [[host_name("kernel_mul_mv_id_q5_K_f32")]] kernel kernel_mul_mv_id_t kernel_mul_mv_id>>; 3420 template [[host_name("kernel_mul_mv_id_q6_K_f32")]] kernel kernel_mul_mv_id_t kernel_mul_mv_id>>; 3421 template [[host_name("kernel_mul_mv_id_iq1_s_f32")]] kernel kernel_mul_mv_id_t kernel_mul_mv_id>>; 3422 template [[host_name("kernel_mul_mv_id_iq1_m_f32")]] kernel kernel_mul_mv_id_t kernel_mul_mv_id>>; 3423 template [[host_name("kernel_mul_mv_id_iq2_xxs_f32")]] kernel kernel_mul_mv_id_t kernel_mul_mv_id>>; 3424 template [[host_name("kernel_mul_mv_id_iq2_xs_f32")]] kernel kernel_mul_mv_id_t kernel_mul_mv_id>>; 3425 template [[host_name("kernel_mul_mv_id_iq3_xxs_f32")]] kernel kernel_mul_mv_id_t kernel_mul_mv_id>>; 3426 template [[host_name("kernel_mul_mv_id_iq3_s_f32")]] kernel kernel_mul_mv_id_t kernel_mul_mv_id>>; 3427 template [[host_name("kernel_mul_mv_id_iq2_s_f32")]] kernel kernel_mul_mv_id_t kernel_mul_mv_id>>; 3428 template [[host_name("kernel_mul_mv_id_iq4_nl_f32")]] kernel kernel_mul_mv_id_t kernel_mul_mv_id>>; 3429 template [[host_name("kernel_mul_mv_id_iq4_xs_f32")]] kernel kernel_mul_mv_id_t kernel_mul_mv_id>>; 3430 template [[host_name("kernel_mul_mv_id_tq2_0_f32")]] kernel kernel_mul_mv_id_t kernel_mul_mv_id>>;