Skip to content

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

[CORE] Check WrapType input arity before evaluating the predicate (#39952)

Mikhail Ryzhov6edecb958812
diff --git a/src/core/dev_api/openvino/core/log_util.hpp b/src/core/dev_api/openvino/core/log_util.hpp
index 49882157ac9..030aef9c9c3 100644
--- a/src/core/dev_api/openvino/core/log_util.hpp
+++ b/src/core/dev_api/openvino/core/log_util.hpp
@@ -577,6 +577,18 @@ bool is_verbose_logging();
" NODES' TYPE MATCHED, but PREDICATE FAILED"); \
} while (0);
+# define OPENVINO_LOG_WRAPTYPE5(matcher, pattern_arg_size, graph_arg_size) \
+ do { \
+ OPENVINO_LOG_MATCHING(matcher, \
+ ov::util::LevelString::get(), \
+ OPENVINO_BLOCK_END, \
+ OPENVINO_RED, \
+ " NUMBER OF ARGUMENTS DOESN'T MATCH. EXPECTED: ", \
+ pattern_arg_size, \
+ ". OBSERVED: ", \
+ graph_arg_size); \
+ } while (0);
+
# define OPENVINO_LOG_WRAPTYPE3(matcher, num_arguments) \
do { \
bool _non_empty_args = num_arguments != 0; \
@@ -809,6 +821,9 @@ bool is_verbose_logging();
# define OPENVINO_LOG_WRAPTYPE4(...) \
do { \
} while (0)
+# define OPENVINO_LOG_WRAPTYPE5(...) \
+ do { \
+ } while (0)
# define OPENVINO_LOG_PREDICATE1(...) \
do { \
diff --git a/src/core/src/pattern/op/wrap_type.cpp b/src/core/src/pattern/op/wrap_type.cpp
index b21d68bae84..12dacd32c13 100644
--- a/src/core/src/pattern/op/wrap_type.cpp
+++ b/src/core/src/pattern/op/wrap_type.cpp
@@ -17,6 +17,11 @@ bool ov::pass::pattern::op::WrapType::match_value(Matcher* matcher,
return false;
}
+ if (get_input_size() != 0 && get_input_size() != graph_value.get_node()->get_input_size()) {
+ OPENVINO_LOG_WRAPTYPE5(matcher, get_input_size(), graph_value.get_node()->get_input_size());
+ return false;
+ }
+
if (std::none_of(m_wrapped_types.begin(), m_wrapped_types.end(), [&](const NodeTypeInfo& type_info) {
return graph_value.get_node_shared_ptr()->get_type_info().is_castable(type_info);
})) {
diff --git a/src/core/tests/pattern.cpp b/src/core/tests/pattern.cpp
index 0f98e2f897f..fd65f4ea906 100644
--- a/src/core/tests/pattern.cpp
+++ b/src/core/tests/pattern.cpp
@@ -1081,6 +1081,30 @@ TEST(pattern, wrap_type_multi_op) {
}
}
+TEST(pattern, wrap_type_checks_input_count_before_predicate) {
+ auto a = make_shared<op::v0::Parameter>(element::f32, Shape{1, 3});
+ auto b = make_shared<op::v0::Parameter>(element::f32, Shape{1, 3});
+ auto concat_1_input = make_shared<op::v0::Concat>(OutputVector{a}, 0);
+ auto concat_2_inputs = make_shared<op::v0::Concat>(OutputVector{a, b}, 0);
+
+ bool predicate_called = false;
+ auto predicate = [&](const Output<Node>& output) {
+ predicate_called = true;
+ // Safe only if the arity was verified before the predicate is invoked
+ return output.get_node()->input_value(1).get_element_type() == element::f32;
+ };
+
+ auto m = pattern::wrap_type<op::v0::Concat>({pattern::any_input(), pattern::any_input()}, predicate);
+ auto matcher = std::make_shared<pattern::Matcher>(m, "ConcatMatcher");
+
+ // The input count mismatch must be rejected before the predicate is evaluated
+ ASSERT_FALSE(matcher->match(concat_1_input->output(0)));
+ ASSERT_FALSE(predicate_called);
+
+ ASSERT_TRUE(matcher->match(concat_2_inputs->output(0)));
+ ASSERT_TRUE(predicate_called);
+}
+
TEST(pattern, simple_model_and_pattern) {
// Create a sample model
PartialShape shape{2, 2};