Skip to content

[Triton/Gluon] Add FP8 Flash Attention v2 kernel (gfx942/gfx950) - #5577

Merged
shengnxu merged 11 commits into
ROCm:mainfrom
WuLei-AMD:dev_lumen_pr9_fp8_attention
Nov 5, 2026
Merged

shengnxu merged 11 commits into
ROCm:mainfrom
WuLei-AMD:dev_lumen_pr9_fp8_attention

Conversation

@WuLei-AMD

@WuLei-AMD WuLei-AMD commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

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 uses float8e4nv/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 via get_tuned_kernel_config() backed by per-arch JSON, removing hardcoded Python tile values. Removes duplicate is_cdna4() definition in favour of import from arch_info.
  • utils/_triton/arch_info.py: Add is_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 for attn_fwd (USE_FP8=False) against PyTorch reference.
  • bench_fp8_attention.py: Benchmark with --causal and -metric options.

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.

@WuLei-AMD
WuLei-AMD requested a review from a team September 16, 2026 07:18
@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; main branch always runs both MI35X and MI300X
multigpu 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: GPT-OSS-120B, DeepSeek-R1-0528, Kimi-K2.5
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 5577 --add-label <label>

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 opt this PR out.

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

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.Config objects still hardcode num_warps=4 and num_stages=1 in Python even though this PR moves tuning to JSON. get_tuned_kernel_config returns 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_fwd with USE_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_kernels entry 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 from op_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_fwd directly from _triton_kernels and 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.

@WuLei-AMD
WuLei-AMD force-pushed the dev_lumen_pr9_fp8_attention branch from fbc9ae2 to 304b08e Compare September 16, 2026 12:42
@zufayu
zufayu requested review from a team and azaidy September 17, 2026 01:11

@micmelesse micmelesse 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.

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?

@WuLei-AMD

Copy link
Copy Markdown
Contributor Author

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?

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.

@WuLei-AMD
WuLei-AMD requested a review from micmelesse October 10, 2026 06:57
Comment thread aiter/ops/triton/attention/mha.py Resolved
@WuLei-AMD
WuLei-AMD force-pushed the dev_lumen_pr9_fp8_attention branch from 7bdfd0c to e7fc64e Compare October 21, 2026 04:28
@WuLei-AMD
WuLei-AMD requested a review from micmelesse October 21, 2026 04:48
@ZhangDanyang-AMD
ZhangDanyang-AMD force-pushed the dev_lumen_pr9_fp8_attention branch 4 times, most recently from c9105b7 to aa9b4a8 Compare October 29, 2026 12:15
@aiter-bot

aiter-bot commented Oct 29, 2026 •

Copy link
Copy Markdown

NOT READY FOR HUMAN REVIEW

PR Self-Review

Revision 869e71d77b52 · mode shadow

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

  1. What behavior does this PR change, and why is the existing behavior insufficient?
  2. What did you validate, and what remains unverified? Include commands, hardware/configuration and results.
  3. Which exact workload keys consume these configuration changes, and what evidence shows that unrelated shapes keep their previous selection?
  4. For every changed op_tests/ file, what behavior does it cover and why is it not redundant? Write None if no op test changed.

Edit the PR Self-Review section in the PR description. Include the current revision SHA; this comment refreshes automatically.

Missing or invalid answers

  • FAIL — missing ### Reviewed revision in the PR description
  • FAIL — missing ### Behavior and intent in the PR description
  • FAIL — missing ### Validation and unverified scope in the PR description
  • FAIL — missing ### PR-specific question in the PR description
  • FAIL — missing ### Changed op tests in the PR description
  • FAIL — Reviewed revision must contain at least the first 7 characters of the current head SHA
  • FAIL — Behavior and intent has no substantive answer
  • FAIL — Validation and unverified scope has no substantive answer
  • FAIL — PR-specific question has no substantive answer
  • FAIL — changed op test not explained in Changed op tests: op_tests/op_benchmarks/triton/bench_block_scale_fp8_attention.py
  • FAIL — changed op test not explained in Changed op tests: op_tests/triton_tests/attention/test_block_scale_fp8_attention.py
  • FAIL — Changed op tests must explain why each changed op test is necessary and non-redundant

Shadow mode reports what would block but keeps the status successful. Set AITER_PR_SELF_REVIEW_MODE=enforce only after reviewing real false positives.

@ZhangDanyang-AMD
ZhangDanyang-AMD force-pushed the dev_lumen_pr9_fp8_attention branch 2 times, most recently from c044dab to 94aa588 Compare October 30, 2026 07:11
@ZhangDanyang-AMD
ZhangDanyang-AMD force-pushed the dev_lumen_pr9_fp8_attention branch 5 times, most recently from 53d074b to 276d04c Compare November 2, 2026 02:48
Dewei-Wang-sh
Dewei-Wang-sh previously approved these changes Nov 3, 2026
@ZhangDanyang-AMD
ZhangDanyang-AMD dismissed micmelesse’s stale review November 3, 2026 05:16

The comments have been addressed and fixed

@ZhangDanyang-AMD
ZhangDanyang-AMD force-pushed the dev_lumen_pr9_fp8_attention branch from 276d04c to 6cbdfdb Compare November 3, 2026 05:17
@WuLei-AMD
WuLei-AMD force-pushed the dev_lumen_pr9_fp8_attention branch from 6cbdfdb to 5d5e245 Compare November 3, 2026 13:21
@ZhangDanyang-AMD
ZhangDanyang-AMD force-pushed the dev_lumen_pr9_fp8_attention branch 3 times, most recently from 72c6f88 to e2e8821 Compare November 4, 2026 08:36
WuLei-AMD and others added 11 commits November 4, 2026 17:49
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]>
@ZhangDanyang-AMD
ZhangDanyang-AMD force-pushed the dev_lumen_pr9_fp8_attention branch from e2e8821 to 869e71d Compare November 4, 2026 09:49
@shengnxu
shengnxu merged commit 1662d80 into ROCm:main Nov 5, 2026
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.

7 participants