Skip to content

[Triton/Gluon] [gfx1250] unified_attention: fast and correct spec-decode verify - #6896

Merged
azaidy merged 2 commits into
mainfrom
vigokhal/gfx1250-ua-3d-spec-verify
Oct 27, 2026
Merged

azaidy merged 2 commits into
mainfrom
vigokhal/gfx1250-ua-3d-spec-verify

Conversation

@vgokhale

@vgokhale vgokhale commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

Problem

On gfx1250, use_2d_kernel sends 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 prefill BLOCK_M of 128:

Qwen3.5 shapes (16 Q / 2 KV heads, head_dim 256), q_len 4, ctx 1280 2D (main)
batch 1 350 µs
batch 64 408 µs
batch 128 449 µs

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_M to short queries

For short queries, _unified_attention_2d_gfx1250 now sets BLOCK_M to 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. 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 while num_tokens * num_query_heads <= 8 * num_sms, and the 2D kernel past that. The rule uses only the batch shape, because during CUDA-graph capture max_seqlen_k is the model's max length.

GPU µs per call, 2D (this PR) / 3D + reduce. Bold is the kernel the rule picks.

q_len 4 bs 1 bs 16 bs 32 bs 64 bs 128
16/2 heads, hd 256, ctx 1280 17 / 9 18 / 15 18 / 24 24 / 45 32 / 86
16/2 heads, hd 256, ctx 8192 87 / 15 128 / 58 132 / 108 141 / 200 167 / 389
64/8 heads, hd 128, ctx 1280 10 / 8 11 / 22 19 / 58 30 / 103 55 / 160
64/8 heads, hd 128, ctx 8192 47 / 12 111 / 170 120 / 324 155 / 628 310 / 964

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.py break:

  1. NaN from fully masked rows (softmax_part0). A row can see no key in its segment: the segment is past that row's horizon but still inside seq_len. Its m_ij is -inf, and exp2(-inf - -inf) is NaN. The guard for this was commented out. The fix shifts such rows by 0, so p and alpha are 0, and keeps M = -inf so the reduce weights the segment 0.
  2. Wrong mask in the epilogue for an empty segment. 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. The fix masks the whole epilogue tile when the segment is empty.
  3. No causal mask in the main loop. The loop assumes every tile but the last is visible to all rows of the block. With BLOCK_Q > 2 (GQA ratio below 8 at BLOCK_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_DECODE is a constexpr, so the decode kernel is unchanged.

Testing

test_unified_attn_spec_verify_gfx1250 (new, 256 cases) covers:

  • mixed query lengths (1..4 and 1..8) and mixed context lengths (up to 3000);
  • GQA ratios 8, 4, 2 and 1, at head_dim 128 and 256;
  • batch sizes 7 to 128;
  • four paths for each case: the 3D kernel, the 2D kernel, the 2D kernel with a 256-token sliding window, and the default route.

It checks with strict assert_close and 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.
  • Decode routing and the decode kernel are unchanged.

Environment note: the gfx1250 Triton I tested on (shared/gfx1250, 2026-10-22) takes tdm.async_gather(desc, rows, dst), with the column offset moved into the descriptor. So I ran these tests with the two async_gather(..., 0, dst) calls in unified_attention_3d.py changed 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.

GSM8K 3-shot (flexible / strict)
no MTP 0.864 / 0.851
MTP-3, routing to 3D without the kernel fixes 0.595 / 0.579
MTP-3, this PR 0.867 / 0.859

1k/1k serving throughput with MTP-3 at mean acceptance length 3.4 (GSM8K's measured value), against no MTP:

Concurrency No MTP (tok/s) MTP-3 (tok/s) Speedup
1 677 847 1.25x
8 4,667 6,056 1.30x
32 14,229 20,259 1.42x
64 21,361 38,914 1.82x
128 30,062 56,618 1.88x

… 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.
@vgokhale
vgokhale requested a review from a team October 24, 2026 01:30
@github-actions

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

  • ✅ Pre-checks (submodule verification, code formatting)
  • ✅ Aiter op tests (gfx942 + gfx950)
  • ✅ Triton tests on MI35X (only when aiter/ops/triton/** or related paths are changed)

Extended tests (opt-in via labels):

Label Tests
ci:gfx1250-ffm-triton Run the five-shard gfx1250 FFM Triton test suite
ci:triton-300x Run an additional Triton test job on MI300X in PRs (added automatically when gfx942 configs change); main branch always runs both MI35X and MI300X
ci:triton-355 Run the full Triton test suite on MI35X, not only the tests the change affects
ci:windows Windows JIT op smoke test on the gfx1201 ROCm runner
ci:multi-gpu Aiter multi-GPU tests on the 8-GPU runner
ci:sglang SGLang integration tests: DeepSeek-R1-MXFP4 accuracy, Qwen 3.5 accuracy
ci:atom ATOM benchmark: DeepSeek-R1-0528, GPT-OSS-120B
ci:atom_full ATOM accuracy suite for PR and main models from ATOM models_accuracy.json
ci:vllm vLLM benchmark: DeepSeek-V4-Pro, MiniMax-M3-MXFP4
ci:all All standard extended tests (excludes ci:atom_full)

Only add ci:atom_full for FlyDSL or Triton upgrades.
Add labels via the sidebar or gh pr edit 6896 --add-label <label>

One backend per PR:
A PR changes one kernel backend: [Triton/Gluon] (Triton and Gluon count as one), [HIP], [ASM], [CK], [OPUS] or [FlyDSL]. If the title ends up with two backend tags, split the PR -- as stacked pull requests when one part cannot merge without the other.

PR title tags & labels:
Component tags ([Triton/Gluon], [HIP], [CK], [ASM], ...) are added to the PR title and as PR labels automatically from the changed files and re-synced on every push — change-type tags like [fix]/[Perf], op tags like [MLA], and human labels (ci:*) are left untouched. Add the no-auto-title label to stop the title rewrites; labels stay in sync either way.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.
@vgokhale vgokhale changed the title [Triton/Gluon] [gfx1250] unified_attention: spec-decode verify on the 3D kernel [Triton/Gluon] [gfx1250] unified_attention: fast and correct spec-decode verify Oct 24, 2026
@zufayu
zufayu requested review from a team and Dewei-Wang-sh October 26, 2026 01:07

@azaidy azaidy left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants