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`] Optimize `import_model` path when `compiled_blob` prop is not empty (#30297)
Mircea-Aurelian Dan528e9c129a2c
diff --git a/src/plugins/intel_npu/src/compiler_adapter/src/graph.cpp b/src/plugins/intel_npu/src/compiler_adapter/src/graph.cppindex b3ac3409ea4..0fa1fa79317 100644--- a/src/plugins/intel_npu/src/compiler_adapter/src/graph.cpp+++ b/src/plugins/intel_npu/src/compiler_adapter/src/graph.cpp@@ -35,7 +35,8 @@ size_t Graph::export_blob(std::ostream& stream) const {std::vector<uint8_t> blob;if (_blobIsReleased) {- OPENVINO_THROW("Model was imported (not compiled) by the plugin. Model export is forbidden in this case!");+ OPENVINO_THROW("Model was optimized away. Try importing it using `ov::hint::compiled_blob` property to extend "+ "its lifetime.");}if (_blobPtr == nullptr) { // when compiling the model using Compiler in Driver, the blob is handled by the driverdiff --git a/src/plugins/intel_npu/src/plugin/src/plugin.cpp b/src/plugins/intel_npu/src/plugin/src/plugin.cppindex 537b6324be6..dc83d251aa8 100644--- a/src/plugins/intel_npu/src/plugin/src/plugin.cpp+++ b/src/plugins/intel_npu/src/plugin/src/plugin.cpp@@ -523,9 +523,30 @@ ov::SoPtr<ov::IRemoteContext> Plugin::get_default_context(const ov::AnyMap&) conreturn std::make_shared<RemoteContextImpl>(_backend);}-std::shared_ptr<ov::ICompiledModel> Plugin::import_model(std::istream& stream, const ov::AnyMap& properties) const {+std::shared_ptr<ov::ICompiledModel> Plugin::import_model(std::istream& origStream, const ov::AnyMap& properties) const {OV_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::hint::compiled_blob has no corresponding "Config" implementation thus we need to remove it from the+ // list of properties+ if (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);+ 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+ }+ npu_plugin_properties.erase(blob_it);+ }+// If was exported via NPUWauto stream_start_pos = stream.tellg();ov::npuw::s11n::IndicatorType serialization_indicator;@@ -538,25 +559,12 @@ std::shared_ptr<ov::ICompiledModel> Plugin::import_model(std::istream& stream, cstream.seekg(-stream.tellg() + stream_start_pos, std::ios::cur);// Drop NPUW properties if there are any- ov::AnyMap npu_plugin_properties;for (auto it = properties.begin(); it != properties.end(); ++it) {- if (it->first.find("NPUW") == it->first.npos) {- npu_plugin_properties.insert(*it);+ if (it->first.find("NPUW") != it->first.npos) {+ npu_plugin_properties.erase(it);}}- std::shared_ptr<ov::AlignedBuffer> modelBuffer;- // ov::hint::compiled_blob has no corresponding "Config" implementation thus we need to remove it from the- // list of properties- if (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);- npu_plugin_properties.erase(blob_it);- }-CompilerAdapterFactory compilerAdapterFactory;auto compiler = compilerAdapterFactory.getCompiler(_backend, resolveCompilerType(_globalConfig, properties));diff --git a/src/plugins/intel_npu/tests/functional/behavior/compiled_model/import_export.cpp b/src/plugins/intel_npu/tests/functional/behavior/compiled_model/import_export.cppnew file mode 100644index 00000000000..41cebfb88b8--- /dev/null+++ b/src/plugins/intel_npu/tests/functional/behavior/compiled_model/import_export.cpp@@ -0,0 +1,24 @@+// Copyright (C) 2018-2025 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "overload/compiled_model/import_export.hpp"++#include <gtest/gtest.h>++#include <map>+#include <string>++#include "common/npu_test_env_cfg.hpp"++using namespace ov::test::behavior;++const std::vector<ov::AnyMap> compiledModelConfigs = {{}};++INSTANTIATE_TEST_SUITE_P(+ smoke_Behavior_NPU,+ OVCompiledGraphImportExportTestNPU,+ ::testing::Combine(::testing::Values(ov::element::f16 /* not used in internal import_export tests so far */),+ ::testing::Values(ov::test::utils::DEVICE_NPU),+ ::testing::ValuesIn(compiledModelConfigs)),+ ov::test::utils::appendPlatformTypeTestName<OVCompiledGraphImportExportTestNPU>);diff --git a/src/plugins/intel_npu/tests/functional/behavior/ov_plugin/life_time.cpp b/src/plugins/intel_npu/tests/functional/behavior/ov_plugin/life_time.cppnew file mode 100644index 00000000000..b5cdcb07be6--- /dev/null+++ b/src/plugins/intel_npu/tests/functional/behavior/ov_plugin/life_time.cpp@@ -0,0 +1,29 @@+// Copyright (C) 2018-2025 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "overload/ov_plugin/life_time.hpp"++#include "common/npu_test_env_cfg.hpp"+#include "common/utils.hpp"+#include "intel_npu/config/options.hpp"++using namespace ov::test::behavior;++namespace {++const std::vector<ov::AnyMap> configs = {{}};++INSTANTIATE_TEST_SUITE_P(compatibility_smoke_BehaviorTests,+ OVHoldersTestNPU,+ ::testing::Combine(::testing::Values(ov::test::utils::DEVICE_NPU),+ ::testing::ValuesIn(configs)),+ ov::test::utils::appendPlatformTypeTestName<OVHoldersTestNPU>);++INSTANTIATE_TEST_SUITE_P(compatibility_smoke_BehaviorTests,+ OVHoldersTestOnImportedNetworkNPU,+ ::testing::Combine(::testing::Values(ov::test::utils::DEVICE_NPU),+ ::testing::ValuesIn(configs)),+ ov::test::utils::appendPlatformTypeTestName<OVHoldersTestOnImportedNetworkNPU>);++} // namespacediff --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.hppnew file mode 100644index 00000000000..3041c427d09--- /dev/null+++ b/src/plugins/intel_npu/tests/functional/internal/overload/compiled_model/import_export.hpp@@ -0,0 +1,106 @@+// Copyright (C) 2018-2025 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "behavior/compiled_model/import_export.hpp"+#include "common_test_utils/subgraph_builders/conv_pool_relu.hpp"+#include "intel_npu/npu_private_properties.hpp"+#include "openvino/runtime/make_tensor.hpp"++namespace ov {++namespace test {++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) {+ ov::Core core;+ const std::string_view headerView("<dummy_application_header>");+ const std::string_view suffixView("<dummy_application_suffix>");+ std::stringstream sstream;++ sstream.write(headerView.data(), headerView.size());+ {+ auto model = ov::test::utils::make_conv_pool_relu();+ 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+ {+ auto strSO = std::make_shared<std::string>(sstream.str());+ auto tensor = ov::Tensor(ov::element::u8,+ ov::Shape{strSO->size() - headerView.size()},+ strSO->data() + headerView.size());+ 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+ OV_ASSERT_NO_THROW(auto compiledModel = core.import_model(sstream, target_device, configuration));+ configuration.erase(ov::hint::compiled_blob.name()); // cleanup+ }++ // suffix tests, stream won't impact import_model if application manages ov::Tensor size+ {+ sstream.write(suffixView.data(), suffixView.size());+ auto strSO = std::make_shared<std::string>(sstream.str());+ auto tensor = ov::Tensor(ov::element::u8,+ ov::Shape{strSO->size() - headerView.size() - suffixView.size()},+ strSO->data() + headerView.size());+ auto impl = ov::get_tensor_impl(tensor);+ impl._so = strSO;+ tensor = ov::make_tensor(impl);+ configuration.emplace(ov::hint::compiled_blob(tensor));+ OV_ASSERT_NO_THROW(auto compiledModel = core.import_model(sstream, target_device, configuration));+ configuration.erase(ov::hint::compiled_blob.name()); // cleanup+ }+}++} // namespace behavior++} // namespace test++} // namespace ovdiff --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.hppindex 48be3f59cfe..d146d8dc7d1 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@@ -5,7 +5,9 @@#pragma once#include "behavior/ov_plugin/life_time.hpp"+#include "common/npu_test_env_cfg.hpp"#include "common_test_utils/subgraph_builders/conv_pool_relu.hpp"+#include "openvino/runtime/make_tensor.hpp"using CompilationParams = std::tuple<std::string, // Device nameov::AnyMap // Config@@ -63,8 +65,10 @@ public:#define EXPECT_NO_CRASH(_statement) EXPECT_EXIT(_statement; exit(0), testing::ExitedWithCode(0), "")-static void release_order_test(std::vector<std::size_t> order, const std::string& deviceName,- std::shared_ptr<ov::Model> function, ov::AnyMap configuration) {+static void release_order_test(std::vector<std::size_t> order,+ const std::string& deviceName,+ std::shared_ptr<ov::Model> function,+ ov::AnyMap configuration) {ov::AnyVector objects;{ov::Core core = createCoreWithTemplate();@@ -90,7 +94,7 @@ TEST_P(OVHoldersTestNPU, Orders) {order_str << objects.at(i) << " ";}EXPECT_NO_CRASH(release_order_test(order, target_device, function, configuration))- << "for order: " << order_str.str();+ << "for order: " << order_str.str();} while (std::next_permutation(order.begin(), order.end()));}@@ -151,9 +155,8 @@ TEST_P(OVHoldersTestNPU, LoadedRemoteContext) {}}-class OVHoldersTestOnImportedNetworkNPU :- public OVPluginTestBase,- public testing::WithParamInterface<CompilationParams> {+class OVHoldersTestOnImportedNetworkNPU : public OVPluginTestBase,+ public testing::WithParamInterface<CompilationParams> {protected:ov::AnyMap configuration;std::string deathTestStyle;@@ -221,6 +224,39 @@ TEST_P(OVHoldersTestOnImportedNetworkNPU, CreateRequestWithCoreRemoved) {auto request = compiled_model.create_infer_request();}+TEST_P(OVHoldersTestOnImportedNetworkNPU, CanInferAfterCompiledBlobPropTensorIsDestroyed) {+ ov::Core core = createCoreWithTemplate();++ for (size_t i = 0; i < 2; ++i) {+ ov::CompiledModel compiled_model;+ if (i != 0) {+ configuration.emplace(ov::intel_npu::defer_weights_load(true));+ }+ {+ std::stringstream sstream;+ core.compile_model(function, target_device, configuration).export_model(sstream);+ 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));+ compiled_model = core.import_model(sstream, target_device, configuration);+ configuration.erase(ov::hint::compiled_blob.name()); // cleanup+ }++ // 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());+ }+ configuration.erase(ov::intel_npu::defer_weights_load.name()); // cleanup+}+} // namespace behavior} // namespace test} // namespace ovdiff --git a/src/plugins/intel_npu/tests/functional/shared_tests_instances/behavior/ov_plugin/life_time.cpp b/src/plugins/intel_npu/tests/functional/shared_tests_instances/behavior/ov_plugin/life_time.cppindex fc370c47404..98094d0807a 100644--- a/src/plugins/intel_npu/tests/functional/shared_tests_instances/behavior/ov_plugin/life_time.cpp+++ b/src/plugins/intel_npu/tests/functional/shared_tests_instances/behavior/ov_plugin/life_time.cpp@@ -7,7 +7,6 @@#include "common/npu_test_env_cfg.hpp"#include "common/utils.hpp"#include "intel_npu/config/options.hpp"-#include "overload/ov_plugin/life_time.hpp"using namespace ov::test::behavior;@@ -37,18 +36,6 @@ INSTANTIATE_TEST_SUITE_P(compatibility_smoke_BehaviorTests,::testing::Values(ov::test::utils::DEVICE_NPU),getTestCaseName);-INSTANTIATE_TEST_SUITE_P(compatibility_smoke_BehaviorTests,- OVHoldersTestNPU,- ::testing::Combine(::testing::Values(ov::test::utils::DEVICE_NPU),- ::testing::ValuesIn(configs)),- OVHoldersTestNPU::getTestCaseName);--INSTANTIATE_TEST_SUITE_P(compatibility_smoke_BehaviorTests,- OVHoldersTestOnImportedNetworkNPU,- ::testing::Combine(::testing::Values(ov::test::utils::DEVICE_NPU),- ::testing::ValuesIn(configs)),- OVHoldersTestOnImportedNetworkNPU::getTestCaseName);-INSTANTIATE_TEST_SUITE_P(compatibility_smoke_VirtualPlugin_BehaviorTests,OVHoldersTestWithConfig,::testing::ValuesIn(device_names_and_priorities),