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
HFA: take the prefill past length from npuw_history_size (#39064)
Anoob4ad5d927810b
diff --git a/src/plugins/intel_npu/src/plugin/npuw/attn/attn_subgraph.cpp b/src/plugins/intel_npu/src/plugin/npuw/attn/attn_subgraph.cppindex 181208befc6..8ec5b140033 100644--- a/src/plugins/intel_npu/src/plugin/npuw/attn/attn_subgraph.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/attn/attn_subgraph.cpp@@ -191,7 +191,16 @@ void ensure_hfa_selector(ov::npuw::v1::subgraphs::InferContext& ctx, RuntimeStatconst size_t original_query_length = hfa->_sdpa_attention_info._query_size;state.hfa_selector = runtime::host_flash_attention::PositionIDs::find(original_query_length, request);if (!state.hfa_selector) {- OPENVINO_THROW("HFA dynamic capability is enabled, but no run-time features were found.");+ // No position_ids: the host must report the history through the npuw_history_size state+ // before every prefill chunk, starting with the first one.+ state.hfa_selector = runtime::host_flash_attention::HistorySize::find(original_query_length, request);+ if (state.hfa_selector) {+ LOG_VERB("HFA: no position_ids input, taking the past length from the npuw_history_size state");+ }+ }+ if (!state.hfa_selector) {+ OPENVINO_THROW("HFA dynamic capability is enabled, but no run-time features were found: the model has no "+ "position_ids input and the npuw_history_size state was not set before the first infer().");}}diff --git a/src/plugins/intel_npu/src/plugin/npuw/host_flash_attention.cpp b/src/plugins/intel_npu/src/plugin/npuw/host_flash_attention.cppindex fbe6cf9bdda..3b952b1b388 100644--- a/src/plugins/intel_npu/src/plugin/npuw/host_flash_attention.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/host_flash_attention.cpp@@ -18,6 +18,7 @@#include "openvino/openvino.hpp"#include "openvino/pass/pattern/op/wrap_type.hpp"#include "util.hpp"+#include "variable_state.hpp"namespace ov {namespace npuw {@@ -1486,6 +1487,39 @@ int64_t PositionIDs::context_length() const {return _original_query_length + _past_length;}+// HistorySize constructor+HistorySize::HistorySize(std::size_t original_query_length) : _original_query_length(original_query_length) {+ _case = _original_query_length == 1 ? Case::GENERATE : Case::PREFILL;+}++void HistorySize::prepare(int64_t past_len) {+ // past_len is the request's history_size(): what the host last set through npuw_history_size,+ // or 0 if it is unset. Same chunk rounding as PositionIDs, which takes its prefill past from+ // past_len too. HFA is prefill-only; for a query length of 1 (GENERATE, rejected by the HFA+ // runtime) the rounding is the identity.+ _past_length = ((past_len + _original_query_length - 1) / _original_query_length) * _original_query_length;+}++int64_t HistorySize::context_length() const {+ return _original_query_length + _past_length;+}++Selector::Ptr HistorySize::find(std::size_t original_query_length, const ov::ISyncInferRequest& rq) {+ for (const auto& state : rq.query_state()) {+ if (state->get_name() != ov::npuw::HistorySizeState::name) {+ continue;+ }+ // Still unset at the first infer(): the host never reports the history, and every chunk+ // would silently run without its past. A later reset() (a new conversation) is fine and+ // reads as an empty history.+ if (state->get_state()->data<int64_t>()[0] == ov::npuw::HistorySizeState::kUnset) {+ return Selector::Ptr{};+ }+ return std::make_shared<HistorySize>(original_query_length);+ }+ return Selector::Ptr{};+}+// ============================================================================// HFARuntimeContext Implementation// ============================================================================diff --git a/src/plugins/intel_npu/src/plugin/npuw/host_flash_attention.hpp b/src/plugins/intel_npu/src/plugin/npuw/host_flash_attention.hppindex 744986c0c42..4b703a224b3 100644--- a/src/plugins/intel_npu/src/plugin/npuw/host_flash_attention.hpp+++ b/src/plugins/intel_npu/src/plugin/npuw/host_flash_attention.hpp@@ -528,6 +528,23 @@ public:static Selector::Ptr find(std::size_t original_query_length, const ov::ISyncInferRequest& rq);};+// Define execution selection based on the history size alone. For a plain compiled model with no+// "position_ids" input, driven by a host that reports the number of tokens already in the KV cache+// through the npuw_history_size variable state (e.g. LiteRT). Prefill only, like HFA itself.+class HistorySize final : public Selector {+ int64_t _past_length = 0;+ std::size_t _original_query_length = 0u; // original query length before PropagateSliceUp++public:+ explicit HistorySize(std::size_t original_query_length);+ void prepare(int64_t past_len) override;+ int64_t context_length() const override;++ // Returns nullptr unless rq exposes the npuw_history_size state and the host has set it, i.e.+ // unless the host is known to report the history. Meant to be called once, on the first infer().+ static Selector::Ptr find(std::size_t original_query_length, const ov::ISyncInferRequest& rq);+};+} // namespace host_flash_attention} // namespace runtimediff --git a/src/plugins/intel_npu/tests/unit/npuw/host_flash_attention_test.cpp b/src/plugins/intel_npu/tests/unit/npuw/host_flash_attention_test.cppindex d76771c5170..14e30a35037 100644--- a/src/plugins/intel_npu/tests/unit/npuw/host_flash_attention_test.cpp+++ b/src/plugins/intel_npu/tests/unit/npuw/host_flash_attention_test.cpp@@ -8,6 +8,8 @@#include <memory>#include <string>+#include <utility>+#include <vector>#include "common_test_utils/node_builders/constant.hpp"#include "npuw_transformations/detect_causal_mask.hpp"@@ -20,6 +22,7 @@#include "openvino/op/softmax.hpp"#include "openvino/runtime/icompiled_model.hpp"#include "openvino/runtime/iplugin.hpp"+#include "variable_state.hpp"namespace {@@ -757,19 +760,25 @@ public:}};-// Minimal ISyncInferRequest: find() only reads get_inputs(), so no tensors are needed.+// Minimal ISyncInferRequest: the find()s only read get_inputs() and query_state(), so no tensors are needed.class PosIdsFakeInferRequest final : public ov::ISyncInferRequest {public:- explicit PosIdsFakeInferRequest(const std::shared_ptr<const ov::ICompiledModel>& cm) : ov::ISyncInferRequest(cm) {}+ explicit PosIdsFakeInferRequest(const std::shared_ptr<const ov::ICompiledModel>& cm,+ std::vector<ov::SoPtr<ov::IVariableState>> states = {})+ : ov::ISyncInferRequest(cm),+ m_states(std::move(states)) {}void infer() override {}std::vector<ov::ProfilingInfo> get_profiling_info() const override {return {};}std::vector<ov::SoPtr<ov::IVariableState>> query_state() const override {- return {};+ return m_states;}void check_tensors() const override {}++private:+ std::vector<ov::SoPtr<ov::IVariableState>> m_states;};// Builds a model with a single "position_ids" parameter of the given shape (plus a dummy@@ -786,6 +795,26 @@ std::shared_ptr<ov::ISyncInferRequest> make_position_ids_request(const ov::Shapereturn std::make_shared<PosIdsFakeInferRequest>(compiled);}+// A request for a model with no position_ids input (like a LiteRT prefill graph), exposing the+// given variable states.+std::shared_ptr<ov::ISyncInferRequest> make_no_position_ids_request(std::vector<ov::SoPtr<ov::IVariableState>> states) {+ auto embeds = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::Shape{1, 16, 8});+ embeds->set_friendly_name("embeddings");+ embeds->output(0).get_tensor().set_names({"embeddings"});+ auto result = std::make_shared<ov::op::v0::Result>(embeds);+ auto model = std::make_shared<ov::Model>(ov::ResultVector{result}, ov::ParameterVector{embeds});++ auto plugin = std::make_shared<PosIdsNullPlugin>();+ auto compiled = std::make_shared<PosIdsStubCompiledModel>(model, plugin);+ return std::make_shared<PosIdsFakeInferRequest>(compiled, std::move(states));+}++ov::SoPtr<ov::IVariableState> make_history_state(int64_t value) {+ auto state = std::make_shared<ov::npuw::HistorySizeState>();+ state->set(value);+ return {state, nullptr};+}+} // namespaceTEST(HostFlashAttentionPositionIdsFindTest, Matches1D) {@@ -819,3 +848,59 @@ TEST(HostFlashAttentionPositionIdsFindTest, RejectsNonUnitBatchDimIn3D) {auto rq = make_position_ids_request(ov::Shape{3, 2, 16});EXPECT_EQ(ov::npuw::runtime::host_flash_attention::PositionIDs::find(16, *rq), nullptr);}++// ---------------------------------------------------------------------------+// runtime::host_flash_attention::HistorySize+// ---------------------------------------------------------------------------++TEST(HostFlashAttentionHistorySizeTest, PrefillWithoutHistoryHasNoPast) {+ ov::npuw::runtime::host_flash_attention::HistorySize selector(1024);+ selector.prepare(0);+ EXPECT_EQ(selector.this_case(), ov::npuw::runtime::host_flash_attention::Selector::Case::PREFILL);+ EXPECT_EQ(selector.context_length(), 1024);+}++TEST(HostFlashAttentionHistorySizeTest, PrefillRoundsHistoryUpToWholeChunks) {+ ov::npuw::runtime::host_flash_attention::HistorySize selector(1024);+ selector.prepare(1024);+ EXPECT_EQ(selector.context_length(), 1024 + 1024);+ // A partly filled last chunk (e.g. LiteRT-LM's 1023-token first chunk) still needs its whole tile.+ selector.prepare(1023);+ EXPECT_EQ(selector.context_length(), 1024 + 1024);+ selector.prepare(2100);+ EXPECT_EQ(selector.context_length(), 3 * 1024 + 1024);+}++TEST(HostFlashAttentionHistorySizeTest, FollowsEveryPrepare) {+ ov::npuw::runtime::host_flash_attention::HistorySize selector(16);+ selector.prepare(48);+ EXPECT_EQ(selector.context_length(), 48 + 16);+ selector.prepare(0); // a new conversation+ EXPECT_EQ(selector.context_length(), 16);+}++TEST(HostFlashAttentionHistorySizeFindTest, MatchesWhenHistoryIsSet) {+ auto rq = make_no_position_ids_request({make_history_state(0)});+ EXPECT_EQ(ov::npuw::runtime::host_flash_attention::PositionIDs::find(16, *rq), nullptr);+ auto selector = ov::npuw::runtime::host_flash_attention::HistorySize::find(16, *rq);+ ASSERT_NE(selector, nullptr);+ EXPECT_EQ(selector->this_case(), ov::npuw::runtime::host_flash_attention::Selector::Case::PREFILL);+}++// A host that never sets the history must get an error, not HFA silently running without the past.+TEST(HostFlashAttentionHistorySizeFindTest, RejectsUnsetHistory) {+ auto rq = make_no_position_ids_request({make_history_state(ov::npuw::HistorySizeState::kUnset)});+ EXPECT_EQ(ov::npuw::runtime::host_flash_attention::HistorySize::find(16, *rq), nullptr);+}++TEST(HostFlashAttentionHistorySizeFindTest, RejectsMissingHistoryState) {+ auto rq = make_no_position_ids_request({});+ EXPECT_EQ(ov::npuw::runtime::host_flash_attention::HistorySize::find(16, *rq), nullptr);+}++TEST(HostFlashAttentionHistorySizeTest, GenerateTakesHistoryAsIs) {+ ov::npuw::runtime::host_flash_attention::HistorySize selector(1);+ selector.prepare(37);+ EXPECT_EQ(selector.this_case(), ov::npuw::runtime::host_flash_attention::Selector::Case::GENERATE);+ EXPECT_EQ(selector.context_length(), 37 + 1);+}