Commit ce94c097b for llama.cpp
commit ce94c097bc2e6d8023728ce9f85de8247264d8b7
Author: Aman Gupta <amangupta052@gmail.com>
Date: Tue Dec 1 14:26:03 2026 +0800
kv-cache : reuse SWA cells shared by several sequences once masked for all of them (#32309)
In a unified KV cache, cells that are shared between sequences (e.g. a
common prompt copied with llama_memory_seq_cp) were never considered by
find_slot, which only recycled SWA-masked cells with a single owner. The
shared cells stayed occupied forever and the SWA cache, sized as
n_swa*n_seq_max + n_ubatch, ran out of slots once the shared prefix grew
past the padding.
Allow find_slot to reuse a shared cell once it is SWA-masked for all of
its sequences, and let apply_ubatch purge the overwritten positions for
each of them.
Fixes #32258
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
diff --git a/src/llama-kv-cache.cpp b/src/llama-kv-cache.cpp
index c0166d30b..80742ed2f 100644
--- a/src/llama-kv-cache.cpp
+++ b/src/llama-kv-cache.cpp
@@ -1076,6 +1076,8 @@ llama_kv_cache::slot_info llama_kv_cache::find_slot(const llama_ubatch & ubatch,
// - (disabled) mask causally, if the sequence is the same as the one we are inserting
// - mask SWA, using current max pos for that sequence in the cache
// always insert in the cell with minimum pos
+ // - the cell is shared by several sequences (e.g. after llama_memory_seq_cp in a unified cache):
+ // - mask SWA, the cell must be masked for all of its sequences
bool can_use = cells.is_empty(idx);
if (!can_use && cells.seq_count(idx) == 1) {
@@ -1095,6 +1097,20 @@ llama_kv_cache::slot_info llama_kv_cache::find_slot(const llama_ubatch & ubatch,
can_use = true;
}
}
+ } else if (!can_use && n_swa > 0 && swa_type != LLAMA_SWA_TYPE_NONE) {
+ const llama_pos pos_cell = cells.pos_get(idx);
+ const auto & seq_set = cells.seq_get_all(idx);
+
+ // SWA mask
+ can_use = true;
+
+ for (uint32_t seq_id_cell = 0; seq_id_cell < LLAMA_MAX_SEQ; ++seq_id_cell) {
+ if (seq_set.test(seq_id_cell) &&
+ !llama_hparams::is_masked_swa(n_swa, swa_type, pos_cell, cells.seq_pos_max(seq_id_cell) + 1)) {
+ can_use = false;
+ break;
+ }
+ }
}
if (can_use) {
@@ -1155,12 +1171,15 @@ void llama_kv_cache::apply_ubatch(const slot_info & sinfo, const llama_ubatch &
const auto idx = sinfo.idxs[s][ii];
if (!cells.is_empty(idx)) {
- assert(cells.seq_count(idx) == 1);
-
- const llama_seq_id seq_id = cells.seq_get(idx);
- const llama_pos pos = cells.pos_get(idx);
+ // the cell can be shared by several sequences (see find_slot)
+ const auto & seq_set = cells.seq_get_all(idx);
+ const llama_pos pos = cells.pos_get(idx);
- seq_pos_max_rm[seq_id] = std::max(seq_pos_max_rm[seq_id], pos);
+ for (uint32_t seq_id = 0; seq_id < LLAMA_MAX_SEQ; ++seq_id) {
+ if (seq_set.test(seq_id)) {
+ seq_pos_max_rm[seq_id] = std::max(seq_pos_max_rm[seq_id], pos);
+ }
+ }
cells.rm(idx);
}
diff --git a/tests/test-kv-cache.cpp b/tests/test-kv-cache.cpp
index 4b48387b9..e23ac194c 100644
--- a/tests/test-kv-cache.cpp
+++ b/tests/test-kv-cache.cpp
@@ -1,6 +1,7 @@
#include "llama-kv-cache.h"
#include "llama-model.h"
+#include <algorithm>
#include <numeric>
#include <string>
#include <vector>
@@ -175,6 +176,74 @@ int main() {
GGML_ASSERT(kv.seq_pos_max(0) == 4);
}
+ // SWA cells shared by several sequences (e.g. after seq_cp in a unified cache) must be reused once they
+ // are masked for all of their sequences, otherwise they stay occupied forever
+ // ref: https://github.com/ggml-org/llama.cpp/issues/32258
+ {
+ std::unique_ptr<llama_model> model(llama_model_create(LLM_ARCH_LLAMA, llama_model_default_params()));
+ init_model(*model, 1);
+
+ const uint32_t kv_size = 8;
+ const uint32_t n_swa = 4;
+
+ llama_kv_cache kv(*model, false, model->hparams, GGML_TYPE_F16, GGML_TYPE_F16,
+ false, false, true, kv_size, 2, n_swa, LLAMA_SWA_TYPE_STANDARD, nullptr, nullptr, nullptr, nullptr);
+ GGML_ASSERT(kv.get_size() == kv_size);
+
+ auto add = [&](llama_seq_id seq_id, llama_pos pos) {
+ llama_token token = 0;
+ int32_t n_seq_id = 1;
+ llama_seq_id seq_ids[1] = { seq_id };
+ llama_seq_id * seq_id_ptr = seq_ids;
+ int32_t seq_idx[LLAMA_MAX_SEQ];
+ int8_t output = 1;
+ std::fill(seq_idx, seq_idx + LLAMA_MAX_SEQ, -1);
+ seq_idx[seq_id] = 0;
+
+ llama_ubatch ubatch = {};
+ ubatch.b_equal_seqs = 1;
+ ubatch.n_tokens = 1;
+ ubatch.n_seq_tokens = 1;
+ ubatch.n_seqs = 1;
+ ubatch.n_seqs_unq = 1;
+ ubatch.n_pos = 1;
+ ubatch.token = &token;
+ ubatch.pos = &pos;
+ ubatch.n_seq_id = &n_seq_id;
+ ubatch.seq_id = &seq_id_ptr;
+ ubatch.seq_id_unq = seq_ids;
+ ubatch.seq_idx = seq_idx;
+ ubatch.output = &output;
+
+ const auto sinfo = kv.find_slot(ubatch, false);
+ if (sinfo.empty()) {
+ return false;
+ }
+ kv.apply_ubatch(sinfo, ubatch);
+ return true;
+ };
+
+ // shared prefix [0, 1] of seq 0 and seq 1
+ GGML_ASSERT(add(0, 0));
+ GGML_ASSERT(add(0, 1));
+ kv.seq_cp(0, 1, -1, -1);
+
+ // fill the rest of the cache with tokens of each sequence that are still inside the window
+ for (llama_pos pos = 2; pos < 5; ++pos) {
+ GGML_ASSERT(add(0, pos));
+ GGML_ASSERT(add(1, pos));
+ }
+
+ // the cache is full and only the shared cells are masked for both sequences
+ for (llama_pos pos = 5; pos < 16; ++pos) {
+ GGML_ASSERT(add(0, pos));
+ GGML_ASSERT(add(1, pos));
+ GGML_ASSERT(kv.seq_pos_max(0) == pos && kv.seq_pos_max(1) == pos);
+ GGML_ASSERT(kv.seq_pos_min(0) == kv.seq_pos_min(1));
+ GGML_ASSERT(kv.seq_pos_max(0) - kv.seq_pos_min(0) + 1 >= (llama_pos) n_swa);
+ }
+ }
+
llama_backend_free();
return 0;
}