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] Fix "Cannot find tensor for port" for pyramid attention after CACHE_DIR import (#38629)
VijayabaskarR-06972aa05b9b62
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 ee9a73c67a3..94b611c122e 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@@ -600,7 +600,6 @@ ov::npuw::v1::subgraphs::RuntimeBehaviorFactory make_runtime_factory() {// KV block parameters, shifting local indices relative to input_idx// (which is always a global/original-model index here).const bool is_mask = (input_idx == pyramid->global_mask_idx);- const auto& iport = compiled_model->inputs()[input_idx];if (pyramid->is_block_mode()) {// Block KV mode: bind block tensors directly to variant ports;// mask and other inputs fall through to the non-KV handling below.@@ -634,20 +633,19 @@ ov::npuw::v1::subgraphs::RuntimeBehaviorFactory make_runtime_factory() {// and retrieve its sequence dimension via the virtual interface.const auto dim_opt = pyramid->kv_param_dim(pyramid_id, input_idx);const bool is_kv_param = dim_opt.has_value();+ // ctx.target_request was created from _compiled_models[pyramid_id], so it must+ // be addressed with that model's own ports. A port of the main compiled model is+ // only resolved by a friendly/tensor name match, which is not guaranteed once the+ // variants are imported from a cache as separate blobs. Contiguous variants keep+ // the main model's parameter order, so input_idx is a valid local index. For the+ // last pyramid model _compiled_models[pyramid_id] == the main compiled model.+ const auto& pyramid_iport = pyramid->_compiled_models[pyramid_id]->inputs()[input_idx];if (is_mask) {// Mask requires context-dependent construction — defer to prologue()io.inputs.at(input_idx) = tensor;} else if (is_kv_param) {const auto dim = dim_opt.value();using namespace ov::npuw::runtime;- // iport comes from the main compiled model (full KV shape).- // For set_tensor we must use the port from the pyramid model's compiled- // model so the zero backend shape check passes. For the last pyramid model- // _compiled_models[pyramid_id] == the main compiled model, so pyramid_iport- // == iport and there is no behavioural difference.- // get_tensor calls below intentionally keep iport: they look up by index /- // name without strict shape validation.- const auto& pyramid_iport = pyramid->_compiled_models[pyramid_id]->inputs()[input_idx];if (state.pyramid_selector->length() == -1) {// Fallback: dynamic range not identified — bind directlyctx.target_request->set_tensor(pyramid_iport, tensor);@@ -676,12 +674,12 @@ ov::npuw::v1::subgraphs::RuntimeBehaviorFactory make_runtime_factory() {const auto& view = ov::npuw::util::view(tensor, dim, 0, effective_past_len);const auto& shape = view->get_shape();if (ov::shape_size(shape) == 0) {- ctx.target_request->get_tensor(iport)->set_shape(shape);+ ctx.target_request->get_tensor(pyramid_iport)->set_shape(shape);} else if (use_tensor_view) {LOG_DEBUG("Use tensor view: past_len=" << effective_past_len);ctx.target_request->set_tensor(pyramid_iport, view);} else {- const auto& dst = ctx.target_request->get_tensor(iport);+ const auto& dst = ctx.target_request->get_tensor(pyramid_iport);ov::npuw::util::copy_tensor_by_dim(view,dst,static_cast<uint32_t>(dim),@@ -691,7 +689,7 @@ ov::npuw::v1::subgraphs::RuntimeBehaviorFactory make_runtime_factory() {} else {NPUW_ASSERT(this_case == pyramid_attention::Selector::Case::GENERATE);NPUW_ASSERT(static_cast<int64_t>(input_shape[dim]) != past_len);- const auto& dst = ctx.target_request->get_tensor(iport);+ const auto& dst = ctx.target_request->get_tensor(pyramid_iport);if (dst->get_shape() == input_shape) {ctx.target_request->set_tensor(pyramid_iport, tensor);} else if (use_tensor_view) {@@ -717,7 +715,7 @@ ov::npuw::v1::subgraphs::RuntimeBehaviorFactory make_runtime_factory() {}} else {// Non-KV, non-mask: bind directly- ctx.target_request->set_tensor(iport, tensor);+ ctx.target_request->set_tensor(pyramid_iport, tensor);}}return true;@@ -743,10 +741,27 @@ ov::npuw::v1::subgraphs::RuntimeBehaviorFactory make_runtime_factory() {bool bind_function_output(ov::npuw::v1::subgraphs::InferContext& ctx,std::size_t output_idx,const ov::SoPtr<ov::ITensor>& tensor) override {- (void)ctx;- (void)output_idx;- (void)tensor;- return false;+ if (m_kind != BehaviorKind::Pyramid) {+ return false;+ }+ const auto& pipeline = get_subgraph_pipeline(ctx, ctx.real_subgraph_idx);+ const auto* pyramid = ov::npuw::attn::get_compiled_pyramid(pipeline.context);+ if (pyramid == nullptr) {+ return false;+ }+ // The active subrequest is a pyramid variant: bind its own output port rather than+ // the main compiled model's one (see bind_function_input for the rationale).+ // Pyramid variants differ only in the past KV length, so their outputs have the+ // same shape and element type as the main model's ones.+ auto& state = get_runtime_state(ctx);+ ensure_pyramid_selector(ctx, state);+ const auto pyramid_id = state.pyramid_selector->pyramid_id();+ const auto& pyramid_oport = pyramid->_compiled_models[pyramid_id]->outputs()[output_idx];+ NPUW_ASSERT(pyramid_oport.get_partial_shape() == ov::PartialShape(tensor->get_shape()) &&+ pyramid_oport.get_element_type() == tensor->get_element_type() &&+ "Pyramid attention variant output must match the function output");+ ctx.target_request->set_tensor(pyramid_oport, tensor);+ return true;}void prologue(ov::npuw::v1::subgraphs::InferContext& ctx) override {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.cppindex 065cce5e02e..d1177bc80f6 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@@ -4,6 +4,7 @@#include <gtest/gtest.h>+#include <algorithm>#include <any>#include <atomic>#include <cstring>@@ -16,10 +17,14 @@#define private public#include "compiled_model.hpp"#undef private+#include "attn/attn_subgraph.hpp"+#include "embedding/redirect_new_kv_to_output.hpp"#include "just_sync_infer_request.hpp"#include "llm_test_helpers.hpp"#include "model_builder.hpp"+#include "npuw_transformations/optimize_value_tensors.hpp"#include "partitioning/patterns/sdpa.hpp"+#include "pyramid_attention.hpp"#include "unfold_sync_infer_request.hpp"#include "openvino/op/scaled_dot_product_attention.hpp"#include "openvino/openvino.hpp"@@ -152,9 +157,11 @@ public:void infer() override;ov::SoPtr<ov::ITensor> get_tensor(const ov::Output<const ov::Node>& port) const override {+ check_port(port);return ov::ISyncInferRequest::get_tensor(port);}void set_tensor(const ov::Output<const ov::Node>& port, const ov::SoPtr<ov::ITensor>& tensor) override {+ check_port(port);ov::ISyncInferRequest::set_tensor(port, tensor);}void check_tensors() const override {}@@ -164,6 +171,9 @@ public:std::vector<ov::ProfilingInfo> get_profiling_info() const override {return {};}++private:+ void check_port(const ov::Output<const ov::Node>& port) const;};class FakeSubAsyncInferRequest final : public ov::IAsyncInferRequest {@@ -247,9 +257,16 @@ private:class FakeSubCompiledModel final : public ov::ICompiledModel {public:- FakeSubCompiledModel(const std::shared_ptr<ov::Model>& model, const std::shared_ptr<const ov::IPlugin>& plugin)+ FakeSubCompiledModel(const std::shared_ptr<ov::Model>& model,+ const std::shared_ptr<const ov::IPlugin>& plugin,+ bool own_ports_only = false): ov::ICompiledModel(model, plugin, nullptr, nullptr),- m_model(model) {}+ m_model(model),+ m_own_ports_only(own_ports_only) {}++ bool own_ports_only() const {+ return m_own_ports_only;+ }void export_model(std::ostream&) const override {}std::shared_ptr<const ov::Model> get_runtime_model() const override {@@ -272,8 +289,25 @@ public:private:std::shared_ptr<ov::Model> m_model;+ bool m_own_ports_only = false;};+// When the compiled submodels are imported from a cache, each blob gets its own I/O nodes+// rebuilt from the blob metadata, so a port of one compiled model is not guaranteed to be+// resolved by an infer request created from another one. own_ports_only models exactly that.+void FakeSubInferRequest::check_port(const ov::Output<const ov::Node>& port) const {+ const auto& compiled_model = get_compiled_model();+ if (!std::static_pointer_cast<const FakeSubCompiledModel>(compiled_model)->own_ports_only()) {+ return;+ }+ const auto& inputs = compiled_model->inputs();+ const auto& outputs = compiled_model->outputs();+ OPENVINO_ASSERT(std::find(inputs.begin(), inputs.end(), port) != inputs.end() ||+ std::find(outputs.begin(), outputs.end(), port) != outputs.end(),+ "Cannot find tensor for port ",+ port);+}+FakeSubInferRequest::FakeSubInferRequest(std::shared_ptr<const FakeSubCompiledModel> compiled_model): ov::ISyncInferRequest(std::move(compiled_model)) {for (const auto& input : get_compiled_model()->inputs()) {@@ -315,7 +349,8 @@ protected:props["NPUW_UNFOLD_IREQS"] = "YES";return props;}- std::shared_ptr<testing::NiceMock<ov::MockICore>> make_core(const std::shared_ptr<const ov::IPlugin>& plugin) const {+ std::shared_ptr<testing::NiceMock<ov::MockICore>> make_core(const std::shared_ptr<const ov::IPlugin>& plugin,+ bool own_ports_only = false) const {auto core = std::make_shared<testing::NiceMock<ov::MockICore>>();ON_CALL(*core, get_supported_property(testing::_, testing::_, testing::_))@@ -358,9 +393,13 @@ protected:compile_model(testing::Matcher<const std::shared_ptr<const ov::Model>&>(testing::_),testing::Matcher<const std::string&>(testing::StrEq("CPU")),testing::Matcher<const ov::AnyMap&>(testing::_)))- .WillByDefault([plugin](const std::shared_ptr<const ov::Model>& submodel, const std::string&, const ov::AnyMap&) {- return ov::SoPtr<ov::ICompiledModel>{std::make_shared<FakeSubCompiledModel>(- std::const_pointer_cast<ov::Model>(submodel), plugin)};+ .WillByDefault([plugin, own_ports_only](const std::shared_ptr<const ov::Model>& submodel,+ const std::string&,+ const ov::AnyMap&) {+ return ov::SoPtr<ov::ICompiledModel>{+ std::make_shared<FakeSubCompiledModel>(std::const_pointer_cast<ov::Model>(submodel),+ plugin,+ own_ports_only)};});return core;@@ -527,4 +566,47 @@ TEST_F(SubgraphBehaviorInferTest, DynAttnBehaviorNotAttachedWithoutAttnIsolation<< "DynAttnBehavior must NOT be attached when NPUW_ONLINE_ISOLATE=ATTN is not set";}+// Regression test for "Cannot find tensor for port" on the first inference of an LLM imported+// from CACHE_DIR with chunked prefill (PYRAMID prefill attention). The pyramid variants are+// separate compiled models with their own infer requests; every port bound on a variant request+// must come from that variant's compiled model, not from the main (last) one.+TEST_F(SubgraphBehaviorInferTest, PyramidBehaviorBindsOnlyActiveVariantPorts) {+ auto model = ov::test::npuw::build_dynamic_attention_llm_model();+ // Same transformations as the chunked LLM prefill pipeline: decompose SDPA so the PYRAMID+ // pattern can match, and output the new chunk KV only.+ ov::npuw::util::OptimizeValueTensors(true).run_on_model(model);+ ov::npuw::RedirectNewKvToOutput().run_on_model(model);++ auto plugin = std::make_shared<TestPlugin>();+ auto core = make_core(plugin, /*own_ports_only=*/true);+ plugin->set_core(core);++ auto props = base_props();+ props["NPUW_ATTN"] = std::string("PYRAMID");+ // No Failsafe wrappers - they are not created for imported blobs either.+ props["NPUW_FALLBACK_EXEC"] = std::string("NO");+ auto compiled = std::make_shared<ov::npuw::CompiledModel>(model, plugin, props);++ std::size_t num_pyramid_models = 0u;+ for (const auto& desc : compiled->m_compiled_submodels) {+ if (const auto* pyramid = ov::npuw::attn::get_compiled_pyramid(desc.pipeline.context)) {+ num_pyramid_models = std::max(num_pyramid_models, pyramid->_compiled_models.size());+ }+ }+ ASSERT_GT(num_pyramid_models, 1u) << "The test model must produce more than one pyramid attention variant";++ auto request = compiled->create_infer_request();+ ASSERT_NE(request, nullptr);+ 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")) {+ // A first chunk without history selects the smallest (non-main) pyramid variant+ 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));+ }+ EXPECT_NO_THROW(request->infer());+}+} // namespace