Skip to content

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

NPUW: Various fixes for FUNCALL_FOR_ALL (#26937)

Dmitry Matveev03617144a0d0
diff --git a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp
index dc326b22f3a..1c75b1cbdf3 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp
@@ -308,6 +308,7 @@ ov::npuw::CompiledModel::CompiledModel(const std::shared_ptr<ov::Model>& model,
m_compiled_submodels[id].lazy_closure = subgraph._lazy_closure;
m_compiled_submodels[id].scales = subgraph._scales;
m_compiled_submodels[id].zerops = subgraph._zerops;
+ m_compiled_submodels[id].forced_to_fcall = subgraph._forced_to_fcall;
m_compiled_submodels[id].is_remote.resize(subgraph._lazy_closure.size(), false);
} // if(!funcall)
@@ -784,7 +785,6 @@ void ov::npuw::CompiledModel::implement_properties() {
// 1.
// OV Public
- // ===============================================
m_prop_to_opt = {{ov::supported_properties.name(),
{ov::PropertyMutability::RO,
[&](const ::intel_npu::Config&) -> std::vector<PropertyName>& {
@@ -821,7 +821,6 @@ void ov::npuw::CompiledModel::implement_properties() {
return m_loaded_from_cache;
}}},
// OV Public Hints
- // =====================================================
{ov::hint::performance_mode.name(),
{ov::PropertyMutability::RO,
[&](const ::intel_npu::Config&) {
diff --git a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp
index 2a89230fefa..7f308d46094 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp
@@ -144,6 +144,8 @@ private:
std::vector<ov::Tensor> zerops;
std::vector<bool> is_remote;
+ bool forced_to_fcall = false;
+
// FIXME: Take it out of structure
ov::SoPtr<ov::ICompiledModel> ref_compiled_model;
bool switched_to_ref = false;
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.cpp
index a853a27b194..93f9f12fe86 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
@@ -53,7 +53,6 @@ ov::npuw::JustInferRequest::JustInferRequest(const std::shared_ptr<ov::npuw::Com
const auto real_idx = comp_model_desc.replaced_by.value();
auto& proto_comp_model_desc = m_npuw_model->m_compiled_submodels[real_idx];
auto& proto_comp_model = proto_comp_model_desc.compiled_model;
-
const auto num_outputs = proto_comp_model->outputs().size();
// Initialize the spatial IO placeholders, if required
@@ -100,12 +99,13 @@ ov::npuw::JustInferRequest::JustInferRequest(const std::shared_ptr<ov::npuw::Com
} // if(replaced_by)
// Special cases are handled -- so nothing to do here
+ const bool is_piped = is_pipelined(i);
bool recompiled = false;
- auto rqs = create_infer_requests(i, m_use_function_pipelining ? 2 : 1, &recompiled);
+ auto rqs = create_infer_requests(i, is_piped ? 2 : 1, &recompiled);
failover_happened |= recompiled;
m_subrequests[i] = rqs.at(0);
m_subrequest_devices[i] = *comp_model_desc.device_it;
- if (comp_model_desc.replaced_by && m_use_function_pipelining) {
+ if (is_piped) {
m_funcall_pipeline[i].subrequest = rqs.at(1);
}
@@ -126,6 +126,10 @@ ov::npuw::JustInferRequest::JustInferRequest(const std::shared_ptr<ov::npuw::Com
for (std::size_t i = 0; i < m_num_submodels; i++) {
auto& comp_model_desc = m_npuw_model->m_compiled_submodels[i];
if (comp_model_desc.replaced_by) { // a function call..
+ if (!is_pipelined(i)) {
+ LOG_INFO("Skip subgraph[" << i << "] as it is a single-call function");
+ continue;
+ }
// Use real_id to accumulate information about
// different functions
const auto real_id = comp_model_desc.replaced_by.value();
@@ -368,8 +372,6 @@ void ov::npuw::JustInferRequest::bind_global_parameters(std::size_t idx) {
auto& comp_model_desc = m_npuw_model->m_compiled_submodels[idx];
const auto real_idx = comp_model_desc.replaced_by.value_or(idx);
- LOG_DEBUG("Real idx is..." << real_idx);
-
const bool do_copy = needs_copy(idx);
const auto& iodesc = m_subrequests_gio.at(idx);
@@ -381,7 +383,7 @@ void ov::npuw::JustInferRequest::bind_global_parameters(std::size_t idx) {
// pick which subrequest we actually work on here
auto subr = [&]() {
- if (now_idx() && real_idx == real(now_idx().value()) && m_use_function_pipelining) {
+ if (now_idx() && real_idx == real(now_idx().value()) && is_pipelined(now_idx().value())) {
LOG_DEBUG("Accessing the pipeline subrequest");
// The real index of request we need to prepare IS
// the same request which executes now AND
@@ -531,7 +533,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 (!m_use_function_pipelining) {
+ if (!is_pipelined(idx)) {
LOG_DEBUG("Unpacking closures...");
LOG_BLOCK();
unpack_closure(idx, m_subrequests[real_idx]);
@@ -635,7 +637,8 @@ void ov::npuw::JustInferRequest::recreate_subrequests(std::size_t idx) {
auto& comp_model_desc = m_npuw_model->m_compiled_submodels[idx];
auto real_idx = comp_model_desc.replaced_by.value_or(idx);
- auto new_rqs = create_infer_requests(idx, m_use_function_pipelining ? 2 : 1);
+ const auto is_piped = is_pipelined(idx);
+ auto new_rqs = create_infer_requests(idx, is_piped ? 2 : 1);
// NB: Regardless if this subrequest was a function call
// or not, always use the real_idx here - for regular
@@ -643,7 +646,7 @@ void ov::npuw::JustInferRequest::recreate_subrequests(std::size_t idx) {
// is critical here to update the function body, not the
// function calls (which are left empty now in the vector)
m_subrequests[real_idx] = new_rqs.at(0);
- if (comp_model_desc.replaced_by && m_use_function_pipelining) {
+ if (is_piped) {
m_funcall_pipeline[real_idx].subrequest = new_rqs.at(1);
}
// After an infer request is recreated, the internal cross-request
@@ -717,7 +720,7 @@ void ov::npuw::JustInferRequest::run_subrequest_for_success(std::size_t idx, boo
if (job_done) {
dump_output_tensors(idx); // FIXME: Called here unconditionally, need to refactor
- if (m_use_function_pipelining && m_funcall_pipeline[idx].next) {
+ if (is_pipelined(idx) && m_funcall_pipeline[idx].next) {
// Swap the next (pipelined, semi-prepared) infer request in the chain
// with the default (to be accessed next) one.
std::swap(m_subrequests[real_idx], m_funcall_pipeline[real_idx].subrequest);
@@ -860,7 +863,7 @@ void ov::npuw::JustInferRequest::unsafe_run_this_prep_next(std::size_t idx, bool
// The next subgraph is a call to the same function...
// At this point, THIS infer request is already prepared.
// Run it, then prepare it again for the next entrace
- if (m_use_function_pipelining) {
+ if (is_pipelined(real_idx)) {
// function pipelining is here! and the next rq is ours.
NPUW_ASSERT(m_funcall_pipeline[idx].next.value() == next_idx);
unsafe_during(real_idx, [&]() {
@@ -891,7 +894,7 @@ void ov::npuw::JustInferRequest::unsafe_run_this_prep_next(std::size_t idx, bool
bind_global_parameters(next_idx);
next_prepared = true;
}
- if (m_use_function_pipelining && m_funcall_pipeline[idx].next) {
+ if (is_pipelined(idx) && m_funcall_pipeline[idx].next) {
const auto my_next_idx = m_funcall_pipeline[idx].next.value();
LOG_DEBUG("Unpacking closures for the NEXT subrequest[" << my_next_idx << "]...");
LOG_BLOCK();
@@ -939,3 +942,8 @@ bool ov::npuw::JustInferRequest::supports_async_pipeline() const {
void ov::npuw::JustInferRequest::update_subrequest_links(std::size_t) {
connect_subrequests();
}
+
+bool ov::npuw::JustInferRequest::is_pipelined(std::size_t idx) const {
+ const auto& desc = m_npuw_model->m_compiled_submodels[real(idx)];
+ return m_use_function_pipelining && desc.replaced_by && !desc.forced_to_fcall;
+}
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.hpp
index bb75eb69d0e..2544647dd00 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
@@ -66,6 +66,7 @@ private:
using TensorPtr = ov::SoPtr<ov::ITensor>;
std::map<LinkFrom, TensorPtr> m_funcall_result;
+ bool is_pipelined(std::size_t idx) const;
bool m_use_function_pipelining = false;
struct FuncallPipeline {
// A "brother" subrequest for a "primary" subrequest. Initialized only
diff --git a/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.cpp b/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.cpp
index cbe9463b265..58a8219f497 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.cpp
@@ -364,6 +364,7 @@ void Partitioner::identifySubgraphs() {
group_nodes.insert(it->second);
}
group.sg._repeated_id = group.repeated_id;
+ group.sg._forced_to_fcall = group.forced_to_fcall;
group.sg._gflops = group.gflops;
group.sg._ops = group.all_layers.size();
P.total_ops += group.sg._ops;
@@ -1473,6 +1474,7 @@ void Partitioner::createFunction(FunctionPipeline& func_ggg) {
funcall._gflops = body_sg._gflops; // preserving this is required for proper stats
funcall._ops = body_sg._ops; // preserving this is requried for proper stats
funcall._avoid_list = body_sg._avoid_list;
+ funcall._forced_to_fcall = body_sg._forced_to_fcall;
// Declare a new function AND record a function call
ov::npuw::Function function;
@@ -1648,6 +1650,7 @@ void Partitioner::matchRepeatedSubgraphs(const std::string& func_name) {
funcall._gflops = this_sg._gflops; // duplicated code again!
funcall._ops = this_sg._ops; // duplicated code again!
funcall._avoid_list = this_sg._avoid_list; // duplicated code again!
+ funcall._forced_to_fcall = this_sg._forced_to_fcall;
rearrange_to_function_protocol(this_sg, body_params, funcall._parameters, func_ggg.param_call_to_proto, true);
rearrange_to_function_protocol(this_sg, body_results, funcall._results, func_ggg.result_call_to_proto);
@@ -2047,7 +2050,7 @@ void Partitioner::decompressionCutOff(const std::string& func_name) {
}
// Finally, remove the function body's parameters here
- ov::npuw::patterns::finalize_remap(f, closure_remap);
+ ov::npuw::patterns::finalize_remap(f, func_group.refs.front(), closure_remap);
} // if (CAST_SCALE && have(params_to_scale))
}
LOG_DEBUG("Function model inputs after the DCOFF:");
@@ -2182,6 +2185,8 @@ ov::npuw::Partitioning ov::npuw::getPartitioning(const std::shared_ptr<ov::Model
LOG_INFO("Turning block " << gid << " into a function " << this_group.repeated_id << "...");
LOG_BLOCK();
this_group.repeated_id = std::move(new_id);
+ this_group.forced_to_fcall = true;
+
ov::npuw::RepeatedBlock this_block;
for (const auto& layer : this_group.all_layers) {
// Note: NOT move(layer)! It breaks the code here.
diff --git a/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.hpp b/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.hpp
index 86394617927..f60c6eff62e 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.hpp
@@ -37,8 +37,8 @@ struct Subgraph {
//
// FIXME: Replace with variant or some other proper way (maybe
// even a class hierarchy)
- std::string _repeated_id;
- std::string _funcall;
+ std::string _repeated_id; // FIXME: What's the difference
+ std::string _funcall; // ..between these two?
std::vector<ov::Tensor> _closure;
std::vector<ov::Tensor> _scales; // Scale coeffs for manual unpacking
std::vector<ov::Tensor> _zerops; // Zero points for manual unpacking
@@ -46,6 +46,8 @@ struct Subgraph {
// Stores transformation history for weights which will be applied before inference
std::vector<weights::LazyTensor> _lazy_closure;
+ bool _forced_to_fcall = false;
+
struct Gather {
// NB.: int64_t is strange but it is used by OV to refer to parameters
int64_t dst_idx = -1;
@@ -95,6 +97,11 @@ struct Group {
std::string avoid_list;
std::string tag;
+ // Set to true if the Group was forcibly turned to functon. Such
+ // function has just a single associated funcall and are subjects
+ // to some optimizations (simplifications).
+ bool forced_to_fcall = false;
+
ov::npuw::Subgraph sg;
};
diff --git a/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/dcoff.cpp b/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/dcoff.cpp
index 887670ca1e6..60f705a0c8f 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/dcoff.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/dcoff.cpp
@@ -147,14 +147,40 @@ void apply_remap(Subgraph& fcall, const ClosureRemap& m) {
fcall._lazy_closure = std::move(new_lazy_closure);
}
-void finalize_remap(Function& fbody, const ClosureRemap& m) {
+void finalize_remap(Function& fbody, Subgraph& fsg, const ClosureRemap& m) {
LOG_DEBUG("Removing retired parameters...");
LOG_BLOCK();
+
+ // Unfortunate truth - this function has to be aware of the
+ // Host Gather existence to properly update indices after
+ // Remap.
+ using PPtr = std::shared_ptr<ov::op::v0::Parameter>;
+ struct GatherParams {
+ PPtr pidx; // Parameter @ function body - input_ids
+ PPtr psrc; // Parameter @ function body - vocab tensor
+ PPtr pdst; // Parameter @ function body - gathered ids
+ };
+ GatherParams gather_params;
+ const auto& params = fbody._model->get_parameters();
+ if (fsg._host_gather.dst_idx != -1) {
+ gather_params = GatherParams{params[fsg._host_gather.idx_idx],
+ params[fsg._host_gather.src_idx],
+ params[fsg._host_gather.dst_idx]};
+ }
+
for (auto&& p : m.params_to_remove) {
LOG_DEBUG("Removing parameter " << p);
LOG_BLOCK();
fbody._model->remove_parameter(p);
}
+
+ // Update indices for gather
+ if (fsg._host_gather.dst_idx != -1) {
+ fsg._host_gather.idx_idx = fbody._model->get_parameter_index(gather_params.pidx);
+ fsg._host_gather.src_idx = fbody._model->get_parameter_index(gather_params.psrc);
+ fsg._host_gather.dst_idx = fbody._model->get_parameter_index(gather_params.pdst);
+ }
+
fbody._model->validate_nodes_and_infer_types();
LOG_DEBUG("DONE");
}
diff --git a/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/dcoff.hpp b/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/dcoff.hpp
index 3c61526be71..55ec9ccd588 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/dcoff.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/dcoff.hpp
@@ -47,7 +47,7 @@ struct ClosureRemap {
ClosureRemap build_remap(const Function& fbody, const DCOFFParams& p);
void apply_remap(Subgraph& fcall, const ClosureRemap& m);
-void finalize_remap(Function& fbody, const ClosureRemap& m);
+void finalize_remap(Function& fbody, Subgraph& fsg, const ClosureRemap& m);
// Various patterns here