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-ops.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-ops.cpp` 6321 lines ยท 226842 bytes Raw Blame History 1 #include "ggml-metal-ops.h" 2 3 #include "ggml.h" 4 #include "ggml-impl.h" 5 #include "ggml-backend-impl.h" 6 7 #include "ggml-metal-impl.h" 8 #include "ggml-metal-common.h" 9 #include "ggml-metal-device.h" 10 #include "ggml-metal-fusion.h" 11 #include "ggml-metal-tuning.h" 12 13 #include 14 #include 15 #include 16 #include 17 18 // threadgroups needed to fill the GPU 19 // TODO: dedup 20 static constexpr int64_t GGML_METAL_MIN_THREADGROUPS = 128; 21 22 static ggml_metal_buffer_id ggml_metal_get_buffer_id(const ggml_tensor * t) { 23 if (!t) { 24 return { nullptr, 0 }; 25 } 26 27 ggml_backend_buffer_t buffer = t->view_src ? t->view_src->buffer : t->buffer; 28 29 ggml_metal_buffer_t ctx = (ggml_metal_buffer_t) buffer->context; 30 31 return ggml_metal_buffer_get_id(ctx, t); 32 } 33 34 struct ggml_metal_op { 35 ggml_metal_op( 36 ggml_metal_device_t dev, 37 ggml_metal_cmd_buf_t cmd_buf, 38 ggml_cgraph * gf, 39 ggml_metal_fusion_info * finfo, 40 int idx_start, 41 int idx_end, 42 bool use_concurrency, 43 bool use_capture, 44 int debug_graph) { 45 this->dev = dev; 46 this->lib = ggml_metal_device_get_library(dev); 47 this->enc = ggml_metal_encoder_init(cmd_buf, use_concurrency); 48 this->mem_ranges = ggml_mem_ranges_init(debug_graph); 49 this->finfo = finfo; 50 this->idx_start = idx_start; 51 this->idx_end = idx_end; 52 this->use_concurrency = use_concurrency; 53 this->use_capture = use_capture; 54 this->debug_graph = debug_graph; 55 this->gf = gf; 56 57 idxs.reserve(gf->n_nodes); 58 59 // filter empty nodes 60 // TODO: this can be removed when the allocator starts filtering them earlier 61 // https://github.com/ggml-org/llama.cpp/pull/16130#issuecomment-`3327905830` 62 for (int i = idx_start; i < idx_end; i++) { 63 if (!ggml_op_is_empty(gf->nodes[i]->op) && !ggml_is_empty(gf->nodes[i])) { 64 idxs.push_back(i); 65 } 66 } 67 } 68 69 ~ggml_metal_op() { 70 ggml_metal_encoder_end_encoding(this->enc); 71 ggml_metal_encoder_free(this->enc); 72 ggml_mem_ranges_free(this->mem_ranges); 73 } 74 75 int n_nodes() const { 76 return idxs.size(); 77 } 78 79 ggml_tensor * node(int i) const { 80 assert(i >= 0 && i < (int) idxs.size()); 81 return ggml_graph_node(gf, idxs[i]); 82 } 83 84 // consult the fusion table for the longest pattern starting at i0 85 // returns the matching pattern (nullptr if no fusion) and sets *n_out to the number of nodes 86 const ggml_metal_fusion * can_fuse(int i0, enum ggml_metal_fusion_mode mode, int * n_out) const { 87 assert(use_fusion()); 88 assert(i0 >= 0 && i0 < n_nodes()); 89 90 return ggml_metal_fusion_next(gf, idxs.data(), (int) idxs.size(), i0, mode, n_out); 91 } 92 93 // whether to attempt fusion; the toggle lives in the shared fusion debugging context owned 94 // by the device (initialized from GGML_METAL_FUSION_DISABLE, overridable by the test) 95 bool use_fusion() const { 96 return ggml_metal_fusion_info_enabled(finfo); 97 } 98 99 // record that a fusion fired, indexed by the matching table entry 100 void count_fusions(const ggml_metal_fusion * fusion) const { 101 ggml_metal_fusion_info_count_fusion(finfo, fusion); 102 } 103 104 ggml_metal_device_t dev; 105 ggml_metal_library_t lib; 106 ggml_metal_encoder_t enc; 107 ggml_mem_ranges_t mem_ranges; 108 109 // shared fusion debugging context 110 ggml_metal_fusion_info * finfo; 111 112 bool use_concurrency; 113 bool use_capture; 114 115 int debug_graph; 116 117 private: 118 ggml_cgraph * gf; 119 120 int idx_start; 121 int idx_end; 122 123 // non-empty node indices 124 std::vector idxs; 125 }; 126 127 ggml_metal_op_t ggml_metal_op_init( 128 ggml_metal_device_t dev, 129 ggml_metal_cmd_buf_t cmd_buf, 130 ggml_cgraph * gf, 131 ggml_metal_fusion_info * finfo, 132 int idx_start, 133 int idx_end, 134 bool use_concurrency, 135 bool use_capture, 136 int debug_graph) { 137 ggml_metal_op_t res = new ggml_metal_op( 138 dev, 139 cmd_buf, 140 gf, 141 finfo, 142 idx_start, 143 idx_end, 144 use_concurrency, 145 use_capture, 146 debug_graph); 147 148 return res; 149 } 150 151 void ggml_metal_op_free(ggml_metal_op_t ctx) { 152 delete ctx; 153 } 154 155 int ggml_metal_op_n_nodes(ggml_metal_op_t ctx) { 156 return ctx->n_nodes(); 157 } 158 159 static bool ggml_metal_op_concurrency_reset(ggml_metal_op_t ctx) { 160 if (!ctx->mem_ranges) { 161 return true; 162 } 163 164 ggml_metal_encoder_memory_barrier(ctx->enc); 165 166 ggml_mem_ranges_reset(ctx->mem_ranges); 167 168 return true; 169 } 170 171 static bool ggml_metal_op_concurrency_check(ggml_metal_op_t ctx, const ggml_tensor * node) { 172 if (!ctx->mem_ranges) { 173 return false; 174 } 175 176 return ggml_mem_ranges_check(ctx->mem_ranges, node); 177 } 178 179 static bool ggml_metal_op_concurrency_add(ggml_metal_op_t ctx, const ggml_tensor * node) { 180 if (!ctx->mem_ranges) { 181 return true; 182 } 183 184 return ggml_mem_ranges_add(ctx->mem_ranges, node); 185 } 186 187 static int ggml_metal_op_encode_impl(ggml_metal_op_t ctx, int idx) { 188 struct ggml_tensor * node = ctx->node(idx); 189 190 //GGML_LOG_INFO("%s: encoding node %3d, op = %8s\n", __func__, idx, ggml_op_name(node->op)); 191 192 if (ggml_is_empty(node)) { 193 return 1; 194 } 195 196 switch (node->op) { 197 case GGML_OP_NONE: 198 case GGML_OP_RESHAPE: 199 case GGML_OP_VIEW: 200 case GGML_OP_TRANSPOSE: 201 case GGML_OP_PERMUTE: 202 { 203 // noop -> next node 204 if (ctx->debug_graph > 0) { 205 GGML_LOG_DEBUG("%s: node[%5d] - %-12s %s\n", __func__, idx, ggml_op_name(node->op), "(noop)"); 206 } 207 } return 1; 208 default: 209 { 210 } break; 211 } 212 213 if (!ggml_metal_device_supports_op(ctx->dev, node)) { 214 GGML_LOG_ERROR("%s: error: unsupported op '%s'\n", __func__, ggml_op_desc(node)); 215 GGML_ABORT("unsupported op"); 216 } 217 218 if ((node->flags & GGML_TENSOR_FLAG_COMPUTE) == 0) { 219 return 1; 220 } 221 222 int n_fuse = 1; 223 224 // check if the current node can run concurrently with other nodes before it 225 // the condition is that: 226 // - the current node cannot write to any previous src or dst ranges 227 // - the current node cannot read from any previous dst ranges 228 // 229 // if the condition is not satisfied, we put a memory barrier and clear all ranges 230 // otherwise, we add the new ranges to the encoding context and process the node concurrently 231 // 232 { 233 bool is_concurrent = ggml_metal_op_concurrency_check(ctx, node); 234 235 if (is_concurrent && ctx->use_fusion()) { 236 int n_fuse = 1; 237 const ggml_metal_fusion * fusion = ctx->can_fuse(idx, GGML_METAL_FUSION_FULL, &n_fuse); 238 if (fusion) { 239 // fused kernels write to the last node of the group, not necessarily to the first node's dst 240 is_concurrent = ggml_mem_ranges_check(ctx->mem_ranges, ctx->node(idx + n_fuse - 1)); 241 } 242 } 243 244 if (!is_concurrent) { 245 ggml_metal_op_concurrency_reset(ctx); 246 } 247 248 if (ctx->debug_graph > 0) { 249 GGML_LOG_DEBUG("%s: node[%5d] - %-12s %-12s %s\n", __func__, idx, ggml_op_name(node->op), ggml_get_name(node), is_concurrent ? "(concurrent)" : ""); 250 } 251 if (ctx->debug_graph > 1) { 252 GGML_TENSOR_LOCALS( int64_t, ne0, node->src[0], ne); 253 GGML_TENSOR_LOCALS(uint64_t, nb0, node->src[0], nb); 254 GGML_TENSOR_LOCALS( int64_t, ne1, node->src[1], ne); 255 GGML_TENSOR_LOCALS(uint64_t, nb1, node->src[1], nb); 256 GGML_TENSOR_LOCALS( int64_t, ne2, node->src[2], ne); 257 GGML_TENSOR_LOCALS(uint64_t, nb2, node->src[2], nb); 258 GGML_TENSOR_LOCALS( int64_t, ne3, node->src[3], ne); 259 GGML_TENSOR_LOCALS(uint64_t, nb3, node->src[3], nb); 260 GGML_TENSOR_LOCALS( int64_t, ne, node, ne); 261 GGML_TENSOR_LOCALS(uint64_t, nb, node, nb); 262 263 if (node->src[0]) { 264 GGML_LOG_DEBUG("%s: src0 - %4s [%5lld, %5lld, %5lld, %5lld] [%5lld, %5lld, %5lld, %5lld], %d, %s\n", __func__, ggml_type_name(node->src[0]->type), ne00, ne01, ne02, ne03, nb00, nb01, nb02, nb03, 265 ggml_is_contiguous(node->src[0]), node->src[0]->name); 266 } 267 if (node->src[1]) { 268 GGML_LOG_DEBUG("%s: src1 - %4s [%5lld, %5lld, %5lld, %5lld] [%5lld, %5lld, %5lld, %5lld], %d, %s\n", __func__, ggml_type_name(node->src[1]->type), ne10, ne11, ne12, ne13, nb10, nb11, nb12, nb13, 269 ggml_is_contiguous(node->src[1]), node->src[1]->name); 270 } 271 if (node->src[2]) { 272 GGML_LOG_DEBUG("%s: src2 - %4s [%5lld, %5lld, %5lld, %5lld] [%5lld, %5lld, %5lld, %5lld], %d, %s\n", __func__, ggml_type_name(node->src[2]->type), ne20, ne21, ne22, ne23, nb20, nb21, nb22, nb23, 273 ggml_is_contiguous(node->src[2]), node->src[2]->name); 274 } 275 if (node->src[3]) { 276 GGML_LOG_DEBUG("%s: src3 - %4s [%5lld, %5lld, %5lld, %5lld] [%5lld, %5lld, %5lld, %5lld], %d, %s\n", __func__, ggml_type_name(node->src[3]->type), ne30, ne31, ne32, ne33, nb30, nb31, nb32, nb33, 277 ggml_is_contiguous(node->src[3]), node->src[3]->name); 278 } 279 if (node) { 280 GGML_LOG_DEBUG("%s: node - %4s [%5lld, %5lld, %5lld, %5lld] [%5lld, %5lld, %5lld, %5lld], 1, %s\n", __func__, ggml_type_name(node->type), ne0, ne1, ne2, ne3, nb0, nb1, nb2, nb3, 281 node->name); 282 } 283 } 284 } 285 286 switch (node->op) { 287 case GGML_OP_CONCAT: 288 { 289 n_fuse = ggml_metal_op_concat(ctx, idx); 290 } break; 291 case GGML_OP_ADD: 292 case GGML_OP_SUB: 293 case GGML_OP_MUL: 294 case GGML_OP_DIV: 295 { 296 n_fuse = ggml_metal_op_bin(ctx, idx); 297 } break; 298 case GGML_OP_ADD_ID: 299 { 300 n_fuse = ggml_metal_op_add_id(ctx, idx); 301 } break; 302 case GGML_OP_REPEAT: 303 { 304 n_fuse = ggml_metal_op_repeat(ctx, idx); 305 } break; 306 case GGML_OP_ACC: 307 { 308 n_fuse = ggml_metal_op_acc(ctx, idx); 309 } break; 310 case GGML_OP_SCALE: 311 case GGML_OP_FILL: 312 case GGML_OP_CLAMP: 313 case GGML_OP_LEAKY_RELU: 314 case GGML_OP_SQR: 315 case GGML_OP_SQRT: 316 case GGML_OP_SIN: 317 case GGML_OP_COS: 318 case GGML_OP_LOG: 319 case GGML_OP_UNARY: 320 { 321 n_fuse = ggml_metal_op_unary(ctx, idx); 322 } break; 323 case GGML_OP_SILU_BACK: 324 { 325 n_fuse = ggml_metal_op_silu_back(ctx, idx); 326 } break; 327 case GGML_OP_GLU: 328 { 329 n_fuse = ggml_metal_op_glu(ctx, idx); 330 } break; 331 case GGML_OP_SUM: 332 { 333 n_fuse = ggml_metal_op_sum(ctx, idx); 334 } break; 335 case GGML_OP_SUM_ROWS: 336 case GGML_OP_MEAN: 337 { 338 n_fuse = ggml_metal_op_sum_rows(ctx, idx); 339 } break; 340 case GGML_OP_CUMSUM: 341 { 342 n_fuse = ggml_metal_op_cumsum(ctx, idx); 343 } break; 344 case GGML_OP_LIGHTNING_INDEXER: 345 { 346 n_fuse = ggml_metal_op_lightning_indexer(ctx, idx); 347 } break; 348 case GGML_OP_TOP_K_MASK: 349 { 350 n_fuse = ggml_metal_op_top_k_mask(ctx, idx); 351 } break; 352 case GGML_OP_DSV4_HC_COMB: 353 case GGML_OP_DSV4_HC_PRE: 354 case GGML_OP_DSV4_HC_POST: 355 { 356 n_fuse = ggml_metal_op_dsv4_hc(ctx, idx); 357 } break; 358 case GGML_OP_SOFT_MAX: 359 { 360 n_fuse = ggml_metal_op_soft_max(ctx, idx); 361 } break; 362 case GGML_OP_SSM_CONV: 363 { 364 n_fuse = ggml_metal_op_ssm_conv(ctx, idx); 365 } break; 366 case GGML_OP_SSM_SCAN: 367 { 368 n_fuse = ggml_metal_op_ssm_scan(ctx, idx); 369 } break; 370 case GGML_OP_RWKV_WKV6: 371 case GGML_OP_RWKV_WKV7: 372 { 373 n_fuse = ggml_metal_op_rwkv(ctx, idx); 374 } break; 375 case GGML_OP_GATED_DELTA_NET: 376 { 377 n_fuse = ggml_metal_op_gated_delta_net(ctx, idx); 378 } break; 379 case GGML_OP_SOLVE_TRI: 380 { 381 n_fuse = ggml_metal_op_solve_tri(ctx, idx); 382 } break; 383 case GGML_OP_MUL_MAT: 384 { 385 n_fuse = ggml_metal_op_mul_mat(ctx, idx); 386 } break; 387 case GGML_OP_MUL_MAT_ID: 388 { 389 n_fuse = ggml_metal_op_mul_mat_id(ctx, idx); 390 } break; 391 case GGML_OP_GET_ROWS: 392 { 393 n_fuse = ggml_metal_op_get_rows(ctx, idx); 394 } break; 395 case GGML_OP_SET_ROWS: 396 { 397 n_fuse = ggml_metal_op_set_rows(ctx, idx); 398 } break; 399 case GGML_OP_DIAG: 400 { 401 n_fuse = ggml_metal_op_diag(ctx, idx); 402 } break; 403 case GGML_OP_L2_NORM: 404 { 405 n_fuse = ggml_metal_op_l2_norm(ctx, idx); 406 } break; 407 case GGML_OP_GROUP_NORM: 408 { 409 n_fuse = ggml_metal_op_group_norm(ctx, idx); 410 } break; 411 case GGML_OP_NORM: 412 case GGML_OP_RMS_NORM: 413 { 414 n_fuse = ggml_metal_op_norm(ctx, idx); 415 } break; 416 case GGML_OP_ROPE: 417 case GGML_OP_ROPE_BACK: 418 { 419 n_fuse = ggml_metal_op_rope(ctx, idx); 420 } break; 421 case GGML_OP_IM2COL: 422 { 423 n_fuse = ggml_metal_op_im2col(ctx, idx); 424 } break; 425 case GGML_OP_CONV_2D: 426 { 427 n_fuse = ggml_metal_op_conv_2d(ctx, idx); 428 } break; 429 case GGML_OP_CONV_2D_DW: 430 { 431 n_fuse = ggml_metal_op_conv_2d_dw(ctx, idx); 432 } break; 433 case GGML_OP_CONV_TRANSPOSE_1D: 434 { 435 n_fuse = ggml_metal_op_conv_transpose_1d(ctx, idx); 436 } break; 437 case GGML_OP_CONV_TRANSPOSE_2D: 438 { 439 n_fuse = ggml_metal_op_conv_transpose_2d(ctx, idx); 440 } break; 441 case GGML_OP_COL2IM_1D: 442 { 443 n_fuse = ggml_metal_op_col2im_1d(ctx, idx); 444 } break; 445 case GGML_OP_CONV_3D: 446 { 447 n_fuse = ggml_metal_op_conv_3d(ctx, idx); 448 } break; 449 case GGML_OP_UPSCALE: 450 { 451 n_fuse = ggml_metal_op_upscale(ctx, idx); 452 } break; 453 case GGML_OP_PAD: 454 { 455 n_fuse = ggml_metal_op_pad(ctx, idx); 456 } break; 457 case GGML_OP_PAD_REFLECT_1D: 458 { 459 n_fuse = ggml_metal_op_pad_reflect_1d(ctx, idx); 460 } break; 461 case GGML_OP_ROLL: 462 { 463 n_fuse = ggml_metal_op_roll(ctx, idx); 464 } break; 465 case GGML_OP_ARANGE: 466 { 467 n_fuse = ggml_metal_op_arange(ctx, idx); 468 } break; 469 case GGML_OP_TIMESTEP_EMBEDDING: 470 { 471 n_fuse = ggml_metal_op_timestep_embedding(ctx, idx); 472 } break; 473 case GGML_OP_ARGSORT: 474 { 475 n_fuse = ggml_metal_op_argsort(ctx, idx); 476 } break; 477 case GGML_OP_TOP_K: 478 { 479 n_fuse = ggml_metal_op_top_k(ctx, idx); 480 } break; 481 case GGML_OP_TRI: 482 { 483 n_fuse = ggml_metal_op_tri(ctx, idx); 484 } break; 485 case GGML_OP_FLASH_ATTN_EXT: 486 { 487 n_fuse = ggml_metal_op_flash_attn_ext(ctx, idx); 488 } break; 489 case GGML_OP_SET: 490 { 491 n_fuse = ggml_metal_op_set(ctx, idx); 492 } break; 493 case GGML_OP_DUP: 494 case GGML_OP_CPY: 495 case GGML_OP_CONT: 496 { 497 n_fuse = ggml_metal_op_cpy(ctx, idx); 498 } break; 499 case GGML_OP_POOL_1D: 500 { 501 n_fuse = ggml_metal_op_pool_1d(ctx, idx); 502 } break; 503 case GGML_OP_POOL_2D: 504 { 505 n_fuse = ggml_metal_op_pool_2d(ctx, idx); 506 } break; 507 case GGML_OP_ARGMAX: 508 { 509 n_fuse = ggml_metal_op_argmax(ctx, idx); 510 } break; 511 case GGML_OP_OPT_STEP_ADAMW: 512 { 513 n_fuse = ggml_metal_op_opt_step_adamw(ctx, idx); 514 } break; 515 case GGML_OP_OPT_STEP_SGD: 516 { 517 n_fuse = ggml_metal_op_opt_step_sgd(ctx, idx); 518 } break; 519 case GGML_OP_COUNT_EQUAL: 520 { 521 n_fuse = ggml_metal_op_count_equal(ctx, idx); 522 } break; 523 default: 524 { 525 GGML_LOG_ERROR("%s: error: node %3d, op = %8s not implemented\n", __func__, idx, ggml_op_name(node->op)); 526 GGML_ABORT("fatal error"); 527 } 528 } 529 530 if (ctx->debug_graph > 0) { 531 if (n_fuse > 1) { 532 GGML_LOG_DEBUG("%s: fuse %d ops\n", __func__, n_fuse); 533 } 534 } 535 536 // update the mem ranges in the encoding context 537 for (int i = 0; i < n_fuse; ++i) { 538 if (!ggml_metal_op_concurrency_add(ctx, ctx->node(idx + i))) { 539 ggml_metal_op_concurrency_reset(ctx); 540 } 541 } 542 543 return n_fuse; 544 } 545 546 int ggml_metal_op_encode(ggml_metal_op_t ctx, int idx) { 547 if (ctx->use_capture) { 548 ggml_metal_encoder_debug_group_push(ctx->enc, ggml_op_desc(ctx->node(idx))); 549 } 550 551 int res = ggml_metal_op_encode_impl(ctx, idx); 552 if (idx + res > ctx->n_nodes()) { 553 GGML_ABORT("fusion error: nodes spanning multiple encoders have been fused. this indicates a bug in the fusion logic %s", 554 "https://github.com/ggml-org/llama.cpp/pull/14849"); 555 } 556 557 if (ctx->use_capture) { 558 ggml_metal_encoder_debug_group_pop(ctx->enc); 559 } 560 561 return res; 562 } 563 564 int ggml_metal_op_concat(ggml_metal_op_t ctx, int idx) { 565 ggml_tensor * op = ctx->node(idx); 566 567 ggml_metal_library_t lib = ctx->lib; 568 ggml_metal_encoder_t enc = ctx->enc; 569 570 GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); 571 GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); 572 GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne); 573 GGML_TENSOR_LOCALS(uint64_t, nb1, op->src[1], nb); 574 GGML_TENSOR_LOCALS( int32_t, ne, op, ne); 575 GGML_TENSOR_LOCALS(uint64_t, nb, op, nb); 576 577 const int32_t dim = ((const int32_t *) op->op_params)[0]; 578 579 const bool is_q = ggml_is_quantized(op->type); 580 581 // for quantized types, concat is done at the block level (nb0 == type_size == block size) 582 int32_t ne00_arg = ne00; 583 int32_t ne10_arg = ne10; 584 int32_t ne0_arg = ne0; 585 if (is_q) { 586 const int32_t blck = ggml_blck_size(op->type); 587 GGML_ASSERT(ne00 % blck == 0); 588 GGML_ASSERT(ne10 % blck == 0); 589 GGML_ASSERT(ne0 % blck == 0); 590 ne00_arg = ne00/blck; 591 ne10_arg = ne10/blck; 592 ne0_arg = ne0/blck; 593 } 594 595 int nth = std::min(256, ne0_arg); 596 597 // when rows are small, we can batch them together in a single threadgroup 598 int nrptg = 1; 599 if (nth < 256) { 600 nrptg = std::min((256 + nth - 1) / nth, ne1); 601 if (nrptg * nth > 256) { 602 nrptg = 256 / nth; 603 } 604 } 605 606 const int nw0 = (ne1 + nrptg - 1) / nrptg; 607 608 // split long rows across threadgroups when there are too few rows to fill the GPU 609 const int64_t n_rows = (int64_t) nw0*ne2*ne3; 610 611 int nc0 = 1; 612 if (nrptg == 1) { 613 nc0 = (int) std::max(1, std::min((ne0_arg + nth - 1)/nth, (GGML_METAL_MIN_THREADGROUPS + n_rows - 1)/n_rows)); 614 } 615 616 ggml_metal_kargs_concat args = { 617 /*.ne00 =*/ ne00_arg, 618 /*.ne01 =*/ ne01, 619 /*.ne02 =*/ ne02, 620 /*.ne03 =*/ ne03, 621 /*.nb00 =*/ nb00, 622 /*.nb01 =*/ nb01, 623 /*.nb02 =*/ nb02, 624 /*.nb03 =*/ nb03, 625 /*.ne10 =*/ ne10_arg, 626 /*.ne11 =*/ ne11, 627 /*.ne12 =*/ ne12, 628 /*.ne13 =*/ ne13, 629 /*.nb10 =*/ nb10, 630 /*.nb11 =*/ nb11, 631 /*.nb12 =*/ nb12, 632 /*.nb13 =*/ nb13, 633 /*.ne0 =*/ ne0_arg, 634 /*.ne1 =*/ ne1, 635 /*.ne2 =*/ ne2, 636 /*.ne3 =*/ ne3, 637 /*.nb0 =*/ nb0, 638 /*.nb1 =*/ nb1, 639 /*.nb2 =*/ nb2, 640 /*.nb3 =*/ nb3, 641 /*.dim =*/ dim, 642 /*.nc0 =*/ nc0, 643 }; 644 645 auto pipeline = ggml_metal_library_get_pipeline_concat(lib, op->type); 646 647 ggml_metal_encoder_set_pipeline(enc, pipeline); 648 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 649 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1); 650 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[1]), 2); 651 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 3); 652 653 ggml_metal_encoder_dispatch_threadgroups(enc, nw0*nc0, ne2, ne3, nth, nrptg, 1); 654 655 return 1; 656 } 657 658 int ggml_metal_op_repeat(ggml_metal_op_t ctx, int idx) { 659 ggml_tensor * op = ctx->node(idx); 660 661 ggml_metal_library_t lib = ctx->lib; 662 ggml_metal_encoder_t enc = ctx->enc; 663 664 GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); 665 GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); 666 GGML_TENSOR_LOCALS( int32_t, ne, op, ne); 667 GGML_TENSOR_LOCALS(uint64_t, nb, op, nb); 668 669 // TODO: optimize for degenerate cases such as ggml_nelements(op->src[0]) == 1 and others 670 671 auto pipeline = ggml_metal_library_get_pipeline_repeat(lib, op->type); 672 673 ggml_metal_kargs_repeat args = { 674 /*.ne00 =*/ ne00, 675 /*.ne01 =*/ ne01, 676 /*.ne02 =*/ ne02, 677 /*.ne03 =*/ ne03, 678 /*.nb00 =*/ nb00, 679 /*.nb01 =*/ nb01, 680 /*.nb02 =*/ nb02, 681 /*.nb03 =*/ nb03, 682 /*.ne0 =*/ ne0, 683 /*.ne1 =*/ ne1, 684 /*.ne2 =*/ ne2, 685 /*.ne3 =*/ ne3, 686 /*.nb0 =*/ nb0, 687 /*.nb1 =*/ nb1, 688 /*.nb2 =*/ nb2, 689 /*.nb3 =*/ nb3, 690 }; 691 692 ggml_metal_encoder_set_pipeline(enc, pipeline); 693 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 694 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1); 695 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 2); 696 697 const int nth = std::min(ggml_metal_pipeline_max_theads_per_threadgroup(pipeline), ne0); 698 699 ggml_metal_encoder_dispatch_threadgroups(enc, ne1, ne2, ne3, nth, 1, 1); 700 701 return 1; 702 } 703 704 int ggml_metal_op_acc(ggml_metal_op_t ctx, int idx) { 705 ggml_tensor * op = ctx->node(idx); 706 707 ggml_metal_library_t lib = ctx->lib; 708 ggml_metal_encoder_t enc = ctx->enc; 709 710 GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); 711 GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); 712 GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne); 713 GGML_TENSOR_LOCALS(uint64_t, nb1, op->src[1], nb); 714 GGML_TENSOR_LOCALS( int32_t, ne, op, ne); 715 GGML_TENSOR_LOCALS(uint64_t, nb, op, nb); 716 717 GGML_ASSERT(op->src[0]->type == GGML_TYPE_F32); 718 GGML_ASSERT(op->src[1]->type == GGML_TYPE_F32); 719 GGML_ASSERT(op->type == GGML_TYPE_F32); 720 721 GGML_ASSERT(ggml_is_contiguous_rows(op->src[0])); 722 GGML_ASSERT(ggml_is_contiguous_rows(op->src[1])); 723 724 const size_t pnb1 = ((const int32_t *) op->op_params)[0]; 725 const size_t pnb2 = ((const int32_t *) op->op_params)[1]; 726 const size_t pnb3 = ((const int32_t *) op->op_params)[2]; 727 const size_t offs = ((const int32_t *) op->op_params)[3]; 728 729 const bool inplace = (bool) ((const int32_t *) op->op_params)[4]; 730 731 if (!inplace) { 732 // run a separate kernel to cpy src->dst 733 // not sure how to avoid this 734 // TODO: make a simpler cpy_bytes kernel 735 736 //const id pipeline = ctx->pipelines[GGML_METAL_PIPELINE_TYPE_CPY_F32_F32].obj; 737 auto pipeline = ggml_metal_library_get_pipeline_cpy(lib, op->src[0]->type, op->type); 738 739 ggml_metal_kargs_cpy args = { 740 /*.nk0 =*/ ne00, 741 /*.ne00 =*/ ne00, 742 /*.ne01 =*/ ne01, 743 /*.ne02 =*/ ne02, 744 /*.ne03 =*/ ne03, 745 /*.nb00 =*/ nb00, 746 /*.nb01 =*/ nb01, 747 /*.nb02 =*/ nb02, 748 /*.nb03 =*/ nb03, 749 /*.ne0 =*/ ne0, 750 /*.ne1 =*/ ne1, 751 /*.ne2 =*/ ne2, 752 /*.ne3 =*/ ne3, 753 /*.nb0 =*/ nb0, 754 /*.nb1 =*/ nb1, 755 /*.nb2 =*/ nb2, 756 /*.nb3 =*/ nb3, 757 }; 758 759 ggml_metal_encoder_set_pipeline(enc, pipeline); 760 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 761 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1); 762 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 2); 763 764 const int nth = std::min(ggml_metal_pipeline_max_theads_per_threadgroup(pipeline), ne00); 765 766 ggml_metal_encoder_dispatch_threadgroups(enc, ne01, ne02, ne03, nth, 1, 1); 767 768 ggml_metal_op_concurrency_reset(ctx); 769 } 770 771 ggml_metal_kargs_bin args = { 772 /*.ne00 =*/ ne10, 773 /*.ne01 =*/ ne11, 774 /*.ne02 =*/ ne12, 775 /*.ne03 =*/ ne13, 776 /*.nb00 =*/ nb00, 777 /*.nb01 =*/ pnb1, 778 /*.nb02 =*/ pnb2, 779 /*.nb03 =*/ pnb3, 780 /*.ne10 =*/ ne10, 781 /*.ne11 =*/ ne11, 782 /*.ne12 =*/ ne12, 783 /*.ne13 =*/ ne13, 784 /*.nb10 =*/ nb10, 785 /*.nb11 =*/ nb11, 786 /*.nb12 =*/ nb12, 787 /*.nb13 =*/ nb13, 788 /*.ne0 =*/ ne10, 789 /*.ne1 =*/ ne11, 790 /*.ne2 =*/ ne12, 791 /*.ne3 =*/ ne13, 792 /*.nb0 =*/ nb0, 793 /*.nb1 =*/ pnb1, 794 /*.nb2 =*/ pnb2, 795 /*.nb3 =*/ pnb3, 796 /*.offs =*/ offs, 797 /*.o1 =*/ { 0 }, 798 }; 799 800 auto pipeline = ggml_metal_library_get_pipeline_bin_one(lib, GGML_OP_ADD); 801 802 ggml_metal_encoder_set_pipeline(enc, pipeline); 803 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 804 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1); 805 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[1]), 2); 806 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 3); 807 808 const int nth_max = MIN(256, ggml_metal_pipeline_max_theads_per_threadgroup(pipeline)); 809 810 int nth = 1; 811 812 while (2*nth < args.ne0 && nth < nth_max) { 813 nth *= 2; 814 } 815 816 ggml_metal_encoder_dispatch_threadgroups(enc, ne11, ne12, ne13, nth, 1, 1); 817 818 return 1; 819 } 820 821 int ggml_metal_op_unary(ggml_metal_op_t ctx, int idx) { 822 ggml_tensor * op = ctx->node(idx); 823 824 ggml_metal_library_t lib = ctx->lib; 825 ggml_metal_encoder_t enc = ctx->enc; 826 827 GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); 828 GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); 829 GGML_TENSOR_LOCALS( int32_t, ne, op, ne); 830 GGML_TENSOR_LOCALS(uint64_t, nb, op, nb); 831 832 GGML_ASSERT(ggml_is_contiguous_rows(op->src[0])); 833 834 ggml_metal_buffer_id bid_src0 = ggml_metal_get_buffer_id(op->src[0]); 835 ggml_metal_buffer_id bid_dst = ggml_metal_get_buffer_id(op); 836 837 ggml_metal_kargs_unary args = { 838 /*.ne00 =*/ ne00, 839 /*.ne01 =*/ ne01, 840 /*.ne02 =*/ ne02, 841 /*.ne03 =*/ ne03, 842 /*.nb00 =*/ nb00, 843 /*.nb01 =*/ nb01, 844 /*.nb02 =*/ nb02, 845 /*.nb03 =*/ nb03, 846 /*.ne0 =*/ ne0, 847 /*.ne1 =*/ ne1, 848 /*.ne2 =*/ ne2, 849 /*.ne3 =*/ ne3, 850 /*.nb0 =*/ nb0, 851 /*.nb1 =*/ nb1, 852 /*.nb2 =*/ nb2, 853 /*.nb3 =*/ nb3, 854 /*.slope =*/ 0.0, 855 /*.scale =*/ 0.0, 856 /*.bias =*/ 0.0, 857 /*.val =*/ 0.0, 858 /*.min =*/ 0.0, 859 /*.max =*/ 0.0, 860 }; 861 862 if (op->op == GGML_OP_LEAKY_RELU) { 863 args.slope = ggml_get_op_params_f32(op, 0); 864 } 865 866 if (op->op == GGML_OP_SCALE) { 867 args.scale = ggml_get_op_params_f32(op, 0); 868 args.bias = ggml_get_op_params_f32(op, 1); 869 } 870 871 if (op->op == GGML_OP_FILL) { 872 args.val = ggml_get_op_params_f32(op, 0); 873 } 874 875 if (op->op == GGML_OP_CLAMP) { 876 args.min = ggml_get_op_params_f32(op, 0); 877 args.max = ggml_get_op_params_f32(op, 1); 878 } 879 880 if (op->op == GGML_OP_UNARY && ggml_get_unary_op(op) == GGML_UNARY_OP_XIELU) { 881 args.slope = ggml_get_op_params_f32(op, 1); // alpha_n 882 args.scale = ggml_get_op_params_f32(op, 2); // alpha_p 883 args.bias = ggml_get_op_params_f32(op, 3); // beta 884 args.val = ggml_get_op_params_f32(op, 4); // eps 885 } 886 887 auto pipeline = ggml_metal_library_get_pipeline_unary(lib, op); 888 889 if (pipeline.c4) { 890 args.ne00 = ne00/4; 891 args.ne0 = ne0/4; 892 } 893 894 ggml_metal_encoder_set_pipeline(enc, pipeline); 895 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 896 ggml_metal_encoder_set_buffer (enc, bid_src0, 1); 897 ggml_metal_encoder_set_buffer (enc, bid_dst, 2); 898 899 if (pipeline.cnt) { 900 const int n = pipeline.c4 ? ggml_nelements(op)/4 : ggml_nelements(op); 901 902 const int nth = MIN(256, n); 903 904 ggml_metal_encoder_dispatch_threadgroups(enc, (n + nth - 1)/nth, 1, 1, nth, 1, 1); 905 } else { 906 const int nth_max = MIN(256, ggml_metal_pipeline_max_theads_per_threadgroup(pipeline)); 907 const int nth = MIN(args.ne00, nth_max); 908 const int nk0 = (args.ne00 + nth - 1)/nth; 909 910 ggml_metal_encoder_dispatch_threadgroups(enc, nk0*ne01, ne02, ne03, nth, 1, 1); 911 } 912 913 return 1; 914 } 915 916 int ggml_metal_op_glu(ggml_metal_op_t ctx, int idx) { 917 ggml_tensor * op = ctx->node(idx); 918 919 ggml_metal_library_t lib = ctx->lib; 920 ggml_metal_encoder_t enc = ctx->enc; 921 922 GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); 923 GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); 924 GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne); 925 GGML_TENSOR_LOCALS(uint64_t, nb1, op->src[1], nb); 926 GGML_TENSOR_LOCALS( int32_t, ne, op, ne); 927 GGML_TENSOR_LOCALS(uint64_t, nb, op, nb); 928 929 if (op->src[1]) { 930 GGML_ASSERT(ggml_are_same_shape(op->src[0], op->src[1])); 931 } 932 933 auto pipeline = ggml_metal_library_get_pipeline_glu(lib, op); 934 935 const int32_t swp = ggml_get_op_params_i32(op, 1); 936 const float alpha = ggml_get_op_params_f32(op, 2); 937 const float limit = ggml_get_op_params_f32(op, 3); 938 939 const int32_t i00 = swp ? ne0 : 0; 940 const int32_t i10 = swp ? 0 : ne0; 941 942 ggml_metal_kargs_glu args = { 943 /*.ne00 =*/ ne00, 944 /*.nb01 =*/ nb01, 945 /*.ne10 =*/ op->src[1] ? ne10 : ne00, 946 /*.nb11 =*/ op->src[1] ? nb11 : nb01, 947 /*.ne0 =*/ ne0, 948 /*.nb1 =*/ nb1, 949 /*.i00 =*/ op->src[1] ? 0 : i00, 950 /*.i10 =*/ op->src[1] ? 0 : i10, 951 /*.alpha=*/ alpha, 952 /*.limit=*/ limit 953 }; 954 955 const int64_t nrows = ggml_nrows(op->src[0]); 956 957 // split each row over several threadgroups: with a single row (decode) one threadgroup would run on one GPU core 958 const int32_t nth = std::max(1, std::min(ggml_metal_pipeline_max_theads_per_threadgroup(pipeline), std::min(256, ne0))); 959 const int32_t nk = (ne0 + nth - 1)/nth; 960 961 ggml_metal_encoder_set_pipeline(enc, pipeline); 962 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 963 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1); 964 if (op->src[1]) { 965 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[1]), 2); 966 } else { 967 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 2); 968 } 969 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 3); 970 971 ggml_metal_encoder_dispatch_threadgroups(enc, nk, nrows, 1, nth, 1, 1); 972 973 return 1; 974 } 975 976 int ggml_metal_op_sum(ggml_metal_op_t ctx, int idx) { 977 ggml_tensor * op = ctx->node(idx); 978 979 ggml_metal_library_t lib = ctx->lib; 980 ggml_metal_encoder_t enc = ctx->enc; 981 982 const uint64_t n = (uint64_t) ggml_nelements(op->src[0]); 983 984 ggml_metal_kargs_sum args = { 985 /*.np =*/ n, 986 }; 987 988 auto pipeline = ggml_metal_library_get_pipeline_sum(lib, op); 989 990 int nth = 32; // SIMD width 991 992 while (nth < (int) n && nth < ggml_metal_pipeline_max_theads_per_threadgroup(pipeline)) { 993 nth *= 2; 994 } 995 996 nth = std::min(nth, ggml_metal_pipeline_max_theads_per_threadgroup(pipeline)); 997 nth = std::min(nth, (int) n); 998 999 const int nsg = (nth + 31) / 32; 1000 1001 ggml_metal_encoder_set_pipeline(enc, pipeline); 1002 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 1003 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1); 1004 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 2); 1005 1006 ggml_metal_encoder_set_threadgroup_memory_size(enc, GGML_PAD(nsg * sizeof(float), 16), 0); 1007 1008 ggml_metal_encoder_dispatch_threadgroups(enc, 1, 1, 1, nth, 1, 1); 1009 1010 return 1; 1011 } 1012 1013 int ggml_metal_op_sum_rows(ggml_metal_op_t ctx, int idx) { 1014 ggml_tensor * op = ctx->node(idx); 1015 1016 ggml_metal_library_t lib = ctx->lib; 1017 ggml_metal_encoder_t enc = ctx->enc; 1018 1019 GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); 1020 GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); 1021 GGML_TENSOR_LOCALS( int32_t, ne, op, ne); 1022 GGML_TENSOR_LOCALS(uint64_t, nb, op, nb); 1023 1024 GGML_ASSERT(ggml_is_contiguous_rows(op->src[0])); 1025 1026 ggml_metal_buffer_id bid_src0 = ggml_metal_get_buffer_id(op->src[0]); 1027 ggml_metal_buffer_id bid_dst = ggml_metal_get_buffer_id(op); 1028 1029 ggml_metal_kargs_sum_rows args = { 1030 /*.ne00 =*/ ne00, 1031 /*.ne01 =*/ ne01, 1032 /*.ne02 =*/ ne02, 1033 /*.ne03 =*/ ne03, 1034 /*.nb00 =*/ nb00, 1035 /*.nb01 =*/ nb01, 1036 /*.nb02 =*/ nb02, 1037 /*.nb03 =*/ nb03, 1038 /*.ne0 =*/ ne0, 1039 /*.ne1 =*/ ne1, 1040 /*.ne2 =*/ ne2, 1041 /*.ne3 =*/ ne3, 1042 /*.nb0 =*/ nb0, 1043 /*.nb1 =*/ nb1, 1044 /*.nb2 =*/ nb2, 1045 /*.nb3 =*/ nb3, 1046 }; 1047 1048 auto pipeline = ggml_metal_library_get_pipeline_sum_rows(lib, op); 1049 1050 if (pipeline.c4) { 1051 args.ne00 = ne00/4; 1052 args.ne0 = ne0/4; 1053 } 1054 1055 int nth = 32; // SIMD width 1056 1057 while (nth < args.ne00 && nth < ggml_metal_pipeline_max_theads_per_threadgroup(pipeline)) { 1058 nth *= 2; 1059 } 1060 1061 nth = std::min(nth, ggml_metal_pipeline_max_theads_per_threadgroup(pipeline)); 1062 nth = std::min(nth, (int) args.ne00); 1063 1064 const size_t smem = pipeline.smem; 1065 1066 ggml_metal_encoder_set_pipeline(enc, pipeline); 1067 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 1068 ggml_metal_encoder_set_buffer (enc, bid_src0, 1); 1069 ggml_metal_encoder_set_buffer (enc, bid_dst, 2); 1070 1071 ggml_metal_encoder_set_threadgroup_memory_size(enc, smem, 0); 1072 1073 ggml_metal_encoder_dispatch_threadgroups(enc, ne01, ne02, ne03, nth, 1, 1); 1074 1075 return 1; 1076 } 1077 1078 int ggml_metal_op_cumsum(ggml_metal_op_t ctx, int idx) { 1079 ggml_tensor * op = ctx->node(idx); 1080 1081 ggml_metal_library_t lib = ctx->lib; 1082 ggml_metal_encoder_t enc = ctx->enc; 1083 1084 GGML_ASSERT(ggml_is_contiguous_rows(op->src[0])); 1085 1086 GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); 1087 GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); 1088 GGML_TENSOR_LOCALS( int32_t, ne, op, ne); 1089 GGML_TENSOR_LOCALS(uint64_t, nb, op, nb); 1090 1091 auto pipeline_blk = ggml_metal_library_get_pipeline_cumsum_blk(lib, op); 1092 1093 int nth = 1; 1094 while (nth < ne00 && 2*nth <= ggml_metal_pipeline_max_theads_per_threadgroup(pipeline_blk)) { 1095 nth *= 2; 1096 } 1097 1098 GGML_ASSERT(ne00 <= nth*nth); 1099 1100 const int64_t net0 = (ne00 + nth - 1) / nth; 1101 const int64_t net1 = ne01; 1102 const int64_t net2 = ne02; 1103 const int64_t net3 = ne03; 1104 1105 const uint64_t nbt0 = sizeof(float); 1106 const uint64_t nbt1 = net0*nbt0; 1107 const uint64_t nbt2 = net1*nbt1; 1108 const uint64_t nbt3 = net2*nbt2; 1109 1110 const size_t smem = GGML_PAD(32*sizeof(float), 16); 1111 1112 ggml_metal_buffer_id bid_src0 = ggml_metal_get_buffer_id(op->src[0]); 1113 ggml_metal_buffer_id bid_dst = ggml_metal_get_buffer_id(op); 1114 1115 ggml_metal_buffer_id bid_tmp = bid_dst; 1116 bid_tmp.offs += ggml_nbytes(op); 1117 1118 { 1119 ggml_metal_kargs_cumsum_blk args = { 1120 /*.ne00 =*/ ne00, 1121 /*.ne01 =*/ ne01, 1122 /*.ne02 =*/ ne02, 1123 /*.ne03 =*/ ne03, 1124 /*.nb00 =*/ nb00, 1125 /*.nb01 =*/ nb01, 1126 /*.nb02 =*/ nb02, 1127 /*.nb03 =*/ nb03, 1128 /*.net0 =*/ net0, 1129 /*.net1 =*/ net1, 1130 /*.net2 =*/ net2, 1131 /*.net3 =*/ net3, 1132 /*.nbt0 =*/ nbt0, 1133 /*.nbt1 =*/ nbt1, 1134 /*.nbt2 =*/ nbt2, 1135 /*.nbt3 =*/ nbt3, 1136 /*.outb =*/ ne00 > nth, 1137 }; 1138 1139 ggml_metal_encoder_set_pipeline(enc, pipeline_blk); 1140 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 1141 ggml_metal_encoder_set_buffer (enc, bid_src0, 1); 1142 ggml_metal_encoder_set_buffer (enc, bid_tmp, 2); 1143 ggml_metal_encoder_set_buffer (enc, bid_dst, 3); 1144 1145 ggml_metal_encoder_set_threadgroup_memory_size(enc, smem, 0); 1146 1147 ggml_metal_encoder_dispatch_threadgroups(enc, net0*ne01, ne02, ne03, nth, 1, 1); 1148 } 1149 1150 if (ne00 > nth) { 1151 ggml_metal_op_concurrency_reset(ctx); 1152 1153 { 1154 ggml_metal_kargs_cumsum_blk args = { 1155 /*.ne00 =*/ net0, 1156 /*.ne01 =*/ net1, 1157 /*.ne02 =*/ net2, 1158 /*.ne03 =*/ net3, 1159 /*.nb00 =*/ nbt0, 1160 /*.nb01 =*/ nbt1, 1161 /*.nb02 =*/ nbt2, 1162 /*.nb03 =*/ nbt3, 1163 /*.net0 =*/ net0, 1164 /*.net1 =*/ net1, 1165 /*.net2 =*/ net2, 1166 /*.net3 =*/ net3, 1167 /*.nbt0 =*/ nbt0, 1168 /*.nbt1 =*/ nbt1, 1169 /*.nbt2 =*/ nbt2, 1170 /*.nbt3 =*/ nbt3, 1171 /*.outb =*/ false, 1172 }; 1173 1174 ggml_metal_encoder_set_pipeline(enc, pipeline_blk); 1175 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 1176 ggml_metal_encoder_set_buffer (enc, bid_tmp, 1); 1177 ggml_metal_encoder_set_buffer (enc, bid_tmp, 2); 1178 ggml_metal_encoder_set_buffer (enc, bid_tmp, 3); 1179 1180 ggml_metal_encoder_set_threadgroup_memory_size(enc, smem, 0); 1181 1182 ggml_metal_encoder_dispatch_threadgroups(enc, net1, net2, net3, nth, 1, 1); 1183 } 1184 1185 ggml_metal_op_concurrency_reset(ctx); 1186 1187 { 1188 auto pipeline_add = ggml_metal_library_get_pipeline_cumsum_add(lib, op); 1189 1190 ggml_metal_kargs_cumsum_add args = { 1191 /*.ne00 =*/ ne00, 1192 /*.ne01 =*/ ne01, 1193 /*.ne02 =*/ ne02, 1194 /*.ne03 =*/ ne03, 1195 /*.nb00 =*/ nb00, 1196 /*.nb01 =*/ nb01, 1197 /*.nb02 =*/ nb02, 1198 /*.nb03 =*/ nb03, 1199 /*.net0 =*/ net0, 1200 /*.net1 =*/ net1, 1201 /*.net2 =*/ net2, 1202 /*.net3 =*/ net3, 1203 /*.nbt0 =*/ nbt0, 1204 /*.nbt1 =*/ nbt1, 1205 /*.nbt2 =*/ nbt2, 1206 /*.nbt3 =*/ nbt3, 1207 }; 1208 1209 ggml_metal_encoder_set_pipeline(enc, pipeline_add); 1210 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 1211 ggml_metal_encoder_set_buffer (enc, bid_tmp, 1); 1212 ggml_metal_encoder_set_buffer (enc, bid_dst, 2); 1213 1214 ggml_metal_encoder_dispatch_threadgroups(enc, net0*ne01, ne02, ne03, nth, 1, 1); 1215 } 1216 } 1217 1218 return 1; 1219 } 1220 1221 int ggml_metal_op_get_rows(ggml_metal_op_t ctx, int idx) { 1222 ggml_tensor * op = ctx->node(idx); 1223 1224 ggml_metal_library_t lib = ctx->lib; 1225 ggml_metal_encoder_t enc = ctx->enc; 1226 1227 GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); 1228 GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); 1229 GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne); 1230 GGML_TENSOR_LOCALS(uint64_t, nb1, op->src[1], nb); 1231 GGML_TENSOR_LOCALS( int32_t, ne, op, ne); 1232 GGML_TENSOR_LOCALS(uint64_t, nb, op, nb); 1233 1234 auto pipeline = ggml_metal_library_get_pipeline_get_rows(lib, op->src[0]->type); 1235 1236 ggml_metal_kargs_get_rows args = { 1237 /*.ne00t =*/ ggml_is_quantized(op->src[0]->type) ? ne00/16 : ne00, 1238 /*.ne00 =*/ ne00, 1239 /*.nb01 =*/ nb01, 1240 /*.nb02 =*/ nb02, 1241 /*.nb03 =*/ nb03, 1242 /*.ne10 =*/ ne10, 1243 /*.nb10 =*/ nb10, 1244 /*.nb11 =*/ nb11, 1245 /*.nb12 =*/ nb12, 1246 /*.nb1 =*/ nb1, 1247 /*.nb2 =*/ nb2, 1248 /*.nb3 =*/ nb3, 1249 }; 1250 1251 const int nth = std::min(args.ne00t, ggml_metal_pipeline_max_theads_per_threadgroup(pipeline)); 1252 1253 const int nw0 = (args.ne00t + nth - 1)/nth; 1254 1255 ggml_metal_encoder_set_pipeline(enc, pipeline); 1256 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 1257 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1); 1258 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[1]), 2); 1259 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 3); 1260 1261 ggml_metal_encoder_dispatch_threadgroups(enc, nw0*ne10, ne11, ne12, nth, 1, 1); 1262 1263 return 1; 1264 } 1265 1266 int ggml_metal_op_set_rows(ggml_metal_op_t ctx, int idx) { 1267 ggml_tensor * op = ctx->node(idx); 1268 1269 ggml_metal_library_t lib = ctx->lib; 1270 ggml_metal_encoder_t enc = ctx->enc; 1271 1272 GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); 1273 GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); 1274 GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne); 1275 GGML_TENSOR_LOCALS(uint64_t, nb1, op->src[1], nb); 1276 GGML_TENSOR_LOCALS( int32_t, ne, op, ne); 1277 GGML_TENSOR_LOCALS(uint64_t, nb, op, nb); 1278 1279 auto pipeline = ggml_metal_library_get_pipeline_set_rows(lib, op); 1280 1281 const int32_t nk0 = ne0/ggml_blck_size(op->type); 1282 1283 int nth = 32; // SIMD width 1284 1285 while (nth < nk0 && nth < ggml_metal_pipeline_max_theads_per_threadgroup(pipeline)) { 1286 nth *= 2; 1287 } 1288 1289 int nrptg = 1; 1290 if (nth > nk0) { 1291 nrptg = (nth + nk0 - 1)/nk0; 1292 nth = nk0; 1293 1294 if (nrptg*nth > ggml_metal_pipeline_max_theads_per_threadgroup(pipeline)) { 1295 nrptg--; 1296 } 1297 } 1298 1299 nth = std::min(nth, nk0); 1300 1301 ggml_metal_kargs_set_rows args = { 1302 /*.nk0 =*/ nk0, 1303 /*.ne01 =*/ ne01, 1304 /*.nb01 =*/ nb01, 1305 /*.nb02 =*/ nb02, 1306 /*.nb03 =*/ nb03, 1307 /*.ne11 =*/ ne11, 1308 /*.ne12 =*/ ne12, 1309 /*.nb10 =*/ nb10, 1310 /*.nb11 =*/ nb11, 1311 /*.nb12 =*/ nb12, 1312 /*.nb1 =*/ nb1, 1313 /*.nb2 =*/ nb2, 1314 /*.nb3 =*/ nb3, 1315 }; 1316 1317 ggml_metal_encoder_set_pipeline(enc, pipeline); 1318 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 1319 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1); 1320 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[1]), 2); 1321 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 3); 1322 1323 ggml_metal_encoder_dispatch_threadgroups(enc, (ne01 + nrptg - 1)/nrptg, ne02, ne03, nth, nrptg, 1); 1324 1325 return 1; 1326 } 1327 1328 int ggml_metal_op_diag(ggml_metal_op_t ctx, int idx) { 1329 ggml_tensor * op = ctx->node(idx); 1330 1331 ggml_metal_library_t lib = ctx->lib; 1332 ggml_metal_encoder_t enc = ctx->enc; 1333 1334 GGML_TENSOR_LOCALS(int32_t, ne0, op->src[0], ne); 1335 GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); 1336 GGML_TENSOR_LOCALS(int32_t, ne, op, ne); 1337 GGML_TENSOR_LOCALS(uint64_t, nb, op, nb); 1338 1339 ggml_metal_kargs_diag args = { 1340 /*.ne00 =*/ne00, 1341 /*.ne01 =*/ne01, 1342 /*.ne02 =*/ne02, 1343 /*.ne03 =*/ne03, 1344 /*.nb00 =*/nb00, 1345 /*.nb01 =*/nb01, 1346 /*.nb02 =*/nb02, 1347 /*.nb03 =*/nb03, 1348 /*.ne0 =*/ne0, 1349 /*.ne1 =*/ne1, 1350 /*.ne2 =*/ne2, 1351 /*.ne3 =*/ne3, 1352 /*.nb0 =*/nb0, 1353 /*.nb1 =*/nb1, 1354 /*.nb2 =*/nb2, 1355 /*.nb3 =*/nb3, 1356 }; 1357 1358 auto pipeline = ggml_metal_library_get_pipeline_diag(lib, op); 1359 1360 ggml_metal_encoder_set_pipeline(enc, pipeline); 1361 ggml_metal_encoder_set_bytes(enc, &args, sizeof(args), 0); 1362 ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(op->src[0]), 1); 1363 ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(op), 2); 1364 1365 ggml_metal_encoder_dispatch_threadgroups(enc, ne1, ne2, ne3, 32, 1, 1); 1366 1367 return 1; 1368 } 1369 1370 int ggml_metal_op_lightning_indexer(ggml_metal_op_t ctx, int idx) { 1371 ggml_tensor * op = ctx->node(idx); 1372 1373 ggml_metal_encoder_t enc = ctx->enc; 1374 1375 GGML_ASSERT(op->op == GGML_OP_LIGHTNING_INDEXER); 1376 1377 const ggml_tensor * q = op->src[0]; 1378 const ggml_tensor * k = op->src[1]; 1379 const ggml_tensor * w = op->src[2]; 1380 const ggml_tensor * m = op->src[3]; 1381 1382 GGML_ASSERT(q->type == GGML_TYPE_F32); 1383 GGML_ASSERT(k->type == GGML_TYPE_F32 || 1384 k->type == GGML_TYPE_F16 || 1385 k->type == GGML_TYPE_BF16 || 1386 k->type == GGML_TYPE_Q4_0 || 1387 k->type == GGML_TYPE_Q4_1 || 1388 k->type == GGML_TYPE_Q5_0 || 1389 k->type == GGML_TYPE_Q5_1 || 1390 k->type == GGML_TYPE_Q8_0); 1391 GGML_ASSERT(w->type == GGML_TYPE_F32); 1392 GGML_ASSERT(m->type == GGML_TYPE_F16); 1393 GGML_ASSERT(op->type == GGML_TYPE_F32); 1394 1395 GGML_ASSERT(q->ne[0] == OP_LIGHTNING_INDEXER_DK); 1396 1397 ggml_metal_kargs_lightning_indexer args = { 1398 /*.n_kv =*/ (int32_t) k->ne[2], 1399 /*.n_batch =*/ (int32_t) q->ne[2], 1400 /*.mask_ne3 =*/ (int32_t) m->ne[3], 1401 /*.nb1 =*/ op->nb[1], 1402 /*.nb3 =*/ op->nb[3], 1403 /*.nbq1 =*/ q->nb[1], 1404 /*.nbq2 =*/ q->nb[2], 1405 /*.nbq3 =*/ q->nb[3], 1406 /*.nbk2 =*/ k->nb[2], 1407 /*.nbk3 =*/ k->nb[3], 1408 /*.nbw1 =*/ w->nb[1], 1409 /*.nbw3 =*/ w->nb[3], 1410 /*.nbm1 =*/ m->nb[1], 1411 /*.nbm3 =*/ m->nb[3], 1412 }; 1413 1414 ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(q), 1); 1415 ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(k), 2); 1416 ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(w), 3); 1417 ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(m), 4); 1418 ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(op), 5); 1419 1420 const int nsg = OP_LIGHTNING_INDEXER_NSG; 1421 const int nkptg = OP_LIGHTNING_INDEXER_NKPSG*nsg; 1422 const int nbptg = OP_LIGHTNING_INDEXER_NBPTG; 1423 1424 auto pipeline = ggml_metal_library_get_pipeline_lightning_indexer(ctx->lib, op); 1425 ggml_metal_encoder_set_pipeline(enc, pipeline); 1426 ggml_metal_encoder_set_bytes(enc, &args, sizeof(args), 0); 1427 ggml_metal_encoder_dispatch_threadgroups(enc, 1428 (k->ne[2] + nkptg - 1)/nkptg, 1429 (q->ne[2] + nbptg - 1)/nbptg, 1430 q->ne[3], 32, nsg, 1); 1431 1432 return 1; 1433 } 1434 1435 int ggml_metal_op_top_k_mask(ggml_metal_op_t ctx, int idx) { 1436 ggml_tensor * op = ctx->node(idx); 1437 1438 ggml_metal_library_t lib = ctx->lib; 1439 ggml_metal_encoder_t enc = ctx->enc; 1440 1441 const ggml_tensor * mask = op->src[0]; 1442 const ggml_tensor * top_k = op->src[1]; 1443 const ggml_tensor * score = op->src[2]; 1444 const ggml_tensor * blocks = op->src[3]; 1445 const ggml_tensor * extra = op->src[4]; 1446 1447 GGML_ASSERT(op->type == GGML_TYPE_F16 || op->type == GGML_TYPE_F32); 1448 GGML_ASSERT(mask->type == op->type); 1449 GGML_ASSERT(top_k->type == GGML_TYPE_I32); 1450 GGML_ASSERT(!score || score->type == GGML_TYPE_F32); 1451 GGML_ASSERT(!blocks || blocks->type == GGML_TYPE_I32); 1452 GGML_ASSERT(!extra || extra->type == GGML_TYPE_I32); 1453 1454 const int32_t n_kv = mask->ne[0]; 1455 const int32_t n_top = (int32_t) top_k->ne[0]; 1456 const int32_t n_pick = blocks ? blocks->ne[1] : score ? score->ne[0] : n_kv; 1457 const int32_t n_blk = blocks ? blocks->ne[0] : 0; 1458 const int32_t n_extra = extra ? extra->ne[0] : 0; 1459 1460 const ggml_metal_buffer_id bid = ggml_metal_get_buffer_id(op); 1461 1462 // the vector fill stores 4 elements at a time 1463 const int32_t ts = ggml_type_size(op->type); 1464 const int64_t n_elems = ggml_nelements(op); 1465 1466 const bool has_score = score != NULL; 1467 const bool has_blocks = blocks != NULL; 1468 const bool has_extra = extra != NULL; 1469 1470 const bool vec = n_elems % 4 == 0 && bid.offs % (4*ts) == 0; 1471 1472 ggml_metal_kargs_top_k_mask args = { 1473 /*.n_kv =*/ n_kv, 1474 /*.m1 =*/ (int32_t) mask->ne[1], 1475 /*.m2 =*/ (int32_t) mask->ne[2], 1476 /*.m3 =*/ (int32_t) mask->ne[3], 1477 /*.n_top =*/ n_top, 1478 /*.n_pick =*/ n_pick, 1479 /*.n_blk =*/ n_blk, 1480 /*.n_extra =*/ n_extra, 1481 /*.n_elems =*/ n_elems, 1482 /*.nb1 =*/ op->nb[1], 1483 /*.nb2 =*/ op->nb[2], 1484 /*.nb3 =*/ op->nb[3], 1485 /*.nbm1 =*/ mask->nb[1], 1486 /*.nbm2 =*/ mask->nb[2], 1487 /*.nbm3 =*/ mask->nb[3], 1488 /*.nbk0 =*/ top_k->nb[0], 1489 /*.nbk1 =*/ top_k->nb[1], 1490 /*.nbk2 =*/ top_k->nb[2], 1491 /*.nbk3 =*/ top_k->nb[3], 1492 /*.nbs0 =*/ score ? score->nb[0] : 0, 1493 /*.nbs1 =*/ score ? score->nb[1] : 0, 1494 /*.nbs2 =*/ score ? score->nb[2] : 0, 1495 /*.nbs3 =*/ score ? score->nb[3] : 0, 1496 /*.nbb0 =*/ blocks ? blocks->nb[0] : 0, 1497 /*.nbb1 =*/ blocks ? blocks->nb[1] : 0, 1498 /*.nbe0 =*/ extra ? extra->nb[0] : 0, 1499 /*.nbe1 =*/ extra ? extra->nb[1] : 0, 1500 /*.nbe2 =*/ extra ? extra->nb[2] : 0, 1501 /*.nbe3 =*/ extra ? extra->nb[3] : 0, 1502 }; 1503 1504 // the fill covers the whole dst, the scatter overwrites the kept cells with the mask values 1505 { 1506 auto pipeline_fill = ggml_metal_library_get_pipeline_top_k_mask(lib, op, true, false, false, false, vec); 1507 1508 const int64_t n_fill = n_elems/(vec ? 4 : 1); 1509 const int32_t nth_max = ggml_metal_pipeline_max_theads_per_threadgroup(pipeline_fill); 1510 1511 int nth = 32; // SIMD width 1512 1513 while (nth < nth_max && nth < n_fill) { 1514 nth *= 2; 1515 } 1516 1517 ggml_metal_encoder_set_pipeline(enc, pipeline_fill); 1518 ggml_metal_encoder_set_bytes(enc, &args, sizeof(args), 0); 1519 ggml_metal_encoder_set_buffer(enc, bid, 1); 1520 1521 ggml_metal_encoder_dispatch_threadgroups(enc, (n_fill + nth - 1)/nth, 1, 1, nth, 1, 1); 1522 1523 ggml_metal_op_concurrency_reset(ctx); 1524 } 1525 1526 { 1527 auto pipeline = ggml_metal_library_get_pipeline_top_k_mask(lib, op, false, has_score, has_blocks, has_extra, false); 1528 1529 const int32_t n_max = n_top + n_extra; 1530 const int32_t nth_max = ggml_metal_pipeline_max_theads_per_threadgroup(pipeline); 1531 1532 int nth = 32; // SIMD width 1533 1534 while (nth < nth_max && nth < n_max) { 1535 nth *= 2; 1536 } 1537 1538 ggml_metal_encoder_set_pipeline(enc, pipeline); 1539 ggml_metal_encoder_set_bytes(enc, &args, sizeof(args), 0); 1540 ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(mask), 1); 1541 ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(top_k), 2); 1542 ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(score ? score : mask), 3); 1543 ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(blocks ? blocks : mask), 4); 1544 ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(extra ? extra : mask), 5); 1545 ggml_metal_encoder_set_buffer(enc, bid, 6); 1546 1547 ggml_metal_encoder_dispatch_threadgroups(enc, op->ne[1], op->ne[2], op->ne[3], nth, 1, 1); 1548 } 1549 1550 return 1; 1551 } 1552 1553 int ggml_metal_op_dsv4_hc(ggml_metal_op_t ctx, int idx) { 1554 ggml_tensor * op = ctx->node(idx); 1555 1556 ggml_metal_encoder_t enc = ctx->enc; 1557 auto pipeline = ggml_metal_library_get_pipeline_dsv4_hc(ctx->lib, op); 1558 1559 ggml_metal_encoder_set_pipeline(enc, pipeline); 1560 1561 switch (op->op) { 1562 case GGML_OP_DSV4_HC_COMB: 1563 { 1564 const ggml_tensor * mixes = op->src[0]; 1565 const ggml_tensor * scale = op->src[1]; 1566 const ggml_tensor * base = op->src[2]; 1567 1568 GGML_ASSERT(mixes->type == GGML_TYPE_F32); 1569 GGML_ASSERT(scale->type == GGML_TYPE_F32); 1570 GGML_ASSERT(base->type == GGML_TYPE_F32); 1571 GGML_ASSERT(op->type == GGML_TYPE_F32); 1572 GGML_ASSERT(mixes->ne[0] == 24); 1573 GGML_ASSERT(op->ne[0] == 4 && op->ne[1] == 4); 1574 1575 ggml_metal_kargs_dsv4_hc_comb args = { 1576 /*.n_tokens =*/ (int32_t) mixes->ne[1], 1577 /*.n_iter =*/ ggml_get_op_params_i32(op, 1), 1578 /*.nb_m0 =*/ mixes->nb[0], 1579 /*.nb_m1 =*/ mixes->nb[1], 1580 /*.nb_s0 =*/ scale->nb[0], 1581 /*.nb_b0 =*/ base->nb[0], 1582 /*.nb_d0 =*/ op->nb[0], 1583 /*.nb_d1 =*/ op->nb[1], 1584 /*.nb_d2 =*/ op->nb[2], 1585 /*.eps =*/ ggml_get_op_params_f32(op, 0), 1586 }; 1587 1588 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 1589 ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(mixes), 1); 1590 ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(scale), 2); 1591 ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(base), 3); 1592 ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(op), 4); 1593 1594 // One SIMDgroup owns one 4x4 Sinkhorn matrix. Packing up to four 1595 // independent tokens per threadgroup keeps both decode and prompt 1596 // dispatches compact without any threadgroup-memory synchronization. 1597 const int nsg = std::min(4, args.n_tokens); 1598 ggml_metal_encoder_dispatch_threadgroups( 1599 enc, (args.n_tokens + nsg - 1)/nsg, 1, 1, 32, nsg, 1); 1600 } break; 1601 case GGML_OP_DSV4_HC_PRE: 1602 { 1603 const ggml_tensor * x = op->src[0]; 1604 const ggml_tensor * weights = op->src[1]; 1605 1606 GGML_ASSERT(x->type == GGML_TYPE_F32); 1607 GGML_ASSERT(weights->type == GGML_TYPE_F32); 1608 GGML_ASSERT(op->type == GGML_TYPE_F32); 1609 1610 ggml_metal_kargs_dsv4_hc_pre args = { 1611 /*.n_embd =*/ (int32_t) x->ne[0], 1612 /*.n_tokens =*/ (int32_t) x->ne[2], 1613 /*.nb_x0 =*/ x->nb[0], 1614 /*.nb_x1 =*/ x->nb[1], 1615 /*.nb_x2 =*/ x->nb[2], 1616 /*.nb_w0 =*/ weights->nb[0], 1617 /*.nb_w1 =*/ weights->nb[1], 1618 /*.nb_w2 =*/ weights->nb[2], 1619 /*.nb_d0 =*/ op->nb[0], 1620 /*.nb_d1 =*/ op->nb[1], 1621 /*.scale =*/ ggml_get_op_params_f32(op, 0), 1622 }; 1623 1624 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 1625 ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(x), 1); 1626 ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(weights), 2); 1627 ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(op), 3); 1628 1629 const int n_tiles = (args.n_embd + 31)/32; 1630 const int nsg = std::min(4, n_tiles); 1631 ggml_metal_encoder_dispatch_threadgroups( 1632 enc, (n_tiles + nsg - 1)/nsg, args.n_tokens, 1, 32, nsg, 1); 1633 } break; 1634 case GGML_OP_DSV4_HC_POST: 1635 { 1636 const ggml_tensor * x = op->src[0]; 1637 const ggml_tensor * residual = op->src[1]; 1638 const ggml_tensor * post = op->src[2]; 1639 const ggml_tensor * comb = op->src[3]; 1640 1641 GGML_ASSERT(x->type == GGML_TYPE_F32); 1642 GGML_ASSERT(residual->type == GGML_TYPE_F32); 1643 GGML_ASSERT(post->type == GGML_TYPE_F32); 1644 GGML_ASSERT(op->type == GGML_TYPE_F32); 1645 GGML_ASSERT(residual->ne[1] == 4); 1646 1647 ggml_metal_kargs_dsv4_hc_post args = { 1648 /*.n_embd =*/ (int32_t) x->ne[0], 1649 /*.n_tokens =*/ (int32_t) x->ne[1], 1650 /*.nb_x0 =*/ x->nb[0], 1651 /*.nb_x1 =*/ x->nb[1], 1652 /*.nb_r0 =*/ residual->nb[0], 1653 /*.nb_r1 =*/ residual->nb[1], 1654 /*.nb_r2 =*/ residual->nb[2], 1655 /*.nb_p0 =*/ post->nb[0], 1656 /*.nb_p1 =*/ post->nb[1], 1657 /*.nb_c0 =*/ comb ? comb->nb[0] : 0, 1658 /*.nb_c1 =*/ comb ? comb->nb[1] : 0, 1659 /*.nb_c2 =*/ comb ? comb->nb[2] : 0, 1660 /*.nb_d0 =*/ op->nb[0], 1661 /*.nb_d1 =*/ op->nb[1], 1662 /*.nb_d2 =*/ op->nb[2], 1663 }; 1664 1665 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 1666 ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(x), 1); 1667 ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(residual), 2); 1668 ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(post), 3); 1669 if (comb) { 1670 ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(comb), 4); 1671 ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(op), 5); 1672 } else { 1673 ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(op), 4); 1674 } 1675 1676 const int n_tiles = (args.n_embd + 31)/32; 1677 const int nsg = std::min(4, n_tiles); 1678 ggml_metal_encoder_dispatch_threadgroups( 1679 enc, (n_tiles + nsg - 1)/nsg, args.n_tokens, 1, 32, nsg, 1); 1680 } break; 1681 default: 1682 GGML_ABORT("fatal error"); 1683 } 1684 1685 return 1; 1686 } 1687 1688 int ggml_metal_op_soft_max(ggml_metal_op_t ctx, int idx) { 1689 ggml_tensor * op = ctx->node(idx); 1690 1691 if (ctx->use_fusion()) { 1692 int n = 1; 1693 const ggml_metal_fusion * fusion = ctx->can_fuse(idx, GGML_METAL_FUSION_FULL, &n); 1694 if (fusion && ggml_metal_fusion_get_id(fusion) == GGML_METAL_FUSION_TOPK_MOE) { 1695 return ggml_metal_op_topk_moe(ctx, idx); 1696 } 1697 } 1698 1699 ggml_metal_library_t lib = ctx->lib; 1700 ggml_metal_encoder_t enc = ctx->enc; 1701 1702 GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); 1703 GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); 1704 GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne); 1705 GGML_TENSOR_LOCALS(uint64_t, nb1, op->src[1], nb); 1706 GGML_TENSOR_LOCALS( int32_t, ne2, op->src[2], ne); 1707 GGML_TENSOR_LOCALS(uint64_t, nb2, op->src[2], nb); 1708 GGML_TENSOR_LOCALS( int32_t, ne, op, ne); 1709 GGML_TENSOR_LOCALS(uint64_t, nb, op, nb); 1710 1711 float scale; 1712 float max_bias; 1713 1714 memcpy(&scale, ((const int32_t *) op->op_params) + 0, sizeof(scale)); 1715 memcpy(&max_bias, ((const int32_t *) op->op_params) + 1, sizeof(max_bias)); 1716 1717 const uint32_t n_head = op->src[0]->ne[2]; 1718 const int32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head)); 1719 1720 const float m0 = powf(2.0f, -(max_bias ) / n_head_log2); 1721 const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2); 1722 1723 // softmax 1724 1725 ggml_metal_kargs_soft_max args = { 1726 /*.ne00 =*/ ne00, 1727 /*.ne01 =*/ ne01, 1728 /*.ne02 =*/ ne02, 1729 /*.nb01 =*/ nb01, 1730 /*.nb02 =*/ nb02, 1731 /*.nb03 =*/ nb03, 1732 /*.ne11 =*/ ne11, 1733 /*.ne12 =*/ ne12, 1734 /*.ne13 =*/ ne13, 1735 /*.nb11 =*/ nb11, 1736 /*.nb12 =*/ nb12, 1737 /*.nb13 =*/ nb13, 1738 /*.nb1 =*/ nb1, 1739 /*.nb2 =*/ nb2, 1740 /*.nb3 =*/ nb3, 1741 /*.scale =*/ scale, 1742 /*.max_bias =*/ max_bias, 1743 /*.m0 =*/ m0, 1744 /*.m1 =*/ m1, 1745 /*.n_head_log2 =*/ n_head_log2, 1746 }; 1747 1748 auto pipeline = ggml_metal_library_get_pipeline_soft_max(lib, op); 1749 1750 int nth = 32; // SIMD width 1751 1752 if (ne00%4 == 0) { 1753 while (nth < ne00/4 && nth*ne01*ne02*ne03 < 256) { 1754 nth *= 2; 1755 } 1756 } else { 1757 while (nth < ne00 && nth*ne01*ne02*ne03 < 256) { 1758 nth *= 2; 1759 } 1760 } 1761 1762 const size_t smem = pipeline.smem; 1763 1764 ggml_metal_encoder_set_pipeline(enc, pipeline); 1765 ggml_metal_encoder_set_bytes(enc, &args, sizeof(args), 0); 1766 ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(op->src[0]), 1); 1767 if (op->src[1]) { 1768 ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(op->src[1]), 2); 1769 } else { 1770 ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(op->src[0]), 2); 1771 } 1772 if (op->src[2]) { 1773 ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(op->src[2]), 3); 1774 } else { 1775 ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(op->src[0]), 3); 1776 } 1777 ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(op), 4); 1778 1779 ggml_metal_encoder_set_threadgroup_memory_size(enc, smem, 0); 1780 1781 ggml_metal_encoder_dispatch_threadgroups(enc, ne01, ne02, ne03, nth, 1, 1); 1782 1783 return 1; 1784 } 1785 1786 int ggml_metal_op_ssm_conv(ggml_metal_op_t ctx, int idx) { 1787 ggml_tensor * op = ctx->node(idx); 1788 1789 ggml_metal_library_t lib = ctx->lib; 1790 ggml_metal_encoder_t enc = ctx->enc; 1791 1792 GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); 1793 GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); 1794 GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne); 1795 GGML_TENSOR_LOCALS(uint64_t, nb1, op->src[1], nb); 1796 GGML_TENSOR_LOCALS( int32_t, ne, op, ne); 1797 GGML_TENSOR_LOCALS(uint64_t, nb, op, nb); 1798 1799 int n_fuse = 1; 1800 bool use_silu = false; 1801 1802 if (ctx->use_fusion()) { 1803 int n = 1; 1804 const ggml_metal_fusion * fusion = ctx->can_fuse(idx, GGML_METAL_FUSION_FULL, &n); 1805 if (fusion && ggml_metal_fusion_get_id(fusion) == GGML_METAL_FUSION_SSM_CONV_SILU) { 1806 n_fuse = n; 1807 use_silu = true; 1808 1809 ctx->count_fusions(fusion); 1810 } 1811 } 1812 1813 ggml_metal_kargs_ssm_conv args = { 1814 /*.ne00 =*/ ne00, 1815 /*.ne01 =*/ ne01, 1816 /*.ne02 =*/ ne02, 1817 /*.nb00 =*/ nb00, 1818 /*.nb01 =*/ nb01, 1819 /*.nb02 =*/ nb02, 1820 /*.ne11 =*/ ne11, 1821 /*.nb10 =*/ nb10, 1822 /*.nb11 =*/ nb11, 1823 /*.ne0 =*/ ne0, 1824 /*.ne1 =*/ ne1, 1825 /*.ne2 =*/ ne2, 1826 /*.nb0 =*/ nb0, 1827 /*.nb1 =*/ nb1, 1828 /*.nb2 =*/ nb2, 1829 }; 1830 1831 const ggml_metal_buffer_id bid_dst = ggml_metal_get_buffer_id(n_fuse > 1 ? ctx->node(idx + n_fuse - 1) : op); 1832 1833 // Use batched kernel for prefill (ne1 > 1) to reduce threadgroup dispatch overhead 1834 const bool use_batched = (ne1 > 1); 1835 1836 if (use_batched) { 1837 // Determine the smallest power of 2 that's >= ne1, but <= 256 1838 int BATCH_SIZE; 1839 if (ne1 > 128) BATCH_SIZE = 256; 1840 else if (ne1 > 64 ) BATCH_SIZE = 128; 1841 else if (ne1 > 32 ) BATCH_SIZE = 64; 1842 else if (ne1 > 16 ) BATCH_SIZE = 32; 1843 else if (ne1 > 8 ) BATCH_SIZE = 16; 1844 else if (ne1 > 4 ) BATCH_SIZE = 8; 1845 else BATCH_SIZE = 2; 1846 1847 auto pipeline = ggml_metal_library_get_pipeline_ssm_conv_batched(lib, op, BATCH_SIZE, (int32_t) ne10, use_silu); 1848 1849 ggml_metal_encoder_set_pipeline(enc, pipeline); 1850 ggml_metal_encoder_set_bytes(enc, &args, sizeof(args), 0); 1851 ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(op->src[0]), 1); 1852 ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(op->src[1]), 2); 1853 ggml_metal_encoder_set_buffer(enc, bid_dst, 3); 1854 1855 // Dispatch: ne01 rows, ceil(ne1/BATCH_SIZE) token batches, ne02 sequences 1856 // Each threadgroup has BATCH_SIZE threads, each handling one token 1857 const int n_token_batches = (ne1 + BATCH_SIZE - 1) / BATCH_SIZE; 1858 ggml_metal_encoder_dispatch_threadgroups(enc, ne01, n_token_batches, ne02, BATCH_SIZE, 1, 1); 1859 } else { 1860 auto pipeline = ggml_metal_library_get_pipeline_ssm_conv(lib, op, (int32_t) ne10, use_silu); 1861 1862 ggml_metal_encoder_set_pipeline(enc, pipeline); 1863 ggml_metal_encoder_set_bytes(enc, &args, sizeof(args), 0); 1864 ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(op->src[0]), 1); 1865 ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(op->src[1]), 2); 1866 ggml_metal_encoder_set_buffer(enc, bid_dst, 3); 1867 1868 ggml_metal_encoder_dispatch_threadgroups(enc, ne01, ne1, ne02, 1, 1, 1); 1869 } 1870 1871 if (n_fuse > 1 && ggml_metal_fusion_info_debug(ctx->finfo) > 1) { 1872 GGML_LOG_DEBUG("%s: fuse: SSM_CONV + UNARY\n", __func__); 1873 } 1874 1875 return n_fuse; 1876 } 1877 1878 int ggml_metal_op_ssm_scan(ggml_metal_op_t ctx, int idx) { 1879 ggml_tensor * op = ctx->node(idx); 1880 1881 ggml_metal_library_t lib = ctx->lib; 1882 ggml_metal_encoder_t enc = ctx->enc; 1883 const ggml_metal_device_props * props_dev = ggml_metal_device_get_props(ctx->dev); 1884 1885 GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); 1886 GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); 1887 GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne); 1888 GGML_TENSOR_LOCALS(uint64_t, nb1, op->src[1], nb); 1889 GGML_TENSOR_LOCALS( int32_t, ne2, op->src[2], ne); 1890 GGML_TENSOR_LOCALS(uint64_t, nb2, op->src[2], nb); 1891 GGML_TENSOR_LOCALS( int32_t, ne3, op->src[3], ne); 1892 GGML_TENSOR_LOCALS(uint64_t, nb3, op->src[3], nb); 1893 GGML_TENSOR_LOCALS( int32_t, ne4, op->src[4], ne); 1894 GGML_TENSOR_LOCALS(uint64_t, nb4, op->src[4], nb); 1895 GGML_TENSOR_LOCALS( int32_t, ne5, op->src[5], ne); 1896 GGML_TENSOR_LOCALS(uint64_t, nb5, op->src[5], nb); 1897 GGML_TENSOR_LOCALS( int32_t, ne6, op->src[6], ne); 1898 GGML_TENSOR_LOCALS(uint64_t, nb6, op->src[6], nb); 1899 GGML_TENSOR_LOCALS( int32_t, ne, op, ne); 1900 GGML_TENSOR_LOCALS(uint64_t, nb, op, nb); 1901 1902 const ggml_tensor * src3 = op->src[3]; 1903 const ggml_tensor * src4 = op->src[4]; 1904 const ggml_tensor * src5 = op->src[5]; 1905 const ggml_tensor * src6 = op->src[6]; 1906 1907 GGML_ASSERT(src3); 1908 GGML_ASSERT(src4); 1909 GGML_ASSERT(src5); 1910 GGML_ASSERT(src6); 1911 1912 const int64_t d_state = ne00; 1913 const int64_t d_inner = ne01; 1914 const int64_t n_head = ne02; 1915 const int64_t n_group = ne41; 1916 const int64_t n_seq_tokens = ne12; 1917 const int64_t n_seqs = ne13; 1918 const int64_t K = ggml_get_op_params_i32(op, 0); 1919 1920 GGML_ASSERT(K >= 1); 1921 GGML_ASSERT(ggml_nelements(op->src[1]) + K*d_state*d_inner*n_head*n_seqs == ggml_nelements(op)); 1922 1923 ggml_metal_kargs_ssm_scan args = { 1924 /*.d_state =*/ d_state, 1925 /*.d_inner =*/ d_inner, 1926 /*.n_head =*/ n_head, 1927 /*.n_group =*/ n_group, 1928 /*.n_seq_tokens =*/ n_seq_tokens, 1929 /*.n_seq_tokens_total =*/ n_seq_tokens, 1930 /*.token_offset =*/ 0, 1931 /*.n_seqs =*/ n_seqs, 1932 /*.K =*/ K, 1933 /*.s_off =*/ ggml_nelements(op->src[1]) * sizeof(float), 1934 /*.nb00 =*/ nb00, 1935 /*.nb01 =*/ nb01, 1936 /*.nb02 =*/ nb02, 1937 /*.nb03 =*/ nb03, 1938 /*.nb10 =*/ nb10, 1939 /*.nb11 =*/ nb11, 1940 /*.nb12 =*/ nb12, 1941 /*.ns12 =*/ nb12/nb10, 1942 /*.nb13 =*/ nb13, 1943 /*.nb20 =*/ nb20, 1944 /*.nb21 =*/ nb21, 1945 /*.ns21 =*/ nb21/nb20, 1946 /*.nb22 =*/ nb22, 1947 /*.ne30 =*/ ne30, 1948 /*.nb31 =*/ nb31, 1949 /*.nb41 =*/ nb41, 1950 /*.nb42 =*/ nb42, 1951 /*.ns42 =*/ nb42/nb40, 1952 /*.nb43 =*/ nb43, 1953 /*.nb51 =*/ nb51, 1954 /*.nb52 =*/ nb52, 1955 /*.ns52 =*/ nb52/nb50, 1956 /*.nb53 =*/ nb53, 1957 /*.nb0 =*/ nb0, 1958 }; 1959 1960 constexpr int64_t CHUNK = OP_SSM_SCAN_SSD_CS; 1961 1962 const int64_t snap_reserve = K > 1 ? K : 0; // tokens reserved for sequential kernel rollback snapshots 1963 const int64_t mma_tokens = ((n_seq_tokens - snap_reserve) / CHUNK) * CHUNK; // largest multiple of CHUNK that leaves snap_reserve for the tail 1964 const bool use_mma = 1965 mma_tokens > 0 && 1966 ne30 == 1 && // checks that A tensor is set to scalar decay per head (A shape {1, n_head}) 1967 props_dev->has_simdgroup_mm && // hardware check for M1 or newer 1968 d_state % 8 == 0 && // d_state must be multiple of 8 to align with simdgroup_float 8x8 tiles 1969 d_inner == OP_SSM_SCAN_SSD_HD; // mma kernel is specialized for the Mamba-2 head dim; this checks it 1970 1971 const auto dispatch = [&](ggml_metal_pipeline_with_params pipeline, int64_t nth, int64_t n_tg_x) { 1972 GGML_ASSERT(nth <= ggml_metal_pipeline_max_theads_per_threadgroup(pipeline)); 1973 GGML_ASSERT(pipeline.smem <= props_dev->max_theadgroup_memory_size); 1974 1975 ggml_metal_encoder_set_pipeline(enc, pipeline); 1976 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 1977 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1); 1978 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[1]), 2); 1979 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[2]), 3); 1980 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[3]), 4); 1981 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[4]), 5); 1982 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[5]), 6); 1983 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[6]), 7); 1984 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 8); 1985 ggml_metal_encoder_set_threadgroup_memory_size(enc, pipeline.smem, 0); 1986 ggml_metal_encoder_dispatch_threadgroups(enc, n_tg_x, n_head, n_seqs, nth, 1, 1); 1987 }; 1988 1989 if (!use_mma) { 1990 dispatch(ggml_metal_library_get_pipeline_ssm_scan(lib, op, false), d_state, d_inner); 1991 return 1; 1992 } 1993 1994 args.n_seq_tokens = mma_tokens; 1995 dispatch( 1996 ggml_metal_library_get_pipeline_ssm_scan_ssd_mma(lib, op), 1997 OP_SSM_SCAN_SSD_NSG*32, 1998 1); 1999 2000 if (mma_tokens < n_seq_tokens) { 2001 ggml_metal_op_concurrency_reset(ctx); 2002 2003 args.n_seq_tokens = n_seq_tokens - mma_tokens; 2004 args.token_offset = mma_tokens; 2005 dispatch(ggml_metal_library_get_pipeline_ssm_scan(lib, op, true), d_state, d_inner); 2006 } 2007 2008 return 1; 2009 } 2010 2011 int ggml_metal_op_rwkv(ggml_metal_op_t ctx, int idx) { 2012 ggml_tensor * op = ctx->node(idx); 2013 2014 ggml_metal_library_t lib = ctx->lib; 2015 ggml_metal_encoder_t enc = ctx->enc; 2016 2017 GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); 2018 GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); 2019 GGML_TENSOR_LOCALS( int32_t, ne, op, ne); 2020 GGML_TENSOR_LOCALS(uint64_t, nb, op, nb); 2021 2022 const int64_t B = op->op == GGML_OP_RWKV_WKV6 ? op->src[5]->ne[1] : op->src[6]->ne[1]; 2023 const int64_t T = op->src[0]->ne[2]; 2024 const int64_t C = op->ne[0]; 2025 const int64_t H = op->src[0]->ne[1]; 2026 2027 auto pipeline = ggml_metal_library_get_pipeline_rwkv(lib, op); 2028 2029 int ida = 0; 2030 2031 ggml_metal_encoder_set_pipeline(enc, pipeline); 2032 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), ida++); 2033 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[1]), ida++); 2034 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[2]), ida++); 2035 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[3]), ida++); 2036 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[4]), ida++); 2037 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[5]), ida++); 2038 if (op->op == GGML_OP_RWKV_WKV7) { 2039 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[6]), ida++); 2040 } 2041 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), ida++); 2042 ggml_metal_encoder_set_bytes (enc, (void *) &B, sizeof(B), ida++); 2043 ggml_metal_encoder_set_bytes (enc, (void *) &T, sizeof(T), ida++); 2044 ggml_metal_encoder_set_bytes (enc, (void *) &C, sizeof(C), ida++); 2045 ggml_metal_encoder_set_bytes (enc, (void *) &H, sizeof(H), ida++); 2046 2047 ggml_metal_encoder_dispatch_threadgroups(enc, B * H, 1, 1, C/H, 1, 1); 2048 2049 return 1; 2050 } 2051 2052 int ggml_metal_op_gated_delta_net(ggml_metal_op_t ctx, int idx) { 2053 ggml_tensor * op = ctx->node(idx); 2054 2055 ggml_metal_library_t lib = ctx->lib; 2056 ggml_metal_encoder_t enc = ctx->enc; 2057 2058 const bool use_fusion = ctx->use_fusion(); 2059 const int debug_fusion = ggml_metal_fusion_info_debug(ctx->finfo); 2060 2061 GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); 2062 GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); 2063 GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne); 2064 GGML_TENSOR_LOCALS(uint64_t, nb1, op->src[1], nb); 2065 GGML_TENSOR_LOCALS( int32_t, ne2, op->src[2], ne); 2066 GGML_TENSOR_LOCALS(uint64_t, nb2, op->src[2], nb); 2067 GGML_TENSOR_LOCALS( int32_t, ne, op, ne); 2068 GGML_TENSOR_LOCALS(uint64_t, nb, op, nb); 2069 2070 auto pipeline = ggml_metal_library_get_pipeline_gated_delta_net(lib, op); 2071 2072 // when fused with the trailing cache cpy, the snapshots are written straight into the 2073 // recurrent cache and the cpy is skipped (see GGML_METAL_FUSION_GDN_CACHE) 2074 ggml_metal_buffer_id bid_out = ggml_metal_get_buffer_id(op); 2075 uint64_t nb_out = 0; 2076 int n_fuse = 1; 2077 2078 if (use_fusion) { 2079 int n = 1; 2080 const ggml_metal_fusion * fusion = ctx->can_fuse(idx, GGML_METAL_FUSION_FULL, &n); 2081 2082 if (fusion && ggml_metal_fusion_get_id(fusion) == GGML_METAL_FUSION_GDN_CACHE) { 2083 const ggml_tensor * dst_cache = ctx->node(idx + 1)->src[1]; // cache view 2084 2085 bid_out = ggml_metal_get_buffer_id(dst_cache); 2086 nb_out = dst_cache->nb[2]/sizeof(float); 2087 n_fuse = 2; 2088 2089 ctx->count_fusions(fusion); 2090 2091 if (debug_fusion > 1) { 2092 GGML_LOG_DEBUG("%s: fuse: GATED_DELTA_NET + CPY\n", __func__); 2093 } 2094 } 2095 } 2096 2097 int ida = 0; 2098 2099 ggml_metal_kargs_gated_delta_net args = { 2100 /*.ne00 =*/ ne00, 2101 /*.ne01 =*/ ne01, 2102 /*.ne02 =*/ ne02, 2103 /*.ne03 =*/ ne03, 2104 /*.nb00 =*/ nb00, 2105 /*.nb01 =*/ nb01, 2106 /*.nb02 =*/ nb02, 2107 /*.nb03 =*/ nb03, 2108 /*.ne10 =*/ ne10, 2109 /*.ne11 =*/ ne11, 2110 /*.ne12 =*/ ne12, 2111 /*.ne13 =*/ ne13, 2112 /*.nb10 =*/ nb10, 2113 /*.nb11 =*/ nb11, 2114 /*.nb12 =*/ nb12, 2115 /*.nb13 =*/ nb13, 2116 /*.ne20 =*/ ne20, 2117 /*.ne21 =*/ ne21, 2118 /*.ne22 =*/ ne22, 2119 /*.ne23 =*/ ne23, 2120 /*.nb20 =*/ nb20, 2121 /*.nb21 =*/ nb21, 2122 /*.nb22 =*/ nb22, 2123 /*.nb23 =*/ nb23, 2124 /*.ns02 =*/ (int32_t) (nb02/sizeof(float)), 2125 /*.ns12 =*/ (int32_t) (nb12/sizeof(float)), 2126 /*.ns22 =*/ (int32_t) (nb22/sizeof(float)), 2127 /*.ne0 =*/ ne0, 2128 /*.ne1 =*/ ne1, 2129 /*.ne2 =*/ ne2, 2130 /*.ne3 =*/ ne3, 2131 /*.nb0 =*/ nb0, 2132 /*.nb1 =*/ nb1, 2133 /*.nb2 =*/ nb2, 2134 /*.nb3 =*/ nb3, 2135 /*.nb_out =*/ nb_out, 2136 }; 2137 2138 ggml_metal_encoder_set_pipeline(enc, pipeline); 2139 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), ida++); // args 2140 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), ida++); // q 2141 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[1]), ida++); // k 2142 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[2]), ida++); // v 2143 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[3]), ida++); // gate 2144 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[4]), ida++); // beta 2145 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[5]), ida++); // state 2146 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), ida++); // dst (attn) 2147 ggml_metal_encoder_set_buffer (enc, bid_out, ida++); // state_out 2148 2149 const int nsg = pipeline.nsg; 2150 2151 ggml_metal_encoder_dispatch_threadgroups(enc, op->src[2]->ne[0]/nsg, op->src[2]->ne[1], op->src[2]->ne[3], 32, nsg, 1); 2152 2153 return n_fuse; 2154 } 2155 2156 int ggml_metal_op_solve_tri(ggml_metal_op_t ctx, int idx) { 2157 ggml_tensor * op = ctx->node(idx); 2158 2159 ggml_metal_library_t lib = ctx->lib; 2160 ggml_metal_encoder_t enc = ctx->enc; 2161 2162 GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); 2163 GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); 2164 GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne); 2165 GGML_TENSOR_LOCALS(uint64_t, nb1, op->src[1], nb); 2166 GGML_TENSOR_LOCALS( int32_t, ne, op, ne); 2167 GGML_TENSOR_LOCALS(uint64_t, nb, op, nb); 2168 2169 ggml_metal_kargs_solve_tri args = { 2170 /*.ne00 =*/ ne00, 2171 /*.ne01 =*/ ne01, 2172 /*.ne02 =*/ ne02, 2173 /*.ne03 =*/ ne03, 2174 /*.nb00 =*/ nb00, 2175 /*.nb01 =*/ nb01, 2176 /*.nb02 =*/ nb02, 2177 /*.nb03 =*/ nb03, 2178 /*.ne10 =*/ ne10, 2179 /*.ne11 =*/ ne11, 2180 /*.ne12 =*/ ne12, 2181 /*.ne13 =*/ ne13, 2182 /*.nb10 =*/ nb10, 2183 /*.nb11 =*/ nb11, 2184 /*.nb12 =*/ nb12, 2185 /*.nb13 =*/ nb13, 2186 /*.ne0 =*/ ne0, 2187 /*.ne1 =*/ ne1, 2188 /*.ne2 =*/ ne2, 2189 /*.ne3 =*/ ne3, 2190 /*.nb0 =*/ nb0, 2191 /*.nb1 =*/ nb1, 2192 /*.nb2 =*/ nb2, 2193 /*.nb3 =*/ nb3, 2194 }; 2195 2196 auto pipeline = ggml_metal_library_get_pipeline_solve_tri(lib, op); 2197 2198 ggml_metal_encoder_set_pipeline(enc, pipeline); 2199 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 2200 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1); 2201 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[1]), 2); 2202 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 3); 2203 2204 const int nsg = pipeline.nsg; 2205 2206 ggml_metal_encoder_set_threadgroup_memory_size(enc, pipeline.smem, 0); 2207 2208 ggml_metal_encoder_dispatch_threadgroups(enc, (ne10 + nsg - 1)/nsg, ne02, ne03, 32, nsg, 1); 2209 2210 return 1; 2211 } 2212 2213 int ggml_metal_op_set(ggml_metal_op_t ctx, int idx) { 2214 ggml_tensor * op = ctx->node(idx); 2215 2216 ggml_metal_library_t lib = ctx->lib; 2217 ggml_metal_encoder_t enc = ctx->enc; 2218 2219 GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); 2220 GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); 2221 GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne); 2222 GGML_TENSOR_LOCALS(uint64_t, nb1, op->src[1], nb); 2223 GGML_TENSOR_LOCALS( int32_t, ne, op, ne); 2224 GGML_TENSOR_LOCALS(uint64_t, nb, op, nb); 2225 2226 ggml_metal_buffer_id bid_src0 = ggml_metal_get_buffer_id(op->src[0]); 2227 ggml_metal_buffer_id bid_src1 = ggml_metal_get_buffer_id(op->src[1]); 2228 ggml_metal_buffer_id bid_dst = ggml_metal_get_buffer_id(op); 2229 2230 const size_t pnb1 = ((const int32_t *) op->op_params)[0]; 2231 const size_t pnb2 = ((const int32_t *) op->op_params)[1]; 2232 const size_t pnb3 = ((const int32_t *) op->op_params)[2]; 2233 const size_t offs = ((const int32_t *) op->op_params)[3]; 2234 2235 const bool inplace = (bool) ((const int32_t *) op->op_params)[4]; 2236 2237 if (!inplace) { 2238 // run a separate kernel to cpy src->dst 2239 // not sure how to avoid this 2240 // TODO: make a simpler cpy_bytes kernel 2241 2242 //const id pipeline = ctx->pipelines[GGML_METAL_PIPELINE_TYPE_CPY_F32_F32].obj; 2243 auto pipeline = ggml_metal_library_get_pipeline_cpy(lib, op->src[0]->type, op->type); 2244 2245 ggml_metal_kargs_cpy args = { 2246 /*.nk0 =*/ ne00, 2247 /*.ne00 =*/ ne00, 2248 /*.ne01 =*/ ne01, 2249 /*.ne02 =*/ ne02, 2250 /*.ne03 =*/ ne03, 2251 /*.nb00 =*/ nb00, 2252 /*.nb01 =*/ nb01, 2253 /*.nb02 =*/ nb02, 2254 /*.nb03 =*/ nb03, 2255 /*.ne0 =*/ ne0, 2256 /*.ne1 =*/ ne1, 2257 /*.ne2 =*/ ne2, 2258 /*.ne3 =*/ ne3, 2259 /*.nb0 =*/ nb0, 2260 /*.nb1 =*/ nb1, 2261 /*.nb2 =*/ nb2, 2262 /*.nb3 =*/ nb3, 2263 }; 2264 2265 ggml_metal_encoder_set_pipeline(enc, pipeline); 2266 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 2267 ggml_metal_encoder_set_buffer (enc, bid_src0, 1); 2268 ggml_metal_encoder_set_buffer (enc, bid_dst, 2); 2269 2270 const int nth = std::min(ggml_metal_pipeline_max_theads_per_threadgroup(pipeline), ne00); 2271 2272 ggml_metal_encoder_dispatch_threadgroups(enc, ne01, ne02, ne03, nth, 1, 1); 2273 2274 ggml_metal_op_concurrency_reset(ctx); 2275 } 2276 2277 auto pipeline = ggml_metal_library_get_pipeline_cpy(lib, op->src[1]->type, op->type); 2278 2279 GGML_ASSERT(ne10 % ggml_blck_size(op->src[1]->type) == 0); 2280 2281 int64_t nk0 = ne10; 2282 if (ggml_is_quantized(op->src[1]->type)) { 2283 nk0 = ne10/16; 2284 } else if (ggml_is_quantized(op->type)) { 2285 nk0 = ne10/ggml_blck_size(op->type); 2286 } 2287 2288 int nth = std::min(nk0*ne11, 256); 2289 2290 // when rows are small, we can batch them together in a single threadgroup 2291 int nrptg = 1; 2292 2293 // TODO: relax this constraint in the future 2294 if (ggml_blck_size(op->src[1]->type) == 1 && ggml_blck_size(op->type) == 1) { 2295 if (nth > nk0) { 2296 nrptg = (nth + nk0 - 1)/nk0; 2297 nth = nk0; 2298 2299 if (nrptg*nth > 256) { 2300 nrptg--; 2301 } 2302 } 2303 } 2304 2305 nth = std::min(nth, nk0); 2306 2307 ggml_metal_kargs_cpy args = { 2308 /*.nk0 =*/ nk0, 2309 /*.ne00 =*/ ne10, 2310 /*.ne01 =*/ ne11, 2311 /*.ne02 =*/ ne12, 2312 /*.ne03 =*/ ne13, 2313 /*.nb00 =*/ nb10, 2314 /*.nb01 =*/ nb11, 2315 /*.nb02 =*/ nb12, 2316 /*.nb03 =*/ nb13, 2317 /*.ne0 =*/ ne10, 2318 /*.ne1 =*/ ne11, 2319 /*.ne2 =*/ ne12, 2320 /*.ne3 =*/ ne13, 2321 /*.nb0 =*/ ggml_element_size(op), 2322 /*.nb1 =*/ pnb1, 2323 /*.nb2 =*/ pnb2, 2324 /*.nb3 =*/ pnb3, 2325 }; 2326 2327 const int nw0 = nrptg == 1 ? (nk0 + nth - 1)/nth : 1; 2328 2329 bid_dst.offs += offs; 2330 2331 ggml_metal_encoder_set_pipeline(enc, pipeline); 2332 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 2333 ggml_metal_encoder_set_buffer (enc, bid_src1, 1); 2334 ggml_metal_encoder_set_buffer (enc, bid_dst, 2); 2335 2336 ggml_metal_encoder_dispatch_threadgroups(enc, nw0*(ne11 + nrptg - 1)/nrptg, ne12, ne13, nth, nrptg, 1); 2337 2338 return 1; 2339 } 2340 2341 int ggml_metal_op_cpy(ggml_metal_op_t ctx, int idx) { 2342 ggml_tensor * op = ctx->node(idx); 2343 2344 ggml_metal_library_t lib = ctx->lib; 2345 ggml_metal_encoder_t enc = ctx->enc; 2346 2347 GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); 2348 GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); 2349 GGML_TENSOR_LOCALS( int32_t, ne, op, ne); 2350 GGML_TENSOR_LOCALS(uint64_t, nb, op, nb); 2351 2352 auto pipeline = ggml_metal_library_get_pipeline_cpy(lib, op->src[0]->type, op->type); 2353 2354 GGML_ASSERT(ne00 % ggml_blck_size(op->src[0]->type) == 0); 2355 2356 int64_t nk0 = ne00; 2357 if (ggml_is_quantized(op->src[0]->type)) { 2358 nk0 = ne00/16; 2359 } else if (ggml_is_quantized(op->type)) { 2360 nk0 = ne00/ggml_blck_size(op->type); 2361 } 2362 2363 int nth = std::min(nk0*ne01, 256); 2364 2365 // when rows are small, we can batch them together in a single threadgroup 2366 int nrptg = 1; 2367 2368 // TODO: relax this constraint in the future 2369 if (ggml_blck_size(op->src[0]->type) == 1 && ggml_blck_size(op->type) == 1) { 2370 if (nth > nk0) { 2371 nrptg = (nth + nk0 - 1)/nk0; 2372 nth = nk0; 2373 2374 if (nrptg*nth > 256) { 2375 nrptg--; 2376 } 2377 } 2378 } 2379 2380 nth = std::min(nth, nk0); 2381 2382 ggml_metal_kargs_cpy args = { 2383 /*.nk0 =*/ nk0, 2384 /*.ne00 =*/ ne00, 2385 /*.ne01 =*/ ne01, 2386 /*.ne02 =*/ ne02, 2387 /*.ne03 =*/ ne03, 2388 /*.nb00 =*/ nb00, 2389 /*.nb01 =*/ nb01, 2390 /*.nb02 =*/ nb02, 2391 /*.nb03 =*/ nb03, 2392 /*.ne0 =*/ ne0, 2393 /*.ne1 =*/ ne1, 2394 /*.ne2 =*/ ne2, 2395 /*.ne3 =*/ ne3, 2396 /*.nb0 =*/ nb0, 2397 /*.nb1 =*/ nb1, 2398 /*.nb2 =*/ nb2, 2399 /*.nb3 =*/ nb3, 2400 }; 2401 2402 const int nw0 = nrptg == 1 ? (nk0 + nth - 1)/nth : 1; 2403 2404 ggml_metal_encoder_set_pipeline(enc, pipeline); 2405 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 2406 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1); 2407 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 2); 2408 2409 ggml_metal_encoder_dispatch_threadgroups(enc, nw0*(ne01 + nrptg - 1)/nrptg, ne02, ne03, nth, nrptg, 1); 2410 2411 return 1; 2412 } 2413 2414 int ggml_metal_op_pool_1d(ggml_metal_op_t ctx, int idx) { 2415 ggml_tensor * op = ctx->node(idx); 2416 2417 ggml_metal_library_t lib = ctx->lib; 2418 ggml_metal_encoder_t enc = ctx->enc; 2419 2420 GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); 2421 GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); 2422 GGML_TENSOR_LOCALS( int32_t, ne, op, ne); 2423 GGML_TENSOR_LOCALS(uint64_t, nb, op, nb); 2424 2425 const int32_t * opts = op->op_params; 2426 ggml_op_pool op_pool = (ggml_op_pool) opts[0]; 2427 2428 const int32_t k0 = opts[1]; 2429 const int32_t s0 = opts[2]; 2430 const int32_t p0 = opts[3]; 2431 2432 const int64_t IW = op->src[0]->ne[0]; 2433 const int64_t OW = op->ne[0]; 2434 2435 const int64_t np = ggml_nelements(op); 2436 2437 ggml_metal_kargs_pool_1d args_pool_1d = { 2438 /* .k0 = */ k0, 2439 /* .s0 = */ s0, 2440 /* .p0 = */ p0, 2441 /* .IW = */ IW, 2442 /* .OW = */ OW, 2443 /* .np = */ np 2444 }; 2445 2446 auto pipeline = ggml_metal_library_get_pipeline_pool_1d(lib, op, op_pool); 2447 2448 const int nth = std::min(256, std::max(1, k0)); 2449 const int ntg = (int) np; 2450 2451 const size_t smem = op_pool == GGML_OP_POOL_AVG ? 2452 2*32*sizeof(float) : 2453 32*sizeof(float); 2454 2455 ggml_metal_encoder_set_pipeline(enc, pipeline); 2456 ggml_metal_encoder_set_bytes (enc, &args_pool_1d, sizeof(args_pool_1d), 0); 2457 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1); 2458 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 2); 2459 ggml_metal_encoder_set_threadgroup_memory_size(enc, smem, 0); 2460 2461 ggml_metal_encoder_dispatch_threadgroups(enc, ntg, 1, 1, nth, 1, 1); 2462 2463 return 1; 2464 } 2465 2466 int ggml_metal_op_fwht(ggml_metal_op_t ctx, int idx) { 2467 ggml_tensor * op = ctx->node(idx); 2468 2469 ggml_metal_library_t lib = ctx->lib; 2470 ggml_metal_encoder_t enc = ctx->enc; 2471 2472 ggml_tensor * src1 = op->src[1]; 2473 2474 const int64_t n = src1->ne[0]; 2475 const int64_t nrows = ggml_nrows(src1); 2476 2477 ggml_metal_kargs_fwht args = { 2478 /*.nrows = */ (int32_t) nrows, 2479 }; 2480 2481 auto pipeline = ggml_metal_library_get_pipeline_fwht(lib, n, src1->type); 2482 2483 ggml_metal_encoder_set_pipeline(enc, pipeline); 2484 ggml_metal_encoder_set_bytes(enc, &args, sizeof(args), 0); 2485 ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(src1), 1); 2486 ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(op), 2); 2487 2488 const int th_max = ggml_metal_pipeline_max_theads_per_threadgroup(pipeline); 2489 const int simd_size = 32; 2490 2491 if (n >= GGML_METAL_FWHT_TG_MIN_N) { 2492 GGML_ASSERT(th_max >= GGML_METAL_FWHT_TG_NT); 2493 ggml_metal_encoder_dispatch_threadgroups(enc, nrows, 1, 1, GGML_METAL_FWHT_TG_NT, 1, 1); 2494 2495 return 1; 2496 } 2497 2498 int sg_per_tg = 2; 2499 sg_per_tg = std::min(sg_per_tg, th_max/simd_size); 2500 sg_per_tg = std::max(sg_per_tg, 1); 2501 2502 const int64_t n_tg = (nrows + sg_per_tg - 1) / sg_per_tg; 2503 ggml_metal_encoder_dispatch_threadgroups(enc, n_tg, 1, 1, 32*sg_per_tg, 1, 1); 2504 2505 return 1; 2506 } 2507 2508 int ggml_metal_op_pool_2d(ggml_metal_op_t ctx, int idx) { 2509 ggml_tensor * op = ctx->node(idx); 2510 2511 ggml_metal_library_t lib = ctx->lib; 2512 ggml_metal_encoder_t enc = ctx->enc; 2513 2514 GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); 2515 GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); 2516 GGML_TENSOR_LOCALS( int32_t, ne, op, ne); 2517 GGML_TENSOR_LOCALS(uint64_t, nb, op, nb); 2518 2519 const int32_t * opts = op->op_params; 2520 ggml_op_pool op_pool = (ggml_op_pool) opts[0]; 2521 2522 const int32_t k0 = opts[1]; 2523 const int32_t k1 = opts[2]; 2524 const int32_t s0 = opts[3]; 2525 const int32_t s1 = opts[4]; 2526 const int32_t p0 = opts[5]; 2527 const int32_t p1 = opts[6]; 2528 2529 const int64_t IH = op->src[0]->ne[1]; 2530 const int64_t IW = op->src[0]->ne[0]; 2531 2532 const int64_t N = op->ne[3]; 2533 const int64_t OC = op->ne[2]; 2534 const int64_t OH = op->ne[1]; 2535 const int64_t OW = op->ne[0]; 2536 2537 const int64_t np = N * OC * OH * OW; 2538 2539 ggml_metal_kargs_pool_2d args_pool_2d = { 2540 /* .k0 = */ k0, 2541 /* .k1 = */ k1, 2542 /* .s0 = */ s0, 2543 /* .s1 = */ s1, 2544 /* .p0 = */ p0, 2545 /* .p1 = */ p1, 2546 /* .IH = */ IH, 2547 /* .IW = */ IW, 2548 /* .OH = */ OH, 2549 /* .OW = */ OW, 2550 /* .np = */ np 2551 }; 2552 2553 auto pipeline = ggml_metal_library_get_pipeline_pool_2d(lib, op, op_pool); 2554 2555 const int nth = std::min(ggml_metal_pipeline_max_theads_per_threadgroup(pipeline), (int) np); 2556 const int ntg = (np + nth - 1) / nth; 2557 2558 ggml_metal_encoder_set_pipeline(enc, pipeline); 2559 ggml_metal_encoder_set_bytes (enc, &args_pool_2d, sizeof(args_pool_2d), 0); 2560 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1); 2561 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 2); 2562 2563 ggml_metal_encoder_dispatch_threadgroups(enc, ntg, 1, 1, nth, 1, 1); 2564 2565 return 1; 2566 } 2567 2568 // the number of nodes from idx on that the fusion table fuses as pattern id into one dispatch, 1 if it fuses none 2569 static int ggml_metal_op_try_fusion(ggml_metal_op_t ctx, int idx, ggml_metal_fusion_id id) { 2570 if (!ctx->use_fusion()) { 2571 return 1; 2572 } 2573 2574 int n = 1; 2575 const ggml_metal_fusion * fusion = ctx->can_fuse(idx, GGML_METAL_FUSION_FULL, &n); 2576 if (fusion == nullptr || ggml_metal_fusion_get_id(fusion) != id) { 2577 return 1; 2578 } 2579 2580 ctx->count_fusions(fusion); 2581 2582 if (ggml_metal_fusion_info_debug(ctx->finfo) > 1) { 2583 GGML_LOG_DEBUG("%s: fuse: %s to %s, %d nodes\n", __func__, ggml_op_name(ctx->node(idx)->op), ggml_op_name(ctx->node(idx + n - 1)->op), n); 2584 } 2585 2586 return n; 2587 } 2588 2589 static int ggml_metal_op_mul_mat_mma(ggml_metal_op_t ctx, int idx) { 2590 ggml_tensor * op = ctx->node(idx); 2591 2592 ggml_metal_library_t lib = ctx->lib; 2593 ggml_metal_encoder_t enc = ctx->enc; 2594 2595 GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); 2596 GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); 2597 GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne); 2598 GGML_TENSOR_LOCALS(uint64_t, nb1, op->src[1], nb); 2599 GGML_TENSOR_LOCALS( int32_t, ne, op, ne); 2600 2601 // the MMA store adds the residual when the table fuses the ADD after this mat-mul 2602 const int n_fuse = ggml_metal_op_try_fusion(ctx, idx, GGML_METAL_FUSION_MUL_MAT_ADD); 2603 const bool fuse_add = n_fuse > 1; 2604 2605 const ggml_tensor * dst = op; 2606 const ggml_tensor * res = dst; 2607 2608 if (fuse_add) { 2609 dst = ctx->node(idx + n_fuse - 1); 2610 // the residual is the other operand of the ADD, by identity: it can itself be a MUL_MAT output 2611 res = dst->src[0] == op ? dst->src[1] : dst->src[0]; 2612 } 2613 2614 auto pipeline = ggml_metal_library_get_pipeline_mul_mv_mma_auto(lib, op, fuse_add); 2615 2616 ggml_metal_kargs_mul_mv_ext args = { 2617 /*.ne00 =*/ ne00, 2618 /*.ne01 =*/ ne01, 2619 /*.ne02 =*/ ne02, 2620 /*.nb00 =*/ nb00, 2621 /*.nb01 =*/ nb01, 2622 /*.nb02 =*/ nb02, 2623 /*.nb03 =*/ nb03, 2624 /*.ne10 =*/ ne10, 2625 /*.ne11 =*/ ne11, 2626 /*.ne12 =*/ ne12, 2627 /*.nb10 =*/ nb10, 2628 /*.nb11 =*/ nb11, 2629 /*.nb12 =*/ nb12, 2630 /*.nb13 =*/ nb13, 2631 /*.ne0 =*/ ne0, 2632 /*.ne1 =*/ ne1, 2633 /*.r2 =*/ (int16_t) (ne12/ne02), 2634 /*.r3 =*/ (int16_t) (ne13/ne03), 2635 }; 2636 2637 ggml_metal_encoder_set_pipeline(enc, pipeline); 2638 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 2639 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1); 2640 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[1]), 2); 2641 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(dst), 3); 2642 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(res), 4); 2643 2644 ggml_metal_encoder_set_threadgroup_memory_size(enc, pipeline.smem, 0); 2645 2646 const int rows0 = pipeline.nr0; 2647 const int rows1 = pipeline.nr1; 2648 2649 ggml_metal_encoder_dispatch_threadgroups(enc, (ne01 + rows0 - 1)/rows0, (ne11 + rows1 - 1)/rows1, ne12*ne13, 32, pipeline.nsg, 1); 2650 2651 return n_fuse; 2652 } 2653 2654 // the generic mat-vec kernel, or its 2-row Q4_0 variant if nc 2655 static int ggml_metal_op_mul_mat_mv(ggml_metal_op_t ctx, int idx) { 2656 ggml_tensor * op = ctx->node(idx); 2657 2658 ggml_metal_library_t lib = ctx->lib; 2659 ggml_metal_encoder_t enc = ctx->enc; 2660 2661 GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); 2662 GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); 2663 GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne); 2664 GGML_TENSOR_LOCALS(uint64_t, nb1, op->src[1], nb); 2665 GGML_TENSOR_LOCALS( int32_t, ne, op, ne); 2666 2667 const int16_t r2 = ne12/ne02; 2668 const int16_t r3 = ne13/ne03; 2669 2670 auto pipeline = ggml_metal_library_get_pipeline_mul_mv(lib, op); 2671 2672 const int nr0 = pipeline.nr0; 2673 const int nr1 = pipeline.nr1; 2674 const int nsg = pipeline.nsg; 2675 2676 const size_t smem = pipeline.smem; 2677 2678 ggml_metal_kargs_mul_mv args = { 2679 /*.ne00 =*/ ne00, 2680 /*.ne01 =*/ ne01, 2681 /*.ne02 =*/ ne02, 2682 /*.nb00 =*/ nb00, 2683 /*.nb01 =*/ nb01, 2684 /*.nb02 =*/ nb02, 2685 /*.nb03 =*/ nb03, 2686 /*.ne10 =*/ ne10, 2687 /*.ne11 =*/ ne11, 2688 /*.ne12 =*/ ne12, 2689 /*.nb10 =*/ nb10, 2690 /*.nb11 =*/ nb11, 2691 /*.nb12 =*/ nb12, 2692 /*.nb13 =*/ nb13, 2693 /*.ne0 =*/ ne0, 2694 /*.ne1 =*/ ne1, 2695 /*.nr0 =*/ nr0, 2696 /*.r2 =*/ r2, 2697 /*.r3 =*/ r3, 2698 }; 2699 2700 ggml_metal_encoder_set_pipeline(enc, pipeline); 2701 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 2702 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1); 2703 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[1]), 2); 2704 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 3); 2705 2706 ggml_metal_encoder_set_threadgroup_memory_size(enc, smem, 0); 2707 2708 if (op->src[0]->type == GGML_TYPE_F32 || 2709 op->src[0]->type == GGML_TYPE_F16 || 2710 op->src[0]->type == GGML_TYPE_BF16 || 2711 op->src[0]->type == GGML_TYPE_Q8_0) { 2712 ggml_metal_encoder_dispatch_threadgroups(enc, ((ne01 + nr0 - 1)/(nr0)), ((ne11 + nr1 - 1)/nr1), ne12*ne13, 32, nsg, 1); 2713 } else { 2714 ggml_metal_encoder_dispatch_threadgroups(enc, ((ne01 + nr0*nsg - 1)/(nr0*nsg)), ((ne11 + nr1 - 1)/nr1), ne12*ne13, 32, nsg, 1); 2715 } 2716 2717 return 1; 2718 } 2719 2720 int ggml_metal_op_mul_mat(ggml_metal_op_t ctx, int idx) { 2721 ggml_tensor * op = ctx->node(idx); 2722 2723 ggml_metal_library_t lib = ctx->lib; 2724 ggml_metal_encoder_t enc = ctx->enc; 2725 2726 const ggml_metal_device_props * props_dev = ggml_metal_device_get_props(ctx->dev); 2727 2728 if (ggml_metal_op_mul_mat_use_fwht(op, props_dev->max_theadgroup_memory_size)) { 2729 return ggml_metal_op_fwht(ctx, idx); 2730 } 2731 2732 if (props_dev->has_simdgroup_mm && ggml_metal_op_mul_mat_use_mma(op)) { 2733 return ggml_metal_op_mul_mat_mma(ctx, idx); 2734 } 2735 2736 GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); 2737 GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); 2738 GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne); 2739 GGML_TENSOR_LOCALS(uint64_t, nb1, op->src[1], nb); 2740 GGML_TENSOR_LOCALS( int32_t, ne, op, ne); 2741 GGML_TENSOR_LOCALS(uint64_t, nb, op, nb); 2742 2743 GGML_ASSERT(ne00 == ne10); 2744 2745 GGML_ASSERT(ne12 % ne02 == 0); 2746 GGML_ASSERT(ne13 % ne03 == 0); 2747 2748 const int16_t r2 = ne12/ne02; 2749 const int16_t r3 = ne13/ne03; 2750 2751 // first try to use small-batch mat-mv kernels 2752 // these should be efficient for BS [2, ~8] 2753 if (op->src[1]->type == GGML_TYPE_F32 && (ne00%128 == 0) && 2754 ( 2755 ( 2756 ( 2757 op->src[0]->type == GGML_TYPE_F32 || // TODO: helper function 2758 op->src[0]->type == GGML_TYPE_F16 || 2759 op->src[0]->type == GGML_TYPE_BF16 || 2760 op->src[0]->type == GGML_TYPE_Q1_0 || 2761 op->src[0]->type == GGML_TYPE_Q2_0 || 2762 op->src[0]->type == GGML_TYPE_Q4_0 || 2763 op->src[0]->type == GGML_TYPE_Q4_1 || 2764 op->src[0]->type == GGML_TYPE_Q5_0 || 2765 op->src[0]->type == GGML_TYPE_Q5_1 || 2766 op->src[0]->type == GGML_TYPE_Q8_0 || 2767 op->src[0]->type == GGML_TYPE_MXFP4 || 2768 op->src[0]->type == GGML_TYPE_IQ4_NL || 2769 false) && (ne11 >= 2 && ne11 <= 8) 2770 ) || 2771 ( 2772 ( 2773 op->src[0]->type == GGML_TYPE_Q4_K || 2774 op->src[0]->type == GGML_TYPE_Q5_K || 2775 op->src[0]->type == GGML_TYPE_Q6_K || 2776 op->src[0]->type == GGML_TYPE_Q2_K || 2777 op->src[0]->type == GGML_TYPE_Q3_K || 2778 false) && (ne11 >= 4 && ne11 <= 8) 2779 ) 2780 ) 2781 ) { 2782 // TODO: determine the optimal parameters based on grid utilization 2783 // I still don't know why we should not always use the maximum available threads: 2784 // 2785 // nsg = pipeline.maxTotalThreadsPerThreadgroup / 32 2786 // 2787 // my current hypothesis is that the work grid is not evenly divisible for different nsg 2788 // values and there can be some tail effects when nsg is high. need to confirm this 2789 // 2790 const int nsg = 2; // num simdgroups per threadgroup 2791 2792 // num threads along row per simdgroup 2793 int16_t nxpsg = 0; 2794 if (ne00 % 256 == 0 && ne11 < 3) { 2795 nxpsg = 16; 2796 } else if (ne00 % 128 == 0) { 2797 nxpsg = 8; 2798 } else { 2799 nxpsg = 4; 2800 } 2801 2802 const int16_t nypsg = 32/nxpsg; // num threads along col per simdgroup (i.e. a simdgroup processes that many src0 rows at a time) 2803 const int16_t r0ptg = nypsg*nsg; // num src0 rows per threadgroup 2804 int16_t r1ptg = 4; // num src1 rows per threadgroup 2805 2806 // note: not sure how optimal are those across all different hardware. there might be something cleverer 2807 switch (ne11) { 2808 case 2: 2809 r1ptg = 2; break; 2810 case 3: 2811 case 6: 2812 r1ptg = 3; break; 2813 case 4: 2814 case 7: 2815 case 8: 2816 r1ptg = 4; break; 2817 case 5: 2818 r1ptg = 5; break; 2819 default: 2820 GGML_ABORT("unsupported ne11"); 2821 }; 2822 2823 auto pipeline = ggml_metal_library_get_pipeline_mul_mv_ext(lib, op, nsg, nxpsg, r1ptg); 2824 2825 ggml_metal_kargs_mul_mv_ext args = { 2826 /*.ne00 =*/ ne00, 2827 /*.ne01 =*/ ne01, 2828 /*.ne02 =*/ ne02, 2829 /*.nb00 =*/ nb00, 2830 /*.nb01 =*/ nb01, 2831 /*.nb02 =*/ nb02, 2832 /*.nb03 =*/ nb03, 2833 /*.ne10 =*/ ne10, 2834 /*.ne11 =*/ ne11, 2835 /*.ne12 =*/ ne12, 2836 /*.nb10 =*/ nb10, 2837 /*.nb11 =*/ nb11, 2838 /*.nb12 =*/ nb12, 2839 /*.nb13 =*/ nb13, 2840 /*.ne0 =*/ ne0, 2841 /*.ne1 =*/ ne1, 2842 /*.r2 =*/ r2, 2843 /*.r3 =*/ r3, 2844 }; 2845 2846 ggml_metal_encoder_set_pipeline(enc, pipeline); 2847 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 2848 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1); 2849 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[1]), 2); 2850 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 3); 2851 2852 ggml_metal_encoder_dispatch_threadgroups(enc, ((ne01 + r0ptg - 1)/r0ptg), ((ne11 + r1ptg - 1)/r1ptg), ne12*ne13, 32, nsg, 1); 2853 } else if (ggml_metal_op_mul_mat_use_mm(op, props_dev->has_simdgroup_mm)) { 2854 //GGML_LOG_INFO("matrix: ne00 = %6d, ne01 = %6d, ne02 = %6d, ne11 = %6d, ne12 = %6d\n", ne00, ne01, ne02, ne11, ne12); 2855 2856 // some Metal matrix data types require aligned pointers 2857 // ref: https://developer.apple.com/metal/Metal-Shading-Language-Specification.pdf (Table 2.5) 2858 //switch (op->src[0]->type) { 2859 // case GGML_TYPE_F32: GGML_ASSERT(nb01 % 16 == 0); break; 2860 // case GGML_TYPE_F16: GGML_ASSERT(nb01 % 8 == 0); break; 2861 // case GGML_TYPE_BF16: GGML_ASSERT(nb01 % 8 == 0); break; 2862 // default: break; 2863 //} 2864 2865 auto pipeline = ggml_metal_library_get_pipeline_mul_mm(lib, op); 2866 2867 ggml_metal_kargs_mul_mm args = { 2868 /*.ne00 =*/ ne00, 2869 /*.ne02 =*/ ne02, 2870 /*.nb01 =*/ nb01, 2871 /*.nb02 =*/ nb02, 2872 /*.nb03 =*/ nb03, 2873 /*.ne12 =*/ ne12, 2874 /*.nb10 =*/ nb10, 2875 /*.nb11 =*/ nb11, 2876 /*.nb12 =*/ nb12, 2877 /*.nb13 =*/ nb13, 2878 /*.ne0 =*/ ne0, 2879 /*.ne1 =*/ ne1, 2880 /*.r2 =*/ r2, 2881 /*.r3 =*/ r3, 2882 }; 2883 2884 ggml_metal_encoder_set_pipeline(enc, pipeline); 2885 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 2886 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1); 2887 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[1]), 2); 2888 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 3); 2889 2890 const size_t smem = pipeline.smem; 2891 2892 ggml_metal_encoder_set_threadgroup_memory_size(enc, smem, 0); 2893 2894 const int nr0 = pipeline.nr0; 2895 const int nr1 = pipeline.nr1; 2896 const int nsg = pipeline.nsg; 2897 2898 ggml_metal_encoder_dispatch_threadgroups(enc, ((ne11 + nr1 - 1) / nr1), ((ne01 + nr0 - 1) / nr0), ne12 * ne13, 32, nsg, 1); 2899 } else { 2900 return ggml_metal_op_mul_mat_mv(ctx, idx); 2901 } 2902 2903 return 1; 2904 } 2905 2906 // upper bound on the number of entry tiles emitted by kernel_mul_mm_id_map0: the full tiles 2907 // over all entries plus at most one partial tile per expert 2908 static int64_t ggml_metal_op_mul_mat_id_max_tiles(const ggml_tensor * op) { 2909 const int64_t ne02 = op->src[0]->ne[2]; // n_expert 2910 const int64_t ne20 = op->src[2]->ne[0]; // n_expert_used 2911 const int64_t ne21 = op->src[2]->ne[1]; // n_token 2912 2913 const int64_t n_rows = ne20*ne21; 2914 const int64_t tiles = (n_rows + 31)/32 + (ne02 < n_rows ? ne02 : n_rows) - 1; 2915 2916 return n_rows < tiles ? n_rows : tiles; 2917 } 2918 2919 size_t ggml_metal_op_mul_mat_id_extra_tpe(const ggml_tensor * op) { 2920 assert(op->op == GGML_OP_MUL_MAT_ID); 2921 2922 // row map of kernel_mul_mm_id: total tile count + one 2923 // (expert, first entry, valid rows) descriptor per tile 2924 const int64_t max_tiles = ggml_metal_op_mul_mat_id_max_tiles(op); 2925 2926 return ggml_type_size(GGML_TYPE_I32)*(1 + 3*max_tiles); 2927 } 2928 2929 size_t ggml_metal_op_mul_mat_id_extra_ids(const ggml_tensor * op) { 2930 assert(op->op == GGML_OP_MUL_MAT_ID); 2931 2932 const int64_t ne02 = op->src[0]->ne[2]; // n_expert 2933 const int64_t ne20 = op->src[2]->ne[0]; // n_expert_used 2934 const int64_t ne21 = op->src[2]->ne[1]; // n_token 2935 2936 // one entry per (expert used, token) pair of ids plus one padding slot per expert 2937 // (see kernel_mul_mm_id_map0) 2938 return ggml_type_size(GGML_TYPE_I32)*(ne20*ne21 + ne02); 2939 } 2940 2941 int ggml_metal_op_mul_mat_id(ggml_metal_op_t ctx, int idx) { 2942 ggml_tensor * op = ctx->node(idx); 2943 2944 ggml_metal_library_t lib = ctx->lib; 2945 ggml_metal_encoder_t enc = ctx->enc; 2946 2947 const ggml_metal_device_props * props_dev = ggml_metal_device_get_props(ctx->dev); 2948 2949 GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); 2950 GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); 2951 GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne); 2952 GGML_TENSOR_LOCALS(uint64_t, nb1, op->src[1], nb); 2953 GGML_TENSOR_LOCALS( int32_t, ne2, op->src[2], ne); 2954 GGML_TENSOR_LOCALS(uint64_t, nb2, op->src[2], nb); 2955 GGML_TENSOR_LOCALS( int32_t, ne, op, ne); 2956 GGML_TENSOR_LOCALS(uint64_t, nb, op, nb); 2957 2958 // src2 = ids 2959 GGML_ASSERT(op->src[2]->type == GGML_TYPE_I32); 2960 2961 GGML_ASSERT(!ggml_is_transposed(op->src[0])); 2962 GGML_ASSERT(!ggml_is_transposed(op->src[1])); 2963 2964 GGML_ASSERT(ne03 == 1); 2965 GGML_ASSERT(ne13 == 1); 2966 2967 ggml_metal_buffer_id bid_src0 = ggml_metal_get_buffer_id(op->src[0]); 2968 ggml_metal_buffer_id bid_src1 = ggml_metal_get_buffer_id(op->src[1]); 2969 ggml_metal_buffer_id bid_src2 = ggml_metal_get_buffer_id(op->src[2]); 2970 ggml_metal_buffer_id bid_dst = ggml_metal_get_buffer_id(op); 2971 2972 const uint32_t r2 = 1; 2973 const uint32_t r3 = 1; 2974 2975 if (ggml_metal_op_mul_mat_id_use_mm(op, props_dev->has_simdgroup_mm)) { 2976 // some Metal matrix data types require aligned pointers 2977 // ref: https://developer.apple.com/metal/Metal-Shading-Language-Specification.pdf (Table 2.5) 2978 //switch (op->src[0]->type) { 2979 // case GGML_TYPE_F32: GGML_ASSERT(nb01 % 16 == 0); break; 2980 // case GGML_TYPE_F16: GGML_ASSERT(nb01 % 8 == 0); break; 2981 // case GGML_TYPE_BF16: GGML_ASSERT(nb01 % 8 == 0); break; 2982 // default: break; 2983 //} 2984 2985 // extra buffers for intermediate id mapping 2986 ggml_metal_buffer_id bid_tpe = bid_dst; 2987 bid_tpe.offs += ggml_nbytes(op); 2988 2989 ggml_metal_buffer_id bid_ids = bid_tpe; 2990 bid_ids.offs += ggml_metal_op_mul_mat_id_extra_tpe(op); 2991 2992 { 2993 ggml_metal_kargs_mul_mm_id_map0 args = { 2994 ne10, 2995 ne11, // n_expert_used (bcast) 2996 nb11, 2997 nb12, 2998 ne21, // n_tokens 2999 ne20, // n_expert_used 3000 nb21, 3001 }; 3002 3003 auto pipeline = ggml_metal_library_get_pipeline_mul_mm_id_map0(lib, ne02, ne20); 3004 3005 const size_t smem = pipeline.smem; 3006 3007 GGML_ASSERT(ne02 <= ggml_metal_pipeline_max_theads_per_threadgroup(pipeline)); 3008 3009 GGML_ASSERT(smem <= props_dev->max_theadgroup_memory_size); 3010 3011 ggml_metal_encoder_set_pipeline(enc, pipeline); 3012 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 3013 ggml_metal_encoder_set_buffer (enc, bid_src2, 1); 3014 ggml_metal_encoder_set_buffer (enc, bid_tpe, 2); 3015 ggml_metal_encoder_set_buffer (enc, bid_ids, 3); 3016 3017 ggml_metal_encoder_set_threadgroup_memory_size(enc, smem, 0); 3018 3019 ggml_metal_encoder_dispatch_threadgroups(enc, 1, 1, 1, ne02, 1, 1); 3020 } 3021 3022 // zero the rows that no expert owns - disjoint from what kernel_mul_mm_id writes 3023 { 3024 ggml_metal_kargs_mul_mm_id_zero args = { 3025 /*.ne0 =*/ ne0, 3026 /*.nb1 =*/ nb1, 3027 /*.nb2 =*/ nb2, 3028 /*.nb21 =*/ nb21, 3029 }; 3030 3031 auto pipeline = ggml_metal_library_get_pipeline_mul_mm_id_zero(lib); 3032 3033 ggml_metal_encoder_set_pipeline(enc, pipeline); 3034 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 3035 ggml_metal_encoder_set_buffer (enc, bid_src2, 1); 3036 ggml_metal_encoder_set_buffer (enc, bid_dst, 2); 3037 3038 ggml_metal_encoder_dispatch_threadgroups(enc, ne20, ne21, 1, 32, 1, 1); 3039 } 3040 3041 // this barrier is always needed because the next kernel has to wait for the id maps to be computed 3042 ggml_metal_op_concurrency_reset(ctx); 3043 3044 { 3045 auto pipeline = ggml_metal_library_get_pipeline_mul_mm_id(lib, op); 3046 3047 ggml_metal_kargs_mul_mm_id args = { 3048 /*.ne00 =*/ ne00, 3049 /*.ne02 =*/ ne02, 3050 /*.nb01 =*/ nb01, 3051 /*.nb02 =*/ nb02, 3052 /*.nb03 =*/ nb03, 3053 /*.ne11 =*/ ne11, // n_expert_used (bcast) 3054 /*.nb10 =*/ nb10, 3055 /*.nb11 =*/ nb11, 3056 /*.nb12 =*/ nb12, 3057 /*.nb13 =*/ nb13, 3058 /*.ne20 =*/ ne20, // n_expert_used 3059 /*.ne0 =*/ ne0, 3060 /*.ne1 =*/ ne1, 3061 /*.r2 =*/ r2, 3062 /*.r3 =*/ r3, 3063 }; 3064 3065 ggml_metal_encoder_set_pipeline(enc, pipeline); 3066 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 3067 ggml_metal_encoder_set_buffer (enc, bid_src0, 1); 3068 ggml_metal_encoder_set_buffer (enc, bid_src1, 2); 3069 ggml_metal_encoder_set_buffer (enc, bid_tpe, 3); 3070 ggml_metal_encoder_set_buffer (enc, bid_ids, 4); 3071 ggml_metal_encoder_set_buffer (enc, bid_dst, 5); 3072 3073 const size_t smem = pipeline.smem; 3074 3075 GGML_ASSERT(smem <= props_dev->max_theadgroup_memory_size); 3076 3077 ggml_metal_encoder_set_threadgroup_memory_size(enc, smem, 0); 3078 3079 const int max_tiles = (int) ggml_metal_op_mul_mat_id_max_tiles(op); 3080 3081 ggml_metal_encoder_dispatch_threadgroups(enc, max_tiles, (ne01 + 63)/64, 1, 128, 1, 1); 3082 } 3083 } else { 3084 auto pipeline = ggml_metal_library_get_pipeline_mul_mv_id(lib, op); 3085 3086 const int nr0 = pipeline.nr0; 3087 const int nr1 = pipeline.nr1; 3088 const int nsg = pipeline.nsg; 3089 3090 const size_t smem = pipeline.smem; 3091 3092 ggml_metal_kargs_mul_mv_id args = { 3093 /*.nei0 =*/ ne20, 3094 /*.nei1 =*/ ne21, 3095 /*.nbi1 =*/ nb21, 3096 /*.ne00 =*/ ne00, 3097 /*.ne01 =*/ ne01, 3098 /*.ne02 =*/ ne02, 3099 /*.nb00 =*/ nb00, 3100 /*.nb01 =*/ nb01, 3101 /*.nb02 =*/ nb02, 3102 /*.ne10 =*/ ne10, 3103 /*.ne11 =*/ ne11, 3104 /*.ne12 =*/ ne12, 3105 /*.ne13 =*/ ne13, 3106 /*.nb10 =*/ nb10, 3107 /*.nb11 =*/ nb11, 3108 /*.nb12 =*/ nb12, 3109 /*.ne0 =*/ ne0, 3110 /*.ne1 =*/ ne1, 3111 /*.nb1 =*/ nb1, 3112 /*.nr0 =*/ nr0, 3113 }; 3114 3115 if (ggml_is_quantized(op->src[0]->type)) { 3116 GGML_ASSERT(ne00 >= nsg*nr0); 3117 } 3118 3119 ggml_metal_encoder_set_pipeline(enc, pipeline); 3120 ggml_metal_encoder_set_bytes(enc, &args, sizeof(args), 0); 3121 ggml_metal_encoder_set_buffer(enc, bid_src0, 1); 3122 ggml_metal_encoder_set_buffer(enc, bid_src1, 2); 3123 ggml_metal_encoder_set_buffer(enc, bid_dst, 3); 3124 ggml_metal_encoder_set_buffer(enc, bid_src2, 4); 3125 3126 const int64_t _ne1 = 1; 3127 const int64_t ne123 = ne20*ne21; 3128 3129 ggml_metal_encoder_set_threadgroup_memory_size(enc, smem, 0); 3130 3131 if (op->src[0]->type == GGML_TYPE_F32 || 3132 op->src[0]->type == GGML_TYPE_F16 || 3133 op->src[0]->type == GGML_TYPE_BF16 || 3134 op->src[0]->type == GGML_TYPE_Q8_0) { 3135 ggml_metal_encoder_dispatch_threadgroups(enc, (ne01 + nr0 - 1)/(nr0), (_ne1 + nr1 - 1)/nr1, ne123, 32, nsg, 1); 3136 } else { 3137 ggml_metal_encoder_dispatch_threadgroups(enc, (ne01 + nr0*nsg - 1)/(nr0*nsg), (_ne1 + nr1 - 1)/nr1, ne123, 32, nsg, 1); 3138 } 3139 } 3140 3141 return 1; 3142 } 3143 3144 int ggml_metal_op_add_id(ggml_metal_op_t ctx, int idx) { 3145 ggml_tensor * op = ctx->node(idx); 3146 3147 ggml_metal_library_t lib = ctx->lib; 3148 ggml_metal_encoder_t enc = ctx->enc; 3149 3150 GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); 3151 GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); 3152 GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne); 3153 GGML_TENSOR_LOCALS(uint64_t, nb1, op->src[1], nb); 3154 GGML_TENSOR_LOCALS( int32_t, ne2, op->src[2], ne); 3155 GGML_TENSOR_LOCALS(uint64_t, nb2, op->src[2], nb); 3156 GGML_TENSOR_LOCALS( int32_t, ne, op, ne); 3157 3158 GGML_ASSERT(op->src[0]->type == GGML_TYPE_F32); 3159 GGML_ASSERT(op->src[1]->type == GGML_TYPE_F32); 3160 GGML_ASSERT(op->src[2]->type == GGML_TYPE_I32); 3161 GGML_ASSERT(op->type == GGML_TYPE_F32); 3162 3163 GGML_ASSERT(ggml_is_contiguous_rows(op->src[0])); 3164 3165 ggml_metal_kargs_add_id args = { 3166 /*.ne0 =*/ ne0, 3167 /*.ne1 =*/ ne1, 3168 /*.nb01 =*/ nb01, 3169 /*.nb02 =*/ nb02, 3170 /*.nb11 =*/ nb11, 3171 /*.nb21 =*/ nb21, 3172 }; 3173 3174 auto pipeline = ggml_metal_library_get_pipeline_base(lib, GGML_OP_ADD_ID); 3175 3176 ggml_metal_encoder_set_pipeline(enc, pipeline); 3177 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 3178 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1); 3179 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[1]), 2); 3180 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[2]), 3); 3181 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 4); 3182 3183 const int nth = std::min(ggml_metal_pipeline_max_theads_per_threadgroup(pipeline), ne00); 3184 3185 ggml_metal_encoder_dispatch_threadgroups(enc, ne01, ne02, 1, nth, 1, 1); 3186 3187 return 1; 3188 } 3189 3190 bool ggml_metal_op_flash_attn_ext_use_vec(const ggml_tensor * op) { 3191 assert(op->op == GGML_OP_FLASH_ATTN_EXT); 3192 3193 const int64_t ne00 = op->src[0]->ne[0]; // head size 3194 const int64_t ne01 = op->src[0]->ne[1]; // batch size 3195 3196 // use vec kernel if the batch size is small and if the head size is supported 3197 return (ne01 < 20) && (ne00 % 32 == 0); 3198 } 3199 3200 // ref: https://github.com/ggml-org/llama.cpp/pull/27390 3201 // dequantize the quantized KV cache to F16 before running the F16 flash attention kernels 3202 static bool ggml_metal_op_flash_attn_ext_use_kv_f16(const ggml_tensor * op) { 3203 assert(op->op == GGML_OP_FLASH_ATTN_EXT); 3204 3205 // depending on compute/bandwidth ratio, dequant to f16 kv is not always beneficial 3206 // ref: https://github.com/ggml-org/llama.cpp/pull/27390#issuecomment-`5355152767` 3207 // TODO: tune per device 3208 // large heads need the upfront dequant to fit the non-vec threadgroup memory 3209 if (op->src[0]->ne[1] < 32 && 3210 (op->src[0]->ne[0] < 512 || ggml_metal_op_flash_attn_ext_use_vec(op))) { 3211 return false; 3212 } 3213 3214 switch (op->src[1]->type) { 3215 case GGML_TYPE_Q4_0: 3216 case GGML_TYPE_Q4_1: 3217 case GGML_TYPE_Q5_0: 3218 case GGML_TYPE_Q5_1: 3219 case GGML_TYPE_Q8_0: 3220 return true; 3221 default: 3222 return false; 3223 } 3224 } 3225 3226 static bool ggml_metal_op_flash_attn_ext_use_tensor(const ggml_tensor * op, bool has_tensor) { 3227 assert(op->op == GGML_OP_FLASH_ATTN_EXT); 3228 3229 if (!has_tensor || ggml_metal_op_flash_attn_ext_use_vec(op)) { 3230 return false; 3231 } 3232 3233 const int64_t ne01 = op->src[0]->ne[1]; 3234 const int64_t ne02 = op->src[0]->ne[2]; 3235 const int64_t ne03 = op->src[0]->ne[3]; 3236 3237 const int64_t dk = op->src[1]->ne[0]; 3238 const int64_t dv = op->src[2]->ne[0]; 3239 3240 const bool dk_dv_ok = (dk == 64 && dv == 64) || 3241 (dk == 128 && dv == 128) || 3242 (dk == 192 && dv == 128) || 3243 (dk == 256 && dv == 256) || 3244 (dk == 512 && dv == 512) || 3245 (dk == 576 && dv == 512); 3246 3247 if (!dk_dv_ok) { 3248 return false; 3249 } 3250 3251 // large heads use fewer queries per threadgroup, so that the queries fit in threadgroup memory 3252 const int64_t nqptg = dk >= 512 ? OP_FLASH_ATTN_EXT_TENSOR_NQPSG_LARGE : OP_FLASH_ATTN_EXT_TENSOR_NQPSG; 3253 3254 // few heads and small batches do not fill the GPU - the half8x8 kernel is faster there 3255 // TODO: tune per device 3256 if (((ne01 + nqptg - 1)/nqptg)*ne02*ne03*dk < 8192) { 3257 return false; 3258 } 3259 3260 if (op->src[1]->type != GGML_TYPE_F16 && op->src[1]->type != GGML_TYPE_BF16 && !ggml_metal_op_flash_attn_ext_use_kv_f16(op)) { 3261 return false; 3262 } 3263 3264 // with kv_rows, the attention runs over the compacted row lists (padded to 256), not over the cache 3265 const int64_t ne11_attn = op->src[5] ? op->src[5]->ne[0] : op->src[1]->ne[1]; 3266 3267 return ne11_attn % OP_FLASH_ATTN_EXT_TENSOR_NCPSG == 0; 3268 } 3269 3270 // returns the n_kv_max hint if the sparse path is available for this op, or 0 otherwise 3271 // the mask (src[3]) remains the single source of truth: finite entries are the valid KV positions, 3272 // n_kv_max is only an upper bound on their number per mask row, used to size the index lists 3273 static int ggml_metal_op_flash_attn_ext_n_kv_max_sparse(const ggml_tensor * op) { 3274 assert(op->op == GGML_OP_FLASH_ATTN_EXT); 3275 3276 // K/V row lists are handled by the dense kernels, not by the sparse path 3277 if (op->src[5] != nullptr) { 3278 return 0; 3279 } 3280 3281 int32_t n_kv_max = 0; 3282 memcpy(&n_kv_max, ((const int32_t *) op->op_params) + 4, sizeof(n_kv_max)); 3283 3284 if (n_kv_max <= 0) { 3285 return 0; 3286 } 3287 3288 // the sparse indices are gathered from the mask 3289 if (!op->src[3]) { 3290 return 0; 3291 } 3292 3293 // bound the size of the index lists 3294 if (n_kv_max > 4096) { 3295 return 0; 3296 } 3297 3298 // vec kernel instantiations exist for these (type, dk, dv) combinations only 3299 const int64_t dk = op->src[1]->ne[0]; 3300 const int64_t dv = op->src[2]->ne[0]; 3301 3302 const bool dk_dv_ok = (dk == 32 && dv == 32) || 3303 (dk == 64 && dv == 64) || 3304 (dk == 96 && dv == 96) || 3305 (dk == 96 && dv == 64) || 3306 (dk == 128 && dv == 128) || 3307 (dk == 128 && dv == 96) || 3308 (dk == 192 && dv == 128) || 3309 (dk == 192 && dv == 192) || 3310 (dk == 256 && dv == 256) || 3311 (dk == 320 && dv == 256) || 3312 (dk == 512 && dv == 512) || 3313 (dk == 576 && dv == 512); 3314 3315 if (!dk_dv_ok) { 3316 return 0; 3317 } 3318 3319 switch (op->src[1]->type) { 3320 case GGML_TYPE_F16: 3321 case GGML_TYPE_BF16: 3322 case GGML_TYPE_F32: 3323 case GGML_TYPE_Q4_0: 3324 case GGML_TYPE_Q4_1: 3325 case GGML_TYPE_Q5_0: 3326 case GGML_TYPE_Q5_1: 3327 case GGML_TYPE_Q8_0: 3328 break; 3329 default: 3330 return 0; 3331 } 3332 3333 return n_kv_max; 3334 } 3335 3336 // in some models (e.g. MLA-based), V is a view of K (the first ne20 elements of each K row); 3337 // the dequantized V is then a view of the dequantized K and does not need its own dequant or scratch 3338 // - ref: https://github.com/ggml-org/llama.cpp/pull/13435 3339 static bool ggml_metal_op_flash_attn_ext_v_is_view_of_k(const ggml_tensor * op) { 3340 assert(op->op == GGML_OP_FLASH_ATTN_EXT); 3341 3342 const ggml_tensor * K = op->src[1]; 3343 const ggml_tensor * V = op->src[2]; 3344 3345 return V->view_src && (V->view_src == K || (V->view_src == K->view_src && V->view_offs == K->view_offs)); 3346 } 3347 3348 // size of the F16 dequantized K tensor; the dequantized V tensor follows it in the same scratch buffer 3349 static size_t ggml_metal_op_flash_attn_ext_kv_f16_k_size(const ggml_tensor * op) { 3350 assert(op->op == GGML_OP_FLASH_ATTN_EXT); 3351 3352 GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne); 3353 3354 return GGML_PAD(sizeof(ggml_fp16_t)*(size_t) ne10*ne11*ne12*ne13, 16); 3355 } 3356 3357 size_t ggml_metal_op_flash_attn_ext_extra_pad(const ggml_tensor * op) { 3358 assert(op->op == GGML_OP_FLASH_ATTN_EXT); 3359 3360 GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); 3361 GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); 3362 GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne); 3363 GGML_TENSOR_LOCALS(uint64_t, nb1, op->src[1], nb); 3364 GGML_TENSOR_LOCALS( int32_t, ne2, op->src[2], ne); 3365 GGML_TENSOR_LOCALS(uint64_t, nb2, op->src[2], nb); 3366 GGML_TENSOR_LOCALS( int32_t, ne3, op->src[3], ne); 3367 GGML_TENSOR_LOCALS(uint64_t, nb3, op->src[3], nb); 3368 3369 size_t res = 0; 3370 3371 const bool has_mask = op->src[3] != nullptr; 3372 const bool use_kv_f16 = ggml_metal_op_flash_attn_ext_use_kv_f16(op); 3373 3374 // when the KV is dequantized to F16, the pad kernel copies the tail chunk from the F16 scratch buffer 3375 // note: when V is a view of K, the dequantized V is read from the dequantized K with K's row stride 3376 const bool v_is_view_of_k = use_kv_f16 && ggml_metal_op_flash_attn_ext_v_is_view_of_k(op); 3377 uint64_t nb11_pad = nb11; 3378 uint64_t nb21_pad = nb21; 3379 3380 if (use_kv_f16) { 3381 nb11_pad = sizeof(ggml_fp16_t)*ne10; 3382 nb21_pad = sizeof(ggml_fp16_t)*(v_is_view_of_k ? ne10 : ne20); 3383 } 3384 3385 // note: the non-vec kernel requires more extra memory, so always reserve for it 3386 GGML_ASSERT(OP_FLASH_ATTN_EXT_NCPSG >= OP_FLASH_ATTN_EXT_VEC_NCPSG); 3387 3388 //if (ggml_metal_op_flash_attn_ext_use_vec(op)) { 3389 if (false) { 3390 // note: always reserve the padding space to avoid graph reallocations 3391 //const bool has_kvpad = ne11 % OP_FLASH_ATTN_EXT_VEC_NCPSG != 0; 3392 const bool has_kvpad = true; 3393 3394 if (has_kvpad) { 3395 res += OP_FLASH_ATTN_EXT_VEC_NCPSG*( 3396 nb11_pad*ne12*ne13 + 3397 nb21_pad*ne22*ne23 + 3398 (has_mask ? ggml_type_size(GGML_TYPE_F16)*ne31*ne32*ne33 : 0)); 3399 } 3400 } else { 3401 //const bool has_kvpad = ne11 % OP_FLASH_ATTN_EXT_NCPSG != 0; 3402 const bool has_kvpad = true; 3403 3404 if (has_kvpad) { 3405 res += OP_FLASH_ATTN_EXT_NCPSG*( 3406 nb11_pad*ne12*ne13 + 3407 nb21_pad*ne22*ne23 + 3408 (has_mask ? ggml_type_size(GGML_TYPE_F16)*ne31*ne32*ne33 : 0)); 3409 } 3410 } 3411 3412 return res; 3413 } 3414 3415 size_t ggml_metal_op_flash_attn_ext_extra_blk(const ggml_tensor * op) { 3416 assert(op->op == GGML_OP_FLASH_ATTN_EXT); 3417 3418 GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); 3419 //GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); 3420 //GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne); 3421 //GGML_TENSOR_LOCALS(uint64_t, nb1, op->src[1], nb); 3422 //GGML_TENSOR_LOCALS( int32_t, ne2, op->src[2], ne); 3423 //GGML_TENSOR_LOCALS(uint64_t, nb2, op->src[2], nb); 3424 GGML_TENSOR_LOCALS( int32_t, ne3, op->src[3], ne); 3425 GGML_TENSOR_LOCALS(uint64_t, nb3, op->src[3], nb); 3426 3427 size_t res = 0; 3428 3429 const bool has_mask = op->src[3] != nullptr; 3430 3431 if (!has_mask) { 3432 return res; 3433 } 3434 3435 const bool is_vec = ggml_metal_op_flash_attn_ext_use_vec(op); 3436 3437 // this optimization is not useful for the vector kernels 3438 // note: always reserve the blk buffer to avoid graph reallocations 3439 //if (is_vec) { 3440 // return res; 3441 //} 3442 3443 const int nqptg = is_vec ? OP_FLASH_ATTN_EXT_VEC_NQPSG : OP_FLASH_ATTN_EXT_NQPSG; 3444 const int ncpsg = is_vec ? OP_FLASH_ATTN_EXT_VEC_NCPSG : OP_FLASH_ATTN_EXT_NCPSG; 3445 3446 const int64_t ne1 = (ne01 + nqptg - 1)/nqptg; 3447 const int64_t ne0 = (ne30 + ncpsg - 1)/ncpsg; 3448 3449 res += GGML_PAD(ggml_type_size(GGML_TYPE_I8)*ne0*ne1*ne32*ne33, 32); 3450 3451 return res; 3452 } 3453 3454 size_t ggml_metal_op_flash_attn_ext_extra_tmp(const ggml_tensor * op) { 3455 assert(op->op == GGML_OP_FLASH_ATTN_EXT); 3456 3457 GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); 3458 GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); 3459 //GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne); 3460 //GGML_TENSOR_LOCALS(uint64_t, nb1, op->src[1], nb); 3461 GGML_TENSOR_LOCALS( int32_t, ne2, op->src[2], ne); 3462 GGML_TENSOR_LOCALS(uint64_t, nb2, op->src[2], nb); 3463 //GGML_TENSOR_LOCALS( int32_t, ne3, op->src[3], ne); 3464 //GGML_TENSOR_LOCALS(uint64_t, nb3, op->src[3], nb); 3465 3466 size_t res = 0; 3467 3468 // note: always reserve the temp buffer to avoid graph reallocations 3469 //if (ggml_metal_op_flash_attn_ext_use_vec(op)) { 3470 if (true) { 3471 const int64_t nwg = 32; 3472 const int64_t ne01_max = std::min(ne01, 32); 3473 3474 // temp buffer for writing the results from each workgroup 3475 // - ne20: the size of the Value head 3476 // - + 2: the S and M values for each intermediate result 3477 res += ggml_type_size(GGML_TYPE_F32)*(ne01_max*ne02*ne03*nwg*(ne20 + 2)); 3478 } 3479 3480 return res; 3481 } 3482 3483 size_t ggml_metal_op_flash_attn_ext_extra_kv_f16(const ggml_tensor * op) { 3484 assert(op->op == GGML_OP_FLASH_ATTN_EXT); 3485 3486 // note: always reserve the temp buffer to avoid graph reallocations 3487 //if (!ggml_metal_op_flash_attn_ext_use_kv_f16(op)) { 3488 // return 0; 3489 //} 3490 3491 GGML_TENSOR_LOCALS( int32_t, ne2, op->src[2], ne); 3492 3493 const size_t k_size = ggml_metal_op_flash_attn_ext_kv_f16_k_size(op); 3494 3495 // when V is a view of K, the dequantized V is a view of the dequantized K 3496 const bool v_is_view_of_k = ggml_metal_op_flash_attn_ext_v_is_view_of_k(op); 3497 if (v_is_view_of_k) { 3498 return k_size; 3499 } 3500 3501 const size_t v_size = GGML_PAD(sizeof(ggml_fp16_t)*(size_t) ne20*ne21*ne22*ne23, 16); 3502 3503 return k_size + v_size; 3504 } 3505 3506 // size of the sparse index lists: one list of KV indices per mask row, 3507 // padded with -1 up to a multiple of OP_FLASH_ATTN_EXT_VEC_NCPSG 3508 size_t ggml_metal_op_flash_attn_ext_extra_idx(const ggml_tensor * op) { 3509 assert(op->op == GGML_OP_FLASH_ATTN_EXT); 3510 3511 GGML_TENSOR_LOCALS( int32_t, ne3, op->src[3], ne); 3512 3513 const int n_kv_max = ggml_metal_op_flash_attn_ext_n_kv_max_sparse(op); 3514 3515 if (n_kv_max <= 0) { 3516 return 0; 3517 } 3518 3519 const int n_kv_max_padded = GGML_PAD(n_kv_max, OP_FLASH_ATTN_EXT_VEC_NCPSG); 3520 3521 return GGML_PAD(sizeof(int32_t)*(size_t) n_kv_max_padded*ne31*ne32*ne33, 16); 3522 } 3523 3524 // size of the contiguous F16 buffers holding the K/V rows referenced by the kv_rows lists 3525 // the attention over these buffers is a standard dense op (no per-tile gather in the main kernel) 3526 size_t ggml_metal_op_flash_attn_ext_extra_rows(const ggml_tensor * op) { 3527 assert(op->op == GGML_OP_FLASH_ATTN_EXT); 3528 3529 const ggml_tensor * kv_rows = op->src[5]; 3530 3531 if (!kv_rows) { 3532 return 0; 3533 } 3534 3535 GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne); 3536 GGML_TENSOR_LOCALS( int32_t, ne2, op->src[2], ne); 3537 3538 const int64_t n_rows = kv_rows->ne[0]; 3539 const int64_t n_slices = kv_rows->ne[1]; 3540 3541 // note: when V is a view of K, the gathered V is read from the gathered K, so only K is gathered 3542 const bool v_is_view_of_k = ggml_metal_op_flash_attn_ext_v_is_view_of_k(op); 3543 3544 const size_t k_size = GGML_PAD(sizeof(ggml_fp16_t)*(size_t) ne10*n_rows*ne12*n_slices, 16); 3545 const size_t v_size = v_is_view_of_k ? 0 : GGML_PAD(sizeof(ggml_fp16_t)*(size_t) ne20*n_rows*ne22*n_slices, 16); 3546 3547 return k_size + v_size; 3548 } 3549 3550 int ggml_metal_op_flash_attn_ext(ggml_metal_op_t ctx, int idx) { 3551 ggml_tensor * op = ctx->node(idx); 3552 3553 ggml_metal_library_t lib = ctx->lib; 3554 ggml_metal_encoder_t enc = ctx->enc; 3555 3556 const ggml_metal_device_props * props_dev = ggml_metal_device_get_props(ctx->dev); 3557 3558 GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); 3559 GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); 3560 GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne); 3561 GGML_TENSOR_LOCALS(uint64_t, nb1, op->src[1], nb); 3562 GGML_TENSOR_LOCALS( int32_t, ne2, op->src[2], ne); 3563 GGML_TENSOR_LOCALS(uint64_t, nb2, op->src[2], nb); 3564 GGML_TENSOR_LOCALS( int32_t, ne3, op->src[3], ne); 3565 GGML_TENSOR_LOCALS(uint64_t, nb3, op->src[3], nb); 3566 GGML_TENSOR_LOCALS( int32_t, ne, op, ne); 3567 GGML_TENSOR_LOCALS( int32_t, nb, op, nb); 3568 3569 GGML_ASSERT(ne00 % 4 == 0); 3570 3571 GGML_ASSERT(op->src[0]->type == GGML_TYPE_F32); 3572 GGML_ASSERT(op->src[1]->type == op->src[2]->type); 3573 3574 //GGML_ASSERT(ggml_are_same_shape (src1, src2)); 3575 GGML_ASSERT(ne11 == ne21); 3576 GGML_ASSERT(ne12 == ne22); 3577 3578 GGML_ASSERT(!op->src[3] || op->src[3]->type == GGML_TYPE_F16); 3579 GGML_ASSERT(!op->src[3] || op->src[3]->ne[1] >= op->src[0]->ne[1] && 3580 "the Flash-Attention Metal kernel requires the mask to be at least n_queries big"); 3581 3582 // K/V row lists (ggml_flash_attn_ext_rows): src[5] holds one list of cache rows per q slice 3583 // the attention loops then run over the mask columns and read K/V rows through the lists 3584 const ggml_tensor * kv_rows = op->src[5]; 3585 const bool has_rows = kv_rows != nullptr; 3586 3587 GGML_ASSERT(!has_rows || (kv_rows->type == GGML_TYPE_I32 && ggml_is_contiguous(kv_rows))); 3588 GGML_ASSERT(!has_rows || (kv_rows->ne[1] == ne03 && kv_rows->ne[2] == 1 && kv_rows->ne[3] == 1)); 3589 GGML_ASSERT(!has_rows || op->src[3]); 3590 3591 // the loop bound over the mask columns / K/V row lists 3592 const int64_t ne11_attn = has_rows ? kv_rows->ne[0] : ne11; 3593 3594 // the row lists are padded by the KV cache to a multiple of 256, so the pad path never triggers 3595 GGML_ASSERT(!has_rows || ne11_attn % OP_FLASH_ATTN_EXT_NCPSG == 0); 3596 GGML_ASSERT(!has_rows || ne11_attn % OP_FLASH_ATTN_EXT_VEC_NCPSG == 0); 3597 3598 float scale; 3599 float max_bias; 3600 float logit_softcap; 3601 3602 memcpy(&scale, ((const int32_t *) op->op_params) + 0, sizeof(scale)); 3603 memcpy(&max_bias, ((const int32_t *) op->op_params) + 1, sizeof(max_bias)); 3604 memcpy(&logit_softcap, ((const int32_t *) op->op_params) + 2, sizeof(logit_softcap)); 3605 3606 if (logit_softcap != 0.0f) { 3607 scale /= logit_softcap; 3608 } 3609 3610 const bool has_mask = op->src[3] != NULL; 3611 const bool has_sinks = op->src[4] != NULL; 3612 3613 GGML_ASSERT(!has_rows || has_mask); 3614 const bool has_bias = max_bias != 0.0f; 3615 const bool has_scap = logit_softcap != 0.0f; 3616 3617 const uint32_t n_head = op->src[0]->ne[2]; 3618 const int32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head)); 3619 3620 const float m0 = powf(2.0f, -(max_bias ) / n_head_log2); 3621 const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2); 3622 3623 GGML_ASSERT(ne01 < 65536); 3624 3625 ggml_metal_buffer_id bid_src0 = ggml_metal_get_buffer_id(op->src[0]); 3626 ggml_metal_buffer_id bid_src1 = ggml_metal_get_buffer_id(op->src[1]); 3627 ggml_metal_buffer_id bid_src2 = ggml_metal_get_buffer_id(op->src[2]); 3628 ggml_metal_buffer_id bid_src3 = has_mask ? ggml_metal_get_buffer_id(op->src[3]) : bid_src0; 3629 ggml_metal_buffer_id bid_src4 = has_sinks ? ggml_metal_get_buffer_id(op->src[4]) : bid_src0; 3630 3631 ggml_metal_buffer_id bid_dst = ggml_metal_get_buffer_id(op); 3632 3633 ggml_metal_buffer_id bid_pad = bid_dst; 3634 bid_pad.offs += ggml_nbytes(op); 3635 3636 ggml_metal_buffer_id bid_blk = bid_pad; 3637 bid_blk.offs += ggml_metal_op_flash_attn_ext_extra_pad(op); 3638 3639 ggml_metal_buffer_id bid_tmp = bid_blk; 3640 bid_tmp.offs += ggml_metal_op_flash_attn_ext_extra_blk(op); 3641 3642 ggml_metal_buffer_id bid_kv_f16 = bid_tmp; 3643 bid_kv_f16.offs += ggml_metal_op_flash_attn_ext_extra_tmp(op); 3644 3645 // sparse path: gather the finite mask entries into index lists and run the vec kernels over them 3646 const int n_kv_max_sparse = ggml_metal_op_flash_attn_ext_n_kv_max_sparse(op); 3647 const bool use_sparse = n_kv_max_sparse > 0; 3648 const int n_kv_max_padded = use_sparse ? GGML_PAD(n_kv_max_sparse, OP_FLASH_ATTN_EXT_VEC_NCPSG) : 0; 3649 3650 const bool use_vec = ggml_metal_op_flash_attn_ext_use_vec(op); 3651 3652 // with kv_rows, the non-vec path gathers the referenced rows into a contiguous F16 buffer and 3653 // attends over it with the F16 kernel - quantized KV is first dequantized with the F16 pass 3654 const bool rows_gather = has_rows && !use_vec; 3655 3656 // the vec kernels dequantize the KV inline; no need for the F16 dequant pass in the sparse path 3657 const bool dequant_kv = !use_sparse && (rows_gather ? 3658 ggml_is_quantized(op->src[1]->type) : ggml_metal_op_flash_attn_ext_use_kv_f16(op)); 3659 3660 const bool use_kv_f16 = !use_sparse && (rows_gather ? true : dequant_kv); 3661 3662 ggml_metal_buffer_id bid_idx = bid_kv_f16; 3663 bid_idx.offs += ggml_metal_op_flash_attn_ext_extra_kv_f16(op); 3664 3665 // contiguous K/V buffers holding the rows referenced by the kv_rows lists 3666 ggml_metal_buffer_id bid_rows = bid_idx; 3667 bid_rows.offs += ggml_metal_op_flash_attn_ext_extra_idx(op); 3668 3669 ggml_metal_buffer_id bid_k = bid_src1; 3670 ggml_metal_buffer_id bid_v = bid_src2; 3671 3672 uint64_t nb10_attn = nb10; 3673 uint64_t nb11_attn = nb11; 3674 uint64_t nb12_attn = nb12; 3675 uint64_t nb13_attn = nb13; 3676 uint64_t nb20_attn = nb20; 3677 uint64_t nb21_attn = nb21; 3678 uint64_t nb22_attn = nb22; 3679 uint64_t nb23_attn = nb23; 3680 3681 if (dequant_kv) { 3682 assert(ggml_metal_op_flash_attn_ext_extra_kv_f16(op) != 0); 3683 3684 const bool v_is_view_of_k = ggml_metal_op_flash_attn_ext_v_is_view_of_k(op); 3685 3686 const int64_t nblocks1_64 = (ne10/ggml_blck_size(op->src[1]->type))*(int64_t) ne11*ne12*ne13; 3687 GGML_ASSERT(nblocks1_64 <= INT32_MAX); 3688 const int32_t nblocks1 = nblocks1_64; 3689 3690 ggml_metal_buffer_id bid_v_f16 = bid_kv_f16; 3691 bid_v_f16.offs += ggml_metal_op_flash_attn_ext_kv_f16_k_size(op); 3692 3693 auto pipeline0 = ggml_metal_library_get_pipeline_flash_attn_ext_kv_f16(lib, op); 3694 const int nth = std::min(ggml_metal_pipeline_max_theads_per_threadgroup(pipeline0), 256); 3695 3696 // K 3697 ggml_metal_kargs_flash_attn_ext_kv_f16 args_k = { 3698 /*.ne0 =*/ ne10, 3699 /*.ne1 =*/ ne11, 3700 /*.ne2 =*/ ne12, 3701 /*.ne3 =*/ ne13, 3702 /*.nb0 =*/ nb10, 3703 /*.nb1 =*/ nb11, 3704 /*.nb2 =*/ nb12, 3705 /*.nb3 =*/ nb13, 3706 /*.nblocks =*/ nblocks1, 3707 }; 3708 3709 ggml_metal_encoder_set_pipeline(enc, pipeline0); 3710 ggml_metal_encoder_set_bytes (enc, &args_k, sizeof(args_k), 0); 3711 ggml_metal_encoder_set_buffer (enc, bid_src1, 1); 3712 ggml_metal_encoder_set_buffer (enc, bid_kv_f16, 2); 3713 3714 ggml_metal_encoder_dispatch_threadgroups(enc, (nblocks1 + nth - 1)/nth, 1, 1, nth, 1, 1); 3715 3716 // V (skip when V is a view of K: the dequantized V is a view of the dequantized K) 3717 if (!v_is_view_of_k) { 3718 const int64_t nblocks2_64 = (ne20/ggml_blck_size(op->src[2]->type))*(int64_t) ne21*ne22*ne23; 3719 GGML_ASSERT(nblocks2_64 <= INT32_MAX); 3720 const int32_t nblocks2 = nblocks2_64; 3721 3722 ggml_metal_kargs_flash_attn_ext_kv_f16 args_v = { 3723 /*.ne0 =*/ ne20, 3724 /*.ne1 =*/ ne21, 3725 /*.ne2 =*/ ne22, 3726 /*.ne3 =*/ ne23, 3727 /*.nb0 =*/ nb20, 3728 /*.nb1 =*/ nb21, 3729 /*.nb2 =*/ nb22, 3730 /*.nb3 =*/ nb23, 3731 /*.nblocks =*/ nblocks2, 3732 }; 3733 3734 ggml_metal_encoder_set_pipeline(enc, pipeline0); 3735 ggml_metal_encoder_set_bytes (enc, &args_v, sizeof(args_v), 0); 3736 ggml_metal_encoder_set_buffer (enc, bid_src2, 1); 3737 ggml_metal_encoder_set_buffer (enc, bid_v_f16, 2); 3738 3739 ggml_metal_encoder_dispatch_threadgroups(enc, (nblocks2 + nth - 1)/nth, 1, 1, nth, 1, 1); 3740 } 3741 3742 // the pad and attention kernels read the dequantized KV 3743 ggml_metal_op_concurrency_reset(ctx); 3744 3745 bid_k = bid_kv_f16; 3746 bid_v = v_is_view_of_k ? bid_k : bid_v_f16; 3747 3748 // contiguous F16 layout of the dequantized K 3749 nb10_attn = sizeof(ggml_fp16_t); 3750 nb11_attn = nb10_attn*ne10; 3751 nb12_attn = nb11_attn*ne11; 3752 nb13_attn = nb12_attn*ne12; 3753 3754 // if V is a view of K, the dequantized V is read from the dequantized K with K's strides 3755 if (v_is_view_of_k) { 3756 nb20_attn = nb10_attn; 3757 nb21_attn = nb11_attn; 3758 nb22_attn = nb12_attn; 3759 nb23_attn = nb13_attn; 3760 } else { 3761 // contiguous F16 layout of the dequantized V 3762 nb20_attn = sizeof(ggml_fp16_t); 3763 nb21_attn = nb20_attn*ne20; 3764 nb22_attn = nb21_attn*ne21; 3765 nb23_attn = nb22_attn*ne22; 3766 } 3767 } 3768 3769 // with kv_rows, gather the cache rows referenced by the lists into contiguous F16 buffers 3770 // the attention then runs as a standard dense op over the gathered rows (tensor or half8x8 kernel) 3771 if (rows_gather) { 3772 assert(ggml_metal_op_flash_attn_ext_extra_rows(op) != 0); 3773 3774 const bool v_is_view_of_k = ggml_metal_op_flash_attn_ext_v_is_view_of_k(op); 3775 3776 GGML_ASSERT(ne20 % 4 == 0); 3777 3778 const size_t k_size = GGML_PAD(sizeof(ggml_fp16_t)*(size_t) ne10*ne11_attn*ne12*ne03, 16); 3779 3780 ggml_metal_kargs_flash_attn_ext_rows args0 = { 3781 /*.ne11 =*/ (int32_t) ne11_attn, 3782 /*.ne_12_2 =*/ ne12, 3783 /*.ne10 =*/ ne10, 3784 /*.ne20 =*/ v_is_view_of_k ? 0 : ne20, // the gathered V is a view of the gathered K 3785 /*.v_off =*/ k_size, 3786 /*.nb11 =*/ nb11_attn, 3787 /*.nb12 =*/ nb12_attn, 3788 /*.nb21 =*/ nb21_attn, 3789 /*.nb22 =*/ nb22_attn, 3790 }; 3791 3792 auto pipeline0 = ggml_metal_library_get_pipeline_flash_attn_ext_rows(lib, op); 3793 const int nth = std::min(ggml_metal_pipeline_max_theads_per_threadgroup(pipeline0), 64); 3794 3795 ggml_metal_encoder_set_pipeline(enc, pipeline0); 3796 ggml_metal_encoder_set_bytes (enc, &args0, sizeof(args0), 0); 3797 ggml_metal_encoder_set_buffer (enc, bid_k, 1); 3798 ggml_metal_encoder_set_buffer (enc, bid_v, 2); 3799 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(kv_rows), 3); 3800 ggml_metal_encoder_set_buffer (enc, bid_rows, 4); 3801 3802 ggml_metal_encoder_dispatch_threadgroups(enc, ne11_attn, ne03, 1, nth, 1, 1); 3803 3804 ggml_metal_op_concurrency_reset(ctx); 3805 3806 // contiguous F16 layout of the gathered rows: [DK, n_rows, n_head_kv, n_streams] 3807 bid_k = bid_rows; 3808 3809 nb10_attn = sizeof(ggml_fp16_t); 3810 nb11_attn = nb10_attn*ne10; 3811 nb12_attn = nb11_attn*ne11_attn; 3812 nb13_attn = nb12_attn*ne12; 3813 3814 // if V is a view of K, the gathered V is read from the gathered K with K's strides 3815 if (v_is_view_of_k) { 3816 bid_v = bid_k; 3817 3818 nb20_attn = nb10_attn; 3819 nb21_attn = nb11_attn; 3820 nb22_attn = nb12_attn; 3821 nb23_attn = nb13_attn; 3822 } else { 3823 bid_v = bid_rows; 3824 bid_v.offs += k_size; 3825 3826 nb20_attn = sizeof(ggml_fp16_t); 3827 nb21_attn = nb20_attn*ne20; 3828 nb22_attn = nb21_attn*ne11_attn; 3829 nb23_attn = nb22_attn*ne22; 3830 } 3831 } 3832 3833 if (!use_sparse && ggml_metal_op_flash_attn_ext_use_tensor(op, props_dev->has_tensor)) { 3834 // tensor API kernel 3835 const int nqptg = ne00 >= 512 ? OP_FLASH_ATTN_EXT_TENSOR_NQPSG_LARGE : OP_FLASH_ATTN_EXT_TENSOR_NQPSG; // queries per threadgroup 3836 const int ncpsg = OP_FLASH_ATTN_EXT_TENSOR_NCPSG; // cache values per threadgroup 3837 const int nsg = OP_FLASH_ATTN_EXT_TENSOR_NSG; 3838 3839 if (has_mask) { 3840 assert(ggml_metal_op_flash_attn_ext_extra_blk(op) != 0); 3841 3842 ggml_metal_kargs_flash_attn_ext_blk args0 = { 3843 /*.ne01 =*/ ne01, 3844 /*.ne30 =*/ ne30, 3845 /*.ne31 =*/ ne31, 3846 /*.ne32 =*/ ne32, 3847 /*.ne33 =*/ ne33, 3848 /*.nb31 =*/ nb31, 3849 /*.nb32 =*/ nb32, 3850 /*.nb33 =*/ nb33, 3851 }; 3852 3853 auto pipeline0 = ggml_metal_library_get_pipeline_flash_attn_ext_blk(lib, op, nqptg, ncpsg); 3854 3855 ggml_metal_encoder_set_pipeline(enc, pipeline0); 3856 ggml_metal_encoder_set_bytes (enc, &args0, sizeof(args0), 0); 3857 ggml_metal_encoder_set_buffer (enc, bid_src3, 1); 3858 ggml_metal_encoder_set_buffer (enc, bid_blk, 2); 3859 3860 const int32_t nblk1 = ((ne01 + nqptg - 1)/nqptg); 3861 const int32_t nblk0 = ((ne30 + ncpsg - 1)/ncpsg); 3862 3863 ggml_metal_encoder_dispatch_threadgroups(enc, nblk0, nblk1, ne32*ne33, 32, 1, 1); 3864 3865 ggml_metal_op_concurrency_reset(ctx); 3866 } 3867 3868 const int32_t ns10 = nb11_attn/nb10_attn; 3869 const int32_t ns20 = nb21_attn/nb20_attn; 3870 3871 ggml_metal_kargs_flash_attn_ext args = { 3872 /*.ne01 =*/ ne01, 3873 /*.ne02 =*/ ne02, 3874 /*.ne03 =*/ ne03, 3875 /*.nb01 =*/ nb01, 3876 /*.nb02 =*/ nb02, 3877 /*.nb03 =*/ nb03, 3878 /*.ne11 =*/ (int32_t) ne11_attn, 3879 /*.ne_12_2 =*/ ne12, 3880 /*.ne_12_3 =*/ has_rows ? ne03 : ne13, // the gathered K/V holds one copy per stream 3881 /*.ns10 =*/ ns10, 3882 /*.nb11 =*/ nb11_attn, 3883 /*.nb12 =*/ nb12_attn, 3884 /*.nb13 =*/ nb13_attn, 3885 /*.ns20 =*/ ns20, 3886 /*.nb21 =*/ nb21_attn, 3887 /*.nb22 =*/ nb22_attn, 3888 /*.nb23 =*/ nb23_attn, 3889 /*.ne31 =*/ ne31, 3890 /*.ne32 =*/ ne32, 3891 /*.ne33 =*/ ne33, 3892 /*.nb31 =*/ nb31, 3893 /*.nb32 =*/ nb32, 3894 /*.nb33 =*/ nb33, 3895 /*.ne1 =*/ ne1, 3896 /*.ne2 =*/ ne2, 3897 /*.ne3 =*/ ne3, 3898 /*.scale =*/ scale, 3899 /*.max_bias =*/ max_bias, 3900 /*.m0 =*/ m0, 3901 /*.m1 =*/ m1, 3902 /*.n_head_log2 =*/ n_head_log2, 3903 /*.logit_softcap =*/ logit_softcap, 3904 }; 3905 3906 // shared memory layout: queries (16-bit), scores (float), probabilities (16-bit), row scale (float), rescale flag (int) 3907 const size_t smem = GGML_PAD(nqptg*ne00*sizeof(ggml_fp16_t) + nqptg*ncpsg*(sizeof(float) + sizeof(ggml_fp16_t)) + nqptg*sizeof(float) + sizeof(int32_t), 16); 3908 3909 auto pipeline = ggml_metal_library_get_pipeline_flash_attn_ext_tensor(lib, op, has_mask, has_sinks, has_bias, has_scap); 3910 3911 GGML_ASSERT(nsg*32 <= ggml_metal_pipeline_max_theads_per_threadgroup(pipeline)); 3912 GGML_ASSERT(smem <= props_dev->max_theadgroup_memory_size); 3913 3914 ggml_metal_encoder_set_pipeline(enc, pipeline); 3915 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 3916 ggml_metal_encoder_set_buffer (enc, bid_src0, 1); 3917 ggml_metal_encoder_set_buffer (enc, bid_k, 2); 3918 ggml_metal_encoder_set_buffer (enc, bid_v, 3); 3919 ggml_metal_encoder_set_buffer (enc, bid_src3, 4); 3920 ggml_metal_encoder_set_buffer (enc, bid_src4, 5); 3921 ggml_metal_encoder_set_buffer (enc, bid_blk, 6); 3922 ggml_metal_encoder_set_buffer (enc, bid_dst, 7); 3923 3924 ggml_metal_encoder_set_threadgroup_memory_size(enc, smem, 0); 3925 3926 ggml_metal_encoder_dispatch_threadgroups(enc, (ne01 + nqptg - 1)/nqptg, ne02, ne03, 32, nsg, 1); 3927 } else if (!use_sparse && !ggml_metal_op_flash_attn_ext_use_vec(op)) { 3928 // half8x8 kernel 3929 const int nqptg = OP_FLASH_ATTN_EXT_NQPSG; // queries per threadgroup 3930 const int ncpsg = OP_FLASH_ATTN_EXT_NCPSG; // cache values per simdgroup 3931 3932 GGML_ASSERT(nqptg <= 32); 3933 GGML_ASSERT(nqptg % 8 == 0); 3934 GGML_ASSERT(ncpsg % 32 == 0); 3935 3936 bool need_sync = false; 3937 3938 const bool has_kvpad = ne11_attn % ncpsg != 0; 3939 3940 if (has_kvpad) { 3941 assert(ggml_metal_op_flash_attn_ext_extra_pad(op) != 0); 3942 3943 ggml_metal_kargs_flash_attn_ext_pad args0 = { 3944 /*.ne11 =*/ne11, 3945 /*.ne_12_2 =*/ne12, 3946 /*.ne_12_3 =*/ne13, 3947 /*.nb11 =*/nb11_attn, 3948 /*.nb12 =*/nb12_attn, 3949 /*.nb13 =*/nb13_attn, 3950 /*.nb21 =*/nb21_attn, 3951 /*.nb22 =*/nb22_attn, 3952 /*.nb23 =*/nb23_attn, 3953 /*.ne31 =*/ne31, 3954 /*.ne32 =*/ne32, 3955 /*.ne33 =*/ne33, 3956 /*.nb31 =*/nb31, 3957 /*.nb32 =*/nb32, 3958 /*.nb33 =*/nb33, 3959 }; 3960 3961 auto pipeline0 = ggml_metal_library_get_pipeline_flash_attn_ext_pad(lib, op, has_mask, ncpsg); 3962 3963 ggml_metal_encoder_set_pipeline(enc, pipeline0); 3964 ggml_metal_encoder_set_bytes (enc, &args0, sizeof(args0), 0); 3965 ggml_metal_encoder_set_buffer (enc, bid_k, 1); 3966 ggml_metal_encoder_set_buffer (enc, bid_v, 2); 3967 ggml_metal_encoder_set_buffer (enc, bid_src3, 3); 3968 ggml_metal_encoder_set_buffer (enc, bid_pad, 4); 3969 3970 assert(ne12 == ne22); 3971 assert(ne13 == ne23); 3972 3973 ggml_metal_encoder_dispatch_threadgroups(enc, ncpsg, std::max(ne12, ne32), std::max(ne13, ne33), 32, 1, 1); 3974 3975 need_sync = true; 3976 } 3977 3978 if (has_mask) { 3979 assert(ggml_metal_op_flash_attn_ext_extra_blk(op) != 0); 3980 3981 ggml_metal_kargs_flash_attn_ext_blk args0 = { 3982 /*.ne01 =*/ ne01, 3983 /*.ne30 =*/ ne30, 3984 /*.ne31 =*/ ne31, 3985 /*.ne32 =*/ ne32, 3986 /*.ne33 =*/ ne33, 3987 /*.nb31 =*/ nb31, 3988 /*.nb32 =*/ nb32, 3989 /*.nb33 =*/ nb33, 3990 }; 3991 3992 auto pipeline0 = ggml_metal_library_get_pipeline_flash_attn_ext_blk(lib, op, nqptg, ncpsg); 3993 3994 ggml_metal_encoder_set_pipeline(enc, pipeline0); 3995 ggml_metal_encoder_set_bytes (enc, &args0, sizeof(args0), 0); 3996 ggml_metal_encoder_set_buffer (enc, bid_src3, 1); 3997 ggml_metal_encoder_set_buffer (enc, bid_blk, 2); 3998 3999 const int32_t nblk1 = ((ne01 + nqptg - 1)/nqptg); 4000 const int32_t nblk0 = ((ne30 + ncpsg - 1)/ncpsg); 4001 4002 ggml_metal_encoder_dispatch_threadgroups(enc, nblk0, nblk1, ne32*ne33, 32, 1, 1); 4003 4004 need_sync = true; 4005 } 4006 4007 if (need_sync) { 4008 ggml_metal_op_concurrency_reset(ctx); 4009 } 4010 4011 const int is_q = !use_kv_f16 && ggml_is_quantized(op->src[1]->type) ? 1 : 0; 4012 4013 // shared memory layout (halfs unless noted): 4014 // queries/attn/result: Q*(DK + 2*PAD2(DV,64) + 4*C) 4015 // quantized KV scratch: 16*32*NSG (only when is_q) 4016 const int64_t dv_pad = GGML_PAD(ne20, 64); 4017 4018 auto fa_smem = [&](int32_t nsg) -> size_t { 4019 const size_t smem_half = nqptg*(ne00 + 2*dv_pad + 4*ncpsg) + is_q*(16*32*nsg); 4020 return GGML_PAD(smem_half*sizeof(ggml_fp16_t), 16); 4021 }; 4022 4023 // simdgroups per threadgroup (a.k.a. warps) 4024 int32_t nsg = ne00 >= 512 ? 8 : 4; 4025 4026 const size_t smem = fa_smem(nsg); 4027 4028 const int32_t ns10 = nb11_attn/nb10_attn; 4029 const int32_t ns20 = nb21_attn/nb20_attn; 4030 4031 ggml_metal_kargs_flash_attn_ext args = { 4032 /*.ne01 =*/ ne01, 4033 /*.ne02 =*/ ne02, 4034 /*.ne03 =*/ ne03, 4035 /*.nb01 =*/ nb01, 4036 /*.nb02 =*/ nb02, 4037 /*.nb03 =*/ nb03, 4038 /*.ne11 =*/ (int32_t) ne11_attn, 4039 /*.ne_12_2 =*/ ne12, 4040 /*.ne_12_3 =*/ has_rows ? ne03 : ne13, // the gathered K/V holds one copy per stream 4041 /*.ns10 =*/ ns10, 4042 /*.nb11 =*/ nb11_attn, 4043 /*.nb12 =*/ nb12_attn, 4044 /*.nb13 =*/ nb13_attn, 4045 /*.ns20 =*/ ns20, 4046 /*.nb21 =*/ nb21_attn, 4047 /*.nb22 =*/ nb22_attn, 4048 /*.nb23 =*/ nb23_attn, 4049 /*.ne31 =*/ ne31, 4050 /*.ne32 =*/ ne32, 4051 /*.ne33 =*/ ne33, 4052 /*.nb31 =*/ nb31, 4053 /*.nb32 =*/ nb32, 4054 /*.nb33 =*/ nb33, 4055 /*.ne1 =*/ ne1, 4056 /*.ne2 =*/ ne2, 4057 /*.ne3 =*/ ne3, 4058 /*.scale =*/ scale, 4059 /*.max_bias =*/ max_bias, 4060 /*.m0 =*/ m0, 4061 /*.m1 =*/ m1, 4062 /*.n_head_log2 =*/ n_head_log2, 4063 /*.logit_softcap =*/ logit_softcap, 4064 }; 4065 4066 auto pipeline = ggml_metal_library_get_pipeline_flash_attn_ext(lib, op, has_mask, has_sinks, has_bias, has_scap, has_kvpad, nsg, use_kv_f16, ns10, ns20); 4067 4068 ggml_metal_encoder_set_pipeline(enc, pipeline); 4069 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 4070 ggml_metal_encoder_set_buffer (enc, bid_src0, 1); 4071 ggml_metal_encoder_set_buffer (enc, bid_k, 2); 4072 ggml_metal_encoder_set_buffer (enc, bid_v, 3); 4073 ggml_metal_encoder_set_buffer (enc, bid_src3, 4); 4074 ggml_metal_encoder_set_buffer (enc, bid_src4, 5); 4075 ggml_metal_encoder_set_buffer (enc, bid_pad, 6); 4076 ggml_metal_encoder_set_buffer (enc, bid_blk, 7); 4077 ggml_metal_encoder_set_buffer (enc, bid_dst, 8); 4078 4079 GGML_ASSERT(smem <= props_dev->max_theadgroup_memory_size); 4080 4081 ggml_metal_encoder_set_threadgroup_memory_size(enc, smem, 0); 4082 4083 ggml_metal_encoder_dispatch_threadgroups(enc, (ne01 + nqptg - 1)/nqptg, ne02, ne03, 32, nsg, 1); 4084 } else { 4085 // half4x4 kernel 4086 // sparse: the index lists are per query row, so a threadgroup can share KV with Q == 1 only 4087 auto cfg = use_sparse 4088 ? ggml_metal_tuning::fa_vec_baseline_cfg((int) ne00, (int) ne20) 4089 : ggml_metal_tuning::fa_vec_pick( 4090 props_dev->gpu_family, 4091 (int) op->src[1]->type, 4092 (int) ne00, (int) ne20, // dk, dv (ne00 == dk for FA) 4093 ne11, ne01); 4094 4095 int nqptg = cfg.Q; // queries per threadgroup 4096 4097 const int ncpsg = OP_FLASH_ATTN_EXT_VEC_NCPSG; // cache values per simdgroup !! sync with kernel template arguments !! 4098 const int nhptg = 1; // heads per threadgroup 4099 4100 GGML_ASSERT(nqptg <= 32); 4101 GGML_ASSERT(nqptg == 1 || nqptg == 2 || nqptg == 4); // only instantiated Q values 4102 GGML_ASSERT(ncpsg % 32 == 0); 4103 4104 bool need_sync = false; 4105 4106 const bool has_kvpad = !use_sparse && ne11_attn % ncpsg != 0; 4107 4108 if (use_sparse) { 4109 assert(ggml_metal_op_flash_attn_ext_extra_idx(op) != 0); 4110 4111 GGML_ASSERT(ne30 == ne11); 4112 4113 ggml_metal_kargs_flash_attn_ext_vec_idx args0 = { 4114 /*.ne30 =*/ ne30, 4115 /*.ne31 =*/ ne31, 4116 /*.ne32 =*/ ne32, 4117 /*.ne33 =*/ ne33, 4118 /*.nb31 =*/ nb31, 4119 /*.nb32 =*/ nb32, 4120 /*.nb33 =*/ nb33, 4121 /*.n_kv_max =*/ n_kv_max_sparse, 4122 /*.n_kv_max_padded =*/ n_kv_max_padded, 4123 }; 4124 4125 auto pipeline0 = ggml_metal_library_get_pipeline_flash_attn_ext_vec_idx(lib, op); 4126 4127 ggml_metal_encoder_set_pipeline(enc, pipeline0); 4128 ggml_metal_encoder_set_bytes (enc, &args0, sizeof(args0), 0); 4129 ggml_metal_encoder_set_buffer (enc, bid_src3, 1); 4130 ggml_metal_encoder_set_buffer (enc, bid_idx, 2); 4131 4132 int nth = std::min(ggml_metal_pipeline_max_theads_per_threadgroup(pipeline0), 256); 4133 nth = std::max(32, (nth/32)*32); 4134 4135 ggml_metal_encoder_dispatch_threadgroups(enc, ne31, ne32, ne33, nth, 1, 1); 4136 4137 need_sync = true; 4138 } 4139 4140 if (has_kvpad) { 4141 assert(ggml_metal_op_flash_attn_ext_extra_pad(op) != 0); 4142 4143 ggml_metal_kargs_flash_attn_ext_pad args0 = { 4144 /*.ne11 =*/ne11, 4145 /*.ne_12_2 =*/ne12, 4146 /*.ne_12_3 =*/ne13, 4147 /*.nb11 =*/nb11_attn, 4148 /*.nb12 =*/nb12_attn, 4149 /*.nb13 =*/nb13_attn, 4150 /*.nb21 =*/nb21_attn, 4151 /*.nb22 =*/nb22_attn, 4152 /*.nb23 =*/nb23_attn, 4153 /*.ne31 =*/ne31, 4154 /*.ne32 =*/ne32, 4155 /*.ne33 =*/ne33, 4156 /*.nb31 =*/nb31, 4157 /*.nb32 =*/nb32, 4158 /*.nb33 =*/nb33, 4159 }; 4160 4161 auto pipeline0 = ggml_metal_library_get_pipeline_flash_attn_ext_pad(lib, op, has_mask, ncpsg); 4162 4163 ggml_metal_encoder_set_pipeline(enc, pipeline0); 4164 ggml_metal_encoder_set_bytes (enc, &args0, sizeof(args0), 0); 4165 ggml_metal_encoder_set_buffer (enc, bid_k, 1); 4166 ggml_metal_encoder_set_buffer (enc, bid_v, 2); 4167 ggml_metal_encoder_set_buffer (enc, bid_src3, 3); 4168 ggml_metal_encoder_set_buffer (enc, bid_pad, 4); 4169 4170 assert(ne12 == ne22); 4171 assert(ne13 == ne23); 4172 4173 ggml_metal_encoder_dispatch_threadgroups(enc, ncpsg, std::max(ne12, ne32), std::max(ne13, ne33), 32, 1, 1); 4174 4175 need_sync = true; 4176 } 4177 4178 if (need_sync) { 4179 ggml_metal_op_concurrency_reset(ctx); 4180 } 4181 4182 // note: for simplicity assume the K is larger or equal than V 4183 GGML_ASSERT(ne10 >= ne20); 4184 4185 // shared memory layout (halfs unless noted): 4186 // queries: Q*NSG*PAD2(ne00, 128) 4187 // attn + mask: NSG*4*Q*C 4188 // results: 2*NSG*Q*PAD2(ne20, 128) 4189 // sparse idx: NSG*C ints (when use_sparse, or when gathering K/V rows) 4190 const int64_t dk_pad = GGML_PAD(ne00, 128); 4191 const int64_t dv_pad = GGML_PAD(ne20, 128); 4192 4193 auto fa_vec_smem = [&](int64_t nsg, int32_t nqptg) -> size_t { 4194 const size_t smem_half = (size_t) (dk_pad + 4*ncpsg + 2*dv_pad)*nqptg*nsg; 4195 return GGML_PAD(smem_half*sizeof(ggml_fp16_t) + ((use_sparse || has_rows) ? (size_t) nsg*ncpsg*sizeof(int) : 0), 16); 4196 }; 4197 4198 int64_t nsg = 1; 4199 4200 // workgroups 4201 // each workgroup handles nsg*nkpsg cache values 4202 int32_t nwg = 1; 4203 if (use_sparse) { 4204 if (ne01 > 32) { 4205 // large sparse batch 4206 nwg = 1; 4207 nsg = 1; 4208 if (n_kv_max_padded == 640) { 4209 nsg = 4; // 640 % (4*32) == 0 4210 } else { 4211 while (2*nwg*nsg*ncpsg < n_kv_max_padded && nsg < 4) { 4212 nsg *= 2; 4213 } 4214 } 4215 } else { 4216 // small sparse batch 4217 nwg = 32; 4218 nsg = 1; 4219 while (2*nwg*nsg*ncpsg < n_kv_max_padded && nsg < 4) { 4220 nsg *= 2; 4221 } 4222 } 4223 } else { 4224 nwg = 32; 4225 nsg = 1; 4226 while (2*nwg*nsg*ncpsg < ne11_attn && nsg < 4) { 4227 nsg *= 2; 4228 } 4229 } 4230 4231 // fall back to baseline (Q=1) if the tuned config exceeds threadgroup memory 4232 if (fa_vec_smem(nsg, nqptg) > props_dev->max_theadgroup_memory_size) { 4233 cfg = ggml_metal_tuning::fa_vec_baseline_cfg((int) ne00, (int) ne20); 4234 nqptg = cfg.Q; // = 1 4235 } 4236 4237 const int32_t ns10 = nb11_attn/nb10_attn; 4238 const int32_t ns20 = nb21_attn/nb20_attn; 4239 4240 ggml_metal_kargs_flash_attn_ext_vec args = { 4241 /*.ne01 =*/ ne01, 4242 /*.ne02 =*/ ne02, 4243 /*.ne03 =*/ ne03, 4244 /*.nb01 =*/ nb01, 4245 /*.nb02 =*/ nb02, 4246 /*.nb03 =*/ nb03, 4247 /*.ne11 =*/ use_sparse ? n_kv_max_padded : (int32_t) ne11_attn, 4248 /*.ne_12_2 =*/ ne12, 4249 /*.ne_12_3 =*/ ne13, 4250 /*.ns10 =*/ ns10, 4251 /*.nb11 =*/ nb11_attn, 4252 /*.nb12 =*/ nb12_attn, 4253 /*.nb13 =*/ nb13_attn, 4254 /*.ns20 =*/ ns20, 4255 /*.nb21 =*/ nb21_attn, 4256 /*.nb22 =*/ nb22_attn, 4257 /*.nb23 =*/ nb23_attn, 4258 /*.ne31 =*/ ne31, 4259 /*.ne32 =*/ ne32, 4260 /*.ne33 =*/ ne33, 4261 /*.nb31 =*/ nb31, 4262 /*.nb32 =*/ nb32, 4263 /*.nb33 =*/ nb33, 4264 /*.ne1 =*/ ne1, 4265 /*.ne2 =*/ ne2, 4266 /*.ne3 =*/ ne3, 4267 /*.scale =*/ scale, 4268 /*.max_bias =*/ max_bias, 4269 /*.m0 =*/ m0, 4270 /*.m1 =*/ m1, 4271 /*.n_head_log2 =*/ n_head_log2, 4272 /*.logit_softcap =*/ logit_softcap, 4273 /*.n_kv_max_padded =*/ n_kv_max_padded, 4274 }; 4275 4276 auto pipeline = ggml_metal_library_get_pipeline_flash_attn_ext_vec(lib, op, has_mask, has_sinks, has_bias, has_scap, has_kvpad, use_sparse, has_rows, nqptg, cfg.NE, nsg, nwg, use_kv_f16, ns10, ns20); 4277 4278 GGML_ASSERT(nsg*32 <= ggml_metal_pipeline_max_theads_per_threadgroup(pipeline)); 4279 4280 ggml_metal_encoder_set_pipeline(enc, pipeline); 4281 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 4282 ggml_metal_encoder_set_buffer (enc, bid_src0, 1); 4283 ggml_metal_encoder_set_buffer (enc, bid_k, 2); 4284 ggml_metal_encoder_set_buffer (enc, bid_v, 3); 4285 ggml_metal_encoder_set_buffer (enc, bid_src3, 4); 4286 ggml_metal_encoder_set_buffer (enc, bid_src4, 5); 4287 ggml_metal_encoder_set_buffer (enc, use_sparse ? bid_idx : (has_rows ? ggml_metal_get_buffer_id(kv_rows) : bid_src0), 8); 4288 4289 const size_t smem = fa_vec_smem(nsg, nqptg); 4290 4291 GGML_ASSERT(smem <= props_dev->max_theadgroup_memory_size); 4292 4293 if (nwg == 1) { 4294 // using 1 workgroup -> write the result directly into dst 4295 ggml_metal_encoder_set_buffer(enc, bid_pad, 6); 4296 ggml_metal_encoder_set_buffer(enc, bid_dst, 7); 4297 4298 ggml_metal_encoder_set_threadgroup_memory_size(enc, smem, 0); 4299 4300 ggml_metal_encoder_dispatch_threadgroups(enc, (ne01 + nqptg - 1)/nqptg, (ne02 + nhptg - 1)/nhptg, ne03*nwg, 32, nsg, 1); 4301 } else { 4302 // sanity checks 4303 assert(ggml_metal_op_flash_attn_ext_extra_tmp(op) != 0); 4304 4305 GGML_ASSERT(ne01*ne02*ne03 == ne1*ne2*ne3); 4306 GGML_ASSERT((uint64_t)ne1*ne2*ne3 <= (1u << 31)); 4307 4308 // write the results from each workgroup into a temp buffer 4309 ggml_metal_encoder_set_buffer(enc, bid_pad, 6); 4310 ggml_metal_encoder_set_buffer(enc, bid_tmp, 7); 4311 4312 ggml_metal_encoder_set_threadgroup_memory_size(enc, smem, 0); 4313 ggml_metal_encoder_dispatch_threadgroups(enc, (ne01 + nqptg - 1)/nqptg, (ne02 + nhptg - 1)/nhptg, ne03*nwg, 32, nsg, 1); 4314 4315 // sync the 2 kernels 4316 ggml_metal_op_concurrency_reset(ctx); 4317 4318 // reduce the results from the workgroups 4319 { 4320 const int32_t nrows = ne1*ne2*ne3; 4321 4322 ggml_metal_kargs_flash_attn_ext_vec_reduce args0 = { 4323 nrows, 4324 }; 4325 4326 auto pipeline0 = ggml_metal_library_get_pipeline_flash_attn_ext_vec_reduce(lib, op, ne20, nwg); 4327 4328 ggml_metal_encoder_set_pipeline(enc, pipeline0); 4329 ggml_metal_encoder_set_bytes (enc, &args0, sizeof(args0), 0); 4330 ggml_metal_encoder_set_buffer (enc, bid_tmp, 1); 4331 ggml_metal_encoder_set_buffer (enc, bid_dst, 2); 4332 4333 ggml_metal_encoder_dispatch_threadgroups(enc, nrows, 1, 1, 32*nwg, 1, 1); 4334 } 4335 } 4336 } 4337 4338 return 1; 4339 } 4340 4341 int ggml_metal_op_bin(ggml_metal_op_t ctx, int idx) { 4342 int n_fuse = 1; 4343 const ggml_metal_fusion * fusion = nullptr; 4344 4345 const bool use_fusion = ctx->use_fusion(); 4346 4347 if (use_fusion) { 4348 int n = 1; 4349 fusion = ctx->can_fuse(idx, GGML_METAL_FUSION_FULL, &n); 4350 n_fuse = n; 4351 4352 // snake activation autofuse: mul -> sin -> sqr -> mul -> add 4353 if (fusion && ggml_metal_fusion_get_id(fusion) == GGML_METAL_FUSION_SNAKE) { 4354 ctx->count_fusions(fusion); 4355 return ggml_metal_op_snake_fused(ctx, idx); 4356 } 4357 4358 // MoE output reduction: experts * weights -> weighted sum 4359 if (fusion && ggml_metal_fusion_get_id(fusion) == GGML_METAL_FUSION_MOE_REDUCE) { 4360 ctx->count_fusions(fusion); 4361 return ggml_metal_op_moe_reduce(ctx, idx); 4362 } 4363 } 4364 4365 ggml_tensor * op = ctx->node(idx); 4366 4367 ggml_metal_library_t lib = ctx->lib; 4368 ggml_metal_encoder_t enc = ctx->enc; 4369 4370 const int debug_fusion = ggml_metal_fusion_info_debug(ctx->finfo); 4371 4372 GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); 4373 GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); 4374 GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne); 4375 GGML_TENSOR_LOCALS(uint64_t, nb1, op->src[1], nb); 4376 GGML_TENSOR_LOCALS( int32_t, ne, op, ne); 4377 GGML_TENSOR_LOCALS(uint64_t, nb, op, nb); 4378 4379 GGML_ASSERT(ggml_is_contiguous_rows(op->src[0])); 4380 GGML_ASSERT(ggml_is_contiguous_rows(op->src[1])); 4381 4382 ggml_metal_buffer_id bid_src0 = ggml_metal_get_buffer_id(op->src[0]); 4383 ggml_metal_buffer_id bid_src1 = ggml_metal_get_buffer_id(op->src[1]); 4384 ggml_metal_buffer_id bid_dst = ggml_metal_get_buffer_id(op); 4385 4386 ggml_metal_kargs_bin args = { 4387 /*.ne00 =*/ ne00, 4388 /*.ne01 =*/ ne01, 4389 /*.ne02 =*/ ne02, 4390 /*.ne03 =*/ ne03, 4391 /*.nb00 =*/ nb00, 4392 /*.nb01 =*/ nb01, 4393 /*.nb02 =*/ nb02, 4394 /*.nb03 =*/ nb03, 4395 /*.ne10 =*/ ne10, 4396 /*.ne11 =*/ ne11, 4397 /*.ne12 =*/ ne12, 4398 /*.ne13 =*/ ne13, 4399 /*.nb10 =*/ nb10, 4400 /*.nb11 =*/ nb11, 4401 /*.nb12 =*/ nb12, 4402 /*.nb13 =*/ nb13, 4403 /*.ne0 =*/ ne0, 4404 /*.ne1 =*/ ne1, 4405 /*.ne2 =*/ ne2, 4406 /*.ne3 =*/ ne3, 4407 /*.nb0 =*/ nb0, 4408 /*.nb1 =*/ nb1, 4409 /*.nb2 =*/ nb2, 4410 /*.nb3 =*/ nb3, 4411 /*.offs =*/ 0, 4412 /*.o1 =*/ { bid_src1.offs }, 4413 }; 4414 4415 // c[0] = add(a, b[0]) 4416 // c[1] = add(c[0], b[1]) 4417 // c[2] = add(c[1], b[2]) 4418 // ... 4419 if (use_fusion && fusion && ggml_metal_fusion_get_id(fusion) == GGML_METAL_FUSION_ADD_CHAIN) { 4420 // the offsets of the fused addends are relative to the start of the src1 buffer 4421 for (int i = 1; i < n_fuse; i++) { 4422 args.o1[i] = ggml_metal_get_buffer_id(ctx->node(idx + i)->src[1]).offs; 4423 } 4424 4425 ctx->count_fusions(fusion); 4426 4427 if (debug_fusion > 1) { 4428 GGML_LOG_DEBUG("%s: fuse: ADD x %d\n", __func__, n_fuse); 4429 } 4430 } 4431 4432 // the offsets of src1 and all fused buffers are relative to the start of the src1 buffer 4433 bid_src1.offs = 0; 4434 4435 struct ggml_metal_pipeline_with_params pipeline; 4436 4437 pipeline = ggml_metal_library_get_pipeline_bin(lib, op, n_fuse); 4438 4439 if (n_fuse > 1) { 4440 bid_dst = ggml_metal_get_buffer_id(ctx->node(idx + n_fuse - 1)); 4441 4442 for (int i = 1; i < n_fuse; ++i) { 4443 if (!ggml_metal_op_concurrency_check(ctx, ctx->node(idx + i))) { 4444 ggml_metal_op_concurrency_reset(ctx); 4445 4446 break; 4447 } 4448 } 4449 } 4450 4451 if (pipeline.c4) { 4452 args.ne00 = ne00/4; 4453 args.ne10 = ne10/4; 4454 args.ne0 = ne0/4; 4455 } 4456 4457 ggml_metal_encoder_set_pipeline(enc, pipeline); 4458 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 4459 ggml_metal_encoder_set_buffer (enc, bid_src0, 1); 4460 ggml_metal_encoder_set_buffer (enc, bid_src1, 2); 4461 ggml_metal_encoder_set_buffer (enc, bid_dst, 3); 4462 4463 if (pipeline.cnt) { 4464 const int nth = MIN(256, args.ne0); 4465 4466 ggml_metal_encoder_dispatch_threadgroups(enc, (args.ne0 + nth - 1)/nth, ggml_nrows(op), 1, nth, 1, 1); 4467 } else { 4468 const int nth_max = MIN(256, ggml_metal_pipeline_max_theads_per_threadgroup(pipeline)); 4469 4470 int nth = 1; 4471 4472 while (2*nth < args.ne0 && nth < nth_max) { 4473 nth *= 2; 4474 } 4475 4476 ggml_metal_encoder_dispatch_threadgroups(enc, ne01, ne02, ne03, nth, 1, 1); 4477 } 4478 4479 return n_fuse; 4480 } 4481 4482 int ggml_metal_op_silu_back(ggml_metal_op_t ctx, int idx) { 4483 ggml_tensor * op = ctx->node(idx); 4484 4485 ggml_metal_library_t lib = ctx->lib; 4486 ggml_metal_encoder_t enc = ctx->enc; 4487 4488 auto pipeline = ggml_metal_library_get_pipeline_silu_back(lib, op); 4489 4490 const int64_t ne = ggml_nelements(op); 4491 4492 ggml_metal_kargs_silu_back args = { 4493 /*.ne =*/ ne, 4494 }; 4495 4496 int arg_idx{0}; 4497 4498 ggml_metal_encoder_set_pipeline(enc, pipeline); 4499 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), arg_idx++); 4500 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), arg_idx++); 4501 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[1]), arg_idx++); 4502 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), arg_idx++); 4503 4504 const int nth = std::min(ggml_metal_pipeline_max_theads_per_threadgroup(pipeline), ne); 4505 const int64_t n = (ne + nth - 1) / nth; 4506 4507 ggml_metal_encoder_dispatch_threadgroups(enc, n, 1, 1, nth, 1, 1); 4508 4509 return 1; 4510 } 4511 4512 int ggml_metal_op_l2_norm(ggml_metal_op_t ctx, int idx) { 4513 ggml_tensor * op = ctx->node(idx); 4514 4515 ggml_metal_library_t lib = ctx->lib; 4516 ggml_metal_encoder_t enc = ctx->enc; 4517 4518 GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); 4519 GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); 4520 GGML_TENSOR_LOCALS( int32_t, ne, op, ne); 4521 GGML_TENSOR_LOCALS(uint64_t, nb, op, nb); 4522 4523 GGML_ASSERT(ggml_is_contiguous_rows(op->src[0])); 4524 4525 ggml_metal_buffer_id bid_src0 = ggml_metal_get_buffer_id(op->src[0]); 4526 ggml_metal_buffer_id bid_dst = ggml_metal_get_buffer_id(op); 4527 4528 float eps; 4529 memcpy(&eps, op->op_params, sizeof(float)); 4530 4531 ggml_metal_kargs_l2_norm args = { 4532 /*.ne00 =*/ ne00, 4533 /*.ne01 =*/ ne01, 4534 /*.ne02 =*/ ne02, 4535 /*.ne03 =*/ ne03, 4536 /*.nb00 =*/ nb00, 4537 /*.nb01 =*/ nb01, 4538 /*.nb02 =*/ nb02, 4539 /*.nb03 =*/ nb03, 4540 /*.ne0 =*/ ne0, 4541 /*.ne1 =*/ ne1, 4542 /*.ne2 =*/ ne2, 4543 /*.ne3 =*/ ne3, 4544 /*.nb0 =*/ nb0, 4545 /*.nb1 =*/ nb1, 4546 /*.nb2 =*/ nb2, 4547 /*.nb3 =*/ nb3, 4548 /*.eps =*/ eps, 4549 }; 4550 4551 auto pipeline = ggml_metal_library_get_pipeline_l2_norm(lib, op); 4552 4553 if (pipeline.c4) { 4554 args.ne00 = ne00/4; 4555 args.ne0 = ne0/4; 4556 } 4557 4558 int nth = 32; // SIMD width 4559 4560 while (nth < ne00 && nth < ggml_metal_pipeline_max_theads_per_threadgroup(pipeline)) { 4561 nth *= 2; 4562 } 4563 4564 nth = std::min(nth, ggml_metal_pipeline_max_theads_per_threadgroup(pipeline)); 4565 4566 const size_t smem = pipeline.smem; 4567 4568 ggml_metal_encoder_set_pipeline(enc, pipeline); 4569 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 4570 ggml_metal_encoder_set_buffer (enc, bid_src0, 1); 4571 ggml_metal_encoder_set_buffer (enc, bid_dst, 2); 4572 4573 ggml_metal_encoder_set_threadgroup_memory_size(enc, smem, 0); 4574 4575 ggml_metal_encoder_dispatch_threadgroups(enc, ne01, ne02, ne03, nth, 1, 1); 4576 4577 return 1; 4578 } 4579 4580 int ggml_metal_op_group_norm(ggml_metal_op_t ctx, int idx) { 4581 ggml_tensor * op = ctx->node(idx); 4582 4583 ggml_metal_library_t lib = ctx->lib; 4584 ggml_metal_encoder_t enc = ctx->enc; 4585 4586 GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); 4587 GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); 4588 GGML_TENSOR_LOCALS( int32_t, ne, op, ne); 4589 GGML_TENSOR_LOCALS(uint64_t, nb, op, nb); 4590 4591 const int32_t ngrp = ((const int32_t *) op->op_params)[0]; 4592 4593 float eps; 4594 memcpy(&eps, op->op_params + 1, sizeof(float)); 4595 4596 ggml_metal_kargs_group_norm args = { 4597 /*.ne00 =*/ ne00, 4598 /*.ne01 =*/ ne01, 4599 /*.ne02 =*/ ne02, 4600 /*.nb00 =*/ nb00, 4601 /*.nb01 =*/ nb01, 4602 /*.nb02 =*/ nb02, 4603 /*.ngrp =*/ ngrp, 4604 /*.eps =*/ eps, 4605 }; 4606 4607 auto pipeline = ggml_metal_library_get_pipeline_group_norm(lib, op); 4608 4609 int nth = 32; // SIMD width 4610 //while (nth < ne00/4 && nth < ggml_metal_pipeline_max_theads_per_threadgroup(pipeline)) { 4611 // nth *= 2; 4612 //} 4613 4614 //nth = std::min(nth, ggml_metal_pipeline_max_theads_per_threadgroup(pipeline)); 4615 //nth = std::min(nth, ne00/4); 4616 4617 const size_t smem = pipeline.smem; 4618 4619 ggml_metal_encoder_set_pipeline(enc, pipeline); 4620 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 4621 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1); 4622 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 2); 4623 4624 ggml_metal_encoder_set_threadgroup_memory_size(enc, smem, 0); 4625 4626 ggml_metal_encoder_dispatch_threadgroups(enc, ngrp, 1, 1, nth, 1, 1); 4627 4628 return 1; 4629 } 4630 4631 int ggml_metal_op_norm(ggml_metal_op_t ctx, int idx) { 4632 ggml_tensor * op = ctx->node(idx); 4633 4634 ggml_metal_library_t lib = ctx->lib; 4635 ggml_metal_encoder_t enc = ctx->enc; 4636 4637 const bool use_fusion = ctx->use_fusion(); 4638 4639 const int debug_fusion = ggml_metal_fusion_info_debug(ctx->finfo); 4640 4641 GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); 4642 GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); 4643 GGML_TENSOR_LOCALS( int32_t, ne, op, ne); 4644 GGML_TENSOR_LOCALS(uint64_t, nb, op, nb); 4645 4646 float eps; 4647 memcpy(&eps, op->op_params, sizeof(float)); 4648 4649 ggml_metal_buffer_id bid_src0 = ggml_metal_get_buffer_id(op->src[0]); 4650 ggml_metal_buffer_id bid_dst = ggml_metal_get_buffer_id(op); 4651 4652 ggml_metal_kargs_norm args = { 4653 /*.ne00 =*/ ne00, 4654 /*.ne00_t =*/ ne00 % 4 == 0 ? ne00/4 : ne00, 4655 /*.nb1 =*/ nb1, 4656 /*.nb2 =*/ nb2, 4657 /*.nb3 =*/ nb3, 4658 /*.eps =*/ eps, 4659 /*.nef1 =*/ { ne01 }, 4660 /*.nef2 =*/ { ne02 }, 4661 /*.nef3 =*/ { ne03 }, 4662 /*.nbf1 =*/ { nb01 }, 4663 /*.nbf2 =*/ { nb02 }, 4664 /*.nbf3 =*/ { nb03 }, 4665 /*.scale =*/ 1.0f, 4666 }; 4667 4668 int n_fuse = 1; 4669 bool fused_norm_scale = false; 4670 4671 ggml_metal_buffer_id bid_fuse[2] = { bid_src0, bid_src0 }; 4672 4673 // d[0] = norm(a) 4674 // d[1] = mul(d[0], b) or scale(d[0]) 4675 // d[2] = add(d[1], c) 4676 if (use_fusion) { 4677 int n = 1; 4678 const ggml_metal_fusion * fusion = ctx->can_fuse(idx, GGML_METAL_FUSION_FULL, &n); 4679 4680 if (fusion && (ggml_metal_fusion_get_id(fusion) == GGML_METAL_FUSION_NORM_MUL || ggml_metal_fusion_get_id(fusion) == GGML_METAL_FUSION_NORM_MUL_ADD)) { 4681 n_fuse = n; 4682 4683 ctx->count_fusions(fusion); 4684 4685 for (int i = 1; i < n_fuse; i++) { 4686 const ggml_tensor * fn = ctx->node(idx + i); 4687 4688 bid_fuse[i - 1] = ggml_metal_get_buffer_id(fn->src[1]); 4689 4690 args.nef1[i] = fn->src[1]->ne[1]; 4691 args.nef2[i] = fn->src[1]->ne[2]; 4692 args.nef3[i] = fn->src[1]->ne[3]; 4693 4694 args.nbf1[i] = fn->src[1]->nb[1]; 4695 args.nbf2[i] = fn->src[1]->nb[2]; 4696 args.nbf3[i] = fn->src[1]->nb[3]; 4697 } 4698 4699 if (debug_fusion > 1) { 4700 if (n_fuse == 2) { 4701 GGML_LOG_DEBUG("%s: fuse: %s + MUL\n", __func__, ggml_op_name(op->op)); 4702 } 4703 if (n_fuse == 3) { 4704 GGML_LOG_DEBUG("%s: fuse: %s + MUL + ADD\n", __func__, ggml_op_name(op->op)); 4705 } 4706 } 4707 } 4708 4709 if (fusion && ggml_metal_fusion_get_id(fusion) == GGML_METAL_FUSION_NORM_SCALE) { 4710 n_fuse = n; 4711 fused_norm_scale = true; 4712 4713 ctx->count_fusions(fusion); 4714 4715 const ggml_tensor * scale_node = ctx->node(idx + 1); 4716 args.scale = ggml_get_op_params_f32(scale_node, 0); 4717 4718 if (debug_fusion > 1) { 4719 GGML_LOG_DEBUG("%s: fuse: %s + SCALE\n", __func__, ggml_op_name(op->op)); 4720 } 4721 } 4722 } 4723 4724 if (n_fuse > 1) { 4725 bid_dst = ggml_metal_get_buffer_id(ctx->node(idx + n_fuse - 1)); 4726 4727 for (int i = 1; i < n_fuse; ++i) { 4728 if (!ggml_metal_op_concurrency_check(ctx, ctx->node(idx + i))) { 4729 ggml_metal_op_concurrency_reset(ctx); 4730 4731 break; 4732 } 4733 } 4734 } 4735 4736 auto pipeline = fused_norm_scale ? 4737 ggml_metal_library_get_pipeline_norm_scale(lib, op) : 4738 ggml_metal_library_get_pipeline_norm(lib, op, n_fuse); 4739 4740 int nth = 32; // SIMD width 4741 4742 while (nth < args.ne00_t && nth < ggml_metal_pipeline_max_theads_per_threadgroup(pipeline)) { 4743 nth *= 2; 4744 } 4745 4746 nth = std::min(nth, ggml_metal_pipeline_max_theads_per_threadgroup(pipeline)); 4747 nth = std::min(nth, (args.ne00_t + 31)/32*32); 4748 4749 const size_t smem = pipeline.smem; 4750 4751 ggml_metal_encoder_set_pipeline(enc, pipeline); 4752 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 4753 ggml_metal_encoder_set_buffer (enc, bid_src0, 1); 4754 ggml_metal_encoder_set_buffer (enc, bid_fuse[0], 2); 4755 ggml_metal_encoder_set_buffer (enc, bid_fuse[1], 3); 4756 ggml_metal_encoder_set_buffer (enc, bid_dst, 4); 4757 4758 ggml_metal_encoder_set_threadgroup_memory_size(enc, smem, 0); 4759 4760 ggml_metal_encoder_dispatch_threadgroups(enc, ne01, ne02, ne03, nth, 1, 1); 4761 4762 return n_fuse; 4763 } 4764 4765 int ggml_metal_op_rope(ggml_metal_op_t ctx, int idx) { 4766 ggml_tensor * op = ctx->node(idx); 4767 4768 ggml_metal_library_t lib = ctx->lib; 4769 ggml_metal_encoder_t enc = ctx->enc; 4770 4771 GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); 4772 GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); 4773 GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne); 4774 GGML_TENSOR_LOCALS(uint64_t, nb1, op->src[1], nb); 4775 GGML_TENSOR_LOCALS( int32_t, ne, op, ne); 4776 GGML_TENSOR_LOCALS(uint64_t, nb, op, nb); 4777 4778 // make sure we have one or more position id(ne10) per token(ne02) 4779 GGML_ASSERT(ne10 % ne02 == 0); 4780 GGML_ASSERT(ne10 >= ne02); 4781 4782 const int nth = std::min(1024, ne00); 4783 4784 const int n_past = ((const int32_t *) op->op_params)[0]; 4785 const int n_dims = ((const int32_t *) op->op_params)[1]; 4786 //const int mode = ((const int32_t *) op->op_params)[2]; 4787 // skip 3, n_ctx, used in GLM RoPE, unimplemented in metal 4788 const int n_ctx_orig = ((const int32_t *) op->op_params)[4]; 4789 4790 float freq_base; 4791 float freq_scale; 4792 float ext_factor; 4793 float attn_factor; 4794 float beta_fast; 4795 float beta_slow; 4796 4797 memcpy(&freq_base, (const int32_t *) op->op_params + 5, sizeof(float)); 4798 memcpy(&freq_scale, (const int32_t *) op->op_params + 6, sizeof(float)); 4799 memcpy(&ext_factor, (const int32_t *) op->op_params + 7, sizeof(float)); 4800 memcpy(&attn_factor, (const int32_t *) op->op_params + 8, sizeof(float)); 4801 memcpy(&beta_fast, (const int32_t *) op->op_params + 9, sizeof(float)); 4802 memcpy(&beta_slow, (const int32_t *) op->op_params + 10, sizeof(float)); 4803 4804 // mrope 4805 const int sect_0 = ((const int32_t *) op->op_params)[11]; 4806 const int sect_1 = ((const int32_t *) op->op_params)[12]; 4807 const int sect_2 = ((const int32_t *) op->op_params)[13]; 4808 const int sect_3 = ((const int32_t *) op->op_params)[14]; 4809 4810 const int n_offs = ((const int32_t *) op->op_params)[15]; 4811 4812 // when dst aliases src0, the channels outside the rotated window already hold the correct data 4813 const bool inplace = op->data == op->src[0]->data; 4814 4815 ggml_metal_kargs_rope args = { 4816 /*.ne00 =*/ ne00, 4817 /*.ne01 =*/ ne01, 4818 /*.ne02 =*/ ne02, 4819 /*.ne03 =*/ ne03, 4820 /*.nb00 =*/ nb00, 4821 /*.nb01 =*/ nb01, 4822 /*.nb02 =*/ nb02, 4823 /*.nb03 =*/ nb03, 4824 /*.ne0 =*/ ne0, 4825 /*.ne1 =*/ ne1, 4826 /*.ne2 =*/ ne2, 4827 /*.ne3 =*/ ne3, 4828 /*.nb0 =*/ nb0, 4829 /*.nb1 =*/ nb1, 4830 /*.nb2 =*/ nb2, 4831 /*.nb3 =*/ nb3, 4832 /*.n_past =*/ n_past, 4833 /*.n_dims =*/ n_dims, 4834 /*.n_offs =*/ n_offs, 4835 /*.n_ctx_orig =*/ n_ctx_orig, 4836 /*.freq_base =*/ freq_base, 4837 /*.freq_scale =*/ freq_scale, 4838 /*.ext_factor =*/ ext_factor, 4839 /*.attn_factor =*/ attn_factor, 4840 /*.beta_fast =*/ beta_fast, 4841 /*.beta_slow =*/ beta_slow, 4842 /* sect_0 =*/ sect_0, 4843 /* sect_1 =*/ sect_1, 4844 /* sect_2 =*/ sect_2, 4845 /* sect_3 =*/ sect_3, 4846 /* src2 =*/ op->src[2] != nullptr, 4847 /* inplace =*/ inplace, 4848 }; 4849 4850 auto pipeline = ggml_metal_library_get_pipeline_rope(lib, op); 4851 4852 ggml_metal_encoder_set_pipeline(enc, pipeline); 4853 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 4854 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1); 4855 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[1]), 2); 4856 if (op->src[2]) { 4857 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[2]), 3); 4858 } else { 4859 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 3); 4860 } 4861 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 4); 4862 4863 ggml_metal_encoder_dispatch_threadgroups(enc, ne01, ne02, ne03, nth, 1, 1); 4864 4865 return 1; 4866 } 4867 4868 int ggml_metal_op_im2col(ggml_metal_op_t ctx, int idx) { 4869 ggml_tensor * op = ctx->node(idx); 4870 4871 ggml_metal_library_t lib = ctx->lib; 4872 ggml_metal_encoder_t enc = ctx->enc; 4873 4874 GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); 4875 GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); 4876 GGML_TENSOR_LOCALS( int32_t, ne, op, ne); 4877 GGML_TENSOR_LOCALS(uint64_t, nb, op, nb); 4878 4879 const int32_t s0 = ((const int32_t *)(op->op_params))[0]; 4880 const int32_t s1 = ((const int32_t *)(op->op_params))[1]; 4881 const int32_t p0 = ((const int32_t *)(op->op_params))[2]; 4882 const int32_t p1 = ((const int32_t *)(op->op_params))[3]; 4883 const int32_t d0 = ((const int32_t *)(op->op_params))[4]; 4884 const int32_t d1 = ((const int32_t *)(op->op_params))[5]; 4885 4886 const bool is_2D = ((const int32_t *)(op->op_params))[6] == 1; 4887 4888 const int32_t N = op->src[1]->ne[is_2D ? 3 : 2]; 4889 const int32_t IC = op->src[1]->ne[is_2D ? 2 : 1]; 4890 const int32_t IH = is_2D ? op->src[1]->ne[1] : 1; 4891 const int32_t IW = op->src[1]->ne[0]; 4892 4893 const int32_t KH = is_2D ? op->src[0]->ne[1] : 1; 4894 const int32_t KW = op->src[0]->ne[0]; 4895 4896 const int32_t OH = is_2D ? op->ne[2] : 1; 4897 const int32_t OW = op->ne[1]; 4898 4899 const int32_t CHW = IC * KH * KW; 4900 4901 const uint64_t ofs0 = op->src[1]->nb[is_2D ? 3 : 2] / 4; 4902 const uint64_t ofs1 = op->src[1]->nb[is_2D ? 2 : 1] / 4; 4903 4904 ggml_metal_kargs_im2col args = { 4905 /*.ofs0 =*/ ofs0, 4906 /*.ofs1 =*/ ofs1, 4907 /*.IW =*/ IW, 4908 /*.IH =*/ IH, 4909 /*.CHW =*/ CHW, 4910 /*.s0 =*/ s0, 4911 /*.s1 =*/ s1, 4912 /*.p0 =*/ p0, 4913 /*.p1 =*/ p1, 4914 /*.d0 =*/ d0, 4915 /*.d1 =*/ d1, 4916 /*.N =*/ N, 4917 /*.KH =*/ KH, 4918 /*.KW =*/ KW, 4919 /*.KHW =*/ KH * KW, 4920 }; 4921 4922 auto pipeline = ggml_metal_library_get_pipeline_im2col(lib, op); 4923 4924 if (KH*KW <= ggml_metal_pipeline_max_theads_per_threadgroup(pipeline)) { 4925 const uint64_t ntptg0 = std::min(ggml_metal_pipeline_max_theads_per_threadgroup(pipeline)/(KH*KW), N); 4926 4927 ggml_metal_encoder_set_pipeline(enc, pipeline); 4928 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 4929 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[1]), 1); 4930 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 2); 4931 4932 ggml_metal_encoder_dispatch_threadgroups(enc, IC, OH, OW, ntptg0, KH, KW); 4933 } else { 4934 const uint64_t n_threads = std::min(ggml_metal_pipeline_max_theads_per_threadgroup(pipeline), N); 4935 const int64_t quotient = N / n_threads + (N % n_threads > 0 ? 1 : 0); 4936 4937 ggml_metal_encoder_set_pipeline(enc, pipeline); 4938 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 4939 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[1]), 1); 4940 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 2); 4941 4942 ggml_metal_encoder_dispatch_threadgroups(enc, quotient * CHW, OH, OW, n_threads, 1, 1); 4943 } 4944 4945 return 1; 4946 } 4947 4948 int ggml_metal_op_conv_2d(ggml_metal_op_t ctx, int idx) { 4949 ggml_tensor * op = ctx->node(idx); 4950 4951 ggml_metal_library_t lib = ctx->lib; 4952 ggml_metal_encoder_t enc = ctx->enc; 4953 4954 GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); 4955 GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); 4956 GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne); 4957 GGML_TENSOR_LOCALS(uint64_t, nb1, op->src[1], nb); 4958 GGML_TENSOR_LOCALS( int32_t, ne, op, ne); 4959 GGML_TENSOR_LOCALS(uint64_t, nb, op, nb); 4960 4961 GGML_ASSERT(ggml_is_contiguous(op->src[0])); 4962 GGML_ASSERT(op->src[1]->type == GGML_TYPE_F32); 4963 GGML_ASSERT(op->type == GGML_TYPE_F32); 4964 GGML_ASSERT(op->src[0]->type == GGML_TYPE_F16 || op->src[0]->type == GGML_TYPE_F32); 4965 4966 const int32_t s0 = ((const int32_t *) op->op_params)[0]; 4967 const int32_t s1 = ((const int32_t *) op->op_params)[1]; 4968 const int32_t p0 = ((const int32_t *) op->op_params)[2]; 4969 const int32_t p1 = ((const int32_t *) op->op_params)[3]; 4970 const int32_t d0 = ((const int32_t *) op->op_params)[4]; 4971 const int32_t d1 = ((const int32_t *) op->op_params)[5]; 4972 4973 ggml_metal_kargs_conv_2d args = { 4974 /*.nb00 =*/ nb00, 4975 /*.nb01 =*/ nb01, 4976 /*.nb02 =*/ nb02, 4977 /*.nb03 =*/ nb03, 4978 /*.nb10 =*/ nb10, 4979 /*.nb11 =*/ nb11, 4980 /*.nb12 =*/ nb12, 4981 /*.nb13 =*/ nb13, 4982 /*.nb0 =*/ nb0, 4983 /*.nb1 =*/ nb1, 4984 /*.nb2 =*/ nb2, 4985 /*.nb3 =*/ nb3, 4986 /*.IW =*/ ne10, 4987 /*.IH =*/ ne11, 4988 /*.KW =*/ ne00, 4989 /*.KH =*/ ne01, 4990 /*.IC =*/ ne02, 4991 /*.OC =*/ ne03, 4992 /*.OW =*/ ne0, 4993 /*.OH =*/ ne1, 4994 /*.N =*/ ne3, 4995 /*.s0 =*/ s0, 4996 /*.s1 =*/ s1, 4997 /*.p0 =*/ p0, 4998 /*.p1 =*/ p1, 4999 /*.d0 =*/ d0, 5000 /*.d1 =*/ d1, 5001 }; 5002 5003 auto pipeline = ggml_metal_library_get_pipeline_conv_2d(lib, op); 5004 5005 int nth = ggml_metal_pipeline_max_theads_per_threadgroup(pipeline); 5006 nth = std::min(nth, 256); 5007 nth = std::max(nth, 1); 5008 5009 const uint64_t n_out = ggml_nelements(op); 5010 5011 uint64_t tg = (n_out + nth - 1)/nth; 5012 tg = std::max(tg, 1); 5013 tg = std::min(tg, (uint64_t) std::numeric_limits::max()); 5014 5015 ggml_metal_encoder_set_pipeline(enc, pipeline); 5016 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 5017 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1); 5018 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[1]), 2); 5019 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 3); 5020 5021 ggml_metal_encoder_dispatch_threadgroups(enc, tg, 1, 1, nth, 1, 1); 5022 5023 return 1; 5024 } 5025 5026 int ggml_metal_op_conv_2d_dw(ggml_metal_op_t ctx, int idx) { 5027 ggml_tensor * op = ctx->node(idx); 5028 5029 ggml_metal_library_t lib = ctx->lib; 5030 ggml_metal_encoder_t enc = ctx->enc; 5031 5032 GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); 5033 GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); 5034 GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne); 5035 GGML_TENSOR_LOCALS(uint64_t, nb1, op->src[1], nb); 5036 GGML_TENSOR_LOCALS( int32_t, ne, op, ne); 5037 GGML_TENSOR_LOCALS(uint64_t, nb, op, nb); 5038 5039 GGML_ASSERT(op->src[1]->type == GGML_TYPE_F32); 5040 GGML_ASSERT(op->type == GGML_TYPE_F32); 5041 GGML_ASSERT(op->src[0]->type == GGML_TYPE_F16 || op->src[0]->type == GGML_TYPE_F32); 5042 5043 const int32_t s0 = ((const int32_t *) op->op_params)[0]; 5044 const int32_t s1 = ((const int32_t *) op->op_params)[1]; 5045 const int32_t p0 = ((const int32_t *) op->op_params)[2]; 5046 const int32_t p1 = ((const int32_t *) op->op_params)[3]; 5047 const int32_t d0 = ((const int32_t *) op->op_params)[4]; 5048 const int32_t d1 = ((const int32_t *) op->op_params)[5]; 5049 5050 ggml_metal_kargs_conv_2d_dw args = { 5051 /*.nb00 =*/ nb00, 5052 /*.nb01 =*/ nb01, 5053 /*.nb02 =*/ nb03, 5054 /*.nb10 =*/ nb10, 5055 /*.nb11 =*/ nb11, 5056 /*.nb12 =*/ nb12, 5057 /*.nb13 =*/ nb13, 5058 /*.nb0 =*/ nb0, 5059 /*.nb1 =*/ nb1, 5060 /*.nb2 =*/ nb2, 5061 /*.nb3 =*/ nb3, 5062 /*.IW =*/ ne10, 5063 /*.IH =*/ ne11, 5064 /*.KW =*/ ne00, 5065 /*.KH =*/ ne01, 5066 /*.C =*/ ne12, 5067 /*.OW =*/ ne0, 5068 /*.OH =*/ ne1, 5069 /*.N =*/ ne13, 5070 /*.s0 =*/ s0, 5071 /*.s1 =*/ s1, 5072 /*.p0 =*/ p0, 5073 /*.p1 =*/ p1, 5074 /*.d0 =*/ d0, 5075 /*.d1 =*/ d1, 5076 }; 5077 5078 const bool use_tiled = (nb12 < nb10); 5079 5080 auto pipeline = ggml_metal_library_get_pipeline_conv_2d_dw(lib, op, use_tiled); 5081 5082 int nth = ggml_metal_pipeline_max_theads_per_threadgroup(pipeline); 5083 nth = std::min(nth, 256); 5084 nth = std::max(nth, 1); 5085 5086 const int32_t OW = ne0; 5087 const int32_t OH = ne1; 5088 const int32_t C = ne12; 5089 const int32_t N = ne13; 5090 5091 const int tg_x = use_tiled ? (C + nth - 1) / nth : (OW + nth - 1) / nth; 5092 const int tg_y = OH; 5093 const int tg_z = use_tiled ? OW * N : C * N; 5094 5095 ggml_metal_encoder_set_pipeline(enc, pipeline); 5096 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 5097 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1); 5098 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[1]), 2); 5099 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 3); 5100 5101 ggml_metal_encoder_dispatch_threadgroups(enc, tg_x, tg_y, tg_z, nth, 1, 1); 5102 5103 return 1; 5104 } 5105 5106 int ggml_metal_op_conv_3d(ggml_metal_op_t ctx, int idx) { 5107 ggml_tensor * op = ctx->node(idx); 5108 5109 ggml_metal_library_t lib = ctx->lib; 5110 ggml_metal_encoder_t enc = ctx->enc; 5111 5112 // 1. Extract standard dimensions and byte strides 5113 GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); 5114 GGML_TENSOR_LOCALS(uint64_t, nb1, op->src[1], nb); 5115 GGML_TENSOR_LOCALS(uint64_t, nb, op, nb); 5116 5117 // 2. Extract hyperparams from op_params 5118 const int32_t s0 = ((const int32_t *)(op->op_params))[0]; 5119 const int32_t s1 = ((const int32_t *)(op->op_params))[1]; 5120 const int32_t s2 = ((const int32_t *)(op->op_params))[2]; 5121 const int32_t p0 = ((const int32_t *)(op->op_params))[3]; 5122 const int32_t p1 = ((const int32_t *)(op->op_params))[4]; 5123 const int32_t p2 = ((const int32_t *)(op->op_params))[5]; 5124 const int32_t d0 = ((const int32_t *)(op->op_params))[6]; 5125 const int32_t d1 = ((const int32_t *)(op->op_params))[7]; 5126 const int32_t d2 = ((const int32_t *)(op->op_params))[8]; 5127 const int32_t IC = ((const int32_t *)(op->op_params))[9]; 5128 const int32_t N = ((const int32_t *)(op->op_params))[10]; 5129 const int32_t OC = ((const int32_t *)(op->op_params))[11]; 5130 5131 // 3. Build the parameter struct using the macro-generated variables 5132 ggml_metal_kargs_conv_3d args = { 5133 /*.IW =*/ (int32_t)op->src[1]->ne[0], 5134 /*.IH =*/ (int32_t)op->src[1]->ne[1], 5135 /*.ID =*/ (int32_t)op->src[1]->ne[2], 5136 /*.OW =*/ (int32_t)op->ne[0], 5137 /*.OH =*/ (int32_t)op->ne[1], 5138 /*.OD =*/ (int32_t)op->ne[2], 5139 /*.KW =*/ (int32_t)op->src[0]->ne[0], 5140 /*.KH =*/ (int32_t)op->src[0]->ne[1], 5141 /*.KD =*/ (int32_t)op->src[0]->ne[2], 5142 s0, s1, s2, 5143 p0, p1, p2, 5144 d0, d1, d2, 5145 IC, N, OC, 5146 nb00, nb01, nb02, nb03, // Weight strides 5147 nb10, nb11, nb12, nb13, // Input strides 5148 nb0, nb1, nb2, nb3 // Output strides 5149 }; 5150 5151 // 4. Fetch the JIT pipeline 5152 auto pipeline = ggml_metal_library_get_pipeline_conv_3d(lib, op); 5153 5154 // 5. Grid mapping 5155 int nth0 = 32; // Standard SIMD width for Apple Silicon 5156 int nth1 = 1; 5157 int nth2 = 1; 5158 5159 int64_t spatial_volume = args.OW * args.OH * args.OD; 5160 5161 int ntg0 = (spatial_volume + nth0 - 1) / nth0; 5162 int ntg1 = args.OC; 5163 int ntg2 = args.N; 5164 5165 // 6. Bind and Dispatch via the ggml C wrapper 5166 ggml_metal_encoder_set_pipeline(enc, pipeline); 5167 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 5168 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1); 5169 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[1]), 2); 5170 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 3); 5171 5172 ggml_metal_encoder_dispatch_threadgroups(enc, ntg0, ntg1, ntg2, nth0, nth1, nth2); 5173 5174 return 1; 5175 } 5176 5177 int ggml_metal_op_conv_transpose_1d(ggml_metal_op_t ctx, int idx) { 5178 ggml_tensor * op = ctx->node(idx); 5179 5180 ggml_metal_library_t lib = ctx->lib; 5181 ggml_metal_encoder_t enc = ctx->enc; 5182 5183 GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); 5184 GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); 5185 GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne); 5186 GGML_TENSOR_LOCALS(uint64_t, nb1, op->src[1], nb); 5187 GGML_TENSOR_LOCALS( int32_t, ne, op, ne); 5188 GGML_TENSOR_LOCALS(uint64_t, nb, op, nb); 5189 5190 const int32_t s0 = ((const int32_t *)(op->op_params))[0]; 5191 5192 const int32_t IC = op->src[1]->ne[1]; 5193 const int32_t IL = op->src[1]->ne[0]; 5194 5195 const int32_t K = op->src[0]->ne[0]; 5196 5197 const int32_t OL = op->ne[0]; 5198 const int32_t OC = op->ne[1]; 5199 5200 ggml_metal_kargs_conv_transpose_1d args = { 5201 /*.IC =*/ IC, 5202 /*.IL =*/ IL, 5203 /*.K =*/ K, 5204 /*.s0 =*/ s0, 5205 /*.nb0 =*/ nb0, 5206 /*.nb1 =*/ nb1, 5207 }; 5208 5209 auto pipeline = ggml_metal_library_get_pipeline_conv_transpose_1d(lib, op); 5210 5211 ggml_metal_encoder_set_pipeline(enc, pipeline); 5212 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 5213 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1); 5214 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[1]), 2); 5215 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 3); 5216 5217 ggml_metal_encoder_dispatch_threadgroups(enc, OL, OC, 1, 1, 1, 1); 5218 5219 return 1; 5220 } 5221 5222 int ggml_metal_op_col2im_1d(ggml_metal_op_t ctx, int idx) { 5223 ggml_tensor * op = ctx->node(idx); 5224 5225 ggml_metal_library_t lib = ctx->lib; 5226 ggml_metal_encoder_t enc = ctx->enc; 5227 5228 const int32_t s0 = ((const int32_t *)(op->op_params))[0]; 5229 const int32_t OC = ((const int32_t *)(op->op_params))[1]; 5230 const int32_t p0 = ((const int32_t *)(op->op_params))[2]; 5231 5232 const int32_t K_OC = (int32_t) op->src[0]->ne[0]; 5233 const int32_t T_in = (int32_t) op->src[0]->ne[1]; 5234 const int32_t K = K_OC / OC; 5235 const int32_t T_out = (int32_t) op->ne[0]; 5236 5237 ggml_metal_kargs_col2im_1d args = { 5238 /*.T_in =*/ T_in, 5239 /*.T_out =*/ T_out, 5240 /*.OC =*/ OC, 5241 /*.K =*/ K, 5242 /*.K_OC =*/ K_OC, 5243 /*.s0 =*/ s0, 5244 /*.p0 =*/ p0, 5245 }; 5246 5247 auto pipeline = ggml_metal_library_get_pipeline_col2im_1d(lib, op); 5248 5249 const int total = T_out * OC; 5250 const int nth = 256; 5251 const int ntg = (total + nth - 1) / nth; 5252 5253 ggml_metal_encoder_set_pipeline(enc, pipeline); 5254 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 5255 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1); 5256 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 2); 5257 5258 ggml_metal_encoder_dispatch_threadgroups(enc, ntg, 1, 1, nth, 1, 1); 5259 5260 return 1; 5261 } 5262 5263 // Dispatch the fused snake kernel from the matched mul -> sin -> sqr -> mul -> add chain. 5264 // idx points at the leading mul. The caller has validated the chain. 5265 int ggml_metal_op_snake_fused(ggml_metal_op_t ctx, int idx) { 5266 ggml_metal_library_t lib = ctx->lib; 5267 ggml_metal_encoder_t enc = ctx->enc; 5268 5269 const ggml_tensor * mul0 = ctx->node(idx + 0); 5270 const ggml_tensor * sqr = ctx->node(idx + 2); 5271 const ggml_tensor * mul1 = ctx->node(idx + 3); 5272 ggml_tensor * add = ctx->node(idx + 4); 5273 5274 const ggml_tensor * x = ggml_are_same_shape(mul0, mul0->src[0]) ? mul0->src[0] : mul0->src[1]; 5275 const ggml_tensor * a = (x == mul0->src[0]) ? mul0->src[1] : mul0->src[0]; 5276 const ggml_tensor * inv_b = (mul1->src[0] == sqr) ? mul1->src[1] : mul1->src[0]; 5277 5278 const int T = (int) x->ne[0]; 5279 const int C = (int) x->ne[1]; 5280 const int total = T * C; 5281 5282 // the encode loop pre-checked the leading mul only, check the rest of the chain 5283 for (int i = 1; i < 5; ++i) { 5284 if (!ggml_metal_op_concurrency_check(ctx, ctx->node(idx + i))) { 5285 ggml_metal_op_concurrency_reset(ctx); 5286 5287 break; 5288 } 5289 } 5290 5291 auto pipeline = ggml_metal_library_get_pipeline_snake(lib, x->type); 5292 5293 ggml_metal_kargs_snake args = { 5294 /*.T =*/ T, 5295 /*.C =*/ C, 5296 }; 5297 5298 ggml_metal_encoder_set_pipeline(enc, pipeline); 5299 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 5300 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(x), 1); 5301 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(a), 2); 5302 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(inv_b), 3); 5303 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(add), 4); 5304 5305 const int nth = 256; 5306 const int ntg = (total + nth - 1) / nth; 5307 ggml_metal_encoder_dispatch_threadgroups(enc, ntg, 1, 1, nth, 1, 1); 5308 5309 return 5; 5310 } 5311 5312 int ggml_metal_op_conv_transpose_2d(ggml_metal_op_t ctx, int idx) { 5313 ggml_tensor * op = ctx->node(idx); 5314 5315 ggml_metal_library_t lib = ctx->lib; 5316 ggml_metal_encoder_t enc = ctx->enc; 5317 5318 GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); 5319 GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); 5320 GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne); 5321 GGML_TENSOR_LOCALS(uint64_t, nb1, op->src[1], nb); 5322 GGML_TENSOR_LOCALS( int32_t, ne, op, ne); 5323 GGML_TENSOR_LOCALS(uint64_t, nb, op, nb); 5324 5325 const int32_t s0 = ((const int32_t *)(op->op_params))[0]; 5326 5327 const int32_t IC = op->src[1]->ne[2]; 5328 const int32_t IH = op->src[1]->ne[1]; 5329 const int32_t IW = op->src[1]->ne[0]; 5330 5331 const int32_t KH = op->src[0]->ne[1]; 5332 const int32_t KW = op->src[0]->ne[0]; 5333 5334 const int32_t OW = op->ne[0]; 5335 const int32_t OH = op->ne[1]; 5336 const int32_t OC = op->ne[2]; 5337 const int32_t N = op->src[1]->ne[3]; 5338 5339 ggml_metal_kargs_conv_transpose_2d args = { 5340 /*.IC =*/ IC, 5341 /*.IH =*/ IH, 5342 /*.IW =*/ IW, 5343 /*.KH =*/ KH, 5344 /*.KW =*/ KW, 5345 /*.OC =*/ OC, 5346 /*.s0 =*/ s0, 5347 /*.nb0 =*/ nb0, 5348 /*.nb1 =*/ nb1, 5349 /*.nb2 =*/ nb2, 5350 /*.nb3 =*/ nb3, 5351 }; 5352 5353 auto pipeline = ggml_metal_library_get_pipeline_conv_transpose_2d(lib, op); 5354 5355 ggml_metal_encoder_set_pipeline(enc, pipeline); 5356 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 5357 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1); 5358 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[1]), 2); 5359 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 3); 5360 5361 // Metal requires buffer size to be multiple of 16 bytes 5362 const size_t smem = GGML_PAD(KW * KH * sizeof(float), 16); 5363 ggml_metal_encoder_set_threadgroup_memory_size(enc, smem, 0); 5364 5365 ggml_metal_encoder_dispatch_threadgroups(enc, OW, OH, OC * N, KW, KH, 1); 5366 5367 return 1; 5368 } 5369 5370 int ggml_metal_op_upscale(ggml_metal_op_t ctx, int idx) { 5371 ggml_tensor * op = ctx->node(idx); 5372 5373 ggml_metal_library_t lib = ctx->lib; 5374 ggml_metal_encoder_t enc = ctx->enc; 5375 5376 GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); 5377 GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); 5378 GGML_TENSOR_LOCALS( int32_t, ne, op, ne); 5379 GGML_TENSOR_LOCALS(uint64_t, nb, op, nb); 5380 5381 float sf0 = (float)ne0/op->src[0]->ne[0]; 5382 float sf1 = (float)ne1/op->src[0]->ne[1]; 5383 float sf2 = (float)ne2/op->src[0]->ne[2]; 5384 float sf3 = (float)ne3/op->src[0]->ne[3]; 5385 5386 const int32_t mode_flags = ggml_get_op_params_i32(op, 0); 5387 5388 float poffs = 0.5f; 5389 5390 if (mode_flags & GGML_SCALE_FLAG_ALIGN_CORNERS) { 5391 poffs = 0.0f; 5392 sf0 = ne0 > 1 && ne00 > 1 ? (float)(ne0 - 1) / (ne00 - 1) : sf0; 5393 sf1 = ne1 > 1 && ne01 > 1 ? (float)(ne1 - 1) / (ne01 - 1) : sf1; 5394 } 5395 5396 ggml_metal_kargs_upscale args = { 5397 /*.ne00 =*/ ne00, 5398 /*.ne01 =*/ ne01, 5399 /*.ne02 =*/ ne02, 5400 /*.ne03 =*/ ne03, 5401 /*.nb00 =*/ nb00, 5402 /*.nb01 =*/ nb01, 5403 /*.nb02 =*/ nb02, 5404 /*.nb03 =*/ nb03, 5405 /*.ne0 =*/ ne0, 5406 /*.ne1 =*/ ne1, 5407 /*.ne2 =*/ ne2, 5408 /*.ne3 =*/ ne3, 5409 /*.nb0 =*/ nb0, 5410 /*.nb1 =*/ nb1, 5411 /*.nb2 =*/ nb2, 5412 /*.nb3 =*/ nb3, 5413 /*.sf0 =*/ sf0, 5414 /*.sf1 =*/ sf1, 5415 /*.sf2 =*/ sf2, 5416 /*.sf3 =*/ sf3, 5417 /*.poffs =*/ poffs, 5418 }; 5419 5420 auto pipeline = ggml_metal_library_get_pipeline_upscale(lib, op); 5421 5422 const int nth = std::min(ggml_metal_pipeline_max_theads_per_threadgroup(pipeline), ne0); 5423 5424 ggml_metal_encoder_set_pipeline(enc, pipeline); 5425 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 5426 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1); 5427 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 2); 5428 5429 ggml_metal_encoder_dispatch_threadgroups(enc, ne1, ne2, ne3, nth, 1, 1); 5430 5431 return 1; 5432 } 5433 5434 int ggml_metal_op_roll(ggml_metal_op_t ctx, int idx) { 5435 ggml_tensor * op = ctx->node(idx); 5436 5437 ggml_metal_library_t lib = ctx->lib; 5438 ggml_metal_encoder_t enc = ctx->enc; 5439 5440 GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); 5441 GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); 5442 GGML_TENSOR_LOCALS( int32_t, ne, op, ne); 5443 GGML_TENSOR_LOCALS(uint64_t, nb, op, nb); 5444 5445 const int32_t s0 = ggml_get_op_params_i32(op, 0); 5446 const int32_t s1 = ggml_get_op_params_i32(op, 1); 5447 const int32_t s2 = ggml_get_op_params_i32(op, 2); 5448 const int32_t s3 = ggml_get_op_params_i32(op, 3); 5449 5450 ggml_metal_kargs_roll args = { 5451 /*.ne00 =*/ ne00, 5452 /*.ne01 =*/ ne01, 5453 /*.ne02 =*/ ne02, 5454 /*.ne03 =*/ ne03, 5455 /*.nb00 =*/ nb00, 5456 /*.nb01 =*/ nb01, 5457 /*.nb02 =*/ nb02, 5458 /*.nb03 =*/ nb03, 5459 /*.ne0 =*/ ne0, 5460 /*.ne1 =*/ ne1, 5461 /*.ne2 =*/ ne2, 5462 /*.ne3 =*/ ne3, 5463 /*.nb0 =*/ nb0, 5464 /*.nb1 =*/ nb1, 5465 /*.nb2 =*/ nb2, 5466 /*.nb3 =*/ nb3, 5467 /*.s0 =*/ s0, 5468 /*.s1 =*/ s1, 5469 /*.s2 =*/ s2, 5470 /*.s3 =*/ s3 5471 }; 5472 5473 auto pipeline = ggml_metal_library_get_pipeline_roll(lib, op); 5474 5475 const int nth = std::min(1024, ne0); 5476 5477 ggml_metal_encoder_set_pipeline(enc, pipeline); 5478 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 5479 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1); 5480 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 2); 5481 5482 ggml_metal_encoder_dispatch_threadgroups(enc, ne1, ne2, ne3, nth, 1, 1); 5483 5484 return 1; 5485 } 5486 5487 int ggml_metal_op_pad(ggml_metal_op_t ctx, int idx) { 5488 ggml_tensor * op = ctx->node(idx); 5489 5490 ggml_metal_library_t lib = ctx->lib; 5491 ggml_metal_encoder_t enc = ctx->enc; 5492 5493 GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); 5494 GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); 5495 GGML_TENSOR_LOCALS( int32_t, ne, op, ne); 5496 GGML_TENSOR_LOCALS(uint64_t, nb, op, nb); 5497 5498 ggml_metal_kargs_pad args = { 5499 /*.ne00 =*/ ne00, 5500 /*.ne01 =*/ ne01, 5501 /*.ne02 =*/ ne02, 5502 /*.ne03 =*/ ne03, 5503 /*.nb00 =*/ nb00, 5504 /*.nb01 =*/ nb01, 5505 /*.nb02 =*/ nb02, 5506 /*.nb03 =*/ nb03, 5507 /*.ne0 =*/ ne0, 5508 /*.ne1 =*/ ne1, 5509 /*.ne2 =*/ ne2, 5510 /*.ne3 =*/ ne3, 5511 /*.nb0 =*/ nb0, 5512 /*.nb1 =*/ nb1, 5513 /*.nb2 =*/ nb2, 5514 /*.nb3 =*/ nb3, 5515 /*.lp0 =*/ ggml_get_op_params_i32(op, 0), 5516 /*.lp1 =*/ ggml_get_op_params_i32(op, 2), 5517 /*.lp2 =*/ ggml_get_op_params_i32(op, 4), 5518 /*.lp3 =*/ ggml_get_op_params_i32(op, 6), 5519 }; 5520 5521 auto pipeline = ggml_metal_library_get_pipeline_pad(lib, op); 5522 5523 const int nth_max = MIN(64, ggml_metal_pipeline_max_theads_per_threadgroup(pipeline)); 5524 const int nth = MIN(args.ne0, nth_max); 5525 const int nk0 = (args.ne0 + 1024 - 1)/1024; // note: 1024 is hardcoded in the kernel! 5526 5527 ggml_metal_encoder_set_pipeline(enc, pipeline); 5528 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 5529 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1); 5530 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 2); 5531 5532 ggml_metal_encoder_dispatch_threadgroups(enc, nk0*ne1, ne2, ne3, nth, 1, 1); 5533 5534 return 1; 5535 } 5536 5537 int ggml_metal_op_pad_reflect_1d(ggml_metal_op_t ctx, int idx) { 5538 ggml_tensor * op = ctx->node(idx); 5539 5540 ggml_metal_library_t lib = ctx->lib; 5541 ggml_metal_encoder_t enc = ctx->enc; 5542 5543 GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); 5544 GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); 5545 GGML_TENSOR_LOCALS( int32_t, ne, op, ne); 5546 GGML_TENSOR_LOCALS(uint64_t, nb, op, nb); 5547 5548 ggml_metal_kargs_pad_reflect_1d args = { 5549 /*.ne00 =*/ ne00, 5550 /*.ne01 =*/ ne01, 5551 /*.ne02 =*/ ne02, 5552 /*.ne03 =*/ ne03, 5553 /*.nb00 =*/ nb00, 5554 /*.nb01 =*/ nb01, 5555 /*.nb02 =*/ nb02, 5556 /*.nb03 =*/ nb03, 5557 /*.ne0 =*/ ne0, 5558 /*.ne1 =*/ ne1, 5559 /*.ne2 =*/ ne2, 5560 /*.ne3 =*/ ne3, 5561 /*.nb0 =*/ nb0, 5562 /*.nb1 =*/ nb1, 5563 /*.nb2 =*/ nb2, 5564 /*.nb3 =*/ nb3, 5565 /*.p0 =*/ ((const int32_t *)(op->op_params))[0], 5566 /*.p1 =*/ ((const int32_t *)(op->op_params))[1] 5567 }; 5568 5569 auto pipeline = ggml_metal_library_get_pipeline_pad_reflect_1d(lib, op); 5570 5571 const int nth = std::min(1024, ne0); 5572 5573 ggml_metal_encoder_set_pipeline(enc, pipeline); 5574 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 5575 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1); 5576 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 2); 5577 5578 ggml_metal_encoder_dispatch_threadgroups(enc, ne1, ne2, ne3, nth, 1, 1); 5579 5580 return 1; 5581 } 5582 5583 int ggml_metal_op_arange(ggml_metal_op_t ctx, int idx) { 5584 ggml_tensor * op = ctx->node(idx); 5585 5586 ggml_metal_library_t lib = ctx->lib; 5587 ggml_metal_encoder_t enc = ctx->enc; 5588 5589 GGML_TENSOR_LOCALS( int32_t, ne, op, ne); 5590 GGML_TENSOR_LOCALS(uint64_t, nb, op, nb); 5591 5592 float start; 5593 float step; 5594 5595 memcpy(&start, ((const int32_t *) op->op_params) + 0, sizeof(float)); 5596 memcpy(&step, ((const int32_t *) op->op_params) + 2, sizeof(float)); 5597 5598 ggml_metal_kargs_arange args = { 5599 /*.ne0 =*/ ne0, 5600 /*.start =*/ start, 5601 /*.step =*/ step 5602 }; 5603 5604 const int nth = std::min(1024, ne0); 5605 5606 auto pipeline = ggml_metal_library_get_pipeline_arange(lib, op); 5607 5608 ggml_metal_encoder_set_pipeline(enc, pipeline); 5609 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 5610 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 1); 5611 5612 ggml_metal_encoder_dispatch_threadgroups(enc, 1, 1, 1, nth, 1, 1); 5613 5614 return 1; 5615 } 5616 5617 int ggml_metal_op_timestep_embedding(ggml_metal_op_t ctx, int idx) { 5618 ggml_tensor * op = ctx->node(idx); 5619 5620 ggml_metal_library_t lib = ctx->lib; 5621 ggml_metal_encoder_t enc = ctx->enc; 5622 5623 GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); 5624 GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); 5625 GGML_TENSOR_LOCALS( int32_t, ne, op, ne); 5626 GGML_TENSOR_LOCALS(uint64_t, nb, op, nb); 5627 5628 const int dim = op->op_params[0]; 5629 const int max_period = op->op_params[1]; 5630 5631 ggml_metal_kargs_timestep_embedding args = { 5632 /*.nb1 =*/ nb1, 5633 /*.dim =*/ dim, 5634 /*.max_period =*/ max_period, 5635 }; 5636 5637 auto pipeline = ggml_metal_library_get_pipeline_timestep_embedding(lib, op); 5638 5639 const int nth = std::max(1, std::min(1024, dim/2)); 5640 5641 ggml_metal_encoder_set_pipeline(enc, pipeline); 5642 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 5643 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1); 5644 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 2); 5645 5646 ggml_metal_encoder_dispatch_threadgroups(enc, ne00, 1, 1, nth, 1, 1); 5647 5648 return 1; 5649 } 5650 5651 size_t ggml_metal_op_argmax_extra(const ggml_tensor * op) { 5652 assert(op->op == GGML_OP_ARGMAX); 5653 5654 const int64_t ne00 = op->src[0]->ne[0]; 5655 const int64_t ne01 = op->src[0]->ne[1]; 5656 5657 int npart = 1; 5658 if (ne00 > N_ARGMAX_CHUNK) { 5659 npart = (int) std::min(N_ARGMAX_NPART_MAX, (ne00 + N_ARGMAX_CHUNK - 1)/N_ARGMAX_CHUNK); 5660 } 5661 5662 if (npart == 1) { 5663 return 0; 5664 } 5665 5666 // partial max + partial argmax for every row 5667 return (sizeof(float) + sizeof(int32_t)) * npart * ne01; 5668 } 5669 5670 int ggml_metal_op_argmax(ggml_metal_op_t ctx, int idx) { 5671 ggml_tensor * op = ctx->node(idx); 5672 5673 ggml_metal_library_t lib = ctx->lib; 5674 ggml_metal_encoder_t enc = ctx->enc; 5675 5676 GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); 5677 GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); 5678 GGML_TENSOR_LOCALS( int32_t, ne, op, ne); 5679 GGML_TENSOR_LOCALS(uint64_t, nb, op, nb); 5680 5681 const int64_t nrows = ggml_nrows(op->src[0]); 5682 5683 int npart = 1; 5684 if (ne00 > N_ARGMAX_CHUNK) { 5685 npart = (int) std::min(N_ARGMAX_NPART_MAX, (ne00 + N_ARGMAX_CHUNK - 1)/N_ARGMAX_CHUNK); 5686 } 5687 5688 ggml_metal_kargs_argmax args = { 5689 /*.ne00 = */ ne00, 5690 /*.nb01 = */ nb01, 5691 /*.npart = */ npart, 5692 /*.ne01 = */ ne01, 5693 }; 5694 5695 const size_t smem = 32*(sizeof(float) + sizeof(int32_t)); 5696 5697 if (npart > 1) { 5698 auto pipeline_part = ggml_metal_library_get_pipeline_argmax_part(lib); 5699 auto pipeline_final = ggml_metal_library_get_pipeline_argmax_final(lib); 5700 5701 ggml_metal_buffer_id bid_dst = ggml_metal_get_buffer_id(op); 5702 ggml_metal_buffer_id bid_tmp = bid_dst; 5703 bid_tmp.offs += ggml_nbytes(op); 5704 5705 ggml_metal_encoder_set_pipeline(enc, pipeline_part); 5706 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 5707 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1); 5708 ggml_metal_encoder_set_buffer (enc, bid_tmp, 2); 5709 ggml_metal_encoder_set_threadgroup_memory_size(enc, smem, 0); 5710 ggml_metal_encoder_dispatch_threadgroups(enc, npart, nrows, 1, 256, 1, 1); 5711 5712 ggml_metal_op_concurrency_reset(ctx); 5713 5714 int nth = 32; // SIMD width 5715 while (nth < npart && nth < 256) { 5716 nth *= 2; 5717 } 5718 5719 ggml_metal_encoder_set_pipeline(enc, pipeline_final); 5720 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 5721 ggml_metal_encoder_set_buffer (enc, bid_tmp, 1); 5722 ggml_metal_encoder_set_buffer (enc, bid_dst, 2); 5723 ggml_metal_encoder_set_threadgroup_memory_size(enc, smem, 0); 5724 ggml_metal_encoder_dispatch_threadgroups(enc, nrows, 1, 1, nth, 1, 1); 5725 5726 return 1; 5727 } 5728 5729 auto pipeline = ggml_metal_library_get_pipeline_argmax(lib, op); 5730 5731 int nth = 32; // SIMD width 5732 while (nth < ne00 && nth*ne01*ne02*ne03 < 256) { 5733 nth *= 2; 5734 } 5735 5736 ggml_metal_encoder_set_pipeline(enc, pipeline); 5737 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 5738 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1); 5739 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 2); 5740 5741 ggml_metal_encoder_set_threadgroup_memory_size(enc, smem, 0); 5742 5743 ggml_metal_encoder_dispatch_threadgroups(enc, nrows, 1, 1, nth, 1, 1); 5744 5745 return 1; 5746 } 5747 5748 int ggml_metal_op_argsort(ggml_metal_op_t ctx, int idx) { 5749 ggml_tensor * op = ctx->node(idx); 5750 5751 ggml_metal_library_t lib = ctx->lib; 5752 ggml_metal_encoder_t enc = ctx->enc; 5753 5754 GGML_ASSERT(ggml_is_contiguous_rows(op->src[0])); 5755 5756 GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); 5757 GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); 5758 GGML_TENSOR_LOCALS( int32_t, ne, op, ne); 5759 GGML_TENSOR_LOCALS(uint64_t, nb, op, nb); 5760 5761 auto pipeline = ggml_metal_library_get_pipeline_argsort(lib, op); 5762 5763 // bitonic sort requires the number of elements to be power of 2 5764 int nth = 1; 5765 while (nth < ne00 && 2*nth <= ggml_metal_pipeline_max_theads_per_threadgroup(pipeline)) { 5766 nth *= 2; 5767 } 5768 5769 const int npr = (ne00 + nth - 1)/nth; 5770 5771 // Metal kernels require the buffer size to be multiple of 16 bytes 5772 // https://developer.apple.com/documentation/metal/mtlcomputecommandencoder/`1443142`-setthreadgroupmemorylength 5773 const size_t smem = GGML_PAD(nth*sizeof(int32_t), 16); 5774 5775 ggml_metal_buffer_id bid_src0 = ggml_metal_get_buffer_id(op->src[0]); 5776 ggml_metal_buffer_id bid_dst = ggml_metal_get_buffer_id(op); 5777 5778 ggml_metal_buffer_id bid_tmp = bid_dst; 5779 bid_tmp.offs += ggml_nbytes(op); 5780 5781 if ((int) ceil(std::log(npr) / std::log(2)) % 2 == 1) { 5782 std::swap(bid_dst, bid_tmp); 5783 } 5784 5785 ggml_metal_kargs_argsort args = { 5786 /*.ne00 =*/ ne00, 5787 /*.ne01 =*/ ne01, 5788 /*.ne02 =*/ ne02, 5789 /*.ne03 =*/ ne03, 5790 /*.nb00 =*/ nb00, 5791 /*.nb01 =*/ nb01, 5792 /*.nb02 =*/ nb02, 5793 /*.nb03 =*/ nb03, 5794 /*.ne0 =*/ ne0, 5795 /*.ne1 =*/ ne1, 5796 /*.ne2 =*/ ne2, 5797 /*.ne3 =*/ ne3, 5798 /*.top_k =*/ nth, 5799 }; 5800 5801 ggml_metal_encoder_set_pipeline(enc, pipeline); 5802 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 5803 ggml_metal_encoder_set_buffer (enc, bid_src0, 1); 5804 ggml_metal_encoder_set_buffer (enc, bid_dst, 2); 5805 5806 ggml_metal_encoder_set_threadgroup_memory_size(enc, smem, 0); 5807 5808 ggml_metal_encoder_dispatch_threadgroups(enc, npr*ne01, ne02, ne03, nth, 1, 1); 5809 5810 auto pipeline_merge = ggml_metal_library_get_pipeline_argsort_merge(lib, op); 5811 5812 int len = nth; 5813 5814 while (len < ne00) { 5815 ggml_metal_op_concurrency_reset(ctx); 5816 5817 ggml_metal_kargs_argsort_merge args_merge = { 5818 /*.ne00 =*/ ne00, 5819 /*.ne01 =*/ ne01, 5820 /*.ne02 =*/ ne02, 5821 /*.ne03 =*/ ne03, 5822 /*.nb00 =*/ nb00, 5823 /*.nb01 =*/ nb01, 5824 /*.nb02 =*/ nb02, 5825 /*.nb03 =*/ nb03, 5826 /*.ne0 =*/ ne0, 5827 /*.ne1 =*/ ne1, 5828 /*.ne2 =*/ ne2, 5829 /*.ne3 =*/ ne3, 5830 /*.top_k =*/ ne00, 5831 /*.len =*/ len, 5832 }; 5833 5834 // merges per row 5835 const int nm = (ne00 + 2*len - 1) / (2*len); 5836 5837 const int nth = std::min(512, ggml_metal_pipeline_max_theads_per_threadgroup(pipeline_merge)); 5838 5839 ggml_metal_encoder_set_pipeline(enc, pipeline_merge); 5840 ggml_metal_encoder_set_bytes (enc, &args_merge, sizeof(args_merge), 0); 5841 ggml_metal_encoder_set_buffer (enc, bid_src0, 1); 5842 ggml_metal_encoder_set_buffer (enc, bid_dst, 2); 5843 ggml_metal_encoder_set_buffer (enc, bid_tmp, 3); 5844 5845 ggml_metal_encoder_dispatch_threadgroups(enc, nm*ne01, ne02, ne03, nth, 1, 1); 5846 5847 std::swap(bid_dst, bid_tmp); 5848 5849 len <<= 1; 5850 } 5851 5852 return 1; 5853 } 5854 5855 // bitonic-sort + merge fallback: efficient when k is small and there are few rows, 5856 // where the single-workgroup-per-row radix-select cannot reach enough parallelism 5857 static void ggml_metal_op_top_k_bitonic(ggml_metal_op_t ctx, int idx) { 5858 ggml_tensor * op = ctx->node(idx); 5859 5860 ggml_metal_library_t lib = ctx->lib; 5861 ggml_metal_encoder_t enc = ctx->enc; 5862 5863 GGML_ASSERT(ggml_is_contiguous_rows(op->src[0])); 5864 5865 GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); 5866 GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); 5867 GGML_TENSOR_LOCALS( int32_t, ne, op, ne); 5868 GGML_TENSOR_LOCALS(uint64_t, nb, op, nb); 5869 5870 auto pipeline = ggml_metal_library_get_pipeline_top_k(lib, op); 5871 5872 // bitonic sort requires the number of elements to be power of 2 5873 int nth = 1; 5874 while (nth < ne00 && 2*nth <= ggml_metal_pipeline_max_theads_per_threadgroup(pipeline)) { 5875 nth *= 2; 5876 } 5877 5878 // blocks per row 5879 const int npr = (ne00 + nth - 1)/nth; 5880 5881 const size_t smem = GGML_PAD(nth*sizeof(int32_t), 16); 5882 5883 ggml_metal_buffer_id bid_src0 = ggml_metal_get_buffer_id(op->src[0]); 5884 ggml_metal_buffer_id bid_dst = ggml_metal_get_buffer_id(op); 5885 5886 ggml_metal_buffer_id bid_tmp = bid_dst; 5887 bid_tmp.offs += sizeof(int32_t)*ggml_nelements(op->src[0]); 5888 5889 if ((int) ceil(std::log(npr) / std::log(2)) % 2 == 1) { 5890 std::swap(bid_dst, bid_tmp); 5891 } 5892 5893 const int top_k = ne0; 5894 5895 ggml_metal_kargs_argsort args = { 5896 /*.ne00 =*/ ne00, 5897 /*.ne01 =*/ ne01, 5898 /*.ne02 =*/ ne02, 5899 /*.ne03 =*/ ne03, 5900 /*.nb00 =*/ nb00, 5901 /*.nb01 =*/ nb01, 5902 /*.nb02 =*/ nb02, 5903 /*.nb03 =*/ nb03, 5904 /*.ne0 =*/ ne0, 5905 /*.ne1 =*/ ne1, 5906 /*.ne2 =*/ ne2, 5907 /*.ne3 =*/ ne3, 5908 /*.top_k =*/ std::min(nth, top_k), // for each block, keep just the top_k indices 5909 }; 5910 5911 if (npr > 1) { 5912 args.ne0 = (npr - 1)*args.top_k + std::min(ne00 - (npr - 1)*nth, args.top_k); 5913 } 5914 5915 ggml_metal_encoder_set_pipeline(enc, pipeline); 5916 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 5917 ggml_metal_encoder_set_buffer (enc, bid_src0, 1); 5918 ggml_metal_encoder_set_buffer (enc, bid_dst, 2); 5919 5920 ggml_metal_encoder_set_threadgroup_memory_size(enc, smem, 0); 5921 5922 ggml_metal_encoder_dispatch_threadgroups(enc, npr*ne01, ne02, ne03, nth, 1, 1); 5923 5924 auto pipeline_merge = ggml_metal_library_get_pipeline_top_k_merge(lib, op); 5925 5926 int len = args.top_k; 5927 5928 while (len < args.ne0) { 5929 ggml_metal_op_concurrency_reset(ctx); 5930 5931 // merges per row 5932 const int nm = (args.ne0 + 2*len - 1) / (2*len); 5933 5934 const int nth = std::min(512, std::min(len, ggml_metal_pipeline_max_theads_per_threadgroup(pipeline_merge))); 5935 5936 ggml_metal_kargs_argsort_merge args_merge = { 5937 /*.ne00 =*/ ne00, 5938 /*.ne01 =*/ ne01, 5939 /*.ne02 =*/ ne02, 5940 /*.ne03 =*/ ne03, 5941 /*.nb00 =*/ nb00, 5942 /*.nb01 =*/ nb01, 5943 /*.nb02 =*/ nb02, 5944 /*.nb03 =*/ nb03, 5945 /*.ne0 =*/ args.ne0, 5946 /*.ne1 =*/ ne1, 5947 /*.ne2 =*/ ne2, 5948 /*.ne3 =*/ ne3, 5949 /*.top_k =*/ nm == 1 ? top_k : args.ne0, // the final merge outputs top_k elements 5950 /*.len =*/ len, 5951 }; 5952 5953 ggml_metal_encoder_set_pipeline(enc, pipeline_merge); 5954 ggml_metal_encoder_set_bytes (enc, &args_merge, sizeof(args_merge), 0); 5955 ggml_metal_encoder_set_buffer (enc, bid_src0, 1); 5956 ggml_metal_encoder_set_buffer (enc, bid_dst, 2); 5957 ggml_metal_encoder_set_buffer (enc, bid_tmp, 3); 5958 5959 ggml_metal_encoder_dispatch_threadgroups(enc, nm*ne01, ne02, ne03, nth, 1, 1); 5960 5961 std::swap(bid_dst, bid_tmp); 5962 5963 len <<= 1; 5964 } 5965 } 5966 5967 // radix-select: one workgroup per row. Maps each float to an order-preserving unsigned 5968 // key, finds the k-th largest via 4 radix-8 histogram passes, then compacts the top-k 5969 // indices. Fast for large k and/or many rows. 5970 static void ggml_metal_op_top_k_radix(ggml_metal_op_t ctx, int idx) { 5971 ggml_tensor * op = ctx->node(idx); 5972 5973 ggml_metal_library_t lib = ctx->lib; 5974 ggml_metal_encoder_t enc = ctx->enc; 5975 5976 GGML_ASSERT(ggml_is_contiguous_rows(op->src[0])); 5977 5978 GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); 5979 GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); 5980 5981 auto pipeline = ggml_metal_library_get_pipeline_top_k_radix(lib, op); 5982 5983 // one workgroup per row; radix-select the k-th largest value 5984 const int nth = std::min(1024, ggml_metal_pipeline_max_theads_per_threadgroup(pipeline)); 5985 5986 ggml_metal_kargs_top_k args = { 5987 /*.ne00 =*/ ne00, 5988 /*.ne01 =*/ ne01, 5989 /*.ne02 =*/ ne02, 5990 /*.ne03 =*/ ne03, 5991 /*.nb01 =*/ nb01, 5992 /*.nb02 =*/ nb02, 5993 /*.nb03 =*/ nb03, 5994 /*.top_k =*/ (int32_t) op->ne[0], 5995 }; 5996 5997 // shared memory: 256-entry histogram + bucket/above scalars + output counter 5998 const size_t smem_histo = GGML_PAD(256*sizeof(uint32_t), 16); 5999 const size_t smem_bucket = GGML_PAD( sizeof(uint32_t), 16); 6000 const size_t smem_above = GGML_PAD( sizeof(uint32_t), 16); 6001 const size_t smem_out = GGML_PAD( sizeof(uint32_t), 16); 6002 6003 ggml_metal_encoder_set_pipeline(enc, pipeline); 6004 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 6005 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1); 6006 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 2); 6007 6008 ggml_metal_encoder_set_threadgroup_memory_size(enc, smem_histo, 0); 6009 ggml_metal_encoder_set_threadgroup_memory_size(enc, smem_bucket, 1); 6010 ggml_metal_encoder_set_threadgroup_memory_size(enc, smem_above, 2); 6011 ggml_metal_encoder_set_threadgroup_memory_size(enc, smem_out, 3); 6012 6013 ggml_metal_encoder_dispatch_threadgroups(enc, ne01, ne02, ne03, nth, 1, 1); 6014 } 6015 6016 int ggml_metal_op_topk_moe(ggml_metal_op_t ctx, int idx) { 6017 ggml_metal_library_t lib = ctx->lib; 6018 ggml_metal_encoder_t enc = ctx->enc; 6019 6020 int n_fuse = 1; 6021 const ggml_metal_fusion * fusion = ctx->can_fuse(idx, GGML_METAL_FUSION_FULL, &n_fuse); 6022 if (!fusion || ggml_metal_fusion_get_id(fusion) != GGML_METAL_FUSION_TOPK_MOE) { 6023 return 1; 6024 } 6025 6026 ggml_tensor * softmax = ctx->node(idx); 6027 ggml_tensor * logits = softmax->src[0]; 6028 ggml_tensor * get_rows = ctx->node(idx + 2); 6029 ggml_tensor * ids = get_rows->src[1]; 6030 ggml_tensor * weights = ctx->node(idx + n_fuse - 1); 6031 6032 const int64_t n_expert = logits->ne[0]; 6033 const int64_t n_tokens = logits->ne[1]; 6034 const int64_t n_expert_used = ids->ne[0]; 6035 6036 const bool with_norm = n_fuse >= 6; 6037 const bool with_scale = n_fuse == 4 || n_fuse == 7; 6038 6039 float clamp = -INFINITY; 6040 if (with_norm) { 6041 ggml_tensor * clamp_node = ctx->node(idx + 4); 6042 clamp = ggml_get_op_params_f32(clamp_node, 0); 6043 } 6044 6045 float scale = 1.0f; 6046 if (with_scale) { 6047 ggml_tensor * scale_node = ctx->node(idx + n_fuse - 1); 6048 scale = ggml_get_op_params_f32(scale_node, 0); 6049 } 6050 6051 ggml_metal_kargs_topk_moe args = { 6052 /*.ne01 =*/ (int32_t) n_tokens, 6053 /*.nb01 =*/ logits->nb[1], 6054 /*.nb1_ids =*/ ids->nb[1], 6055 /*.clamp =*/ clamp, 6056 /*.scale =*/ scale, 6057 }; 6058 6059 auto pipeline = ggml_metal_library_get_pipeline_topk_moe(lib, (int32_t) n_expert, (int32_t) n_expert_used, with_norm); 6060 6061 ggml_metal_encoder_set_pipeline(enc, pipeline); 6062 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 6063 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(logits), 1); 6064 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(weights), 2); 6065 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(ids), 3); 6066 6067 ggml_metal_encoder_dispatch_threadgroups(enc, (uint32_t) n_tokens, 1, 1, 32, 1, 1); 6068 6069 ctx->count_fusions(fusion); 6070 6071 if (ggml_metal_fusion_info_debug(ctx->finfo) > 1) { 6072 GGML_LOG_DEBUG("%s: fuse: SOFT_MAX + ARGSORT + GET_ROWS\n", __func__); 6073 } 6074 6075 return n_fuse; 6076 } 6077 6078 int ggml_metal_op_moe_reduce(ggml_metal_op_t ctx, int idx) { 6079 ggml_metal_library_t lib = ctx->lib; 6080 ggml_metal_encoder_t enc = ctx->enc; 6081 6082 int n_fuse = 1; 6083 const ggml_metal_fusion * fusion = ctx->can_fuse(idx, GGML_METAL_FUSION_FULL, &n_fuse); 6084 if (!fusion || ggml_metal_fusion_get_id(fusion) != GGML_METAL_FUSION_MOE_REDUCE) { 6085 return 1; 6086 } 6087 6088 ggml_tensor * mul = ctx->node(idx); 6089 ggml_tensor * experts = mul->src[0]; 6090 ggml_tensor * weights = mul->src[1]; 6091 ggml_tensor * dst = ctx->node(idx + n_fuse - 1); 6092 6093 ggml_metal_kargs_moe_reduce args = { 6094 /*.ne00 =*/ (int32_t) experts->ne[0], 6095 /*.ne02 =*/ (int32_t) experts->ne[2], 6096 }; 6097 6098 auto pipeline = ggml_metal_library_get_pipeline_moe_reduce(lib, (int32_t) experts->ne[1]); 6099 6100 const int nth = std::min(256, ggml_metal_pipeline_max_theads_per_threadgroup(pipeline)); 6101 const int n_col_tiles = (args.ne00 + nth - 1) / nth; 6102 6103 ggml_metal_encoder_set_pipeline(enc, pipeline); 6104 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 6105 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(experts), 1); 6106 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(weights), 2); 6107 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(dst), 3); 6108 6109 ggml_metal_encoder_dispatch_threadgroups(enc, (uint32_t) args.ne02, (uint32_t) n_col_tiles, 1, nth, 1, 1); 6110 6111 ctx->count_fusions(fusion); 6112 6113 if (ggml_metal_fusion_info_debug(ctx->finfo) > 1) { 6114 GGML_LOG_DEBUG("%s: fuse: MOE_REDUCE\n", __func__); 6115 } 6116 6117 return n_fuse; 6118 } 6119 6120 int ggml_metal_op_top_k(ggml_metal_op_t ctx, int idx) { 6121 ggml_tensor * op = ctx->node(idx); 6122 6123 // radix-select has a fixed single-workgroup-per-row cost (~50-60us) that is only 6124 // amortized for long rows, many rows, or a large k; otherwise the bitonic path wins 6125 const int ncols = op->src[0]->ne[0]; 6126 const int k = op->ne[0]; 6127 const int nrows = ggml_nrows(op->src[0]); 6128 6129 const bool use_radix = 6130 ncols > 2048 && (k > 64 || (nrows > 4 && ncols >= 8192)); 6131 6132 if (use_radix) { 6133 ggml_metal_op_top_k_radix(ctx, idx); 6134 } else { 6135 ggml_metal_op_top_k_bitonic(ctx, idx); 6136 } 6137 6138 return 1; 6139 } 6140 6141 int ggml_metal_op_tri(ggml_metal_op_t ctx, int idx) { 6142 ggml_tensor * op = ctx->node(idx); 6143 6144 ggml_metal_library_t lib = ctx->lib; 6145 ggml_metal_encoder_t enc = ctx->enc; 6146 6147 GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); 6148 GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); 6149 GGML_TENSOR_LOCALS( int32_t, ne, op, ne); 6150 GGML_TENSOR_LOCALS(uint64_t, nb, op, nb); 6151 6152 ggml_metal_kargs_tri args = { 6153 /*.ne00 =*/ ne00, 6154 /*.ne01 =*/ ne01, 6155 /*.ne02 =*/ ne02, 6156 /*.ne03 =*/ ne03, 6157 /*.nb00 =*/ nb00, 6158 /*.nb01 =*/ nb01, 6159 /*.nb02 =*/ nb02, 6160 /*.nb03 =*/ nb03, 6161 /*.ne0 =*/ ne0, 6162 /*.ne1 =*/ ne1, 6163 /*.ne2 =*/ ne2, 6164 /*.ne3 =*/ ne3, 6165 /*.nb0 =*/ nb0, 6166 /*.nb1 =*/ nb1, 6167 /*.nb2 =*/ nb2, 6168 /*.nb3 =*/ nb3, 6169 }; 6170 6171 auto pipeline = ggml_metal_library_get_pipeline_tri(lib, op); 6172 6173 int nth = 32; // SIMD width 6174 6175 while (nth < ne00 && nth < ggml_metal_pipeline_max_theads_per_threadgroup(pipeline)) { 6176 nth *= 2; 6177 } 6178 6179 nth = std::min(nth, ggml_metal_pipeline_max_theads_per_threadgroup(pipeline)); 6180 nth = std::min(nth, ne00); 6181 6182 ggml_metal_encoder_set_pipeline(enc, pipeline); 6183 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0); 6184 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1); 6185 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 2); 6186 6187 ggml_metal_encoder_dispatch_threadgroups(enc, ne01, ne02, ne03, nth, 1, 1); 6188 6189 return 1; 6190 } 6191 6192 int ggml_metal_op_opt_step_adamw(ggml_metal_op_t ctx, int idx) { 6193 ggml_tensor * op = ctx->node(idx); 6194 6195 ggml_metal_library_t lib = ctx->lib; 6196 ggml_metal_encoder_t enc = ctx->enc; 6197 6198 GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); 6199 GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); 6200 GGML_TENSOR_LOCALS( int32_t, ne, op, ne); 6201 GGML_TENSOR_LOCALS(uint64_t, nb, op, nb); 6202 6203 auto pipeline = ggml_metal_library_get_pipeline_opt_step_adamw(lib, op); 6204 6205 const int64_t np = ggml_nelements(op->src[0]); 6206 ggml_metal_kargs_opt_step_adamw args = { 6207 /*.np =*/ np, 6208 }; 6209 6210 int ida = 0; 6211 6212 ggml_metal_encoder_set_pipeline(enc, pipeline); 6213 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), ida++); 6214 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), ida++); 6215 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[1]), ida++); 6216 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[2]), ida++); 6217 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[3]), ida++); 6218 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[4]), ida++); 6219 6220 const int nth = std::min(ggml_metal_pipeline_max_theads_per_threadgroup(pipeline), ne0); 6221 const int64_t n = (np + nth - 1) / nth; 6222 6223 ggml_metal_encoder_dispatch_threadgroups(enc, n, 1, 1, nth, 1, 1); 6224 6225 return 1; 6226 } 6227 6228 int ggml_metal_op_opt_step_sgd(ggml_metal_op_t ctx, int idx) { 6229 ggml_tensor * op = ctx->node(idx); 6230 6231 ggml_metal_library_t lib = ctx->lib; 6232 ggml_metal_encoder_t enc = ctx->enc; 6233 6234 GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne); 6235 GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); 6236 GGML_TENSOR_LOCALS( int32_t, ne, op, ne); 6237 GGML_TENSOR_LOCALS(uint64_t, nb, op, nb); 6238 6239 auto pipeline = ggml_metal_library_get_pipeline_opt_step_sgd(lib, op); 6240 6241 const int64_t np = ggml_nelements(op->src[0]); 6242 ggml_metal_kargs_opt_step_sgd args = { 6243 /*.np =*/ np, 6244 }; 6245 6246 int ida = 0; 6247 6248 ggml_metal_encoder_set_pipeline(enc, pipeline); 6249 ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), ida++); 6250 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), ida++); 6251 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[1]), ida++); 6252 ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[2]), ida++); 6253 6254 const int nth = std::min(ggml_metal_pipeline_max_theads_per_threadgroup(pipeline), ne0); 6255 const int64_t n = (np + nth - 1) / nth; 6256 6257 ggml_metal_encoder_dispatch_threadgroups(enc, n, 1, 1, nth, 1, 1); 6258 6259 return 1; 6260 } 6261 6262 int ggml_metal_op_count_equal(ggml_metal_op_t ctx, int idx) { 6263 ggml_tensor * op = ctx->node(idx); 6264 6265 ggml_metal_library_t lib = ctx->lib; 6266 ggml_metal_encoder_t enc = ctx->enc; 6267 6268 GGML_TENSOR_LOCALS(int32_t, ne0, op->src[0], ne); 6269 GGML_TENSOR_LOCALS(uint64_t, nb0, op->src[0], nb); 6270 GGML_TENSOR_LOCALS(uint64_t, nb1, op->src[1], nb); 6271 6272 { 6273 ggml_metal_kargs_memset args = { /*.val =*/ 0 }; 6274 6275 auto pipeline = ggml_metal_library_get_pipeline_memset(lib, op); 6276 6277 ggml_metal_encoder_set_pipeline(enc, pipeline); 6278 ggml_metal_encoder_set_bytes(enc, &args, sizeof(args), 0); 6279 ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(op), 1); 6280 6281 ggml_metal_encoder_dispatch_threadgroups(enc, 1, 1, 1, 1, 1, 1); 6282 } 6283 6284 ggml_metal_op_concurrency_reset(ctx); 6285 6286 { 6287 ggml_metal_kargs_count_equal args = { 6288 /*.ne00 =*/ ne00, 6289 /*.ne01 =*/ ne01, 6290 /*.ne02 =*/ ne02, 6291 /*.ne03 =*/ ne03, 6292 /*.nb00 =*/ nb00, 6293 /*.nb01 =*/ nb01, 6294 /*.nb02 =*/ nb02, 6295 /*.nb03 =*/ nb03, 6296 /*.nb10 =*/ nb10, 6297 /*.nb11 =*/ nb11, 6298 /*.nb12 =*/ nb12, 6299 /*.nb13 =*/ nb13, 6300 }; 6301 6302 auto pipeline = ggml_metal_library_get_pipeline_count_equal(lib, op); 6303 6304 const size_t smem = pipeline.smem; 6305 6306 const int nth = 32*pipeline.nsg; 6307 6308 GGML_ASSERT(nth <= ggml_metal_pipeline_max_theads_per_threadgroup(pipeline)); 6309 6310 ggml_metal_encoder_set_pipeline(enc, pipeline); 6311 ggml_metal_encoder_set_bytes(enc, &args, sizeof(args), 0); 6312 ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(op->src[0]), 1); 6313 ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(op->src[1]), 2); 6314 ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(op), 3); 6315 6316 ggml_metal_encoder_set_threadgroup_memory_size(enc, smem, 0); 6317 ggml_metal_encoder_dispatch_threadgroups(enc, ne01, ne02, ne03, nth, 1, 1); 6318 } 6319 6320 return 1; 6321 }