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] Improve HSM reader to avoid data materialization on read. (#38971)
Pawel Raasz325ea916ecd3
diff --git a/src/inference/dev_api/openvino/runtime/hsm_reader.hpp b/src/inference/dev_api/openvino/runtime/hsm_reader.hppindex 480b384f4d3..da98ff8f5e4 100644--- a/src/inference/dev_api/openvino/runtime/hsm_reader.hpp+++ b/src/inference/dev_api/openvino/runtime/hsm_reader.hpp@@ -12,8 +12,8 @@#include <variant>#include <vector>-#include "openvino/runtime/hsm_format.hpp"#include "openvino/runtime/common.hpp"+#include "openvino/runtime/hsm_format.hpp"namespace ov::runtime::hsm {inline namespace v1 {@@ -129,10 +129,10 @@ SectionDecoder<T> make_section_decoder(std::function<std::optional<T>(ov::util::}/**- * @brief Per-entry hook a #Reader offers to plugins/devices: given one manifest entry's section content, a- * concrete handler self-selects by checking `(entry.device, entry.tag)` and returns whether it recognized- * and handled it. Per the unknown-tag rule on #SectionTag, an entry no handler recognizes must be skipped,- * never fail import.+ * @brief Per-entry hook a #Reader offers to plugins/devices: given one manifest entry and its #Section+ * handle, a concrete handler self-selects by checking `(entry.device, entry.tag)` and reports whether it+ * recognized and handled it. Per the unknown-tag rule on #SectionTag, an entry no handler recognizes must be+ * skipped, never fail import.*/class OPENVINO_RUNTIME_API ISectionReaderHandler {public:@@ -143,10 +143,10 @@ public:* @note Not itself a dispatch loop - a caller tries this once per candidate entry against each handler* it holds, in turn, until one returns true.* @param entry Manifest entry being considered - not necessarily one this extension owns.- * @param section Bounds-checked view of the payload.+ * @param section Lazy handle to @p entry's payload. The handler reads it if needed.* @return true if `(entry.device, entry.tag)` was recognized and handled, false otherwise.*/- virtual bool handle_section(const ManifestEntry& entry, ov::util::MemoryView section) = 0;+ virtual bool handle_section(const ManifestEntry& entry, const Section& section) = 0;};/**@@ -309,6 +309,7 @@ public:/*** @brief Dispatches every manifest entry - Core-owned sections included - to @p handlers, in order,* stopping at the first one that recognizes it. An entry no handler recognizes is silently skipped.+ * Handlers receive a lazy #Section and read its payload only if needed.** @param handlers The list of handlers to dispatch the manifest entries to.* @return The number of entries actually handled by some handler.@@ -317,9 +318,9 @@ public:private:Reader(std::optional<ov::util::MemoryView> buffer,- std::istream* stream,- std::streampos start,- std::vector<ManifestEntry> manifest) noexcept+ std::istream* stream,+ std::streampos start,+ std::vector<ManifestEntry> manifest) noexcept: m_buffer(buffer),m_stream(stream),m_start(start),diff --git a/src/inference/src/dev/hsm_reader.cpp b/src/inference/src/dev/hsm_reader.cppindex 2e7ea52e710..137b89fa15f 100644--- a/src/inference/src/dev/hsm_reader.cpp+++ b/src/inference/src/dev/hsm_reader.cpp@@ -167,19 +167,8 @@ size_t Reader::read_sections(const std::vector<ISectionReaderHandler*>& handlerssize_t handled = 0;for (const auto& entry : m_manifest) {const auto section = make_section(entry);- std::optional<std::vector<std::byte>> owned; // only materialized when a zero-copy view isn't possible- ov::util::MemoryView view;- if (const auto direct = section.view()) {- view = *direct;- } else {- owned = section.to_bytes();- if (!owned) {- continue;- }- view = {owned->data(), owned->size()};- }for (const auto& handler : handlers) {- if (handler != nullptr && handler->handle_section(entry, view)) {+ if (handler != nullptr && handler->handle_section(entry, section)) {++handled;break;}diff --git a/src/inference/tests/unit/hsm_reader.cpp b/src/inference/tests/unit/hsm_reader.cppindex a90d2680f1e..e91de32b856 100644--- a/src/inference/tests/unit/hsm_reader.cpp+++ b/src/inference/tests/unit/hsm_reader.cpp@@ -106,13 +106,18 @@ class RecordingHandler : public hsm::ISectionReaderHandler {public:RecordingHandler(hsm::DeviceId device, uint32_t tag_id) : m_device(device), m_tag_id(tag_id) {}- bool handle_section(const hsm::ManifestEntry& entry, ov::util::MemoryView section) override {+ bool handle_section(const hsm::ManifestEntry& entry, const hsm::Section& section) override {if (entry.device != m_device || entry.tag.id() != m_tag_id) {return false;}- last_payload = {reinterpret_cast<const char*>(section.data()), section.size()};- ++handled_count;- return true;++ if (const auto bytes = section.to_bytes()) {+ last_payload = to_string(*bytes);+ ++handled_count;+ return true;+ } else {+ return false; // matches the previous read-failure-skips-the-entry behavior+ }}std::string last_payload;@@ -222,10 +227,12 @@ TEST(ISectionReaderHandlerTest, recognizes_own_device_and_tag) {hsm::ManifestEntry entry{};entry.device = fake_device_id;entry.tag = hsm::SectionTag::make(/*id=*/42, /*is_inline=*/false);+ entry.size = 4;const std::byte payload[4] = {};+ const hsm::Section section(entry, ov::util::MemoryView{payload, 4});RecordingHandler handler(fake_device_id, /*tag_id=*/42);- EXPECT_TRUE(handler.handle_section(entry, ov::util::MemoryView{payload, 4}));+ EXPECT_TRUE(handler.handle_section(entry, section));EXPECT_EQ(handler.last_payload.size(), 4u);EXPECT_EQ(handler.handled_count, 1u);}@@ -235,8 +242,9 @@ TEST(ISectionReaderHandlerTest, skips_entry_it_does_not_own) {entry.device = fake_device_id + 1; // different deviceentry.tag = hsm::SectionTag::make(/*id=*/42, /*is_inline=*/false);+ const hsm::Section section(entry, ov::util::MemoryView{});RecordingHandler handler(fake_device_id, /*tag_id=*/42);- EXPECT_FALSE(handler.handle_section(entry, ov::util::MemoryView{}));+ EXPECT_FALSE(handler.handle_section(entry, section));EXPECT_EQ(handler.handled_count, 0u); // never called}