Skip to content

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

Optimization of SharedOpOptimization transformation (#29601)

Evgenya Nugmanovaee5c6411219f
diff --git a/src/common/transformations/src/transformations/common_optimizations/shared_ops_optimization.cpp b/src/common/transformations/src/transformations/common_optimizations/shared_ops_optimization.cpp
index 46eb5bdd6c9..7cb953078ef 100644
--- a/src/common/transformations/src/transformations/common_optimizations/shared_ops_optimization.cpp
+++ b/src/common/transformations/src/transformations/common_optimizations/shared_ops_optimization.cpp
@@ -45,23 +45,26 @@ public:
void on_adapter(const std::string& name, ValueAccessor<std::shared_ptr<ov::Model>>& adapter) override {
OPENVINO_THROW_NOT_IMPLEMENTED("Can not compare models");
};
- ov::AnyMap get_attributes_map() const {
+
+ ov::AnyMap visit_attributes(const std::shared_ptr<Node>& node) {
+ m_attributes_map.clear();
+ node->visit_attributes(*this);
return m_attributes_map;
- };
+ }
private:
ov::AnyMap m_attributes_map;
};
bool inputs_from_same_source_or_equal_constants(const std::shared_ptr<Node>& lhs, const std::shared_ptr<Node>& rhs) {
- if (lhs->get_input_size() != rhs->get_input_size())
- return false;
size_t input_size = lhs->get_input_size();
+ if (input_size != rhs->get_input_size())
+ return false;
for (size_t i = 0; i < input_size; ++i) {
- if (lhs->input_value(i) == rhs->input_value(i))
+ if (ov::op::util::input_sources_are_equal(lhs, rhs, i))
continue;
- auto lhs_constant = as_type_ptr<v0::Constant>(lhs->get_input_node_shared_ptr(i));
- auto rhs_constant = as_type_ptr<v0::Constant>(rhs->get_input_node_shared_ptr(i));
+ auto lhs_constant = as_type<v0::Constant>(lhs->get_input_node_ptr(i));
+ auto rhs_constant = as_type<v0::Constant>(rhs->get_input_node_ptr(i));
if (!lhs_constant || !rhs_constant)
return false;
if (lhs_constant->get_element_type() != rhs_constant->get_element_type())
@@ -74,7 +77,17 @@ bool inputs_from_same_source_or_equal_constants(const std::shared_ptr<Node>& lhs
return true;
}
-bool nodes_are_equal(const std::shared_ptr<Node>& lhs, const std::shared_ptr<Node>& rhs) {
+void collect_node_attrs(const std::shared_ptr<Node>& node,
+ std::unordered_map<std::shared_ptr<ov::Node>, ov::AnyMap>& node_attributes_cache) {
+ if (node_attributes_cache.count(node))
+ return;
+ static auto visitor = NodeComparingVisitor();
+ node_attributes_cache[node] = visitor.visit_attributes(node);
+}
+
+bool nodes_are_equal(const std::shared_ptr<Node>& lhs,
+ const std::shared_ptr<Node>& rhs,
+ std::unordered_map<std::shared_ptr<ov::Node>, ov::AnyMap>& node_attributes_cache) {
// making sure that nodes are of the same type
if (lhs->get_type_info() != rhs->get_type_info())
return false;
@@ -86,19 +99,22 @@ bool nodes_are_equal(const std::shared_ptr<Node>& lhs, const std::shared_ptr<Nod
if (!rhs->get_control_dependents().empty() || !rhs->get_control_dependencies().empty())
return false;
// skip comparing rt_info. example: fused_name may have different strings
+ // compare inputs: should be same Output<Node> or equal constants
+ if (!inputs_from_same_source_or_equal_constants(lhs, rhs))
+ return false;
// compare attributes
try {
- auto lhs_visitor = NodeComparingVisitor(), rhs_visitor = NodeComparingVisitor();
- lhs->visit_attributes(lhs_visitor);
- rhs->visit_attributes(rhs_visitor);
- if (lhs_visitor.get_attributes_map() != rhs_visitor.get_attributes_map())
+ collect_node_attrs(lhs, node_attributes_cache);
+ collect_node_attrs(rhs, node_attributes_cache);
+ OPENVINO_ASSERT(node_attributes_cache.count(lhs) && node_attributes_cache.count(rhs));
+ if (node_attributes_cache[lhs] != node_attributes_cache[rhs])
return false;
} catch (...) {
// we avoid errors during comparison of objects without equality operands
// assuming they are not equal
return false;
}
- return inputs_from_same_source_or_equal_constants(lhs, rhs);
+ return true;
}
bool shared_node_optimization(const shared_ptr<Model>& model) {
@@ -107,6 +123,7 @@ bool shared_node_optimization(const shared_ptr<Model>& model) {
const auto& order = model->get_ordered_ops();
for (size_t i = 0; i < order.size(); ++i)
index_map[order[i]] = i;
+ std::unordered_map<std::shared_ptr<ov::Node>, ov::AnyMap> node_attributes_cache;
for (const auto& op : order) {
// Recursively apply transformation for sub-graph based operations
if (auto multi_subgraph_op = ov::as_type_ptr<op::util::MultiSubGraphOp>(op)) {
@@ -115,7 +132,7 @@ bool shared_node_optimization(const shared_ptr<Model>& model) {
rewritten = shared_node_optimization(sub_graph) || rewritten;
}
}
- for (auto& output : op->outputs()) {
+ for (const auto& output : op->outputs()) {
const auto& target_inputs = output.get_target_inputs();
if (target_inputs.size() <= 1)
continue; // nothing to optimize
@@ -151,7 +168,7 @@ bool shared_node_optimization(const shared_ptr<Model>& model) {
continue;
}
- if (nodes_are_equal(root_op, child_op)) {
+ if (nodes_are_equal(root_op, child_op, node_attributes_cache)) {
rewritten =
replace_output_update_name(child_op->output(0), root_op->output(0)) || rewritten;
visited_nodes[j] = true;
diff --git a/src/core/include/openvino/core/descriptor/output.hpp b/src/core/include/openvino/core/descriptor/output.hpp
index 723459b87cb..fd2e43e0e84 100644
--- a/src/core/include/openvino/core/descriptor/output.hpp
+++ b/src/core/include/openvino/core/descriptor/output.hpp
@@ -35,6 +35,12 @@ public:
Output(Node* node, size_t index, const std::shared_ptr<Tensor>& tensor);
std::shared_ptr<Node> get_node() const;
+
+ /// \return the raw pointer to the node that this is an output of
+ Node* get_raw_pointer_node() const {
+ return m_node;
+ }
+
size_t get_index() const {
return m_index;
}
diff --git a/src/core/include/openvino/core/node.hpp b/src/core/include/openvino/core/node.hpp
index c83e48cc611..c41adde139b 100644
--- a/src/core/include/openvino/core/node.hpp
+++ b/src/core/include/openvino/core/node.hpp
@@ -43,6 +43,19 @@ namespace v0 {
class Result;
} // namespace v0
struct AutoBroadcastSpec;
+namespace util {
+/**
+ * @brief Checks if sources of inputs of two nodes are equal
+ * @param lhs - node to check input
+ * @param rhs - other node to check input
+ * @param input_index - input port index to get the source
+ * @return true if sources share same node and output index otherwise false
+ */
+OPENVINO_API
+bool input_sources_are_equal(const std::shared_ptr<ov::Node>& lhs,
+ const std::shared_ptr<ov::Node>& rhs,
+ const size_t input_index);
+} // namespace util
} // namespace op
namespace pass {
@@ -117,6 +130,9 @@ class OPENVINO_API Node : public std::enable_shared_from_this<Node> {
friend class Model;
protected:
+ friend OPENVINO_API bool ov::op::util::input_sources_are_equal(const std::shared_ptr<ov::Node>&,
+ const std::shared_ptr<ov::Node>&,
+ const size_t);
descriptor::Input& get_input_descriptor(size_t position);
descriptor::Output& get_output_descriptor(size_t position);
diff --git a/src/core/src/descriptor/output.cpp b/src/core/src/descriptor/output.cpp
index 7845e25a199..4220d572a49 100644
--- a/src/core/src/descriptor/output.cpp
+++ b/src/core/src/descriptor/output.cpp
@@ -34,7 +34,7 @@ std::shared_ptr<ov::Node> ov::descriptor::Output::get_node() const {
}
ov::Output<ov::Node> ov::descriptor::Output::get_output() const {
- return get_node()->output(m_index);
+ return m_node->output(m_index);
}
ov::descriptor::Tensor& ov::descriptor::Output::get_tensor() const {
diff --git a/src/core/src/node.cpp b/src/core/src/node.cpp
index 6ce0cca4ce8..b06087bbe6a 100644
--- a/src/core/src/node.cpp
+++ b/src/core/src/node.cpp
@@ -298,7 +298,7 @@ void ov::Node::set_friendly_name(const string& name) {
ov::Node* ov::Node::get_input_node_ptr(size_t index) const {
OPENVINO_ASSERT(index < m_inputs.size(), idx_txt, index, out_of_range_txt);
- return m_inputs[index].get_output().get_node().get();
+ return m_inputs[index].get_output().get_raw_pointer_node();
}
std::shared_ptr<ov::Node> ov::Node::get_input_node_shared_ptr(size_t index) const {
@@ -603,7 +603,7 @@ ov::Output<ov::Node> ov::Node::output(size_t output_index) {
OPENVINO_THROW(node_idx_out_of_range_txt);
}
- return Output<Node>(this, output_index);
+ return {this, output_index};
}
ov::Output<const ov::Node> ov::Node::output(size_t output_index) const {
@@ -811,4 +811,15 @@ bool AttributeAdapter<NodeVector>::visit_attributes(AttributeVisitor& visitor) {
}
AttributeAdapter<ov::NodeVector>::~AttributeAdapter() = default;
+
+namespace op::util {
+bool input_sources_are_equal(const std::shared_ptr<ov::Node>& lhs,
+ const std::shared_ptr<ov::Node>& rhs,
+ const size_t input_index) {
+ const auto& lhs_source = lhs->get_input_descriptor(input_index).get_output();
+ const auto& rhs_source = rhs->get_input_descriptor(input_index).get_output();
+ return lhs_source.get_raw_pointer_node() == rhs_source.get_raw_pointer_node() &&
+ lhs_source.get_index() == rhs_source.get_index();
+}
+} // namespace op::util
} // namespace ov