Online Softmax for Reduced Memory Footprint
Instead of storing the entire N×N attention matrix, FlashAttention employs an 'online softmax' mechanism, recomputing attention values during the backward pass and maintaining running statistics in fast registers, which reduces memory usage from quadratic to linear with sequence length.
The top 3
- Online Softmax's Drastic Memory Footprint Reduction: FlashAttention, leveraging online softmax, reduces attention mechanism's memory complexity from O(N²) to O(N) and can decrease High-Bandwidth Memory (HBM) traffic by approximately 33x for typical configurations (e.g., N=4096, d=128), making long-context LLMs practical.
- Single-Pass Algorithm: Tracking Key Scalars: Online softmax achieves its efficiency by reformulating the algorithm to track and incrementally update only two scalar values—a running maximum (`m`) and a running denominator (`d`)—during a single pass over the input data, thereby avoiding the storage of large intermediate `exp(x)` buffers.
- Up to 9x Speedup & GPU Bandwidth Optimization: By reducing global memory traffic from ~3N to ~2N operations and enabling kernel fusion, online softmax contributes to 2-3x faster execution for softmax functions and allows FlashAttention to achieve 6-9x speedups in LLM inference on GPUs, particularly benefiting bandwidth-bound operations.
Sources
Open the full topic