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] Do not treat function inputs as closures in head optimization passes (#38630)
Alexander Kalistratov1a528613441c
diff --git a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cppindex 28cac934ed7..610a5adac1f 100644--- a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp@@ -920,7 +920,8 @@ bool ov::npuw::CompiledModel::should_use_quantized_host_gather(const std::shared}// Check NPUW_HOST_GATHER:QUANT based on the patterns (for tail vocab)- ov::npuw::patterns::opt::Context ctx;+ // Before closure extraction, every Parameter is a function input.+ ov::npuw::patterns::opt::Context ctx(model, model->get_parameters().size());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);diff --git a/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.cpp b/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.cppindex 728a335f42f..a4ee6f5a367 100644--- a/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.cpp@@ -1619,7 +1619,8 @@ void Partitioner::saveTailDictConstants(const std::string& func_name) {using CPtr = std::shared_ptr<ov::op::v0::Constant>;std::vector<CPtr> to_keep;- ov::npuw::patterns::opt::Context ctx;+ // Before closure extraction, every Parameter is a function input.+ ov::npuw::patterns::opt::Context ctx(model_group.front(), model_group.front()->get_parameters().size());ctx.mm_gate = cfg.get<::intel_npu::NPUW_MM_GATED>();ov::pass::GraphRewrite rewr;@@ -2158,6 +2159,7 @@ void Partitioner::optimize(const std::string& func_name) {auto do_permute = [&](ov::npuw::patterns::opt::Context& ctx) {for (auto&& p : ctx.closures_to_permute) {auto param_idx = f._model->get_parameter_index(p.first);+ NPUW_ASSERT(param_idx >= static_cast<int64_t>(f._param_offset));auto closure_idx = param_idx - f._param_offset;ov::npuw::util::non_parallel_for(func_group.refs.size(), [&](std::size_t f_idx) {auto& funcall = func_group.refs[f_idx].get();@@ -2168,7 +2170,7 @@ void Partitioner::optimize(const std::string& func_name) {auto do_cvtf16 = [&](ov::npuw::patterns::opt::Context& ctx) {for (auto&& p : ctx.closures_to_f16) {auto param_idx = f._model->get_parameter_index(p);- NPUW_ASSERT(param_idx != -1);+ NPUW_ASSERT(param_idx >= static_cast<int64_t>(f._param_offset));auto closure_idx = param_idx - f._param_offset;ov::npuw::util::non_parallel_for(func_group.refs.size(), [&](std::size_t f_idx) {auto& funcall = func_group.refs[f_idx].get();@@ -2179,7 +2181,7 @@ void Partitioner::optimize(const std::string& func_name) {// Regardless of DQ setting, run this first{- ov::npuw::patterns::opt::Context ctx;+ ov::npuw::patterns::opt::Context ctx(f._model, f._param_offset);ctx.is_spatial = f._spatial.has_value();ctx.pmm_dims = cfg.get<::intel_npu::NPUW_PMM>();@@ -2234,6 +2236,7 @@ void Partitioner::optimize(const std::string& func_name) {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);+ NPUW_ASSERT(p_to_concat_idx >= static_cast<int64_t>(f._param_offset));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);@@ -2260,6 +2263,7 @@ void Partitioner::optimize(const std::string& func_name) {for (auto&& p : ctx.params_to_nf4_gather) {const auto& tensor_to_gather = p.second;auto w_idx = f._model->get_parameter_index(tensor_to_gather.w);+ NPUW_ASSERT(w_idx >= static_cast<int64_t>(f._param_offset));// Need to add a new parameter right away, since it's going to be processed by the unpack belowf._model->add_parameters({p.first});@@ -2287,6 +2291,9 @@ void Partitioner::optimize(const std::string& func_name) {auto w_idx = f._model->get_parameter_index(tensor_to_unpack.w);auto z_idx = f._model->get_parameter_index(tensor_to_unpack.z);auto s_idx = f._model->get_parameter_index(tensor_to_unpack.s);+ NPUW_ASSERT(w_idx >= static_cast<int64_t>(f._param_offset));+ NPUW_ASSERT(s_idx >= static_cast<int64_t>(f._param_offset));+ NPUW_ASSERT(z_idx == -1 || z_idx >= static_cast<int64_t>(f._param_offset));new_params.push_back(p.first);to_remove.push_back(tensor_to_unpack.w);@@ -2468,7 +2475,7 @@ void Partitioner::optimize(const std::string& func_name) {LOG_BLOCK();// Run "dynamic quantization"- ov::npuw::patterns::opt::Context ctx;+ ov::npuw::patterns::opt::Context ctx(f._model, f._param_offset);ctx.is_spatial = f._spatial.has_value();ctx.mm_dq_full = cfg.get<::intel_npu::NPUW_DQ_FULL>();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.cppindex 6f3bbed8ce4..5018caf36a4 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@@ -28,6 +28,21 @@ bool is_subtract_128(const std::shared_ptr<ov::Node>& node) {} // namespace+Context::Context(std::shared_ptr<const ov::Model> model, std::size_t param_base_idx)+ : model(std::move(model)),+ param_base_idx(param_base_idx) {+ NPUW_ASSERT(this->model);+ NPUW_ASSERT(param_base_idx <= this->model->get_parameters().size());+}++bool Context::is_closure(const PPtr& param) const {+ if (param_base_idx == 0) {+ return true;+ }+ const auto idx = model->get_parameter_index(param);+ return idx < 0 || static_cast<std::size_t>(idx) >= param_base_idx;+}+void Context::permute(const PPtr& orig_param, const Context::Axes& order) {closures_to_permute[orig_param] = order;@@ -978,6 +993,11 @@ DQParMMGQ::DQParMMGQ(Context::Ref ctx) {return false;}+ if (!ctx.get().is_closure(w_param) || !ctx.get().is_closure(s_param)) {+ // Only closures can be concatenated. Function inputs stay unchanged.+ return false;+ }+if (!matmul->get_transpose_a() && !matmul->get_transpose_b()) {ctx.get().register_parallel_matmul(node_to_output.at(qmmi),@@ -1688,6 +1708,8 @@ HostGather::HostGather(Context::Ref ctx) {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();+ auto matched_qweight =+ std::static_pointer_cast<ov::op::v0::Parameter>(matched_out_qweight.get_node_shared_ptr());const auto& matched_out_gather = node_to_output.at(qgthrw);@@ -1701,13 +1723,14 @@ HostGather::HostGather(Context::Ref ctx) {return readers.begin()->get_node();};+ // NB: Only a closure can be the vocab. A function input (an output of+ // another subgraph) is not a weight and stays unchanged.if (out_shape.back() >= 2048 && (qweight_type == ov::element::f16 || qweight_type == ov::element::f32) &&+ ctx.get().is_closure(matched_qweight) &&(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) {@@ -2039,7 +2062,8 @@ DQUnpackDictMatMulGQi::DQUnpackDictMatMulGQi(Context::Ref ctx) {// TO:// Param(W):f16 -------->// ???(Act) -> to(f16) -> MatMul -> to(f32) -> Result-// NB: This pass only worsens the performance so is disabled+// NB: Only closure weights are compressed. A Parameter that is a function+// input (an output of another subgraph) is not a weight and stays unchanged.CompressDictMatMulf32::CompressDictMatMulf32(Context::Ref ctx) {auto weight = opp::wrap_type<ov::op::v0::Parameter>();auto mmi = opp::any_input();@@ -2059,7 +2083,7 @@ CompressDictMatMulf32::CompressDictMatMulf32(Context::Ref ctx) {auto matched_matmul = std::static_pointer_cast<ov::op::v0::MatMul>(matched_node_matmul);auto matched_result = std::static_pointer_cast<ov::op::v0::Result>(matched_node_res);- if (ov::element::f32 == matched_weight->get_element_type() &&+ if (ov::element::f32 == matched_weight->get_element_type() && ctx.get().is_closure(matched_weight) &&matched_matmul->output(0).get_target_inputs().size() == 1) {auto new_cvt_a = std::make_shared<ov::op::v0::Convert>(matched_mmi, ov::element::f16);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.hppindex 163d62900d6..d2ac0248720 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@@ -7,6 +7,7 @@#include <functional>#include <memory>#include <optional>+#include <set>#include <string>#include "openvino/openvino.hpp"@@ -29,6 +30,13 @@ struct Context {using NPtr = std::shared_ptr<ov::Node>;using CPtr = std::shared_ptr<ov::op::v0::Constant>;+ // Parameters [0, param_base_idx) of model are function inputs, not closures.+ // Passes must not change them.+ const std::shared_ptr<const ov::Model> model;+ const std::size_t param_base_idx;+ Context(std::shared_ptr<const ov::Model> model, std::size_t param_base_idx);+ bool is_closure(const PPtr& param) const;+using Axes = std::vector<std::size_t>;std::map<PPtr, Axes> closures_to_permute;void permute(const PPtr& orig_param, const Axes& order);diff --git a/src/plugins/intel_npu/tests/unit/CMakeLists.txt b/src/plugins/intel_npu/tests/unit/CMakeLists.txtindex 97df62f88b7..2b6ae5d411a 100644--- a/src/plugins/intel_npu/tests/unit/CMakeLists.txt+++ b/src/plugins/intel_npu/tests/unit/CMakeLists.txt@@ -202,6 +202,7 @@ target_sources(${TARGET_NAME} PRIVATE${CMAKE_CURRENT_SOURCE_DIR}/npuw/subgraph_pipeline.cpp${CMAKE_CURRENT_SOURCE_DIR}/npuw/subgraph_behavior_infer_test.cpp${CMAKE_CURRENT_SOURCE_DIR}/npuw/preserve_const_matmul_pattern_test.cpp+ ${CMAKE_CURRENT_SOURCE_DIR}/npuw/compress_dict_matmul_f32_test.cpp${CMAKE_CURRENT_SOURCE_DIR}/npuw/pipeline_passes/add_position_ids_param_test.cpp${CMAKE_CURRENT_SOURCE_DIR}/npuw/pipeline_passes/right_align_mask_slice_for_conv_test.cpp${CMAKE_CURRENT_SOURCE_DIR}/npuw/pipeline_passes/collapse_unqdq_test.cppdiff --git a/src/plugins/intel_npu/tests/unit/npuw/compress_dict_matmul_f32_test.cpp b/src/plugins/intel_npu/tests/unit/npuw/compress_dict_matmul_f32_test.cppnew file mode 100644index 00000000000..cd15c04a64d--- /dev/null+++ b/src/plugins/intel_npu/tests/unit/npuw/compress_dict_matmul_f32_test.cpp@@ -0,0 +1,339 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include <gtest/gtest.h>++#include <algorithm>+#include <set>++#include "openvino/op/ops.hpp"+#include "openvino/pass/graph_rewrite.hpp"+#include "partitioning/patterns/opt.hpp"++// Regression test for a segfault found on Gemma-3n (dense path, F16IC off):+// CompressDictMatMulf32 matched an f32 function input (an output of another+// subgraph) on the MatMul weight port, and Partitioner::optimize() then used it+// as a closure index (size_t underflow). Only closure Parameters may be rewritten.+// HostGather has the same closure guard for vocab Parameters.+// DQParMMGQ also guards closure Parameters before parallel MatMul merging.++namespace {++using namespace ov;++std::shared_ptr<ov::Model> mark_function_inputs(const std::shared_ptr<ov::Model>& model,+ const ov::ParameterVector& inputs) {+ ov::ParameterVector reordered = inputs;+ for (const auto& param : model->get_parameters()) {+ if (std::find(inputs.begin(), inputs.end(), param) == inputs.end()) {+ reordered.push_back(param);+ }+ }+ return std::make_shared<ov::Model>(model->get_results(), reordered);+}++struct MatMulModel {+ std::shared_ptr<ov::Model> model;+ std::shared_ptr<op::v0::Parameter> act;+ std::shared_ptr<op::v0::Parameter> weight;+ std::shared_ptr<op::v0::MatMul> matmul;+ std::shared_ptr<op::v0::Result> result;+};++MatMulModel make_model(element::Type type) {+ auto act = std::make_shared<op::v0::Parameter>(type, Shape{1, 1, 8});+ auto weight = std::make_shared<op::v0::Parameter>(type, Shape{4, 8});+ auto matmul = std::make_shared<op::v0::MatMul>(act, weight, false, true);+ auto result = std::make_shared<op::v0::Result>(matmul);+ auto model = std::make_shared<ov::Model>(ov::ResultVector{result}, ov::ParameterVector{act, weight});+ return {model, act, weight, matmul, result};+}++void run_pass(const MatMulModel& graph, ov::npuw::patterns::opt::Context& ctx) {+ ov::pass::GraphRewrite rewrite;+ rewrite.add_matcher<ov::npuw::patterns::opt::CompressDictMatMulf32>(std::ref(ctx));+ rewrite.run_on_model(graph.model);+}++void expect_compressed(const MatMulModel& graph, const ov::npuw::patterns::opt::Context& ctx) {+ EXPECT_EQ(ctx.closures_to_f16.size(), 1u);+ EXPECT_EQ(ctx.closures_to_f16.count(graph.weight), 1u);+ EXPECT_EQ(graph.weight->get_element_type(), element::f16);++ auto output_cvt = ov::as_type_ptr<op::v0::Convert>(graph.result->input_value(0).get_node_shared_ptr());+ ASSERT_NE(output_cvt, nullptr);+ EXPECT_EQ(output_cvt->get_destination_type(), element::f32);++ auto new_matmul = ov::as_type_ptr<op::v0::MatMul>(output_cvt->input_value(0).get_node_shared_ptr());+ ASSERT_NE(new_matmul, nullptr);+ EXPECT_NE(new_matmul, graph.matmul);+ EXPECT_EQ(new_matmul->input_value(1).get_node_shared_ptr(), graph.weight);++ auto input_cvt = ov::as_type_ptr<op::v0::Convert>(new_matmul->input_value(0).get_node_shared_ptr());+ ASSERT_NE(input_cvt, nullptr);+ EXPECT_EQ(input_cvt->get_destination_type(), element::f16);+ EXPECT_EQ(input_cvt->input_value(0).get_node_shared_ptr(), graph.act);+ EXPECT_NO_THROW(graph.model->validate_nodes_and_infer_types());+}++struct GatherModel {+ std::shared_ptr<ov::Model> model;+ std::shared_ptr<op::v0::Parameter> vocab;+ std::shared_ptr<op::v0::Parameter> ids;+ std::shared_ptr<op::v8::Gather> gather;+ std::shared_ptr<op::v0::Convert> convert;+};++GatherModel make_gather_model() {+ auto vocab = std::make_shared<op::v0::Parameter>(element::f32, Shape{16, 2048});+ auto ids = std::make_shared<op::v0::Parameter>(element::i64, Shape{1, 3});+ auto axis = op::v0::Constant::create(element::i64, Shape{}, {0});+ auto gather = std::make_shared<op::v8::Gather>(vocab, ids, axis);+ auto convert = std::make_shared<op::v0::Convert>(gather, element::f16);+ auto result = std::make_shared<op::v0::Result>(convert);+ auto model = std::make_shared<ov::Model>(ov::ResultVector{result}, ov::ParameterVector{vocab, ids});+ return {model, vocab, ids, gather, convert};+}++void run_host_gather(const GatherModel& graph, ov::npuw::patterns::opt::Context& ctx) {+ ov::pass::GraphRewrite rewrite;+ rewrite.add_matcher<ov::npuw::patterns::opt::HostGather>(std::ref(ctx));+ rewrite.run_on_model(graph.model);+}++struct ParallelMatMulModel {+ std::shared_ptr<ov::Model> model;+ ov::ParameterVector weights;+ ov::ParameterVector scales;+ std::vector<std::shared_ptr<op::v0::MatMul>> matmuls;+ ov::ResultVector results;+};++ParallelMatMulModel make_parallel_matmul_model(std::size_t branches) {+ auto act = std::make_shared<op::v0::Parameter>(element::f32, Shape{1, 1, 8});+ auto scalar = op::v0::Constant::create(element::f32, Shape{}, {1.0f});+ auto qmmi = std::make_shared<op::v1::Multiply>(act, scalar);+ ov::ParameterVector params{act};+ ov::ParameterVector weights, scales;+ std::vector<std::shared_ptr<op::v0::MatMul>> matmuls;+ ov::ResultVector results;++ for (std::size_t i = 0; i < branches; ++i) {+ auto weight = std::make_shared<op::v0::Parameter>(element::f16, Shape{1, 8, 2});+ auto scale = std::make_shared<op::v0::Parameter>(element::f32, Shape{1, 1, 2});+ auto weight_cvt = std::make_shared<op::v0::Convert>(weight, element::f32);+ auto scaled = std::make_shared<op::v1::Multiply>(weight_cvt, scale);+ auto shape = op::v0::Constant::create(element::i64, Shape{2}, {8, 2});+ auto reshaped = std::make_shared<op::v1::Reshape>(scaled, shape, false);+ auto matmul = std::make_shared<op::v0::MatMul>(qmmi, reshaped, false, false);+ results.push_back(std::make_shared<op::v0::Result>(matmul));+ matmuls.push_back(matmul);+ weights.push_back(weight);+ scales.push_back(scale);+ params.push_back(weight);+ params.push_back(scale);+ }++ auto model = std::make_shared<ov::Model>(results, params);+ return {model, weights, scales, matmuls, results};+}++void run_parallel_matmul(const ParallelMatMulModel& graph, ov::npuw::patterns::opt::Context& ctx) {+ ctx.pmm_dims = "2";+ ov::pass::GraphRewrite rewrite;+ rewrite.add_matcher<ov::npuw::patterns::opt::DQParMMGQ>(std::ref(ctx));+ rewrite.run_on_model(graph.model);+ ov::npuw::patterns::opt::mergeParallelMatMuls(graph.model, ctx);+}++void expect_original_branch(const ParallelMatMulModel& graph, std::size_t i) {+ EXPECT_EQ(graph.results[i]->input_value(0).get_node_shared_ptr(), graph.matmuls[i]);+ auto reshape = ov::as_type_ptr<op::v1::Reshape>(graph.matmuls[i]->input_value(1).get_node_shared_ptr());+ ASSERT_NE(reshape, nullptr);+ auto scaled = ov::as_type_ptr<op::v1::Multiply>(reshape->input_value(0).get_node_shared_ptr());+ ASSERT_NE(scaled, nullptr);+ EXPECT_EQ(scaled->input_value(1).get_node_shared_ptr(), graph.scales[i]);+ auto weight_cvt = ov::as_type_ptr<op::v0::Convert>(scaled->input_value(0).get_node_shared_ptr());+ ASSERT_NE(weight_cvt, nullptr);+ EXPECT_EQ(weight_cvt->input_value(0).get_node_shared_ptr(), graph.weights[i]);+}++void expect_merged_closures(const ParallelMatMulModel& graph, const ov::npuw::patterns::opt::Context& ctx) {+ EXPECT_EQ(ctx.params_to_concat.size(), 2u);+ std::set<ov::npuw::patterns::opt::Context::PPtr> expected_weights{graph.weights[0], graph.weights[1]};+ std::set<ov::npuw::patterns::opt::Context::PPtr> expected_scales{graph.scales[0], graph.scales[1]};+ bool found_weights = false;+ bool found_scales = false;+ for (const auto& entry : ctx.params_to_concat) {+ EXPECT_EQ(entry.second.second, 2u);+ std::set<ov::npuw::patterns::opt::Context::PPtr> originals(entry.second.first.begin(),+ entry.second.first.end());+ if (entry.first->get_element_type() == element::f16) {+ EXPECT_EQ(originals, expected_weights);+ EXPECT_EQ(entry.first->get_shape(), (Shape{1, 8, 4}));+ found_weights = true;+ } else if (entry.first->get_element_type() == element::f32) {+ EXPECT_EQ(originals, expected_scales);+ EXPECT_EQ(entry.first->get_shape(), (Shape{1, 1, 4}));+ found_scales = true;+ } else {+ ADD_FAILURE() << "Unexpected concatenated Parameter type";+ }+ }+ EXPECT_TRUE(found_weights);+ EXPECT_TRUE(found_scales);+}++} // namespace++TEST(CompressDictMatMulf32Test, NonClosureParamIsNotCompressed) {+ auto graph = make_model(element::f32);+ graph.model = mark_function_inputs(graph.model, {graph.weight});+ ov::npuw::patterns::opt::Context ctx(graph.model, 1);++ run_pass(graph, ctx);++ EXPECT_EQ(graph.weight->get_element_type(), element::f32);+ EXPECT_TRUE(ctx.closures_to_f16.empty());+ EXPECT_EQ(graph.result->input_value(0).get_node_shared_ptr(), graph.matmul);+ EXPECT_NO_THROW(graph.model->validate_nodes_and_infer_types());+}++TEST(CompressDictMatMulf32Test, ClosureParamIsCompressed) {+ auto graph = make_model(element::f32);+ ov::npuw::patterns::opt::Context ctx(graph.model, 0);++ run_pass(graph, ctx);++ expect_compressed(graph, ctx);+}++TEST(CompressDictMatMulf32Test, OtherNonClosureParamDoesNotBlockClosure) {+ auto graph = make_model(element::f32);+ graph.model = mark_function_inputs(graph.model, {graph.act});+ ov::npuw::patterns::opt::Context ctx(graph.model, 1);++ run_pass(graph, ctx);++ expect_compressed(graph, ctx);+}++TEST(CompressDictMatMulf32Test, F16WeightIsNotTouched) {+ auto graph = make_model(element::f16);+ ov::npuw::patterns::opt::Context ctx(graph.model, 0);++ run_pass(graph, ctx);++ EXPECT_EQ(graph.weight->get_element_type(), element::f16);+ EXPECT_TRUE(ctx.closures_to_f16.empty());+ EXPECT_EQ(graph.result->input_value(0).get_node_shared_ptr(), graph.matmul);+ EXPECT_NO_THROW(graph.model->validate_nodes_and_infer_types());+}++TEST(HostGatherClosureTest, NonClosureVocabIsNotGathered) {+ auto graph = make_gather_model();+ graph.model = mark_function_inputs(graph.model, {graph.vocab});+ ov::npuw::patterns::opt::Context ctx(graph.model, 1);++ run_host_gather(graph, ctx);++ EXPECT_EQ(graph.vocab->get_element_type(), element::f32);+ EXPECT_TRUE(ctx.closures_to_f16.empty());+ EXPECT_FALSE(ctx.params_to_gather.has_value());+ EXPECT_EQ(graph.convert->input_value(0).get_node_shared_ptr(), graph.gather);+ EXPECT_NO_THROW(graph.model->validate_nodes_and_infer_types());+}++TEST(HostGatherClosureTest, ClosureVocabIsGathered) {+ auto graph = make_gather_model();+ ov::npuw::patterns::opt::Context ctx(graph.model, 0);++ run_host_gather(graph, ctx);++ EXPECT_EQ(ctx.closures_to_f16.size(), 1u);+ EXPECT_EQ(ctx.closures_to_f16.count(graph.vocab), 1u);+ EXPECT_EQ(graph.vocab->get_element_type(), element::f16);+ ASSERT_TRUE(ctx.params_to_gather.has_value());+ EXPECT_EQ(ctx.params_to_gather->pold, graph.vocab);+ EXPECT_EQ(ctx.params_to_gather->pids, graph.ids);+ EXPECT_EQ(ctx.params_to_gather->pnew->get_element_type(), element::f16);+ EXPECT_EQ(ctx.params_to_gather->pnew->get_shape(), (Shape{1, 3, 2048}));++ auto new_convert = ov::as_type_ptr<op::v0::Convert>(graph.convert->input_value(0).get_node_shared_ptr());+ ASSERT_NE(new_convert, nullptr);+ EXPECT_EQ(new_convert->get_destination_type(), element::f32);+ EXPECT_EQ(new_convert->input_value(0).get_node_shared_ptr(), ctx.params_to_gather->pnew);+ EXPECT_EQ(graph.convert->get_destination_type(), element::f16);+}++TEST(DQParMMGQClosureTest, ClosuresAreMerged) {+ auto graph = make_parallel_matmul_model(2);+ ov::npuw::patterns::opt::Context ctx(graph.model, 0);++ run_parallel_matmul(graph, ctx);++ expect_merged_closures(graph, ctx);+}++TEST(DQParMMGQClosureTest, NonClosureBranchIsNotMerged) {+ auto graph = make_parallel_matmul_model(2);+ graph.model = mark_function_inputs(graph.model, {graph.weights[1]});+ ov::npuw::patterns::opt::Context ctx(graph.model, 1);++ run_parallel_matmul(graph, ctx);++ EXPECT_TRUE(ctx.params_to_concat.empty());+ EXPECT_EQ(graph.weights[1]->get_element_type(), element::f16);+ expect_original_branch(graph, 0);+ expect_original_branch(graph, 1);+ EXPECT_NO_THROW(graph.model->validate_nodes_and_infer_types());+}++TEST(DQParMMGQClosureTest, ClosureBranchesStillMergeWithOneNonClosure) {+ auto graph = make_parallel_matmul_model(3);+ graph.model = mark_function_inputs(graph.model, {graph.scales[2]});+ ov::npuw::patterns::opt::Context ctx(graph.model, 1);++ run_parallel_matmul(graph, ctx);++ expect_merged_closures(graph, ctx);+ expect_original_branch(graph, 2);+}++TEST(ContextIsClosureTest, ZeroBaseTreatsAllAsClosures) {+ auto param = std::make_shared<op::v0::Parameter>(element::f32, Shape{1});+ auto model = std::make_shared<ov::Model>(ov::ResultVector{std::make_shared<op::v0::Result>(param)},+ ov::ParameterVector{param});+ ov::npuw::patterns::opt::Context ctx(model, 0);+ EXPECT_TRUE(ctx.is_closure(param));+}++TEST(ContextIsClosureTest, InputsBeforeBaseAreNotClosures) {+ auto first = std::make_shared<op::v0::Parameter>(element::f32, Shape{1});+ auto second = std::make_shared<op::v0::Parameter>(element::f32, Shape{1});+ auto third = std::make_shared<op::v0::Parameter>(element::f32, Shape{1});+ auto model = std::make_shared<ov::Model>(ov::ResultVector{std::make_shared<op::v0::Result>(third)},+ ov::ParameterVector{first, second, third});+ ov::npuw::patterns::opt::Context ctx(model, 2);++ EXPECT_FALSE(ctx.is_closure(first));+ EXPECT_FALSE(ctx.is_closure(second));+ EXPECT_TRUE(ctx.is_closure(third));+}++TEST(ContextIsClosureTest, ParameterNotInModelIsClosure) {+ auto first = std::make_shared<op::v0::Parameter>(element::f32, Shape{1});+ auto second = std::make_shared<op::v0::Parameter>(element::f32, Shape{1});+ auto model = std::make_shared<ov::Model>(ov::ResultVector{std::make_shared<op::v0::Result>(second)},+ ov::ParameterVector{first, second});+ ov::npuw::patterns::opt::Context ctx(model, 2);++ auto new_param = std::make_shared<op::v0::Parameter>(element::f32, Shape{1});+ EXPECT_TRUE(ctx.is_closure(new_param));+}++TEST(ContextIsClosureTest, ConstructorRejectsBaseAboveParamCount) {+ auto graph = make_model(element::f32);+ EXPECT_ANY_THROW(ov::npuw::patterns::opt::Context(graph.model, graph.model->get_parameters().size() + 1));+}diff --git a/src/plugins/intel_npu/tests/unit/npuw/dq_matmul_cwi_shared_weight_test.cpp b/src/plugins/intel_npu/tests/unit/npuw/dq_matmul_cwi_shared_weight_test.cppindex 79a992552db..f8e47819999 100644--- a/src/plugins/intel_npu/tests/unit/npuw/dq_matmul_cwi_shared_weight_test.cpp+++ b/src/plugins/intel_npu/tests/unit/npuw/dq_matmul_cwi_shared_weight_test.cpp@@ -105,7 +105,7 @@ void run_dqmatmulcwi(const std::shared_ptr<ov::Model>& model, ov::npuw::patternsTEST(DQMatMulCWiSharedWeightTest, SharedWeightChainIsNotRewritten) {auto model = make_shared_weight_model(/*num_matmuls=*/2);- ov::npuw::patterns::opt::Context ctx;+ ov::npuw::patterns::opt::Context ctx(model, 0);ctx.mm_dq_full = true;run_dqmatmulcwi(model, ctx);@@ -133,7 +133,7 @@ TEST(DQMatMulCWiSharedWeightTest, SharedWeightChainIsNotRewritten) {TEST(DQMatMulCWiSharedWeightTest, MultiplyBranchBeforeOptionalConvertIsNotRewritten) {auto graph = make_branched_weight_model();- ov::npuw::patterns::opt::Context ctx;+ ov::npuw::patterns::opt::Context ctx(graph.model, 0);ctx.mm_dq_full = true;run_dqmatmulcwi(graph.model, ctx);@@ -151,7 +151,7 @@ TEST(DQMatMulCWiSharedWeightTest, MultiplyBranchBeforeOptionalConvertIsNotRewritTEST(DQMatMulCWiSharedWeightTest, SingleConsumerChainIsStillOptimized) {auto model = make_shared_weight_model(/*num_matmuls=*/1);- ov::npuw::patterns::opt::Context ctx;+ ov::npuw::patterns::opt::Context ctx(model, 0);ctx.mm_dq_full = true;run_dqmatmulcwi(model, ctx);diff --git a/src/plugins/intel_npu/tests/unit/npuw/gather_sub128_matcher_test.cpp b/src/plugins/intel_npu/tests/unit/npuw/gather_sub128_matcher_test.cppindex 1e141a62752..f5bfa81d75d 100644--- a/src/plugins/intel_npu/tests/unit/npuw/gather_sub128_matcher_test.cpp+++ b/src/plugins/intel_npu/tests/unit/npuw/gather_sub128_matcher_test.cpp@@ -262,15 +262,16 @@ TEST(DQLiftGatherAsymCWTest, RejectsUnmarked128Subtractions) {}TEST(HostGatherQuantAsymmTest, AcceptsPairedSub128Shifts) {- ov::npuw::patterns::opt::Context context;- EXPECT_TRUE(run_host_gather(make_parameter_gather_model(128.0f), context));+ const auto model = make_parameter_gather_model(128.0f);+ ov::npuw::patterns::opt::Context context(model, 0);+ EXPECT_TRUE(run_host_gather(model, context));ASSERT_TRUE(context.params_to_quant_gather_unpack.has_value());ASSERT_EQ(context.params_to_quant_gather_unpack->params_to_runtime_unpack_gather.size(), 1);}TEST(HostGatherQuantAsymmTest, MarksPairedSub128Sources) {- ov::npuw::patterns::opt::Context context;const auto model = make_parameter_gather_model(128.0f);+ ov::npuw::patterns::opt::Context context(model, 0);EXPECT_TRUE(run_host_gather(model, context));ASSERT_TRUE(context.params_to_quant_gather_unpack.has_value());@@ -305,11 +306,11 @@ TEST(HostGatherQuantAsymmTest, MarksPairedSub128Sources) {}TEST(HostGatherQuantAsymmTest, KeepsRawSourcesWithoutMarkedPattern) {- ov::npuw::patterns::opt::Context context;const auto model = make_parameter_gather_model(std::nullopt,std::nullopt,ov::element::u8,ov::element::u8);+ ov::npuw::patterns::opt::Context context(model, 0);EXPECT_TRUE(run_host_gather(model, context));ASSERT_TRUE(context.params_to_quant_gather_unpack.has_value());@@ -320,14 +321,15 @@ TEST(HostGatherQuantAsymmTest, KeepsRawSourcesWithoutMarkedPattern) {}TEST(HostGatherQuantAsymmTest, RejectsNon128Subtractions) {- ov::npuw::patterns::opt::Context context;- EXPECT_FALSE(run_host_gather(make_parameter_gather_model(127.0f), context));+ const auto model = make_parameter_gather_model(127.0f);+ ov::npuw::patterns::opt::Context context(model, 0);+ EXPECT_FALSE(run_host_gather(model, context));}TEST(HostGatherQuantAsymmTest, RejectsUnmarked128Subtractions) {const auto model = make_parameter_gather_model(128.0f);clear_sub128_shift_markers(model);- ov::npuw::patterns::opt::Context context;+ ov::npuw::patterns::opt::Context context(model, 0);EXPECT_FALSE(run_host_gather(model, context));EXPECT_FALSE(context.params_to_quant_gather_unpack.has_value());@@ -335,8 +337,8 @@ TEST(HostGatherQuantAsymmTest, RejectsUnmarked128Subtractions) {}TEST(ConvertDQVocabTest, ReplacesMarkedShiftsWithLazySources) {- ov::npuw::patterns::opt::Context context;const auto model = make_parameter_vocab_dq_model();+ ov::npuw::patterns::opt::Context context(model, 0);ov::pass::GraphRewrite rewrite;rewrite.add_matcher<ov::npuw::patterns::opt::ConvertDQVocab>(std::ref(context));diff --git a/src/plugins/intel_npu/tests/unit/npuw/host_gather_id_clamp_test.cpp b/src/plugins/intel_npu/tests/unit/npuw/host_gather_id_clamp_test.cppindex 8513be2d2ce..0720d1744a5 100644--- a/src/plugins/intel_npu/tests/unit/npuw/host_gather_id_clamp_test.cpp+++ b/src/plugins/intel_npu/tests/unit/npuw/host_gather_id_clamp_test.cpp@@ -192,7 +192,7 @@ using namespace ov::npuw::patterns::opt;TEST(HostGatherIdClamp, HostGather_NoClamp_Matches) {auto model = make_host_gather_model(Clamp::None);- Context ctx;+ Context ctx(model, 0);run_pass<HostGather>(model, std::ref(ctx));EXPECT_TRUE(ctx.params_to_gather.has_value());@@ -202,7 +202,7 @@ TEST(HostGatherIdClamp, HostGather_NoClamp_Matches) {TEST(HostGatherIdClamp, HostGather_ExactClamp_Matches) {auto model = make_host_gather_model(Clamp::Exact);- Context ctx;+ Context ctx(model, 0);run_pass<HostGather>(model, std::ref(ctx));EXPECT_TRUE(ctx.params_to_gather.has_value());@@ -212,7 +212,7 @@ TEST(HostGatherIdClamp, HostGather_ExactClamp_Matches) {TEST(HostGatherIdClamp, HostGather_MaximumOnlyClamp_Matches) {auto model = make_host_gather_model(Clamp::MaxOnly);- Context ctx;+ Context ctx(model, 0);run_pass<HostGather>(model, std::ref(ctx));EXPECT_TRUE(ctx.params_to_gather.has_value());@@ -222,7 +222,7 @@ TEST(HostGatherIdClamp, HostGather_MaximumOnlyClamp_Matches) {TEST(HostGatherIdClamp, HostGather_WrongBoundClamp_DoesNotMatch) {auto model = make_host_gather_model(Clamp::WrongBound);- Context ctx;+ Context ctx(model, 0);run_pass<HostGather>(model, std::ref(ctx));EXPECT_FALSE(ctx.params_to_gather.has_value());@@ -235,7 +235,7 @@ TEST(HostGatherIdClamp, HostGather_WrongBoundClamp_DoesNotMatch) {TEST(HostGatherIdClamp, HostGatherDQ_NoClamp_Matches) {auto model = make_host_gather_dq_model(Clamp::None);- Context ctx;+ Context ctx(model, 0);run_pass<HostGatherDQ>(model, std::ref(ctx));EXPECT_TRUE(ctx.params_to_gather.has_value());@@ -245,7 +245,7 @@ TEST(HostGatherIdClamp, HostGatherDQ_NoClamp_Matches) {TEST(HostGatherIdClamp, HostGatherDQ_ExactClamp_Matches) {auto model = make_host_gather_dq_model(Clamp::Exact);- Context ctx;+ Context ctx(model, 0);run_pass<HostGatherDQ>(model, std::ref(ctx));EXPECT_TRUE(ctx.params_to_gather.has_value());@@ -255,7 +255,7 @@ TEST(HostGatherIdClamp, HostGatherDQ_ExactClamp_Matches) {TEST(HostGatherIdClamp, HostGatherDQ_WrongBoundClamp_DoesNotMatch) {auto model = make_host_gather_dq_model(Clamp::WrongBound);- Context ctx;+ Context ctx(model, 0);run_pass<HostGatherDQ>(model, std::ref(ctx));EXPECT_FALSE(ctx.params_to_gather.has_value());@@ -268,7 +268,7 @@ TEST(HostGatherIdClamp, HostGatherDQ_WrongBoundClamp_DoesNotMatch) {TEST(HostGatherIdClamp, HostGatherQuantAsymm_NoClamp_Matches) {auto model = make_host_gather_quant_asymm_model(Clamp::None);- Context ctx;+ Context ctx(model, 0);run_pass<HostGatherQuantAsymm<op::v0::Parameter>>(model, std::ref(ctx), false);ASSERT_TRUE(ctx.params_to_quant_gather_unpack.has_value());@@ -278,7 +278,7 @@ TEST(HostGatherIdClamp, HostGatherQuantAsymm_NoClamp_Matches) {TEST(HostGatherIdClamp, HostGatherQuantAsymm_ExactClamp_Matches) {auto model = make_host_gather_quant_asymm_model(Clamp::Exact);- Context ctx;+ Context ctx(model, 0);run_pass<HostGatherQuantAsymm<op::v0::Parameter>>(model, std::ref(ctx), false);ASSERT_TRUE(ctx.params_to_quant_gather_unpack.has_value());@@ -288,7 +288,7 @@ TEST(HostGatherIdClamp, HostGatherQuantAsymm_ExactClamp_Matches) {TEST(HostGatherIdClamp, HostGatherQuantAsymm_WrongBoundClamp_DoesNotMatch) {auto model = make_host_gather_quant_asymm_model(Clamp::WrongBound);- Context ctx;+ Context ctx(model, 0);run_pass<HostGatherQuantAsymm<op::v0::Parameter>>(model, std::ref(ctx), false);EXPECT_FALSE(ctx.params_to_quant_gather_unpack.has_value());@@ -300,7 +300,7 @@ TEST(HostGatherIdClamp, HostGatherQuantAsymm_WrongBoundClamp_DoesNotMatch) {TEST(HostGatherIdClamp, HostGatherQuantSymm_NoClamp_Matches) {auto model = make_host_gather_quant_symm_model(Clamp::None);- Context ctx;+ Context ctx(model, 0);run_pass<HostGatherQuantSymm<op::v0::Parameter>>(model, std::ref(ctx), false);ASSERT_TRUE(ctx.params_to_quant_gather_unpack.has_value());@@ -310,7 +310,7 @@ TEST(HostGatherIdClamp, HostGatherQuantSymm_NoClamp_Matches) {TEST(HostGatherIdClamp, HostGatherQuantSymm_ExactClamp_Matches) {auto model = make_host_gather_quant_symm_model(Clamp::Exact);- Context ctx;+ Context ctx(model, 0);run_pass<HostGatherQuantSymm<op::v0::Parameter>>(model, std::ref(ctx), false);ASSERT_TRUE(ctx.params_to_quant_gather_unpack.has_value());@@ -320,7 +320,7 @@ TEST(HostGatherIdClamp, HostGatherQuantSymm_ExactClamp_Matches) {TEST(HostGatherIdClamp, HostGatherQuantSymm_WrongBoundClamp_DoesNotMatch) {auto model = make_host_gather_quant_symm_model(Clamp::WrongBound);- Context ctx;+ Context ctx(model, 0);run_pass<HostGatherQuantSymm<op::v0::Parameter>>(model, std::ref(ctx), false);EXPECT_FALSE(ctx.params_to_quant_gather_unpack.has_value());@@ -332,7 +332,7 @@ TEST(HostGatherIdClamp, HostGatherQuantSymm_WrongBoundClamp_DoesNotMatch) {TEST(HostGatherIdClamp, HostGatherCB4_NoClamp_Matches) {auto model = make_host_gather_cb4_model(Clamp::None);- Context ctx;+ Context ctx(model, 0);run_pass<HostGatherCB4>(model, std::ref(ctx));EXPECT_TRUE(ctx.params_to_gather.has_value());@@ -342,7 +342,7 @@ TEST(HostGatherIdClamp, HostGatherCB4_NoClamp_Matches) {TEST(HostGatherIdClamp, HostGatherCB4_ExactClamp_Matches) {auto model = make_host_gather_cb4_model(Clamp::Exact);- Context ctx;+ Context ctx(model, 0);run_pass<HostGatherCB4>(model, std::ref(ctx));EXPECT_TRUE(ctx.params_to_gather.has_value());@@ -352,7 +352,7 @@ TEST(HostGatherIdClamp, HostGatherCB4_ExactClamp_Matches) {TEST(HostGatherIdClamp, HostGatherCB4_WrongBoundClamp_DoesNotMatch) {auto model = make_host_gather_cb4_model(Clamp::WrongBound);- Context ctx;+ Context ctx(model, 0);run_pass<HostGatherCB4>(model, std::ref(ctx));EXPECT_FALSE(ctx.params_to_gather.has_value());diff --git a/src/plugins/intel_npu/tests/unit/npuw/preserve_const_matmul_pattern_test.cpp b/src/plugins/intel_npu/tests/unit/npuw/preserve_const_matmul_pattern_test.cppindex 8156d622dfe..d1838178f24 100644--- a/src/plugins/intel_npu/tests/unit/npuw/preserve_const_matmul_pattern_test.cpp+++ b/src/plugins/intel_npu/tests/unit/npuw/preserve_const_matmul_pattern_test.cpp@@ -100,7 +100,7 @@ SubgraphNodes build_asymm_matmul_subgraph(const ov::Shape& weight_shape,void run_preserve_pattern(std::shared_ptr<ov::Model>& model, ResultNodes& to_keep) {ov::pass::Manager manager;using namespace ov::npuw::patterns::opt;- Context ctx;+ Context ctx(model, 0);ctx.mm_gate = false;manager.register_pass<PreserveConstDictMatMulAsymm>(std::ref(ctx), std::ref(to_keep));manager.run_passes(model);