Standard multi-head self-attention represents the core computational bottleneck in scaling modern Transformer models to long context windows. While the theoretical arithmetic operations of self-attention scale quadratically with sequence length , modern GPU execution profiles reveal that standard implementations are constrained not by floating-point arithmetic throughput (FLOPs), but by memory access latency and bandwidth between High Bandwidth Memory (HBM) and on-chip Static Random-Access Memory (SRAM).
FlashAttention, introduced by Tri Dao, Daniel Fu, Stefano Ermon, Atri Rudra, and Christopher Ré in 2022, re-engineers attention as an exact, IO-aware algorithm. By restructuring the computation to tile input matrices into on-chip SRAM blocks, computing softmax incrementally via the online softmax trick, and recomputing intermediate attention matrices during the backward pass, FlashAttention reduces memory access complexity from to , where is the SRAM capacity and is the head dimension. This eliminates the quadratic memory footprint without approximating attention weights, yielding substantial end-to-end training and inference acceleration.
The Hardware Memory Hierarchy and the Roofline Model
Understanding why standard attention bottlenecks on modern GPUs requires analyzing GPU memory hierarchies through the lens of the Roofline performance model.
Modern accelerator hardware architectures, such as NVIDIA Ampere (A100) and Hopper (H100), partition memory into distinct physical layers with contrasting capacity and bandwidth characteristics:
- High-Bandwidth Memory (HBM / Global Memory): Large capacity (40 GB to 80 GB on A100; 80 GB to 144 GB on H100), but constrained bandwidth (1.55 TB/s to 2.0 TB/s on A100 SXM4; 3.35 TB/s on H100 SXM5).
- On-Chip SRAM (Shared Memory / L1 Cache): Ultra-fast bandwidth (approximately 19 TB/s aggregate on A100; over 33 TB/s on H100), but constrained physical capacity (192 KB per Streaming Multiprocessor on A100; 228 KB per SM on H100, totaling roughly 20 MB to 50 MB across the entire chip).
- Tensor Core Register Files: Immediate single-cycle register storage directly wired into specialized matrix-multiply-accumulate (MMA) processing units.
The Roofline model defines the theoretical maximum performance (FLOP/s) as a function of arithmetic intensity (FLOPs per byte of memory transferred):
Where is peak compute throughput and is peak memory bandwidth.
When arithmetic intensity falls below the machine balance threshold , the operation is strictly memory-bandwidth bound (IO-bound). On an NVIDIA A100 (312 TFLOP/s BF16 Tensor Core compute, 2.0 TB/s HBM bandwidth), . Pointwise and reduction operations (such as dropout, masking, and elementwise softmax) operate at , spending the majority of their execution cycles stalling on HBM memory transfers.
The Standard Attention IO Bottleneck: Quadratic Memory Traffic
Given input sequence length , head dimension , and batch size (analyzed below for a single attention head), the standard attention mechanism evaluates:
Where are query, key, and value matrices.
In standard PyTorch or CUDA implementations, each distinct operation is executed as a separate GPU kernel call:
Algorithm 1: Standard Multi-Head Attention Execution Flow
-----------------------------------------------------------
1. Load Q, K from HBM -> Compute S = Q K^T in SRAM -> Write S to HBM (size N x N)
2. Load S from HBM -> Compute P = softmax(S) in SRAM -> Write P to HBM (size N x N)
3. Load P, V from HBM -> Compute O = P V in SRAM -> Write O to HBM (size N x d)Even though the matrix multiplications ( FLOPs) and ( FLOPs) exhibit high theoretical arithmetic intensity when and are large, the intermediate materialization of and in HBM creates severe IO overhead:
- HBM Reads: (), (), (), () elements.
- HBM Writes: (), (), () elements.
- Total HBM Access: memory reads and writes.
When and , the intermediate matrices and each require elements ( in 16-bit precision per head). Across 32 attention heads, materializing and requires over of intermediate HBM allocations per layer. At sequence lengths of or , standard attention causes immediate out-of-memory (OOM) failures or throttles GPU compute utilization to under 15% of peak theoretical capacity.

The IO-Awareness Theorem and Theoretical Lower Bounds
Dao et al. formalized the memory communication model for attention by analyzing data movement between fast memory (SRAM of capacity ) and slow memory (HBM).
Theorem (IO Complexity of Standard Attention vs. FlashAttention)
Let be the sequence length, be the head dimension, and be the fast memory size where .
- Standard Attention requires HBM memory accesses.
- FlashAttention computes exact attention using HBM memory accesses.
- For any algorithm computing attention within the class of matrix-multiplication based operations where the output , any computation must perform memory accesses between HBM and SRAM.
Because FlashAttention matches the lower bound , it is asymptotically optimal in IO complexity. By choosing block sizes , FlashAttention reduces total memory access traffic by a factor of , which in practice translates to a to reduction in HBM bandwidth consumption.
Online Softmax Mathematics and Numerical Stability
The mathematical barrier to tiling the attention computation across blocks is the non-local normalization term in the standard three-pass softmax function.
Given a row vector , standard numerically stable softmax computes:
Computing and requires observing all elements before normalizing any individual component, which traditionally prevents executing softmax in a single pass over sub-blocks.
FlashAttention overcomes this limitation using the online softmax formulation (pioneered by Milakov and Gimelshein and analyzed for attention by Rabe and Staats).
Suppose a vector is partitioned into two consecutive concatenated blocks where and .
Let the local statistics for block 1 be:
When block 2 arrives, local statistics are calculated:
The global maximum and global normalizer combining both blocks are computed analytically without revisiting individual elements of :
Output Vector Rescaling Dynamics
To compute the attention output incrementally, let be the intermediate output accumulator computed up to block 1:
Upon processing block 2, the updated unnormalized accumulated vector is scaled to reflect the new global maximum and summed with the new block contribution:
This recursive formulation generalizes inductively across arbitrary numbers of blocks , ensuring mathematical equivalence to full-sequence softmax while maintaining numeric stability against floating-point overflow.
Tiling Algorithm: SRAM Block Execution and Forward Pass Mechanics
FlashAttention maps the mathematical recurrence directly to GPU execution blocks.
Given SRAM capacity , block sizes are configured as:
Let and . The matrices are partitioned into row and column blocks:
- where
- where
- where
Algorithm 2: FlashAttention Forward Pass
-----------------------------------------------------------
Require: Q, K, V in HBM of dimensions N x d; SRAM memory size M.
1. Initialize O = (0)_{N x d} in HBM, l = (0)_N in HBM, m = (-inf)_N in HBM.
2. For j = 1 to T_c do:
3. Load K_j, V_j from HBM to SRAM
4. For i = 1 to T_r do:
5. Load Q_i, O_i, l_i, m_i from HBM to SRAM
6. Compute S_ij = (Q_i K_j^T) / sqrt(d) in SRAM
7. Compute m_ij = rowmax(S_ij), p_tilde_ij = exp(S_ij - m_ij), l_ij = rowsum(p_tilde_ij)
8. Compute m_i_new = max(m_i, m_ij)
9. Compute l_i_new = exp(m_i - m_i_new) * l_i + exp(m_ij - m_i_new) * l_ij
10. Compute O_i = diag(l_i * exp(m_i - m_i_new) / l_i_new) * O_i
+ diag(exp(m_ij - m_i_new) / l_i_new) * p_tilde_ij * V_j
11. Write O_i, l_i_new, m_i_new back to HBM
12. Return OBy fusing matrix multiplication, masking, softmax reduction, and value multiplication into a single fused GPU kernel, the attention matrix is never written to or read from HBM.
Exact Gradient Backpropagation with On-Chip Recomputation
During standard deep learning backpropagation, intermediate activations from the forward pass must be retained in memory to compute backward gradients. For standard attention, this requires retaining the matrix in HBM for each attention head across every transformer layer.
FlashAttention completely eliminates the backward memory storage requirement through activation recomputation.
Instead of storing , the forward pass writes only the output and the compact softmax statistics to HBM.
Activation Storage Footprint Comparison per Layer (Single Head):
-----------------------------------------------------------------
Standard Attention: O(N^2) elements (Matrix P stored in HBM)
FlashAttention: O(N d + N) elements (Q, K, V and statistics m, l)During the backward pass:
- Blocks of and statistics are reloaded into SRAM from HBM.
- The attention matrix block and normalized probability block are recomputed on-the-fly directly inside fast SRAM registers.
- Gradients with respect to queries , keys , and values are accumulated:
Although recomputing and requires an additional FLOPs (a theoretical 33% increase in backward FLOPs), execution speed increases by to because the kernel avoids reading gigabytes of attention weights over the slow HBM interconnect.
Evolution Across Generations: FlashAttention-1, FlashAttention-2, and FlashAttention-3
The FlashAttention architecture has progressed through three major generational refinements, adapting to modern GPU microarchitectures:
FlashAttention-1 (2022)
- Target Hardware: NVIDIA Ampere (A100) and Turing architectures.
- Outer Loop Structure: Outer loop iterates over blocks; inner loop iterates over blocks.
- Parallelization Scheme: Parallelized across Batch and Attention Heads dimensions.
- Accumulator Rescaling: Rescaled output at each step in SRAM, incurring non-matmul FLOPs.
- Peak Compute Efficiency: Achieved 25% to 40% of theoretical peak device FLOPs.
FlashAttention-2 (2023)
- Target Hardware: NVIDIA Ampere, Ada Lovelace, and Hopper architectures.
- Outer Loop Structure: Inverted loop ordering: outer loop iterates over blocks; inner loop iterates over blocks.
- Parallelization Scheme: Parallelized across Batch, Attention Heads, and Sequence Length ( blocks), maximizing Streaming Multiprocessor occupancy even for batch size 1.
- Accumulator Rescaling: Deferring division by the normalizer until final loop exit, accumulating unnormalized values directly in registers.
- Peak Compute Efficiency: Achieved 50% to 73% of theoretical peak device FLOPs.
FlashAttention-3 (2024)
- Target Hardware: NVIDIA Hopper architecture (H100, H200).
- Asynchronous Data Movement: Leverages Tensor Memory Accelerator (TMA) hardware units to transfer tensor tiles directly between HBM and SRAM bypassing register files.
- Warp Specialization: Partitions warps into dedicated producer and consumer groups, achieving complete hardware overlap between memory loading and matrix multiply operations.
- Low-Precision FP8 Attention: Supports FP8 (E4M3 and E5M2) attention pipelines with block quantization and GEMM scaling.
- Peak Compute Efficiency: Achieved 75% to 85% of theoretical peak device compute (over 650 to 800 TFLOP/s FP16 and up to 1.2 PFLOP/s FP8).
Quantitative Hardware Utilization and Serving Benchmarks
The operational benefits of IO-aware attention extend across both foundation model pre-training and high-concurrency inference serving.
GPU Execution Profile on NVIDIA A100 (BF16, d=128, Forward Pass):
-----------------------------------------------------------------
Sequence Length (N) Standard PyTorch Attention FlashAttention-2 Compute Speedup
2,048 0.95 ms 0.32 ms 3.0x
8,192 12.80 ms 2.10 ms 6.1x
16,384 51.40 ms 6.40 ms 8.0x
64,512 Out Of Memory (OOM) 52.30 ms Enables Long ContextMemory Footprint Reduction
By reducing peak activation memory from to (where is model depth and is head count), FlashAttention allows training context windows to scale from 2,048 tokens to 32,768, 131,072, and beyond on standard cluster topologies without requiring activation checkpointing to disk or multi-node tensor parallel communication overhead.
Impact on Modern LLM Architectures
FlashAttention has been adopted as the standard attention kernel across major inference and training runtimes, including vLLM, TensorRT-LLM, Hugging Face Transformers, Megatron-LM, and PyTorch (torch.nn.functional.scaled_dot_product_attention). Its principles have also been extended to adjacent paradigms:
- PagedAttention: Applying FlashAttention block tiling over non-contiguous virtual memory page tables to eliminate KV cache fragmentation in inference engines.
- FlashDecoding / FlashDecoding++: Partitioning the key-value sequence length across multiple thread blocks and performing parallel reductions to accelerate single-token autoregressive generation at long context lengths.
- Multi-Head Latent Attention (MLA): Combining low-rank key-value projections with fused FlashAttention kernels to compress KV cache memory bandwidth during autoregressive decoding.
By identifying the memory hierarchy as the true performance limiter of deep sequence models, IO-aware computing has transformed the algorithmic foundation of large-scale artificial intelligence.
Sources
- FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness (Dao et al., NeurIPS 2022)
- FlashAttention-2: Faster Attention with Better Parallelism and Work Partitioning (Dao, ICLR 2024)
- FlashAttention-3: Fast and Accurate Attention with Asynchrony and Low-Precision (Shah et al., 2024)
- Online normalizer calculation for softmax (Milakov & Gimelshein, 2018)
- Self-attention Does Not Need Memory (Rabe & Staats, 2021)
- Tri Dao's FlashAttention Repository (GitHub)



