fix(flex): support GQA/MQA in attention (q_heads % kv_heads == 0)#5142
Open
AnayGarodia wants to merge 2 commits into
Open
fix(flex): support GQA/MQA in attention (q_heads % kv_heads == 0)#5142AnayGarodia wants to merge 2 commits into
AnayGarodia wants to merge 2 commits into
Conversation
Charles23R
approved these changes
Jul 16, 2026
`attention_impl` and `attention_naive_impl` asserted `k_heads == q_heads` and `v_heads == q_heads`, panicking on ONNX-compliant Group Query Attention (GQA) and Multi-Query Attention (MQA) where `kv_heads < q_heads`. Relax the head-count checks to require `q_heads % kv_heads == 0` (matching the ONNX `Attention` spec and burn-ndarray), and map each query head `h` to its shared K/V head `h / (q_heads / kv_heads)`. K/V batch strides now use `kv_heads`. Plain MHA is the special case `q_heads == kv_heads`. Add tests asserting GQA (4 q-heads, 2 kv-heads) and MQA (4 q-heads, 1 kv-head) match plain MHA with K/V heads repeated, across both the naive and flash inner loops.
AnayGarodia
force-pushed
the
fix/4930-flex-attention-gqa
branch
from
July 17, 2026 19:30
25231dc to
15ff2b5
Compare
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.
Fixes #4930.
Problem
attention_impl(flash path) andattention_naive_implasserted strict head-count equality between Q and K/V, panicking on ONNX-compliant Grouped-Query / Multi-Query Attention wherekv_heads < q_heads.Fix
Relax the check to require
q_heads % kv_heads == 0(matching the ONNXAttentionspec and burn-ndarray) and map each query head to its shared K/V headkv_h = h / (q_heads / kv_heads). K/V batch strides now usekv_heads. Standard MHA is theq_per_kv == 1special case; mask/bias broadcasting is unchanged (still keyed on q-heads). Applied identically to both the flash and naive paths.Testing
Added
test_attention_gqa_matches_repeated_kv(4 q-heads / 2 kv-heads) andtest_attention_mqa_matches_repeated_kv(4 q-heads / 1 kv-head); both assert grouped output equals plain MHA with K/V heads repeatedq_heads/kv_headstimes, exercising both loops.