Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

Transformations Infrastructure: pattern::Block (#31415)

Ivan Tikhonov2841da5eafff
diff --git a/src/core/include/openvino/pass/pattern/op/block.hpp b/src/core/include/openvino/pass/pattern/op/block.hpp
new file mode 100644
index 00000000000..6c4e3de82de
--- /dev/null
+++ b/src/core/include/openvino/pass/pattern/op/block.hpp
@@ -0,0 +1,105 @@
+#pragma once
+
+#include <optional>
+
+#include "openvino/pass/pattern/matcher.hpp"
+#include "openvino/pass/pattern/op/block_util.hpp"
+#include "openvino/pass/pattern/op/pattern.hpp"
+
+namespace {
+
+// _MAKE_ANCHOR is an internal macro for REGISTER_ANCHORS that is not supposed to used separately.
+#define _MAKE_ANCHOR(x) block->register_anchor(#x, x);
+
+} // namespace
+
+namespace ov::pass::pattern::op {
+
+/**
+ * @brief A utility macro to register named anchors in a Block.
+ *
+ * This macro simplifies the process of registering multiple anchors in a Block.
+ * It expands to a series of calls to `block->register_anchor(...)` for each provided argument.
+ *
+ */
+
+#define REGISTER_ANCHORS(block, ...) \
+ do { \
+ FOR_EACH(_MAKE_ANCHOR, __VA_ARGS__) \
+ } while (0)
+
+/**
+ * @brief Block is a reusable subgraph pattern composed of named inputs and outputs.
+ *
+ * It wraps a group of connected pattern nodes and allows treating them as a single unit.
+ * Block uses a local Matcher internally and merges its results into the main matcher context.
+ *
+ * Example:
+ *
+ * Input (x)
+ * │
+ * ▼
+ * ┌────────────────────┐
+ * │ Block: │
+ * │ "norm_block" │
+ * │ │
+ * │ NormalizeL2 │
+ * │ │ │
+ * │ ▼ │
+ * │ Multiply |
+ * └────────────────────┘
+ * │
+ * Output
+ *
+ * Block inputs/outputs are exposed for use in higher-level patterns.
+ */
+class OPENVINO_API Block : public Pattern {
+public:
+ OPENVINO_RTTI("Block");
+
+ Block(const OutputVector& inputs, const OutputVector& outputs, const std::string& name = "");
+
+ bool match_value(Matcher* matcher, const Output<Node>& pattern_value, const Output<Node>& graph_value) override;
+
+ const OutputVector& get_inputs() const {
+ return m_inputs;
+ }
+
+ const OutputVector& get_outputs() const {
+ return m_outputs;
+ }
+
+ void register_anchor(const std::string& name, const ov::Output<ov::Node>& output) {
+ m_named_anchors[name] = output;
+ }
+
+ std::optional<Output<Node>> get_anchor(const std::string& name, const PatternValueMap& pm) const {
+ if (m_named_anchors.empty()) {
+ return std::nullopt;
+ }
+ auto it = m_named_anchors.find(name);
+ if (it == m_named_anchors.end()) {
+ return std::nullopt;
+ }
+
+ auto pattern_node = it->second.get_node_shared_ptr();
+ auto matched_it = pm.find(pattern_node);
+ if (matched_it == pm.end()) {
+ return std::nullopt;
+ }
+
+ return matched_it->second;
+ }
+
+ std::map<std::string, Output<Node>>& get_registered_anchors() {
+ return m_named_anchors;
+ }
+
+private:
+ OutputVector m_inputs;
+ OutputVector m_outputs;
+
+ std::map<std::string, Output<Node>> m_named_anchors;
+};
+
+} // namespace ov::pass::pattern::op
\ No newline at end of file
diff --git a/src/core/include/openvino/pass/pattern/op/block_util.hpp b/src/core/include/openvino/pass/pattern/op/block_util.hpp
new file mode 100644
index 00000000000..9e334bc0f4e
--- /dev/null
+++ b/src/core/include/openvino/pass/pattern/op/block_util.hpp
@@ -0,0 +1,53 @@
+#pragma once
+
+namespace {
+
+// FOR_EACH macros up to 16 arguments:
+#define FOR_EACH_1(M, x1) M(x1)
+#define FOR_EACH_2(M, x1, x2) M(x1) M(x2)
+#define FOR_EACH_3(M, x1, x2, x3) M(x1) M(x2) M(x3)
+#define FOR_EACH_4(M, x1, x2, x3, x4) M(x1) M(x2) M(x3) M(x4)
+#define FOR_EACH_5(M, x1, x2, x3, x4, x5) M(x1) M(x2) M(x3) M(x4) M(x5)
+#define FOR_EACH_6(M, x1, x2, x3, x4, x5, x6) M(x1) M(x2) M(x3) M(x4) M(x5) M(x6)
+#define FOR_EACH_7(M, x1, x2, x3, x4, x5, x6, x7) M(x1) M(x2) M(x3) M(x4) M(x5) M(x6) M(x7)
+#define FOR_EACH_8(M, x1, x2, x3, x4, x5, x6, x7, x8) M(x1) M(x2) M(x3) M(x4) M(x5) M(x6) M(x7) M(x8)
+#define FOR_EACH_9(M, x1, x2, x3, x4, x5, x6, x7, x8, x9) M(x1) M(x2) M(x3) M(x4) M(x5) M(x6) M(x7) M(x8) M(x9)
+#define FOR_EACH_10(M, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10) \
+ M(x1) M(x2) M(x3) M(x4) M(x5) M(x6) M(x7) M(x8) M(x9) M(x10)
+#define FOR_EACH_11(M, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11) \
+ M(x1) M(x2) M(x3) M(x4) M(x5) M(x6) M(x7) M(x8) M(x9) M(x10) M(x11)
+#define FOR_EACH_12(M, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12) \
+ M(x1) M(x2) M(x3) M(x4) M(x5) M(x6) M(x7) M(x8) M(x9) M(x10) M(x11) M(x12)
+#define FOR_EACH_13(M, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13) \
+ M(x1) M(x2) M(x3) M(x4) M(x5) M(x6) M(x7) M(x8) M(x9) M(x10) M(x11) M(x12) M(x13)
+#define FOR_EACH_14(M, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14) \
+ M(x1) M(x2) M(x3) M(x4) M(x5) M(x6) M(x7) M(x8) M(x9) M(x10) M(x11) M(x12) M(x13) M(x14)
+#define FOR_EACH_15(M, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15) \
+ M(x1) M(x2) M(x3) M(x4) M(x5) M(x6) M(x7) M(x8) M(x9) M(x10) M(x11) M(x12) M(x13) M(x14) M(x15)
+#define FOR_EACH_16(M, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16) \
+ M(x1) M(x2) M(x3) M(x4) M(x5) M(x6) M(x7) M(x8) M(x9) M(x10) M(x11) M(x12) M(x13) M(x14) M(x15) M(x16)
+
+#define GET_MACRO(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, NAME, ...) NAME
+
+#define EXPAND(x) x
+
+#define FOR_EACH(M, ...) \
+ EXPAND(GET_MACRO(__VA_ARGS__, \
+ FOR_EACH_16, \
+ FOR_EACH_15, \
+ FOR_EACH_14, \
+ FOR_EACH_13, \
+ FOR_EACH_12, \
+ FOR_EACH_11, \
+ FOR_EACH_10, \
+ FOR_EACH_9, \
+ FOR_EACH_8, \
+ FOR_EACH_7, \
+ FOR_EACH_6, \
+ FOR_EACH_5, \
+ FOR_EACH_4, \
+ FOR_EACH_3, \
+ FOR_EACH_2, \
+ FOR_EACH_1)(M, __VA_ARGS__))
+
+} // namespace
\ No newline at end of file
diff --git a/src/core/src/pattern/op/block.cpp b/src/core/src/pattern/op/block.cpp
new file mode 100644
index 00000000000..cac68a69bc2
--- /dev/null
+++ b/src/core/src/pattern/op/block.cpp
@@ -0,0 +1,56 @@
+
+#include "openvino/pass/pattern/op/block.hpp"
+
+#include "openvino/core/rt_info.hpp"
+
+using namespace ov;
+using namespace ov::pass::pattern;
+using namespace ov::pass::pattern::op;
+
+Block::Block(const OutputVector& inputs, const OutputVector& outputs, const std::string& name)
+ : Pattern(inputs, op::Predicate{}),
+ m_inputs(inputs),
+ m_outputs(outputs) {
+ set_output_type(0, element::dynamic, PartialShape::dynamic());
+ set_friendly_name(name);
+}
+
+bool Block::match_value(Matcher* matcher, const Output<Node>& pattern_value, const Output<Node>& graph_value) {
+ auto block_pattern_root = m_outputs.front();
+
+ // Using a local matcher to match only those patterns that are encapsulated into the current Block.
+ auto local_matcher = std::make_shared<Matcher>(block_pattern_root.get_node_shared_ptr(), "BlockMatcher");
+ if (!local_matcher->match_value(block_pattern_root, graph_value)) {
+ return false;
+ }
+
+ auto& local_pm = local_matcher->get_pattern_value_map();
+
+ OutputVector real_inputs, real_outputs;
+ for (const auto& input : m_inputs) {
+ if (local_pm.count(input.get_node_shared_ptr())) {
+ real_inputs.push_back(local_pm.at(input.get_node_shared_ptr()));
+ }
+ }
+
+ for (const auto& output : m_outputs) {
+ if (local_pm.count(output.get_node_shared_ptr())) {
+ real_outputs.push_back(local_pm.at(output.get_node_shared_ptr()));
+ }
+ }
+
+ // Creating a mapping Pattern Block -> Graph Block.
+ // Pattern Block contains inputs, outputs from the pattern graph
+ // Graph Block contains inputs, outputs from the real graph (ov::Model)
+ auto matched_block = std::make_shared<Block>(real_inputs, real_outputs, get_friendly_name());
+
+ // Merge the local_matcher state to the external matcher.
+ auto& pattern_map = matcher->get_pattern_value_map();
+ pattern_map[shared_from_this()] = matched_block->output(0);
+ pattern_map.merge(local_pm);
+
+ for (const auto& matched_node : local_matcher->get_matched_nodes()) {
+ matcher->add_node(matched_node);
+ }
+ return true;
+}
\ No newline at end of file
diff --git a/src/core/tests/pattern_block.cpp b/src/core/tests/pattern_block.cpp
new file mode 100644
index 00000000000..c0d970693e2
--- /dev/null
+++ b/src/core/tests/pattern_block.cpp
@@ -0,0 +1,221 @@
+// Copyright (C) 2018-2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include <gtest/gtest.h>
+
+#include "openvino/op/add.hpp"
+#include "openvino/op/constant.hpp"
+#include "openvino/op/matmul.hpp"
+#include "openvino/op/parameter.hpp"
+#include "openvino/op/subtract.hpp"
+#include "openvino/pass/pattern/matcher.hpp"
+#include "openvino/pass/pattern/op/block.hpp"
+#include "openvino/pass/pattern/op/label.hpp"
+#include "openvino/pass/pattern/op/wrap_type.hpp"
+
+using namespace ov;
+using namespace ov::pass::pattern;
+using namespace ov::pass::pattern::op;
+
+// Simple single-node block match
+TEST(PatternBlockTest, block_matches_simple_matmul) {
+ auto input = any_input();
+ auto weights = any_input();
+ auto matmul = wrap_type<ov::op::v0::MatMul>({input, weights});
+
+ auto block = std::make_shared<Block>(OutputVector{input, weights}, OutputVector{matmul}, "matmul_block");
+
+ Matcher matcher(block);
+
+ auto input_node = std::make_shared<ov::op::v0::Parameter>(element::f32, Shape{1, 4});
+ auto weights_node = ov::op::v0::Constant::create(element::f32, Shape{4, 4}, {0});
+ auto real_matmul = std::make_shared<ov::op::v0::MatMul>(input_node, weights_node);
+
+ ASSERT_TRUE(matcher.match(real_matmul->output(0)));
+
+ auto block_node = ov::as_type_ptr<Block>(matcher.get_pattern_value_map().at(block).get_node_shared_ptr());
+ ASSERT_NE(block_node, nullptr);
+ EXPECT_EQ(block_node->get_inputs()[0].get_node_shared_ptr(), input_node);
+ EXPECT_EQ(block_node->get_inputs()[1].get_node_shared_ptr(), weights_node);
+ EXPECT_EQ(block_node->get_outputs()[0].get_node_shared_ptr(), real_matmul);
+}
+
+// Block matching MatMul followed by Add
+TEST(PatternBlockTest, block_matches_matmul_add_chain) {
+ auto input = any_input();
+ auto weights = any_input();
+ auto bias = any_input();
+
+ auto matmul = wrap_type<ov::op::v0::MatMul>({input, weights});
+ auto add = wrap_type<ov::op::v1::Add>({matmul, bias});
+
+ auto block = std::make_shared<Block>(OutputVector{input, weights, bias}, OutputVector{add}, "matmul_add_block");
+
+ Matcher matcher(block);
+
+ auto input_node = std::make_shared<ov::op::v0::Parameter>(element::f32, Shape{1, 4});
+ auto weights_node = ov::op::v0::Constant::create(element::f32, Shape{4, 4}, {0});
+ auto bias_node = ov::op::v0::Constant::create(element::f32, Shape{1, 4}, {0});
+
+ auto matmul_node = std::make_shared<ov::op::v0::MatMul>(input_node, weights_node);
+ auto add_node = std::make_shared<ov::op::v1::Add>(matmul_node, bias_node);
+
+ ASSERT_TRUE(matcher.match(add_node->output(0)));
+
+ auto block_node = ov::as_type_ptr<Block>(matcher.get_pattern_value_map().at(block).get_node_shared_ptr());
+ ASSERT_NE(block_node, nullptr);
+ EXPECT_EQ(block_node->get_outputs()[0].get_node_shared_ptr(), add_node);
+}
+
+// Block fails to match if operator type differs (Subtract instead of Add)
+TEST(PatternBlockTest, block_does_not_match_wrong_operator) {
+ auto input = any_input();
+ auto weights = any_input();
+ auto bias = any_input();
+
+ auto matmul = wrap_type<ov::op::v0::MatMul>({input, weights});
+ auto add = wrap_type<ov::op::v1::Add>({matmul, bias});
+
+ auto block = std::make_shared<Block>(OutputVector{input, weights, bias}, OutputVector{add}, "matmul_add_block");
+
+ Matcher matcher(block);
+
+ auto input_node = std::make_shared<ov::op::v0::Parameter>(element::f32, Shape{1, 4});
+ auto weights_node = ov::op::v0::Constant::create(element::f32, Shape{4, 4}, {0});
+ auto bias_node = ov::op::v0::Constant::create(element::f32, Shape{1, 4}, {0});
+
+ auto matmul_node = std::make_shared<ov::op::v0::MatMul>(input_node, weights_node);
+ auto subtract_node = std::make_shared<ov::op::v1::Subtract>(matmul_node, bias_node);
+
+ ASSERT_FALSE(matcher.match(subtract_node->output(0)));
+}
+
+// Block fails to match if an input is missing
+TEST(PatternBlockTest, block_does_not_match_if_input_missing) {
+ auto input = any_input();
+ auto weights = any_input();
+ auto bias = any_input();
+
+ auto matmul = wrap_type<ov::op::v0::MatMul>({input, weights});
+ auto add = wrap_type<ov::op::v1::Add>({matmul, bias});
+
+ auto block = std::make_shared<Block>(OutputVector{input, weights, bias}, OutputVector{add}, "matmul_add_block");
+
+ Matcher matcher(block);
+
+ auto input_node = std::make_shared<ov::op::v0::Parameter>(element::f32, Shape{1, 4});
+ auto weights_node = ov::op::v0::Constant::create(element::f32, Shape{4, 4}, {0});
+ auto matmul_node = std::make_shared<ov::op::v0::MatMul>(input_node, weights_node);
+
+ ASSERT_FALSE(matcher.match(matmul_node->output(0)));
+}
+
+// Nested Block (Block inside a Block)
+TEST(PatternBlockTest, block_nested_blocks) {
+ auto input = any_input();
+ auto weights = any_input();
+ auto bias = any_input();
+
+ auto inner_matmul = wrap_type<ov::op::v0::MatMul>({input, weights});
+ auto inner_block =
+ std::make_shared<Block>(OutputVector{input, weights}, OutputVector{inner_matmul}, "inner_matmul_block");
+
+ auto add = wrap_type<ov::op::v1::Add>({inner_block, bias});
+ auto outer_block = std::make_shared<Block>(OutputVector{input, weights, bias}, OutputVector{add}, "outer_block");
+
+ Matcher matcher(outer_block);
+
+ auto input_node = std::make_shared<ov::op::v0::Parameter>(element::f32, Shape{1, 4});
+ auto weights_node = ov::op::v0::Constant::create(element::f32, Shape{4, 4}, {0});
+ auto bias_node = ov::op::v0::Constant::create(element::f32, Shape{1, 4}, {0});
+ auto matmul_node = std::make_shared<ov::op::v0::MatMul>(input_node, weights_node);
+ auto add_node = std::make_shared<ov::op::v1::Add>(matmul_node, bias_node);
+
+ ASSERT_TRUE(matcher.match(add_node->output(0)));
+}
+
+// Chained Blocks (Block output feeds into another Block)
+TEST(PatternBlockTest, block_chained_blocks) {
+ auto input = any_input();
+ auto weights = any_input();
+ auto bias = any_input();
+
+ auto matmul = wrap_type<ov::op::v0::MatMul>({input, weights});
+ auto block1 = std::make_shared<Block>(OutputVector{input, weights}, OutputVector{matmul}, "matmul_block");
+
+ auto add = wrap_type<ov::op::v1::Add>({block1, bias});
+ auto block2 = std::make_shared<Block>(OutputVector{input, weights, bias}, OutputVector{add}, "add_block");
+
+ Matcher matcher(block2);
+
+ auto input_node = std::make_shared<ov::op::v0::Parameter>(element::f32, Shape{1, 4});
+ auto weights_node = ov::op::v0::Constant::create(element::f32, Shape{4, 4}, {0});
+ auto bias_node = ov::op::v0::Constant::create(element::f32, Shape{1, 4}, {0});
+ auto matmul_node = std::make_shared<ov::op::v0::MatMul>(input_node, weights_node);
+ auto add_node = std::make_shared<ov::op::v1::Add>(matmul_node, bias_node);
+
+ ASSERT_TRUE(matcher.match(add_node->output(0)));
+}
+
+TEST(PatternBlockTest, block_registers_and_retrieves_anchors) {
+ using ov::op::v0::Constant;
+ using ov::op::v0::MatMul;
+ using ov::op::v0::Parameter;
+ using ov::op::v1::Add;
+
+ // Pattern
+ auto input = any_input();
+ auto weights = any_input();
+ auto bias = any_input();
+
+ auto matmul = wrap_type<MatMul>({input, weights});
+ auto add = wrap_type<Add>({matmul, bias});
+ auto block = std::make_shared<Block>(OutputVector{input, weights, bias}, OutputVector{add}, "matmul_add_block");
+ REGISTER_ANCHORS(block, input, weights, bias, matmul, add);
+
+ // Anchors exist
+ const auto& anchors = block->get_registered_anchors();
+ EXPECT_EQ(anchors.size(), 5);
+ EXPECT_TRUE(anchors.count("input"));
+ EXPECT_TRUE(anchors.count("weights"));
+ EXPECT_TRUE(anchors.count("bias"));
+ EXPECT_TRUE(anchors.count("matmul"));
+ EXPECT_TRUE(anchors.count("add"));
+
+ EXPECT_EQ(anchors.at("input"), input);
+ EXPECT_EQ(anchors.at("weights"), weights);
+ EXPECT_EQ(anchors.at("bias"), bias);
+ EXPECT_EQ(anchors.at("matmul"), matmul);
+ EXPECT_EQ(anchors.at("add"), add);
+
+ // Real model:
+ auto input_node = std::make_shared<Parameter>(element::f32, Shape{1, 4});
+ auto weights_node = Constant::create(element::f32, Shape{4, 4}, {1});
+ auto bias_node = Constant::create(element::f32, Shape{1, 4}, {2});
+ auto matmul_node = std::make_shared<MatMul>(input_node, weights_node);
+ auto add_node = std::make_shared<Add>(matmul_node, bias_node);
+
+ Matcher matcher(block);
+ ASSERT_TRUE(matcher.match(add_node->output(0)));
+
+ // check that we can use anchors to get matched nodes:
+ auto pm = matcher.get_pattern_value_map();
+ auto o_input = block->get_anchor("input", pm);
+ auto o_weights = block->get_anchor("weights", pm);
+ auto o_bias = block->get_anchor("bias", pm);
+ auto o_matmul = block->get_anchor("matmul", pm);
+ auto o_add = block->get_anchor("add", pm);
+
+ ASSERT_TRUE(o_input.has_value());
+ ASSERT_TRUE(o_weights.has_value());
+ ASSERT_TRUE(o_bias.has_value());
+ ASSERT_TRUE(o_matmul.has_value());
+ ASSERT_TRUE(o_add.has_value());
+
+ EXPECT_EQ(o_input.value().get_node_shared_ptr(), input_node);
+ EXPECT_EQ(o_weights.value().get_node_shared_ptr(), weights_node);
+ EXPECT_EQ(o_bias.value().get_node_shared_ptr(), bias_node);
+ EXPECT_EQ(o_matmul.value().get_node_shared_ptr(), matmul_node);
+ EXPECT_EQ(o_add.value().get_node_shared_ptr(), add_node);
+}