AttnFuse: A Composable DSL for Compiling Attentions to Fused GPU Kernels
Organizations: Department of Computer Science University of Wisconsin–Milwaukee
Abstract
Modern AI systems are built on the Transformer architecture, whose core operation, attention, accounts for the majority of computation and memory cost. Researchers continually propose new attention variants to improve quality, efficiency, or context length, but each variant currently requires expert-written GPU code to run at usable speeds. PyTorch's recent flex_attention lets researchers describe custom attention patterns in Python and compile them to fused kernels, but its design is limited to modifications applied after the central matrix multiplication, excluding Rotary Position Embedding (RoPE), the positional encoding used by every major LLM. We introduce AttnFuse, a small DSL for attention that makes pre-multiplication transformations like RoPE first-class operations. Researchers compose ten high-level building blocks to describe a variant, and AttnFuse's compiler emits a single fused GPU kernel for the entire computation. On an RTX 3090, AttnFuse achieves a 2.10 speedup over flex_attention on the RoPE+causal pattern. On an H100, it runs a full Llama-3-8B training step within 5% of PyTorch's hand-tuned backend. Our investigation reveals the Rotation Calculus: whether to fuse RoPE or apply it separately depends on the GPU's compute-to-bandwidth ratio, with a derived crossover that matches measurement. AttnFuse demonstrates that a small, attention-specific compiler can close the gap between flexible research code and production kernels.
Figures & tables
| Metric | Production | Spike | flex_attention |
|---|---|---|---|
| HMMA pipe (%) | 15.8 | 29.4 | 32.6 |
| SM throughput (%) | 35.5 | 58.6 | 43.7 |
| Warp occupancy (%) | 12.0 | 24.1 | 12.0 |
| DRAM throughput (%) | 2.1 | 3.8 | 4.3 |
| Wait stalls (%) | 29.9 | 13.4 | 21.5 |
| Long-SB stalls (%) | 1.2 | 3.1 | — |
| Variant | Winner | Latency |
|---|---|---|
| Plain causal | BN=64, nw=8, ns=3 | 0.488 ms |
| RoPE+causal | BN=128, nw=8, ns=3 | 0.779 ms |
| Configuration | Latency | vs. prod. |
|---|---|---|
| Production Ampere kernel on H100 | 0.931 ms | 1.00 |
| + FA-2 causal split + bigger tiles | 0.694 ms | 1.34 |
| + BLOCK_N=64 sweep winner | 0.488 ms | 1.91 |
| Hopper spike (dispatched) | 0.488 ms | 1.91 |
| flex_attention reference | 0.443 ms | 2.10 |
| GPU N | 512 | 1024 | 2048 | 4096 | 8192 | 16384 |
|---|---|---|---|---|---|---|
| RTX 3090 | 2.10 | 2.04 | 1.95 | 2.10 | — | — |
| H100 NVL | — | — | 1.37 | 1.05 | 0.82 | 0.69 |
| Counter | Plain | RoPE |
| HMMA pipe % | 29.4 | 16.6 |
| SM throughput % | 58.6 | 38.5 |
| Warp occupancy % | 24.1 | 12.5 |
| Wait stalls % (math-pipe) | 13.4 | 26.3 |
| Long-scoreboard % (HBM) | 3.1 | 17.5 |
| DRAM throughput % | 3.8 | 2.2 |
| Variant | N=512 | N=1024 | N=2048 | N=4096 |
|---|---|---|---|---|
| Dense | 1.74 | 1.00 | 0.98 | 0.95 |
| Causal | 1.10 | 1.15 | 1.04 | 0.94 |
| SW (W=256) | 1.16 | 1.16 | 1.10 | 1.05 |
| Causal+ALiBi | 1.20 | 1.19 | 1.10 | 0.96 |
| Variant | N=512 | N=1024 | N=2048 | N=4096 |
|---|---|---|---|---|
| RoPE + Dense | 1.99 | 2.05 | 1.92 | 1.78 |
| RoPE + Causal | 2.10 | 2.04 | 1.95 | 2.10 |
| RoPE + SW | 1.81 | 1.66 | 1.74 | 1.65 |
| Geometry | Cache | AttnFuse | flex |
|---|---|---|---|
| Llama-3-8B (32q/8kv/128) | 4096 | 0.034 ms | 0.039 ms |
| Llama-3-70B (64q/8kv/128) | 32768 | 0.184 ms | 0.196 ms |
| Mistral-7B (32q/8kv/128) | 16384 | 0.072 ms | 0.079 ms |
| Workload | Sequence | SDPA | flex | AttnFuse |
|---|---|---|---|---|
| Forward only | 2.82 | 2.71 | 2.87 | |
| Forward only | 5.90 | 5.93 | 6.36 | |
| Forward+backward+step | 7.27 | 7.49 | 7.65 | |
| Forward+backward+step | 14.39 | 16.02 | 16.51 |
| Metric | S7-A Spike | Prong B (failed) | Change |
|---|---|---|---|
| Long-SB stalls (%) | 17.45 | 3.33 | -14.12 pp |
| Short-SB stalls (%) | 7.04 | 15.68 | +8.64 pp |
| HMMA pipe (%) | 16.6 | 10.3 | -6.3 pp |
| SM throughput (%) | 38.5 | 26.8 | -11.7 pp |
| Latency (ms) | 0.793 | 0.952 |
Efficient Attention
Efficient attention reduces the compute, memory traffic, and cache costs of Transformer attention while preserving useful long-range interactions.
See all 134 papers