Repository navigation
[Triton/Gluon] [gfx1250] unified_attention: fast and correct spec-decode verify - #6896
Conversation
… 3D kernel On gfx1250 every batch that is not all-decode takes the 2D Gluon kernel. For speculative-decode verify steps (q_len = 1 + num_spec) its grid is about two workgroups per sequence: at q_len 4, ctx 1280, Qwen3.5 shapes (16 Q / 2 KV heads, head_dim 256) it takes ~350 us at batch 1 and ~450 us at batch 128, against 7 us and 65 us (+ reduce) for the 3D split-KV kernel. Send batches whose longest query is at most 16 tokens to the 3D kernel. That makes the 3D kernel see q_len > 1, which it has never been run with on gfx1250 (all-decode only). The rows of a query block then have different causal horizons, and three things break: - softmax_part0: a row that sees no key in its segment (the segment lies past that row's horizon but inside seq_len) has m_ij = -inf, and exp2(-inf - -inf) is NaN. Shift such rows by 0 so p and alpha are 0, and keep M = -inf for the reduce. - Epilogue: when a query block's horizon ends before the segment, tile_end == tile_start. The epilogue then computes the prologue's tile_start block, but builds seq_offset from tile_end - 1, the previous tile, so keys past the horizon pass the causal mask. Mask the whole tile for an empty segment. - Main loop: it never applies the causal mask, assuming every tile but the last is visible to all rows. With BLOCK_Q > 2 (GQA ratio < 8) a tile before the last can hold keys past an early row's horizon. Apply the causal mask in the loop when the batch is not all-decode; ALL_DECODE is a constexpr, so the decode kernel is unchanged. test_unified_attn_3d_spec_verify_gfx1250 covers mixed query (1..4, 1..8) and context lengths for GQA ratios 8, 4, 2 and 1 at head_dim 128 and 256, with strict tolerance and a NaN check. Reverting any one of the three kernel fixes fails it (NaN, or wrong rows). Qwen3.5-35B-A3B MXFP4 with MTP-3 in ATOM on gfx1250, TP1, 1k/1k: GSM8K 0.864 (0.864 without MTP; 0.595 with the routing change alone). Throughput at mean acceptance length 3.4 is 1.26x at concurrency 1 and 1.83x at 64 and 128 over no MTP.
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: |
There was a problem hiding this comment.
🟡 Changes recommended
The expanded dispatch exposes unsupported output strides, query-bias shapes, and FP16 operands in the 3D path.
3 open issues
What changed in this PR
This PR accelerates speculative-decode verification on gfx1250 by routing short queries through unified attention’s 3D split-KV path.
Changes:
- Routes queries of up to 16 tokens to 3D, while retaining 2D for sliding-window attention and longer queries.
- Corrects masked-row softmax handling, empty-segment masking, and main-loop causal masking.
- Adds 64 regression cases covering mixed query lengths, context lengths, and head configurations.
| File | Description |
|---|---|
op_tests/triton_tests/attention/test_unified_attention.py |
Adds speculative-decode verification tests for gfx1250. |
aiter/ops/triton/attention/unified_attention.py |
Extends 3D dispatch to short multi-token queries. |
aiter/ops/triton/_gluon_kernels/gfx1250/attention/unified_attention_3d.py |
Fixes softmax and causal masking for multi-token queries. |
Review effort: Balanced
…he 2D kernel The 2D Gluon kernel pads a short query (speculative-decode verify) to the prefill BLOCK_M of 128 rows. Size BLOCK_M to the query rows instead (next_pow2(max_seqlen_q * num_queries_per_kv), at least 16), with one 16-row WMMA tile per warp and the plain double-buffered loop. Qwen3.5 shapes (16 Q / 2 KV heads, head_dim 256), q_len 4, ctx 1280: batch 1, 64 and 128 go from 350, 408 and 449 us to 17, 24 and 32 us. With that, the 2D kernel beats the 3D split-KV kernel once a short-query batch fills the GPU, and the 3D kernel only wins while it does not. Route short queries to 3D while num_tokens * num_query_heads <= 8 * num_sms, and to 2D past that. The rule uses only the batch shape, since max_seqlen_k is the model's max length during CUDA-graph capture. Over q_len 4 and 8, ctx 1280 to 8192, and heads 16/2 (head_dim 256) and 64/8 (head_dim 128), it keeps the faster kernel or one within a few microseconds of it; always-3D was up to 4.5x slower at batch 128. test_unified_attn_spec_verify_gfx1250 now runs each case on the 3D kernel, the 2D kernel (with and without a 256-token sliding window), and the default route.


Problem
On gfx1250,
use_2d_kernelsends every batch that is not all-decode to the 2D Gluon kernel. Speculative-decode verify steps (q_len = 1 + num_spec) are such batches, and the 2D kernel pads their few query rows to the prefillBLOCK_Mof 128:In ATOM with MTP-3 on Qwen3.5-35B-A3B, this kernel took about half of every decode step, so speculative decoding was slower than no speculation at concurrency 4 and above.
Fix
2D kernel: size
BLOCK_Mto short queriesFor short queries,
_unified_attention_2d_gfx1250now setsBLOCK_Mtonext_pow2(max_seqlen_q * num_queries_per_kv)(at least 16), with one 16-row WMMA tile per warp and the plain double-buffered loop. At the shapes above it takes 17, 24 and 32 µs.Routing: 3D while the batch is small
The 3D split-KV kernel still wins while a short-query batch is too small to fill the GPU, especially at long context. On gfx1250, short queries (
max_seqlen_q <= 16) take the 3D kernel whilenum_tokens * num_query_heads <= 8 * num_sms, and the 2D kernel past that. The rule uses only the batch shape, because during CUDA-graph capturemax_seqlen_kis the model's max length.GPU µs per call, 2D (this PR) / 3D + reduce. Bold is the kernel the rule picks.
Over q_len 4 and 8 the rule picks the faster kernel, or one within a few microseconds of it.
3D kernel: q_len > 1 fixes
Until now the gfx1250 3D kernel only ever ran with one query per sequence. With q_len > 1 the rows of one query block have different causal horizons, and three things in
unified_attention_3d.pybreak:softmax_part0). A row can see no key in its segment: the segment is past that row's horizon but still insideseq_len. Itsm_ijis -inf, andexp2(-inf - -inf)is NaN. The guard for this was commented out. The fix shifts such rows by 0, sopandalphaare 0, and keepsM = -infso the reduce weights the segment 0.tile_end == tile_start. The epilogue then computes the prologue'stile_startblock but buildsseq_offsetfromtile_end - 1, the previous tile, so keys past the horizon pass the causal mask. The fix masks the whole epilogue tile when the segment is empty.BLOCK_Q > 2(GQA ratio below 8 atBLOCK_M = 16), a tile before the last can hold keys past an early row's horizon. The fix applies the causal mask inside the loop when the batch is not all-decode.ALL_DECODEis a constexpr, so the decode kernel is unchanged.Testing
test_unified_attn_spec_verify_gfx1250(new, 256 cases) covers:It checks with strict
assert_closeand asserts the output has no NaN. All 256 cases pass. Reverting any one of the three 3D-kernel fixes makes the 3D cases fail: fix 1 gives NaN, fixes 2 and 3 give wrong rows.Other tests on gfx1250:
test_triton_unified_attn_asym_head: 192/192 pass. Its[(1, 137), (5, 1024), (1, 19)]case now takes the 3D kernel.test_triton_unified_attn_3d: the same 6 fp8/nvfp4 failures with and without this change.(test_triton_unified_attn and not 3d) or asym_head: 2,492 passed, 136 failed, 1,664 skipped. The same 136 cases fail without this change: 128 are fp16 cases the gfx1250 Gluon kernel rejects, and 8 are bf16 non-shuffled cases with softcap and a sliding window.Environment note: the gfx1250 Triton I tested on (
shared/gfx1250, 2026-10-22) takestdm.async_gather(desc, rows, dst), with the column offset moved into the descriptor. So I ran these tests with the twoasync_gather(..., 0, dst)calls inunified_attention_3d.pychanged to the 3-argument form. This PR leaves those calls as they are on main.End to end: Qwen3.5-35B-A3B MXFP4 in ATOM, MTP-3, TP1, gfx1250.
1k/1k serving throughput with MTP-3 at mean acceptance length 3.4 (GSM8K's measured value), against no MTP: