Skip to content

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

[Core] Fix string Constant serialization incorrect `ConstantWriter` deduplication (#34955)

Przemyslaw Wysocki00780a3ea5d6
diff --git a/src/core/dev_api/openvino/xml_util/constant_writer.hpp b/src/core/dev_api/openvino/xml_util/constant_writer.hpp
index 59190b9be83..210dad45073 100644
--- a/src/core/dev_api/openvino/xml_util/constant_writer.hpp
+++ b/src/core/dev_api/openvino/xml_util/constant_writer.hpp
@@ -6,6 +6,8 @@
#include <iostream>
#include <map>
+#include <string_view>
+#include <vector>
#include "openvino/core/attribute_visitor.hpp"
#include "openvino/core/type/element_type.hpp"
@@ -29,6 +31,8 @@ public:
ov::element::Type src_type = ov::element::dynamic,
bool ptr_is_temporary = false);
+ virtual FilePosition write(const std::vector<std::string_view>& chunks, size_t& new_size);
+
uint64_t get_data_hash() const {
return m_data_hash;
}
@@ -40,6 +44,7 @@ private:
size_t& compressed_size);
ConstWritePositions m_hash_to_file_positions;
+ std::vector<std::vector<char>> m_packed_string_data;
std::reference_wrapper<std::ostream> m_binary_output;
bool m_enable_compression;
FilePosition m_blob_offset; // blob offset inside output stream
diff --git a/src/core/src/xml_util/constant_writer.cpp b/src/core/src/xml_util/constant_writer.cpp
index f933e3db53e..86b10b9c6b1 100644
--- a/src/core/src/xml_util/constant_writer.cpp
+++ b/src/core/src/xml_util/constant_writer.cpp
@@ -61,6 +61,46 @@ ConstantWriter::FilePosition ConstantWriter::write(const char* ptr,
return offset;
}
+ConstantWriter::FilePosition ConstantWriter::write(const std::vector<std::string_view>& chunks, size_t& new_size) {
+ new_size = 0;
+ for (const auto& sv : chunks)
+ new_size += sv.size();
+
+ if (m_enable_compression) {
+ std::vector<char> tmp(new_size);
+ char* dst = tmp.data();
+ for (const auto& sv : chunks) {
+ std::memcpy(dst, sv.data(), sv.size());
+ dst += sv.size();
+ }
+
+ const HashValue hash = ov::runtime::compute_hash(tmp.data(), new_size);
+ const auto found = m_hash_to_file_positions.equal_range(hash);
+ for (auto it = found.first; it != found.second; ++it) {
+ if (memcmp(tmp.data(), it->second.second, new_size) == 0) {
+ return it->second.first;
+ }
+ }
+
+ // Cache miss: store the packed buffer so its pointer stays valid for future memcmp
+ m_packed_string_data.push_back(std::move(tmp));
+ const char* stable_ptr = m_packed_string_data.back().data();
+ const FilePosition write_pos = m_binary_output.get().tellp();
+ const FilePosition offset = write_pos - m_blob_offset;
+ m_hash_to_file_positions.insert({hash, {offset, static_cast<const void*>(stable_ptr)}});
+ m_data_hash = util::u64_hash_combine(m_data_hash, hash);
+ m_binary_output.get().write(stable_ptr, new_size);
+ return offset;
+ } else {
+ const FilePosition write_pos = m_binary_output.get().tellp();
+ const FilePosition offset = write_pos - m_blob_offset;
+ m_data_hash = util::u64_hash_combine(m_data_hash, new_size);
+ for (const auto& sv : chunks)
+ m_binary_output.get().write(sv.data(), sv.size());
+ return offset;
+ }
+}
+
std::unique_ptr<char[]> ConstantWriter::compress_data_to_fp16(const char* ptr,
size_t size,
const element::Type& src_type,
diff --git a/src/core/src/xml_util/xml_serialize_util.cpp b/src/core/src/xml_util/xml_serialize_util.cpp
index 56333027b8f..2241d265517 100644
--- a/src/core/src/xml_util/xml_serialize_util.cpp
+++ b/src/core/src/xml_util/xml_serialize_util.cpp
@@ -6,6 +6,7 @@
#include <functional>
#include <pugixml.hpp>
+#include <string_view>
#include "openvino/core/descriptor_tensor.hpp"
#include "openvino/core/except.hpp"
@@ -707,9 +708,7 @@ void XmlSerializer::on_adapter(const std::string& name, ov::ValueAccessor<void>&
if (name == "value" && translate_type_name(m_node_type_name) == "Const") {
auto a1 = ov::as_type<ov::AttributeAdapter<std::shared_ptr<ov::StringAlignedBuffer>>>(&adapter);
auto a2 = ov::as_type<ov::AttributeAdapter<std::shared_ptr<ov::SharedStringAlignedBuffer>>>(&adapter);
- size_t new_size = 0;
- size_t inter_size = 0;
- // write a header of packed string tensor
+
std::shared_ptr<uint8_t> header_ptr = nullptr;
size_t header_size = 0;
if (a1) {
@@ -718,22 +717,16 @@ void XmlSerializer::on_adapter(const std::string& name, ov::ValueAccessor<void>&
a2->get_header(header_ptr, header_size);
}
- int64_t offset = get_constant_write_handler().write(
- reinterpret_cast<const char*>(header_ptr.get()),
- header_size,
- inter_size,
- m_compress_to_fp16,
- m_output_element_type,
- true); // header_ptr is allocated in AttributeAdapter that has limited life time
- new_size += inter_size;
-
- // write raw strings part
size_t num_elements = 0;
if (a1) {
num_elements = a1->get()->get_num_elements();
} else {
num_elements = a2->get()->get_num_elements();
}
+
+ std::vector<std::string_view> chunks;
+ chunks.reserve(1 + num_elements);
+ chunks.emplace_back(reinterpret_cast<const char*>(header_ptr.get()), header_size);
for (size_t ind = 0; ind < num_elements; ++ind) {
const char* raw_string_ptr;
size_t raw_string_size;
@@ -742,16 +735,12 @@ void XmlSerializer::on_adapter(const std::string& name, ov::ValueAccessor<void>&
} else {
a2->get_raw_string_by_index(raw_string_ptr, raw_string_size, ind);
}
+ chunks.emplace_back(raw_string_ptr, raw_string_size);
+ }
- get_constant_write_handler().write(raw_string_ptr,
- raw_string_size,
- inter_size,
- m_compress_to_fp16,
- m_output_element_type,
- m_data_is_temporary);
+ size_t new_size = 0;
+ const auto offset = get_constant_write_handler().write(chunks, new_size);
- new_size += inter_size;
- }
m_xml_node.append_attribute("offset").set_value(static_cast<unsigned long long>(offset));
m_xml_node.append_attribute("size").set_value(static_cast<unsigned long long>(new_size));
}
diff --git a/src/core/tests/pass/serialization/const_compression.cpp b/src/core/tests/pass/serialization/const_compression.cpp
index 8b743f2936e..1cef829551b 100644
--- a/src/core/tests/pass/serialization/const_compression.cpp
+++ b/src/core/tests/pass/serialization/const_compression.cpp
@@ -376,3 +376,71 @@ TEST_F(SerializationConstantCompressionTest, EmptyAndNotEmptyConstantsDifferentV
const auto& [success, message] = compare_functions(model_initial, model_imported, true, true, false, true, true);
ASSERT_TRUE(success) << message;
}
+
+TEST_F(SerializationConstantCompressionTest, StringConstantsRoundTrip) {
+ // Three distinct string constant tensors: verify each one round-trips with exact string content.
+ auto vocab_A = ov::op::v0::Constant::create(ov::element::string,
+ ov::Shape{4},
+ std::vector<std::string>{"UNKNOWN", "cat", "dog", "fish"});
+
+ auto vocab_B = ov::op::v0::Constant::create(ov::element::string,
+ ov::Shape{4},
+ std::vector<std::string>{"UNKNOWN", "red", "green", "blue"});
+
+ auto vocab_C = ov::op::v0::Constant::create(ov::element::string,
+ ov::Shape{3},
+ std::vector<std::string>{"sports", "politics", "tech"});
+
+ auto model_initial =
+ std::make_shared<ov::Model>(ov::OutputVector{vocab_A, vocab_B, vocab_C}, ov::ParameterVector{});
+
+ ov::pass::Serialize(m_out_xml_path_1, m_out_bin_path_1).run_on_model(model_initial);
+
+ ov::Core core;
+ auto model_imported = core.read_model(m_out_xml_path_1, m_out_bin_path_1);
+
+ std::vector<std::shared_ptr<ov::op::v0::Constant>> consts;
+ for (const auto& result : model_imported->get_results()) {
+ if (const auto c = std::dynamic_pointer_cast<ov::op::v0::Constant>(result->get_input_node_shared_ptr(0))) {
+ if (c->get_element_type() == ov::element::string) {
+ consts.push_back(c);
+ }
+ }
+ }
+ ASSERT_EQ(consts.size(), 3u);
+
+ std::vector<std::vector<std::string>> actual;
+ for (const auto& c : consts) {
+ actual.push_back(c->get_vector<std::string>());
+ }
+
+ std::vector<std::vector<std::string>> expected{
+ {"UNKNOWN", "cat", "dog", "fish"},
+ {"UNKNOWN", "red", "green", "blue"},
+ {"sports", "politics", "tech"},
+ };
+
+ EXPECT_EQ(actual, expected);
+}
+
+TEST_F(SerializationConstantCompressionTest, IdenticalStringConstantsRoundTrip) {
+ const std::vector<std::string> vocab{"UNKNOWN", "cat", "dog", "fish"};
+
+ auto A = ov::op::v0::Constant::create(ov::element::string, ov::Shape{4}, vocab);
+ auto B = ov::op::v0::Constant::create(ov::element::string, ov::Shape{4}, vocab);
+
+ auto model = std::make_shared<ov::Model>(ov::OutputVector{A, B}, ov::ParameterVector{});
+
+ ov::pass::Serialize(m_out_xml_path_1, m_out_bin_path_1).run_on_model(model);
+
+ ov::Core core;
+ auto model_imported = core.read_model(m_out_xml_path_1, m_out_bin_path_1);
+
+ for (const auto& result : model_imported->get_results()) {
+ if (const auto c = std::dynamic_pointer_cast<ov::op::v0::Constant>(result->get_input_node_shared_ptr(0))) {
+ if (c->get_element_type() == ov::element::string) {
+ EXPECT_EQ(c->get_vector<std::string>(), vocab);
+ }
+ }
+ }
+}