Skip to content

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

[core] Expand IR serializer to known OV precisions (#30773)

Pawel Raasz27c475fa236f
diff --git a/src/core/src/pass/serialize.cpp b/src/core/src/pass/serialize.cpp
index 9eb957bd8e1..0446d276109 100644
--- a/src/core/src/pass/serialize.cpp
+++ b/src/core/src/pass/serialize.cpp
@@ -37,6 +37,7 @@
#include "openvino/runtime/aligned_buffer.hpp"
#include "openvino/runtime/compute_hash.hpp"
#include "openvino/runtime/string_aligned_buffer.hpp"
+#include "openvino/util/common_util.hpp"
#include "openvino/util/file_util.hpp"
#include "pugixml.hpp"
#include "transformations/hash.hpp"
@@ -753,54 +754,22 @@ std::string get_opset_name(const ov::Node* n) {
std::string get_precision_name(const ov::element::Type& elem_type) {
switch (elem_type) {
- case ::ov::element::Type_t::dynamic:
+ case ov::element::dynamic:
return "UNSPECIFIED";
- case ::ov::element::Type_t::f16:
+ case ov::element::boolean:
+ return "BOOL";
+ case ov::element::u1:
+ return "BIN";
+ case ov::element::f16:
return "FP16";
- case ::ov::element::Type_t::f32:
+ case ov::element::f32:
return "FP32";
- case ::ov::element::Type_t::bf16:
+ case ov::element::bf16:
return "BF16";
- case ::ov::element::Type_t::f64:
+ case ov::element::f64:
return "FP64";
- case ::ov::element::Type_t::i4:
- return "I4";
- case ::ov::element::Type_t::i8:
- return "I8";
- case ::ov::element::Type_t::i16:
- return "I16";
- case ::ov::element::Type_t::i32:
- return "I32";
- case ::ov::element::Type_t::i64:
- return "I64";
- case ::ov::element::Type_t::u4:
- return "U4";
- case ::ov::element::Type_t::u8:
- return "U8";
- case ::ov::element::Type_t::u16:
- return "U16";
- case ::ov::element::Type_t::u32:
- return "U32";
- case ::ov::element::Type_t::u64:
- return "U64";
- case ::ov::element::Type_t::u1:
- return "BIN";
- case ::ov::element::Type_t::boolean:
- return "BOOL";
- case ::ov::element::Type_t::nf4:
- return "NF4";
- case ::ov::element::Type_t::f8e4m3:
- return "F8E4M3";
- case ::ov::element::Type_t::f8e5m2:
- return "F8E5M2";
- case ::ov::element::Type_t::string:
- return "STRING";
- case ::ov::element::Type_t::f4e2m1:
- return "F4E2M1";
- case ::ov::element::Type_t::f8e8m0:
- return "F8E8M0";
default:
- OPENVINO_THROW("Unsupported precision: ", elem_type);
+ return ov::util::to_upper(elem_type.get_type_name());
}
}
diff --git a/src/core/tests/pass/serialization/serialize.cpp b/src/core/tests/pass/serialization/serialize.cpp
index 21000882d23..d38b447e44f 100644
--- a/src/core/tests/pass/serialization/serialize.cpp
+++ b/src/core/tests/pass/serialization/serialize.cpp
@@ -12,9 +12,85 @@
#include "common_test_utils/graph_comparator.hpp"
#include "common_test_utils/test_common.hpp"
#include "openvino/core/graph_util.hpp"
+#include "openvino/op/add.hpp"
#include "openvino/util/file_util.hpp"
#include "read_ir.hpp"
+namespace ov::test {
+
+using op::v0::Parameter, op::v0::Constant, op::v1::Add;
+
+class SerializePassTest : public testing::Test {
+protected:
+ std::filesystem::path m_out_xml_path;
+ std::filesystem::path m_out_bin_path;
+ std::shared_ptr<Model> m_model;
+
+ void SetUp() override {
+ const auto filePrefix = ov::test::utils::generateTestFilePrefix();
+ m_out_xml_path = filePrefix + ".xml";
+ m_out_bin_path = filePrefix + ".bin";
+ }
+
+ void TearDown() override {
+ if (std::filesystem::exists(m_out_xml_path)) {
+ std::filesystem::remove(m_out_xml_path);
+ }
+
+ if (std::filesystem::exists(m_out_bin_path)) {
+ std::filesystem::remove(m_out_bin_path);
+ }
+ }
+
+ static FunctionsComparator model_comparator() {
+ return FunctionsComparator::with_default()
+ .enable(FunctionsComparator::ATTRIBUTES)
+ .enable(FunctionsComparator::CONST_VALUES);
+ }
+};
+
+class SerializePassTestP : public SerializePassTest, public testing::WithParamInterface<element::Type> {};
+
+INSTANTIATE_TEST_SUITE_P(numeric_types,
+ SerializePassTestP,
+ testing::Values(element::bf16,
+ element::f16,
+ element::f32,
+ element::f64,
+ element::i4,
+ element::i8,
+ element::i16,
+ element::i32,
+ element::i64,
+ element::u1,
+ element::u2,
+ element::u4,
+ element::u8,
+ element::u16,
+ element::u32,
+ element::u64,
+ element::nf4,
+ element::f8e4m3,
+ element::f8e5m2,
+ element::f4e2m1,
+ element::f8e8m0),
+ testing::PrintToStringParamName());
+
+TEST_P(SerializePassTestP, serialize_simple_model_with_constant) {
+ const auto& precision = GetParam();
+ const auto p1 = std::make_shared<Parameter>(precision, PartialShape{5});
+ const auto c1 = std::make_shared<Constant>(precision, Shape{5}, std::vector{1, 0, 1, 1, 1});
+ const auto add = std::make_shared<Add>(p1, c1);
+ m_model = std::make_shared<Model>(OutputVector{add}, ParameterVector{p1}, "simple_model");
+
+ OV_ASSERT_NO_THROW(pass::Serialize(m_out_xml_path, m_out_bin_path).run_on_model(m_model));
+
+ const auto serialized_model = test::readModel(m_out_xml_path.string(), m_out_bin_path.string());
+ const auto& [is_valid, error_msg] = model_comparator().compare(serialized_model, m_model);
+ EXPECT_TRUE(is_valid) << error_msg;
+}
+} // namespace ov::test
+
using SerializationParams = std::tuple<std::string, std::string>;
class SerializationTest : public ov::test::TestsCommon, public testing::WithParamInterface<SerializationParams> {