Skip to content

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

[CORE] Property cache header alignment (#32178)

barnasm1e8054b4bb9e3
diff --git a/src/inference/dev_api/openvino/runtime/compilation_context.hpp b/src/inference/dev_api/openvino/runtime/compilation_context.hpp
index 135541911d8..14799620db5 100644
--- a/src/inference/dev_api/openvino/runtime/compilation_context.hpp
+++ b/src/inference/dev_api/openvino/runtime/compilation_context.hpp
@@ -35,10 +35,14 @@ class CompiledBlobHeader final {
std::string m_ieVersion;
std::string m_fileInfo;
std::string m_runtimeInfo;
+ uint32_t m_headerSizeAlignment{0};
public:
CompiledBlobHeader();
- CompiledBlobHeader(const std::string& ieVersion, const std::string& fileInfo, const std::string& runtimeInfo);
+ CompiledBlobHeader(const std::string& ieVersion,
+ const std::string& fileInfo,
+ const std::string& runtimeInfo,
+ const uint32_t headerSizeAlignment = 0);
const std::string& get_openvino_version() const {
return m_ieVersion;
@@ -52,6 +56,10 @@ public:
return m_runtimeInfo;
}
+ const uint32_t get_header_size_alignment() const {
+ return m_headerSizeAlignment;
+ }
+
friend std::istream& operator>>(std::istream& stream, CompiledBlobHeader& header);
friend std::ostream& operator<<(std::ostream& stream, const CompiledBlobHeader& header);
diff --git a/src/inference/dev_api/openvino/runtime/internal_properties.hpp b/src/inference/dev_api/openvino/runtime/internal_properties.hpp
index 206c7c72dde..7100aeb9a87 100644
--- a/src/inference/dev_api/openvino/runtime/internal_properties.hpp
+++ b/src/inference/dev_api/openvino/runtime/internal_properties.hpp
@@ -90,6 +90,12 @@ static constexpr Property<float, PropertyMutability::RW> query_model_ratio{"QUER
*/
static constexpr Property<bool, PropertyMutability::RW> enable_lp_transformations{"LP_TRANSFORMS_MODE"};
+/**
+ * @brief Read-only property to get plugin specific needed alignment for cache header
+ * @ingroup ov_runtime_cpp_prop_api
+ */
+static constexpr Property<uint32_t, PropertyMutability::RO> cache_header_alignment{"CACHE_HEADER_ALIGNMENT"};
+
/**
* @brief Enum to define possible cache quant schema hints.
*/
@@ -146,13 +152,13 @@ inline std::istream& operator>>(std::istream& is, CacheQuantMode& mode) {
├───────────────────────────────┤ token[0]
├───────────────────────────────┤ token[1]
│ │ ...
- │ │
- │ │
+ │ │
+ │ │
│ │ token[seq_len - 1]
└───────────────────────────────┘
c[0] c[1] ... c[head_size - 1]
*/
-
+
static constexpr Property<CacheQuantMode, PropertyMutability::RW> key_cache_quant_mode{"KEY_CACHE_QUANT_MODE"};
/**
diff --git a/src/inference/src/dev/compilation_context.cpp b/src/inference/src/dev/compilation_context.cpp
index 319570688ed..2ab46e42638 100644
--- a/src/inference/src/dev/compilation_context.cpp
+++ b/src/inference/src/dev/compilation_context.cpp
@@ -174,14 +174,19 @@ CompiledBlobHeader::CompiledBlobHeader() {}
CompiledBlobHeader::CompiledBlobHeader(const std::string& ieVersion,
const std::string& fileInfo,
- const std::string& runtimeInfo)
+ const std::string& runtimeInfo,
+ const uint32_t headerSizeAlignment)
: m_ieVersion(ieVersion),
m_fileInfo(fileInfo),
- m_runtimeInfo(runtimeInfo) {}
+ m_runtimeInfo(runtimeInfo),
+ m_headerSizeAlignment(headerSizeAlignment) {}
std::istream& operator>>(std::istream& stream, CompiledBlobHeader& header) {
std::string xmlStr;
+
+ auto start = stream.tellg();
std::getline(stream, xmlStr);
+ auto end = stream.tellg();
pugi::xml_document document;
pugi::xml_parse_result res = document.load_string(xmlStr.c_str());
@@ -191,6 +196,15 @@ std::istream& operator>>(std::istream& stream, CompiledBlobHeader& header) {
header.m_ieVersion = ov::util::pugixml::get_str_attr(compiledBlobNode, "ie_version");
header.m_fileInfo = ov::util::pugixml::get_str_attr(compiledBlobNode, "file_info");
header.m_runtimeInfo = ov::util::pugixml::get_str_attr(compiledBlobNode, "runtime_info");
+ header.m_headerSizeAlignment = ov::util::pugixml::get_uint_attr(compiledBlobNode, "header_size_alignment");
+
+ if (const uint32_t headerSizeAlignment = header.m_headerSizeAlignment; headerSizeAlignment) {
+ size_t bytes_read = static_cast<size_t>(end - start);
+ size_t pad =
+ (headerSizeAlignment - (bytes_read % headerSizeAlignment)) % headerSizeAlignment; // 0 if already aligned
+ stream.seekg(static_cast<std::streamoff>(pad), std::ios::cur);
+ OPENVINO_ASSERT(stream.good(), "Failed to seek over padding in compiled blob header");
+ }
return stream;
}
@@ -201,10 +215,22 @@ std::ostream& operator<<(std::ostream& stream, const CompiledBlobHeader& header)
compiledBlobNode.append_attribute("ie_version").set_value(header.m_ieVersion.c_str());
compiledBlobNode.append_attribute("file_info").set_value(header.m_fileInfo.c_str());
compiledBlobNode.append_attribute("runtime_info").set_value(header.m_runtimeInfo.c_str());
+ compiledBlobNode.append_attribute("header_size_alignment")
+ .set_value(std::to_string(header.m_headerSizeAlignment).c_str());
+ auto start = stream.tellp();
document.save(stream, nullptr, pugi::format_raw);
document.reset();
stream << std::endl;
+ auto end = stream.tellp();
+
+ // add padding
+ if (const uint32_t headerSizeAlignment = header.m_headerSizeAlignment; headerSizeAlignment) {
+ size_t bytes_written = static_cast<size_t>(end - start);
+ size_t pad = (headerSizeAlignment - (bytes_written % headerSizeAlignment)) %
+ headerSizeAlignment; // 0 if already aligned
+ std::fill_n(std::ostream_iterator<char>(stream), pad, 0);
+ }
return stream;
}
@@ -230,7 +256,9 @@ inline std::string getline_from_buffer(const char* buffer, size_t size, size_t&
} // namespace
void CompiledBlobHeader::read_from_buffer(const char* buffer, size_t buffer_size, size_t& pos) {
+ size_t start = pos;
std::string xmlStr = ov::getline_from_buffer(buffer, buffer_size, pos);
+ size_t end = pos;
pugi::xml_document document;
pugi::xml_parse_result res = document.load_string(xmlStr.c_str());
@@ -240,5 +268,13 @@ void CompiledBlobHeader::read_from_buffer(const char* buffer, size_t buffer_size
m_ieVersion = ov::util::pugixml::get_str_attr(compiledBlobNode, "ie_version");
m_fileInfo = ov::util::pugixml::get_str_attr(compiledBlobNode, "file_info");
m_runtimeInfo = ov::util::pugixml::get_str_attr(compiledBlobNode, "runtime_info");
+ m_headerSizeAlignment = ov::util::pugixml::get_uint_attr(compiledBlobNode, "header_size_alignment");
+
+ if (const uint32_t headerSizeAlignment = m_headerSizeAlignment; headerSizeAlignment) {
+ size_t bytes_read = static_cast<size_t>(end - start);
+ size_t pad =
+ (headerSizeAlignment - (bytes_read % headerSizeAlignment)) % headerSizeAlignment; // 0 if already aligned
+ pos += pad;
+ }
}
} // namespace ov
diff --git a/src/inference/src/dev/core_impl.cpp b/src/inference/src/dev/core_impl.cpp
index 6a047eda792..db916280eab 100644
--- a/src/inference/src/dev/core_impl.cpp
+++ b/src/inference/src/dev/core_impl.cpp
@@ -271,6 +271,7 @@ std::filesystem::path get_cache_model_path(const ov::AnyMap& config) {
const auto it = config.find(ov::cache_model_path.name());
return it == config.end() ? std::filesystem::path{} : it->second.as<std::filesystem::path>();
}
+
} // namespace
bool ov::is_config_applicable(const std::string& user_device_name, const std::string& subprop_device_name) {
@@ -1506,9 +1507,17 @@ ov::SoPtr<ov::ICompiledModel> ov::CoreImpl::compile_model_and_cache(ov::Plugin&
plugin.get_property(ov::internal::compiled_model_runtime_properties.name(), {}).as<std::string>();
}
cacheContent.cacheManager->write_cache_entry(cacheContent.blobId, [&](std::ostream& networkStream) {
+ uint32_t header_size_alignment{};
+ if (device_supports_internal_property(plugin, ov::internal::cache_header_alignment.name())) {
+ header_size_alignment =
+ plugin.get_property(ov::internal::cache_header_alignment.name(), {}).as<uint32_t>();
+ }
+
networkStream << ov::CompiledBlobHeader(ov::get_openvino_version().buildNumber,
ov::ModelCache::calculate_file_info(cacheContent.modelPath),
- compiled_model_runtime_properties);
+ compiled_model_runtime_properties,
+ header_size_alignment);
+
compiled_model->export_model(networkStream);
});
} catch (...) {
diff --git a/src/plugins/template/src/config.cpp b/src/plugins/template/src/config.cpp
index 34e13fe06aa..b304b3af964 100644
--- a/src/plugins/template/src/config.cpp
+++ b/src/plugins/template/src/config.cpp
@@ -173,6 +173,8 @@ ov::Any Configuration::Get(const std::string& name) const {
return compiled_model_runtime_properties;
} else if (name == ov::cache_mode) {
return cache_mode;
+ } else if (name == ov::internal::cache_header_alignment) {
+ return 4096; // page size in bytes
} else {
OPENVINO_THROW("Property was not found: ", name);
}
diff --git a/src/plugins/template/src/plugin.cpp b/src/plugins/template/src/plugin.cpp
index b7edea78d83..b635201cc96 100644
--- a/src/plugins/template/src/plugin.cpp
+++ b/src/plugins/template/src/plugin.cpp
@@ -392,7 +392,9 @@ ov::Any ov::template_plugin::Plugin::get_property(const std::string& name, const
ov::PropertyName{ov::internal::exclusive_async_requests.name(), ov::PropertyMutability::RW},
ov::PropertyName{ov::inference_num_threads.name(), ov::PropertyMutability::RW},
ov::PropertyName{ov::internal::threads_per_stream.name(), ov::PropertyMutability::RW},
- ov::PropertyName{ov::internal::compiled_model_runtime_properties.name(), ov::PropertyMutability::RO}};
+ ov::PropertyName{ov::internal::compiled_model_runtime_properties.name(), ov::PropertyMutability::RO},
+ ov::PropertyName{ov::internal::cache_header_alignment.name(), ov::PropertyMutability::RO},
+ };
} else if (ov::available_devices == name) {
// TODO: fill list of available devices
return decltype(ov::available_devices)::value_type{{""}};
diff --git a/src/tests/functional/base_func_tests/src/behavior/compiled_model/properties.cpp b/src/tests/functional/base_func_tests/src/behavior/compiled_model/properties.cpp
index 7d22c90bb0e..d77228ba706 100644
--- a/src/tests/functional/base_func_tests/src/behavior/compiled_model/properties.cpp
+++ b/src/tests/functional/base_func_tests/src/behavior/compiled_model/properties.cpp
@@ -4,12 +4,13 @@
#include "behavior/compiled_model/properties.hpp"
+#include <locale.h>
+
#include <cstdint>
-#include "openvino/runtime/properties.hpp"
#include "common_test_utils/subgraph_builders/conv_pool_relu.hpp"
-
-#include <locale.h>
+#include "openvino/op/relu.hpp"
+#include "openvino/runtime/properties.hpp"
namespace ov {
namespace test {
@@ -85,6 +86,162 @@ TEST_P(OVClassCompiledModelPropertiesTests, CanUseCache) {
ov::test::utils::removeDir(cache_dir);
}
+TEST(CacheHeaderAlignmentTests, CacheHeaderPaddingAndAlignment) {
+ ov::Core core;
+ const std::string device_with_alignment{"TEMPLATE"};
+ uint32_t alignment{};
+ try {
+ alignment =
+ core.get_property(device_with_alignment, ov::internal::cache_header_alignment.name(), {}).as<uint32_t>();
+ } catch (...) {
+ GTEST_SKIP() << device_with_alignment
+ << " device is not supported or cache_header_alignment property is not defined";
+ }
+
+ auto p = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::Shape{1, 2});
+ auto relu = std::make_shared<ov::op::v0::Relu>(p);
+ auto r = std::make_shared<ov::op::v0::Result>(relu);
+ auto model = std::make_shared<ov::Model>(ov::ResultVector{r}, ov::ParameterVector{p});
+
+ auto cache_dir =
+ std::string("cache_alignment_") + std::to_string(std::chrono::steady_clock::now().time_since_epoch().count());
+ ov::test::utils::createDirectory(cache_dir);
+ core.set_property(ov::cache_dir(cache_dir));
+
+ // Generate cache blob
+ auto compiled = core.compile_model(model, device_with_alignment);
+ ASSERT_FALSE(compiled.get_property(ov::loaded_from_cache));
+
+ auto blobs = ov::test::utils::listFilesWithExt(cache_dir, "blob");
+ ASSERT_EQ(blobs.size(), 1) << "Expected exactly one cache blob";
+ const auto& blob_path = blobs.front();
+
+ std::ifstream ifs(blob_path, std::ios::binary);
+ ASSERT_TRUE(ifs.is_open());
+ std::vector<uint8_t> data((std::istreambuf_iterator<char>(ifs)), {});
+ ASSERT_GT(data.size(), alignment) << "Blob too small";
+
+ // Find end of header
+ auto it_nl = std::find(data.begin(), data.end(), '\n');
+ ASSERT_NE(it_nl, data.end()) << "No newline in header";
+ size_t header_len = (it_nl - data.begin()) + 1;
+
+ size_t pad_expected = (alignment - (header_len % alignment)) % alignment;
+ ASSERT_LE(header_len + pad_expected, data.size());
+
+ for (size_t i = 0; i < pad_expected; ++i) {
+ ASSERT_EQ(data[header_len + i], 0) << "Non-zero byte in padding at +" << i;
+ }
+
+ size_t binary_start = header_len + pad_expected;
+ ASSERT_EQ(binary_start % alignment, 0U) << "Binary start not aligned";
+
+ size_t probe = std::min<size_t>(32, data.size() - binary_start);
+ bool any_non_zero = false;
+ for (size_t i = 0; i < probe; ++i) {
+ if (data[binary_start + i] != 0) {
+ any_non_zero = true;
+ break;
+ }
+ }
+ ASSERT_TRUE(any_non_zero) << "Binary section is empty/zeroed";
+
+ ov::test::utils::removeFilesWithExt(cache_dir, "blob");
+ ov::test::utils::removeDir(cache_dir);
+}
+
+TEST(CacheHeaderAlignmentTests, CacheHeaderPaddingAndAlignmentReadPath) {
+ ov::Core core;
+ const std::string device_with_alignment{"TEMPLATE"};
+ uint32_t alignment{};
+ try {
+ alignment =
+ core.get_property(device_with_alignment, ov::internal::cache_header_alignment.name(), {}).as<uint32_t>();
+ } catch (...) {
+ GTEST_SKIP() << device_with_alignment
+ << " device is not supported or cache_header_alignment property is not defined";
+ }
+
+ auto p = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::Shape{1, 2});
+ auto relu = std::make_shared<ov::op::v0::Relu>(p);
+ auto r = std::make_shared<ov::op::v0::Result>(relu);
+ auto model = std::make_shared<ov::Model>(ov::ResultVector{r}, ov::ParameterVector{p});
+
+ auto cache_dir = std::string("cache_alignment_read_") +
+ std::to_string(std::chrono::steady_clock::now().time_since_epoch().count());
+ ov::test::utils::createDirectory(cache_dir);
+ core.set_property(ov::cache_dir(cache_dir));
+
+ // Generate cache blob
+ auto compiled = core.compile_model(model, device_with_alignment);
+ ASSERT_FALSE(compiled.get_property(ov::loaded_from_cache));
+
+ auto blobs = ov::test::utils::listFilesWithExt(cache_dir, "blob");
+ ASSERT_EQ(blobs.size(), 1u);
+ const std::string blob_path = blobs.front();
+
+ // Second core to force read path
+ ov::Core core_reader;
+ core_reader.set_property(ov::cache_dir(cache_dir));
+ auto cm_second = core_reader.compile_model(model, device_with_alignment);
+
+ // If supported, must be loaded from cache now
+ {
+ auto supp = cm_second.get_property(ov::supported_properties);
+ if (std::find(supp.begin(), supp.end(), ov::loaded_from_cache.name()) != supp.end()) {
+ ASSERT_TRUE(cm_second.get_property(ov::loaded_from_cache));
+ }
+ }
+
+ // Read whole file
+ std::ifstream ifs(blob_path, std::ios::binary);
+ ASSERT_TRUE(ifs.is_open());
+ std::vector<uint8_t> bytes((std::istreambuf_iterator<char>(ifs)), {});
+ ASSERT_GT(bytes.size(), alignment);
+
+ // Locate textual header end (newline) and compute padding
+ auto it_nl = std::find(bytes.begin(), bytes.end(), '\n');
+ ASSERT_NE(it_nl, bytes.end()) << "No newline in cache header";
+ size_t header_len = (it_nl - bytes.begin()) + 1;
+ size_t pad = (alignment - (header_len % alignment)) % alignment;
+
+ // Verify padding bytes are zero
+ for (size_t i = 0; i < pad; ++i) {
+ ASSERT_LT(header_len + i, bytes.size());
+ ASSERT_EQ(bytes[header_len + i], 0) << "Padding byte not zero at +" << i;
+ }
+
+ size_t binary_start = header_len + pad;
+ ASSERT_EQ(binary_start % alignment, 0u) << "Binary segment not aligned";
+
+ // Export compiled model (should correspond exactly to binary segment on disk)
+ std::ostringstream oss;
+ cm_second.export_model(oss);
+ std::string exported = oss.str();
+
+ ASSERT_EQ(exported.size(), bytes.size() - binary_start)
+ << "Mismatch between exported binary size and file tail size";
+
+ // Byte-for-byte compare
+ const uint8_t* file_bin = bytes.data() + binary_start;
+ for (size_t i = 0; i < exported.size(); ++i) {
+ ASSERT_EQ(static_cast<unsigned char>(exported[i]), file_bin[i]) << "Binary mismatch at offset " << i;
+ }
+
+ // Quick inference sanity (ensures model usable after cache load)
+ auto ir = cm_second.create_infer_request();
+ for (size_t i = 0; i < cm_second.inputs().size(); ++i) {
+ auto t = ir.get_input_tensor(i);
+ if (t.get_element_type() == ov::element::f32) {
+ std::fill(t.data<float>(), t.data<float>() + t.get_size(), 1.0f);
+ }
+ }
+ ASSERT_NO_THROW(ir.infer());
+
+ ov::test::utils::removeFilesWithExt(cache_dir, "blob");
+ ov::test::utils::removeDir(cache_dir);
+}
+
TEST_P(OVClassCompiledModelPropertiesTests, canCompileModelWithPropertiesAndCheckGetProperty) {
auto compiled_model = core->compile_model(model, target_device, properties);
auto supported_properties = compiled_model.get_property(ov::supported_properties);