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
[NPU] Adding support for the import_model path with remote context (#25971)
Bogdan Pereanu941e246e9e42
diff --git a/src/plugins/intel_npu/src/plugin/src/plugin.cpp b/src/plugins/intel_npu/src/plugin/src/plugin.cppindex f18fc6aa13c..7a6c16062cb 100644--- a/src/plugins/intel_npu/src/plugin/src/plugin.cpp+++ b/src/plugins/intel_npu/src/plugin/src/plugin.cpp@@ -471,10 +471,10 @@ Plugin::Plugin()ov::PropertyMutability::RW,[&](const Config& config) {if (!config.has<STEPPING>()) {- const auto specifiedDeviceName = get_specified_device_name(config);- return static_cast<int64_t>(_metrics->GetSteppingNumber(specifiedDeviceName));+ const auto specifiedDeviceName = get_specified_device_name(config);+ return static_cast<int64_t>(_metrics->GetSteppingNumber(specifiedDeviceName));} else {- return config.get<STEPPING>();+ return config.get<STEPPING>();}}}},{ov::intel_npu::max_tiles.name(),@@ -482,10 +482,10 @@ Plugin::Plugin()ov::PropertyMutability::RW,[&](const Config& config) {if (!config.has<MAX_TILES>()) {- const auto specifiedDeviceName = get_specified_device_name(config);- return static_cast<int64_t>(_metrics->GetMaxTiles(specifiedDeviceName));+ const auto specifiedDeviceName = get_specified_device_name(config);+ return static_cast<int64_t>(_metrics->GetMaxTiles(specifiedDeviceName));} else {- return config.get<MAX_TILES>();+ return config.get<MAX_TILES>();}}}},{ov::intel_npu::compilation_mode.name(),@@ -690,7 +690,9 @@ std::shared_ptr<ov::ICompiledModel> Plugin::compile_model(const std::shared_ptr<const ov::AnyMap& properties,const ov::SoPtr<ov::IRemoteContext>& context) const {auto casted = std::dynamic_pointer_cast<RemoteContextImpl>(context._ptr);- OPENVINO_ASSERT(casted, "Invalid remote context type. Can't cast to ov::intel_npu::RemoteContext type");+ if (casted == nullptr) {+ OPENVINO_THROW("Invalid remote context type. Can't cast to ov::intel_npu::RemoteContext type");+ }return compile_model(model, properties);}@@ -766,10 +768,15 @@ std::shared_ptr<ov::ICompiledModel> Plugin::import_model(std::istream& stream, creturn compiledModel;}-std::shared_ptr<ov::ICompiledModel> Plugin::import_model(std::istream& /*stream*/,- const ov::SoPtr<ov::IRemoteContext>& /*context*/,- const ov::AnyMap& /*properties*/) const {- OPENVINO_THROW_NOT_IMPLEMENTED("The remote context feature is not supported by the NPU plugin");+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);}ov::SupportedOpsMap Plugin::query_model(const std::shared_ptr<const ov::Model>& model,diff --git a/src/plugins/intel_npu/tests/functional/behavior/remote_tensor_tests/remote_run.hpp b/src/plugins/intel_npu/tests/functional/behavior/remote_tensor_tests/remote_run.hppindex a58da0253a9..1746085a5f8 100644--- a/src/plugins/intel_npu/tests/functional/behavior/remote_tensor_tests/remote_run.hpp+++ b/src/plugins/intel_npu/tests/functional/behavior/remote_tensor_tests/remote_run.hpp@@ -33,6 +33,8 @@ protected:std::shared_ptr<ov::Model> ov_model;ov::CompiledModel compiled_model;+ std::string m_cache_dir;+public:static std::string getTestCaseName(testing::TestParamInfo<CompilationParams> obj) {std::string targetDevice;@@ -62,7 +64,27 @@ public:ov_model = getDefaultNGraphFunctionForTheDeviceNPU(); // FIXME: E#80555}+ std::string generateCacheDirName(const std::string& test_name) {+ using namespace std::chrono;+ // Generate unique file names based on test name, thread id and timestamp+ // This allows execution of tests in parallel (stress mode)+ auto hash = std::to_string(std::hash<std::string>()(test_name));+ std::stringstream ss;+ auto ts = duration_cast<nanoseconds>(high_resolution_clock::now().time_since_epoch());+ ss << hash << "_"+ << "_" << ts.count();+ return ss.str();+ }+void TearDown() override {+ if (!m_cache_dir.empty()) {+ core->set_property({ov::cache_dir()});+ core.reset();+ ov::test::utils::PluginCache::get().reset();+ ov::test::utils::removeFilesWithExt(m_cache_dir, "blob");+ ov::test::utils::removeDir(m_cache_dir);+ }+if (!configuration.empty()) {utils::PluginCache::get().reset();}@@ -76,18 +98,46 @@ TEST_P(RemoteRunTests, CheckRemoteTensorInternalBuf) {SKIP_IF_CURRENT_TEST_IS_DISABLED()ov::InferRequest inference_request;- OV_ASSERT_NO_THROW(compiled_model = core->compile_model(ov_model, target_device, configuration));+ auto zero_context = core->get_default_context(target_device).as<ov::intel_npu::level_zero::ZeroContext>();+ OV_ASSERT_NO_THROW(compiled_model = core->compile_model(ov_model, zero_context, configuration));OV_ASSERT_NO_THROW(inference_request = compiled_model.create_infer_request());auto tensor = inference_request.get_input_tensor();++ auto remote_tensor =+ zero_context.create_l0_host_tensor(ov::element::f32, tensor.get_shape(), ov::intel_npu::TensorType::INPUT);+ tensor = {};++ ov::Tensor check_remote_tensor;+ OV_ASSERT_NO_THROW(check_remote_tensor = remote_tensor);+ ASSERT_THROW(check_remote_tensor.data(), ov::Exception);++ OV_ASSERT_NO_THROW(inference_request.set_input_tensor(check_remote_tensor));+ OV_ASSERT_NO_THROW(inference_request.infer());+}++TEST_P(RemoteRunTests, CheckImportModelPath) {+ // Skip test according to plugin specific disabledTestPatterns() (if any)+ SKIP_IF_CURRENT_TEST_IS_DISABLED()+ ov::InferRequest inference_request;+auto zero_context = core->get_default_context(target_device).as<ov::intel_npu::level_zero::ZeroContext>();+ m_cache_dir = generateCacheDirName(GetTestName());+ core->set_property({ov::cache_dir(m_cache_dir)});+ auto compiled_model_no_cache = core->compile_model(ov_model, zero_context, configuration);+ compiled_model = core->compile_model(ov_model, zero_context, configuration);++ OV_ASSERT_NO_THROW(inference_request = compiled_model.create_infer_request());++ auto tensor = inference_request.get_input_tensor();+auto remote_tensor =zero_context.create_l0_host_tensor(ov::element::f32, tensor.get_shape(), ov::intel_npu::TensorType::INPUT);tensor = {};ov::Tensor check_remote_tensor;- ASSERT_NO_THROW(check_remote_tensor = remote_tensor);+ OV_ASSERT_NO_THROW(check_remote_tensor = remote_tensor);ASSERT_THROW(check_remote_tensor.data(), ov::Exception);OV_ASSERT_NO_THROW(inference_request.set_input_tensor(check_remote_tensor));@@ -109,7 +159,7 @@ TEST_P(RemoteRunTests, CheckRemoteTensorInternalBufChangingTensors) {context.create_l0_host_tensor(ov::element::f32, tensor.get_shape(), ov::intel_npu::TensorType::INPUT);ov::Tensor check_remote_tensor;- ASSERT_NO_THROW(check_remote_tensor = remote_tensor);+ OV_ASSERT_NO_THROW(check_remote_tensor = remote_tensor);ASSERT_THROW(check_remote_tensor.data(), ov::Exception);OV_ASSERT_NO_THROW(inference_request.set_input_tensor(check_remote_tensor));@@ -365,7 +415,7 @@ TEST_P(RemoteRunTests, CheckOutputDataFromTwoRunsInOutRemoteTensorsHostTensor2)auto remote_input_tensor =context.create_l0_host_tensor(ov::element::f32, input_shape, ov::intel_npu::TensorType::INPUT);auto remote_output_tensor =- context.create_l0_host_tensor(ov::element::f32, output_shape, ov::intel_npu::TensorType::INPUT);+ context.create_l0_host_tensor(ov::element::f32, output_shape, ov::intel_npu::TensorType::OUTPUT);memset(remote_input_tensor.get(), 1, byte_size);OV_ASSERT_NO_THROW(inference_request.set_input_tensor(remote_input_tensor));OV_ASSERT_NO_THROW(inference_request.set_output_tensor(remote_output_tensor));