Skip to content

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

[Core] Validate model before serialization (#26184)

Tomasz Jankowski01ddcd2bb84c
diff --git a/src/core/src/pass/serialize.cpp b/src/core/src/pass/serialize.cpp
index 1d5e295e8db..55acbda9b41 100644
--- a/src/core/src/pass/serialize.cpp
+++ b/src/core/src/pass/serialize.cpp
@@ -1236,6 +1236,8 @@ namespace ov {
bool pass::Serialize::run_on_model(const std::shared_ptr<ov::Model>& model) {
RUN_ON_FUNCTION_SCOPE(Serialize);
+ model->validate_nodes_and_infer_types();
+
// TODO xxx-105807: if rt_info is set in python api as a string ['precise_0'] = '',
// we need to convert value to a class in order to have rt_info in the IR. The code below will convert
// ['precise_0'] = '' into => rt_info['precise_0'] = DisableFP16Compression{}
diff --git a/src/core/tests/pass/serialization/cleanup.cpp b/src/core/tests/pass/serialization/cleanup.cpp
index bf13d5e4a2f..683e9c1369c 100644
--- a/src/core/tests/pass/serialization/cleanup.cpp
+++ b/src/core/tests/pass/serialization/cleanup.cpp
@@ -56,3 +56,12 @@ TEST_F(SerializationCleanupTest, SerializationShouldWorkWithDynamicFunction) {
ASSERT_TRUE(std::ifstream(m_out_xml_path, std::ios::in).good());
ASSERT_TRUE(std::ifstream(m_out_bin_path, std::ios::in).good());
}
+
+TEST_F(SerializationCleanupTest, SerializationShouldNotWorkWithMissingParameter) {
+ const auto model = create_test_model("RemovedParameter", ov::PartialShape{2});
+ model->remove_parameter(model->get_parameters()[0]);
+
+ ASSERT_ANY_THROW(ov::pass::Serialize(m_out_xml_path, m_out_bin_path).run_on_model(model));
+ EXPECT_FALSE(std::ifstream(m_out_xml_path, std::ios::in).good());
+ EXPECT_FALSE(std::ifstream(m_out_bin_path, std::ios::in).good());
+}