Repository navigation
[Triton/Gluon] [GFX12] A8K4V8 Attention - #7421
Conversation
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: |
WAITING ON AUTHORPR Self-ReviewRevision Waiting for the PR author. The readiness report will appear after the self-review answers are complete for this revision. Questions to answer in the PR description
Edit the Missing or invalid answers
Shadow mode reports what would block but keeps the status successful. Set |
…2D NVFP4 support 3D Gluon kernel (A8W4 / A4W4 on an NVFP4 KV cache): - Fix the BF16 PV operand k_width (32 on a 16x16x32 WMMA dropped keys 16-31 of each 32 and doubled keys 0-15): k_width-8 packed V load, k_width-16 operands, matching V-scale layout. - force_fp8_pv_on_fp4_kv_cache: optional FP8 16x16x64 PV WMMA (default off; BF16 PV is faster and more accurate). - USE_STORE_TDM (priority over USE_STORE_BUFFER_OP): output through LDS + TDM store in TDM_STORE_SPLIT head-dim pieces (2 for HEAD_SIZE >= 256). Cuts the A8W4 epilogue spill 95 -> 33 VGPRs; TP4 decode 113 -> 105 us, q4 64x32k 99 -> 88 us. - AITER_UA_FP4_HEAD_SPLIT experiment knob (HEAD_SIZE_SPLIT=2, not faster; default 1). 2D Gluon kernel: NVFP4 KV support (A8W4 and A4W4), loop variant 0 with one 128-token page per tile, waves_per_eu 1. Sliding-window NVFP4 calls (routed to 2D) now run instead of failing to compile in the generic Triton kernel. Bench: -nvfp4_kv / -nvfp4 / -fp8_pv; chunked NVFP4 cache quantization and the fp32 reference copies only with -test, so -b 2048 -sk 32768 fits. Test: force_fp8_pv parametrization for the 3D test. Co-Authored-By: Claude <[email protected]>
…with 64-token pages - 3D / 2D kernels: the K and V cache dtypes are separate (3D V_CACHE_DTYPE, 2D V_NVFP4; default = the K dtype). K-side code follows the K dtype, V-side code the V dtype, so an NVFP4 K with an fp8 V runs the A8W4 scaled QK and the A8W8 fp8 PV (no V upcast). - TDM wait counts follow the loads per page (NVFP4: data + scales) instead of fixed numbers. - NVFP4 K scales preshuffled in min(128, page)-row groups, so A8K4V8 also runs 64-token pages. - Wrapper: v_cache_dtype; 2D gate and the shuffled head-size check accept an NVFP4 K with fp8 V. - Bench: -k4v8; NVFP4 cache builder for 64-token pages. Co-Authored-By: Claude <[email protected]>
…t store The padded layout on the [token, q-head, dim] box puts columns in the wrong place once a KV head has more than 4 query heads (bf16, D 256, 16/2 heads: columns 36/37/44/45 wrong), and the TDM store then writes outside its box: with a single 256-wide piece it faulted the GPU (out-of-bounds write) and wedged the queue. The unpadded layout is correct for every case tested. Co-Authored-By: Claude <[email protected]>
…ope_reshape_and_cache fused_qk_rope_reshape_and_cache now writes an A8K4V8 cache when key_cache is uint8 (NVFP4) and value_cache is FP8: K goes through the existing NVFP4 quantization into the shuffled NVFP4 layout, V is scaled by 1/v_scale and stored as FP8 in the shuffled (T_cache, KH, block_size // 16, D, 16) layout that shuffle_kv_cache builds and unified_attention reads (k_descale = 1, v_descale = v_scale). The mode is selected by the cache dtypes; no new arguments. The NVFP4 row store is factored into _store_nvfp4_shuffled_row and the scale preshuffle group is now min(128, block_size) (SCALE_PRESHUFFLE) instead of a fixed 128, so 64-token pages match nvfp4_quant_shuffle_cache and what the attention kernels unshuffle; 128-token pages are unchanged. Tests: test_fused_qk_rope_reshape_and_cache_k4v8 (decode / prefill slots, pages 64 / 128, head 64 / 128 / 256, KH 1 / 4) checks the whole caches against the attention tests' builders (V bit-exact, K bit-exact except e2m1 codes at exact rounding ties, see _assert_nvfp4_cache_equal_up_to_ties); test_k4v8_cache_write_unified_attention runs unified_attention on a written cache against ref_paged_attn. Co-Authored-By: Claude <[email protected]>
…-page tiles (A8K4V8) - Gather loader: NVFP4 K pages (data and scales, two gathers) with fp8 V pages, so A8K4V8 runs tiles of several 64- or 128-token pages. K scales layout spans the tile. - Loop variant 1: K scales next to K; P in Q's dtype (the packed K is uint8). - TDM waits: each wait site of loop variants 0 and 1 names the loads issued after the awaited one, and the loader waits for their op count (NVFP4 page = 2 ops, data then scales). For one-op caches this equals the old wait count (asserted), so fp8 / bf16 are unchanged. - tdm.async_gather calls go through _tdm_gather, which takes the source column only when this Triton's signature has it (upstream: yes; some gfx1250 builds: no). - Wrapper: an NVFP4 V (A8W4) keeps loop variant 0 with one page per tile; NVFP4 K with fp8 V takes loop variant 0 or 1 (others map to 1), two buffers. A8K4V8 prefill -b 1 -sq 4096 -sk 4096 (2D): 28.3 -> 19.6 us at page 128 (loop variant 1, tile 128, BLOCK_M 64), 24.7 -> 19.2 us at page 64 (loop variant 1, tile 256). Co-Authored-By: Claude <[email protected]>
…K4V8 pytest - Config lookup: a V cache dtype unlike the K cache's (A8K4V8: fp8 Q, NVFP4 K, fp8 V) first tries DT_<q>_<k>_<v> (e.g. DT_fp8_nvfp4_fp8), then the existing K-keyed chain (DT_q_k, DT_q_any, DT_any_k, any), so A8K4V8 can carry its own configs apart from A8W4's DT_fp8_nvfp4. Other dtype combinations look up as before. - test_triton_unified_attn_k4v8: 2D, 3D and auto routing x pages 64/128 x head size 128/256 x 4/1 and 16/2 heads x decode / spec-verify / prefill batches against the reference on the dequantized K and the fp8 V (72 cases, gfx1250 only). Co-Authored-By: Claude <[email protected]>
…n-NVFP4 K
load_k_scales_from_shared returned None for fp8 / bf16 K; loop variant 1 carries k_scales across
iterations and Triton cannot carry None ("Loop carried variable k_scales is not a triton value"),
so A8W8 with loop variant 1 (e.g. 16/2 heads, page 64, 4k prefill) failed to compile. A one-element
int32 placeholder instead; compute_qk ignores it without NVFP4 K.
Co-Authored-By: Claude <[email protected]>
…le_end (q_len > 1) With q_len > 1 (not ALL_DECODE) the main loop masked every KV tile, although only tiles at or past safe_tile_end can hold keys beyond an early query row's causal horizon: at most the sequence's last one or two tiles, so only the last segment has any. The loop now runs the tiles below safe_tile_end without the mask, then the rest with it. Pure decode (ALL_DECODE) keeps its single loop. A8K4V8 spec-decode verify (4 tokens, 16q/2kv, head 256, page 128), main kernel, bit-exact vs before: bs 25, ctx 32k-100k: 112.3 -> 104.7 us (12.4 -> 13.3 TB/s) bs 1024, ctx 32k-100k: 3327 -> 3177 us (16.4 -> 17.1 TB/s) bs 1024, ctx 64k: 3178 -> 3046 us (16.9 -> 17.6 TB/s) The masking (126 v_cmp + 127 v_cndmask per tile) and its SGPR-spill readlane/writelane traffic leave the hot loop. test_unified_attention.py -k "spec_verify_gfx1250 or k4v8 or equal_seqlen_q_gfx1250": 360 passed (on the 1104 production tree with this change). Co-Authored-By: Claude <[email protected]>
… avoid instruction cache eviction during production run
99ed847 to
5294aa7
Compare
…d q<=8 Ports the two attn_3d entries of 2fc36e4 (#7177, ...) from alizaidy/gfx1250-qwen-support; the rest of #7177 (cached launch paths, AITER_GFX12_MIN_SEGMENTS) is not part of this change. <the two entries> <why: no D256 bf16 entry past 4 query tokens, so MTP-4 verify fell to "any" (BLOCK_M 16, 3 query blocks a sequence, 32 -> 8 segments, 1117 vs 479 us); with the q<=8 entry ~1.17x the q=4 time> <lookup effect: GQA-keyed entries still win; q=1 changes only at G=2; q=5..8 picks the new entry at every G; 128-token pages and A8K4V8 unchanged> <test: spec_verify -k 256, 160 passed>
…ck ids at the row end With block_table_stride a runtime argument (the previous commit), the gather loader's `offs % block_table_stride` gave wrong outputs for spec-decode verify with a sliding window (test_unified_attn_spec_verify_gfx1250, 2d path, window 256, d 256, 4 / 8 / 16 kv heads: 20 cases) on this branch's Triton; as a constexpr it was right. The ids past the row only prefetch tiles the loop never reads, so clamp them to the last block as TDMKVLoader does. test_unified_attention.py then fails only where it fails without the runtime stride (the same 132 cases).
… decode and spec-decode verify
The gfx1250 gluon 3d kernel cuts every sequence's KV into the same NUM_SEGMENTS.
With mixed context lengths, the long sequences' segments run long after the rest
are done, and the GPU sits mostly idle. Qwen3.5 (16 q / 2 kv heads, d 256, fp8,
4-token verify), 64 sequences of 4k-191k tokens: KV read at 5.5 TB/s, against
16.7 TB/s for 64 x 64k.
AITER_GFX12_TOKEN_SPLIT=1 makes every segment cover the same number of tiles, so a
long sequence gets more segments rather than longer ones.
- Segment size is computed on the device each launch from the batch's total KV:
clamp(ceil(tiles x kv heads / 2048), 32, 256). The knobs are
AITER_GFX12_TOKEN_SPLIT_{PROGRAMS,MIN_TILES,MAX_TILES}. Because the computation is
plain torch ops, a CUDA graph recomputes it from the live batch on replay.
- A graph keeps its capture's grid, so the grid covers AITER_GFX12_TOKEN_SPLIT_MAX_KV
(262144) tokens at the minimum segment size. Spare segments exit at once, as empty
segments already did.
- The 3d kernel and both reduces (gluon and triton) take the segment size through a
new segm_tiles_ptr. When it is None, they compile today's code.
- While the split is on, short-query verify routes to the 3d kernel. The 2d kernel
runs one program per sequence over its whole context, which has the same tail.
One gfx1250 GPU, attention + reduce:
| batch | split off | split on |
|---|---:|---:|
| 64 mixed | 584 us | 274 us (2.1x faster) |
| 128 mixed | 1011 us | 431 us (2.3x faster) |
| uniform long contexts | - | 1-13% slower (the wider reduce) |
| 16 x 8k | 17 us | 40 us |
Outputs agree with the default split to within its own split-order rounding.
A graph captured with 4k contexts replays 4k-200k contexts identically to eager
execution.
op_tests/triton_tests/attention/test_unified_attention.py with the split on: 2806
passed. The 158 failures (fp16 inputs, shared-memory limits, 8 tolerance misses) fail
the same way with the split off.
End to end, ATOM Qwen3.5-35B-A3B MXFP4 at MLPerf agentic c512 on 4 x gfx1250 (DP4 / EP4, fp8 KV, MTP-3),
minutes 2-8 of a 10-minute point: generation 33.1k -> 35.8k tok/s (+8%), estimated
interactivity 109 -> 131 (+21%). MTP acceptance unchanged (70%).
… in one program AITER_GFX12_TOKEN_SPLIT computes the 3d kernel's tiles per segment from seqused_k before every launch with nine torch ops (add, floor-divide, sum, multiply, add, floor-divide, clamp, cast). AITER_GFX12_TOKEN_SPLIT_FUSED=1 (default 0) runs the same integer arithmetic as one single-program Triton kernel (gfx12_token_split_tiles): bitwise the same [1] int32, a fixed grid and no host read, so it captures into a CUDA graph and recomputes from the live seqused_k on every replay. MI455X, CUDA-graph replay, split alone: 12.8-14.6 us -> 1.42 us a call (bs 8-320). Qwen3.5 MTP-3 decode runs it 13 times a step (10 layers + 3 draft passes): 117 kernels -> 13, ~150-170 us a step. Full decode attention call (fp8 shuffled KV, 16/2 heads, d 256, 4k-190k contexts): 15-17 us faster at bs 8-80, output bitwise equal at bs 8-160. test_gfx12_token_split_tiles_fused: equality vs the torch ops over lengths / dtypes / heads / tile sizes, and on graph replay with rewritten lengths.
…8-256 segments (AITER_GFX12_SPLIT_REDUCE) The decode / spec-decode verify reduce after the gfx1250 3D attention with the token split (128- or 256-segment grid, ~9-25 live segments a sequence) ran the Triton reduce_segments: a binary search over cu_seqlens_q per program, one-warp programs over a [64 segments x 64 dims] tile at waves_per_eu=2, masked loads lowered to branches with a wait after each. Qwen3.5 c=1024 verify (340 tokens, 16 heads, d 256): 21 us in the trace. reduce_segments_split_gluon: one program per (query token, head), the sequence from the token (equal query lengths), the live segments' [8, D] partial tiles by TDM through two LDS buffers (descriptor extent = live segments), statistics by masked buffer loads; output gate / row selection epilogue as the Triton reduce's. Opt-in: AITER_GFX12_SPLIT_REDUCE=1 or unified_attention(split_reduce=True). Graph replay, synthetic partials of the 3D layout, 10k-150k-token contexts (cold): M=84 7.07 -> 5.18 us, 160 12.19 -> 7.88, 340 22.27 -> 7.77, 384 24.96 -> 7.97, 480 30.39 -> 8.02, drafter 85 x 256 segments 5.80 -> 3.03. Kernel time in situ after the real 3D kernel (profiler): 85 x 4 20.99 -> 7.41 us. Another fp32 summation order: ~0.005% of the bf16 outputs differ, by one rounding step (same max error against an fp64 merge).
reduce_segments_split_oneshot_gluon, behind unified_attention(split_reduce_oneshot=True) or AITER_GFX12_SPLIT_REDUCE_ONESHOT=1 (with the split reduce; default off). Bitwise reduce_segments_split_gluon: the same [S] statistics reduction and the same 8-segment merge steps in the same order. reduce_segments_split_gluon is latency bound (a few MB of fp32 partials, ~10-20x off the bandwidth roofline): a chain of dependent round trips -- context length, then the statistics and the first TDM step, then one L2 trip per 8-segment step (that step's maxima; one step of partials in flight), then the output gate after the loop. The new kernel has every load in flight at once: the first 64 segments' partials and maxima go to LDS by TDM (32 of them speculatively, before the context length arrives; the rest clipped to the live segments and skipped when none are left), the statistics and the gate alongside (the gate pinned before the merge loop), then the merge reads LDS only. Contexts past 64 segments (131k tokens at 16 tiles a segment) take one more round trip per 64. 672 instructions vs 811. Config AITER_GFX12_SPLIT_REDUCE_ONESHOT_CONFIG (CHUNK,SPEC,num_warps), default 64,32,2. Qwen3.5-35B-A3B a8k4v8, 16/2 heads, d 256, token split 16..128 tiles, output gate, graph replay with GEMM/pointwise kernels between calls (us, reduce alone): verify 4 rows/seq, ctx 35k 3.81 -> 3.00 verify 33k,56k 4.21 -> 2.84 verify 15k,35k,74k 4.33 -> 2.96 verify 28k,33k,56k,20k 3.68 -> 2.84 verify 15k,28k,35k,56k,74k 4.52 -> 3.44 verify 8 seqs 15k..110k 6.01 -> 3.40 draft 1 row/seq, 35k 3.04 -> 2.00 draft 33k,56k 4.23 -> 2.56 draft 28k,33k,56k,20k 4.21 -> 2.52 draft 15k,28k,35k,56k,74k 4.57 -> 2.68 The 3D attention kernel is unchanged. test_unified_attn_split_reduce_oneshot_k4v8: bitwise against the split reduce (gate / row-selection epilogue, 1..128 live segments, zero-length pad sequence, four configs).
8 38.8k 28.1 us 23.0 us 1.22× 18 52.2k 50.7 us 44.3 us 1.15× 22 49.4k 61.2 us 49.7 us 1.23× 32 47.3k 71.2 us 51.6 us 1.38× 64 47.8k 115.9 us 88.0 us 1.32×
This reverts commit 8e8f2eb. c64 A/B (3 pairs) showed no measurable difference: +0.7% req/s, -0.1% output tok/s, within run-to-run noise. Co-Authored-By: Claude Opus 5.5 <[email protected]>
There was a problem hiding this comment.
🟡 Changes recommended
Unresolved correctness issues permit out-of-bounds cache reads, invalid attention values from scaling masked logits, and incorrect NVFP4 decoding on a fallback path.
13 open issues
Apply scaling before the causal mask · New Prevent unsupported packed-byte casts for NVFP4 dispatch · New Honor or reject the FP8 PV option on the 2D route · New Allocate enough cache pages for the rectangular page table · New Set the rounding-check tensor dtype explicitly · New Move Triton tuning values into attention JSON configs · New Benchmark the gate and row-selection kernels · New Keep unrelated BF16 and FP8 retunes out of this PR · New Add a runnable benchmark for the cache-write wrapper · New Load cache-write warp settings from a family config · New Document the three-dtype config-key convention · New Reuse the shared batched scale-shuffle helper · New Test JSON-selected schedules instead of injecting tuning settings · New
What changed in this PR
This PR adds gfx1250 A8K4V8 support to AITER’s unified attention path, using FP8 queries and values with NVFP4 keys.
Changes:
- Adds mixed-format cache writes, attention dispatch, and dtype-specific configurations.
- Adds output gating, row selection, and split-rebalanced attention reduction.
- Extends correctness tests and benchmarks for mixed-precision attention.
| File | Description |
|---|---|
op_tests/triton_tests/fusions/test_fused_kv_cache.py |
Tests mixed-format cache writes and attention integration. |
op_tests/triton_tests/attention/test_unified_attention.py |
Tests A8K4V8 dispatch, gating, row selection, and reduction. |
op_tests/op_benchmarks/triton/bench_unified_attention.py |
Adds NVFP4 and A8K4V8 benchmark modes and reference inputs. |
aiter/ops/triton/utils/unified_attention_utils.py |
Adds separate V-cache dtype matching to config lookup. |
aiter/ops/triton/fusions/fused_kv_cache.py |
Exposes mixed-format cache writes through public wrappers. |
aiter/ops/triton/configs/gfx1250/gluon/attention/unified_attention/DEFAULT.json |
Adds mixed-format schedules and existing-format tuning entries. |
aiter/ops/triton/attention/unified_attention.py |
Integrates mixed-format dispatch, output epilogues, and split rebalancing. |
aiter/ops/triton/_triton_kernels/fusions/fused_kv_cache.py |
Implements NVFP4-key and FP8-value cache stores. |
aiter/ops/triton/_triton_kernels/attention/unified_attention.py |
Adds output epilogues and segment-size calculation. |
aiter/ops/triton/_gluon_kernels/gfx1250/attention/unified_attention_reduce.py |
Adds reduction for rebalanced segments with optional gating and row selection. |
aiter/ops/triton/_gluon_kernels/gfx1250/attention/unified_attention_3d.py |
Extends mixed-format loading, PV computation, segment scheduling, and output stores. |
Review effort: Balanced
…7958) * [Triton/Gluon] unified_attention: expose the gfx950 Gluon gate as gfx950_gluon_supported `_gfx950_gluon_supported` takes the private `_UAParams`, so a caller that decides something from the gate up front (ATOM's Kimi-K3 DSpark draft picks its q dtype by it) has to build all of `_UAParams` by hand, and every new required field breaks it (#7100, #7421). `gfx950_gluon_supported` takes only what the gate reads; `_gfx950_gluon_supported` delegates to it, so the gate keeps one definition and unified_attention's behaviour is unchanged. * [Triton/Gluon] unified_attention: shorten the gfx950_gluon_supported docstring A one-line summary, one entry per argument and the return value, as the Triton ops guidelines ask for a public function. * [Triton/Gluon] unified_attention: drop the gfx950_gluon_supported tests Review: the private gate delegates to the public function, so a test that compares the two cannot fail, and the rest tests plumbing.


No description provided.