From 4783b1145c7073bb809f4c282ff4853cbfe42954 Mon Sep 17 00:00:00 2001 From: Aman Gupta Date: Fri, 20 Nov 2026 18:49:16 +0800 Subject: [PATCH] llama : move the indexer key rotation choice into the model classes (#32653) * llama : move the indexer key rotation choice into the model classes The KV cache forced a Hadamard rotation for the lightning indexer keys based on a list of architectures. The virtual llama_model::indexer_k_rot() now returns true for the same models. DeepSeek4.1 inherits from DeepSeek4 but was not in the list, so it overrides it back to false. * llama : rename indexer_k_rot() to lid_k_hdmd() --- src/llama-kv-cache.cpp | 7 ++----- src/llama-model.h | 3 +++ src/models/models.h | 6 ++++++ 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/llama-kv-cache.cpp b/src/llama-kv-cache.cpp index 5dd0e5f02..fbe422a27 100644 --- a/src/llama-kv-cache.cpp +++ b/src/llama-kv-cache.cpp @@ -314,11 +314,8 @@ llama_kv_cache::llama_kv_cache( ggml_is_quantized(type_k) && hparams.n_embd_head_k() % 64 == 0; - // always create Hadamard rotation tensors for DeepSeek lightning indexers - if ((model.arch == LLM_ARCH_DEEPSEEK32 || model.arch == LLM_ARCH_DEEPSEEK4 || - model.arch == LLM_ARCH_GLM_DSA || model.arch == LLM_ARCH_DOTS3NOTE || - model.arch == LLM_ARCH_MISTRAL_LARGE4) && - hparams.n_embd_head_k_full == hparams.indexer_head_size) { + // always create Hadamard rotation tensors for lightning indexer keys + if (model.lid_k_hdmd() && hparams.n_embd_head_k_full == hparams.indexer_head_size) { attn_rot_k = true; } diff --git a/src/llama-model.h b/src/llama-model.h index 1cc5d743a..e86e5e54f 100644 --- a/src/llama-model.h +++ b/src/llama-model.h @@ -1055,6 +1055,9 @@ struct llama_model { // graph size budget: max graph nodes per model tensor virtual uint32_t graph_nodes_per_tensor() const { return 8; } + // apply Hadamard transform to the lightning indexer keys + virtual bool lid_k_hdmd() const { return false; } + ggml_cgraph * build_graph(const llm_graph_params & params) const; virtual void load_stats (llama_model_loader & ml) = 0; diff --git a/src/models/models.h b/src/models/models.h index 979ceffc2..822d707b9 100644 --- a/src/models/models.h +++ b/src/models/models.h @@ -1246,6 +1246,7 @@ struct llama_model_deepseek32 : public llama_model_base { llama_model_deepseek32(const struct llama_model_params & params) : llama_model_base(params) {} void load_arch_hparams(llama_model_loader & ml) override; void load_arch_tensors(llama_model_loader & ml) override; + bool lid_k_hdmd() const override { return true; } struct graph : public llm_graph_context { graph(const llm_graph_params & params); @@ -1262,6 +1263,7 @@ struct llama_model_dots3note : public llama_model_base { llama_model_dots3note(const struct llama_model_params & params) : llama_model_base(params) {} void load_arch_hparams(llama_model_loader & ml) override; void load_arch_tensors(llama_model_loader & ml) override; + bool lid_k_hdmd() const override { return true; } struct graph : public llm_graph_context { graph(const llm_graph_params & params); @@ -1285,6 +1287,7 @@ struct llama_model_deepseek4 : public llama_model_base { void load_arch_hparams(llama_model_loader & ml) override; void load_arch_tensors(llama_model_loader & ml) override; uint32_t graph_nodes_per_tensor() const override { return 32; } + bool lid_k_hdmd() const override { return true; } struct graph : public llm_graph_context { struct no_build {}; @@ -1485,6 +1488,7 @@ struct llama_model_deepseek41 : public llama_model_deepseek4 { llama_model_deepseek41(const struct llama_model_params & params) : llama_model_deepseek4(params) {} void load_arch_hparams(llama_model_loader & ml) override; void load_arch_tensors(llama_model_loader & ml) override; + bool lid_k_hdmd() const override { return false; } // Engram: the hash constants are hparams.ple_* (one module per Engram layer), hashed on the host (ggml has no int64 mul/xor/mod) int engram_index(int il) const; @@ -1543,6 +1547,7 @@ struct llama_model_glm_dsa : public llama_model_base { llama_model_glm_dsa(const struct llama_model_params & params) : llama_model_base(params) {} void load_arch_hparams(llama_model_loader & ml) override; void load_arch_tensors(llama_model_loader & ml) override; + bool lid_k_hdmd() const override { return true; } struct graph : public llm_graph_context { graph(const llm_graph_params & params); @@ -1677,6 +1682,7 @@ struct llama_model_mistral_large4 : public llama_model_base { llama_model_mistral_large4(const struct llama_model_params & params) : llama_model_base(params) {} void load_arch_hparams(llama_model_loader & ml) override; void load_arch_tensors(llama_model_loader & ml) override; + bool lid_k_hdmd() const override { return true; } struct graph : public llm_graph_context { graph(const llm_graph_params & params);