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 / squeeze_matmul.cpp
| 1 | #include "squeeze_matmul.h" |
| 2 | |
| 3 | #include <openvino/core/graph_util.hpp> |
| 4 | #include <openvino/core/rt_info.hpp> |
| 5 | #include <openvino/core/type.hpp> |
| 6 | #include <openvino/op/constant.hpp> |
| 7 | #include <openvino/op/matmul.hpp> |
| 8 | #include <openvino/op/squeeze.hpp> |
| 9 | #include <openvino/op/unsqueeze.hpp> |
| 10 | #include <openvino/pass/pattern/op/label.hpp> |
| 11 | #include <openvino/pass/pattern/op/pattern.hpp> |
| 12 | #include <openvino/pass/pattern/op/wrap_type.hpp> |
| 13 | |
| 14 | namespace opp = ov::pass::pattern; |
| 15 | |
| 16 | namespace ov { |
| 17 | namespace frontend { |
| 18 | namespace ggml { |
| 19 | namespace pass { |
| 20 | |
| 21 | // For quantized models, NPUW expects the activation to be 3d in DQ(DynamicQuantization) opt, e.g. DQMatMulGQ2i |
| 22 | SqueezeMatmul::SqueezeMatmul() { |
| 23 | auto m_act = opp::any_input(); |
| 24 | auto m_wei = opp::any_input(); |
| 25 | auto m_matmul = opp::wrap_type<ov::op::v0::MatMul>({m_act, m_wei}); |
| 26 | |
| 27 | const auto callback = [=](ov::pass::pattern::Matcher & m) { |
| 28 | const auto & pattern_map = m.get_pattern_value_map(); |
| 29 | auto matmul_node = |
| 30 | ov::as_type_ptr<ov::op::v0::MatMul>(pattern_map.at(m_matmul).get_node_shared_ptr()); |
| 31 | auto act = pattern_map.at(m_act); |
| 32 | auto wei = pattern_map.at(m_wei); |
| 33 | auto act_shape = act.get_partial_shape(); |
| 34 | auto wei_shape = wei.get_partial_shape(); |
| 35 | if (act_shape.rank().is_dynamic() || wei_shape.rank().is_dynamic()) { |
| 36 | return false; |
| 37 | } |
| 38 | if (act_shape.rank().get_length() == 4 && wei_shape.rank().get_length() == 2) { |
| 39 | auto axis = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, {0}); |
| 40 | auto squeezed_act = std::make_shared<ov::op::v0::Squeeze>(act, axis); |
| 41 | auto new_matmul = std::make_shared<ov::op::v0::MatMul>(squeezed_act, wei, matmul_node->get_transpose_a(), |
| 42 | matmul_node->get_transpose_b()); |
| 43 | auto unsqueezed_output = std::make_shared<ov::op::v0::Unsqueeze>(new_matmul, axis); |
| 44 | unsqueezed_output->set_friendly_name(matmul_node->get_friendly_name()); |
| 45 | ov::copy_runtime_info(matmul_node, {squeezed_act, new_matmul, unsqueezed_output}); |
| 46 | ov::replace_node(matmul_node, unsqueezed_output); |
| 47 | return true; |
| 48 | } |
| 49 | return false; |
| 50 | }; |
| 51 | |
| 52 | register_matcher(std::make_shared<ov::pass::pattern::Matcher>(m_matmul, "ov::frontend::ggml::pass::SqueezeMatmul"), |
| 53 | callback); |
| 54 | } |
| 55 | |
| 56 | } // namespace pass |
| 57 | } // namespace ggml |
| 58 | } // namespace frontend |
| 59 | } // namespace ov |