Skip to content

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

[core] HSM section view member improve (#38983)

Pawel Raaszfd1b5367f6d9
diff --git a/src/inference/dev_api/openvino/runtime/hsm_reader.hpp b/src/inference/dev_api/openvino/runtime/hsm_reader.hpp
index da98ff8f5e4..93830c4f71e 100644
--- a/src/inference/dev_api/openvino/runtime/hsm_reader.hpp
+++ b/src/inference/dev_api/openvino/runtime/hsm_reader.hpp
@@ -58,10 +58,9 @@ public:
/**
* @brief Zero-copy view of the whole section.
- * @return `std::nullopt` only for a pointer-mode section read from a stream-only #Reader - there's no
- * addressable memory to view; use #read() instead. Slice the returned view yourself for a sub-range.
+ * @return Default memory view to addressable memory or empty view if no such memory is available.
*/
- std::optional<ov::util::MemoryView> view() const noexcept;
+ ov::util::MemoryView view() const noexcept;
/**
* @brief Reads `[offset, offset + length)` into caller-owned @p destination - the only copy this type
@@ -119,11 +118,11 @@ using SectionDecoder = std::function<std::optional<T>(const Section&)>;
template <typename T>
SectionDecoder<T> make_section_decoder(std::function<std::optional<T>(ov::util::MemoryView)> parse) {
return [parse = std::move(parse)](const Section& section) -> std::optional<T> {
- if (const auto view = section.view()) {
- return parse(*view);
- } else {
+ if (const auto view = section.view(); view.empty()) {
const auto bytes = section.to_bytes();
return bytes ? parse(ov::util::MemoryView(bytes->data(), bytes->size())) : std::nullopt;
+ } else {
+ return parse(view);
}
};
}
diff --git a/src/inference/src/dev/hsm_reader.cpp b/src/inference/src/dev/hsm_reader.cpp
index 137b89fa15f..a14336f7f05 100644
--- a/src/inference/src/dev/hsm_reader.cpp
+++ b/src/inference/src/dev/hsm_reader.cpp
@@ -13,7 +13,7 @@
namespace ov::runtime::hsm {
inline namespace v1 {
-std::optional<ov::util::MemoryView> Section::view() const noexcept {
+ov::util::MemoryView Section::view() const noexcept {
if (m_entry.tag.is_inline()) {
return ov::util::MemoryView{reinterpret_cast<const std::byte*>(m_entry.inline_bytes.data()),
m_entry.inline_bytes.size()};
@@ -21,14 +21,13 @@ std::optional<ov::util::MemoryView> Section::view() const noexcept {
// m_source's alternatives are all nothrow-constructible and m_source is only ever set once via a constructor
// init-list, never reassigned - valueless_by_exception() is unreachable.
// coverity[UNCAUGHT_EXCEPT:FALSE]
- return std::visit(
- ov::util::VariantVisitor{[](const auto&) -> std::optional<ov::util::MemoryView> {
- return std::nullopt;
- },
- [](const ov::util::MemoryView& payload) -> std::optional<ov::util::MemoryView> {
- return payload;
- }},
- m_source);
+ return std::visit(ov::util::VariantVisitor{[](const auto&) -> ov::util::MemoryView {
+ return {};
+ },
+ [](const ov::util::MemoryView& payload) -> ov::util::MemoryView {
+ return payload;
+ }},
+ m_source);
}
}
diff --git a/src/inference/tests/unit/hsm_reader.cpp b/src/inference/tests/unit/hsm_reader.cpp
index e91de32b856..acdbb838f4e 100644
--- a/src/inference/tests/unit/hsm_reader.cpp
+++ b/src/inference/tests/unit/hsm_reader.cpp
@@ -197,8 +197,8 @@ TEST(HsmSectionTest, pointer_mode_reads_from_an_explicitly_supplied_buffer) {
const hsm::Section section(entry, view);
const auto direct = section.view();
- ASSERT_TRUE(direct.has_value());
- EXPECT_EQ(to_string(std::vector<std::byte>(direct->begin(), direct->end())), payload);
+ ASSERT_FALSE(direct.empty());
+ EXPECT_EQ(to_string(std::vector<std::byte>(direct.begin(), direct.end())), payload);
}
TEST(HsmSectionTest, read_rejects_a_null_destination) {
@@ -215,7 +215,7 @@ TEST(HsmSectionTest, pointer_mode_entry_built_with_the_inline_only_constructor_h
entry.size = 4;
const hsm::Section section(entry);
- EXPECT_FALSE(section.view().has_value());
+ EXPECT_TRUE(section.view().empty());
std::vector<std::byte> destination(4);
EXPECT_FALSE(section.read(destination.data()));
EXPECT_FALSE(section.to_bytes().has_value());
@@ -288,8 +288,8 @@ TEST(HsmReaderTest, model_id_section_reads_inline_payload) {
ASSERT_TRUE(section.has_value());
ASSERT_EQ(section->size(), 24u); // full inline capacity - see ManifestEntry::inline_bytes
const auto view = section->view();
- ASSERT_TRUE(view.has_value()); // inline is always viewable, regardless of source
- EXPECT_EQ(static_cast<uint8_t>(view->data()[0]), 0xAA);
+ ASSERT_FALSE(view.empty()); // inline is always viewable, regardless of source
+ EXPECT_EQ(static_cast<uint8_t>(view.data()[0]), 0xAA);
}
TEST(HsmReaderTest, model_section_reads_pointer_payload) {
@@ -335,8 +335,8 @@ TEST(HsmReaderTest, move_constructed_reader_still_reads_sections) {
const auto section = moved.section(hsm::model);
ASSERT_TRUE(section.has_value());
const auto view = section->view();
- ASSERT_TRUE(view.has_value());
- EXPECT_EQ(std::string(reinterpret_cast<const char*>(view->data()), view->size()), "compiled-model-bytes");
+ ASSERT_FALSE(view.empty());
+ EXPECT_EQ(std::string(reinterpret_cast<const char*>(view.data()), view.size()), "compiled-model-bytes");
}
TEST(HsmReaderTest, move_assignment_replaces_existing_reader) {
@@ -355,8 +355,8 @@ TEST(HsmReaderTest, move_assignment_replaces_existing_reader) {
const auto section = reader_a->section(hsm::model_id);
ASSERT_TRUE(section.has_value());
const auto view = section->view();
- ASSERT_TRUE(view.has_value());
- EXPECT_EQ(static_cast<uint8_t>(view->data()[0]), 0xBB);
+ ASSERT_FALSE(view.empty());
+ EXPECT_EQ(static_cast<uint8_t>(view.data()[0]), 0xBB);
}
TEST(HsmReaderTest, section_returns_nullopt_for_unknown_device_or_tag) {
@@ -551,13 +551,13 @@ TEST(HsmReaderTest, section_view_points_into_the_original_buffer) {
const auto section = reader->section(hsm::model);
ASSERT_TRUE(section.has_value());
const auto view = section->view();
- ASSERT_TRUE(view.has_value());
- EXPECT_EQ(std::string(reinterpret_cast<const char*>(view->data()), view->size()), "compiled-model-bytes");
+ ASSERT_FALSE(view.empty());
+ EXPECT_EQ(std::string(reinterpret_cast<const char*>(view.data()), view.size()), "compiled-model-bytes");
const auto* blob_begin = reinterpret_cast<const std::byte*>(blob.data());
const auto* blob_end = blob_begin + blob.size();
- EXPECT_GE(view->data(), blob_begin);
- EXPECT_LE(view->data() + view->size(), blob_end);
+ EXPECT_GE(view.data(), blob_begin);
+ EXPECT_LE(view.data() + view.size(), blob_end);
}
TEST(HsmReaderTest, section_view_available_even_for_inline_entries) {
@@ -568,9 +568,9 @@ TEST(HsmReaderTest, section_view_available_even_for_inline_entries) {
const auto section = reader->section(hsm::model_id);
ASSERT_TRUE(section.has_value());
const auto view = section->view();
- ASSERT_TRUE(view.has_value());
- ASSERT_EQ(view->size(), 24u);
- EXPECT_EQ(static_cast<uint8_t>(view->data()[0]), 0xAA);
+ ASSERT_FALSE(view.empty());
+ ASSERT_EQ(view.size(), 24u);
+ EXPECT_EQ(static_cast<uint8_t>(view.data()[0]), 0xAA);
}
// --- Section::read(): section()/view() are whole-section only; this is for reading arbitrary ranges one
@@ -691,8 +691,8 @@ TEST(HsmReaderTest, decode_with_an_explicit_device_finds_a_device_owned_section)
const hsm::SectionDecoder<std::string> decode_bytes = [](const hsm::Section& section) {
const auto view = section.view();
- return view ? std::make_optional(std::string(reinterpret_cast<const char*>(view->data()), view->size()))
- : std::nullopt;
+ return view.empty() ? std::nullopt
+ : std::make_optional(std::string(reinterpret_cast<const char*>(view.data()), view.size()));
};
const auto decoded = reader->decode(fake_device_id,
@@ -713,8 +713,8 @@ TEST(HsmReaderTest, decode_all_returns_every_matching_entry_decoded_in_order) {
const hsm::SectionDecoder<std::string> decode_shard = [](const hsm::Section& section) {
const auto view = section.view();
- return view ? std::make_optional(std::string(reinterpret_cast<const char*>(view->data()), view->size()))
- : std::nullopt;
+ return view.empty() ? std::nullopt
+ : std::make_optional(std::string(reinterpret_cast<const char*>(view.data()), view.size()));
};
const auto shards = reader->decode_all(fake_device_id, shard_tag.id(), decode_shard);
@@ -742,8 +742,8 @@ TEST(HsmReaderTest, decode_all_any_device_id_overload_matches_the_explicit_any_d
const hsm::SectionDecoder<std::string> decode_bytes = [](const hsm::Section& section) {
const auto view = section.view();
- return view ? std::make_optional(std::string(reinterpret_cast<const char*>(view->data()), view->size()))
- : std::nullopt;
+ return view.empty() ? std::nullopt
+ : std::make_optional(std::string(reinterpret_cast<const char*>(view.data()), view.size()));
};
const auto decoded = reader->decode_all(hsm::model, decode_bytes);
@@ -869,8 +869,8 @@ TEST(HsmReaderStreamTest, model_id_and_model_sections_read_correctly) {
ASSERT_TRUE(model_id.has_value());
ASSERT_EQ(model_id->size(), 24u);
const auto model_id_view = model_id->view(); // inline - always viewable, even without a buffer
- ASSERT_TRUE(model_id_view.has_value());
- EXPECT_EQ(static_cast<uint8_t>(model_id_view->data()[0]), 0xAA);
+ ASSERT_FALSE(model_id_view.empty());
+ EXPECT_EQ(static_cast<uint8_t>(model_id_view.data()[0]), 0xAA);
const auto model = reader->section(hsm::model);
ASSERT_TRUE(model.has_value());
@@ -888,7 +888,7 @@ TEST(HsmReaderStreamTest, section_read_reads_only_the_requested_slice_without_a_
// a section without also reading (or allocating for) the rest.
const auto section = reader->section(hsm::model);
ASSERT_TRUE(section.has_value());
- EXPECT_FALSE(section->view().has_value());
+ EXPECT_TRUE(section->view().empty());
std::vector<std::byte> destination(5);
ASSERT_TRUE(section->read(9, 5, destination.data()));
@@ -985,8 +985,8 @@ TEST(HsmReaderStreamTest, move_assignment_replaces_existing_reader) {
const auto section = reader_a->section(hsm::model_id);
ASSERT_TRUE(section.has_value());
const auto view = section->view();
- ASSERT_TRUE(view.has_value());
- EXPECT_EQ(static_cast<uint8_t>(view->data()[0]), 0xBB);
+ ASSERT_FALSE(view.empty());
+ EXPECT_EQ(static_cast<uint8_t>(view.data()[0]), 0xBB);
}
TEST(HsmReaderStreamTest, view_siblings_work_for_inline_but_not_pointer_mode_entries) {
@@ -998,13 +998,13 @@ TEST(HsmReaderStreamTest, view_siblings_work_for_inline_but_not_pointer_mode_ent
const auto model_id = reader->section(hsm::model_id);
ASSERT_TRUE(model_id.has_value());
const auto model_id_view = model_id->view();
- ASSERT_TRUE(model_id_view.has_value());
- ASSERT_EQ(model_id_view->size(), 24u);
- EXPECT_EQ(static_cast<uint8_t>(model_id_view->data()[0]), 0xAA);
+ ASSERT_FALSE(model_id_view.empty());
+ ASSERT_EQ(model_id_view.size(), 24u);
+ EXPECT_EQ(static_cast<uint8_t>(model_id_view.data()[0]), 0xAA);
const auto model = reader->section(hsm::model);
ASSERT_TRUE(model.has_value());
- EXPECT_FALSE(model->view().has_value());
+ EXPECT_TRUE(model->view().empty());
}
TEST(HsmReaderStreamTest, common_sections_absent_when_manifest_is_empty) {