OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[Core] Add HSM model id codecs (#38760)
Tomasz Jankowskif80f6d8f88e4
diff --git a/src/cmake/openvino.cmake b/src/cmake/openvino.cmakeindex 92888f5a45b..232accf3062 100644--- a/src/cmake/openvino.cmake+++ b/src/cmake/openvino.cmake@@ -156,6 +156,11 @@ set(dev_api_headers${OpenVINO_SOURCE_DIR}/src/inference/dev_api/openvino/runtime/compilation_context.hpp${OpenVINO_SOURCE_DIR}/src/inference/dev_api/openvino/runtime/device_id_parser.hpp${OpenVINO_SOURCE_DIR}/src/inference/dev_api/openvino/runtime/exec_model_info.hpp+ ${OpenVINO_SOURCE_DIR}/src/inference/dev_api/openvino/runtime/hsm_deferred_writer.hpp+ ${OpenVINO_SOURCE_DIR}/src/inference/dev_api/openvino/runtime/hsm_format.hpp+ ${OpenVINO_SOURCE_DIR}/src/inference/dev_api/openvino/runtime/hsm_model_id_codec.hpp+ ${OpenVINO_SOURCE_DIR}/src/inference/dev_api/openvino/runtime/hsm_reader.hpp+ ${OpenVINO_SOURCE_DIR}/src/inference/dev_api/openvino/runtime/hsm_writer.hpp${OpenVINO_SOURCE_DIR}/src/inference/dev_api/openvino/runtime/iasync_infer_request.hpp${OpenVINO_SOURCE_DIR}/src/inference/dev_api/openvino/runtime/icache_manager.hpp${OpenVINO_SOURCE_DIR}/src/inference/dev_api/openvino/runtime/icompiled_model.hppdiff --git a/src/common/util/include/openvino/util/memory.hpp b/src/common/util/include/openvino/util/memory.hppindex fe6b7bab227..efe7b214b91 100644--- a/src/common/util/include/openvino/util/memory.hpp+++ b/src/common/util/include/openvino/util/memory.hpp@@ -12,6 +12,7 @@#include <optional>#include <string>#include <system_error>+#include <type_traits>namespace ov::util {@@ -167,6 +168,16 @@ private:size_t m_size = 0;};+/** @brief Non-owning byte view of @p value's object representation; @p value must outlive the view. */+template <typename T>+MemoryView as_memory_view(T& value) noexcept {+ static_assert(std::is_trivially_copyable_v<T>, "as_memory_view requires a trivially copyable type");+ return {reinterpret_cast<const std::byte*>(&value), sizeof(T)};+}++template <typename T>+MemoryView as_memory_view(T&&) = delete;+/*** @brief Capability: hints for managing a buffer's physical-memory residency.*/diff --git a/src/inference/dev_api/openvino/runtime/hsm_model_id_codec.hpp b/src/inference/dev_api/openvino/runtime/hsm_model_id_codec.hppnew file mode 100644index 00000000000..1fd4d0843d9--- /dev/null+++ b/src/inference/dev_api/openvino/runtime/hsm_model_id_codec.hpp@@ -0,0 +1,35 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++/**+ * @brief Small per-tag section codecs that don't warrant a dedicated file of their own.+ * @file openvino/runtime/hsm_model_id_codec.hpp+ */++#pragma once++#include <cstdint>+#include <optional>++#include "openvino/runtime/common.hpp"+#include "openvino/runtime/hsm_format.hpp"+#include "openvino/runtime/hsm_reader.hpp"++namespace ov::runtime::hsm {+inline namespace v1 {++// No encoder needed: #model_id_tag is inline, so the id goes straight into #IWriter::add_section() as+// `ov::util::as_memory_view(id)`.++/**+ * @brief Reads the model id from a #model_id_tag section.+ * @note Layout: bytes [0, 8) hold the id as a native-byte-order `uint64_t`; the rest of the inline slot is+ * ignored.+ * @param section Section to decode; its tag must be #model_id_tag (debug-asserted).+ * @return `std::nullopt` if the read fails.+ */+OPENVINO_RUNTIME_API std::optional<uint64_t> decode_model_id(const Section& section) noexcept;++} // namespace v1+} // namespace ov::runtime::hsmdiff --git a/src/inference/dev_api/openvino/runtime/hsm_reader.hpp b/src/inference/dev_api/openvino/runtime/hsm_reader.hppindex 93830c4f71e..18bd6b09caa 100644--- a/src/inference/dev_api/openvino/runtime/hsm_reader.hpp+++ b/src/inference/dev_api/openvino/runtime/hsm_reader.hpp@@ -188,7 +188,7 @@ public:/*** @brief Returns a reference to the vector of all manifest entries in the container.* @return A reference to the vector of all manifest entries in the container.- */+ */const std::vector<ManifestEntry>& entries() const noexcept {return m_manifest;}@@ -234,7 +234,8 @@ public:}/**- * @brief Finds all manifest entries matching (@p device, @p tag) and returns a #Section handle for each, in manifest order.+ * @brief Finds all manifest entries matching (@p device, @p tag) and returns a #Section handle for each, in+ * manifest order.** @tparam Tag The type of the tag, typically an enum or `uint32_t`.* @param device The device ID to match.diff --git a/src/inference/sources.cmake b/src/inference/sources.cmakeindex 418db51f103..c49c2441167 100644--- a/src/inference/sources.cmake+++ b/src/inference/sources.cmake@@ -22,6 +22,7 @@ set(OV_RUNTIME_SRCS${CMAKE_CURRENT_LIST_DIR}/src/dev/exec_model_info.cpp${CMAKE_CURRENT_LIST_DIR}/src/dev/hsm_format.cpp${CMAKE_CURRENT_LIST_DIR}/src/dev/hsm_deferred_writer.cpp+ ${CMAKE_CURRENT_LIST_DIR}/src/dev/hsm_model_id_codec.cpp${CMAKE_CURRENT_LIST_DIR}/src/dev/hsm_reader.cpp${CMAKE_CURRENT_LIST_DIR}/src/dev/hsm_writer.cpp${CMAKE_CURRENT_LIST_DIR}/src/dev/iasync_infer_request.cppdiff --git a/src/inference/src/dev/hsm_model_id_codec.cpp b/src/inference/src/dev/hsm_model_id_codec.cppnew file mode 100644index 00000000000..75f908c9c69--- /dev/null+++ b/src/inference/src/dev/hsm_model_id_codec.cpp@@ -0,0 +1,21 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "openvino/runtime/hsm_model_id_codec.hpp"++#include <cassert>++#include "openvino/core/except.hpp"++namespace ov::runtime::hsm {+inline namespace v1 {++std::optional<uint64_t> decode_model_id(const Section& section) noexcept {+ OPENVINO_DEBUG_ASSERT(section.tag().value == model_id_tag.value);+ uint64_t id{};+ return section.read(0, sizeof(id), &id) ? std::optional{id} : std::nullopt;+}++} // namespace v1+} // namespace ov::runtime::hsmdiff --git a/src/inference/tests/unit/CMakeLists.txt b/src/inference/tests/unit/CMakeLists.txtindex ba9b2f889b6..aedb02bc048 100644--- a/src/inference/tests/unit/CMakeLists.txt+++ b/src/inference/tests/unit/CMakeLists.txt@@ -28,6 +28,7 @@ ov_add_test_target(${CMAKE_CURRENT_SOURCE_DIR}/executor_manager_tests.cpp${CMAKE_CURRENT_SOURCE_DIR}/hsm_deferred_writer.cpp${CMAKE_CURRENT_SOURCE_DIR}/hsm_format.cpp+ ${CMAKE_CURRENT_SOURCE_DIR}/hsm_model_id_codec.cpp${CMAKE_CURRENT_SOURCE_DIR}/hsm_reader.cpp${CMAKE_CURRENT_SOURCE_DIR}/hsm_writer.cpp${CMAKE_CURRENT_SOURCE_DIR}/infer_request_test.cppdiff --git a/src/inference/tests/unit/hsm_model_id_codec.cpp b/src/inference/tests/unit/hsm_model_id_codec.cppnew file mode 100644index 00000000000..961e102acb5--- /dev/null+++ b/src/inference/tests/unit/hsm_model_id_codec.cpp@@ -0,0 +1,50 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "openvino/runtime/hsm_model_id_codec.hpp"++#include <gtest/gtest.h>++#include <algorithm>+#include <array>+#include <cstddef>+#include <cstdint>+#include <cstring>+#include <limits>+#include <type_traits>++#include "openvino/runtime/hsm_format.hpp"+#include "openvino/runtime/hsm_reader.hpp"++namespace ov::test {+namespace hsm = ov::runtime::hsm;+namespace {++using WireId = std::array<uint8_t, sizeof(uint64_t)>;++constexpr uint64_t sample_id = 0x0123456789ABCDEF;+constexpr WireId sample_wire{0xEF, 0xCD, 0xAB, 0x89, 0x67, 0x45, 0x23, 0x01};++constexpr uint64_t max_id = std::numeric_limits<uint64_t>::max();+constexpr WireId max_wire{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};++static_assert(std::is_convertible_v<decltype(&hsm::decode_model_id), hsm::SectionDecoder<uint64_t>>,+ "decode_model_id must be usable as a SectionDecoder<uint64_t>.");++hsm::Section inline_section(const WireId& wire) {+ hsm::ManifestEntry entry{};+ entry.tag = hsm::model_id_tag;+ std::copy(wire.begin(), wire.end(), entry.inline_bytes.begin());+ return hsm::Section{entry};+}++} // namespace++TEST(HsmModelIdSectionTest, decode_reads_the_little_endian_wire_bytes) {+ EXPECT_EQ(hsm::decode_model_id(inline_section(sample_wire)), sample_id);+ EXPECT_EQ(hsm::decode_model_id(inline_section(max_wire)), max_id);+ EXPECT_EQ(hsm::decode_model_id(inline_section(WireId{})), 0u);+}++} // namespace ov::testdiff --git a/src/tests/test_utils/common_test_utils/tests/memory_test.cpp b/src/tests/test_utils/common_test_utils/tests/memory_test.cppindex 37ad813dfcd..d7d2935ab88 100644--- a/src/tests/test_utils/common_test_utils/tests/memory_test.cpp+++ b/src/tests/test_utils/common_test_utils/tests/memory_test.cpp@@ -2,19 +2,16 @@// SPDX-License-Identifier: Apache-2.0//+#include "openvino/util/memory.hpp"+#include <gmock/gmock.h>+#include <array>#include <cstdint>-#include <filesystem>+#include <cstring>#include <limits>-#include <system_error>-#include <vector>--#include "common_test_utils/common_utils.hpp"-#include "common_test_utils/file_utils.hpp"-#include "openvino/util/file_util.hpp"-#include "openvino/util/memory.hpp"-#include "openvino/util/mmap_object.hpp"+#include <type_traits>+#include <utility>namespace ov::test {@@ -220,4 +217,54 @@ TEST_F(MemoryViewTest, non_zero_size_with_valid_pointer_is_not_empty) {EXPECT_EQ(view.data() + view.size(), view.end());}+TEST_F(MemoryViewTest, as_memory_view_spans_object) {+ const int32_t value = 42;+ const auto view = util::as_memory_view(value);+ EXPECT_EQ(reinterpret_cast<const std::byte*>(&value), view.data());+ EXPECT_EQ(sizeof(value), view.size());+ EXPECT_FALSE(view.empty());+}++TEST_F(MemoryViewTest, as_memory_view_exposes_object_representation) {+ const uint32_t value = 0x01020304;+ const auto view = util::as_memory_view(value);+ EXPECT_EQ(0, std::memcmp(view.data(), &value, sizeof(value)));+}++TEST_F(MemoryViewTest, as_memory_view_is_non_owning) {+ uint32_t value = 1;+ const auto view = util::as_memory_view(value);+ value = 0xAABBCCDD;+ EXPECT_EQ(0, std::memcmp(view.data(), &value, sizeof(value)));+}++TEST_F(MemoryViewTest, as_memory_view_of_struct_covers_padding) {+ struct Padded {+ char c;+ int64_t i;+ };+ const Padded value{};+ EXPECT_EQ(sizeof(Padded), util::as_memory_view(value).size());+}++TEST_F(MemoryViewTest, as_memory_view_of_array_covers_all_elements) {+ const std::array<uint16_t, 4> arr{1, 2, 3, 4};+ const auto view = util::as_memory_view(arr);+ EXPECT_EQ(sizeof(arr), view.size());+ EXPECT_EQ(reinterpret_cast<const std::byte*>(arr.data()), view.data());+}++template <typename T, typename = void>+struct CanMakeMemoryView : std::false_type {};++template <typename T>+struct CanMakeMemoryView<T, std::void_t<decltype(util::as_memory_view(std::declval<T>()))>> : std::true_type {};++TEST_F(MemoryViewTest, as_memory_view_rejects_rvalues) {+ static_assert(CanMakeMemoryView<int&>::value);+ static_assert(CanMakeMemoryView<const int&>::value);+ static_assert(!CanMakeMemoryView<int>::value);+ static_assert(!CanMakeMemoryView<const int>::value);+}+} // namespace ov::test