Commit ee17f293b for llama.cpp
commit ee17f293b638295651c6ff4752a4dce102c6b8e4
Author: Aman Gupta <amangupta052@gmail.com>
Date: Tue Nov 3 13:51:53 2026 +0800
kv-cache : never return 0 kv_rows for sequences without cells (#31570)
A cache that shares the cells of another one (f.ex. the gemma4 assistant) can build a graph before its sequences have any cells, f.ex. at the server warmup. get_n_kv_rows() then returned 0, which means that kv_rows are not used, so the graph fell back to the dense KQ mask and the scheduler re-planned for it. Later graphs with larger masks then aborted with GGML_SCHED_DEBUG_REALLOC=1.
diff --git a/src/llama-kv-cache.cpp b/src/llama-kv-cache.cpp
index 84f547ae1..d9f46f0ad 100644
--- a/src/llama-kv-cache.cpp
+++ b/src/llama-kv-cache.cpp
@@ -1413,7 +1413,8 @@ uint32_t llama_kv_cache::get_n_kv_rows(const llama_ubatch & ubatch) const {
}
// pad like n_kv so that the graph can be reused
- return std::min(GGML_PAD(n_max, n_pad_cur), get_size());
+ // never 0, it means that kv_rows are not used (f.ex. the sequences have no cells yet in a cache that shares the cells of another one)
+ return std::min(std::max(n_pad_cur, GGML_PAD(n_max, n_pad_cur)), get_size());
}
ggml_tensor * llama_kv_cache::get_k(ggml_context * ctx, int32_t il, uint32_t n_kv, const slot_info & sinfo) const {