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-sycl/topk-moe.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-sycl / topk-moe.cpp` 619 lines ยท 23011 bytes Raw Blame History 1 #include 2 #include 3 #include 4 5 #include "ggml.h" 6 #include "ggml-impl.h" 7 #include "ggml-backend-impl.h" 8 #include "topk-moe.hpp" 9 10 // SYCL port of ggml-cuda/topk-moe.cu. The kernel is a translation of the CUDA no-bias, no-PDL 11 // path of topk_moe_cuda; the fusion-detection helpers below are ported near-verbatim from 12 // ggml-cuda.cu (pure graph / pointer inspection, backend-agnostic). Bias is not implemented here: 13 // if a routing bias is detected, the fusion is declined and the eager path runs unchanged. 14 15 struct ggml_sycl_topk_moe_args { 16 bool sigmoid{}; 17 bool softmax{}; 18 bool delayed_softmax{}; 19 bool prob_bias{}; 20 bool norm{}; 21 bool scale{}; 22 }; 23 24 struct topk_moe_config { 25 bool use_sigmoid; 26 bool with_norm; 27 bool delayed_softmax; 28 }; 29 30 // warp-local softmax used for both the pre-top-k logits and the post-top-k delayed path 31 template 32 static inline void softmax_warp_inplace(float (&vals)[experts_per_thread], const int limit, const int lane) { 33 float max_val = -INFINITY; 34 #pragma unroll 35 for (int i = 0; i < experts_per_thread; i++) { 36 const int idx = lane + i * WARP_SIZE; 37 const bool active = !use_limit || (idx < limit); 38 if (active) { 39 max_val = sycl::fmax(max_val, vals[i]); 40 } 41 } 42 max_val = warp_reduce_max(max_val); 43 44 float sum = 0.f; 45 #pragma unroll 46 for (int i = 0; i < experts_per_thread; i++) { 47 const int idx = lane + i * WARP_SIZE; 48 const bool active = !use_limit || (idx < limit); 49 if (active) { 50 const float val = sycl::exp(vals[i] - max_val); 51 vals[i] = val; 52 sum += val; 53 } else { 54 vals[i] = 0.f; 55 } 56 } 57 sum = warp_reduce_sum(sum); 58 59 const float inv_sum = 1.0f / sum; 60 #pragma unroll 61 for (int i = 0; i < experts_per_thread; i++) { 62 const int idx = lane + i * WARP_SIZE; 63 if (!use_limit || idx < limit) { 64 vals[i] *= inv_sum; 65 } 66 } 67 } 68 69 template 70 static inline void sigmoid_warp_inplace(float (&vals)[experts_per_thread], const int limit, const int lane) { 71 #pragma unroll 72 for (int i = 0; i < experts_per_thread; i++) { 73 const int idx = lane + i * WARP_SIZE; 74 const bool active = !use_limit || (idx < limit); 75 vals[i] = active ? 1.f / (1.f + sycl::exp(-vals[i])) : -INFINITY; 76 } 77 } 78 79 /* 80 This kernel does the following: 81 1. optionally softmax/sigmoid over the logits per token [n_experts, n_tokens] 82 2. argmax reduce over the top-k (n_experts_used) logits 83 3. write weights + ids to global memory 84 4. optionally normalize the weights or apply softmax over the selected logits 85 86 It is intended as a fusion of the softmax->top-k->get_rows pipeline for MoE models. 87 One sub-group handles one row/token, mirroring topk_moe_cuda's one-warp-per-row layout. 88 */ 89 template 90 static void topk_moe_kernel(const float * __restrict__ logits, 91 float * __restrict__ weights, 92 int32_t * __restrict__ ids, 93 const int n_rows, 94 const int n_expert_used, 95 const float clamp_val, 96 const float scale_val, 97 const topk_moe_config config) { 98 auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item<1>(); 99 const int row = item_ct1.get_group(0); 100 if (row >= n_rows) { 101 return; 102 } 103 const int lane = item_ct1.get_local_id(0); 104 105 logits += (size_t) n_experts * row; 106 weights += (size_t) n_expert_used * row; 107 ids += (size_t) n_experts * row; // ids row stride is n_experts (matches the argsort tensor) 108 109 constexpr int experts_per_thread = (n_experts > WARP_SIZE) ? n_experts / WARP_SIZE : 1; 110 111 float wt[experts_per_thread]; 112 #pragma unroll 113 for (int i = 0; i < experts_per_thread; i++) { 114 wt[i] = -INFINITY; 115 } 116 #pragma unroll 117 for (int i = 0; i < n_experts; i += WARP_SIZE) { 118 const int expert = i + lane; 119 wt[i / WARP_SIZE] = (n_experts % WARP_SIZE == 0 || expert < n_experts) ? logits[expert] : -INFINITY; 120 } 121 122 if (!config.delayed_softmax) { 123 if (config.use_sigmoid) { 124 sigmoid_warp_inplace(wt, n_experts, lane); 125 } else { 126 softmax_warp_inplace(wt, n_experts, lane); 127 } 128 } 129 130 // Sanitize NaN to -FLT_MAX so the iterative argmax produces unique expert IDs. NaN comparisons 131 // always return false, which would cause the same expert to be selected repeatedly. 132 #pragma unroll 133 for (int i = 0; i < experts_per_thread; i++) { 134 if (sycl::isnan(wt[i])) { 135 wt[i] = -FLT_MAX; 136 } 137 } 138 139 // each thread now holds either a portion of the softmax distribution or the raw logits. Do the 140 // argmax reduce over n_expert_used, each time marking the selected expert as -inf to exclude it 141 // from the next iteration. 142 143 float wt_sum = 0.f; 144 float output_weights[experts_per_thread]; 145 #pragma unroll 146 for (int i = 0; i < experts_per_thread; i++) { 147 output_weights[i] = 0.f; 148 } 149 150 const sycl::sub_group sg = item_ct1.get_sub_group(); 151 152 for (int k = 0; k < n_expert_used; k++) { 153 float max_val = wt[0]; 154 int max_expert = lane; 155 #pragma unroll 156 for (int i = 1; i < experts_per_thread; i++) { 157 const int expert = lane + i * WARP_SIZE; 158 if ((n_experts % WARP_SIZE == 0 || expert < n_experts) && wt[i] > max_val) { 159 max_val = wt[i]; 160 max_expert = expert; 161 } 162 } 163 #pragma unroll 164 for (int mask = WARP_SIZE / 2; mask > 0; mask >>= 1) { 165 const float val = dpct::permute_sub_group_by_xor(sg, max_val, mask); 166 const int expert = dpct::permute_sub_group_by_xor(sg, max_expert, mask); 167 if (val > max_val || (val == max_val && expert < max_expert)) { 168 max_val = val; 169 max_expert = expert; 170 } 171 } 172 173 if ((max_expert & (WARP_SIZE - 1)) == lane) { 174 wt[max_expert / WARP_SIZE] = -INFINITY; 175 } 176 if ((k & (WARP_SIZE - 1)) == lane) { 177 output_weights[k / WARP_SIZE] = max_val; 178 } 179 if ((max_expert & (WARP_SIZE - 1)) == lane) { 180 ids[k] = max_expert; 181 if (config.with_norm) { 182 wt_sum += max_val; 183 } 184 } 185 } 186 187 if (config.with_norm) { 188 wt_sum = warp_reduce_sum(wt_sum); 189 wt_sum = sycl::fmax(wt_sum, clamp_val); 190 const float inv = 1.0f / wt_sum; 191 #pragma unroll 192 for (int i = 0; i < experts_per_thread; i++) { 193 output_weights[i] *= inv; 194 } 195 } 196 197 if (config.delayed_softmax) { 198 softmax_warp_inplace(output_weights, n_expert_used, lane); 199 } 200 201 #pragma unroll 202 for (int i = 0; i < experts_per_thread; i++) { 203 const int idx = i * WARP_SIZE + lane; 204 if (idx < n_expert_used) { 205 weights[idx] = output_weights[i] * scale_val; 206 } 207 } 208 } 209 210 template 211 static void launch_topk_moe(queue_ptr stream, const float * logits, float * weights, int32_t * ids, int n_rows, 212 int n_expert_used, float clamp_val, float scale_val, const topk_moe_config & config) { 213 const sycl::range<1> block_dims(WARP_SIZE); 214 const sycl::range<1> block_nums(n_rows); 215 stream->parallel_for(sycl::nd_range<1>(block_nums * block_dims, block_dims), 216 [=](sycl::nd_item<1> item_ct1) [[sycl::reqd_sub_group_size(WARP_SIZE)]] { 217 topk_moe_kernel(logits, weights, ids, n_rows, n_expert_used, clamp_val, 218 scale_val, config); 219 GGML_UNUSED(item_ct1); 220 }); 221 } 222 223 static void ggml_sycl_op_topk_moe(ggml_backend_sycl_context & ctx, 224 const ggml_tensor * logits, 225 ggml_tensor * weights, 226 ggml_tensor * ids, 227 const ggml_tensor * clamp, 228 const ggml_tensor * scale, 229 const ggml_sycl_topk_moe_args & args) { 230 GGML_ASSERT(logits->type == GGML_TYPE_F32); 231 GGML_ASSERT(weights->type == GGML_TYPE_F32); 232 GGML_ASSERT(ids->type == GGML_TYPE_I32); 233 234 const int n_experts = logits->ne[0]; 235 const int n_rows = logits->ne[1]; 236 const int n_expert_used = weights->ne[1]; 237 238 GGML_ASSERT(ids->nb[1] / ggml_type_size(ids->type) == (size_t) n_experts); 239 240 const float * logits_d = (const float *) logits->data; 241 float * weights_d = (float *) weights->data; 242 int32_t * ids_d = (int32_t *) ids->data; 243 244 const bool with_norm = clamp != nullptr; 245 const float clamp_val = clamp ? ggml_get_op_params_f32(clamp, 0) : -INFINITY; 246 const float scale_val = scale ? ggml_get_op_params_f32(scale, 0) : 1.0f; 247 248 topk_moe_config config; 249 config.use_sigmoid = args.sigmoid; 250 config.with_norm = with_norm; 251 config.delayed_softmax = args.delayed_softmax; 252 253 queue_ptr stream = ctx.stream(); 254 ggml_sycl_set_device(ctx.device); 255 256 switch (n_experts) { 257 case 1: 258 launch_topk_moe<1>(stream, logits_d, weights_d, ids_d, n_rows, n_expert_used, clamp_val, scale_val, 259 config); 260 break; 261 case 2: 262 launch_topk_moe<2>(stream, logits_d, weights_d, ids_d, n_rows, n_expert_used, clamp_val, scale_val, 263 config); 264 break; 265 case 4: 266 launch_topk_moe<4>(stream, logits_d, weights_d, ids_d, n_rows, n_expert_used, clamp_val, scale_val, 267 config); 268 break; 269 case 8: 270 launch_topk_moe<8>(stream, logits_d, weights_d, ids_d, n_rows, n_expert_used, clamp_val, scale_val, 271 config); 272 break; 273 case 16: 274 launch_topk_moe<16>(stream, logits_d, weights_d, ids_d, n_rows, n_expert_used, clamp_val, scale_val, 275 config); 276 break; 277 case 32: 278 launch_topk_moe<32>(stream, logits_d, weights_d, ids_d, n_rows, n_expert_used, clamp_val, scale_val, 279 config); 280 break; 281 case 64: 282 launch_topk_moe<64>(stream, logits_d, weights_d, ids_d, n_rows, n_expert_used, clamp_val, scale_val, 283 config); 284 break; 285 case 128: 286 launch_topk_moe<128>(stream, logits_d, weights_d, ids_d, n_rows, n_expert_used, clamp_val, scale_val, 287 config); 288 break; 289 case 256: 290 launch_topk_moe<256>(stream, logits_d, weights_d, ids_d, n_rows, n_expert_used, clamp_val, scale_val, 291 config); 292 break; 293 case 512: 294 launch_topk_moe<512>(stream, logits_d, weights_d, ids_d, n_rows, n_expert_used, clamp_val, scale_val, 295 config); 296 break; 297 default: 298 GGML_ASSERT(false && "fatal error"); 299 } 300 } 301 302 static bool ggml_sycl_should_use_topk_moe(const ggml_tensor * gating_op, const ggml_tensor * weights, 303 const ggml_tensor * logits, const ggml_tensor * ids) { 304 const int n_expert = ids->nb[1] / ids->nb[0]; 305 if ((n_expert & (n_expert - 1)) != 0 || n_expert > 512) { 306 return false; 307 } 308 309 if (!ggml_is_contiguous(weights) || !ggml_is_contiguous(logits)) { 310 return false; 311 } 312 313 if (gating_op->op == GGML_OP_SOFT_MAX) { 314 float scale = 1.0f; 315 float max_bias = 0.0f; 316 317 memcpy(&scale, (const float *) gating_op->op_params + 0, sizeof(float)); 318 memcpy(&max_bias, (const float *) gating_op->op_params + 1, sizeof(float)); 319 320 if (!ggml_is_contiguous(gating_op->src[0])) { 321 return false; 322 } 323 if (scale != 1.0f || max_bias != 0.0f) { 324 return false; 325 } 326 // don't fuse when masks or sinks are present 327 if (gating_op->src[1] || gating_op->src[2]) { 328 return false; 329 } 330 } else if (gating_op->op == GGML_OP_UNARY) { 331 if (ggml_get_unary_op(gating_op) != GGML_UNARY_OP_SIGMOID) { 332 return false; 333 } 334 } 335 336 return true; 337 } 338 339 // ported from ggml_cuda_topk_moe_fusion - pure graph inspection, backend-agnostic 340 static bool ggml_sycl_topk_moe_fusion(const ggml_cgraph * cgraph, int node_idx, ggml_sycl_topk_moe_args & args) { 341 args = ggml_sycl_topk_moe_args{}; 342 343 const int n_nodes = cgraph->n_nodes; 344 ggml_tensor ** nodes = cgraph->nodes; 345 346 if (nodes[node_idx]->op == GGML_OP_SOFT_MAX) { 347 args.softmax = true; 348 } 349 350 if (nodes[node_idx]->op == GGML_OP_UNARY) { 351 if (ggml_get_unary_op(nodes[node_idx]) != GGML_UNARY_OP_SIGMOID) { 352 return false; 353 } 354 args.sigmoid = true; 355 } 356 357 if (nodes[node_idx]->op == GGML_OP_ARGSORT) { 358 args.delayed_softmax = true; 359 } 360 361 node_idx++; 362 363 if (args.sigmoid || args.softmax) { 364 // SOFTMAX -> RESHAPE 365 if (node_idx >= n_nodes || nodes[node_idx]->op != GGML_OP_RESHAPE || 366 nodes[node_idx]->src[0] != nodes[node_idx - 1]) { 367 return false; 368 } 369 ggml_tensor * probs_reshaped = nodes[node_idx]; 370 node_idx++; 371 372 if (node_idx >= n_nodes) { 373 return false; 374 } 375 376 // src of bias add is the unreshaped probs (-2 instead of -1) 377 if (nodes[node_idx]->op == GGML_OP_ADD && nodes[node_idx]->src[0] == nodes[node_idx - 2]) { 378 args.prob_bias = true; 379 node_idx++; 380 } 381 // RESHAPE/ADD -> ARGSORT 382 if (node_idx >= n_nodes || nodes[node_idx]->op != GGML_OP_ARGSORT) { 383 return false; 384 } 385 386 if (args.prob_bias && nodes[node_idx]->src[0] != nodes[node_idx - 1]) { 387 return false; 388 } else if (!args.prob_bias && nodes[node_idx]->src[0] != nodes[node_idx - 2]) { 389 return false; 390 } 391 392 node_idx++; 393 394 // ARGSORT -> VIEW 395 if (node_idx >= n_nodes || nodes[node_idx]->op != GGML_OP_VIEW || 396 nodes[node_idx]->src[0] != nodes[node_idx - 1]) { 397 return false; 398 } 399 node_idx++; 400 401 if (node_idx >= n_nodes || nodes[node_idx]->op != GGML_OP_GET_ROWS) { 402 return false; 403 } 404 405 // GET_ROWS 406 if (nodes[node_idx]->src[0] != probs_reshaped || nodes[node_idx]->src[1] != nodes[node_idx - 1]) { 407 return false; 408 } 409 node_idx++; 410 } else if (args.delayed_softmax) { 411 if (node_idx - 2 < 0) { 412 return false; 413 } 414 ggml_tensor * probs_reshaped = nodes[node_idx - 2]; 415 416 // VIEW -> ARGSORT 417 if (node_idx >= n_nodes || nodes[node_idx]->op != GGML_OP_VIEW || 418 nodes[node_idx]->src[0] != nodes[node_idx - 1]) { 419 return false; 420 } 421 node_idx++; 422 423 // GET_ROWS 424 if (node_idx >= n_nodes || nodes[node_idx]->src[1] != nodes[node_idx - 1] || 425 nodes[node_idx]->src[0] != probs_reshaped) { 426 return false; 427 } 428 node_idx++; 429 430 static const std::vector remaining_ops = { GGML_OP_RESHAPE, GGML_OP_SOFT_MAX, GGML_OP_RESHAPE }; 431 432 for (const ggml_op op : remaining_ops) { 433 if (node_idx >= n_nodes || nodes[node_idx]->op != op || nodes[node_idx]->src[0] != nodes[node_idx - 1]) { 434 return false; 435 } 436 node_idx++; 437 } 438 } 439 440 // at this point we can check for norm + scale; everything is now at least valid up to the norm 441 if (node_idx >= n_nodes) { 442 return true; 443 } 444 445 if (nodes[node_idx]->op == GGML_OP_RESHAPE) { 446 // check RESHAPE -> SUM_ROWS -> CLAMP -> DIV -> RESHAPE 447 static const std::vector norm_ops = { GGML_OP_RESHAPE, GGML_OP_SUM_ROWS, GGML_OP_CLAMP }; 448 449 args.norm = true; 450 for (const ggml_op op : norm_ops) { 451 if (nodes[node_idx]->op == op && nodes[node_idx]->src[0] == nodes[node_idx - 1]) { 452 node_idx++; 453 } else { 454 args.norm = false; 455 return true; 456 } 457 } 458 459 // DIV <- CLAMP, RESHAPE 460 if (nodes[node_idx]->op != GGML_OP_DIV || nodes[node_idx]->src[1] != nodes[node_idx - 1] || 461 nodes[node_idx]->src[0] != nodes[node_idx - 3]) { 462 args.norm = false; 463 return true; 464 } 465 node_idx++; 466 467 if (nodes[node_idx]->op != GGML_OP_RESHAPE || nodes[node_idx]->src[0] != nodes[node_idx - 1]) { 468 args.norm = false; 469 return true; 470 } 471 node_idx++; 472 } 473 474 if (nodes[node_idx]->op == GGML_OP_SCALE && nodes[node_idx]->src[0] == nodes[node_idx - 1]) { 475 args.scale = true; 476 } 477 478 return true; 479 } 480 481 // returns whether the write (out) nodes overwrite the read nodes in operation 482 // ported from ggml_cuda_check_fusion_memory_ranges - pure pointer/range inspection 483 static bool ggml_sycl_check_fusion_memory_ranges(const ggml_cgraph * cgraph, const int node_idx, 484 const int node_count, const int * out_nodes, const int out_count, 485 const bool is_topk_moe = false) { 486 auto nodes_overlap = [&](const ggml_tensor * a, const ggml_tensor * b) { 487 const int64_t a_start = (int64_t) a->data; 488 const int64_t a_end = a_start + ggml_backend_buft_get_alloc_size(a->buffer->buft, a); 489 490 const int64_t b_start = (int64_t) b->data; 491 const int64_t b_end = b_start + ggml_backend_buft_get_alloc_size(b->buffer->buft, b); 492 493 if ((b_start <= a_start && a_start < b_end) || (a_start <= b_start && b_start < a_end)) { 494 return true; 495 } 496 497 return false; 498 }; 499 500 bool is_ok = true; 501 // exception for topk-moe, as each row is read entirely before writing 502 if (ggml_nrows(cgraph->nodes[node_idx]) == 1 && is_topk_moe) { 503 return true; 504 } 505 506 for (int i = 0; i < out_count; ++i) { 507 const ggml_tensor * dst = cgraph->nodes[out_nodes[i]]; 508 509 for (int j = node_idx; j < node_idx + node_count; ++j) { 510 // loop over all srcs of all nodes in the fusion. If the src overlaps the destination and 511 // the src is not an intermediate node that's being elided, then disable fusion. 512 for (int src_idx = 0; src_idx < GGML_MAX_SRC; ++src_idx) { 513 const ggml_tensor * src = cgraph->nodes[j]->src[src_idx]; 514 515 if (!src || src->op == GGML_OP_NONE) { 516 continue; 517 } 518 519 if (nodes_overlap(dst, src)) { 520 bool found = false; 521 522 for (int k = node_idx; k < j; ++k) { 523 if (cgraph->nodes[k] == src) { 524 found = true; 525 break; 526 } 527 } 528 529 if (!found) { 530 is_ok = false; 531 break; 532 } 533 } 534 } 535 } 536 } 537 538 return is_ok; 539 } 540 541 int ggml_sycl_fuse(ggml_backend_sycl_context & ctx, ggml_cgraph * cgraph, int i) { 542 if (!g_ggml_sycl_enable_fusion) { 543 return 0; 544 } 545 546 return ggml_sycl_fuse_topk_moe(ctx, cgraph, i); 547 } 548 549 int ggml_sycl_fuse_topk_moe(ggml_backend_sycl_context & ctx, ggml_cgraph * cgraph, int i) { 550 ggml_tensor * node = cgraph->nodes[i]; 551 552 if (node->op != GGML_OP_UNARY && node->op != GGML_OP_SOFT_MAX && node->op != GGML_OP_ARGSORT) { 553 return 0; 554 } 555 556 ggml_sycl_topk_moe_args args; 557 if (!ggml_sycl_topk_moe_fusion(cgraph, i, args)) { 558 return 0; 559 } 560 561 // this kernel implements the no-bias path only; decline anything with a routing bias 562 if (args.prob_bias) { 563 return 0; 564 } 565 566 const ggml_tensor * logits = node->src[0]; 567 ggml_tensor * weights = nullptr; 568 ggml_tensor * ids = nullptr; 569 const ggml_tensor * clamp = nullptr; 570 const ggml_tensor * scale = nullptr; 571 572 std::vector ops; 573 int out_nodes[2]; 574 575 if (!args.delayed_softmax) { 576 const ggml_op gating_op = args.sigmoid ? GGML_OP_UNARY : GGML_OP_SOFT_MAX; 577 ops.insert(ops.end(), { gating_op, GGML_OP_RESHAPE, GGML_OP_ARGSORT, GGML_OP_VIEW, GGML_OP_GET_ROWS }); 578 out_nodes[0] = i + 3; 579 ids = cgraph->nodes[i + 3]; 580 581 if (args.norm) { 582 ops.insert(ops.end(), { GGML_OP_RESHAPE, GGML_OP_SUM_ROWS, GGML_OP_CLAMP, GGML_OP_DIV, GGML_OP_RESHAPE }); 583 clamp = cgraph->nodes[i + (int) ops.size() - 3]; 584 } 585 if (args.scale) { 586 ops.insert(ops.end(), { GGML_OP_SCALE }); 587 scale = cgraph->nodes[i + (int) ops.size() - 1]; 588 } 589 590 weights = cgraph->nodes[i + (int) ops.size() - 1]; 591 out_nodes[1] = i + (int) ops.size() - 1; 592 593 if (ggml_can_fuse_subgraph(cgraph, i, ops.size(), ops.data(), out_nodes, 2) && 594 ggml_sycl_should_use_topk_moe(node, weights, logits, ids) && 595 ggml_sycl_check_fusion_memory_ranges(cgraph, i, (int) ops.size(), out_nodes, 2, /*is_topk_moe=*/true)) { 596 ggml_sycl_op_topk_moe(ctx, logits, weights, ids, clamp, scale, args); 597 return (int) ops.size() - 1; 598 } 599 } else if (!args.norm && !args.prob_bias) { 600 // gpt-oss style: argsort -> view -> get_rows -> reshape -> softmax -> reshape, no norm/bias 601 ops.insert(ops.end(), 602 { GGML_OP_ARGSORT, GGML_OP_VIEW, GGML_OP_GET_ROWS, GGML_OP_RESHAPE, GGML_OP_SOFT_MAX, 603 GGML_OP_RESHAPE }); 604 weights = cgraph->nodes[i + 5]; 605 ids = cgraph->nodes[i + 1]; 606 const ggml_tensor * softmax = cgraph->nodes[i + 4]; 607 out_nodes[0] = i + 1; 608 out_nodes[1] = i + 5; 609 610 if (ggml_can_fuse_subgraph(cgraph, i, ops.size(), ops.data(), out_nodes, 2) && 611 ggml_sycl_should_use_topk_moe(softmax, weights, logits, ids) && 612 ggml_sycl_check_fusion_memory_ranges(cgraph, i, (int) ops.size(), out_nodes, 2, /*is_topk_moe=*/true)) { 613 ggml_sycl_op_topk_moe(ctx, logits, weights, ids, clamp, scale, args); 614 return (int) ops.size() - 1; 615 } 616 } 617 618 return 0; 619 }