FlashAttention is an exact, hardware-aware algorithm that dramatically speeds up transformer training and inference by optimizing memory access between GPU memory hierarchies. It computes the standard softmax attention exactly but avoids the costly reading and writing of the large, intermediate attention matrix to slow High-Bandwidth Memory (HBM). Instead, it performs repeated recomputation of attention scores within fast SRAM (Static Random-Access Memory), trading increased FLOPs for a massive reduction in memory I/O, which is the true bottleneck.
Glossary
FlashAttention

What is FlashAttention?
FlashAttention is a groundbreaking I/O-aware algorithm that redefines the computational efficiency of the transformer's self-attention mechanism.
The algorithm employs tiling to chunk the attention computation, and online softmax techniques to maintain numerical stability, all while keeping the key-value (KV) cache operations efficient. This I/O complexity reduction from quadratic to linear in sequence length enables processing of much longer contexts. It is a foundational optimization behind modern large language models, directly enabling faster iteration and lower costs for both training and autoregressive inference.
Key Features and Benefits of FlashAttention
FlashAttention is an I/O-aware, exact attention algorithm that recomputes attention scores on-the-fly within fast SRAM to avoid reading and writing the large attention matrix to slow HBM, dramatically speeding up training and inference.
Dramatic Speedup for Long Sequences
The performance gains of FlashAttention scale quadratically with sequence length due to the avoided O(N²) HBM accesses.
- Training: Enables training transformers on sequences up to 16k tokens long, unlocking new domains like long-form text, high-resolution image, and audio generation.
- Inference: Reduces memory footprint of the key-value (KV) cache, allowing for larger batch sizes or longer context windows during generation.
- Benchmark: Can be 2-4x faster than standard attention for sequences of 2k tokens, with gains exceeding 10x for longer sequences, while using 5-20x less memory.
Enabler for Longer Context Models
FlashAttention directly addresses the core bottleneck preventing long-context transformers: the quadratic memory complexity of attention. It has become a foundational component for state-of-the-art large language models (LLMs) that feature extended context windows, such as GPT-4, Claude, and Llama 2. By making it feasible to train and run inference on sequences of 32k, 128k, or even 1M+ tokens, it enables applications like:
- Legal document analysis
- Long-context conversation
- Codebase-wide reasoning
- Genomic sequence modeling
Integration with Other Optimizations
FlashAttention is not a standalone technique but a core primitive that combines synergistically with other efficiency methods:
- Multi-Query Attention (MQA) & Grouped-Query Attention (GQA): Reduces the size of the KV cache that FlashAttention must manage.
- PageAttention (vLLM): Uses a similar block-wise approach for managing the KV cache in LLM serving systems.
- Quantization: FlashAttention-2 supports FP16 and BF16 precision, and research extends it to INT8 quantization.
- Causal Masking: Optimized implementation for the autoregressive masking used in language model decoders.
FlashAttention vs. Standard Attention: A Comparison
A technical comparison of the core algorithmic and performance characteristics between the standard Transformer attention mechanism and the FlashAttention optimization.
| Feature / Metric | Standard Attention | FlashAttention |
|---|---|---|
Algorithmic Complexity | O(N²) in memory & time | O(N²) in time, O(N) in memory |
Primary Memory Bottleneck | High Bandwidth Memory (HBM) I/O | Fast On-Chip SRAM (Static RAM) |
Attention Matrix Storage | Explicitly materialized in HBM | Never fully materialized; recomputed on-the-fly |
Key Technical Method | Compute attention scores, save matrix, apply softmax, multiply with values | Tiling & Recomputation: Process blocks in SRAM, recompute attention scores in backward pass |
Memory Footprint (Forward + Backward) | ~O(N² + N*d) for storing scores & gradients | ~O(N) for storing only output & block-level statistics |
Exactness | Exact (up to floating point) | Exact (numerically identical to standard attention) |
Typical Speedup (Training, Long Sequences) | 1x (Baseline) | 2-4x |
Typical Memory Reduction (Training) | 1x (Baseline) | 5-20x |
Inference Optimization | Requires full KV cache in HBM | Enables very large KV caches via memory savings |
Hardware Requirement | Standard GPU | GPU with sufficient SRAM (e.g., NVIDIA A100, H100) |
Integration Complexity | Standard implementation | Requires custom CUDA kernel implementation |
Where is FlashAttention Used?
FlashAttention is not a theoretical algorithm but a foundational engineering breakthrough deployed across the AI stack to overcome the memory bottleneck of standard attention, enabling longer contexts, faster training, and cheaper inference.
Enabling Efficient Model Architectures
FlashAttention directly enables other efficient model architectures listed as sibling topics. It provides the computational foundation for:
- Mixture of Experts (MoE): Efficiently routes long sequences through sparse experts.
- Grouped-Query & Multi-Query Attention (GQA/MQA): Reduces KV cache size, and FlashAttention optimizes the remaining attention computation.
- Long-Context Positional Encodings: Methods like ALiBi and RoPE rely on FlashAttention to efficiently apply positional biases over long sequences.
- Dynamic Inference & Early Exiting: Makes the evaluation of attention at each layer more efficient, improving the viability of conditional computation paths. Thus, FlashAttention is a primitive upon which many other efficiency techniques are built.
Frequently Asked Questions About FlashAttention
FlashAttention is a groundbreaking algorithm that dramatically speeds up transformer training and inference by optimizing memory access. This FAQ addresses the core technical questions developers and architects have about its mechanism, benefits, and applications.
FlashAttention is an I/O-aware, exact attention algorithm that recomputes attention scores on-the-fly within fast SRAM to avoid reading and writing the large attention matrix to slow high-bandwidth memory (HBM). It works by tiling the attention computation: the input queries, keys, and values are loaded in blocks from HBM into SRAM, the attention output for that block is computed, and the result is written back to HBM. This recomputation in the backward pass avoids storing the massive O(N²) attention matrix, trading extra FLOPs for significantly reduced memory I/O, which is the primary bottleneck.
Key Mechanism:
- Tiling: Splits the Q, K, V matrices into blocks that fit in SRAM.
- Recomputation: In the backward pass, it recomputes attention on-chip instead of reading the saved matrix from HBM.
- I/O Complexity: Reduces HBM accesses from O(N²) to O(N²d²M⁻¹), where M is SRAM size.
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 in Efficient Model Architectures
FlashAttention is a pivotal algorithm for efficient transformer models. Understanding its related concepts is essential for architects designing high-performance, low-latency systems.
Memory Hierarchy & I/O Complexity
FlashAttention's core innovation is being I/O-aware. It optimizes for the memory hierarchy of modern accelerators (GPU/TPU), where:
- SRAM is fast but small (tens of MB).
- High Bandwidth Memory (HBM) is large but slow (hundreds of GB). The standard attention algorithm's quadratic memory complexity stems from writing the large attention matrix (N x N) to HBM. FlashAttention avoids this by fusing operations and recomputing scores on-the-fly in SRAM, trading extra FLOPs for drastically reduced HBM reads/writes.
Key Techniques: Tiling & Recomputation
FlashAttention achieves its efficiency through two classical techniques applied to the attention mechanism:
- Tiling: It splits the input Query (Q), Key (K), and Value (V) matrices into blocks that fit in SRAM. Attention is computed block-by-block, accumulating a running statistic.
- Recomputation (Backward Pass): During the backward pass, it does not store the large attention matrix. Instead, it recomputes the attention scores from the stored output and the input blocks (Q, K, V). This gradient checkpointing-like approach slashes memory usage from O(N²) to O(N) with a manageable compute overhead.
FlashAttention-2 & Algorithmic Optimizations
FlashAttention-2 is a major revision that further reduces the number of non-matmul FLOPs and improves work partitioning across GPU warps and thread blocks. Key optimizations include:
- Better Parallelization: It parallelizes over sequence length (more warps) instead of just batch and heads.
- Reduced Synchronization: Minimizes communication between warps when computing softmax.
- Improved Occupation: Better utilization of GPU streaming multiprocessors (SMs). These changes lead to up to 2x speedup over the original FlashAttention, approaching closer to the hardware's theoretical peak performance for attention.
Related Efficient Attention Variants
FlashAttention is an exact attention algorithm. Other approaches trade exactness for efficiency:
- Sparse Attention (e.g., Sliding Window): Each token attends only to a local window, reducing complexity to O(N*W).
- Linear Attention: Reformulates attention as a kernelized linear dot-product (e.g., Performer), achieving O(N) complexity.
- Multi-Query (MQA) / Grouped-Query (GQA) Attention: Reduce the size of the KV Cache in autoregressive decoding, which pairs exceptionally well with FlashAttention to speed up inference. FlashAttention is often complementary, used to accelerate the core attention operation within these broader architectural patterns.
Impact on Model Scaling & Training
By reducing memory and runtime, FlashAttention directly enables:
- Longer Context Windows: Training and inference on sequences of >100k tokens become feasible, unlocking new applications in long-document analysis and code generation.
- Larger Batch Sizes: More samples per batch improve hardware utilization and training stability.
- Reduced Training Costs: Faster iteration and lower memory overhead decrease the dollar cost and carbon footprint of training large models. It is a foundational technology behind modern long-context models like Llama 3 and GPT-4, making the quadratic attention bottleneck practically manageable for unprecedented sequence lengths.
Integration with Hardware Kernels
FlashAttention is not just an algorithm but a hand-optimized GPU kernel. Its performance stems from:
- CUDA-Level Optimization: Direct use of NVIDIA's CUDA programming model and libraries like CUTLASS for efficient matrix multiplications.
- Memory Access Patterns: Careful design to ensure coalesced memory reads/writes and effective use of shared memory (SRAM).
- Fused Operations: Combining softmax, masking, and dropout into a single kernel launch to avoid expensive intermediate HBM I/O. This makes it a prime example of hardware-aware model design, where the algorithm and its low-level implementation are co-designed for maximum efficiency on target silicon.

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