Skip to content

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

[NPUW] Add decompose SDPA passes for Whisper (#34665)

Ekaterina Shiryaevace7cfa3a8dda
diff --git a/src/plugins/intel_npu/src/al/include/intel_npu/config/npuw_option_defs.inc b/src/plugins/intel_npu/src/al/include/intel_npu/config/npuw_option_defs.inc
index da573e30e99..ce53df2652c 100644
--- a/src/plugins/intel_npu/src/al/include/intel_npu/config/npuw_option_defs.inc
+++ b/src/plugins/intel_npu/src/al/include/intel_npu/config/npuw_option_defs.inc
@@ -77,6 +77,7 @@ INTEL_NPU_NPUW_ANYMAP_OPT(NPUW_LLM_SHARED_LM_HEAD_CONFIG, ov::intel_npu::npuw::l
INTEL_NPU_NPUW_ANYMAP_OPT(NPUW_LLM_ADDITIONAL_SHARED_LM_HEAD_CONFIG, ov::intel_npu::npuw::llm, additional_shared_lm_head_config, "++NPUW_LLM_SHARED_HEAD_CONFIG", LLM, EXPOSED, CACHED, ALL)
INTEL_NPU_NPUW_SIMPLE_OPT(NPUW_WHISPER, bool, false, ov::intel_npu::npuw::whisper, enabled, "NPUW_WHISPER", LLM, EXPOSED, CACHED, ALL)
INTEL_NPU_NPUW_SIMPLE_OPT(NPUW_WHISPER_EOS_TOKEN, uint64_t, 50257, ov::intel_npu::npuw::whisper, whisper_eos_token, "NPUW_WHISPER_EOS_TOKEN", LLM, EXPOSED, UNCACHED, ALL)
+INTEL_NPU_NPUW_SIMPLE_OPT(NPUW_WHISPER_DECOMPOSE_SDPA, bool, false, ov::intel_npu::npuw::whisper, whisper_decompose_sdpa, "NPUW_WHISPER_DECOMPOSE_SDPA", LLM, EXPOSED, CACHED, ALL)
INTEL_NPU_NPUW_SIMPLE_OPT(NPUW_EAGLE, bool, false, ov::intel_npu::npuw::eagle, enabled, "NPUW_EAGLE", LLM, EXPOSED, CACHED, ALL)
INTEL_NPU_NPUW_SIMPLE_OPT(NPUW_TEXT_EMBED, bool, false, ov::intel_npu::npuw::text_embed, enabled, "NPUW_TEXT_EMBED", LLM, EXPOSED, CACHED, ALL)
INTEL_NPU_NPUW_SIMPLE_OPT(NPUW_KOKORO, bool, false, ov::intel_npu::npuw::kokoro, enabled, "NPUW_KOKORO", KOKORO, EXPOSED, UNCACHED, ALL)
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.cpp
index d819a653042..4aaf0067ef1 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
@@ -373,7 +373,7 @@ void merge_config_with(ov::AnyMap& lhs, const ov::AnyMap& rhs) {
void split_llm_properties(const ov::AnyMap& properties, ov::AnyMap& llm_properties, ov::AnyMap& other_properties) {
for (auto it = properties.begin(); it != properties.end(); ++it) {
- if (it->first.find("NPUW_LLM") != it->first.npos) {
+ if (it->first.find("NPUW_LLM") != it->first.npos || it->first.find("NPUW_WHISPER") != it->first.npos) {
llm_properties.insert(*it);
} else {
other_properties.insert(*it);
@@ -637,8 +637,6 @@ ov::npuw::LLMCompiledModel::LLMCompiledModel(const std::shared_ptr<ov::Model>& m
ov::AnyMap other_props;
split_llm_properties(properties, npuw_llm_props, other_props);
const auto npudesc = extract_npu_descriptor(plugin, other_props);
- auto use_whisper_key = pop_option(other_props, std::string("NPUW_WHISPER"));
- auto whisper_eos_token = pop_option(other_props, std::string("NPUW_WHISPER_EOS_TOKEN"));
auto use_eagle_key = pop_option(other_props, std::string("NPUW_EAGLE"));
// Remove map-valued section configs before m_cfg.update(any_copy(...)), since Config expects string options.
@@ -672,14 +670,14 @@ ov::npuw::LLMCompiledModel::LLMCompiledModel(const std::shared_ptr<ov::Model>& m
LOG_INFO("Set NPUW_ATTN_HFA_FUSED to YES");
}
- m_is_whisper = use_whisper_key.value_or(false).as<bool>() == true;
+ m_is_whisper = m_cfg.get<::intel_npu::NPUW_WHISPER>();
if (m_is_whisper) {
m_cfg.update({{"NPUW_LLM_SHARED_HEAD", "NO"}});
m_cfg.update({{"NPUW_LLM_PREFILL_CHUNK_SIZE", "0"}});
m_cfg.update({{"NPUW_LLM_CACHE_ROPE", "NO"}});
m_cfg.update({{"NPUW_LLM_OPTIMIZE_V_TENSORS", "NO"}});
- m_eos_token_id = whisper_eos_token.value_or(50257).as<uint64_t>();
+ m_eos_token_id = m_cfg.get<::intel_npu::NPUW_WHISPER_EOS_TOKEN>();
}
m_is_eagle = use_eagle_key.value_or(false).as<bool>() == true;
@@ -805,11 +803,32 @@ ov::npuw::LLMCompiledModel::LLMCompiledModel(const std::shared_ptr<ov::Model>& m
m_kvcache_desc = KVCacheDesc{whisper_max_prompt_size, whisper_kvcache_size, 0u, whisper_seq_len_dim, 1u};
whisper_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) {
+ m_kvcache_desc.max_prompt_size = whisper_kvcache_size - 1;
+ }
- ov::npuw::util::PrepareWhisperPrefillModel(m_kvcache_desc.max_prompt_size,
- whisper_lhs_seq_size)
- .run_on_model(prefill_model); // Whisper decoder model
+ auto prepare_prefill_model = ov::npuw::util::PrepareWhisperPrefillModel(m_kvcache_desc.max_prompt_size,
+ whisper_lhs_seq_size,
+ whisper_decompose_sdpa);
+ prepare_prefill_model.run_on_model(prefill_model); // Whisper decoder model
ov::npuw::util::PrepareWhisperKVCacheModel().run_on_model(kvcache_model); // Whisper decoder_with_past model
+
+ // FIXME: Whisper Decompose SDPA
+ // WA: to mock new "cross_attention_qk_scaled_scores" outputs in original model
+ if (whisper_decompose_sdpa) {
+ m_decomposed_sdpa_size = prepare_prefill_model.get_decomposed_sdpa_size();
+ auto& mutable_outputs = const_cast<std::vector<ov::Output<const ov::Node>>&>(this->outputs());
+ for (size_t idx = 0; idx < m_decomposed_sdpa_size; idx++) {
+ auto fake_param = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{});
+ auto fake_result = std::make_shared<ov::op::v0::Result>(fake_param);
+ fake_result->output(0).get_tensor().add_names(
+ {WhisperInferRequest::whisper_layer_names::qk_scores,
+ WhisperInferRequest::whisper_layer_names::qk_scores_ + std::to_string(idx)});
+
+ mutable_outputs.emplace_back(fake_result->output(0));
+ }
+ }
}
LOG_DEBUG("Make prefill model with static shapes");
@@ -1170,7 +1189,7 @@ void ov::npuw::LLMCompiledModel::serialize(std::ostream& raw_stream, const ov::n
m_kvcache_desc.dim & m_kvcache_desc.max_generation_token_len & m_kvcache_desc.v_tensors_transposed_pre &
m_kvcache_desc.v_tensors_transposed_gen & m_prefill_chunk_size & m_use_chunk_prefill & m_max_lora_rank &
m_enable_prefix_caching & m_prefix_caching_block_size & m_prefix_caching_max_num_blocks & m_is_whisper &
- m_eos_token_id & m_is_eagle & m_is_embedding;
+ m_eos_token_id & m_decomposed_sdpa_size & m_is_eagle & m_is_embedding;
// Write config
stream & m_cfg;
@@ -1386,7 +1405,8 @@ std::shared_ptr<ov::npuw::LLMCompiledModel> ov::npuw::LLMCompiledModel::deserial
compiled->m_kvcache_desc.v_tensors_transposed_gen & compiled->m_prefill_chunk_size &
compiled->m_use_chunk_prefill & compiled->m_max_lora_rank & compiled->m_enable_prefix_caching &
compiled->m_prefix_caching_block_size & compiled->m_prefix_caching_max_num_blocks & compiled->m_is_whisper &
- compiled->m_eos_token_id & compiled->m_is_eagle & compiled->m_is_embedding;
+ compiled->m_eos_token_id & compiled->m_decomposed_sdpa_size & compiled->m_is_eagle &
+ compiled->m_is_embedding;
// Deserialize config
stream & compiled->m_cfg;
@@ -1519,6 +1539,7 @@ void ov::npuw::LLMCompiledModel::implement_properties() {
BIND(npuw::llm::shared_lm_head, NPUW_LLM_SHARED_HEAD, get),
BIND(npuw::whisper::enabled, NPUW_WHISPER, get),
BIND(npuw::whisper::whisper_eos_token, NPUW_WHISPER_EOS_TOKEN, get),
+ BIND(npuw::whisper::whisper_decompose_sdpa, NPUW_WHISPER_DECOMPOSE_SDPA, get),
BIND(npuw::eagle::enabled, NPUW_EAGLE, get),
BIND(npuw::text_embed::enabled, NPUW_TEXT_EMBED, get)});
#undef BIND
diff --git a/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.hpp b/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.hpp
index b1c13601e3b..2a91ee3cfc4 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.hpp
@@ -124,6 +124,7 @@ private:
bool m_is_whisper = false;
uint64_t m_eos_token_id = 0;
+ size_t m_decomposed_sdpa_size = 0;
bool m_is_embedding = false;
diff --git a/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/reshape_to_static.cpp b/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/reshape_to_static.cpp
index 3491d8b2258..a88738efc0b 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/reshape_to_static.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/reshape_to_static.cpp
@@ -33,7 +33,7 @@ void reshape_to_static(std::shared_ptr<ov::Model> model,
new_shape = ov::PartialShape({1, input_size, input.get_partial_shape()[2]});
} else if (input_name.find("attention_mask") != std::string::npos) {
new_shape = ov::PartialShape({1, kvcache_size});
- if (lhs_seq_size && kvcache_size > 4)
+ if (lhs_seq_size && !is_prefill)
// NB: for whisper kvcache model attn mask should be size + 1
new_shape = ov::PartialShape({1, kvcache_size + 1});
} else if (input_name.find("position_ids") != std::string::npos) {
diff --git a/src/plugins/intel_npu/src/plugin/npuw/serialization.hpp b/src/plugins/intel_npu/src/plugin/npuw/serialization.hpp
index 0ffbc79c0ca..731c6c3361d 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/serialization.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/serialization.hpp
@@ -43,7 +43,7 @@ const constexpr ov::npuw::s11n::IndicatorType NPUW_COMPILED_MODEL_INDICATOR =
const constexpr ov::npuw::s11n::IndicatorType NPUW_LLM_COMPILED_MODEL_INDICATOR =
{char{0x4c}, char{0x4c}, char{0x4d}, char{0x43}, char{0x4d}, char{0x4f}};
-const constexpr char* NPUW_SERIALIZATION_VERSION = "0.22";
+const constexpr char* NPUW_SERIALIZATION_VERSION = "0.23";
// Forward declaration
namespace intel_npu {
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.cpp
index 35bdc392a74..abc41c2bae1 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
@@ -8,9 +8,11 @@
#include "../llm_compiled_model_utils.hpp"
#include "openvino/op/ops.hpp"
+#include "openvino/op/scaled_dot_product_attention.hpp"
#include "openvino/openvino.hpp"
#include "openvino/opsets/opset13.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/or.hpp"
@@ -227,6 +229,193 @@ public:
}
};
+bool can_move_scale_after_matmul(const ov::Output<ov::Node>& query,
+ const ov::Output<ov::Node>& kT,
+ const ov::Output<ov::Node>& scale) {
+ const auto& scale_pshape = scale.get_partial_shape();
+ const auto& query_pshape = query.get_partial_shape();
+ if (scale_pshape.is_dynamic() || query_pshape.is_dynamic()) {
+ return false;
+ }
+
+ // According to the ov SDPA specification, the scale input have to be 1d with 1 element
+ // or scalar.
+ if (ov::shape_size(scale_pshape.to_shape()) != 1) {
+ return false;
+ }
+
+ // using the original implementation to calculate the shapes.
+ // we need to move the scale after MatMul only if the tensor after MatMul is smaller.
+ auto q_scaled = std::make_shared<ov::op::v1::Multiply>(query, scale);
+ auto scaled_attn = std::make_shared<ov::op::v0::MatMul>(q_scaled, kT);
+ const auto& scaled_attn_pshape = scaled_attn->output(0).get_partial_shape();
+ if (scaled_attn_pshape.is_static()) {
+ return ov::shape_size(query_pshape.to_shape()) > ov::shape_size(scaled_attn_pshape.to_shape());
+ }
+ return false;
+}
+
+// FIXME: Whisper Decompose SDPA
+class WhisperScaledDotProductAttentionDecomposition : public ov::pass::MatcherPass {
+public:
+ OPENVINO_MATCHER_PASS_RTTI("WhisperScaledDotProductAttentionDecomposition");
+ WhisperScaledDotProductAttentionDecomposition() {
+ auto pattern_node = ov::pass::pattern::wrap_type<ov::op::v13::ScaledDotProductAttention>();
+
+ ov::matcher_pass_callback callback = [this, pattern_node](ov::pass::pattern::Matcher& m) {
+ auto& pattern_to_output = m.get_pattern_value_map();
+
+ auto node = ov::as_type_ptr<ov::op::v13::ScaledDotProductAttention>(
+ pattern_to_output.at(pattern_node).get_node_shared_ptr());
+
+ if (node == nullptr || transformation_callback(node)) {
+ return false;
+ }
+
+ const std::string& node_name = node->get_friendly_name();
+ if (node_name.find("encoder_attn") == std::string::npos) {
+ // This pass is only for encoder-decoder cross-attention layers
+ return false;
+ }
+
+ auto new_output_node = decompose(node);
+ ov::replace_node(node, new_output_node);
+ return true;
+ };
+
+ auto m = std::make_shared<ov::pass::pattern::Matcher>(pattern_node,
+ "WhisperScaledDotProductAttentionDecompositionMatcher");
+ register_matcher(m, callback);
+ }
+
+ std::shared_ptr<ov::Node> decompose(std::shared_ptr<ov::op::v13::ScaledDotProductAttention> node) {
+ using namespace ov::op;
+ using namespace ov;
+ auto query = node->input_value(0);
+ auto key = node->input_value(1);
+ auto value = node->input_value(2);
+ auto q_shape = register_new_node<v3::ShapeOf>(query, element::i32);
+ auto k_shape = register_new_node<v3::ShapeOf>(key, element::i32);
+ auto minus_one = register_new_node(v0::Constant::create(element::i32, Shape{}, {-1}));
+ auto minus_two = register_new_node(v0::Constant::create(element::i32, Shape{}, {-2}));
+ auto zero_i = register_new_node(v0::Constant::create(element::i32, Shape{}, {0}));
+ auto one_i = register_new_node(v0::Constant::create(element::i32, Shape{}, {1}));
+ auto one_f = register_new_node<v1::ConvertLike>(one_i, query);
+ auto zero_f = register_new_node<v1::ConvertLike>(zero_i, query);
+
+ auto build_extract_dim_subgraph = [this, &zero_i](const std::shared_ptr<v3::ShapeOf>& shape_of,
+ const int64_t idx) -> std::shared_ptr<ov::Node> {
+ const auto dim_to_extract_const = v0::Constant::create(element::i32, Shape{}, {idx});
+ const auto gather = std::make_shared<v8::Gather>(shape_of, dim_to_extract_const, zero_i);
+
+ register_new_node(dim_to_extract_const);
+ return register_new_node(gather);
+ };
+
+ Output<Node> scale;
+ Output<Node> sink;
+ bool has_sink = false;
+ if (node->get_input_size() < 5) {
+ scale = build_extract_dim_subgraph(q_shape, -1);
+ scale = register_new_node<v1::ConvertLike>(scale, query);
+ auto sqrt_scale = register_new_node<v0::Sqrt>(scale);
+ scale = register_new_node<v1::Divide>(one_f, sqrt_scale);
+ } else {
+ scale = node->input_value(4);
+ if (node->get_input_size() == 6) {
+ sink = node->input_value(5);
+ has_sink = true;
+ }
+ }
+
+ auto k_rank = register_new_node<v3::ShapeOf>(k_shape, element::i32)->output(0);
+ auto k_last_dim = register_new_node<v1::Add>(k_rank, minus_one);
+ auto k_next_dim = register_new_node<v1::Add>(k_rank, minus_two)->output(0);
+ k_rank = register_new_node<v0::Squeeze>(k_rank, zero_i);
+ auto minus_inf =
+ register_new_node(v0::Constant::create(element::f32, Shape{}, {-std::numeric_limits<float>::infinity()}))
+ ->output(0);
+ auto keep_dim_last = register_new_node<v0::Squeeze>(k_next_dim, zero_i);
+ auto k_dims_before_transpose = register_new_node<v4::Range>(zero_i, keep_dim_last, one_i, element::i32);
+
+ auto transpose_dims =
+ register_new_node<v0::Concat>(OutputVector{k_dims_before_transpose, k_last_dim, k_next_dim}, 0);
+ auto k_transposed = register_new_node<v1::Transpose>(key, transpose_dims);
+
+ ov::Output<Node> scaled_atten;
+ if (can_move_scale_after_matmul(query, k_transposed, scale)) {
+ auto atten = register_new_node<v0::MatMul>(query, k_transposed)->output(0);
+ scaled_atten = register_new_node<v1::Multiply>(atten, scale)->output(0);
+ } else {
+ auto q_scaled = register_new_node<v1::Multiply>(query, scale);
+ scaled_atten = register_new_node<v0::MatMul>(q_scaled, k_transposed)->output(0);
+ }
+
+ minus_inf = register_new_node<v1::ConvertLike>(minus_inf, scaled_atten);
+
+ if (node->get_causal() || node->get_input_size() > 3) {
+ Output<Node> mask;
+ Output<Node> atten_mask;
+ if (!node->get_causal()) {
+ mask = node->input_value(3);
+
+ // two types of masks are supported. A boolean mask where a value of True indicates that the element
+ // should take part in attention. A float mask of the same type as query, key, value that is added to
+ // the attention score.
+ if (mask.get_element_type() == element::boolean) {
+ atten_mask = register_new_node<v1::Select>(mask, zero_f, minus_inf);
+ } else {
+ atten_mask = mask;
+ }
+ } else {
+ auto target_s_len = build_extract_dim_subgraph(q_shape, -2);
+ auto source_s_len = build_extract_dim_subgraph(k_shape, -2);
+ auto ssl = register_new_node<v0::Unsqueeze>(source_s_len, zero_i);
+ auto tsl = register_new_node<v0::Unsqueeze>(target_s_len, zero_i);
+ auto mask_shape = register_new_node<v0::Concat>(OutputVector{tsl, ssl}, 0);
+ mask = register_new_node<v1::Broadcast>(minus_inf, mask_shape);
+ auto horizontal_range =
+ register_new_node<v4::Range>(zero_i, source_s_len, one_i, element::i32)->output(0);
+ horizontal_range = register_new_node<v0::Unsqueeze>(horizontal_range, zero_i);
+ auto stop = register_new_node<v1::Add>(target_s_len, one_i);
+ auto vertical_range = register_new_node<v4::Range>(one_i, stop, one_i, element::i32)->output(0);
+ vertical_range = register_new_node<v0::Unsqueeze>(vertical_range, one_i);
+ auto triu = register_new_node<v1::GreaterEqual>(horizontal_range, vertical_range);
+ atten_mask = register_new_node<v1::Select>(triu, mask, zero_f);
+ }
+ scaled_atten = register_new_node<v1::Add>(scaled_atten, atten_mask);
+ }
+
+ scaled_atten.add_names({"cross_attention_qk_scaled_scores"});
+
+ if (has_sink) {
+ auto minus_two = register_new_node(v0::Constant::create(element::i32, Shape{1}, {-2}));
+ auto minus_one = register_new_node(v0::Constant::create(element::i32, Shape{1}, {-1}));
+ auto zero_i = register_new_node(v0::Constant::create(element::i32, Shape{1}, {0}));
+ auto one_i = register_new_node(v0::Constant::create(element::i32, Shape{1}, {1}));
+
+ auto q_last_but_one_dim = register_new_node<v1::Subtract>(register_new_node<v0::ShapeOf>(q_shape),
+ v0::Constant::create(element::i64, Shape{}, {1}));
+ auto sink_target_shape_1 = register_new_node<v8::Slice>(q_shape, zero_i, q_last_but_one_dim, one_i);
+ auto sink_target_shape = register_new_node<v0::Concat>(OutputVector{sink_target_shape_1, one_i}, 0);
+ auto sink_broadcast = register_new_node<v1::Broadcast>(sink, sink_target_shape);
+
+ auto scaled_attn_sink = register_new_node<v0::Concat>(OutputVector{scaled_atten, sink_broadcast}, -1);
+ scaled_atten = register_new_node<v8::Softmax>(scaled_attn_sink, -1);
+
+ auto prev_seq_len = register_new_node<v8::Gather>(k_shape, minus_two, zero_i);
+ scaled_atten = register_new_node<v8::Slice>(scaled_atten, zero_i, prev_seq_len, one_i, minus_one);
+ } else {
+ scaled_atten = register_new_node<v8::Softmax>(scaled_atten, -1);
+ }
+
+ auto result = register_new_node<v0::MatMul>(scaled_atten, value);
+ result->set_friendly_name(node->get_friendly_name());
+ copy_runtime_info(node, get_new_nodes());
+ return result;
+ }
+};
+
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) {
@@ -470,6 +659,48 @@ void add_cache_position_input(const std::shared_ptr<ov::Model>& model) {
ov::pass::Validate().run_on_model(model);
}
+// FIXME: Whisper Decompose SDPA
+void decompose_scaled_dot_product_attention_for_whisper(std::shared_ptr<ov::Model> model) {
+ ov::pass::Manager manager;
+ manager.register_pass<WhisperScaledDotProductAttentionDecomposition>();
+ manager.run_passes(model);
+}
+
+// FIXME: Whisper Decompose SDPA
+size_t add_cross_attention_qk_scaled_scores_outputs_for_whisper(std::shared_ptr<ov::Model> model) {
+ size_t idx = 0;
+ for (auto& op : model->get_ordered_ops()) {
+ if (op->get_type_info().name != std::string("Add")) {
+ continue;
+ }
+
+ bool should_skip_op = true;
+
+ for (const auto& output : op->outputs()) {
+ for (const auto& name : output.get_names()) {
+ if (name.find("cross_attention_qk_scaled_scores") != std::string::npos) {
+ should_skip_op = false;
+ break;
+ }
+ }
+
+ // output found, exit outputs loop
+ if (!should_skip_op) {
+ break;
+ }
+ }
+
+ if (should_skip_op) {
+ continue;
+ }
+
+ model->add_output(op->output(0)).set_names({"cross_attention_qk_scaled_scores_" + std::to_string(idx)});
+ idx++;
+ }
+
+ return idx;
+}
+
#ifdef __GNUC__
# pragma GCC diagnostic pop
#endif
@@ -490,6 +721,12 @@ bool ov::npuw::util::PrepareWhisperPrefillModel::run_on_model(const std::shared_
add_attention_mask_input(model, m_max_prompt_size, m_lhs_seq_size, true);
+ // FIXME: Whisper Decompose SDPA
+ if (m_decompose_sdpa) {
+ decompose_scaled_dot_product_attention_for_whisper(model);
+ m_decomposed_layers_size = add_cross_attention_qk_scaled_scores_outputs_for_whisper(model);
+ }
+
model->validate_nodes_and_infer_types();
return true;
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.hpp
index 69265be42d4..9a9bafb765a 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
@@ -16,14 +16,23 @@ namespace util {
class PrepareWhisperPrefillModel : public ov::pass::ModelPass {
uint32_t m_max_prompt_size;
uint32_t m_lhs_seq_size;
+ bool m_decompose_sdpa;
+ size_t m_decomposed_layers_size;
public:
OPENVINO_MODEL_PASS_RTTI("ov::npuw::PrepareWhisperPrefillModel");
- explicit PrepareWhisperPrefillModel(uint32_t max_prompt_size, uint32_t lhs_seq_size)
+ explicit PrepareWhisperPrefillModel(uint32_t max_prompt_size, uint32_t lhs_seq_size, bool decompose_sdpa)
: m_max_prompt_size(max_prompt_size),
- m_lhs_seq_size(lhs_seq_size) {}
+ m_lhs_seq_size(lhs_seq_size),
+ m_decompose_sdpa(decompose_sdpa),
+ m_decomposed_layers_size(0) {}
bool run_on_model(const std::shared_ptr<ov::Model>& model) override;
+
+ // FIXME: Whisper Decompose SDPA
+ size_t get_decomposed_sdpa_size() const {
+ return m_decomposed_layers_size;
+ }
};
class PrepareWhisperKVCacheModel : public ov::pass::ModelPass {
diff --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.cpp
index cc11d571560..0044d1cd488 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
@@ -22,6 +22,8 @@ void ov::npuw::WhisperInferRequest::prepare_for_new_conversation() {
ov::npuw::util::fill_tensor<int64_t>(m_prefill_request->get_tensor(m_prefill_in_ports.at("attention_mask")), 0);
ov::npuw::util::fill_tensor<int64_t>(m_kvcache_request->get_tensor(m_kvcache_in_ports.at("attention_mask")), 0);
m_npuw_llm_compiled_model->m_kvcache_desc.num_stored_tokens = 0u;
+
+ m_alignment_tensors.clear();
}
void ov::npuw::WhisperInferRequest::infer_prefill(ov::SoPtr<ov::ITensor> input_ids,
@@ -56,6 +58,13 @@ void ov::npuw::WhisperInferRequest::infer_prefill(ov::SoPtr<ov::ITensor> input_i
0u,
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))});
+ }
+
LOG_DEBUG("Done");
}
@@ -210,3 +219,23 @@ void ov::npuw::WhisperInferRequest::infer() {
infer_generate(input_ids);
}
}
+
+// FIXME: Whisper Decompose SDPA
+ov::SoPtr<ov::ITensor> ov::npuw::WhisperInferRequest::get_tensor(const ov::Output<const ov::Node>& port) const {
+ const auto& port_names = port.get_names();
+
+ if (port_names.count(whisper_layer_names::qk_scores) > 0) {
+ for (auto name : port_names) {
+ if (name.find(whisper_layer_names::qk_scores_) != std::string::npos) {
+ auto alignment_tensor = m_alignment_tensors.at(name);
+ if (!alignment_tensor) {
+ OPENVINO_THROW(
+ "Cross-attention qk scaled scores tensor is not available. Please run inference first.");
+ }
+ return alignment_tensor;
+ }
+ }
+ }
+
+ return ov::npuw::LLMInferRequest::get_tensor(port);
+}
diff --git a/src/plugins/intel_npu/src/plugin/npuw/whisper/whisper_infer_request.hpp b/src/plugins/intel_npu/src/plugin/npuw/whisper/whisper_infer_request.hpp
index 766a1af6caa..9479aaa2802 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/whisper/whisper_infer_request.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/whisper/whisper_infer_request.hpp
@@ -11,11 +11,18 @@ namespace npuw {
class WhisperInferRequest final : public LLMInferRequest {
public:
+ struct whisper_layer_names {
+ static constexpr const char* qk_scores = "cross_attention_qk_scaled_scores";
+ static constexpr const char* qk_scores_ = "cross_attention_qk_scaled_scores_";
+ };
+
explicit WhisperInferRequest(const std::shared_ptr<LLMCompiledModel>& compiled_model)
: LLMInferRequest(compiled_model) {}
void infer() override;
+ ov::SoPtr<ov::ITensor> get_tensor(const ov::Output<const ov::Node>& port) const override;
+
protected:
void prepare_for_new_conversation() override;
@@ -24,6 +31,7 @@ protected:
void infer_generate(ov::SoPtr<ov::ITensor> input_ids);
bool m_need_copy_kvcache = false;
+ std::map<std::string, ov::SoPtr<ov::ITensor>> m_alignment_tensors{};
};
} // namespace npuw
diff --git a/src/plugins/intel_npu/tests/unit/npuw/llm_compiled_model_factory_options_test.cpp b/src/plugins/intel_npu/tests/unit/npuw/llm_compiled_model_factory_options_test.cpp
index 22581aa28cb..b9a5b46ffb3 100644
--- a/src/plugins/intel_npu/tests/unit/npuw/llm_compiled_model_factory_options_test.cpp
+++ b/src/plugins/intel_npu/tests/unit/npuw/llm_compiled_model_factory_options_test.cpp
@@ -545,7 +545,8 @@ TEST_F(LLMCompiledModelFactoryOptionsTest, WhisperPrefillPreparationAddsCrossAtt
model = model->clone();
EXPECT_TRUE(ov::npuw::util::PrepareWhisperPrefillModel(
- 128, static_cast<uint32_t>(ov::test::npuw::WhisperConfig{}.max_source_positions))
+ 128, static_cast<uint32_t>(ov::test::npuw::WhisperConfig{}.max_source_positions),
+ false /*decompose_sdpa*/)
.run_on_model(model));
auto prepared = model;