Skip to content

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

[NPUW] 1st token generation profiling (#34147)

Dylan Neve04293e614982
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.cpp
index c25ec5aee58..56de158d444 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
@@ -220,6 +220,9 @@ ov::npuw::LLMInferRequest::LLMInferRequest(const std::shared_ptr<ov::npuw::LLMCo
}
m_generate_initialized = false;
+
+ m_llm_profile.report_on_die = ov::npuw::profiling_enabled();
+ m_llm_profile.area = "LLM/execution";
}
std::string ov::npuw::LLMInferRequest::init_pre_alloc_device() {
@@ -463,6 +466,7 @@ void ov::npuw::LLMInferRequest::prepare_for_new_conversation() {
void ov::npuw::LLMInferRequest::prepare_for_new_conversation(int64_t prompt_length) {
namespace uu = ov::npuw::util;
+
uu::fill_tensor_bytes(m_prefill_request->get_tensor(m_prefill_in_ports.at(m_input_ids_name)), 0u);
if (auto type_ids_port = m_prefill_in_ports.find(layer_names::token_type_ids);
type_ids_port != m_prefill_in_ports.end()) {
@@ -483,8 +487,6 @@ void ov::npuw::LLMInferRequest::prepare_for_new_conversation(int64_t prompt_leng
m_kvcache_request = select_generate_request(prompt_length);
m_kvcache_in_ports = m_generate_variant_in_ports.at(m_kvcache_request);
m_kvcache_out_ports = m_generate_variant_out_ports.at(m_kvcache_request);
-
- apply_lora();
}
void ov::npuw::LLMInferRequest::copy_kvcache() {
@@ -657,86 +659,92 @@ void ov::npuw::LLMInferRequest::infer_chunked_prefill(ov::SoPtr<ov::ITensor> inp
// the chunk size
auto current_prompts_len = std::min(remaining_prompts, chunk_prompt_len);
- // Handle first chunk with prefix caching: populate attention mask for restored cache
- if (enable_prefix_caching && cache_context.restore_prefix_cache) {
- m_prefix_caching_helper->populate_attention_mask_for_restored_cache(attention_mask,
- attn_mask_in_tensor,
- kvcache_desc.num_stored_tokens);
- cache_context.restore_prefix_cache = false;
- }
-
- // Populate the attention mask for the present chunk
- // For the already processed tokens, they will be added into the attention mask after inference call
- size_t last_chunk_offset = attn_mask_in_tensor->get_size() - chunk_prompt_len;
- if (current_prompts_len < chunk_prompt_len) {
- // We will populate current_prompts_len on the right side of attention mask for the processing tokens
- // If the current prompt length is smaller than the chunk prompt length,
- // clear the last chunk of the attention mask to ensure non-relevant tokens are masked
- ov::npuw::util::fill_tensor<int64_t>(attn_mask_in_tensor, 0, last_chunk_offset);
- }
-
- std::copy_n(attention_mask->data<int64_t>() + kvcache_desc.num_stored_tokens,
- current_prompts_len,
- attn_mask_in_tensor->data<int64_t>() + attn_mask_in_tensor->get_size() - current_prompts_len);
-
- auto current_prefill_bytes = current_prompts_len * input_ids_elem_size;
- auto prefilled_bytes = kvcache_desc.num_stored_tokens * input_ids_elem_size;
- if (is_input_embeds) {
- current_prefill_bytes *= input_ids->get_shape().back();
- prefilled_bytes *= input_ids->get_shape().back();
- }
+ m_llm_profile["1/prefill:3a.prepare_chunk"].record([&]() {
+ // Handle first chunk with prefix caching: populate attention mask for restored cache
+ if (enable_prefix_caching && cache_context.restore_prefix_cache) {
+ m_prefix_caching_helper->populate_attention_mask_for_restored_cache(attention_mask,
+ attn_mask_in_tensor,
+ kvcache_desc.num_stored_tokens);
+ cache_context.restore_prefix_cache = false;
+ }
- ov::npuw::util::fill_tensor_bytes(input_ids_in_tensor, 0u);
- std::copy_n(reinterpret_cast<uint8_t*>(input_ids->data()) + prefilled_bytes,
- current_prefill_bytes,
- reinterpret_cast<uint8_t*>(input_ids_in_tensor->data()) + input_ids_in_tensor->get_byte_size() -
- current_prefill_bytes);
-
- // NB: Regular LLM uses 2D position_ids [BATCH, SEQ_LEN], Qwen2.5 VL/Omni uses 3D position_ids [3, BATCH,
- // SEQ_LEN]
- // Copy postion ids with considering the 3D position_ids
- auto last_dim = position_ids->get_shape().size() - 1;
- auto actual_position_ids_slice = ov::npuw::util::make_tensor_slice(
- position_ids,
- static_cast<uint32_t>(last_dim),
- kvcache_desc.num_stored_tokens,
- kvcache_desc.num_stored_tokens + static_cast<uint32_t>(current_prompts_len));
-
- auto pos_ids_slice =
- ov::npuw::util::make_tensor_slice(pos_ids_in_tensor,
- static_cast<uint32_t>(last_dim),
- static_cast<uint32_t>(chunk_prompt_len - current_prompts_len),
- static_cast<uint32_t>(chunk_prompt_len));
-
- // Copy with proper stride handling
- actual_position_ids_slice->copy_to(pos_ids_slice._ptr);
+ // Populate the attention mask for the present chunk
+ // For the already processed tokens, they will be added into the attention mask after inference call
+ size_t last_chunk_offset = attn_mask_in_tensor->get_size() - chunk_prompt_len;
+ if (current_prompts_len < chunk_prompt_len) {
+ // We will populate current_prompts_len on the right side of attention mask for the processing tokens
+ // If the current prompt length is smaller than the chunk prompt length,
+ // clear the last chunk of the attention mask to ensure non-relevant tokens are masked
+ ov::npuw::util::fill_tensor<int64_t>(attn_mask_in_tensor, 0, last_chunk_offset);
+ }
- if (m_eagle3_ext.is_eagle3_model()) {
- m_eagle3_ext.prepare_inputs_for_chunk(m_prefill_request,
- m_prefill_in_ports,
- kvcache_desc.num_stored_tokens,
- static_cast<uint32_t>(current_prompts_len));
- }
+ std::copy_n(attention_mask->data<int64_t>() + kvcache_desc.num_stored_tokens,
+ current_prompts_len,
+ attn_mask_in_tensor->data<int64_t>() + attn_mask_in_tensor->get_size() - current_prompts_len);
- // Update history size for dynamic context:
- // dynamic attention selector needs history size to determin the past KV shape and attention mask shape
- m_prefill_base_request->update_history_size(kvcache_desc.num_stored_tokens);
+ auto current_prefill_bytes = current_prompts_len * input_ids_elem_size;
+ auto prefilled_bytes = kvcache_desc.num_stored_tokens * input_ids_elem_size;
+ if (is_input_embeds) {
+ current_prefill_bytes *= input_ids->get_shape().back();
+ prefilled_bytes *= input_ids->get_shape().back();
+ }
- m_prefill_request->infer();
+ ov::npuw::util::fill_tensor_bytes(input_ids_in_tensor, 0u);
+ std::copy_n(reinterpret_cast<uint8_t*>(input_ids->data()) + prefilled_bytes,
+ current_prefill_bytes,
+ reinterpret_cast<uint8_t*>(input_ids_in_tensor->data()) + input_ids_in_tensor->get_byte_size() -
+ current_prefill_bytes);
+
+ // NB: Regular LLM uses 2D position_ids [BATCH, SEQ_LEN], Qwen2.5 VL/Omni uses 3D position_ids
+ // [3, BATCH, SEQ_LEN]
+ // Copy postion ids with considering the 3D position_ids
+ auto last_dim = position_ids->get_shape().size() - 1;
+ auto actual_position_ids_slice = ov::npuw::util::make_tensor_slice(
+ position_ids,
+ static_cast<uint32_t>(last_dim),
+ kvcache_desc.num_stored_tokens,
+ kvcache_desc.num_stored_tokens + static_cast<uint32_t>(current_prompts_len));
+
+ auto pos_ids_slice =
+ ov::npuw::util::make_tensor_slice(pos_ids_in_tensor,
+ static_cast<uint32_t>(last_dim),
+ static_cast<uint32_t>(chunk_prompt_len - current_prompts_len),
+ static_cast<uint32_t>(chunk_prompt_len));
+
+ // Copy with proper stride handling
+ actual_position_ids_slice->copy_to(pos_ids_slice._ptr);
+
+ if (m_eagle3_ext.is_eagle3_model()) {
+ m_eagle3_ext.prepare_inputs_for_chunk(m_prefill_request,
+ m_prefill_in_ports,
+ kvcache_desc.num_stored_tokens,
+ static_cast<uint32_t>(current_prompts_len));
+ }
- // Accumulate Eagle3 last_hidden_state from this chunk
- if (m_eagle3_ext.is_eagle3_model()) {
- m_eagle3_ext.accumulate_chunk_last_hidden_state(m_prefill_request,
- m_prefill_out_ports,
- static_cast<uint32_t>(current_prompts_len),
- static_cast<uint32_t>(input_prompt_len));
- }
+ // Update history size for dynamic context:
+ // dynamic attention selector needs history size to determin the past KV shape and attention mask shape
+ m_prefill_base_request->update_history_size(kvcache_desc.num_stored_tokens);
+ });
+
+ m_llm_profile["1/prefill:3b.infer"].record([&]() {
+ m_prefill_request->infer();
+ });
+
+ m_llm_profile["1/prefill:3c.post_chunk"].record([&]() {
+ // Accumulate Eagle3 last_hidden_state from this chunk
+ if (m_eagle3_ext.is_eagle3_model()) {
+ m_eagle3_ext.accumulate_chunk_last_hidden_state(m_prefill_request,
+ m_prefill_out_ports,
+ static_cast<uint32_t>(current_prompts_len),
+ static_cast<uint32_t>(input_prompt_len));
+ }
- if (enable_prefix_caching) {
- m_prefix_caching_helper->store_computed_blocks(current_prompts_len,
- cache_context.prompt_hashes,
- cache_context.token_idx);
- }
+ if (enable_prefix_caching) {
+ m_prefix_caching_helper->store_computed_blocks(current_prompts_len,
+ cache_context.prompt_hashes,
+ cache_context.token_idx);
+ }
+ });
remaining_prompts -= current_prompts_len;
kvcache_desc.num_stored_tokens += static_cast<uint32_t>(current_prompts_len);
@@ -748,17 +756,19 @@ void ov::npuw::LLMInferRequest::infer_chunked_prefill(ov::SoPtr<ov::ITensor> inp
break;
}
- // Copy calculated key/values chunk from present k/v layer to past k/v layer for storage
- update_kvcache_for(m_prefill_request,
- m_prefill_in_ports,
- m_prefill_out_ports,
- static_cast<uint32_t>(current_prompts_len),
- kvcache_desc.v_tensors_transposed_pre);
-
- // Update attention mask for the next iteration
- std::copy_n(attn_mask_in_tensor->data<int64_t>() + attn_mask_in_tensor->get_size() - current_prompts_len,
- current_prompts_len,
- attn_mask_in_tensor->data<int64_t>() + kvcache_desc.num_stored_tokens - current_prompts_len);
+ m_llm_profile["1/prefill:3d.update_kvcache"].record([&]() {
+ // Copy calculated key/values chunk from present k/v layer to past k/v layer for storage
+ update_kvcache_for(m_prefill_request,
+ m_prefill_in_ports,
+ m_prefill_out_ports,
+ static_cast<uint32_t>(current_prompts_len),
+ kvcache_desc.v_tensors_transposed_pre);
+
+ // Update attention mask for the next iteration
+ std::copy_n(attn_mask_in_tensor->data<int64_t>() + attn_mask_in_tensor->get_size() - current_prompts_len,
+ current_prompts_len,
+ attn_mask_in_tensor->data<int64_t>() + kvcache_desc.num_stored_tokens - current_prompts_len);
+ });
}
LOG_DEBUG("Done.");
@@ -775,34 +785,40 @@ void ov::npuw::LLMInferRequest::infer_whole_prefill(ov::SoPtr<ov::ITensor> input
LOG_DEBUG("Calling inference for prefill model in a single launch.");
LOG_BLOCK();
- // NB: padded_input can be either fp32(VLM) or i64(LLM)
- auto padded_input = m_prefill_request->get_tensor(m_prefill_in_ports.at(m_input_ids_name));
- std::copy_n(
- reinterpret_cast<uint8_t*>(input_ids->data()),
- input_ids->get_byte_size(),
- reinterpret_cast<uint8_t*>(padded_input->data()) + padded_input->get_byte_size() - input_ids->get_byte_size());
+ m_llm_profile["1/prefill:3a.prepare"].record([&]() {
+ // NB: padded_input can be either fp32(VLM) or i64(LLM)
+ auto padded_input = m_prefill_request->get_tensor(m_prefill_in_ports.at(m_input_ids_name));
+ std::copy_n(reinterpret_cast<uint8_t*>(input_ids->data()),
+ input_ids->get_byte_size(),
+ reinterpret_cast<uint8_t*>(padded_input->data()) + padded_input->get_byte_size() -
+ input_ids->get_byte_size());
- auto padded_attention_mask = m_prefill_request->get_tensor(m_prefill_in_ports.at(layer_names::attention_mask));
- std::copy_n(
- attention_mask->data<int64_t>(),
- attention_mask->get_size(),
- padded_attention_mask->data<int64_t>() + padded_attention_mask->get_size() - attention_mask->get_size());
+ auto padded_attention_mask = m_prefill_request->get_tensor(m_prefill_in_ports.at(layer_names::attention_mask));
+ std::copy_n(
+ attention_mask->data<int64_t>(),
+ attention_mask->get_size(),
+ padded_attention_mask->data<int64_t>() + padded_attention_mask->get_size() - attention_mask->get_size());
- if (token_type_ids) {
- auto padded_token_type_ids = m_prefill_request->get_tensor(m_prefill_in_ports.at(layer_names::token_type_ids));
+ if (token_type_ids) {
+ auto padded_token_type_ids =
+ m_prefill_request->get_tensor(m_prefill_in_ports.at(layer_names::token_type_ids));
- std::fill_n(reinterpret_cast<uint8_t*>(padded_token_type_ids->data()), token_type_ids->get_byte_size(), 0);
- util::copy_to_right(token_type_ids, padded_token_type_ids);
- }
+ std::fill_n(reinterpret_cast<uint8_t*>(padded_token_type_ids->data()), token_type_ids->get_byte_size(), 0);
+ util::copy_to_right(token_type_ids, padded_token_type_ids);
+ }
- auto padded_position_ids = m_prefill_request->get_tensor(m_prefill_in_ports.at(layer_names::position_ids));
- ov::npuw::util::pad_position_ids(padded_position_ids, position_ids);
+ auto padded_position_ids = m_prefill_request->get_tensor(m_prefill_in_ports.at(layer_names::position_ids));
+ ov::npuw::util::pad_position_ids(padded_position_ids, position_ids);
- if (m_eagle3_ext.is_eagle3_model()) {
- m_eagle3_ext.prepare_inputs(m_prefill_request, m_prefill_in_ports);
- }
+ if (m_eagle3_ext.is_eagle3_model()) {
+ m_eagle3_ext.prepare_inputs(m_prefill_request, m_prefill_in_ports);
+ }
+ });
+
+ m_llm_profile["1/prefill:3b.infer"].record([&]() {
+ m_prefill_request->infer();
+ });
- m_prefill_request->infer();
auto& kvcache_desc = m_npuw_llm_compiled_model->m_kvcache_desc;
kvcache_desc.num_stored_tokens += static_cast<uint32_t>(input_ids->get_shape()[layer_ids::INPUT_IDS_SEQ_LEN_DIM]);
@@ -825,32 +841,43 @@ void ov::npuw::LLMInferRequest::infer_prefill(ov::SoPtr<ov::ITensor> input_ids,
"\"NPUW_LLM_MAX_PROMPT_LEN\" or shorten the prompt.");
}
- prepare_for_new_conversation(prompt_length);
+ m_llm_profile["1/prefill:1.prepare_for_new_conversation"].record([&]() {
+ prepare_for_new_conversation(prompt_length);
+ });
process_longrope(m_prefill_request, m_prefill_in_ports, position_ids);
+
+ m_llm_profile["1/prefill:2.apply_lora"].record([&]() {
+ apply_lora();
+ });
+
const bool use_chunk_prefill = m_npuw_llm_compiled_model->m_use_chunk_prefill;
- if (use_chunk_prefill) {
- OPENVINO_ASSERT(!token_type_ids,
- "Chunking is not implemented for Gemma model family yet. "
- "Please set NPUW_LLM_PREFILL_HINT to 'STATIC'");
- infer_chunked_prefill(input_ids, attention_mask, position_ids);
- } else {
- infer_whole_prefill(input_ids, attention_mask, position_ids, token_type_ids);
- }
+ m_llm_profile["1/prefill:3.infer"].record([&]() {
+ if (use_chunk_prefill) {
+ OPENVINO_ASSERT(!token_type_ids,
+ "Chunking is not implemented for Gemma model family yet. "
+ "Please set NPUW_LLM_PREFILL_HINT to 'STATIC'");
+ infer_chunked_prefill(input_ids, attention_mask, position_ids);
+ } else {
+ infer_whole_prefill(input_ids, attention_mask, position_ids, token_type_ids);
+ }
+ });
- if (m_lm_head_request) {
- LOG_DEBUG("Calling inference for LM head model.");
- m_lm_head_request->infer();
- m_logits = m_lm_head_request->get_tensor(m_lm_head_logits_port);
- } else {
- m_logits = m_prefill_request->get_tensor(m_prefill_out_ports.at(layer_names::logits));
- }
+ m_llm_profile["1/prefill:4.lm_head"].record([&]() {
+ if (m_lm_head_request) {
+ LOG_DEBUG("Calling inference for LM head model.");
+ m_lm_head_request->infer();
+ m_logits = m_lm_head_request->get_tensor(m_lm_head_logits_port);
+ } else {
+ m_logits = m_prefill_request->get_tensor(m_prefill_out_ports.at(layer_names::logits));
+ }
- // Update last_hidden_state only for non-chunked prefill
- // For chunked prefill, accumulate_chunk_last_hidden_state() already set the tensor
- if (m_eagle3_ext.is_eagle3_model() && !use_chunk_prefill) {
- m_eagle3_ext.update_last_hidden_state(m_prefill_request, m_prefill_out_ports);
- }
+ // Update last_hidden_state only for non-chunked prefill
+ // For chunked prefill, accumulate_chunk_last_hidden_state() already set the tensor
+ if (m_eagle3_ext.is_eagle3_model() && !use_chunk_prefill) {
+ m_eagle3_ext.update_last_hidden_state(m_prefill_request, m_prefill_out_ports);
+ }
+ });
m_generate_initialized = false;
@@ -873,96 +900,109 @@ void ov::npuw::LLMInferRequest::infer_generate(ov::SoPtr<ov::ITensor> input_ids,
// Note: m_kvcache_request, m_kvcache_in_ports, and m_kvcache_out_ports are selected in
// prepare_for_new_conversation()
+ m_llm_profile["N/generate:1.prepare"].record([&]() {
+ if (!m_generate_initialized) {
+ LOG_DEBUG("Copy kv-cache from prefill to generate model.");
+ if (kvcache_desc.num_stored_tokens > 0) {
+ copy_kvcache();
+ }
+
+ LOG_DEBUG("Prepare inputs.");
+ namespace uu = ov::npuw::util;
+ uu::fill_tensor_bytes(m_kvcache_request->get_tensor(m_kvcache_in_ports.at(m_input_ids_name)), 0u);
+ uu::fill_tensor<int64_t>(m_kvcache_request->get_tensor(m_kvcache_in_ports.at(layer_names::attention_mask)),
+ 0);
+ uu::fill_tensor<int64_t>(m_kvcache_request->get_tensor(m_kvcache_in_ports.at(layer_names::position_ids)),
+ 0);
+ if (token_type_ids) {
+ uu::fill_tensor<int64_t>(
+ m_kvcache_request->get_tensor(m_kvcache_in_ports.at(layer_names::token_type_ids)),
+ 0);
+ }
- if (!m_generate_initialized) {
- LOG_DEBUG("Copy kv-cache from prefill to generate model.");
- if (kvcache_desc.num_stored_tokens > 0) {
- copy_kvcache();
+ m_generate_initialized = true;
}
- LOG_DEBUG("Prepare inputs.");
- namespace uu = ov::npuw::util;
- uu::fill_tensor_bytes(m_kvcache_request->get_tensor(m_kvcache_in_ports.at(m_input_ids_name)), 0u);
- uu::fill_tensor<int64_t>(m_kvcache_request->get_tensor(m_kvcache_in_ports.at(layer_names::attention_mask)), 0);
- uu::fill_tensor<int64_t>(m_kvcache_request->get_tensor(m_kvcache_in_ports.at(layer_names::position_ids)), 0);
- if (token_type_ids) {
- uu::fill_tensor<int64_t>(m_kvcache_request->get_tensor(m_kvcache_in_ports.at(layer_names::token_type_ids)),
- 0);
+ // NB: KV-cache is full, further generation is impossible
+ if (kvcache_desc.num_stored_tokens + input_tokens_len > kvcache_desc.total_size) {
+ OPENVINO_THROW("KV-Cache is full.");
}
- m_generate_initialized = true;
- }
- // NB: KV-cache is full, further generation is impossible
- if (kvcache_desc.num_stored_tokens + input_tokens_len > kvcache_desc.total_size) {
- OPENVINO_THROW("KV-Cache is full.");
- }
+ process_longrope(m_kvcache_request, m_kvcache_in_ports, position_ids);
+ // FIXME: these tensors should be shared between the parent & child models
+ // NB: input_ids can be either fp32(VLM) or i64(LLM)
+ auto kv_input_ids = m_kvcache_request->get_tensor(m_kvcache_in_ports.at(m_input_ids_name));
+ // NOTE: As `input_tokens_len` can be less than the value of `max_generation_token_len`, which
+ // input layers of generation model are resized to, then we need to put
+ // `input_tokens_len` prompt to the right of `max_generation_token_len`-sized tensors.
+ // Attention mask should rule out all left unusable space.
+ std::copy_n(reinterpret_cast<uint8_t*>(input_ids->data()),
+ input_ids->get_byte_size(),
+ reinterpret_cast<uint8_t*>(kv_input_ids->data()) + kv_input_ids->get_byte_size() -
+ input_ids->get_byte_size());
- process_longrope(m_kvcache_request, m_kvcache_in_ports, position_ids);
-
- // FIXME: these tensors should be shared between the parent & child models
- // NB: input_ids can be either fp32(VLM) or i64(LLM)
- auto kv_input_ids = m_kvcache_request->get_tensor(m_kvcache_in_ports.at(m_input_ids_name));
- // NOTE: As `input_tokens_len` can be less than the value of `max_generation_token_len`, which
- // input layers of generation model are resized to, then we need to put
- // `input_tokens_len` prompt to the right of `max_generation_token_len`-sized tensors.
- // Attention mask should rule out all left unusable space.
- std::copy_n(
- reinterpret_cast<uint8_t*>(input_ids->data()),
- input_ids->get_byte_size(),
- reinterpret_cast<uint8_t*>(kv_input_ids->data()) + kv_input_ids->get_byte_size() - input_ids->get_byte_size());
-
- if (token_type_ids) {
- auto kv_token_type_ids = m_kvcache_request->get_tensor(m_kvcache_in_ports.at(layer_names::token_type_ids));
- util::copy_to_right(token_type_ids, kv_token_type_ids);
- }
+ if (token_type_ids) {
+ auto kv_token_type_ids = m_kvcache_request->get_tensor(m_kvcache_in_ports.at(layer_names::token_type_ids));
+ util::copy_to_right(token_type_ids, kv_token_type_ids);
+ }
- // NOTE: Attention mask pattern for generate model requires the set of "1"
- // units of length of the current prompt on the right (for present
- // 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>());
- 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,
- kvcache_desc.max_generation_token_len - input_tokens_len,
- 0);
- }
- std::fill_n(kv_attn_mask->data<int64_t>() + kv_attn_mask->get_size() - input_tokens_len, input_tokens_len, 1);
+ // NOTE: Attention mask pattern for generate model requires the set of "1"
+ // units of length of the current prompt on the right (for present
+ // 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>());
+ 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,
+ kvcache_desc.max_generation_token_len - input_tokens_len,
+ 0);
+ }
+ std::fill_n(kv_attn_mask->data<int64_t>() + kv_attn_mask->get_size() - input_tokens_len, input_tokens_len, 1);
- auto kv_pos_ids = m_kvcache_request->get_tensor(m_kvcache_in_ports.at(layer_names::position_ids));
- ov::npuw::util::pad_position_ids(kv_pos_ids, position_ids);
+ auto kv_pos_ids = m_kvcache_request->get_tensor(m_kvcache_in_ports.at(layer_names::position_ids));
+ ov::npuw::util::pad_position_ids(kv_pos_ids, position_ids);
- if (m_eagle3_ext.is_eagle3_model()) {
- m_eagle3_ext.prepare_inputs(m_kvcache_request, m_kvcache_in_ports);
- }
+ if (m_eagle3_ext.is_eagle3_model()) {
+ m_eagle3_ext.prepare_inputs(m_kvcache_request, m_kvcache_in_ports);
+ }
+ });
- m_kvcache_request->infer();
+ m_llm_profile["N/generate:2.infer"].record([&]() {
+ m_kvcache_request->infer();
+ });
kvcache_desc.num_stored_tokens += input_tokens_len;
if (m_lm_head_request) {
LOG_DEBUG("Calling inference for LM head model asynchronously");
m_lm_head_request->start_async();
- if (kvcache_desc.num_stored_tokens < kvcache_desc.total_size) {
- update_kvcache_for(m_kvcache_request,
- m_kvcache_in_ports,
- m_kvcache_out_ports,
- input_tokens_len,
- kvcache_desc.v_tensors_transposed_gen);
- }
- m_lm_head_request->wait();
- LOG_DEBUG("Calling inference for LM head model -- done.");
+ m_llm_profile["N/generate:3.update_kvcache"].record([&]() {
+ if (kvcache_desc.num_stored_tokens < kvcache_desc.total_size) {
+ update_kvcache_for(m_kvcache_request,
+ m_kvcache_in_ports,
+ m_kvcache_out_ports,
+ input_tokens_len,
+ kvcache_desc.v_tensors_transposed_gen);
+ }
+ });
+ m_llm_profile["N/generate:4.lm_head"].record([&]() {
+ m_lm_head_request->wait();
+ LOG_DEBUG("Calling inference for LM head model -- done.");
- m_logits = m_lm_head_request->get_tensor(m_lm_head_logits_port);
+ m_logits = m_lm_head_request->get_tensor(m_lm_head_logits_port);
+ });
} else {
- if (kvcache_desc.num_stored_tokens < kvcache_desc.total_size) {
- update_kvcache_for(m_kvcache_request,
- m_kvcache_in_ports,
- m_kvcache_out_ports,
- input_tokens_len,
- kvcache_desc.v_tensors_transposed_gen);
- }
+ m_llm_profile["N/generate:3.update_kvcache"].record([&]() {
+ if (kvcache_desc.num_stored_tokens < kvcache_desc.total_size) {
+ update_kvcache_for(m_kvcache_request,
+ m_kvcache_in_ports,
+ m_kvcache_out_ports,
+ input_tokens_len,
+ kvcache_desc.v_tensors_transposed_gen);
+ }
+ });
m_logits = m_kvcache_request->get_tensor(m_kvcache_out_ports.at(layer_names::logits));
}
diff --git a/src/plugins/intel_npu/src/plugin/npuw/llm_infer_request.hpp b/src/plugins/intel_npu/src/plugin/npuw/llm_infer_request.hpp
index b7c1aea123f..82ffb341b57 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/llm_infer_request.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/llm_infer_request.hpp
@@ -13,6 +13,7 @@
#include "llm_lora_states.hpp"
#include "llm_prefix_caching.hpp"
#include "openvino/core/descriptor/output.hpp"
+#include "perf.hpp"
namespace ov {
namespace npuw {
@@ -123,6 +124,10 @@ protected:
// Support prefix caching
std::unique_ptr<PrefixCachingHelper> m_prefix_caching_helper;
+ // LLM-level profiling for 1st token generation analysis
+ using MS = ov::npuw::perf::metric<ov::npuw::perf::MSec>;
+ ov::npuw::perf::Profile<MS> m_llm_profile;
+
// Friend declarations for PrefixCachingHelper to access protected members
friend class PrefixCachingHelper;
};