Skip to content

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

[NPUW]Expose history size on the base infer request as a variable state (#39050)

intelgaoxiongec339ccd0ed7
diff --git a/src/plugins/intel_npu/src/plugin/npuw/base_sync_infer_request.cpp b/src/plugins/intel_npu/src/plugin/npuw/base_sync_infer_request.cpp
index e2011e9332a..16666493091 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/base_sync_infer_request.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/base_sync_infer_request.cpp
@@ -203,6 +203,7 @@ std::vector<ov::SoPtr<ov::IVariableState>> ov::npuw::IBaseInferRequest::query_st
variable_states.emplace_back(state);
}
}
+ variable_states.emplace_back(m_history_state, nullptr);
return variable_states;
}
diff --git a/src/plugins/intel_npu/src/plugin/npuw/base_sync_infer_request.hpp b/src/plugins/intel_npu/src/plugin/npuw/base_sync_infer_request.hpp
index 2182fe937a8..ddab011915b 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/base_sync_infer_request.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/base_sync_infer_request.hpp
@@ -5,6 +5,7 @@
#pragma once
+#include <algorithm>
#include <functional>
#include <map>
#include <memory>
@@ -20,6 +21,7 @@
#include "pyramid_attention.hpp"
#include "spatial.hpp"
#include "util.hpp"
+#include "variable_state.hpp"
namespace ov {
namespace npuw {
@@ -72,11 +74,12 @@ public:
virtual bool supports_async_pipeline() const = 0;
void update_history_size(int64_t history_size) {
- m_history_size = history_size;
+ m_history_state->set(history_size);
}
int64_t get_history_size() const {
- return m_history_size;
+ // An unset history behaves as an empty one.
+ return std::max<int64_t>(m_history_state->get(), 0);
}
std::size_t get_run_iteration() const {
@@ -88,7 +91,8 @@ public:
}
protected:
- int64_t m_history_size = 0;
+ // Exposed through query_state() so callers holding only a public InferRequest can set it.
+ std::shared_ptr<ov::npuw::HistorySizeState> m_history_state = std::make_shared<ov::npuw::HistorySizeState>();
// After set_tensor() has replaced block tensors with dummies on the outer (LLM-level)
// request, call this to push those updated tensors into sub-requests via bind_global_params.
diff --git a/src/plugins/intel_npu/src/plugin/npuw/just_sync_infer_request.cpp b/src/plugins/intel_npu/src/plugin/npuw/just_sync_infer_request.cpp
index 974d8668a28..a17095e603e 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/just_sync_infer_request.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/just_sync_infer_request.cpp
@@ -76,7 +76,7 @@ bool ov::npuw::JustInferRequest::is_subrequest_pipelined(size_t idx) const {
return is_pipelined(idx);
}
-std::size_t ov::npuw::JustInferRequest::history_size() const {
+int64_t ov::npuw::JustInferRequest::history_size() const {
return get_history_size();
}
diff --git a/src/plugins/intel_npu/src/plugin/npuw/just_sync_infer_request.hpp b/src/plugins/intel_npu/src/plugin/npuw/just_sync_infer_request.hpp
index efefe985c39..079f7e4b4a1 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/just_sync_infer_request.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/just_sync_infer_request.hpp
@@ -92,7 +92,7 @@ public:
ov::SoPtr<ov::IAsyncInferRequest> get_pipeline_subrequest(size_t idx) const;
void set_pipeline_subrequest(size_t idx, ov::SoPtr<ov::IAsyncInferRequest> request);
bool is_subrequest_pipelined(size_t idx) const;
- std::size_t history_size() const;
+ int64_t history_size() const;
bool subgraph_needs_copy(std::size_t idx) const;
bool attention_no_copy() const;
const ov::SoPtr<ov::ICompiledModel>& compiled_submodel(size_t idx) const;
diff --git a/src/plugins/intel_npu/src/plugin/npuw/variable_state.hpp b/src/plugins/intel_npu/src/plugin/npuw/variable_state.hpp
index 0be89acbcdc..b99c0ad72ae 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/variable_state.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/variable_state.hpp
@@ -4,8 +4,13 @@
#pragma once
+#include <cstdint>
+
+#include "openvino/core/except.hpp"
#include "openvino/runtime/itensor.hpp"
#include "openvino/runtime/ivariable_state.hpp"
+#include "openvino/runtime/make_tensor.hpp"
+#include "openvino/runtime/tensor.hpp"
namespace ov {
namespace npuw {
@@ -44,5 +49,57 @@ private:
bool m_state_updapted;
};
+// Out-of-band channel for the number of tokens already stored in the KV cache before the
+// next infer() (the "history"). Dynamic attention needs it to pick the past KV / mask shape,
+// and a caller that only holds a public ov::InferRequest has no other way to provide it:
+// for (auto& state : request.query_state()) {
+// if (state.get_name() == ov::npuw::HistorySizeState::name) {
+// ov::Tensor t(ov::element::i64, ov::Shape{1});
+// t.data<int64_t>()[0] = history;
+// state.set_state(t);
+// }
+// }
+// The value persists across infer() calls and must be set before every prefill chunk.
+// It starts as kUnset (-1), and so does it after reset(); an unset value is used as 0.
+// Only selectors that derive the past length from it use it; decode does not.
+class HistorySizeState final : public ov::IVariableState {
+public:
+ static constexpr const char* name = "npuw_history_size";
+ static constexpr int64_t kUnset = -1;
+
+ HistorySizeState() : ov::IVariableState(name) {
+ m_state = ov::get_tensor_impl(ov::Tensor(ov::element::i64, ov::Shape{1}));
+ reset();
+ }
+
+ void reset() override {
+ set(kUnset);
+ }
+
+ // A copy, so mutating the returned tensor cannot change the history behind the request's back.
+ ov::SoPtr<ov::ITensor> get_state() const override {
+ ov::Tensor copy(ov::element::i64, ov::Shape{1});
+ copy.data<int64_t>()[0] = get();
+ return ov::get_tensor_impl(copy);
+ }
+
+ void set_state(const ov::SoPtr<ov::ITensor>& newState) override {
+ OPENVINO_ASSERT(newState, "HistorySizeState expects a non-null tensor");
+ OPENVINO_ASSERT(newState->get_element_type() == ov::element::i64, "HistorySizeState expects an int64 tensor");
+ OPENVINO_ASSERT(newState->get_size() == 1, "HistorySizeState expects a single-element tensor");
+ const auto value = newState->data<int64_t>()[0];
+ OPENVINO_ASSERT(value >= 0, "HistorySizeState expects a non-negative history size");
+ set(value);
+ }
+
+ int64_t get() const {
+ return m_state->data<int64_t>()[0];
+ }
+
+ void set(int64_t value) {
+ m_state->data<int64_t>()[0] = value;
+ }
+};
+
} // namespace npuw
} // namespace ov
diff --git a/src/plugins/intel_npu/tests/unit/CMakeLists.txt b/src/plugins/intel_npu/tests/unit/CMakeLists.txt
index bc1339cca73..bd20c119525 100644
--- a/src/plugins/intel_npu/tests/unit/CMakeLists.txt
+++ b/src/plugins/intel_npu/tests/unit/CMakeLists.txt
@@ -195,6 +195,7 @@ target_sources(${TARGET_NAME} PRIVATE
${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
+ ${CMAKE_CURRENT_SOURCE_DIR}/npuw/history_size_state_test.cpp
${CMAKE_CURRENT_SOURCE_DIR}/npuw/llm_infer_request_variant_switch_test.cpp
${CMAKE_CURRENT_SOURCE_DIR}/npuw/llm_infer_request_port_names_test.cpp
${CMAKE_CURRENT_SOURCE_DIR}/npuw/llm_infer_request_no_position_ids_test.cpp
diff --git a/src/plugins/intel_npu/tests/unit/npuw/history_size_state_test.cpp b/src/plugins/intel_npu/tests/unit/npuw/history_size_state_test.cpp
new file mode 100644
index 00000000000..b496fc209d3
--- /dev/null
+++ b/src/plugins/intel_npu/tests/unit/npuw/history_size_state_test.cpp
@@ -0,0 +1,76 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include <gtest/gtest.h>
+
+#include <algorithm>
+#include <cstdint>
+
+#include "openvino/runtime/make_tensor.hpp"
+#include "openvino/runtime/tensor.hpp"
+#include "variable_state.hpp"
+
+namespace {
+
+ov::SoPtr<ov::ITensor> make_history(const ov::Shape& shape, int64_t value) {
+ ov::Tensor tensor(ov::element::i64, shape);
+ std::fill_n(tensor.data<int64_t>(), tensor.get_size(), value);
+ return ov::get_tensor_impl(tensor);
+}
+
+TEST(HistorySizeStateTest, NameAndInitialValue) {
+ ov::npuw::HistorySizeState state;
+ EXPECT_EQ(state.get_name(), "npuw_history_size");
+ EXPECT_EQ(state.get(), ov::npuw::HistorySizeState::kUnset);
+ EXPECT_EQ(state.get_state()->data<int64_t>()[0], -1);
+}
+
+TEST(HistorySizeStateTest, SetStateUpdatesHistory) {
+ ov::npuw::HistorySizeState state;
+ state.set_state(make_history(ov::Shape{1}, 128));
+ EXPECT_EQ(state.get(), 128);
+ EXPECT_EQ(state.get_state()->data<int64_t>()[0], 128);
+}
+
+TEST(HistorySizeStateTest, InternalSetterIsVisibleThroughGetState) {
+ ov::npuw::HistorySizeState state;
+ state.set(512);
+ EXPECT_EQ(state.get_state()->data<int64_t>()[0], 512);
+}
+
+TEST(HistorySizeStateTest, ResetMarksHistoryUnset) {
+ ov::npuw::HistorySizeState state;
+ state.set(64);
+ state.reset();
+ EXPECT_EQ(state.get(), ov::npuw::HistorySizeState::kUnset);
+}
+
+// The caller's tensor must not be aliased: later changes to it, or to the tensor returned
+// by get_state(), must not alter the history.
+TEST(HistorySizeStateTest, DoesNotAliasCallerOrReturnedTensors) {
+ ov::npuw::HistorySizeState state;
+
+ auto input = make_history(ov::Shape{1}, 32);
+ state.set_state(input);
+ input->data<int64_t>()[0] = 999;
+ EXPECT_EQ(state.get(), 32);
+
+ state.get_state()->data<int64_t>()[0] = 999;
+ EXPECT_EQ(state.get(), 32);
+}
+
+TEST(HistorySizeStateTest, SetStateRejectsInvalidTensors) {
+ ov::npuw::HistorySizeState state;
+ state.set(7);
+
+ ov::Tensor wrong_type(ov::element::i32, ov::Shape{1});
+ EXPECT_ANY_THROW(state.set_state(ov::get_tensor_impl(wrong_type)));
+ EXPECT_ANY_THROW(state.set_state(make_history(ov::Shape{2}, 1)));
+ EXPECT_ANY_THROW(state.set_state(make_history(ov::Shape{1}, -1)));
+ EXPECT_ANY_THROW(state.set_state(ov::SoPtr<ov::ITensor>{}));
+
+ EXPECT_EQ(state.get(), 7) << "a rejected set_state must leave the history untouched";
+}
+
+} // namespace
diff --git a/src/plugins/intel_npu/tests/unit/npuw/subgraph_behavior_infer_test.cpp b/src/plugins/intel_npu/tests/unit/npuw/subgraph_behavior_infer_test.cpp
index af8717358c9..fb37642d8c3 100644
--- a/src/plugins/intel_npu/tests/unit/npuw/subgraph_behavior_infer_test.cpp
+++ b/src/plugins/intel_npu/tests/unit/npuw/subgraph_behavior_infer_test.cpp
@@ -896,4 +896,104 @@ TEST_F(SubgraphBehaviorInferTest, PyramidBehaviorBindsOnlyActiveVariantPorts) {
EXPECT_NO_THROW(request->infer());
}
+// The history size reaches the base infer request only through the npuw_history_size variable
+// state, which a caller holding nothing but the public request can find via query_state().
+// Dynamic attention uses it to pick the pyramid variant for a prefill chunk, so driving that
+// state from the outside must change which variant executes.
+TEST_F(SubgraphBehaviorInferTest, PyramidSelectionFollowsHistorySizeStateSetThroughRequest) {
+ auto model = ov::test::npuw::build_dynamic_attention_llm_model();
+ ov::npuw::util::OptimizeValueTensors(true).run_on_model(model);
+ ov::npuw::RedirectNewKvToOutput().run_on_model(model);
+
+ auto evaluation = std::make_shared<EvaluationLog>();
+ auto plugin = std::make_shared<TestPlugin>();
+ auto core = make_core(plugin, /*own_ports_only=*/true, evaluation);
+ plugin->set_core(core);
+
+ auto props = base_props();
+ props["NPUW_ATTN"] = std::string("PYRAMID");
+ props["NPUW_FALLBACK_EXEC"] = std::string("NO");
+ auto compiled = std::make_shared<ov::npuw::CompiledModel>(model, plugin, props);
+
+ const ov::npuw::compiled::PyramidAttention* pyramid = nullptr;
+ for (const auto& desc : compiled->m_compiled_submodels) {
+ if (const auto* candidate = ov::npuw::attn::get_compiled_pyramid(desc.pipeline.context)) {
+ pyramid = candidate;
+ break;
+ }
+ }
+ ASSERT_NE(pyramid, nullptr);
+ ASSERT_GT(pyramid->_compiled_models.size(), 1u) << "Need more than one pyramid variant to tell them apart";
+ const ov::ICompiledModel* smallest = pyramid->_compiled_models.front()._ptr.get();
+ const ov::ICompiledModel* largest = pyramid->_compiled_models.back()._ptr.get();
+ ASSERT_NE(smallest, largest);
+
+ // Exactly what an external framework sees: the request's states, nothing else.
+ auto request = compiled->create_infer_request();
+ ASSERT_NE(request, nullptr);
+ const auto find_history_state = [&]() {
+ for (const auto& state : request->query_state()) {
+ if (state->get_name() == ov::npuw::HistorySizeState::name) {
+ return state;
+ }
+ }
+ ADD_FAILURE() << ov::npuw::HistorySizeState::name << " is not exposed by the request";
+ return ov::SoPtr<ov::IVariableState>{};
+ };
+ const auto set_history = [&](int64_t history) {
+ ov::Tensor tensor(ov::element::i64, ov::Shape{1});
+ tensor.data<int64_t>()[0] = history;
+ find_history_state()->set_state(ov::get_tensor_impl(tensor));
+ };
+
+ for (const auto& input : compiled->inputs()) {
+ ov::Tensor tensor(input.get_element_type(), input.get_shape());
+ std::memset(tensor.data(), 0, tensor.get_byte_size());
+ if (input.get_names().count("position_ids")) {
+ std::iota(tensor.data<int64_t>(), tensor.data<int64_t>() + tensor.get_size(), int64_t{0});
+ }
+ request->set_tensor(input, ov::get_tensor_impl(tensor));
+ }
+
+ const auto executed = [&](const ov::ICompiledModel* variant) {
+ std::lock_guard<std::mutex> lock(evaluation->mutex);
+ const auto it = evaluation->executions.find(variant);
+ return it != evaluation->executions.end() && !it->second.empty();
+ };
+ const auto run = [&]() {
+ {
+ std::lock_guard<std::mutex> lock(evaluation->mutex);
+ evaluation->executions.clear(); // Drop warmups and the previous run.
+ }
+ ASSERT_NO_THROW(request->infer());
+ };
+
+ EXPECT_EQ(find_history_state()->get_state()->data<int64_t>()[0], -1) << "No history until a caller provides it";
+ run();
+ EXPECT_TRUE(executed(smallest)) << "An unset history behaves as an empty one";
+ EXPECT_FALSE(executed(largest));
+
+ set_history(0);
+ run();
+ EXPECT_TRUE(executed(smallest)) << "A first chunk without history runs the smallest variant";
+ EXPECT_FALSE(executed(largest));
+
+ // Enough history to need the largest context.
+ const auto history = static_cast<int64_t>(pyramid->_context_lengths.back());
+ set_history(history);
+ EXPECT_EQ(find_history_state()->get_state()->data<int64_t>()[0], history);
+ run();
+ EXPECT_TRUE(executed(largest)) << "History set through the request state must reach attention selection";
+ EXPECT_FALSE(executed(smallest));
+
+ // Resetting the request's states marks history unset, which behaves as an empty history.
+ for (const auto& state : request->query_state()) {
+ state->reset();
+ }
+ EXPECT_EQ(find_history_state()->get_state()->data<int64_t>()[0], -1);
+ run();
+ EXPECT_TRUE(executed(smallest));
+ EXPECT_FALSE(executed(largest));
+}
+
} // namespace