Skip to content

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

[Core] Expand HSM format with new device and tag definitions (#38694)

Tomasz Jankowski22a13ddc7986
diff --git a/src/inference/dev_api/openvino/runtime/hsm_format.hpp b/src/inference/dev_api/openvino/runtime/hsm_format.hpp
index 6d2ce82a13a..58bff353bc3 100644
--- a/src/inference/dev_api/openvino/runtime/hsm_format.hpp
+++ b/src/inference/dev_api/openvino/runtime/hsm_format.hpp
@@ -135,14 +135,48 @@ static_assert(sizeof(Header) == 32,
"this struct; readers and writers must be updated together.");
/**
- * @brief Device/plugin that owns a ManifestEntry's section. Device catalog and collision rules follow later.
+ * @brief Device/plugin that owns a ManifestEntry's section - see #Devices for the catalog of assigned ids.
* @note Paired with #SectionTag: a reader always compares `(device, tag)` together, never `tag` alone, so two
* devices may reuse the same tag id for unrelated content.
*/
using DeviceId = uint8_t;
+/**
+ * @brief Catalog of assigned #DeviceId values; never change or reuse a value once shipped.
+ * #sentinel_count auto-tracks the count and must stay last.
+ * @note A plugin gets an id here if it emits a blob of its own - including HETERO, whose `export_model()`
+ * writes its own container around the submodel blobs it embeds. AUTO, MULTI and AUTO-BATCH have no id:
+ * their `export_model()` is not implemented, so they never own a section. Anything else - sample, test and out-of-tree
+ * plugins - takes an id from #private_device_id_range_start instead.
+ */
+enum class Devices : DeviceId {
+ any = 0, //!< See #any_device_id.
+ cpu = 1, //!< See #cpu_device_id.
+ gpu = 2, //!< See #gpu_device_id.
+ npu = 3, //!< See #npu_device_id.
+ hetero = 4, //!< See #hetero_device_id.
+ // Add new real devices above this line only, each with the next explicit value - never change or reuse an
+ // existing device's value.
+ sentinel_count, // Not a real device id - always exactly one past the last real entry above.
+};
+
+/**
+ * @brief First #DeviceId with no interoperability guarantee: never assigned here, free for anyone -
+ * samples, tests, out-of-tree vendor plugins - to use without colliding with a real device.
+ */
+inline constexpr DeviceId private_device_id_range_start = 0x80;
+static_assert(static_cast<DeviceId>(Devices::sentinel_count) <= private_device_id_range_start,
+ "Too many real devices defined for private_device_id_range_start - widen the boundary.");
+
/** @brief Reserved #DeviceId for sections not tied to one specific device. */
-inline constexpr DeviceId any_device_id = 0;
+inline constexpr DeviceId any_device_id = static_cast<DeviceId>(Devices::any);
+
+inline constexpr DeviceId cpu_device_id = static_cast<DeviceId>(Devices::cpu); //!< CPU plugin.
+inline constexpr DeviceId gpu_device_id = static_cast<DeviceId>(Devices::gpu); //!< GPU plugin.
+inline constexpr DeviceId npu_device_id = static_cast<DeviceId>(Devices::npu); //!< NPU plugin.
+
+/// HETERO plugin - the one meta-plugin with an id; see the #Devices note for why.
+inline constexpr DeviceId hetero_device_id = static_cast<DeviceId>(Devices::hetero);
/**
* @brief Boundary in the 23-bit #SectionTag id space: Core ids are `< core_tag_id_range_end`;
@@ -237,6 +271,8 @@ enum class Tags : uint32_t {
model_id = 1, //!< See #model_id.
model = 2, //!< See #model.
runtime_requirements = 3, //!< See #runtime_requirements_tag.
+ weights = 4, //!< See #weights_tag.
+ compiled_options = 5, //!< See #compiled_options_tag.
// Add new Core tags above this line only, each with the next explicit value - never change or reuse an
// existing tag's value.
sentinel_count, // Not a real tag id - always exactly one past the last real entry above.
@@ -247,6 +283,8 @@ static_assert(static_cast<uint32_t>(Tags::sentinel_count) <= core_tag_id_range_e
inline constexpr uint32_t model_id = static_cast<uint32_t>(Tags::model_id); //!< Model identifier (e.g. a hash).
inline constexpr uint32_t model = static_cast<uint32_t>(Tags::model); //!< The serialized compiled model itself.
inline constexpr uint32_t runtime_requirements = static_cast<uint32_t>(Tags::runtime_requirements);
+inline constexpr uint32_t weights = static_cast<uint32_t>(Tags::weights);
+inline constexpr uint32_t compiled_options = static_cast<uint32_t>(Tags::compiled_options);
/// Wire tag for #model_id - always inline-mode.
inline constexpr SectionTag model_id_tag = SectionTag::make(model_id, /*is_inline=*/true);
@@ -263,6 +301,12 @@ inline constexpr SectionTag model_tag = SectionTag::make(model, /*is_inline=*/fa
*/
inline constexpr SectionTag runtime_requirements_tag = SectionTag::make(runtime_requirements, /*is_inline=*/false);
+/// Wire tag for #weights - always pointer-mode. Payload contract: CVS-196451.
+inline constexpr SectionTag weights_tag = SectionTag::make(weights, /*is_inline=*/false);
+
+/// Wire tag for #compiled_options - always pointer-mode. Payload contract: CVS-196452.
+inline constexpr SectionTag compiled_options_tag = SectionTag::make(compiled_options, /*is_inline=*/false);
+
/**
* @brief One fixed-size, 32-byte record of the manifest table (see #Header::manifest_offset).
*
diff --git a/src/inference/tests/unit/hsm_format.cpp b/src/inference/tests/unit/hsm_format.cpp
index e2f36931bb3..d7c5c4201e2 100644
--- a/src/inference/tests/unit/hsm_format.cpp
+++ b/src/inference/tests/unit/hsm_format.cpp
@@ -6,7 +6,10 @@
#include <gtest/gtest.h>
+#include <algorithm>
+#include <array>
#include <cstddef>
+#include <cstdint>
#include <cstring>
#include <limits>
#include <string>
@@ -113,6 +116,34 @@ public:
size_t last_section_size = 0;
};
+template <typename T, size_t N>
+bool all_unique(std::array<T, N> values) {
+ std::sort(values.begin(), values.end());
+ return std::adjacent_find(values.begin(), values.end()) == values.end();
+}
+
+template <typename T, size_t N>
+bool all_in_range(const std::array<T, N>& values, T low, T high) {
+ return std::all_of(values.begin(), values.end(), [low, high](const T& value) {
+ return low <= value && value < high;
+ });
+}
+
+// Every Core tag id. Listing them here is what makes the uniqueness and count guards below meaningful:
+// adding a tag to Tags without adding it here fails the build.
+constexpr std::array<uint32_t, 5> k_core_tag_ids{hsm::model_id,
+ hsm::model,
+ hsm::runtime_requirements,
+ hsm::weights,
+ hsm::compiled_options};
+
+// Every assigned device id, pinned the same way as k_core_tag_ids.
+constexpr std::array<hsm::DeviceId, 5> k_device_ids{hsm::any_device_id,
+ hsm::cpu_device_id,
+ hsm::gpu_device_id,
+ hsm::npu_device_id,
+ hsm::hetero_device_id};
+
} // namespace
// --- HSM wire-format layout/version compatibility --------------------------------------------------------
@@ -184,6 +215,12 @@ TEST_F(HsmFormatLayoutCompatibilityTest, core_tags_have_fixed_mode) {
static_assert(hsm::runtime_requirements_tag.id() == hsm::runtime_requirements, "runtime_requirements_tag id");
static_assert(hsm::runtime_requirements_tag.is_pointer(), "runtime_requirements_tag must always be pointer-mode");
+ static_assert(hsm::weights_tag.id() == hsm::weights, "weights_tag id");
+ static_assert(hsm::weights_tag.is_pointer(), "weights_tag must always be pointer-mode");
+
+ static_assert(hsm::compiled_options_tag.id() == hsm::compiled_options, "compiled_options_tag id");
+ static_assert(hsm::compiled_options_tag.is_pointer(), "compiled_options_tag must always be pointer-mode");
+
SUCCEED();
}
@@ -208,9 +245,36 @@ TEST_F(HsmFormatLayoutCompatibilityTest, core_tag_values_are_pinned) {
static_assert(static_cast<uint32_t>(hsm::Tags::model) == 2, "Tags::model value changed");
static_assert(static_cast<uint32_t>(hsm::Tags::runtime_requirements) == 3,
"Tags::runtime_requirements value changed");
+ static_assert(static_cast<uint32_t>(hsm::Tags::weights) == 4, "Tags::weights value changed");
+ static_assert(static_cast<uint32_t>(hsm::Tags::compiled_options) == 5, "Tags::compiled_options value changed");
SUCCEED();
}
+
+TEST_F(HsmFormatLayoutCompatibilityTest, core_tag_ids_cannot_be_shared) {
+ // +1 for Tags::invalid, which is not a real tag and so is absent from k_core_tag_ids.
+ static_assert(k_core_tag_ids.size() + 1 == static_cast<size_t>(hsm::Tags::sentinel_count),
+ "a Core tag was added to Tags but not listed in k_core_tag_ids");
+
+ EXPECT_TRUE(all_unique(k_core_tag_ids)) << "two Core tags resolve to the same wire id";
+ EXPECT_TRUE(all_in_range(k_core_tag_ids, 1U, hsm::core_tag_id_range_end))
+ << "a Core tag id reuses Tags::invalid or escapes the Core-owned range";
+}
+
+TEST_F(HsmFormatLayoutCompatibilityTest, device_values_are_pinned_and_cannot_be_shared) {
+ static_assert(static_cast<hsm::DeviceId>(hsm::Devices::any) == 0, "Devices::any value changed");
+ static_assert(static_cast<hsm::DeviceId>(hsm::Devices::cpu) == 1, "Devices::cpu value changed");
+ static_assert(static_cast<hsm::DeviceId>(hsm::Devices::gpu) == 2, "Devices::gpu value changed");
+ static_assert(static_cast<hsm::DeviceId>(hsm::Devices::npu) == 3, "Devices::npu value changed");
+ static_assert(static_cast<hsm::DeviceId>(hsm::Devices::hetero) == 4, "Devices::hetero value changed");
+ static_assert(k_device_ids.size() == static_cast<size_t>(hsm::Devices::sentinel_count),
+ "a device was added to Devices but not listed in k_device_ids");
+
+ EXPECT_TRUE(all_unique(k_device_ids)) << "two devices resolve to the same wire id";
+ EXPECT_TRUE(
+ all_in_range(k_device_ids, hsm::any_device_id, static_cast<hsm::DeviceId>(hsm::Devices::sentinel_count)))
+ << "a device id falls outside the Devices catalog";
+}
// --- HSM wire-format layout/version compatibility end ----------------------------------------------------
TEST(HsmHeaderTest, check_single_blob_header) {
diff --git a/src/inference/tests/unit/hsm_reader.cpp b/src/inference/tests/unit/hsm_reader.cpp
index ec500eb0082..a90d2680f1e 100644
--- a/src/inference/tests/unit/hsm_reader.cpp
+++ b/src/inference/tests/unit/hsm_reader.cpp
@@ -20,7 +20,7 @@ namespace {
enum class FakePluginTag : uint32_t { shard = 0 };
-constexpr hsm::DeviceId fake_device_id = 7;
+constexpr hsm::DeviceId fake_device_id = hsm::private_device_id_range_start + 7;
struct EntrySpec {
hsm::ManifestEntry entry;
@@ -381,6 +381,35 @@ TEST(HsmReaderTest, section_accepts_any_enum_typed_tag_with_or_without_a_device)
EXPECT_EQ(to_string(*shard), "plugin-shard");
}
+TEST(HsmReaderTest, device_overrides_core_tag_by_reusing_it_under_its_own_device_id) {
+ // The device-id override convention - see the note on hsm::DeviceId: a device needing content different
+ // from a Core tag's shared meaning reuses that tag id under its own id instead of inventing a new tag.
+ constexpr hsm::DeviceId other_device_id = fake_device_id + 1;
+
+ const auto blob = make_container({
+ {make_pointer_entry(hsm::any_device_id, hsm::compiled_options_tag), "shared-options"},
+ {make_pointer_entry(fake_device_id, hsm::compiled_options_tag), "device-options"},
+ });
+ const auto reader = hsm::Reader::open(blob.data(), blob.size());
+ ASSERT_TRUE(reader.has_value());
+
+ // One tag id, two entries - they stay distinct because lookup is always by (device, tag), never tag alone.
+ const auto shared = reader->section(hsm::Tags::compiled_options);
+ ASSERT_TRUE(shared.has_value());
+ EXPECT_EQ(to_string(*shared), "shared-options");
+
+ const auto overridden = reader->section(fake_device_id, hsm::Tags::compiled_options);
+ ASSERT_TRUE(overridden.has_value());
+ EXPECT_EQ(to_string(*overridden), "device-options");
+
+ EXPECT_EQ(reader->count(hsm::Tags::compiled_options), 1U);
+ EXPECT_EQ(reader->count(fake_device_id, hsm::Tags::compiled_options), 1U);
+
+ // A device that published no override of its own finds nothing under its id because section(device, tag)
+ // performs an exact (device, tag) lookup; callers that want shared content must explicitly query any_device_id.
+ EXPECT_FALSE(reader->section(other_device_id, hsm::Tags::compiled_options).has_value());
+}
+
TEST(HsmReaderTest, section_returns_only_the_first_of_several_matching_entries) {
const auto shard_tag = hsm::SectionTag::make_device_tag(/*local_id=*/2, /*is_inline=*/false);
const auto blob = make_container({