FlashAttention is an exact attention algorithm that recomputes attention scores on-the-fly during the backward pass instead of storing the massive intermediate attention matrix in high-bandwidth memory (HBM). This IO-aware design minimizes costly memory reads/writes between HBM and fast SRAM, transforming the operation from memory-bound to compute-bound. The result is a dramatic reduction in memory usage from quadratic to linear in sequence length, enabling the training of transformers with much longer context windows.
Glossary
FlashAttention

What is FlashAttention?
FlashAttention is an exact, IO-aware algorithm for computing the standard attention mechanism in transformer models, designed to dramatically reduce memory usage and increase computational speed, especially for long sequences.
The algorithm achieves its efficiency through tiling, where the input sequences are broken into blocks that fit into SRAM, and recomputation (also known as online softmax), which trades extra FLOPs for vastly reduced memory I/O. This makes it a foundational technique for scaling large language models (LLMs) and is a key enabler for models processing long documents, high-resolution images, or lengthy audio. Its successor, FlashAttention-2, further optimizes parallelism and work partitioning for near-peak GPU utilization.
Key Features of FlashAttention
FlashAttention is an exact attention algorithm optimized for modern hardware memory hierarchies. Its core innovations address the primary bottlenecks of training and inference with long sequences.
IO-Aware Computation
FlashAttention is explicitly designed around the memory hierarchy of GPUs (HBM vs. SRAM). It minimizes slow High-Bandwidth Memory (HBM) reads/writes by performing the entire attention operation in fast SRAM (on-chip memory). The algorithm tiles the input matrices and recomputes attention scores on-the-fly during the backward pass, trading extra FLOPs for a massive reduction in memory I/O—the true bottleneck for long sequences.
Exact Attention with Recomputation
Unlike approximate sparse or linear attention methods, FlashAttention computes the exact same attention matrix as the standard algorithm. It achieves massive memory savings through gradient checkpointing within the attention block. The softmax normalization is recomputed in the backward pass from block-level statistics, avoiding the need to store the entire O(N²) attention matrix. This makes it a drop-in replacement for standard attention in transformers without altering model behavior.
Dramatic Memory Footprint Reduction
The primary benefit is a reduction in memory complexity from O(N²) to O(N), where N is the sequence length.
- Standard Attention: Must store the N x N attention matrix for the backward pass.
- FlashAttention: Only stores the O(N) output and a few statistics, recomputing intermediates. This enables training and inference on sequences that are orders of magnitude longer (e.g., 16K, 32K, 64K tokens) on the same hardware.
Significant Wall-Clock Speedup
By minimizing I/O, FlashAttention provides substantial wall-clock speed improvements for both training and inference, especially on long sequences. Benchmarks show:
- Up to 3x faster training for BERT-large (seq length 512).
- Up to 2.4x faster training for GPT-2 (seq length 1K).
- The speedup increases with sequence length, as the I/O bottleneck becomes more dominant relative to compute.
Support for Attention Variants
The core tiling and recomputation technique is flexible and has been extended to support various attention variants crucial for modern architectures:
- FlashAttention-2: An optimized version with better work partitioning, reducing non-matmul FLOPs and improving parallelism.
- FlashAttention with Multi-Query/Grouped-Query Attention: Efficiently handles the key/value caching used during autoregressive decoding.
- Flash-Decoding: A variant optimized for long-context inference, efficiently parallelizing across query tokens when key/value sequences are very long.
Enabler for Long-Context Models
FlashAttention is a foundational technology behind the recent wave of long-context large language models. It makes training on book-length or multi-document sequences computationally feasible. This capability is critical for applications like:
- Long document summarization and analysis.
- Extended conversational agents with large memory.
- Code generation across entire codebases.
- Scientific reasoning over lengthy research papers.
FlashAttention vs. Standard Attention
A technical comparison of the core algorithmic and performance characteristics between the standard Transformer attention mechanism and the IO-aware FlashAttention optimization.
| Feature / Metric | Standard Attention | FlashAttention |
|---|---|---|
Algorithmic Complexity | O(N²) memory, O(N²) runtime | O(N) memory, O(N²) runtime |
Memory Access Pattern | HBM-Bound: Reads/writes full NxN attention matrix | IO-Aware: Fuses operations, avoids materializing full matrix |
Backward Pass Strategy | Stores full attention matrix for gradient calculation | Recomputes attention scores on-the-fly (recomputation) |
Exactness | Exact (numerically stable) | Exact (numerically equivalent) |
Primary Bottleneck | Memory bandwidth (HBM) | Arithmetic computation (FLOPS) |
Support for Long Sequences | ||
Typical Speedup (Training) | 1x (Baseline) | 2-4x |
Typical Memory Reduction | 1x (Baseline) | 5-20x |
Frameworks and Models Using FlashAttention
FlashAttention's dramatic reduction in memory usage and computational overhead has made it a foundational optimization, integrated into leading machine learning frameworks and powering state-of-the-art large language models.
Core Framework Integration
FlashAttention is not a standalone library but a kernel-level optimization integrated into deep learning frameworks. Its primary implementations are:
- PyTorch: Officially supported via the
torch.nn.functional.scaled_dot_product_attentionfunction, which uses FlashAttention kernels when available. - xFormers: A dedicated library from Meta that provides production-ready, optimized transformer building blocks, including stable FlashAttention implementations.
- NVIDIA's TransformerEngine: Integrates FlashAttention with FP8 quantization for optimal performance on Hopper and newer NVIDIA GPUs. These integrations allow developers to use FlashAttention transparently, often with a single line of code change, to accelerate any transformer-based model.
PagedAttention & vLLM
FlashAttention's principles directly inspired key innovations in high-throughput inference. PagedAttention is an attention algorithm that manages the Key-Value (KV) cache in non-contiguous, paged blocks, analogous to virtual memory in operating systems. This drastically reduces memory fragmentation during continuous batching. This algorithm is the core of the vLLM inference serving engine, which achieves near-zero waste in KV cache memory. While distinct from FlashAttention, PagedAttention solves the complementary problem of efficient memory management during generation, making it a critical partner technology for serving models trained with FlashAttention.
Llama & Mistral Model Families
The open-source LLM revolution was accelerated by FlashAttention. Major model families that used it during training include:
- Meta's Llama 2 & 3: Trained with FlashAttention to handle long context windows efficiently.
- Mistral AI's Models: The Mistral 7B and Mixtral 8x7B (a Sparse MoE model) leveraged FlashAttention for training speed and memory savings.
- Code Llama: Benefited from the ability to process long code sequences. Using FlashAttention allowed these models to be trained on longer sequences (e.g., 32k tokens) without prohibitive memory costs, directly contributing to their advanced capabilities.
FlashAttention-2 & Triton
FlashAttention-2 is a major revision that delivers up to 2x speedup over the original by:
- Better parallelization across thread blocks and warps on the GPU.
- Redesigning the workflow to reduce non-matmul operations.
- Optimizing for the Hopper GPU architecture (H100). It is written using OpenAI's Triton language, a Python-like DSL for writing efficient GPU kernels. This demonstrates how FlashAttention has evolved from an algorithm into a platform for designing ultra-efficient low-level neural network operations, influencing kernel design beyond just attention.
Long Context & Multimodal Models
FlashAttention is essential for pushing the boundaries of context length and multimodal understanding:
- GPT-4: Heavily rumored to use FlashAttention-like IO-aware algorithms to manage its massive context window.
- Claude (Anthropic): Models are known for extremely long contexts (100k+ tokens), necessitating memory-efficient attention.
- Multimodal LLMs (MLLMs): Models like LLaVA and Flamingo that process both images and text use FlashAttention to manage the long sequences created by concatenating visual tokens with text tokens. These use cases highlight FlashAttention's role as an enabling technology for the next generation of AI applications that require deep, long-form reasoning.
Research & Extended Algorithms
FlashAttention has spawned a family of related research algorithms that address its limitations:
- FlashDecoding: Optimizes the attention step during auto-regressive inference, where the KV cache is already computed, focusing on parallelizing across query tokens.
- FlashAttention-3: Introduces support for asynchronous copy and float8 data types on newer GPUs.
- Ring Attention: A distributed variant that partitions the sequence across devices and uses a ring communication pattern to compute attention for infinitely long sequences, overcoming single-device memory limits. These extensions ensure the core innovation continues to evolve for both training and inference at scale.
Frequently Asked Questions
FlashAttention is a foundational algorithm for modern large language models, enabling efficient processing of long sequences. These FAQs address its core mechanisms, benefits, and practical implications for AI engineering.
FlashAttention is an IO-aware, exact attention algorithm that recomputes attention scores on-the-fly during the backward pass to dramatically reduce memory reads/writes, enabling faster training and inference for long sequences. It works by leveraging the memory hierarchy of modern hardware (GPU SRAM vs. HBM). Instead of storing the massive attention matrix (size N x N for sequence length N) in high-bandwidth memory (HBM), which is slow to access, FlashAttention computes attention in smaller blocks within fast SRAM. It performs the softmax operation using a tiling strategy and saves only the final output to HBM. For backpropagation, it recomputes the attention scores from the input queries, keys, and values, trading off extra FLOPs for a massive reduction in memory I/O, which is the true bottleneck. This results in both wall-clock speedup and memory footprint reduction.
Enabling Efficiency, Speed & Accuracy
Intelligent Analysis, Decision & Execution
We build AI systems for teams that need search across company data, workflow automation across tools, or AI features inside products and internal software.
Talk to Us
Search across company data
Give teams answers from docs, tickets, runbooks, and product data with sources and permissions.
Useful when people spend too long searching or get different answers from different systems.

Automate internal workflows
Use AI to route work, draft outputs, trigger actions, and keep approvals and logs in place.
Useful when repetitive work moves across multiple tools and teams.

Add AI to products and internal tools
Build assistants, guided actions, or decision support into the software your team or customers already use.
Useful when AI needs to be part of the product, not a separate tool.
Related Terms
FlashAttention is a pivotal algorithm within a broader ecosystem of techniques designed to make large-scale neural networks more efficient, dynamic, and scalable. The following concepts are foundational to understanding its context and impact.
Sparse Attention
Sparse attention is a class of attention mechanisms that restrict the pairwise interactions between tokens to a predefined or dynamically computed sparse pattern. This is a direct alternative to the full, quadratic attention that FlashAttention optimizes.
- Key Idea: Instead of computing attention scores for every token pair (N²), it uses patterns like sliding windows, dilated windows, or random/bigbird patterns.
- Trade-off: While it reduces FLOPs, it is an approximation of full attention. FlashAttention, in contrast, is an exact algorithm that achieves efficiency through memory hierarchy optimization.
- Use Case: Essential for models handling extremely long contexts (e.g., 64k+ tokens) where even FlashAttention's linear memory usage might be prohibitive.
Linear Attention
Linear attention is a reformulation of the standard dot-product attention that approximates the softmax operation using kernel feature maps. This enables computation with linear complexity (O(N)) with respect to sequence length.
- Mechanism: It decomposes the attention operation into a form that allows associative aggregation, similar to a recurrent neural network.
- Comparison to FlashAttention: FlashAttention also achieves O(N) memory usage, but through IO-aware recomputation, not a mathematical reformulation. Linear attention models can sometimes trade some accuracy for massive sequence length scalability.
- Example: Models like Performer use a random feature map approximation to enable linear-time attention.
Reversible Layers
Reversible layers are neural network designs, such as the Reversible Transformer (RevNet), where activations can be exactly reconstructed from the output of the subsequent layer during the backward pass.
- Core Benefit: This eliminates the need to store most intermediate activations in memory for backpropagation, trading compute for a massive reduction in memory footprint.
- Synergy with FlashAttention: FlashAttention and reversible layers are complementary techniques. FlashAttention optimizes the attention block's memory, while reversible layers optimize the residual blocks. They can be combined to train exceptionally deep models on long sequences.
- Impact: Was crucial for models like Reformer, enabling longer context learning before FlashAttention's development.
Mixture of Experts (MoE)
Mixture of Experts is a neural architecture consisting of multiple specialized sub-networks (experts) and a gating network that dynamically routes each input to the most relevant subset of experts.
- Connection to Efficiency: Like FlashAttention, MoE is a conditional computation paradigm aimed at scalable models. It increases model capacity (parameters) without a proportional increase in compute (FLOPs per token).
- Sparse MoE: The prevalent variant where only a top-k (e.g., 2) experts are activated per token, making computation feasible. This sparsity is analogous to the memory access sparsity FlashAttention exploits.
- System Challenge: MoE models require sophisticated expert parallelism and routing, which are system-level optimizations parallel to the kernel-level optimizations of FlashAttention.
Conditional Computation
Conditional computation is a broad paradigm in deep learning where a model dynamically activates or selects different subsets of its parameters or computational pathways based on the input.
- Umbrella Term: Encompasses techniques like Mixture of Experts, Adaptive Computation Time (ACT), and Dynamic Filter Networks.
- Philosophical Link: FlashAttention is a form of conditional computation at the algorithmic and hardware level. It conditionally recomputes attention scores (activates compute) based on the memory hierarchy state to minimize IO.
- Goal: Both aim for more efficient than worst-case computation, adapting the model's resource usage to the specific demands of the input.
ZeRO (Zero Redundancy Optimizer)
The Zero Redundancy Optimizer (ZeRO) is a memory optimization technique for distributed training that partitions optimizer states, gradients, and model parameters across devices.
- Different Scope: While FlashAttention optimizes memory within a single GPU kernel for a single operation (attention), ZeRO optimizes memory across an entire cluster of GPUs for the whole training process.
- Complementary Role: ZeRO enables the training of models with trillions of parameters by eliminating memory redundancy. FlashAttention enables training these models on longer sequences by making the attention operation itself memory-efficient. They solve different bottlenecks in large model training.
- Usage: Foundational in frameworks like Microsoft DeepSpeed for training massive models like GPT-3 and beyond.

About the author
Prasad Kumkar
CEO & MD, Inference Systems
Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.
His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.
Partnered with leading AI, data, and software stack.
How We Work
Custom AI workflows for your Business
One-fit-all AI don't work for modern businesses. At Inferensys, we aim to understand your business & custom requirements; which we use to define most efficient agentic workflows, the data, and the tools for your business.
01
Review the use case
We understand the task, the users, and where AI can actually help.
Read more02
Pick the right approach
We define what needs search, automation, or product integration.
Read more03
Build the first useful version
We implement the part that proves the value first.
Read more04
Improve from there
We add the checks and visibility needed to keep it useful.
Read moreThe first call is a practical review of your use case and the right next step.
Talk to Us