The original FlashAttention eliminated attention’s quadratic memory bottleneck, yet it still left more than half of an NVIDIA A100’s compute units idle. While optimized matrix multiplication (GEMM) kernels routinely reach 80% to 90% of theoretical hardware peak, FlashAttention hit only 30% to 50% in the forward pass and 25% to 35% in the backward pass.
In 2023, Tri Dao (Princeton University and Stanford University) showed that the remaining bottleneck was no longer memory bandwidth alone: it was how the kernel scheduled work across GPU hardware. By shrinking non-matmul arithmetic, parallelizing across the sequence dimension, and reordering how warps split each tile, FlashAttention-2 roughly doubled kernel speed to 230 TFLOPs/s (73% of theoretical A100 peak) and trained 8k-context GPT-style models at up to 225 TFLOPs/s per GPU (72% model FLOPs utilization). For teams scaling context length, doubling kernel efficiency cuts the cost of training a 16k-context model down to what an 8k-context run previously required.
- A 16x hardware asymmetry: On an A100, FP16/BF16 Tensor Core matmuls run at 312 TFLOPs/s while non-matmul FP32 operations run at 19.5 TFLOPs/s, making every extra exponential or rescaling step 16x more expensive than a matmul FLOP.
- Parallelism across sequence length: Splitting work over rows (forward) and columns (backward) keeps all 108 A100 streaming multiprocessors busy even when long contexts force batch sizes down to 1.
- Splitting
Qinstead ofKacross warps: Changing warp work partitioning eliminates the “split-K” shared-memory reduction barrier inside each thread block. - Measured kernel and training gains: FlashAttention-2 runs 1.7x to 3.0x faster than FlashAttention and up to 10x faster than standard PyTorch attention, reaching 1.3x end-to-end training speedup at 8k context (220 to 225 TFLOPs/s vs. 170 to 175 TFLOPs/s).
Why IO-aware tiling still stalled below 40% FLOPs
Modern GPUs pair fast compute with a tiered memory hierarchy. An NVIDIA A100 holds 40 GB to 80 GB of high-bandwidth memory (HBM) at 1.5 TB/s to 2.0 TB/s alongside 192 KB of on-chip SRAM per streaming multiprocessor (108 SMs total) at roughly 19 TB/s. Because standard attention executes S = QK^T, P = softmax(S), and O = PV as separate kernels, it materializes the N x N matrices S and P in HBM. On an 8,192-token sequence in FP16, one head’s score matrix alone consumes 128 MiB of HBM, and standard PyTorch runs out of memory by 16k tokens.
FlashAttention removed those HBM round-trips using tiling and online softmax. It streams B_r x d blocks of Q and B_c x d blocks of K, V into SRAM, tracks running softmax statistics (m for row max, l for exponential sum), and rescales partial outputs block by block. In backpropagation, it recomputes S and P inside SRAM from saved Q, K, V, O and softmax statistics, cutting extra memory from O(N^2) to O(N) for a 2x to 4x speedup over PyTorch.
Yet even without quadratic HBM traffic, FlashAttention hit an arithmetic ceiling. An A100 delivers 312 TFLOPs/s for FP16/BF16 Tensor Core matmuls, versus 19.5 TFLOPs/s for non-matmul FP32 ops: every exponential, max, or division costs 16x more per FLOP. FlashAttention rescaled both terms of the output accumulator by diag(l)^-1 on every tile and wrote both m and l to HBM.
FlashAttention-2 rewrites the inner loop to keep Tensor Cores fed without changing the mathematical result. It accumulates an unscaled output O_tilde, scaling only the prior accumulator by exp(m_old - m_new) when a tile updates the row maximum, and divides by diag(l) once at the end. For backward recomputation, it stores only one logsumexp vector L = m + log(l) per row, simplifying recovery to P_ij = exp(S_ij - L_i). Under causal masking, it skips upper-triangular blocks entirely and masks at most one diagonal block per row (a 1.7x to 1.8x speed boost), and handles multi-query and grouped-query attention by indexing KV heads directly without duplicating tensors in memory.
Parallelizing across sequences and cutting warp communication
CUDA packs 32 threads into a warp, 4 or 8 warps into a thread block, and schedules blocks onto 108 SMs. FlashAttention parallelized only over batch_size and num_heads, which saturates the GPU when their product exceeds 80 (such as batch 32 with 32 heads, or 1,024 blocks). On long sequences, however, fixed token budgets force small batches. In Dao’s benchmark (16,384 tokens per batch across 16 heads of dimension 128), a 16k-context run has batch size 1 and just 16 thread blocks, leaving 92 of 108 SMs idle.
FlashAttention-2 parallelizes along the sequence dimension as well. Forward pass adopts the loop order pioneered by Phil Tillet in Triton: outer loop over query blocks Q_i, inner loop over K_j, V_j. Every (batch, head, row_block) becomes an independent thread block with zero cross-block communication. Backward pass parallelizes over K_j, V_j column blocks instead, keeping dK_j and dV_j updates local in SRAM while using HBM atomic adds for shared dQ_i updates.
Inside each thread block, FlashAttention split K and V across 4 warps (“split-K”) while sharing Q, forcing all warps to write partial outputs to SRAM, synchronize, and sum on every tile. FlashAttention-2 splits Q across 4 warps while all warps read shared K and V. Each warp computes its own rows of QK^T and O, eliminating inter-warp shared-memory writes in the forward pass.
Tile sizing introduces a sharp hardware constraint alongside warp partitioning. Larger blocks in {64, 128} x {64, 128} reduce shared-memory traffic, yet pushing block size too high overflows the register file (causing slow register spilling to local memory) or exceeds the 192 KB SRAM limit per SM, preventing the kernel from launching at all. Dao manually tunes among those four block-size combinations per head dimension (d = 64 or 128).
Comparing the optimizations across kernel and training regimes
Switch across the four operating regimes below to see where sequence parallelism, warp partitioning, causal block skipping, and end-to-end linear layers shift the size of FlashAttention-2’s gain.
Select a regime (or use Left and Right arrow keys) to compare the hardware bottleneck, mechanism, and measured TFLOPs/s against PyTorch, FlashAttention, xformers, and Triton.
16k sequence length, batch size 1 (head dim 128, non-causal)
SM occupancyWith only 16 heads and batch 1, head-level parallelism leaves 92 of 108 SMs idle unless work splits along the sequence.
- Why v1 stalls
- Batch times heads equals 16 thread blocks, underutilizing 85% of the A100's 108 streaming multiprocessors; PyTorch goes OOM.
- FlashAttention-2 fix
- Parallelizes over query row blocks (forward) and key/value column blocks (backward) so every SM stays saturated.
- Measured evidence
- Reaches 203 TFLOPs/s combined forward + backward (223 forward, 196 backward) vs. 83 TFLOPs/s for FlashAttention v1 and 95 for Triton.
- Builder takeaway
- Long-context fine-tuning and inference prefills gain more than 2.4x over v1 without changing a single attention weight.
512 sequence length, batch size 32 (head dim 128, non-causal)
Warp & matmulWhen batch 32 times 16 heads already creates 512 thread blocks, SMs are full and intra-block warp communication dominates.
- Why v1 stalls
- Even with high SM occupancy, split-K warps write partial outputs to SRAM and synchronize, while eager rescaling burns 16x slower FP32 ops.
- FlashAttention-2 fix
- Splitting Q across 4 warps removes forward inter-warp barriers, and deferred softmax rescaling raises Tensor Core duty cycle.
- Measured evidence
- Reaches 151 TFLOPs/s combined (209 forward, 136 backward) vs. 76 TFLOPs/s for FlashAttention v1 and 53 for standard PyTorch.
- Builder takeaway
- Even standard short-sequence pretraining runs roughly 2x faster at the attention kernel level.
Autoregressive causal attention (head dim 128, 16k tokens)
Block skippingLanguage models mask out future tokens; block-sparse skipping drops half the tiles and avoids elementwise masking off the diagonal.
- Why naive masking lags
- Evaluating upper-triangular blocks or applying elementwise masks to every tile wastes half the FLOPs and burns non-matmul cycles.
- FlashAttention-2 fix
- Skips all blocks strictly above the diagonal and applies causal masking to only one diagonal block per query row.
- Measured evidence
- Delivers 189 TFLOPs/s combined at 16k (197 forward, 186 backward) vs. 83 TFLOPs/s for FlashAttention v1 and 80 for Triton.
- Builder takeaway
- Block skipping gives causal kernels a 1.7x to 1.8x wall-clock speed boost over full unmasked attention.
GPT3-1.3B and GPT3-2.7B training on 8x A100 80GB SXM
End-to-endAttention is only part of a Transformer layer; end-to-end training speedups scale up as longer context makes attention a larger slice of runtime.
- Why 2k gains stay modest
- At 2k context on GPT3-1.3B, dense projections and optimizer overhead dominate, so FlashAttention-2 hits 196 TFLOPs/s vs. 189 for v1.
- What shifts at 8k
- At 8k context, quadratic attention grows relative to linear projections, letting kernel gains pull total training throughput upward.
- Measured evidence
- GPT3-2.7B at 8k reaches 225 TFLOPs/s per GPU (72% MFU) vs. 175 for FlashAttention v1 (1.3x) and 80 without FlashAttention (2.8x).
- Builder takeaway
- Expect 25% to 30% end-to-end training speedups at 8k context, not 2x, because MLP and projection GEMMs still dominate at shorter lengths.
Source: Tri Dao (2023), Section 4, Figures 4 through 6, and Table 1.
For readers skipping the tabs, here is how FlashAttention-2 performs across all four settings:
- Long context (16k, batch 1): sequence-length block parallelism wakes up idle SMs, lifting combined forward + backward speed from 83 to 203 TFLOPs/s (head dimension 128).
- Short batch (512, batch 32): removing split-K warp synchronization and deferring softmax scaling doubles throughput from 76 to 151 TFLOPs/s even when SMs are already full.
- Causal masking (16k, head dim 128): skipping upper-triangular blocks and masking only diagonal tiles reaches 189 TFLOPs/s combined vs. 83 TFLOPs/s in v1 and 80 in Triton.
- End-to-end GPT-3 training (8x A100): improvements grow with context length, moving from 189 to 196 TFLOPs/s at 2k on 1.3B, and reaching 225 TFLOPs/s (72% MFU) at 8k on 2.7B.
What FlashAttention-2 does not solve
Three technical caveats bound how much speedup a team will see in production.
First, exact attention remains O(N^2 * d) in arithmetic. FlashAttention-2 matches FlashAttention’s O(N) extra memory footprint and pushes compute utilization up to 73% of A100 peak, yet doubling sequence length still quadruples total attention FLOPs. Scaling to 100k or 1M tokens still requires combining IO-aware kernels with architectural sparsity, ring or sequence parallelism, or KV-compression methods.
Second, kernel benchmarks overstate end-to-end gains on short sequences, and MFU conventions matter. At 2k context on GPT3-1.3B, FlashAttention-2 improves full training throughput by under 4% over FlashAttention (196 vs. 189 TFLOPs/s) because feed-forward GEMMs, layer norms, and gradient reductions dominate. Notice also how Dao computes the 72% MFU figure in Section 4.2: following Megatron-LM’s formula (6 * N * params plus 12 * L * hidden_dim * N^2), the paper deliberately does not halve the attention term for causal masking, so teams comparing MFU across papers must check whether causal attention FLOPs were halved.
Third, the Ampere kernel leaves Hopper hardware features untapped and does not address decode bandwidth. Running FlashAttention-2 unmodified on an H100 80GB SXM5 reaches up to 335 TFLOPs/s (338 at 16k non-causal in Figure 7) because it uses neither Hopper’s Tensor Memory Accelerator (TMA) nor 4th-generation Tensor Core warp-group instructions or FP8. Exploiting those units required FlashAttention-3 to reach 740 TFLOPs/s in FP16 and 1.2 PFLOPs/s in FP8. Likewise, during single-token autoregressive decoding, GPU performance is gated by reading the cached keys and values from HBM rather than compute scheduling, which is the memory-capacity bottleneck tackled by PagedAttention.
For engineers building or profiling Transformer stacks today, the lesson from FlashAttention-2 goes beyond attention kernels. Once you eliminate obvious memory round-trips, GPU efficiency hinges on matching hardware asymmetry: avoid 16x slower scalar/vector ops inside Tensor Core loops, expose enough independent thread blocks when batch sizes drop, and partition work across warps so they never have to talk through shared memory when they can finish independent rows alone.


