Skip to content

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

[RtInfo/IR] Serialize non RuntimeAttribute rt_info entries of Node and Tensor (#27358)

Tomasz Jankowskib1fd24e507de
diff --git a/src/core/src/pass/serialize.cpp b/src/core/src/pass/serialize.cpp
index 300e11d20a5..efddf02e06f 100644
--- a/src/core/src/pass/serialize.cpp
+++ b/src/core/src/pass/serialize.cpp
@@ -922,7 +922,7 @@ void serialize_rt_info(pugi::xml_node& root, const std::string& name, const ov::
child.append_attribute("name").set_value(name.c_str());
}
if (data.is<std::shared_ptr<ov::Meta>>()) {
- auto meta = data.as<std::shared_ptr<ov::Meta>>();
+ const auto& meta = data.as<std::shared_ptr<ov::Meta>>();
do {
if (auto meta_with_pugixml_node = std::dynamic_pointer_cast<ov::MetaDataWithPugixml>(meta)) {
if (auto pugi_node = meta_with_pugixml_node->get_pugi_node()) {
@@ -945,11 +945,32 @@ void serialize_rt_info(pugi::xml_node& root, const std::string& name, const ov::
serialize_rt_info(child, it.first, it.second);
}
} else {
- std::string value = data.as<std::string>();
+ const auto& value = data.as<std::string>();
child.append_attribute("value").set_value(value.c_str());
}
}
+bool append_custom_rt_info(pugi::xml_node& node, const std::string& name, const ov::Any& data) {
+ auto custom_node = node.append_child("custom");
+ custom_node.append_attribute("name").set_value(name.c_str());
+ bool appended = false;
+
+ if (data.is<ov::AnyMap>()) {
+ const auto& any_map = data.as<ov::AnyMap>();
+ for (const auto& it : any_map)
+ appended |= append_custom_rt_info(custom_node, it.first, it.second);
+
+ } else {
+ const auto& value = data.as<std::string>();
+ custom_node.append_attribute("value").set_value(value.c_str());
+ appended = true;
+ }
+
+ if (!appended)
+ node.remove_child(custom_node);
+ return appended;
+}
+
void ngfunction_2_ir(pugi::xml_node& netXml,
const ov::Model& model,
ConstantWriter& constant_node_write_handler,
@@ -1013,10 +1034,10 @@ void ngfunction_2_ir(pugi::xml_node& netXml,
// <layers/data> general attributes
pugi::xml_node data = layer.append_child("data");
- auto append_runtime_info = [](pugi::xml_node& node, ov::RTMap& attributes) {
+ auto append_runtime_info = [](pugi::xml_node& node, const ov::RTMap& attributes) {
pugi::xml_node rt_node = node.append_child("rt_info");
bool has_attrs = false;
- for (auto& item : attributes) {
+ for (const auto& item : attributes) {
if (item.second.is<ov::RuntimeAttribute>()) {
auto attribute_node = rt_node.append_child("attribute");
auto& rt_attribute = item.second.as<ov::RuntimeAttribute>();
@@ -1031,9 +1052,13 @@ void ngfunction_2_ir(pugi::xml_node& netXml,
}
}
}
- if (!has_attrs) {
+
+ for (const auto& item : attributes)
+ if (!item.second.is<ov::RuntimeAttribute>())
+ has_attrs |= append_custom_rt_info(rt_node, item.first, item.second);
+
+ if (!has_attrs)
node.remove_child(rt_node);
- }
};
if (version >= 11) {
diff --git a/src/core/tests/pass/serialization/rt_info_serialization.cpp b/src/core/tests/pass/serialization/rt_info_serialization.cpp
index 34ced48283b..beab07ba024 100644
--- a/src/core/tests/pass/serialization/rt_info_serialization.cpp
+++ b/src/core/tests/pass/serialization/rt_info_serialization.cpp
@@ -4,6 +4,9 @@
#include <gtest/gtest.h>
+#include <iostream>
+#include <sstream>
+
#include "common_test_utils/common_utils.hpp"
#include "common_test_utils/file_utils.hpp"
#include "common_test_utils/test_common.hpp"
@@ -299,3 +302,180 @@ TEST(OvSerializationTests, SerializeRawMeta) {
EXPECT_EQ(0, serialized_model.compare(ir_with_rt_info));
}
}
+
+namespace ov {
+namespace test {
+
+TEST(RTInfoSerialization, custom_info) {
+ std::string ref_ir_xml = R"V0G0N(<?xml version="1.0"?>
+<net name="CustomRTI" version="11">
+ <layers>
+ <layer id="0" name="node_A" type="Parameter" version="opset1">
+ <data shape="10,10" element_type="f32" />
+ <rt_info>
+ <custom name="node_info_A" value="v_A" />
+ </rt_info>
+ <output>
+ <port id="0" precision="FP32">
+ <dim>10</dim>
+ <dim>10</dim>
+ <rt_info>
+ <custom name="output_info_A" value="o_A" />
+ </rt_info>
+ </port>
+ </output>
+ </layer>
+ <layer id="1" name="node_B" type="Const" version="opset1">
+ <data element_type="f32" shape="1" offset="0" size="4" />
+ <rt_info>
+ <custom name="node_info_B" value="v_B" />
+ </rt_info>
+ <output>
+ <port id="0" precision="FP32">
+ <dim>1</dim>
+ <rt_info>
+ <custom name="output_info_B" value="o_B" />
+ </rt_info>
+ </port>
+ </output>
+ </layer>
+ <layer id="2" name="node_C" type="Add" version="opset1">
+ <data auto_broadcast="numpy" />
+ <rt_info>
+ <custom name="node_info_C" value="v_C" />
+ </rt_info>
+ <input>
+ <port id="0" precision="FP32">
+ <dim>10</dim>
+ <dim>10</dim>
+ </port>
+ <port id="1" precision="FP32">
+ <dim>1</dim>
+ </port>
+ </input>
+ <output>
+ <port id="2" precision="FP32">
+ <dim>10</dim>
+ <dim>10</dim>
+ <rt_info>
+ <custom name="output_info_C" value="o_C" />
+ <custom name="output_info_D" value="o_D" />
+ </rt_info>
+ </port>
+ </output>
+ </layer>
+ <layer id="3" name="node_D" type="Result" version="opset1">
+ <rt_info>
+ <custom name="node_info_D" value="v_D" />
+ </rt_info>
+ <input>
+ <port id="0" precision="FP32">
+ <dim>10</dim>
+ <dim>10</dim>
+ </port>
+ </input>
+ </layer>
+ </layers>
+ <edges>
+ <edge from-layer="0" from-port="0" to-layer="2" to-port="0" />
+ <edge from-layer="1" from-port="0" to-layer="2" to-port="1" />
+ <edge from-layer="2" from-port="2" to-layer="3" to-port="0" />
+ </edges>
+ <rt_info />
+</net>
+)V0G0N";
+
+ const auto data = std::make_shared<op::v0::Parameter>(element::Type_t::f32, Shape{10, 10});
+ const auto one = std::make_shared<op::v0::Constant>(element::f32, Shape{1}, std::vector<float>{1.f});
+ const auto add = std::make_shared<op::v1::Add>(data, one);
+ const auto result = std::make_shared<op::v0::Result>(add);
+
+ const auto add_info = [](const std::shared_ptr<Node>& node, const std::string& value) {
+ node->set_friendly_name("node_" + value);
+ node->get_rt_info()["node_info_" + value] = "v_" + value;
+ node->output(0).get_rt_info()["output_info_" + value] = "o_" + value;
+ };
+ add_info(data, "A");
+ add_info(one, "B");
+ add_info(add, "C");
+ add_info(result, "D");
+
+ const auto model = std::make_shared<Model>(ResultVector{result}, ParameterVector{data});
+ model->set_friendly_name("CustomRTI");
+
+ std::stringstream model_ss, weights_ss;
+ EXPECT_NO_THROW((ov::pass::Serialize{model_ss, weights_ss}.run_on_model(model)));
+ EXPECT_EQ(ref_ir_xml.compare(model_ss.str()), 0);
+}
+
+TEST(RTInfoSerialization, AnyMap_info) {
+ std::string ref_ir_xml = R"V0G0N(<?xml version="1.0"?>
+<net name="CustomRTI" version="11">
+ <layers>
+ <layer id="0" name="data" type="Parameter" version="opset1">
+ <data shape="111" element_type="f64" />
+ <output>
+ <port id="0" precision="FP64">
+ <dim>111</dim>
+ </port>
+ </output>
+ </layer>
+ <layer id="1" name="abs" type="Abs" version="opset1">
+ <rt_info>
+ <custom name="AnyMap">
+ <custom name="a" value="b" />
+ <custom name="i" value="7" />
+ <custom name="nested">
+ <custom name="c" value="d" />
+ </custom>
+ <custom name="x" value="3.14" />
+ </custom>
+ </rt_info>
+ <input>
+ <port id="0" precision="FP64">
+ <dim>111</dim>
+ </port>
+ </input>
+ <output>
+ <port id="1" precision="FP64">
+ <dim>111</dim>
+ </port>
+ </output>
+ </layer>
+ <layer id="2" name="result" type="Result" version="opset1">
+ <input>
+ <port id="0" precision="FP64">
+ <dim>111</dim>
+ </port>
+ </input>
+ </layer>
+ </layers>
+ <edges>
+ <edge from-layer="0" from-port="0" to-layer="1" to-port="0" />
+ <edge from-layer="1" from-port="1" to-layer="2" to-port="0" />
+ </edges>
+ <rt_info />
+</net>
+)V0G0N";
+
+ const auto data = std::make_shared<op::v0::Parameter>(element::Type_t::f64, Shape{111});
+ const auto abs = std::make_shared<op::v0::Abs>(data);
+ const auto result = std::make_shared<op::v0::Result>(abs);
+
+ data->set_friendly_name("data");
+ abs->set_friendly_name("abs");
+ result->set_friendly_name("result");
+
+ const auto empty = AnyMap{};
+ const auto nested = AnyMap{{"c", "d"}};
+ abs->get_rt_info()["AnyMap"] = AnyMap{{"a", "b"}, {"empty", empty}, {"i", 7}, {"x", 3.14}, {"nested", nested}};
+
+ const auto model = std::make_shared<Model>(ResultVector{result}, ParameterVector{data});
+ model->set_friendly_name("CustomRTI");
+
+ std::stringstream model_ss, weights_ss;
+ EXPECT_NO_THROW((ov::pass::Serialize{model_ss, weights_ss}.run_on_model(model)));
+ EXPECT_EQ(ref_ir_xml.compare(model_ss.str()), 0);
+}
+} // namespace test
+} // namespace ov
diff --git a/src/frontends/ir/src/ir_deserializer.cpp b/src/frontends/ir/src/ir_deserializer.cpp
index dadd7d1b593..0d44c24e6c0 100644
--- a/src/frontends/ir/src/ir_deserializer.cpp
+++ b/src/frontends/ir/src/ir_deserializer.cpp
@@ -833,6 +833,25 @@ static const std::string& translate_type_name(const std::string& name) {
return name;
}
+namespace {
+void set_custom_rt_info(const pugi::xml_node& rt_attrs, ov::AnyMap& rt_info) {
+ std::string custom_name, custom_value;
+ for (const auto& item : rt_attrs) {
+ if (std::strcmp(item.name(), "custom") == 0) {
+ if (ov::getStrAttribute(item, "name", custom_name)) {
+ if (ov::getStrAttribute(item, "value", custom_value)) {
+ rt_info.emplace(custom_name, custom_value);
+ } else {
+ rt_info.emplace(custom_name, ov::AnyMap{});
+ auto& nested = rt_info[custom_name].as<ov::AnyMap>();
+ set_custom_rt_info(item, nested);
+ }
+ }
+ }
+ }
+}
+} // namespace
+
std::shared_ptr<ov::Node> ov::XmlDeserializer::create_node(const std::vector<ov::Output<ov::Node>>& inputs,
const pugi::xml_node& node,
const std::shared_ptr<ov::AlignedBuffer>& weights,
@@ -986,33 +1005,36 @@ std::shared_ptr<ov::Node> ov::XmlDeserializer::create_node(const std::vector<ov:
if (!rt_attrs)
return;
for (const auto& item : rt_attrs) {
- std::string attribute_name, attribute_version;
- // For view:
- // <attribute name="old_api_map_order" version="0" value="0,3,1,2"/>
- if (!getStrAttribute(item, "name", attribute_name) || !getStrAttribute(item, "version", attribute_version))
- continue;
-
- const auto& type_info = ov::DiscreteTypeInfo(attribute_name.c_str(), attribute_version.c_str());
- auto attr = attrs_factory.create_by_type_info(type_info);
- if (!attr.empty()) {
- if (attr.is<ov::RuntimeAttribute>()) {
- RTInfoDeserializer attribute_visitor(item);
- if (attr.as<ov::RuntimeAttribute>().visit_attributes(attribute_visitor)) {
- auto res = rt_info.emplace(type_info, attr);
- if (!res.second) {
- OPENVINO_THROW("multiple rt_info attributes are detected: ", attribute_name);
+ if (std::strcmp(item.name(), "attribute") == 0) {
+ std::string attribute_name, attribute_version;
+ if (!getStrAttribute(item, "name", attribute_name) ||
+ !getStrAttribute(item, "version", attribute_version))
+ continue;
+
+ const auto& type_info = ov::DiscreteTypeInfo(attribute_name.c_str(), attribute_version.c_str());
+ auto attr = attrs_factory.create_by_type_info(type_info);
+ if (!attr.empty()) {
+ if (attr.is<ov::RuntimeAttribute>()) {
+ RTInfoDeserializer attribute_visitor(item);
+ if (attr.as<ov::RuntimeAttribute>().visit_attributes(attribute_visitor)) {
+ auto res = rt_info.emplace(type_info, attr);
+ if (!res.second) {
+ OPENVINO_THROW("multiple rt_info attributes are detected: ", attribute_name);
+ }
+ } else {
+ OPENVINO_THROW("VisitAttributes is not supported for: ", item.name(), " attribute");
}
} else {
- OPENVINO_THROW("VisitAttributes is not supported for: ", item.name(), " attribute");
+ OPENVINO_THROW("Attribute: ", item.name(), " is not recognized as runtime attribute");
}
} else {
- OPENVINO_THROW("Attribute: ", item.name(), " is not recognized as runtime attribute");
+ // As runtime attributes are optional, so we skip attribute if it is unknown to avoid exception
+ // when loading new IR with new attribute in old OV version.
}
- } else {
- // As runtime attributes are optional, so we skip attribute if it is unknown to avoid exception
- // when loading new IR with new attribute in old OV version.
}
}
+
+ set_custom_rt_info(rt_attrs, rt_info);
};
// read runtime info only for IR v11+
diff --git a/src/frontends/ir/tests/rt_info_custom.cpp b/src/frontends/ir/tests/rt_info_custom.cpp
new file mode 100644
index 00000000000..5699f1ed785
--- /dev/null
+++ b/src/frontends/ir/tests/rt_info_custom.cpp
@@ -0,0 +1,188 @@
+// Copyright (C) 2018-2024 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include <gtest/gtest.h>
+
+#include "common_test_utils/test_assertions.hpp"
+#include "openvino/runtime/core.hpp"
+
+namespace ov {
+namespace test {
+
+TEST(RTInfoCustom, simple_entries) {
+ std::string ref_ir_xml = R"V0G0N(
+<net name="Network" version="11">
+ <layers>
+ <layer name="in1" type="Parameter" id="0" version="opset8">
+ <data element_type="f32" shape="27"/>
+ <rt_info>
+ <custom name="infoA" value="A"/>
+ <custom name="infoB" value="B"/>
+ <custom name="infoB" value="BB"/>
+ <custom name="fused_names_0" value="a_name"/>
+ <attribute name="fused_names" version="0" value="the_name"/>
+ </rt_info>
+ <output>
+ <port id="0" precision="FP32" names="input_tensor">
+ <dim>27</dim>
+ </port>
+ </output>
+ </layer>
+ <layer name="Abs" id="1" type="Abs" version="opset8">
+ <rt_info>
+ <custom name="infoC" value="C"/>
+ <attribute name="fused_names" version="0" value=""/>
+ </rt_info>
+ <input>
+ <port id="0" precision="FP32">
+ <dim>27</dim>
+ </port>
+ </input>
+ <output>
+ <port id="1" precision="FP32" names="output_tensor">
+ <rt_info>
+ <custom name="infoD" value="D"/>
+ </rt_info>
+ <dim>27</dim>
+ </port>
+ </output>
+ </layer>
+ <layer name="output" type="Result" id="2" version="opset8">
+ <rt_info>
+ <attribute name="primitives_priority" version="0" value="the_prior"/>
+ <custom name="primitives_priority_0" value="a_prior"/>
+ </rt_info>
+ <input>
+ <port id="0" precision="FP32">
+ <dim>27</dim>
+ </port>
+ </input>
+ </layer>
+ </layers>
+ <edges>
+ <edge from-layer="0" from-port="0" to-layer="1" to-port="0"/>
+ <edge from-layer="1" from-port="1" to-layer="2" to-port="0"/>
+ </edges>
+</net>
+)V0G0N";
+
+ Core core;
+ auto model = core.read_model(ref_ir_xml, Tensor{});
+ ASSERT_NE(nullptr, model);
+ std::string value;
+
+ const auto& param_rti = model->get_parameters().at(0)->get_rt_info();
+ EXPECT_EQ(param_rti.size(), 3);
+
+ OV_ASSERT_NO_THROW(value = param_rti.at("fused_names_0").as<std::string>());
+ EXPECT_EQ(value.compare("the_name"), 0);
+
+ OV_ASSERT_NO_THROW(value = param_rti.at("infoA").as<std::string>());
+ EXPECT_EQ(value.compare("A"), 0);
+
+ OV_ASSERT_NO_THROW(value = param_rti.at("infoB").as<std::string>());
+ EXPECT_EQ(value.compare("B"), 0);
+
+ const auto& result = model->get_results().at(0);
+ const auto abs = result->get_input_node_ptr(0);
+
+ const auto& abs_rti = abs->get_rt_info();
+ EXPECT_EQ(abs_rti.size(), 2);
+ OV_ASSERT_NO_THROW(value = abs_rti.at("infoC").as<std::string>());
+ EXPECT_EQ(value.compare("C"), 0);
+
+ const auto& abs_output_rti = abs->output(0).get_rt_info();
+ EXPECT_EQ(abs_output_rti.size(), 1);
+ OV_ASSERT_NO_THROW(value = abs_output_rti.at("infoD").as<std::string>());
+ EXPECT_EQ(value.compare("D"), 0);
+
+ const auto& result_rti = result->get_rt_info();
+ EXPECT_EQ(result_rti.size(), 1);
+ OV_ASSERT_NO_THROW(value = result_rti.at("primitives_priority_0").as<std::string>());
+ EXPECT_EQ(value.compare("the_prior"), 0);
+}
+
+TEST(RTInfoCustom, nested_entries) {
+ std::string ref_ir_xml = R"V0G0N(
+<net name="Network" version="11">
+ <layers>
+ <layer name="in1" type="Parameter" id="0" version="opset8">
+ <data element_type="f32" shape="27"/>
+ <rt_info>
+ <custom name="infoA" value="A"/>
+ <custom name="nested">
+ <custom name="infoB" value="B"/>
+ <custom name="infoC" value="C"/>
+ </custom>
+ </rt_info>
+ <output>
+ <port id="0" precision="FP32" names="input_tensor">
+ <dim>27</dim>
+ </port>
+ </output>
+ </layer>
+ <layer name="Abs" id="1" type="Abs" version="opset8">
+ <input>
+ <port id="0" precision="FP32">
+ <dim>27</dim>
+ </port>
+ </input>
+ <output>
+ <port id="1" precision="FP32" names="output_tensor">
+ <rt_info>
+ <custom name="nested_0">
+ <custom name="nested_1">
+ <custom name="infoD" value="D"/>
+ </custom>
+ </custom>
+ </rt_info>
+ <dim>27</dim>
+ </port>
+ </output>
+ </layer>
+ <layer name="output" type="Result" id="2" version="opset8">
+ <input>
+ <port id="0" precision="FP32">
+ <dim>27</dim>
+ </port>
+ </input>
+ </layer>
+ </layers>
+ <edges>
+ <edge from-layer="0" from-port="0" to-layer="1" to-port="0"/>
+ <edge from-layer="1" from-port="1" to-layer="2" to-port="0"/>
+ </edges>
+</net>
+)V0G0N";
+
+ Core core;
+ auto model = core.read_model(ref_ir_xml, Tensor{});
+ ASSERT_NE(nullptr, model);
+ std::string value;
+ AnyMap any_map;
+
+ const auto& param_rti = model->get_parameters().at(0)->get_rt_info();
+ EXPECT_EQ(param_rti.size(), 2);
+ OV_ASSERT_NO_THROW(any_map = param_rti.at("nested").as<AnyMap>());
+ EXPECT_EQ(any_map.size(), 2);
+ OV_ASSERT_NO_THROW(value = any_map.at("infoB").as<std::string>());
+ EXPECT_EQ(value.compare("B"), 0);
+ OV_ASSERT_NO_THROW(value = any_map.at("infoC").as<std::string>());
+ EXPECT_EQ(value.compare("C"), 0);
+
+ const auto abs = model->get_results().at(0)->get_input_node_ptr(0);
+ const auto& abs_rti = abs->output(0).get_rt_info();
+ EXPECT_EQ(abs_rti.size(), 1);
+ OV_ASSERT_NO_THROW(any_map = abs_rti.at("nested_0").as<AnyMap>());
+ EXPECT_EQ(any_map.size(), 1);
+
+ AnyMap nested_map;
+ OV_ASSERT_NO_THROW(nested_map = any_map.at("nested_1").as<AnyMap>());
+ EXPECT_EQ(nested_map.size(), 1);
+ OV_ASSERT_NO_THROW(value = nested_map.at("infoD").as<std::string>());
+ EXPECT_EQ(value.compare("D"), 0);
+}
+
+} // namespace test
+} // namespace ov