OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
Transformations Infrastructure: MultiMatcher (#30587)
Ivan Tikhonov652e211ad0dc
diff --git a/src/core/include/openvino/pass/pattern/multi_matcher.hpp b/src/core/include/openvino/pass/pattern/multi_matcher.hppnew file mode 100644index 00000000000..b00c037a623--- /dev/null+++ b/src/core/include/openvino/pass/pattern/multi_matcher.hpp@@ -0,0 +1,80 @@+// Copyright (C) 2018-2025 Intel Corporation+// SPDX-License-Identifier: Apache-2.0++#pragma once++#include "openvino/pass/pass.hpp"+#include "openvino/pass/pattern/matcher.hpp"++namespace ov::pass {++/**+ * @brief MultiMatcher applies multiple independent pattern matchers across the graph.+ *+ * Each registered pattern is independently matched across all graph nodes.+ * Matches are grouped by pattern root and passed to a single callback.+ *+ * This is especially useful for repeated blocks like attention heads, Q/K/V projections,+ * and residual connections.+ *+ * Match repeated Q/K/V branches+ *+ * ┌──────────── Pattern 1 ─────────────┐ ┌──────────── Pattern 2 ─────────────┐+ * │ │ │ │+ * │ Input ──► MatMul_Q ──► Add_Q │ │ Input ──► MatMul_K ──► Add_K │+ * └────────────────────────────────────┘ └────────────────────────────────────┘+ * │ │+ * ▼ ▼+ * concat_qkv concat_qkv+ * │ │+ * ▼ ▼+ * SDPA (shared)+ *+ * Each Q/K/V branch is matched separately using a different pattern.+ *+ * After matching, callback receives:+ *+ * matches = {+ * Pattern1: [MatchQ1, MatchQ2, ...],+ * Pattern2: [MatchK1, MatchK2, ...],+ * Pattern3: [MatchV1, MatchV2, ...]+ * }+ */+class OPENVINO_API MultiMatcher : public ov::pass::ModelPass {+public:+ using Callback =+ std::function<void(const std::unordered_map<std::shared_ptr<Node>, std::vector<pattern::PatternValueMap>>&)>;++ OPENVINO_RTTI("MultiMatcher", "0", ModelPass);++ explicit MultiMatcher(const std::string& name = "MultiMatcher");++ /**+ * @brief Register multiple patterns with a unified callback+ * @param patterns Vector of pattern root nodes+ * @param callback Callback applied to all matches grouped by pattern+ * @param strict Whether to use strict mode in Matcher+ */+ void register_patterns(const std::vector<std::shared_ptr<Node>>& patterns, Callback callback, bool strict = false);++ /**+ * @brief Run all matchers once over the model+ * @return true if any matches were found and callback invoked+ */+ bool run_on_model(const std::shared_ptr<Model>& model) override;++private:+ struct PatternEntry {+ ov::Output<ov::Node> pattern;+ std::shared_ptr<ov::Node> root_ptr;+ bool strict_mode = false;+ };++ std::string m_name;+ Callback m_callback;+ std::vector<PatternEntry> m_patterns;+ std::unordered_set<Node*> m_matched_nodes;+ std::unordered_set<Node*> m_all_roots;+};++} // namespace ov::passdiff --git a/src/core/src/pattern/multi_matcher.cpp b/src/core/src/pattern/multi_matcher.cppnew file mode 100644index 00000000000..1f4d01040ff--- /dev/null+++ b/src/core/src/pattern/multi_matcher.cpp@@ -0,0 +1,51 @@+// Copyright (C) 2018-2025 Intel Corporation+// SPDX-License-Identifier: Apache-2.0++#include "openvino/pass/pattern/multi_matcher.hpp"++using namespace ov::pass;+using namespace ov::pass::pattern;++MultiMatcher::MultiMatcher(const std::string& name) : m_name(name) {}++void MultiMatcher::register_patterns(const ov::NodeVector& patterns, Callback callback, bool strict) {+ m_callback = std::move(callback);+ m_patterns.clear();+ m_all_roots.clear();++ for (const auto& p : patterns) {+ m_patterns.push_back(PatternEntry{p->output(0), p, strict});+ }+}++bool MultiMatcher::run_on_model(const std::shared_ptr<Model>& model) {+ bool changed = false;+ m_matched_nodes.clear();++ std::unordered_map<std::shared_ptr<Node>, std::vector<PatternValueMap>> matches_by_pattern;+ for (const auto& node : model->get_ordered_ops()) {+ for (const auto& pattern : m_patterns) {+ Matcher matcher(pattern.pattern, m_name, pattern.strict_mode);+ if (!matcher.match(node->output(0)))+ continue;++ m_all_roots.insert(node.get());++ const auto& match_map = matcher.get_pattern_value_map();+ const auto& matched_nodes = matcher.get_matched_nodes();++ for (const auto& n : matched_nodes)+ m_matched_nodes.insert(n.get());++ matches_by_pattern[pattern.root_ptr].push_back(match_map);+ break; // Skip trying other patterns on this node+ }+ }++ if (!matches_by_pattern.empty()) {+ m_callback(matches_by_pattern);+ changed = true;+ }++ return changed;+}diff --git a/src/core/tests/multi_matcher_pass.cpp b/src/core/tests/multi_matcher_pass.cppnew file mode 100644index 00000000000..09c23a70102--- /dev/null+++ b/src/core/tests/multi_matcher_pass.cpp@@ -0,0 +1,228 @@+// Copyright (C) 2018-2025 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include <gtest/gtest.h>++#include <algorithm>+#include <cstdio>+#include <iostream>+#include <list>+#include <memory>++#include "common_test_utils/matcher.hpp"+#include "common_test_utils/ov_test_utils.hpp"+#include "common_test_utils/test_tools.hpp"+#include "openvino/core/except.hpp"+#include "openvino/core/graph_util.hpp"+#include "openvino/op/abs.hpp"+#include "openvino/op/add.hpp"+#include "openvino/op/broadcast.hpp"+#include "openvino/op/constant.hpp"+#include "openvino/op/cos.hpp"+#include "openvino/op/cosh.hpp"+#include "openvino/op/divide.hpp"+#include "openvino/op/matmul.hpp"+#include "openvino/op/relu.hpp"+#include "openvino/op/sigmoid.hpp"+#include "openvino/pass/pattern/multi_matcher.hpp"+#include "openvino/pass/pattern/op/wrap_type.hpp"++using namespace ov::pass;+using namespace ov::pass::pattern;+using namespace ov::pass::pattern::op;++namespace ov::test {++// Match multiple independent MatMul nodes+TEST(MultiMatcherTest, matches_multiple_matmuls) {+ auto input1 = std::make_shared<ov::op::v0::Parameter>(element::f32, Shape{1, 4});+ auto input2 = std::make_shared<ov::op::v0::Parameter>(element::f32, Shape{1, 4});+ auto input3 = std::make_shared<ov::op::v0::Parameter>(element::f32, Shape{1, 4});++ auto weight1 = ov::op::v0::Constant::create(element::f32, Shape{4, 4}, {0});+ auto weight2 = ov::op::v0::Constant::create(element::f32, Shape{4, 4}, {0});+ auto weight3 = ov::op::v0::Constant::create(element::f32, Shape{4, 4}, {0});++ auto mm1 = std::make_shared<ov::op::v0::MatMul>(input1, weight1);+ auto mm2 = std::make_shared<ov::op::v0::MatMul>(input2, weight2);+ auto mm3 = std::make_shared<ov::op::v0::MatMul>(input3, weight3);++ auto model = std::make_shared<Model>(OutputVector{mm1, mm2, mm3}, ParameterVector{input1, input2, input3});++ auto a = any_input();+ auto b = any_input();+ auto pat = wrap_type<ov::op::v0::MatMul>({a, b});++ MultiMatcher matcher("matmul");+ int count = 0;+ matcher.register_patterns({pat}, [&](const auto& matches) {+ count = static_cast<int>(matches.at(pat).size());+ });++ ASSERT_TRUE(matcher.run_on_model(model));+ EXPECT_EQ(count, 3);+}++// Overlapping is allowed, probably will be disabled in the future+TEST(MultiMatcherTest, matches_overlapped_add_and_matmul) {+ auto input = std::make_shared<ov::op::v0::Parameter>(element::f32, Shape{1, 4});+ auto w1 = ov::op::v0::Constant::create(element::f32, Shape{4, 4}, {0});+ auto w2 = ov::op::v0::Constant::create(element::f32, Shape{1, 4}, {1});+ auto mm = std::make_shared<ov::op::v0::MatMul>(input, w1);+ auto add = std::make_shared<ov::op::v1::Add>(mm, w2);++ auto model = std::make_shared<Model>(OutputVector{add}, ParameterVector{input});++ auto a = any_input();+ auto b = any_input();+ auto pat_matmul = wrap_type<ov::op::v0::MatMul>({a, b});+ auto pat_add = wrap_type<ov::op::v1::Add>({pat_matmul, any_input()});++ int adds = 0, matmuls = 0;++ MultiMatcher matcher("mixed");+ matcher.register_patterns({pat_add, pat_matmul}, [&](const auto& matches) {+ if (matches.count(pat_add))+ adds = static_cast<int>(matches.at(pat_add).size());+ if (matches.count(pat_matmul))+ matmuls = static_cast<int>(matches.at(pat_matmul).size());+ });++ ASSERT_TRUE(matcher.run_on_model(model));+ EXPECT_EQ(adds, 1);+ EXPECT_EQ(matmuls, 1);+}++// Overlapping is allowed, probably will be disabled in the future+TEST(MultiMatcherTest, overlap_between_patterns) {+ auto input = std::make_shared<ov::op::v0::Parameter>(element::f32, Shape{1, 4});+ auto w1 = ov::op::v0::Constant::create(element::f32, Shape{4, 4}, {1});+ auto w2 = ov::op::v0::Constant::create(element::f32, Shape{4, 4}, {1});+ auto mm1 = std::make_shared<ov::op::v0::MatMul>(input, w1);+ auto mm2 = std::make_shared<ov::op::v0::MatMul>(input, w2);++ auto model = std::make_shared<Model>(OutputVector{mm1, mm2}, ParameterVector{input});++ auto a = any_input();+ auto b = any_input();+ auto pat = wrap_type<ov::op::v0::MatMul>({a, b});++ int count = 0;+ MultiMatcher matcher("conflict_test");+ matcher.register_patterns({pat}, [&](const auto& matches) {+ count = static_cast<int>(matches.at(pat).size());+ });++ ASSERT_TRUE(matcher.run_on_model(model));+ EXPECT_EQ(count, 2);+}++// No match should be found+TEST(MultiMatcherTest, no_match_found) {+ auto input = std::make_shared<ov::op::v0::Parameter>(element::f32, Shape{1});+ auto relu = std::make_shared<ov::op::v0::Relu>(input);+ auto model = std::make_shared<Model>(OutputVector{relu}, ParameterVector{input});++ auto a = any_input();+ auto b = any_input();+ auto pat = wrap_type<ov::op::v0::MatMul>({a, b});++ bool callback_invoked = false;+ MultiMatcher matcher("no_match");+ matcher.register_patterns({pat}, [&](const auto&) {+ callback_invoked = true;+ });++ ASSERT_FALSE(matcher.run_on_model(model));+ EXPECT_FALSE(callback_invoked);+}++// Match different operator types (Relu and Sigmoid)+TEST(MultiMatcherTest, matches_multiple_pattern_types) {+ auto input1 = std::make_shared<ov::op::v0::Parameter>(element::f32, Shape{1});+ auto relu = std::make_shared<ov::op::v0::Relu>(input1);++ auto input2 = std::make_shared<ov::op::v0::Parameter>(element::f32, Shape{1});+ auto sigmoid = std::make_shared<ov::op::v0::Sigmoid>(input2);++ auto model = std::make_shared<Model>(OutputVector{relu, sigmoid}, ParameterVector{input1, input2});++ auto relu_pat = wrap_type<ov::op::v0::Relu>({any_input()});+ auto sigm_pat = wrap_type<ov::op::v0::Sigmoid>({any_input()});++ int relu_count = 0;+ int sigm_count = 0;++ MultiMatcher matcher("types");+ matcher.register_patterns({relu_pat, sigm_pat}, [&](const auto& matches) {+ if (matches.count(relu_pat))+ relu_count = static_cast<int>(matches.at(relu_pat).size());+ if (matches.count(sigm_pat))+ sigm_count = static_cast<int>(matches.at(sigm_pat).size());+ });++ ASSERT_TRUE(matcher.run_on_model(model));+ EXPECT_EQ(relu_count, 1);+ EXPECT_EQ(sigm_count, 1);+}++// Multiple matches from one pattern+TEST(MultiMatcherTest, grouped_matches_from_single_pattern) {+ auto input1 = std::make_shared<ov::op::v0::Parameter>(element::f32, Shape{1});+ auto relu1 = std::make_shared<ov::op::v0::Relu>(input1);++ auto input2 = std::make_shared<ov::op::v0::Parameter>(element::f32, Shape{1});+ auto relu2 = std::make_shared<ov::op::v0::Relu>(input2);++ auto model = std::make_shared<Model>(OutputVector{relu1, relu2}, ParameterVector{input1, input2});++ auto pat = wrap_type<ov::op::v0::Relu>({any_input()});++ int count = 0;+ MultiMatcher matcher("multi_out");+ matcher.register_patterns({pat}, [&](const auto& matches) {+ count = static_cast<int>(matches.at(pat).size());+ });++ ASSERT_TRUE(matcher.run_on_model(model));+ EXPECT_EQ(count, 2);+}++TEST(MultiMatcherTest, uses_pattern_value_map_contents) {+ auto input = std::make_shared<ov::op::v0::Parameter>(element::f32, Shape{1, 4});+ auto weights = ov::op::v0::Constant::create(element::f32, Shape{4, 4}, {1});+ auto matmul = std::make_shared<ov::op::v0::MatMul>(input, weights);++ auto model = std::make_shared<Model>(OutputVector{matmul}, ParameterVector{input});++ auto pat_input = any_input();+ auto pat_weight = any_input();+ auto pat_matmul = wrap_type<ov::op::v0::MatMul>({pat_input, pat_weight});++ bool callback_triggered = false;++ MultiMatcher matcher("value_map");+ matcher.register_patterns({pat_matmul}, [&](const auto& matches) {+ callback_triggered = true;+ ASSERT_EQ(matches.at(pat_matmul).size(), 1);++ const auto& map = matches.at(pat_matmul)[0];+ ASSERT_TRUE(map.count(pat_input));+ ASSERT_TRUE(map.count(pat_weight));+ ASSERT_TRUE(map.count(pat_matmul));++ auto captured_input = map.at(pat_input).get_node_shared_ptr();+ auto captured_weight = map.at(pat_weight).get_node_shared_ptr();+ auto captured_mm = map.at(pat_matmul).get_node_shared_ptr();++ EXPECT_EQ(captured_input, input);+ EXPECT_EQ(captured_weight, weights);+ EXPECT_EQ(captured_mm, matmul);+ });++ ASSERT_TRUE(matcher.run_on_model(model));+ EXPECT_TRUE(callback_triggered);+}++} // namespace ov::test