How does FlashAttention handle the backward pass without storing the full attention matrix?
Last updated
The forward pass saves only Q, K, V, the output O, a per-row logsumexp statistic (O(N) memory), and the dropout RNG seed.
The backward pass recomputes each attention tile in SRAM as
P_ij = exp(Q_i K_j^T - L_i)and accumulates dQ, dK and dV block by block.Recomputation adds FLOPs but avoids reading an N×N matrix from HBM, so it is still faster. It is effectively gradient checkpointing inside one fused kernel.