Add flex_attention (score_mod / mask_mod) on the generic flash-attention kernel - #931
Draft
RichardChamberlain1 wants to merge 4 commits into
Draft
Add flex_attention (score_mod / mask_mod) on the generic flash-attention kernel#931RichardChamberlain1 wants to merge 4 commits into
RichardChamberlain1 wants to merge 4 commits into
Conversation
…ttention kernel Port PyTorch flex_attention's per-element hooks onto the dense f16/bf16 forward kernel: score_mod(score,b,h,q,kv) transforms each logit and mask_mod(b,h,q,kv) keeps/drops via where(mask,score,-inf). Mods are compile-time callables specialized per kernel and keyed into the JIT cache by identity, so the no-mod path is unchanged. - New kernels/attention/flex_attention.py: public flydsl_flex_attention() + built-in alibi_score_mod / sliding_window_mask_mod / causal_mask_mod. - Thread score_mod/mask_mod through the builder (incl. auto-tile and pad-mask dispatch recursion sites) and into traits/cache_tag; disable the fused gpfetch path when a mod is present. - Apply score_mod (scale-then-unscale to match PyTorch's qk*sm_scale semantics) before the mask hook; extend the -inf clamp and epilogue reciprocal guard to mask_mod builds so fully-masked rows yield 0 instead of NaN. - Tests (tests/kernels/test_flex_attention.py) and a run_benchmark.sh op entry. Verified on MI300X: 37 flex cases pass (no-mod parity, alibi, sliding-window, causal-via-mask; odd/multi-tile seqlens; cross-attention incl. fully-masked rows) with no regression in test_flash_attn_fwd.py LSE-dense. Co-Authored-By: Claude <noreply@anthropic.com>
…pe sweep Make the flex_attention benchmark consistent with the flash_attn one: - test_flex_attention.py main(): print a flash_attn-style aligned row (GPU header + "config shape | St | MaxErr MinCos | Time(us) TFLOPS") mirroring _fmt_result/_fmt_extra_normal_row, instead of the terse "TFLOPS=.. TB/s=.." line. run_benchmark.sh still parses TFLOPS via its flash-attn table regex. Also dedup: import _acc_metric/_flops from test_flash_attn_fwd instead of copying them (drops an unused F import). - run_benchmark.sh: expand DEFAULT_FLEX_ATTENTION_SHAPES from 3 rows to a curated 14-row sweep (seq-len ladder 2048/4096/8192 x all flex cases + two GQA rows); flex cases only, no cross-kernel baselines. Verified on MI300X: 37 correctness tests pass; run_benchmark.sh --only flex_attention parses real TFLOPS for every row. Co-Authored-By: Claude <noreply@anthropic.com>
Reformat test_flex_attention.py with black (line-length 120) to pass the "Check Python Code Style" CI gate on PR #931. Formatting-only: black explodes multi-arg calls one-per-line and normalizes whitespace. ruff already clean. 37 tests still pass on MI300X. Co-Authored-By: Claude <noreply@anthropic.com>
Author
FlyDSL flex_attention Performance ReportRun mi300 (SMC-SC-DI10-33.dh144.dcgpu)GPU: AMD MI300 (gfx942), CU count 304, pciChipId 0x74a1 · roofline peak 1307 TFLOPS BF16 (full device)
Shape S=2048 (B=2, H=32, Hkv=32, D=128)
Shape S=4096 (B=2, H=32, Hkv=32, D=128)
Shape S=8192 (B=2, H=32, Hkv=32, D=128)
|
RichardChamberlain1
marked this pull request as draft
July 30, 2026 14:23
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
Ports PyTorch's
flex_attentionper-element hooks onto FlyDSL's existing dense f16/bf16 flash-attention forward kernel (Phase 1: dense, forward-only, correctness-first).score_mod(score, b, h, q_idx, kv_idx) -> scoreandmask_mod(b, h, q_idx, kv_idx) -> boolas compile-time callables overfxscalars, inlined into the kernel MLIR and specialized per mod (keyed into the JIT cache by callable identity). No-mod path compiles byte-identical to today.kernels/attention/flex_attention.py: publicflydsl_flex_attention(...)+ built-inalibi_score_mod/sliding_window_mask_mod/causal_mask_mod.GenericSoftmaxHelper(flash_attn_utils.py):apply_score_mod(PyTorch scale-then-mod semantics) runs before the mask;mask_modextendsapply_kv_mask. Mods threaded through the builder (incl. auto-tile + pad-mask recursion sites) and intotraits.cache_tag.c_neg_floorclamp and the epilogue1/lreciprocal guard, so fully-masked rows yield0/-inf(matching torch) instead ofNaN. Fused gpfetch path is disabled when a mod is present.scripts/run_benchmark.sh:flex_attentionop entry with a 14-row shape sweep (seq-len ladder x all cases + GQA), mirroring theflash_attnbenchmark pattern.test_flex_attention.py main()prints flash_attn-style aligned rows.Test plan
pytest tests/kernels/test_flex_attention.py— 37 cases pass on MI300X (gfx942): no-mod parity, alibi, sliding-window, causal-via-mask; bf16/f16; MHA/GQA/D=64; odd/multi-tile seqlens (250/384/1024); cross-attention incl. fully-masked rows; LSE checks.test_flash_attn_fwd.pyLSE-dense (20 cases) pass — shared softmax/epilogue unaffected.bash scripts/run_benchmark.sh --only flex_attention— all 14 rows emit parsed TFLOPS.Out of scope (future phases): block sparsity (
BlockMask), backward, tensor-capturing mods, gfx950 dualwave/fp8 flex paths.🤖 Generated with Claude Code