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
Implement RewriteGQAAttentionMask transformation to align attention m… (#39090)
liangd13a17a3ab1f
diff --git a/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.cpp b/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.cppindex 2ae9515076c..5c4396eb55b 100644--- a/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.cpp@@ -33,6 +33,7 @@#include "npuw_transformations/replace_deepstack_scatter_with_add.hpp"#include "npuw_transformations/reshape_sliced_head_to_static.hpp"#include "npuw_transformations/reshape_to_static.hpp"+#include "npuw_transformations/rewrite_gqa_attention_mask.hpp"#include "npuw_transformations/right_align_mask_slice_for_conv.hpp"#include "npuw_transformations/shrink_sliding_window_kv_cache.hpp"#include "npuw_transformations/slice_out_embeds.hpp"@@ -728,6 +729,8 @@ std::vector<std::shared_ptr<ov::Model>> ov::npuw::LLMCompiledModel::create_generov::npuw::ReshapeToStatic(max_generation_token_len, kv_size, axes, m_max_lora_rank, whisper_lhs_seq_size).run_on_model(generate_variant);+ ov::npuw::RewriteGQAAttentionMask().run_on_model(generate_variant);+if (m_cfg.get<::intel_npu::NPUW_LLM_ENABLE_SWA_KV_CACHE_SHRINK>()) {// Must run after ReshapeToStatic, before OptimizeValueTensors (V-tensor optimization).// No-op (returns false) for models without sliding-window attention layers.@@ -1107,6 +1110,7 @@ ov::npuw::LLMCompiledModel::LLMCompiledModel(const std::shared_ptr<ov::Model>& mtrue).run_on_model(prefill_model);}+ ov::npuw::RewriteGQAAttentionMask().run_on_model(prefill_model);LOG_DEBUG("Make kvcache model with static shapes");if (m_cfg.get<::intel_npu::NPUW_LLM_ENABLE_SWA_KV_CACHE_SHRINK>()) {diff --git a/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/rewrite_gqa_attention_mask.cpp b/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/rewrite_gqa_attention_mask.cppnew file mode 100644index 00000000000..dda9d68cf6c--- /dev/null+++ b/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/rewrite_gqa_attention_mask.cpp@@ -0,0 +1,181 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "rewrite_gqa_attention_mask.hpp"++#include <numeric>+#include <optional>++#include "../llm_infer_base_request.hpp"+#include "../logging.hpp"+#include "openvino/core/rt_info.hpp"+#include "openvino/op/ops.hpp"+#include "openvino/pass/graph_rewrite.hpp"+#include "openvino/pass/manager.hpp"+#include "openvino/pass/matcher_pass.hpp"+#include "openvino/pass/pattern/op/optional.hpp"+#include "openvino/pass/pattern/op/wrap_type.hpp"++namespace opp = ov::pass::pattern;++namespace {++constexpr const char* kGQANameMarker = "GroupQueryAttention";++// Query branch of the mask: CumSum -> Add -> Broadcast -> Transpose+struct QueryBranch {+ std::shared_ptr<ov::Node> cumsum;+ std::shared_ptr<ov::Node> add;+ std::shared_ptr<ov::Node> broadcast;+ std::shared_ptr<ov::Node> transpose;+};++std::optional<QueryBranch> match_query_branch(const ov::Output<ov::Node>& value) {+ auto transpose = ov::as_type_ptr<ov::op::v1::Transpose>(value.get_node_shared_ptr());+ if (!transpose) {+ return std::nullopt;+ }+ auto broadcast = transpose->input_value(0).get_node_shared_ptr();+ if (!ov::is_type_any_of<ov::op::v1::Broadcast, ov::op::v3::Broadcast>(broadcast)) {+ return std::nullopt;+ }+ auto add = ov::as_type_ptr<ov::op::v1::Add>(broadcast->input_value(0).get_node_shared_ptr());+ if (!add) {+ return std::nullopt;+ }+ for (size_t i = 0; i < add->get_input_size(); ++i) {+ auto cumsum = add->input_value(i).get_node_shared_ptr();+ if (ov::is_type<ov::op::v0::CumSum>(cumsum)) {+ return QueryBranch{cumsum, add, broadcast, transpose};+ }+ }+ return std::nullopt;+}++// diagnostics warnings on OPENVINO_MATCHER_PASS_RTTI() definition: visibility hidden+#ifdef __GNUC__+# pragma GCC diagnostic push+# pragma GCC diagnostic ignored "-Wattributes"+#endif++class RewriteGQAAttentionMaskImpl : public ov::pass::MatcherPass {+public:+ OPENVINO_MATCHER_PASS_RTTI("ov::npuw::RewriteGQAAttentionMaskImpl");++ explicit RewriteGQAAttentionMaskImpl(const std::shared_ptr<ov::Node>& attention_mask_bool) {+ auto less = opp::wrap_type<ov::op::v1::Less>({opp::any_input(), opp::any_input()});+ auto select = opp::wrap_type<ov::op::v1::Select>(+ {less, opp::any_input(), opp::any_input()},+ [](const ov::Output<ov::Node>& output) {+ return output.get_node()->get_friendly_name().find(kGQANameMarker) != std::string::npos;+ });+ auto add_mask = opp::wrap_type<ov::op::v1::Add>({select, opp::any_input()});+ // GroupQueryAttentionDecomposition inserts a Maximum clamp for f16/bf16 logits.+ auto clamp = opp::optional<ov::op::v1::Maximum>({add_mask, opp::any_input()});+ auto softmax = opp::wrap_type<ov::op::v1::Softmax, ov::op::v8::Softmax>({clamp});++ auto callback = [=](opp::Matcher& m) {+ const auto& node_to_output = m.get_pattern_value_map();+ auto matched_less = node_to_output.at(less).get_node_shared_ptr();+ auto matched_select = node_to_output.at(select).get_node_shared_ptr();++ // Less can take the key CumSum and the query branch in either order; keep the original order.+ std::optional<QueryBranch> query;+ size_t query_idx = 0;+ for (size_t i = 0; i < 2 && !query; ++i) {+ const auto other = matched_less->input_value(1 - i).get_node_shared_ptr();+ if (!ov::is_type<ov::op::v0::CumSum>(other)) {+ continue;+ }+ query = match_query_branch(matched_less->input_value(i));+ query_idx = i;+ }+ if (!query) {+ return false;+ }++ const auto& mask_shape = matched_less->get_output_partial_shape(0);+ if (mask_shape.rank().is_dynamic() || mask_shape.rank().get_length() < 2) {+ LOG_WARN("RewriteGQAAttentionMask: unexpected mask rank for " << matched_less->get_friendly_name()+ << ", skipping");+ return false;+ }+ const auto& query_length_dim = mask_shape[mask_shape.rank().get_length() - 2];+ OPENVINO_ASSERT(query_length_dim.is_static() && query_length_dim.get_length() > 0,+ "RewriteGQAAttentionMask: query length must be static and positive");+ const bool is_kvcache_model = query_length_dim.get_length() == 1;+ ov::NodeVector new_nodes{attention_mask_bool};+ std::shared_ptr<ov::Node> attn_mask = attention_mask_bool;+ const auto rank = mask_shape.rank().get_length();+ if (rank > 2) {+ std::vector<int64_t> axes(rank - 2);+ std::iota(axes.begin(), axes.end(), 1);+ auto axes_const = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{axes.size()}, axes);+ attn_mask = std::make_shared<ov::op::v0::Unsqueeze>(attention_mask_bool, axes_const);+ new_nodes.insert(new_nodes.end(), {axes_const, attn_mask});+ }+ if (is_kvcache_model) {+ ov::copy_runtime_info({matched_less, query->add, query->broadcast, query->transpose}, new_nodes);+ matched_select->input(0).replace_source_output(attn_mask);+ return true;+ }++ // Clone Broadcast/Transpose instead of editing them in place: they may be shared by other masks.+ auto bcast_inputs = query->broadcast->input_values();+ bcast_inputs[0] = query->cumsum;+ auto new_broadcast = query->broadcast->clone_with_new_inputs(bcast_inputs);+ auto new_transpose =+ query->transpose->clone_with_new_inputs({new_broadcast, query->transpose->input_value(1)});++ ov::OutputVector less_inputs = matched_less->input_values();+ less_inputs[query_idx] = new_transpose;+ auto less_equal = std::make_shared<ov::op::v1::LessEqual>(less_inputs[0], less_inputs[1]);++ auto logical_and = std::make_shared<ov::op::v1::LogicalAnd>(less_equal, attn_mask);++ new_broadcast->set_friendly_name(query->broadcast->get_friendly_name() + "/npuw_gqa_mask");+ new_transpose->set_friendly_name(query->transpose->get_friendly_name() + "/npuw_gqa_mask");+ less_equal->set_friendly_name(matched_less->get_friendly_name() + "/npuw_less_equal");+ logical_and->set_friendly_name(matched_less->get_friendly_name() + "/npuw_and_attention_mask");+ new_nodes.insert(new_nodes.end(), {new_broadcast, new_transpose, less_equal, logical_and});+ ov::copy_runtime_info({matched_less, query->add, query->broadcast, query->transpose}, new_nodes);++ matched_select->input(0).replace_source_output(logical_and);+ LOG_DEBUG("RewriteGQAAttentionMask: rewrote mask for " << matched_select->get_friendly_name());+ return true;+ };+ register_matcher(std::make_shared<opp::Matcher>(softmax, "RewriteGQAAttentionMaskImpl"), std::move(callback));+ }+};++#ifdef __GNUC__+# pragma GCC diagnostic pop+#endif++} // namespace++namespace ov::npuw {++bool RewriteGQAAttentionMask::run_on_model(const std::shared_ptr<ov::Model>& model) {+ std::shared_ptr<ov::Node> attention_mask = nullptr;+ for (const auto& input : model->inputs()) {+ if (input.get_names().count(LLMInferBaseRequest::layer_names::attention_mask) != 0) {+ attention_mask = input.get_node_shared_ptr();+ break;+ }+ }+ if (!attention_mask) {+ return false;+ }++ auto attention_mask_bool = std::make_shared<ov::op::v0::Convert>(attention_mask, ov::element::boolean);+ attention_mask_bool->set_friendly_name("attention_mask/npuw_gqa_bool");++ ov::pass::Manager manager("rewrite-gqa-attention-mask");+ manager.set_per_pass_validation(true);+ manager.register_pass<RewriteGQAAttentionMaskImpl>(attention_mask_bool);+ return manager.run_passes(model);+}++} // namespace ov::npuwdiff --git a/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/rewrite_gqa_attention_mask.hpp b/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/rewrite_gqa_attention_mask.hppnew file mode 100644index 00000000000..90dda497015--- /dev/null+++ b/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/rewrite_gqa_attention_mask.hpp@@ -0,0 +1,38 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#pragma once++#include "openvino/pass/pass.hpp"++namespace ov::npuw {++// Rewrites the softmax mask produced by a decomposed GroupQueryAttention op so that+// it honours the model's `attention_mask` input (needed for NPUW static, padded KV buffers).+//+// Matched pattern (the Select's friendly name must contain "GroupQueryAttention"):+//+// CumSum(K) ----------------------------------------------++// v+// CumSum(Q) -> Add -> Broadcast -> Transpose ---------> Less -> Select -> Add -> [Maximum] -> Softmax+//+// Rewritten to:+//+// CumSum(K) ------------------------------------++// v+// CumSum(Q) -> Broadcast -> Transpose -----> LessEqual -> LogicalAnd -> Select -> ...+// ^+// attention_mask -> Convert(boolean) -> [Unsqueeze] ---------++//+// Run after static reshaping. If the mask's penultimate dimension is greater than 1,+// remove the original Add and use LessEqual AND attention_mask (prefill).+// If it equals 1, connect the boolean attention_mask directly to Select (KVCache).+class RewriteGQAAttentionMask : public ov::pass::ModelPass {+public:+ OPENVINO_MODEL_PASS_RTTI("ov::npuw::RewriteGQAAttentionMask");+ RewriteGQAAttentionMask() = default;+ bool run_on_model(const std::shared_ptr<ov::Model>& model) override;+};++} // namespace ov::npuwdiff --git a/src/plugins/intel_npu/src/plugin/sources.cmake b/src/plugins/intel_npu/src/plugin/sources.cmakeindex 54c31d13bd2..36047225657 100644--- a/src/plugins/intel_npu/src/plugin/sources.cmake+++ b/src/plugins/intel_npu/src/plugin/sources.cmake@@ -163,6 +163,8 @@ set(NPUW_SOURCES${CMAKE_CURRENT_SOURCE_DIR}/npuw/npuw_transformations/reshape_to_static.hpp${CMAKE_CURRENT_SOURCE_DIR}/npuw/npuw_transformations/reshape_to_static_gqa.cpp${CMAKE_CURRENT_SOURCE_DIR}/npuw/npuw_transformations/reshape_to_static_gqa.hpp+ ${CMAKE_CURRENT_SOURCE_DIR}/npuw/npuw_transformations/rewrite_gqa_attention_mask.cpp+ ${CMAKE_CURRENT_SOURCE_DIR}/npuw/npuw_transformations/rewrite_gqa_attention_mask.hpp${CMAKE_CURRENT_SOURCE_DIR}/npuw/npuw_transformations/propagate_slice.cpp${CMAKE_CURRENT_SOURCE_DIR}/npuw/npuw_transformations/propagate_slice.hpp${CMAKE_CURRENT_SOURCE_DIR}/npuw/npuw_transformations/slice_out_embeds.cppdiff --git a/src/plugins/intel_npu/tests/unit/CMakeLists.txt b/src/plugins/intel_npu/tests/unit/CMakeLists.txtindex ce0a586b323..7a7f0a57642 100644--- a/src/plugins/intel_npu/tests/unit/CMakeLists.txt+++ b/src/plugins/intel_npu/tests/unit/CMakeLists.txt@@ -133,6 +133,7 @@ ov_add_test_target(${OpenVINO_SOURCE_DIR}/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/reshape_to_static_gqa.cpp${OpenVINO_SOURCE_DIR}/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/replace_deepstack_scatter_with_add.cpp${OpenVINO_SOURCE_DIR}/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/right_align_mask_slice_for_conv.cpp+ ${OpenVINO_SOURCE_DIR}/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/rewrite_gqa_attention_mask.cpp${OpenVINO_SOURCE_DIR}/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/propagate_slice.cpp${OpenVINO_SOURCE_DIR}/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/slice_out_embeds.cpp# kokoro pipelinediff --git a/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/rewrite_gqa_attention_mask_test.cpp b/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/rewrite_gqa_attention_mask_test.cppnew file mode 100644index 00000000000..41484b89025--- /dev/null+++ b/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/rewrite_gqa_attention_mask_test.cpp@@ -0,0 +1,205 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "npuw_transformations/rewrite_gqa_attention_mask.hpp"++#include <gtest/gtest.h>++#include <algorithm>+#include <limits>+#include <memory>+#include <string>+#include <vector>++#include "common_test_utils/ov_test_utils.hpp"+#include "openvino/op/ops.hpp"+#include "openvino/pass/constant_folding.hpp"++namespace {++using namespace ov;++constexpr int64_t kKvLen = 8;+constexpr int64_t kQLen = 4;++struct MaskModelParams {+ int64_t kv_len = kKvLen;+ int64_t q_len = kQLen;+ bool rank4 = false; // [1, 1, Q, KV] mask instead of [Q, KV]+ bool query_first = false; // Less(query, key) instead of Less(key, query)+ bool with_attention_mask = true;+ std::string select_name = "GroupQueryAttention_0/Select";+};++// Builds:+// CumSum(K) ------------------------------------------++// CumSum(Q) -> [Add] -> Broadcast -> Transpose --> Less|LessEqual -> [LogicalAnd(attention_mask)] -> Select+// -> Add(logits) -> Softmax+std::shared_ptr<Model> build_model(const MaskModelParams& p, bool rewritten) {+ ParameterVector params;++ auto attention_mask = std::make_shared<op::v0::Parameter>(element::i64, PartialShape{1, p.kv_len});+ attention_mask->output(0).set_names({p.with_attention_mask ? "attention_mask" : "other_input"});+ params.push_back(attention_mask);++ const Shape k_shape =+ p.rank4 ? Shape{1, 1, 1, static_cast<size_t>(p.kv_len)} : Shape{1, static_cast<size_t>(p.kv_len)};+ auto k_src = std::make_shared<op::v0::Parameter>(element::i64, k_shape);+ auto q_src = std::make_shared<op::v0::Parameter>(element::i64, Shape{static_cast<size_t>(p.q_len)});+ params.push_back(k_src);+ params.push_back(q_src);++ auto k_axis = op::v0::Constant::create(element::i64, Shape{}, {static_cast<int64_t>(k_shape.size()) - 1});+ auto k_cumsum = std::make_shared<op::v0::CumSum>(k_src, k_axis);++ auto q_axis = op::v0::Constant::create(element::i64, Shape{}, {0});+ std::shared_ptr<Node> q_value = std::make_shared<op::v0::CumSum>(q_src, q_axis);+ if (!rewritten) {+ q_value = std::make_shared<op::v1::Add>(q_value, op::v0::Constant::create(element::i64, Shape{}, {123}));+ }++ const std::vector<int64_t> bcast_shape =+ p.rank4 ? std::vector<int64_t>{1, 1, 1, p.q_len} : std::vector<int64_t>{1, p.q_len};+ auto bcast = std::make_shared<op::v3::Broadcast>(+ q_value,+ op::v0::Constant::create(element::i64, Shape{bcast_shape.size()}, bcast_shape));+ const std::vector<int64_t> order = p.rank4 ? std::vector<int64_t>{0, 1, 3, 2} : std::vector<int64_t>{1, 0};+ auto transpose =+ std::make_shared<op::v1::Transpose>(bcast, op::v0::Constant::create(element::i64, Shape{order.size()}, order));++ Output<Node> lhs = k_cumsum, rhs = transpose;+ if (p.query_first) {+ std::swap(lhs, rhs);+ }+ std::shared_ptr<Node> cond;+ if (rewritten) {+ cond = std::make_shared<op::v1::LessEqual>(lhs, rhs);+ std::shared_ptr<Node> mask = std::make_shared<op::v0::Convert>(attention_mask, element::boolean);+ if (p.rank4) {+ mask = std::make_shared<op::v0::Unsqueeze>(mask, op::v0::Constant::create(element::i64, Shape{2}, {1, 2}));+ }+ cond = p.q_len == 1 ? mask : std::make_shared<op::v1::LogicalAnd>(cond, mask);+ } else {+ cond = std::make_shared<op::v1::Less>(lhs, rhs);+ }++ auto zero = op::v0::Constant::create(element::f32, Shape{}, {0.0f});+ auto lowest = op::v0::Constant::create(element::f32, Shape{}, {std::numeric_limits<float>::lowest()});+ auto select = std::make_shared<op::v1::Select>(cond, zero, lowest);+ select->set_friendly_name(p.select_name);++ auto logits =+ std::make_shared<op::v0::Parameter>(element::f32,+ Shape{1, 2, static_cast<size_t>(p.q_len), static_cast<size_t>(p.kv_len)});+ params.push_back(logits);+ auto add = std::make_shared<op::v1::Add>(logits, select);+ auto softmax = std::make_shared<op::v8::Softmax>(add, -1);++ return std::make_shared<Model>(OutputVector{softmax}, params);+}++class RewriteGQAAttentionMaskTest : public TransformationTestsF,+ public ::testing::WithParamInterface<std::tuple<bool, bool, int64_t>> {};++TEST_P(RewriteGQAAttentionMaskTest, RewritesMask) {+ MaskModelParams p;+ std::tie(p.rank4, p.query_first, p.q_len) = GetParam();+ model = build_model(p, false);+ manager.register_pass<npuw::RewriteGQAAttentionMask>();+ manager.register_pass<pass::ConstantFolding>();+ comparator.enable(FunctionsComparator::CONST_VALUES);+ model_ref = build_model(p, true);+}++INSTANTIATE_TEST_SUITE_P(NPUW,+ RewriteGQAAttentionMaskTest,+ ::testing::Combine(::testing::Bool(), ::testing::Bool(), ::testing::Values(1, 4)),+ [](const ::testing::TestParamInfo<std::tuple<bool, bool, int64_t>>& info) {+ return std::string(std::get<0>(info.param) ? "Rank4" : "Rank2") ++ (std::get<1>(info.param) ? "_QueryFirst" : "_KeyFirst") + "_Q" ++ std::to_string(std::get<2>(info.param));+ });++TEST_F(TransformationTestsF, RewriteGQAAttentionMask_NotAppliedWithoutGQAName) {+ MaskModelParams p;+ p.select_name = "SomeOtherAttention/Select";+ model = build_model(p, false);+ manager.register_pass<npuw::RewriteGQAAttentionMask>();+ model_ref = build_model(p, false);+}++TEST_F(TransformationTestsF, RewriteGQAAttentionMask_NotAppliedWithoutAttentionMask) {+ MaskModelParams p;+ p.with_attention_mask = false;+ model = build_model(p, false);+ manager.register_pass<npuw::RewriteGQAAttentionMask>();+ model_ref = build_model(p, false);+}++class RewriteGQAAttentionMaskValuesTest : public ::testing::TestWithParam<std::tuple<int64_t, int64_t, bool, int64_t>> {+};++TEST_P(RewriteGQAAttentionMaskValuesTest, StageMaskPreservesValidKeysAndCausality) {+ MaskModelParams p;+ int64_t reshape_delta = 0;+ std::tie(p.kv_len, p.q_len, p.rank4, reshape_delta) = GetParam();+ auto model = build_model(p, false);+ npuw::RewriteGQAAttentionMask().run_on_model(model);++ std::shared_ptr<Node> condition;+ for (const auto& node : model->get_ordered_ops()) {+ if (is_type<op::v1::Select>(node)) {+ condition = node->input_value(0).get_node_shared_ptr();+ }+ }+ ASSERT_NE(condition, nullptr);+ ASSERT_EQ(is_type<op::v1::LogicalAnd>(condition), p.q_len != 1);++ // Changing key capacity must preserve the selected mask behavior.+ const int64_t key_length = p.kv_len + reshape_delta;+ auto key_shape = model->get_parameters()[1]->get_shape();+ key_shape.back() = key_length;+ model->get_parameters()[0]->set_partial_shape(PartialShape{1, key_length});+ model->get_parameters()[1]->set_partial_shape(key_shape);+ auto logits_shape = model->get_parameters()[3]->get_shape();+ logits_shape.back() = key_length;+ model->get_parameters()[3]->set_partial_shape(logits_shape);+ model->validate_nodes_and_infer_types();+ auto mask_model = std::make_shared<Model>(OutputVector{condition}, model->get_parameters());++ TensorVector inputs;+ for (const auto& parameter : model->get_parameters()) {+ inputs.emplace_back(parameter->get_element_type(), parameter->get_shape());+ auto& tensor = inputs.back();+ if (tensor.get_element_type() == element::i64) {+ std::fill_n(tensor.data<int64_t>(), tensor.get_size(), 1);+ } else {+ std::fill_n(tensor.data<float>(), tensor.get_size(), 0.f);+ }+ }+ for (bool padded : {false, true}) {+ // Keep past tokens on the left and current tokens on the right, with an invalid KV gap.+ inputs[0].data<int64_t>()[2] = padded ? 0 : 1;+ inputs[0].data<int64_t>()[3] = padded ? 0 : 1;+ const auto outputs = ov::test::utils::infer_on_template(mask_model, inputs);+ ASSERT_EQ(outputs[0].get_size(), static_cast<size_t>(p.q_len * key_length));+ for (int64_t q = 0; q < p.q_len; ++q) {+ for (int64_t k = 0; k < key_length; ++k) {+ const bool causal = p.q_len == 1 || k <= q;+ const bool expected = causal && inputs[0].data<int64_t>()[k] != 0;+ EXPECT_EQ(outputs[0].data<char>()[q * key_length + k] != 0, expected)+ << "q=" << q << ", k=" << k << ", padded=" << padded;+ }+ }+ }+}++INSTANTIATE_TEST_SUITE_P(NPUW,+ RewriteGQAAttentionMaskValuesTest,+ ::testing::Combine(::testing::Values(8, 1152),+ ::testing::Values(1, 4, 8),+ ::testing::Bool(),+ ::testing::Values(0, 2)));++} // namespace