Skip to content

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

[NPUW] Enable quantized host gather by default (#31336)

Alexey Smirnovc619ac6a5960
diff --git a/src/plugins/intel_npu/src/al/include/intel_npu/config/npuw.hpp b/src/plugins/intel_npu/src/al/include/intel_npu/config/npuw.hpp
index fe9be6feb0f..6bae3fdd1e9 100644
--- a/src/plugins/intel_npu/src/al/include/intel_npu/config/npuw.hpp
+++ b/src/plugins/intel_npu/src/al/include/intel_npu/config/npuw.hpp
@@ -82,7 +82,6 @@ DEFINE_OPT(NPUW_DQ_FULL, bool, true, npuw::partitioning::dyn_quant_full, RunTime
DEFINE_OPT(NPUW_PMM, std::string, "2", npuw::partitioning::par_matmul_merge_dims, RunTime);
DEFINE_OPT(NPUW_SLICE_OUT, bool, false, npuw::partitioning::slice_out, RunTime);
DEFINE_OPT(NPUW_HOST_GATHER, bool, true, npuw::partitioning::host_gather, RunTime);
-DEFINE_OPT(NPUW_HOST_GATHER_QUANT, bool, false, npuw::partitioning::gather_quant, RunTime);
DEFINE_OPT(NPUW_SPATIAL, bool, false, npuw::partitioning::spatial, RunTime);
DEFINE_OPT(NPUW_F16IC, bool, true, npuw::partitioning::f16_interconnect, RunTime);
DEFINE_OPT(NPUW_SPATIAL_NWAY, std::size_t, 128, npuw::partitioning::spatial_nway, RunTime);
diff --git a/src/plugins/intel_npu/src/al/include/intel_npu/npuw_private_properties.hpp b/src/plugins/intel_npu/src/al/include/intel_npu/npuw_private_properties.hpp
index fd6b5de8239..2863c86a900 100644
--- a/src/plugins/intel_npu/src/al/include/intel_npu/npuw_private_properties.hpp
+++ b/src/plugins/intel_npu/src/al/include/intel_npu/npuw_private_properties.hpp
@@ -250,14 +250,6 @@ static constexpr ov::Property<bool> f16_interconnect{"NPUW_F16IC"};
*/
static constexpr ov::Property<bool> host_gather{"NPUW_HOST_GATHER"};
-/**
- * @brief
- * Type: boolean
- * When applicable, do embedding gather on host but leave it quantized.
- * Default value: false.
- */
-static constexpr ov::Property<bool> gather_quant{"NPUW_HOST_GATHER_QUANT"};
-
/**
* @brief
* Type: std::string.
diff --git a/src/plugins/intel_npu/src/al/src/config/npuw.cpp b/src/plugins/intel_npu/src/al/src/config/npuw.cpp
index cd7131cc238..5e5350008ae 100644
--- a/src/plugins/intel_npu/src/al/src/config/npuw.cpp
+++ b/src/plugins/intel_npu/src/al/src/config/npuw.cpp
@@ -34,7 +34,6 @@ void intel_npu::registerNPUWOptions(OptionsDesc& desc) {
desc.add<NPUW_SPATIAL_NWAY>();
desc.add<NPUW_SPATIAL_DYN>();
desc.add<NPUW_HOST_GATHER>();
- desc.add<NPUW_HOST_GATHER_QUANT>();
desc.add<NPUW_F16IC>();
desc.add<NPUW_DCOFF_TYPE>();
desc.add<NPUW_DCOFF_SCALE>();
diff --git a/src/plugins/intel_npu/src/plugin/npuw/base_sync_infer_request.cpp b/src/plugins/intel_npu/src/plugin/npuw/base_sync_infer_request.cpp
index cecaff60de5..e660ada1824 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/base_sync_infer_request.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/base_sync_infer_request.cpp
@@ -277,6 +277,15 @@ void ov::npuw::IBaseInferRequest::alloc_io() {
auto tensor = alloc_global_out(i);
m_port_to_tensor[port] = TensorStorage{tensor, true};
}
+
+ // Try to allocate intermediate tensors to gather into, when host quant gather is enabled
+ for (size_t i = 0; i < m_num_submodels; i++) {
+ auto& comp_model_desc = m_npuw_model->m_compiled_submodels[i];
+ if (!comp_model_desc.compiled_model && !comp_model_desc.replaced_by) {
+ continue; // Optimized out
+ }
+ alloc_quant_gather_tensors(i, m_subrequests[i]);
+ }
}
ov::npuw::TensorPtr ov::npuw::IBaseInferRequest::alloc_global_out(std::size_t out_idx) {
@@ -473,20 +482,17 @@ void ov::npuw::IBaseInferRequest::bind_global_params(std::size_t idx, RqPtr requ
LOG_DEBUG("Done");
}
-void ov::npuw::IBaseInferRequest::handle_quant_host_gather(std::size_t idx, RqPtr request) {
+void ov::npuw::IBaseInferRequest::alloc_quant_gather_tensors(std::size_t idx, RqPtr request) {
auto& comp_model_desc = m_npuw_model->m_compiled_submodels[idx];
+ auto& quant_unpack_gather = comp_model_desc.quant_unpack_gather;
- if (comp_model_desc.quant_unpack_gather.dst_idx != -1) {
- NPUW_ASSERT(comp_model_desc.quant_unpack_gather.idx_idx != -1 &&
- comp_model_desc.quant_unpack_gather.src_w_idx != -1);
+ if (quant_unpack_gather.dst_idx != -1) {
+ NPUW_ASSERT(quant_unpack_gather.idx_idx != -1 && quant_unpack_gather.src_w_idx != -1);
- const auto& lport = comp_model_desc.compiled_model->inputs()[comp_model_desc.quant_unpack_gather.idx_idx];
+ const auto& lport = comp_model_desc.compiled_model->inputs()[quant_unpack_gather.idx_idx];
const auto& lookup = request->get_tensor(lport);
- const auto& gport = comp_model_desc.compiled_model->inputs()[comp_model_desc.quant_unpack_gather.dst_idx];
- const auto& gather = request->get_tensor(gport);
-
- const auto& wport = comp_model_desc.compiled_model->inputs()[comp_model_desc.quant_unpack_gather.src_w_idx];
+ const auto& wport = comp_model_desc.compiled_model->inputs()[quant_unpack_gather.src_w_idx];
const auto& vocabw = request->get_tensor(wport);
auto ids_shape = lookup->get_shape();
@@ -495,42 +501,74 @@ void ov::npuw::IBaseInferRequest::handle_quant_host_gather(std::size_t idx, RqPt
return ov::Shape{1, ids_shape[1], shape.size() == 3 ? shape[1] * shape[2] : shape[1]};
};
- ov::Tensor gatherw(vocabw->get_element_type(), get_gathered_shape(vocabw->get_shape()));
+ m_quant_gather_tensors.w = ov::Tensor(vocabw->get_element_type(), get_gathered_shape(vocabw->get_shape()));
+
+ if (quant_unpack_gather.src_z_idx != -1 && quant_unpack_gather.src_s_idx != -1) {
+ const auto& zport = comp_model_desc.compiled_model->inputs()[quant_unpack_gather.src_z_idx];
+ const auto& vocabz = request->get_tensor(zport);
+
+ const auto& sport = comp_model_desc.compiled_model->inputs()[quant_unpack_gather.src_s_idx];
+ const auto& vocabs = request->get_tensor(sport);
+
+ m_quant_gather_tensors.z = ov::Tensor(vocabz->get_element_type(), get_gathered_shape(vocabz->get_shape()));
+ m_quant_gather_tensors.s = ov::Tensor(vocabs->get_element_type(), get_gathered_shape(vocabs->get_shape()));
+ } else if (quant_unpack_gather.src_s_idx != -1) {
+ const auto& sport = comp_model_desc.compiled_model->inputs()[quant_unpack_gather.src_s_idx];
+ const auto& vocabs = request->get_tensor(sport);
+
+ m_quant_gather_tensors.s = ov::Tensor(vocabs->get_element_type(), get_gathered_shape(vocabs->get_shape()));
+ }
+ }
+}
+
+void ov::npuw::IBaseInferRequest::handle_quant_host_gather(std::size_t idx, RqPtr request) {
+ auto& comp_model_desc = m_npuw_model->m_compiled_submodels[idx];
+ auto& quant_unpack_gather = comp_model_desc.quant_unpack_gather;
+
+ if (quant_unpack_gather.dst_idx != -1) {
+ NPUW_ASSERT(quant_unpack_gather.idx_idx != -1 && quant_unpack_gather.src_w_idx != -1);
+
+ const auto& lport = comp_model_desc.compiled_model->inputs()[quant_unpack_gather.idx_idx];
+ const auto& lookup = request->get_tensor(lport);
+
+ const auto& gport = comp_model_desc.compiled_model->inputs()[quant_unpack_gather.dst_idx];
+ const auto& gather = request->get_tensor(gport);
+
+ const auto& wport = comp_model_desc.compiled_model->inputs()[quant_unpack_gather.src_w_idx];
+ const auto& vocabw = request->get_tensor(wport);
+
// Gather weight
- ov::npuw::util::gather(vocabw, lookup, ov::get_tensor_impl(gatherw));
+ ov::npuw::util::gather(vocabw, lookup, ov::get_tensor_impl(m_quant_gather_tensors.w));
- if (comp_model_desc.quant_unpack_gather.src_z_idx != -1 &&
- comp_model_desc.quant_unpack_gather.src_s_idx != -1) {
- const auto& zport = comp_model_desc.compiled_model->inputs()[comp_model_desc.quant_unpack_gather.src_z_idx];
+ if (quant_unpack_gather.src_z_idx != -1 && quant_unpack_gather.src_s_idx != -1) {
+ const auto& zport = comp_model_desc.compiled_model->inputs()[quant_unpack_gather.src_z_idx];
const auto& vocabz = request->get_tensor(zport);
- const auto& sport = comp_model_desc.compiled_model->inputs()[comp_model_desc.quant_unpack_gather.src_s_idx];
+ const auto& sport = comp_model_desc.compiled_model->inputs()[quant_unpack_gather.src_s_idx];
const auto& vocabs = request->get_tensor(sport);
- ov::Tensor gatherz(vocabz->get_element_type(), get_gathered_shape(vocabz->get_shape()));
- ov::Tensor gathers(vocabs->get_element_type(), get_gathered_shape(vocabs->get_shape()));
// Gather first
- ov::npuw::util::gather(vocabz, lookup, ov::get_tensor_impl(gatherz));
- ov::npuw::util::gather(vocabs, lookup, ov::get_tensor_impl(gathers));
+ ov::npuw::util::gather(vocabz, lookup, ov::get_tensor_impl(m_quant_gather_tensors.z));
+ ov::npuw::util::gather(vocabs, lookup, ov::get_tensor_impl(m_quant_gather_tensors.s));
// Then unpack
- ov::npuw::util::unpack(ov::get_tensor_impl(gatherw),
- ov::get_tensor_impl(gatherz),
- ov::get_tensor_impl(gathers),
+ ov::npuw::util::unpack(ov::get_tensor_impl(m_quant_gather_tensors.w),
+ ov::get_tensor_impl(m_quant_gather_tensors.z),
+ ov::get_tensor_impl(m_quant_gather_tensors.s),
gather);
- } else if (comp_model_desc.quant_unpack_gather.src_s_idx != -1) {
- const auto& sport = comp_model_desc.compiled_model->inputs()[comp_model_desc.quant_unpack_gather.src_s_idx];
+ } else if (quant_unpack_gather.src_s_idx != -1) {
+ const auto& sport = comp_model_desc.compiled_model->inputs()[quant_unpack_gather.src_s_idx];
const auto& vocabs = request->get_tensor(sport);
- ov::Tensor gathers(vocabs->get_element_type(), get_gathered_shape(vocabs->get_shape()));
// Gather first
- ov::npuw::util::gather(vocabs, lookup, ov::get_tensor_impl(gathers));
+ ov::npuw::util::gather(vocabs, lookup, ov::get_tensor_impl(m_quant_gather_tensors.s));
// Then unpack
- ov::npuw::util::unpack(ov::get_tensor_impl(gatherw), ov::get_tensor_impl(gathers), gather);
+ ov::npuw::util::unpack(ov::get_tensor_impl(m_quant_gather_tensors.w),
+ ov::get_tensor_impl(m_quant_gather_tensors.s),
+ gather);
} else {
- // Already gathered above - just unpack
- ov::npuw::util::unpack(ov::get_tensor_impl(gatherw), gather);
+ NPUW_ASSERT(false && "Not supported");
}
}
}
diff --git a/src/plugins/intel_npu/src/plugin/npuw/base_sync_infer_request.hpp b/src/plugins/intel_npu/src/plugin/npuw/base_sync_infer_request.hpp
index 8e7c56c14eb..844d6d95d99 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/base_sync_infer_request.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/base_sync_infer_request.hpp
@@ -97,6 +97,11 @@ protected:
// FROM(Every subrequests' output port) TO(Its output tensor)
std::map<ov::Output<const ov::Node>, TensorStorage> m_port_to_tensor;
+ struct QuantGatherTensors {
+ ov::Tensor w, z, s;
+ };
+ QuantGatherTensors m_quant_gather_tensors;
+
// FIXME: Currently is initialized/managed by subclass as well.
// Moved here dumping purposes only
// Another sparse vector. Represents populated spatial I/O parameters
@@ -143,6 +148,7 @@ protected:
void unpack_closure(std::size_t idx, RqPtr request);
virtual void bind_global_params(std::size_t idx, RqPtr request);
virtual void bind_global_results(std::size_t idx, RqPtr request);
+ void alloc_quant_gather_tensors(std::size_t idx, RqPtr request);
void handle_quant_host_gather(std::size_t idx, RqPtr request);
void dump_input_tensors(std::size_t idx);
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 37898b24563..291cf649155 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp
@@ -222,7 +222,11 @@ ov::npuw::CompiledModel::CompiledModel(const std::shared_ptr<ov::Model>& model,
// Store original constants' offset for serialization purposes
store_const_offsets(model);
- auto partitioning = getPartitioning(model, m_cfg);
+ ov::npuw::PartitioningContext ctx;
+ // Identify based on compiler version, user config and pattern
+ ctx.use_host_gather_quant = should_use_quantized_host_gather(model, npuw_props);
+
+ auto partitioning = getPartitioning(model, m_cfg, ctx);
m_total_stat.gflops = partitioning.total_gflops;
m_total_stat.ops = partitioning.total_ops;
const std::vector<ov::npuw::Subgraph>& orderedSubgraphs = partitioning.subgraphs;
@@ -526,6 +530,69 @@ ov::npuw::CompiledModel::CompiledModel(const std::shared_ptr<ov::Model>& model,
LOG_DEBUG("CompiledModel is being deserialized, skipping the full constructor flow...");
}
+bool ov::npuw::CompiledModel::should_use_quantized_host_gather(const std::shared_ptr<ov::Model>& model,
+ const ov::AnyMap& properties) const {
+ LOG_INFO("Identifying best HOST_GATHER config value...");
+ LOG_BLOCK();
+ // Check if was explicitly specified
+ auto it_hg = properties.find(intel_npu::npuw::partitioning::host_gather.name());
+ std::optional<bool> explicit_hg_value;
+ if (it_hg != properties.end()) {
+ explicit_hg_value = it_hg->second.as<bool>();
+ }
+
+ // Check NPUW_HOST_GATHER:QUANT based on the patterns (for tail vocab)
+ ov::npuw::patterns::opt::Context ctx;
+ ov::pass::GraphRewrite rewr;
+ rewr.add_matcher<ov::npuw::patterns::opt::HostGatherQuantAsymm<ov::op::v0::Constant>>(std::ref(ctx), true);
+ rewr.add_matcher<ov::npuw::patterns::opt::HostGatherQuantSymm<ov::op::v0::Constant>>(std::ref(ctx), true);
+ rewr.run_on_model(model);
+
+ using CPtr = std::shared_ptr<ov::op::v0::Constant>;
+ std::vector<CPtr> to_keep;
+
+ ov::pass::GraphRewrite rewr2;
+ rewr2.add_matcher<ov::npuw::patterns::opt::PreserveConstDictMatMulAsymm>(std::ref(to_keep));
+ rewr2.add_matcher<ov::npuw::patterns::opt::PreserveConstDictMatMulSymm>(std::ref(to_keep));
+ rewr2.run_on_model(model);
+ // FIXME: since 3-model pipeline is the default option, the tail will be separate,
+ // so we need to match either head or tail pattern here for host gather quantized feature to work.
+ // However, there might be a case where tail pattern is matched, but head is not (for the same model)
+ // or vice versa. This would lead to worse performance. Consider adding this check to LLMCompiledModel
+ // as well, since there we have uncut model.
+ // Head or tail
+ const bool pattern_matched = ctx.found_host_gather_quant() || !to_keep.empty();
+
+ // Check the compiler version
+ const auto npu_devices = get_plugin()->get_core()->get_property("NPU", ov::available_devices);
+ const bool compiler_version_enough =
+ !npu_devices.empty() &&
+ get_plugin()->get_core()->get_property("NPU", ov::intel_npu::compiler_version) >= ONEAPI_MAKE_VERSION(7, 21);
+
+ const bool can_enable_hgq = pattern_matched && (compiler_version_enough || npu_devices.empty());
+
+ // Now make a decision based on the checks above
+ if (!explicit_hg_value) {
+ // Default value is used, can force the best option
+ if (can_enable_hgq) {
+ LOG_INFO("Forcing quantized tail vocabulary for better performance.");
+ return true;
+ }
+ } else if (!explicit_hg_value.value()) { // explicit NO
+ if (can_enable_hgq) {
+ LOG_WARN("Consider removing NPUW_HOST_GATHER:NO from the config for better performance.");
+ } else {
+ LOG_WARN("Consider enabling NPUW_HOST_GATHER:YES for better performance.");
+ }
+ } else { // explicit YES
+ if (can_enable_hgq) {
+ LOG_WARN("Consider removing NPUW_HOST_GATHER:YES from the config for better performance.");
+ }
+ } // explicit_hg_value
+ LOG_INFO("DONE.");
+ return false;
+}
+
void ov::npuw::CompiledModel::CompiledModelDesc::serialize(std::ostream& stream,
const ov::npuw::s11n::WeightsContext& ctx) const {
using namespace ov::npuw::s11n;
@@ -1716,7 +1783,6 @@ void ov::npuw::CompiledModel::implement_properties() {
BIND(npuw::partitioning::spatial_nway, NPUW_SPATIAL_NWAY),
BIND(npuw::partitioning::spatial_dyn, NPUW_SPATIAL_DYN),
BIND(npuw::partitioning::host_gather, NPUW_HOST_GATHER),
- BIND(npuw::partitioning::gather_quant, NPUW_HOST_GATHER_QUANT),
BIND(npuw::partitioning::funcall_for_all, NPUW_FUNCALL_FOR_ALL),
BIND(npuw::partitioning::f16_interconnect, NPUW_F16IC),
BIND(npuw::partitioning::dcoff_type, NPUW_DCOFF_TYPE),
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 dffd58ccd14..09f9722dfaf 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp
@@ -100,6 +100,8 @@ private:
void log_device_dist() const;
void implement_properties();
+ bool should_use_quantized_host_gather(const std::shared_ptr<ov::Model>& model, const ov::AnyMap& properties) const;
+
// For full deserialization flow with weights
void reconstruct_closure();
// For weightless serialization flow
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 37ac28f92e6..4c682b77d29 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.cpp
@@ -294,12 +294,14 @@ public:
Partitioner(const std::shared_ptr<ov::Model>& _model,
ov::npuw::Ensemble& _ens,
ov::npuw::Partitioning& _P,
- ::intel_npu::Config& _cfg)
+ ::intel_npu::Config& _cfg,
+ const ov::npuw::PartitioningContext& _ctx)
: model(_model),
ens(_ens),
P(_P),
func_pipeline_type(FunctionPipelineType::FOLD),
- cfg(_cfg) {}
+ cfg(_cfg),
+ part_ctx(_ctx) {}
////////////////////////////////////////////////////////
// Partitioning execution pipeline
@@ -337,6 +339,7 @@ public:
private:
FunctionPipelineType func_pipeline_type;
::intel_npu::Config& cfg;
+ ov::npuw::PartitioningContext part_ctx;
std::size_t m_f16ic_counter = 0u;
@@ -1432,7 +1435,7 @@ void Partitioner::saveRepeatedConstants(const std::string& func_name) {
}
void Partitioner::saveTailDictConstants(const std::string& func_name) {
- if (!cfg.get<::intel_npu::NPUW_HOST_GATHER_QUANT>()) {
+ if (!part_ctx.use_host_gather_quant) {
// No need to preserve as constants
return;
}
@@ -1456,8 +1459,8 @@ void Partitioner::saveTailDictConstants(const std::string& func_name) {
std::vector<CPtr> to_keep;
ov::pass::GraphRewrite rewr;
- rewr.add_matcher<ov::npuw::patterns::opt::PreserveConstDictMatMulCWu>(std::ref(to_keep));
- rewr.add_matcher<ov::npuw::patterns::opt::PreserveConstDictMatMulCWf8>(std::ref(to_keep));
+ rewr.add_matcher<ov::npuw::patterns::opt::PreserveConstDictMatMulAsymm>(std::ref(to_keep));
+ rewr.add_matcher<ov::npuw::patterns::opt::PreserveConstDictMatMulSymm>(std::ref(to_keep));
rewr.run_on_model(model_group.front());
for (auto&& const_to_keep : to_keep) {
@@ -1911,14 +1914,9 @@ void Partitioner::optimize(const std::string& func_name) {
ctx.is_spatial = f._spatial.has_value();
ctx.pmm_dims = cfg.get<::intel_npu::NPUW_PMM>();
- if (cfg.get<::intel_npu::NPUW_HOST_GATHER_QUANT>() && cfg.get<::intel_npu::NPUW_HOST_GATHER>()) {
- NPUW_ASSERT(false && "Conflicting configuration: NPUW_HOST_GATHER and NPUW_HOST_GATHER_QUANT should not be "
- "enabled together!");
- }
-
// Run Head/Tail passes
ov::pass::GraphRewrite rewr;
- if (!cfg.get<::intel_npu::NPUW_HOST_GATHER_QUANT>()) {
+ if (cfg.get<::intel_npu::NPUW_HOST_GATHER>() && !part_ctx.use_host_gather_quant) {
rewr.add_matcher<ov::npuw::patterns::opt::DQUnpackDictGatheru>(std::ref(ctx));
rewr.add_matcher<ov::npuw::patterns::opt::DQUnpackDictGatherGQi>(std::ref(ctx));
rewr.add_matcher<ov::npuw::patterns::opt::DQUnpackDictMatMulCWu>(std::ref(ctx));
@@ -1934,30 +1932,15 @@ void Partitioner::optimize(const std::string& func_name) {
rewr.run_on_model(f._model);
// Quantized Gather + Unpack on host in the runtime
- if (cfg.get<::intel_npu::NPUW_HOST_GATHER_QUANT>()) {
- // FIXME: since we are running it after lifted Gather,
- // we need to first try to match Asymm or Symm patterns.
- // Otherwise smaller HostGatherQuant might be matched first and break
- // the quantization logic.
- {
- ov::pass::GraphRewrite rewr2;
- rewr2.add_matcher<ov::npuw::patterns::opt::HostGatherQuantAsymm>(std::ref(ctx));
- rewr2.run_on_model(f._model);
- }
- {
- ov::pass::GraphRewrite rewr2;
- rewr2.add_matcher<ov::npuw::patterns::opt::HostGatherQuantSymm>(std::ref(ctx));
- rewr2.run_on_model(f._model);
- }
- {
- ov::pass::GraphRewrite rewr2;
- rewr2.add_matcher<ov::npuw::patterns::opt::HostGatherQuant>(std::ref(ctx));
- rewr2.run_on_model(f._model);
- }
+ if (cfg.get<::intel_npu::NPUW_HOST_GATHER>() && part_ctx.use_host_gather_quant) {
+ ov::pass::GraphRewrite rewr2;
+ rewr2.add_matcher<ov::npuw::patterns::opt::HostGatherQuantAsymm<>>(std::ref(ctx));
+ rewr2.add_matcher<ov::npuw::patterns::opt::HostGatherQuantSymm<>>(std::ref(ctx));
+ rewr2.run_on_model(f._model);
}
// Move Gather to host, if required
- if (cfg.get<::intel_npu::NPUW_HOST_GATHER>()) {
+ if (cfg.get<::intel_npu::NPUW_HOST_GATHER>() && !part_ctx.use_host_gather_quant) {
ov::pass::GraphRewrite rewr2;
rewr2.add_matcher<ov::npuw::patterns::opt::HostGather>(std::ref(ctx));
rewr2.add_matcher<ov::npuw::patterns::opt::HostGatherDQ>(std::ref(ctx));
@@ -2407,7 +2390,9 @@ void Partitioner::finalizeLinks() {
} // namespace
-ov::npuw::Partitioning ov::npuw::getPartitioning(const std::shared_ptr<ov::Model>& model, ::intel_npu::Config& cfg) {
+ov::npuw::Partitioning ov::npuw::getPartitioning(const std::shared_ptr<ov::Model>& model,
+ ::intel_npu::Config& cfg,
+ const ov::npuw::PartitioningContext& ctx) {
LOG_INFO("Building partitioning for model " << model->get_friendly_name() << "...");
LOG_BLOCK();
@@ -2468,7 +2453,7 @@ ov::npuw::Partitioning ov::npuw::getPartitioning(const std::shared_ptr<ov::Model
Partitioning P;
P.total_gflops = ens.gflops;
- Partitioner p(model, ens, P, cfg);
+ Partitioner p(model, ens, P, cfg, ctx);
p.identifySubgraphs();
if (!ens.repeated.empty()) {
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 d1ded00c0be..ea091080c1d 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.hpp
@@ -147,7 +147,13 @@ struct Partitioning {
float total_gflops = 0.f;
};
-Partitioning getPartitioning(const std::shared_ptr<ov::Model>& model, ::intel_npu::Config& config);
+struct PartitioningContext {
+ bool use_host_gather_quant = false;
+};
+
+Partitioning getPartitioning(const std::shared_ptr<ov::Model>& model,
+ ::intel_npu::Config& config,
+ const PartitioningContext& ctx = {});
} // namespace npuw
} // namespace ov
diff --git a/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/opt.cpp b/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/opt.cpp
index 82c15c6802f..361b8d42382 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/opt.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/opt.cpp
@@ -182,6 +182,10 @@ Context::PPtr Context::host_gather_unpack_quant(const Context::PPtr& ids,
return new_param;
}
+bool Context::found_host_gather_quant() const {
+ return params_to_quant_gather_unpack.has_value();
+}
+
namespace opp = ov::pass::pattern;
namespace uat = ov::npuw::util::at;
@@ -1355,13 +1359,14 @@ DQUnpackDictGatherGQi::DQUnpackDictGatherGQi(Context::Ref ctx) {
// compile-time converts asymmetric MM to fp16, do the same thing here
// Overall it's a combination of DQUnpackDictGather and HostGather
// but we do unpack and gather in the runtime.
-HostGatherQuantAsymm::HostGatherQuantAsymm(Context::Ref ctx) {
+template <typename WType>
+HostGatherQuantAsymm<WType>::HostGatherQuantAsymm(Context::Ref ctx, bool verify_only) {
auto pids = opp::wrap_type<ov::op::v0::Parameter>();
auto cvtids = opp::optional<ov::op::v0::Convert>({pids->output(0)});
- auto qweight = opp::wrap_type<ov::op::v0::Parameter>();
- auto qzerop = opp::wrap_type<ov::op::v0::Parameter>();
- auto qcoeff = opp::wrap_type<ov::op::v0::Parameter>();
+ auto qweight = opp::wrap_type<WType>();
+ auto qzerop = opp::wrap_type<WType>();
+ auto qcoeff = opp::wrap_type<WType>();
auto qgthrw = opp::wrap_type<ov::op::v8::Gather>({qweight, cvtids, opp::any_input()});
auto qgthrz = opp::wrap_type<ov::op::v8::Gather>({qzerop, cvtids, opp::any_input()});
auto qgthrs = opp::wrap_type<ov::op::v8::Gather>({qcoeff, cvtids, opp::any_input()});
@@ -1395,6 +1400,11 @@ HostGatherQuantAsymm::HostGatherQuantAsymm(Context::Ref ctx) {
if (out_len >= 2048 && (matched_out_mul.get_target_inputs().size() > 1 ||
ov::is_type<ov::op::v0::Convert>(sole_reader(matched_out_mul)))) {
+ if (verify_only) {
+ // No transformations needed, just set dummy QuantizedGather
+ ctx.get().params_to_quant_gather_unpack = Context::QuantizedGather{};
+ return false; // root hasn't changed
+ }
auto matched_node_qweight = node_to_output.at(qweight).get_node_shared_ptr();
auto matched_node_qzerop = node_to_output.at(qzerop).get_node_shared_ptr();
auto matched_node_qcoeff = node_to_output.at(qcoeff).get_node_shared_ptr();
@@ -1424,16 +1434,20 @@ HostGatherQuantAsymm::HostGatherQuantAsymm(Context::Ref ctx) {
register_matcher(std::make_shared<opp::Matcher>(qcvtm, "HostGatherQuantAsymm"), std::move(callback));
}
+template class HostGatherQuantAsymm<ov::op::v0::Parameter>;
+template class HostGatherQuantAsymm<ov::op::v0::Constant>;
+
// This is a follow-up to DQLiftGatherSymGQ step, which happens if the respective
// block (mainly, a head) was turned a function (e.g. with FUNCALL_FOR_ALL)
// Overall it's a combination of DQUnpackDictGatherGQi and HostGatherDQ
// but we do unpack and gather in the runtime.
-HostGatherQuantSymm::HostGatherQuantSymm(Context::Ref ctx) {
+template <typename WType>
+HostGatherQuantSymm<WType>::HostGatherQuantSymm(Context::Ref ctx, bool verify_only) {
auto pids = opp::wrap_type<ov::op::v0::Parameter>();
auto cvtids = opp::optional<ov::op::v0::Convert>({pids->output(0)});
- auto qweight = opp::wrap_type<ov::op::v0::Parameter>();
- auto qcoeff = opp::wrap_type<ov::op::v0::Parameter>();
+ auto qweight = opp::wrap_type<WType>();
+ auto qcoeff = opp::wrap_type<WType>();
auto qgthrw = opp::wrap_type<ov::op::v8::Gather>({qweight, cvtids, opp::any_input()});
auto qgthrs = opp::wrap_type<ov::op::v8::Gather>({qcoeff, cvtids, opp::any_input()});
@@ -1473,6 +1487,11 @@ HostGatherQuantSymm::HostGatherQuantSymm(Context::Ref ctx) {
qweight_type == ov::element::f8e8m0) &&
(matched_out_reshape.get_target_inputs().size() > 1 ||
ov::is_type<ov::op::v0::Convert>(sole_reader(matched_out_reshape)))) {
+ if (verify_only) {
+ // No transformations needed, just set dummy QuantizedGather
+ ctx.get().params_to_quant_gather_unpack = Context::QuantizedGather{};
+ return false; // root hasn't changed
+ }
auto matched_node_qweight = node_to_output.at(qweight).get_node_shared_ptr();
auto matched_node_qcoeff = node_to_output.at(qcoeff).get_node_shared_ptr();
auto matched_node_ids = node_to_output.at(pids).get_node_shared_ptr();
@@ -1499,59 +1518,8 @@ HostGatherQuantSymm::HostGatherQuantSymm(Context::Ref ctx) {
register_matcher(std::make_shared<opp::Matcher>(qcvtm, "HostGatherQuantSymm"), std::move(callback));
}
-// Overall it's a combination of DQUnpackDictGather and HostGather
-// but we do unpack and gather in the runtime.
-HostGatherQuant::HostGatherQuant(Context::Ref ctx) {
- auto pids = opp::wrap_type<ov::op::v0::Parameter>();
- auto cvtids = opp::optional<ov::op::v0::Convert>({pids->output(0)});
-
- auto qweight = opp::wrap_type<ov::op::v0::Parameter>();
- auto qgthrw = opp::wrap_type<ov::op::v8::Gather>({qweight, cvtids, opp::any_input()});
-
- auto callback = [=](ov::pass::pattern::Matcher& m) {
- auto& node_to_output = m.get_pattern_value_map();
- auto out_shape = node_to_output.at(qgthrw).get_shape();
- auto& matched_out_qweight = node_to_output.at(qweight);
- auto qweight_type = matched_out_qweight.get_element_type();
-
- const auto& matched_out_gather = node_to_output.at(qgthrw);
-
- auto sole_reader = [](ov::Output<ov::Node> out) {
- const auto readers = out.get_target_inputs();
- NPUW_ASSERT(readers.size() >= 1);
- return readers.begin()->get_node();
- };
-
- if (out_shape.back() >= 2048 && (matched_out_gather.get_target_inputs().size() > 1 ||
- ov::is_type<ov::op::v0::Convert>(sole_reader(matched_out_gather)))) {
- auto matched_node_qweight = node_to_output.at(qweight).get_node_shared_ptr();
- auto matched_node_ids = node_to_output.at(pids).get_node_shared_ptr();
- const auto& matched_out_gthr = node_to_output.at(qgthrw);
- auto matched_qweight = std::static_pointer_cast<ov::op::v0::Parameter>(matched_node_qweight);
- auto matched_ids = std::static_pointer_cast<ov::op::v0::Parameter>(matched_node_ids);
-
- if (qweight_type == ov::element::f32) {
- ctx.get().to_f16(matched_qweight);
- }
-
- auto new_param =
- ctx.get().host_gather_unpack_quant(matched_ids, matched_qweight, nullptr, nullptr, ov::element::f16);
- std::shared_ptr<ov::Node> new_cvt;
- if (qweight_type == ov::element::f16) {
- new_cvt = new_param;
- } else {
- new_cvt = std::make_shared<ov::op::v0::Convert>(new_param, ov::element::f32);
- }
- NPUW_ASSERT(new_cvt);
- for (auto&& r : matched_out_gthr.get_target_inputs()) {
- r.replace_source_output(new_cvt);
- }
- return true; // root has changed
- }
- return false; // root hasn't changed
- };
- register_matcher(std::make_shared<opp::Matcher>(qgthrw, "HostGatherQuant"), std::move(callback));
-}
+template class HostGatherQuantSymm<ov::op::v0::Parameter>;
+template class HostGatherQuantSymm<ov::op::v0::Constant>;
// Identify the case* where the FP16/32 vocab tensor is gathered with
// input_ids and the embedding size is high. In this case, substitute
@@ -1949,7 +1917,7 @@ CompressDictMatMulf32::CompressDictMatMulf32(Context::Ref ctx) {
// Const(S) ---------------------> Multiply -> to(f32) -> MatMul -> Result
// ???(Act) -------------------------------------------->
-PreserveConstDictMatMulCWu::PreserveConstDictMatMulCWu(PreserveConstDictMatMulCWu::Results to_keep) {
+PreserveConstDictMatMulAsymm::PreserveConstDictMatMulAsymm(PreserveConstDictMatMulAsymm::Results to_keep) {
auto qweight = opp::wrap_type<ov::op::v0::Constant>();
auto qcoeff = opp::wrap_type<ov::op::v0::Constant>();
auto qzerop = opp::wrap_type<ov::op::v0::Constant>();
@@ -1968,10 +1936,7 @@ PreserveConstDictMatMulCWu::PreserveConstDictMatMulCWu(PreserveConstDictMatMulCW
auto matched_node_qweight = node_to_output.at(qweight).get_node_shared_ptr();
auto matched_node_qzerop = node_to_output.at(qzerop).get_node_shared_ptr();
- auto matched_node_cvtz = node_to_output.at(qcvtz).get_node_shared_ptr();
- auto matched_node_cvtw = node_to_output.at(qcvtw).get_node_shared_ptr();
auto matched_node_qcoeff = node_to_output.at(qcoeff).get_node_shared_ptr();
- auto matched_node_qmuls = node_to_output.at(qmuls).get_node_shared_ptr();
auto matched_node_matmul = node_to_output.at(qmm).get_node_shared_ptr();
auto matched_qweight = std::static_pointer_cast<ov::op::v0::Constant>(matched_node_qweight);
@@ -1986,19 +1951,18 @@ PreserveConstDictMatMulCWu::PreserveConstDictMatMulCWu(PreserveConstDictMatMulCW
to_keep.get().push_back(matched_qweight);
to_keep.get().push_back(matched_qzerop);
to_keep.get().push_back(matched_qcoeff);
-
return false; // root hasn't changed
}
return false; // root hasn't changed
};
- register_matcher(std::make_shared<opp::Matcher>(qres, "OptPreserveConstDictMatMulCWu"), std::move(callback));
+ register_matcher(std::make_shared<opp::Matcher>(qres, "OptPreserveConstDictMatMulAsymm"), std::move(callback));
}
// Const(W) ---> to(f16) --->
// Const(S) ----------------> Multiply -> MatMul -> Result
// ???(Act) ---------------------------->
-PreserveConstDictMatMulCWf8::PreserveConstDictMatMulCWf8(PreserveConstDictMatMulCWf8::Results to_keep) {
+PreserveConstDictMatMulSymm::PreserveConstDictMatMulSymm(PreserveConstDictMatMulSymm::Results to_keep) {
auto qweight = opp::wrap_type<ov::op::v0::Constant>();
auto qcoeff = opp::wrap_type<ov::op::v0::Constant>();
auto qcvtw = opp::wrap_type<ov::op::v0::Convert>({qweight});
@@ -2012,10 +1976,8 @@ PreserveConstDictMatMulCWf8::PreserveConstDictMatMulCWf8(PreserveConstDictMatMul
auto& node_to_output = m.get_pattern_value_map();
auto matched_node_qweight = node_to_output.at(qweight).get_node_shared_ptr();
- auto matched_node_cvtw = node_to_output.at(qcvtw).get_node_shared_ptr();
auto matched_node_qcoeff = node_to_output.at(qcoeff).get_node_shared_ptr();
auto matched_node_matmul = node_to_output.at(qmm).get_node_shared_ptr();
- auto matched_node_qmuls = node_to_output.at(qmuls).get_node_shared_ptr();
auto matched_qweight = std::static_pointer_cast<ov::op::v0::Constant>(matched_node_qweight);
auto matched_qcoeff = std::static_pointer_cast<ov::op::v0::Constant>(matched_node_qcoeff);
@@ -2029,12 +1991,11 @@ PreserveConstDictMatMulCWf8::PreserveConstDictMatMulCWf8(PreserveConstDictMatMul
qcoeff_shape[1] == 1 && !matched_matmul->get_transpose_a() && matched_matmul->get_transpose_b()) {
to_keep.get().push_back(matched_qweight);
to_keep.get().push_back(matched_qcoeff);
-
return false; // root hasn't changed
}
return false; // root hasn't changed
};
- register_matcher(std::make_shared<opp::Matcher>(qres, "OptPreserveConstDictMatMulCWf8"), std::move(callback));
+ register_matcher(std::make_shared<opp::Matcher>(qres, "OptPreserveConstDictMatMulSymm"), std::move(callback));
}
SliceLastMatmul::SliceLastMatmul() {
diff --git a/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/opt.hpp b/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/opt.hpp
index 6f77cf0e433..f25f03c4b0b 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/opt.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/opt.hpp
@@ -73,6 +73,7 @@ struct Context {
PPtr pids;
};
std::optional<QuantizedGather> params_to_quant_gather_unpack;
+ bool found_host_gather_quant() const;
PPtr host_gather_unpack_quant(const PPtr& ids, const PPtr& w, const PPtr& z, const PPtr& s, ov::element::Type type);
using Ref = std::reference_wrapper<Context>;
@@ -162,22 +163,18 @@ public:
DQUnpackDictGatherGQi(Context::Ref ctx);
};
+template <typename WType = ov::op::v0::Parameter>
class HostGatherQuantAsymm : public ov::pass::MatcherPass {
public:
OPENVINO_MATCHER_PASS_RTTI("npuw::patterns::opt::HostGatherQuantAsymm");
- HostGatherQuantAsymm(Context::Ref ctx);
+ HostGatherQuantAsymm(Context::Ref ctx, bool verify_only = false);
};
+template <typename WType = ov::op::v0::Parameter>
class HostGatherQuantSymm : public ov::pass::MatcherPass {
public:
OPENVINO_MATCHER_PASS_RTTI("npuw::patterns::opt::HostGatherQuantSymm");
- HostGatherQuantSymm(Context::Ref ctx);
-};
-
-class HostGatherQuant : public ov::pass::MatcherPass {
-public:
- OPENVINO_MATCHER_PASS_RTTI("npuw::patterns::opt::HostGatherQuant");
- HostGatherQuant(Context::Ref ctx);
+ HostGatherQuantSymm(Context::Ref ctx, bool verify_only = false);
};
class HostGather : public ov::pass::MatcherPass {
@@ -225,24 +222,24 @@ public:
};
// Tail vocab transformations
-class PreserveConstDictMatMulCWu : public ov::pass::MatcherPass {
+class PreserveConstDictMatMulAsymm : public ov::pass::MatcherPass {
public:
- OPENVINO_MATCHER_PASS_RTTI("npuw::patterns::opt::PreserveConstDictMatMulCWu");
+ OPENVINO_MATCHER_PASS_RTTI("npuw::patterns::opt::PreserveConstDictMatMulAsymm");
using CPtr = std::shared_ptr<ov::op::v0::Constant>;
using Results = std::reference_wrapper<std::vector<CPtr>>;
- PreserveConstDictMatMulCWu(Results to_keep);
+ PreserveConstDictMatMulAsymm(Results to_keep);
};
-class PreserveConstDictMatMulCWf8 : public ov::pass::MatcherPass {
+class PreserveConstDictMatMulSymm : public ov::pass::MatcherPass {
public:
- OPENVINO_MATCHER_PASS_RTTI("npuw::patterns::opt::PreserveConstDictMatMulCWf8");
+ OPENVINO_MATCHER_PASS_RTTI("npuw::patterns::opt::PreserveConstDictMatMulSymm");
using CPtr = std::shared_ptr<ov::op::v0::Constant>;
using Results = std::reference_wrapper<std::vector<CPtr>>;
- PreserveConstDictMatMulCWf8(Results to_keep);
+ PreserveConstDictMatMulSymm(Results to_keep);
};
// Slice last Matmul