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
NPUW: Fix memory clean-up for MoE model parts (-11GB) (#35910)
Dmitry Matveevd79503638633
diff --git a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cppindex 8aa35535b2c..0d0fe2fed5f 100644--- a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp@@ -579,6 +579,7 @@ ov::npuw::CompiledModel::CompiledModel(const std::shared_ptr<ov::Model>& model,if (fcn_template._pipeline.compile_stage) {fcn_template._pipeline.compile_stage(m_compiled_submodels[id].pipeline,m_compiled_submodels[id].pipeline.context);+ ov::npuw::moe::clear_partition_state(fcn_template._pipeline.context);}} else {const auto real_id = m_compiled_submodels[id].replaced_by.value();diff --git a/src/plugins/intel_npu/src/plugin/npuw/moe/moe_subgraph.cpp b/src/plugins/intel_npu/src/plugin/npuw/moe/moe_subgraph.cppindex 0823e7836c6..366f6e0856c 100644--- a/src/plugins/intel_npu/src/plugin/npuw/moe/moe_subgraph.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/moe/moe_subgraph.cpp@@ -112,7 +112,11 @@ void compile_transformed_expert_models(const CompiledExpertsState& runtime_experOPENVINO_ASSERT(runtime_experts != nullptr, "Expected compiled MoE experts state");LOG_INFO("Compiling MoE expert models...");LOG_BLOCK();- for (const auto& entry : runtime_experts->_models_to_compile) {+ // Compile from a snapshot so set_compiled_model() can erase the temporary+ // source ov::Model safely. Erasing from the original map while iterating it+ // is undefined behavior and may leave transformed expert models retained.+ const auto models_to_compile = runtime_experts->_models_to_compile;+ for (const auto& entry : models_to_compile) {runtime_experts->set_compiled_model(entry.first,compile_ctx.compile_model(entry.second, "/moe_chunk_" + std::to_string(entry.first), compile_ctx.devices));@@ -133,6 +137,10 @@ void configure_expert_compile(ov::npuw::v1::subgraphs::CompiledPipeline& compileconst auto& models_to_compile = compiled_experts->_models_to_compile;OPENVINO_ASSERT(!models_to_compile.empty(), "Fatal: MoEExperts has no models to compile!");put_compiled_experts(compiled_context, compiled_experts);+ // The compiled state owns the temporary models needed for compilation. Drop the+ // partition-time transform state so those ov::Model instances do not stay pinned+ // in the persistent subgraph context after compilation finishes.+ compiled_context.erase<ov::npuw::function::MoEExperts>();attach_runtime_behavior(compiled_pipeline, compiled_context, BehaviorRole::EXPERTS, true);compiled_pipeline.compile_executor = [compiled_experts](ov::npuw::v1::subgraphs::CompileContext& compile_ctx) {@@ -175,6 +183,10 @@ void configure_downstream_compile(ov::npuw::v1::subgraphs::CompiledPipeline& com}auto compiled_downstream = std::make_shared<ov::npuw::compiled::MoEDownstream>(*downstream);put_compiled_downstream(compiled_context, compiled_downstream);+ // Keep only the compiled/runtime representation in the persistent context.+ // The transform-time holder still owns the modified ov::Model and would keep it+ // alive for the whole CompiledModel lifetime otherwise.+ compiled_context.erase<ov::npuw::function::MoEDownstream>();attach_runtime_behavior(compiled_pipeline, compiled_context, BehaviorRole::DOWNSTREAM, true);const auto previous_compile_executor = compiled_pipeline.compile_executor;@@ -222,6 +234,11 @@ bool has_compiled_state(const v1::subgraphs::CompiledPipeline& pipeline) {return has_compiled_experts(pipeline) || has_compiled_downstream(pipeline);}+void clear_partition_state(v1::subgraphs::Context& context) {+ context.erase<ov::npuw::function::MoEExperts>();+ context.erase<ov::npuw::function::MoEDownstream>();+}+void serialize_compiled_state(v1::subgraphs::Context& context,ov::npuw::s11n::Stream& stream,const ov::npuw::s11n::SubmodelDeserializeCtx* submodel_ctx) {diff --git a/src/plugins/intel_npu/src/plugin/npuw/moe/moe_subgraph.hpp b/src/plugins/intel_npu/src/plugin/npuw/moe/moe_subgraph.hppindex d2933950818..2aa5a8b8b16 100644--- a/src/plugins/intel_npu/src/plugin/npuw/moe/moe_subgraph.hpp+++ b/src/plugins/intel_npu/src/plugin/npuw/moe/moe_subgraph.hpp@@ -47,6 +47,8 @@ bool has_compiled_experts(const v1::subgraphs::CompiledPipeline& pipeline);bool has_compiled_downstream(const v1::subgraphs::CompiledPipeline& pipeline);bool has_compiled_state(const v1::subgraphs::CompiledPipeline& pipeline);+void clear_partition_state(v1::subgraphs::Context& context);+void serialize_compiled_state(v1::subgraphs::Context& context,ov::npuw::s11n::Stream& stream,const ov::npuw::s11n::SubmodelDeserializeCtx* submodel_ctx);