From 56eeee93aafa2d23b2b3895a03c45d3b796faa3b Mon Sep 17 00:00:00 2001 From: MirceaDan99 Date: Thu, 12 Dec 2024 16:37:04 +0200 Subject: [PATCH] Prepare `BlobContainerAlignedBuffer` for `OV versioning metadata` --- .../include/intel_npu/common/blob_container.hpp | 15 ++++++++++----- .../src/compiler_adapter/src/plugin_graph.cpp | 2 +- src/plugins/intel_npu/src/plugin/src/plugin.cpp | 10 +++++++--- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/plugins/intel_npu/src/common/include/intel_npu/common/blob_container.hpp b/src/plugins/intel_npu/src/common/include/intel_npu/common/blob_container.hpp index fbceccb26824e0..13f833ddfccc88 100644 --- a/src/plugins/intel_npu/src/common/include/intel_npu/common/blob_container.hpp +++ b/src/plugins/intel_npu/src/common/include/intel_npu/common/blob_container.hpp @@ -46,16 +46,20 @@ class BlobContainerVector : public BlobContainer { class BlobContainerAlignedBuffer : public BlobContainer { public: - BlobContainerAlignedBuffer(const std::shared_ptr& blobSO, size_t offset) + BlobContainerAlignedBuffer(const std::shared_ptr& blobSO, + size_t ovHeaderOffset, + size_t metadataSize) : _ownershipBlob(blobSO), - _offset(offset) {} + _ovHeaderOffset(ovHeaderOffset), + _metadataSize(metadataSize) {} void* get_ptr() override { - return _ownershipBlob->get_ptr(_offset); + return _ownershipBlob->get_ptr(_ovHeaderOffset); } size_t size() const override { - return _ownershipBlob->size(); + // remove OV header offset and metadata from blob size + return _ownershipBlob->size() - _ovHeaderOffset - _metadataSize; } bool release_from_memory() override { @@ -64,7 +68,8 @@ class BlobContainerAlignedBuffer : public BlobContainer { private: std::shared_ptr _ownershipBlob; - size_t _offset; + size_t _ovHeaderOffset; + size_t _metadataSize; }; } // namespace intel_npu diff --git a/src/plugins/intel_npu/src/compiler_adapter/src/plugin_graph.cpp b/src/plugins/intel_npu/src/compiler_adapter/src/plugin_graph.cpp index a54d14c0462587..90e531d5b940c7 100644 --- a/src/plugins/intel_npu/src/compiler_adapter/src/plugin_graph.cpp +++ b/src/plugins/intel_npu/src/compiler_adapter/src/plugin_graph.cpp @@ -30,7 +30,7 @@ PluginGraph::PluginGraph(const std::shared_ptr& zeGraphExt, initialize(config); } -void PluginGraph::export_blob(std::ostream& stream) const { +size_t PluginGraph::export_blob(std::ostream& stream) const { stream.write(reinterpret_cast(_blobPtr->get_ptr()), _blobPtr->size()); if (!stream) { diff --git a/src/plugins/intel_npu/src/plugin/src/plugin.cpp b/src/plugins/intel_npu/src/plugin/src/plugin.cpp index 29b675b9baa659..26ed6e7347f40f 100644 --- a/src/plugins/intel_npu/src/plugin/src/plugin.cpp +++ b/src/plugins/intel_npu/src/plugin/src/plugin.cpp @@ -790,11 +790,15 @@ std::shared_ptr Plugin::import_model(std::istream& stream, c CompilerAdapterFactory compilerAdapterFactory; auto compiler = compilerAdapterFactory.getCompiler(_backends->getIEngineBackend(), localConfig); + auto storedMeta = read_metadata_from(stream); + if (!storedMeta->is_compatible()) { + OPENVINO_THROW("Incompatible blob version!"); + } + std::unique_ptr blobPtr; + auto graphSize = storedMeta->get_blob_size(); if (modelBuffer == nullptr) { - auto graphSize = getFileSize(stream); - std::vector blob(graphSize); stream.read(reinterpret_cast(blob.data()), graphSize); if (!stream) { @@ -804,7 +808,7 @@ std::shared_ptr Plugin::import_model(std::istream& stream, c blobPtr = std::make_unique(std::move(blob)); } else { - blobPtr = std::make_unique(modelBuffer, stream.tellg()); + blobPtr = std::make_unique(modelBuffer, stream.tellg(), graphSize); } auto graph = compiler->parse(std::move(blobPtr), localConfig);