Fix broadcast crash in quantized SDPA with GQA + batched padding mask (batch >= 2)#1467
Open
pinglin wants to merge 1 commit into
Open
Fix broadcast crash in quantized SDPA with GQA + batched padding mask (batch >= 2)#1467pinglin wants to merge 1 commit into
pinglin wants to merge 1 commit into
Conversation
For grouped-query attention (n_repeats > 1) the quantized attention path reshapes scores to 5D (B, n_kv_heads, n_repeats, L, L) but leaves the mask unchanged, so a batched padding mask (B, 1, L, L) fails to broadcast at batch >= 2 (its B axis aligns against n_kv_heads). Insert a singleton n_repeats axis into the mask so it broadcasts across the repeat groups; guarded on mask.ndim >= 3 so the 2D causal and MHA (n_repeats == 1) paths are untouched.
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
quantized_scaled_dot_product_attentioncrashes with[broadcast_shapes] … cannot be broadcastfor any grouped-query-attention model using a quantized KV cache once the batch size is ≥ 2. This makeskv-bitsunusable with batched decoding on GQA models (e.g. Qwen3, Qwen3.5/3.6). Tested onmain@2ed22318.Root cause
For GQA (
n_repeats = n_q_heads // n_kv_heads > 1) the function reshapes queries to 5D(B, n_kv_heads, n_repeats, L, D), soscoresbecome 5D(B, n_kv_heads, n_repeats, L, L)— but the mask is passed through unchanged. A batched padding mask is 4D(B, 1, L, L). Right-aligned broadcasting lines the mask'sBaxis up againstn_kv_heads: harmless atB == 1(1-vs-n_kv_heads), but atB ≥ 2it becomesB-vs-n_kv_heads(e.g.2vs8) with neither equal to 1 → broadcast failure.Minimal repro
Direct call to
quantized_scaled_dot_product_attentionwith GQA dims (n_q_heads=40, n_kv_heads=8→n_repeats=5), 8-bit quantized KV, batch=2, a(2,1,L,L)bool mask:Fix
Inserts a singleton
n_repeatsaxis so(B,1,L,L)→(B,1,1,L,L)and broadcasts across the repeat groups. Guarded onmask.ndim >= 3so the 2D causal-string path and the MHA (n_repeats == 1) path are untouched.Verification
Patched, same B=2 GQA quantized-KV inputs:
Also verified end-to-end: Qwen3.6-35B-A3B with
--kv-bits 8and 8-way in-flight — which crashed on every batch ≥ 2 before — now serves concurrent generations cleanly (0 broadcast errors, coherent output).