Skip to content

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

[NPUW] Support chunked prefill. (#31294)

intelgaoxiong3d300e1dae2a
diff --git a/src/plugins/intel_npu/src/al/include/intel_npu/config/npuw.hpp b/src/plugins/intel_npu/src/al/include/intel_npu/config/npuw.hpp
index 5dd340b70fe..c17376f2eb7 100644
--- a/src/plugins/intel_npu/src/al/include/intel_npu/config/npuw.hpp
+++ b/src/plugins/intel_npu/src/al/include/intel_npu/config/npuw.hpp
@@ -110,6 +110,7 @@ DEFINE_OPT(NPUW_LLM_SEQ_LEN_DIM, uint32_t, 2, npuw::llm::seq_len_dim, RunTime);
DEFINE_OPT(NPUW_LLM_MAX_PROMPT_LEN, uint32_t, 1024, npuw::llm::max_prompt_len, RunTime);
DEFINE_OPT(NPUW_LLM_MIN_RESPONSE_LEN, uint32_t, 128, npuw::llm::min_response_len, RunTime);
DEFINE_OPT(NPUW_LLM_OPTIMIZE_V_TENSORS, bool, true, npuw::llm::optimize_v_tensors, RunTime);
+DEFINE_OPT(NPUW_LLM_PREFILL_CHUNK_SIZE, uint64_t, 0, npuw::llm::prefill_chunk_size, RunTime);
namespace npuw {
namespace llm {
diff --git a/src/plugins/intel_npu/src/al/include/intel_npu/npuw_private_properties.hpp b/src/plugins/intel_npu/src/al/include/intel_npu/npuw_private_properties.hpp
index aa11eeaf0c7..54db62b46d3 100644
--- a/src/plugins/intel_npu/src/al/include/intel_npu/npuw_private_properties.hpp
+++ b/src/plugins/intel_npu/src/al/include/intel_npu/npuw_private_properties.hpp
@@ -439,6 +439,16 @@ static constexpr ov::Property<uint32_t> max_prompt_len{"NPUW_LLM_MAX_PROMPT_LEN"
*/
static constexpr ov::Property<uint32_t> min_response_len{"NPUW_LLM_MIN_RESPONSE_LEN"};
+/**
+ * @brief
+ * Type: uint64_t.
+ * Prompt chunk size for chunk prefill.
+ * The chunk size should be a power of two.
+ * Chunk prefill feature is disabled in case the value is 0.
+ * Default value: 0.
+ */
+static constexpr ov::Property<uint64_t> prefill_chunk_size{"NPUW_LLM_PREFILL_CHUNK_SIZE"};
+
/**
* @brief
* FIXME: Should be removed.
diff --git a/src/plugins/intel_npu/src/al/src/config/npuw.cpp b/src/plugins/intel_npu/src/al/src/config/npuw.cpp
index 4cbff551987..11986a28b6a 100644
--- a/src/plugins/intel_npu/src/al/src/config/npuw.cpp
+++ b/src/plugins/intel_npu/src/al/src/config/npuw.cpp
@@ -63,6 +63,7 @@ void intel_npu::registerNPUWLLMOptions(OptionsDesc& desc) {
desc.add<NPUW_LLM_SEQ_LEN_DIM>();
desc.add<NPUW_LLM_MAX_PROMPT_LEN>();
desc.add<NPUW_LLM_MIN_RESPONSE_LEN>();
+ desc.add<NPUW_LLM_PREFILL_CHUNK_SIZE>();
desc.add<NPUW_LLM_OPTIMIZE_V_TENSORS>();
desc.add<NPUW_LLM_PREFILL_HINT>();
desc.add<NPUW_LLM_GENERATE_HINT>();
diff --git a/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.cpp b/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.cpp
index 3d340992a22..1437c0dbec8 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.cpp
@@ -886,9 +886,41 @@ ov::npuw::LLMCompiledModel::LLMCompiledModel(const std::shared_ptr<ov::Model>& m
const uint32_t max_prompt_len = align_to(m_cfg.get<::intel_npu::NPUW_LLM_MAX_PROMPT_LEN>(), 64u);
const uint32_t min_response_len = align_to(m_cfg.get<::intel_npu::NPUW_LLM_MIN_RESPONSE_LEN>(), 64u);
+ m_prefill_chunk_size = m_cfg.get<::intel_npu::NPUW_LLM_PREFILL_CHUNK_SIZE>();
+ const bool use_chunk_prefill = m_prefill_chunk_size > 0;
+ LOG_VERB("Enabled prefill chunking: " << use_chunk_prefill);
+ LOG_VERB("Prefill chunk size: " << m_prefill_chunk_size);
+ LOG_VERB("Maximum prompt length: " << max_prompt_len);
+
+ auto is_power_of_two = [](uint64_t n) {
+ return n > 0 && (n & (n - 1)) == 0;
+ };
+ if (use_chunk_prefill) {
+ if (!is_power_of_two(m_prefill_chunk_size)) {
+ OPENVINO_THROW("Configuration Error: chunk size (",
+ m_prefill_chunk_size,
+ ") is not power of 2. Please adjust NPUW_LLM_PREFILL_CHUNK_SIZE.");
+ }
+
+ if (max_prompt_len % m_prefill_chunk_size) {
+ OPENVINO_THROW("Configuration Error: The maximum prompt length (",
+ max_prompt_len,
+ ") is not a multiple of chunk size (",
+ m_prefill_chunk_size,
+ "). Please adjust NPUW_LLM_MAX_PROMPT_LEN to be a multiple of NPUW_LLM_PREFILL_CHUNK_SIZE.");
+ }
+ }
+
m_kvcache_desc = KVCacheDesc{max_prompt_len, max_prompt_len + min_response_len, 0u, seq_len_dim};
LOG_DEBUG("4. Make prefill model with static shapes");
- reshape_to_static(prefill_model, m_kvcache_desc.max_prompt_size, m_kvcache_desc.max_prompt_size, axes);
+ if (use_chunk_prefill) {
+ reshape_to_static(prefill_model,
+ static_cast<uint32_t>(m_prefill_chunk_size),
+ m_kvcache_desc.max_prompt_size,
+ axes);
+ } else {
+ reshape_to_static(prefill_model, m_kvcache_desc.max_prompt_size, m_kvcache_desc.max_prompt_size, axes);
+ }
LOG_DEBUG("5. Make kvcache model with static shapes");
reshape_to_static(kvcache_model, 1u, m_kvcache_desc.total_size, axes);
@@ -909,7 +941,12 @@ ov::npuw::LLMCompiledModel::LLMCompiledModel(const std::shared_ptr<ov::Model>& m
} else {
LOG_DEBUG("6. Check and apply opt layout --- SKIPPED");
}
- NPUW_ASSERT(remove_empty_kv_inputs(prefill_model));
+
+ if (!use_chunk_prefill) {
+ NPUW_ASSERT(remove_empty_kv_inputs(prefill_model));
+ } else {
+ prefill_model = redirect_new_kv_to_output(prefill_model);
+ }
LOG_DEBUG("7. Optimize kvcache model to output key/values for new token.");
kvcache_model = redirect_new_kv_to_output(kvcache_model);
LOG_DEBUG("8. Converting KV-cache in kvcache model to FP16.");
diff --git a/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.hpp b/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.hpp
index fc2258acb4b..0b32857dea4 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.hpp
@@ -68,6 +68,8 @@ private:
KVCacheDesc m_kvcache_desc;
std::shared_ptr<ov::npuw::CompiledModel> m_kvcache_compiled;
std::shared_ptr<ov::npuw::CompiledModel> m_prefill_compiled;
+
+ uint64_t m_prefill_chunk_size;
};
} // namespace npuw
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 87c9766dfeb..b80566f3587 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
@@ -12,6 +12,7 @@
#include "util_xarch.hpp"
namespace {
+
template <typename T>
void fill_tensor(ov::SoPtr<ov::ITensor> tensor, T fill_val, size_t offset = 0u) {
T* tensor_data = tensor->data<T>();
@@ -106,6 +107,16 @@ void copy_columns_by_row_chunks(ov::SoPtr<ov::ITensor> src, ov::SoPtr<ov::ITenso
}
}
+void update_tensor_by_dim(ov::SoPtr<ov::ITensor> src_tensor, ov::SoPtr<ov::ITensor> dst_tensor, uint32_t kv_dim) {
+ if (kv_dim == 3u) {
+ copy_columns_by_row_chunks(src_tensor, dst_tensor);
+ } else if (kv_dim == 2u) {
+ copy_by_planes(src_tensor, dst_tensor);
+ } else {
+ src_tensor->copy_to(dst_tensor._ptr);
+ }
+}
+
std::optional<ov::Output<const ov::Node>> find_port_by_name(const std::vector<ov::Output<const ov::Node>>& ports,
const std::string& name) {
auto it = std::find_if(ports.begin(), ports.end(), [&](const auto& port) {
@@ -157,6 +168,8 @@ void pad_position_ids(const ov::SoPtr<ov::ITensor>& padded_position_ids, const o
constexpr uint32_t INPUT_IDS_SEQ_LEN_DIM = 1;
+constexpr std::size_t kStartOutputKVCacheLayers = 1;
+
} // anonymous namespace
ov::npuw::LLMInferRequest::LLMInferRequest(const std::shared_ptr<ov::npuw::LLMCompiledModel>& compiled_model)
@@ -193,6 +206,12 @@ ov::npuw::LLMInferRequest::LLMInferRequest(const std::shared_ptr<ov::npuw::LLMCo
for (const auto& output_port : m_kvcache_request->get_compiled_model()->outputs()) {
m_kvcache_out_ports.emplace(output_port.get_any_name(), output_port);
}
+
+ const auto prefill_chunk_size = m_npuw_llm_compiled_model->m_prefill_chunk_size;
+ const bool use_chunk_prefill = prefill_chunk_size > 0;
+ if (use_chunk_prefill) {
+ clear_chunk_prefill_kv_cache();
+ }
}
void ov::npuw::LLMInferRequest::init_tensor(const ov::Output<const ov::Node>& port) {
@@ -224,6 +243,127 @@ void ov::npuw::LLMInferRequest::prepare_for_new_conversation() {
m_npuw_llm_compiled_model->m_kvcache_desc.num_stored_tokens = 0u;
}
+void ov::npuw::LLMInferRequest::clear_chunk_prefill_kv_cache() {
+ const auto& prefill_compiled = m_prefill_request->get_compiled_model();
+
+ for (std::size_t i = kStartOutputKVCacheLayers; i < prefill_compiled->outputs().size(); ++i) {
+ const auto& output_name = prefill_compiled->outputs()[i].get_any_name();
+ const auto& input_name = std::regex_replace(output_name, std::regex("present"), "past_key_values");
+ if (m_prefill_in_ports.find(input_name) == m_prefill_in_ports.end()) {
+ LOG_DEBUG("Input name " << input_name << " doesn't contain kv cache. Skipping.");
+ continue;
+ }
+
+ auto chunk_prefill_kvcache_in_tensor = m_prefill_request->get_tensor(m_prefill_in_ports.at(input_name));
+
+ fill_tensor<ov::float16>(chunk_prefill_kvcache_in_tensor, 0);
+ }
+}
+
+void ov::npuw::LLMInferRequest::infer_prefill_in_chunk(ov::SoPtr<ov::ITensor> input_ids,
+ ov::SoPtr<ov::ITensor> attention_mask,
+ ov::SoPtr<ov::ITensor> position_ids) {
+ LOG_DEBUG("Calling chunked inference for prefill model...");
+ LOG_BLOCK();
+
+ prepare_for_new_conversation();
+
+ auto& kvcache_desc = m_npuw_llm_compiled_model->m_kvcache_desc;
+ const auto input_prompt_len = input_ids->get_shape()[INPUT_IDS_SEQ_LEN_DIM];
+ const auto input_ids_elem_size = input_ids->get_element_type().size();
+ auto input_ids_in_tensor = m_prefill_request->get_tensor(m_prefill_in_ports.at(m_input_ids_name));
+ const int64_t chunk_prompt_len = m_npuw_llm_compiled_model->m_prefill_chunk_size;
+
+ auto attn_mask_in_tensor = m_prefill_request->get_tensor(m_prefill_in_ports.at("attention_mask"));
+ auto pos_ids_in_tensor = m_prefill_request->get_tensor(m_prefill_in_ports.at("position_ids"));
+
+ int64_t remaining_prompts = input_prompt_len;
+ if (input_prompt_len >= kvcache_desc.total_size) {
+ OPENVINO_THROW("Input prompt length (" + std::to_string(input_prompt_len) +
+ ") exceeds maximum kv-cache size (" + std::to_string(kvcache_desc.total_size) + ")");
+ }
+ while (remaining_prompts > 0) {
+ // NB: input_ids can be either fp32(VLM) or i64(LLM)
+ // The last chunk may not be completely filled if the actual length of the prompts is not evenly divisible by
+ // the chunk size
+ auto current_prompts_len = std::min(remaining_prompts, chunk_prompt_len);
+
+ // 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
+ 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;
+ 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 =
+ 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));
+ pad_position_ids(pos_ids_in_tensor, actual_position_ids_slice);
+
+ m_prefill_request->infer();
+
+ remaining_prompts -= current_prompts_len;
+ kvcache_desc.num_stored_tokens += static_cast<uint32_t>(current_prompts_len);
+
+ if (remaining_prompts <= 0) {
+ LOG_DEBUG("All prompts have been prefilled in chunks");
+ break;
+ }
+
+ const auto& prefill_compiled = m_prefill_request->get_compiled_model();
+ // FIXME: Find only matching by names outputs and copy them, having previously checked that such inputs exist
+ for (std::size_t i = kStartOutputKVCacheLayers; i < prefill_compiled->outputs().size(); ++i) {
+ const auto& output_name = prefill_compiled->outputs()[i].get_any_name();
+ const auto& input_name = std::regex_replace(output_name, std::regex("present"), "past_key_values");
+ if (m_prefill_in_ports.find(input_name) == m_prefill_in_ports.end()) {
+ LOG_DEBUG("Input name " << input_name << " doesn't contain kv cache. Skipping.");
+ continue;
+ }
+
+ auto chunk_prefill_past_kv_in_tensor = m_prefill_request->get_tensor(m_prefill_in_ports.at(input_name));
+ const auto& kv_dim = (output_name.find("value") != std::string::npos && kvcache_desc.v_tensors_transposed)
+ ? 3u
+ : kvcache_desc.dim;
+ auto chunk_prefill_past_kv_in_slice =
+ make_tensor_slice(chunk_prefill_past_kv_in_tensor,
+ kv_dim,
+ kvcache_desc.num_stored_tokens - static_cast<uint32_t>(current_prompts_len),
+ kvcache_desc.num_stored_tokens);
+ auto chunk_prefill_present_kv_out_tensor =
+ m_prefill_request->get_tensor(m_prefill_out_ports.at(output_name));
+ update_tensor_by_dim(chunk_prefill_present_kv_out_tensor, chunk_prefill_past_kv_in_slice, kv_dim);
+ }
+
+ // 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_need_copy_kvcache = true;
+
+ m_logits = m_prefill_request->get_tensor(m_prefill_out_ports.at("logits"));
+}
+
void ov::npuw::LLMInferRequest::infer_prefill(ov::SoPtr<ov::ITensor> input_ids,
ov::SoPtr<ov::ITensor> attention_mask,
ov::SoPtr<ov::ITensor> position_ids) {
@@ -232,8 +372,8 @@ void ov::npuw::LLMInferRequest::infer_prefill(ov::SoPtr<ov::ITensor> input_ids,
prepare_for_new_conversation();
- auto padded_input = m_prefill_request->get_tensor(m_prefill_in_ports.at(m_input_ids_name));
// 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(),
@@ -273,11 +413,10 @@ void ov::npuw::LLMInferRequest::infer_generate(ov::SoPtr<ov::ITensor> input_ids,
if (m_need_copy_kvcache) {
LOG_DEBUG("Copying kv-cache from prefill to generate model.");
- const std::size_t kStartOutputKVCacheLayers = 1u;
const auto& kvcache_compiled = m_kvcache_request->get_compiled_model();
// FIXME: Find only matching by names outputs and copy them, having previously checked that such inputs exist
- for (std::size_t i = 0; i < kvcache_compiled->outputs().size() - 1; ++i) {
- const auto& output_name = kvcache_compiled->outputs()[kStartOutputKVCacheLayers + i].get_any_name();
+ for (std::size_t i = kStartOutputKVCacheLayers; i < kvcache_compiled->outputs().size(); ++i) {
+ const auto& output_name = kvcache_compiled->outputs()[i].get_any_name();
auto prefill_out_tensor = m_prefill_request->get_tensor(m_prefill_out_ports.at(output_name));
const auto& input_name = std::regex_replace(output_name, std::regex("present"), "past_key_values");
@@ -296,19 +435,55 @@ void ov::npuw::LLMInferRequest::infer_generate(ov::SoPtr<ov::ITensor> input_ids,
? 3u
: kvcache_desc.dim;
- auto prefill_out_slice = make_tensor_slice(prefill_out_tensor,
- kv_dim,
- kvcache_desc.max_prompt_size - kvcache_desc.num_stored_tokens,
- kvcache_desc.max_prompt_size);
+ const auto prefill_chunk_size = m_npuw_llm_compiled_model->m_prefill_chunk_size;
+ const bool use_chunk_prefill = prefill_chunk_size > 0;
+ if (use_chunk_prefill) {
+ // The chunk prefilled KV results are divided into two parts:
+ // Part 1: The KV results from loops 1 to n-1 have been copied into the 'past' KV input tensor
+ // Part 2: The kv results from the last loop remain in the 'present' KV output tensor
+ // The task is to copy both parts into the KV-cache input tensor for the decoding process
+
+ // Copy part 1 KV results
+ auto tokens_in_present_chunk = kvcache_desc.num_stored_tokens % prefill_chunk_size;
+ tokens_in_present_chunk = tokens_in_present_chunk ? tokens_in_present_chunk : prefill_chunk_size;
+
+ // tokens_in_past_chunks may be 0 in case short prompts are prefilled in single chunk
+ auto tokens_in_past_chunks = kvcache_desc.num_stored_tokens - tokens_in_present_chunk;
+ if (tokens_in_past_chunks > 0) {
+ auto prefill_past_kv = m_prefill_request->get_tensor(m_prefill_in_ports.at(input_name));
+ auto prefill_past_kv_chunks =
+ make_tensor_slice(prefill_past_kv, kv_dim, 0u, static_cast<uint32_t>(tokens_in_past_chunks));
+
+ auto kvcache_past_kv_chunks =
+ make_tensor_slice(kvcache_in_tensor, kv_dim, 0u, static_cast<uint32_t>(tokens_in_past_chunks));
+
+ update_tensor_by_dim(prefill_past_kv_chunks, kvcache_past_kv_chunks, kv_dim);
+ }
+
+ // Copy part 2 KV results
+ auto prefill_present_kv_chunk =
+ make_tensor_slice(prefill_out_tensor,
+ kv_dim,
+ static_cast<uint32_t>(prefill_chunk_size - tokens_in_present_chunk),
+ static_cast<uint32_t>(prefill_chunk_size));
+
+ auto kvcache_last_kv_chunk = make_tensor_slice(kvcache_in_tensor,
+ kv_dim,
+ static_cast<uint32_t>(tokens_in_past_chunks),
+ kvcache_desc.num_stored_tokens);
+
+ update_tensor_by_dim(prefill_present_kv_chunk, kvcache_last_kv_chunk, kv_dim);
+ } else {
+ auto prefill_out_slice =
+ make_tensor_slice(prefill_out_tensor,
+ kv_dim,
+ kvcache_desc.max_prompt_size - kvcache_desc.num_stored_tokens,
+ kvcache_desc.max_prompt_size);
- auto kvcache_in_slice = make_tensor_slice(kvcache_in_tensor, kv_dim, 0u, kvcache_desc.num_stored_tokens);
+ auto kvcache_in_slice =
+ make_tensor_slice(kvcache_in_tensor, kv_dim, 0u, kvcache_desc.num_stored_tokens);
- if (kv_dim == 3u) {
- copy_columns_by_row_chunks(prefill_out_slice, kvcache_in_slice);
- } else if (kv_dim == 2u) {
- copy_by_planes(prefill_out_slice, kvcache_in_slice);
- } else {
- prefill_out_slice->copy_to(kvcache_in_slice._ptr);
+ update_tensor_by_dim(prefill_out_slice, kvcache_in_slice, kv_dim);
}
}
@@ -342,11 +517,10 @@ void ov::npuw::LLMInferRequest::infer_generate(ov::SoPtr<ov::ITensor> input_ids,
}
LOG_DEBUG("Write KV-cache for the new token to the correct input position for next iteration.");
- const std::size_t kStartOutputKVCacheLayers = 1u;
const auto& kvcache_compiled = m_kvcache_request->get_compiled_model();
// FIXME: Find only matching by names outputs and copy them, having previously checked that such inputs exist
- for (std::size_t i = 0; i < kvcache_compiled->outputs().size() - 1; ++i) {
- const auto& output_name = kvcache_compiled->outputs()[kStartOutputKVCacheLayers + i].get_any_name();
+ for (std::size_t i = kStartOutputKVCacheLayers; i < kvcache_compiled->outputs().size(); ++i) {
+ const auto& output_name = kvcache_compiled->outputs()[i].get_any_name();
const auto& input_name = std::regex_replace(output_name, std::regex("present"), "past_key_values");
if (m_kvcache_in_ports.find(input_name) == m_kvcache_in_ports.end()) {
LOG_DEBUG("Input name " << input_name << " doesn't contain kv cache. Skipping.");
@@ -390,7 +564,13 @@ void ov::npuw::LLMInferRequest::infer() {
// NB: Check the sequence length provided for input_ids
// in order to distinguish prefill / generate stages
if (input_ids->get_shape()[INPUT_IDS_SEQ_LEN_DIM] != 1) {
- infer_prefill(input_ids, attention_mask, position_ids);
+ const auto prefill_chunk_size = m_npuw_llm_compiled_model->m_prefill_chunk_size;
+ const bool use_chunk_prefill = prefill_chunk_size > 0;
+ if (use_chunk_prefill) {
+ infer_prefill_in_chunk(input_ids, attention_mask, position_ids);
+ } else {
+ infer_prefill(input_ids, attention_mask, position_ids);
+ }
} else {
infer_generate(input_ids, attention_mask, position_ids);
}
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 6bc7f6c987f..94786147f41 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
@@ -32,12 +32,19 @@ public:
private:
void prepare_for_new_conversation();
+
+ void clear_chunk_prefill_kv_cache();
+
void init_tensor(const ov::Output<const ov::Node>& port);
void infer_prefill(ov::SoPtr<ov::ITensor> input_ids,
ov::SoPtr<ov::ITensor> attention_mask,
ov::SoPtr<ov::ITensor> position_ids);
+ void infer_prefill_in_chunk(ov::SoPtr<ov::ITensor> input_ids,
+ ov::SoPtr<ov::ITensor> attention_mask,
+ ov::SoPtr<ov::ITensor> position_ids);
+
void infer_generate(ov::SoPtr<ov::ITensor> input_ids,
ov::SoPtr<ov::ITensor> attention_mask,
ov::SoPtr<ov::ITensor> position_ids);