Skip to content

Commit

Permalink
Add nested AnyMap serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
t-jankowski committed Sep 26, 2024
1 parent 42987a7 commit 7a1c68d
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 20 deletions.
43 changes: 30 additions & 13 deletions src/core/src/pass/serialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,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()) {
Expand All @@ -939,11 +939,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_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 has_value = false;

if (data.is<ov::AnyMap>()) {
const auto& any_map = data.as<ov::AnyMap>();
for (const auto& it : any_map)
has_value |= append_custom_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());
has_value = true;
}

if (!has_value)
node.remove_child(custom_node);
return has_value;
}

void ngfunction_2_ir(pugi::xml_node& netXml,
const ov::Model& model,
ConstantWriter& constant_node_write_handler,
Expand Down Expand Up @@ -1007,7 +1028,7 @@ 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 (const auto& item : attributes) {
Expand All @@ -1025,17 +1046,13 @@ void ngfunction_2_ir(pugi::xml_node& netXml,
}
}
}
for (const auto& item : attributes) {
if (!item.second.is<ov::RuntimeAttribute>()) {
auto custom_node = rt_node.append_child("custom");
custom_node.append_attribute("name").set_value(item.first.c_str());
custom_node.append_attribute("value").set_value(item.second.as<std::string>().c_str());
has_attrs = true;
}
}
if (!has_attrs) {

for (const auto& item : attributes)
if (!item.second.is<ov::RuntimeAttribute>())
has_attrs |= append_custom_info(rt_node, item.first, item.second);

if (!has_attrs)
node.remove_child(rt_node);
}
};

if (version >= 11) {
Expand Down
86 changes: 79 additions & 7 deletions src/core/tests/pass/serialization/rt_info_serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,13 +304,14 @@ TEST(OvSerializationTests, SerializeRawMeta) {
#include <sstream>

using namespace ov;
using ov::op::v0::Abs;
using ov::op::v0::Parameter;
using ov::op::v0::Result;
using ov::op::v1::Add;

TEST(SerializeCustomRTI, basic_RENAMEME) {
std::string ir = R"V0G0N(<?xml version="1.0"?>
<net name="model_T" version="11">
<net name="CustomRTI" version="11">
<layers>
<layer id="0" name="node_A" type="Parameter" version="opset1">
<data shape="10,10" element_type="f32" />
Expand Down Expand Up @@ -387,22 +388,93 @@ TEST(SerializeCustomRTI, basic_RENAMEME) {
</net>
)V0G0N";

const auto data = std::make_shared<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<Add>(data, one);
const auto result = std::make_shared<Result>(add);

const auto add_info = [](const std::shared_ptr<Node>& node, const std::string& value) {
const_cast<std::string&>(node->get_name()) = "node_" + value;
node->get_rt_info()["node_info_" + value] = "v_" + value;
node->output(0).get_rt_info()["output_info_" + value] = "o_" + value;
};

const auto data = std::make_shared<Parameter>(element::Type_t::f32, Shape{10, 10});
add_info(data, "A");
const auto one = std::make_shared<op::v0::Constant>(element::f32, Shape{1}, std::vector<float>{1.f});
add_info(one, "B");
const auto add = std::make_shared<Add>(data, one);
add_info(add, "C");
const auto result = std::make_shared<Result>(add);
add_info(result, "D");

const auto model = std::make_shared<Model>(ResultVector{result}, ParameterVector{data});
const_cast<std::string&>(model->get_name()) = "CustomRTI";

std::stringstream model_ss, weights_ss;
EXPECT_NO_THROW((ov::pass::Serialize{model_ss, weights_ss}.run_on_model(model)));
EXPECT_EQ(ir.compare(model_ss.str()), 0);
}

TEST(SerializeCustomRTI, AnyMap_RENAMEME) {
std::string ir = 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<Parameter>(element::Type_t::f64, Shape{111});
const auto abs = std::make_shared<Abs>(data);
const auto result = std::make_shared<Result>(abs);

const_cast<std::string&>(data->get_name()) = "data";
const_cast<std::string&>(abs->get_name()) = "abs";
const_cast<std::string&>(result->get_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});
const_cast<std::string&>(model->get_name()) = "model_T";
const_cast<std::string&>(model->get_name()) = "CustomRTI";

std::stringstream model_ss, weights_ss;
EXPECT_NO_THROW((ov::pass::Serialize{model_ss, weights_ss}.run_on_model(model)));
Expand Down

0 comments on commit 7a1c68d

Please sign in to comment.