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/ggml-metal-device.cpp # 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 / ggml-metal-device.cpp` 2801 lines ยท 94530 bytes Raw Blame History 1 #include "ggml-metal-device.h" 2 #include "ggml-metal-common.h" 3 4 #include "ggml-metal-impl.h" 5 #include "ggml-metal-tuning.h" 6 7 #include "ggml-impl.h" 8 9 #include 10 #include 11 #include 12 #include 13 14 struct ggml_metal_device_deleter { 15 void operator()(ggml_metal_device_t ctx) { 16 ggml_metal_device_free(ctx); 17 } 18 }; 19 20 typedef std::unique_ptr ggml_metal_device_ptr; 21 22 ggml_metal_device_t ggml_metal_device_get(int device, int n_devices) { 23 static std::vector devs; 24 25 devs.emplace_back(ggml_metal_device_init(device, n_devices)); 26 27 return devs.back().get(); 28 } 29 30 struct ggml_metal_pipelines { 31 std::unordered_map data; 32 }; 33 34 ggml_metal_pipelines_t ggml_metal_pipelines_init(void) { 35 ggml_metal_pipelines_t res = new ggml_metal_pipelines(); 36 37 return res; 38 } 39 40 void ggml_metal_pipelines_free(ggml_metal_pipelines_t ppls) { 41 if (!ppls) { 42 return; 43 } 44 45 for (auto it = ppls->data.begin(); it != ppls->data.end(); ++it) { 46 ggml_metal_pipeline_free(it->second); 47 } 48 49 delete ppls; 50 } 51 52 void ggml_metal_pipelines_add(ggml_metal_pipelines_t ppls, const char * name, ggml_metal_pipeline_t pipeline) { 53 ppls->data[name] = pipeline; 54 } 55 56 ggml_metal_pipeline_t ggml_metal_pipelines_get(ggml_metal_pipelines_t ppls, const char * name) { 57 if (ppls->data.find(name) == ppls->data.end()) { 58 return nullptr; 59 } 60 61 return ppls->data[name]; 62 } 63 64 struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_base(ggml_metal_library_t lib, ggml_op op) { 65 char base[256]; 66 char name[256]; 67 68 const char * op_str = "undefined"; 69 switch (op) { 70 case GGML_OP_ADD_ID: op_str = "add_id"; break; 71 default: GGML_ABORT("fatal error"); 72 }; 73 74 snprintf(base, 256, "kernel_%s", op_str); 75 snprintf(name, 256, "%s", base); 76 77 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 78 if (!res.pipeline) { 79 res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr); 80 } 81 82 return res; 83 } 84 85 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_cpy(ggml_metal_library_t lib, ggml_type tsrc, ggml_type tdst) { 86 char base[256]; 87 char name[256]; 88 89 snprintf(base, 256, "kernel_cpy_%s_%s", ggml_type_name(tsrc), ggml_type_name(tdst)); 90 snprintf(name, 256, "%s", base); 91 92 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 93 if (!res.pipeline) { 94 res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr); 95 } 96 97 return res; 98 } 99 100 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_pool_1d(ggml_metal_library_t lib, const ggml_tensor * op, ggml_op_pool op_pool) { 101 GGML_ASSERT(ggml_is_contiguous(op->src[0])); 102 GGML_ASSERT(op->src[0]->type == GGML_TYPE_F32 && op->src[0]->type == op->type); 103 104 const char * pool_str = "undefined"; 105 switch (op_pool) { 106 case GGML_OP_POOL_AVG: pool_str = "avg"; break; 107 case GGML_OP_POOL_MAX: pool_str = "max"; break; 108 default: GGML_ASSERT(false && "not implemented"); 109 }; 110 111 char base[256]; 112 char name[256]; 113 114 snprintf(base, sizeof(base), "kernel_pool_1d_%s_%s", pool_str, ggml_type_name(op->src[0]->type)); 115 snprintf(name, sizeof(name), "%s", base); 116 117 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 118 if (!res.pipeline) { 119 res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr); 120 } 121 122 return res; 123 } 124 125 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_pool_2d(ggml_metal_library_t lib, const ggml_tensor * op, ggml_op_pool op_pool) { 126 GGML_ASSERT(ggml_is_contiguous(op->src[0])); 127 GGML_ASSERT(op->src[0]->type == GGML_TYPE_F32 && op->src[0]->type == op->type); 128 129 const char * pool_str = "undefined"; 130 switch (op_pool) { 131 case GGML_OP_POOL_AVG: pool_str = "avg"; break; 132 case GGML_OP_POOL_MAX: pool_str = "max"; break; 133 default: GGML_ASSERT(false && "not implemented"); 134 }; 135 136 char base[256]; 137 char name[256]; 138 139 snprintf(base, 256, "kernel_pool_2d_%s_%s", pool_str, ggml_type_name(op->src[0]->type)); 140 snprintf(name, 256, "%s", base); 141 142 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 143 if (!res.pipeline) { 144 res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr); 145 } 146 147 return res; 148 } 149 150 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_get_rows(ggml_metal_library_t lib, ggml_type tsrc) { 151 char base[256]; 152 char name[256]; 153 154 snprintf(base, 256, "kernel_get_rows_%s", ggml_type_name(tsrc)); 155 snprintf(name, 256, "%s", base); 156 157 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 158 if (!res.pipeline) { 159 res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr); 160 } 161 162 return res; 163 } 164 165 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_set_rows(ggml_metal_library_t lib, const ggml_tensor * op) { 166 char base[256]; 167 char name[256]; 168 169 const auto tsrc = op->src[0]->type; 170 const auto tidx = op->src[1]->type; 171 const auto tdst = op->type; 172 173 snprintf(base, 256, "kernel_set_rows_%s_%s_%s", ggml_type_name(tsrc), ggml_type_name(tidx), ggml_type_name(tdst)); 174 snprintf(name, 256, "%s", base); 175 176 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 177 if (!res.pipeline) { 178 res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr); 179 } 180 181 return res; 182 } 183 184 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_diag(ggml_metal_library_t lib, const ggml_tensor * op) { 185 char base[256]; 186 char name[256]; 187 188 const int n = op->src[0]->ne[0]; 189 190 snprintf(base, 256, "kernel_diag_%s", ggml_type_name(op->src[0]->type)); 191 snprintf(name, 256, "%s_n=%d", base, n); 192 193 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 194 if (!res.pipeline) { 195 res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr); 196 } 197 198 res.nsg = 1; 199 res.smem = 0; 200 201 return res; 202 } 203 204 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_repeat(ggml_metal_library_t lib, ggml_type tsrc) { 205 char base[256]; 206 char name[256]; 207 208 snprintf(base, 256, "kernel_repeat_%s", ggml_type_name(tsrc)); 209 snprintf(name, 256, "%s", base); 210 211 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 212 if (!res.pipeline) { 213 res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr); 214 } 215 216 return res; 217 } 218 219 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_concat(ggml_metal_library_t lib, ggml_type tsrc) { 220 char base[256]; 221 char name[256]; 222 223 snprintf(base, 256, "kernel_concat_%s", ggml_type_name(tsrc)); 224 snprintf(name, 256, "%s", base); 225 226 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 227 if (!res.pipeline) { 228 res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr); 229 } 230 231 return res; 232 } 233 234 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_unary(ggml_metal_library_t lib, const ggml_tensor * op) { 235 char base[256]; 236 char name[256]; 237 238 int op_num = -1; 239 240 switch (op->op) { 241 case GGML_OP_SCALE: op_num = OP_UNARY_NUM_SCALE; break; 242 case GGML_OP_FILL: op_num = OP_UNARY_NUM_FILL; break; 243 case GGML_OP_CLAMP: op_num = OP_UNARY_NUM_CLAMP; break; 244 case GGML_OP_SQR: op_num = OP_UNARY_NUM_SQR; break; 245 case GGML_OP_SQRT: op_num = OP_UNARY_NUM_SQRT; break; 246 case GGML_OP_SIN: op_num = OP_UNARY_NUM_SIN; break; 247 case GGML_OP_COS: op_num = OP_UNARY_NUM_COS; break; 248 case GGML_OP_LOG: op_num = OP_UNARY_NUM_LOG; break; 249 case GGML_OP_LEAKY_RELU: op_num = OP_UNARY_NUM_LEAKY_RELU; break; 250 case GGML_OP_UNARY: 251 switch (ggml_get_unary_op(op)) { 252 case GGML_UNARY_OP_TANH: op_num = OP_UNARY_NUM_TANH; break; 253 case GGML_UNARY_OP_RELU: op_num = OP_UNARY_NUM_RELU; break; 254 case GGML_UNARY_OP_SIGMOID: op_num = OP_UNARY_NUM_SIGMOID; break; 255 case GGML_UNARY_OP_GELU: op_num = OP_UNARY_NUM_GELU; break; 256 case GGML_UNARY_OP_GELU_ERF: op_num = OP_UNARY_NUM_GELU_ERF; break; 257 case GGML_UNARY_OP_GELU_QUICK: op_num = OP_UNARY_NUM_GELU_QUICK; break; 258 case GGML_UNARY_OP_SILU: op_num = OP_UNARY_NUM_SILU; break; 259 case GGML_UNARY_OP_ELU: op_num = OP_UNARY_NUM_ELU; break; 260 case GGML_UNARY_OP_NEG: op_num = OP_UNARY_NUM_NEG; break; 261 case GGML_UNARY_OP_ABS: op_num = OP_UNARY_NUM_ABS; break; 262 case GGML_UNARY_OP_SGN: op_num = OP_UNARY_NUM_SGN; break; 263 case GGML_UNARY_OP_STEP: op_num = OP_UNARY_NUM_STEP; break; 264 case GGML_UNARY_OP_HARDSWISH: op_num = OP_UNARY_NUM_HARDSWISH; break; 265 case GGML_UNARY_OP_HARDSIGMOID: op_num = OP_UNARY_NUM_HARDSIGMOID; break; 266 case GGML_UNARY_OP_EXP: op_num = OP_UNARY_NUM_EXP; break; 267 case GGML_UNARY_OP_SOFTPLUS: op_num = OP_UNARY_NUM_SOFTPLUS; break; 268 case GGML_UNARY_OP_EXPM1: op_num = OP_UNARY_NUM_EXPM1; break; 269 case GGML_UNARY_OP_FLOOR: op_num = OP_UNARY_NUM_FLOOR; break; 270 case GGML_UNARY_OP_CEIL: op_num = OP_UNARY_NUM_CEIL; break; 271 case GGML_UNARY_OP_ROUND: op_num = OP_UNARY_NUM_ROUND; break; 272 case GGML_UNARY_OP_TRUNC: op_num = OP_UNARY_NUM_TRUNC; break; 273 case GGML_UNARY_OP_XIELU: op_num = OP_UNARY_NUM_XIELU; break; 274 default: GGML_ABORT("fatal error"); 275 } break; 276 default: GGML_ABORT("fatal error"); 277 }; 278 279 const char * t0_str = ggml_type_name(op->src[0]->type); 280 const char * t_str = ggml_type_name(op->type); 281 282 const bool is_c4 = op->src[0]->ne[0] % 4 == 0; 283 const bool is_cnt = ggml_is_contiguous(op->src[0]) && ggml_nelements(op) < 32768; 284 285 snprintf(base, 256, "kernel_unary_%s_%s%s", t0_str, t_str, is_c4 ? "_4" : ""); 286 snprintf(name, 256, "%s_op=%d_cnt=%d", base, op_num, is_cnt); 287 288 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 289 if (!res.pipeline) { 290 ggml_metal_cv_t cv = ggml_metal_cv_init(); 291 292 ggml_metal_cv_set_int16(cv, op_num, FC_UNARY + 0); 293 ggml_metal_cv_set_bool (cv, is_cnt, FC_UNARY + 1); 294 295 res = ggml_metal_library_compile_pipeline(lib, base, name, cv); 296 297 ggml_metal_cv_free(cv); 298 } 299 300 res.c4 = is_c4; 301 res.cnt = is_cnt; 302 303 return res; 304 } 305 306 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_glu(ggml_metal_library_t lib, const ggml_tensor * op) { 307 GGML_ASSERT(ggml_is_contiguous_1(op->src[0])); 308 309 char base[256]; 310 char name[256]; 311 312 const char * op_str = "undefined"; 313 switch (op->op) { 314 case GGML_OP_GLU: 315 switch (ggml_get_glu_op(op)) { 316 case GGML_GLU_OP_REGLU: op_str = "reglu"; break; 317 case GGML_GLU_OP_GEGLU: op_str = "geglu"; break; 318 case GGML_GLU_OP_SWIGLU: op_str = "swiglu"; break; 319 case GGML_GLU_OP_SWIGLU_OAI: op_str = "swiglu_oai"; break; 320 case GGML_GLU_OP_GEGLU_ERF: op_str = "geglu_erf"; break; 321 case GGML_GLU_OP_GEGLU_QUICK: op_str = "geglu_quick"; break; 322 case GGML_GLU_OP_SWIGLU_CLAMP: op_str = "swiglu_clamp"; break; 323 default: GGML_ABORT("fatal error"); 324 } break; 325 default: GGML_ABORT("fatal error"); 326 }; 327 328 snprintf(base, 256, "kernel_%s_%s", op_str, ggml_type_name(op->src[0]->type)); 329 snprintf(name, 256, "%s", base); 330 331 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 332 if (!res.pipeline) { 333 res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr); 334 } 335 336 return res; 337 } 338 339 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_sum(ggml_metal_library_t lib, const ggml_tensor * op) { 340 assert(op->op == GGML_OP_SUM); 341 342 char base[256]; 343 char name[256]; 344 345 snprintf(base, 256, "kernel_op_sum_%s", ggml_type_name(op->src[0]->type)); 346 snprintf(name, 256, "%s", base); 347 348 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 349 if (!res.pipeline) { 350 res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr); 351 } 352 353 return res; 354 } 355 356 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_sum_rows(ggml_metal_library_t lib, const ggml_tensor * op) { 357 GGML_ASSERT(ggml_is_contiguous_rows(op->src[0])); 358 359 char base[256]; 360 char name[256]; 361 362 int op_num = -1; 363 364 switch (op->op) { 365 case GGML_OP_SUM_ROWS: op_num = OP_SUM_ROWS_NUM_SUM_ROWS; break; 366 case GGML_OP_MEAN: op_num = OP_SUM_ROWS_NUM_MEAN; break; 367 default: GGML_ABORT("fatal error"); 368 }; 369 370 const char * t0_str = ggml_type_name(op->src[0]->type); 371 const char * t_str = ggml_type_name(op->type); 372 373 const bool is_c4 = op->src[0]->ne[0] % 4 == 0; 374 375 snprintf(base, 256, "kernel_sum_rows_%s_%s%s", t0_str, t_str, is_c4 ? "_4" : ""); 376 snprintf(name, 256, "%s_op=%d", base, op_num); 377 378 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 379 if (!res.pipeline) { 380 ggml_metal_cv_t cv = ggml_metal_cv_init(); 381 382 ggml_metal_cv_set_int16(cv, op_num, FC_SUM_ROWS + 0); 383 384 res = ggml_metal_library_compile_pipeline(lib, base, name, cv); 385 386 ggml_metal_cv_free(cv); 387 } 388 389 res.smem = 32*sizeof(float); 390 391 if (is_c4) { 392 res.smem *= 4; 393 } 394 395 res.c4 = is_c4; 396 397 return res; 398 } 399 400 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_cumsum_blk(ggml_metal_library_t lib, const ggml_tensor * op) { 401 GGML_ASSERT(op->op == GGML_OP_CUMSUM); 402 403 char base[256]; 404 char name[256]; 405 406 snprintf(base, 256, "kernel_cumsum_blk_%s", ggml_type_name(op->src[0]->type)); 407 snprintf(name, 256, "%s", base); 408 409 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 410 if (!res.pipeline) { 411 res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr); 412 } 413 414 return res; 415 } 416 417 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_cumsum_add(ggml_metal_library_t lib, const ggml_tensor * op) { 418 GGML_ASSERT(op->op == GGML_OP_CUMSUM); 419 420 char base[256]; 421 char name[256]; 422 423 snprintf(base, 256, "kernel_cumsum_add_%s", ggml_type_name(op->src[0]->type)); 424 snprintf(name, 256, "%s", base); 425 426 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 427 if (!res.pipeline) { 428 res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr); 429 } 430 431 return res; 432 } 433 434 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_tri(ggml_metal_library_t lib, const ggml_tensor * op) { 435 GGML_ASSERT(op->op == GGML_OP_TRI); 436 GGML_ASSERT(op->src[0]->nb[0] == ggml_type_size(op->src[0]->type)); 437 438 char base[256]; 439 char name[256]; 440 441 const char * op_str = "tri"; 442 const int ttype = op->op_params[0]; 443 444 snprintf(base, 256, "kernel_%s_%s_%d", op_str, ggml_type_name(op->src[0]->type), ttype); 445 446 snprintf(name, 256, "%s", base); 447 448 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 449 if (!res.pipeline) { 450 res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr); 451 } 452 453 return res; 454 } 455 456 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_soft_max(ggml_metal_library_t lib, const ggml_tensor * op) { 457 GGML_ASSERT(!op->src[1] || op->src[1]->type == GGML_TYPE_F16 || op->src[1]->type == GGML_TYPE_F32); 458 459 char base[256]; 460 char name[256]; 461 462 const char * suffix = ""; 463 464 if (op->src[0]->ne[0] % 4 == 0) { 465 suffix = "_4"; 466 } 467 468 const ggml_type tsrc1 = op->src[1] ? op->src[1]->type : GGML_TYPE_F32; 469 470 snprintf(base, 256, "kernel_soft_max_%s%s", ggml_type_name(tsrc1), suffix); 471 snprintf(name, 256, "%s", base); 472 473 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 474 if (!res.pipeline) { 475 res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr); 476 } 477 478 res.smem = 32*sizeof(float); 479 480 return res; 481 } 482 483 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_lightning_indexer( 484 ggml_metal_library_t lib, 485 const ggml_tensor * op) { 486 GGML_ASSERT(op->op == GGML_OP_LIGHTNING_INDEXER); 487 488 char base[256]; 489 char name[256]; 490 491 const int16_t nh = op->src[0]->ne[1]; 492 493 snprintf(base, 256, "kernel_lightning_indexer_%s", ggml_type_name(op->src[1]->type)); 494 snprintf(name, 256, "%s_nh=%d", base, nh); 495 496 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 497 if (!res.pipeline) { 498 ggml_metal_cv_t cv = ggml_metal_cv_init(); 499 500 ggml_metal_cv_set_int16(cv, nh, FC_LIGHTNING_INDEXER + 0); 501 502 res = ggml_metal_library_compile_pipeline(lib, base, name, cv); 503 504 ggml_metal_cv_free(cv); 505 } 506 507 return res; 508 } 509 510 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_top_k_mask( 511 ggml_metal_library_t lib, 512 const ggml_tensor * op, 513 bool fill, 514 bool has_score, 515 bool has_blocks, 516 bool has_extra, 517 bool vec) { 518 GGML_ASSERT(op->op == GGML_OP_TOP_K_MASK); 519 520 char base[256]; 521 char name[256]; 522 523 snprintf(base, 256, "kernel_top_k_mask%s_%s", fill ? "_fill" : "", ggml_type_name(op->type)); 524 525 ggml_metal_pipeline_with_params res; 526 527 ggml_metal_cv_t cv = ggml_metal_cv_init(); 528 529 if (fill) { 530 snprintf(name, 256, "%s_vec=%d", base, vec ? 1 : 0); 531 532 ggml_metal_cv_set_bool(cv, vec, FC_TOP_K_MASK + 3); 533 } else { 534 snprintf(name, 256, "%s_sc=%d_bl=%d_ex=%d", base, has_score ? 1 : 0, has_blocks ? 1 : 0, has_extra ? 1 : 0); 535 536 ggml_metal_cv_set_bool(cv, has_score, FC_TOP_K_MASK + 0); 537 ggml_metal_cv_set_bool(cv, has_blocks, FC_TOP_K_MASK + 1); 538 ggml_metal_cv_set_bool(cv, has_extra, FC_TOP_K_MASK + 2); 539 } 540 541 res = ggml_metal_library_get_pipeline(lib, name); 542 if (!res.pipeline) { 543 res = ggml_metal_library_compile_pipeline(lib, base, name, cv); 544 } 545 546 ggml_metal_cv_free(cv); 547 548 return res; 549 } 550 551 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_dsv4_hc(ggml_metal_library_t lib, const ggml_tensor * op) { 552 char name[256]; 553 const char * base = nullptr; 554 555 switch (op->op) { 556 case GGML_OP_DSV4_HC_COMB: 557 base = "kernel_dsv4_hc_comb_f32"; 558 snprintf(name, 256, "%s", base); 559 break; 560 case GGML_OP_DSV4_HC_PRE: 561 base = ggml_get_op_params_i32(op, 1) != 0 ? "kernel_dsv4_hc_pre_gated_f32" : "kernel_dsv4_hc_pre_f32"; 562 snprintf(name, 256, "%s_n_hc=%d", base, (int) op->src[0]->ne[1]); 563 break; 564 case GGML_OP_DSV4_HC_POST: 565 base = op->src[3] ? "kernel_dsv4_hc_post_f32" : "kernel_dsv4_hc_post_nocomb_f32"; 566 snprintf(name, 256, "%s", base); 567 break; 568 default: 569 GGML_ABORT("fatal error"); 570 } 571 572 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 573 if (!res.pipeline) { 574 ggml_metal_cv_t cv = nullptr; 575 576 if (op->op == GGML_OP_DSV4_HC_PRE) { 577 cv = ggml_metal_cv_init(); 578 ggml_metal_cv_set_int32(cv, (int32_t) op->src[0]->ne[1], FC_DSV4_HC + 0); 579 } 580 581 res = ggml_metal_library_compile_pipeline(lib, base, name, cv); 582 583 if (cv) { 584 ggml_metal_cv_free(cv); 585 } 586 } 587 588 return res; 589 } 590 591 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_ssm_conv( 592 ggml_metal_library_t lib, const ggml_tensor * op, int32_t nc, bool use_silu) { 593 GGML_ASSERT(op->src[0]->type == GGML_TYPE_F32); 594 GGML_ASSERT(op->src[1]->type == GGML_TYPE_F32); 595 596 GGML_ASSERT(ggml_is_contiguous(op->src[0])); 597 GGML_ASSERT(ggml_is_contiguous(op->src[1])); 598 599 char base[256]; 600 char name[256]; 601 602 const char * suffix = ""; 603 604 if (op->src[1]->ne[0] % 4 == 0) { 605 suffix = "_4"; 606 } 607 608 snprintf(base, 256, "kernel_ssm_conv_%s_%s%s", ggml_type_name(op->src[0]->type), ggml_type_name(op->src[1]->type), suffix); 609 snprintf(name, 256, "%s_nc=%d_silu=%d", base, nc, use_silu ? 1 : 0); 610 611 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 612 if (!res.pipeline) { 613 ggml_metal_cv_t cv = ggml_metal_cv_init(); 614 ggml_metal_cv_set_bool(cv, use_silu, FC_SSM_CONV + 1); 615 ggml_metal_cv_set_int32(cv, nc, FC_SSM_CONV + 2); 616 617 res = ggml_metal_library_compile_pipeline(lib, base, name, cv); 618 619 ggml_metal_cv_free(cv); 620 } 621 622 return res; 623 } 624 625 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_ssm_conv_batched( 626 ggml_metal_library_t lib, const ggml_tensor * op, int ssm_conv_bs, int32_t nc, bool use_silu) { 627 GGML_ASSERT(op->src[0]->type == GGML_TYPE_F32); 628 GGML_ASSERT(op->src[1]->type == GGML_TYPE_F32); 629 630 GGML_ASSERT(ggml_is_contiguous(op->src[0])); 631 GGML_ASSERT(ggml_is_contiguous(op->src[1])); 632 633 char base[256]; 634 char name[256]; 635 636 const char * suffix = ""; 637 if (op->src[1]->ne[0] % 4 == 0) { 638 suffix = "_4"; 639 } 640 641 snprintf(base, 256, "kernel_ssm_conv_%s_%s_batched%s", ggml_type_name(op->src[0]->type), ggml_type_name(op->src[1]->type), suffix); 642 snprintf(name, 256, "%s_ssm_conv_bs=%d_nc=%d_silu=%d", base, ssm_conv_bs, nc, use_silu ? 1 : 0); 643 644 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 645 if (!res.pipeline) { 646 ggml_metal_cv_t cv = ggml_metal_cv_init(); 647 648 ggml_metal_cv_set_int16(cv, ssm_conv_bs, FC_SSM_CONV + 0); 649 ggml_metal_cv_set_bool(cv, use_silu, FC_SSM_CONV + 1); 650 ggml_metal_cv_set_int32(cv, nc, FC_SSM_CONV + 2); 651 652 res = ggml_metal_library_compile_pipeline(lib, base, name, cv); 653 654 ggml_metal_cv_free(cv); 655 } 656 657 return res; 658 } 659 660 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_ssm_scan(ggml_metal_library_t lib, const ggml_tensor * op, bool tail) { 661 GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); 662 663 char base[256]; 664 char name[256]; 665 666 const int nsg = (ne00 + 31)/32; 667 668 snprintf(base, 256, "kernel_ssm_scan_%s%s", ggml_type_name(op->src[0]->type), tail ? "_tail" : ""); 669 snprintf(name, 256, "%s_nsg=%d", base, nsg); 670 671 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 672 if (!res.pipeline) { 673 res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr); 674 } 675 676 // Shared memory layout: 677 // - sgptg * NW floats for partial sums (nsg * 32) 678 // - sgptg floats for shared_x_dt (nsg) 679 // - sgptg floats for shared_dA (nsg) 680 // Total: nsg * (32 + 2) floats 681 res.smem = GGML_PAD((32 + 2)*sizeof(float)*nsg, 16); 682 683 return res; 684 } 685 686 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_ssm_scan_ssd_mma(ggml_metal_library_t lib, const ggml_tensor * op) { 687 char base[256]; 688 char name[256]; 689 690 snprintf(base, 256, "kernel_ssm_scan_ssd_mma_%s", ggml_type_name(op->src[0]->type)); 691 snprintf(name, 256, "%s", base); 692 693 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 694 if (!res.pipeline) { 695 res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr); 696 } 697 698 // acs/exp(acs)/state-decay vectors + dtX + SAM rows + two 8x8 tiles per simdgroup 699 res.smem = (3*OP_SSM_SCAN_SSD_CS + 700 OP_SSM_SCAN_SSD_CS*OP_SSM_SCAN_SSD_HD + 701 OP_SSM_SCAN_SSD_NSG*8*OP_SSM_SCAN_SSD_CS + 702 OP_SSM_SCAN_SSD_NSG*2*8*8)*sizeof(float); 703 704 return res; 705 } 706 707 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_rwkv(ggml_metal_library_t lib, const ggml_tensor * op) { 708 char base[256]; 709 char name[256]; 710 711 const int64_t C = op->ne[0]; 712 const int64_t H = op->src[0]->ne[1]; 713 714 switch (op->op) { 715 case GGML_OP_RWKV_WKV6: 716 { 717 GGML_ASSERT(op->src[5]->type == GGML_TYPE_F32); 718 GGML_ASSERT(C % H == 0); 719 GGML_ASSERT(C / H == 64); 720 721 snprintf(base, 256, "kernel_rwkv_wkv6_%s", ggml_type_name(op->src[0]->type)); 722 } break; 723 case GGML_OP_RWKV_WKV7: 724 { 725 GGML_ASSERT(op->src[6]->type == GGML_TYPE_F32); 726 GGML_ASSERT(C % H == 0); 727 GGML_ASSERT(C / H == 64); 728 729 snprintf(base, 256, "kernel_rwkv_wkv7_%s", ggml_type_name(op->src[0]->type)); 730 } break; 731 default: 732 GGML_ABORT("fatal error"); 733 } 734 735 snprintf(name, 256, "%s", base); 736 737 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 738 if (!res.pipeline) { 739 res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr); 740 } 741 742 return res; 743 } 744 745 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_gated_delta_net(ggml_metal_library_t lib, const ggml_tensor * op) { 746 char base[256]; 747 char name[256]; 748 749 // v is src[2], dimensions: S_v = ne[0], H = ne[1] 750 const int ne20 = op->src[2]->ne[0]; // S_v 751 const int ne21 = op->src[2]->ne[1]; // H 752 const int ne30 = op->src[3]->ne[0]; // G 753 // state is src[5], 4D [S_v, S_v, H_v, n_seqs] (s0 only); K is op param 0. 754 const int K = ggml_get_op_params_i32(op, 0); 755 756 const int nsg = op->src[2]->ne[0]/32; 757 758 GGML_ASSERT(op->src[5]->type == GGML_TYPE_F32); 759 GGML_ASSERT(op->ne[0] == ne20 * ne21); 760 GGML_ASSERT(ne20 % 32 == 0); 761 762 snprintf(base, 256, "kernel_gated_delta_net_%s_%d", ggml_type_name(op->src[0]->type), nsg); 763 snprintf(name, 256, "%s_ne20=%d_ne30=%d_K=%d", base, ne20, ne30, K); 764 765 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 766 if (!res.pipeline) { 767 ggml_metal_cv_t cv = ggml_metal_cv_init(); 768 769 ggml_metal_cv_set_int16(cv, ne20, FC_GATED_DELTA_NET + 0); 770 ggml_metal_cv_set_int16(cv, ne30, FC_GATED_DELTA_NET + 1); 771 ggml_metal_cv_set_int16(cv, K, FC_GATED_DELTA_NET + 2); 772 773 res = ggml_metal_library_compile_pipeline(lib, base, name, cv); 774 775 ggml_metal_cv_free(cv); 776 } 777 778 res.nsg = nsg; 779 780 return res; 781 } 782 783 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_solve_tri(ggml_metal_library_t lib, const ggml_tensor * op) { 784 char base[256]; 785 char name[256]; 786 787 const int nsg = 8; 788 const int n = op->src[1]->ne[1]; 789 const int k = op->src[1]->ne[0]; 790 791 snprintf(base, 256, "kernel_solve_tri_%s", ggml_type_name(op->src[0]->type)); 792 snprintf(name, 256, "%s_nsg=%d_n=%d_k=%d", base, nsg, n, k); 793 794 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 795 if (!res.pipeline) { 796 ggml_metal_cv_t cv = ggml_metal_cv_init(); 797 798 ggml_metal_cv_set_int16(cv, nsg, FC_SOLVE_TRI + 0); 799 ggml_metal_cv_set_int16(cv, n, FC_SOLVE_TRI + 1); 800 ggml_metal_cv_set_int16(cv, k, FC_SOLVE_TRI + 2); 801 802 res = ggml_metal_library_compile_pipeline(lib, base, name, cv); 803 804 ggml_metal_cv_free(cv); 805 } 806 807 res.nsg = nsg; 808 res.smem = GGML_PAD(GGML_PAD(n, 32)*nsg*sizeof(float), 16); 809 810 return res; 811 } 812 813 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_mul_mv_ext(ggml_metal_library_t lib, const ggml_tensor * op, int nsg, int nxpsg, int r1ptg) { 814 char base[256]; 815 char name[256]; 816 817 const ggml_type tsrc0 = op->src[0]->type; 818 const ggml_type tsrc1 = op->src[1]->type; 819 const int ne12 = op->src[1]->ne[2]; 820 const int r2 = ne12 / op->src[0]->ne[2]; 821 const int r3 = op->src[1]->ne[3] / op->src[0]->ne[3]; 822 823 GGML_ASSERT(ne12 <= INT16_MAX && r2 <= INT16_MAX && r3 <= INT16_MAX); 824 825 snprintf(base, 256, "kernel_mul_mv_ext_%s_%s_r1_%d", ggml_type_name(tsrc0), ggml_type_name(tsrc1), r1ptg); 826 snprintf(name, 256, "%s_nsg=%d_nxpsg=%d_ne12=%d_r2=%d_r3=%d", base, nsg, nxpsg, ne12, r2, r3); 827 828 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 829 if (!res.pipeline) { 830 ggml_metal_cv_t cv = ggml_metal_cv_init(); 831 832 ggml_metal_cv_set_int16(cv, nsg, FC_MUL_MV + 0); 833 ggml_metal_cv_set_int16(cv, nxpsg, FC_MUL_MV + 1); 834 ggml_metal_cv_set_int16(cv, (int16_t) ne12, FC_MUL_MV + 2); 835 ggml_metal_cv_set_int16(cv, (int16_t) r2, FC_MUL_MV + 3); 836 ggml_metal_cv_set_int16(cv, (int16_t) r3, FC_MUL_MV + 4); 837 838 res = ggml_metal_library_compile_pipeline(lib, base, name, cv); 839 840 ggml_metal_cv_free(cv); 841 } 842 843 return res; 844 } 845 846 // threadgroups needed to fill the GPU 847 // TODO: dedup 848 static constexpr int64_t GGML_METAL_MIN_THREADGROUPS = 128; 849 850 // few-row MMA mat-mul: 8x8 simdgroup matrices for the src1 rows of ggml_metal_mul_mat_use_mma 851 static constexpr int GGML_METAL_MMA_TILE = 8; 852 static constexpr int GGML_METAL_MMA_NT_MAX = 4; 853 static constexpr int GGML_METAL_MMA_SMEM_MAX = 16384; 854 855 // simdgroups per threadgroup for at most FEW_ROWS, at most MANY_ROWS, and more src0 rows 856 static constexpr int64_t GGML_METAL_MMA_FEW_ROWS = 64; 857 static constexpr int64_t GGML_METAL_MMA_MANY_ROWS = 6144; 858 static constexpr int GGML_METAL_MMA_NSG_FEW_ROWS = 32; 859 static constexpr int GGML_METAL_MMA_NSG_MID_ROWS = 16; 860 static constexpr int GGML_METAL_MMA_NSG_MANY_ROWS = 8; 861 862 struct ggml_metal_mma_tiling { 863 int nsg; // simdgroups per threadgroup, each over a slice of K 864 int nt; // 8-row src0 tiles per threadgroup 865 int rt; // 8-row src1 tiles per threadgroup 866 }; 867 868 // halves n while it is above limit 869 static int ggml_metal_halve_to_limit(int n, int64_t limit) { 870 while (n > 1 && n > limit) { 871 n /= 2; 872 } 873 return n; 874 } 875 876 static size_t ggml_metal_mul_mv_mma_smem(int nsg, int nt, int rt) { 877 // one 8x8 float simdgroup matrix per output tile 878 constexpr size_t tile_bytes = 8*8*sizeof(float); 879 return (size_t) nsg*nt*rt*tile_bytes; 880 } 881 882 // fewer src0 rows need more simdgroups per threadgroup (a finer K split) to fill the GPU. 883 // the tiles become narrower when the K-slice reduction buffer is too large or when too few threadgroups fill the GPU. 884 static ggml_metal_mma_tiling ggml_metal_op_mul_mat_mma_tiling(const ggml_tensor * op) { 885 const ggml_type type = op->src[0]->type; 886 const int64_t ne00 = op->src[0]->ne[0]; 887 const int64_t ne01 = op->src[0]->ne[1]; 888 889 ggml_metal_mma_tiling res; 890 res.rt = ggml_metal_mul_mv_mma_rt(op); 891 892 const int64_t n_steps = ne00/ggml_metal_mul_mv_mma_k_step(type, res.rt); 893 894 int nsg = GGML_METAL_MMA_NSG_MANY_ROWS; 895 if (ne01 <= GGML_METAL_MMA_FEW_ROWS) { 896 nsg = GGML_METAL_MMA_NSG_FEW_ROWS; 897 } else if (ne01 <= GGML_METAL_MMA_MANY_ROWS) { 898 nsg = GGML_METAL_MMA_NSG_MID_ROWS; 899 } 900 res.nsg = ggml_metal_halve_to_limit(nsg, n_steps); 901 902 const int64_t nt_smem = GGML_METAL_MMA_SMEM_MAX/(int64_t) ggml_metal_mul_mv_mma_smem(res.nsg, 1, res.rt); 903 const int64_t nt_rows = ne01/(GGML_METAL_MIN_THREADGROUPS*GGML_METAL_MMA_TILE); 904 res.nt = ggml_metal_halve_to_limit(GGML_METAL_MMA_NT_MAX, std::min(nt_smem, nt_rows)); 905 906 return res; 907 } 908 909 // the pipeline for the tiling, with fewer simdgroups when the device cannot run that many threads 910 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_mul_mv_mma_auto(ggml_metal_library_t lib, const ggml_tensor * op, bool add) { 911 ggml_metal_mma_tiling tiling = ggml_metal_op_mul_mat_mma_tiling(op); 912 913 auto pipeline = ggml_metal_library_get_pipeline_mul_mv_mma(lib, op, tiling.nsg, tiling.nt, tiling.rt, add); 914 while (tiling.nsg > 1 && ggml_metal_pipeline_max_theads_per_threadgroup(pipeline) < tiling.nsg*32) { 915 tiling.nsg /= 2; 916 pipeline = ggml_metal_library_get_pipeline_mul_mv_mma(lib, op, tiling.nsg, tiling.nt, tiling.rt, add); 917 } 918 919 return pipeline; 920 } 921 922 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_mul_mv_mma(ggml_metal_library_t lib, const ggml_tensor * op, int nsg, int nt, int rt, bool add) { 923 char base[256]; 924 char name[256]; 925 926 const ggml_type tsrc0 = op->src[0]->type; 927 const ggml_type tsrc1 = op->src[1]->type; 928 const int ne12 = op->src[1]->ne[2]; 929 const int r2 = ne12 / op->src[0]->ne[2]; 930 const int r3 = op->src[1]->ne[3] / op->src[0]->ne[3]; 931 932 GGML_ASSERT(ne12 <= INT16_MAX && r2 <= INT16_MAX && r3 <= INT16_MAX); 933 934 // the specialized kernels unroll over a compile-time row length 935 const int ne00 = ggml_metal_mul_mv_mma_kind(tsrc0, rt) != GGML_METAL_MMA_KIND_GEN ? op->src[0]->ne[0] : 0; 936 937 snprintf(base, 256, "kernel_mul_mv_mma_%s_%s_nt%d_rt%d", ggml_type_name(tsrc0), ggml_type_name(tsrc1), nt, rt); 938 snprintf(name, 256, "%s_nsg=%d_ne12=%d_r2=%d_r3=%d_ne00=%d_add=%d", base, nsg, ne12, r2, r3, ne00, add); 939 940 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 941 if (!res.pipeline) { 942 ggml_metal_cv_t cv = ggml_metal_cv_init(); 943 944 ggml_metal_cv_set_int32(cv, ne00, FC_MUL_MV_MMA + 4); 945 ggml_metal_cv_set_int16(cv, nsg, FC_MUL_MV_MMA + 0); 946 ggml_metal_cv_set_int16(cv, (int16_t) ne12, FC_MUL_MV_MMA + 1); 947 ggml_metal_cv_set_int16(cv, (int16_t) r2, FC_MUL_MV_MMA + 2); 948 ggml_metal_cv_set_int16(cv, (int16_t) r3, FC_MUL_MV_MMA + 3); 949 ggml_metal_cv_set_bool (cv, add, FC_MUL_MV_MMA + 5); 950 951 res = ggml_metal_library_compile_pipeline(lib, base, name, cv); 952 953 ggml_metal_cv_free(cv); 954 } 955 956 res.nsg = nsg; 957 res.smem = ggml_metal_mul_mv_mma_smem(nsg, nt, rt); 958 959 res.nr0 = GGML_METAL_MMA_TILE*nt; 960 res.nr1 = GGML_METAL_MMA_TILE*rt; 961 962 return res; 963 } 964 965 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_mul_mm(ggml_metal_library_t lib, const ggml_tensor * op) { 966 char base[256]; 967 char name[256]; 968 969 const ggml_type tsrc0 = op->src[0]->type; 970 const ggml_type tsrc1 = op->src[1]->type; 971 972 const bool bc_inp = op->src[0]->ne[0] % 32 != 0; 973 974 constexpr int NRA = SZ_SIMDGROUP * N_MM_BLOCK_Y * N_MM_SIMD_GROUP_Y; 975 constexpr int NRB = SZ_SIMDGROUP * N_MM_BLOCK_X * N_MM_SIMD_GROUP_X; 976 977 const bool has_tensor = ggml_metal_device_get_props(ggml_metal_library_get_device(lib))->has_tensor; 978 979 const bool bc_out = has_tensor 980 ? (op->ne[0] % NRA != 0 || op->ne[1] % NRB != 0) 981 : (op->ne[0] % 64 != 0 || op->ne[1] % 32 != 0); 982 983 GGML_ASSERT(op->src[1]->ne[2] <= INT16_MAX && op->src[1]->ne[3] <= INT16_MAX); 984 const int16_t ne12 = (int16_t) op->src[1]->ne[2]; 985 const int16_t ne13 = (int16_t) op->src[1]->ne[3]; 986 const int16_t r2 = (int16_t) (ne12 / op->src[0]->ne[2]); 987 const int16_t r3 = (int16_t) (ne13 / op->src[0]->ne[3]); 988 989 snprintf(base, 256, "kernel_mul_mm_%s_%s", ggml_type_name(tsrc0), ggml_type_name(tsrc1)); 990 snprintf(name, 256, "%s_bci=%d_bco=%d_ne12=%d_ne13=%d_r2=%d_r3=%d", 991 base, bc_inp, bc_out, ne12, ne13, r2, r3); 992 993 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 994 if (!res.pipeline) { 995 ggml_metal_cv_t cv = ggml_metal_cv_init(); 996 997 ggml_metal_cv_set_bool(cv, bc_inp, FC_MUL_MM + 0); 998 ggml_metal_cv_set_bool(cv, bc_out, FC_MUL_MM + 1); 999 ggml_metal_cv_set_int16(cv, ne12, FC_MUL_MM + 2); 1000 ggml_metal_cv_set_int16(cv, ne13, FC_MUL_MM + 3); 1001 ggml_metal_cv_set_int16(cv, r2, FC_MUL_MM + 4); 1002 ggml_metal_cv_set_int16(cv, r3, FC_MUL_MM + 5); 1003 1004 res = ggml_metal_library_compile_pipeline(lib, base, name, cv); 1005 1006 ggml_metal_cv_free(cv); 1007 } 1008 1009 if (has_tensor) { 1010 res.nr0 = NRA; 1011 res.nr1 = NRB; 1012 1013 const size_t smem_a = NRA * N_MM_NK_TOTAL * sizeof(ggml_fp16_t); 1014 res.smem = smem_a; 1015 } else { 1016 res.nr0 = 64; 1017 res.nr1 = 32; 1018 1019 res.smem = bc_out ? 8192 : (4096 + 2048); 1020 } 1021 1022 res.nsg = N_MM_SIMD_GROUP_X * N_MM_SIMD_GROUP_Y; 1023 1024 return res; 1025 } 1026 1027 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_mul_mv(ggml_metal_library_t lib, const ggml_tensor * op) { 1028 GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); 1029 GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne); 1030 1031 char base[256]; 1032 char name[256]; 1033 1034 int nsg = 0; // number of simdgroups 1035 int nr0 = 0; // number of src0 rows per simdgroup 1036 int nr1 = 1; // number of src1 rows per threadgroup 1037 1038 size_t smem = 0; // shared memory 1039 1040 const ggml_type tsrc0 = op->src[0]->type; 1041 const ggml_type tsrc1 = op->src[1]->type; 1042 1043 const char * suffix = ""; 1044 1045 bool split = false; 1046 1047 // use custom matrix x vector kernel 1048 switch (tsrc0) { 1049 case GGML_TYPE_F32: 1050 case GGML_TYPE_F16: 1051 case GGML_TYPE_BF16: 1052 { 1053 if (ne00 < 32) { 1054 nsg = 1; 1055 nr0 = 32; 1056 nr1 = 1; 1057 suffix = "_short"; 1058 } else { 1059 nsg = std::min(4, (ne00 + 127) / 128); 1060 nr0 = 2; 1061 nr1 = 1; 1062 smem = 32*sizeof(float)*nr0; 1063 suffix = ne00 % 4 == 0 ? "_4" : ""; 1064 } 1065 } break; 1066 case GGML_TYPE_Q1_0: 1067 { 1068 nsg = N_SG_Q1_0; 1069 nr0 = N_R0_Q1_0; 1070 } break; 1071 case GGML_TYPE_Q2_0: 1072 { 1073 nsg = N_SG_Q2_0; 1074 nr0 = N_R0_Q2_0; 1075 } break; 1076 case GGML_TYPE_Q4_0: 1077 { 1078 nsg = N_SG_Q4_0; 1079 nr0 = N_R0_Q4_0; 1080 } break; 1081 case GGML_TYPE_Q4_1: 1082 { 1083 nsg = N_SG_Q4_1; 1084 nr0 = N_R0_Q4_1; 1085 } break; 1086 case GGML_TYPE_Q5_0: 1087 { 1088 nsg = N_SG_Q5_0; 1089 nr0 = N_R0_Q5_0; 1090 } break; 1091 case GGML_TYPE_Q5_1: 1092 { 1093 nsg = N_SG_Q5_1; 1094 nr0 = N_R0_Q5_1; 1095 } break; 1096 case GGML_TYPE_Q8_0: 1097 { 1098 nsg = N_SG_Q8_0; 1099 nr0 = N_R0_Q8_0; 1100 smem = 32*sizeof(float)*N_R0_Q8_0; 1101 } break; 1102 case GGML_TYPE_MXFP4: 1103 { 1104 nsg = N_SG_MXFP4; 1105 nr0 = N_R0_MXFP4; 1106 } break; 1107 case GGML_TYPE_Q2_K: 1108 { 1109 nsg = N_SG_Q2_K; 1110 nr0 = N_R0_Q2_K; 1111 } break; 1112 case GGML_TYPE_Q3_K: 1113 { 1114 nsg = N_SG_Q3_K; 1115 nr0 = N_R0_Q3_K; 1116 } break; 1117 case GGML_TYPE_Q4_K: 1118 { 1119 nsg = N_SG_Q4_K; 1120 nr0 = N_R0_Q4_K; 1121 } break; 1122 case GGML_TYPE_Q5_K: 1123 { 1124 nsg = N_SG_Q5_K; 1125 nr0 = N_R0_Q5_K; 1126 } break; 1127 case GGML_TYPE_Q6_K: 1128 { 1129 nsg = N_SG_Q6_K; 1130 nr0 = N_R0_Q6_K; 1131 } break; 1132 case GGML_TYPE_IQ2_XXS: 1133 { 1134 nsg = N_SG_IQ2_XXS; 1135 nr0 = N_R0_IQ2_XXS; 1136 smem = 256*8+128; 1137 1138 const int nb32 = ne00/32; 1139 if (nb32 < 32 && (32 % nb32) == 0) { 1140 nr0 = N_R0_IQ2_XXS_SPLIT; 1141 split = true; 1142 } 1143 } break; 1144 case GGML_TYPE_IQ2_XS: 1145 { 1146 nsg = N_SG_IQ2_XS; 1147 nr0 = N_R0_IQ2_XS; 1148 smem = 512*8+128; 1149 1150 const int nb32 = ne00/32; 1151 if (nb32 < 32 && (32 % nb32) == 0) { 1152 nr0 = N_R0_IQ2_XS_SPLIT; 1153 split = true; 1154 } 1155 } break; 1156 case GGML_TYPE_IQ3_XXS: 1157 { 1158 nsg = N_SG_IQ3_XXS; 1159 nr0 = N_R0_IQ3_XXS; 1160 smem = 256*4+128; 1161 1162 // split the rows across threads when there are fewer than 32 chunks per row 1163 const int nb32 = ne00/32; 1164 if (nb32 < 32 && (32 % nb32) == 0) { 1165 nr0 = N_R0_IQ3_XXS_SPLIT; 1166 split = true; 1167 } 1168 } break; 1169 case GGML_TYPE_IQ3_S: 1170 { 1171 nsg = N_SG_IQ3_S; 1172 nr0 = N_R0_IQ3_S; 1173 smem = 512*4; 1174 1175 const int nb32 = ne00/32; 1176 if (nb32 < 32 && (32 % nb32) == 0) { 1177 nr0 = N_R0_IQ3_S_SPLIT; 1178 split = true; 1179 } 1180 } break; 1181 case GGML_TYPE_IQ2_S: 1182 { 1183 nsg = N_SG_IQ2_S; 1184 nr0 = N_R0_IQ2_S; 1185 1186 const int nb32 = ne00/32; 1187 if (nb32 < 32 && (32 % nb32) == 0) { 1188 nr0 = N_R0_IQ2_S_SPLIT; 1189 split = true; 1190 } 1191 } break; 1192 case GGML_TYPE_IQ1_S: 1193 { 1194 nsg = N_SG_IQ1_S; 1195 nr0 = N_R0_IQ1_S; 1196 1197 const int nb32 = ne00/32; 1198 if (nb32 < 32 && (32 % nb32) == 0) { 1199 nr0 = N_R0_IQ1_S_SPLIT; 1200 split = true; 1201 } 1202 } break; 1203 case GGML_TYPE_IQ1_M: 1204 { 1205 nsg = N_SG_IQ1_M; 1206 nr0 = N_R0_IQ1_M; 1207 1208 const int nb32 = ne00/32; 1209 if (nb32 < 32 && (32 % nb32) == 0) { 1210 nr0 = N_R0_IQ1_M_SPLIT; 1211 split = true; 1212 } 1213 } break; 1214 case GGML_TYPE_IQ4_NL: 1215 { 1216 nsg = N_SG_IQ4_NL; 1217 nr0 = N_R0_IQ4_NL; 1218 smem = 32*sizeof(float); 1219 } break; 1220 case GGML_TYPE_IQ4_XS: 1221 { 1222 nsg = N_SG_IQ4_XS; 1223 nr0 = N_R0_IQ4_XS; 1224 smem = 32*sizeof(float); 1225 } break; 1226 case GGML_TYPE_TQ2_0: 1227 { 1228 nsg = N_SG_TQ2_0; 1229 nr0 = N_R0_TQ2_0; 1230 } break; 1231 default: 1232 { 1233 GGML_LOG_ERROR("Asserting on type %d\n", (int) tsrc0); 1234 GGML_ABORT("not implemented"); 1235 } 1236 }; 1237 1238 GGML_ASSERT(ne12 <= INT16_MAX && ne13 <= INT16_MAX); 1239 const int16_t r2 = (int16_t) (ne12 / ne02); 1240 const int16_t r3 = (int16_t) (ne13 / ne03); 1241 1242 snprintf(base, 256, "kernel_mul_mv_%s_%s%s", ggml_type_name(tsrc0), ggml_type_name(tsrc1), suffix); 1243 snprintf(name, 256, "%s_nsg=%d_ne12=%d_r2=%d_r3=%d_split=%d", base, nsg, ne12, r2, r3, split); 1244 1245 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 1246 if (!res.pipeline) { 1247 ggml_metal_cv_t cv = ggml_metal_cv_init(); 1248 1249 ggml_metal_cv_set_int16(cv, nsg, FC_MUL_MV + 0); 1250 ggml_metal_cv_set_int16(cv, (int16_t) ne12, FC_MUL_MV + 2); 1251 ggml_metal_cv_set_int16(cv, r2, FC_MUL_MV + 3); 1252 ggml_metal_cv_set_int16(cv, r3, FC_MUL_MV + 4); 1253 ggml_metal_cv_set_bool (cv, split, FC_MUL_MV + 5); 1254 1255 res = ggml_metal_library_compile_pipeline(lib, base, name, cv); 1256 1257 ggml_metal_cv_free(cv); 1258 } 1259 1260 res.nr0 = nr0; 1261 res.nr1 = nr1; 1262 res.nsg = nsg; 1263 res.smem = smem; 1264 1265 return res; 1266 } 1267 1268 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_mul_mm_id_map0(ggml_metal_library_t lib, int ne02, int ne20) { 1269 char base[256]; 1270 char name[256]; 1271 1272 snprintf(base, 256, "kernel_mul_mm_id_map0_ne20_%d", ne20); 1273 snprintf(name, 256, "%s_ne02=%d", base, ne02); 1274 1275 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 1276 if (!res.pipeline) { 1277 ggml_metal_cv_t cv = ggml_metal_cv_init(); 1278 ggml_metal_cv_set_int32(cv, ne02, FC_MUL_MM + 7); 1279 1280 res = ggml_metal_library_compile_pipeline(lib, base, name, cv); 1281 1282 ggml_metal_cv_free(cv); 1283 } 1284 1285 // per expert: entry count + base offset, followed by the staging of the ids of ntg tokens 1286 res.smem = 2*(size_t) ne02*sizeof(uint32_t) + (size_t) ne02*ne20*sizeof(uint16_t); 1287 res.smem = GGML_PAD(res.smem, 16); 1288 1289 return res; 1290 } 1291 1292 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_mul_mm_id_zero(ggml_metal_library_t lib) { 1293 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, "kernel_mul_mm_id_zero"); 1294 if (!res.pipeline) { 1295 res = ggml_metal_library_compile_pipeline(lib, "kernel_mul_mm_id_zero", "kernel_mul_mm_id_zero", nullptr); 1296 } 1297 1298 return res; 1299 } 1300 1301 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_mul_mm_id(ggml_metal_library_t lib, const ggml_tensor * op) { 1302 char base[256]; 1303 char name[256]; 1304 1305 const ggml_type tsrc0 = op->src[0]->type; 1306 const ggml_type tsrc1 = op->src[1]->type; 1307 1308 const bool bc_inp = op->src[0]->ne[0] % 32 != 0; 1309 1310 snprintf(base, 256, "kernel_mul_mm_id_%s_%s", ggml_type_name(tsrc0), ggml_type_name(tsrc1)); 1311 snprintf(name, 256, "%s_bci=%d", base, bc_inp); 1312 1313 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 1314 if (!res.pipeline) { 1315 ggml_metal_cv_t cv = ggml_metal_cv_init(); 1316 1317 ggml_metal_cv_set_bool(cv, bc_inp, FC_MUL_MM + 0); 1318 1319 res = ggml_metal_library_compile_pipeline(lib, base, name, cv); 1320 1321 ggml_metal_cv_free(cv); 1322 } 1323 1324 res.smem = 8192; 1325 1326 return res; 1327 } 1328 1329 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_mul_mv_id(ggml_metal_library_t lib, const ggml_tensor * op) { 1330 GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); 1331 GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne); 1332 1333 char base[256]; 1334 char name[256]; 1335 1336 int nsg = 0; // number of simdgroups 1337 int nr0 = 0; // number of src0 rows per simdgroup 1338 int nr1 = 1; // number of src1 rows per threadgroup 1339 1340 size_t smem = 0; // shared memory 1341 1342 const ggml_type tsrc0 = op->src[0]->type; 1343 const ggml_type tsrc1 = op->src[1]->type; 1344 1345 const char * suffix = ""; 1346 1347 bool split = false; 1348 1349 // use custom matrix x vector kernel 1350 switch (tsrc0) { 1351 case GGML_TYPE_F32: 1352 case GGML_TYPE_F16: 1353 case GGML_TYPE_BF16: 1354 { 1355 nsg = std::min(4, (ne00 + 127) / 128); 1356 nr0 = 2; 1357 nr1 = 1; 1358 smem = 32*sizeof(float)*nr0; 1359 suffix = ne00 % 4 == 0 ? "_4" : ""; 1360 } break; 1361 case GGML_TYPE_Q1_0: 1362 { 1363 nsg = N_SG_Q1_0; 1364 nr0 = N_R0_Q1_0; 1365 } break; 1366 case GGML_TYPE_Q2_0: 1367 { 1368 nsg = N_SG_Q2_0; 1369 nr0 = N_R0_Q2_0; 1370 } break; 1371 case GGML_TYPE_Q4_0: 1372 { 1373 nsg = N_SG_Q4_0; 1374 nr0 = N_R0_Q4_0; 1375 } break; 1376 case GGML_TYPE_Q4_1: 1377 { 1378 nsg = N_SG_Q4_1; 1379 nr0 = N_R0_Q4_1; 1380 } break; 1381 case GGML_TYPE_Q5_0: 1382 { 1383 nsg = N_SG_Q5_0; 1384 nr0 = N_R0_Q5_0; 1385 } break; 1386 case GGML_TYPE_Q5_1: 1387 { 1388 nsg = N_SG_Q5_1; 1389 nr0 = N_R0_Q5_1; 1390 } break; 1391 case GGML_TYPE_Q8_0: 1392 { 1393 nsg = N_SG_Q8_0; 1394 nr0 = N_R0_Q8_0; 1395 smem = 32*sizeof(float)*N_R0_Q8_0; 1396 } break; 1397 case GGML_TYPE_MXFP4: 1398 { 1399 nsg = N_SG_MXFP4; 1400 nr0 = N_R0_MXFP4; 1401 } break; 1402 case GGML_TYPE_Q2_K: 1403 { 1404 nsg = N_SG_Q2_K; 1405 nr0 = N_R0_Q2_K; 1406 } break; 1407 case GGML_TYPE_Q3_K: 1408 { 1409 nsg = N_SG_Q3_K; 1410 nr0 = N_R0_Q3_K; 1411 } break; 1412 case GGML_TYPE_Q4_K: 1413 { 1414 nsg = N_SG_Q4_K; 1415 nr0 = N_R0_Q4_K; 1416 } break; 1417 case GGML_TYPE_Q5_K: 1418 { 1419 nsg = N_SG_Q5_K; 1420 nr0 = N_R0_Q5_K; 1421 } break; 1422 case GGML_TYPE_Q6_K: 1423 { 1424 nsg = N_SG_Q6_K; 1425 nr0 = N_R0_Q6_K; 1426 } break; 1427 case GGML_TYPE_IQ2_XXS: 1428 { 1429 nsg = N_SG_IQ2_XXS; 1430 nr0 = N_R0_IQ2_XXS; 1431 smem = 256*8+128; 1432 1433 const int nb32 = ne00/32; 1434 if (nb32 < 32 && (32 % nb32) == 0) { 1435 nr0 = N_R0_IQ2_XXS_SPLIT; 1436 split = true; 1437 } 1438 } break; 1439 case GGML_TYPE_IQ2_XS: 1440 { 1441 nsg = N_SG_IQ2_XS; 1442 nr0 = N_R0_IQ2_XS; 1443 smem = 512*8+128; 1444 1445 const int nb32 = ne00/32; 1446 if (nb32 < 32 && (32 % nb32) == 0) { 1447 nr0 = N_R0_IQ2_XS_SPLIT; 1448 split = true; 1449 } 1450 } break; 1451 case GGML_TYPE_IQ3_XXS: 1452 { 1453 nsg = N_SG_IQ3_XXS; 1454 nr0 = N_R0_IQ3_XXS; 1455 smem = 256*4+128; 1456 1457 // split the rows across threads when there are fewer than 32 chunks per row 1458 const int nb32 = ne00/32; 1459 if (nb32 < 32 && (32 % nb32) == 0) { 1460 nr0 = N_R0_IQ3_XXS_SPLIT; 1461 split = true; 1462 } 1463 } break; 1464 case GGML_TYPE_IQ3_S: 1465 { 1466 nsg = N_SG_IQ3_S; 1467 nr0 = N_R0_IQ3_S; 1468 smem = 512*4; 1469 1470 const int nb32 = ne00/32; 1471 if (nb32 < 32 && (32 % nb32) == 0) { 1472 nr0 = N_R0_IQ3_S_SPLIT; 1473 split = true; 1474 } 1475 } break; 1476 case GGML_TYPE_IQ2_S: 1477 { 1478 nsg = N_SG_IQ2_S; 1479 nr0 = N_R0_IQ2_S; 1480 1481 const int nb32 = ne00/32; 1482 if (nb32 < 32 && (32 % nb32) == 0) { 1483 nr0 = N_R0_IQ2_S_SPLIT; 1484 split = true; 1485 } 1486 } break; 1487 case GGML_TYPE_IQ1_S: 1488 { 1489 nsg = N_SG_IQ1_S; 1490 nr0 = N_R0_IQ1_S; 1491 1492 const int nb32 = ne00/32; 1493 if (nb32 < 32 && (32 % nb32) == 0) { 1494 nr0 = N_R0_IQ1_S_SPLIT; 1495 split = true; 1496 } 1497 } break; 1498 case GGML_TYPE_IQ1_M: 1499 { 1500 nsg = N_SG_IQ1_M; 1501 nr0 = N_R0_IQ1_M; 1502 1503 const int nb32 = ne00/32; 1504 if (nb32 < 32 && (32 % nb32) == 0) { 1505 nr0 = N_R0_IQ1_M_SPLIT; 1506 split = true; 1507 } 1508 } break; 1509 case GGML_TYPE_IQ4_NL: 1510 { 1511 nsg = N_SG_IQ4_NL; 1512 nr0 = N_R0_IQ4_NL; 1513 smem = 32*sizeof(float); 1514 } break; 1515 case GGML_TYPE_IQ4_XS: 1516 { 1517 nsg = N_SG_IQ4_XS; 1518 nr0 = N_R0_IQ4_XS; 1519 smem = 32*sizeof(float); 1520 } break; 1521 case GGML_TYPE_TQ2_0: 1522 { 1523 nsg = N_SG_TQ2_0; 1524 nr0 = N_R0_TQ2_0; 1525 } break; 1526 default: 1527 { 1528 GGML_LOG_ERROR("Asserting on type %d\n", (int)op->src[2]->type); 1529 GGML_ABORT("not implemented"); 1530 } 1531 }; 1532 1533 snprintf(base, 256, "kernel_mul_mv_id_%s_%s%s", ggml_type_name(tsrc0), ggml_type_name(tsrc1), suffix); 1534 snprintf(name, 256, "%s_nsg=%d_split=%d", base, nsg, split); 1535 1536 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 1537 if (!res.pipeline) { 1538 ggml_metal_cv_t cv = ggml_metal_cv_init(); 1539 1540 ggml_metal_cv_set_int16(cv, nsg, FC_MUL_MV + 0); 1541 ggml_metal_cv_set_int16(cv, 1, FC_MUL_MV + 2); 1542 ggml_metal_cv_set_int16(cv, 1, FC_MUL_MV + 3); 1543 ggml_metal_cv_set_int16(cv, 1, FC_MUL_MV + 4); 1544 ggml_metal_cv_set_bool (cv, split, FC_MUL_MV + 5); 1545 1546 res = ggml_metal_library_compile_pipeline(lib, base, name, cv); 1547 1548 ggml_metal_cv_free(cv); 1549 } 1550 1551 res.nr0 = nr0; 1552 res.nr1 = nr1; 1553 res.nsg = nsg; 1554 res.smem = smem; 1555 1556 return res; 1557 } 1558 1559 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_argmax(ggml_metal_library_t lib, const ggml_tensor * op) { 1560 GGML_ASSERT(op->src[0]->type == GGML_TYPE_F32); 1561 GGML_ASSERT(ggml_is_contiguous_1(op->src[0])); 1562 GGML_ASSERT(op->src[0]->nb[0] == ggml_type_size(op->src[0]->type)); 1563 1564 char base[256]; 1565 char name[256]; 1566 1567 snprintf(base, 256, "kernel_argmax_%s", ggml_type_name(op->src[0]->type)); 1568 snprintf(name, 256, "%s", base); 1569 1570 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 1571 if (!res.pipeline) { 1572 res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr); 1573 } 1574 1575 res.smem = 32*(sizeof(float) + sizeof(int32_t)); 1576 1577 return res; 1578 } 1579 1580 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_argmax_part(ggml_metal_library_t lib) { 1581 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, "kernel_argmax_part_f32"); 1582 if (!res.pipeline) { 1583 res = ggml_metal_library_compile_pipeline(lib, "kernel_argmax_part_f32", "kernel_argmax_part_f32", nullptr); 1584 } 1585 1586 res.smem = 32*(sizeof(float) + sizeof(int32_t)); 1587 1588 return res; 1589 } 1590 1591 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_argmax_final(ggml_metal_library_t lib) { 1592 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, "kernel_argmax_final_f32"); 1593 if (!res.pipeline) { 1594 res = ggml_metal_library_compile_pipeline(lib, "kernel_argmax_final_f32", "kernel_argmax_final_f32", nullptr); 1595 } 1596 1597 res.smem = 32*(sizeof(float) + sizeof(int32_t)); 1598 1599 return res; 1600 } 1601 1602 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_argsort(ggml_metal_library_t lib, const ggml_tensor * op) { 1603 assert(op->op == GGML_OP_ARGSORT); 1604 1605 char base[256]; 1606 char name[256]; 1607 1608 ggml_sort_order order = (ggml_sort_order) op->op_params[0]; 1609 1610 const char * order_str = "undefined"; 1611 switch (order) { 1612 case GGML_SORT_ORDER_ASC: order_str = "asc"; break; 1613 case GGML_SORT_ORDER_DESC: order_str = "desc"; break; 1614 default: GGML_ABORT("fatal error"); 1615 }; 1616 1617 snprintf(base, 256, "kernel_argsort_%s_%s_%s", ggml_type_name(op->src[0]->type), ggml_type_name(op->type), order_str); 1618 snprintf(name, 256, "%s", base); 1619 1620 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 1621 if (!res.pipeline) { 1622 res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr); 1623 } 1624 1625 return res; 1626 } 1627 1628 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_argsort_merge(ggml_metal_library_t lib, const ggml_tensor * op) { 1629 assert(op->op == GGML_OP_ARGSORT); 1630 1631 char base[256]; 1632 char name[256]; 1633 1634 ggml_sort_order order = (ggml_sort_order) op->op_params[0]; 1635 1636 const char * order_str = "undefined"; 1637 switch (order) { 1638 case GGML_SORT_ORDER_ASC: order_str = "asc"; break; 1639 case GGML_SORT_ORDER_DESC: order_str = "desc"; break; 1640 default: GGML_ABORT("fatal error"); 1641 }; 1642 1643 snprintf(base, 256, "kernel_argsort_merge_%s_%s_%s", ggml_type_name(op->src[0]->type), ggml_type_name(op->type), order_str); 1644 snprintf(name, 256, "%s", base); 1645 1646 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 1647 if (!res.pipeline) { 1648 res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr); 1649 } 1650 1651 return res; 1652 } 1653 1654 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_fwht(ggml_metal_library_t lib, int n, ggml_type tsrc) { 1655 char base[256]; 1656 char name[256]; 1657 1658 snprintf(base, 256, "kernel_fwht_%s_%d", ggml_type_name(tsrc), n); 1659 snprintf(name, 256, "%s", base); 1660 1661 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 1662 if (!res.pipeline) { 1663 res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr); 1664 } 1665 1666 return res; 1667 } 1668 1669 // note: reuse the argsort kernel for the bitonic top_k fallback 1670 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_top_k(ggml_metal_library_t lib, const ggml_tensor * op) { 1671 assert(op->op == GGML_OP_TOP_K); 1672 1673 char base[256]; 1674 char name[256]; 1675 1676 // note: the top_k kernel is always descending order 1677 ggml_sort_order order = GGML_SORT_ORDER_DESC; 1678 1679 const char * order_str = "undefined"; 1680 switch (order) { 1681 case GGML_SORT_ORDER_ASC: order_str = "asc"; break; 1682 case GGML_SORT_ORDER_DESC: order_str = "desc"; break; 1683 default: GGML_ABORT("fatal error"); 1684 }; 1685 1686 snprintf(base, 256, "kernel_argsort_%s_%s_%s", ggml_type_name(op->src[0]->type), ggml_type_name(op->type), order_str); 1687 snprintf(name, 256, "%s", base); 1688 1689 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 1690 if (!res.pipeline) { 1691 res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr); 1692 } 1693 1694 return res; 1695 } 1696 1697 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_top_k_radix(ggml_metal_library_t lib, const ggml_tensor * op) { 1698 assert(op->op == GGML_OP_TOP_K); 1699 1700 char base[256]; 1701 char name[256]; 1702 1703 snprintf(base, 256, "kernel_top_k_%s_%s", ggml_type_name(op->src[0]->type), ggml_type_name(op->type)); 1704 snprintf(name, 256, "%s", base); 1705 1706 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 1707 if (!res.pipeline) { 1708 res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr); 1709 } 1710 1711 return res; 1712 } 1713 1714 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_top_k_merge(ggml_metal_library_t lib, const ggml_tensor * op) { 1715 assert(op->op == GGML_OP_TOP_K); 1716 1717 char base[256]; 1718 char name[256]; 1719 1720 ggml_sort_order order = GGML_SORT_ORDER_DESC; 1721 1722 const char * order_str = "undefined"; 1723 switch (order) { 1724 case GGML_SORT_ORDER_ASC: order_str = "asc"; break; 1725 case GGML_SORT_ORDER_DESC: order_str = "desc"; break; 1726 default: GGML_ABORT("fatal error"); 1727 }; 1728 1729 snprintf(base, 256, "kernel_argsort_merge_%s_%s_%s", ggml_type_name(op->src[0]->type), ggml_type_name(op->type), order_str); 1730 snprintf(name, 256, "%s", base); 1731 1732 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 1733 if (!res.pipeline) { 1734 res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr); 1735 } 1736 1737 return res; 1738 } 1739 1740 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_topk_moe( 1741 ggml_metal_library_t lib, int32_t n_expert, int32_t top_k, bool with_norm) { 1742 char base[256]; 1743 char name[256]; 1744 1745 snprintf(base, 256, "kernel_topk_moe_f32"); 1746 snprintf(name, 256, "%s_n_expert=%d_top_k=%d_with_norm=%d", base, n_expert, top_k, with_norm ? 1 : 0); 1747 1748 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 1749 if (!res.pipeline) { 1750 ggml_metal_cv_t cv = ggml_metal_cv_init(); 1751 ggml_metal_cv_set_bool (cv, with_norm, FC_TOPK_MOE + 0); 1752 ggml_metal_cv_set_int32(cv, n_expert, FC_TOPK_MOE + 1); 1753 ggml_metal_cv_set_int32(cv, top_k, FC_TOPK_MOE + 2); 1754 1755 res = ggml_metal_library_compile_pipeline(lib, base, name, cv); 1756 1757 ggml_metal_cv_free(cv); 1758 } 1759 1760 return res; 1761 } 1762 1763 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_moe_reduce(ggml_metal_library_t lib, int32_t n_expert_used) { 1764 char base[256]; 1765 char name[256]; 1766 1767 snprintf(base, 256, "kernel_moe_reduce_f32"); 1768 snprintf(name, 256, "%s_n_expert_used=%d", base, n_expert_used); 1769 1770 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 1771 if (!res.pipeline) { 1772 ggml_metal_cv_t cv = ggml_metal_cv_init(); 1773 ggml_metal_cv_set_int32(cv, n_expert_used, FC_MOE_REDUCE + 0); 1774 1775 res = ggml_metal_library_compile_pipeline(lib, base, name, cv); 1776 1777 ggml_metal_cv_free(cv); 1778 } 1779 1780 return res; 1781 } 1782 1783 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_flash_attn_ext_pad( 1784 ggml_metal_library_t lib, 1785 const struct ggml_tensor * op, 1786 bool has_mask, 1787 int32_t ncpsg) { 1788 assert(op->op == GGML_OP_FLASH_ATTN_EXT); 1789 GGML_UNUSED(op); 1790 1791 char base[256]; 1792 char name[256]; 1793 1794 snprintf(base, 256, "kernel_%s", 1795 "flash_attn_ext_pad"); 1796 1797 snprintf(name, 256, "%s_mask=%d_ncpsg=%d", 1798 base, 1799 has_mask, 1800 ncpsg); 1801 1802 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 1803 if (!res.pipeline) { 1804 ggml_metal_cv_t cv = ggml_metal_cv_init(); 1805 1806 ggml_metal_cv_set_bool(cv, has_mask, FC_FLASH_ATTN_EXT_PAD + 0); 1807 //ggml_metal_cv_set_bool(cv, has_sinks, FC_FLASH_ATTN_EXT_PAD + 1); 1808 //ggml_metal_cv_set_bool(cv, has_bias, FC_FLASH_ATTN_EXT_PAD + 2); 1809 //ggml_metal_cv_set_bool(cv, has_scap, FC_FLASH_ATTN_EXT_PAD + 3); 1810 1811 //ggml_metal_cv_set_int32(cv, ns10, FC_FLASH_ATTN_EXT_PAD + 20); 1812 //ggml_metal_cv_set_int32(cv, ns20, FC_FLASH_ATTN_EXT_PAD + 21); 1813 //ggml_metal_cv_set_int32(cv, nsg, FC_FLASH_ATTN_EXT_PAD + 22); 1814 //ggml_metal_cv_set_int32(cv, nwg, FC_FLASH_ATTN_EXT_PAD + 23); 1815 //ggml_metal_cv_set_int32(cv, nqptg, FC_FLASH_ATTN_EXT_PAD + 24); 1816 ggml_metal_cv_set_int32(cv, ncpsg, FC_FLASH_ATTN_EXT_PAD + 25); 1817 1818 res = ggml_metal_library_compile_pipeline(lib, base, name, cv); 1819 1820 ggml_metal_cv_free(cv); 1821 } 1822 1823 return res; 1824 } 1825 1826 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_flash_attn_ext_kv_f16( 1827 ggml_metal_library_t lib, 1828 const ggml_tensor * op) { 1829 assert(op->op == GGML_OP_FLASH_ATTN_EXT); 1830 1831 char base[256]; 1832 1833 snprintf(base, 256, "kernel_flash_attn_ext_kv_%s_f16", ggml_type_name(op->src[1]->type)); 1834 1835 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, base); 1836 if (!res.pipeline) { 1837 res = ggml_metal_library_compile_pipeline(lib, base, base, nullptr); 1838 } 1839 1840 return res; 1841 } 1842 1843 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_flash_attn_ext_blk( 1844 ggml_metal_library_t lib, 1845 const struct ggml_tensor * op, 1846 int32_t nqptg, 1847 int32_t ncpsg) { 1848 assert(op->op == GGML_OP_FLASH_ATTN_EXT); 1849 GGML_UNUSED(op); 1850 1851 char base[256]; 1852 char name[256]; 1853 1854 snprintf(base, 256, "kernel_%s", 1855 "flash_attn_ext_blk"); 1856 1857 snprintf(name, 256, "%s_nqptg=%d_ncpsg=%d", 1858 base, 1859 nqptg, 1860 ncpsg); 1861 1862 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 1863 if (!res.pipeline) { 1864 ggml_metal_cv_t cv = ggml_metal_cv_init(); 1865 1866 //ggml_metal_cv_set_bool(cv, has_mask, FC_FLASH_ATTN_EXT_BLK + 0); 1867 //ggml_metal_cv_set_bool(cv, has_sinks, FC_FLASH_ATTN_EXT_BLK + 1); 1868 //ggml_metal_cv_set_bool(cv, has_bias, FC_FLASH_ATTN_EXT_BLK + 2); 1869 //ggml_metal_cv_set_bool(cv, has_scap, FC_FLASH_ATTN_EXT_BLK + 3); 1870 1871 //ggml_metal_cv_set_int32(cv, ns10, FC_FLASH_ATTN_EXT_BLK + 20); 1872 //ggml_metal_cv_set_int32(cv, ns20, FC_FLASH_ATTN_EXT_BLK + 21); 1873 //ggml_metal_cv_set_int32(cv, nsg, FC_FLASH_ATTN_EXT_BLK + 22); 1874 //ggml_metal_cv_set_int32(cv, nwg, FC_FLASH_ATTN_EXT_BLK + 23); 1875 ggml_metal_cv_set_int32(cv, nqptg, FC_FLASH_ATTN_EXT_BLK + 24); 1876 ggml_metal_cv_set_int32(cv, ncpsg, FC_FLASH_ATTN_EXT_BLK + 25); 1877 1878 res = ggml_metal_library_compile_pipeline(lib, base, name, cv); 1879 1880 ggml_metal_cv_free(cv); 1881 } 1882 1883 return res; 1884 } 1885 1886 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_flash_attn_ext_tensor( 1887 ggml_metal_library_t lib, 1888 const ggml_tensor * op, 1889 bool has_mask, 1890 bool has_sinks, 1891 bool has_bias, 1892 bool has_scap) { 1893 assert(op->op == GGML_OP_FLASH_ATTN_EXT); 1894 1895 char base[256]; 1896 char name[256]; 1897 1898 const int32_t dk = (int32_t) op->src[1]->ne[0]; 1899 const int32_t dv = (int32_t) op->src[2]->ne[0]; 1900 1901 const char * type = op->src[1]->type == GGML_TYPE_BF16 ? "bf16" : "f16"; 1902 1903 snprintf(base, 256, "kernel_flash_attn_ext_tensor_%s_dk%d_dv%d", type, dk, dv); 1904 snprintf(name, 256, "%s_mask=%d_sinks=%d_bias=%d_scap=%d", base, has_mask, has_sinks, has_bias, has_scap); 1905 1906 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 1907 if (!res.pipeline) { 1908 ggml_metal_cv_t cv = ggml_metal_cv_init(); 1909 1910 ggml_metal_cv_set_bool(cv, has_mask, FC_FLASH_ATTN_EXT_TENSOR + 0); 1911 ggml_metal_cv_set_bool(cv, has_sinks, FC_FLASH_ATTN_EXT_TENSOR + 1); 1912 ggml_metal_cv_set_bool(cv, has_bias, FC_FLASH_ATTN_EXT_TENSOR + 2); 1913 ggml_metal_cv_set_bool(cv, has_scap, FC_FLASH_ATTN_EXT_TENSOR + 3); 1914 1915 res = ggml_metal_library_compile_pipeline(lib, base, name, cv); 1916 1917 ggml_metal_cv_free(cv); 1918 } 1919 1920 return res; 1921 } 1922 1923 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_flash_attn_ext( 1924 ggml_metal_library_t lib, 1925 const ggml_tensor * op, 1926 bool has_mask, 1927 bool has_sinks, 1928 bool has_bias, 1929 bool has_scap, 1930 bool has_kvpad, 1931 int32_t nsg, 1932 bool use_kv_f16, 1933 int32_t ns10, 1934 int32_t ns20) { 1935 assert(op->op == GGML_OP_FLASH_ATTN_EXT); 1936 1937 char base[256]; 1938 char name[256]; 1939 1940 const int32_t dk = (int32_t) op->src[1]->ne[0]; 1941 const int32_t dv = (int32_t) op->src[2]->ne[0]; 1942 1943 const char * type = use_kv_f16 ? "f16" : ggml_type_name(op->src[1]->type); 1944 1945 // do bounds checks for the mask? 1946 const bool bc_mask = op->src[3] && (op->src[3]->ne[1] % 8 != 0); 1947 1948 snprintf(base, 256, "kernel_%s_%s_dk%d_dv%d", 1949 "flash_attn_ext", 1950 type, 1951 dk, 1952 dv); 1953 1954 snprintf(name, 256, "%s_mask=%d_sinks=%d_bias=%d_scap=%d_kvpad=%d_bcm=%d_ns10=%d_ns20=%d_nsg=%d", 1955 base, 1956 has_mask, 1957 has_sinks, 1958 has_bias, 1959 has_scap, 1960 has_kvpad, 1961 bc_mask, 1962 ns10, 1963 ns20, 1964 nsg); 1965 1966 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 1967 if (!res.pipeline) { 1968 ggml_metal_cv_t cv = ggml_metal_cv_init(); 1969 1970 ggml_metal_cv_set_bool(cv, has_mask, FC_FLASH_ATTN_EXT + 0); 1971 ggml_metal_cv_set_bool(cv, has_sinks, FC_FLASH_ATTN_EXT + 1); 1972 ggml_metal_cv_set_bool(cv, has_bias, FC_FLASH_ATTN_EXT + 2); 1973 ggml_metal_cv_set_bool(cv, has_scap, FC_FLASH_ATTN_EXT + 3); 1974 ggml_metal_cv_set_bool(cv, has_kvpad, FC_FLASH_ATTN_EXT + 4); 1975 1976 ggml_metal_cv_set_bool(cv, bc_mask, FC_FLASH_ATTN_EXT + 10); 1977 1978 ggml_metal_cv_set_int32(cv, ns10, FC_FLASH_ATTN_EXT + 20); 1979 ggml_metal_cv_set_int32(cv, ns20, FC_FLASH_ATTN_EXT + 21); 1980 ggml_metal_cv_set_int32(cv, nsg, FC_FLASH_ATTN_EXT + 22); 1981 1982 res = ggml_metal_library_compile_pipeline(lib, base, name, cv); 1983 1984 ggml_metal_cv_free(cv); 1985 } 1986 1987 return res; 1988 } 1989 1990 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_flash_attn_ext_rows( 1991 ggml_metal_library_t lib, 1992 const ggml_tensor * op) { 1993 assert(op->op == GGML_OP_FLASH_ATTN_EXT); 1994 1995 const char * base = "kernel_flash_attn_ext_rows"; 1996 1997 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, base); 1998 if (!res.pipeline) { 1999 res = ggml_metal_library_compile_pipeline(lib, base, base, nullptr); 2000 } 2001 2002 GGML_UNUSED(op); 2003 2004 return res; 2005 } 2006 2007 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_flash_attn_ext_vec_idx( 2008 ggml_metal_library_t lib, 2009 const ggml_tensor * op) { 2010 assert(op->op == GGML_OP_FLASH_ATTN_EXT); 2011 assert(op->src[3]); 2012 2013 char name[256]; 2014 2015 snprintf(name, 256, "kernel_flash_attn_ext_vec_idx"); 2016 2017 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 2018 if (!res.pipeline) { 2019 res = ggml_metal_library_compile_pipeline(lib, name, name, nullptr); 2020 } 2021 2022 GGML_UNUSED(op); 2023 2024 return res; 2025 } 2026 2027 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_flash_attn_ext_vec( 2028 ggml_metal_library_t lib, 2029 const ggml_tensor * op, 2030 bool has_mask, 2031 bool has_sinks, 2032 bool has_bias, 2033 bool has_scap, 2034 bool has_kvpad, 2035 bool has_sparse, 2036 bool has_rows, 2037 int32_t nqpsg, 2038 int32_t ne, 2039 int32_t nsg, 2040 int32_t nwg, 2041 bool use_kv_f16, 2042 int32_t ns10, 2043 int32_t ns20) { 2044 assert(op->op == GGML_OP_FLASH_ATTN_EXT); 2045 2046 char base[256]; 2047 char name[256]; 2048 2049 const int32_t dk = (int32_t) op->src[1]->ne[0]; 2050 const int32_t dv = (int32_t) op->src[2]->ne[0]; 2051 2052 const char * type = use_kv_f16 ? "f16" : ggml_type_name(op->src[1]->type); 2053 2054 char qne_suffix[16] = {0}; 2055 if (!(nqpsg == 1 && ne == ggml_metal_tuning::fa_vec_baseline_ne(dk, dv))) { 2056 snprintf(qne_suffix, sizeof(qne_suffix), "_q%d_ne%d", nqpsg, ne); 2057 } 2058 2059 snprintf(base, 256, "kernel_%s_%s_dk%d_dv%d%s", 2060 "flash_attn_ext_vec", 2061 type, 2062 dk, 2063 dv, 2064 qne_suffix); 2065 2066 snprintf(name, 256, "%s_mask=%d_sink=%d_bias=%d_scap=%d_kvpad=%d_sparse=%d_rows=%d_ns10=%d_ns20=%d_nsg=%d_nwg=%d", 2067 base, 2068 has_mask, 2069 has_sinks, 2070 has_bias, 2071 has_scap, 2072 has_kvpad, 2073 has_sparse, 2074 has_rows, 2075 ns10, 2076 ns20, 2077 nsg, nwg); 2078 2079 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 2080 if (!res.pipeline) { 2081 ggml_metal_cv_t cv = ggml_metal_cv_init(); 2082 2083 ggml_metal_cv_set_bool(cv, has_mask, FC_FLASH_ATTN_EXT_VEC + 0); 2084 ggml_metal_cv_set_bool(cv, has_sinks, FC_FLASH_ATTN_EXT_VEC + 1); 2085 ggml_metal_cv_set_bool(cv, has_bias, FC_FLASH_ATTN_EXT_VEC + 2); 2086 ggml_metal_cv_set_bool(cv, has_scap, FC_FLASH_ATTN_EXT_VEC + 3); 2087 ggml_metal_cv_set_bool(cv, has_kvpad, FC_FLASH_ATTN_EXT_VEC + 4); 2088 ggml_metal_cv_set_bool(cv, has_sparse, FC_FLASH_ATTN_EXT_VEC + 5); 2089 ggml_metal_cv_set_bool(cv, has_rows, FC_FLASH_ATTN_EXT_VEC + 6); 2090 2091 ggml_metal_cv_set_int32(cv, ns10, FC_FLASH_ATTN_EXT_VEC + 20); 2092 ggml_metal_cv_set_int32(cv, ns20, FC_FLASH_ATTN_EXT_VEC + 21); 2093 ggml_metal_cv_set_int32(cv, nsg, FC_FLASH_ATTN_EXT_VEC + 22); 2094 ggml_metal_cv_set_int32(cv, nwg, FC_FLASH_ATTN_EXT_VEC + 23); 2095 2096 res = ggml_metal_library_compile_pipeline(lib, base, name, cv); 2097 2098 ggml_metal_cv_free(cv); 2099 } 2100 2101 return res; 2102 } 2103 2104 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_flash_attn_ext_vec_reduce( 2105 ggml_metal_library_t lib, 2106 const ggml_tensor * op, 2107 int32_t dv, 2108 int32_t nwg) { 2109 assert(op->op == GGML_OP_FLASH_ATTN_EXT); 2110 2111 char base[256]; 2112 char name[256]; 2113 2114 snprintf(base, 256, "kernel_flash_attn_ext_vec_reduce"); 2115 snprintf(name, 256, "%s_dv=%d_nwg=%d", base, dv, nwg); 2116 2117 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 2118 if (!res.pipeline) { 2119 ggml_metal_cv_t cv = ggml_metal_cv_init(); 2120 2121 ggml_metal_cv_set_int32(cv, dv, FC_FLASH_ATTN_EXT_VEC_REDUCE + 0); 2122 ggml_metal_cv_set_int32(cv, nwg, FC_FLASH_ATTN_EXT_VEC_REDUCE + 1); 2123 2124 res = ggml_metal_library_compile_pipeline(lib, base, name, cv); 2125 2126 ggml_metal_cv_free(cv); 2127 } 2128 2129 return res; 2130 2131 GGML_UNUSED(op); 2132 } 2133 2134 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_bin(ggml_metal_library_t lib, const ggml_tensor * op, int32_t n_fuse) { 2135 char base[256]; 2136 char name[256]; 2137 2138 int op_num = -1; 2139 2140 switch (op->op) { 2141 case GGML_OP_ADD: op_num = 0; break; 2142 case GGML_OP_SUB: op_num = 1; break; 2143 case GGML_OP_MUL: op_num = 2; break; 2144 case GGML_OP_DIV: op_num = 3; break; 2145 default: GGML_ABORT("fatal error"); 2146 }; 2147 2148 const char * t0_str = ggml_type_name(op->src[0]->type); 2149 const char * t1_str = ggml_type_name(op->src[1]->type); 2150 const char * t_str = ggml_type_name(op->type); 2151 2152 const bool is_c4 = (op->src[0]->ne[0] % 4 == 0) && (op->src[1]->ne[0] % 4 == 0); 2153 2154 const bool is_cb = op->src[0]->ne[0] != op->src[1]->ne[0]; 2155 const bool is_rb = ggml_is_contiguous(op->src[0]) && ggml_is_contiguous(op->src[1]) && (ggml_nrows(op->src[1]) == 1) && ggml_nelements(op) < 65536; 2156 2157 snprintf(base, 256, "kernel_bin_fuse_%s_%s_%s%s", t0_str, t1_str, t_str, is_c4 ? "_4" : ""); 2158 snprintf(name, 256, "%s_op=%d_nf=%d_rb=%d_cb=%d", base, op_num, n_fuse, is_rb, is_cb); 2159 2160 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 2161 if (!res.pipeline) { 2162 ggml_metal_cv_t cv = ggml_metal_cv_init(); 2163 2164 ggml_metal_cv_set_int16(cv, op_num, FC_BIN + 0); 2165 ggml_metal_cv_set_int16(cv, n_fuse, FC_BIN + 1); 2166 ggml_metal_cv_set_bool (cv, is_rb, FC_BIN + 2); 2167 ggml_metal_cv_set_bool (cv, is_cb, FC_BIN + 3); 2168 2169 res = ggml_metal_library_compile_pipeline(lib, base, name, cv); 2170 2171 ggml_metal_cv_free(cv); 2172 } 2173 2174 res.c4 = is_c4; 2175 res.cnt = is_rb; 2176 2177 return res; 2178 } 2179 2180 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_bin_one(ggml_metal_library_t lib, ggml_op op) { 2181 char base[256]; 2182 char name[256]; 2183 2184 int op_num = -1; 2185 2186 switch (op) { 2187 case GGML_OP_ADD: op_num = 0; break; 2188 case GGML_OP_SUB: op_num = 1; break; 2189 case GGML_OP_MUL: op_num = 2; break; 2190 case GGML_OP_DIV: op_num = 3; break; 2191 default: GGML_ABORT("fatal error"); 2192 }; 2193 2194 snprintf(base, 256, "kernel_bin_fuse_%s_%s_%s", "f32", "f32", "f32"); 2195 snprintf(name, 256, "%s_op=%d_nf=%d", base, op_num, 1); 2196 2197 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 2198 if (!res.pipeline) { 2199 ggml_metal_cv_t cv = ggml_metal_cv_init(); 2200 2201 ggml_metal_cv_set_int16(cv, op_num, FC_BIN + 0); 2202 ggml_metal_cv_set_int16(cv, 1, FC_BIN + 1); 2203 ggml_metal_cv_set_bool (cv, false, FC_BIN + 2); 2204 2205 res = ggml_metal_library_compile_pipeline(lib, base, name, cv); 2206 2207 ggml_metal_cv_free(cv); 2208 } 2209 2210 return res; 2211 } 2212 2213 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_l2_norm(ggml_metal_library_t lib, const ggml_tensor * op) { 2214 assert(op->op == GGML_OP_L2_NORM); 2215 2216 char base[256]; 2217 char name[256]; 2218 2219 const bool is_c4 = op->src[0]->ne[0] % 4 == 0; 2220 2221 const char * t0_str = ggml_type_name(op->src[0]->type); 2222 const char * t_str = ggml_type_name(op->type); 2223 2224 snprintf(base, 256, "kernel_l2_norm_%s_%s%s", t0_str, t_str, is_c4 ? "_4" : ""); 2225 snprintf(name, 256, "%s", base); 2226 2227 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 2228 if (!res.pipeline) { 2229 res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr); 2230 } 2231 2232 res.c4 = is_c4; 2233 res.smem = 32*sizeof(float); 2234 2235 return res; 2236 } 2237 2238 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_group_norm(ggml_metal_library_t lib, const ggml_tensor * op) { 2239 assert(op->op == GGML_OP_GROUP_NORM); 2240 2241 GGML_ASSERT(ggml_is_contiguous(op->src[0])); 2242 2243 char base[256]; 2244 char name[256]; 2245 2246 snprintf(base, 256, "kernel_group_norm_f32"); 2247 snprintf(name, 256, "%s", base); 2248 2249 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 2250 if (!res.pipeline) { 2251 res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr); 2252 } 2253 2254 res.smem = 32*sizeof(float); 2255 2256 return res; 2257 } 2258 2259 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_norm(ggml_metal_library_t lib, const ggml_tensor * op, int n_fuse) { 2260 assert(op->op == GGML_OP_NORM || op->op == GGML_OP_RMS_NORM); 2261 2262 GGML_ASSERT(ggml_is_contiguous_rows(op->src[0])); 2263 2264 char base[256]; 2265 char name[256]; 2266 2267 const char * suffix = ""; 2268 if (op->ne[0] % 4 == 0) { 2269 suffix = "_4"; 2270 } 2271 2272 switch (op->op) { 2273 case GGML_OP_NORM: 2274 switch (n_fuse) { 2275 case 1: snprintf(base, 256, "kernel_norm_f32%s", suffix); break; 2276 case 2: snprintf(base, 256, "kernel_norm_mul_f32%s", suffix); break; 2277 case 3: snprintf(base, 256, "kernel_norm_mul_add_f32%s", suffix); break; 2278 default: GGML_ABORT("fatal error"); 2279 } break; 2280 case GGML_OP_RMS_NORM: 2281 switch (n_fuse) { 2282 case 1: snprintf(base, 256, "kernel_rms_norm_f32%s", suffix); break; 2283 case 2: snprintf(base, 256, "kernel_rms_norm_mul_f32%s", suffix); break; 2284 case 3: snprintf(base, 256, "kernel_rms_norm_mul_add_f32%s", suffix); break; 2285 default: GGML_ABORT("fatal error"); 2286 } break; 2287 default: GGML_ABORT("fatal error"); 2288 } 2289 2290 snprintf(name, 256, "%s", base); 2291 2292 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 2293 if (!res.pipeline) { 2294 ggml_metal_cv_t cv = ggml_metal_cv_init(); 2295 ggml_metal_cv_set_bool(cv, false, FC_NORM + 0); 2296 2297 res = ggml_metal_library_compile_pipeline(lib, base, name, cv); 2298 2299 ggml_metal_cv_free(cv); 2300 } 2301 2302 res.smem = 32*sizeof(float); 2303 2304 return res; 2305 } 2306 2307 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_norm_scale(ggml_metal_library_t lib, const ggml_tensor * op) { 2308 assert(op->op == GGML_OP_NORM || op->op == GGML_OP_RMS_NORM); 2309 2310 GGML_ASSERT(ggml_is_contiguous_rows(op->src[0])); 2311 2312 char base[256]; 2313 char name[256]; 2314 2315 const char * suffix = ""; 2316 if (op->ne[0] % 4 == 0) { 2317 suffix = "_4"; 2318 } 2319 2320 switch (op->op) { 2321 case GGML_OP_NORM: snprintf(base, 256, "kernel_norm_mul_f32%s", suffix); break; 2322 case GGML_OP_RMS_NORM: snprintf(base, 256, "kernel_rms_norm_mul_f32%s", suffix); break; 2323 default: GGML_ABORT("fatal error"); 2324 } 2325 2326 snprintf(name, 256, "%s_use_scale", base); 2327 2328 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 2329 if (!res.pipeline) { 2330 ggml_metal_cv_t cv = ggml_metal_cv_init(); 2331 ggml_metal_cv_set_bool(cv, true, FC_NORM + 0); 2332 2333 res = ggml_metal_library_compile_pipeline(lib, base, name, cv); 2334 2335 ggml_metal_cv_free(cv); 2336 } 2337 2338 res.smem = 32*sizeof(float); 2339 2340 return res; 2341 } 2342 2343 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_rope(ggml_metal_library_t lib, const ggml_tensor * op) { 2344 assert(op->op == GGML_OP_ROPE || op->op == GGML_OP_ROPE_BACK); 2345 2346 const bool is_back = op->op == GGML_OP_ROPE_BACK; 2347 2348 char base[256]; 2349 char name[256]; 2350 2351 const int mode = ((const int32_t *) op->op_params)[2]; 2352 2353 const bool is_neox = mode & GGML_ROPE_TYPE_NEOX; 2354 const bool is_mrope = mode & GGML_ROPE_TYPE_MROPE; 2355 const bool is_imrope = mode == GGML_ROPE_TYPE_IMROPE; 2356 const bool is_vision = mode == GGML_ROPE_TYPE_VISION; 2357 2358 if (is_neox) { 2359 snprintf(base, 256, "kernel_rope_neox_%s", ggml_type_name(op->src[0]->type)); 2360 } else if ((is_mrope || is_imrope) && !is_vision) { 2361 GGML_ASSERT(op->src[1]->ne[0]*4 >= op->src[0]->ne[2]); // need at least 4 pos per token 2362 snprintf(base, 256, "kernel_rope_multi_%s", ggml_type_name(op->src[0]->type)); 2363 } else if (is_vision) { 2364 GGML_ASSERT(op->src[1]->ne[0]*4 >= op->src[0]->ne[2]); // need at least 4 pos per token 2365 snprintf(base, 256, "kernel_rope_vision_%s", ggml_type_name(op->src[0]->type)); 2366 } else { 2367 snprintf(base, 256, "kernel_rope_norm_%s", ggml_type_name(op->src[0]->type)); 2368 } 2369 2370 snprintf(name, 256, "%s_imrope=%d_is_back=%d", base, is_imrope ? 1 : 0, is_back ? 1 : 0); 2371 2372 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 2373 if (!res.pipeline) { 2374 ggml_metal_cv_t cv = ggml_metal_cv_init(); 2375 2376 ggml_metal_cv_set_bool(cv, is_imrope, FC_ROPE + 0); 2377 ggml_metal_cv_set_bool(cv, is_back, FC_ROPE + 1); 2378 2379 res = ggml_metal_library_compile_pipeline(lib, base, name, cv); 2380 2381 ggml_metal_cv_free(cv); 2382 } 2383 2384 return res; 2385 } 2386 2387 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_im2col(ggml_metal_library_t lib, const ggml_tensor * op) { 2388 assert(op->op == GGML_OP_IM2COL); 2389 2390 GGML_TENSOR_LOCALS(int64_t, ne0, op->src[0], ne); 2391 2392 GGML_ASSERT(ggml_is_contiguous(op->src[1])); 2393 GGML_ASSERT(op->src[1]->type == GGML_TYPE_F32); 2394 GGML_ASSERT(op->type == GGML_TYPE_F16 || op->type == GGML_TYPE_F32); 2395 2396 const bool is_2D = ((const int32_t *)(op->op_params))[6] == 1; 2397 const int64_t KH = is_2D ? ne01 : 1; 2398 const int64_t KW = ne00; 2399 2400 char base[256]; 2401 char name[256]; 2402 2403 if (KH*KW <= 1024) { 2404 snprintf(base, 256, "kernel_im2col_%s", ggml_type_name(op->type)); 2405 } else { 2406 snprintf(base, 256, "kernel_im2col_ext_%s", ggml_type_name(op->type)); 2407 } 2408 snprintf(name, 256, "%s", base); 2409 2410 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 2411 if (!res.pipeline) { 2412 res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr); 2413 } 2414 2415 return res; 2416 } 2417 2418 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_conv_transpose_1d(ggml_metal_library_t lib, const ggml_tensor * op) { 2419 assert(op->op == GGML_OP_CONV_TRANSPOSE_1D); 2420 2421 GGML_ASSERT(ggml_is_contiguous(op->src[0])); 2422 GGML_ASSERT(ggml_is_contiguous(op->src[1])); 2423 GGML_ASSERT(op->src[0]->type == GGML_TYPE_F16 || op->src[0]->type == GGML_TYPE_F32); 2424 GGML_ASSERT(op->src[1]->type == GGML_TYPE_F32); 2425 GGML_ASSERT(op->type == GGML_TYPE_F32); 2426 2427 char base[256]; 2428 char name[256]; 2429 2430 snprintf(base, 256, "kernel_conv_transpose_1d_%s_%s", ggml_type_name(op->src[0]->type), ggml_type_name(op->src[1]->type)); 2431 snprintf(name, 256, "%s", base); 2432 2433 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 2434 if (!res.pipeline) { 2435 res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr); 2436 } 2437 2438 return res; 2439 } 2440 2441 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_col2im_1d(ggml_metal_library_t lib, const ggml_tensor * op) { 2442 assert(op->op == GGML_OP_COL2IM_1D); 2443 2444 GGML_ASSERT(ggml_is_contiguous(op->src[0])); 2445 GGML_ASSERT(op->src[0]->type == GGML_TYPE_F32 || op->src[0]->type == GGML_TYPE_F16 || op->src[0]->type == GGML_TYPE_BF16); 2446 2447 char base[256]; 2448 char name[256]; 2449 2450 snprintf(base, 256, "kernel_col2im_1d_%s", ggml_type_name(op->src[0]->type)); 2451 snprintf(name, 256, "%s", base); 2452 2453 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 2454 if (!res.pipeline) { 2455 res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr); 2456 } 2457 2458 return res; 2459 } 2460 2461 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_snake(ggml_metal_library_t lib, enum ggml_type type) { 2462 GGML_ASSERT(type == GGML_TYPE_F32 || type == GGML_TYPE_F16 || type == GGML_TYPE_BF16); 2463 2464 char base[256]; 2465 char name[256]; 2466 2467 snprintf(base, 256, "kernel_snake_%s", ggml_type_name(type)); 2468 snprintf(name, 256, "%s", base); 2469 2470 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 2471 if (!res.pipeline) { 2472 res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr); 2473 } 2474 2475 return res; 2476 } 2477 2478 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_conv_transpose_2d(ggml_metal_library_t lib, const ggml_tensor * op) { 2479 assert(op->op == GGML_OP_CONV_TRANSPOSE_2D); 2480 2481 GGML_ASSERT(ggml_is_contiguous(op->src[0])); 2482 GGML_ASSERT(ggml_is_contiguous(op->src[1])); 2483 GGML_ASSERT(op->src[0]->type == GGML_TYPE_F16 || op->src[0]->type == GGML_TYPE_F32); 2484 GGML_ASSERT(op->src[1]->type == GGML_TYPE_F32); 2485 GGML_ASSERT(op->type == GGML_TYPE_F32); 2486 2487 char base[256]; 2488 char name[256]; 2489 2490 snprintf(base, 256, "kernel_conv_transpose_2d_%s_%s", ggml_type_name(op->src[0]->type), ggml_type_name(op->src[1]->type)); 2491 snprintf(name, 256, "%s", base); 2492 2493 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 2494 if (!res.pipeline) { 2495 res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr); 2496 } 2497 2498 return res; 2499 } 2500 2501 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_conv_2d(ggml_metal_library_t lib, const ggml_tensor * op) { 2502 assert(op->op == GGML_OP_CONV_2D); 2503 2504 GGML_ASSERT(ggml_is_contiguous(op->src[0])); 2505 GGML_ASSERT(op->src[0]->type == GGML_TYPE_F16 || op->src[0]->type == GGML_TYPE_F32); 2506 GGML_ASSERT(op->src[1]->type == GGML_TYPE_F32); 2507 GGML_ASSERT(op->type == GGML_TYPE_F32); 2508 2509 char base[256]; 2510 char name[256]; 2511 2512 snprintf(base, 256, "kernel_conv_2d_%s_%s", ggml_type_name(op->src[0]->type), ggml_type_name(op->src[1]->type)); 2513 snprintf(name, 256, "%s", base); 2514 2515 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 2516 if (!res.pipeline) { 2517 res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr); 2518 } 2519 2520 return res; 2521 } 2522 2523 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_conv_2d_dw(ggml_metal_library_t lib, const ggml_tensor * op, bool tiled) { 2524 assert(op->op == GGML_OP_CONV_2D_DW); 2525 2526 GGML_ASSERT(op->src[0]->type == GGML_TYPE_F16 || op->src[0]->type == GGML_TYPE_F32); 2527 GGML_ASSERT(op->src[1]->type == GGML_TYPE_F32); 2528 GGML_ASSERT(op->type == GGML_TYPE_F32); 2529 2530 char base[256]; 2531 char name[256]; 2532 2533 snprintf(base, 256, "kernel_conv_2d_dw%s_%s_%s", 2534 tiled ? "_tiled" : "", 2535 ggml_type_name(op->src[0]->type), ggml_type_name(op->src[1]->type)); 2536 snprintf(name, 256, "%s", base); 2537 2538 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 2539 if (!res.pipeline) { 2540 res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr); 2541 } 2542 2543 return res; 2544 } 2545 2546 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_conv_3d(ggml_metal_library_t lib, const ggml_tensor * op) { 2547 assert(op->op == GGML_OP_CONV_3D); 2548 2549 GGML_ASSERT(ggml_is_contiguous(op->src[0])); 2550 GGML_ASSERT(op->src[0]->type == GGML_TYPE_F16 || op->src[0]->type == GGML_TYPE_F32); 2551 GGML_ASSERT(op->src[1]->type == GGML_TYPE_F32); 2552 GGML_ASSERT(op->type == GGML_TYPE_F32); 2553 2554 char base[256]; 2555 char name[256]; 2556 2557 snprintf(base, 256, "kernel_conv_3d_%s_%s", ggml_type_name(op->src[0]->type), ggml_type_name(op->src[1]->type)); 2558 snprintf(name, 256, "%s", base); 2559 2560 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 2561 if (!res.pipeline) { 2562 res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr); 2563 } 2564 2565 return res; 2566 } 2567 2568 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_upscale(ggml_metal_library_t lib, const ggml_tensor * op) { 2569 assert(op->op == GGML_OP_UPSCALE); 2570 2571 char base[256]; 2572 char name[256]; 2573 2574 const int32_t mode_flags = ggml_get_op_params_i32(op, 0); 2575 const ggml_scale_mode mode = (ggml_scale_mode) (mode_flags & 0xFF); 2576 2577 const bool antialias = (mode_flags & GGML_SCALE_FLAG_ANTIALIAS); 2578 2579 if (mode == GGML_SCALE_MODE_BILINEAR) { 2580 snprintf(base, 256, "kernel_upscale_bilinear_%s", ggml_type_name(op->src[0]->type)); 2581 } else if (mode == GGML_SCALE_MODE_BICUBIC) { 2582 snprintf(base, 256, "kernel_upscale_bicubic_%s", ggml_type_name(op->src[0]->type)); 2583 } else { 2584 snprintf(base, 256, "kernel_upscale_nearest_%s", ggml_type_name(op->src[0]->type)); 2585 } 2586 snprintf(name, 256, "%s_aa=%d", base, antialias); 2587 2588 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 2589 if (!res.pipeline) { 2590 ggml_metal_cv_t cv = ggml_metal_cv_init(); 2591 2592 ggml_metal_cv_set_bool(cv, antialias, FC_UPSCALE + 0); 2593 2594 res = ggml_metal_library_compile_pipeline(lib, base, name, cv); 2595 2596 ggml_metal_cv_free(cv); 2597 } 2598 2599 return res; 2600 } 2601 2602 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_roll(ggml_metal_library_t lib, const ggml_tensor * op) { 2603 assert(op->op == GGML_OP_ROLL); 2604 2605 char base[256]; 2606 char name[256]; 2607 2608 snprintf(base, 256, "kernel_roll_%s", ggml_type_name(op->src[0]->type)); 2609 snprintf(name, 256, "%s", base); 2610 2611 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 2612 if (!res.pipeline) { 2613 res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr); 2614 } 2615 2616 return res; 2617 } 2618 2619 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_pad(ggml_metal_library_t lib, const ggml_tensor * op) { 2620 assert(op->op == GGML_OP_PAD); 2621 2622 char base[256]; 2623 char name[256]; 2624 2625 const bool circular = ggml_get_op_params_i32(op, 8) != 0; 2626 2627 snprintf(base, 256, "kernel_pad_%s", ggml_type_name(op->src[0]->type)); 2628 snprintf(name, 256, "%s_circular=%d", base, circular); 2629 2630 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 2631 if (!res.pipeline) { 2632 ggml_metal_cv_t cv = ggml_metal_cv_init(); 2633 2634 ggml_metal_cv_set_bool(cv, circular, FC_PAD + 0); 2635 2636 res = ggml_metal_library_compile_pipeline(lib, base, name, cv); 2637 2638 ggml_metal_cv_free(cv); 2639 } 2640 2641 return res; 2642 } 2643 2644 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_pad_reflect_1d(ggml_metal_library_t lib, const ggml_tensor * op) { 2645 assert(op->op == GGML_OP_PAD_REFLECT_1D); 2646 2647 char base[256]; 2648 char name[256]; 2649 2650 snprintf(base, 256, "kernel_pad_reflect_1d_%s", ggml_type_name(op->src[0]->type)); 2651 snprintf(name, 256, "%s", base); 2652 2653 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 2654 if (!res.pipeline) { 2655 res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr); 2656 } 2657 2658 return res; 2659 } 2660 2661 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_arange(ggml_metal_library_t lib, const ggml_tensor * op) { 2662 assert(op->op == GGML_OP_ARANGE); 2663 2664 char base[256]; 2665 char name[256]; 2666 2667 snprintf(base, 256, "kernel_arange_%s", ggml_type_name(op->type)); 2668 snprintf(name, 256, "%s", base); 2669 2670 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 2671 if (!res.pipeline) { 2672 res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr); 2673 } 2674 2675 return res; 2676 } 2677 2678 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_timestep_embedding(ggml_metal_library_t lib, const ggml_tensor * op) { 2679 assert(op->op == GGML_OP_TIMESTEP_EMBEDDING); 2680 2681 char base[256]; 2682 char name[256]; 2683 2684 snprintf(base, 256, "kernel_timestep_embedding_%s", ggml_type_name(op->src[0]->type)); 2685 snprintf(name, 256, "%s", base); 2686 2687 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 2688 if (!res.pipeline) { 2689 res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr); 2690 } 2691 2692 return res; 2693 } 2694 2695 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_opt_step_adamw(ggml_metal_library_t lib, const ggml_tensor * op) { 2696 assert(op->op == GGML_OP_OPT_STEP_ADAMW); 2697 2698 char base[256]; 2699 char name[256]; 2700 2701 snprintf(base, 256, "kernel_opt_step_adamw_%s", ggml_type_name(op->src[0]->type)); 2702 snprintf(name, 256, "%s", base); 2703 2704 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 2705 if (!res.pipeline) { 2706 res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr); 2707 } 2708 2709 return res; 2710 } 2711 2712 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_opt_step_sgd(ggml_metal_library_t lib, const ggml_tensor * op) { 2713 assert(op->op == GGML_OP_OPT_STEP_SGD); 2714 2715 char base[256]; 2716 char name[256]; 2717 2718 snprintf(base, 256, "kernel_opt_step_sgd_%s", ggml_type_name(op->src[0]->type)); 2719 snprintf(name, 256, "%s", base); 2720 2721 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 2722 if (!res.pipeline) { 2723 res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr); 2724 } 2725 2726 return res; 2727 } 2728 2729 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_silu_back(ggml_metal_library_t lib, const ggml_tensor * op) { 2730 assert(op->op == GGML_OP_SILU_BACK); 2731 2732 char base[256]; 2733 char name[256]; 2734 2735 snprintf(base, 256, "kernel_silu_back_%s", ggml_type_name(op->src[0]->type)); 2736 snprintf(name, 256, "%s", base); 2737 2738 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 2739 if (!res.pipeline) { 2740 res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr); 2741 } 2742 2743 return res; 2744 } 2745 2746 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_memset(ggml_metal_library_t lib, const ggml_tensor * op) { 2747 GGML_ASSERT(op->type == GGML_TYPE_I64); 2748 2749 char base[256]; 2750 char name[256]; 2751 2752 snprintf(base, 256, "kernel_memset_%s", ggml_type_name(op->type)); 2753 snprintf(name, 256, "%s", base); 2754 2755 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 2756 if (!res.pipeline) { 2757 res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr); 2758 } 2759 2760 return res; 2761 } 2762 2763 ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_count_equal(ggml_metal_library_t lib, const ggml_tensor * op) { 2764 assert(op->op == GGML_OP_COUNT_EQUAL); 2765 2766 GGML_TENSOR_LOCALS(int64_t, ne0, op->src[0], ne); 2767 2768 GGML_ASSERT(op->src[0]->type == op->src[1]->type); 2769 GGML_ASSERT(op->src[0]->type == GGML_TYPE_I32); 2770 GGML_ASSERT(op->type == GGML_TYPE_I64); 2771 2772 // note: the kernel only supports i32 output due to metal atomic add only supporting atomic_int 2773 GGML_ASSERT(ggml_nelements(op->src[0]) < (1LL << 31)); 2774 2775 char base[256]; 2776 char name[256]; 2777 2778 int nsg = 1; 2779 while (32*nsg < ne00 && nsg < 32) { 2780 nsg *= 2; 2781 } 2782 2783 snprintf(base, 256, "kernel_count_equal_%s", ggml_type_name(op->src[0]->type)); 2784 snprintf(name, 256, "%s_nsg=%d", base, nsg); 2785 2786 ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name); 2787 if (!res.pipeline) { 2788 ggml_metal_cv_t cv = ggml_metal_cv_init(); 2789 2790 ggml_metal_cv_set_int16(cv, nsg, FC_COUNT_EQUAL + 0); 2791 2792 res = ggml_metal_library_compile_pipeline(lib, base, name, cv); 2793 2794 ggml_metal_cv_free(cv); 2795 } 2796 2797 res.smem = 32 * sizeof(int32_t); 2798 res.nsg = nsg; 2799 2800 return res; 2801 }