Skip to content

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

[NPU] Add support for compiled model release memory (#35084)

Bogdan Pereanu86b7de0be716
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.hpp
index 4c551a40e93..f323dec4bcc 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
@@ -71,6 +71,8 @@ public:
virtual void set_last_submitted_id(uint32_t id_index);
virtual uint32_t get_last_submitted_id() const;
+ virtual void evict_memory();
+
virtual std::optional<bool> is_profiling_blob() const = 0;
protected:
diff --git a/src/plugins/intel_npu/src/common/src/igraph.cpp b/src/plugins/intel_npu/src/common/src/igraph.cpp
index 248410d5089..7009720d4f0 100644
--- a/src/plugins/intel_npu/src/common/src/igraph.cpp
+++ b/src/plugins/intel_npu/src/common/src/igraph.cpp
@@ -94,4 +94,6 @@ uint32_t IGraph::get_last_submitted_id() const {
OPENVINO_THROW("get_last_submitted_id not implemented");
}
+void IGraph::evict_memory() {}
+
} // namespace intel_npu
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 272fab4c624..c74177a1ee5 100644
--- a/src/plugins/intel_npu/src/compiler_adapter/include/graph.hpp
+++ b/src/plugins/intel_npu/src/compiler_adapter/include/graph.hpp
@@ -59,6 +59,8 @@ public:
std::optional<bool> is_profiling_blob() const override;
+ void evict_memory() override;
+
~Graph() override;
protected:
diff --git a/src/plugins/intel_npu/src/compiler_adapter/include/ze_graph_ext_wrappers.hpp b/src/plugins/intel_npu/src/compiler_adapter/include/ze_graph_ext_wrappers.hpp
index 85161b5085f..735e74e05da 100644
--- a/src/plugins/intel_npu/src/compiler_adapter/include/ze_graph_ext_wrappers.hpp
+++ b/src/plugins/intel_npu/src/compiler_adapter/include/ze_graph_ext_wrappers.hpp
@@ -87,6 +87,8 @@ public:
bool isBlobDataImported(const GraphDescriptor& graphDescriptor) const;
+ void evict_memory(const GraphDescriptor& graphDescriptor) const;
+
private:
void getMetadata(ze_graph_handle_t graphHandle,
uint32_t indexUsedByDriver,
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 2cf3cf9562b..a03d8421bc8 100644
--- a/src/plugins/intel_npu/src/compiler_adapter/src/graph.cpp
+++ b/src/plugins/intel_npu/src/compiler_adapter/src/graph.cpp
@@ -339,6 +339,12 @@ const std::optional<std::size_t> Graph::get_batch_size() const {
return _batchSize;
}
+void Graph::evict_memory() {
+ if (_zeGraphExt != nullptr) {
+ _zeGraphExt->evict_memory(_graphDesc);
+ }
+}
+
Graph::~Graph() {
// make sure all the context-dependent components are destroyed before the zero context is destroyed
if (_zeGraphExt != nullptr) {
diff --git a/src/plugins/intel_npu/src/compiler_adapter/src/ze_graph_ext_wrappers.cpp b/src/plugins/intel_npu/src/compiler_adapter/src/ze_graph_ext_wrappers.cpp
index 5032e16e399..90b57d95b22 100644
--- a/src/plugins/intel_npu/src/compiler_adapter/src/ze_graph_ext_wrappers.cpp
+++ b/src/plugins/intel_npu/src/compiler_adapter/src/ze_graph_ext_wrappers.cpp
@@ -640,4 +640,18 @@ bool ZeGraphExtWrappers::isPluginModelHashSupported() const {
return _graphExtVersion > ZE_MAKE_VERSION(1, 13);
}
+void ZeGraphExtWrappers::evict_memory(const GraphDescriptor& graphDescriptor) const {
+ if (_graphExtVersion < ZE_MAKE_VERSION(1, 16)) {
+ _logger.info("Memory eviction is not supported by the current driver version.");
+ return;
+ }
+ if (graphDescriptor._handle == nullptr) {
+ _logger.info("Graph handle is null, nothing to release memory.");
+ return;
+ }
+ THROW_ON_FAIL_FOR_LEVELZERO_EXT("pfnEvict",
+ _zeroInitStruct->getGraphDdiTable().pfnEvict(graphDescriptor._handle),
+ _zeroInitStruct->getGraphDdiTable());
+}
+
} // namespace intel_npu
diff --git a/src/plugins/intel_npu/src/plugin/include/compiled_model.hpp b/src/plugins/intel_npu/src/plugin/include/compiled_model.hpp
index df8b9eddde4..cb3f3173c19 100644
--- a/src/plugins/intel_npu/src/plugin/include/compiled_model.hpp
+++ b/src/plugins/intel_npu/src/plugin/include/compiled_model.hpp
@@ -58,6 +58,8 @@ public:
const FilteredConfig& get_config() const override;
+ void release_memory() override;
+
private:
void configure_stream_executors();
diff --git a/src/plugins/intel_npu/src/plugin/src/compiled_model.cpp b/src/plugins/intel_npu/src/plugin/src/compiled_model.cpp
index 8ff6b6147de..3eb3074c52a 100644
--- a/src/plugins/intel_npu/src/plugin/src/compiled_model.cpp
+++ b/src/plugins/intel_npu/src/plugin/src/compiled_model.cpp
@@ -191,6 +191,12 @@ const FilteredConfig& CompiledModel::get_config() const {
return _propertiesManager->getConfig();
}
+void CompiledModel::release_memory() {
+ if (_graph != nullptr) {
+ _graph->evict_memory();
+ }
+}
+
void CompiledModel::configure_stream_executors() {
std::shared_ptr<ov::threading::ITaskExecutor> task_executor;
if (get_plugin()->get_property(ov::internal::exclusive_async_requests.name(), {}).as<bool>()) {
diff --git a/src/plugins/intel_npu/tests/functional/behavior/ov_infer_request/compile_and_infer.hpp b/src/plugins/intel_npu/tests/functional/behavior/ov_infer_request/compile_and_infer.hpp
index b590fe2258e..8e45b187449 100644
--- a/src/plugins/intel_npu/tests/functional/behavior/ov_infer_request/compile_and_infer.hpp
+++ b/src/plugins/intel_npu/tests/functional/behavior/ov_infer_request/compile_and_infer.hpp
@@ -135,6 +135,53 @@ TEST_P(OVCompileAndInferRequest, AsyncInferRequest) {
ASSERT_TRUE(is_called);
}
+TEST_P(OVCompileAndInferRequest, EvictGraphMemoryThenInferAgain) {
+ OV_ASSERT_NO_THROW(execNet = core->compile_model(function, target_device, configuration));
+ ov::InferRequest req;
+ OV_ASSERT_NO_THROW(req = execNet.create_infer_request());
+ OV_ASSERT_NO_THROW(req.infer());
+
+ OV_ASSERT_NO_THROW(execNet.release_memory());
+
+ OV_ASSERT_NO_THROW(req.infer());
+}
+
+// Disabled test that checks that memory eviction works correctly and does not cause memory leaks after inference runs.
+// The test is disabled due to the fact that it requires another check of the memory since device_alloc_mem_size doesn't
+// provide memory feedback after eviction is called.
+TEST_P(OVCompileAndInferRequest, DISABLED_CheckMemoryAfterEvictGraphMemoryAndAfterRunningInferAgain) {
+ if (std::make_shared<::intel_npu::ZeroInitStructsHolder>()->getGraphDdiTable().version() < ZE_MAKE_VERSION(1, 16)) {
+ GTEST_SKIP() << "Memory eviction is not supported by the current driver version.";
+ }
+
+ OV_ASSERT_NO_THROW(execNet = core->compile_model(function, target_device, configuration));
+ ov::InferRequest req;
+ OV_ASSERT_NO_THROW(req = execNet.create_infer_request());
+ OV_ASSERT_NO_THROW(req.infer());
+
+ uint64_t device_alloc_mem_size = 0;
+ OV_ASSERT_NO_THROW(
+ device_alloc_mem_size =
+ core->get_property(target_device, ov::intel_npu::device_alloc_mem_size.name()).as<uint64_t>());
+
+ OV_ASSERT_NO_THROW(execNet.release_memory());
+
+ uint64_t device_alloc_mem_size_after_evict = 0;
+ OV_ASSERT_NO_THROW(
+ device_alloc_mem_size_after_evict =
+ core->get_property(target_device, ov::intel_npu::device_alloc_mem_size.name()).as<uint64_t>());
+
+ OV_ASSERT_NO_THROW(req.infer());
+
+ uint64_t device_alloc_mem_size_final = 0;
+ OV_ASSERT_NO_THROW(
+ device_alloc_mem_size_final =
+ core->get_property(target_device, ov::intel_npu::device_alloc_mem_size.name()).as<uint64_t>());
+
+ ASSERT_EQ(device_alloc_mem_size, device_alloc_mem_size_final);
+ ASSERT_LT(device_alloc_mem_size_after_evict, device_alloc_mem_size);
+}
+
TEST_P(OVCompileAndInferRequest, PluginWorkloadType) {
configuration[workload_type.name()] = WorkloadType::DEFAULT;
auto supportedProperties = core->get_property("NPU", supported_properties.name()).as<std::vector<PropertyName>>();