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] Moved whisper model transformations to NPUW (#31643)
Ekaterina Shiryaeva231ae69602b6
diff --git a/src/plugins/intel_npu/src/al/include/intel_npu/config/npuw.hpp b/src/plugins/intel_npu/src/al/include/intel_npu/config/npuw.hppindex 4c393209b99..5659011b850 100644--- a/src/plugins/intel_npu/src/al/include/intel_npu/config/npuw.hpp+++ b/src/plugins/intel_npu/src/al/include/intel_npu/config/npuw.hpp@@ -148,6 +148,7 @@ DEFINE_OPT(NPUW_LLM_CACHE_ROPE, bool, true, npuw::llm::cache_rope, RunTime);DEFINE_OPT(NPUW_LLM_PREFILL_CHUNK_SIZE, uint64_t, 1024, npuw::llm::prefill_chunk_size, RunTime);DEFINE_OPT(NPUW_LLM_SHARED_HEAD, bool, true, npuw::llm::shared_lm_head, RunTime);DEFINE_OPT(NPUW_LLM_MAX_LORA_RANK, uint32_t, 32, npuw::llm::max_lora_rank, RunTime);+DEFINE_OPT(NPUW_WHISPER, bool, false, npuw::whisper::enabled, RunTime);DEFINE_ANYMAP_OPT(NPUW_LLM_PREFILL_CONFIG, npuw::llm::prefill_config);DEFINE_ANYMAP_OPT(NPUW_LLM_ADDITIONAL_PREFILL_CONFIG, npuw::llm::additional_prefill_config);DEFINE_ANYMAP_OPT(NPUW_LLM_GENERATE_CONFIG, npuw::llm::generate_config);diff --git a/src/plugins/intel_npu/src/al/include/intel_npu/npuw_private_properties.hpp b/src/plugins/intel_npu/src/al/include/intel_npu/npuw_private_properties.hppindex b4ae012f4a5..4ffb00b9442 100644--- a/src/plugins/intel_npu/src/al/include/intel_npu/npuw_private_properties.hpp+++ b/src/plugins/intel_npu/src/al/include/intel_npu/npuw_private_properties.hpp@@ -607,6 +607,16 @@ static constexpr ov::Property<ov::AnyMap> shared_lm_head_config{"NPUW_LLM_SHAREDstatic constexpr ov::Property<ov::AnyMap> additional_shared_lm_head_config{"++NPUW_LLM_SHARED_HEAD_CONFIG"};} // namespace llm+namespace whisper {+/**+ * @brief+ * Type: bool.+ * Tell NPUW that you want to pass Whisper model.+ * Default value: false.+ */+static constexpr ov::Property<bool> enabled{"NPUW_WHISPER"};+} // namespace whisper+} // namespace npuw} // namespace intel_npu} // namespace ovdiff --git a/src/plugins/intel_npu/src/al/src/config/npuw.cpp b/src/plugins/intel_npu/src/al/src/config/npuw.cppindex 238f7687141..8ed46b977c4 100644--- a/src/plugins/intel_npu/src/al/src/config/npuw.cpp+++ b/src/plugins/intel_npu/src/al/src/config/npuw.cpp@@ -75,6 +75,7 @@ void intel_npu::registerNPUWLLMOptions(OptionsDesc& desc) {desc.add<NPUW_LLM_PREFILL_ATTENTION_HINT>();desc.add<NPUW_LLM_GENERATE_ATTENTION_HINT>();desc.add<NPUW_LLM_SHARED_HEAD>();+ desc.add<NPUW_WHISPER>();}std::string ov::npuw::s11n::anyToString(const ov::Any& var) {diff --git a/src/plugins/intel_npu/src/plugin/npuw/infer_request_utils.cpp b/src/plugins/intel_npu/src/plugin/npuw/infer_request_utils.cppnew file mode 100644index 00000000000..82fe5cae911--- /dev/null+++ b/src/plugins/intel_npu/src/plugin/npuw/infer_request_utils.cpp@@ -0,0 +1,152 @@+// Copyright (C) 2025 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "infer_request_utils.hpp"++#include "logging.hpp"+#include "openvino/runtime/make_tensor.hpp" // get_tensor_impl+#include "util_xarch.hpp"++// FIXME: Use ov::npuw::util::view instead+ov::SoPtr<ov::ITensor> ov::npuw::util::make_tensor_slice(ov::SoPtr<ov::ITensor> tensor,+ uint32_t dim,+ uint32_t start_pos,+ uint32_t end_pos) {+ ov::Shape start_shape(std::vector<size_t>(tensor->get_shape().size(), 0u));+ start_shape[dim] = start_pos;+ ov::Shape end_shape = tensor->get_shape();+ end_shape[dim] = end_pos;+ return ov::get_tensor_impl(ov::Tensor(ov::make_tensor(tensor), start_shape, end_shape));+}++void ov::npuw::util::copy_by_planes(ov::SoPtr<ov::ITensor> src_tensor, ov::SoPtr<ov::ITensor> dst_tensor) {+ // [1, H, S1, E] -> [1, H, S2, E]+ const int N = 0;+ const int H = 1;+ const int S = 2;+ const int E = 3;++ OPENVINO_ASSERT(src_tensor->get_shape()[N] == dst_tensor->get_shape()[N]);+ OPENVINO_ASSERT(src_tensor->get_shape()[H] == dst_tensor->get_shape()[H]);+ OPENVINO_ASSERT(src_tensor->get_shape()[E] == dst_tensor->get_shape()[E]);+ OPENVINO_ASSERT(src_tensor->get_element_type() == dst_tensor->get_element_type());+ OPENVINO_ASSERT(src_tensor->get_shape()[N] == 1u);+ OPENVINO_ASSERT(src_tensor->get_shape().size() == 4u);++ const auto* src_tensor_data = reinterpret_cast<uint8_t*>(src_tensor->data());+ auto* dst_tensor_data = reinterpret_cast<uint8_t*>(dst_tensor->data());++ const auto num_planes = src_tensor->get_shape()[H];+ const auto src_plane_stride = src_tensor->get_strides()[H];+ const auto dst_plane_stride = dst_tensor->get_strides()[H];+ const auto plane_size_in_bytes = src_tensor->get_strides()[S] * src_tensor->get_shape()[S];++ for (size_t i = 0; i < num_planes; ++i) {+ std::copy_n(src_tensor_data, plane_size_in_bytes, dst_tensor_data);+ dst_tensor_data += dst_plane_stride;+ src_tensor_data += src_plane_stride;+ }+}++void ov::npuw::util::copy_columns_by_row_chunks(ov::SoPtr<ov::ITensor> src, ov::SoPtr<ov::ITensor>& dst) {+ /*+ src/dst layout: [1, heads, emb_size, seq_len]++ X[*,i] - embedding for i-th token,+ Instead of copy columns, copy rows X[i,*]++ [[X00 X01 ... X0n] [[X00 X01 ... X0n]+ [X10 X11 ... X1n] [X10 X11 ... X1n]+ [X20 X21 ... X2n] ... [X20 X21 ... X2n]+ ... ...+ [Xm0 Xm1 ... Xmn]] [Xm0 Xm1 ... Xmn]]+ */++ const auto& src_shape = src->get_shape();++ OPENVINO_ASSERT(src_shape.size() == 4u);+ OPENVINO_ASSERT(src_shape == dst->get_shape());+ OPENVINO_ASSERT(src->get_byte_size() == dst->get_byte_size());++ const auto& src_strides = src->get_strides();+ const auto& dst_strides = dst->get_strides();+ const auto elem_size = src->get_byte_size() / src->get_size();++ const auto C = src_shape[1];+ const auto H = src_shape[2];+ const auto W = src_shape[3];++ const auto IS_H = src_strides[2];+ const auto OS_H = dst_strides[2];++ const size_t chunk_byte_size = W * elem_size;++ const auto* src_p = static_cast<uint8_t*>(src->data());+ auto* dst_p = static_cast<uint8_t*>(dst->data());++ for (size_t i = 0; i < C * H; ++i) {+ const size_t src_offset = i * IS_H;+ const size_t dst_offset = i * OS_H;+ std::copy_n(src_p + src_offset, chunk_byte_size, dst_p + dst_offset);+ }+}++void ov::npuw::util::copy_tensor_by_dim(ov::SoPtr<ov::ITensor> src_tensor,+ ov::SoPtr<ov::ITensor> dst_tensor,+ uint32_t kv_dim_src,+ uint32_t kv_dim_dst) {+ if (kv_dim_src != kv_dim_dst) {+ // new case - do a generic copy for now (in fact it is a permute)+ // Example:+ // kv_dim_src kv_dim_dst+ // v v+ // [1,8,256,128] --> [1,8,128,256]+ const auto& src_shape = src_tensor->get_shape();+ const auto& dst_shape = dst_tensor->get_shape();+ NPUW_ASSERT(src_shape.size() == 4);+ NPUW_ASSERT(dst_shape.size() == 4);+ NPUW_ASSERT(kv_dim_src < 4);+ NPUW_ASSERT(kv_dim_dst < 4);+ NPUW_ASSERT(src_shape[kv_dim_src] == dst_shape[kv_dim_dst]);++ std::array<int, 4> axis = {0, 1, 2, 3};+ // Remap like 0,1,2,3 => 0,1,3,2 (see example)+ std::swap(axis[kv_dim_src], axis[kv_dim_dst]);+ ov::npuw::util::permute_i4d(src_tensor, dst_tensor, axis);+ return;+ }+ // Old behavior+ NPUW_ASSERT(kv_dim_src == kv_dim_dst);+ if (kv_dim_src == 3u) {+ // Asserting that we work with last dimenston here:+ const auto& src_shape = src_tensor->get_shape();+ OPENVINO_ASSERT(src_shape.size() == 4);+ // If last dimenstion of src_tensor is equal to 1, then we can squeeze+ // src_shape from [1, heads, d_v, seq_len=1] to [heads, d_v].+ // We can then treat src_tensor as a continuous tensor of row value vectors+ // for multiple heads, while dst_tensor will still have [1, heads, d_v, seq_len!=1],+ // shape, awaiting updates at column dimension, as value vectors are columns now.+ if (src_shape[kv_dim_src] == 1 && src_tensor->is_continuous()) {+ ov::npuw::util::XARCH::copy_row_as_column(src_tensor, dst_tensor);+ } else {+ copy_columns_by_row_chunks(src_tensor, dst_tensor);+ }+ } else if (kv_dim_src == 2u) {+ copy_by_planes(src_tensor, dst_tensor);+ } else {+ src_tensor->copy_to(dst_tensor._ptr);+ }+}++std::optional<ov::Output<const ov::Node>> ov::npuw::util::find_port_by_name(+ const std::vector<ov::Output<const ov::Node>>& ports,+ const std::string& name) {+ auto it = std::find_if(ports.begin(), ports.end(), [&](const auto& port) {+ return port.get_names().count(name) != 0;+ });+ if (it == ports.end()) {+ return std::nullopt;+ }+ return std::make_optional(*it);+}diff --git a/src/plugins/intel_npu/src/plugin/npuw/infer_request_utils.hpp b/src/plugins/intel_npu/src/plugin/npuw/infer_request_utils.hppnew file mode 100644index 00000000000..022d49b56a1--- /dev/null+++ b/src/plugins/intel_npu/src/plugin/npuw/infer_request_utils.hpp@@ -0,0 +1,39 @@+// Copyright (C) 2025 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#pragma once++#include <optional>+#include <string>++#include "llm_compiled_model_utils.hpp"+#include "logging.hpp"+#include "openvino/op/constant.hpp"+#include "openvino/runtime/itensor.hpp"+#include "openvino/runtime/so_ptr.hpp"++namespace ov {+namespace npuw {+namespace util {++ov::SoPtr<ov::ITensor> make_tensor_slice(ov::SoPtr<ov::ITensor> tensor,+ uint32_t dim,+ uint32_t start_pos,+ uint32_t end_pos);++void copy_by_planes(ov::SoPtr<ov::ITensor> src_tensor, ov::SoPtr<ov::ITensor> dst_tensor);++void copy_columns_by_row_chunks(ov::SoPtr<ov::ITensor> src, ov::SoPtr<ov::ITensor>& dst);++void copy_tensor_by_dim(ov::SoPtr<ov::ITensor> src_tensor,+ ov::SoPtr<ov::ITensor> dst_tensor,+ uint32_t kv_dim_src,+ uint32_t kv_dim_dst);++std::optional<ov::Output<const ov::Node>> find_port_by_name(const std::vector<ov::Output<const ov::Node>>& ports,+ const std::string& name);++} // namespace util+} // namespace npuw+} // namespace ovdiff --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 331ebc9403c..8e1c30137d2 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@@ -5,8 +5,11 @@#include "llm_infer_request.hpp"#include "logging.hpp"+#include "openvino/op/convert.hpp"+#include "openvino/op/greater.hpp"#include "openvino/op/group_query_attention.hpp"#include "openvino/op/ops.hpp"+#include "openvino/op/range.hpp"#include "openvino/op/util/node_util.hpp"#include "openvino/openvino.hpp"#include "openvino/opsets/opset13.hpp"@@ -24,6 +27,7 @@#include "serialization.hpp"#include "transformations/convert_precision.hpp"#include "util.hpp"+#include "whisper_infer_request.hpp"namespace opp = ov::pass::pattern;@@ -550,7 +554,8 @@ void reshape_to_static(std::shared_ptr<ov::Model> model,const uint32_t input_size,const uint32_t kvcache_size,const KVAxesPosition& kv_axes_position,- const uint32_t lora_rank) {+ const uint32_t lora_rank,+ const uint32_t lhs_seq_size = 0) {std::map<std::string, ov::PartialShape> new_shapes;for (const auto& input : model->inputs()) {const auto& input_name = input.get_any_name();@@ -566,6 +571,9 @@ 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)+ // 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) {const auto partial_shape_size = input.get_partial_shape().size();// NB: Regular LLM uses 2D shapes, Qwen2.5 VL/Omni uses 3D shapes@@ -574,6 +582,14 @@ void reshape_to_static(std::shared_ptr<ov::Model> model,NPUW_ASSERT(partial_shape_size == 3u || partial_shape_size == 2u);new_shape =partial_shape_size == 3u ? ov::PartialShape({3, 1, input_size}) : ov::PartialShape({1, input_size});+ } else if (input_name.find("cache_position") != std::string::npos) {+ // NB: Whisper case+ new_shape = ov::PartialShape({1});+ } else if (input_name.find("encoder_hidden_states") != std::string::npos) {+ // NB: Whisper case+ const auto& partial_shape = input.get_partial_shape();+ new_shape = partial_shape;+ new_shape[0] = 1; // batch_dim} else if (ov::npuw::util::matchLoRAMatMulAString(input_name)) {new_shape = ov::PartialShape({lora_rank, input.get_partial_shape()[1]});} else if (ov::npuw::util::matchLoRAMatMulAlphaString(input_name)) {@@ -584,7 +600,13 @@ void reshape_to_static(std::shared_ptr<ov::Model> model,const auto& partial_shape = input.get_partial_shape();new_shape = partial_shape;new_shape[kv_axes_position.batch] = 1;- new_shape[kv_axes_position.seq_len] = kvcache_size - input_size;+ if (lhs_seq_size) { // Whisper model+ new_shape[kv_axes_position.seq_len] = (input_name.find(".decoder") != std::string::npos)+ ? kvcache_size - input_size // kv_size for decoder+ : lhs_seq_size; // sequence size for encoder hidden states+ } else { // LLM/VLM+ new_shape[kv_axes_position.seq_len] = kvcache_size - input_size;+ }}new_shapes.emplace(input_name, new_shape);}@@ -838,6 +860,10 @@ void refine_dynamic_props(ov::AnyMap& llm_properties, const std::optional<NPUDes}}+void update_config_for_whisper(ov::AnyMap& config) {+ config.erase("NPUW_SLICE_OUT");+}+std::map<std::string, std::string> any_copy(const ov::AnyMap& params) {std::map<std::string, std::string> result;for (auto&& value : params) {@@ -942,6 +968,7 @@ ov::npuw::LLMCompiledModel::LLMCompiledModel(const std::shared_ptr<ov::Model>& mov::AnyMap npuw_llm_props;ov::AnyMap other_props;split_llm_properties(properties, npuw_llm_props, other_props);+ auto use_whisper_key = pop_option(other_props, std::string("NPUW_WHISPER"));// Solely used for serialization at the momentm_non_llm_props = other_props;@@ -958,6 +985,13 @@ ov::npuw::LLMCompiledModel::LLMCompiledModel(const std::shared_ptr<ov::Model>& mrefine_dynamic_props(npuw_llm_props, npudesc);m_cfg.update(any_copy(npuw_llm_props));+ m_is_whisper = use_whisper_key.value_or(false).as<bool>() == true;+ 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"}});+ }+LOG_DEBUG("Creating kvcache model as clone of passed one.");auto kvcache_model = model->clone();LOG_DEBUG("Transform kvcache model from stateful to stateless.");@@ -1029,6 +1063,19 @@ ov::npuw::LLMCompiledModel::LLMCompiledModel(const std::shared_ptr<ov::Model>& mm_kvcache_desc =KVCacheDesc{max_prompt_len, max_prompt_len + min_response_len, 0u, seq_len_dim, max_generation_token_len};+ uint32_t whisper_lhs_seq_size = 0; // Not applicable for LLMs/VLMs+ if (m_is_whisper) {+ axes = KVAxesPosition{whisper_batch_dim, whisper_seq_len_dim};+ 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());++ ov::npuw::util::prepare_whisper_prefill_model(prefill_model,+ m_kvcache_desc.max_prompt_size,+ whisper_lhs_seq_size); // Whisper decoder model+ ov::npuw::util::prepare_whisper_kvcache_model(kvcache_model); // Whisper decoder_with_past model+ }+LOG_DEBUG("Make prefill model with static shapes");m_max_lora_rank = m_cfg.get<::intel_npu::NPUW_LLM_MAX_LORA_RANK>();if (m_use_chunk_prefill) {@@ -1042,14 +1089,16 @@ ov::npuw::LLMCompiledModel::LLMCompiledModel(const std::shared_ptr<ov::Model>& mm_kvcache_desc.max_prompt_size,m_kvcache_desc.max_prompt_size,axes,- m_max_lora_rank);+ m_max_lora_rank,+ whisper_lhs_seq_size);}LOG_DEBUG("Make kvcache model with static shapes");reshape_to_static(kvcache_model,m_kvcache_desc.max_generation_token_len,m_kvcache_desc.total_size,axes,- m_max_lora_rank);+ m_max_lora_rank,+ whisper_lhs_seq_size);gemma_transformations(kvcache_model);if (lm_head_model) {@@ -1156,6 +1205,10 @@ ov::npuw::LLMCompiledModel::LLMCompiledModel(const std::shared_ptr<ov::Model>& mmerge_config_with(generate_config, no_runtime_fallback);}+ if (m_is_whisper) {+ update_config_for_whisper(prefill_config);+ }+if (m_cfg.get<::intel_npu::NPUW_LLM_CACHE_ROPE>()) {LOG_DEBUG("Caching preROPE ");const uint32_t CACHE_ROPE_START = 2048;@@ -1311,6 +1364,7 @@ void ov::npuw::LLMCompiledModel::serialize(std::ostream& stream, const ov::npuw:write(model_stream, m_use_chunk_prefill);write(model_stream, m_max_lora_rank);write(model_stream, m_gemma_sliding_window_size);+ write(model_stream, m_is_whisper);// Write configwrite(model_stream, m_cfg);@@ -1522,6 +1576,7 @@ std::shared_ptr<ov::npuw::LLMCompiledModel> ov::npuw::LLMCompiledModel::deserialread(model_stream, compiled->m_use_chunk_prefill);read(model_stream, compiled->m_max_lora_rank);read(model_stream, compiled->m_gemma_sliding_window_size);+ read(model_stream, compiled->m_is_whisper);// Deserialize configread(model_stream, compiled->m_cfg);@@ -1582,7 +1637,7 @@ ov::Any ov::npuw::LLMCompiledModel::get_property(const std::string& name) conststd::shared_ptr<ov::ISyncInferRequest> ov::npuw::LLMCompiledModel::create_sync_infer_request() const {auto* non_const_this = const_cast<ov::npuw::LLMCompiledModel*>(this); // because of const in API- return non_const_this->create_llm_infer_request();+ return m_is_whisper ? non_const_this->create_whisper_infer_request() : non_const_this->create_llm_infer_request();}std::shared_ptr<ov::ISyncInferRequest> ov::npuw::LLMCompiledModel::create_llm_infer_request() {@@ -1590,6 +1645,11 @@ std::shared_ptr<ov::ISyncInferRequest> ov::npuw::LLMCompiledModel::create_llm_inreturn std::make_shared<ov::npuw::LLMInferRequest>(this_sptr);}+std::shared_ptr<ov::ISyncInferRequest> ov::npuw::LLMCompiledModel::create_whisper_infer_request() {+ auto this_sptr = std::static_pointer_cast<ov::npuw::LLMCompiledModel>(shared_from_this());+ return std::make_shared<ov::npuw::WhisperInferRequest>(this_sptr);+}+void ov::npuw::LLMCompiledModel::implement_properties() {#define BIND(N, T, GETTER) \{ \@@ -1611,6 +1671,7 @@ void ov::npuw::LLMCompiledModel::implement_properties() {BIND(npuw::llm::generate_hint, NPUW_LLM_GENERATE_HINT, getString),BIND(npuw::llm::prefill_attn_hint, NPUW_LLM_PREFILL_ATTENTION_HINT, getString),BIND(npuw::llm::generate_attn_hint, NPUW_LLM_GENERATE_ATTENTION_HINT, getString),- BIND(npuw::llm::shared_lm_head, NPUW_LLM_SHARED_HEAD, get)});+ BIND(npuw::llm::shared_lm_head, NPUW_LLM_SHARED_HEAD, get),+ BIND(npuw::whisper::enabled, NPUW_WHISPER, 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.hppindex f486bb6f251..c831f0919ae 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@@ -12,6 +12,7 @@ namespace ov {namespace npuw {class LLMInferRequest;+class WhisperInferRequest;class LLMCompiledModel : public ov::npuw::ICompiledModel {using GetPropertiesMap =std::map<std::string, std::tuple<ov::PropertyMutability, std::function<ov::Any(const ::intel_npu::Config&)>>>;@@ -19,6 +20,11 @@ class LLMCompiledModel : public ov::npuw::ICompiledModel {public:static constexpr const char* output_embeds = "npuw_output_embed";+ static constexpr uint32_t whisper_batch_dim = 0u;+ static constexpr uint32_t whisper_seq_len_dim = 2u;+ static constexpr uint32_t whisper_max_prompt_size = 4u;+ static constexpr uint32_t whisper_kvcache_size = 448u;+struct KVCacheDesc {uint32_t max_prompt_size = 0u;uint32_t total_size = 0u;@@ -49,8 +55,10 @@ public:private:friend class LLMInferRequest;+ friend class WhisperInferRequest;std::shared_ptr<ov::ISyncInferRequest> create_llm_infer_request();+ std::shared_ptr<ov::ISyncInferRequest> create_whisper_infer_request();std::shared_ptr<ov::ISyncInferRequest> create_sync_infer_request() const override;void implement_properties();@@ -83,6 +91,8 @@ private:void gemma_transformations(const std::shared_ptr<ov::Model>& model);int32_t m_gemma_sliding_window_size = 0;++ bool m_is_whisper = false;};} // namespace npuwdiff --git a/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model_utils.cpp b/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model_utils.cppindex 2e203fff65d..2f367d6ebb9 100644--- a/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model_utils.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model_utils.cpp@@ -4,6 +4,8 @@#include "llm_compiled_model_utils.hpp"+#include <regex>+#include "logging.hpp"#include "openvino/op/ops.hpp"#include "openvino/openvino.hpp"@@ -291,14 +293,170 @@ public:}};+class AttentionMaskInputPast : public ov::pass::MatcherPass {+public:+ OPENVINO_MATCHER_PASS_RTTI("npuw::LLMCompiledModel::AttentionMaskInputPast");++ AttentionMaskInputPast(std::shared_ptr<ov::Model> model) {+ auto range = opp::wrap_type<ov::op::v4::Range>();+ auto convert1 = opp::wrap_type<ov::op::v0::Convert>({range});+ auto greater = opp::wrap_type<ov::op::v1::Greater>({convert1, opp::any_input()});+ auto convert2 = opp::wrap_type<ov::op::v0::Convert>({greater});++ register_matcher(std::make_shared<opp::Matcher>(convert2, this->get_type_info().name),+ [model](opp::Matcher& m) {+ auto node = m.get_match_root();+ auto attention_mask =+ std::make_shared<ov::op::v0::Parameter>(ov::element::i64, ov::PartialShape{-1, -1});+ attention_mask->get_output_tensor(0).set_names({"attention_mask"});+ model->add_parameters({attention_mask});++ auto cvt =+ std::make_shared<ov::op::v0::Convert>(attention_mask->output(0), ov::element::f32);+ ov::replace_node(node, cvt);+ return false;+ });+ }+};++class AttentionMaskInputPast_2 : public ov::pass::MatcherPass {+public:+ OPENVINO_MATCHER_PASS_RTTI("npuw::LLMCompiledModel::AttentionMaskInputPast_2");++ AttentionMaskInputPast_2(std::shared_ptr<ov::Model> model) {+ auto range = opp::wrap_type<ov::op::v4::Range>();+ auto unsqueeze1 = opp::wrap_type<ov::op::v0::Unsqueeze>({range, opp::any_input()});+ auto unsqueeze2 = opp::wrap_type<ov::op::v0::Unsqueeze>({unsqueeze1, opp::any_input()});+ auto unsqueeze3 = opp::wrap_type<ov::op::v0::Unsqueeze>({unsqueeze2, opp::any_input()});+ auto lessequal = opp::wrap_type<ov::op::v1::LessEqual>({unsqueeze3, opp::any_input()});++ register_matcher(+ std::make_shared<opp::Matcher>(lessequal, this->get_type_info().name),+ [model](opp::Matcher& m) {+ auto node = m.get_match_root();+ auto attention_mask =+ std::make_shared<ov::op::v0::Parameter>(ov::element::i64, ov::PartialShape{1, -1});+ attention_mask->get_output_tensor(0).set_names({"attention_mask"});+ model->add_parameters({attention_mask});++ auto cst_0 = std::make_shared<ov::op::v0::Constant>(ov::element::i64, ov::Shape{1}, 0);+ auto cst_1 = std::make_shared<ov::op::v0::Constant>(ov::element::i64, ov::Shape{1}, 1);+ auto cst_2 = std::make_shared<ov::op::v0::Constant>(ov::element::i64, ov::Shape{1}, 2);++ auto attn_mask_shape =+ std::make_shared<ov::op::v3::ShapeOf>(attention_mask, ov::element::i64)->output(0);+ auto gather = std::make_shared<ov::op::v8::Gather>(attn_mask_shape, cst_1, cst_0)->output(0);+ auto attn_mask_size_minus_one = std::make_shared<ov::op::v1::Subtract>(gather, cst_1)->output(0);+ auto slice = std::make_shared<ov::op::v8::Slice>(attention_mask->output(0),+ cst_0,+ attn_mask_size_minus_one,+ cst_1,+ cst_1);++ auto unsqueeze_1 = std::make_shared<ov::op::v0::Unsqueeze>(slice->output(0), cst_1->output(0));+ auto unsqueeze_2 = std::make_shared<ov::op::v0::Unsqueeze>(unsqueeze_1->output(0), cst_2->output(0));++ auto equal = std::make_shared<ov::op::v1::Equal>(unsqueeze_2->output(0), cst_0->output(0));++ ov::replace_node(node, equal);+ return false;+ });+ }+};++class AttentionMaskInput : public ov::pass::MatcherPass {+public:+ OPENVINO_MATCHER_PASS_RTTI("npuw::LLMCompiledModel::AttentionMaskInput");++ AttentionMaskInput(std::shared_ptr<ov::Model> model,+ const uint32_t& max_prompt_len,+ const uint32_t& lhs_seq_size,+ bool transform_cross_attn) {+ std::vector<std::shared_ptr<ov::Node>> self_attn_nodes;+ std::vector<std::shared_ptr<ov::Node>> cross_attn_nodes;+ const auto kAttnMaskPort = 3;+ for (auto node : model->get_ops()) {+ if (ov::is_type<ov::op::v13::ScaledDotProductAttention>(node)) {+ if (node->inputs().size() > kAttnMaskPort &&+ ov::is_type<ov::op::v8::Slice>(node->input(kAttnMaskPort).get_source_output().get_node())) {+ self_attn_nodes.push_back(node);+ } else {+ cross_attn_nodes.push_back(node);+ }+ }+ }++ // Self-attention+ OPENVINO_ASSERT(!self_attn_nodes.empty());++ auto attention_mask = std::make_shared<ov::op::v0::Parameter>(ov::element::i64, ov::PartialShape{-1, -1});+ attention_mask->get_output_tensor(0).set_names({"attention_mask"});+ model->add_parameters({attention_mask});++ auto slice = self_attn_nodes[0]->input(kAttnMaskPort).get_source_output().get_node();+ auto cvt = std::make_shared<ov::op::v0::Convert>(attention_mask->output(0), ov::element::f32);+ auto add = std::make_shared<ov::op::v1::Add>(slice->output(0), cvt->output(0));++ auto trps = std::make_shared<ov::op::v1::Transpose>(+ cvt->output(0),+ ov::op::v0::Constant::create(ov::element::i32, ov::Shape{2}, std::vector<int>{1, 0}));+ auto mtpl = std::make_shared<ov::op::v1::Multiply>(trps->output(0), add->output(0));++ auto cst_ninf = std::make_shared<ov::op::v0::Constant>(ov::element::f32,+ ov::Shape{1},+ std::vector<float>{-std::numeric_limits<float>::max()});+ auto cst_1 = std::make_shared<ov::op::v0::Constant>(ov::element::f32, ov::Shape{1}, std::vector<float>{1});+ auto cst_0 = std::make_shared<ov::op::v0::Constant>(ov::element::f32, ov::Shape{1}, std::vector<float>{0});++ auto equal = std::make_shared<ov::op::v1::Equal>(mtpl->output(0), cst_1->output(0));+ auto select = std::make_shared<ov::op::v1::Select>(equal->output(0), cst_0->output(0), cst_ninf->output(0));++ for (auto self_attn : self_attn_nodes) {+ self_attn->input(3).replace_source_output(select->output(0));+ }++ if (transform_cross_attn) {+ // Cross attn+ OPENVINO_ASSERT(!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,+ ov::Shape{2},+ std::vector<float>{static_cast<float>(max_prompt_len), 1});+ auto target_shape = std::make_shared<ov::op::v0::Constant>(+ ov::element::i64,+ ov::Shape{2},+ std::vector<float>{static_cast<float>(max_prompt_len), static_cast<float>(lhs_seq_size)});+ // FIXME: Must be transpose if batch present+ auto reshape = std::make_shared<ov::op::v1::Reshape>(cvt->output(0), shape_cst->output(0), false);+ auto equal = std::make_shared<ov::op::v1::Equal>(reshape->output(0), cst_1->output(0));+ auto select = std::make_shared<ov::op::v1::Select>(equal->output(0), cst_0->output(0), cst_ninf->output(0));+ auto broadcast = std::make_shared<ov::op::v3::Broadcast>(select->output(0), target_shape->output(0));+ auto unsq1 = std::make_shared<ov::op::v0::Unsqueeze>(broadcast->output(0), cst_0->output(0));+ auto unsq2 = std::make_shared<ov::op::v0::Unsqueeze>(unsq1->output(0), cst_1->output(0));+ for (auto cross_attn_node : cross_attn_nodes) {+ if (cross_attn_node->inputs().size() == 3) {+ auto sdpa = std::make_shared<ov::op::v13::ScaledDotProductAttention>(+ cross_attn_node->input(0).get_source_output(),+ cross_attn_node->input(1).get_source_output(),+ cross_attn_node->input(2).get_source_output(),+ unsq2->output(0),+ false);+ ov::replace_node(cross_attn_node, sdpa);+ } else {+ cross_attn_node->input(3).replace_source_output(unsq2->output(0));+ }+ }+ }+ }+};} // namespace#ifdef __GNUC__# pragma GCC diagnostic pop#endif-namespace ov::npuw::util {-bool optimize_value_tensors(std::shared_ptr<ov::Model> model, bool isPrefill) {+bool ov::npuw::util::optimize_value_tensors(std::shared_ptr<ov::Model> model, bool isPrefill) {ov::pass::GraphRewrite rewr;rewr.add_matcher<ScaledDotProductAttentionDecomposition>(isPrefill);TransposeValueTensors::Context ctx;@@ -311,4 +469,272 @@ bool optimize_value_tensors(std::shared_ptr<ov::Model> model, bool isPrefill) {// NB: matmul parameters gets transposed, if pass appliedreturn ctx.bTransposed;}-} // namespace ov::npuw::util++namespace {+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+ 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));+}++std::string transform_key_value_name(std::string input_string,+ std::string prefix,+ std::string enc_or_dec,+ std::string key_or_value) {+ std::regex pattern("[0-9]+");+ std::smatch match;+ std::regex_search(input_string, match, pattern);++ if (match.empty())+ OPENVINO_THROW("Input string does not match the expected pattern");++ auto number = std::string(match[0]);+ return prefix + "." + number + enc_or_dec + key_or_value;+}++void set_name(std::shared_ptr<ov::Node> result, const std::string& name) {+ result->set_friendly_name(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(std::shared_ptr<ov::Model>& model) {+ // Find all ReadValue nodes+ ov::NodeVector read_value_nodes;+ for (const auto& op : model->get_ops()) {+ if (strstr(op->get_type_name(), "ReadValue") != nullptr) {+ read_value_nodes.push_back(op);+ }+ }++ // Holds result layers for cross-attn KV-cache tensors+ ov::ResultVector results;+ ov::SinkVector assigns;++ // Go through all ReadValue nodes and remove them+ for (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);+ }+ }+ }++ // Add, remove, validate+ model->add_results(results);+ for (const auto& assign : assigns) {+ model->remove_sink(assign);+ }+ model->validate_nodes_and_infer_types();+}++void remove_cache_position(std::shared_ptr<ov::Model>& model) {+ // Build subgraph that will replace cache_pos+ auto input_ids = model->input("input_ids").get_node();+ auto shape_of_node = std::make_shared<ov::op::v3::ShapeOf>(input_ids->outputs()[0]);++ std::vector<int> v_0{0};+ std::vector<int> v_1{1};++ auto indices = std::make_shared<ov::op::v0::Constant>(ov::element::i64, ov::Shape{}, v_1);+ indices->set_friendly_name("indices");+ auto axis = std::make_shared<ov::op::v0::Constant>(ov::element::i64, ov::Shape{}, v_0);+ axis->set_friendly_name("axis");++ auto gather_node = std::make_shared<ov::op::v8::Gather>(shape_of_node->outputs()[0], indices, axis);++ auto cst_node = std::make_shared<ov::op::v0::Constant>(ov::element::i64, ov::Shape{}, v_0);+ auto step = std::make_shared<ov::op::v0::Constant>(ov::element::i64, ov::Shape{}, v_1);+ step->set_friendly_name("step");+ auto range_node = std::make_shared<ov::op::v4::Range>(cst_node->outputs()[0],+ gather_node->outputs()[0],+ step->outputs()[0],+ ov::element::i64);+ // Replace cache_position+ auto cache_pos =+ ov::as_type_ptr<ov::op::v0::Parameter>(model->input("cache_position").get_node()->shared_from_this());+ for (const auto& target_input : cache_pos->outputs()[0].get_target_inputs()) {+ target_input.replace_source_output(range_node->outputs()[0]);+ }++ model->remove_parameter(cache_pos);+ model->validate_nodes_and_infer_types();+}++void expose_runtime_states_as_inputs(std::shared_ptr<ov::Model>& model) {+ // Store Assign nodes to perform remove_sink later on+ ov::SinkVector assigns;+ // To add new Params to the model+ ov::ParameterVector params;++ ov::NodeVector read_value_nodes;+ for (const auto& op : model->get_ops()) {+ if (strstr(op->get_type_name(), "ReadValue") != nullptr) {+ read_value_nodes.push_back(op);+ }+ }++ 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]);+ }+ }+ }++ // Remove sinks and add new params+ model->add_parameters(params);+ for (const auto& assign : assigns) {+ model->remove_sink(assign);+ }+}++void normalize_input_key_value_names(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) {+ continue;+ }++ auto key_or_value = (in.get_any_name().find(".key") != std::string::npos) ? "key" : "value";+ auto normalized_name =+ transform_key_value_name(in.get_any_name(), "past_key_values", ".decoder.", key_or_value);+ set_name(in.get_node_shared_ptr(), normalized_name);+ }++ model->validate_nodes_and_infer_types();+}++void normalize_output_key_value_names(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) {+ continue;+ }++ auto key_or_value = (out.get_any_name().find(".key") != std::string::npos) ? "key" : "value";+ auto normalized_name = transform_key_value_name(out.get_any_name(), "present", ".decoder.", key_or_value);+ set_name(out.get_node_shared_ptr(), normalized_name);+ }++ model->validate_nodes_and_infer_types();+}++void add_attention_mask_input(std::shared_ptr<ov::Model> model,+ const uint32_t& max_prompt_size = 0,+ const uint32_t& lhs_seq_size = 0,+ bool transform_cross_attn = false) {+ ov::pass::GraphRewrite rewr;+ if (transform_cross_attn) {+ rewr.add_matcher<AttentionMaskInput>(model, max_prompt_size, lhs_seq_size, transform_cross_attn);+ } else {+ rewr.add_matcher<AttentionMaskInputPast>(model);+ rewr.add_matcher<AttentionMaskInputPast_2>(model); // transformers>=4.53+ }++ rewr.run_on_model(model);++ ov::pass::Validate().run_on_model(model);+}+} // namespace++std::shared_ptr<ov::Model> ov::npuw::util::prepare_whisper_prefill_model(std::shared_ptr<ov::Model>& model,+ const uint32_t& max_prompt_size,+ const uint32_t& lhs_seq_size) {+ // 2) Remove all non-runtime states from inputs (they empty on first iteration)+ // remove_input_kv_tensors(model); -> Done for LLM also+ // 3) Expose all states that requires initialization on the first run as outputs+ expose_runtime_states_as_outputs(model);+ // 4) Remove cache_position input+ remove_cache_position(model);+ // 5) Normalize output names - should be done in stateful_to_stateless_transformation+ normalize_output_key_value_names(model);++ add_attention_mask_input(model, max_prompt_size, lhs_seq_size, true);++ model->validate_nodes_and_infer_types();+ return model;+}++std::shared_ptr<ov::Model> ov::npuw::util::prepare_whisper_kvcache_model(std::shared_ptr<ov::Model>& model) {+ // FIXME: normalization should be done inside stateful_to_stateless_transformation+ normalize_input_key_value_names(model);+ normalize_output_key_value_names(model);+ expose_runtime_states_as_inputs(model);++ add_attention_mask_input(model);++ model->reshape({{"input_ids", ov::PartialShape({-1, 1})}});++ model->validate_nodes_and_infer_types();+ return model;+}diff --git a/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model_utils.hpp b/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model_utils.hppindex 2407a9a1f2c..df26684e142 100644--- a/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model_utils.hpp+++ b/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model_utils.hpp@@ -26,6 +26,12 @@ public:// SDPA-unroll and transpose transformationsbool optimize_value_tensors(std::shared_ptr<ov::Model> model, bool isPrefill);+std::shared_ptr<ov::Model> prepare_whisper_prefill_model(std::shared_ptr<ov::Model>& model,+ const uint32_t& max_prompt_size,+ const uint32_t& lhs_seq_size);++std::shared_ptr<ov::Model> prepare_whisper_kvcache_model(std::shared_ptr<ov::Model>& model);+// clang-format off} // namespace ov// clang-format ondiff --git a/src/plugins/intel_npu/src/plugin/npuw/llm_infer_request.cpp b/src/plugins/intel_npu/src/plugin/npuw/llm_infer_request.cppindex abaedf17c57..6cc6d68a03e 100644--- a/src/plugins/intel_npu/src/plugin/npuw/llm_infer_request.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/llm_infer_request.cpp@@ -6,156 +6,15 @@#include <regex>+#include "infer_request_utils.hpp"#include "llm_compiled_model.hpp"#include "logging.hpp"#include "openvino/core/parallel.hpp"#include "openvino/runtime/iasync_infer_request.hpp"-#include "util_xarch.hpp"+#include "util.hpp"namespace {-// FIXME: Use ov::npuw::util::view instead-ov::SoPtr<ov::ITensor> make_tensor_slice(ov::SoPtr<ov::ITensor> tensor,- uint32_t dim,- uint32_t start_pos,- uint32_t end_pos) {- ov::Shape start_shape(std::vector<size_t>(tensor->get_shape().size(), 0u));- start_shape[dim] = start_pos;- ov::Shape end_shape = tensor->get_shape();- end_shape[dim] = end_pos;- return ov::get_tensor_impl(ov::Tensor(ov::make_tensor(tensor), start_shape, end_shape));-}--void copy_by_planes(ov::SoPtr<ov::ITensor> src_tensor, ov::SoPtr<ov::ITensor> dst_tensor) {- // [1, H, S1, E] -> [1, H, S2, E]- const int N = 0;- const int H = 1;- const int S = 2;- const int E = 3;-- OPENVINO_ASSERT(src_tensor->get_shape()[N] == dst_tensor->get_shape()[N]);- OPENVINO_ASSERT(src_tensor->get_shape()[H] == dst_tensor->get_shape()[H]);- OPENVINO_ASSERT(src_tensor->get_shape()[E] == dst_tensor->get_shape()[E]);- OPENVINO_ASSERT(src_tensor->get_element_type() == dst_tensor->get_element_type());- OPENVINO_ASSERT(src_tensor->get_shape()[N] == 1u);- OPENVINO_ASSERT(src_tensor->get_shape().size() == 4u);-- const auto* src_tensor_data = reinterpret_cast<uint8_t*>(src_tensor->data());- auto* dst_tensor_data = reinterpret_cast<uint8_t*>(dst_tensor->data());-- const auto num_planes = src_tensor->get_shape()[H];- const auto src_plane_stride = src_tensor->get_strides()[H];- const auto dst_plane_stride = dst_tensor->get_strides()[H];- const auto plane_size_in_bytes = src_tensor->get_strides()[S] * src_tensor->get_shape()[S];-- for (size_t i = 0; i < num_planes; ++i) {- std::copy_n(src_tensor_data, plane_size_in_bytes, dst_tensor_data);- dst_tensor_data += dst_plane_stride;- src_tensor_data += src_plane_stride;- }-}--void copy_columns_by_row_chunks(ov::SoPtr<ov::ITensor> src, ov::SoPtr<ov::ITensor>& dst) {- /*- src/dst layout: [1, heads, emb_size, seq_len]-- X[*,i] - embedding for i-th token,- Instead of copy columns, copy rows X[i,*]-- [[X00 X01 ... X0n] [[X00 X01 ... X0n]- [X10 X11 ... X1n] [X10 X11 ... X1n]- [X20 X21 ... X2n] ... [X20 X21 ... X2n]- ... ...- [Xm0 Xm1 ... Xmn]] [Xm0 Xm1 ... Xmn]]- */-- const auto& src_shape = src->get_shape();-- OPENVINO_ASSERT(src_shape.size() == 4u);- OPENVINO_ASSERT(src_shape == dst->get_shape());- OPENVINO_ASSERT(src->get_byte_size() == dst->get_byte_size());-- const auto& src_strides = src->get_strides();- const auto& dst_strides = dst->get_strides();- const auto elem_size = src->get_byte_size() / src->get_size();-- const auto C = src_shape[1];- const auto H = src_shape[2];- const auto W = src_shape[3];-- const auto IS_H = src_strides[2];- const auto OS_H = dst_strides[2];-- const size_t chunk_byte_size = W * elem_size;-- const auto* src_p = static_cast<uint8_t*>(src->data());- auto* dst_p = static_cast<uint8_t*>(dst->data());-- for (size_t i = 0; i < C * H; ++i) {- const size_t src_offset = i * IS_H;- const size_t dst_offset = i * OS_H;- std::copy_n(src_p + src_offset, chunk_byte_size, dst_p + dst_offset);- }-}--void copy_tensor_by_dim(ov::SoPtr<ov::ITensor> src_tensor,- ov::SoPtr<ov::ITensor> dst_tensor,- uint32_t kv_dim_src,- uint32_t kv_dim_dst) {- if (kv_dim_src != kv_dim_dst) {- // new case - do a generic copy for now (in fact it is a permute)- // Example:- // kv_dim_src kv_dim_dst- // v v- // [1,8,256,128] --> [1,8,128,256]- const auto& src_shape = src_tensor->get_shape();- const auto& dst_shape = dst_tensor->get_shape();- NPUW_ASSERT(src_shape.size() == 4);- NPUW_ASSERT(dst_shape.size() == 4);- NPUW_ASSERT(kv_dim_src < 4);- NPUW_ASSERT(kv_dim_dst < 4);- NPUW_ASSERT(src_shape[kv_dim_src] == dst_shape[kv_dim_dst]);-- std::array<int, 4> axis = {0, 1, 2, 3};- // Remap like 0,1,2,3 => 0,1,3,2 (see example)- std::swap(axis[kv_dim_src], axis[kv_dim_dst]);- ov::npuw::util::permute_i4d(src_tensor, dst_tensor, axis);- return;- }- // Old behavior- NPUW_ASSERT(kv_dim_src == kv_dim_dst);- if (kv_dim_src == 3u) {- // Asserting that we work with last dimenston here:- const auto& src_shape = src_tensor->get_shape();- OPENVINO_ASSERT(src_shape.size() == 4);- // If last dimenstion of src_tensor is equal to 1, then we can squeeze- // src_shape from [1, heads, d_v, seq_len=1] to [heads, d_v].- // We can then treat src_tensor as a continuous tensor of row value vectors- // for multiple heads, while dst_tensor will still have [1, heads, d_v, seq_len!=1],- // shape, awaiting updates at column dimension, as value vectors are columns now.- if (src_shape[kv_dim_src] == 1 && src_tensor->is_continuous()) {- ov::npuw::util::XARCH::copy_row_as_column(src_tensor, dst_tensor);- } else {- copy_columns_by_row_chunks(src_tensor, dst_tensor);- }- } else if (kv_dim_src == 2u) {- copy_by_planes(src_tensor, dst_tensor);- } else {- src_tensor->copy_to(dst_tensor._ptr);- }-}--std::optional<ov::Output<const ov::Node>> find_port_by_name(const std::vector<ov::Output<const ov::Node>>& ports,- const std::string& name) {- auto it = std::find_if(ports.begin(), ports.end(), [&](const auto& port) {- return port.get_names().count(name) != 0;- });- if (it == ports.end()) {- return std::nullopt;- }- return std::make_optional(*it);-}-void copy_columns_by_row_chunks_2d(ov::SoPtr<ov::ITensor> src, ov::SoPtr<ov::ITensor>& dst) {const auto& src_shape = src->get_shape();@@ -312,12 +171,14 @@ ov::npuw::LLMInferRequest::LLMInferRequest(const std::shared_ptr<ov::npuw::LLMCoinit_tensor(output_port);}- auto input_ids_port = find_port_by_name(compiled_model->m_prefill_compiled->inputs(), layer_names::input_ids);+ auto input_ids_port =+ ov::npuw::util::find_port_by_name(compiled_model->m_prefill_compiled->inputs(), layer_names::input_ids);if (input_ids_port.has_value()) {m_input_ids_name = layer_names::input_ids;} else {OPENVINO_ASSERT(- find_port_by_name(compiled_model->m_prefill_compiled->inputs(), layer_names::inputs_embeds).has_value());+ ov::npuw::util::find_port_by_name(compiled_model->m_prefill_compiled->inputs(), layer_names::inputs_embeds)+ .has_value());m_input_ids_name = layer_names::inputs_embeds;}@@ -491,7 +352,8 @@ void ov::npuw::LLMInferRequest::apply_lora() {return;}- auto new_tensor_slice = make_tensor_slice(infer_tensor, low_rank_dim, 0u, state_tensor_rank);+ auto new_tensor_slice =+ ov::npuw::util::make_tensor_slice(infer_tensor, low_rank_dim, 0u, state_tensor_rank);if (low_rank_dim == 1) {copy_columns_by_row_chunks_2d(state_tensor, new_tensor_slice);} else {@@ -523,6 +385,7 @@ void ov::npuw::LLMInferRequest::prepare_for_new_conversation() {}void ov::npuw::LLMInferRequest::copy_kvcache() {+ namespace uu = ov::npuw::util;LOG_DEBUG("Copying kv-cache from prefill to generate model.");LOG_BLOCK();auto& kvcache_desc = m_npuw_llm_compiled_model->m_kvcache_desc;@@ -564,38 +427,43 @@ void ov::npuw::LLMInferRequest::copy_kvcache() {auto tokens_in_past_chunks = kvcache_desc.num_stored_tokens - tokens_in_present_chunk;if (tokens_in_past_chunks > 0) {auto prefill_past_kv = m_prefill_request->get_tensor(m_prefill_in_ports.at(input_name));- auto prefill_past_kv_chunks =- make_tensor_slice(prefill_past_kv, pre_kv_dim, 0u, static_cast<uint32_t>(tokens_in_past_chunks));+ auto prefill_past_kv_chunks = uu::make_tensor_slice(prefill_past_kv,+ pre_kv_dim,+ 0u,+ static_cast<uint32_t>(tokens_in_past_chunks));- auto kvcache_past_kv_chunks =- make_tensor_slice(kvcache_in_tensor, gen_kv_dim, 0u, static_cast<uint32_t>(tokens_in_past_chunks));+ auto kvcache_past_kv_chunks = uu::make_tensor_slice(kvcache_in_tensor,+ gen_kv_dim,+ 0u,+ static_cast<uint32_t>(tokens_in_past_chunks));- copy_tensor_by_dim(prefill_past_kv_chunks, kvcache_past_kv_chunks, pre_kv_dim, gen_kv_dim);+ uu::copy_tensor_by_dim(prefill_past_kv_chunks, kvcache_past_kv_chunks, pre_kv_dim, gen_kv_dim);}// Copy part 2 KV resultsauto prefill_present_kv_chunk =- make_tensor_slice(prefill_out_tensor,- pre_kv_dim,- static_cast<uint32_t>(prefill_chunk_size - tokens_in_present_chunk),- static_cast<uint32_t>(prefill_chunk_size));+ uu::make_tensor_slice(prefill_out_tensor,+ pre_kv_dim,+ static_cast<uint32_t>(prefill_chunk_size - tokens_in_present_chunk),+ static_cast<uint32_t>(prefill_chunk_size));- auto kvcache_last_kv_chunk = make_tensor_slice(kvcache_in_tensor,- gen_kv_dim,- static_cast<uint32_t>(tokens_in_past_chunks),- kvcache_desc.num_stored_tokens);+ auto kvcache_last_kv_chunk = uu::make_tensor_slice(kvcache_in_tensor,+ gen_kv_dim,+ static_cast<uint32_t>(tokens_in_past_chunks),+ kvcache_desc.num_stored_tokens);- copy_tensor_by_dim(prefill_present_kv_chunk, kvcache_last_kv_chunk, pre_kv_dim, gen_kv_dim);+ uu::copy_tensor_by_dim(prefill_present_kv_chunk, kvcache_last_kv_chunk, pre_kv_dim, gen_kv_dim);} else {- auto prefill_out_slice = make_tensor_slice(prefill_out_tensor,- pre_kv_dim,- kvcache_desc.max_prompt_size - kvcache_desc.num_stored_tokens,- kvcache_desc.max_prompt_size);+ auto prefill_out_slice =+ uu::make_tensor_slice(prefill_out_tensor,+ pre_kv_dim,+ kvcache_desc.max_prompt_size - kvcache_desc.num_stored_tokens,+ kvcache_desc.max_prompt_size);auto kvcache_in_slice =- make_tensor_slice(kvcache_in_tensor, gen_kv_dim, 0u, kvcache_desc.num_stored_tokens);+ uu::make_tensor_slice(kvcache_in_tensor, gen_kv_dim, 0u, kvcache_desc.num_stored_tokens);- copy_tensor_by_dim(prefill_out_slice, kvcache_in_slice, pre_kv_dim, gen_kv_dim);+ uu::copy_tensor_by_dim(prefill_out_slice, kvcache_in_slice, pre_kv_dim, gen_kv_dim);}});LOG_DEBUG("Done.");@@ -607,6 +475,7 @@ void ov::npuw::LLMInferRequest::update_kvcache_for(const std::unordered_map<std::string, ov::Output<const ov::Node>>& out_ports,uint32_t num_tokens,bool v_transposed) {+ namespace uu = ov::npuw::util;LOG_DEBUG("Store computed key and values for passed number of tokens in the input kv-cache"" layers.");LOG_BLOCK();@@ -623,10 +492,10 @@ void ov::npuw::LLMInferRequest::update_kvcache_for(}auto dst_tensor = request->get_tensor(in_ports.at(input_name));const auto& kv_dim = (output_name.find("value") != std::string::npos && v_transposed) ? 3u : kvcache_desc.dim;- auto dst_slice = make_tensor_slice(dst_tensor,- kv_dim,- kvcache_desc.num_stored_tokens - num_tokens,- kvcache_desc.num_stored_tokens);+ auto dst_slice = uu::make_tensor_slice(dst_tensor,+ kv_dim,+ kvcache_desc.num_stored_tokens - num_tokens,+ kvcache_desc.num_stored_tokens);auto src_tensor = request->get_tensor(out_ports.at(output_name));// NOTE: Sometimes present kv layer can contain greater seq_len@@ -634,10 +503,10 @@ void ov::npuw::LLMInferRequest::update_kvcache_for(uint32_t src_seq_len = static_cast<uint32_t>(src_tensor->get_shape()[kv_dim]);OPENVINO_ASSERT(num_tokens <= src_seq_len);if (src_seq_len > num_tokens) {- auto src_slice = make_tensor_slice(src_tensor, kv_dim, src_seq_len - num_tokens, src_seq_len);- copy_tensor_by_dim(src_slice, dst_slice, kv_dim, kv_dim);+ auto src_slice = uu::make_tensor_slice(src_tensor, kv_dim, src_seq_len - num_tokens, src_seq_len);+ uu::copy_tensor_by_dim(src_slice, dst_slice, kv_dim, kv_dim);} else {- copy_tensor_by_dim(src_tensor, dst_slice, kv_dim, kv_dim);+ uu::copy_tensor_by_dim(src_tensor, dst_slice, kv_dim, kv_dim);}}LOG_DEBUG("Done.");@@ -731,11 +600,11 @@ void ov::npuw::LLMInferRequest::infer_chunked_prefill(ov::SoPtr<ov::ITensor> inp// SEQ_LEN]// Copy postion ids with considering the 3D position_idsauto last_dim = position_ids->get_shape().size() - 1;- auto actual_position_ids_slice =- make_tensor_slice(position_ids,- static_cast<uint32_t>(last_dim),- kvcache_desc.num_stored_tokens,- kvcache_desc.num_stored_tokens + static_cast<uint32_t>(current_prompts_len));+ auto actual_position_ids_slice = ov::npuw::util::make_tensor_slice(+ position_ids,+ static_cast<uint32_t>(last_dim),+ kvcache_desc.num_stored_tokens,+ kvcache_desc.num_stored_tokens + static_cast<uint32_t>(current_prompts_len));pad_position_ids(pos_ids_in_tensor, actual_position_ids_slice);m_prefill_request->infer();@@ -957,14 +826,15 @@ void ov::npuw::LLMInferRequest::infer_generate(ov::SoPtr<ov::ITensor> input_ids,void ov::npuw::LLMInferRequest::infer() {const auto& inputs = get_inputs();- auto input_ids = get_tensor(find_port_by_name(inputs, m_input_ids_name).value());- auto attention_mask = get_tensor(find_port_by_name(inputs, layer_names::attention_mask).value());+ auto input_ids = get_tensor(ov::npuw::util::find_port_by_name(inputs, m_input_ids_name).value());+ auto attention_mask = get_tensor(ov::npuw::util::find_port_by_name(inputs, layer_names::attention_mask).value());// FIXME: position_ids might be optional for some models!- auto position_ids = get_tensor(find_port_by_name(inputs, layer_names::position_ids).value());+ auto position_ids = get_tensor(ov::npuw::util::find_port_by_name(inputs, layer_names::position_ids).value());auto token_type_ids = ov::npuw::util::TensorPtr();- if (auto type_ids_port = find_port_by_name(inputs, layer_names::token_type_ids); type_ids_port.has_value()) {+ if (auto type_ids_port = ov::npuw::util::find_port_by_name(inputs, layer_names::token_type_ids);+ type_ids_port.has_value()) {token_type_ids = get_tensor(type_ids_port.value());}diff --git a/src/plugins/intel_npu/src/plugin/npuw/llm_infer_request.hpp b/src/plugins/intel_npu/src/plugin/npuw/llm_infer_request.hppindex 517555c6001..0b1a3a02de3 100644--- a/src/plugins/intel_npu/src/plugin/npuw/llm_infer_request.hpp+++ b/src/plugins/intel_npu/src/plugin/npuw/llm_infer_request.hpp@@ -14,7 +14,7 @@namespace ov {namespace npuw {-class LLMInferRequest final : public ov::ISyncInferRequest {+class LLMInferRequest : public ov::ISyncInferRequest {public:struct layer_names {static constexpr const char* input_ids = "input_ids";@@ -41,8 +41,8 @@ public:}std::vector<ov::SoPtr<ov::IVariableState>> query_state() const override;-private:- void prepare_for_new_conversation();+protected:+ virtual void prepare_for_new_conversation();void apply_lora();diff --git a/src/plugins/intel_npu/src/plugin/npuw/serialization.hpp b/src/plugins/intel_npu/src/plugin/npuw/serialization.hppindex 5378a81dda8..c7575991920 100644--- a/src/plugins/intel_npu/src/plugin/npuw/serialization.hpp+++ b/src/plugins/intel_npu/src/plugin/npuw/serialization.hpp@@ -34,7 +34,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.11";+const constexpr char* NPUW_SERIALIZATION_VERSION = "0.12";// Forward declarationnamespace intel_npu {diff --git a/src/plugins/intel_npu/src/plugin/npuw/whisper_infer_request.cpp b/src/plugins/intel_npu/src/plugin/npuw/whisper_infer_request.cppnew file mode 100644index 00000000000..18dc977d8af--- /dev/null+++ b/src/plugins/intel_npu/src/plugin/npuw/whisper_infer_request.cpp@@ -0,0 +1,203 @@+// Copyright (C) 2025 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "whisper_infer_request.hpp"++#include <regex>++#include "../../utils/include/intel_npu/utils/zero/zero_remote_tensor.hpp"+#include "infer_request_utils.hpp"+#include "logging.hpp"+#include "openvino/runtime/iasync_infer_request.hpp"++namespace {++constexpr uint32_t INPUT_IDS_SEQ_LEN_DIM = 1;++} // namespace++void ov::npuw::WhisperInferRequest::prepare_for_new_conversation() {+ ov::npuw::util::fill_tensor_bytes(m_prefill_request->get_tensor(m_prefill_in_ports.at(m_input_ids_name)), 0u);+ 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;+}++void ov::npuw::WhisperInferRequest::infer_prefill(ov::SoPtr<ov::ITensor> input_ids,+ ov::SoPtr<ov::ITensor> enc_hidden_states) {+ LOG_DEBUG("Calling inference for Whisper prefill model...");+ LOG_BLOCK();++ prepare_for_new_conversation();++ // NB: input_ids for whisper: [token, token, pad, pad]+ auto padded_input = m_prefill_request->get_tensor(m_prefill_in_ports.at(m_input_ids_name));+ std::copy_n(input_ids->data<int64_t>(), input_ids->get_size(), padded_input->data<int64_t>());++ auto padded_attention_mask = m_prefill_request->get_tensor(m_prefill_in_ports.at("attention_mask"));+ std::fill_n(padded_attention_mask->data<int64_t>(), input_ids->get_size(), 1u);++ auto encoder_hidden_states = m_prefill_request->get_tensor(m_prefill_in_ports.at("encoder_hidden_states"));+ auto remote_tensor = std::dynamic_pointer_cast<::intel_npu::ZeroRemoteTensor>(enc_hidden_states._ptr);+ void* enc_hidden_states_data = !remote_tensor ? enc_hidden_states->data() : remote_tensor->get_original_memory();+ std::copy_n(reinterpret_cast<uint8_t*>(enc_hidden_states_data),+ enc_hidden_states->get_byte_size(),+ reinterpret_cast<uint8_t*>(encoder_hidden_states->data()));++ m_prefill_request->infer();++ m_npuw_llm_compiled_model->m_kvcache_desc.num_stored_tokens +=+ static_cast<uint32_t>(input_ids->get_shape()[INPUT_IDS_SEQ_LEN_DIM]);+ m_need_copy_kvcache = true;++ m_logits = ov::npuw::util::make_tensor_slice(m_prefill_request->get_tensor(m_prefill_out_ports.at("logits")),+ 1u,+ 0u,+ m_npuw_llm_compiled_model->m_kvcache_desc.num_stored_tokens);++ LOG_DEBUG("Done");+}++void ov::npuw::WhisperInferRequest::infer_generate(ov::SoPtr<ov::ITensor> input_ids) {+ LOG_DEBUG("Calling inference for Whisper generate model...");+ LOG_BLOCK();++ auto& kvcache_desc = m_npuw_llm_compiled_model->m_kvcache_desc;+ // NB: KV-cache is full, further generation is impossible+ if (kvcache_desc.num_stored_tokens == kvcache_desc.total_size) {+ OPENVINO_THROW("KV-Cache is full.");+ }++ if (m_need_copy_kvcache) {+ LOG_DEBUG("Copying kv-cache from prefill to generate model.");+ const std::size_t kStartOutputKVCacheLayers = 1u;+ const auto& kvcache_compiled = m_kvcache_request->get_compiled_model();+ // FIXME: Find only matching by names outputs and copy them, having previously checked that such inputs exist+ for (std::size_t i = 0; i < kvcache_compiled->outputs().size() - 1; ++i) {+ const auto& output_name = kvcache_compiled->outputs()[kStartOutputKVCacheLayers + i].get_any_name();+ auto prefill_out_tensor = m_prefill_request->get_tensor(m_prefill_out_ports.at(output_name));++ // Find self-attention KVcache tensors for Whisper, cross attn copy later+ if (output_name.find("decoder") == std::string::npos) {+ continue;+ }++ const auto& input_name = std::regex_replace(output_name, std::regex("present"), "past_key_values");+ if (m_kvcache_in_ports.find(input_name) == m_kvcache_in_ports.end()) {+ LOG_DEBUG("Input name " << input_name << " doesn't contain kv cache. Skipping.");+ continue;+ }++ auto kvcache_in_tensor = m_kvcache_request->get_tensor(m_kvcache_in_ports.at(input_name));++ // FIXME: We don't need to fill whole tensor with 0s, but only tensor.size() - num_stored_tokens+ // taking into account kvcache dimension.+ ov::npuw::util::fill_tensor<ov::float16>(kvcache_in_tensor, 0);++ const auto is_value_tensor = output_name.find("value") != std::string::npos;+ const auto kv_dim = [&](bool v_trans) -> uint32_t {+ return (is_value_tensor && v_trans) ? 3u : kvcache_desc.dim;+ };++ const auto& pre_kv_dim = kv_dim(kvcache_desc.v_tensors_transposed_pre);+ const auto& gen_kv_dim = kv_dim(kvcache_desc.v_tensors_transposed_gen);++ auto prefill_out_slice =+ ov::npuw::util::make_tensor_slice(prefill_out_tensor, pre_kv_dim, 0, kvcache_desc.num_stored_tokens);++ auto kvcache_in_slice =+ ov::npuw::util::make_tensor_slice(kvcache_in_tensor, gen_kv_dim, 0u, kvcache_desc.num_stored_tokens);+ ov::npuw::util::copy_tensor_by_dim(prefill_out_slice, kvcache_in_slice, pre_kv_dim, gen_kv_dim);+ }++ LOG_DEBUG("Copying cross attn key value for Whisper.");+ const auto& prefill_compiled = m_prefill_request->get_compiled_model();+ for (std::size_t i = 0; i < prefill_compiled->outputs().size() - 1; ++i) {+ const auto& output_name = prefill_compiled->outputs()[kStartOutputKVCacheLayers + i].get_any_name();+ auto prefill_out_tensor = m_prefill_request->get_tensor(m_prefill_out_ports.at(output_name));++ if (output_name.find("encoder") == std::string::npos) {+ continue;+ }++ const auto& input_name = std::regex_replace(output_name, std::regex("present"), "past_key_values");+ m_kvcache_request->set_tensor(m_kvcache_in_ports.at(input_name), prefill_out_tensor);+ }++ LOG_DEBUG("Prepare attention mask pattern.");+ // NB: Prepare attention mask to be in a format [0, 0, 0, 1, 1, 1, ..., 1, 0, 1]+ auto* attention_mask_data =+ m_kvcache_request->get_tensor(m_kvcache_in_ports.at("attention_mask"))->data<uint64_t>();+ auto attention_mask_size = m_kvcache_request->get_tensor(m_kvcache_in_ports.at("attention_mask"))->get_size();+ std::fill(attention_mask_data, attention_mask_data + kvcache_desc.num_stored_tokens, 0);+ std::fill(attention_mask_data + kvcache_desc.num_stored_tokens,+ attention_mask_data + attention_mask_size - 2,+ 1);+ attention_mask_data[attention_mask_size - 2] = 0;+ attention_mask_data[attention_mask_size - 1] = 1;++ m_need_copy_kvcache = false;+ }++ auto kv_input_ids = m_kvcache_request->get_tensor(m_kvcache_in_ports.at(m_input_ids_name));+ std::copy_n(input_ids->data<int64_t>(), input_ids->get_size(), kv_input_ids->data<int64_t>());++ auto kv_attn_mask = m_kvcache_request->get_tensor(m_kvcache_in_ports.at("attention_mask"));+ kv_attn_mask->data<int64_t>()[kvcache_desc.num_stored_tokens - 1] = 0;++ auto kv_cache_pos = m_kvcache_request->get_tensor(m_kvcache_in_ports.at("cache_position"));+ kv_cache_pos->data<int64_t>()[0] = kvcache_desc.num_stored_tokens;++ m_kvcache_request->infer();+ m_logits = m_kvcache_request->get_tensor(m_kvcache_out_ports.at("logits"));++ kvcache_desc.num_stored_tokens += 1;++ if (kvcache_desc.num_stored_tokens == kvcache_desc.total_size) {+ return;+ }++ LOG_DEBUG("Write KV-cache for the new token to the correct input position for next iteration.");+ const std::size_t kStartOutputKVCacheLayers = 1u;+ const auto& kvcache_compiled = m_kvcache_request->get_compiled_model();+ // FIXME: Find only matching by names outputs and copy them, having previously checked that such inputs exist+ for (std::size_t i = 0; i < kvcache_compiled->outputs().size() - 1; ++i) {+ const auto& output_name = kvcache_compiled->outputs()[kStartOutputKVCacheLayers + i].get_any_name();+ const auto& input_name = std::regex_replace(output_name, std::regex("present"), "past_key_values");+ if (m_kvcache_in_ports.find(input_name) == m_kvcache_in_ports.end()) {+ LOG_DEBUG("Input name " << input_name << " doesn't contain kv cache. Skipping.");+ continue;+ }++ auto kvcache_in_tensor = m_kvcache_request->get_tensor(m_kvcache_in_ports.at(input_name));+ const auto& kv_dim = (output_name.find("value") != std::string::npos && kvcache_desc.v_tensors_transposed_gen)+ ? 3u+ : kvcache_desc.dim;+ auto kvcache_in_slice = ov::npuw::util::make_tensor_slice(kvcache_in_tensor,+ kv_dim,+ kvcache_desc.num_stored_tokens - 1,+ kvcache_desc.num_stored_tokens);+ auto kvcache_out_tensor = m_kvcache_request->get_tensor(m_kvcache_out_ports.at(output_name));+ ov::npuw::util::copy_tensor_by_dim(kvcache_out_tensor, kvcache_in_slice, kv_dim, kv_dim);+ }+ LOG_DEBUG("Done");+}++void ov::npuw::WhisperInferRequest::infer() {+ const auto& inputs = get_inputs();++ auto input_ids = get_tensor(ov::npuw::util::find_port_by_name(inputs, m_input_ids_name).value());+ OPENVINO_ASSERT(ov::element::i64 == input_ids->get_element_type());++ auto encoder_hidden_states = get_tensor(ov::npuw::util::find_port_by_name(inputs, "encoder_hidden_states").value());+ OPENVINO_ASSERT(ov::element::f32 == encoder_hidden_states->get_element_type());++ // NB: Check the sequence length provided for input_ids+ // in order to distinguish prefill / generate stages+ if (input_ids->get_shape()[INPUT_IDS_SEQ_LEN_DIM] != 1) {+ infer_prefill(input_ids, encoder_hidden_states);+ } else {+ infer_generate(input_ids);+ }+}diff --git a/src/plugins/intel_npu/src/plugin/npuw/whisper_infer_request.hpp b/src/plugins/intel_npu/src/plugin/npuw/whisper_infer_request.hppnew file mode 100644index 00000000000..2f4a68cf45d--- /dev/null+++ b/src/plugins/intel_npu/src/plugin/npuw/whisper_infer_request.hpp@@ -0,0 +1,30 @@+// Copyright (C) 2025 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#pragma once++#include "llm_infer_request.hpp"++namespace ov {+namespace npuw {++class WhisperInferRequest final : public LLMInferRequest {+public:+ explicit WhisperInferRequest(const std::shared_ptr<LLMCompiledModel>& compiled_model)+ : LLMInferRequest(compiled_model) {}++ void infer() override;++protected:+ void prepare_for_new_conversation() override;++ void infer_prefill(ov::SoPtr<ov::ITensor> input_ids, ov::SoPtr<ov::ITensor> enc_hidden_states);++ void infer_generate(ov::SoPtr<ov::ITensor> input_ids);++ bool m_need_copy_kvcache = false;+};++} // namespace npuw+} // namespace ovdiff --git a/src/plugins/intel_npu/src/plugin/src/plugin.cpp b/src/plugins/intel_npu/src/plugin/src/plugin.cppindex 2dbab6197e3..8633bc5454b 100644--- a/src/plugins/intel_npu/src/plugin/src/plugin.cpp+++ b/src/plugins/intel_npu/src/plugin/src/plugin.cpp@@ -356,6 +356,7 @@ void Plugin::init_options() {REGISTER_OPTION(NPUW_LLM_PREFILL_CHUNK_SIZE);REGISTER_OPTION(NPUW_LLM_SHARED_HEAD);REGISTER_OPTION(NPUW_LLM_MAX_LORA_RANK);+ REGISTER_OPTION(NPUW_WHISPER);REGISTER_OPTION(NPUW_LLM_PREFILL_HINT);REGISTER_OPTION(NPUW_LLM_PREFILL_CONFIG);REGISTER_OPTION(NPUW_LLM_ADDITIONAL_PREFILL_CONFIG);diff --git a/src/plugins/intel_npu/src/plugin/src/properties.cpp b/src/plugins/intel_npu/src/plugin/src/properties.cppindex f7180e96127..72167b6d9ac 100644--- a/src/plugins/intel_npu/src/plugin/src/properties.cpp+++ b/src/plugins/intel_npu/src/plugin/src/properties.cpp@@ -475,6 +475,7 @@ void Properties::registerPluginProperties() {TRY_REGISTER_SIMPLE_PROPERTY(ov::intel_npu::npuw::llm::shared_lm_head_config, NPUW_LLM_SHARED_LM_HEAD_CONFIG);TRY_REGISTER_SIMPLE_PROPERTY(ov::intel_npu::npuw::llm::additional_shared_lm_head_config,NPUW_LLM_ADDITIONAL_SHARED_LM_HEAD_CONFIG);+ TRY_REGISTER_SIMPLE_PROPERTY(ov::intel_npu::npuw::whisper::enabled, NPUW_WHISPER);// 2. Metrics (static device and enviroment properties)// ========