Skip to content

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

[Core] Serialize custom RT Info (#32066)

Tomasz Jankowski3a9843e8473f
diff --git a/src/core/dev_api/openvino/xml_util/xml_serialize_util.hpp b/src/core/dev_api/openvino/xml_util/xml_serialize_util.hpp
index c847b61bd3a..72ab75d637b 100644
--- a/src/core/dev_api/openvino/xml_util/xml_serialize_util.hpp
+++ b/src/core/dev_api/openvino/xml_util/xml_serialize_util.hpp
@@ -102,4 +102,9 @@ public:
void on_adapter(const std::string& name, ov::ValueAccessor<std::vector<std::string>>& adapter) override;
void on_adapter(const std::string& name, ov::ValueAccessor<std::shared_ptr<ov::Model>>& adapter) override;
}; // class XmlSerializer
+
+OPENVINO_API std::string rt_info_get_user_name(const std::string& custom_name);
+OPENVINO_API Any& rt_info_get_user_data(AnyMap& rt_map, const std::string& custom_name);
+OPENVINO_API const Any& rt_info_get_user_data(const AnyMap& rt_map, const std::string& custom_name);
+OPENVINO_API void rt_info_set_user_data(AnyMap& rt_map, const std::string& custom_name, const Any& value);
} // namespace ov::util
diff --git a/src/core/src/xml_util/xml_serialize_util.cpp b/src/core/src/xml_util/xml_serialize_util.cpp
index 96a8c177234..539f61ff59c 100644
--- a/src/core/src/xml_util/xml_serialize_util.cpp
+++ b/src/core/src/xml_util/xml_serialize_util.cpp
@@ -309,6 +309,50 @@ bool is_exec_graph(const ov::Model& model) {
return false;
}
+constexpr std::string_view rt_info_user_data_tag{"user_data"};
+
+bool append_custom_rt_info(pugi::xml_node& node,
+ const std::string& key,
+ const ov::Any& data,
+ bool prefix_needed = true) {
+ std::string name;
+ constexpr auto tag_sz = rt_info_user_data_tag.size();
+ if (key.compare(0, tag_sz, rt_info_user_data_tag) == 0) {
+ name = key.substr(tag_sz);
+ } else if (!prefix_needed) {
+ name = key;
+ }
+ if (name.empty()) {
+ return false;
+ }
+ auto custom_node = node.append_child(rt_info_user_data_tag.data());
+ 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, false) || appended;
+
+ } else if (!data.empty() && !data.is<ov::RuntimeAttribute>() && !data.is<std::shared_ptr<ov::RuntimeAttribute>>()) {
+ const auto& value = data.as<std::string>();
+ custom_node.append_attribute("value").set_value(value.c_str());
+ appended = true;
+ }
+
+ if (appended) {
+ // The 'version' attribute is added for backward compatibility only, it's not deserialized (not impact on
+ // rt_info entry). Older versions of IR deserializer require this attribute to be present in all tags contained
+ // within rt_info tag, despite of the tagname. Such tag to be ignored (without throwing) must have 'name' and
+ // 'version' values which are not present in predefined list of deserializable Runtime Attributes - to assure
+ // this 'version' value is empty (it must not be zero).
+ // https://github.com/openvinotoolkit/openvino/blob/dd16602824c66c53935a2d084ab4d7ace36a6414/src/frontends/ir/src/ir_deserializer.cpp#L976
+ custom_node.append_attribute("version").set_value("");
+ } else {
+ node.remove_child(custom_node);
+ }
+ return appended;
+}
} // namespace
namespace rt_info {
@@ -540,6 +584,8 @@ void XmlSerializer::append_rt_info(pugi::xml_node& node, ov::RTMap& attributes)
rt_node.remove_child(attribute_node);
}
}
+ } else if (!m_deterministic) {
+ has_attrs = append_custom_rt_info(rt_node, item.first, item.second) || has_attrs;
}
}
if (!has_attrs) {
@@ -1085,4 +1131,20 @@ std::string get_ir_precision_name(const element::Type& precision) {
return ov::util::to_upper(precision.get_type_name());
}
}
+
+std::string rt_info_get_user_name(const std::string& custom_name) {
+ return std::string{util::rt_info_user_data_tag} + custom_name;
+}
+
+Any& rt_info_get_user_data(AnyMap& rt_map, const std::string& custom_name) {
+ return rt_map.at(rt_info_get_user_name(custom_name));
+}
+
+const Any& rt_info_get_user_data(const AnyMap& rt_map, const std::string& custom_name) {
+ return rt_map.at(rt_info_get_user_name(custom_name));
+}
+
+void rt_info_set_user_data(AnyMap& rt_map, const std::string& custom_name, const Any& value) {
+ rt_map[rt_info_get_user_name(custom_name)] = value;
+}
} // namespace ov::util
diff --git a/src/core/tests/pass/serialization/rt_info_serialization.cpp b/src/core/tests/pass/serialization/rt_info_serialization.cpp
index 1859bdf5896..ecef77beed2 100644
--- a/src/core/tests/pass/serialization/rt_info_serialization.cpp
+++ b/src/core/tests/pass/serialization/rt_info_serialization.cpp
@@ -4,17 +4,25 @@
#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"
#include "openvino/frontend/manager.hpp"
+#include "openvino/op/abs.hpp"
#include "openvino/op/add.hpp"
+#include "openvino/op/constant.hpp"
#include "openvino/op/matmul.hpp"
#include "openvino/pass/manager.hpp"
#include "openvino/pass/serialize.hpp"
#include "openvino/runtime/core.hpp"
+#include "transformations/hash.hpp"
#include "transformations/rt_info/attributes.hpp"
+namespace ov::test {
+
class RTInfoSerializationTest : public ov::test::TestsCommon {
protected:
std::string m_out_xml_path;
@@ -301,3 +309,220 @@ TEST(OvSerializationTests, SerializeRawMeta) {
EXPECT_EQ(0, serialized_model.compare(ir_with_rt_info));
}
}
+
+TEST(RTInfoSerialization, user_data) {
+ std::string ref_ir_xml = R"V0G0N(<?xml version="1.0"?>
+<net name="UserDataTest" version="11">
+ <layers>
+ <layer id="0" name="node_A" type="Parameter" version="opset1">
+ <data shape="10,10" element_type="f32" />
+ <rt_info>
+ <user_data name="node_info_A" value="v_A" version="" />
+ </rt_info>
+ <output>
+ <port id="0" precision="FP32">
+ <dim>10</dim>
+ <dim>10</dim>
+ <rt_info>
+ <user_data name="output_info_A" value="o_A" version="" />
+ </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>
+ <user_data name="node_info_B" value="v_B" version="" />
+ </rt_info>
+ <output>
+ <port id="0" precision="FP32">
+ <dim>1</dim>
+ <rt_info>
+ <user_data name="output_info_B" value="o_B" version="" />
+ </rt_info>
+ </port>
+ </output>
+ </layer>
+ <layer id="2" name="node_C" type="Add" version="opset1">
+ <data auto_broadcast="numpy" />
+ <rt_info>
+ <user_data name="node_info_C" value="v_C" version="" />
+ </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>
+ <user_data name="output_info_C" value="o_C" version="" />
+ <user_data name="output_info_D" value="o_D" version="" />
+ </rt_info>
+ </port>
+ </output>
+ </layer>
+ <layer id="3" name="node_D" type="Result" version="opset1">
+ <rt_info>
+ <user_data name="node_info_D" value="v_D" version="" />
+ </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 model = std::make_shared<Model>(ResultVector{result}, ParameterVector{data});
+ uint64_t bare_model_hash;
+ pass::Hash{bare_model_hash}.run_on_model(model);
+
+ model->set_friendly_name("UserDataTest");
+
+ const auto prefix = std::string{"user_data"};
+ const auto add_info = [&prefix](const std::shared_ptr<Node>& node, const std::string& value) {
+ node->set_friendly_name("node_" + value);
+ node->get_rt_info()[prefix + "node_info_" + value] = "v_" + value;
+ node->output(0).get_rt_info()[prefix + "output_info_" + value] = "o_" + value;
+ };
+ add_info(data, "A");
+ add_info(one, "B");
+ add_info(add, "C");
+ add_info(result, "D");
+
+ auto& one_rti = one->get_rt_info();
+ one_rti[prefix] = "stand alone prefix not saved";
+ one_rti["User_data name"] = "non predefined prefix not saved";
+ one_rti["b name"] = "missing prefix not saved";
+
+ 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);
+
+ uint64_t with_custom_rt_info_hash;
+ pass::Hash{with_custom_rt_info_hash}.run_on_model(model);
+ EXPECT_EQ(bare_model_hash, with_custom_rt_info_hash)
+ << "`ov::pass::Hash' output value must not be affected by custom rt info.";
+}
+
+TEST(RTInfoSerialization, user_AnyMap) {
+ std::string ref_ir_xml = R"V0G0N(<?xml version="1.0"?>
+<net name="UserDataTest" 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>
+ <user_data name="AnyMap" version="">
+ <user_data name="User_data" value="non predefined prefix saved as is" version="" />
+ <user_data name="a" value="b" version="" />
+ <user_data name="i" value="7" version="" />
+ <user_data name="nested" version="">
+ <user_data name="c" value="d" version="" />
+ </user_data>
+ <user_data name="x" value="3.14" version="" />
+ </user_data>
+ </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);
+
+ const auto model = std::make_shared<Model>(ResultVector{result}, ParameterVector{data});
+ uint64_t bare_model_hash;
+ pass::Hash{bare_model_hash}.run_on_model(model);
+
+ model->set_friendly_name("UserDataTest");
+ data->set_friendly_name("data");
+ abs->set_friendly_name("abs");
+ result->set_friendly_name("result");
+
+ const auto prefix = std::string{"user_data"};
+ const auto empty = AnyMap{};
+ const auto nested = AnyMap{{"c", "d"}};
+ abs->get_rt_info()[prefix + "AnyMap"] = AnyMap{{"a", "b"},
+ {"empty", empty},
+ {"i", 7},
+ {"x", 3.14},
+ {prefix + "nested", nested},
+ {"User_data", "non predefined prefix saved as is"}};
+
+ 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);
+
+ uint64_t with_custom_rt_info_hash;
+ pass::Hash{with_custom_rt_info_hash}.run_on_model(model);
+ EXPECT_EQ(bare_model_hash, with_custom_rt_info_hash)
+ << "`ov::pass::Hash' output value must not be affected by custom rt info.";
+}
+
+TEST(RTInfoSerialization, nullptr_no_throw) {
+ 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);
+
+ const auto prefix = std::string{"user_data"};
+ abs->get_rt_info()[prefix + "bare"] = ov::Any{nullptr};
+ abs->get_rt_info()[prefix + "void*"] = ov::Any{static_cast<void*>(nullptr)};
+ abs->get_rt_info()[prefix + "shared_ptr"] = ov::Any{std::shared_ptr<void>{}};
+ abs->get_rt_info()[prefix + "RuntimeAttribute"] = ov::Any{std::shared_ptr<ov::RuntimeAttribute>{}};
+
+ const auto model = std::make_shared<Model>(ResultVector{result}, ParameterVector{data});
+ std::stringstream model_ss, weights_ss;
+ EXPECT_NO_THROW((ov::pass::Serialize{model_ss, weights_ss}.run_on_model(model)));
+}
+} // namespace ov::test
diff --git a/src/core/xml_util/src/xml_deserialize_util.cpp b/src/core/xml_util/src/xml_deserialize_util.cpp
index 540bde2374b..99b325cfeed 100644
--- a/src/core/xml_util/src/xml_deserialize_util.cpp
+++ b/src/core/xml_util/src/xml_deserialize_util.cpp
@@ -151,6 +151,27 @@ std::unordered_set<std::string> deserialize_tensor_names(const std::string_view&
return output_names;
}
+
+void set_custom_rt_info(const pugi::xml_node& rt_attrs, ov::AnyMap& rt_info, bool prefix_needed = true) {
+ constexpr std::string_view rt_info_user_data_tag{"user_data"};
+ std::string custom_name, custom_value;
+ for (const auto& item : rt_attrs) {
+ if (std::strcmp(item.name(), rt_info_user_data_tag.data()) == 0) {
+ if (getStrAttribute(item, "name", custom_name)) {
+ const auto name = std::string{prefix_needed ? rt_info_user_data_tag : ""} + custom_name;
+ if (getStrAttribute(item, "value", custom_value)) {
+ rt_info.emplace(name, custom_value);
+ } else {
+ rt_info.erase(name);
+ if (auto map_elem = rt_info.emplace(name, ov::AnyMap{}); map_elem.second) {
+ auto& nested_map = map_elem.first->second.as<ov::AnyMap>();
+ set_custom_rt_info(item, nested_map, false);
+ }
+ }
+ }
+ }
+ }
+}
} // namespace
struct GenericLayerParams {
@@ -1350,32 +1371,32 @@ std::shared_ptr<ov::Node> XmlDeserializer::create_node(const std::vector<ov::Out
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 && getStrAttribute(item, "name", attribute_name) &&
+ getStrAttribute(item, "version", attribute_version)) {
+ 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..c63ad23e40a
--- /dev/null
+++ b/src/frontends/ir/tests/rt_info_custom.cpp
@@ -0,0 +1,256 @@
+// Copyright (C) 2018-2024 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include <gtest/gtest.h>
+
+#include <sstream>
+
+#include "common_test_utils/test_assertions.hpp"
+#include "openvino/op/abs.hpp"
+#include "openvino/pass/serialize.hpp"
+#include "openvino/runtime/core.hpp"
+
+#define XML_UTIL_LINKAGE_WORKAROUND
+/*
+ There is an issue with doubled definition of stringstream on MSVC 14.44 (VS 17 2022):
+ openvinod.lib(openvinod.dll) : error LNK2005: "protected: __cdecl std::basic_streambuf<char,struct
+ std::char_traits<char> >::basic_streambuf<char,struct std::char_traits<ch ar> >(void)"
+ (??0?$basic_streambuf@DU?$char_traits@D@std@@@std@@IEAA@XZ) already defined in frontend_test_basic.obj
+ [D:\ov\build\src\frontends\ir\tests\ov_ir_frontend_tests .vcxproj]
+ It's not certain whether it's OV or MS problem, thus the workaround.
+*/
+#ifdef XML_UTIL_LINKAGE_WORKAROUND
+namespace ov::util {
+OPENVINO_API std::string rt_info_get_user_name(const std::string& custom_name);
+OPENVINO_API Any& rt_info_get_user_data(AnyMap& rt_map, const std::string& custom_name);
+OPENVINO_API const Any& rt_info_get_user_data(const AnyMap& rt_map, const std::string& custom_name);
+OPENVINO_API void rt_info_set_user_data(AnyMap& rt_map, const std::string& custom_name, const Any& value);
+} // namespace ov::util
+#else
+# include "openvino/xml_util/xml_serialize_util.hpp"
+#endif
+
+namespace ov::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>
+ <!-- 'version' tag-attribute presence or value shouldn't matter -->
+ <user_data name="infoA" value="A" version="" />
+ <user_data name="infoB" value="B" version="0" />
+ <user_data name="infoB" value="BB" version="A" />
+ <user_data name="fused_names_0" value="a_name" />
+ <user_data name="fused_names" value="b_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>
+ <user_data 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>
+ <user_data 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" />
+ <user_data 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";
+
+ const auto check_model = [](const Model* const model) {
+ std::string value;
+
+ const auto& param_rti = model->get_parameters().at(0)->get_rt_info();
+ EXPECT_EQ(param_rti.size(), 5);
+
+ 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 = util::rt_info_get_user_data(param_rti, "fused_names_0").as<std::string>());
+ EXPECT_EQ(value.compare("a_name"), 0);
+ OV_ASSERT_NO_THROW(value = util::rt_info_get_user_data(param_rti, "fused_names").as<std::string>());
+ EXPECT_EQ(value.compare("b_name"), 0);
+
+ OV_ASSERT_NO_THROW(value = util::rt_info_get_user_data(param_rti, "infoA").as<std::string>());
+ EXPECT_EQ(value.compare("A"), 0);
+ OV_ASSERT_NO_THROW(value = util::rt_info_get_user_data(param_rti, "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 = util::rt_info_get_user_data(abs_rti, "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 = util::rt_info_get_user_data(abs_output_rti, "infoD").as<std::string>());
+ EXPECT_EQ(value.compare("D"), 0);
+
+ const auto& result_rti = result->get_rt_info();
+ EXPECT_EQ(result_rti.size(), 2);
+ OV_ASSERT_NO_THROW(value = result_rti.at("primitives_priority_0").as<std::string>());
+ EXPECT_EQ(value.compare("the_prior"), 0);
+ OV_ASSERT_NO_THROW(value = util::rt_info_get_user_data(result_rti, "primitives_priority_0").as<std::string>());
+ EXPECT_EQ(value.compare("a_prior"), 0);
+ };
+
+ Core core;
+ auto model_0 = core.read_model(ref_ir_xml, Tensor{});
+ ASSERT_NE(nullptr, model_0);
+ check_model(model_0.get());
+
+ std::stringstream model_s, weights_s;
+ pass::Serialize{model_s, weights_s}.run_on_model(model_0);
+ const auto model_1 = core.read_model(model_s.str(), Tensor{});
+ ASSERT_NE(nullptr, model_1);
+ check_model(model_1.get());
+}
+
+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>
+ <user_data name="infoA" value="A" />
+ <user_data name="nested">
+ <user_data name="infoB" value="B" />
+ <user_data name="infoC" value="C" />
+ </user_data>
+ </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>
+ <user_data name="nested_0">
+ <user_data name="nested_1">
+ <user_data name="infoD" value="D" />
+ </user_data>
+ </user_data>
+ </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";
+
+ 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 = util::rt_info_get_user_data(param_rti, "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 = util::rt_info_get_user_data(abs_rti, "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);
+}
+
+TEST(RTInfoCustom, RuntimeAttribute_priority) {
+ 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);
+ const auto model = std::make_shared<Model>(ResultVector{result}, ParameterVector{data});
+
+ auto& info = abs->get_rt_info();
+ const auto layout_custom_id = util::rt_info_get_user_name("layout");
+ const auto layout_custom_value = std::string{"ABCxyz"};
+ const auto layout_attribute_id = std::string{LayoutAttribute::get_type_info_static()};
+ const auto layout_attribute_value = LayoutAttribute{"NCHW"};
+ info[layout_custom_id] = layout_custom_value;
+ info[layout_attribute_id] = "CWHN";
+ info["L_A_Y_O_U_T"] = layout_attribute_value;
+
+ std::stringstream model_s, weights_s;
+ pass::Serialize{model_s, weights_s}.run_on_model(model);
+ const auto r_model = Core{}.read_model(model_s.str(), Tensor{});
+
+ const auto& r_abs_rt_info = r_model->get_output_op(0)->input(0).get_source_output().get_node()->get_rt_info();
+ EXPECT_EQ(r_abs_rt_info.size(), 2);
+
+ LayoutAttribute la;
+ OV_ASSERT_NO_THROW(la = r_abs_rt_info.at(layout_attribute_id).as<LayoutAttribute>());
+ EXPECT_EQ(la.to_string(), layout_attribute_value.to_string());
+
+ std::string custom;
+ OV_ASSERT_NO_THROW(custom = r_abs_rt_info.at(layout_custom_id).as<std::string>());
+ EXPECT_EQ(custom, layout_custom_value);
+}
+} // namespace ov::test