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
CVS-195132: Fix the infer crash caused by attention mask size mismatch with generate var (#38267)
yzhou515b769d61cf20
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.cppindex d03ced2b535..d8f46f52580 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@@ -1682,9 +1682,50 @@ void ov::npuw::LLMInferRequest::infer_generate(ov::SoPtr<ov::ITensor> input_ids,// kv layers) and the set of "1" units of number of previously calculated// tokens on the left (for past kv layers).auto kv_attn_mask = m_kvcache_request->get_tensor(m_kvcache_in_ports.at(layer_names::attention_mask));- std::copy_n(attention_mask->data<int64_t>(),- attention_mask->get_size() - input_tokens_len,- kv_attn_mask->data<int64_t>());+ // NB: Past length comes from the tracked KV-cache, not the caller mask size: the caller mask+ // may be a static max-length buffer (e.g. WebNN) rather than past_len + input_tokens_len.+ OPENVINO_ASSERT(kv_attn_mask->get_size() >= kvcache_desc.max_generation_token_len,+ "Generate attention mask is smaller than the configured generation window.");+ const size_t past_len = kvcache_desc.num_stored_tokens;+ const size_t dest_past_capacity = kv_attn_mask->get_size() - kvcache_desc.max_generation_token_len;+ OPENVINO_ASSERT(past_len <= dest_past_capacity,+ "Selected generate variant cannot hold the stored attention context: ",+ past_len,+ " past tokens exceed capacity ",+ dest_past_capacity,+ ".");+ OPENVINO_ASSERT(attention_mask->get_size() >= past_len,+ "Attention mask does not cover the stored past: expected at least ",+ past_len,+ " entries, got ",+ attention_mask->get_size(),+ ".");+ const size_t used_mask_len = past_len + input_tokens_len;+ if (attention_mask->get_size() > used_mask_len) {+ // Static max-length caller mask (e.g. WebNN): layout is [past][current][padding],+ // so the ignored tail must not carry any valid context.+ const auto* mask_data = attention_mask->data<int64_t>();+ const auto tail_begin = mask_data + used_mask_len;+ const auto tail_end = mask_data + attention_mask->get_size();+ const auto valid_it = std::find_if(tail_begin, tail_end, [](int64_t v) {+ return v != 0;+ });+ OPENVINO_ASSERT(valid_it == tail_end,+ "Static attention mask has a valid entry at position ",+ static_cast<size_t>(valid_it - mask_data),+ " beyond the tracked context of ",+ used_mask_len,+ " tokens (",+ past_len,+ " past + ",+ input_tokens_len,+ " current); truncating it would drop context.");+ }+ std::copy_n(attention_mask->data<int64_t>(), past_len, kv_attn_mask->data<int64_t>());+ // Clear stale entries left by a larger past (e.g. after speculative-decoding trim).+ if (past_len < dest_past_capacity) {+ std::fill_n(kv_attn_mask->data<int64_t>() + past_len, dest_past_capacity - past_len, 0);+ }if (input_tokens_len < kvcache_desc.max_generation_token_len) {std::fill_n(kv_attn_mask->data<int64_t>() + kv_attn_mask->get_size() - kvcache_desc.max_generation_token_len,diff --git a/src/plugins/intel_npu/tests/unit/npuw/llm_infer_request_variant_switch_test.cpp b/src/plugins/intel_npu/tests/unit/npuw/llm_infer_request_variant_switch_test.cppindex 27cc4f71081..9a160df82af 100644--- a/src/plugins/intel_npu/tests/unit/npuw/llm_infer_request_variant_switch_test.cpp+++ b/src/plugins/intel_npu/tests/unit/npuw/llm_infer_request_variant_switch_test.cpp@@ -42,11 +42,22 @@ struct LLMVariantSwitchTestAccess {return compiled->m_is_block_kv_cache;}- static void set_num_stored_tokens(const std::shared_ptr<ov::npuw::LLMCompiledModel>& compiled,- uint32_t num_tokens) {+ static uint32_t max_generation_token_len(const std::shared_ptr<ov::npuw::LLMCompiledModel>& compiled) {+ return compiled->m_kvcache_desc.max_generation_token_len;+ }++ static void set_num_stored_tokens(const std::shared_ptr<ov::npuw::LLMCompiledModel>& compiled, uint32_t num_tokens) {compiled->m_kvcache_desc.num_stored_tokens = num_tokens;}+ static void set_num_stored_tokens(ov::npuw::LLMInferRequest& req, uint32_t num_tokens) {+ req.m_npuw_llm_compiled_model->m_kvcache_desc.num_stored_tokens = num_tokens;+ }++ static void set_total_size(ov::npuw::LLMInferRequest& req, uint32_t total_size) {+ req.m_npuw_llm_compiled_model->m_kvcache_desc.total_size = total_size;+ }+static void set_kv_dim(const std::shared_ptr<ov::npuw::LLMCompiledModel>& compiled, uint32_t dim) {compiled->m_kvcache_desc.dim = dim;}@@ -115,6 +126,18 @@ struct LLMVariantSwitchTestAccess {return req.m_kvcache_request;}+ static void generate(ov::npuw::LLMInferRequest& req,+ ov::SoPtr<ov::ITensor> input_ids,+ ov::SoPtr<ov::ITensor> attention_mask,+ ov::SoPtr<ov::ITensor> position_ids) {+ req.m_generate_initialized = true;+ req.infer_generate(std::move(input_ids), std::move(attention_mask), std::move(position_ids), {});+ }++ static ov::SoPtr<ov::ITensor> generate_attention_mask(const ov::npuw::LLMInferRequest& req) {+ return req.m_kvcache_request->get_tensor(req.m_kvcache_in_ports.at("attention_mask"));+ }+static void set_kvcache_sizes(const std::shared_ptr<ov::npuw::LLMCompiledModel>& compiled,std::vector<uint32_t> sizes) {compiled->m_kvcache_sizes = std::move(sizes);@@ -599,6 +622,54 @@ TEST_F(LLMInferRequestVariantSwitchTest, RejectsImportedKvcacheDimOutOfPrefillTeEXPECT_THROW(LLMVariantSwitchTestAccess::validate_imported_kvcache_dim(compiled), ov::Exception);}+TEST_F(LLMInferRequestVariantSwitchTest, GenerateBuildsInternalMaskFromStaticCallerMaskAndClearsTrimmedPast) {+ VariantSwitchFactory factory;+ auto compiled = create_compiled_model({}, factory);+ ASSERT_NE(compiled, nullptr);++ ov::npuw::LLMInferRequest req(compiled);+ auto internal_mask = LLMVariantSwitchTestAccess::generate_attention_mask(req);+ const auto max_generation_len = LLMVariantSwitchTestAccess::max_generation_token_len(compiled);+ ASSERT_GE(internal_mask->get_size(), max_generation_len);+ const size_t dest_past_capacity = internal_mask->get_size() - max_generation_len;+ ASSERT_GT(dest_past_capacity, 3u);++ auto input_ids = ov::make_tensor(ov::element::i64, ov::Shape{1, 1});+ input_ids->data<int64_t>()[0] = 42;+ auto position_ids = ov::make_tensor(ov::element::i64, ov::Shape{1, 1});+ position_ids->data<int64_t>()[0] = 3;+ auto static_attention_mask = ov::make_tensor(ov::element::i64, ov::Shape{1, 64});+ ASSERT_GT(static_attention_mask->get_size(), 4u);+ std::fill_n(static_attention_mask->data<int64_t>(), static_attention_mask->get_size(), 0);+ std::fill_n(static_attention_mask->data<int64_t>(), 4u, 1);+ static_attention_mask->data<int64_t>()[1] = 0;++ LLMVariantSwitchTestAccess::set_num_stored_tokens(req, 3u);+ LLMVariantSwitchTestAccess::set_total_size(req, 4u);+ LLMVariantSwitchTestAccess::generate(req, input_ids, static_attention_mask, position_ids);++ EXPECT_EQ(internal_mask->data<int64_t>()[0], 1);+ EXPECT_EQ(internal_mask->data<int64_t>()[1], 0);+ EXPECT_EQ(internal_mask->data<int64_t>()[2], 1);+ for (size_t i = 3u; i < dest_past_capacity; ++i) {+ EXPECT_EQ(internal_mask->data<int64_t>()[i], 0);+ }+ for (size_t i = internal_mask->get_size() - max_generation_len; i < internal_mask->get_size() - 1u; ++i) {+ EXPECT_EQ(internal_mask->data<int64_t>()[i], 0);+ }+ EXPECT_EQ(internal_mask->data<int64_t>()[internal_mask->get_size() - 1u], 1);++ LLMVariantSwitchTestAccess::set_num_stored_tokens(req, 1u);+ LLMVariantSwitchTestAccess::set_total_size(req, 2u);+ std::fill_n(static_attention_mask->data<int64_t>() + 2u, static_attention_mask->get_size() - 2u, 0);+ LLMVariantSwitchTestAccess::generate(req, input_ids, static_attention_mask, position_ids);++ EXPECT_EQ(internal_mask->data<int64_t>()[0], 1);+ for (size_t i = 1u; i < dest_past_capacity; ++i) {+ EXPECT_EQ(internal_mask->data<int64_t>()[i], 0);+ }+}+// m_prefill_other_outs_seqdims is keyed by prefill output ports, which can't be serialized// directly. serialize() exports the table by tensor name and deserialize() rebuilds the port keys// against the restored prefill model. This pins that name<->port round-trip through the real