fix: build_attn V-padded reshape uses Q-head count, not KV-head count (#78)#116
Merged
TheTom merged 1 commit intofeature/turboquant-kv-cachefrom May 1, 2026
Merged
Conversation
…nt (#78) Post-attention V-padded reshape in build_attn was using hparams.n_head_kv(il), but cur returned from build_attn_mha has shape (n_embd_head * n_head, n_tokens) — n_head is the Q-head count. On GQA models where n_head != n_head_kv (e.g. Qwen2.5-0.5B with head_dim=64 padded → 128, n_head=14, n_head_kv=2), the reshape element count fails the assertion in ggml_reshape_3d and the process aborts. Symptom: GGML_ASSERT(ggml_nelements(a) == ne0*ne1*ne2) at ggml.c:3656. Reported and diagnosed by @bingh0 in #78. Verified locally on Qwen2.5-7B (head_dim=128, no padding, regression check passes) and on AMD MI300X with Qwen2.5-0.5B (head_dim=64, was crashing pre-fix). Three sites fixed (lines 2285, 2412, 2532 — same idiom in three build_attn overloads). Closes #78. Likely also closes #108 (speculative decoding hits the same assertion). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
dbf3d40 to
b6f8e7f
Compare
This was referenced May 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #78 (and likely #108 — same assertion in the speculative decoding path). One-liner diagnosed by @bingh0:
The post-MHA reshape in
build_attnuseshparams.n_head_kv(il), butcurreturned frombuild_attn_mhahas shape(n_embd_head * n_head, n_tokens)—n_headis the Q-head count, not the KV-head count. On GQA models wheren_head != n_head_kv, the element count fails the assertion inggml_reshape_3dand the process aborts with:The bug only triggers when V padding is also active (head_dim < 128 → padded to 128) AND n_head ≠ n_head_kv, which is why it didn't surface on Qwen2.5-7B (head_dim=128, no padding) but does on Qwen2.5-0.5B (head_dim=64 → padded → reshape executes → crash).
Three identical sites in three
build_attnoverloads (lines 2285, 2412, 2532).Validation
AMD MI300X (gfx942, ROCm 7.2)
GGML_ASSERTabort, core dumpedMetal (M5 Max), regression check
PPL match exactly — the fix is element-count correctness, not numerical change.
Caveats
Closes
build_attnGGML_ASSERT)Credit: @bingh0 for the diagnosis and proposed fix.
🤖 Generated with Claude Code