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.
The top 3
- Key Scalars: Max and Logsumexp for Attention: FlashAttention's single-pass online softmax tracks two crucial scalars per query row: a running maximum (m) for numerical stability and a running logsumexp (l) to accurately normalize probabilities, eliminating the need to store a full N x N attention matrix.
- Drastic Memory Reduction: From Quadratic to Linear: By avoiding the materialization of the N x N attention matrix, FlashAttention's single-pass online softmax reduces memory usage from quadratic (O(N²)) to linear (O(N*d)) with respect to sequence length, drastically cutting High Bandwidth Memory (HBM) traffic by up to 33x for typical configurations (e.g., N=4096, d=128).
- Iterative Accumulation: Softmax Normalization in One Pass: The single-pass algorithm iteratively updates the maximum value and log-sum-exp for each token, allowing the softmax normalization constant to be computed and applied 'on-the-fly' without storing the full, large attention matrix, a key to FlashAttention's efficiency.
Sources
Open the full topic