Skip to content

Commit fdeaf7e

Browse files
author
mmtmu
committed
glm-dsa: only rotate lid K when cache is quantized
The online Hadamard rotation (PR ggml-org#21038) exists to suppress quantization error. On an F16 lid cache it is a no-op and costs one extra mul_mat per layer. Remove the force-enable for GLM_DSA and DEEPSEEK2 and let the lid cache follow the standard rule (quantized type + aligned head dim). Guard the Hadamard mul_mats in the builder and the set_input_k_rot call on self_k_rot_lid being non-null, since it can legitimately be null now.
1 parent d987cba commit fdeaf7e

3 files changed

Lines changed: 15 additions & 9 deletions

File tree

src/llama-graph.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,10 @@ void llm_graph_input_attn_k_dsa::set_input(const llama_ubatch * ubatch) {
500500

501501
mctx->get_lid()->set_input_k_idxs(self_k_idxs_lid, ubatch);
502502
mctx->get_lid()->set_input_kq_mask(self_kq_mask_lid, ubatch, cparams.causal_attn);
503-
mctx->get_lid()->set_input_k_rot(self_k_rot_lid);
503+
504+
if (self_k_rot_lid) {
505+
mctx->get_lid()->set_input_k_rot(self_k_rot_lid);
506+
}
504507
}
505508

506509
bool llm_graph_input_attn_k_dsa::can_reuse(const llm_graph_params & params) {

src/llama-kv-cache.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -281,12 +281,6 @@ llama_kv_cache::llama_kv_cache(
281281
!hparams.is_n_embd_k_gqa_variable() &&
282282
hparams.n_embd_head_k() % 64 == 0;
283283

284-
if ((model.arch == LLM_ARCH_GLM_DSA || model.arch == LLM_ARCH_DEEPSEEK2) &&
285-
hparams.n_embd_head_k_full == hparams.indexer_head_size &&
286-
hparams.indexer_head_size > 0) {
287-
attn_rot_k = true;
288-
}
289-
290284
attn_rot_v =
291285
!attn_rot_disable &&
292286
ggml_is_quantized(type_v) &&

src/models/glm-dsa.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,17 @@ llm_build_glm_dsa::llm_build_glm_dsa(const llama_model & model, const llm_graph_
9191
indexer_k = ggml_concat(ctx0, indexer_k_pe, indexer_k_nope, 0);
9292
cb(indexer_k, "indexer_k", il);
9393

94-
indexer_q = ggml_mul_mat(ctx0, inp_attn_dsa->self_k_rot_lid, indexer_q);
95-
indexer_k = ggml_mul_mat(ctx0, inp_attn_dsa->self_k_rot_lid, indexer_k);
94+
// Online Hadamard rotation (PR #21038) — only built when the lid cache is quantized.
95+
// Note on direction: ggml_mul_mat(A, B) contracts on dim 0, i.e. computes Aᵀ·B,
96+
// so these calls apply Hᵀ to q and k. That is safe because ggml_gen_hadamard
97+
// returns a symmetric orthogonal matrix (H = Hᵀ, H² = I), so Hᵀx and Hx are the
98+
// same tensor — the direction cannot mismatch with the cache's internal k-shift
99+
// path. If the rotation is ever swapped for a non-symmetric orthogonal matrix,
100+
// both sites must agree on H vs Hᵀ.
101+
if (inp_attn_dsa->self_k_rot_lid) {
102+
indexer_q = ggml_mul_mat(ctx0, inp_attn_dsa->self_k_rot_lid, indexer_q);
103+
indexer_k = ggml_mul_mat(ctx0, inp_attn_dsa->self_k_rot_lid, indexer_k);
104+
}
96105

97106
const auto * mctx_lid = inp_attn_dsa->mctx->get_lid();
98107
const auto & k_idxs_lid = inp_attn_dsa->get_k_idxs_lid();

0 commit comments

Comments
 (0)