Skip to content

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

[NPUW] Skip input bounds check for HFA function calls (#38451)

Alexis Menarde0fc3be851db
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.cpp
index ee1a41a2f47..ee9a73c67a3 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
@@ -1296,6 +1296,14 @@ bool has_compiled_state(const v1::subgraphs::CompiledPipeline& pipeline) {
get_compiled_hfa(pipeline.context) != nullptr;
}
+bool has_block_kv_hfa(const v1::subgraphs::CompiledPipeline& pipeline) {
+ if (!pipeline.runtime_behavior.has_value() || !pipeline.runtime_behavior->handles_function_prologue) {
+ return false;
+ }
+ const auto* hfa = get_compiled_hfa(pipeline.context);
+ return hfa != nullptr && hfa->is_block_mode();
+}
+
void serialize_compiled_state(v1::subgraphs::Context& context,
ov::npuw::s11n::Stream& stream,
const ov::npuw::s11n::SubmodelDeserializeCtx* submodel_ctx) {
diff --git a/src/plugins/intel_npu/src/plugin/npuw/attn/attn_subgraph.hpp b/src/plugins/intel_npu/src/plugin/npuw/attn/attn_subgraph.hpp
index 111f2f0080a..08ef8b873d5 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/attn/attn_subgraph.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/attn/attn_subgraph.hpp
@@ -45,6 +45,7 @@ ov::npuw::compiled::HostFlashAttention* get_compiled_hfa(v1::subgraphs::Context&
const ov::npuw::compiled::HostFlashAttention* get_compiled_hfa(const v1::subgraphs::Context& context);
bool has_compiled_state(const v1::subgraphs::CompiledPipeline& pipeline);
+bool has_block_kv_hfa(const v1::subgraphs::CompiledPipeline& pipeline);
void serialize_compiled_state(v1::subgraphs::Context& context,
ov::npuw::s11n::Stream& stream,
diff --git a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp
index 92fd3cc2b3e..427fe8f277c 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp
@@ -1071,7 +1071,8 @@ void ov::npuw::validate_submodel_indices(const Subgraph::Gather& host_gather,
std::size_t param_base,
std::size_t closure_size,
bool has_compiled_model,
- std::size_t n_model_inputs) {
+ std::size_t n_model_inputs,
+ bool skip_param_base_bound_check) {
if (!has_compiled_model) {
// No compiled model was loaded: all routing indices must be the disabled sentinel (-1).
auto require_disabled = [](int64_t idx, const char* field_name) {
@@ -1114,16 +1115,17 @@ void ov::npuw::validate_submodel_indices(const Subgraph::Gather& host_gather,
check_input_idx(quant_unpack_gather.src_s_idx, "quant_unpack_gather.src_s_idx");
check_input_idx(quant_unpack_gather.idx_idx, "quant_unpack_gather.idx_idx");
- // param_base + closure_size must not overflow compiled_model->inputs() (used in unpack_closure and funcall
- // prologue).
- OPENVINO_ASSERT(param_base <= n_model_inputs && closure_size <= n_model_inputs - param_base,
- "NPUW routing: param_base (",
- param_base,
- ") + closure_size (",
- closure_size,
- ") exceeds n_model_inputs (",
- n_model_inputs,
- ")");
+ // Block-KV HFA has one function param per KV block, more than its tile model's inputs.
+ if (!skip_param_base_bound_check) {
+ OPENVINO_ASSERT(param_base <= n_model_inputs && closure_size <= n_model_inputs - param_base,
+ "NPUW routing: param_base (",
+ param_base,
+ ") + closure_size (",
+ closure_size,
+ ") exceeds n_model_inputs (",
+ n_model_inputs,
+ ")");
+ }
}
void ov::npuw::CompiledModel::validate_submodels(const std::vector<CompiledModelDesc>& submodels) {
@@ -1154,13 +1156,15 @@ void ov::npuw::CompiledModel::validate_submodels(const std::vector<CompiledModel
const std::size_t closure_size = closure_desc.closure.size();
const bool has_compiled_model = static_cast<bool>(effective_compiled_model);
const std::size_t n_model_inputs = has_compiled_model ? effective_compiled_model->inputs().size() : 0u;
+ const bool skip_param_base_bound_check = ov::npuw::attn::has_block_kv_hfa(subm.pipeline);
validate_submodel_indices(subm.host_gather,
subm.quant_unpack_gather,
subm.param_base,
closure_size,
has_compiled_model,
- n_model_inputs);
+ n_model_inputs,
+ skip_param_base_bound_check);
}
}
@@ -1263,12 +1267,14 @@ void ov::npuw::CompiledModel::CompiledModelDesc::serialize(ov::npuw::s11n::Strea
if (is_fcall) {
validate_routing_self_consistency(host_gather, quant_unpack_gather, param_base, closure_size);
} else {
+ const bool skip_param_base_bound_check = ov::npuw::attn::has_block_kv_hfa(pipeline);
ov::npuw::validate_submodel_indices(host_gather,
quant_unpack_gather,
param_base,
closure_size,
static_cast<bool>(compiled_model),
- compiled_model ? compiled_model->inputs().size() : 0u);
+ compiled_model ? compiled_model->inputs().size() : 0u,
+ skip_param_base_bound_check);
}
};
diff --git a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp
index 0133c6d3ddf..c1b2d7ea8fe 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp
@@ -366,12 +366,16 @@ struct CompiledModelDescTestAccessor {
// Throws ov::Exception when an index would cause an out-of-bounds vector access
// in bind_global_params / unpack_closure at inference time.
// has_compiled_model: true when a compiled_model was loaded (enables input-bounds checks).
+// skip_param_base_bound_check: true when a runtime behavior (e.g. HostFlashAttention) owns the
+// function prologue and substitutes its own, differently-shaped compiled model, making the
+// generic param_base/closure_size vs n_model_inputs invariant inapplicable.
void validate_submodel_indices(const Subgraph::Gather& host_gather,
const Subgraph::QuantUnpackGather& quant_unpack_gather,
std::size_t param_base,
std::size_t closure_size,
bool has_compiled_model,
- std::size_t n_model_inputs);
+ std::size_t n_model_inputs,
+ bool skip_param_base_bound_check = false);
} // namespace npuw
} // namespace ov
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.hpp
index 0148bff98b7..744986c0c42 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
@@ -251,6 +251,12 @@ struct HostFlashAttention {
_final_tile_model_to_compile.reset(); // Free memory after compilation
}
+ // Past K/V split into per-block inputs by SplitKVCacheIntoBlocks.
+ bool is_block_mode() const {
+ const auto& indices = _sdpa_attention_info._sdpa_indices;
+ return indices.past_key_blocks.size() > 1 && indices.past_value_blocks.size() > 1;
+ }
+
bool is_valid() const;
};
diff --git a/src/plugins/intel_npu/tests/unit/npuw/serialization.cpp b/src/plugins/intel_npu/tests/unit/npuw/serialization.cpp
index 0f3f44ef8b6..308bf257c79 100644
--- a/src/plugins/intel_npu/tests/unit/npuw/serialization.cpp
+++ b/src/plugins/intel_npu/tests/unit/npuw/serialization.cpp
@@ -13,6 +13,7 @@
#include <system_error>
#include "attention.hpp"
+#include "attn/attn_subgraph.hpp"
#include "common_test_utils/file_utils.hpp"
#include "common_test_utils/test_assertions.hpp"
#include "compiled_model.hpp"
@@ -2044,6 +2045,37 @@ TEST(SerializationTest, NoCompiledModelNonSentinelQuantDstIdxFails) {
EXPECT_THROW(expect_serialize_throws({-1, -1, -1}, {0, -1, -1, -1, -1}, 0, 0, 0), ov::Exception);
}
+TEST(SerializationTest, BlockKVHFAAllowsSmallerCompiledModel) {
+ auto plugin = std::make_shared<NullPlugin>();
+ auto model = make_validation_model(7);
+ auto submodel = ov::npuw::CompiledModelDescTestAccessor::make();
+ ov::npuw::CompiledModelDescTestAccessor::compiled_model(submodel) =
+ ov::SoPtr<ov::ICompiledModel>{std::make_shared<MockSubCompiledModel>(model, plugin, ov::AnyMap{}), {}};
+ ov::npuw::CompiledModelDescTestAccessor::param_base(submodel) = 18;
+
+ ov::npuw::CompiledModelDescTestAccessor::SubmodelVec submodels;
+ submodels.push_back(std::move(submodel));
+ EXPECT_THROW(ov::npuw::CompiledModelDescTestAccessor::validate_submodels(submodels), ov::Exception);
+
+ ov::npuw::v1::subgraphs::RuntimeBehaviorSpec behavior;
+ behavior.handles_function_prologue = true;
+ submodels[0].pipeline.runtime_behavior = std::move(behavior);
+ // Other prologue-owning behaviors (Dynamic/Pyramid attention, MoE) still get the check.
+ EXPECT_THROW(ov::npuw::CompiledModelDescTestAccessor::validate_submodels(submodels), ov::Exception);
+
+ auto hfa = std::make_shared<ov::npuw::compiled::HostFlashAttention>();
+ auto& sdpa_indices = hfa->_sdpa_attention_info._sdpa_indices;
+ sdpa_indices.past_key_blocks = {0};
+ sdpa_indices.past_value_blocks = {1};
+ ov::npuw::attn::put_compiled_hfa(submodels[0].pipeline.context, hfa);
+ // Regular HFA has a single past K/V input each and still gets the check.
+ EXPECT_THROW(ov::npuw::CompiledModelDescTestAccessor::validate_submodels(submodels), ov::Exception);
+
+ sdpa_indices.past_key_blocks = {0, 2};
+ sdpa_indices.past_value_blocks = {1, 3};
+ EXPECT_NO_THROW(ov::npuw::CompiledModelDescTestAccessor::validate_submodels(submodels));
+}
+
TEST(SerializationTest, FuncallSubmodelValidHostGatherPasses) {
auto plugin = std::make_shared<NullPlugin>();
auto model = make_validation_model(8);