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-openvino/openvino/pass/fuse_moe_router.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-openvino / openvino / pass / fuse_moe_router.cpp` 153 lines ยท 8429 bytes Raw Blame History 1 #include "fuse_moe_router.h" 2 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 9 #include 10 #include 11 #include 12 #include 13 #include 14 #include 15 #include 16 #include 17 #include 18 #include 19 #include 20 #include 21 #include 22 23 namespace ov { 24 namespace frontend { 25 namespace ggml { 26 namespace pass { 27 28 namespace { 29 30 bool constant_is(const ov::Output & output, const std::vector & values) { 31 auto node = ov::as_type_ptr(output.get_node_shared_ptr()); 32 return node && node->get_element_type().is_integral_number() && node->cast_vector() == values; 33 } 34 35 } // namespace 36 37 FuseMoeRouter::FuseMoeRouter() { 38 using namespace ov::pass::pattern; 39 using namespace ov::op; 40 41 // Match the GGML rank-4 routing chain. The GPU plugin recognizes the resulting 42 // Softmax -> TopK -> ReduceSum -> Divide subgraph as MoERouterFused. 43 auto logits = wrap_type(); 44 auto softmax = wrap_type({logits}); 45 auto topk = wrap_type({softmax, any_input()}); 46 topk->set_output_size(2); 47 auto ids = wrap_type({topk->output(1), any_input(), any_input(), any_input(), any_input()}); 48 auto ids_2d = wrap_type({ids, any_input()}); 49 auto probs = wrap_type({softmax, any_input()}); 50 auto data = wrap_type({probs, any_input()}); 51 auto data_batch = wrap_type({wrap_type({data}), any_input(), any_input()}); 52 auto ids_count = wrap_type({wrap_type({ids_2d}), any_input(), any_input()}); 53 auto target = wrap_type({data_batch, ids_count}); 54 auto broadcast = wrap_type({ids_2d, target}); 55 auto gather = wrap_type({data, broadcast, any_input()}); 56 auto weights_4d = wrap_type({gather, any_input()}); 57 auto weights = wrap_type({weights_4d, any_input()}); 58 auto sum = wrap_type({weights, any_input()}); 59 auto clamp = wrap_type({sum}); 60 auto tile = wrap_type({clamp, any_input()}); 61 auto norm = wrap_type({weights, tile}); 62 63 const auto callback = [=](Matcher & m) { 64 const auto & pm = m.get_pattern_value_map(); 65 const auto node = [&](const std::shared_ptr & p) { return pm.at(p).get_node_shared_ptr(); }; 66 const auto input_is = [&](const std::shared_ptr & p, size_t i, const std::vector & v) { 67 return constant_is(node(p)->input_value(i), v); 68 }; 69 auto mm = ov::as_type_ptr(node(logits)); 70 auto sm = ov::as_type_ptr(node(softmax)); 71 auto tk = ov::as_type_ptr(node(topk)); 72 auto reduce = ov::as_type_ptr(node(sum)); 73 auto limit = ov::as_type_ptr(node(clamp)); 74 const auto shape = mm->get_output_partial_shape(0); 75 if (shape.rank() != 4 || shape[0] != 1 || shape[1] != 1 || shape[3].is_dynamic() || 76 mm->get_transpose_a() || mm->get_input_partial_shape(0).rank() != 4 || 77 mm->get_input_partial_shape(1).rank() != 2 || (sm->get_axis() != -1 && sm->get_axis() != 3) || 78 tk->get_axis() != 3 || tk->get_mode() != v11::TopK::Mode::MAX || 79 tk->get_sort_type() != v11::TopK::SortType::SORT_VALUES || tk->get_stable() || 80 tk->get_index_element_type() != ov::element::i32 || 81 !tk->output(0).get_target_inputs().empty()) { 82 return false; 83 } 84 const int64_t experts = shape[3].get_length(); 85 auto end = ov::as_type_ptr(node(ids)->get_input_node_shared_ptr(2)); 86 if (!end || !end->get_element_type().is_integral_number() || ov::shape_size(end->get_shape()) != 1) { 87 return false; 88 } 89 const int64_t k = end->cast_vector()[0]; 90 const auto sorted_shape = tk->get_output_partial_shape(1); 91 if (k <= 0 || k > experts || sorted_shape[3].is_dynamic() || sorted_shape[3].get_length() < k || 92 !input_is(probs, 1, {1, -1, experts, 1}) || 93 !input_is(data, 1, {0}) || !input_is(ids_2d, 1, {0, 1}) || 94 !input_is(weights_4d, 1, {0}) || !input_is(weights, 1, {1, 1, -1, k}) || 95 ov::as_type_ptr(node(probs))->get_special_zero() || 96 ov::as_type_ptr(node(weights))->get_special_zero() || 97 !input_is(gather, 2, {1}) || ov::as_type_ptr(node(gather))->get_batch_dims() != 1 || 98 !input_is(data_batch, 1, {0}) || !input_is(data_batch, 2, {0}) || 99 !input_is(ids_count, 1, {1}) || !input_is(ids_count, 2, {0}) || 100 ov::as_type_ptr(node(data_batch))->get_batch_dims() != 0 || 101 ov::as_type_ptr(node(ids_count))->get_batch_dims() != 0 || 102 ov::as_type_ptr(node(target))->get_axis() != 0 || 103 ov::as_type_ptr(node(broadcast))->get_broadcast_spec().m_type != ov::op::BroadcastType::BIDIRECTIONAL || 104 !reduce->get_keep_dims() || (!input_is(sum, 1, {-1}) && !input_is(sum, 1, {3})) || 105 !input_is(tile, 1, {1, 1, 1, k}) || 106 ov::as_type_ptr(node(norm))->get_autob().m_type != ov::op::AutoBroadcastType::NUMPY) { 107 return false; 108 } 109 // The top-k softmax sum is at least k / experts. Leave margin for rounding. 110 // This guard is only valid for unbiased softmax routing. 111 if (!(limit->get_min() <= 0.5 * double(k) / experts && limit->get_max() >= 2.0)) { 112 return false; 113 } 114 std::vector> slices; 115 for (const auto & input : tk->output(1).get_target_inputs()) { 116 auto slice = ov::as_type_ptr(input.get_node()->shared_from_this()); 117 if (!slice || input.get_index() != 0 || slice->get_input_size() != 5 || 118 !constant_is(slice->input_value(1), {0}) || !constant_is(slice->input_value(2), {k}) || 119 !constant_is(slice->input_value(3), {1}) || 120 (!constant_is(slice->input_value(4), {3}) && !constant_is(slice->input_value(4), {-1}))) { 121 return false; 122 } 123 slices.push_back(slice); 124 } 125 126 // Remove GGML's leading singleton dimension while building the plugin pattern, 127 // then restore it on both outputs for the following GGML nodes. 128 auto axis0 = v0::Constant::create(ov::element::i64, ov::Shape{1}, {0}); 129 auto hidden = std::make_shared(mm->input_value(0), axis0); 130 auto routing = std::make_shared(hidden, mm->input_value(1), false, mm->get_transpose_b()); 131 auto probabilities = std::make_shared(routing, -1); 132 auto selected = std::make_shared(probabilities, 133 v0::Constant::create(ov::element::i64, ov::Shape{}, {k}), 2, 134 v11::TopK::Mode::MAX, v11::TopK::SortType::SORT_VALUES, tk->get_index_element_type(), false); 135 auto total = std::make_shared(selected->output(0), 136 v0::Constant::create(ov::element::i64, ov::Shape{1}, {-1}), true); 137 auto normalized = std::make_shared(selected->output(0), total); 138 auto weights_out = std::make_shared(normalized, axis0); 139 auto ids_out = std::make_shared(selected->output(1), axis0); 140 ov::copy_runtime_info({mm, sm, tk, node(norm)}, {hidden, routing, probabilities, selected, total, normalized, weights_out, ids_out}); 141 ov::replace_node(node(norm), weights_out); 142 for (const auto & slice : slices) { 143 ov::replace_node(slice, ids_out); 144 } 145 return true; 146 }; 147 register_matcher(std::make_shared(norm, "ov::frontend::ggml::pass::FuseMoeRouter"), callback); 148 } 149 150 } // namespace pass 151 } // namespace ggml 152 } // namespace frontend 153 } // namespace ov