[Feature]: fp8 flash attention
Suggestion Description
Current flash attention has good perf. 1300+T. But not support fp8 yet which achieves 2000+T in ater asm. Port the aiter asm pipeline and optimize perf to align with asm.
Operating System
No response
GPU
No response
ROCm Component
No response
@jhinpan
Thanks for opening this and looping us in, @coderfeli.
I took a quick pass over current main and wanted to propose an incremental RFC for adding FP8 support to the existing flash-attn forward path.
Current FlyDSL state
kernels/flash_attn_generic.pyis the main dense BSHD flash-attn forward builder. It currently acceptsdtype_str in ("f16", "bf16"), supports MHA/GQA, and routes gfx950 + D=128 + f16/bf16 to the dual-wave SWP path.kernels/flash_attn_gfx950.pyis the high-performance D=128 dual-wave SWP path. It is explicitly bf16/f16 only today, and a lot of the LDS layout constants assume 2-byte elements.- QKV varlen is currently tied to the gfx950 dual-wave path; if
cu_seqlens_qis provided and the dual-wave path cannot be built, the generic builder rejects it. - FlyDSL already has useful FP8 building blocks:
- arch-dependent
T.f8/T.f8x*inpython/flydsl/expr/typing.py; rocdl.mfma_f32_16x16x32_fp8_fp8,cvt_pk_fp8_f32, andcvt_pk_f32_fp8wrappers;- FP8 load/scale/pack patterns in
kernels/pa_decode_fp8.pyand FP8 GEMM kernels.
- arch-dependent
Suggested MVP contract
Start with pre-quantized FP8 Q/K/V input, not in-kernel quantization:
- input Q/K/V: FP8, probably using the existing arch-dependent f8 type mapping;
- output O: bf16 first;
- accumulation: fp32 for QK logits, online softmax state, and PV accumulator;
- first supported shape: dense flash-attn forward,
head_dim == 128, causal and non-causal, MHA/GQA; - no dropout/bias/alibi in the initial scope;
- API adds descale tensors, e.g.
q_descale,k_descale,v_descale; - first scale granularity: per-tensor
[1], then per-batch/head such as[B, H_kv]after correctness is stable.
This matches the direction AITER took: start with FP8 MHA forward, then wire descales and optimized ASM variants.
Relevant AITER references:
- ROCm/aiter#1053:
Support fp8 mha forward - ROCm/aiter#1459:
mha fp8 support descale - ROCm/aiter#2105:
mha fwd v3 hdim128 support per tensor fp8 for MI300/MI308 - ROCm/aiter#2911:
F8 fmha ASM gfx950
Kernel changes I think are needed
-
Extend dtype plumbing.
Add an FP8 dtype mode such as
dtype_str == "fp8"ordtype_str == "fp8bf16"to the flash-attn builders and to common dtype helpers. This should not be just widening the existing("f16", "bf16")assertions, because the element byte size, vector pack width, LDS layout, and MFMA op all change. -
Add descale args to the launcher/kernel ABI.
Dense MVP can pass three scale tensors:
Q, K, V, O, q_descale, k_descale, v_descale, seq_len, ...QK should multiply by
q_descale * k_descale * sm_scale. PV should applyv_descaleto the V contribution / output accumulator. For GQA, scale indexing needs to follow the KV-head mapping if we move beyond per-tensor scales. -
Rework the QK path for FP8 MFMA.
The FP16/BF16 path currently uses f16/bf16 MFMA variants. FP8 should use
rocdl.mfma_f32_16x16x32_fp8_fp8where possible, with 32 K elements per MFMA. That means different packing/load fragments than the current f16/bf16K_STEPassumptions. -
Rework LDS layout and vectorized loads.
The gfx950 dual-wave path has constants like
BF16_BYTES = 2,D_128B_SIZE = 64, and bf16-based LDS tile sizes. FP8 needs 1-byte element accounting, likely 16 FP8 elems per 16B vector load, and fresh padding/swizzle choices rather than reusing bf16 offsets. -
Decide the PV implementation strategy.
For an optimized FP8 path, the most natural route seems to be similar to existing FP8 decode code: pack softmax probabilities into FP8 in LDS using
cvt_pk_fp8_f32, runmfma_f32_16x16x32_fp8_fp8with V, then apply the probability normalization correction andv_descale. For a correctness-first bring-up, we could temporarily dequantize V to f16/bf16 and reuse more of the existing PV structure, but that probably will not reach the AITER ASM performance target. -
Keep varlen as a separate follow-up.
Varlen is not just a dtype switch. The kernel has packed
[total_tokens, H, D]layouts,cu_seqlens-based sequence offsets, max-seqlen launch sizing, and different scale-indexing questions. I would suggest landing dense FP8 first, then adding varlen once the scale ABI and dense numerics are settled.
Test plan
- Extend
tests/kernels/test_flash_attn_fwd.pywith an FP8 mode instead of onlyf16/bf16. - Generate fp16/bf16 random Q/K/V, quantize them to FP8 on the host, keep descale tensors, run the FP8 kernel, and compare against a PyTorch SDPA reference using dequantized Q/K/V.
- Cover causal/non-causal, MHA, GQA, and D=128 dense first.
- Use looser FP8 tolerances than current f16/bf16 tests; exact threshold should be based on measured AITER-like behavior.
- Add varlen tests only after dense support is merged.
Questions for @coderfeli
- Should the first implementation target the generic flash-attn path for correctness, or should we go directly into the gfx950 dual-wave SWP path because perf parity with AITER ASM is the main goal?
- What scale granularity do you want for the MVP: per-tensor
[1], per-batch/head[B, H_kv], or something closer to per-token/block scale? - Should the first supported output dtype be bf16 only?
- Is gfx950 the right first target, or do you also want gfx942 in scope from the first PR?
- For PV, is packing softmax probabilities to FP8 and using FP8xFP8 MFMA acceptable for the MVP, or would you prefer a simpler dequantize-to-f16/bf16 bring-up first?
If this direction looks good, I think the work can be split into:
- API/dtype/scales + dense correctness path;
- optimized D=128 gfx950 path;
- GQA/per-head scales polish;
- varlen support;
- benchmarking against AITER ASM.
Thanks @jhinpan for quick update. For your questions:
- go directly into the gfx950 dual-wave SWP path because perf parity with AITER ASM is the main goal
- Per Tensor first then per token.
- yes. output bf16.
- GFX950
- Need to align with asm. I thought it's high precision softmax.