Skip to content

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

[NPU] Create execution context for dynamic graph execution (#35380)

Xin Wangb62da294cf82
diff --git a/src/plugins/intel_npu/src/backend/src/zero_dynamic_infer_request.cpp b/src/plugins/intel_npu/src/backend/src/zero_dynamic_infer_request.cpp
index 1c809482e6f..de73337f907 100644
--- a/src/plugins/intel_npu/src/backend/src/zero_dynamic_infer_request.cpp
+++ b/src/plugins/intel_npu/src/backend/src/zero_dynamic_infer_request.cpp
@@ -269,6 +269,10 @@ void ZeroDynamicInferRequest::predict_shapes(std::vector<IDynamicGraph::MemRefTy
void ZeroDynamicInferRequest::check_tensor_and_predicted_shapes(
const std::vector<IDynamicGraph::MemRefType>& outputProps) {
+ if (outputProps.empty()) {
+ _logger.debug("check_tensor_and_predicted_shapes - no output props to check, skip check");
+ return;
+ }
// check_tensor in set_tensor already checked the input tensor and output tensor with metadata
// Check again here to see if the shape is right compared with predicted shape
// If user set output tensor, need check if the tensor is large enough
diff --git a/src/plugins/intel_npu/src/backend/src/zero_dynamic_pipeline.cpp b/src/plugins/intel_npu/src/backend/src/zero_dynamic_pipeline.cpp
index ff41fa8cf6c..fcf8eeeedf2 100644
--- a/src/plugins/intel_npu/src/backend/src/zero_dynamic_pipeline.cpp
+++ b/src/plugins/intel_npu/src/backend/src/zero_dynamic_pipeline.cpp
@@ -220,7 +220,7 @@ void DynamicPipeline::push() {
command_lists->resetCommandList();
dynamicGraph->execute(_init_structs,
- command_lists->getBinding(),
+ graphArguments,
command_lists->getHandles(),
commandQueueHandle,
fence,
diff --git a/src/plugins/intel_npu/src/compiler_adapter/include/dynamic_graph.hpp b/src/plugins/intel_npu/src/compiler_adapter/include/dynamic_graph.hpp
index 954f6a5f588..896e6609e82 100644
--- a/src/plugins/intel_npu/src/compiler_adapter/include/dynamic_graph.hpp
+++ b/src/plugins/intel_npu/src/compiler_adapter/include/dynamic_graph.hpp
@@ -77,10 +77,17 @@ public:
}
};
- struct GraphArgumentsImpl : public GraphArguments {
+ struct GraphArgumentsImpl {
std::vector<npu_vm_runtime_mem_ref_handle_t> _inputMemRefs;
std::vector<npu_vm_runtime_mem_ref_handle_t> _outputMemRefs;
npu_vm_runtime_execute_params_t _executeParams = {};
+
+ ~GraphArgumentsImpl() {
+ if (_executeParams.executionContext != nullptr) {
+ npuVMRuntimeDestroyExecutionContext(_executeParams.executionContext);
+ _executeParams.executionContext = nullptr;
+ }
+ }
};
class Impl {
diff --git a/src/plugins/intel_npu/src/compiler_adapter/src/dynamic_graph.cpp b/src/plugins/intel_npu/src/compiler_adapter/src/dynamic_graph.cpp
index 817666f24d6..5535698d952 100644
--- a/src/plugins/intel_npu/src/compiler_adapter/src/dynamic_graph.cpp
+++ b/src/plugins/intel_npu/src/compiler_adapter/src/dynamic_graph.cpp
@@ -337,6 +337,15 @@ void DynamicGraphImpl::executeGraph(const std::shared_ptr<ZeroInitStructsHolder>
}
}
+ // Prepare execution context for each graph arguments
+ if (params->executionContext == nullptr) {
+ if (npuVMRuntimeCreateExecutionContext(_engine, &params->executionContext) != NPU_VM_RUNTIME_RESULT_SUCCESS) {
+ OPENVINO_THROW("Failed to create a VM execution context");
+ } else {
+ _logger.debug("Execution context is created successfully.");
+ }
+ }
+
params->pInputs = argsImpl->_inputMemRefs.data();
params->numOfInputs = static_cast<uint32_t>(argsImpl->_inputMemRefs.size());
params->pOutputs = argsImpl->_outputMemRefs.data();
diff --git a/src/plugins/intel_npu/tests/functional/behavior/dynamic_host_pipeline/infer_with_host_compile.hpp b/src/plugins/intel_npu/tests/functional/behavior/dynamic_host_pipeline/infer_with_host_compile.hpp
index 582b4ff1c46..e3d40de92f5 100644
--- a/src/plugins/intel_npu/tests/functional/behavior/dynamic_host_pipeline/infer_with_host_compile.hpp
+++ b/src/plugins/intel_npu/tests/functional/behavior/dynamic_host_pipeline/infer_with_host_compile.hpp
@@ -71,31 +71,26 @@ public:
std::tie(target_device, configuration) = this->GetParam();
- APIBaseTest::SetUp();
- }
-
- bool isLLVMFormat(std::stringstream& modelStream) {
- auto pos = modelStream.tellg();
- if (pos == std::streampos(-1)) {
- return false;
+ std::vector<std::string> deviceNames =
+ core->get_property("NPU", ov::available_devices.name()).as<std::vector<std::string>>();
+ for (auto name : deviceNames) {
+ if (target_device.find(name) != std::string::npos) {
+ isTargetDevice = true;
+ break;
+ }
}
- modelStream.seekg(0, std::ios::beg);
-
- // Assume the first 20 char of LLVM blob shall have key word 'llvm'
- std::string region(20, '\0');
- modelStream.read(&region[0], 20);
- region.resize(modelStream.gcount());
-
- modelStream.clear();
- modelStream.seekg(pos);
+ APIBaseTest::SetUp();
+ }
- return region.find("llvm") != std::string::npos;
+ void TearDown() {
+ core->set_property("NPU", ov::log::level(ov::log::Level::ERR));
}
protected:
std::shared_ptr<ov::Core> core = utils::PluginCache::get().core();
ov::AnyMap configuration;
+ bool isTargetDevice = false;
};
TEST_P(InferWithHostCompileTests, Compile) {
@@ -110,12 +105,15 @@ TEST_P(InferWithHostCompileTests, Compile) {
std::stringstream modelStream;
OV_ASSERT_NO_THROW(compiledModel.export_model(modelStream));
- // With HostCompile, the modelStream shall contain "llvm.func"
- ASSERT_TRUE(isLLVMFormat(modelStream)) << "CompiledStream from HostCompile mode shall has 'llvm.func' inside it";
-
ov::InferRequest reqDynamic;
- // Add shape check once npu_mlir_runtime is inside test package
- EXPECT_THROW(reqDynamic = compiledModel.create_infer_request(), ov::Exception);
+
+ try {
+ reqDynamic = compiledModel.create_infer_request();
+ } catch (const ov::Exception& e) {
+ if (std::string(e.what()).find("Cannot load library") == std::string::npos) {
+ FAIL() << "Expected exception message to contain 'Cannot load library', but got: " << e.what();
+ }
+ }
}
TEST_P(InferWithHostCompileTests, CompileAndImport) {
@@ -130,15 +128,51 @@ TEST_P(InferWithHostCompileTests, CompileAndImport) {
std::stringstream modelStream;
OV_ASSERT_NO_THROW(compiledModel.export_model(modelStream));
- // With HostCompile, the modelStream shall contain "llvm.func"
- ASSERT_TRUE(isLLVMFormat(modelStream)) << "CompiledStream from HostCompile mode shall has 'llvm.func' inside it";
+ ov::CompiledModel importedModel;
+ OV_ASSERT_NO_THROW(core->import_model(modelStream, target_device, configuration));
+
+ ov::InferRequest reqDynamic;
+
+ try {
+ reqDynamic = compiledModel.create_infer_request();
+ } catch (const ov::Exception& e) {
+ if (std::string(e.what()).find("Cannot load library") == std::string::npos) {
+ FAIL() << "Expected exception message to contain 'Cannot load library', but got: " << e.what();
+ }
+ }
+}
+
+TEST_P(InferWithHostCompileTests, CompileAndImportAndInfer) {
+ // Skip test according to plugin specific disabledTestPatterns() (if any)
+ SKIP_IF_CURRENT_TEST_IS_DISABLED()
+ if (!isTargetDevice) {
+ GTEST_SKIP() << "Skip test for current device";
+ }
+ auto model = createMaxPoolModel();
+
+ ov::CompiledModel compiledModel;
+ // Compilation shall pass since load of npu_mlir_runtime is deffered with NPU_CREATE_EXECUTOR=0
+ OV_ASSERT_NO_THROW(compiledModel = core->compile_model(model, target_device, configuration));
+
+ std::stringstream modelStream;
+ OV_ASSERT_NO_THROW(compiledModel.export_model(modelStream));
ov::CompiledModel importedModel;
OV_ASSERT_NO_THROW(core->import_model(modelStream, target_device, configuration));
ov::InferRequest reqDynamic;
- // Add shape check once npu_mlir_runtime is inside test package
- EXPECT_THROW(reqDynamic = importedModel.create_infer_request(), ov::Exception);
+
+ try {
+ reqDynamic = compiledModel.create_infer_request();
+ } catch (const ov::Exception& e) {
+ if (std::string(e.what()).find("Cannot load library") == std::string::npos) {
+ FAIL() << "Expected exception message to contain 'Cannot load library', but got: " << e.what();
+ } else {
+ GTEST_SKIP() << "Cannot load library, skip test.";
+ }
+ }
+
+ OV_ASSERT_NO_THROW(reqDynamic.infer());
}
} // namespace behavior