Flash Attention is an I/O-aware, exact attention algorithm that recomputes parts of the attention mechanism on-the-fly to drastically reduce the memory reads and writes between high-bandwidth memory (HBM) and on-chip SRAM, leading to significant speedups and memory savings for Transformer models. It transforms the standard, memory-intensive attention computation by using tiling and recomputation to achieve linear memory complexity in sequence length, enabling the processing of much longer contexts than previously possible.
Glossary
Flash Attention

What is Flash Attention?
A foundational algorithm for efficient Transformer model execution on modern hardware accelerators.
The algorithm is a prime example of hardware-aware model optimization, explicitly designed to align with the memory hierarchy of accelerators like GPUs and NPUs. By minimizing expensive HBM accesses—the primary bottleneck—and maximizing data reuse in fast SRAM, Flash Attention provides both wall-clock speedup and reduced memory footprint. It is a critical enabler for large language models (LLMs) and belongs to the broader class of memory-efficient attention mechanisms essential for modern AI systems.
Key Features and Benefits
Flash Attention is an I/O-aware, exact attention algorithm that recomputes parts of the attention mechanism on-the-fly to drastically reduce memory traffic between high-bandwidth memory (HBM) and on-chip SRAM.
I/O-Aware Algorithm Design
Flash Attention is explicitly designed to minimize the number of reads and writes between slow, high-capacity High-Bandwidth Memory (HBM) and fast, limited Static Random-Access Memory (SRAM). It treats these memory transfers as the primary bottleneck, not floating-point operations (FLOPs). By strategically recomputing intermediate attention scores during the backward pass instead of storing them, it achieves an optimal trade-off, reducing HBM accesses from quadratic O(N²) to linear O(N) with respect to sequence length.
Tiling and Recomputation
The core mechanism enabling memory savings is tiling. The algorithm splits the input Query (Q), Key (K), and Value (V) matrices into smaller blocks that fit in SRAM.
- It loads a block of Q and a block of K/V into SRAM.
- Computes a local block of the attention matrix and immediately applies the softmax, scaling intermediate results with a correction factor.
- Accumulates the final output block-by-block. Crucially, it does not store the large NxN attention matrix. During backpropagation, it re-computes the attention blocks on-the-fly using the same tiling strategy, trading extra FLOPs for massive memory savings.
Exact Attention with Linear Memory
Unlike approximate methods (e.g., sparse or linear attention), Flash Attention computes the exact standard softmax attention. It produces the same numerical output as the naive implementation but does so with O(N) memory complexity instead of O(N²). This is a paradigm shift: it removes the fundamental memory barrier that limited Transformer context lengths, enabling training and inference with sequences of 16k, 32k, or even 100k+ tokens without approximation error.
Significant Speedup and Throughput
By minimizing expensive HBM I/O, Flash Attention provides substantial wall-clock speedups, often 2-4x faster than standard attention implementations on modern GPUs (e.g., A100, H100). This translates directly to:
- Higher training throughput (samples/second).
- Faster inference for long-context models.
- Reduced hardware costs and energy consumption per experiment. The speedup is most pronounced on memory-bound operations, making attention the new efficiency frontier.
Enabler for Long-Context LLMs
Flash Attention is the foundational technology that made today's long-context Large Language Models (LLMs) practical. By eliminating the quadratic memory bottleneck, it allows models to process entire books, lengthy codebases, or extended multi-turn conversations in a single forward pass. This capability is critical for advanced Retrieval-Augmented Generation (RAG), agentic workflows, and multi-document analysis that require large context windows.
Hardware-Aware Optimization
The algorithm is meticulously tuned for modern accelerator architectures. It leverages:
- SRAM hierarchy (e.g., GPU shared memory / L1 cache).
- Warp-level operations for efficient on-chip communication.
- Non-persistent kernel design to reduce launch overhead. This hardware-aware design makes it a prime example of Hardware-Aware Model Optimization, where the algorithm is co-designed with the physical constraints of the silicon to achieve peak efficiency.
Flash Attention vs. Standard Attention
A technical comparison of algorithmic and hardware-level differences between the standard attention mechanism and its I/O-optimized variant, Flash Attention.
| Feature / Metric | Standard Attention | Flash Attention |
|---|---|---|
Algorithmic Approach | Compute full attention matrix S=QK^T, apply softmax, compute output O=SV. | Fuses scaling, masking, softmax, and dropout into a single, tiled kernel using online softmax. |
Memory Complexity (HBM) | O(N²) for storing the full NxN attention matrix S and its gradient. | O(N) for storing only the final output O and its gradient; intermediate matrices are not materialized. |
Primary Bottleneck | Memory bandwidth (I/O-bound). Repeated reads/writes of the large S matrix to/from HBM. | Compute (compute-bound). Efficient use of fast on-chip SRAM for fused operations. |
I/O Operations (Theoretical) | O(N²) reads/writes between HBM and SRAM for S and its gradient. | O(N²d / M) reads/writes, where M is SRAM size. Dramatically lower total HBM traffic. |
Kernel Count | Multiple separate kernels (matmul, softmax, dropout, another matmul). | Single, fused kernel implementing the entire attention block. |
Exactness | Exact, numerically stable. | Exact, numerically equivalent to standard attention. |
Support for Dropout | Yes, applied after softmax. | Yes, fused into the main kernel with dropout mask recomputation. |
Support for Causal Masking | Yes, applied before softmax. | Yes, integrated into the tiled computation with online softmax. |
Typical Speedup (Training, Long Seq.) | Baseline (1x). | 2-4x faster on GPT-2 scale models with sequence length 1K-4K. |
Typical Memory Saving (Training) | Baseline (1x). | 10-20x reduction in memory footprint, enabling longer sequence lengths. |
Hardware Utilization | Often memory-bandwidth limited, leaving compute units underutilized. | Shifts bottleneck to compute, achieving higher FLOP/s utilization on modern accelerators. |
Implementation Complexity | Straightforward, implemented as separate PyTorch/TensorFlow ops. | High. Requires careful manual CUDA/GPU kernel programming for tiling and online softmax. |
Compiler Friendliness | High. Standard operations are easily recognized and optimized by compilers like XLA/TVM. | Low. The custom, monolithic kernel is a black box to high-level compilers. |
Frameworks and Hardware Support
Flash Attention is not a standalone library but an optimized algorithm integrated into major deep learning frameworks and hardware backends. Its adoption is driven by its ability to provide exact attention with significantly improved performance and memory efficiency.
NVIDIA GPU Architecture
Flash Attention is meticulously optimized for NVIDIA GPU memory hierarchies, particularly leveraging:
- High-Bandwidth Memory (HBM): The slow, large-capacity main memory. The algorithm minimizes HBM transactions.
- SRAM (Shared Memory): Fast, on-chip memory (≈19TB/s bandwidth on H100). Flash Attention tiles the computation to keep blocks of the Q, K, V matrices and the attention output in SRAM.
- Tensor Cores: Uses mixed-precision (FP16/BF16) matrix multiply-accumulate operations on dedicated Tensor Cores for the core attention score (QK^T) computation. Its performance gains are most pronounced on architectures with large SRAM and high memory bandwidth, such as the Ampere (A100) and Hopper (H100) architectures.
AMD & Other Hardware Ports
While initially developed for NVIDIA CUDA, the algorithm's principles are hardware-agnostic, leading to ports for other platforms:
- AMD ROCm: Official PyTorch builds with ROCm support include Flash Attention kernels optimized for AMD GPUs (e.g., MI250X, MI300) using HIP.
- Apple Silicon: The MLX framework from Apple includes a Flash Attention implementation optimized for the unified memory architecture of M-series chips.
- Custom Accelerators: The algorithm's design—recomputation and tiling to minimize external memory I/O—serves as a blueprint for implementing attention on custom AI accelerators (ASICs) and neural processing units (NPUs).
Frequently Asked Questions
Flash Attention is a foundational algorithm for optimizing Transformer models on modern hardware. These questions address its core mechanisms, benefits, and practical applications.
Flash Attention is an I/O-aware, exact attention algorithm that recomputes parts of the attention mechanism on-the-fly to drastically reduce memory reads and writes between high-bandwidth memory (HBM) and on-chip SRAM. It works by tiling the large attention matrices, loading small blocks into fast SRAM, performing the full attention computation (softmax and scaling) on that block, and then writing only the final output back to HBM. This approach trades extra FLOPs for a massive reduction in slower HBM accesses, which is the true bottleneck. The algorithm maintains mathematical equivalence to standard attention while achieving linear memory complexity in sequence length, enabling the processing of much longer contexts.
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
Flash Attention is a pivotal technique within hardware-aware model optimization. These related concepts detail the broader ecosystem of methods for adapting neural networks to maximize efficiency on modern accelerators.
Memory-Efficient Attention
A broad category of algorithms designed to reduce the quadratic memory complexity of the standard attention mechanism. Flash Attention is the most prominent exact algorithm in this class. Other variants include:
- Approximate methods like Linformer or Reformer, which use low-rank projections or hashing.
- The core innovation is achieving linear memory scaling with sequence length, which is critical for processing long documents, code, or audio.
Operator Fusion
A foundational compiler optimization where multiple sequential operations are merged into a single fused kernel. This is the conceptual precursor to Flash Attention's approach. Key benefits:
- Eliminates intermediate memory writes (e.g., saving the large attention matrix to HBM).
- Reduces kernel launch overhead on the GPU/NPU.
- In attention, fusion combines the softmax, masking, and dropout steps with the matrix multiplies, which Flash Attention extends via on-the-fly recomputation within SRAM.
Gradient Checkpointing
A training-time memory optimization that trades compute for memory. It selectively saves only a subset of layer activations during the forward pass, then recomputes the non-checkpointed activations during the backward pass. Relation to Flash Attention:
- Both use selective recomputation as a core strategy to bypass memory bottlenecks.
- Flash Attention applies this principle within a single layer's computation (the attention block) to avoid storing the large attention matrix, while gradient checkpointing operates across layers of the entire network.
Roofline Model & Operational Intensity
An analytical framework for understanding hardware performance limits. The Roofline Model plots attainable performance (FLOPS/sec) against a kernel's Operational Intensity (Ops/Byte).
- Standard attention is memory-bound: Its operational intensity is low due to massive data movement of the
QK^Tmatrix. - Flash Attention increases operational intensity by keeping data in fast SRAM, making the computation more compute-bound and thus achieving a higher fraction of the hardware's peak FLOPS.
Hardware-Aware Neural Architecture Search (NAS)
An automated process for designing neural network architectures that are optimized for specific hardware constraints like latency, memory, or power. Relation to Flash Attention:
- Flash Attention is a fixed, optimal algorithm for a given operation (attention).
- Hardware-Aware NAS searches for optimal model structures that would benefit most from algorithms like Flash Attention (e.g., models with long sequence lengths).
- Together, they represent algorithm-level and architecture-level approaches to hardware efficiency.
Just-In-Time (JIT) Compilation
A compilation strategy where code is translated to machine instructions at runtime. Frameworks like TorchDynamo or Triton (which implements Flash Attention) use JIT.
- Enables hardware-specific optimizations based on actual input shapes (e.g., sequence length, batch size).
- Flash Attention kernels are often JIT-compiled to specialize for the exact problem size, allowing for optimal selection of tiling parameters and thread block sizes for the target GPU/NPU.

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