Online Softmax

Definition

Online (streaming) softmax computes a numerically-stable softmax in a single pass over the inputs, maintaining a running max and running denominator and applying a scalar rescale correction whenever a new element raises the max — instead of the standard two/three passes (find max → sum exponentials → normalize). Introduced by Milakov & Gimelshein (arXiv:1805.02867); it is the mathematical heart of FlashAttention, enabling attention to be tiled so the score matrix never touches HBM.

Key math

Safe softmax over scores subtracts the row max for stability:

Both and depend on the whole row — the obstacle to streaming. Maintain, after processing blocks : running max , running denominator (referred to ), and unnormalized output accumulator . Init . For block with local max :


Final: .

Why the correction ? Old terms were referred to ; when the max jumps, each must be re-referenced: . Multiplying the whole accumulator by the scalar does this to every stored term at once. If the max is unchanged, (no rescale). Since , always and no overflow. The invariant makes the result exact (bit-for-bit up to FP associativity), not an approximation.

Why it matters

Because and are just running scalars/vectors, each block’s scores can be discarded immediately after updating state — which is why FlashAttention never writes the matrix to HBM. The same trick unlocks streaming/tiled softmax for very wide rows that exceed SMEM. FA-2 defers the rescale to a single end-of-loop normalization; FA-4 skips rescaling unless the max shift threatens stability (~10× fewer rescale ops).

Taught in

See also