Add Flex Attention and expose torch.compile modes - #15870
Conversation
|
✅ All contributors have signed the CLA. Thank you! This PR is ready to be merged. |
|
I have read and agree to the Contributor License Agreement |
bdd2f06 to
99e2986
Compare
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Plus Run ID: 📒 Files selected for processing (4)
Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review. 📜 Recent review details🧰 Additional context used📓 Path-based instructions (7)Community-contributed extra nodes. Focus on:⚙️ CodeRabbit configuration file Files:
Core ML/diffusion engine. Focus on:⚙️ CodeRabbit configuration file Files:
IMPORTANT: Only comment on issues directly introduced by this PR's code changes.⚙️ CodeRabbit configuration file Files:
Treat legacy combo, `io.Combo`, and `io.DynamicCombo` values affecting filesystem access as untrusted; revalidate them at load/save boundaries with `folder_paths`, containment checks, or fixed allowlists.📄 CodeRabbit inference engine (AGENTS.md) Files:
Keep state and capability flags on the object that owns the behavior. Prefer explicit parent-owned attributes over probing child objects with `getattr`; use child checks only when the child owns the delegated behavior.📄 CodeRabbit inference engine (AGENTS.md) Files:
Keep changes small, direct, and limited to the narrowest necessary code path and smallest number of files.📄 CodeRabbit inference engine (AGENTS.md) Files:
Keep warning and info messages short and actionable, remove noisy or misleading logging, and make documentation edits concise, factual, and tied to changed behavior.📄 CodeRabbit inference engine (AGENTS.md) Files:
🔇 Additional comments (9)
📝 WalkthroughWalkthroughAdds the Merge Risk: 🔵 Low · up to The PR adds opt-in attention and compile-mode behavior while preserving existing defaults. Its compile backend discovery requires PyTorch 2.1 or newer without declaring or guarding that requirement, so older installations could fail at runtime; the PR is mergeable with explicit owner awareness. 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary
This PR adds an opt-in PyTorch Flex Attention backend and exposes
torch.compilebackends and modes throughTorchCompileModel.The changes are complementary: Flex Attention benefits substantially from selecting an appropriate compile mode, especially
max-autotune-no-cudagraphson ROCm.Changes
Flex Attention
--use-flex-attention.torch.compilegraph is being traced.TorchCompileModel
torch.compiler.list_backends().default,max-autotune, andmax-autotune-no-cudagraphs.guard_filter_fnbehavior when no compile mode is selected.guard_filter_fnwhen a mode is selected because PyTorch rejects combining that option withmode.Benchmark evidence
The attention shapes below were derived from an actual Anima Turbo sampling pass. For each input size, the largest observed attention call was replayed by the microbenchmark.
Measurements used FP16 on AMD Radeon Graphics with ROCm 7.2 and PyTorch 2.13.0+rocm7.2.
defaultmeans Inductor compilation without an explicit compile mode. Values are steady-state medians; compilation and autotuning startup are excluded from timed evaluations.512²
Q:
[1, 16, 1024, 128]K:
[1, 16, 1024, 128]V:
[1, 16, 1024, 128]Attention FLOPs: 8.59 GFLOPs
(22.61 TFLOP/s)
(19.27 TFLOP/s)
(22.71 TFLOP/s)
(20.59 TFLOP/s)
(22.38 TFLOP/s)
(28.82 TFLOP/s)
(15.89 TFLOP/s)
(27.77 TFLOP/s)
(34.27 TFLOP/s)
1024²
Q:
[1, 16, 4096, 128]K:
[1, 16, 4096, 128]V:
[1, 16, 4096, 128]Attention FLOPs: 137.44 GFLOPs
(32.66 TFLOP/s)
(30.45 TFLOP/s)
(32.00 TFLOP/s)
(22.03 TFLOP/s)
(36.48 TFLOP/s)
(41.64 TFLOP/s)
(17.63 TFLOP/s)
(45.59 TFLOP/s)
(53.02 TFLOP/s)
2048²
Q:
[1, 16, 16384, 128]K:
[1, 16, 16384, 128]V:
[1, 16, 16384, 128]Attention FLOPs: 2.20 TFLOPs
(36.98 TFLOP/s)
(36.24 TFLOP/s)
(36.86 TFLOP/s)
(22.63 TFLOP/s)
(44.59 TFLOP/s)
(46.13 TFLOP/s)
(19.28 TFLOP/s)
(54.93 TFLOP/s)
(56.82 TFLOP/s)
These results suggest that
max-autotune-no-cudagraphsmay be a better default for Flex Attention compilation on ROCm, and possibly CUDA. No default change is proposed here: this needs broader testing across NVIDIA and AMD hardware, driver versions, PyTorch/Triton versions, model families, masks, GQA, sequence lengths, and compilation cold-start costs.The Triton 3.8.0 local-patch results are supporting evidence only; this PR does not add a Triton dependency.
Appendix: Reproducing the benchmarks
The benchmark implementation is available at
bench.py@73abe92.The aggregated results and raw JSON outputs are available in
results/flex-attention-sdpa@252aed9.Common arguments:
The size-specific arguments were:
BACKENDwaspytorchorflex. The compile-mode variants used:Compilation runs were launched with
CXX=clang++in anix shell nixpkgs#clangenvironment. The patched Flex results used Triton 3.8.0 with the local patches; the baseline Flex results used Triton 3.7.1.Testing
default,max-autotune, andmax-autotune-no-cudagraphswere exercised.