How does FlashAttention handle the backward pass without storing the full attention matrix?

Last updated 22 November 2026

  • 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.

  • https://www.profrod.ai/notes/flashattention-computes-exact-attention-without-storing-the-matrix
  • https://arxiv.org/html/2205.14135v2
All Transformers interview questions