Skip to content

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

[NPUW] Support reset state for Whisper (#39421)

Ekaterina Shiryaeva641e19a386f6
diff --git a/src/plugins/intel_npu/src/plugin/npuw/llm_infer_request.cpp b/src/plugins/intel_npu/src/plugin/npuw/llm_infer_request.cpp
index 8569a6a2a94..1bceb804dfb 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
@@ -2268,3 +2268,11 @@ std::vector<ov::SoPtr<ov::IVariableState>> ov::npuw::LLMInferRequest::query_stat
return states;
}
+
+int64_t ov::npuw::LLMInferRequest::get_external_stored_tokens() const {
+ return m_stored_tokens_state->get_num_stored_tokens();
+}
+
+void ov::npuw::LLMInferRequest::publish_stored_tokens(int64_t num_tokens) {
+ m_stored_tokens_state->set_num_stored_tokens(num_tokens);
+}
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.hpp
index c7d88b1ccc3..35f9ee90a33 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
@@ -58,6 +58,8 @@ protected:
void prepare_for_new_conversation(int64_t prompt_length);
void apply_lora();
void clear_chunk_prefill_kv_cache();
+ int64_t get_external_stored_tokens() const;
+ void publish_stored_tokens(int64_t num_tokens);
void copy_kvcache();
void update_kvcache_for(std::shared_ptr<ov::IAsyncInferRequest> request,
const PortsMap& in_ports,
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 fa5044f45db..97c9448012d 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
@@ -213,17 +213,23 @@ void ov::npuw::WhisperInferRequest::infer() {
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());
+ const auto prompt_length = input_ids->get_shape()[INPUT_IDS_SEQ_LEN_DIM];
+ // Sync num_stored_tokens before infer with external updates
+ auto& kvcache_desc = m_npuw_llm_compiled_model->m_kvcache_desc;
+ kvcache_desc.num_stored_tokens = static_cast<uint32_t>(get_external_stored_tokens());
+
// NB: Check the sequence length provided for input_ids
// in order to distinguish prefill / generate stages.
// It's possible that for language detection, the first prefill
// will be for a single token. So we also run prefill if
// m_kvcache_desc.num_stored_tokens is 0.
- if (input_ids->get_shape()[INPUT_IDS_SEQ_LEN_DIM] != 1 ||
- m_npuw_llm_compiled_model->m_kvcache_desc.num_stored_tokens == 0) {
+ if (prompt_length != 1 || kvcache_desc.num_stored_tokens == 0) {
infer_prefill(input_ids, encoder_hidden_states);
} else {
infer_generate(input_ids);
}
+
+ publish_stored_tokens(static_cast<int64_t>(kvcache_desc.num_stored_tokens));
}
// FIXME: Whisper Decompose SDPA
diff --git a/src/plugins/intel_npu/tests/unit/CMakeLists.txt b/src/plugins/intel_npu/tests/unit/CMakeLists.txt
index e49699a8d74..41eaaa4c427 100644
--- a/src/plugins/intel_npu/tests/unit/CMakeLists.txt
+++ b/src/plugins/intel_npu/tests/unit/CMakeLists.txt
@@ -193,6 +193,7 @@ target_sources(${TARGET_NAME} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/npuw/gather_sub128_matcher_test.cpp
${CMAKE_CURRENT_SOURCE_DIR}/npuw/llm_continuation_test.cpp
${CMAKE_CURRENT_SOURCE_DIR}/npuw/llm_continued_prefill_test.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/npuw/whisper_infer_request_test.cpp
${CMAKE_CURRENT_SOURCE_DIR}/npuw/llm_swa_cache_test.cpp
${CMAKE_CURRENT_SOURCE_DIR}/npuw/batched_element_test.cpp
${CMAKE_CURRENT_SOURCE_DIR}/npuw/stored_tokens_state_test.cpp
diff --git a/src/plugins/intel_npu/tests/unit/npuw/whisper_infer_request_test.cpp b/src/plugins/intel_npu/tests/unit/npuw/whisper_infer_request_test.cpp
new file mode 100644
index 00000000000..b57b90d8fa9
--- /dev/null
+++ b/src/plugins/intel_npu/tests/unit/npuw/whisper_infer_request_test.cpp
@@ -0,0 +1,166 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include <gtest/gtest.h>
+
+#include <algorithm>
+#include <cstdint>
+#include <cstring>
+#include <memory>
+#include <ostream>
+#include <string>
+#include <vector>
+
+#include "executor.hpp"
+#include "llm_compiled_model.hpp"
+#include "llm_stored_tokens_state.hpp"
+#include "llm_test_helpers.hpp"
+#include "openvino/openvino.hpp"
+#include "whisper/whisper_infer_request.hpp"
+
+namespace {
+
+using ov::test::npuw::build_whisper_decoder_test_model;
+using ov::test::npuw::make_test_model_config;
+using ov::test::npuw::NullPlugin;
+
+class FakeSubCompiledModel;
+
+class FakeSubInferRequest final : public ov::ISyncInferRequest {
+public:
+ explicit FakeSubInferRequest(std::shared_ptr<const FakeSubCompiledModel> compiled_model);
+
+ void infer() override;
+ ov::SoPtr<ov::ITensor> get_tensor(const ov::Output<const ov::Node>& port) const override {
+ return ov::ISyncInferRequest::get_tensor(port);
+ }
+ void set_tensor(const ov::Output<const ov::Node>& port, const ov::SoPtr<ov::ITensor>& tensor) override {
+ ov::ISyncInferRequest::set_tensor(port, tensor);
+ }
+ void check_tensors() const override {}
+ std::vector<ov::SoPtr<ov::IVariableState>> query_state() const override {
+ return {};
+ }
+ std::vector<ov::ProfilingInfo> get_profiling_info() const override {
+ return {};
+ }
+};
+
+class FakeSubCompiledModel final : public ov::npuw::ICompiledModel_v0 {
+public:
+ FakeSubCompiledModel(const std::shared_ptr<ov::Model>& model,
+ const std::shared_ptr<const ov::IPlugin>& plugin,
+ const ov::AnyMap&)
+ : ov::npuw::ICompiledModel_v0(model, plugin),
+ m_model(model) {}
+
+ void export_model(std::ostream&) const override {}
+ std::shared_ptr<const ov::Model> get_runtime_model() const override {
+ return m_model;
+ }
+ void set_property(const ov::AnyMap&) override {}
+ ov::Any get_property(const std::string&) const override {
+ return {};
+ }
+ std::shared_ptr<ov::ISyncInferRequest> create_sync_infer_request() const override {
+ auto self = std::static_pointer_cast<const FakeSubCompiledModel>(shared_from_this());
+ return std::make_shared<FakeSubInferRequest>(std::move(self));
+ }
+ std::shared_ptr<ov::npuw::IBaseInferRequest> create_base_infer_request() const override {
+ return {};
+ }
+ std::shared_ptr<ov::IAsyncInferRequest> wrap_async_infer_request(
+ std::shared_ptr<ov::npuw::IBaseInferRequest>) const override {
+ return std::make_shared<ov::IAsyncInferRequest>(create_sync_infer_request(),
+ intel_npu::make_executor("whisper_infer_task", 1),
+ intel_npu::make_executor("whisper_infer_callback", 1));
+ }
+ std::string submodel_device(std::size_t) const override {
+ return "CPU";
+ }
+ std::size_t num_submodels() const override {
+ return 0;
+ }
+ std::shared_ptr<ov::npuw::weights::Bank> get_weights_bank() const override {
+ return {};
+ }
+ void set_weights_bank(std::shared_ptr<ov::npuw::weights::Bank>) override {}
+ void finalize_weights_bank() override {}
+ void reconstruct_closure() override {}
+ void serialize(std::ostream&, const ov::npuw::s11n::CompiledContext&) const override {}
+
+private:
+ std::shared_ptr<ov::Model> m_model;
+};
+
+FakeSubInferRequest::FakeSubInferRequest(std::shared_ptr<const FakeSubCompiledModel> compiled_model)
+ : ov::ISyncInferRequest(std::move(compiled_model)) {
+ for (const auto& input : get_compiled_model()->inputs()) {
+ ov::ISyncInferRequest::set_tensor(input,
+ ov::get_tensor_impl(ov::Tensor(input.get_element_type(), input.get_shape())));
+ }
+ for (const auto& output : get_compiled_model()->outputs()) {
+ ov::ISyncInferRequest::set_tensor(
+ output,
+ ov::get_tensor_impl(ov::Tensor(output.get_element_type(), output.get_shape())));
+ }
+}
+
+void FakeSubInferRequest::infer() {
+ for (const auto& output : get_compiled_model()->outputs()) {
+ auto tensor = ov::ISyncInferRequest::get_tensor(output);
+ std::memset(tensor->data(), 0, tensor->get_byte_size());
+ }
+}
+
+ov::npuw::LLMCompiledModel::CompiledModelFactory make_fake_factory() {
+ return [](const std::shared_ptr<ov::Model>& model,
+ const std::shared_ptr<const ov::IPlugin>& plugin,
+ const ov::AnyMap& props) -> std::shared_ptr<ov::npuw::ICompiledModel_v0> {
+ return std::make_shared<FakeSubCompiledModel>(model, plugin, props);
+ };
+}
+
+void set_input(ov::npuw::WhisperInferRequest& request, const std::string& name, const ov::Tensor& tensor) {
+ const auto port = ov::npuw::util::find_port_by_name(request.get_inputs(), name);
+ ASSERT_TRUE(port.has_value()) << "Missing input " << name;
+ request.set_tensor(*port, ov::get_tensor_impl(tensor));
+}
+
+} // namespace
+
+TEST(WhisperInferRequest, ResetStoredTokensStartsFreshPrefillForSingleToken) {
+ auto plugin = std::make_shared<NullPlugin>();
+ auto model = build_whisper_decoder_test_model();
+ ov::AnyMap properties{{"NPUW_LLM", "YES"},
+ {"NPUW_WHISPER", "YES"},
+ {"NPUW_WHISPER_EOS_TOKEN", "42"},
+ {"NPUW_DEVICES", "CPU"}};
+ auto compiled = std::make_shared<ov::npuw::LLMCompiledModel>(model, plugin, properties, make_fake_factory());
+ ov::npuw::WhisperInferRequest request(compiled);
+
+ ov::Tensor input_ids(ov::element::i64, ov::Shape{1, 2});
+ std::fill_n(input_ids.data<int64_t>(), input_ids.get_size(), 1);
+ const auto config = make_test_model_config<ov::test::npuw::WhisperConfig>();
+ ov::Tensor encoder_hidden_states(
+ ov::element::f32,
+ ov::Shape{1, config.get_encoder_seq_len(), config.hidden_size});
+ std::fill_n(encoder_hidden_states.data<float>(), encoder_hidden_states.get_size(), 0.0f);
+ set_input(request, "input_ids", input_ids);
+ set_input(request, "encoder_hidden_states", encoder_hidden_states);
+
+ ASSERT_NO_THROW(request.infer());
+ ASSERT_EQ(request.query_state().size(), 1u);
+ auto stored_tokens_state = request.query_state().front();
+ ASSERT_EQ(stored_tokens_state->get_name(), ov::npuw::StoredTokensState::state_name);
+ ASSERT_EQ(stored_tokens_state->get_state()->data<int64_t>()[0], 2);
+
+ stored_tokens_state->reset();
+ ov::Tensor one_token(ov::element::i64, ov::Shape{1, 1});
+ one_token.data<int64_t>()[0] = 1;
+ set_input(request, "input_ids", one_token);
+
+ ASSERT_NO_THROW(request.infer());
+ EXPECT_EQ(stored_tokens_state->get_state()->data<int64_t>()[0], 1);
+}
\ No newline at end of file