From 31df030fec3e4b9b2bff308d301826c41458ced2 Mon Sep 17 00:00:00 2001 From: Dr Sujit Vasanth Date: Mon, 11 May 2026 14:56:36 +0100 Subject: [PATCH] fix: Gemma 4 + TurboQuant KV no longer crashes on second prompt when --cache-reuse enabled TurboQuant (turbo2/3/4) uses kernel-level WHT rotation which is position-invariant -- WHT preserves inner products so no RoPE correction is needed after a KV position shift. build_graph_shift() assumed standard quantized tensors with upstream rotation, but TurboQuant sets attn_rot_k=0 and handles rotation at kernel level. Building the shift graph with turbo-padded tensors causes a null buffer assert and segfault on the second prompt. Fix: skip build_graph_shift() layers and get_has_shift() entirely for turbo KV types. Position tracking via seq_add() still works correctly -- only the broken RoPE re-rotation kernel is skipped. --- src/llama-kv-cache.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/llama-kv-cache.cpp b/src/llama-kv-cache.cpp index be923dafced0..9b7bf86f47e2 100644 --- a/src/llama-kv-cache.cpp +++ b/src/llama-kv-cache.cpp @@ -1315,6 +1315,8 @@ uint32_t llama_kv_cache::get_n_stream() const { } bool llama_kv_cache::get_has_shift() const { + // TurboQuant uses kernel-level WHT rotation -- position shift is a no-op + if (!layers.empty() && (layers[0].k->type == GGML_TYPE_TURBO2_0 || layers[0].k->type == GGML_TYPE_TURBO3_0 || layers[0].k->type == GGML_TYPE_TURBO4_0)) { return false; } bool result = false; for (uint32_t s = 0; s < n_stream; ++s) { @@ -2070,6 +2072,8 @@ ggml_cgraph * llama_kv_cache::build_graph_shift(llm_graph_result * res, llama_co for (const auto & layer : layers) { const uint32_t il = layer.il; + const bool is_turbo_k = (layer.k->type == GGML_TYPE_TURBO2_0 || layer.k->type == GGML_TYPE_TURBO3_0 || layer.k->type == GGML_TYPE_TURBO4_0); + if (is_turbo_k) { continue; } const int64_t n_head_kv = hparams.n_head_kv(il); const int64_t n_embd_k_gqa = hparams.n_embd_k_gqa(il);