FlexAttention: A Programming Model for Generating Fused Attention Variants
Juechu Dong, Boyuan Feng, Driss Guessous, Yanbo Liang, Horace He (Member of the PyTorch team · Meta)
Conference on Machine Learning and Systems 2025 · Day 4 · Session 10: LLM and Diffusion Model Serving
Overview
The landscape of deep learning, particularly within large language models (LLMs), is dominated by the Transformer architecture, where the attention mechanism is a foundational component. Achieving high performance with these models critically depends on highly optimized, fused implementations of attention. However, the rapid proliferation of diverse attention variants—each tailored for specific tasks, computational efficiencies, or model characteristics—has created a significant challenge. Traditionally, each novel attention variant would necessitate the laborious and error-prone development of a custom, highly optimized kernel, often in low-level languages like CUDA or Triton. This predicament, dubbed the "software lottery," severely impedes research velocity and the practical adoption of new attention mechanisms.

Key moments
- 0:00 FlexAttention: Problem of custom kernels for attention variants
- 0:45 FlexAttention solution: Flexible API, fused kernels via PyTorch
- 1:40 Score Mod: User-defined pre-Softmax attention score modification
- 2:15 Score Mod in action: Relative positional encodings
- 3:30 Mask Mod: Leveraging structured sparsity in attention computation
- 4:40 Mask Mod example: Document masking for jagged sequences
- 5:00 Mod Transformations: Combining mask mods for complex variants
FlexAttention: A Programming Model for Generating Fused Attention Variants
Speakers: Juechu Dong, Boyuan Feng, Driss Guessous, Yanbo Liang, Horace He
Conference: MLSys 2025
YouTube: https://www.youtube.com/watch?v=None
Overview
The landscape of deep learning, particularly within large language models (LLMs), is dominated by the Transformer architecture, where the attention mechanism is a foundational component. Achieving high performance with these models critically depends on highly optimized, fused implementations of attention. However, the rapid proliferation of diverse attention variants—each tailored for specific tasks, computational efficiencies, or model characteristics—has created a significant challenge. Traditionally, each novel attention variant would necessitate the laborious and error-prone development of a custom, highly optimized kernel, often in low-level languages like CUDA or Triton. This predicament, dubbed the "software lottery," severely impedes research velocity and the practical adoption of new attention mechanisms.
FlexAttention, presented by Rius (Juechu Dong) from the PyTorch team at Meta, offers a sophisticated programming model designed to overcome this bottleneck. It provides a flexible, high-level API that allows researchers and engineers to define custom attention variants using a few lines of idiomatic PyTorch code. This code is then automatically lowered and compiled into highly efficient, fused FlashAttention-like kernels, leveraging PyTorch's torch.compile and autograd machinery. The core innovation lies in its ability to abstract away the complexities of low-level kernel optimization by introducing two powerful concepts: user-defined score modifications and mask modifications, applied to a generalized attention template.
This breakthrough matters immensely for the ML/systems community. By decoupling the definition of attention variants from the necessity of bespoke kernel engineering, FlexAttention democratizes the exploration of novel attention designs. It promises to accelerate innovation in Transformer architectures, reduce development overhead, and ensure that cutting-edge attention research can be translated into performant implementations more rapidly. The framework's ability to express complex structured sparsity and handle various bias types without materializing large tensors further underscores its potential to unlock new efficiencies and capabilities in the development and deployment of next-generation AI models.
Background
▶ Watch: FlexAttention: Problem of custom kernels for attention variants (0:00)
The attention mechanism, as introduced in the "Attention Is All You Need" paper, revolutionized sequence modeling and forms the bedrock of modern Transformer architectures. At its core, attention computes a weighted sum of input values, where the weights are derived from the similarity between a query and a set of keys. Mathematically, this is often expressed as $\text{Attention}(Q, K, V) = \text{softmax}(\frac{QK^T}{\sqrt{d_k}})V$. While conceptually straightforward, the naive implementation of this equation involves multiple matrix multiplications and a Softmax operation, which can be computationally and memory-intensively expensive, especially for long sequences.
To address these performance bottlenecks, highly optimized, fused attention implementations have emerged as critical components in the ML/systems stack. FlashAttention, for instance, is a seminal work that demonstrated significant speedups by fusing the attention computation into a single kernel, thereby reducing expensive memory I/O between GPU global memory and on-chip SRAM. This fusion is crucial for maximizing hardware utilization and achieving state-of-the-art throughput.
However, the rapid pace of research in Transformer architectures has led to an explosion of specialized attention variants. These include:
- Causal Attention: Used in autoregressive models, where a token can only attend to previous tokens.
- Alibi (Attention with Linear Biases): Incorporates a fixed, non-learned bias that depends on the distance between query and key.
- Relative Positional Encodings (RPEs): Modifies attention scores based on the relative positions of tokens.
- Prefix LM Attention: A hybrid form combining bidirectional attention for a prefix with causal attention for the generated sequence.
- Sliding Window Attention: Restricts attention to a local window around each token, reducing quadratic complexity.
- Document Masking (Jagged Sequences/Block Diagonal Mask): Used for processing batches of documents where attention should not cross document boundaries.
- Soft Capping: Limits the magnitude of attention logits to prevent numerical instability or over-confidence.
The proliferation of these variants creates a significant "software lottery." Each variant, if implemented naively, would require its own custom, hand-optimized kernel to achieve performance comparable to FlashAttention. This is a formidable task, demanding deep expertise in low-level GPU programming and detailed knowledge of hardware architectures. The lack of readily available, performant custom kernels acts as a major bottleneck, limiting the adoption of new research and slowing down experimentation. Researchers are often forced to choose between implementing a less optimal, unfused version or investing substantial engineering effort into kernel development, diverting resources from core ML research.
Existing ML compilers have historically struggled with automatically generating highly optimized fused kernels for attention for several reasons:
- Back-to-back Matrix Multiplications: Attention involves two successive matrix multiplications ($Q K^T$ followed by $A V$). Fusing these efficiently, especially with the Softmax in between, is complex.
- Online Softmax and Normalization: The Softmax operation in attention requires a specific mathematical rewrite (e.g., for numerical stability and to handle large intermediate sums) for both the forward and backward passes. This rewrite often involves techniques like online normalization, which are difficult for general-purpose compilers to infer and optimize.
- Structured Sparsity: Many attention variants introduce structured sparsity (e.g., causal masks, sliding windows, document masks). Expressing and leveraging this sparsity to reduce computation and memory access is critical for performance but challenging for compilers to automatically detect and exploit without explicit guidance.
FlexAttention directly confronts these challenges by providing a principled way to define and fuse these diverse attention patterns, aiming to eliminate the "software lottery" and empower faster innovation in Transformer models.
Key Findings
▶ Watch: Score Mod: User-defined pre-Softmax attention score modification (1:40)
FlexAttention introduces a novel programming model that effectively addresses the "software lottery" inherent in developing and deploying diverse attention variants. Its key findings and contributions can be summarized as follows:
- Unified and Flexible API: FlexAttention provides a highly flexible API capable of expressing a wide array of attention variants, including causal, Alibi, Relative Positional Encodings (RPEs), Prefix LM, sliding window, soft capping, and document masking. This API is designed to be idiomatic PyTorch, allowing researchers to define complex attention logic with just a few lines of Python code.
- Automatic Fused Kernel Generation: The core innovation is the ability to automatically lower these high-level PyTorch definitions into highly optimized, fused FlashAttention-like kernels. This is achieved through the integration with
torch.compile, PyTorch's native compiler, which handles the complex task of generating efficient low-level code (currently leveraging Triton as a backend). This eliminates the need for manual, custom kernel development for each new variant. - Automated Backward Pass Generation: Leveraging PyTorch's powerful autograd machinery, FlexAttention automatically generates the corresponding backward passes for any user-defined attention variant. This significantly reduces the development burden and potential for error, ensuring that custom attention implementations are fully differentiable and compatible with standard deep learning training pipelines.
- Core Abstractions: Score Modification (
score_mod) and Mask Modification (mask_mod): The framework introduces two primary, powerful abstractions that enable its flexibility:
score_mod: A user-defined function applied point-wise to the raw attention scores before the Softmax operation. This allows for dynamic biasing, relative positional embeddings, and other score transformations without materializing full bias tensors in global memory.mask_mod: A user-defined function that determines which attention scores are relevant for computation, enabling the expression and exploitation of structured sparsity. This function returns a Boolean, allowing the system to pre-compute a block mask and only compute necessary scores, significantly reducing computation for sparse variants.
- Decoupled Kernel Compilation and Data Dependency: For variants leveraging
mask_mod, FlexAttention allows the underlying fused kernel to be compiled once. The block mask, which encodes the sparsity pattern, can then be updated dynamically at runtime based on data dependencies (e.g., document IDs for document masking) without requiring recompilation of the kernel. This enhances flexibility and efficiency, especially during inference or when sparsity patterns change frequently. - Avoidance of Materializing Large Bias Tensors: Through the
score_modmechanism, FlexAttention can implement bias schemes like RPEs or Alibi without needing to materialize potentially large bias tensors in global memory. This reduces memory footprint and improves performance. - Foundation for Further Research: FlexAttention has already served as a foundation for new research, with several papers building upon its capabilities. It provides a robust platform for exploring novel attention mechanisms and system optimizations.
In essence, FlexAttention transforms the process of attention variant development from a low-level kernel engineering challenge into a high-level, declarative programming task within the familiar PyTorch ecosystem, significantly accelerating innovation in Transformer architectures.
Technical Deep Dive
▶ Watch: Score Mod in action: Relative positional encodings (2:15)
FlexAttention's power stems from its elegant abstraction of the attention mechanism, breaking it down into a vanilla computation augmented by user-defined modifications. Let's dissect the technical components.
Vanilla Attention Recap
To understand FlexAttention, it's helpful to first recall the standard, unfused attention process:
- Query-Key Dot Product: Given Query ($Q$) and Key ($K$) matrices, compute the dot product $Q K^T$. This produces the raw attention scores.
- Softmax: Apply a Softmax operation along the rows of the attention scores. This normalizes the scores into attention weights, which sum to one for each query.
- Weights-Value Dot Product: Multiply the attention weights by the Value ($V$) matrix. This produces the final attention output.
The challenge, as discussed, is fusing these steps and handling variations efficiently.
The Score Modification (score_mod)
FlexAttention's primary mechanism for customizing attention is the score_mod function. This user-defined function is applied point-wise to the raw attention scores before the Softmax operation. Crucially, the scores are not materialized in global memory before this modification; score_mod operates on them conceptually as they are computed.
The signature of the score_mod function is:
score_mod(score, batch, H, Q_index, KV_index)
score: The raw dot product score between a specific query and key.batch: The batch index.H: The head index.Q_index: The index of the current query token.KV_index: The index of the current key/value token.
This seemingly simple interface proves incredibly powerful:
- Full Attention: For standard, full attention,
score_modis a no-op:return score. - Relative Positional Encodings (RPEs): RPEs typically add a bias to the attention score based on the relative distance between query and key tokens. With
score_mod, this can be implemented by simply adding(Q_index - KV_index)or a learned embedding based on this difference to thescore. The key benefit is that the full bias matrix, which could be very large, does not need to be explicitly materialized in memory. - Alibi Bias: Similar to RPEs, Alibi adds a linear bias based on relative distance. FlexAttention supports this by allowing
score_modto "close over" external tensors. For instance, analibi_biastensor can be defined outside thescore_modand then read from within it. The system ensures this external data is correctly incorporated into the fused kernel. - Soft Capping: To prevent attention logits from growing excessively large,
score_modcan apply clipping or non-linear transformations (e.g.,torch.clamportorch.tanh) to thescorebefore Softmax. The mental model is a point-wise mapping over scores that are not realized in global memory, transforming each value as it's conceptually processed.
The Mask Modification (mask_mod) and Structured Sparsity
Many attention variants introduce structured sparsity, meaning only a subset of query-key interactions are relevant. Leveraging this sparsity is crucial for performance. FlexAttention addresses this with the mask_mod function. Its purpose is to pre-determine which attention scores need to be computed at all, thereby reducing redundant computation.
The signature of the mask_mod function is:
mask_mod(batch, H, Q_index, KV_index)
- Noticeably,
mask_moddoes not receive thescoreargument, as its job is to decide whether a score should be computed, not to modify its value. - It returns a Boolean:
Trueif the score at(Q_index, KV_index)is relevant,Falseotherwise.
The output of mask_mod is then piped through a function called create_block_mask. This process is vital for exploiting sparsity:
- Tile-based Processing: The attention matrix is conceptually broken down into query tiles and key tiles.
- Block Mask Generation: For a given query tile, the system iterates along the key tiles. If
mask_modindicates that any attention score within a specific key tile is needed, that key tile is marked for computation. KV_num_blocksandKV_indices:create_block_maskproduces two tensors:KV_num_blocks(indicating how many key blocks are relevant for each query block) andKV_indices(the actual indices of those relevant key blocks). These lookups allow the kernel to skip computations for irrelevant blocks, potentially halving computation for variants like causal attention.
Examples of mask_mod in action:
- Causal Attention: The
mask_modwould returnTrueonly ifKV_index <= Q_index. This ensures that a query token only attends to previous or current key tokens. - Prefix LM: Popularized by models like T5, Prefix LM uses a
mask_modto define a specific sparsity pattern where the prefix attends bidirectionally, and the generated sequence attends causally to the prefix and itself. Similar toscore_mod,mask_modcan close over external tensors (e.g., aprefix_lengthtensor) to define its logic. - Document Masking (Jagged Sequences/Block Diagonal Mask): This is for scenarios where a batch contains multiple independent sequences (e.g., documents), and attention should not cross document boundaries. A
document_id_maptensor can be passed tomask_mod, which then returnsTrueonly ifdocument_id_map[Q_index]equalsdocument_id_map[KV_index]. This creates a block-diagonal attention pattern.
Mod Transformations
FlexAttention further enhances flexibility with mod transformations. These are higher-order functions that take a mask_mod (or potentially a score_mod) as input and return a new, transformed mask_mod. This allows for composing complex sparsity patterns. For example, one could define a generic causal_mask_mod transformation that, when applied to a document_id_mask_mod, produces a causal_document_mask_mod. This modularity simplifies the creation of hybrid attention variants. The talk also briefly mentions variants for Page Attention, indicating its broad applicability.
System Integration and Backend
FlexAttention leverages the existing PyTorch ecosystem:
torch.compile: This is the cornerstone for lowering the high-level PyTorch code into efficient, fused kernels. It acts as the bridge between the user-definedscore_mod/mask_modlogic and the optimized low-level implementation.- Autograd: PyTorch's automatic differentiation engine handles the generation of backward passes, ensuring that gradients are correctly computed for the custom attention variants.
- Backend: Currently, the primary backend for kernel generation is Triton, a Python-based DSL for writing highly optimized GPU kernels. There is also a CPU-based C++ kernel for compatibility. The team is exploring alternative template backends for future work.
A crucial design decision is that the kernel generated by torch.compile does not need to be recompiled if only the data dependencies for the block mask change. This means a kernel can be generated once, and the KV_num_blocks and KV_indices (derived from create_block_mask) can be updated dynamically on the fly, offering significant flexibility without incurring recompilation overhead.
Experimental Setup & Results
▶ Watch: Mask Mod example: Document masking for jagged sequences (4:40)
During the 10-minute presentation, the speaker explicitly stated that a detailed discussion of performance benchmarks was beyond the scope of the talk. The focus was primarily on introducing the programming model and its capabilities.
However, the speaker did mention that comprehensive profiling numbers comparing FlexAttention against FlashAttention and other common fused attention variants are available in the associated research papers and blog posts. These external resources delve into the quantitative performance gains and efficiencies achieved by FlexAttention.
The talk also highlighted ongoing work-in-progress features aimed at further increasing performance:
- TMA Support on H100+ GPUs: The team is actively working on adding Tensor Memory Accelerator (TMA) support for NVIDIA H100 GPUs and beyond. TMA is a hardware feature designed to accelerate asynchronous data transfers, which can be critical for maximizing throughput in memory-bound operations like attention.
- Automatic Warp Specialization: This optimization technique aims to automatically specialize the execution paths for different warps (groups of threads) within a GPU kernel, potentially leading to more efficient resource utilization and higher performance.
While specific headline numbers, dataset names, baseline comparisons, or ablation studies were not presented in this particular conference talk, the acknowledgment of their existence in accompanying materials confirms that performance is a key consideration and has been rigorously evaluated in the broader FlexAttention project. The mention of future work targeting Blackwell architecture suggests a commitment to maintaining state-of-the-art performance on upcoming hardware generations.
Practical Implications
▶ Watch: Mod Transformations: Combining mask mods for complex variants (5:00)
FlexAttention carries profound practical implications for a wide range of stakeholders in the ML ecosystem, from researchers to deployment teams. It fundamentally shifts the paradigm for developing and deploying attention mechanisms, offering significant advantages but also introducing certain tradeoffs and limitations.
For Practitioners and Model Builders
- Accelerated Research and Development: The most immediate benefit is the dramatic acceleration of research into new attention variants. Researchers are no longer constrained by the availability of custom kernel engineers or the prohibitive effort of writing low-level CUDA/Triton code. They can rapidly prototype, iterate, and evaluate novel attention ideas using familiar PyTorch syntax. This democratizes attention research, making it accessible to a broader audience.
- Simplified Implementation: Defining a complex attention variant, which previously might have required hundreds of lines of specialized C++/CUDA code, can now be achieved with just a few lines of idiomatic PyTorch using
score_modandmask_mod. This significantly reduces development time, debugging effort, and the barrier to entry for customizing attention. - Mix-and-Match Capabilities: The modular nature of
score_modandmask_mod, especially with the concept of mod transformations, allows for easy combination and composition of different attention features. For example, one could combine a relative positional encoding with a document mask and causal attention without significant rework. - Memory Efficiency: By avoiding the materialization of large bias tensors (e.g., for RPEs or Alibi bias) in global memory, FlexAttention can lead to a reduced memory footprint, which is crucial for training and deploying large models, especially on memory-constrained hardware.
- Dynamic Sparsity Pattern Updates: For variants relying on
mask_mod, the ability to update the block mask dynamically at runtime without recompiling the underlying kernel is a powerful feature. This is particularly useful for inference scenarios where sparsity patterns might change per batch or per sequence (e.g., varying document lengths or prefix sizes).
For Infrastructure Teams and Deployers
- Unified Framework: FlexAttention offers a unified framework for handling diverse attention types. This can simplify the infrastructure required for deploying and managing models with various attention mechanisms, reducing the need for multiple specialized libraries or custom builds.
- Leveraging Existing PyTorch Infrastructure: By building on
torch.compileand PyTorch's autograd, FlexAttention seamlessly integrates into existing PyTorch-based ML pipelines. This reduces integration overhead and allows teams to leverage their existing knowledge and toolchains. - Future-Proofing: The commitment to supporting new hardware architectures (e.g., Blackwell) ensures that FlexAttention remains relevant and performant as hardware evolves. This helps infrastructure teams plan for future deployments with confidence.
- Potential for Optimization: Features like TMA support on H100+ GPUs and automatic warp specialization indicate that the framework is continuously being optimized for cutting-edge hardware, promising sustained high performance.
Tradeoffs and Limitations
While powerful, FlexAttention does have certain limitations based on the current implementation:
- Limited Dynamic Sparsity: The current framework does not support fully dynamic sparsity where the sparsity pattern is decided on the fly based on computed scores (e.g., "H2O style" dynamic sparse attention). The block mask must be pre-computed. This means the sparsity pattern needs to be known or derivable from input metadata before the core attention computation loop.
- Kernel Scheduling for Sparsity: While
mask_modhelps identify relevant scores, the current implementation for causal masks primarily determines the bounds of iteration loops. Optimal scheduling to ensure that each sub-tile of computation performs a roughly equal proportion of work (to maximize GPU utilization) is an area of ongoing research and future work. - A10 Ops (C++ without Python): There are no current plans to directly lower FlexAttention into A10 ops for C++ inference without the Python runtime. While potentially possible in the future, it's not supported today, which might limit its use in extremely low-latency, Python-free deployment environments.
- Performance Nuances: While designed for high performance, the ultimate speedup will depend on the complexity of the
score_modandmask_modfunctions and how effectivelytorch.compilecan optimize them. Practitioners still need to benchmark their specific custom variants to ensure they meet performance targets.
In summary, FlexAttention represents a significant step towards abstracting away low-level kernel complexities, empowering ML researchers and engineers to innovate faster and deploy more diverse and efficient Transformer models. Its limitations are primarily in niche dynamic sparsity patterns and specific deployment environments, which are common areas of ongoing development in the ML systems space.
Key Takeaways
- Solves the "Software Lottery": FlexAttention eliminates the need for custom, hand-optimized kernels for each new attention variant, addressing a major bottleneck in Transformer research and development.
- High-Level API for Attention Variants: It provides a flexible, idiomatic PyTorch API using
score_modandmask_modto define diverse attention behaviors with minimal code. - Automated Fused Kernel Generation: Leveraging
torch.compileand Triton, FlexAttention automatically transforms high-level definitions into highly efficient, fused FlashAttention-like kernels, including automatic backward pass generation via PyTorch's autograd. - Powerful Abstractions:
score_modenables point-wise transformations of attention scores (e.g., RPEs, Alibi, soft capping) without materializing large bias tensors.mask_modallows expressing structured sparsity (e.g., causal, Prefix LM, document masking) to reduce computation. - Decoupled Compilation and Data Dependencies: The generated kernels can be compiled once, and the underlying block mask, derived from
mask_mod, can be dynamically updated at runtime based on data, offering flexibility without recompilation overhead. - Future-Oriented Development: The project has already inspired new research, with ongoing work to enhance performance (e.g., TMA support on H100+ GPUs, automatic warp specialization) and support future hardware like Blackwell.
About the Speaker(s)
The talk on FlexAttention was presented by Rius, a member of the PyTorch team at Meta. Rius introduced FlexAttention as a crucial development for generating fused attention variants, highlighting the challenges faced by researchers due to the "software lottery" of needing custom kernels. The co-authors of the work include Juechu Dong, Boyuan Feng, Driss Guessous, Yanbo Liang, and Horace He, all contributing to this significant advancement in ML systems. Their collective expertise lies in developing robust and performant infrastructure for deep learning, particularly within the PyTorch ecosystem.
Reviews
Simon Wisk (Open Source Developer & AI Tooling Expert) — SOLID
FlexAttention is genuinely useful engineering from the PyTorch team — a clean abstraction that lets you define attention variants in idiomatic Python and get a fused Triton kernel out the other end. The talk explains the scoremod/maskmod split clearly and the block mask approach to structured sparsity is the right design. But this write-up reads more like documentation than a conference talk review, and the talk itself apparently skipped benchmarks entirely in a 10-minute slot. For a systems paper at MLSys, that's a meaningful gap.
Jensen Hitch (AI Compute Platform CEO) — STRONG ACCEPT
FlexAttention is a genuine platform-level contribution to the attention kernel problem — it treats the 'software lottery' of custom kernel engineering as a systems constraint, not a research inconvenience, and addresses it with a principled compiler abstraction. The scoremod and maskmod API is well-designed: it separates score transformation from sparsity pattern definition, enables block mask precomputation to exploit structured sparsity, and integrates cleanly with torch.compile and autograd. The decoupled compilation model — compile once, update block mask dynamically — is exactly the kind of insight that survives contact with production. What keeps this from a 5 is the absence of…
→ Top-rated talks at Conference on Machine Learning and Systems 2025
All talks from Conference on Machine Learning and Systems 2025