Skip to content

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

[core] Create single file multiple blobs storage (#34191)

Tomasz Jankowski5625ab2fb26e
diff --git a/src/common/util/include/openvino/util/ov_version.hpp b/src/common/util/include/openvino/util/ov_version.hpp
index 3d9828503e9..496314bb96f 100644
--- a/src/common/util/include/openvino/util/ov_version.hpp
+++ b/src/common/util/include/openvino/util/ov_version.hpp
@@ -57,6 +57,13 @@ struct Version {
explicit Version(std::string_view version_str) : Version(version_str.data()) {}
+ explicit constexpr Version(size_t major, size_t minor, size_t patch, size_t tweak = 0, size_t build = 0)
+ : major{major},
+ minor{minor},
+ patch{patch},
+ tweak{tweak},
+ build{build} {}
+
// Comparison operators
bool operator==(const Version& other) const {
return std::tie(major, minor, patch, tweak, build) ==
diff --git a/src/core/src/runtime/tensor.cpp b/src/core/src/runtime/tensor.cpp
index d2995d0b35c..65b2f5fe6f2 100644
--- a/src/core/src/runtime/tensor.cpp
+++ b/src/core/src/runtime/tensor.cpp
@@ -8,6 +8,7 @@
#include <numeric>
#include "openvino/core/except.hpp"
+#include "openvino/core/memory_util.hpp"
#include "openvino/core/node_output.hpp"
#include "openvino/core/shape.hpp"
#include "openvino/core/shape_util.hpp"
@@ -141,8 +142,14 @@ ov::Shape calc_static_shape_for_file(size_t file_size,
size_t offset) {
if (partial_shape.is_static()) {
auto static_shape = partial_shape.get_shape();
- OPENVINO_ASSERT((ov::shape_size(static_shape)) * element_type.bitwidth() + offset * 8 == file_size * 8,
- "Cannot fit file size into requested static PartialShape");
+ const auto memory_size = ov::util::get_memory_size_safe(element_type, static_shape);
+ OPENVINO_ASSERT(memory_size && *memory_size + offset <= file_size,
+ "Requested space exceeds file bounds: file size=",
+ file_size,
+ " offset=",
+ offset,
+ " requested size=",
+ memory_size ? std::to_string(*memory_size) : "uncountable");
return static_shape;
}
auto partial_shape_copy = partial_shape;
diff --git a/src/inference/dev_api/openvino/runtime/icache_manager.hpp b/src/inference/dev_api/openvino/runtime/icache_manager.hpp
new file mode 100644
index 00000000000..f3ddd433e42
--- /dev/null
+++ b/src/inference/dev_api/openvino/runtime/icache_manager.hpp
@@ -0,0 +1,115 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include <clocale>
+#include <functional>
+#include <iostream>
+#include <string>
+#include <variant>
+
+#include "openvino/core/weight_sharing_util.hpp"
+#include "openvino/runtime/tensor.hpp"
+
+namespace ov {
+
+/**
+ * @brief This class represents private interface for Cache Manager
+ */
+class ICacheManager {
+public:
+ /**
+ * @brief Default destructor
+ */
+ virtual ~ICacheManager() = default;
+
+ /**
+ * @brief Function passing created output stream
+ */
+ using StreamWriter = std::function<void(std::ostream&)>;
+ /**
+ * @brief Callback when OpenVINO intends to write model to cache
+ *
+ * Client needs to call create std::ostream object and call writer(ostream)
+ * Otherwise, model will not be cached
+ *
+ * @param id Id of cache (hash of the model)
+ * @param writer Lambda function to be called when stream is created
+ */
+ virtual void write_cache_entry(const std::string& id, StreamWriter writer) = 0;
+
+ /**
+ * @brief Variant type for compiled blob representation
+ */
+ using CompiledBlobVariant = std::variant<const ov::Tensor, std::reference_wrapper<std::istream>>;
+
+ /**
+ * @brief Function passing created input stream
+ */
+ using StreamReader = std::function<void(CompiledBlobVariant&)>;
+
+ /**
+ * @brief Callback when OpenVINO intends to read model from cache
+ *
+ * Client needs to call create std::istream object and call reader(istream)
+ * Otherwise, model will not be read from cache and will be loaded as usual
+ *
+ * @param id Id of cache (hash of the model)
+ * @param enable_mmap use mmap or ifstream to read model file
+ * @param reader Lambda function to be called when input stream is created
+ */
+ virtual void read_cache_entry(const std::string& id, bool enable_mmap, StreamReader reader) = 0;
+
+ /**
+ * @brief Callback when OpenVINO intends to remove cache entry
+ *
+ * Client needs to perform appropriate cleanup (e.g. delete a cache file)
+ *
+ * @param id Id of cache (hash of the model)
+ */
+ virtual void remove_cache_entry(const std::string& id) = 0;
+};
+
+/**
+ * @brief Interface to store and get shared context.
+ */
+class IContextStore {
+public:
+ /**
+ * @brief Writes context to the storage
+ * @param context The context to be stored
+ */
+ virtual void write_context(const ov::weight_sharing::Context&) = 0;
+
+ /**
+ * @brief Gets context from the storage
+ * @return The stored context
+ */
+ virtual ov::weight_sharing::Context get_context() const = 0;
+};
+
+/**
+ * @brief This class limits the locale env to a special value in sub-scope
+ */
+class ScopedLocale {
+public:
+ ScopedLocale(int category, std::string new_locale) : m_category(category) {
+ m_old_locale = setlocale(category, nullptr);
+ setlocale(m_category, new_locale.c_str());
+ }
+ ~ScopedLocale() {
+ setlocale(m_category, m_old_locale.c_str());
+ }
+
+ // Disable heap allocation - it's meant for local scope usage only.
+ static void* operator new(std::size_t) = delete;
+ static void* operator new[](std::size_t) = delete;
+
+private:
+ int m_category;
+ std::string m_old_locale;
+};
+
+} // namespace ov
diff --git a/src/inference/dev_api/openvino/runtime/single_file_storage.hpp b/src/inference/dev_api/openvino/runtime/single_file_storage.hpp
new file mode 100644
index 00000000000..70f0be86a74
--- /dev/null
+++ b/src/inference/dev_api/openvino/runtime/single_file_storage.hpp
@@ -0,0 +1,82 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include "openvino/core/weight_sharing_util.hpp"
+#include "openvino/runtime/icache_manager.hpp"
+#include "openvino/runtime/tlv_format.hpp"
+#include "openvino/util/ov_version.hpp"
+
+namespace ov::runtime {
+class SingleFileStorage final : public ICacheManager, public IContextStore {
+public:
+ /** @brief Current version of the single file storage format. */
+ static constexpr util::Version m_version{0, 1, 0};
+
+ enum class Tag : TLVTraits::TagType {
+ String = 0x02,
+ Blob = 0x03,
+ BlobMap = 0x04,
+ ConstantMeta = 0x10,
+ WeightSource = 0x11,
+ };
+
+ explicit SingleFileStorage(const std::filesystem::path& path);
+
+ /**
+ * @brief Write a cache entry to the storage.
+ * @param blob_id The identifier of the blob.
+ * @param writer The function to write the blob data.
+ */
+ void write_cache_entry(const std::string& blob_id, StreamWriter writer) override;
+
+ /**
+ * @brief Read a cache entry from the storage.
+ * @param blob_id The identifier of the blob.
+ * @param enable_mmap Whether to use memory mapping for reading the blob data.
+ * @param reader The function to read the blob data.
+ */
+ void read_cache_entry(const std::string& blob_id, bool mmap_enabled, StreamReader reader) override;
+
+ /**
+ * @brief Remove a cache entry from the storage.
+ * @note This function does nothing - the storage is append-only.
+ * @param blob_id The identifier of the blob to be removed.
+ */
+ void remove_cache_entry(const std::string& blob_id) override;
+
+ /**
+ * @brief Write the weight sharing context to the storage.
+ * @param context The weight sharing context to be stored.
+ */
+ void write_context(const weight_sharing::Context& context) override;
+
+ /**
+ * @brief Get the weight sharing context from the storage.
+ * @return The weight sharing context stored in the storage.
+ */
+ weight_sharing::Context get_context() const override;
+
+ using BlobIdType = uint64_t;
+ using DataIdType = uint64_t;
+ using PadSizeType = uint64_t;
+
+private:
+ std::filesystem::path m_file_path;
+
+ struct BlobInfo {
+ std::streampos offset;
+ std::streamoff size;
+ std::string model_name;
+ };
+ std::unordered_map<BlobIdType, BlobInfo> m_blob_index;
+ weight_sharing::Context m_shared_context;
+ bool build_content_index(std::ifstream& stream);
+
+ static BlobIdType convert_blob_id(const std::string& blob_id);
+ void write_blob_entry(std::ofstream& stream, BlobIdType blob_id, StreamWriter& writer);
+ bool has_blob_id(BlobIdType blob_id) const;
+};
+} // namespace ov::runtime
diff --git a/src/inference/dev_api/openvino/runtime/tlv_format.hpp b/src/inference/dev_api/openvino/runtime/tlv_format.hpp
new file mode 100644
index 00000000000..ccc9c6164c4
--- /dev/null
+++ b/src/inference/dev_api/openvino/runtime/tlv_format.hpp
@@ -0,0 +1,88 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include <cstdint>
+#include <functional>
+#include <iostream>
+#include <string>
+#include <unordered_map>
+#include <vector>
+
+namespace ov::runtime {
+/**
+ * @brief Utility for reading and writing records in TLV (Tag-Length-Value) scheme to/from streams. TLV scheme allows to
+ * store multiple records in a single stream with each record having a tag to identify the type of the record and length
+ * to identify the size of the record value. Such scheme is useful for storing multiple pieces of data in a single file
+ * or stream without requiring a predefined structure, as each record can be read independently based on its tag and
+ * length.
+ */
+struct TLVTraits {
+ /** @brief Type of the tag field in TLV record. */
+ using TagType = uint32_t;
+
+ /** @brief Type of the length field in TLV record. */
+ using LengthType = uint64_t;
+};
+
+/**
+ * @brief Write a TLV record to the stream.
+ * @param stream Output stream to write the record to.
+ * @param tag Tag of the record.
+ * @param size Size of the record value.
+ * @param data Pointer to the record value data.
+ */
+void write_tlv_record(std::ostream& stream, TLVTraits::TagType tag, TLVTraits::LengthType size, const char* data);
+
+/**
+ * @brief Type of callable for writing TLV record values based on their tag.
+ * The callable takes an output stream to write the record value to as a parameter.
+ */
+using TLVValueWriter = std::function<void(std::ostream&)>;
+
+/**
+ * @brief Write a TLV record to the stream using a value writer callable.
+ * @param stream Output stream to write the record to.
+ * @param tag Tag of the record.
+ * @param writer Callable that writes the record value to the stream.
+ */
+void write_tlv_record(std::ostream& stream, TLVTraits::TagType tag, const TLVValueWriter& writer);
+
+/**
+ * @brief Read a TLV record from the stream into a container.
+ * @param stream Input stream to read the record from. It should be positioned at the beginning of a record.
+ * @param tag Output parameter to store the tag of the read record.
+ * @param size Output parameter to store the size of the read record value.
+ * @param data Output parameter to store the read record value.
+ * @return True if the record was successfully read, false otherwise.
+ */
+bool read_tlv_record(std::istream& stream,
+ TLVTraits::TagType& tag,
+ TLVTraits::LengthType& size,
+ std::vector<char>& data);
+
+/**
+ * @brief Type of callable for reading TLV record values based on their tag.
+ * The callable takes an input stream positioned at the beginning of the record value and the size of the value as
+ * parameters.
+ */
+using TLVValueReader = std::function<bool(std::istream& stream, TLVTraits::LengthType size)>;
+
+/** @brief Map of tag to value reader callable. */
+using TLVValueScanner = std::unordered_map<TLVTraits::TagType, TLVValueReader>;
+
+/**
+ * @brief Scan records in the stream and call corresponding reader from scanners for each record with matching tag.
+ * If tag doesn't exist in scanners, record will be skipped.
+ * @note This function doesn't support nested TLV records and is expected to be used for top-level records only.
+ * @param stream Input stream to read records from. It should be positioned at the beginning of a record.
+ * @param scanners Map of tag to value reader callable. Value reader will be called with the stream positioned at
+ * the beginning of record value and size of the value as parameters. Value reader is expected to read exactly size
+ * bytes from the stream. Failure to do so may result in incorrect parsing of subsequent records.
+ * @return False if an error occurred during scanning, true otherwise.
+ */
+bool scan_tlv_records(std::istream& stream, const TLVValueScanner& scanners);
+
+} // namespace ov::runtime
diff --git a/src/inference/src/cache_manager.hpp b/src/inference/src/cache_manager.hpp
index a54fdae9d84..984701815b7 100644
--- a/src/inference/src/cache_manager.hpp
+++ b/src/inference/src/cache_manager.hpp
@@ -16,6 +16,7 @@
#include <string>
#include <variant>
+#include "openvino/runtime/icache_manager.hpp"
#include "openvino/runtime/shared_buffer.hpp"
#include "openvino/runtime/tensor.hpp"
#include "openvino/util/file_util.hpp"
@@ -23,84 +24,6 @@
namespace ov {
-/**
- * @brief This class limits the locale env to a special value in sub-scope
- *
- */
-class ScopedLocale {
-public:
- ScopedLocale(int category, std::string newLocale) : m_category(category) {
- m_oldLocale = setlocale(category, nullptr);
- setlocale(m_category, newLocale.c_str());
- }
- ~ScopedLocale() {
- setlocale(m_category, m_oldLocale.c_str());
- }
-
-private:
- int m_category;
- std::string m_oldLocale;
-};
-
-/**
- * @brief This class represents private interface for Cache Manager
- *
- */
-class ICacheManager {
-public:
- /**
- * @brief Default destructor
- */
- virtual ~ICacheManager() = default;
-
- /**
- * @brief Function passing created output stream
- *
- */
- using StreamWriter = std::function<void(std::ostream&)>;
- /**
- * @brief Callback when OpenVINO intends to write model to cache
- *
- * Client needs to call create std::ostream object and call writer(ostream)
- * Otherwise, model will not be cached
- *
- * @param id Id of cache (hash of the model)
- * @param writer Lambda function to be called when stream is created
- */
- virtual void write_cache_entry(const std::string& id, StreamWriter writer) = 0;
-
- /**
- * @brief Variant type for compiled blob representation
- */
- using CompiledBlobVariant = std::variant<const ov::Tensor, std::reference_wrapper<std::istream>>;
-
- /**
- * @brief Function passing created input stream
- */
- using StreamReader = std::function<void(CompiledBlobVariant&)>;
-
- /**
- * @brief Callback when OpenVINO intends to read model from cache
- *
- * Client needs to call create std::istream object and call reader(istream)
- * Otherwise, model will not be read from cache and will be loaded as usual
- *
- * @param id Id of cache (hash of the model)
- * @param enable_mmap use mmap or ifstream to read model file
- * @param reader Lambda function to be called when input stream is created
- */
- virtual void read_cache_entry(const std::string& id, bool enable_mmap, StreamReader reader) = 0;
-
- /**
- * @brief Callback when OpenVINO intends to remove cache entry
- *
- * Client needs to perform appropriate cleanup (e.g. delete a cache file)
- *
- * @param id Id of cache (hash of the model)
- */
- virtual void remove_cache_entry(const std::string& id) = 0;
-};
-
/**
* @brief File storage-based Implementation of ICacheManager
*
diff --git a/src/inference/src/dev/tlv_format.cpp b/src/inference/src/dev/tlv_format.cpp
new file mode 100644
index 00000000000..96a449ff8cc
--- /dev/null
+++ b/src/inference/src/dev/tlv_format.cpp
@@ -0,0 +1,98 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "openvino/runtime/tlv_format.hpp"
+
+#include <type_traits>
+#include <utility>
+
+namespace ov::runtime {
+
+void write_tlv_record(std::ostream& stream, TLVTraits::TagType tag, TLVTraits::LengthType size, const char* data) {
+ stream.write(reinterpret_cast<const char*>(&tag), sizeof(tag));
+ stream.write(reinterpret_cast<const char*>(&size), sizeof(size));
+ if (size && data) {
+ stream.write(reinterpret_cast<const char*>(data), size);
+ }
+}
+
+void write_tlv_record(std::ostream& stream, TLVTraits::TagType tag, const TLVValueWriter& writer) {
+ stream.write(reinterpret_cast<const char*>(&tag), sizeof(tag));
+ const auto size_pos = stream.tellp();
+ TLVTraits::LengthType size = 0;
+ stream.write(reinterpret_cast<const char*>(&size), sizeof(size));
+ if (writer) {
+ const auto value_pos = stream.tellp();
+ writer(stream);
+ const auto end_pos = stream.tellp();
+ size = end_pos - value_pos;
+ stream.seekp(size_pos);
+ stream.write(reinterpret_cast<const char*>(&size), sizeof(size));
+ stream.seekp(end_pos);
+ }
+}
+
+namespace {
+template <typename Container>
+static bool read_record(std::istream& stream, TLVTraits::TagType& tag, TLVTraits::LengthType& size, Container& data) {
+ stream.read(reinterpret_cast<char*>(&tag), sizeof(tag));
+ if (!stream.good()) {
+ return false;
+ }
+ stream.read(reinterpret_cast<char*>(&size), sizeof(size));
+ if (!stream.good()) {
+ return false;
+ }
+ if (size == 0) {
+ data.clear();
+ return true;
+ }
+ data.resize(size);
+ stream.read(reinterpret_cast<char*>(data.data()), size);
+ return stream.good();
+}
+} // namespace
+
+bool read_tlv_record(std::istream& stream,
+ TLVTraits::TagType& tag,
+ TLVTraits::LengthType& size,
+ std::vector<char>& data) {
+ return read_record(stream, tag, size, data);
+}
+
+bool scan_tlv_records(std::istream& stream, const TLVValueScanner& scanners) {
+ const auto beginning_pos = stream.tellg();
+ stream.seekg(0, std::ios::end);
+ const auto stream_end = stream.tellg();
+ stream.seekg(beginning_pos);
+
+ while (stream.good() && stream.tellg() < stream_end) {
+ TLVTraits::TagType tag{};
+ TLVTraits::LengthType size{};
+ stream.read(reinterpret_cast<char*>(&tag), sizeof(tag));
+ if (!stream.good()) {
+ return false;
+ }
+ stream.read(reinterpret_cast<char*>(&size), sizeof(size));
+ if (!stream.good()) {
+ return false;
+ }
+
+ if (auto scanner_it = scanners.find(tag); scanner_it != scanners.end()) {
+ if (!scanner_it->second(stream, size)) {
+ return false;
+ }
+ } else {
+ if (stream_end - stream.tellg() < static_cast<std::streamoff>(size) || !stream.good()) {
+ return false;
+ }
+ stream.seekg(size, std::ios::cur);
+ if (!stream.good()) {
+ return false;
+ }
+ }
+ }
+ return true;
+}
+} // namespace ov::runtime
diff --git a/src/inference/src/single_file_storage.cpp b/src/inference/src/single_file_storage.cpp
new file mode 100644
index 00000000000..3ce99aa8c7d
--- /dev/null
+++ b/src/inference/src/single_file_storage.cpp
@@ -0,0 +1,344 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "openvino/runtime/single_file_storage.hpp"
+
+#ifdef _WIN32
+# include <windows.h>
+#else
+# include <unistd.h>
+#endif
+
+#include "openvino/runtime/aligned_buffer.hpp"
+#include "openvino/util/file_util.hpp"
+#include "openvino/util/mmap_object.hpp"
+#include "openvino/util/variant_visitor.hpp"
+
+namespace ov::runtime {
+
+namespace {
+void write_version(std::ostream& stream, const util::Version& version) {
+ const uint16_t major = static_cast<uint16_t>(version.major);
+ const uint16_t minor = static_cast<uint16_t>(version.minor);
+ const uint16_t patch = static_cast<uint16_t>(version.patch);
+ stream.write(reinterpret_cast<const char*>(&major), sizeof(major));
+ stream.write(reinterpret_cast<const char*>(&minor), sizeof(minor));
+ stream.write(reinterpret_cast<const char*>(&patch), sizeof(patch));
+}
+
+void read_version(std::istream& stream, util::Version& version) {
+ uint16_t major = 0, minor = 0, patch = 0;
+ stream.read(reinterpret_cast<char*>(&major), sizeof(major));
+ stream.read(reinterpret_cast<char*>(&minor), sizeof(minor));
+ stream.read(reinterpret_cast<char*>(&patch), sizeof(patch));
+ if (stream.good()) {
+ version.major = major;
+ version.minor = minor;
+ version.patch = patch;
+ }
+}
+
+void validate_version(const util::Version& version) {
+ util::is_version_compatible(SingleFileStorage::m_version, version);
+}
+
+void write_tlv_string(std::ostream& stream, const std::string& str) {
+ write_tlv_record(stream, static_cast<TLVTraits::TagType>(SingleFileStorage::Tag::String), str.size(), str.data());
+}
+
+static const uint64_t alignment = []() {
+#ifdef _WIN32
+ SYSTEM_INFO sysInfo;
+ GetSystemInfo(&sysInfo);
+ return static_cast<uint64_t>(sysInfo.dwPageSize);
+#else
+ return static_cast<uint64_t>(sysconf(_SC_PAGE_SIZE));
+#endif
+}();
+
+bool read_tlv_string(std::istream& stream, std::string& str) {
+ TLVTraits::TagType tag;
+ TLVTraits::LengthType size;
+ std::vector<char> buffer;
+ const auto read = read_tlv_record(stream, tag, size, buffer);
+ if (read) {
+ OPENVINO_ASSERT(SingleFileStorage::Tag{tag} == SingleFileStorage::Tag::String);
+ str = std::string{buffer.begin(), buffer.end()};
+ }
+ return read;
+}
+
+void write_padding(std::ostream& stream, uint64_t alignment) {
+ const uint64_t padding_pos = static_cast<uint64_t>(stream.tellp()) + sizeof(SingleFileStorage::PadSizeType);
+ auto aligned_pos = padding_pos + alignment - 1;
+ aligned_pos -= aligned_pos % alignment;
+
+ SingleFileStorage::PadSizeType pad_size = aligned_pos - padding_pos;
+ stream.write(reinterpret_cast<const char*>(&pad_size), sizeof(pad_size));
+ if (pad_size > 0) {
+ std::vector<char> padding(pad_size, 0);
+ stream.write(padding.data(), padding.size());
+ }
+}
+} // namespace
+
+SingleFileStorage::SingleFileStorage(const std::filesystem::path& path) : m_file_path{path} {
+ util::create_directory_recursive(m_file_path.parent_path());
+ if (!util::file_exists(m_file_path)) {
+ std::ofstream stream(m_file_path, std::ios::binary);
+ write_version(stream, m_version);
+ } else {
+ std::ifstream stream(m_file_path, std::ios::binary);
+ util::Version file_version{0, 0, 0};
+ read_version(stream, file_version);
+ validate_version(file_version);
+
+ if (!build_content_index(stream)) {
+ m_blob_index.clear();
+ m_shared_context.m_cache_sources.clear();
+ m_shared_context.m_weight_registry.clear();
+ }
+ }
+}
+
+bool SingleFileStorage::build_content_index(std::ifstream& stream) {
+ const auto current_pos = stream.tellg();
+ const auto end_pos = stream.seekg(0, std::ios::end).tellg();
+ stream.seekg(current_pos);
+ const auto blob_reader = [this, end_pos](std::istream& s, TLVTraits::LengthType size) {
+ if (size == 0) {
+ return true;
+ }
+ BlobIdType id;
+ PadSizeType padding_size;
+ s.read(reinterpret_cast<char*>(&id), sizeof(id));
+ s.read(reinterpret_cast<char*>(&padding_size), sizeof(padding_size));
+ if (s.tellg() + static_cast<std::streamoff>(padding_size) > end_pos) {
+ return false;
+ }
+ s.seekg(padding_size, std::ios::cur);
+ if (!s.good()) {
+ return false;
+ }
+ const auto blob_data_pos = s.tellg();
+ const auto blob_data_size =
+ static_cast<std::streamoff>(size - sizeof(id) - sizeof(padding_size) - padding_size);
+ m_blob_index[id].offset = blob_data_pos;
+ m_blob_index[id].size = blob_data_size;
+ if (blob_data_pos + blob_data_size > end_pos) {
+ return false;
+ }
+ s.seekg(blob_data_size, std::ios::cur);
+ return s.good();
+ };
+ const auto blob_map_reader = [this](std::istream& s, TLVTraits::LengthType size) {
+ if (size == 0) {
+ return true;
+ }
+ BlobIdType id;
+ s.read(reinterpret_cast<char*>(&id), sizeof(id));
+ if (!s.good()) {
+ return false;
+ }
+ if (std::string model_name; read_tlv_string(s, model_name)) {
+ m_blob_index[id].model_name = model_name;
+ }
+ return s.good();
+ };
+ const auto constant_meta_reader = [this, end_pos](std::istream& s, TLVTraits::LengthType size) {
+ if (size == 0) {
+ return true;
+ }
+ if (s.tellg() + static_cast<std::streamoff>(size) > end_pos) {
+ return false;
+ }
+ uint64_t source_id;
+ s.read(reinterpret_cast<char*>(&source_id), sizeof(source_id));
+ auto left_size = size - sizeof(source_id);
+
+ while (s.good() && left_size > 0) {
+ uint64_t const_id, const_offset, const_size;
+ uint8_t const_type;
+ constexpr auto const_meta_size =
+ sizeof(const_id) + sizeof(const_offset) + sizeof(const_size) + sizeof(const_type);
+ if (left_size < const_meta_size) {
+ return false;
+ }
+ left_size -= const_meta_size;
+ s.read(reinterpret_cast<char*>(&const_id), sizeof(const_id));
+ s.read(reinterpret_cast<char*>(&const_offset), sizeof(const_offset));
+ s.read(reinterpret_cast<char*>(&const_size), sizeof(const_size));
+ s.read(reinterpret_cast<char*>(&const_type), sizeof(const_type));
+ if (!s.good()) {
+ return false;
+ }
+ m_shared_context.m_weight_registry[source_id][const_id] = {static_cast<size_t>(const_offset),
+ static_cast<size_t>(const_size),
+ element::Type_t{const_type}};
+ }
+ return s.good();
+ };
+ const auto constant_source_reader = [this, end_pos](std::istream& s, TLVTraits::LengthType size) {
+ if (size == 0) {
+ return true;
+ }
+ DataIdType device_id, source_id;
+ PadSizeType padding_size;
+ if (size < sizeof(device_id) + sizeof(source_id) + sizeof(padding_size) ||
+ s.tellg() + static_cast<std::streamoff>(size) > end_pos) {
+ return false;
+ }
+ s.read(reinterpret_cast<char*>(&device_id), sizeof(device_id));
+ s.read(reinterpret_cast<char*>(&source_id), sizeof(source_id));
+ s.read(reinterpret_cast<char*>(&padding_size), sizeof(padding_size));
+ if (s.tellg() + static_cast<std::streamoff>(padding_size) > end_pos) {
+ return false;
+ }
+ s.seekg(padding_size, std::ios::cur);
+ if (!s.good()) {
+ return false;
+ }
+ const auto weight_size = size - sizeof(device_id) - sizeof(source_id) - sizeof(padding_size) - padding_size;
+ m_shared_context.m_cache_sources[source_id] = {};
+ if (s.tellg() + static_cast<std::streamoff>(weight_size) > end_pos) {
+ return false;
+ }
+ s.seekg(weight_size, std::ios::cur);
+ return s.good();
+ };
+ const TLVValueScanner scanners = {
+ {static_cast<TLVTraits::TagType>(Tag::Blob), blob_reader},
+ {static_cast<TLVTraits::TagType>(Tag::BlobMap), blob_map_reader},
+ {static_cast<TLVTraits::TagType>(Tag::ConstantMeta), constant_meta_reader},
+ {static_cast<TLVTraits::TagType>(Tag::WeightSource), constant_source_reader},
+ };
+ return scan_tlv_records(stream, scanners);
+}
+
+SingleFileStorage::BlobIdType SingleFileStorage::convert_blob_id(const std::string& blob_id) {
+ return static_cast<BlobIdType>(std::stoull(blob_id.c_str()));
+}
+
+bool SingleFileStorage::has_blob_id(BlobIdType blob_id) const {
+ return m_blob_index.find(blob_id) != m_blob_index.end();
+}
+
+void SingleFileStorage::write_blob_entry(std::ofstream& stream, BlobIdType blob_id, StreamWriter& writer) {
+ OPENVINO_ASSERT(!has_blob_id(blob_id), "Blob with id ", blob_id, " already exists in cache.");
+
+ std::streampos blob_pos;
+ std::streamoff blob_size;
+
+ const auto blob_writer = [&](std::ostream& s) {
+ s.write(reinterpret_cast<const char*>(&blob_id), sizeof(blob_id));
+ write_padding(s, alignment);
+ blob_pos = s.tellp();
+ writer(s);
+ blob_size = s.tellp() - blob_pos;
+ };
+ write_tlv_record(stream, static_cast<TLVTraits::TagType>(Tag::Blob), blob_writer);
+
+ std::string model_name; // Intentionally empty
+
+ const auto blob_map_writer = [&](std::ostream& s) {
+ s.write(reinterpret_cast<const char*>(&blob_id), sizeof(blob_id));
+ write_tlv_string(s, model_name);
+ };
+ write_tlv_record(stream, static_cast<TLVTraits::TagType>(Tag::BlobMap), blob_map_writer);
+
+ m_blob_index[blob_id] = {blob_pos, blob_size, model_name};
+}
+
+void SingleFileStorage::write_cache_entry(const std::string& blob_id, StreamWriter writer) {
+ ScopedLocale plocal_C(LC_ALL, "C");
+ std::ofstream stream(m_file_path, std::ios::binary | std::ios::in | std::ios::ate);
+ write_blob_entry(stream, convert_blob_id(blob_id), writer);
+}
+
+void SingleFileStorage::read_cache_entry(const std::string& blob_id, bool enable_mmap, StreamReader reader) {
+ ScopedLocale plocal_C(LC_ALL, "C");
+
+ const auto cid = convert_blob_id(blob_id);
+
+ if (std::filesystem::exists(m_file_path) && has_blob_id(cid)) {
+ const auto& [blob_pos, blob_size, model_name] = m_blob_index[cid];
+ if (enable_mmap) {
+ // CVS-181859 Extend memory mapping helpers to suport partial file mapping
+ CompiledBlobVariant compiled_blob{std::in_place_index<0>,
+ ov::read_tensor_data(m_file_path,
+ element::u8,
+ {static_cast<PartialShape::value_type>(blob_size)},
+ blob_pos)};
+ reader(compiled_blob);
+ } else {
+ std::ifstream stream(m_file_path, std::ios::binary);
+ stream.seekg(blob_pos);
+ CompiledBlobVariant compiled_blob{std::in_place_index<1>, std::ref(stream)};
+ reader(compiled_blob);
+ }
+ }
+}
+
+void SingleFileStorage::remove_cache_entry(const std::string& id) {}
+
+weight_sharing::Context SingleFileStorage::get_context() const {
+ return m_shared_context;
+}
+
+void SingleFileStorage::write_context(const weight_sharing::Context& context) {
+ ScopedLocale plocal_C(LC_ALL, "C");
+ std::ofstream stream(m_file_path, std::ios::binary | std::ios::in | std::ios::ate);
+
+ weight_sharing::WeightRegistry delta_weight_registry;
+ for (const auto& [source_id, const_meta_map] : context.m_weight_registry) {
+ for (const auto& [const_id, const_meta] : const_meta_map) {
+ if (m_shared_context.m_weight_registry.count(source_id) == 0 ||
+ m_shared_context.m_weight_registry[source_id].count(const_id) == 0) {
+ delta_weight_registry[source_id][const_id] = const_meta;
+ }
+ }
+ }
+ for (const auto& meta_map : delta_weight_registry) {
+ const auto const_meta_writer = [&](std::ostream& s) {
+ const auto& [source_id, const_meta] = meta_map;
+ s.write(reinterpret_cast<const char*>(&source_id), sizeof(source_id));
+ for (const auto& [id, props] : const_meta) {
+ const auto const_id = static_cast<DataIdType>(id);
+ const auto const_offset = static_cast<uint64_t>(props.m_offset);
+ const auto const_size = static_cast<uint64_t>(props.m_size);
+ const auto const_type = static_cast<uint8_t>(element::Type_t{props.m_type});
+ s.write(reinterpret_cast<const char*>(&const_id), sizeof(const_id));
+ s.write(reinterpret_cast<const char*>(&const_offset), sizeof(const_offset));
+ s.write(reinterpret_cast<const char*>(&const_size), sizeof(const_size));
+ s.write(reinterpret_cast<const char*>(&const_type), sizeof(const_type));
+
+ m_shared_context.m_weight_registry[source_id][const_id] = props;
+ }
+ };
+ write_tlv_record(stream, static_cast<TLVTraits::TagType>(Tag::ConstantMeta), const_meta_writer);
+ }
+
+ weight_sharing::WeightSourceRegistry delta_cache_sources;
+ for (const auto& [source_id, weight_buffer] : context.m_cache_sources) {
+ if (m_shared_context.m_cache_sources.count(source_id) == 0) {
+ delta_cache_sources[source_id] = weight_buffer;
+ }
+ }
+ for (const auto& cache_registry : delta_cache_sources) {
+ const auto weight_source_writer = [&](std::ostream& s) {
+ const auto& [source_id, weight_buffer] = cache_registry;
+ if (auto buf = weight_buffer.m_weights.lock()) {
+ const auto device_id = static_cast<uint64_t>(std::strtoul(weight_buffer.m_device.c_str(), nullptr, 10));
+ s.write(reinterpret_cast<const char*>(&device_id), sizeof(device_id));
+ s.write(reinterpret_cast<const char*>(&source_id), sizeof(source_id));
+ write_padding(s, alignment);
+ s.write(reinterpret_cast<const char*>(buf->get_ptr()), buf->size());
+ }
+
+ m_shared_context.m_cache_sources[source_id] = weight_buffer;
+ };
+ write_tlv_record(stream, static_cast<TLVTraits::TagType>(Tag::WeightSource), weight_source_writer);
+ }
+}
+}; // namespace ov::runtime
diff --git a/src/inference/tests/unit/single_file_storage.cpp b/src/inference/tests/unit/single_file_storage.cpp
new file mode 100644
index 00000000000..cef9d7d076a
--- /dev/null
+++ b/src/inference/tests/unit/single_file_storage.cpp
@@ -0,0 +1,324 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "openvino/runtime/single_file_storage.hpp"
+
+#include <gtest/gtest.h>
+
+#ifdef _WIN32
+# include <windows.h>
+#else
+# include <unistd.h>
+#endif
+
+#include "common_test_utils/common_utils.hpp"
+#include "common_test_utils/file_utils.hpp"
+#include "common_test_utils/test_assertions.hpp"
+#include "openvino/runtime/aligned_buffer.hpp"
+
+namespace ov::test {
+
+using runtime::SingleFileStorage;
+
+namespace {
+constexpr uint64_t version_size() {
+ return 3 * sizeof(uint16_t); // major, minor, patch
+}
+
+std::streamoff get_system_page_size() {
+#ifdef _WIN32
+ SYSTEM_INFO sysInfo;
+ GetSystemInfo(&sysInfo);
+ return static_cast<std::streamoff>(sysInfo.dwPageSize);
+#else
+ return static_cast<std::streamoff>(sysconf(_SC_PAGE_SIZE));
+#endif
+}
+} // namespace
+
+class SingleFileStorageTest : public ::testing::Test {
+protected:
+ std::filesystem::path m_file_path;
+ std::unique_ptr<SingleFileStorage> m_storage;
+
+ void SetUp() override {
+ m_file_path = ov::test::utils::generateTestFilePrefix() + ".bin";
+ m_storage = std::make_unique<SingleFileStorage>(m_file_path);
+ ASSERT_TRUE(std::filesystem::exists(m_file_path));
+ }
+
+ void TearDown() override {
+ m_storage.reset();
+ std::filesystem::remove(m_file_path);
+ }
+};
+
+TEST_F(SingleFileStorageTest, FileHeader) {
+ m_storage.reset();
+ std::ifstream stream(m_file_path, std::ios::binary);
+
+ std::vector<uint16_t> header_data(3);
+ stream.read(reinterpret_cast<char*>(header_data.data()), 6);
+ const auto last_pos = stream.tellg();
+ ASSERT_NE(last_pos, std::streampos(-1));
+ const auto end_pos = stream.seekg(0, std::ios::end).tellg();
+ EXPECT_EQ(last_pos, end_pos); // No more data after header in just created file
+
+ util::Version read_version{header_data[0], header_data[1], header_data[2]};
+ EXPECT_EQ(read_version, SingleFileStorage::m_version);
+}
+
+TEST_F(SingleFileStorageTest, WriteReadCacheEntry) {
+ const std::vector<std::pair<std::string, std::vector<uint8_t>>> test_blobs{
+ {"12", std::vector<uint8_t>(124, 0xAB)},
+ {"0345", std::vector<uint8_t>(481, 0xCD)},
+ {"006789", std::vector<uint8_t>(4967, 0xEF)},
+ {std::to_string(UINT64_MAX), std::vector<uint8_t>(1, 0)},
+ };
+
+ for (const auto& test_blob : test_blobs) {
+ const auto& blob_id = test_blob.first;
+ const auto& blob_data = test_blob.second;
+ m_storage->write_cache_entry(blob_id, [&](std::ostream& stream) {
+ stream.write(reinterpret_cast<const char*>(blob_data.data()), blob_data.size());
+ });
+ }
+
+ const auto blob_read_test = [&](SingleFileStorage& storage) {
+ size_t read_count = 0;
+ for (const auto& test_blob : test_blobs) {
+ const auto& blob_id = test_blob.first;
+ const auto& blob_data = test_blob.second;
+ storage.read_cache_entry(blob_id, false, [&](const ICacheManager::CompiledBlobVariant& compiled_blob) {
+ ASSERT_TRUE(std::holds_alternative<std::reference_wrapper<std::istream>>(compiled_blob));
+ ++read_count;
+ auto& stream = std::get<std::reference_wrapper<std::istream>>(compiled_blob).get();
+ std::vector<uint8_t> read_data(blob_data.size());
+ stream.read(reinterpret_cast<char*>(read_data.data()), read_data.size());
+ EXPECT_EQ(blob_data, read_data);
+ });
+
+ storage.read_cache_entry(blob_id, true, [&](const ICacheManager::CompiledBlobVariant& compiled_blob) {
+ ASSERT_TRUE(std::holds_alternative<const ov::Tensor>(compiled_blob));
+ ++read_count;
+ // CVS-181859 Check support for multimap memory mapping
+ auto& tensor = std::get<const ov::Tensor>(compiled_blob);
+ ASSERT_EQ(tensor.get_byte_size(), blob_data.size());
+ std::vector<uint8_t> read_data(blob_data.size());
+ std::memcpy(read_data.data(), tensor.data(), tensor.get_byte_size());
+ ASSERT_EQ(blob_data, read_data);
+ });
+ }
+ EXPECT_EQ(read_count, 2 * test_blobs.size());
+ };
+
+ blob_read_test(*m_storage);
+ m_storage.reset();
+ SingleFileStorage reopened_storage(m_file_path);
+ blob_read_test(reopened_storage);
+}
+
+TEST_F(SingleFileStorageTest, BlobAlignment) {
+ const std::unordered_map<uint64_t, std::vector<uint8_t>> test_blobs{{1, std::vector<uint8_t>(4099, 0xAB)},
+ {2, std::vector<uint8_t>(400, 0xCD)},
+ {3, std::vector<uint8_t>(5, 0xEF)}};
+ for (const auto& test_blob : test_blobs) {
+ const auto& blob_id = test_blob.first;
+ const auto& blob_data = test_blob.second;
+ m_storage->write_cache_entry(std::to_string(blob_id), [&](std::ostream& stream) {
+ stream.write(reinterpret_cast<const char*>(blob_data.data()), blob_data.size());
+ });
+ }
+
+ std::ifstream stream(m_file_path, std::ios::binary | std::ios::ate);
+ const auto stream_end = stream.tellg();
+ stream.seekg(version_size(), std::ios::beg);
+
+ const auto alignment = get_system_page_size();
+
+ while (stream.good() && stream.tellg() < stream_end) {
+ SingleFileStorage::Tag tag;
+ runtime::TLVTraits::LengthType length;
+ stream.read(reinterpret_cast<char*>(&tag), sizeof(tag));
+ ASSERT_TRUE(stream.good());
+ stream.read(reinterpret_cast<char*>(&length), sizeof(length));
+ ASSERT_TRUE(stream.good());
+ const auto blob_id_pos = stream.tellg();
+ if (tag == SingleFileStorage::Tag::Blob) {
+ SingleFileStorage::BlobIdType id;
+ stream.read(reinterpret_cast<char*>(&id), sizeof(id));
+ ASSERT_TRUE(stream.good());
+ SingleFileStorage::PadSizeType padding_size;
+ stream.read(reinterpret_cast<char*>(&padding_size), sizeof(padding_size));
+ ASSERT_TRUE(stream.good());
+
+ stream.seekg(padding_size, std::ios::cur);
+ const auto blob_data_pos = stream.tellg();
+ EXPECT_EQ(blob_data_pos % alignment, 0) << "Blob with id " << id << " is not properly aligned";
+
+ const auto expected_pos = blob_id_pos + static_cast<std::streamoff>(length);
+ stream.seekg(test_blobs.at(id).size(), std::ios::cur);
+ ASSERT_EQ(expected_pos, stream.tellg()) << "Blob with id " << id << " has incorrect record size";
+ } else {
+ stream.seekg(length, std::ios::cur);
+ }
+ }
+}
+
+TEST_F(SingleFileStorageTest, AppendOnlyCacheEntry) {
+ const auto blob_id = std::string{"123"};
+ m_storage->write_cache_entry(blob_id, [&](std::ostream& s) {
+ // Although pointless it shall be harmless to write nothing
+ });
+ EXPECT_NO_THROW(m_storage->remove_cache_entry(blob_id)); // removal does nothing => expect legit read
+ bool read_called = false;
+ EXPECT_NO_THROW(m_storage->read_cache_entry(blob_id, false, [&](const ICacheManager::CompiledBlobVariant&) {
+ read_called = true;
+ }));
+ EXPECT_TRUE(read_called);
+
+ OV_EXPECT_THROW_HAS_SUBSTRING(m_storage->write_cache_entry(blob_id, [&](std::ostream&) {}),
+ ov::AssertFailure,
+ blob_id + " already exists in cache");
+ m_storage.reset();
+
+ SingleFileStorage reopened_storage(m_file_path);
+ OV_EXPECT_THROW_HAS_SUBSTRING(reopened_storage.write_cache_entry(blob_id, [&](std::ostream&) {}),
+ ov::AssertFailure,
+ blob_id + " already exists in cache");
+
+ EXPECT_NO_THROW(reopened_storage.read_cache_entry("987", false, [](const ICacheManager::CompiledBlobVariant&) {
+ throw "Unexpected read for not stored blob id";
+ }));
+ EXPECT_NO_THROW(reopened_storage.remove_cache_entry("987")) << "Removal of non-existing blob id should be no-op";
+
+ read_called = false;
+ EXPECT_NO_THROW(reopened_storage.read_cache_entry(blob_id, false, [&](const ICacheManager::CompiledBlobVariant&) {
+ read_called = true;
+ }));
+ EXPECT_TRUE(read_called);
+}
+
+TEST_F(SingleFileStorageTest, ContextMetaWriteRead) {
+ weight_sharing::Context test_context;
+ test_context.m_weight_registry[1][11] = {100, 200, element::Type_t::f32};
+ test_context.m_weight_registry[1][12] = {300, 400, element::Type_t::i8};
+ test_context.m_weight_registry[2][21] = {500, 600, element::Type_t::u8};
+ m_storage->write_context(test_context);
+
+ const auto meta_read_test = [&](SingleFileStorage& storage) {
+ auto got_context = storage.get_context();
+ EXPECT_EQ(got_context.m_weight_registry.size(), 2);
+
+ for (const auto& [source_id, const_meta] : test_context.m_weight_registry) {
+ auto& got_meta_data = got_context.m_weight_registry;
+ ASSERT_EQ(got_meta_data.count(source_id), 1);
+ EXPECT_EQ(got_context.m_weight_registry[source_id].size(),
+ test_context.m_weight_registry[source_id].size());
+ for (const auto& [const_id, expected_props] : const_meta) {
+ ASSERT_EQ(got_meta_data[source_id].count(const_id), 1);
+
+ auto& got_props = got_meta_data[source_id][const_id];
+ EXPECT_EQ(got_props.m_offset, expected_props.m_offset);
+ EXPECT_EQ(got_props.m_size, expected_props.m_size);
+ EXPECT_EQ(got_props.m_type, expected_props.m_type);
+ }
+ }
+ };
+
+ meta_read_test(*m_storage);
+ m_storage.reset();
+
+ SingleFileStorage reopened_storage(m_file_path);
+ meta_read_test(reopened_storage);
+}
+
+TEST_F(SingleFileStorageTest, ContextMetaAppendDelta) {
+ weight_sharing::Context test_context;
+ test_context.m_weight_registry[1][11] = {100, 200, element::Type_t::f32};
+ m_storage->write_context(test_context);
+
+ test_context.m_weight_registry[1][12] = {300, 400, element::Type_t::i8};
+ test_context.m_weight_registry[2][21] = {500, 600, element::Type_t::u8};
+ m_storage->write_context(test_context);
+
+ auto got_context = m_storage->get_context();
+ EXPECT_EQ(got_context.m_weight_registry.size(), 2);
+ EXPECT_EQ(got_context.m_weight_registry[1].size(), 2);
+ EXPECT_EQ(got_context.m_weight_registry[2].size(), 1);
+ m_storage.reset();
+ const auto file_size_after_first_write = test::utils::fileSize(m_file_path.string());
+
+ SingleFileStorage{m_file_path}.write_context(test_context);
+ const auto file_size_after_second_write = test::utils::fileSize(m_file_path.string());
+ EXPECT_EQ(file_size_after_second_write, file_size_after_first_write)
+ << "Rewriting the same context should not increase file size";
+}
+
+TEST_F(SingleFileStorageTest, ContextWeightSourceWrite) {
+ weight_sharing::Context test_context;
+ const auto buffer = std::make_shared<ov::AlignedBuffer>(1024);
+ for (size_t i = 0; i < buffer->size(); i += 2) {
+ buffer->get_ptr<uint8_t>()[i] = 0xAB;
+ buffer->get_ptr<uint8_t>()[i + 1] = 0xCD;
+ }
+ test_context.m_cache_sources[1].m_weights = std::weak_ptr<ov::AlignedBuffer>{buffer};
+ m_storage->write_context(test_context);
+
+ std::ifstream stream(m_file_path, std::ios::binary | std::ios::ate);
+ const auto stream_end = stream.tellg();
+ stream.seekg(version_size(), std::ios::beg);
+
+ const auto alignment = get_system_page_size();
+
+ while (stream.good() && stream.tellg() < stream_end) {
+ SingleFileStorage::Tag tag;
+ runtime::TLVTraits::LengthType length;
+ stream.read(reinterpret_cast<char*>(&tag), sizeof(tag));
+ ASSERT_TRUE(stream.good());
+ stream.read(reinterpret_cast<char*>(&length), sizeof(length));
+ ASSERT_TRUE(stream.good());
+ if (tag == SingleFileStorage::Tag::WeightSource) {
+ SingleFileStorage::DataIdType device_id, source_id;
+ SingleFileStorage::PadSizeType padding_size;
+ stream.read(reinterpret_cast<char*>(&device_id), sizeof(device_id));
+ stream.read(reinterpret_cast<char*>(&source_id), sizeof(source_id));
+ stream.read(reinterpret_cast<char*>(&padding_size), sizeof(padding_size));
+ ASSERT_TRUE(stream.good());
+
+ stream.seekg(padding_size, std::ios::cur);
+ const auto weight_pos = stream.tellg();
+ ASSERT_EQ(weight_pos % alignment, 0);
+ const auto weight_size =
+ length - sizeof(device_id) - sizeof(source_id) - sizeof(padding_size) - padding_size;
+ ASSERT_EQ(weight_size, buffer->size());
+ std::vector<uint8_t> read_data(weight_size);
+ stream.read(reinterpret_cast<char*>(read_data.data()), read_data.size());
+ EXPECT_EQ(std::memcmp(read_data.data(), buffer->get_ptr(), buffer->size()), 0);
+ } else {
+ stream.seekg(length, std::ios::cur);
+ }
+ }
+}
+
+TEST_F(SingleFileStorageTest, ContextWeightSourceAppendDelta) {
+ weight_sharing::Context test_context;
+ const auto buffer_1 = std::make_shared<ov::AlignedBuffer>(1024);
+ test_context.m_cache_sources[1].m_weights = buffer_1;
+ m_storage->write_context(test_context);
+
+ const auto buffer_2 = std::make_shared<ov::AlignedBuffer>(47);
+ test_context.m_cache_sources[11].m_weights = buffer_2;
+ m_storage->write_context(test_context);
+
+ EXPECT_EQ(m_storage->get_context().m_cache_sources.size(), 2);
+ m_storage.reset();
+ const auto file_size_after_first_write = test::utils::fileSize(m_file_path.string());
+
+ SingleFileStorage{m_file_path}.write_context(test_context);
+ const auto file_size_after_second_write = test::utils::fileSize(m_file_path.string());
+ EXPECT_EQ(file_size_after_second_write, file_size_after_first_write)
+ << "Rewriting the same context should not increase file size";
+}
+} // namespace ov::test
diff --git a/src/inference/tests/unit/tlv_format.cpp b/src/inference/tests/unit/tlv_format.cpp
new file mode 100644
index 00000000000..c79dd0321d0
--- /dev/null
+++ b/src/inference/tests/unit/tlv_format.cpp
@@ -0,0 +1,147 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "openvino/runtime/tlv_format.hpp"
+
+#include <gtest/gtest.h>
+
+#include <array>
+#include <sstream>
+
+namespace ov::test {
+namespace {
+std::stringstream make_stream() {
+ return std::stringstream(std::ios::in | std::ios::out | std::ios::binary);
+}
+} // namespace
+
+using runtime::TLVTraits;
+
+TEST(TLVFormatTest, WriteReadRecordWithRawData) {
+ auto stream = make_stream();
+ const TLVTraits::TagType expected_tag = 77;
+ const std::vector<char> expected_data = {0x12, 0x34, 0x56, 0x78};
+
+ runtime::write_tlv_record(stream, expected_tag, expected_data.size(), expected_data.data());
+ stream.seekg(0);
+
+ TLVTraits::TagType tag = 0;
+ TLVTraits::LengthType size = 0;
+ std::vector<char> data;
+ ASSERT_TRUE(runtime::read_tlv_record(stream, tag, size, data));
+ EXPECT_EQ(tag, expected_tag);
+ EXPECT_EQ(size, expected_data.size());
+ EXPECT_EQ(data, expected_data);
+ EXPECT_EQ(stream.tellg(), sizeof(TLVTraits::TagType) + sizeof(TLVTraits::LengthType) + expected_data.size());
+}
+
+TEST(TLVFormatTest, WriteReadRecordWithWriter) {
+ auto stream = make_stream();
+ const TLVTraits::TagType expected_tag = 101;
+ const std::string expected_data = "payload_data";
+
+ runtime::write_tlv_record(stream, expected_tag, [&](std::ostream& s) {
+ s.write(expected_data.data(), expected_data.size());
+ });
+ stream.seekg(0);
+
+ TLVTraits::TagType tag = 0;
+ TLVTraits::LengthType size = 0;
+ std::vector<char> buffer;
+ ASSERT_TRUE(runtime::read_tlv_record(stream, tag, size, buffer));
+ const std::string data{buffer.begin(), buffer.end()};
+ EXPECT_EQ(tag, expected_tag);
+ EXPECT_EQ(size, expected_data.size());
+ EXPECT_EQ(data, expected_data);
+ EXPECT_EQ(stream.tellg(), sizeof(TLVTraits::TagType) + sizeof(TLVTraits::LengthType) + expected_data.size());
+}
+
+TEST(TLVFormatTest, WriteReadRecordWithZeroSize) {
+ auto stream = make_stream();
+ const TLVTraits::TagType expected_tag = 15;
+ const auto validate = [&]() {
+ stream.seekg(0);
+ TLVTraits::TagType tag = 0;
+ TLVTraits::LengthType size = 123;
+ std::vector<char> data = {'A', 'B', 'C'};
+ ASSERT_TRUE(runtime::read_tlv_record(stream, tag, size, data));
+ EXPECT_EQ(tag, expected_tag);
+ EXPECT_EQ(size, 0);
+ EXPECT_TRUE(data.empty());
+ stream.clear();
+ };
+
+ runtime::write_tlv_record(stream, expected_tag, 0, nullptr);
+ validate();
+
+ runtime::write_tlv_record(stream, expected_tag, [](std::ostream&) {});
+ validate();
+
+ runtime::write_tlv_record(stream, expected_tag, {});
+ validate();
+}
+
+TEST(TLVFormatTest, ReadRecordReturnsFalseForTruncatedHeader) {
+ auto stream = make_stream();
+ const TLVTraits::TagType tag = 11;
+ stream.write(reinterpret_cast<const char*>(&tag), sizeof(tag));
+ stream.seekg(0);
+
+ TLVTraits::TagType read_tag = 0;
+ TLVTraits::LengthType read_size = 0;
+ std::vector<char> data;
+ EXPECT_FALSE(runtime::read_tlv_record(stream, read_tag, read_size, data));
+}
+
+TEST(TLVFormatTest, ReadRecordReturnsFalseForTruncatedPayload) {
+ auto stream = make_stream();
+ const TLVTraits::TagType tag = 17;
+ const TLVTraits::LengthType size = 6;
+ const std::array<uint8_t, 3> partial_data = {0xAA, 0xBB, 0xCC};
+
+ stream.write(reinterpret_cast<const char*>(&tag), sizeof(tag));
+ stream.write(reinterpret_cast<const char*>(&size), sizeof(size));
+ stream.write(reinterpret_cast<const char*>(partial_data.data()), partial_data.size());
+ stream.seekg(0);
+
+ TLVTraits::TagType read_tag = 0;
+ TLVTraits::LengthType read_size = 0;
+ std::vector<char> data;
+ EXPECT_FALSE(runtime::read_tlv_record(stream, read_tag, read_size, data));
+}
+
+TEST(TLVFormatTest, ScanRecordsReadsMatchingTagsAndSkipsUnknown) {
+ auto stream = make_stream();
+ const std::string first_data = "one";
+ const std::string second_data = "skip_me";
+ const std::string third_data = "three";
+
+ runtime::write_tlv_record(stream, 1, first_data.size(), first_data.data());
+ runtime::write_tlv_record(stream, 2, second_data.size(), second_data.data());
+ runtime::write_tlv_record(stream, 3, third_data.size(), third_data.data());
+
+ size_t scanner_calls = 0;
+ std::string scanned_first;
+ std::string scanned_third;
+ runtime::TLVValueScanner scanners;
+ scanners.emplace(1, [&](std::istream& s, TLVTraits::LengthType size) {
+ scanned_first.resize(size);
+ s.read(scanned_first.data(), size);
+ ++scanner_calls;
+ return s.good();
+ });
+ scanners.emplace(3, [&](std::istream& s, TLVTraits::LengthType size) {
+ scanned_third.resize(size);
+ s.read(scanned_third.data(), size);
+ ++scanner_calls;
+ return s.good();
+ });
+
+ stream.seekg(0);
+ runtime::scan_tlv_records(stream, scanners);
+ EXPECT_EQ(scanner_calls, 2);
+ EXPECT_EQ(scanned_first, first_data);
+ EXPECT_EQ(scanned_third, third_data);
+}
+} // namespace ov::test