[Kernel] Add CDNA SageAttention kernel#869
Open
LiuYinfeng01 wants to merge 2 commits into
Open
Conversation
LiuYinfeng01
force-pushed
the
kernel/sage-attention-cdna
branch
from
July 16, 2026 11:23
9541e6b to
d01afc5
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add a standalone FlyDSL SageAttention V1 kernel for CDNA GPUs.
Motivation
SageAttention uses INT8 Q/K and FP8 V to reduce attention bandwidth and improve dense diffusion-model attention performance. This contribution makes the production attention kernel available directly in the FlyDSL kernel repository.
SageAttention principle and this kernel's role
SageAttention is a mixed-precision attention recipe rather than only an INT8 matrix multiplication. The complete path has two layers:
delta_s)delta_s = softmax_scale * q_mean * K_smooth^T.For one Q block, the data flow is:
K_smooth = K - mean_sequence(K)Q_smooth = Q - mean_block(Q)Q_int8 ~= Q_smooth / q_scale,K_int8 ~= K_smooth / k_scale,V_fp8 ~= V / v_scalescore ~= softmax_scale * (Q_int8 * K_int8^T) * q_scale * k_scale + delta_sO ~= softmax(score) * V_fp8 * v_scaleThe Q correction is exact before quantization:
Therefore Q smoothing lowers INT8 quantization error without changing the K-smoothed floating-point target. K smoothing deliberately changes every score row only by a key-independent constant when the mean is taken over keys; softmax is invariant to that common row shift. A normalized Hadamard rotation, when enabled by a higher-level wrapper, similarly preserves floating-point QK while making channel magnitudes more uniform before quantization.
This PR starts after preprocessing. Its inputs are already-quantized INT8 Q/K, blocked FP8 V, Q/K/V descale tensors, and an optional score-bias tensor carrying
delta_s. The kernel then performs:delta_saddition before softmax.The production Qwen path uses Q-smoothing blocks of 128 query tokens (
BLKQ=128) independently of this kernel's attention scheduling tile (BLOCK_M=256). A 256-row FlyDSL workgroup therefore consumes the correction data for two 128-row Q-smoothing blocks.BLOCK_Mand HadamardBLOCK_Rare unrelated: the former tiles sequence rows; the latter, when used, tiles head-dimension rotation.Changes
kernels/attention/.BLOCK_M=256,BLOCK_N=64FlyDSL configuration on gfx942.ds_bpermute.Performance
Measured on AMD Instinct MI308X (
gfx942). In both tables,speedup = baseline latency / FlyDSL latency, so values above 1 mean FlyDSL is faster.Production configurations: attention kernel only
Quantization is outside the timed region. Each implementation uses its independently tuned production tile: Triton A3 uses
BLOCK_M=128,BLOCK_N=64, 4 warps; FlyDSL usesBLOCK_M=256,BLOCK_N=64.Iso-tile integration benchmark: complete SmoothQ wrapper
This historical integration benchmark forces both Triton PR4033 and FlyDSL attention to
BLOCK_M=256,BLOCK_N=64. Timing includes SmoothQ preprocessing, quantization, attention, and output handling. It isolates implementation efficiency at the same problem tile, but it is not the production comparison because production Triton A3 is faster at128x64.Qwen-Image-Edit integration
This PR intentionally contributes the standalone low-level kernel builder. It accepts pre-quantized INT8 Q/K, blocked FP8 V, descale tensors, and an optional score-bias tensor. It does not add the AITER high-level
flydsl_sage_attn_func, SmoothQ preprocessing/quantization wrapper, or Qwen-Image-Edit attention dispatcher to this repository.The same kernel source was exercised in the external AITER/Qwen-Image-Edit integration with
1920FlyDSL attention calls and0fallbacks. An 8-step, 1024x1024, seed-0 run completed and produced the validated FlyDSL PNG. However, checking out this FlyDSL PR alone is not sufficient to rerun that model: the AITER wrapper/quantization integration is also required.Testing
2 passedon AMD Instinct MI308X (gfx942).ruff check kernels/attention/sage_attn_cdna.py tests/kernels/test_sage_attn.pyruff format --check kernels/attention/sage_attn_cdna.py tests/kernels/test_sage_attn.pypython3 -m py_compile kernels/attention/sage_attn_cdna.py tests/kernels/test_sage_attn.pyDependencies
Breaking Changes
None.