Skip to content

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

[`intel_npu`] [`import_model API`] [`ov::Tensor`] Support new `import_model(tensor, ...) API` (#30496)

Mircea-Aurelian Dan3ab1b4a85869
diff --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.hpp
index 108e3baee99..9bf0cc8ce2b 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
@@ -34,8 +34,6 @@ public:
* be loaded on the device depending on the provided configuration (if "NPU_DEFER_WEIGHTS_LOAD" is off).
*
* @param mainBlob The core binary object to be parsed and used to build the "Graph" object.
- * @param blobAllocatedByPlugin Information required for managing the memory region allocated for the compiled
- * model.
* @param config Used to influence the downstream flow of the implementation based on preferences.
* @param initBlobs Optional. If provided, the "weights separation" flow is enabled and the binary objects
* corresponding to the init schedules will be parsed as well.
@@ -46,7 +44,6 @@ public:
*/
virtual std::shared_ptr<IGraph> parse(
ov::Tensor mainBlob,
- const bool blobAllocatedByPlugin,
const Config& config,
std::optional<std::vector<ov::Tensor>> initBlobs = std::nullopt,
const std::optional<std::shared_ptr<const ov::Model>>& model = std::nullopt) const = 0;
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.hpp
index b58630217a4..140085cef7c 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
@@ -27,7 +27,6 @@ public:
std::shared_ptr<IGraph> parse(
ov::Tensor mainBlob,
- const bool blobAllocatedByPlugin,
const Config& config,
std::optional<std::vector<ov::Tensor>> initBlobs = std::nullopt,
const std::optional<std::shared_ptr<const ov::Model>>& model = std::nullopt) 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.hpp
index fad630c4766..0856e534229 100644
--- a/src/plugins/intel_npu/src/compiler_adapter/include/graph.hpp
+++ b/src/plugins/intel_npu/src/compiler_adapter/include/graph.hpp
@@ -23,7 +23,7 @@ public:
ze_graph_handle_t graphHandle,
NetworkMetadata metadata,
std::optional<ov::Tensor> blob,
- bool blobAllocatedByPlugin,
+ const bool persistentBlob,
const Config& config,
const ov::SoPtr<ICompiler>& compiler = {nullptr},
const bool calledFromWeightlessGraph = false);
@@ -49,7 +49,7 @@ protected:
// In the case of the import path, the blob is released after graph initialization so it can not be any longer
// exported
bool _blobIsReleased = false;
- bool _blobAllocatedByPlugin = false;
+ bool _persistentBlob = false;
const ov::SoPtr<ICompiler> _compiler;
Logger _logger;
diff --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.hpp
index 534332fec76..b62cb91b297 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
@@ -25,7 +25,6 @@ public:
std::shared_ptr<IGraph> parse(
ov::Tensor mainBlob,
- const bool blobAllocatedByPlugin,
const Config& config,
std::optional<std::vector<ov::Tensor>> initBlobs = std::nullopt,
const std::optional<std::shared_ptr<const ov::Model>>& model = std::nullopt) const override;
diff --git a/src/plugins/intel_npu/src/compiler_adapter/include/weightless_graph.hpp b/src/plugins/intel_npu/src/compiler_adapter/include/weightless_graph.hpp
index 4e772fa9e22..5468735b214 100644
--- a/src/plugins/intel_npu/src/compiler_adapter/include/weightless_graph.hpp
+++ b/src/plugins/intel_npu/src/compiler_adapter/include/weightless_graph.hpp
@@ -23,7 +23,7 @@ class WeightlessGraph final : public Graph {
public:
WeightlessGraph(const std::shared_ptr<ZeGraphExtWrappers>& zeGraphExt,
const std::shared_ptr<ZeroInitStructsHolder>& zeroInitStruct,
- const bool blobAllocatedByPlugin,
+ const bool persistentBlob,
ze_graph_handle_t mainGraphHandle,
NetworkMetadata mainMetadata,
std::optional<ov::Tensor> mainBlob,
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.cpp
index 6ef6717a95c..29dd80f298f 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
@@ -247,7 +247,7 @@ std::shared_ptr<IGraph> DriverCompilerAdapter::compile(const std::shared_ptr<con
graphHandle,
std::move(networkMeta),
/* blob = */ std::nullopt,
- /* blobAllocatedByPlugin = */ false,
+ /* persistentBlob = */ false,
config);
}
@@ -347,7 +347,7 @@ std::shared_ptr<IGraph> DriverCompilerAdapter::compileWS(const std::shared_ptr<o
return std::make_shared<WeightlessGraph>(_zeGraphExt,
_zeroInitStruct,
- /* blobAllocatedByPlugin = */ false,
+ /* persistentBlob = */ false,
mainGraphHandle,
std::move(mainNetworkMetadata),
/* mainBlob = */ std::nullopt,
@@ -360,7 +360,6 @@ std::shared_ptr<IGraph> DriverCompilerAdapter::compileWS(const std::shared_ptr<o
std::shared_ptr<IGraph> DriverCompilerAdapter::parse(
ov::Tensor mainBlob,
- const bool blobAllocatedByPlugin,
const Config& config,
std::optional<std::vector<ov::Tensor>> initBlobs,
const std::optional<std::shared_ptr<const ov::Model>>& model) const {
@@ -374,13 +373,15 @@ std::shared_ptr<IGraph> DriverCompilerAdapter::parse(
OV_ITT_TASK_NEXT(PARSE_BLOB, "getNetworkMeta");
auto networkMeta = _zeGraphExt->getNetworkMeta(graphHandle);
+ const bool persistentBlob = config.get<LOADED_FROM_CACHE>();
+
if (!initBlobs.has_value()) {
return std::make_shared<Graph>(_zeGraphExt,
_zeroInitStruct,
graphHandle,
std::move(networkMeta),
std::move(mainBlob),
- blobAllocatedByPlugin,
+ persistentBlob,
config);
}
@@ -397,7 +398,7 @@ std::shared_ptr<IGraph> DriverCompilerAdapter::parse(
return std::make_shared<WeightlessGraph>(_zeGraphExt,
_zeroInitStruct,
- blobAllocatedByPlugin,
+ persistentBlob,
graphHandle,
std::move(networkMeta),
std::move(mainBlob),
diff --git a/src/plugins/intel_npu/src/compiler_adapter/src/graph.cpp b/src/plugins/intel_npu/src/compiler_adapter/src/graph.cpp
index ca8cc2ffbcf..d45ce2fc493 100644
--- a/src/plugins/intel_npu/src/compiler_adapter/src/graph.cpp
+++ b/src/plugins/intel_npu/src/compiler_adapter/src/graph.cpp
@@ -15,14 +15,14 @@ Graph::Graph(const std::shared_ptr<ZeGraphExtWrappers>& zeGraphExt,
ze_graph_handle_t graphHandle,
NetworkMetadata metadata,
std::optional<ov::Tensor> blob,
- bool blobAllocatedByPlugin,
+ const bool persistentBlob,
const Config& config,
const ov::SoPtr<ICompiler>& compiler,
const bool calledFromWeightlessGraph)
: IGraph(graphHandle, std::move(metadata), config, std::move(blob)),
_zeGraphExt(zeGraphExt),
_zeroInitStruct(zeroInitStruct),
- _blobAllocatedByPlugin(blobAllocatedByPlugin),
+ _persistentBlob(persistentBlob),
_compiler(compiler),
_logger("Graph", config.get<LOG_LEVEL>()) {
if (!config.get<CREATE_EXECUTOR>() || config.get<DEFER_WEIGHTS_LOAD>()) {
@@ -42,8 +42,7 @@ std::pair<uint64_t, std::optional<std::vector<uint64_t>>> Graph::export_blob(std
std::vector<uint8_t> blobVec; // plugin needs to keep a copy of the blob for older drivers
if (_blobIsReleased) {
- OPENVINO_THROW("Model was optimized away. Try importing it using `ov::hint::compiled_blob` property to extend "
- "its lifetime.");
+ OPENVINO_THROW("Model was imported and released after initialization. Model export is not allowed anymore.");
}
if (_blob ==
@@ -177,7 +176,7 @@ void Graph::initialize(const Config& config) {
}
bool Graph::release_blob(const Config& config) {
- if (!_blobAllocatedByPlugin) {
+ if (_persistentBlob) {
return false;
}
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.cpp
index 8a194457f21..ae32c1b3629 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
@@ -117,7 +117,7 @@ std::shared_ptr<IGraph> PluginCompilerAdapter::compile(const std::shared_ptr<con
graphHandle,
std::move(networkDesc.metadata),
std::move(tensor),
- /* blobAllocatedByPlugin = */ false,
+ /* persistentBlob = */ true,
config,
_compiler);
}
@@ -237,7 +237,7 @@ std::shared_ptr<IGraph> PluginCompilerAdapter::compileWS(const std::shared_ptr<o
return std::make_shared<WeightlessGraph>(_zeGraphExt,
_zeroInitStruct,
- /* blobAllocatedByPlugin = */ false,
+ /* persistentBlob = */ true,
mainGraphHandle,
std::move(mainNetworkDescription->metadata),
std::move(tensorMain),
@@ -251,7 +251,6 @@ std::shared_ptr<IGraph> PluginCompilerAdapter::compileWS(const std::shared_ptr<o
std::shared_ptr<IGraph> PluginCompilerAdapter::parse(
ov::Tensor mainBlob,
- const bool blobAllocatedByPlugin,
const Config& config,
std::optional<std::vector<ov::Tensor>> initBlobs,
const std::optional<std::shared_ptr<const ov::Model>>& model) const {
@@ -274,13 +273,15 @@ std::shared_ptr<IGraph> PluginCompilerAdapter::parse(
_logger.debug("main schedule parse end");
+ const bool persistentBlob = config.get<LOADED_FROM_CACHE>();
+
if (!initBlobs.has_value()) {
return std::make_shared<Graph>(_zeGraphExt,
_zeroInitStruct,
graphHandle,
std::move(networkMeta),
std::move(mainBlob),
- blobAllocatedByPlugin,
+ persistentBlob,
config,
_compiler);
}
@@ -306,7 +307,7 @@ std::shared_ptr<IGraph> PluginCompilerAdapter::parse(
_logger.debug("init schedules parse end");
return std::make_shared<WeightlessGraph>(_zeGraphExt,
_zeroInitStruct,
- blobAllocatedByPlugin,
+ persistentBlob,
graphHandle,
std::move(networkMeta),
std::move(mainBlob),
diff --git a/src/plugins/intel_npu/src/compiler_adapter/src/weightless_graph.cpp b/src/plugins/intel_npu/src/compiler_adapter/src/weightless_graph.cpp
index e9103f9a20c..a74038ebf16 100644
--- a/src/plugins/intel_npu/src/compiler_adapter/src/weightless_graph.cpp
+++ b/src/plugins/intel_npu/src/compiler_adapter/src/weightless_graph.cpp
@@ -147,7 +147,7 @@ void merge_two_maps(std::unordered_map<std::string, std::shared_ptr<ov::ITensor>
WeightlessGraph::WeightlessGraph(const std::shared_ptr<ZeGraphExtWrappers>& zeGraphExt,
const std::shared_ptr<ZeroInitStructsHolder>& zeroInitStruct,
- const bool blobAllocatedByPlugin,
+ const bool persistentBlob,
ze_graph_handle_t mainGraphHandle,
NetworkMetadata mainMetadata,
std::optional<ov::Tensor> mainBlob,
@@ -162,7 +162,7 @@ WeightlessGraph::WeightlessGraph(const std::shared_ptr<ZeGraphExtWrappers>& zeGr
mainGraphHandle,
std::move(mainMetadata),
std::move(mainBlob),
- blobAllocatedByPlugin,
+ persistentBlob,
config,
compiler,
true),
@@ -571,7 +571,7 @@ void WeightlessGraph::set_weights_inputs() {
}
void WeightlessGraph::release_init_blob(const size_t initIndex, const Config& config) {
- if (!_blobAllocatedByPlugin || _initBlobs == std::nullopt || config.get<PERF_COUNT>()) {
+ if (_initBlobs == std::nullopt || config.get<PERF_COUNT>()) {
return;
}
diff --git a/src/plugins/intel_npu/src/plugin/include/metadata.hpp b/src/plugins/intel_npu/src/plugin/include/metadata.hpp
index 52b6ea00e97..25fa58921ad 100644
--- a/src/plugins/intel_npu/src/plugin/include/metadata.hpp
+++ b/src/plugins/intel_npu/src/plugin/include/metadata.hpp
@@ -11,6 +11,7 @@
#include <vector>
#include "openvino/core/version.hpp"
+#include "openvino/runtime/tensor.hpp"
namespace intel_npu {
@@ -21,7 +22,12 @@ public:
/**
* @brief Reads metadata from a stream.
*/
- virtual void read(std::istream& stream) = 0;
+ virtual void read(std::istream& tensor) = 0;
+
+ /**
+ * @brief Reads metadata from a ov::Tensor.
+ */
+ virtual void read(const ov::Tensor& tensor) = 0;
/**
* @brief Writes metadata to a stream.
@@ -126,7 +132,12 @@ public:
/**
* @brief Reads version data from a stream.
*/
- void read(std::istream& stream);
+ void read(std::istream& istream);
+
+ /**
+ * @brief Reads version data from a ov::Tensor.
+ */
+ void read(const ov::Tensor& tensor);
/**
* @brief Writes version data to a stream.
@@ -158,14 +169,16 @@ template <uint32_t version>
struct Metadata : public MetadataBase {};
/**
- * @brief Template specialization for metadata version 1.0.
+ * @brief Template specialization for metadata version 2.0.
*/
template <>
class Metadata<METADATA_VERSION_2_0> : public MetadataBase {
public:
Metadata(uint64_t blobSize, std::optional<OpenvinoVersion> ovVersion = std::nullopt);
- void read(std::istream& stream) override;
+ void read(std::istream& tensor) override;
+
+ void read(const ov::Tensor& tensor) override;
/**
* @attention It's a must to first write metadata version in any metadata specialization.
@@ -186,7 +199,8 @@ public:
*
* - true: if all versions match.
*
- * @note The version check can be disabled if the "OV_NPU_DISABLE_VERSION_CHECK" environment variable is set to '1'.
+ * @note The version check can be disabled if the "OV_NPU_DISABLE_VERSION_CHECK" environment variable is set to
+ * 'YES'.
*/
bool is_compatible() override;
@@ -212,6 +226,8 @@ public:
*/
void read(std::istream& stream) override;
+ void read(const ov::Tensor& tensor) override;
+
/**
* @details The number of init schedules, along with the size of each init binary object are written in addition to
* the information registered by the previous metadata versions.
@@ -233,11 +249,19 @@ private:
std::unique_ptr<MetadataBase> create_metadata(uint32_t version, uint64_t blobSize);
/**
- * @brief Reads metadata from a blob.
+ * @brief Reads metadata from a blob (istream).
*
* @return If the blob is versioned and its major version is supported, returns an unique pointer to the read
* MetadataBase object; otherwise, returns 'nullptr'.
*/
std::unique_ptr<MetadataBase> read_metadata_from(std::istream& stream);
+/**
+ * @brief Reads metadata from a blob (ov::Tensor).
+ *
+ * @return If the blob is versioned and its major version is supported, returns an unique pointer to the read
+ * MetadataBase object; otherwise, returns 'nullptr'.
+ */
+std::unique_ptr<MetadataBase> read_metadata_from(const ov::Tensor& tensor);
+
} // namespace intel_npu
diff --git a/src/plugins/intel_npu/src/plugin/include/plugin.hpp b/src/plugins/intel_npu/src/plugin/include/plugin.hpp
index a1920008809..2fad2f0c2be 100644
--- a/src/plugins/intel_npu/src/plugin/include/plugin.hpp
+++ b/src/plugins/intel_npu/src/plugin/include/plugin.hpp
@@ -14,6 +14,7 @@
#include "intel_npu/common/npu.hpp"
#include "intel_npu/config/config.hpp"
#include "intel_npu/utils/logger/logger.hpp"
+#include "metadata.hpp"
#include "metrics.hpp"
#include "openvino/runtime/iplugin.hpp"
#include "openvino/runtime/so_ptr.hpp"
@@ -52,10 +53,10 @@ public:
const ov::SoPtr<ov::IRemoteContext>& context,
const ov::AnyMap& properties) const override;
- std::shared_ptr<ov::ICompiledModel> import_model(const ov::Tensor& stream,
+ std::shared_ptr<ov::ICompiledModel> import_model(const ov::Tensor& compiled_blob,
const ov::AnyMap& properties) const override;
- std::shared_ptr<ov::ICompiledModel> import_model(const ov::Tensor& stream,
+ std::shared_ptr<ov::ICompiledModel> import_model(const ov::Tensor& compiled_blob,
const ov::SoPtr<ov::IRemoteContext>& context,
const ov::AnyMap& properties) const override;
@@ -76,22 +77,14 @@ private:
* the model and its weights. If weights separation has been enabled, the size of the weights is reduced, and there
* will be one or multiple weights initialization schedules found there as well.
*
- * @param stream Contains the whole binary object.
* @param tensorBig Contains the whole binary object.
- * @param compiler Instance used for parsing the compiled model.
- * @param tensorFromProperty Indicates whether or not the compiled model has been provided to the plugin as a tensor
- * object.
- * @param localConfig Propagated to the compiler. Multiple entries are also extracted by this method.
+ * @param metadata Parsed metadata at the end of the blob. Can be nullptr if compatibility checks were disabled.
* @param properties Configuration taking the form of an "ov::AnyMap".
- * @return A wrapper over the L0 handle that can be used for running predictions. The inherited type will depend on
- * the active flow (e.g. may be a "WeightlessGraph" if "weights separation" has been enabled).
+ * @return A compiled model
*/
- std::shared_ptr<IGraph> parse(std::istream& stream,
- const ov::Tensor& tensorBig,
- const std::unique_ptr<ICompilerAdapter>& compiler,
- const bool tensorFromProperty,
- const Config& localConfig,
- const ov::AnyMap& properties) const;
+ std::shared_ptr<ov::ICompiledModel> parse(const ov::Tensor& tensorBig,
+ std::unique_ptr<MetadataBase> metadata,
+ const ov::AnyMap& properties) const;
std::unique_ptr<BackendsRegistry> _backendsRegistry;
diff --git a/src/plugins/intel_npu/src/plugin/src/metadata.cpp b/src/plugins/intel_npu/src/plugin/src/metadata.cpp
index a2fe2c31ab0..c2fdee9d454 100644
--- a/src/plugins/intel_npu/src/plugin/src/metadata.cpp
+++ b/src/plugins/intel_npu/src/plugin/src/metadata.cpp
@@ -39,6 +39,12 @@ void OpenvinoVersion::read(std::istream& stream) {
stream.read(reinterpret_cast<char*>(&_patch), sizeof(_patch));
}
+void OpenvinoVersion::read(const ov::Tensor& tensor) {
+ _major = *reinterpret_cast<const decltype(_major)*>(tensor.data<const char>());
+ _minor = *reinterpret_cast<const decltype(_minor)*>(tensor.data<const char>() + sizeof(_major));
+ _patch = *reinterpret_cast<const decltype(_patch)*>(tensor.data<const char>() + sizeof(_major) + sizeof(_minor));
+}
+
void OpenvinoVersion::write(std::ostream& stream) {
stream.write(reinterpret_cast<const char*>(&_major), sizeof(_major));
stream.write(reinterpret_cast<const char*>(&_minor), sizeof(_minor));
@@ -63,6 +69,10 @@ void Metadata<METADATA_VERSION_2_0>::read(std::istream& stream) {
_ovVersion.read(stream);
}
+void Metadata<METADATA_VERSION_2_0>::read(const ov::Tensor& tensor) {
+ _ovVersion.read(tensor);
+}
+
void Metadata<METADATA_VERSION_2_1>::read(std::istream& stream) {
Metadata<METADATA_VERSION_2_0>::read(stream);
@@ -77,6 +87,27 @@ void Metadata<METADATA_VERSION_2_1>::read(std::istream& stream) {
}
}
+void Metadata<METADATA_VERSION_2_1>::read(const ov::Tensor& tensor) {
+ Metadata<METADATA_VERSION_2_0>::read(tensor);
+ auto roiTensor = ov::Tensor(tensor,
+ ov::Coordinate{sizeof(decltype(std::declval<OpenvinoVersion>().get_major())) +
+ sizeof(decltype(std::declval<OpenvinoVersion>().get_minor())) +
+ sizeof(decltype(std::declval<OpenvinoVersion>().get_patch()))},
+ ov::Coordinate{tensor.get_byte_size()});
+
+ uint64_t numberOfInits;
+ numberOfInits = *reinterpret_cast<const decltype(numberOfInits)*>(roiTensor.data<const char>());
+
+ if (numberOfInits) {
+ _initSizes = std::vector<uint64_t>(numberOfInits);
+ for (uint64_t initIndex = 0; initIndex < numberOfInits; ++initIndex) {
+ _initSizes->at(initIndex) =
+ reinterpret_cast<const std::remove_reference_t<decltype(_initSizes->at(initIndex))>*>(
+ roiTensor.data<const char>() + sizeof(numberOfInits))[initIndex];
+ }
+ }
+}
+
void MetadataBase::append_blob_size_and_magic(std::ostream& stream) {
stream.write(reinterpret_cast<const char*>(&_blobDataSize), sizeof(_blobDataSize));
stream.write(MAGIC_BYTES.data(), MAGIC_BYTES.size());
@@ -201,6 +232,46 @@ std::unique_ptr<MetadataBase> read_metadata_from(std::istream& stream) {
return storedMeta;
}
+std::unique_ptr<MetadataBase> read_metadata_from(const ov::Tensor& tensor) {
+ size_t magicBytesSize = MAGIC_BYTES.size();
+ std::string_view blobMagicBytes(tensor.data<const char>() + tensor.get_byte_size() - magicBytesSize,
+ magicBytesSize);
+
+ if (MAGIC_BYTES != blobMagicBytes) {
+ OPENVINO_THROW("Blob is missing NPU metadata!");
+ }
+
+ uint64_t blobDataSize;
+ blobDataSize = *reinterpret_cast<const decltype(blobDataSize)*>(tensor.data<const char>() + tensor.get_byte_size() -
+ magicBytesSize - sizeof(blobDataSize));
+
+ uint32_t metaVersion;
+ metaVersion = *reinterpret_cast<const decltype(metaVersion)*>(tensor.data<const char>() + blobDataSize);
+
+ std::unique_ptr<MetadataBase> storedMeta;
+ try {
+ auto roiTensor = ov::Tensor(tensor,
+ ov::Coordinate{blobDataSize + sizeof(metaVersion)},
+ ov::Coordinate{tensor.get_byte_size()});
+ storedMeta = create_metadata(metaVersion, blobDataSize);
+ storedMeta->read(roiTensor);
+ } catch (const std::exception& ex) {
+ OPENVINO_THROW(ex.what(),
+ "Imported blob metadata version: ",
+ MetadataBase::get_major(metaVersion),
+ ".",
+ MetadataBase::get_minor(metaVersion),
+ " but the current version is: ",
+ CURRENT_METADATA_MAJOR_VERSION,
+ ".",
+ CURRENT_METADATA_MINOR_VERSION);
+ } catch (...) {
+ OPENVINO_THROW("Unexpected exception while reading blob NPU metadata");
+ }
+
+ return storedMeta;
+}
+
uint64_t MetadataBase::get_blob_size() const {
return _blobDataSize;
}
@@ -208,6 +279,7 @@ uint64_t MetadataBase::get_blob_size() const {
std::optional<std::vector<uint64_t>> Metadata<METADATA_VERSION_2_0>::get_init_sizes() const {
return std::nullopt;
}
+
std::optional<std::vector<uint64_t>> Metadata<METADATA_VERSION_2_1>::get_init_sizes() const {
return _initSizes;
}
diff --git a/src/plugins/intel_npu/src/plugin/src/plugin.cpp b/src/plugins/intel_npu/src/plugin/src/plugin.cpp
index 2ba26102eb8..1fda16a353b 100644
--- a/src/plugins/intel_npu/src/plugin/src/plugin.cpp
+++ b/src/plugins/intel_npu/src/plugin/src/plugin.cpp
@@ -16,7 +16,6 @@
#include "intel_npu/config/npuw.hpp"
#include "intel_npu/config/options.hpp"
#include "intel_npu/utils/zero/zero_init.hpp"
-#include "metadata.hpp"
#include "npuw/compiled_model.hpp"
#include "npuw/llm_compiled_model.hpp"
#include "npuw/serialization.hpp"
@@ -153,6 +152,39 @@ void check_weightless_cache_attribute_occurrence(const std::shared_ptr<const ov:
"attribute is required for running the \"weights separation\" flow.");
}
+std::shared_ptr<ov::ICompiledModel> import_model_npuw(std::istream& stream,
+ ov::AnyMap& properties,
+ std::shared_ptr<const ov::IPlugin> pluginSO) {
+ // If was exported via NPUW
+ auto stream_start_pos = stream.tellg();
+ ov::npuw::s11n::IndicatorType serialization_indicator;
+ ov::npuw::s11n::read(stream, serialization_indicator);
+ if (serialization_indicator == NPUW_SERIALIZATION_INDICATOR) {
+ ov::npuw::s11n::IndicatorType compiled_model_indicator;
+ ov::npuw::s11n::read(stream, compiled_model_indicator);
+ stream.seekg(-stream.tellg() + stream_start_pos, std::ios::cur);
+
+ if (compiled_model_indicator == NPUW_LLM_COMPILED_MODEL_INDICATOR) {
+ // Properties are required for ov::weights_path
+ return ov::npuw::LLMCompiledModel::import_model(stream, pluginSO, properties);
+ } else if (compiled_model_indicator == NPUW_COMPILED_MODEL_INDICATOR) {
+ // Properties are required for ov::weights_path
+ return ov::npuw::CompiledModel::import_model(stream, pluginSO, properties);
+ } else {
+ OPENVINO_THROW("Couldn't deserialize NPUW blob - fatal error!");
+ }
+ }
+ stream.seekg(-stream.tellg() + stream_start_pos, std::ios::cur);
+
+ // Drop NPUW properties if there are any
+ for (auto it = properties.begin(); it != properties.end(); ++it) {
+ if (it->first.find("NPUW") != it->first.npos) {
+ properties.erase(it->first);
+ }
+ }
+ return nullptr;
+}
+
} // namespace
namespace intel_npu {
@@ -627,65 +659,144 @@ ov::SoPtr<ov::IRemoteContext> Plugin::get_default_context(const ov::AnyMap&) con
return std::make_shared<RemoteContextImpl>(_backend);
}
-std::shared_ptr<ov::ICompiledModel> Plugin::import_model(std::istream& origStream, const ov::AnyMap& properties) const {
+std::shared_ptr<ov::ICompiledModel> Plugin::import_model(std::istream& stream, const ov::AnyMap& properties) const {
OV_ITT_SCOPED_TASK(itt::domains::NPUPlugin, "Plugin::import_model");
- ov::AnyMap npu_plugin_properties = properties;
- ov::Tensor tensor;
- bool tensorFromProperty = false;
-
- std::istream stream{origStream.rdbuf()};
- ov::SharedStreamBuffer buffer{nullptr, 0}; // used only if blob is given by tensor, but it is not OV cached blob
-
- if (auto blob_it = npu_plugin_properties.find(ov::hint::compiled_blob.name());
- blob_it != npu_plugin_properties.end()) {
- tensor = blob_it->second.as<ov::Tensor>();
- tensorFromProperty = true;
- if (auto loadedFromCache = npu_plugin_properties.find(ov::loaded_from_cache.name());
- loadedFromCache != npu_plugin_properties.end() && loadedFromCache->second.as<bool>() != false) {
- tensor = ov::Tensor(
- tensor,
- ov::Coordinate{static_cast<size_t>(origStream.tellg())},
- ov::Coordinate{tensor.get_byte_size()}); // ROI tensor to skip OV header in case of cached blob
- } else {
- buffer = ov::SharedStreamBuffer(reinterpret_cast<char*>(tensor.data()), tensor.get_byte_size());
- stream.rdbuf(&buffer);
- }
+ if (properties.find(ov::hint::compiled_blob.name()) != properties.end()) {
+ _logger.warning("ov::hint::compiled_blob is no longer supported for import_model(stream) API! Please use new "
+ "import_model(tensor) API instead.");
}
- // If was exported via NPUW
- auto stream_start_pos = stream.tellg();
- ov::npuw::s11n::IndicatorType serialization_indicator;
- ov::npuw::s11n::read(stream, serialization_indicator);
- if (serialization_indicator == NPUW_SERIALIZATION_INDICATOR) {
- ov::npuw::s11n::IndicatorType compiled_model_indicator;
- ov::npuw::s11n::read(stream, compiled_model_indicator);
- stream.seekg(-stream.tellg() + stream_start_pos, std::ios::cur);
+ auto npu_plugin_properties = properties;
+ // NPUW properties from npu_plugin_properties will be erased if import_model_npuw returns nullptr
+ auto compiledModel = import_model_npuw(stream, npu_plugin_properties, shared_from_this());
+ if (compiledModel) {
+ return compiledModel;
+ }
- if (compiled_model_indicator == NPUW_LLM_COMPILED_MODEL_INDICATOR) {
- // Properties are required for ov::weights_path
- return ov::npuw::LLMCompiledModel::import_model(stream, shared_from_this(), properties);
- } else if (compiled_model_indicator == NPUW_COMPILED_MODEL_INDICATOR) {
- // Properties are required for ov::weights_path
- return ov::npuw::CompiledModel::import_model(stream, shared_from_this(), properties);
- } else {
- OPENVINO_THROW("Couldn't deserialize NPUW blob - fatal error!");
+ try {
+ const bool skipCompatibility =
+ npu_plugin_properties.find(DISABLE_VERSION_CHECK::key().data()) != npu_plugin_properties.end() &&
+ npu_plugin_properties[DISABLE_VERSION_CHECK::key().data()].as<bool>() == true;
+ std::unique_ptr<MetadataBase> metadata = nullptr;
+ size_t blobSize = MetadataBase::getFileSize(stream);
+ if (!skipCompatibility) {
+ // Read only metadata from the stream and check if blob is compatible. Load blob into memory only in case it
+ // passes compatibility checks.
+ metadata = read_metadata_from(stream);
+ if (!metadata->is_compatible()) {
+ OPENVINO_THROW("Incompatible blob version!");
+ }
+ blobSize = metadata->get_blob_size();
}
+ ov::Tensor tensor(ov::element::u8, ov::Shape{blobSize});
+ if (blobSize > static_cast<decltype(blobSize)>(std::numeric_limits<std::streamsize>::max())) {
+ OPENVINO_THROW("Blob size is too large to be represented on a std::streamsize!");
+ }
+ stream.read(tensor.data<char>(), static_cast<std::streamsize>(blobSize));
+ return parse(tensor, std::move(metadata), npu_plugin_properties);
+ } catch (const std::exception& ex) {
+ OPENVINO_THROW("Can't import network: ", ex.what());
+ } catch (...) {
+ OPENVINO_THROW("NPU import_model got unexpected exception from CompiledModel");
}
- stream.seekg(-stream.tellg() + stream_start_pos, std::ios::cur);
+}
- // Drop NPUW properties if there are any
- for (auto it = properties.begin(); it != properties.end(); ++it) {
- if (it->first.find("NPUW") != it->first.npos) {
- npu_plugin_properties.erase(it->first);
+std::shared_ptr<ov::ICompiledModel> Plugin::import_model(std::istream& stream,
+ const ov::SoPtr<ov::IRemoteContext>& context,
+ const ov::AnyMap& properties) const {
+ auto casted = std::dynamic_pointer_cast<RemoteContextImpl>(context._ptr);
+ if (casted == nullptr) {
+ OPENVINO_THROW("Invalid remote context type. Can't cast to ov::intel_npu::RemoteContext type");
+ }
+
+ return import_model(stream, properties);
+}
+
+std::shared_ptr<ov::ICompiledModel> Plugin::import_model(const ov::Tensor& compiled_blob,
+ const ov::AnyMap& properties) const {
+ OV_ITT_SCOPED_TASK(itt::domains::NPUPlugin, "Plugin::import_model");
+
+ // Need to create intermediate istream for NPUW
+ ov::SharedStreamBuffer buffer{reinterpret_cast<char*>(compiled_blob.data()), compiled_blob.get_byte_size()};
+ std::istream stream{&buffer};
+
+ auto npu_plugin_properties = properties;
+ // NPUW properties from npu_plugin_properties will be erased if import_model_npuw returns nullptr
+ auto compiledModel = import_model_npuw(stream, npu_plugin_properties, shared_from_this());
+ if (compiledModel) {
+ return compiledModel;
+ }
+
+ try {
+ const bool skipCompatibility =
+ npu_plugin_properties.find(DISABLE_VERSION_CHECK::key().data()) != npu_plugin_properties.end() &&
+ npu_plugin_properties[DISABLE_VERSION_CHECK::key().data()].as<bool>() == true;
+ std::unique_ptr<MetadataBase> metadata = nullptr;
+ size_t blobSize = compiled_blob.get_byte_size();
+ if (!skipCompatibility) {
+ metadata = read_metadata_from(compiled_blob);
+ if (!metadata->is_compatible()) {
+ OPENVINO_THROW("Incompatible blob version!");
+ }
+ blobSize = metadata->get_blob_size();
}
+ const ov::Tensor roiTensor(compiled_blob,
+ ov::Coordinate{0},
+ ov::Coordinate{blobSize}); // ROI tensor to skip NPU plugin metadata
+ return parse(roiTensor, std::move(metadata), npu_plugin_properties);
+ } catch (const std::exception& ex) {
+ OPENVINO_THROW("Can't import network: ", ex.what());
+ } catch (...) {
+ OPENVINO_THROW("NPU import_model got unexpected exception from CompiledModel");
}
+}
+std::shared_ptr<ov::ICompiledModel> Plugin::import_model(const ov::Tensor& compiled_blob,
+ const ov::SoPtr<ov::IRemoteContext>& context,
+ const ov::AnyMap& properties) const {
+ auto casted = std::dynamic_pointer_cast<RemoteContextImpl>(context._ptr);
+ if (casted == nullptr) {
+ OPENVINO_THROW("Invalid remote context type. Can't cast to ov::intel_npu::RemoteContext type");
+ }
+ return import_model(compiled_blob, properties);
+}
+
+ov::SupportedOpsMap Plugin::query_model(const std::shared_ptr<const ov::Model>& model,
+ const ov::AnyMap& properties) const {
+ OV_ITT_SCOPED_TASK(itt::domains::NPUPlugin, "Plugin::query_model");
CompilerAdapterFactory compilerAdapterFactory;
auto compiler = compilerAdapterFactory.getCompiler(_backend, resolveCompilerType(_globalConfig, properties));
+ const std::map<std::string, std::string> propertiesMap = any_copy(properties);
+ auto localConfig = fork_local_config(propertiesMap, compiler, OptionMode::CompileTime);
+ _logger.setLevel(localConfig.get<LOG_LEVEL>());
+ const auto platform =
+ utils::getCompilationPlatform(localConfig.get<PLATFORM>(),
+ localConfig.get<DEVICE_ID>(),
+ _backend == nullptr ? std::vector<std::string>() : _backend->getDeviceNames());
+ localConfig.update({{ov::intel_npu::platform.name(), platform}});
- const auto propertiesMap = any_copy(npu_plugin_properties);
- OV_ITT_TASK_CHAIN(PLUGIN_IMPORT_MODEL, itt::domains::NPUPlugin, "Plugin::import_model", "fork_local_config");
+ ov::SupportedOpsMap supportedOpsMap;
+ try {
+ supportedOpsMap = compiler->query(model, localConfig);
+ } catch (const std::runtime_error& e) {
+ OPENVINO_THROW(e.what());
+ } catch (...) {
+ OPENVINO_THROW("NPU query_model got unexpected error from compiler");
+ }
+
+ return supportedOpsMap;
+}
+
+std::shared_ptr<ov::ICompiledModel> Plugin::parse(const ov::Tensor& tensorBig,
+ std::unique_ptr<MetadataBase> metadata,
+ const ov::AnyMap& properties) const {
+ OV_ITT_SCOPED_TASK(itt::domains::NPUPlugin, "Plugin::parse");
+ CompilerAdapterFactory compilerAdapterFactory;
+ auto compiler = compilerAdapterFactory.getCompiler(_backend, resolveCompilerType(_globalConfig, properties));
+
+ const auto propertiesMap = any_copy(properties);
+ OV_ITT_TASK_CHAIN(PLUGIN_PARSE_MODEL, itt::domains::NPUPlugin, "Plugin::parse", "fork_local_config");
auto localConfig = fork_local_config(propertiesMap, compiler, OptionMode::RunTime);
_logger.setLevel(localConfig.get<LOG_LEVEL>());
const auto platform =
@@ -701,63 +812,22 @@ std::shared_ptr<ov::ICompiledModel> Plugin::import_model(std::istream& origStrea
"The usage of a compiled model can lead to undefined behavior. Please use OpenVINO IR instead!");
}
- OV_ITT_TASK_NEXT(PLUGIN_IMPORT_MODEL, "parse");
-
- std::shared_ptr<ov::ICompiledModel> compiledModel;
-
- try {
- const std::shared_ptr<IGraph> graph =
- parse(stream, tensor, compiler, tensorFromProperty, localConfig, properties);
-
- const std::shared_ptr<ov::Model> modelDummy =
- create_dummy_model(graph->get_metadata().inputs, graph->get_metadata().outputs);
- compiledModel = std::make_shared<CompiledModel>(modelDummy, shared_from_this(), device, graph, localConfig);
- } catch (const std::exception& ex) {
- OPENVINO_THROW("Can't import network: ", ex.what());
- } catch (...) {
- OPENVINO_THROW("NPU import_model got unexpected exception from CompiledModel");
- }
-
- OV_ITT_TASK_SKIP(PLUGIN_IMPORT_MODEL);
-
- return compiledModel;
-}
-
-std::shared_ptr<IGraph> Plugin::parse(std::istream& stream,
- const ov::Tensor& tensorBig,
- const std::unique_ptr<ICompilerAdapter>& compiler,
- const bool tensorFromProperty,
- const Config& config,
- const ov::AnyMap& properties) const {
- uint64_t mainSize;
+ uint64_t mainSize = tensorBig.get_byte_size();
std::optional<std::vector<uint64_t>> initSizes;
- const bool skipCompatibility = config.get<DISABLE_VERSION_CHECK>();
- if (!skipCompatibility) {
- auto storedMeta = read_metadata_from(stream);
- if (!storedMeta->is_compatible()) {
- OPENVINO_THROW("Incompatible blob version!");
- }
-
+ if (metadata) {
size_t accumulator = 0;
- initSizes = storedMeta->get_init_sizes();
- mainSize = initSizes.has_value() ? storedMeta->get_blob_size() -
- std::accumulate(initSizes->begin(), initSizes->end(), accumulator)
- : storedMeta->get_blob_size();
+ initSizes = metadata->get_init_sizes();
+ mainSize = initSizes.has_value()
+ ? metadata->get_blob_size() - std::accumulate(initSizes->begin(), initSizes->end(), accumulator)
+ : metadata->get_blob_size();
} else {
_logger.info("Blob compatibility check skipped.");
- mainSize = MetadataBase::getFileSize(stream);
}
- ov::Tensor tensorMain;
- if (tensorFromProperty == false) { // tensor was not received from ov::compiled_blob property, copy from stream
- tensorMain = ov::Tensor(ov::element::u8, ov::Shape{mainSize});
- stream.read(tensorMain.data<char>(), mainSize);
- } else {
- tensorMain = ov::Tensor(tensorBig,
- ov::Coordinate{0},
- ov::Coordinate{mainSize}); // ROI tensor to skip NPU plugin metadata
- }
+ ov::Tensor tensorMain(tensorBig,
+ ov::Coordinate{0},
+ ov::Coordinate{mainSize}); // ROI tensor to skip NPU plugin metadata
std::shared_ptr<const ov::Model> originalModel;
std::vector<ov::Tensor> tensorsInits;
@@ -766,15 +836,8 @@ std::shared_ptr<IGraph> Plugin::parse(std::istream& stream,
if (weightsSeparationEnabled) {
// Read the init compiled models as well
size_t cursorPosition = mainSize;
- ov::Tensor tensorInit;
for (uint64_t initSize : initSizes.value()) {
- if (tensorFromProperty == false) {
- tensorInit = ov::Tensor(ov::element::u8, ov::Shape{initSize});
- stream.read(tensorInit.data<char>(), initSize);
- } else {
- tensorInit =
- ov::Tensor(tensorBig, ov::Coordinate{cursorPosition}, ov::Coordinate{cursorPosition + initSize});
- }
+ ov::Tensor tensorInit(tensorBig, ov::Coordinate{cursorPosition}, ov::Coordinate{cursorPosition + initSize});
tensorsInits.push_back(tensorInit);
cursorPosition += initSize;
}
@@ -792,8 +855,8 @@ std::shared_ptr<IGraph> Plugin::parse(std::istream& stream,
"wrong data type. Expected: std::shared_ptr<const ov::Model>.");
}
}
- } else if (!config.get<WEIGHTS_PATH>().empty()) {
- const std::string weightsPath = config.get<WEIGHTS_PATH>();
+ } else if (!localConfig.get<WEIGHTS_PATH>().empty()) {
+ const std::string weightsPath = localConfig.get<WEIGHTS_PATH>();
const size_t weightsPathLength = weightsPath.length();
std::string xmlPath = weightsPath;
@@ -822,63 +885,16 @@ std::shared_ptr<IGraph> Plugin::parse(std::istream& stream,
}
auto graph = compiler->parse(std::move(tensorMain),
- !tensorFromProperty,
- config,
+ localConfig,
weightsSeparationEnabled ? std::make_optional(std::move(tensorsInits)) : std::nullopt,
weightsSeparationEnabled ? std::make_optional(originalModel) : std::nullopt);
graph->update_network_name("net" + std::to_string(_compiledModelLoadCounter++));
- return graph;
-}
-
-std::shared_ptr<ov::ICompiledModel> Plugin::import_model(std::istream& stream,
- const ov::SoPtr<ov::IRemoteContext>& context,
- const ov::AnyMap& properties) const {
- auto casted = std::dynamic_pointer_cast<RemoteContextImpl>(context._ptr);
- if (casted == nullptr) {
- OPENVINO_THROW("Invalid remote context type. Can't cast to ov::intel_npu::RemoteContext type");
- }
-
- return import_model(stream, properties);
-}
-
-std::shared_ptr<ov::ICompiledModel> Plugin::import_model(const ov::Tensor& model, const ov::AnyMap& properties) const {
- ov::SharedStreamBuffer buffer{reinterpret_cast<char*>(model.data()), model.get_byte_size()};
- std::istream stream{&buffer};
- return import_model(stream, properties);
-}
-
-std::shared_ptr<ov::ICompiledModel> Plugin::import_model(const ov::Tensor& model,
- const ov::SoPtr<ov::IRemoteContext>& context,
- const ov::AnyMap& properties) const {
- ov::SharedStreamBuffer buffer{reinterpret_cast<char*>(model.data()), model.get_byte_size()};
- std::istream stream{&buffer};
- return import_model(stream, context, properties);
-}
-
-ov::SupportedOpsMap Plugin::query_model(const std::shared_ptr<const ov::Model>& model,
- const ov::AnyMap& properties) const {
- OV_ITT_SCOPED_TASK(itt::domains::NPUPlugin, "Plugin::query_model");
- CompilerAdapterFactory compilerAdapterFactory;
- auto compiler = compilerAdapterFactory.getCompiler(_backend, resolveCompilerType(_globalConfig, properties));
- const std::map<std::string, std::string> propertiesMap = any_copy(properties);
- auto localConfig = fork_local_config(propertiesMap, compiler, OptionMode::CompileTime);
- _logger.setLevel(localConfig.get<LOG_LEVEL>());
- const auto platform =
- utils::getCompilationPlatform(localConfig.get<PLATFORM>(),
- localConfig.get<DEVICE_ID>(),
- _backend == nullptr ? std::vector<std::string>() : _backend->getDeviceNames());
- localConfig.update({{ov::intel_npu::platform.name(), platform}});
+ const std::shared_ptr<ov::Model> modelDummy =
+ create_dummy_model(graph->get_metadata().inputs, graph->get_metadata().outputs);
- ov::SupportedOpsMap supportedOpsMap;
- try {
- supportedOpsMap = compiler->query(model, localConfig);
- } catch (const std::runtime_error& e) {
- OPENVINO_THROW(e.what());
- } catch (...) {
- OPENVINO_THROW("NPU query_model got unexpected error from compiler");
- }
+ OV_ITT_TASK_NEXT(PLUGIN_PARSE_MODEL, "parse");
- return supportedOpsMap;
+ return std::make_shared<CompiledModel>(modelDummy, shared_from_this(), device, graph, localConfig);
}
std::atomic<int> Plugin::_compiledModelLoadCounter{1};
diff --git a/src/plugins/intel_npu/tests/functional/internal/overload/compiled_model/blob_compatibility.hpp b/src/plugins/intel_npu/tests/functional/internal/overload/compiled_model/blob_compatibility.hpp
index 9c301f6f722..8ee4e8a8a1d 100644
--- a/src/plugins/intel_npu/tests/functional/internal/overload/compiled_model/blob_compatibility.hpp
+++ b/src/plugins/intel_npu/tests/functional/internal/overload/compiled_model/blob_compatibility.hpp
@@ -6,11 +6,10 @@
#include <gtest/gtest.h>
-#include "shared_test_classes/base/ov_behavior_test_utils.hpp"
-
#include "common/npu_test_env_cfg.hpp"
#include "openvino/core/except.hpp"
#include "openvino/core/version.hpp"
+#include "shared_test_classes/base/ov_behavior_test_utils.hpp"
// models generation
#include "openvino/op/abs.hpp"
@@ -121,9 +120,11 @@ using OVBlobCompatibilityNPU_PV_Driver_No_Throw = OVBlobCompatibilityNPU;
std::ifstream blobStream(blobPath, std::ios::binary | std::ios::in); \
ASSERT_TYPE(core.import_model(blobStream, target_device, {ov::intel_npu::disable_version_check(true)}), \
##__VA_ARGS__); \
- APPEND_EXPORT_HELPER(ASSERT_TYPE, ##__VA_ARGS__)
+ // CVS-166953: skip compile_model(model, {compiled_blob(tensor)}) scenario until ov::hint::compiled_blob
+ // is passed to fallback on import_model(tensor)
+ // APPEND_EXPORT_HELPER(ASSERT_TYPE, ##__VA_ARGS__)
-TEST_P(OVBlobCompatibilityNPU, DISABLED_CanImportAllPrecompiledBlobsForAllOVVersionsAndDrivers) {
+TEST_P(OVBlobCompatibilityNPU, CanImportAllPrecompiledBlobsForAllOVVersionsAndDrivers) {
if (auto current_driver =
core.get_property(ov::test::utils::DEVICE_NPU, ov::intel_npu::driver_version.name()).as<std::string>();
current_driver == DRIVERS.at(E_DRIVERS::DRIVER_1688) && blobName.find(current_driver) == std::string::npos) {
diff --git a/src/plugins/intel_npu/tests/functional/internal/overload/compiled_model/import_export.hpp b/src/plugins/intel_npu/tests/functional/internal/overload/compiled_model/import_export.hpp
index 3041c427d09..722b794e066 100644
--- a/src/plugins/intel_npu/tests/functional/internal/overload/compiled_model/import_export.hpp
+++ b/src/plugins/intel_npu/tests/functional/internal/overload/compiled_model/import_export.hpp
@@ -15,32 +15,7 @@ namespace behavior {
using OVCompiledGraphImportExportTestNPU = OVCompiledGraphImportExportTest;
-TEST_P(OVCompiledGraphImportExportTestNPU, CanImportModelWithEmptyIStreamAndCompiledBlobProp) {
- ov::Core core;
- std::shared_ptr<std::string> strSO;
- {
- std::stringstream sstream;
- auto model = ov::test::utils::make_conv_pool_relu();
- core.compile_model(model, target_device, configuration).export_model(sstream);
- strSO = std::make_shared<std::string>(sstream.str());
- }
- auto tensor = ov::Tensor(ov::element::u8, ov::Shape{strSO->size()}, strSO->data());
- auto impl = ov::get_tensor_impl(tensor);
- impl._so = strSO;
- tensor = ov::make_tensor(impl);
- configuration.emplace(ov::hint::compiled_blob(tensor));
- std::ifstream emptyIFileStream;
- std::fstream emptyFileStream;
- std::istringstream emptyIStringStream;
- std::stringstream emptyStringStream;
- OV_ASSERT_NO_THROW(auto compiledModel = core.import_model(emptyIFileStream, target_device, configuration));
- OV_ASSERT_NO_THROW(auto compiledModel = core.import_model(emptyFileStream, target_device, configuration));
- OV_ASSERT_NO_THROW(auto compiledModel = core.import_model(emptyIStringStream, target_device, configuration));
- OV_ASSERT_NO_THROW(auto compiledModel = core.import_model(emptyStringStream, target_device, configuration));
- configuration.erase(ov::hint::compiled_blob.name()); // cleanup
-}
-
-TEST_P(OVCompiledGraphImportExportTestNPU, CanImportModelWithApplicationHeaderAndCompiledBlobProp) {
+TEST_P(OVCompiledGraphImportExportTestNPU, CanImportModelWithApplicationHeaderAndTensorAPI) {
ov::Core core;
const std::string_view headerView("<dummy_application_header>");
const std::string_view suffixView("<dummy_application_suffix>");
@@ -52,23 +27,7 @@ TEST_P(OVCompiledGraphImportExportTestNPU, CanImportModelWithApplicationHeaderAn
core.compile_model(model, target_device, configuration).export_model(sstream);
}
- // header tests, stream won't work if not handled by OV caching mechanism
- {
- auto strSO = std::make_shared<std::string>(sstream.str());
- auto tensor = ov::Tensor(ov::element::u8, ov::Shape{strSO->size()}, strSO->data());
- auto impl = ov::get_tensor_impl(tensor);
- impl._so = strSO;
- tensor = ov::make_tensor(impl);
- configuration.emplace(ov::hint::compiled_blob(tensor));
- sstream.seekg(headerView.size(), std::ios::beg); // skip header
- OV_EXPECT_THROW(
- auto compiledModel = core.import_model(sstream, target_device, configuration),
- ov::Exception,
- testing::HasSubstr("metadata")); // OVNPU suffix can be parsed from metadata, but not correct version
- configuration.erase(ov::hint::compiled_blob.name()); // cleanup
- }
-
- // header tests, stream won't impact import_model if application manages ov::Tensor offset
+ // header tests, application correctly manages offsets
{
auto strSO = std::make_shared<std::string>(sstream.str());
auto tensor = ov::Tensor(ov::element::u8,
@@ -77,13 +36,13 @@ TEST_P(OVCompiledGraphImportExportTestNPU, CanImportModelWithApplicationHeaderAn
auto impl = ov::get_tensor_impl(tensor);
impl._so = strSO;
tensor = ov::make_tensor(impl);
- configuration.emplace(ov::hint::compiled_blob(tensor));
- // header is no longer skipped by stream
+ sstream.seekg(headerView.size(), std::ios::beg); // skip header
+
OV_ASSERT_NO_THROW(auto compiledModel = core.import_model(sstream, target_device, configuration));
- configuration.erase(ov::hint::compiled_blob.name()); // cleanup
+ // OV_ASSERT_NO_THROW(auto compiledModel = core.import_model(tensor, target_device, configuration));
}
- // suffix tests, stream won't impact import_model if application manages ov::Tensor size
+ // suffix tests, application correctly manages ov::Tensor offsets or disables metadata checking
{
sstream.write(suffixView.data(), suffixView.size());
auto strSO = std::make_shared<std::string>(sstream.str());
@@ -93,9 +52,15 @@ TEST_P(OVCompiledGraphImportExportTestNPU, CanImportModelWithApplicationHeaderAn
auto impl = ov::get_tensor_impl(tensor);
impl._so = strSO;
tensor = ov::make_tensor(impl);
- configuration.emplace(ov::hint::compiled_blob(tensor));
+ sstream.seekg(headerView.size(), std::ios::beg);
+
+ configuration.emplace(ov::intel_npu::disable_version_check(true));
OV_ASSERT_NO_THROW(auto compiledModel = core.import_model(sstream, target_device, configuration));
- configuration.erase(ov::hint::compiled_blob.name()); // cleanup
+ configuration.erase(ov::intel_npu::disable_version_check.name());
+ // OV_ASSERT_NO_THROW(auto compiledModel = core.import_model(tensor, target_device, configuration));
+ OV_EXPECT_THROW(auto compiledModel = core.import_model(sstream, target_device, configuration),
+ ov::Exception,
+ testing::HasSubstr("metadata")); // OVNPU suffix cannot be parsed from metadata
}
}
diff --git a/src/plugins/intel_npu/tests/functional/internal/overload/ov_plugin/life_time.hpp b/src/plugins/intel_npu/tests/functional/internal/overload/ov_plugin/life_time.hpp
index d146d8dc7d1..c979a9de690 100644
--- a/src/plugins/intel_npu/tests/functional/internal/overload/ov_plugin/life_time.hpp
+++ b/src/plugins/intel_npu/tests/functional/internal/overload/ov_plugin/life_time.hpp
@@ -224,7 +224,7 @@ TEST_P(OVHoldersTestOnImportedNetworkNPU, CreateRequestWithCoreRemoved) {
auto request = compiled_model.create_infer_request();
}
-TEST_P(OVHoldersTestOnImportedNetworkNPU, CanInferAfterCompiledBlobPropTensorIsDestroyed) {
+TEST_P(OVHoldersTestOnImportedNetworkNPU, DISABLED_CanInferAfterTensorIsDestroyed) {
ov::Core core = createCoreWithTemplate();
for (size_t i = 0; i < 2; ++i) {
@@ -240,19 +240,19 @@ TEST_P(OVHoldersTestOnImportedNetworkNPU, CanInferAfterCompiledBlobPropTensorIsD
auto impl = ov::get_tensor_impl(tensor);
impl._so = strSO;
tensor = ov::make_tensor(impl);
- configuration.emplace(ov::hint::compiled_blob(tensor));
- compiled_model = core.import_model(sstream, target_device, configuration);
- configuration.erase(ov::hint::compiled_blob.name()); // cleanup
+ // compiled_model = core.import_model(tensor, target_device, configuration);
}
// check if the shared object (strSO destroyed above) persists in compiled_model
- std::ostringstream sstream;
- ov::InferRequest inferRequest;
- OV_ASSERT_NO_THROW(compiled_model.export_model(sstream));
- EXPECT_TRUE(sstream.tellp() > 0);
- OV_ASSERT_NO_THROW(inferRequest = compiled_model.create_infer_request());
- compiled_model = {}; // dtor of compiled model won't affect created infer request
- OV_ASSERT_NO_THROW(inferRequest.infer());
+ {
+ std::ostringstream sstream;
+ ov::InferRequest infer_request;
+ OV_ASSERT_NO_THROW(compiled_model.export_model(sstream));
+ EXPECT_TRUE(sstream.tellp() > 0);
+ OV_ASSERT_NO_THROW(infer_request = compiled_model.create_infer_request());
+ compiled_model = {}; // dtor of compiled model won't affect created infer request
+ OV_ASSERT_NO_THROW(infer_request.infer());
+ }
}
configuration.erase(ov::intel_npu::defer_weights_load.name()); // cleanup
}
diff --git a/src/plugins/intel_npu/tests/unit/npu/metadata_version.cpp b/src/plugins/intel_npu/tests/unit/npu/metadata_version.cpp
index 5a6f423cc9a..e44b2dca278 100644
--- a/src/plugins/intel_npu/tests/unit/npu/metadata_version.cpp
+++ b/src/plugins/intel_npu/tests/unit/npu/metadata_version.cpp
@@ -26,6 +26,12 @@ TEST_F(MetadataUnitTests, readUnversionedBlob) {
std::unique_ptr<MetadataBase> storedMeta;
ASSERT_ANY_THROW(storedMeta = read_metadata_from(blob));
+
+ blob.seekg(0, std::ios::beg);
+ size_t streamSize = MetadataBase::getFileSize(blob);
+ auto tensor = ov::Tensor(ov::element::u8, ov::Shape{streamSize});
+ blob.read(tensor.data<char>(), tensor.get_byte_size());
+ ASSERT_ANY_THROW(storedMeta = read_metadata_from(tensor));
}
TEST_F(MetadataUnitTests, writeAndReadCurrentMetadataFromBlob) {
@@ -39,6 +45,13 @@ TEST_F(MetadataUnitTests, writeAndReadCurrentMetadataFromBlob) {
std::unique_ptr<MetadataBase> storedMeta;
OV_ASSERT_NO_THROW(storedMeta = read_metadata_from(stream));
ASSERT_TRUE(storedMeta->is_compatible());
+
+ stream.seekg(0, std::ios::beg);
+ size_t streamSize = MetadataBase::getFileSize(stream);
+ auto tensor = ov::Tensor(ov::element::u8, ov::Shape{streamSize});
+ stream.read(tensor.data<char>(), tensor.get_byte_size());
+ OV_ASSERT_NO_THROW(storedMeta = read_metadata_from(tensor));
+ ASSERT_TRUE(storedMeta->is_compatible());
}
TEST_F(MetadataUnitTests, writeAndReadInvalidOpenvinoVersion) {
@@ -53,6 +66,13 @@ TEST_F(MetadataUnitTests, writeAndReadInvalidOpenvinoVersion) {
std::unique_ptr<MetadataBase> storedMeta;
OV_ASSERT_NO_THROW(storedMeta = read_metadata_from(stream));
ASSERT_FALSE(storedMeta->is_compatible());
+
+ stream.seekg(0, std::ios::beg);
+ size_t streamSize = MetadataBase::getFileSize(stream);
+ auto tensor = ov::Tensor(ov::element::u8, ov::Shape{streamSize});
+ stream.read(tensor.data<char>(), tensor.get_byte_size());
+ OV_ASSERT_NO_THROW(storedMeta = read_metadata_from(tensor));
+ ASSERT_FALSE(storedMeta->is_compatible());
}
TEST_F(MetadataUnitTests, writeAndReadInvalidMetadataVersion) {
@@ -66,6 +86,12 @@ TEST_F(MetadataUnitTests, writeAndReadInvalidMetadataVersion) {
OV_ASSERT_NO_THROW(meta.write(stream));
OV_ASSERT_NO_THROW(meta.append_blob_size_and_magic(stream));
ASSERT_ANY_THROW(auto storedMeta = read_metadata_from(stream));
+
+ stream.seekg(0, std::ios::beg);
+ size_t streamSize = MetadataBase::getFileSize(stream);
+ auto tensor = ov::Tensor(ov::element::u8, ov::Shape{streamSize});
+ stream.read(tensor.data<char>(), tensor.get_byte_size());
+ ASSERT_ANY_THROW(auto storedMeta = read_metadata_from(tensor));
}
TEST_F(MetadataUnitTests, writeAndReadMetadataWithNewerMinorVersion) {
@@ -82,6 +108,13 @@ TEST_F(MetadataUnitTests, writeAndReadMetadataWithNewerMinorVersion) {
std::unique_ptr<MetadataBase> storedMeta;
OV_ASSERT_NO_THROW(storedMeta = read_metadata_from(stream));
storedMeta->is_compatible();
+
+ stream.seekg(0, std::ios::beg);
+ size_t streamSize = MetadataBase::getFileSize(stream);
+ auto tensor = ov::Tensor(ov::element::u8, ov::Shape{streamSize});
+ stream.read(tensor.data<char>(), tensor.get_byte_size());
+ OV_ASSERT_NO_THROW(storedMeta = read_metadata_from(tensor));
+ storedMeta->is_compatible();
}
struct MetadataVersionTestFixture : Metadata<CURRENT_METADATA_VERSION>, ::testing::TestWithParam<uint32_t> {
@@ -123,6 +156,13 @@ TEST_P(MetadataVersionTestFixture, writeAndReadInvalidMetadataVersion) {
OV_ASSERT_NO_THROW(dummyMeta.write(blob));
EXPECT_ANY_THROW(read_metadata_from(blob));
ASSERT_FALSE(dummyMeta.is_compatible());
+
+ blob.seekg(0, std::ios::beg);
+ size_t streamSize = MetadataBase::getFileSize(blob);
+ auto tensor = ov::Tensor(ov::element::u8, ov::Shape{streamSize});
+ blob.read(tensor.data<char>(), tensor.get_byte_size());
+ EXPECT_ANY_THROW(read_metadata_from(tensor));
+ ASSERT_FALSE(dummyMeta.is_compatible());
}
const std::vector badMetadataVersions = {
@@ -162,6 +202,13 @@ TEST_F(MetadataUnitTests, writeAndReadMetadataWithNewerFieldAtEnd) {
std::unique_ptr<MetadataBase> storedMeta;
OV_ASSERT_NO_THROW(storedMeta = read_metadata_from(stream));
ASSERT_FALSE(storedMeta->is_compatible());
+
+ stream.seekg(0, std::ios::beg);
+ size_t streamSize = MetadataBase::getFileSize(stream);
+ auto tensor = ov::Tensor(ov::element::u8, ov::Shape{streamSize});
+ stream.read(tensor.data<char>(), tensor.get_byte_size());
+ OV_ASSERT_NO_THROW(storedMeta = read_metadata_from(tensor));
+ ASSERT_FALSE(storedMeta->is_compatible());
}
TEST_F(MetadataUnitTests, writeAndReadMetadataWithNewerFieldAtMiddle) {
@@ -186,6 +233,12 @@ TEST_F(MetadataUnitTests, writeAndReadMetadataWithNewerFieldAtMiddle) {
std::unique_ptr<MetadataBase> storedMeta;
EXPECT_ANY_THROW(storedMeta = read_metadata_from(stream));
+
+ stream.seekg(0, std::ios::beg);
+ size_t streamSize = MetadataBase::getFileSize(stream);
+ auto tensor = ov::Tensor(ov::element::u8, ov::Shape{streamSize});
+ stream.read(tensor.data<char>(), tensor.get_byte_size());
+ EXPECT_ANY_THROW(storedMeta = read_metadata_from(tensor));
}
TEST_F(MetadataUnitTests, writeAndReadMetadataWithRemovedField) {
@@ -210,4 +263,10 @@ TEST_F(MetadataUnitTests, writeAndReadMetadataWithRemovedField) {
std::unique_ptr<MetadataBase> storedMeta;
EXPECT_ANY_THROW(storedMeta = read_metadata_from(stream));
+
+ stream.seekg(0, std::ios::beg);
+ size_t streamSize = MetadataBase::getFileSize(stream);
+ auto tensor = ov::Tensor(ov::element::u8, ov::Shape{streamSize});
+ stream.read(tensor.data<char>(), tensor.get_byte_size());
+ EXPECT_ANY_THROW(storedMeta = read_metadata_from(tensor));
}