Repository navigation
[Triton/Gluon] Add FP8 Flash Attention v2 kernel (gfx942/gfx950) - #5577
Conversation
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
PR title tags & labels: |
7c8e52b to
fbc9ae2
Compare
There was a problem hiding this comment.
🟡 Changes recommended
Critical autotune and LSE memory-safety issues remain, along with missing wrapper integration and FP8/backward coverage.
Get a fresh assessment by requesting another Copilot review.
Pull request overview
Adds a pure-Triton FP8 Flash Attention v2 implementation for gfx942/gfx950 with forward/backward kernels, scaling, tuning configs, tests, and benchmarks.
Changes:
- Adds FP8 attention kernels and scaling helpers.
- Adds architecture detection and per-architecture tuning configs.
- Adds forward correctness tests and benchmark tooling.
File summaries
| File | Review summary |
|---|---|
aiter/ops/triton/_triton_kernels/attention/fp8_attention_kernel.py |
Critical: invalid backward autotune key (3 votes); undersized LSE allocation (2 votes). Moderate: hardcoded fallback tuning (1 vote), missing public wrapper (2 votes), and missing FP8/backward coverage (1 vote). |
op_tests/op_benchmarks/triton/bench_fp8_attention.py |
Critical: undersized LSE allocation (1 vote). Nit: benchmarks the internal kernel instead of a public wrapper (1 vote). |
op_tests/triton_tests/attention/test_fp8_attention_kernel.py |
Critical: undersized LSE allocation (1 vote). Moderate: missing FP8/backward coverage (2 votes) and direct internal-kernel launch (1 vote). |
aiter/ops/triton/utils/_triton/arch_info.py |
Adds canonical is_cdna4() detection. |
aiter/ops/triton/configs/gfx950/triton/attention/fp8_attn/DEFAULT.json |
Adds gfx950 attention tuning defaults. |
aiter/ops/triton/configs/gfx942/triton/attention/fp8_attn/DEFAULT.json |
Adds gfx942 attention tuning defaults. |
Review details
Suppressed comments (4)
aiter/ops/triton/_triton_kernels/attention/fp8_attention_kernel.py:36
- [verified] These fallback
triton.Configobjects still hardcodenum_warps=4andnum_stages=1in Python even though this PR moves tuning to JSON.get_tuned_kernel_configreturns these objects whenever a device has no published entry, so the fallback tuning remains outside the config source of truth. Author must move the fallback tuning into the supported config-resolution path.
_FWD_FALLBACK = triton.Config({"PRE_LOAD_V": False}, num_warps=4, num_stages=1)
_BWD_FALLBACK = triton.Config({}, num_warps=4, num_stages=1)
aiter/ops/triton/_triton_kernels/attention/fp8_attention_kernel.py:1073
- [verified] The only added correctness test invokes
attn_fwdwithUSE_FP8=False; it does not exercise the FP8 scale/dtype path or any of the backward entry points introduced in this file. A broken FP8 or backward implementation can therefore ship while the reported test suite still passes. Author must add pytest coverage for the supported FP8 forward path and backward kernels, or remove those unsupported paths from this PR.
@triton.jit(repr=_bwd_kernel_dkdv_repr)
def _bwd_kernel_dkdv(
op_tests/op_benchmarks/triton/bench_fp8_attention.py:21
- [verified] The benchmark also imports and launches the internal
_triton_kernelsentry point directly, so it does not measure a public API path and will preserve an unsupported internal import even after the kernel is integrated. Author must benchmark the public attention wrapper rather than launching the kernel body fromop_tests.
from aiter.ops.triton._triton_kernels.attention.fp8_attention_kernel import (
attn_fwd,
get_padded_headsize,
)
op_tests/triton_tests/attention/test_fp8_attention_kernel.py:19
- [verified] The new test imports and launches
attn_fwddirectly from_triton_kernelsand manually reproduces the launch ABI; no public wrapper for this operation is added in the PR. This can pass while the production-facing attention API has no route to the new kernel. Author must add the public attention wrapper and exercise that wrapper from the test.
from aiter.ops.triton._triton_kernels.attention.fp8_attention_kernel import (
attn_fwd,
get_padded_headsize,
)
- Files reviewed: 6/6 changed files
- Comments generated: 6
- Review effort level: Lite
💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
fbc9ae2 to
304b08e
Compare
micmelesse
left a comment
There was a problem hiding this comment.
We already support fp8 in the mha api right. See https://github.com/ROCm/aiter/blob/main/aiter/ops/triton/attention/mha.py#L978-L996. Does the existing api lack features or functionality?
5c96e34 to
7bdfd0c
Compare
The existing flash_attn_func FP8 support uses q_descale / k_descale / v_descale: one fp32 scalar per (batch, head) for the whole sequence. That is the path at the lines you linked. This kernel needs a different scale layout, so those arguments cannot carry it. Q and K descales are (batch, heads, n_blocks), one value per 64 tokens. V is a single tensor scale, and the softmax probabilities take a separate p_scale. The output is bf16, and it runs on both gfx942 and gfx950. I added that as keyword-only arguments on flash_attn_func (q_block_descale, k_block_descale, v_scale, p_scale). q / k / v stay in the existing (batch, seqlen, nheads, headdim) layout. If the new arguments are omitted, the call takes the same path as before. |
7bdfd0c to
e7fc64e
Compare
c9105b7 to
aa9b4a8
Compare
NOT READY FOR HUMAN REVIEWPR 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 |
c044dab to
94aa588
Compare
53d074b to
276d04c
Compare
The comments have been addressed and fixed
276d04c to
6cbdfdb
Compare
6cbdfdb to
5d5e245
Compare
72c6f88 to
e2e8821
Compare
Adds a full FP8 Flash Attention v2 implementation (attn_fwd, _attn_bwd_preprocess, _attn_bwd_dkdv, _attn_bwd_dq) in _triton_kernels/attention/fp8_attention_kernel.py (~1850 lines). Key properties: - Supports both per-tensor (p_scale, q_descale, k_descale, v_scale) and block-scale (stride_qdescale_m, stride_kdescale_m) FP8 layouts - Variable-length sequences via cu_seqlens_q / cu_seqlens_k (VARLEN) - Causal masking, ALiBi slopes, dropout, return_softmax - Paged-KV layout via block_table - All stride multiplications use int64 (no overflow at long-context scale) Architecture scope: gfx942 (fnuz) and gfx950 (OCP e4m3), selected via USE_FP8 constexpr and fp8 dtype dispatch. No public wrapper in this PR; the kernel is consumed by the mxfp8_attention wrapper added in the follow-on PR.
attn_fwd: [BLOCK_M, BLOCK_N, BLOCK_DMODEL_QK, BLOCK_DMODEL_V, USE_FP8, IS_CAUSAL, VARLEN, USE_EXP2] compute_fp8_scaling_factors: [fp8_max] _bwd_preprocess_use_o: [USE_FP8, BLOCK_M, BLOCK_DMODEL_V, ACTUAL_BLOCK_DMODEL_V, IS_VARLEN] _bwd_kernel_dkdv: [BLOCK_M, BLOCK_N, BLOCK_DMODEL_QK, BLOCK_DMODEL_V, USE_FP8, CAUSAL, IS_VARLEN, USE_EXP2] _attn_bwd_dkdv: [BLOCK_M, BLOCK_N, USE_FP8, CAUSAL, USE_EXP2] _bwd_kernel_dq: [BLOCK_M, BLOCK_N, BLOCK_DMODEL_QK, BLOCK_DMODEL_V, USE_FP8, CAUSAL, IS_VARLEN, USE_EXP2] _attn_bwd_dq: [BLOCK_M, BLOCK_N, USE_FP8, CAUSAL, USE_EXP2] Helper @triton.jit functions (cdiv_fn, dropout_*, load_fn, compute_alibi_block, _attn_fwd_inner) are not direct-launch kernels and remain without repr.
Adds repr decorators to all 7 public @triton.jit kernels: attn_fwd, compute_fp8_scaling_factors, _bwd_preprocess_use_o, _bwd_kernel_dkdv, _attn_bwd_dkdv, _bwd_kernel_dq, _attn_bwd_dq. Adds test_fp8_attention_kernel.py testing attn_fwd correctness (USE_FP8=False) against PyTorch reference across causal/non-causal, GQA, and S_q != S_k shapes. 10 passed, 2 skipped (causal + S_q!=S_k).
Benchmarks attn_fwd (USE_FP8=False path) across representative (B, HQ, HK, S, D) shapes including GQA, with --causal flag and -metric time/bandwidth options. Skips automatically on non-FP8 architectures.
Replace hardcoded triton.Config in get_autotune_fwd_configs() and get_autotune_bwd_configs() with get_tuned_kernel_config() backed by DEFAULT.json under configs/<arch>/triton/attention/fp8_attn/. autotune_configs() wraps the pinned tile so runtime tuning is opt-in via FP8_ATTN_FWD_TRITON_AUTOTUNE / FP8_ATTN_BWD_TRITON_AUTOTUNE envvars.
fp8_attention_kernel.py defined its own is_cdna4() which duplicates the one added to arch_info.py. Replace with a canonical import and add is_cdna4() to arch_info.py so both PR9 and PR10 share the same definition.
Remove unused Python functions: get_shape_from_layout, get_strides_from_layout, get_input_shapes, is_hip, is_cdna, is_rdna, get_tl_f8_bwd_dtype, get_padded_head_dim. These were either duplicates of existing utilities (get_shape_from_layout in flash_attn_triton_amd/utils.py) or never called within the file or by any wrapper. Also adds is_cdna4() to arch_info.py (canonical location) and removes the duplicate local is_cdna4() definition that was previously cleaned up.
kernel: - Fix bwd autotune key: replace nonexistent 'BLOCK_DMODEL' with the actual 'BLOCK_DMODEL_QK' and 'BLOCK_DMODEL_V' kernel parameters. wrapper: - Add public aiter/ops/triton/attention/fp8_attention.py re-exporting attn_fwd, compute_fp8_scaling_factors, get_padded_headsize, and the three backward kernels; test and bench now import through this path. test + bench: - Fix LSE buffer allocation: kernel writes at start_m * BLOCK_M * 2, so the interleaved LSE/Delta workspace must be 2 * S_q rows to avoid out-of-bounds writes.
The previous tests only launched attn_fwd with USE_FP8=False, so the FP8 forward path and the three backward kernels could regress undetected.
Add keyword-only block-scale arguments and dispatch to the FP8 kernel only when they are set, so existing flash_attn_func callers stay on the current Triton and Gluon paths.
The public entry, kernel, configs, and tests now use block_scale_fp8_attention, and forward/backward cover batch and multi-block shapes. K-scale block counts are padded to a power of two so tl.arange compiles when the block count is not itself a power of two. Co-authored-by: Cursor <[email protected]>
e2e8821 to
869e71d
Compare
Summary
Adds a pure-Triton FP8 Flash Attention v2 implementation supporting gfx942 and gfx950, including forward and backward passes with per-block FP8 scale tensors.
The kernel selects architecture-specific FP8 dtypes at import time: gfx942 uses
float8e4b8/float8e5b16, gfx950 usesfloat8e4nv/float8e5.Changes
_triton_kernels/attention/fp8_attention_kernel.py: Triton kernels for forward (attn_fwd) and backward (_bwd_kernel_dkdv,_bwd_kernel_dq,_bwd_preprocess_use_o,_attn_bwd_dkdv,_attn_bwd_dq,compute_fp8_scaling_factors). All 7 public kernels have repr decorators. Autotune configs resolved viaget_tuned_kernel_config()backed by per-arch JSON, removing hardcoded Python tile values. Removes duplicateis_cdna4()definition in favour of import fromarch_info.utils/_triton/arch_info.py: Addis_cdna4()canonical definition.configs/gfx942/triton/attention/fp8_attn/DEFAULT.json+configs/gfx950/triton/attention/fp8_attn/DEFAULT.json: Pinned tile configs for forward and backward kernels.triton_tests/attention/test_fp8_attention_kernel.py: Correctness tests forattn_fwd(USE_FP8=False) against PyTorch reference.bench_fp8_attention.py: Benchmark with--causaland-metricoptions.Test results
Verified on MI350X (gfx950) and MI308X (gfx942):
pytest -v op_tests/triton_tests/attention/test_fp8_attention_kernel.py
10 passed, 2 skipped in ~10s
✅ MHA / GQA (num_q_heads ≠ num_kv_heads)
✅ causal on / off
✅ bf16 / fp16
✅ S_q ≠ S_k (non-causal)
⏭ causal + S_q ≠ S_k (intentionally skipped — undefined behavior)
Note: Tests cover the USE_FP8=False (BF16/FP16) forward path. The USE_FP8=True path is not yet tested.