llama : fix rotation of attention tensors in layers with shared KV (#31406)

* llama : fix rotation of layers that reuse another layer's KV cache

* llama : use KV source rotation only for attn_* tensors
This commit is contained in:
Niklas Wenzel authored and GitHub committed 2026-10-31 23:30:49 +01:00
1 parent ddf48f85c9
commit 0138937e3f
5 files changed
+23 -27

No files matched your search

+6
View File
@@ -11,6 +11,12 @@ void llama_model_gemma4::load_arch_hparams(llama_model_loader & ml) {
hparams.n_layer_kv_from_start = hparams.n_layer_all - (int32_t)n_kv_shared_layers;
hparams.f_attention_scale = 1.0f; // Gemma4 uses self.scaling = 1.0 (no pre-attn scaling)
// shared layers read the KV of the last layer with own KV and the same SWA type
GGML_ASSERT(hparams.n_layer_kv_from_start >= 2);
for (uint32_t il = hparams.n_layer_kv_from_start; il < hparams.n_layer_all; il++) {
hparams.kv_source_impl[il] = hparams.n_layer_kv_from_start - (hparams.is_swa(il) ? 2 : 1);
}
ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA, hparams.rope_freq_base_train_swa, false);
ml.get_key_or_arr(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp_arr, hparams.n_layer_all, false);
ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa);