[Kernel] Add CDNA SageAttention kernel#869
Open
LiuYinfeng01 wants to merge 4 commits into
Open
Conversation
LiuYinfeng01
force-pushed
the
kernel/sage-attention-cdna
branch
from
July 16, 2026 11:23
9541e6b to
d01afc5
Compare
Collaborator
|
Clean codes using |
LiuYinfeng01
force-pushed
the
kernel/sage-attention-cdna
branch
from
July 24, 2026 03:37
02838e5 to
e29f0cc
Compare
Refactor monolithic sage_attn_cdna; extract sage_attn_utils helpers; traits + kernel class structure; trim docstrings; gate on MI308X/gfx942; skip gfx950 tests; drop gfx950 dead code; reuse shared MFMA/SCF helpers. Black + ruff clean; bit-identical on MI308X harness shapes.
Split sage_attn_cdna into pipeline + sage_attn_helpers/utils; migrate K/Q/O GM loads to make_buffer_tensor + copy_atom; fx vector ops and @flyc.jit mask dispatch; gate tests to MI308X; cleanup gfx950 dead code and formatting. Descale/bias GM remains on pointer load (bit-exact on 0721-zimage). Validated: harness S256/S4608/S8448 sha256 bit-exact; test_sage_attn pass.
Add test_sage_attn.py to CDNA_ONLY_TESTS so RDNA runners (e.g. navi-2) skip at collection time, complementing the existing MI308X/gfx942 module skip.
LiuYinfeng01
force-pushed
the
kernel/sage-attention-cdna
branch
from
July 24, 2026 03:40
e29f0cc to
42aea08
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 AMD Instinct MI308X (gfx942).
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:
For one Q block, the data flow is:
The 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:
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_M and Hadamard BLOCK_R are unrelated: the former tiles sequence rows; the latter, when used, tiles head-dimension rotation.
Changes
Performance
Measured on AMD Instinct MI308X (gfx942). Throughput is reported in TFLOPS using the dense attention FLOP count
4 * B * H * S^2 * D(QK^T and PV, FP32-accumulated), with B=1 and D=128; higher TFLOPS is faster.speedup = FlyDSL TFLOPS / Triton TFLOPS, 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 uses BLOCK_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. The timed region includes SmoothQ preprocessing, quantization, attention, and output handling, so the values below are effective attention TFLOPS over the full wrapper time (
4 * B * H * S^2 * D / total_time), not attention-kernel-only throughput. It isolates implementation efficiency at the same problem tile, but it is not the production comparison because production Triton A3 is faster at 128x64.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 1920 FlyDSL attention calls and 0 fallbacks. 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
Validated on AMD Instinct MI308X (gfx942); the test module skips before importing the CDNA kernel on Navi and all other GPUs.
ruff check kernels/attention/sage_attn_cdna.py kernels/attention/sage_attn_utils.py tests/kernels/test_sage_attn.pyblack --check --line-length 120 kernels/attention/sage_attn_cdna.py kernels/attention/sage_attn_utils.py tests/kernels/test_sage_attn.pypython3 -m py_compile kernels/attention/sage_attn_cdna.py kernels/attention/sage_attn_utils.py tests/kernels/test_sage_attn.pyGQA/MQA and arbitrary tail lengths are not claimed as validated capabilities by this PR.
Dependencies