Commit 4066b9d40 for llama.cpp
commit 4066b9d406ec1dd4f82809cd735f515adca607ce
Author: Aman Gupta <amangupta052@gmail.com>
Date: Sat Dec 5 19:06:10 2026 +0800
llama : place Gemma 4 assistant attention heads with the shared target KV for -sm tensor (#33659)
The Gemma 4 assistant (MTP) model reads the KV cache of the last target layer
with own KV of each SWA type. With -sm tensor, the meta split rotation of the
assistant's attention heads was computed from its own layer index, while the
target's KV cache used the target's layer index, so for single-KV-head layers
the query heads and the KV ended up on different devices:
GGML_ASSERT(all_axes_none_or_mirrored) failed (FLASH_ATTN_EXT)
For Gemma 4 targets, count the layers with own KV from the end, so the KV of
the last layer of each SWA type always gets rotation 0, and use rotation 0 for
the assistant's attention tensors.
Fixes #33572
Assisted-by: Claude Opus 5.5
diff --git a/src/llama-model.cpp b/src/llama-model.cpp
index 4de6e71c1..d2b774709 100644
--- a/src/llama-model.cpp
+++ b/src/llama-model.cpp
@@ -538,6 +538,21 @@ struct ggml_backend_meta_split_state llama_meta_device_get_split_state(const str
return ret;
};
+ // a Gemma 4 assistant (MTP) model shares the KV cache of the last target layer with own KV of each SWA type,
+ // but it cannot see the target's layer layout when its weights are split,
+ // so count the layers with own KV from the end for the target: the shared layers then always get rotation 0
+ auto get_il_eff_kv = [&](const size_t il){
+ if (ud->model->arch != LLM_ARCH_GEMMA4) {
+ return get_il_eff(il);
+ }
+ size_t ret = 0;
+ const bool il_is_swa = hparams.is_swa(il);
+ for (size_t il_next = il + 1; il_next < hparams.n_layer(); il_next++) {
+ ret += hparams.has_kv(il_next) && hparams.is_swa(il_next) == il_is_swa;
+ }
+ return ret;
+ };
+
uint32_t il;
std::string prefix;
size_t rotation;
@@ -547,14 +562,19 @@ struct ggml_backend_meta_split_state llama_meta_device_get_split_state(const str
prefix = tensor_name.substr(0, length_prefix + 1);
il = std::stoull(tensor_name.substr(4, length_prefix));
const bool is_attn = tensor_name.compare(prefix.size(), 5, "attn_") == 0;
- // attention heads must be on the same device as the KV of the source layer
- rotation = get_il_eff(is_attn ? hparams.kv_source(il) : il) % ud->n_devices;
+ if (is_attn && ud->model->arch == LLM_ARCH_GEMMA4_ASSISTANT) {
+ // the assistant reads the KV of the last target layer with own KV and the same SWA type, see get_il_eff_kv
+ rotation = 0;
+ } else {
+ // attention heads must be on the same device as the KV of the source layer
+ rotation = (is_attn ? get_il_eff_kv(hparams.kv_source(il)) : get_il_eff(il)) % ud->n_devices;
+ }
} else if (tensor_name.substr(0, 6) == "cache_") {
const size_t layer_index_start = tensor_name.find("_l", 6);
GGML_ASSERT(layer_index_start != std::string::npos);
il = std::stoull(tensor_name.substr(layer_index_start + 2));
prefix = "blk." + std::to_string(il) + ".";
- rotation = get_il_eff(il) % ud->n_devices;
+ rotation = get_il_eff_kv(il) % ud->n_devices;
} else {
il = 0;
rotation = hparams.n_layer() % ud->n_devices;