Repository navigation
[Triton/Gluon] fav3_sage: RDNA3 (gfx1100) support: FP16 V and a tuned gfx1100 table - #6770
Conversation
🏷️ 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.
🔵 Needs a closer look
The precision change and hardware-specific tuning need human review because the reported GPU accuracy and performance results were not independently verified.
No open issues 🎉
What changed in this PR
Improves fav3_sage support on gfx1100 without changing the attention kernel.
Changes:
- Uses FP16 V on architectures without FP8 support.
- Adds gfx1100 launch settings with 64×64 tiles and four warps.
| File | Description |
|---|---|
aiter/ops/triton/configs/gfx1100/triton/attention/fav3_sage/DEFAULT.json |
Adds tuned gfx1100 launch settings. |
aiter/ops/triton/attention/fav3_sage.py |
Selects FP16 V when FP8 is unavailable. |
Review effort: Balanced
…cores fav3_sage quantizes V to FP8 for the P*V product. RDNA3 (gfx11) has no FP8 WMMA, so Triton converts the FP8 V tile and the FP8 P tile to FP16 in software in the inner loop. On gfx1100 this limits the kernel to about 8 TFLOP/s for every tile size. When arch_info.is_fp8_avail() is False, sage_quant now writes V as FP16 with the same per-channel scale. The kernel takes its P*V type from V, so it runs FP16 WMMA. The archs that have FP8 (gfx942, gfx950, gfx12, gfx1250) keep the FP8 path.
gfx1100 has no fav3_sage table, so it used the gfx942 table: BLOCK_M=256, BLOCK_N=128, num_warps=8. With wave32 and 256 VGPRs per lane, this tile spills more than 1000 VGPRs at head dim 128 (183 at head dim 64). With FP8 V it ran at 5-6 TFLOP/s, and the first call compiled for 9 minutes (4608 tokens) to 40 minutes (32760 tokens). The new table uses BLOCK_M=64, BLOCK_N=64, num_warps=4, num_stages=1, waves_per_eu=1. This tile does not spill. It was the best single tile in a sweep over BLOCK_M, BLOCK_N, num_warps, num_stages, waves_per_eu and PRE_LOAD_V on a Radeon PRO W7900, for head dim 64 and 128 at 4608 and 32760 tokens. fwd_a3_tuned has the same values, so a3_tuned does not change the launch on gfx1100.
With an FP16 V, sage_quant still computed the per-channel amax, divided V by it in the quant kernel, and the attention epilogue multiplied the scale back. For a 16-bit type this costs an amax, a full read/write pass over V and an extra rounding, with no precision benefit. sage_quant now quantizes V only when v_dtype is FP8. For FP16/BF16 it returns V as is (cast to v_dtype if needed) with a v_scale of ones and launches no V programs; sage_quant_kernel guards the V branch with a QUANTIZE_V constexpr (it replaces the unused FP8_MAX). The FP8_TYPE / FP8_MAX / v_fp8 names become v_dtype / v_dtype_max / v_out. fav3_sage_wrapper_func takes a v_dtype argument (default: FP8 when the arch has FP8, else FP16), so the 16-bit V path is tested on every arch. New tests: sage_quant pass-through, accuracy for each V type, and non-dense inputs with an FP16 V. gfx1100 (W7900), Triton 3.8: sage_quant 1.04-1.06 -> 0.74-0.79 ms at (1, 24, 4608, 128) and 4.2 -> 3.0-3.1 ms at (1, 12, 32760, 128); output unchanged (same cosine, rel. L2 and max abs vs fp32 SDPA). gfx942: FP8 path unchanged.
f38dfb5 to
b18334a
Compare
brunomazzottiamd
left a comment
There was a problem hiding this comment.
Hi @TwelveNights. The PR LGTM, so I'm approving it!
I'd like to have a thumbs up from @juuso-oskari (or someone else from his team) before merging, since they are the owners of Sage Attention.
|
Hi, this PR looks good! just wondering if you know any models/frameworks that would use this kernel on gfx1100. I am also interested in doing similar works, but do not know any popular models/concrete use cases. |
|
Thanks. The main users are diffusion (DiT) models, where attention over long image/video token sequences dominates: video models like Wan 2.1 (32760 tokens at 480p, 81 frames) and image models like FLUX (4608 tokens at 1024²), which are the shapes in the tables above. One concrete path: SGLang's diffusion runtime has an |
|
I'm merging this PR given the approval from @amd-xavierwang (he said "this PR looks good!" in #6770 (comment)). Thanks everyone! |
Motivation
fav3_sage(SageAttention v1: INT8 QK, FP8 PV) has tuned tables only for gfx942 and gfx950. Other archs use the gfx942 table. On gfx1100 (Radeon PRO W7900) the results are correct, but the op is not usable:torchSDPA and 10x slower than aiter's own bf16 Triton MHA on the same GPU.MAX_SEQLENS_*are constexpr, so each new sequence length compiles again. The unit tests did 17 cases in 27 minutes.There are two causes:
BLOCK_M=256, BLOCK_N=128, num_warps=8is too large for wave32 with 256 VGPRs per lane. At head dim 128 it spills more than 1000 VGPRs (183 at head dim 64).Technical details
Three commits, 5 files, +161 / -27:
fav3_sage.py: whenarch_info.is_fp8_avail()is False, the wrapper askssage_quantfor an FP16 V instead of an FP8 V. The kernel takes the P*V type from V (p.to(v.type.element_ty)), so it uses FP16 WMMA. There is no attention kernel change. gfx942, gfx950, gfx1200, gfx1201 and gfx1250 have FP8, so they keep the FP8 path.fav3_sage_wrapper_funchas av_dtypeargument (default: FP8 if the arch has FP8, else FP16), so the FP16/BF16 V path can be tested on every arch.configs/gfx1100/triton/attention/fav3_sage/DEFAULT.json:BLOCK_M=64, BLOCK_N=64, num_warps=4, num_stages=1, waves_per_eu=1, PRE_LOAD_V=false. This tile does not spill (250 VGPRs at head dim 128, 239 at head dim 64; 16 KB LDS).fwd_a3_tunedhas the same values, soa3_tuned=Truedoes not change the launch on gfx1100.sage_quant(review follow-up): V is quantized only whenv_dtypeis FP8. A 16-bit V is passed through (cast tov_dtypeif needed) with av_scaleof ones, and no V programs are launched;sage_quant_kernelguards the V branch with aQUANTIZE_Vconstexpr (replacing the unusedFP8_MAX).FP8_TYPE/FP8_MAX/v_fp8are renamedv_dtype/v_dtype_max/v_out. New tests:test_sage_quant_16bit_v_passthrough,test_sage_v_dtype(FP16, BF16, FP8 V), andtest_sage_non_dense_inputswith an FP16 V. On gfx942 a 16-bit V needsBLOCK_N=64(the FP8-sized table needs 80 KB LDS), so the tests passget_sage_fwd_configs(block_n=64)for 16-bit V.How the table was tuned: the scripts in
utils/_triton/tuning/support only GEMM families. So we used a sweep script that calls the public wrappers (sage_quant, thenfav3_sage_func(config=...)) and times them withtriton.testing.do_bench. Steps:BLOCK_M{32, 64, 128, 256} xBLOCK_N{16, 32, 64, 128} xnum_warps{2, 4, 8} xnum_stages{1, 2} xwaves_per_eu{1, 2} xPRE_LOAD_V.The table has one
fwdentry for all head dims, the same as the gfx942 and gfx950 tables.Test plan
pytest op_tests/triton_tests/attention/test_fav3_sage.py op_tests/triton_tests/attention/test_fav3_sage_compile.pybshd.python op_tests/op_benchmarks/triton/bench_fav3_sage.py -b 1 -hq 24 -sq 4608 -d 128 -metric throughput(and the other three shapes).Baselines on gfx1100: aiter Triton MHA
flash_attn_func(bf16) andtorch.nn.functional.scaled_dot_product_attention(bf16).black26.3.0 andruff0.15.7 on the changed Python file.Software: torch 2.10.0+rocm7.0, Triton 3.6.0 for the first two commits; Triton 3.8.0 (the wheel aiter CI uses) for the
sage_quantfollow-up and the updated unit-test counts. GPUs: Radeon PRO W7900 (gfx1100), MI300X (gfx942).Test results
Update: 16-bit V passed through (commit 3, Triton 3.8.0)
sage_quantand fullfav3_sagecall on the W7900, bf16bshdinputs, ms (range of 3-4 alternating runs; "before" = commits 1-2):sage_quantbeforesage_quantafterfav3_sagebeforefav3_sageafterAccuracy against fp32 SDPA is unchanged (same cosine, rel. L2 and max abs). A BF16 V passed through without the FP16 cast made
sage_quantabout 0.15 ms faster but the P*V kernel 1-8% slower and slightly less accurate, so the default is FP16. gfx942 FP8 path, before/after commit 3: 343-344, 210, 496-498 and 314-318 TFLOP/s on the four shapes, no change.Unit tests with all three commits on Triton 3.8.0: gfx1100 529 passed, 596 skipped; gfx942 554 passed, 571 skipped.
Unit tests (commits 1-2, Triton 3.6.0)
The skipped tests are the MXFP4 tests (
is_fp4_avail()is False on these archs).Performance on gfx1100 (W7900), commits 1-2, Triton 3.6.0
TFLOP/s = 4·B·H·N²·D / time, the
bench_fav3_sage.pyformula.fav3_sageincludes the Q/K/V quantization.(*) aiter's Triton MHA has no gfx1100 table either (it fails with "Required config file doesn't exist"). We ran it with its gfx1101 table, passed through
config=.The kernel alone (without quantization) runs at 58-66 TFLOP/s. At 4608 tokens the quantization takes approximately 1.06 ms of the call (approximately 20%), so
fav3_sageis at or below the bf16 MHA there. At 32760 tokens the quantization cost is small, andfav3_sageis 4-20% faster than the bf16 MHA.Contribution of each change (4608 tokens, TFLOP/s with quantization):
First-call compile time at (1, 24, 4608, 128): before 518 s, after approximately 1 s.
Accuracy (against fp32 SDPA)
FP16 V also makes the result on gfx1100 more accurate.
gfx942 (MI300X): no change
bench_fav3_sage.py, TFLOP/s, three alternating runs each:Lint
black --checkandruff checkpass.Notes and limits
sage_quant) is the next bottleneck at short sequences. At (1, 24, 4608, 128) it took 1.06 ms before commit 3 (K smoothing 0.19 ms, V amax 0.25 ms, quant kernel 0.62 ms) and 0.74-0.79 ms after it. Its launch parameters (num_warps=8, num_stages=3) are fixed in Python. Tuning them per arch needs a config table, so it is not in this PR.attention/mha.py) has no gfx1100 table.