AttnFuse: A Composable DSL for Compiling Attentions to Fused GPU Kernels
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.
Create a lesson
Related papers
Linear Attention Has Superlinear Capacity
Shared title terminologycs.LGHao Guo, Sudhaunshu Hardikar, Simon Dennis et al.
Interpretable generative graph attention for representation learning and reconstruction of multimodal neuroimaging data
Shared title terminologycs.LGMeenu Ajith, Ishaan Batta, Vince Calhoun
Tabular Selective Attention
Shared title terminologycs.LGRohith Saai Pemmasani Prabakaran, Daniel De Dios Allegue, Nour Shaheen et al.
STORK: STeps Or Reduced-precision Kernels?
Shared title terminologycs.LGArnab Sanyal, Sandeep P. Chinchali
PickPack: Scaling Context Extension To Millions Of Tokens With Sparse Attention
Shared title terminologycs.LGKumar Krishna Agrawal, Vihaan Thaker, Keshaw Ranjan et al.
Dual-Band Attention: Efficient LLM Inference With Lightweight Block Sparsity Estimation
Shared title terminologycs.LGZheng Liu, Kaixin Huang, Jiaxi Li et al.