Skip to content

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

Fix postponed constant serialization to keep model unchanged (#32606)

Oleg Pipikinb32be6d184e2
diff --git a/src/core/src/xml_util/xml_serialize_util.cpp b/src/core/src/xml_util/xml_serialize_util.cpp
index 028e0eb733f..36bae43b0ba 100644
--- a/src/core/src/xml_util/xml_serialize_util.cpp
+++ b/src/core/src/xml_util/xml_serialize_util.cpp
@@ -53,8 +53,12 @@ public:
if (node->get_rt_info().count("postponed_constant")) {
OPENVINO_ASSERT(node->get_output_size() == 1);
ov::OutputVector outputs(1);
+ std::shared_ptr<ov::Node> node_clone;
if (ov::pass::constant_folding_is_disabled(node)) {
- node->get_rt_info().erase(ov::pass::DisableConstantFolding::get_type_info_static());
+ // clone to keep original node unchanged
+ node_clone = node->clone_with_new_inputs(node->input_values());
+ node_clone->get_rt_info().erase(ov::pass::DisableConstantFolding::get_type_info_static());
+ node = node_clone.get();
}
OPENVINO_ASSERT(
node->constant_fold(outputs, node->input_values()),
@@ -916,8 +920,6 @@ void find_postponed_constants_and_exclude_nodes(const std::vector<std::shared_pt
// If all outputs are excluded, mark this node and continue DFS
if (all_outputs_excluded && node->get_output_size() > 0) {
nodes_to_exclude.insert(node);
- node->get_rt_info()["disabled_for_serialization"] = true;
-
// Recursively process all input nodes
for (const auto& input : node->inputs()) {
reverse_dfs(input.get_source_output().get_node());
diff --git a/src/core/tests/pass/serialization/custom_ops.cpp b/src/core/tests/pass/serialization/custom_ops.cpp
index 4442ad0f257..5b69d84c7f5 100644
--- a/src/core/tests/pass/serialization/custom_ops.cpp
+++ b/src/core/tests/pass/serialization/custom_ops.cpp
@@ -12,6 +12,7 @@
#include "openvino/op/concat.hpp"
#include "openvino/op/constant.hpp"
#include "openvino/op/multiply.hpp"
+#include "openvino/pass/constant_folding.hpp"
#include "openvino/pass/manager.hpp"
#include "openvino/pass/serialize.hpp"
#include "openvino/runtime/core.hpp"
@@ -312,3 +313,48 @@ TEST(PostponedConstantTest, ParameterNotExcluded) {
EXPECT_THROW(ov::pass::Serialize(serialized_xml, serialized_bin).run_on_model(model), ov::Exception);
}
+
+TEST(PostponedConstantTest, ModelIsUnchangedAfterSerialization) {
+ std::stringstream serialized_xml, serialized_bin;
+ {
+ auto const1 =
+ std::make_shared<ov::op::v0::Constant>(ov::element::f32, ov::Shape{2, 2}, std::vector<float>{1, 2, 3, 4});
+ auto const2 =
+ std::make_shared<ov::op::v0::Constant>(ov::element::f32, ov::Shape{2, 2}, std::vector<float>{5, 6, 7, 8});
+ auto concat = std::make_shared<ov::op::v0::Concat>(ov::OutputVector{const1, const2}, 0);
+ auto param = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::Shape{4, 2});
+ auto add = std::make_shared<ov::op::v1::Add>(concat, param);
+ auto model = std::make_shared<ov::Model>(add->outputs(), ov::ParameterVector{param}, "ConcatAddModel");
+
+ concat->get_rt_info()["postponed_constant"] = true;
+ ov::pass::disable_constant_folding(concat);
+
+ auto model_copy = model->clone();
+ ov::pass::Serialize(serialized_xml, serialized_bin).run_on_model(model);
+
+ const auto& [success, message] = compare_functions(model_copy, model, true, true, true, true, true);
+ ASSERT_TRUE(success) << message;
+ }
+
+ // check if the model was serialized with disable_constant_folding rt_info on postponed constant
+ ov::Core core;
+
+ auto weights = serialized_bin.str();
+ ov::Tensor weights_tensor(ov::element::u8, ov::Shape{weights.size()}, weights.data());
+
+ auto deserialized_model = core.read_model(serialized_xml.str(), weights_tensor);
+
+ {
+ auto constant = std::make_shared<ov::op::v0::Constant>(ov::element::f32,
+ ov::Shape{4, 2},
+ std::vector<float>{1, 2, 3, 4, 5, 6, 7, 8});
+ auto param = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::Shape{4, 2});
+ auto add = std::make_shared<ov::op::v1::Add>(constant, param);
+
+ auto expected = std::make_shared<ov::Model>(add->outputs(), ov::ParameterVector{param}, "ConcatAddModel");
+
+ const auto& [success, message] =
+ compare_functions(deserialized_model, expected, true, false, false, true, true);
+ ASSERT_TRUE(success) << message;
+ }
+}