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] Make Whisper cross-attention passes tolerate decomposed SDPA (#37402)
Parth Maheshwari64fe79fa89a5
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 36ae9cf8989..819e5d8f7c4 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@@ -1023,7 +1023,13 @@ ov::npuw::LLMCompiledModel::LLMCompiledModel(const std::shared_ptr<ov::Model>& mwhisper_lhs_seq_size =static_cast<uint32_t>(prefill_model->input("encoder_hidden_states").get_partial_shape()[1].get_length());auto whisper_decompose_sdpa = m_cfg.get<::intel_npu::NPUW_WHISPER_DECOMPOSE_SDPA>();- if (whisper_decompose_sdpa) {++ // With word-level timestamps, the decoder may receive tokens decoded from the+ // entire audio chunk in one prefill call, so the static prompt length needs to+ // reach almost the full KV-cache size rather than the usual (smaller) max prompt.+ // This is needed whether GenAI already decomposed cross-attention SDPA itself+ // (CPU/GPU) or NPUW is about to decompose it below (NPU).+ if (whisper_decompose_sdpa || ov::npuw::util::has_decomposed_cross_attention_sdpa(prefill_model)) {m_kvcache_desc.max_prompt_size = whisper_kvcache_size - 1;}diff --git a/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/convert_kvcache_to_precision.cpp b/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/convert_kvcache_to_precision.cppindex 12a211be059..fc66b0210f1 100644--- a/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/convert_kvcache_to_precision.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/convert_kvcache_to_precision.cpp@@ -83,6 +83,10 @@ std::shared_ptr<ov::Model> cvt_kvcache_to_low_precision(const std::shared_ptr<ovov::preprocess::PrePostProcessor ppp(model);for (const auto& tensor : model->inputs()) {+ // Defensive: get_any_name() throws on a nameless port.+ if (tensor.get_names().empty()) {+ continue;+ }const auto& name = tensor.get_any_name();if (ov::npuw::util::isPastKeyValuesKey(name).has_value()) {ppp.input(name).tensor().set_element_type(key_storage_type);@@ -92,6 +96,10 @@ std::shared_ptr<ov::Model> cvt_kvcache_to_low_precision(const std::shared_ptr<ov}for (const auto& tensor : model->outputs()) {+ // See above.+ if (tensor.get_names().empty()) {+ continue;+ }const auto& name = tensor.get_any_name();if (ov::npuw::util::isPresentKeyValuesKey(name).has_value()) {ppp.output(name).tensor().set_element_type(key_storage_type);diff --git a/src/plugins/intel_npu/src/plugin/npuw/whisper/prepare_whisper_model.cpp b/src/plugins/intel_npu/src/plugin/npuw/whisper/prepare_whisper_model.cppindex d61c745e53d..32dcecf5809 100644--- a/src/plugins/intel_npu/src/plugin/npuw/whisper/prepare_whisper_model.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/whisper/prepare_whisper_model.cpp@@ -4,6 +4,7 @@#include "prepare_whisper_model.hpp"+#include <optional>#include <regex>#include "../llm_compiled_model_utils.hpp"@@ -101,6 +102,32 @@ public:}};+// If cross-attention SDPA has already been decomposed (by GenAI, or by+// decompose_scaled_dot_product_attention_for_whisper() below), there's no SDPA node left+// to find for it. The decomposition always tags the "QK scaled scores" node with this+// well-known tensor name, so it doubles as a reliable marker for where the block lives.+std::vector<std::shared_ptr<ov::Node>> find_decomposed_cross_attn_score_nodes(const std::shared_ptr<ov::Model>& model) {+ std::vector<std::shared_ptr<ov::Node>> found;+ for (const auto& op : model->get_ordered_ops()) {+ bool matched = false;+ for (const auto& output : op->outputs()) {+ for (const auto& name : output.get_names()) {+ if (name.find("cross_attention_qk_scaled_scores") != std::string::npos) {+ matched = true;+ break;+ }+ }+ if (matched) {+ break;+ }+ }+ if (matched) {+ found.push_back(op);+ }+ }+ return found;+}+class AttentionMaskInput : public ov::pass::MatcherPass {public:OPENVINO_MATCHER_PASS_RTTI("npuw::LLMCompiledModel::AttentionMaskInput");@@ -123,6 +150,8 @@ public:}}}+ auto decomposed_cross_attn_nodes = transform_cross_attn ? find_decomposed_cross_attn_score_nodes(model)+ : std::vector<std::shared_ptr<ov::Node>>{};// Self-attentionOPENVINO_ASSERT(!self_attn_nodes.empty());@@ -161,7 +190,7 @@ public:if (transform_cross_attn) {// Cross attn- OPENVINO_ASSERT(!cross_attn_nodes.empty());+ OPENVINO_ASSERT(!cross_attn_nodes.empty() || !decomposed_cross_attn_nodes.empty());// FIXME: Should be taken from topology - don't hardcode!!!auto shape_cst =std::make_shared<ov::op::v0::Constant>(ov::element::i64,@@ -191,6 +220,30 @@ public:cross_attn_node->input(3).replace_source_output(unsq2->output(0));}}+ for (const auto& qk_score_node : decomposed_cross_attn_nodes) {+ if (ov::is_type<ov::op::v1::Add>(qk_score_node)) {+ // The original SDPA already had a mask input (or was causal), so+ // decomposition produced Add(scores, mask) - mirrors the "cross_attn_node+ // already has 4/5 inputs" branch above.+ qk_score_node->input(1).replace_source_output(unsq2->output(0));+ } else {+ // Mirrors the "cross_attn_node has 3 inputs" branch above: the original+ // SDPA had no mask at all, so decomposition produced no Add either - splice+ // one in and move the "cross_attention_qk_scaled_scores*" tensor name(s)+ // onto it, since that's the value word-level-timestamp extraction expects.+ // Snapshot readers before wiring up new_add itself as a reader.+ auto readers = qk_score_node->output(0).get_target_inputs();+ auto new_add = std::make_shared<ov::op::v1::Add>(qk_score_node->output(0), unsq2->output(0));+ new_add->set_friendly_name(qk_score_node->get_friendly_name() + "/with_mask");+ auto names = qk_score_node->output(0).get_names();+ // Still feeds new_add - give it a synthetic name instead of leaving it nameless.+ qk_score_node->output(0).set_names({qk_score_node->get_friendly_name() + "/orig"});+ new_add->output(0).add_names(names);+ for (const auto& reader : readers) {+ reader.replace_source_output(new_add->output(0));+ }+ }+ }}}};@@ -471,24 +524,81 @@ public:}};-auto remove_encoder_attn_read_value(const std::shared_ptr<ov::Node>& rv_node,- const ov::Output<ov::Node>& kv_out,- const ov::Input<ov::Node>& sdpa_in) {- // Find Assign node+// A cross-attention KV-cache state (the ReadValue producing the once-computed+// encoder key/value) is consumed differently depending on whether the model+// still has a fused cross-attention SDPA node, or whether cross-attention SDPA+// has already been decomposed (by GenAI, or by+// decompose_scaled_dot_product_attention_for_whisper() below):+// - fused: ReadValue -> [FakeConvert ->] SDPA (key at port 1, value at port 2)+// - decomposed: key feeds the Transpose that builds "kT" for QK^T (and,+// separately, a ShapeOf reading the same state to size that+// transpose - both are redirected, only the Transpose decides the role)+// value feeds the final (softmax @ value) MatMul directly+// Returns the role for a single direct reader of the ReadValue's output, or+// nullopt if that reader alone doesn't tell us (e.g. Assign, ShapeOf).+enum class EncoderKvRole { Key, Value };++std::optional<EncoderKvRole> classify_encoder_kv_reader(const ov::Input<ov::Node>& reader) {+ auto* node = reader.get_node();+ if (strstr(node->get_type_name(), "FakeConvert") != nullptr) {+ // fp8: ReadValue -> FakeConvert -> {SDPA | Transpose | MatMul}. FakeConvert has a+ // single consumer, so its role is exactly the role of that consumer.+ auto fc_readers = node->outputs()[0].get_target_inputs();+ OPENVINO_ASSERT(fc_readers.size() == 1);+ return classify_encoder_kv_reader(*fc_readers.begin());+ }+ if (strstr(node->get_type_name(), "ScaledDotProductAttention") != nullptr) {+ return reader.get_index() == 1 ? EncoderKvRole::Key : EncoderKvRole::Value;+ }+ if (strstr(node->get_type_name(), "Transpose") != nullptr) {+ return EncoderKvRole::Key;+ }+ if (strstr(node->get_type_name(), "MatMul") != nullptr) {+ return EncoderKvRole::Value;+ }+ return std::nullopt;+}++// Splits an encoder-attn KV-cache ReadValue's readers into its single Assign and the+// (one or more) readers that consume the state's value, and determines whether this+// state is "key" or "value" from among the latter.+struct EncoderKvState {+ std::shared_ptr<ov::op::v6::Assign> assign_node;+ std::vector<ov::Input<ov::Node>> read_values;+ EncoderKvRole role;+};++EncoderKvState analyze_encoder_kv_read_value(const std::shared_ptr<ov::Node>& rv_node) {OPENVINO_ASSERT(rv_node->outputs().size() == 1);- auto rv_out = rv_node->outputs()[0];- ov::NodeVector rv_readers;- for (const auto& target_in : rv_out.get_target_inputs()) {- rv_readers.push_back(target_in.get_node()->shared_from_this());- }- // Assign and SDPA- OPENVINO_ASSERT(rv_readers.size() == 2);- auto assign_node = (strstr(rv_readers[0]->get_type_name(), "Assign") != nullptr) ? rv_readers[0] : rv_readers[1];- OPENVINO_ASSERT(strstr(assign_node->get_type_name(), "Assign") != nullptr);- // Redirect KV-cache tensor to SDPA- sdpa_in.replace_source_output(kv_out);- return std::make_pair(std::make_shared<ov::op::v0::Result>(kv_out),- ov::as_type_ptr<ov::op::v6::Assign>(assign_node));+ EncoderKvState state;+ std::optional<EncoderKvRole> role;+ for (const auto& reader : rv_node->output(0).get_target_inputs()) {+ if (strstr(reader.get_node()->get_type_name(), "Assign") != nullptr) {+ OPENVINO_ASSERT(!state.assign_node, "More than one Assign reads an encoder-attn KV-cache state");+ state.assign_node = ov::as_type_ptr<ov::op::v6::Assign>(reader.get_node()->shared_from_this());+ continue;+ }+ state.read_values.push_back(reader);+ if (!role) {+ role = classify_encoder_kv_reader(reader);+ }+ }+ OPENVINO_ASSERT(state.assign_node, "encoder-attn KV-cache state has no Assign");+ OPENVINO_ASSERT(!state.read_values.empty(), "encoder-attn KV-cache state is never read");+ OPENVINO_ASSERT(role, "Could not classify encoder-attn KV-cache state as key or value");+ state.role = *role;+ return state;+}++auto remove_encoder_attn_read_value(const std::shared_ptr<ov::Node>& rv_node, const EncoderKvState& state) {+ auto kv_out = rv_node->input_value(0);+ // Redirect every consumer of the state directly to its initial value - covers both+ // the single fused-SDPA reader and the several decomposed-key readers (Transpose ++ // ShapeOf) alike.+ for (const auto& reader : state.read_values) {+ reader.replace_source_output(kv_out);+ }+ return std::make_pair(std::make_shared<ov::op::v0::Result>(kv_out), state.assign_node);}std::string transform_key_value_name(std::string input_string,@@ -510,14 +620,6 @@ void set_name(std::shared_ptr<ov::Node> result, const std::string& name) {result->get_output_tensor(0).set_names({name});}-bool is_fake_cvt_to_key_tensor(const ov::Input<ov::Node>& reader) {- auto fc_reader = reader.get_node()->outputs()[0].get_target_inputs();- // FakeConvert node has only 1 consumer- OPENVINO_ASSERT(fc_reader.size() == 1);- // FakeConvert -> SDPA : 'key' tensor is input with index 1 to SDPA- return fc_reader.begin()->get_index() == 1;-}-void expose_runtime_states_as_outputs(const std::shared_ptr<ov::Model>& model) {// Find all ReadValue nodesov::NodeVector read_value_nodes;@@ -534,35 +636,16 @@ void expose_runtime_states_as_outputs(const std::shared_ptr<ov::Model>& model) {// Go through all ReadValue nodes and remove themfor (const auto& rv_node : read_value_nodes) {OPENVINO_ASSERT(rv_node->inputs().size() == 1);- OPENVINO_ASSERT(rv_node->outputs().size() == 1);- auto rv_in = rv_node->inputs()[0];- auto x = rv_in.get_source_output();- auto rv_out = rv_node->outputs()[0];- // Gather all nodes that read from ReadValue, there must be SDPA and Assign- auto rv_readers = rv_out.get_target_inputs();- OPENVINO_ASSERT(rv_readers.size() == 2);- // Input port for SDPA node- for (const auto& reader : rv_readers) {- bool is_fake_cvt = strstr(reader.get_node()->get_type_name(), "FakeConvert") != nullptr;- if (strstr(reader.get_node()->get_type_name(), "ScaledDotProductAttention") != nullptr || is_fake_cvt) {- auto sdpa_in = reader;-- // In case there's additional FakeConvert node(fp8): ReadValue -> FakeConvert -> SDPA- auto is_fc_key_tensor = is_fake_cvt ? is_fake_cvt_to_key_tensor(reader) : false;-- // Remove ReadValue, store new Result and Assign- auto key_or_value = (sdpa_in.get_index() == 1 || is_fc_key_tensor) ? "key" : "value";- auto [result, assign] = remove_encoder_attn_read_value(rv_node, rv_in.get_source_output(), sdpa_in);- auto normalized_name =- transform_key_value_name(rv_node->inputs()[0].get_source_output().get_node()->get_friendly_name(),- "present",- ".encoder.",- key_or_value);- set_name(result, normalized_name);- results.push_back(result);- assigns.push_back(assign);- }- }+ auto state = analyze_encoder_kv_read_value(rv_node);+ auto [result, assign] = remove_encoder_attn_read_value(rv_node, state);+ auto key_or_value = state.role == EncoderKvRole::Key ? "key" : "value";+ auto normalized_name = transform_key_value_name(rv_node->input_value(0).get_node()->get_friendly_name(),+ "present",+ ".encoder.",+ key_or_value);+ set_name(result, normalized_name);+ results.push_back(result);+ assigns.push_back(assign);}// Add, remove, validate@@ -620,34 +703,25 @@ void expose_runtime_states_as_inputs(const std::shared_ptr<ov::Model>& model) {}for (const auto& rv_node : read_value_nodes) {- auto rv_out = rv_node->outputs()[0];- auto rv_readers = rv_out.get_target_inputs();- for (auto rv_reader : rv_readers) {- bool is_fake_cvt = strstr(rv_reader.get_node()->get_type_name(), "FakeConvert") != nullptr;- if (strstr(rv_reader.get_node()->get_type_name(), "Assign") != nullptr) {- auto assign_node = ov::as_type_ptr<ov::op::v6::Assign>(rv_reader.get_node()->shared_from_this());- assigns.push_back(assign_node);- } else if (strstr(rv_reader.get_node()->get_type_name(), "ScaledDotProductAttention") != nullptr ||- is_fake_cvt) {- auto sdpa_in = rv_reader;-- auto shape = rv_node->get_output_partial_shape(0);- auto new_param = std::make_shared<ov::op::v0::Parameter>(rv_node->get_output_element_type(0), shape);-- // In case there's additional FakeConvert node(fp8): ReadValue -> FakeConvert -> SDPA- auto is_fc_key_tensor = is_fake_cvt ? is_fake_cvt_to_key_tensor(rv_reader) : false;-- auto key_or_value = (sdpa_in.get_index() == 1 || is_fc_key_tensor) ? "key" : "value";- auto normalized_name = transform_key_value_name(sdpa_in.get_node()->get_friendly_name(),- "past_key_values",- ".encoder.",- key_or_value);- set_name(new_param, normalized_name);-- params.push_back(new_param);- sdpa_in.replace_source_output(new_param->outputs()[0]);- }+ auto state = analyze_encoder_kv_read_value(rv_node);++ auto shape = rv_node->get_output_partial_shape(0);+ auto new_param = std::make_shared<ov::op::v0::Parameter>(rv_node->get_output_element_type(0), shape);+ auto key_or_value = state.role == EncoderKvRole::Key ? "key" : "value";+ // Layer index comes from the state's producer (the initial-value subgraph),+ // which is unaffected by whether cross-attention SDPA is fused or decomposed -+ // unlike the readers, whose names/types differ between the two shapes.+ auto normalized_name = transform_key_value_name(rv_node->input_value(0).get_node()->get_friendly_name(),+ "past_key_values",+ ".encoder.",+ key_or_value);+ set_name(new_param, normalized_name);+ params.push_back(new_param);++ for (const auto& reader : state.read_values) {+ reader.replace_source_output(new_param->output(0));}+ assigns.push_back(state.assign_node);}// Remove sinks and add new params@@ -660,7 +734,8 @@ void expose_runtime_states_as_inputs(const std::shared_ptr<ov::Model>& model) {void normalize_input_key_value_names(const std::shared_ptr<ov::Model>& model) {ov::ResultVector new_results, old_results;for (const auto& in : model->inputs()) {- if (in.get_any_name().find("decoder") == std::string::npos) {+ // Defensive: get_any_name() throws on a nameless port.+ if (in.get_names().empty() || in.get_any_name().find("decoder") == std::string::npos) {continue;}@@ -676,7 +751,8 @@ void normalize_input_key_value_names(const std::shared_ptr<ov::Model>& model) {void normalize_output_key_value_names(const std::shared_ptr<ov::Model>& model) {ov::ResultVector new_results, old_results;for (const auto& out : model->outputs()) {- if (out.get_any_name().find("decoder") == std::string::npos) {+ // See normalize_input_key_value_names.+ if (out.get_names().empty() || out.get_any_name().find("decoder") == std::string::npos) {continue;}@@ -804,3 +880,7 @@ bool ov::npuw::util::PrepareWhisperKVCacheModel::run_on_model(const std::shared_return true;}++bool ov::npuw::util::has_decomposed_cross_attention_sdpa(const std::shared_ptr<ov::Model>& model) {+ return !find_decomposed_cross_attn_score_nodes(model).empty();+}diff --git a/src/plugins/intel_npu/src/plugin/npuw/whisper/prepare_whisper_model.hpp b/src/plugins/intel_npu/src/plugin/npuw/whisper/prepare_whisper_model.hppindex 9a9bafb765a..e61238a8ab9 100644--- a/src/plugins/intel_npu/src/plugin/npuw/whisper/prepare_whisper_model.hpp+++ b/src/plugins/intel_npu/src/plugin/npuw/whisper/prepare_whisper_model.hpp@@ -43,6 +43,12 @@ public:bool run_on_model(const std::shared_ptr<ov::Model>& model) override;};+// True if cross-attention SDPA in this model has already been decomposed (word-level+// timestamps requested), detected from the "cross_attention_qk_scaled_scores" tensor name+// the decomposition attaches - covers GenAI having decomposed it upfront (CPU/GPU) as well+// as NPUW's own decomposition below (NPU) having already run on this model.+bool has_decomposed_cross_attention_sdpa(const std::shared_ptr<ov::Model>& model);+} // namespace util} // namespace npuw} // namespace ovdiff --git a/src/plugins/intel_npu/src/plugin/npuw/whisper/whisper_infer_request.cpp b/src/plugins/intel_npu/src/plugin/npuw/whisper/whisper_infer_request.cppindex 0044d1cd488..fa5044f45db 100644--- a/src/plugins/intel_npu/src/plugin/npuw/whisper/whisper_infer_request.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/whisper/whisper_infer_request.cpp@@ -58,11 +58,17 @@ void ov::npuw::WhisperInferRequest::infer_prefill(ov::SoPtr<ov::ITensor> input_i0u,m_npuw_llm_compiled_model->m_kvcache_desc.num_stored_tokens);- // for word-level timestamps- auto decomposed_sdpa_size = m_npuw_llm_compiled_model->m_decomposed_sdpa_size;- for (size_t idx = 0; idx < decomposed_sdpa_size; idx++) {- auto name = whisper_layer_names::qk_scores_ + std::to_string(idx);- m_alignment_tensors.insert({name, m_prefill_request->get_tensor(m_prefill_out_ports.at(name))});+ // For word-level timestamps. These real prefill outputs only carry their own indexed+ // qk_scores_N name (see add_cross_attention_qk_scaled_scores_outputs_for_whisper), never+ // the generic qk_scores marker - that marker only gets added to the fake outer-compiled-model+ // ports in llm_compiled_model.cpp. So key on the indexed name directly, not the marker.+ for (const auto& [_, port] : m_prefill_out_ports) {+ for (const auto& port_name : port.get_names()) {+ if (port_name.find(WhisperInferRequest::whisper_layer_names::qk_scores_) != std::string::npos) {+ m_alignment_tensors.insert({port_name, m_prefill_request->get_tensor(port)});+ break;+ }+ }}LOG_DEBUG("Done");diff --git a/src/plugins/intel_npu/tests/unit/npuw/prepare_whisper_model_test.cpp b/src/plugins/intel_npu/tests/unit/npuw/prepare_whisper_model_test.cppnew file mode 100644index 00000000000..ef6e6799f15--- /dev/null+++ b/src/plugins/intel_npu/tests/unit/npuw/prepare_whisper_model_test.cpp@@ -0,0 +1,148 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include <gtest/gtest.h>++#include <memory>+#include <string>++#include "llm_test_helpers.hpp"+#include "openvino/op/ops.hpp"+#include "openvino/op/scaled_dot_product_attention.hpp"+#include "openvino/pass/stateful_to_stateless.hpp"+#include "whisper/prepare_whisper_model.hpp"++namespace {++using ov::test::npuw::build_whisper_decoder_test_model;+using ov::test::npuw::WhisperConfig;++bool has_input_name(const std::shared_ptr<ov::Model>& model, const std::string& substr) {+ for (const auto& in : model->inputs()) {+ for (const auto& name : in.get_names()) {+ if (name.find(substr) != std::string::npos) {+ return true;+ }+ }+ }+ return false;+}++bool has_output_name(const std::shared_ptr<ov::Model>& model, const std::string& substr) {+ for (const auto& out : model->outputs()) {+ for (const auto& name : out.get_names()) {+ if (name.find(substr) != std::string::npos) {+ return true;+ }+ }+ }+ return false;+}++// Replicates the query/key/value part of GenAI's WhisperScaledDotProductAttentionDecomposition+// (and NPUW's own copy of it) for a cross-attention SDPA with no mask input - exactly the shape+// build_whisper_decoder_test_model() produces (encoder_attn SDPA has 3 inputs, no explicit mask).+// This is what a model handed to NPUW already looks like once GenAI decomposes cross-attention+// SDPA for NPU too, matching what it already does for CPU/GPU.+std::shared_ptr<ov::Node> decompose_one_cross_attn_sdpa(const std::shared_ptr<ov::op::v13::ScaledDotProductAttention>& sdpa) {+ using namespace ov::op;+ auto query = sdpa->input_value(0);+ auto key = sdpa->input_value(1);+ auto value = sdpa->input_value(2);++ auto q_shape = std::make_shared<v3::ShapeOf>(query, ov::element::i32);+ auto minus_one = v0::Constant::create(ov::element::i32, ov::Shape{}, {-1});+ auto zero_i = v0::Constant::create(ov::element::i32, ov::Shape{}, {0});+ auto one_i = v0::Constant::create(ov::element::i32, ov::Shape{}, {1});+ auto head_dim = std::make_shared<v8::Gather>(q_shape, minus_one, zero_i);+ auto head_dim_f = std::make_shared<v1::ConvertLike>(head_dim, query);+ auto sqrt_hd = std::make_shared<v0::Sqrt>(head_dim_f);+ auto one_f = std::make_shared<v1::ConvertLike>(one_i, query);+ auto scale = std::make_shared<v1::Divide>(one_f, sqrt_hd);++ // Q/K/V are always [batch, heads, seq, head_dim] in this test model - swap the last two axes.+ auto perm = v0::Constant::create(ov::element::i32, ov::Shape{4}, {0, 1, 3, 2});+ auto kT = std::make_shared<v1::Transpose>(key, perm);++ auto q_scaled = std::make_shared<v1::Multiply>(query, scale);+ auto scaled_atten = std::make_shared<v0::MatMul>(q_scaled, kT);+ scaled_atten->output(0).add_names({"cross_attention_qk_scaled_scores"});++ auto softmax = std::make_shared<v8::Softmax>(scaled_atten, -1);+ auto result = std::make_shared<v0::MatMul>(softmax, value);+ result->set_friendly_name(sdpa->get_friendly_name());+ return result;+}++// Decomposes every cross-attention (encoder_attn) SDPA node in-place, simulating a model that+// arrives at NPUW already decomposed by GenAI.+void decompose_cross_attention_sdpa_for_test(const std::shared_ptr<ov::Model>& model) {+ for (const auto& op : model->get_ordered_ops()) {+ auto sdpa = ov::as_type_ptr<ov::op::v13::ScaledDotProductAttention>(op);+ if (!sdpa || sdpa->get_friendly_name().find("encoder_attn") == std::string::npos) {+ continue;+ }+ auto new_node = decompose_one_cross_attn_sdpa(sdpa);+ ov::replace_node(sdpa, new_node);+ }+ model->validate_nodes_and_infer_types();+}++std::shared_ptr<ov::Model> stateless_whisper_decoder_model() {+ auto model = build_whisper_decoder_test_model();+ ov::pass::StatefulToStateless().run_on_model(model);+ return model->clone();+}++class PrepareWhisperModelTest : public ::testing::TestWithParam<bool> {};++// Runs PrepareWhisperPrefillModel/PrepareWhisperKVCacheModel on both a model with fused+// cross-attention SDPA (today's shape) and one with cross-attention SDPA already decomposed+// (the shape NPUW must also handle once GenAI decomposes for NPU, per CVS-184242). Both must+// succeed and produce the same encoder-attn KV-cache input/output names.+TEST_P(PrepareWhisperModelTest, PrefillPreparationHandlesFusedAndDecomposedCrossAttention) {+ const bool decompose = GetParam();++ auto model = stateless_whisper_decoder_model();+ if (decompose) {+ decompose_cross_attention_sdpa_for_test(model);+ }++ WhisperConfig cfg;+ ASSERT_TRUE(ov::npuw::util::PrepareWhisperPrefillModel(128,+ static_cast<uint32_t>(cfg.max_source_positions),+ false /*decompose_sdpa*/)+ .run_on_model(model));++ EXPECT_TRUE(has_input_name(model, "attention_mask"));+ EXPECT_TRUE(has_output_name(model, "present.0.encoder.key"));+ EXPECT_TRUE(has_output_name(model, "present.0.encoder.value"));+ EXPECT_TRUE(has_output_name(model, "present.1.encoder.key"));+ EXPECT_TRUE(has_output_name(model, "present.1.encoder.value"));+}++TEST_P(PrepareWhisperModelTest, KVCachePreparationHandlesFusedAndDecomposedCrossAttention) {+ const bool decompose = GetParam();++ auto model = stateless_whisper_decoder_model();+ if (decompose) {+ decompose_cross_attention_sdpa_for_test(model);+ }++ ASSERT_TRUE(ov::npuw::util::PrepareWhisperKVCacheModel().run_on_model(model));++ EXPECT_TRUE(has_input_name(model, "past_key_values.0.encoder.key"));+ EXPECT_TRUE(has_input_name(model, "past_key_values.0.encoder.value"));+ EXPECT_TRUE(has_input_name(model, "past_key_values.1.encoder.key"));+ EXPECT_TRUE(has_input_name(model, "past_key_values.1.encoder.value"));+}++INSTANTIATE_TEST_SUITE_P(CrossAttentionShape,+ PrepareWhisperModelTest,+ ::testing::Bool(),+ [](const ::testing::TestParamInfo<bool>& info) {+ return info.param ? "Decomposed" : "Fused";+ });++} // namespace