Commit 713fee7fb for llama.cpp

commit 713fee7fb05ceb2d5850dece2233019254da2055
Author: Aman Gupta <amangupta052@gmail.com>
Date:   Tue Dec 1 11:01:37 2026 +0800

    llama : budget graph nodes for KV cache rotation (#33382)

diff --git a/src/llama-context.cpp b/src/llama-context.cpp
index 603697802..88cc05630 100644
--- a/src/llama-context.cpp
+++ b/src/llama-context.cpp
@@ -345,6 +345,7 @@ llama_context::llama_context(

     cparams.op_offload     = params.op_offload;
     cparams.kv_unified     = params.kv_unified;
+    cparams.kv_quantized   = ggml_is_quantized(params.type_k) || ggml_is_quantized(params.type_v);

     // the Hy4 flash pools gather their members from a single KV stream
     if (model.arch == LLM_ARCH_HY_V4_FLASH && !cparams.kv_unified && cparams.n_seq_max > 1) {
@@ -2886,6 +2887,13 @@ uint32_t llama_context::graph_max_nodes() const {
         res += 32u*model.hparams.n_layer_all;
     }

+    // a quantized KV cache may rotate Q and K (K cache) and V and the attention output (V cache) of each layer,
+    // 3 nodes per rotation plus the rotation matrices, which the per-tensor estimate above does not cover for
+    // models with few tensors per layer (e.g. fused QKV)
+    if (cparams.kv_quantized) {
+        res += 16u*model.hparams.n_layer_all;
+    }
+
     // every sampler builds n_outputs_max_per_seq chains, see llm_graph_context::build_sampling
     for (const auto & [seq_id, sampler] : sampling.samplers) {
         res += llama_sampler_backend_n_nodes(sampler) * cparams.n_outputs_max_per_seq;
diff --git a/src/llama-cparams.h b/src/llama-cparams.h
index c71582a3c..d1e7e1284 100644
--- a/src/llama-cparams.h
+++ b/src/llama-cparams.h
@@ -51,6 +51,7 @@ struct llama_cparams {
     bool no_perf;
     bool op_offload;
     bool kv_unified;
+    bool kv_quantized;       // quantized K or V cache: the cache may rotate Q/K/V and the attention output
     bool pipeline_parallel;
     bool training;           // set by llama_opt_init()
     bool moe_random_routing; // benchmark only: random expert ids in the MoE GEMMs