How does FlashAttention compute exact attention without materializing the full N by N attention matrix in high-bandwidth memory?

Last updated 22 November 2026

  • Tiling: blocks of Q, K and V are loaded into fast on-chip SRAM, and each score tile is computed, used and discarded, so the N×N matrix never sits in HBM (off-chip GPU memory).

  • Online softmax keeps a running max, normalizer and unnormalized output per row, rescaling them as each new block arrives. The result is exact up to rounding.

  • All steps are fused in one kernel: FLOPs stay O(N²d) but extra memory drops to O(N) and HBM traffic falls sharply, which gives the speedup.

  • https://dev.to/lewis_won/online-softmax-by-hand-4h13
  • https://handbook.modular.com/kernel-optimization/flashattention/
  • https://alexdremov.me/understanding-flash-attention-writing-the-algorithm-from-scratch-in-triton/
All Transformers interview questions