ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-openvino / openvino / pass / fuse_moe_router.cpp
| 1 | #include "fuse_moe_router.h" |
| 2 | |
| 3 | #include <openvino/core/graph_util.hpp> |
| 4 | #include <openvino/core/rt_info.hpp> |
| 5 | #include <openvino/op/broadcast.hpp> |
| 6 | #include <openvino/op/clamp.hpp> |
| 7 | #include <openvino/op/concat.hpp> |
| 8 | #include <openvino/op/constant.hpp> |
| 9 | #include <openvino/op/divide.hpp> |
| 10 | #include <openvino/op/gather.hpp> |
| 11 | #include <openvino/op/matmul.hpp> |
| 12 | #include <openvino/op/reduce_sum.hpp> |
| 13 | #include <openvino/op/reshape.hpp> |
| 14 | #include <openvino/op/shape_of.hpp> |
| 15 | #include <openvino/op/slice.hpp> |
| 16 | #include <openvino/op/softmax.hpp> |
| 17 | #include <openvino/op/squeeze.hpp> |
| 18 | #include <openvino/op/tile.hpp> |
| 19 | #include <openvino/op/topk.hpp> |
| 20 | #include <openvino/op/unsqueeze.hpp> |
| 21 | #include <openvino/pass/pattern/op/wrap_type.hpp> |
| 22 | |
| 23 | namespace ov { |
| 24 | namespace frontend { |
| 25 | namespace ggml { |
| 26 | namespace pass { |
| 27 | |
| 28 | namespace { |
| 29 | |
| 30 | bool constant_is(const ov::Output<ov::Node> & output, const std::vector<int64_t> & values) { |
| 31 | auto node = ov::as_type_ptr<ov::op::v0::Constant>(output.get_node_shared_ptr()); |
| 32 | return node && node->get_element_type().is_integral_number() && node->cast_vector<int64_t>() == 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<v0::MatMul>(); |
| 44 | auto softmax = wrap_type<v8::Softmax>({logits}); |
| 45 | auto topk = wrap_type<v11::TopK>({softmax, any_input()}); |
| 46 | topk->set_output_size(2); |
| 47 | auto ids = wrap_type<v8::Slice>({topk->output(1), any_input(), any_input(), any_input(), any_input()}); |
| 48 | auto ids_2d = wrap_type<v0::Squeeze>({ids, any_input()}); |
| 49 | auto probs = wrap_type<v1::Reshape>({softmax, any_input()}); |
| 50 | auto data = wrap_type<v0::Squeeze>({probs, any_input()}); |
| 51 | auto data_batch = wrap_type<v8::Gather>({wrap_type<v3::ShapeOf>({data}), any_input(), any_input()}); |
| 52 | auto ids_count = wrap_type<v8::Gather>({wrap_type<v3::ShapeOf>({ids_2d}), any_input(), any_input()}); |
| 53 | auto target = wrap_type<v0::Concat>({data_batch, ids_count}); |
| 54 | auto broadcast = wrap_type<v3::Broadcast>({ids_2d, target}); |
| 55 | auto gather = wrap_type<v8::Gather>({data, broadcast, any_input()}); |
| 56 | auto weights_4d = wrap_type<v0::Unsqueeze>({gather, any_input()}); |
| 57 | auto weights = wrap_type<v1::Reshape>({weights_4d, any_input()}); |
| 58 | auto sum = wrap_type<v1::ReduceSum>({weights, any_input()}); |
| 59 | auto clamp = wrap_type<v0::Clamp>({sum}); |
| 60 | auto tile = wrap_type<v0::Tile>({clamp, any_input()}); |
| 61 | auto norm = wrap_type<v1::Divide>({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<ov::Node> & p) { return pm.at(p).get_node_shared_ptr(); }; |
| 66 | const auto input_is = [&](const std::shared_ptr<ov::Node> & p, size_t i, const std::vector<int64_t> & v) { |
| 67 | return constant_is(node(p)->input_value(i), v); |
| 68 | }; |
| 69 | auto mm = ov::as_type_ptr<v0::MatMul>(node(logits)); |
| 70 | auto sm = ov::as_type_ptr<v8::Softmax>(node(softmax)); |
| 71 | auto tk = ov::as_type_ptr<v11::TopK>(node(topk)); |
| 72 | auto reduce = ov::as_type_ptr<v1::ReduceSum>(node(sum)); |
| 73 | auto limit = ov::as_type_ptr<v0::Clamp>(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<v0::Constant>(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<int64_t>()[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<v1::Reshape>(node(probs))->get_special_zero() || |
| 96 | ov::as_type_ptr<v1::Reshape>(node(weights))->get_special_zero() || |
| 97 | !input_is(gather, 2, {1}) || ov::as_type_ptr<v8::Gather>(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<v8::Gather>(node(data_batch))->get_batch_dims() != 0 || |
| 101 | ov::as_type_ptr<v8::Gather>(node(ids_count))->get_batch_dims() != 0 || |
| 102 | ov::as_type_ptr<v0::Concat>(node(target))->get_axis() != 0 || |
| 103 | ov::as_type_ptr<v3::Broadcast>(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<v1::Divide>(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<std::shared_ptr<ov::Node>> slices; |
| 115 | for (const auto & input : tk->output(1).get_target_inputs()) { |
| 116 | auto slice = ov::as_type_ptr<v8::Slice>(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<v0::Squeeze>(mm->input_value(0), axis0); |
| 130 | auto routing = std::make_shared<v0::MatMul>(hidden, mm->input_value(1), false, mm->get_transpose_b()); |
| 131 | auto probabilities = std::make_shared<v8::Softmax>(routing, -1); |
| 132 | auto selected = std::make_shared<v11::TopK>(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<v1::ReduceSum>(selected->output(0), |
| 136 | v0::Constant::create(ov::element::i64, ov::Shape{1}, {-1}), true); |
| 137 | auto normalized = std::make_shared<v1::Divide>(selected->output(0), total); |
| 138 | auto weights_out = std::make_shared<v0::Unsqueeze>(normalized, axis0); |
| 139 | auto ids_out = std::make_shared<v0::Unsqueeze>(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<Matcher>(norm, "ov::frontend::ggml::pass::FuseMoeRouter"), callback); |
| 148 | } |
| 149 | |
| 150 | } // namespace pass |
| 151 | } // namespace ggml |
| 152 | } // namespace frontend |
| 153 | } // namespace ov |