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] Fixing NodePredicate conversion to ValuePredicate (#29172)
Evgenya Nugmanovacce35ac4ecc3
diff --git a/src/common/transformations/include/transformations/utils/utils.hpp b/src/common/transformations/include/transformations/utils/utils.hppindex d85058b218a..05afdac10b2 100644--- a/src/common/transformations/include/transformations/utils/utils.hpp+++ b/src/common/transformations/include/transformations/utils/utils.hpp@@ -294,8 +294,8 @@ TRANSFORMATIONS_API bool is_on_constant_path(const ov::Output<ov::Node>& output)TRANSFORMATIONS_API bool process_subgraph(ov::pass::ModelPass& model_pass, const std::shared_ptr<Node>& node);template <typename T>-ov::pass::pattern::op::ValuePredicate constant_predicate(std::function<bool(const std::vector<T>&)> predicate) {- return pass::pattern::op::as_value_predicate([=](std::shared_ptr<Node> n) -> bool {+ov::pass::pattern::op::Predicate constant_predicate(std::function<bool(const std::vector<T>&)> predicate) {+ return ov::pass::pattern::op::Predicate([=](std::shared_ptr<Node> n) -> bool {if (auto constant = as_type_ptr<v0::Constant>(n)) {auto values = constant->cast_vector<T>();return predicate(values);diff --git a/src/common/transformations/src/transformations/common_optimizations/nop_elimination.cpp b/src/common/transformations/src/transformations/common_optimizations/nop_elimination.cppindex 093275a5cb9..78cef0d7034 100644--- a/src/common/transformations/src/transformations/common_optimizations/nop_elimination.cpp+++ b/src/common/transformations/src/transformations/common_optimizations/nop_elimination.cpp@@ -893,12 +893,11 @@ pass::EliminateScatterUpdate::EliminateScatterUpdate() {ov::pass::EliminateNopBroadcast::EliminateNopBroadcast() {MATCHER_SCOPE(EliminateNopBroadcast);- auto root = pattern::wrap_type<op::v1::Broadcast, op::v3::Broadcast, op::v0::Tile>(- pattern::op::as_value_predicate([](std::shared_ptr<Node> node) {- auto input_rank = node->get_input_partial_shape(0).rank();- auto output_rank = node->get_output_partial_shape(0).rank();- return input_rank.is_static() && output_rank.is_static() && input_rank == output_rank;- }));+ auto root = pattern::wrap_type<op::v1::Broadcast, op::v3::Broadcast, op::v0::Tile>([](std::shared_ptr<Node> node) {+ auto input_rank = node->get_input_partial_shape(0).rank();+ auto output_rank = node->get_output_partial_shape(0).rank();+ return input_rank.is_static() && output_rank.is_static() && input_rank == output_rank;+ });ov::matcher_pass_callback matcher_pass_callback = [](pattern::Matcher& m) {const auto& op = m.get_match_root();diff --git a/src/common/transformations/src/transformations/symbolic_transformations/dereshape_matmul.cpp b/src/common/transformations/src/transformations/symbolic_transformations/dereshape_matmul.cppindex ddce0069002..544db369b1b 100644--- a/src/common/transformations/src/transformations/symbolic_transformations/dereshape_matmul.cpp+++ b/src/common/transformations/src/transformations/symbolic_transformations/dereshape_matmul.cpp@@ -182,10 +182,10 @@ void pull_reshape_through_optional_concat_and_bea(const ov::pass::pattern::Patte}} // namespace-#define IN_RESHAPE \- pattern::wrap_type<op::v1::Reshape>(pattern::op::as_value_predicate([](std::shared_ptr<Node> n) -> bool { \- return pattern::consumers_count(1)(n->output(0)) && reshape_keeps_last_two_dims(n); \- }));+#define IN_RESHAPE \+ pattern::wrap_type<op::v1::Reshape>([](std::shared_ptr<Node> n) -> bool { \+ return pattern::consumers_count(1)(n->output(0)) && reshape_keeps_last_two_dims(n); \+ });#define SCALAR_INPUT \pattern::any_input([](ov::Output<Node> out) { \@@ -252,10 +252,9 @@ ov::pass::DeReshapeMatMul::DeReshapeMatMul() {auto matmul_or_add = std::make_shared<pattern::op::Or>(OutputVector{matmul, add});auto final_reshape =- pattern::wrap_type<op::v1::Reshape>({matmul_or_add, pattern::any_input()},- pattern::op::as_value_predicate([](std::shared_ptr<Node> n) -> bool {- return reshape_keeps_last_two_dims(n);- }));+ pattern::wrap_type<op::v1::Reshape>({matmul_or_add, pattern::any_input()}, [](std::shared_ptr<Node> n) -> bool {+ return reshape_keeps_last_two_dims(n);+ });ov::matcher_pass_callback matcher_pass_callback = [=](pattern::Matcher& m) {const auto& pm = m.get_pattern_map();diff --git a/src/core/include/openvino/pass/pattern/op/any.hpp b/src/core/include/openvino/pass/pattern/op/any.hppindex 2227fe2bf4e..205e5aa9cd9 100644--- a/src/core/include/openvino/pass/pattern/op/any.hpp+++ b/src/core/include/openvino/pass/pattern/op/any.hpp@@ -20,15 +20,17 @@ public:: Pattern(wrapped_values, Predicate(pred)) {set_output_type(0, type, s);}- Any(const element::Type& type, const PartialShape& s, const NodePredicate& pred, const NodeVector& wrapped_values)- : Any(type, s, as_value_predicate(pred), as_output_vector(wrapped_values)) {}+ template <typename TPredicate>+ Any(const element::Type& type, const PartialShape& s, const TPredicate& pred, const NodeVector& wrapped_values)+ : Any(type, s, Predicate(pred), as_output_vector(wrapped_values)) {}/// \brief creates a Any node containing a sub-pattern described by the type and/// shape of \sa node.template <typename TPredicate>Any(const Output<Node>& node, const TPredicate& pred, const OutputVector& wrapped_values): Any(node.get_element_type(), node.get_partial_shape(), pred, wrapped_values) {}- Any(const Output<Node>& node, const NodePredicate& pred, const NodeVector& wrapped_values)- : Any(node, as_value_predicate(pred), as_output_vector(wrapped_values)) {}+ template <typename TPredicate>+ Any(const Output<Node>& node, const TPredicate& pred, const NodeVector& wrapped_values)+ : Any(node, Predicate(pred), as_output_vector(wrapped_values)) {}bool match_value(pattern::Matcher* matcher,const Output<Node>& pattern_value,diff --git a/src/core/include/openvino/pass/pattern/op/any_of.hpp b/src/core/include/openvino/pass/pattern/op/any_of.hppindex 6b57cdc02b4..a3551beff79 100644--- a/src/core/include/openvino/pass/pattern/op/any_of.hpp+++ b/src/core/include/openvino/pass/pattern/op/any_of.hpp@@ -28,16 +28,18 @@ public:}set_output_type(0, type, s);}- AnyOf(const element::Type& type, const PartialShape& s, const NodePredicate& pred, const NodeVector& wrapped_values)- : AnyOf(type, s, as_value_predicate(pred), as_output_vector(wrapped_values)) {}+ template <typename TPredicate>+ AnyOf(const element::Type& type, const PartialShape& s, const TPredicate& pred, const NodeVector& wrapped_values)+ : AnyOf(type, s, Predicate(pred), as_output_vector(wrapped_values)) {}/// \brief creates a AnyOf node containing a sub-pattern described by the type and/// shape of \sa node.template <typename TPredicate>AnyOf(const Output<Node>& node, const TPredicate& pred, const OutputVector& wrapped_values): AnyOf(node.get_element_type(), node.get_partial_shape(), pred, wrapped_values) {}- AnyOf(const std::shared_ptr<Node>& node, const NodePredicate& pred, const NodeVector& wrapped_values)- : AnyOf(node, as_value_predicate(pred), as_output_vector(wrapped_values)) {}+ template <typename TPredicate>+ AnyOf(const std::shared_ptr<Node>& node, const TPredicate& pred, const NodeVector& wrapped_values)+ : AnyOf(node, Predicate(pred), as_output_vector(wrapped_values)) {}bool match_value(Matcher* matcher, const Output<Node>& pattern_value, const Output<Node>& graph_value) override;};} // namespace ov::pass::pattern::opdiff --git a/src/core/include/openvino/pass/pattern/op/label.hpp b/src/core/include/openvino/pass/pattern/op/label.hppindex ee74982ea9e..c535c91ecf5 100644--- a/src/core/include/openvino/pass/pattern/op/label.hpp+++ b/src/core/include/openvino/pass/pattern/op/label.hpp@@ -34,19 +34,14 @@ public:/// nullptr,/// OutputVector{add});/// \endcode- template <typename TPredicate>+ template <typename TPredicate, typename TArg = OutputVector>Label(const element::Type& type,const PartialShape& s,const TPredicate& pred,- const OutputVector& wrapped_values = {})+ const TArg& wrapped_values = OutputVector{}): Pattern(OutputVector{wrap_values(wrapped_values)}, Predicate(pred)) {set_output_type(0, type, s);}- Label(const element::Type& type,- const PartialShape& s,- const NodePredicate& pred,- const NodeVector& wrapped_values = {})- : Label(type, s, as_value_predicate(pred), as_output_vector(wrapped_values)) {}/// \brief creates a Label node containing a sub-pattern described by the type and/// shape of \sa node.@@ -60,14 +55,9 @@ public:/// nullptr,/// OutputVector{add});/// \endcode- template <typename TPredicate>- Label(const Output<Node>& value, const TPredicate& pred, const OutputVector& wrapped_values = {})+ template <typename TPredicate, typename TArg = OutputVector>+ Label(const Output<Node>& value, const TPredicate& pred, const TArg& wrapped_values = OutputVector{}): Label(value.get_element_type(), value.get_partial_shape(), Predicate(pred), wrapped_values) {}- Label(const Output<Node>& node, const NodePredicate& pred, const NodeVector& wrapped_values = {})- : Label(node.get_element_type(),- node.get_partial_shape(),- as_value_predicate(pred),- as_output_vector(wrapped_values)) {}explicit Label(const element::Type& type = element::dynamic, const PartialShape& s = PartialShape::dynamic()): Label(type, s, nullptr, OutputVector{}) {}@@ -78,6 +68,7 @@ public:protected:static Output<Node> wrap_values(const OutputVector& wrapped_values);+ static Output<Node> wrap_values(const NodeVector& wrapped_values);};} // namespace opdiff --git a/src/core/include/openvino/pass/pattern/op/pattern.hpp b/src/core/include/openvino/pass/pattern/op/pattern.hppindex b2d4ad1dcd3..d76289408cf 100644--- a/src/core/include/openvino/pass/pattern/op/pattern.hpp+++ b/src/core/include/openvino/pass/pattern/op/pattern.hpp@@ -64,15 +64,17 @@ OPENVINO_API op::Predicate all_of(const std::vector<std::function<bool(Output<NoOPENVINO_API op::Predicate shape_matches(const std::string& shape_notation);namespace op {--OPENVINO_API-ValuePredicate as_value_predicate(NodePredicate pred);+OPENVINO_DEPRECATED("This method is deprecated. Use constructor of ov::pass::pattern::Predicate instead")+OPENVINO_API Predicate as_value_predicate(NodePredicate pred);class OPENVINO_API Pattern : public Node {public:+ Pattern();+ explicit Pattern(const OutputVector& patterns);+ explicit Pattern(const NodeVector& patterns);/// \brief A base class for all the utility operators used to describe a pattern to matchPattern(const OutputVector& patterns, const Predicate& pred);- Pattern(const OutputVector& patterns);+ Pattern(const NodeVector& patterns, const Predicate& pred);std::shared_ptr<Node> clone_with_new_inputs(const OutputVector& /* new_args */) const override {OPENVINO_THROW("Uncopyable");diff --git a/src/core/include/openvino/pass/pattern/op/predicate.hpp b/src/core/include/openvino/pass/pattern/op/predicate.hppindex b339f978536..2ffd5844024 100644--- a/src/core/include/openvino/pass/pattern/op/predicate.hpp+++ b/src/core/include/openvino/pass/pattern/op/predicate.hpp@@ -94,18 +94,8 @@ public:}bool operator()(PatternSymbolMap& m, const Output<Node>& output) const;-- template <typename T>- bool operator()(const T& arg) const {- OPENVINO_ASSERT(!m_requires_map,- "Predicate " + m_name + " called with unexpected argument: " + std::string(typeid(T).name()));- if constexpr (std::is_convertible_v<T, Output<Node>>) {- PatternSymbolMap dummy_map;- return m_pred(dummy_map, arg);- }- OPENVINO_ASSERT(false,- "Predicate " + m_name + " called with unexpected argument: " + std::string(typeid(T).name()));- }+ bool operator()(const std::shared_ptr<Node>& node) const;+ bool operator()(const Output<Node>& output) const;template <typename TPredicate>Predicate operator||(const TPredicate& other) const {diff --git a/src/core/include/openvino/pass/pattern/op/true.hpp b/src/core/include/openvino/pass/pattern/op/true.hppindex ac09fc0b595..22b0cf7adad 100644--- a/src/core/include/openvino/pass/pattern/op/true.hpp+++ b/src/core/include/openvino/pass/pattern/op/true.hpp@@ -13,7 +13,7 @@ class OPENVINO_API True : public Pattern {public:OPENVINO_RTTI("patternTrue");/// \brief Always matches, does not add node to match list.- True() : Pattern(OutputVector{}) {}+ True() : Pattern() {}bool match_value(pattern::Matcher* matcher,const Output<Node>& pattern_value,const Output<Node>& graph_value) override;diff --git a/src/core/include/openvino/pass/pattern/op/wrap_type.hpp b/src/core/include/openvino/pass/pattern/op/wrap_type.hppindex ffad9aa7ef7..df3a6c6251e 100644--- a/src/core/include/openvino/pass/pattern/op/wrap_type.hpp+++ b/src/core/include/openvino/pass/pattern/op/wrap_type.hpp@@ -14,7 +14,7 @@ class OPENVINO_API WrapType : public Pattern {public:OPENVINO_RTTI("WrapType");- explicit WrapType(const std::vector<NodeTypeInfo>& wrapped_types) : Pattern({}), m_wrapped_types(wrapped_types) {+ explicit WrapType(const std::vector<NodeTypeInfo>& wrapped_types) : Pattern(), m_wrapped_types(wrapped_types) {set_output_type(0, element::Type_t::dynamic, PartialShape::dynamic());}@@ -27,7 +27,7 @@ public:set_output_type(0, element::Type_t::dynamic, PartialShape::dynamic());}- explicit WrapType(NodeTypeInfo wrapped_type) : Pattern({}), m_wrapped_types({wrapped_type}) {+ explicit WrapType(NodeTypeInfo wrapped_type) : Pattern(), m_wrapped_types({wrapped_type}) {set_output_type(0, element::Type_t::dynamic, PartialShape::dynamic());}diff --git a/src/core/src/pattern/op/label.cpp b/src/core/src/pattern/op/label.cppindex d87aad4b323..3b4ec752276 100644--- a/src/core/src/pattern/op/label.cpp+++ b/src/core/src/pattern/op/label.cpp@@ -21,6 +21,17 @@ ov::Output<ov::Node> ov::pass::pattern::op::Label::wrap_values(const ov::OutputV}}+ov::Output<ov::Node> ov::pass::pattern::op::Label::wrap_values(const ov::NodeVector& wrapped_values) {+ switch (wrapped_values.size()) {+ case 0:+ return std::make_shared<pattern::op::True>()->output(0);+ case 1:+ return wrapped_values[0];+ default:+ return std::make_shared<pattern::op::Or>(as_output_vector(wrapped_values))->output(0);+ }+}+bool ov::pass::pattern::op::Label::match_value(ov::pass::pattern::Matcher* matcher,const ov::Output<ov::Node>& pattern_value,const ov::Output<ov::Node>& graph_value) {diff --git a/src/core/src/pattern/op/pattern.cpp b/src/core/src/pattern/op/pattern.cppindex 07dd9f88d3a..2108d742b13 100644--- a/src/core/src/pattern/op/pattern.cpp+++ b/src/core/src/pattern/op/pattern.cpp@@ -11,30 +11,16 @@namespace ov::pass::pattern {namespace op {-namespace {-constexpr bool value_true_predicate(const Output<Node>&) {- return true;-}-} // namespace--struct NodeValuePredicate {- bool operator()(const Output<Node>& value) const {- return pred(value.get_node_shared_ptr());- }-- NodePredicate pred;-};+Pattern::Pattern() : Node(), m_predicate() {}Pattern::Pattern(const OutputVector& patterns) : Node(patterns), m_predicate() {}+Pattern::Pattern(const NodeVector& patterns) : Pattern(as_output_vector(patterns)) {}Pattern::Pattern(const OutputVector& patterns, const op::Predicate& pred) : Node(patterns), m_predicate(pred) {}+Pattern::Pattern(const NodeVector& patterns, const op::Predicate& pred) : Pattern(as_output_vector(patterns), pred) {}-ValuePredicate as_value_predicate(NodePredicate pred) {- if (pred) {- return NodeValuePredicate{std::move(pred)};- } else {- return value_true_predicate;- }+Predicate as_value_predicate(NodePredicate pred) {+ return Predicate(pred);}std::ostream& Pattern::write_type_description(std::ostream& out) const {diff --git a/src/core/src/pattern/op/predicate.cpp b/src/core/src/pattern/op/predicate.cppindex bebc1bcfb84..2044fef377c 100644--- a/src/core/src/pattern/op/predicate.cpp+++ b/src/core/src/pattern/op/predicate.cpp@@ -78,5 +78,17 @@ bool Predicate::operator()(pass::pattern::PatternSymbolMap& m, const Output<NodeOPENVINO_DEBUG("Predicate `", m_name, "` has ", (result ? "passed" : "failed"), ". Applied to ", output);return result;}++bool Predicate::operator()(const std::shared_ptr<Node>& node) const {+ OPENVINO_ASSERT(!m_requires_map, "Predicate " + m_name + " called with unexpected argument: std::shared_ptr<Node>");+ PatternSymbolMap dummy_map;+ return m_pred(dummy_map, node);+}++bool Predicate::operator()(const Output<Node>& output) const {+ OPENVINO_ASSERT(!m_requires_map, "Predicate " + m_name + " called with unexpected argument: Output<Node>");+ PatternSymbolMap dummy_map;+ return m_pred(dummy_map, output);+}} // namespace op} // namespace ov::pass::patterndiff --git a/src/plugins/intel_gpu/src/plugin/transformations/clamp_fp16_output.cpp b/src/plugins/intel_gpu/src/plugin/transformations/clamp_fp16_output.cppindex 0ccc40cf2b7..44e29a12189 100644--- a/src/plugins/intel_gpu/src/plugin/transformations/clamp_fp16_output.cpp+++ b/src/plugins/intel_gpu/src/plugin/transformations/clamp_fp16_output.cpp@@ -28,8 +28,8 @@ ClampFP16Output::ClampFP16Output() {using namespace ov::pass::pattern;using namespace ov::pass::pattern::op;- auto in0 = any_input(as_value_predicate(class_other_than<v0::Constant>()));- auto in1 = any_input(as_value_predicate(class_other_than<v0::Constant>()));+ auto in0 = any_input(class_other_than<v0::Constant>());+ auto in1 = any_input(class_other_than<v0::Constant>());auto matmul_m = wrap_type<v0::MatMul>({in0, in1}, type_matches(ov::element::f16) && consumers_count(1));auto reshape_m = wrap_type<v1::Reshape>({matmul_m, any_input()}, type_matches(ov::element::f16) && consumers_count(1));auto add_m = wrap_type<v1::Add>({matmul_m, any_input()}, type_matches(ov::element::f16) && consumers_count(1));diff --git a/src/plugins/intel_gpu/src/plugin/transformations/lora_horizontal_fusion.cpp b/src/plugins/intel_gpu/src/plugin/transformations/lora_horizontal_fusion.cppindex 7b9682c3d4c..4959a8156bd 100644--- a/src/plugins/intel_gpu/src/plugin/transformations/lora_horizontal_fusion.cpp+++ b/src/plugins/intel_gpu/src/plugin/transformations/lora_horizontal_fusion.cpp@@ -58,7 +58,7 @@ LoRAHorizontalFusion::LoRAHorizontalFusion() {auto axis_const = wrap_type<ov::op::v0::Constant>();auto split_const = wrap_type<ov::op::v0::Constant>();- auto split = wrap_type<ov::op::v1::VariadicSplit>({main_flow, axis_const, split_const}, ov::pass::pattern::op::as_value_predicate(is_target_pattern));+ auto split = wrap_type<ov::op::v1::VariadicSplit>({main_flow, axis_const, split_const}, is_target_pattern);ov::matcher_pass_callback callback = [=](Matcher& m) {const auto& pattern_map = m.get_pattern_value_map();