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
[`intel_npu`] [`BlobContainer`] Remove `BlobContainer` and use `ov::Tensor` instead for compiled models (#30454)
Mircea-Aurelian Dan0d07a4f88f34
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.hppdeleted file mode 100644index 2f6b31aceac..00000000000--- a/src/plugins/intel_npu/src/common/include/intel_npu/common/blob_container.hpp+++ /dev/null@@ -1,81 +0,0 @@-// Copyright (C) 2018-2025 Intel Corporation-// SPDX-License-Identifier: Apache-2.0-//--#pragma once--#include <memory>-#include <vector>--#include "openvino/runtime/shared_buffer.hpp"--namespace intel_npu {--class BlobContainer {-public:- /**- * @brief Returns the address at the beginning of the blob.- */- virtual const void* get_ptr() const = 0;-- /**- * @brief Size of the blob.- */- virtual size_t size() const = 0;-- /**- * @brief Returns true if the blob can be deallocated from memory, false otherwise.- */- virtual bool release_from_memory() = 0;-- virtual ~BlobContainer() = default;-};--class BlobContainerVector : public BlobContainer {-public:- BlobContainerVector(std::vector<uint8_t> blob) : _blob(std::move(blob)) {}-- const void* get_ptr() const override {- return reinterpret_cast<const void*>(_blob.data());- }-- size_t size() const override {- return _blob.size();- }-- bool release_from_memory() override {- _blob.clear();- _blob.shrink_to_fit();- return true;- }--private:- std::vector<uint8_t> _blob;-};--class BlobContainerAlignedBuffer : public BlobContainer {-public:- BlobContainerAlignedBuffer(const std::shared_ptr<ov::AlignedBuffer>& blobSO, size_t ovHeaderOffset, uint64_t size)- : _size(size),- _ovHeaderOffset(ovHeaderOffset),- _blobSO(blobSO) {}-- const void* get_ptr() const override {- return _blobSO->get_ptr(_ovHeaderOffset);- }-- size_t size() const override {- return _size;- }-- bool release_from_memory() override {- return false;- }--private:- uint64_t _size;- size_t _ovHeaderOffset;- std::shared_ptr<ov::AlignedBuffer> _blobSO;-};--} // namespace intel_npudiff --git a/src/plugins/intel_npu/src/common/include/intel_npu/common/icompiler_adapter.hpp b/src/plugins/intel_npu/src/common/include/intel_npu/common/icompiler_adapter.hppindex a65a75d79db..4c71d75f1e3 100644--- a/src/plugins/intel_npu/src/common/include/intel_npu/common/icompiler_adapter.hpp+++ b/src/plugins/intel_npu/src/common/include/intel_npu/common/icompiler_adapter.hpp@@ -12,7 +12,7 @@ class ICompilerAdapter {public:virtual std::shared_ptr<IGraph> compile(const std::shared_ptr<const ov::Model>& model,const Config& config) const = 0;- virtual std::shared_ptr<IGraph> parse(std::unique_ptr<BlobContainer> blobPtr, const Config& config) const = 0;+ virtual std::shared_ptr<IGraph> parse(ov::Tensor blob, bool blobAllocatedByPlugin, const Config& config) const = 0;virtual ov::SupportedOpsMap query(const std::shared_ptr<const ov::Model>& model, const Config& config) const = 0;virtual uint32_t get_version() const = 0;virtual std::vector<std::string> get_supported_options() const = 0;diff --git a/src/plugins/intel_npu/src/common/include/intel_npu/common/igraph.hpp b/src/plugins/intel_npu/src/common/include/intel_npu/common/igraph.hppindex 46929354895..61910a817ff 100644--- a/src/plugins/intel_npu/src/common/include/intel_npu/common/igraph.hpp+++ b/src/plugins/intel_npu/src/common/include/intel_npu/common/igraph.hpp@@ -8,7 +8,6 @@#include <mutex>#include <vector>-#include "intel_npu/common/blob_container.hpp"#include "intel_npu/network_metadata.hpp"#include "intel_npu/utils/zero/zero_utils.hpp"#include "intel_npu/utils/zero/zero_wrappers.hpp"@@ -18,10 +17,7 @@ namespace intel_npu {class IGraph : public std::enable_shared_from_this<IGraph> {public:- IGraph(ze_graph_handle_t handle,- NetworkMetadata metadata,- const Config& config,- std::unique_ptr<BlobContainer> blobPtr);+ IGraph(ze_graph_handle_t handle, NetworkMetadata metadata, const Config& config, std::optional<ov::Tensor> blob);virtual size_t export_blob(std::ostream& stream) const = 0;@@ -91,7 +87,7 @@ protected:// first inference starts runningstd::mutex _mutex;- std::unique_ptr<BlobContainer> _blobPtr;+ std::optional<ov::Tensor> _blob;uint32_t _unique_id = 0;uint32_t _last_submitted_id = 0;diff --git a/src/plugins/intel_npu/src/common/src/igraph.cpp b/src/plugins/intel_npu/src/common/src/igraph.cppindex f775a653af8..513cea7a81d 100644--- a/src/plugins/intel_npu/src/common/src/igraph.cpp+++ b/src/plugins/intel_npu/src/common/src/igraph.cpp@@ -13,13 +13,10 @@ constexpr std::size_t DEFAULT_BATCH_SIZE = 1;namespace intel_npu {-IGraph::IGraph(ze_graph_handle_t handle,- NetworkMetadata metadata,- const Config& config,- std::unique_ptr<BlobContainer> blobPtr)+IGraph::IGraph(ze_graph_handle_t handle, NetworkMetadata metadata, const Config& config, std::optional<ov::Tensor> blob): _handle(handle),_metadata(std::move(metadata)),- _blobPtr(std::move(blobPtr)),+ _blob(std::move(blob)),_logger("IGraph", config.get<LOG_LEVEL>()) {}const NetworkMetadata& IGraph::get_metadata() const {diff --git a/src/plugins/intel_npu/src/compiler_adapter/include/driver_compiler_adapter.hpp b/src/plugins/intel_npu/src/compiler_adapter/include/driver_compiler_adapter.hppindex 311afca860f..9316a43e001 100644--- a/src/plugins/intel_npu/src/compiler_adapter/include/driver_compiler_adapter.hpp+++ b/src/plugins/intel_npu/src/compiler_adapter/include/driver_compiler_adapter.hpp@@ -23,7 +23,7 @@ public:std::shared_ptr<IGraph> compile(const std::shared_ptr<const ov::Model>& model, const Config& config) const override;- std::shared_ptr<IGraph> parse(std::unique_ptr<BlobContainer> blobPtr, const Config& config) const override;+ std::shared_ptr<IGraph> parse(ov::Tensor blob, bool blobAllocatedByPlugin, const Config& config) const override;ov::SupportedOpsMap query(const std::shared_ptr<const ov::Model>& model, const Config& config) const override;diff --git a/src/plugins/intel_npu/src/compiler_adapter/include/graph.hpp b/src/plugins/intel_npu/src/compiler_adapter/include/graph.hppindex 272204ea7b1..8a31af4b466 100644--- a/src/plugins/intel_npu/src/compiler_adapter/include/graph.hpp+++ b/src/plugins/intel_npu/src/compiler_adapter/include/graph.hpp@@ -22,7 +22,8 @@ public:const std::shared_ptr<ZeroInitStructsHolder>& zeroInitStruct,ze_graph_handle_t graphHandle,NetworkMetadata metadata,- std::unique_ptr<BlobContainer> blobPtr,+ std::optional<ov::Tensor> blob,+ bool blobAllocatedByPlugin,const Config& config,const ov::SoPtr<ICompiler>& compiler = {nullptr});@@ -43,13 +44,13 @@ private:std::shared_ptr<ZeGraphExtWrappers> _zeGraphExt;std::shared_ptr<ZeroInitStructsHolder> _zeroInitStruct;- const ov::SoPtr<ICompiler> _compiler;-- Logger _logger;-// In the case of the import path, the blob is released after graph initialization so it can not be any longer// exportedbool _blobIsReleased = false;+ bool _blobAllocatedByPlugin = false;++ const ov::SoPtr<ICompiler> _compiler;+ Logger _logger;};} // namespace intel_npudiff --git a/src/plugins/intel_npu/src/compiler_adapter/include/plugin_compiler_adapter.hpp b/src/plugins/intel_npu/src/compiler_adapter/include/plugin_compiler_adapter.hppindex 742286a8778..c5bd4be79a7 100644--- a/src/plugins/intel_npu/src/compiler_adapter/include/plugin_compiler_adapter.hpp+++ b/src/plugins/intel_npu/src/compiler_adapter/include/plugin_compiler_adapter.hpp@@ -21,7 +21,7 @@ public:std::shared_ptr<IGraph> compile(const std::shared_ptr<const ov::Model>& model, const Config& config) const override;- std::shared_ptr<IGraph> parse(std::unique_ptr<BlobContainer> blobPtr, const Config& config) const override;+ std::shared_ptr<IGraph> parse(ov::Tensor blob, bool blobAllocatedByPlugin, const Config& config) const override;ov::SupportedOpsMap query(const std::shared_ptr<const ov::Model>& model, const Config& config) const override;diff --git a/src/plugins/intel_npu/src/compiler_adapter/src/driver_compiler_adapter.cpp b/src/plugins/intel_npu/src/compiler_adapter/src/driver_compiler_adapter.cppindex 12c810f6ce4..848ae74a614 100644--- a/src/plugins/intel_npu/src/compiler_adapter/src/driver_compiler_adapter.cpp+++ b/src/plugins/intel_npu/src/compiler_adapter/src/driver_compiler_adapter.cpp@@ -191,16 +191,23 @@ std::shared_ptr<IGraph> DriverCompilerAdapter::compile(const std::shared_ptr<conauto networkMeta = _zeGraphExt->getNetworkMeta(graphHandle);networkMeta.name = model->get_friendly_name();- return std::make_shared<Graph>(_zeGraphExt, _zeroInitStruct, graphHandle, std::move(networkMeta), nullptr, config);+ return std::make_shared<Graph>(_zeGraphExt,+ _zeroInitStruct,+ graphHandle,+ std::move(networkMeta),+ /* blob = */ std::nullopt,+ /* blobAllocatedByPlugin = */ false,+ config);}-std::shared_ptr<IGraph> DriverCompilerAdapter::parse(std::unique_ptr<BlobContainer> blobPtr,+std::shared_ptr<IGraph> DriverCompilerAdapter::parse(ov::Tensor blob,+ bool blobAllocatedByPlugin,const Config& config) const {OV_ITT_TASK_CHAIN(PARSE_BLOB, itt::domains::NPUPlugin, "DriverCompilerAdapter", "parse");_logger.debug("parse start");ze_graph_handle_t graphHandle =- _zeGraphExt->getGraphHandle(*reinterpret_cast<const uint8_t*>(blobPtr->get_ptr()), blobPtr->size());+ _zeGraphExt->getGraphHandle(*reinterpret_cast<const uint8_t*>(blob.data()), blob.get_byte_size());_logger.debug("parse end");OV_ITT_TASK_NEXT(PARSE_BLOB, "getNetworkMeta");@@ -210,7 +217,8 @@ std::shared_ptr<IGraph> DriverCompilerAdapter::parse(std::unique_ptr<BlobContain_zeroInitStruct,graphHandle,std::move(networkMeta),- std::move(blobPtr),+ std::move(blob),+ blobAllocatedByPlugin,config);}diff --git a/src/plugins/intel_npu/src/compiler_adapter/src/graph.cpp b/src/plugins/intel_npu/src/compiler_adapter/src/graph.cppindex 0fa1fa79317..06fffee11dc 100644--- a/src/plugins/intel_npu/src/compiler_adapter/src/graph.cpp+++ b/src/plugins/intel_npu/src/compiler_adapter/src/graph.cpp@@ -6,6 +6,7 @@#include "intel_npu/config/options.hpp"#include "intel_npu/utils/zero/zero_api.hpp"+#include "openvino/runtime/make_tensor.hpp"namespace intel_npu {@@ -13,12 +14,14 @@ Graph::Graph(const std::shared_ptr<ZeGraphExtWrappers>& zeGraphExt,const std::shared_ptr<ZeroInitStructsHolder>& zeroInitStruct,ze_graph_handle_t graphHandle,NetworkMetadata metadata,- std::unique_ptr<BlobContainer> blobPtr,+ std::optional<ov::Tensor> blob,+ bool blobAllocatedByPlugin,const Config& config,const ov::SoPtr<ICompiler>& compiler)- : IGraph(graphHandle, std::move(metadata), config, std::move(blobPtr)),+ : IGraph(graphHandle, std::move(metadata), config, std::move(blob)),_zeGraphExt(zeGraphExt),_zeroInitStruct(zeroInitStruct),+ _blobAllocatedByPlugin(blobAllocatedByPlugin),_compiler(compiler),_logger("Graph", config.get<LOG_LEVEL>()) {if (!config.get<CREATE_EXECUTOR>() || config.get<DEFER_WEIGHTS_LOAD>()) {@@ -39,11 +42,12 @@ size_t Graph::export_blob(std::ostream& stream) const {"its lifetime.");}- if (_blobPtr == nullptr) { // when compiling the model using Compiler in Driver, the blob is handled by the driver+ if (_blob ==+ std::nullopt) { // when compiling the model using Compiler in Driver, the blob is handled by the driver_zeGraphExt->getGraphBinary(_handle, blob, blobPtr, blobSize);} else { // in all other cases, the blob is handled by the plugin- blobPtr = static_cast<const uint8_t*>(_blobPtr->get_ptr());- blobSize = _blobPtr->size();+ blobPtr = static_cast<const uint8_t*>(_blob->data());+ blobSize = _blob->get_byte_size();}stream.write(reinterpret_cast<const char*>(blobPtr), blobSize);@@ -73,9 +77,9 @@ std::vector<ov::ProfilingInfo> Graph::process_profiling_output(const std::vectorOPENVINO_THROW("Profiling post-processing is not supported.");}- std::vector<uint8_t> blob(_blobPtr->size());- blob.assign(reinterpret_cast<const uint8_t*>(_blobPtr->get_ptr()),- reinterpret_cast<const uint8_t*>(_blobPtr->get_ptr()) + _blobPtr->size());+ std::vector<uint8_t> blob(_blob->get_byte_size());+ blob.assign(reinterpret_cast<const uint8_t*>(_blob->data()),+ reinterpret_cast<const uint8_t*>(_blob->data()) + _blob->get_byte_size());return _compiler->process_profiling_output(profData, blob, config);}@@ -163,7 +167,11 @@ void Graph::initialize(const Config& config) {}bool Graph::release_blob(const Config& config) {- if (_blobPtr == nullptr || _zeroInitStruct->getGraphDdiTable().version() < ZE_GRAPH_EXT_VERSION_1_8 ||+ if (!_blobAllocatedByPlugin) {+ return false;+ }++ if (_blob == std::nullopt || _zeroInitStruct->getGraphDdiTable().version() < ZE_GRAPH_EXT_VERSION_1_8 ||config.get<PERF_COUNT>()) {return false;}@@ -176,10 +184,7 @@ bool Graph::release_blob(const Config& config) {return false;}- if (!_blobPtr->release_from_memory()) {- return false;- }-+ _blob = std::nullopt;_logger.debug("Blob is released");return true;diff --git a/src/plugins/intel_npu/src/compiler_adapter/src/plugin_compiler_adapter.cpp b/src/plugins/intel_npu/src/compiler_adapter/src/plugin_compiler_adapter.cppindex 0dc23d261d5..a47b31a4ea8 100644--- a/src/plugins/intel_npu/src/compiler_adapter/src/plugin_compiler_adapter.cpp+++ b/src/plugins/intel_npu/src/compiler_adapter/src/plugin_compiler_adapter.cpp@@ -91,12 +91,6 @@ std::shared_ptr<IGraph> PluginCompilerAdapter::compile(const std::shared_ptr<conimpl._so = std::move(sharedCompiledNetwork);tensor = ov::make_tensor(impl);- auto modelBuffer = std::make_shared<ov::SharedBuffer<ov::Tensor>>(reinterpret_cast<char*>(tensor.data()),- tensor.get_byte_size(),- tensor);-- auto blobPtr = std::make_unique<BlobContainerAlignedBuffer>(modelBuffer, 0, tensor.get_byte_size());-ze_graph_handle_t graphHandle = nullptr;if (_zeGraphExt) {@@ -104,7 +98,7 @@ std::shared_ptr<IGraph> PluginCompilerAdapter::compile(const std::shared_ptr<con// networktry {graphHandle =- _zeGraphExt->getGraphHandle(*reinterpret_cast<const uint8_t*>(blobPtr->get_ptr()), blobPtr->size());+ _zeGraphExt->getGraphHandle(*reinterpret_cast<const uint8_t*>(tensor.data()), tensor.get_byte_size());} catch (...) {_logger.info("Failed to obtain the level zero graph handle. Inference requests for this model are not ""allowed. Only exports are available");@@ -115,19 +109,21 @@ std::shared_ptr<IGraph> PluginCompilerAdapter::compile(const std::shared_ptr<con_zeroInitStruct,graphHandle,std::move(networkDesc.metadata),- std::move(blobPtr),+ std::move(tensor),+ /* blobAllocatedByPlugin = */ false,config,_compiler);}-std::shared_ptr<IGraph> PluginCompilerAdapter::parse(std::unique_ptr<BlobContainer> blobPtr,+std::shared_ptr<IGraph> PluginCompilerAdapter::parse(ov::Tensor blob,+ bool blobAllocatedByPlugin,const Config& config) const {OV_ITT_TASK_CHAIN(PARSE_BLOB, itt::domains::NPUPlugin, "PluginCompilerAdapter", "parse");_logger.debug("parse start");- std::vector<uint8_t> network(blobPtr->size());- network.assign(reinterpret_cast<const uint8_t*>(blobPtr->get_ptr()),- reinterpret_cast<const uint8_t*>(blobPtr->get_ptr()) + blobPtr->size());+ std::vector<uint8_t> network(blob.get_byte_size());+ network.assign(reinterpret_cast<const uint8_t*>(blob.data()),+ reinterpret_cast<const uint8_t*>(blob.data()) + blob.get_byte_size());auto networkMeta = _compiler->parse(network, config);network.clear();network.shrink_to_fit();@@ -136,15 +132,15 @@ std::shared_ptr<IGraph> PluginCompilerAdapter::parse(std::unique_ptr<BlobContainze_graph_handle_t graphHandle = nullptr;if (_zeGraphExt) {- graphHandle =- _zeGraphExt->getGraphHandle(*reinterpret_cast<const uint8_t*>(blobPtr->get_ptr()), blobPtr->size());+ graphHandle = _zeGraphExt->getGraphHandle(*reinterpret_cast<const uint8_t*>(blob.data()), blob.get_byte_size());}return std::make_shared<Graph>(_zeGraphExt,_zeroInitStruct,graphHandle,std::move(networkMeta),- std::move(blobPtr),+ std::move(blob),+ blobAllocatedByPlugin,config,_compiler);}diff --git a/src/plugins/intel_npu/src/plugin/src/plugin.cpp b/src/plugins/intel_npu/src/plugin/src/plugin.cppindex b285e52e62c..c726aeee1ea 100644--- a/src/plugins/intel_npu/src/plugin/src/plugin.cpp+++ b/src/plugins/intel_npu/src/plugin/src/plugin.cpp@@ -525,26 +525,38 @@ std::shared_ptr<ov::ICompiledModel> Plugin::import_model(std::istream& origStreaOV_ITT_SCOPED_TASK(itt::domains::NPUPlugin, "Plugin::import_model");ov::AnyMap npu_plugin_properties = properties;- std::shared_ptr<ov::AlignedBuffer> modelBuffer;- ov::SharedStreamBuffer buffer = {nullptr, 0};- std::istream stream{origStream.rdbuf()};+ ov::Tensor tensor;+ bool blobAllocatedByPlugin = false;+// ov::hint::compiled_blob has no corresponding "Config" implementation thus we need to remove it from the// list of propertiesif (auto blob_it = npu_plugin_properties.find(ov::hint::compiled_blob.name());blob_it != npu_plugin_properties.end()) {- auto compiled_blob = blob_it->second.as<ov::Tensor>();- modelBuffer = std::make_shared<ov::SharedBuffer<ov::Tensor>>(reinterpret_cast<char*>(compiled_blob.data()),- compiled_blob.get_byte_size(),- compiled_blob);- buffer = ov::SharedStreamBuffer(reinterpret_cast<char*>(compiled_blob.data()), compiled_blob.get_byte_size());- stream.rdbuf(&buffer);+ auto compiledBlob = blob_it->second.as<ov::Tensor>();if (auto loadedFromCache = npu_plugin_properties.find(ov::loaded_from_cache.name());loadedFromCache != npu_plugin_properties.end() && loadedFromCache->second.as<bool>() != false) {- stream.seekg(origStream.tellg(), std::ios::cur); // skip OV header in case of cached blob+ tensor = ov::Tensor(+ compiledBlob,+ ov::Coordinate{static_cast<size_t>(origStream.tellg())},+ ov::Coordinate{compiledBlob.get_byte_size()}); // ROI tensor to skip OV header in case of cached blob+ } else {+ tensor = compiledBlob;}npu_plugin_properties.erase(blob_it);+ } else { // fallback creating plugin handled buffer for blob+ size_t streamSize = MetadataBase::getFileSize(origStream);+ tensor = ov::Tensor(ov::element::u8, ov::Shape{streamSize});+ blobAllocatedByPlugin = true;+ origStream.read(reinterpret_cast<char*>(tensor.data()), tensor.get_byte_size());+ if (!origStream) {+ OPENVINO_THROW("Failed to read data from stream!");+ }}+ ov::SharedStreamBuffer buffer =+ ov::SharedStreamBuffer(reinterpret_cast<char*>(tensor.data()), tensor.get_byte_size());+ std::istream stream{&buffer};+// If was exported via NPUWauto stream_start_pos = stream.tellg();ov::npuw::s11n::IndicatorType serialization_indicator;@@ -598,35 +610,17 @@ std::shared_ptr<ov::ICompiledModel> Plugin::import_model(std::istream& origStreastd::shared_ptr<ov::ICompiledModel> compiledModel;try {- uint64_t graphSize;const bool skipCompatibility = localConfig.get<DISABLE_VERSION_CHECK>();if (!skipCompatibility) {auto storedMeta = read_metadata_from(stream);if (!storedMeta->is_compatible()) {OPENVINO_THROW("Incompatible blob version!");}- graphSize = storedMeta->get_blob_size();- } else {- _logger.info("Blob compatibility check skipped.");- graphSize = MetadataBase::getFileSize(stream);- }-- std::unique_ptr<BlobContainer> blobPtr;-- if (modelBuffer == nullptr) {- std::vector<uint8_t> blob(graphSize);- stream.read(reinterpret_cast<char*>(blob.data()), graphSize);- if (!stream) {- OPENVINO_THROW("Failed to read data from stream!");- }- _logger.debug("Successfully read %zu bytes into blob.", graphSize);-- blobPtr = std::make_unique<BlobContainerVector>(std::move(blob));- } else {- blobPtr = std::make_unique<BlobContainerAlignedBuffer>(modelBuffer, stream.tellg(), graphSize);+ tensor = ov::Tensor(tensor,+ ov::Coordinate{0},+ ov::Coordinate{storedMeta->get_blob_size()}); // ROI tensor to skip NPU plugin metadata}-- auto graph = compiler->parse(std::move(blobPtr), localConfig);+ auto graph = compiler->parse(std::move(tensor), blobAllocatedByPlugin, localConfig);graph->update_network_name("net" + std::to_string(_compiledModelLoadCounter++));const std::shared_ptr<ov::Model> modelDummy =