Skip to content

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

NPUW: Merge parallel MMs, if possible + Update GQ transformation (#26580)

Dmitry Matveev3cefe7007879
diff --git a/src/plugins/intel_npu/src/al/include/intel_npu/al/config/npuw.hpp b/src/plugins/intel_npu/src/al/include/intel_npu/al/config/npuw.hpp
index c5e91369ce8..ca80ebfd074 100644
--- a/src/plugins/intel_npu/src/al/include/intel_npu/al/config/npuw.hpp
+++ b/src/plugins/intel_npu/src/al/include/intel_npu/al/config/npuw.hpp
@@ -42,6 +42,7 @@ DEFINE_OPT(NPUW_PLAN, std::string, "", npuw::partitioning::plan, CompileTime);
DEFINE_OPT(NPUW_FOLD, bool, false, npuw::partitioning::fold, CompileTime);
DEFINE_OPT(NPUW_CWAI, bool, false, npuw::partitioning::cwai, CompileTime);
DEFINE_OPT(NPUW_DQ, bool, false, npuw::partitioning::dyn_quant, CompileTime);
+DEFINE_OPT(NPUW_PMM, std::string, "2", npuw::partitioning::par_matmul_merge_dims, CompileTime);
DEFINE_OPT(NPUW_DCOFF_TYPE, std::string, "", npuw::partitioning::dcoff_type, CompileTime);
DEFINE_OPT(NPUW_DCOFF_SCALE, bool, false, npuw::partitioning::dcoff_with_scale, CompileTime);
DEFINE_OPT(NPUW_FUNCALL_FOR_ALL, bool, false, npuw::partitioning::funcall_for_all, CompileTime);
diff --git a/src/plugins/intel_npu/src/al/include/npuw_private_properties.hpp b/src/plugins/intel_npu/src/al/include/npuw_private_properties.hpp
index b0cf6f4a608..27e88efb1c5 100644
--- a/src/plugins/intel_npu/src/al/include/npuw_private_properties.hpp
+++ b/src/plugins/intel_npu/src/al/include/npuw_private_properties.hpp
@@ -167,6 +167,16 @@ static constexpr ov::Property<bool> cwai{"NPUW_CWAI"};
*/
static constexpr ov::Property<bool> dyn_quant{"NPUW_DQ"};
+/**
+ * @brief
+ * Type: string.
+ * Identify and merge parallel MatMuls over dimension(s) specified.
+ * When set to YES, applies transformation for all dimensions.
+ * Works with FOLD enabled only.
+ * Default value: 2.
+ */
+static constexpr ov::Property<std::string> par_matmul_merge_dims{"NPUW_PMM"};
+
/**
* @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 406fb9e6288..32c21ec43c6 100644
--- a/src/plugins/intel_npu/src/al/src/config/npuw.cpp
+++ b/src/plugins/intel_npu/src/al/src/config/npuw.cpp
@@ -27,6 +27,7 @@ void intel_npu::registerNPUWOptions(OptionsDesc& desc) {
desc.add<NPUW_FOLD>();
desc.add<NPUW_CWAI>();
desc.add<NPUW_DQ>();
+ desc.add<NPUW_PMM>();
desc.add<NPUW_DCOFF_TYPE>();
desc.add<NPUW_DCOFF_SCALE>();
desc.add<NPUW_FUNCALL_FOR_ALL>();
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 bfb1b474ee0..eea817b5261 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp
@@ -798,6 +798,7 @@ void ov::npuw::CompiledModel::implement_properties() {
BIND(npuw::partitioning::fold, NPUW_FOLD),
BIND(npuw::partitioning::cwai, NPUW_CWAI),
BIND(npuw::partitioning::dyn_quant, NPUW_DQ),
+ BIND(npuw::partitioning::par_matmul_merge_dims, NPUW_PMM),
BIND(npuw::partitioning::funcall_for_all, NPUW_FUNCALL_FOR_ALL),
BIND(npuw::partitioning::dcoff_type, NPUW_DCOFF_TYPE),
BIND(npuw::partitioning::dcoff_with_scale, NPUW_DCOFF_SCALE),
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 fed5126f62a..130da23b3c3 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.cpp
@@ -1562,6 +1562,60 @@ void Partitioner::matchRepeatedSubgraphs(const std::string& func_name) {
}
void Partitioner::optimize(const std::string& func_name) {
+ ov::npuw::Function& f = P.functions.at(func_name);
+ auto& func_group = all_functions.at(func_name);
+
+ // Regardless of DQ setting, run this first
+ {
+ ov::npuw::patterns::opt::Context ctx;
+ ctx.pmm_dims = cfg.get<::intel_npu::NPUW_PMM>();
+ mergeParallelMatMuls(f._model, ctx);
+
+ // Concatenate closures for "concatenated" parameters
+ ov::ParameterVector new_params;
+ std::vector<ov::npuw::patterns::opt::Context::PPtr> to_remove;
+ std::set<std::size_t> to_remove_idx;
+ for (auto&& p : ctx.params_to_concat) {
+ new_params.push_back(p.first);
+ const auto& params_to_concat = p.second.first;
+ const auto axis = p.second.second;
+
+ std::vector<std::size_t> to_concat_idx;
+ for (auto&& p_to_concat : params_to_concat) {
+ auto p_to_concat_idx = f._model->get_parameter_index(p_to_concat);
+ to_remove.push_back(p_to_concat);
+ to_concat_idx.push_back(p_to_concat_idx - f._param_offset);
+ to_remove_idx.insert(p_to_concat_idx);
+ }
+ ov::parallel_for(func_group.refs.size(), [&](std::size_t f_idx) {
+ auto& funcall = func_group.refs[f_idx].get();
+ std::vector<ov::Tensor> to_concat;
+ for (auto&& cidx : to_concat_idx) {
+ to_concat.push_back(funcall._closure[cidx]);
+ }
+ funcall._closure.push_back(ov::npuw::util::concat(to_concat, axis));
+ });
+ }
+ f._model->add_parameters(new_params);
+
+ // Remove parameters and closures that were concatenated
+ std::vector<ov::Tensor> new_closure;
+ for (auto&& fref : func_group.refs) {
+ auto& funcall = fref.get();
+ std::vector<ov::Tensor> new_closure;
+ for (std::size_t cidx = 0; cidx < funcall._closure.size(); cidx++) {
+ if (to_remove_idx.count(f._param_offset + cidx) == 0) {
+ new_closure.push_back(funcall._closure[cidx]);
+ }
+ }
+ funcall._closure = std::move(new_closure);
+ }
+ for (auto&& now_remove : to_remove) {
+ f._model->remove_parameter(now_remove);
+ }
+ f._model->validate_nodes_and_infer_types();
+ }
+
if (!cfg.get<::intel_npu::NPUW_DQ>()) {
LOG_VERB("No optimizations will be done to " << func_name << " in model " << model->get_friendly_name()
<< "...");
@@ -1571,8 +1625,6 @@ void Partitioner::optimize(const std::string& func_name) {
LOG_VERB("Optimize function " << func_name << " in model " << model->get_friendly_name() << "...");
LOG_BLOCK();
- ov::npuw::Function& f = P.functions.at(func_name);
-
ov::npuw::patterns::opt::Context ctx;
ov::pass::GraphRewrite rewr;
rewr.add_matcher<ov::npuw::patterns::opt::DQMatMulCWi>();
@@ -1582,7 +1634,6 @@ void Partitioner::optimize(const std::string& func_name) {
ov::pass::Validate().run_on_model(f._model);
// Permute tensors where required
- auto& func_group = all_functions.at(func_name);
for (auto&& p : ctx.closures_to_permute) {
auto param_idx = f._model->get_parameter_index(p.first);
auto closure_idx = param_idx - f._param_offset;
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 d2204982cd5..7d44ae04835 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
@@ -5,6 +5,7 @@
#include "opt.hpp"
#include "../../logging.hpp"
+#include "../../util.hpp"
#include "openvino/op/add.hpp"
#include "openvino/op/concat.hpp"
#include "openvino/op/convert.hpp"
@@ -13,19 +14,16 @@
#include "openvino/op/multiply.hpp"
#include "openvino/op/reduce_sum.hpp"
#include "openvino/op/reshape.hpp"
+#include "openvino/op/slice.hpp"
#include "openvino/op/split.hpp"
#include "openvino/op/squeeze.hpp"
#include "openvino/op/unsqueeze.hpp"
#include "openvino/op/util/op_types.hpp"
#include "openvino/pass/pattern/op/label.hpp" // any_input
+#include "openvino/pass/pattern/op/optional.hpp"
#include "openvino/pass/pattern/op/wrap_type.hpp"
#include "openvino/util/common_util.hpp"
-// FIXME: It is probably much better to have these transformations done
-// just once per function BODY
-// On LLMs, it may reduce the # of such transformations by a factor of
-// 20..30x.
-
namespace ov {
namespace npuw {
namespace patterns {
@@ -39,6 +37,35 @@ void Context::to_f16(PPtr orig_param) {
closures_to_f16.insert(orig_param);
}
+void Context::register_parallel_matmul(O multiply, std::size_t axis, DQParMM&& mm) {
+ par_dq_mms[std::make_pair(multiply, axis)].push_back(std::move(mm));
+}
+
+Context::PPtr Context::concat(ov::ParameterVector&& v, std::size_t dim) {
+ // Sanity check dimensions - all dims other tham dim must match
+ std::size_t sum = 0u;
+ const auto& first = v.front();
+ const auto first_shape = first->get_shape();
+ for (auto&& p : v) {
+ const auto this_shape = p->get_shape();
+ NPUW_ASSERT(first_shape.size() == this_shape.size());
+ for (std::size_t d = 0; d < first_shape.size(); d++) {
+ if (d != dim) {
+ NPUW_ASSERT(first_shape[d] == this_shape[d]);
+ } else {
+ sum += this_shape[d];
+ }
+ }
+ NPUW_ASSERT(first->get_element_type() == p->get_element_type());
+ }
+ auto out_shape = first_shape;
+ out_shape[dim] = sum;
+
+ auto new_param = std::make_shared<ov::op::v0::Parameter>(first->get_element_type(), out_shape);
+ params_to_concat[new_param] = {std::move(v), dim};
+ return new_param;
+}
+
namespace opp = ov::pass::pattern;
// FROM:
@@ -245,15 +272,17 @@ DQMatMulGQi::DQMatMulGQi(Context::Ref ctx) {
// [1, 1,128] x
// TO: [1,512,128]T =
// [16,1,128] [1, 1,512] [16,1,512]
-// ???(Act) -> Reshape > Split(/16) ->[to(f16) -> ]}
-// Param(W*) -----------> Split(/16) ->[to(f16) -> MatMul v ]} Concat
-// Param(S) -----------> Split(/16) ->[---------> Multiply ]} v
-// Reshape(1,16,1,512)
-// ReduceSum(1)
-// Reshape( 1,1,512)
-// to(f32)
+// ???(Act) -> Reshape > Split(/16) ->[to(f16) -> ]}
+// Param(W*) -----------> Split(/16) ->[to(f16) -> MatMul >]} Concat
+// v
+// Param(S*) ---------------------------------------------> Multiply
+// Reshape(1,16,1,512)
+// ReduceSum(1)
+// Reshape( 1,1,512)
+// to(f32)
// WHERE:
// W* : [16,512,128]
+// S* : [16, 1,512]
DQMatMulGQ2i::DQMatMulGQ2i(Context::Ref ctx) {
auto qweight = opp::wrap_type<ov::op::v0::Parameter>();
@@ -295,6 +324,13 @@ DQMatMulGQ2i::DQMatMulGQ2i(Context::Ref ctx) {
matched_qweight->set_partial_shape(tw_shape);
matched_qweight->validate_and_infer_types();
+ // Also transpose S, but in a different way (see diagram above)
+ ctx.get().permute(matched_qcoeff, {1, 2, 0});
+
+ ov::Shape ts_shape = {qcoeff_shape[1], qcoeff_shape[2], qcoeff_shape[0]};
+ matched_qcoeff->set_partial_shape(ts_shape);
+ matched_qcoeff->validate_and_infer_types();
+
// Reshape the Act to group format
const auto NSPLIT = qweight_shape[1];
std::vector<std::size_t> rshp_act_v = {NSPLIT, 1, act_shape[2] / NSPLIT};
@@ -306,9 +342,6 @@ DQMatMulGQ2i::DQMatMulGQ2i(Context::Ref ctx) {
auto split_a = std::make_shared<ov::op::v1::Split>(rshp_act, split_axis, NSPLIT);
auto split_w = std::make_shared<ov::op::v1::Split>(matched_qweight, split_axis, NSPLIT);
- auto split_axis_s = std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{}, 1);
- auto split_s = std::make_shared<ov::op::v1::Split>(matched_qcoeff, split_axis_s, NSPLIT);
-
std::vector<std::size_t> rshp_scale_v = {1, 1, qcoeff_shape[0]};
auto rshp_scale_c = std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{3}, rshp_scale_v);
@@ -318,19 +351,17 @@ DQMatMulGQ2i::DQMatMulGQ2i(Context::Ref ctx) {
auto a_f16 = std::make_shared<ov::op::v0::Convert>(split_a->output(i), ov::element::f16);
auto w_f16 = std::make_shared<ov::op::v0::Convert>(split_w->output(i), ov::element::f16);
auto m_f16 = std::make_shared<ov::op::v0::MatMul>(a_f16, w_f16, false, true);
-
- auto r_f16 = std::make_shared<ov::op::v1::Reshape>(split_s->output(i), rshp_scale_c, false);
- auto s_f16 = std::make_shared<ov::op::v1::Multiply>(m_f16, r_f16);
- to_concat.push_back(s_f16);
+ to_concat.push_back(m_f16);
}
// Now concat and scale the result
auto concat = std::make_shared<ov::op::v0::Concat>(to_concat, 0);
+ auto scaled = std::make_shared<ov::op::v1::Multiply>(concat, matched_qcoeff);
// Now reshape to a better shape, ReduceSum, and reshape to the right size again
std::vector<std::size_t> rshp_ccat_v = {1, NSPLIT, 1, qweight_shape[0]};
auto rshp_ccat_c = std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{4}, rshp_ccat_v);
- auto rshp_ccat = std::make_shared<ov::op::v1::Reshape>(concat, rshp_ccat_c, false);
+ auto rshp_ccat = std::make_shared<ov::op::v1::Reshape>(scaled, rshp_ccat_c, false);
auto reduce_axis = std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{}, 1);
auto reduce = std::make_shared<ov::op::v1::ReduceSum>(rshp_ccat, reduce_axis, true);
@@ -352,6 +383,139 @@ DQMatMulGQ2i::DQMatMulGQ2i(Context::Ref ctx) {
register_matcher(std::make_shared<opp::Matcher>(qmm, "OptDQMatMulGQ2i"), std::move(callback));
}
+// Identifies this pattern
+//
+// Multiply -----------------------------------> MatMul
+// Param(W) -> to(f32) -> Multiply -> Reshape ->
+// Param(S) ------------>
+
+DQParMMGQ::DQParMMGQ(Context::Ref ctx) {
+ auto qweight = opp::wrap_type<ov::op::v0::Parameter>();
+ auto qcoeff = opp::wrap_type<ov::op::v0::Parameter>();
+ auto qcvtw = opp::wrap_type<ov::op::v0::Convert>({qweight});
+ auto qmuls = opp::wrap_type<ov::op::v1::Multiply>({qcvtw, qcoeff});
+ auto qreshp = opp::wrap_type<ov::op::v1::Reshape>({qmuls, opp::any_input()});
+ auto qmmi = opp::wrap_type<ov::op::v1::Multiply>({opp::any_input(), opp::any_input()});
+ auto qcvtr = opp::optional<ov::op::v0::Convert>({qreshp->output(0)});
+ auto qmm = opp::wrap_type<ov::op::v0::MatMul>({qmmi, qcvtr});
+
+ // Note: Use [=] to make sure the above objects stay alive in the callback
+ auto callback = [=](ov::pass::pattern::Matcher& m) {
+ auto& node_to_output = m.get_pattern_value_map();
+ auto w_param =
+ std::static_pointer_cast<ov::op::v0::Parameter>(node_to_output.at(qweight).get_node_shared_ptr());
+ auto s_param = std::static_pointer_cast<ov::op::v0::Parameter>(node_to_output.at(qcoeff).get_node_shared_ptr());
+ auto matmul = std::static_pointer_cast<ov::op::v0::MatMul>(node_to_output.at(qmm).get_node_shared_ptr());
+
+ auto qmmi_shape = node_to_output.at(qmm).get_shape();
+
+ if (qmmi_shape.size() != 3 || qmmi_shape[0] != 1 || qmmi_shape[1] != 1) {
+ // Limit token to 1-token shapes only (prefill requires its own tranformation)
+ return false;
+ }
+
+ if (!matmul->get_transpose_a() && !matmul->get_transpose_b()) {
+ ctx.get().register_parallel_matmul(node_to_output.at(qmmi), 2, Context::DQParMM{w_param, s_param, matmul});
+ } else if (!matmul->get_transpose_a() && matmul->get_transpose_b()) {
+ ctx.get().register_parallel_matmul(node_to_output.at(qmmi), 0, Context::DQParMM{w_param, s_param, matmul});
+ }
+ return false; // no change here
+ };
+ register_matcher(std::make_shared<opp::Matcher>(qmm, "OptDQParMMGQ"), std::move(callback));
+}
+
+void mergeParallelMatMuls(const std::shared_ptr<ov::Model>& m, Context& ctx) {
+ ov::pass::GraphRewrite rewr;
+ rewr.add_matcher<ov::npuw::patterns::opt::DQParMMGQ>(std::ref(ctx));
+ rewr.run_on_model(m);
+
+ for (auto&& mul_to_mms : ctx.par_dq_mms) {
+ auto& parallel_matmuls = mul_to_mms.second;
+ if (parallel_matmuls.size() < 2) {
+ continue;
+ }
+ ov::Output<ov::Node> orig_multiply;
+ std::size_t axis_to_concat = -1;
+ std::tie(orig_multiply, axis_to_concat) = mul_to_mms.first;
+
+ if (!util::is_set(axis_to_concat, ctx.pmm_dims)) {
+ LOG_VERB("Parallel MatMuls found, but fusion over dim " << axis_to_concat << " is not enabled");
+ continue;
+ }
+
+ const auto& first_w = parallel_matmuls[0].w;
+ const auto& first_s = parallel_matmuls[0].s;
+ ov::ParameterVector old_w, old_s;
+ bool all_ok = true;
+ for (auto&& dqmm : parallel_matmuls) {
+ if (first_w->get_shape().size() != dqmm.w->get_shape().size() ||
+ first_s->get_shape().size() != dqmm.s->get_shape().size() ||
+ dqmm.w->get_shape().size() != dqmm.s->get_shape().size()) {
+ all_ok = false;
+ break;
+ }
+ for (std::size_t d = 0u; d < first_w->get_shape().size(); d++) {
+ if (d != axis_to_concat && (first_w->get_shape()[d] != dqmm.w->get_shape()[d] ||
+ first_s->get_shape()[d] != dqmm.s->get_shape()[d])) {
+ all_ok = false;
+ break;
+ }
+ }
+ old_w.push_back(dqmm.w);
+ old_s.push_back(dqmm.s);
+ }
+ if (!all_ok) {
+ continue;
+ }
+ auto new_w = ctx.concat(std::move(old_w), axis_to_concat);
+ auto new_s = ctx.concat(std::move(old_s), axis_to_concat);
+ auto new_cvt = std::make_shared<ov::op::v0::Convert>(new_w, new_s->get_element_type());
+
+ std::shared_ptr<ov::Node> new_mul = std::make_shared<ov::op::v1::Multiply>(new_cvt, new_s);
+ if (new_s->get_element_type() == ov::element::f16) {
+ new_mul = std::make_shared<ov::op::v0::Convert>(new_mul, ov::element::f32);
+ }
+ auto new_w_shape = new_w->get_shape();
+
+ using S = std::vector<std::size_t>;
+ S new_rshp_v;
+ if (axis_to_concat == 2) {
+ new_rshp_v = S{new_w_shape[0] * new_w_shape[1], new_w_shape[2]};
+ } else if (axis_to_concat == 0) {
+ new_rshp_v = S{new_w_shape[0], new_w_shape[1] * new_w_shape[2]};
+ } else {
+ NPUW_ASSERT(false);
+ }
+ auto new_rshp_c = std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{2}, new_rshp_v);
+ auto new_rshp = std::make_shared<ov::op::v1::Reshape>(new_mul, new_rshp_c, false);
+
+ // Transpose input_b if concat was done by 0th axis (meaning the original MM's input_b were also transposed)
+ auto new_mm = std::make_shared<ov::op::v0::MatMul>(orig_multiply, new_rshp, false, (axis_to_concat == 0));
+
+ // Create new slices & reconnect matmuls
+ // FIXME: use zip
+ std::size_t offset = 0u;
+ for (std::size_t i = 0u; i < parallel_matmuls.size(); i++) {
+ auto this_orig_wshape = parallel_matmuls[i].w->get_shape();
+ auto this_slice_start =
+ std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{3}, S{0, 0, offset});
+ auto this_slice_end =
+ std::make_shared<ov::op::v0::Constant>(ov::element::i32,
+ ov::Shape{3},
+ S{1, 1, offset + this_orig_wshape[axis_to_concat]});
+ auto this_slice_step = std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{3}, S{1, 1, 1});
+ auto this_slice =
+ std::make_shared<ov::op::v8::Slice>(new_mm, this_slice_start, this_slice_end, this_slice_step);
+
+ // redirect the original matmul's readers to the slice
+ for (auto&& r : parallel_matmuls[i].mm->output(0).get_target_inputs()) {
+ r.replace_source_output(this_slice);
+ }
+ offset += this_orig_wshape[axis_to_concat];
+ }
+ }
+}
+
} // namespace opt
} // namespace patterns
} // namespace npuw
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 fee216b7066..fb867f8a344 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
@@ -24,7 +24,10 @@ public:
};
struct Context {
+ std::string pmm_dims;
+
using PPtr = std::shared_ptr<ov::op::v0::Parameter>;
+ using NPtr = std::shared_ptr<ov::Node>;
using Axes = std::vector<std::size_t>;
std::map<PPtr, Axes> closures_to_permute;
@@ -33,6 +36,18 @@ struct Context {
std::set<PPtr> closures_to_f16;
void to_f16(PPtr orig_param);
+ using O = ov::Output<ov::Node>;
+ struct DQParMM {
+ PPtr w, s;
+ NPtr mm;
+ };
+ using DQParMMs = std::vector<DQParMM>;
+ std::map<std::pair<O, std::size_t>, DQParMMs> par_dq_mms;
+ void register_parallel_matmul(O multiply, std::size_t axis, DQParMM&& mm);
+
+ std::map<PPtr, std::pair<ov::ParameterVector, std::size_t>> params_to_concat;
+ PPtr concat(ov::ParameterVector&& v, std::size_t dim);
+
using Ref = std::reference_wrapper<Context>;
};
@@ -46,6 +61,13 @@ public:
explicit DQMatMulGQ2i(Context::Ref ctx);
};
+class DQParMMGQ : public ov::pass::MatcherPass {
+public:
+ explicit DQParMMGQ(Context::Ref ctx);
+};
+
+void mergeParallelMatMuls(const std::shared_ptr<ov::Model>& m, Context& ctx);
+
} // namespace opt
} // namespace patterns
} // namespace npuw
diff --git a/src/plugins/intel_npu/src/plugin/npuw/util.cpp b/src/plugins/intel_npu/src/plugin/npuw/util.cpp
index 5a5f8f733d1..9f7a404e0d9 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/util.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/util.cpp
@@ -1470,14 +1470,36 @@ void ov::npuw::util::transpose(ov::Tensor& t) {
t = std::move(tnew);
}
+template <typename T>
+void permute120(const ov::Tensor& src, ov::Tensor& dst) {
+ const ov::Shape src_shape = src.get_shape();
+ const ov::Shape dst_shape = dst.get_shape();
+ NPUW_ASSERT(src_shape.size() == 3); // Yes, so far only transpose 3D tensors
+
+ const T* pSrc = static_cast<T*>(src.data());
+ T* pDst = static_cast<T*>(dst.data());
+
+ // DSTs [b,r,c] map to SRC's [r,c,b]
+
+ for (std::size_t b = 0; b < dst_shape[0]; b++) {
+ for (std::size_t r = 0; r < dst_shape[1]; r++) {
+ for (std::size_t c = 0; c < dst_shape[2]; c++) {
+ auto dst_idx = b * dst_shape[1] * dst_shape[2] + r * dst_shape[2] + c;
+ auto src_idx = r * src_shape[1] * src_shape[2] + c * src_shape[1] + b;
+ pDst[dst_idx] = pSrc[src_idx];
+ }
+ }
+ }
+}
+
void ov::npuw::util::permute(ov::Tensor& t, const std::vector<std::size_t>& axes) {
ov::Shape shape = t.get_shape();
- NPUW_ASSERT(shape.size() == 3); // Yes, so far only transpose 3D tensors
- NPUW_ASSERT(t.get_element_type() == ov::element::i4); // And, yes, 4bit only!
+ NPUW_ASSERT(shape.size() == 3); // Yes, so far only transpose 3D tensors
if (axes[0] == 2 && axes[1] == 0 && axes[2] == 1) {
transpose(t);
} else if (axes[0] == 0 && axes[1] == 2 && axes[2] == 1) {
+ NPUW_ASSERT(t.get_element_type() == ov::element::i4); // 4bit only here
ov::Shape tshape = {shape[0], shape[2], shape[1]};
ov::Tensor tnew(t.get_element_type(), tshape);
@@ -1491,6 +1513,7 @@ void ov::npuw::util::permute(ov::Tensor& t, const std::vector<std::size_t>& axes
}
t = std::move(tnew);
} else if (axes[0] == 1 && axes[1] == 0 && axes[2] == 2) {
+ NPUW_ASSERT(t.get_element_type() == ov::element::i4); // 4bit only here too
ov::Shape tshape = {shape[1], shape[0], shape[2]};
ov::Tensor tnew(t.get_element_type(), tshape);
@@ -1504,6 +1527,88 @@ void ov::npuw::util::permute(ov::Tensor& t, const std::vector<std::size_t>& axes
}
}
t = std::move(tnew);
+ } else if (axes[0] == 1 && axes[1] == 2 && axes[2] == 0) {
+ ov::Shape tshape = {shape[1], shape[2], shape[0]};
+ ov::Tensor tnew(t.get_element_type(), tshape);
+ switch (t.get_element_type()) {
+ case ov::element::f32:
+ permute120<uint32_t>(t, tnew);
+ break;
+ case ov::element::f16:
+ permute120<uint16_t>(t, tnew);
+ break;
+ default:
+ NPUW_ASSERT("Element type is not supported yet");
+ }
+ t = std::move(tnew);
+ } else {
+ NPUW_ASSERT(false && "Not supported yet");
+ }
+}
+
+ov::Tensor ov::npuw::util::concat(const std::vector<ov::Tensor>& tt, std::size_t axis) {
+ NPUW_ASSERT(axis == 0 || axis == 2);
+
+ const auto type = tt.front().get_element_type();
+ auto shape = tt.front().get_shape();
+ std::size_t new_dim = 0;
+
+ std::vector<std::size_t> offsets;
+ std::vector<std::size_t> lens;
+ for (auto&& t : tt) {
+ NPUW_ASSERT(tt.front().get_element_type() == t.get_element_type());
+ NPUW_ASSERT(t.is_continuous());
+
+ auto tshape = t.get_shape();
+ for (std::size_t d = 0; d < tshape.size(); d++) {
+ if (d != axis) {
+ NPUW_ASSERT(shape[d] == tshape[d]);
+ } else {
+ offsets.push_back(new_dim);
+ lens.push_back(tshape[d]);
+ new_dim += tshape[d];
+ }
+ }
+ }
+ shape[axis] = new_dim;
+
+ if (axis == 0) {
+ ov::Tensor tnew(tt.front().get_element_type(), shape);
+ uint8_t* pDst = static_cast<uint8_t*>(tnew.data());
+
+ const bool is_4bit = (type == ov::element::i4 || type == ov::element::u4);
+ for (std::size_t t_idx = 0; t_idx < tt.size(); t_idx++) {
+ const uint8_t* pSrc = static_cast<uint8_t*>(tt[t_idx].data());
+
+ const auto copy_size = lens[t_idx] * shape[1] * shape[2];
+ const auto copy_len = is_4bit ? copy_size / 2 : copy_size * type.size();
+
+ std::copy_n(pSrc, copy_len, pDst);
+ pDst += copy_len;
+ }
+ return tnew;
+ } else if (axis == 2) {
+ ov::Tensor tnew(tt.front().get_element_type(), shape);
+ uint8_t* pDst = static_cast<uint8_t*>(tnew.data());
+
+ const bool is_4bit = (type == ov::element::i4 || type == ov::element::u4);
+ for (std::size_t t_idx = 0; t_idx < tt.size(); t_idx++) {
+ const auto& t_src = tt[t_idx];
+
+ for (std::size_t r = 0; r < shape[0] * shape[1]; r++) {
+ const auto r_offset = is_4bit ? new_dim * r / 2 : new_dim * r * type.size();
+ const auto c_offset = is_4bit ? offsets[t_idx] / 2 : offsets[t_idx] * type.size();
+ const auto copy_len = is_4bit ? lens[t_idx] / 2 : lens[t_idx] * type.size();
+ uint8_t* pDstRow = pDst + r_offset + c_offset;
+
+ const auto r_offset_src = is_4bit ? lens[t_idx] * r / 2 : lens[t_idx] * r * type.size();
+ const uint8_t* pSrc = static_cast<uint8_t*>(t_src.data());
+ const uint8_t* pSrcRow = pSrc + r_offset_src;
+
+ std::copy_n(pSrcRow, copy_len, pDstRow);
+ }
+ }
+ return tnew;
} else {
NPUW_ASSERT(false && "Not supported yet");
}
diff --git a/src/plugins/intel_npu/src/plugin/npuw/util.hpp b/src/plugins/intel_npu/src/plugin/npuw/util.hpp
index 2a3c0ea7787..3a935d0a656 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/util.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/util.hpp
@@ -54,6 +54,7 @@ void to_f32(const ov::Tensor& in, ov::Tensor& out);
void to_f16(ov::Tensor& t);
void transpose(ov::Tensor& t);
void permute(ov::Tensor& t, const std::vector<std::size_t>& axes);
+ov::Tensor concat(const std::vector<ov::Tensor>& tt, std::size_t axis);
namespace at {
template <class M>