Skip to content

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

[NPUW] Share kvcache between prefill and generate when chunking is enabled (#32642)

Alexey Smirnov3797c559cc51
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 037bf86fa16..496cb8035fb 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
@@ -195,10 +195,13 @@ ov::npuw::LLMInferRequest::LLMInferRequest(const std::shared_ptr<ov::npuw::LLMCo
m_prefill_out_ports.emplace(output_port.get_any_name(), output_port);
}
+ init_pre_alloc_device();
init_lora_states();
const bool use_chunk_prefill = m_npuw_llm_compiled_model->m_use_chunk_prefill;
if (use_chunk_prefill) {
+ // FIXME: enable w/o chunking as well. Although need to align the paddings beforehand
+ bind_past_kv();
clear_chunk_prefill_kv_cache();
}
@@ -264,6 +267,53 @@ ov::npuw::LLMInferRequest::LLMInferRequest(const std::shared_ptr<ov::npuw::LLMCo
m_gemma_sliding_window_size = compiled_model->m_gemma_sliding_window_size;
}
+std::string ov::npuw::LLMInferRequest::init_pre_alloc_device() {
+ bool pre_alloc_on_npu = true;
+ const auto& kvcache_compiled = m_npuw_llm_compiled_model->m_kvcache_compiled;
+ for (std::size_t idx = 0; idx < kvcache_compiled->m_compiled_submodels.size(); ++idx) {
+ if (kvcache_compiled->submodel_device(idx) != "NPU") {
+ pre_alloc_on_npu = false;
+ break;
+ }
+ }
+
+ return pre_alloc_on_npu ? "NPU" : "CPU";
+}
+
+void ov::npuw::LLMInferRequest::bind_past_kv() {
+ auto& kvcache_desc = m_npuw_llm_compiled_model->m_kvcache_desc;
+ if (kvcache_desc.v_tensors_transposed_pre != kvcache_desc.v_tensors_transposed_gen) {
+ // FIXME: disable kv cache sharing when one of the models is transposed for now
+ return;
+ }
+
+ // Only reuse KV cache related tensors (past_key_values)
+ for (const auto& [input_name, prefill_in_port] : m_prefill_in_ports) {
+ // Only process KV cache inputs (past_key_values)
+ if (input_name.find(layer_names::past_key_values) == std::string::npos) {
+ continue;
+ }
+
+ // Check if the kv cache request has the same input port
+ if (m_kvcache_in_ports.find(input_name) == m_kvcache_in_ports.end()) {
+ continue;
+ }
+
+ const auto& kvcache_in_port = m_kvcache_in_ports.at(input_name);
+ const auto& kvcache_past_kv_in_tensor = m_kvcache_request->get_tensor(kvcache_in_port);
+ auto data = kvcache_past_kv_in_tensor->data();
+
+ auto origTensor = m_prefill_request->get_tensor(prefill_in_port);
+ auto new_tensor =
+ ov::get_tensor_impl(ov::Tensor(origTensor->get_element_type(), origTensor->get_shape(), data));
+ m_prefill_request->set_tensor(prefill_in_port, new_tensor);
+
+ // Record that we have already bind past_kv, will need data copy when update past kv in infer requests to
+ // ensure correct data layout
+ m_past_kv_bound = true;
+ }
+}
+
void ov::npuw::LLMInferRequest::create_generate_request_variants(
const std::shared_ptr<ov::npuw::LLMCompiledModel>& compiled_model) {
// Create multiple generate model variants' requests
@@ -332,6 +382,10 @@ void ov::npuw::LLMInferRequest::create_generate_request_variants(
// Set default to the largest variant for backward compatibility
m_kvcache_request = m_generate_requests.back();
+
+ // Need to set ports to ensure tensors aren't empty during bind_past_kv()
+ 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);
}
std::shared_ptr<ov::IAsyncInferRequest> ov::npuw::LLMInferRequest::select_generate_request(int64_t prompt_length) {
@@ -388,16 +442,6 @@ void ov::npuw::LLMInferRequest::init_tensor(const ov::Output<const ov::Node>& po
void ov::npuw::LLMInferRequest::apply_lora() {
uint32_t max_low_rank_dim_size = m_npuw_llm_compiled_model->m_max_lora_rank;
- bool pre_alloc_on_npu = true;
- const auto& prefill_compiled = m_npuw_llm_compiled_model->m_prefill_compiled;
- for (std::size_t idx = 0; idx < prefill_compiled->m_compiled_submodels.size(); ++idx) {
- if (prefill_compiled->submodel_device(idx) != "NPU") {
- pre_alloc_on_npu = false;
- break;
- }
- }
- std::string device = pre_alloc_on_npu ? "NPU" : "CPU";
-
for (auto state : m_variableStates) {
auto state_name = state->get_name();
auto state_tensor = state->get_state();
@@ -444,7 +488,7 @@ void ov::npuw::LLMInferRequest::apply_lora() {
auto prefill_lora_in_tensor = m_prefill_request->get_tensor(m_prefill_in_ports.at(state_name));
auto new_infer_tensor = ov::npuw::util::allocMem(prefill_lora_in_tensor->get_element_type(),
prefill_lora_in_tensor->get_shape(),
- device,
+ m_pre_alloc_device,
m_npuw_llm_compiled_model->get_plugin());
bool has_padding = state_tensor_rank != target_lora_rank;
if (has_padding) {
@@ -548,11 +592,27 @@ void ov::npuw::LLMInferRequest::copy_kvcache() {
// 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 - m_tokens_in_present_chunk;
if (tokens_in_past_chunks > 0) {
+ // Create backup of past KV tensor when buffer sharing is enabled to prevent data corruption
+ // This is necessary because subsequent copy operations would overwrite the shared buffer
auto prefill_past_kv = m_prefill_request->get_tensor(m_prefill_in_ports.at(input_name));
- auto prefill_past_kv_chunks = uu::make_tensor_slice(prefill_past_kv,
- pre_kv_dim,
- 0u,
- static_cast<uint32_t>(tokens_in_past_chunks));
+ ov::SoPtr<ov::ITensor> tmp_dense_kv_tensor;
+ ov::SoPtr<ov::ITensor> prefill_past_kv_chunks;
+ if (m_past_kv_bound) {
+ tmp_dense_kv_tensor = ov::npuw::util::allocMem(prefill_past_kv->get_element_type(),
+ prefill_past_kv->get_shape(),
+ m_pre_alloc_device,
+ m_npuw_llm_compiled_model->get_plugin());
+ prefill_past_kv->copy_to(tmp_dense_kv_tensor._ptr);
+ prefill_past_kv_chunks = make_tensor_slice(tmp_dense_kv_tensor,
+ pre_kv_dim,
+ 0u,
+ static_cast<uint32_t>(tokens_in_past_chunks));
+ } else {
+ prefill_past_kv_chunks = make_tensor_slice(prefill_past_kv,
+ pre_kv_dim,
+ 0u,
+ static_cast<uint32_t>(tokens_in_past_chunks));
+ }
auto kvcache_past_kv_chunks = uu::make_tensor_slice(kvcache_in_tensor,
gen_kv_dim,
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 6c4af7feedd..a586a2eb47b 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
@@ -147,6 +147,13 @@ protected:
std::vector<ov::SoPtr<ov::IVariableState>> m_variableStates;
void init_lora_states();
+ // To share kvcache between prefill and generate
+ bool m_past_kv_bound = false;
+ void bind_past_kv();
+
+ std::string m_pre_alloc_device = "CPU";
+ std::string init_pre_alloc_device();
+
// Support prefix caching
std::unique_ptr<PrefixCachingHelper> m_prefix_caching_helper;