Skip to content

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

[core] Split ConstantWriter and streambuffer responsibilities (#34170)

Pawel Raaszcf6e20fa4d03
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 5ea3eb4c644..59190b9be83 100644
--- a/src/core/dev_api/openvino/xml_util/constant_writer.hpp
+++ b/src/core/dev_api/openvino/xml_util/constant_writer.hpp
@@ -13,14 +13,6 @@
namespace ov::util {
-class OstreamHashWrapperBin final : public std::streambuf {
- uint64_t m_res = 0lu;
-
-public:
- uint64_t get_result() const;
- std::streamsize xsputn(const char* s, std::streamsize n) override;
-};
-
class OPENVINO_API ConstantWriter {
public:
using FilePosition = int64_t;
@@ -37,16 +29,20 @@ public:
ov::element::Type src_type = ov::element::dynamic,
bool ptr_is_temporary = false);
+ uint64_t get_data_hash() const {
+ return m_data_hash;
+ }
+
private:
static std::unique_ptr<char[]> compress_data_to_fp16(const char* ptr,
size_t size,
- ov::element::Type src_type,
+ const element::Type& src_type,
size_t& compressed_size);
ConstWritePositions m_hash_to_file_positions;
std::reference_wrapper<std::ostream> m_binary_output;
bool m_enable_compression;
- bool m_write_hash_value;
FilePosition m_blob_offset; // blob offset inside output stream
+ uint64_t m_data_hash;
};
} // namespace ov::util
diff --git a/src/core/src/pass/serialize.cpp b/src/core/src/pass/serialize.cpp
index 764a35e292d..207b022af88 100644
--- a/src/core/src/pass/serialize.cpp
+++ b/src/core/src/pass/serialize.cpp
@@ -267,11 +267,11 @@ std::unique_ptr<util::XmlSerializer> pass::StreamSerialize::make_serializer(
namespace {
-class OstreamHashWrapper final : public std::streambuf {
+class HashStreamBuffer final : public std::streambuf {
uint64_t m_res = 0lu;
public:
- uint64_t getResult() const {
+ uint64_t get_result() const {
return m_res;
}
@@ -282,24 +282,29 @@ public:
return n;
}
};
+
+class NullStreamBuffer final : public std::streambuf {
+public:
+ std::streamsize xsputn(const char*, std::streamsize n) override {
+ return n;
+ }
+};
} // namespace
bool pass::Hash::run_on_model(const std::shared_ptr<ov::Model>& model) {
RUN_ON_MODEL_SCOPE(Hash);
- OstreamHashWrapper xmlHash;
- util::OstreamHashWrapperBin binHash;
- std::ostream xml(&xmlHash);
- std::ostream bin(&binHash);
+ HashStreamBuffer xml_hash;
+ NullStreamBuffer null_buffer;
+ std::ostream xml(&xml_hash);
+ std::ostream bin(&null_buffer);
// Determinism is important for hash calculation
- // disable compression when skip weight to speed hash calculation
+ // If skip weights set, disable compression to skip internal data hashing
auto constant_writer = util::ConstantWriter(bin, !m_skip_weights);
serialize_func(xml, bin, model, Serialize::Version::UNSPECIFIED, true, constant_writer);
- uint64_t seed = 0;
- seed = util::u64_hash_combine(seed, xmlHash.getResult());
- seed = util::u64_hash_combine(seed, binHash.get_result());
- m_hash = seed;
+ auto seed = util::u64_hash_combine(0, xml_hash.get_result());
+ m_hash = util::u64_hash_combine(seed, constant_writer.get_data_hash());
// Return false because we didn't change OpenVINO Model
return false;
}
diff --git a/src/core/src/xml_util/constant_writer.cpp b/src/core/src/xml_util/constant_writer.cpp
index 82cfe4d3934..f933e3db53e 100644
--- a/src/core/src/xml_util/constant_writer.cpp
+++ b/src/core/src/xml_util/constant_writer.cpp
@@ -11,20 +11,12 @@
namespace ov::util {
-uint64_t OstreamHashWrapperBin::get_result() const {
- return m_res;
-}
-
-std::streamsize OstreamHashWrapperBin::xsputn(const char* s, std::streamsize n) {
- m_res = u64_hash_combine(m_res, n);
- return n;
-}
-
ConstantWriter::ConstantWriter(std::ostream& bin_data, bool enable_compression)
- : m_binary_output(bin_data),
+ : m_hash_to_file_positions{},
+ m_binary_output(bin_data),
m_enable_compression(enable_compression),
- m_write_hash_value(static_cast<bool>(dynamic_cast<OstreamHashWrapperBin*>(bin_data.rdbuf()))),
- m_blob_offset(bin_data.tellp()) {}
+ m_blob_offset(bin_data.tellp()),
+ m_data_hash{} {}
ConstantWriter::~ConstantWriter() = default;
@@ -34,41 +26,21 @@ ConstantWriter::FilePosition ConstantWriter::write(const char* ptr,
bool compress_to_fp16,
ov::element::Type src_type,
bool ptr_is_temporary) {
- // when true, do not rely on ptr after this function call, data
- // is temporary allocated
const FilePosition write_pos = m_binary_output.get().tellp();
const auto offset = write_pos - m_blob_offset;
new_size = size;
- if (!m_enable_compression) {
- if (!compress_to_fp16) {
- m_binary_output.get().write(ptr, size);
- } else {
- OPENVINO_ASSERT(size % src_type.size() == 0);
- auto fp16_buffer = compress_data_to_fp16(ptr, size, src_type, new_size);
- m_binary_output.get().write(fp16_buffer.get(), new_size);
- }
- return offset;
- } else {
- std::unique_ptr<char[]> fp16_buffer = nullptr;
- if (compress_to_fp16) {
- OPENVINO_ASSERT(size % src_type.size() == 0);
- fp16_buffer = compress_data_to_fp16(ptr, size, src_type, new_size);
- }
- const char* ptr_to_write;
- if (fp16_buffer) {
- ptr_to_write = fp16_buffer.get();
- } else {
- ptr_to_write = ptr;
- }
+ const auto fp16_data = compress_to_fp16 ? compress_data_to_fp16(ptr, size, src_type, new_size) : nullptr;
+ const auto data_ptr = compress_to_fp16 ? fp16_data.get() : ptr;
+ if (m_enable_compression) {
// This hash is weak (but efficient). For example current hash algorithms gives
// the same hash for {2, 2} and {0, 128} arrays.
// But even strong hashing algorithms sometimes give collisions.
// Therefore we always have to compare values when finding a match in the hash multimap.
- const HashValue hash = ov::runtime::compute_hash(ptr_to_write, new_size);
+ const HashValue hash = ov::runtime::compute_hash(data_ptr, new_size);
- auto found = m_hash_to_file_positions.equal_range(hash);
+ const auto found = m_hash_to_file_positions.equal_range(hash);
// iterate over all matches of the key in the multimap
for (auto it = found.first; it != found.second; ++it) {
if (memcmp(ptr, it->second.second, size) == 0) {
@@ -80,22 +52,23 @@ ConstantWriter::FilePosition ConstantWriter::write(const char* ptr,
// ostream, we store pointer to the original uncompressed blob.
m_hash_to_file_positions.insert({hash, {offset, static_cast<const void*>(ptr)}});
}
- if (m_write_hash_value) {
- // hash stored with special ostream only
- m_binary_output.get().rdbuf()->sputn(reinterpret_cast<const char*>(&hash), hash);
- } else {
- m_binary_output.get().write(ptr_to_write, new_size);
- }
+ m_data_hash = util::u64_hash_combine(m_data_hash, hash);
+ } else {
+ // fast hash (skip data)
+ m_data_hash = util::u64_hash_combine(m_data_hash, new_size);
}
+ m_binary_output.get().write(data_ptr, new_size);
return offset;
}
std::unique_ptr<char[]> ConstantWriter::compress_data_to_fp16(const char* ptr,
size_t size,
- ov::element::Type src_type,
+ const element::Type& src_type,
size_t& compressed_size) {
auto num_src_elements = size / src_type.size();
- compressed_size = num_src_elements * ov::element::f16.size();
+ OPENVINO_ASSERT(num_src_elements * src_type.size() == size);
+ using T = fundamental_type_for<ov::element::Type_t::f16>;
+ compressed_size = num_src_elements * sizeof(T);
if (src_type == ov::element::f32) {
auto new_ptr = std::unique_ptr<char[]>(new char[compressed_size]);
auto dst_data = reinterpret_cast<ov::float16*>(new_ptr.get());