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: Handle closures just once for single-shot functions (#27132)
Dmitry Matveev899886e5fc5b
diff --git a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cppindex 4bf92588e0e..b52dd40ea59 100644--- a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp@@ -246,8 +246,6 @@ ov::npuw::CompiledModel::CompiledModel(const std::shared_ptr<ov::Model>& model,} // for(ordered_subgraphs)// NOTE(dm): there's a better way to do it, like we do in G-API backends.- m_update_required = m_cfg.get<::intel_npu::NPUW_FOLD>();-// Store mapping between manually splitted inputs/outputs// to connect tensors between compiled submodelsm_submodels_input_to_prev_output = partitioning.input_to_prev_output;diff --git a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hppindex 9f07998166e..6199ac66c0c 100644--- a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp+++ b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp@@ -149,8 +149,6 @@ private:};std::vector<CompiledModelDesc> m_compiled_submodels;- bool m_update_required;-std::function<bool(const ov::SoPtr<ov::ITensor>&, const ov::SoPtr<ov::ITensor>&)> m_acc_check;std::string m_ref_device;diff --git a/src/plugins/intel_npu/src/plugin/npuw/just_sync_infer_request.cpp b/src/plugins/intel_npu/src/plugin/npuw/just_sync_infer_request.cppindex bac69f0a3b0..4a9a3e06a0a 100644--- a/src/plugins/intel_npu/src/plugin/npuw/just_sync_infer_request.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/just_sync_infer_request.cpp@@ -187,6 +187,7 @@ ov::npuw::JustInferRequest::JustInferRequest(const std::shared_ptr<ov::npuw::Comm_func_mem_mgr.set_alloc(std::bind(&JustInferRequest::allocMem, this, _1, _2, _3));m_func_mem_mgr.assign_memory();+ m_closure_update_required = m_npuw_model->m_cfg.get<::intel_npu::NPUW_FOLD>();m_use_function_pipelining = m_npuw_model->m_cfg.get<::intel_npu::NPUW_FUNCALL_ASYNC>();if (m_use_function_pipelining) {LOG_WARN("Function call pipelining is enabled for " << m_npuw_model->m_name@@ -377,30 +378,18 @@ ov::npuw::JustInferRequest::JustInferRequest(const std::shared_ptr<ov::npuw::ComLOG_VERB("Trying to preemptively set tensors for Subgraph[" << i << "]...");LOG_BLOCK();auto& comp_model_desc = m_npuw_model->m_compiled_submodels[i];-if (!comp_model_desc.compiled_model || !comp_model_desc.replaced_by) {continue;}-const auto real_idx = comp_model_desc.replaced_by.value();auto& func_desc = m_npuw_model->m_compiled_submodels[real_idx];- auto& request = m_subrequests[real_idx];-- for (std::size_t cidx = 0u; cidx < comp_model_desc.closure.size(); cidx++) {- const auto& closure = comp_model_desc.closure[cidx];-- const auto closure_param_id = comp_model_desc.param_base + cidx;- const auto& iport = func_desc.compiled_model->inputs()[closure_param_id];-- // No update required to this tensor in runtime - so it can be set only once- // Will be utilized when there is no FOLDing- if (!m_npuw_model->m_update_required) {- // At this point closure already contains allocated and transformed tensor ready to be used- request->set_tensor(iport, ov::get_tensor_impl(closure));- }- } // for(closure)- LOG_VERB("DONE");+ // So - closure update is NOT required, OR the function is SINGLE -+ // just handle it's closure here and don't do it in runtime+ if (!m_closure_update_required || func_desc.forced_to_fcall) {+ unpack_closure(i, m_subrequests[real_idx]);+ }+ LOG_VERB("Done");}// Handle spatial dynamic submission@@ -628,11 +617,13 @@ void ov::npuw::JustInferRequest::bind_global_parameters(std::size_t idx) {// Run host-side gather, if requiredif (comp_model_desc.host_gather.dst_idx != -1) {- auto& dst = comp_model_desc.closure[comp_model_desc.host_gather.dst_idx - comp_model_desc.param_base];+ const auto& gport = comp_model_desc.compiled_model->inputs()[comp_model_desc.host_gather.dst_idx];+ const auto gather = subr->get_tensor(gport);+const auto& vocab = comp_model_desc.closure[comp_model_desc.host_gather.src_idx - comp_model_desc.param_base];const auto& lport = comp_model_desc.compiled_model->inputs()[comp_model_desc.host_gather.idx_idx];const auto lookup = subr->get_tensor(lport);- ov::npuw::util::gather(ov::get_tensor_impl(vocab), lookup, ov::get_tensor_impl(dst));+ ov::npuw::util::gather(ov::get_tensor_impl(vocab), lookup, gather);}LOG_DEBUG("Done");@@ -718,7 +709,7 @@ void ov::npuw::JustInferRequest::function_prologue(std::size_t idx) {// 2. Unpack the function closure -- right here, if pipelining if not enabled.// If it is enabled, the flow is a little bit different - see run_subrequest_for_success()// for details.- if (!is_pipelined(idx)) {+ if (!is_pipelined(idx) && m_closure_update_required && !func_desc.forced_to_fcall) {LOG_DEBUG("Unpacking closures...");LOG_BLOCK();unpack_closure(idx, m_subrequests[real_idx]);@@ -760,12 +751,20 @@ void ov::npuw::JustInferRequest::unpack_closure(std::size_t idx, RqPtr request)auto& closure = comp_model_desc.closure[cidx];const auto closure_param_id = comp_model_desc.param_base + cidx;++ if (func_desc.host_gather.dst_idx != -1 &&+ static_cast<uint64_t>(func_desc.host_gather.dst_idx) == closure_param_id) {+ // No need to set/copy the host_gather's closure tensor int+ // the subrequest - it is just a dummy. host_gather writes+ // to the right buffer directly.+ continue;+ }+auto& iport = func_desc.compiled_model->inputs()[closure_param_id];- const auto& clparam = request->get_tensor(iport);- if (closure.get_element_type() != clparam->get_element_type()) {+ if (closure.get_element_type() != iport.get_element_type()) {// Remember where the unpack is requiredclosure_unpack_required.push_back(cidx);- } else if (m_npuw_model->m_update_required) {+ } else {if (needs_copy(idx, cidx)) {// Remember where copy is requriedclosure_copy_required.push_back(cidx);diff --git a/src/plugins/intel_npu/src/plugin/npuw/just_sync_infer_request.hpp b/src/plugins/intel_npu/src/plugin/npuw/just_sync_infer_request.hppindex fb105fd7629..d219f170a8e 100644--- a/src/plugins/intel_npu/src/plugin/npuw/just_sync_infer_request.hpp+++ b/src/plugins/intel_npu/src/plugin/npuw/just_sync_infer_request.hpp@@ -153,6 +153,9 @@ private:// Represents spatial run-time inforuntime::spatial::Selector::Ptr m_spatial_selector;++ // Cached check if we do FOLDing and need to update closures in the repeating blocks+ bool m_closure_update_required = false;};} // namespace npuwdiff --git a/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.cpp b/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.cppindex c4eb5feb732..954df49e39f 100644--- a/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.cpp@@ -1892,6 +1892,12 @@ void Partitioner::optimize(const std::string& func_name) {auto new_elem_type = params_to_gather.pnew->get_element_type();const auto& new_shape = params_to_gather.pnew->get_shape();// Note: no allocation needed for this tensor - set to _closure and dummy in _lazy_closure+ // FIXME: It turns out this tensor will be completely unused.+ // It will just sit in the memory to do nothing.+ // Most likely it may stay empty since we need a 1:1 matching between+ // closure tensors and parameters (minus base).+ // Based on our logic (when tensors get transferred from lazy tensors via bank+ // to the closure), this tensor should be non-empty to avoid this process.funcall.get()._closure.push_back(ov::Tensor(new_elem_type, new_shape));funcall.get()._lazy_closure.push_back(LazyTensor(TransformType::THIS, ov::Tensor()));}