Repository navigation
fix: build_attn V-padded reshape uses Q-head count, not KV-head count (#78) - #116
Merged
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) <[email protected]>
TheTom
force-pushed
the
fix/issue-78-gqa-reshape
branch
from
May 1, 2026 13:33
dbf3d40 to
b6f8e7f
Compare
This was referenced May 1, 2026
crusaderky
pushed a commit
to crusaderky/llama.cpp
that referenced
this pull request
Aug 9, 2026
…sharedmem fix(cuda): KVarN decode combine kernel crashes at ≥768K context (missing shared-mem opt-in)
crusaderky
pushed a commit
to crusaderky/llama.cpp
that referenced
this pull request
Aug 9, 2026
…path Follow-up to 695a3f4 (PR TheTom#116), which opted the KVarN decode combine kernel into the larger dynamic-shared-mem limit but introduced two problems: - The attribute was set per launch to the current demand. Since cudaFuncAttributeMaxDynamicSharedMemorySize is a ceiling, a small early launch shrank it below what a later, larger launch of the same kernel template needed. test-kvarn hit exactly this: an MMA decode launch (n_splits=16, 64B) ran before a VEC launch (n_splits=64, 256B) and the VEC launch failed with cudaErrorInvalidConfiguration ("invalid argument"). - The VEC combine launcher (fattn-kvarn-vec.cuh) was never opted in at all; with split_tokens as low as 8 it hits the 48KB default ceiling at ~196K context, far below the 768K threshold of the MMA path. Fix by raising the limit once per device to the device opt-in max (cached ggml_cuda_info().smpbo instead of a live query), shared via a new ggml_cuda_fattn_kvarn_decode_combine_prepare<D>() helper called from both combine launch sites. The raise accounts for the kernel's static reduce_sh[] footprint (cudaFuncSetAttribute rejects value + static > smpbo), and a GGML_ASSERT turns the residual over-limit case (planner does not gate on combine shared-mem demand) into a clear message instead of an opaque launch failure. Verified on RTX 3090 (sm_86): full ctest suite 75/75 (test-kvarn crashed pre-fix), plus a Qwen3.6-27B kvarn4/kvarn4 + FA decode smoke test at 32K context.
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