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
[core] Save Parameter and Result ids ordered (#33083)
Tomasz Jankowskifb89d8b3e3bd
diff --git a/src/core/src/xml_util/xml_serialize_util.cpp b/src/core/src/xml_util/xml_serialize_util.cppindex 4be9db0aad9..8577546d2fd 100644--- a/src/core/src/xml_util/xml_serialize_util.cpp+++ b/src/core/src/xml_util/xml_serialize_util.cpp@@ -151,9 +151,9 @@ struct Edge {};const std::vector<Edge> create_edge_mapping(const std::unordered_map<ov::Node*, int>& layer_ids,- const ov::Model& model) {+ const NodeVector& nodes) {std::vector<Edge> edges;- for (const auto& node : model.get_ordered_ops()) {+ for (const auto& node : nodes) {if (ov::op::util::is_parameter(node)) {continue;}@@ -301,10 +301,10 @@ std::string translate_type_name(const std::string& name) {return name;}-const std::unordered_map<ov::Node*, int> create_layer_ids(const ov::Model& model) {+const std::unordered_map<ov::Node*, int> create_layer_ids(const NodeVector& nodes) {std::unordered_map<ov::Node*, int> layer_ids;int id = 0;- for (const auto& node : model.get_ordered_ops()) {+ for (const auto& node : nodes) {layer_ids[node.get()] = id++;}return layer_ids;@@ -948,8 +948,6 @@ void XmlSerializer::serialize(pugi::xml_node& net_xml, const ov::Model& model) {net_xml.append_attribute("version").set_value(static_cast<long long>(m_version));pugi::xml_node layers = net_xml.append_child("layers");- const std::unordered_map<ov::Node*, int> layer_ids = create_layer_ids(model);-const bool exec_graph = is_exec_graph(model);auto sorted_ops = model.get_ordered_ops();@@ -976,6 +974,7 @@ void XmlSerializer::serialize(pugi::xml_node& net_xml, const ov::Model& model) {}sorted_ops = std::move(result);}+ const std::unordered_map<ov::Node*, int> layer_ids = create_layer_ids(sorted_ops);// Mark nodes that are only used by postponed_constant nodesstd::unordered_set<ov::Node*> nodes_to_exclude;@@ -1151,10 +1150,10 @@ void XmlSerializer::serialize(pugi::xml_node& net_xml, const ov::Model& model) {}}// <edges>- const std::vector<Edge> edge_mapping = create_edge_mapping(layer_ids, model);+ const std::vector<Edge> edge_mapping = create_edge_mapping(layer_ids, sorted_ops);pugi::xml_node edges = net_xml.append_child("edges");- auto ordered_ops = model.get_ordered_ops();for (auto e : edge_mapping) {+ const auto& ordered_ops = sorted_ops;// Skip edges that involve excluded nodesif (nodes_to_exclude.count(ordered_ops[e.from_layer].get()) ||nodes_to_exclude.count(ordered_ops[e.to_layer].get()) ||diff --git a/src/core/tests/pass/serialization/deterministicity.cpp b/src/core/tests/pass/serialization/deterministicity.cppindex 2f18ed3c3be..e0d5e3c7dc1 100644--- a/src/core/tests/pass/serialization/deterministicity.cpp+++ b/src/core/tests/pass/serialization/deterministicity.cpp@@ -5,14 +5,19 @@#include <gtest/gtest.h>#include <fstream>+#include <regex>#include "common_test_utils/common_utils.hpp"#include "common_test_utils/file_utils.hpp"#include "common_test_utils/test_common.hpp"+#include "openvino/op/add.hpp"#include "openvino/pass/serialize.hpp"+#include "openvino/util/common_util.hpp"#include "openvino/util/file_util.hpp"#include "read_ir.hpp"+namespace ov::test {+class DeterministicityCommon {protected:std::string m_out_xml_path_1{};@@ -350,3 +355,41 @@ TEST_P(SerializationDeterministicityInputOutputTest, FromOvModelBybPath) {INSTANTIATE_TEST_SUITE_P(DeterministicityInputOutput,SerializationDeterministicityInputOutputTest,::testing::Values(ov::pass::Serialize::Version::IR_V10, ov::pass::Serialize::Version::IR_V11));++TEST(DeterministicityInputOutput, LayerIdOrder) {+ const auto p1 = std::make_shared<op::v0::Parameter>(element::f32, Shape{1});+ const auto p2 = std::make_shared<op::v0::Parameter>(element::f32, Shape{1});+ const auto p3 = std::make_shared<op::v0::Parameter>(element::f32, Shape{1});+ const auto a1 = std::make_shared<op::v1::Add>(p3, p2);+ const auto a2 = std::make_shared<op::v1::Add>(p2, p1);+ const auto a3 = std::make_shared<op::v1::Add>(p1, p3);+ const auto r1 = std::make_shared<op::v0::Result>(a1);+ const auto r2 = std::make_shared<op::v0::Result>(a2);+ const auto r3 = std::make_shared<op::v0::Result>(a3);+ const auto model = std::make_shared<Model>(ResultVector{r3, r1, r2}, ParameterVector{p2, p3, p1}, "param_order");+ p2->set_friendly_name("expect id 0");+ p3->set_friendly_name("expect id 1");+ p1->set_friendly_name("expect id 2");+ r3->set_friendly_name("expect id 6");+ r1->set_friendly_name("expect id 7");+ r2->set_friendly_name("expect id 8");++ std::stringstream xml, bin;+ ov::pass::Serialize(xml, bin).run_on_model(model);++ // order matters+ constexpr auto expected_layer_id = util::make_array(R"(<layer id="0" name="expect id 0" type="Parameter")",+ R"(<layer id="1" name="expect id 1" type="Parameter")",+ R"(<layer id="2" name="expect id 2" type="Parameter")",+ R"(<layer id="6" name="expect id 6" type="Result")",+ R"(<layer id="7" name="expect id 7" type="Result")",+ R"(<layer id="8" name="expect id 8" type="Result")");+ const std::string xml_str = xml.str();+ std::string::size_type pos = 0;+ for (const auto& n : expected_layer_id) {+ const auto found = xml_str.find(n, pos);+ ASSERT_NE(found, std::string::npos) << "Not found: " << n;+ pos = found;+ }+}+} // namespace ov::test