Flash Attention

Volume IV, Chapter 17 — Part I. IO-aware exact attention: SRAM vs. HBM memory hierarchy, tiling strategy, online softmax, and O(N) memory complexity derivation.

Advanced

Table of Contents

  1. Learning Objectives
  2. Prerequisites
  3. Notation
  4. Core Intuition
  5. Memory Hierarchy and the IO Bottleneck
  6. Standard Attention Memory Analysis
  7. Tiling Strategy
  8. Online Softmax
  9. Flash Attention Algorithm
  10. Complexity Analysis
  11. Flash Attention 2 Improvements
  12. Worked Examples
  13. Connection to the Broader Curriculum
  14. Common Pitfalls and Misconceptions
  15. Research Perspective
  16. Summary of Takeaways
  17. Exercises

Learning Objectives

After reading this chapter, you should be able to:

  1. Explain the GPU memory hierarchy (SRAM vs. HBM) and IO bottleneck in attention.
  2. Prove standard attention requires O(N2)O(N^2) HBM accesses for sequence length NN.
  3. Describe tiling that keeps blocks of Q, K, V in SRAM.
  4. Derive online softmax for computing attention without materializing full SRN×N\mathbf{S} \in \mathbb{R}^{N \times N}.
  5. State Flash Attention's O(N2d)O(N^2 d) FLOPs with O(N)O(N) memory.

Prerequisites


Notation

  • N,dN, d — Sequence length and head dimension
  • Q,K,V\mathbf{Q}, \mathbf{K}, \mathbf{V} — Attention factor matrices
  • Br,BcB_r, B_c — Query and key/value tile sizes
  • MSRAMM_{\mathrm{SRAM}} — On-chip SRAM capacity
  • m~,~\tilde{m}, \tilde{\ell} — Online softmax running max and sum

Core Intuition

Self-Attention is O(N2)O(N^2) in memory for the attention matrix S=QKTRN×N\mathbf{S} = QK^T \in \mathbb{R}^{N \times N}. For N=64N = 64K, storing S\mathbf{S} in FP16 requires 8 GB — prohibitive.

Flash Attention (Dao et al., 2022) computes exact attention (not approximation) with O(N)O(N) memory by exploiting GPU memory hierarchy: fast SRAM (on-chip, ~20 MB) vs. slow HBM (off-chip, ~80 GB). The algorithm tiles computation to minimize HBM reads/writes.

Series context. Volume IV, Chapter 17 (Memory Optimization).

Flash Attention Memory Optimization

HBM AccessStandard O(N²) = 64Flash O(N) ≈ 16SRAM tiles, seq=5124× memory reduction — tiles stay in fast SRAM
Seq len
512
Block
64
HBM (full matrix)SRAM tiles
Explore: FlashAttention tiles the attention computation — never materializing the full N×N matrix in HBM. Memory scales O(N) enabling 64K+ context lengths.

Memory Hierarchy and the IO Bottleneck

Definition 1 (Memory Levels).

  • HBM (High Bandwidth Memory): Large (\sim80 GB), high latency, main GPU memory
  • SRAM (Shared Memory): Small (\sim20–100 KB per SM), fast, on-chip

Proposition 1. Matrix operations are often memory-bound, not compute-bound: reading/writing HBM dominates wall-clock time.

Definition 2 (IO Complexity). Count HBM accesses — the relevant cost metric for attention optimization.


Standard Attention Memory Analysis

Standard Self-Attention:

  1. Compute S=QKTRN×N\mathbf{S} = QK^T \in \mathbb{R}^{N \times N} — write N2N^2 to HBM
  2. Softmax on S\mathbf{S} — read/write N2N^2
  3. Compute O=softmax(S)V\mathbf{O} = \text{softmax}(\mathbf{S}) V — read N2N^2, write N×dN \times d

Theorem 1. Naive attention materialization requires O(N2)O(N^2) HBM memory for S\mathbf{S} and O(N2)O(N^2) IO per forward pass.

For long sequences, memory — not FLOPs — is the bottleneck.


Tiling Strategy

Definition 3 (Block Tiling). Partition Q,K,VQ, K, V into blocks of size Br×dB_r \times d and Bc×dB_c \times d that fit in SRAM.

Idea. Compute attention block by block without ever forming full S\mathbf{S}:

For each block of queries QiQ_i, iterate over key/value blocks Kj,VjK_j, V_j, accumulating partial attention output.


Online Softmax

Problem. Softmax requires global max and sum over entire row — seemingly needs full S\mathbf{S} row.

Theorem 2 (Online Softmax). Softmax can be computed incrementally over blocks using running max mm and running sum ll:

Given previous blocks with max moldm_{\text{old}}, sum loldl_{\text{old}}, and output accumulator OoldO_{\text{old}}, for new block with local max mnewm_{\text{new}}, local exp-sum lnewl_{\text{new}}, local contribution OnewO_{\text{new}}:

m=max(mold,mnew),(1)m = \max(m_{\text{old}}, m_{\text{new}}), \tag{1} l=emoldmlold+emnewmlnew,(2)l = e^{m_{\text{old}} - m} l_{\text{old}} + e^{m_{\text{new}} - m} l_{\text{new}}, \tag{2} O=emoldmloldOold+emnewmOnewl.(3)O = \frac{e^{m_{\text{old}} - m} l_{\text{old}} O_{\text{old}} + e^{m_{\text{new}} - m} O_{\text{new}}}{l}. \tag{3}

Proof. Rescale previous partial softmax when discovering new maximum. \blacksquare

This enables exact softmax over tiled blocks without storing full row.


Flash Attention Algorithm

Algorithm (FlashAttention Forward).

Input: Q,K,VRN×dQ, K, V \in \mathbb{R}^{N \times d}, block sizes Br,BcB_r, B_c

For each query block ii:

  1. Load QiQ_i to SRAM
  2. Initialize Oi=0O_i = 0, li=0l_i = 0, mi=m_i = -\infty
  3. For each key/value block jj:
    • Load Kj,VjK_j, V_j to SRAM
    • Compute Sij=QiKjTS_{ij} = Q_i K_j^T (in SRAM)
    • Update mi,li,Oim_i, l_i, O_i via online softmax (1)–(3)
  4. Write OiO_i to HBM

Key property: Sij\mathbf{S}_{ij} blocks never written to HBM — only final O\mathbf{O}.


Complexity Analysis

Theorem 3 (Flash Attention Complexity).

  • FLOPs: O(N2d)O(N^2 d) — same as standard attention (exact computation)
  • HBM Memory: O(N)O(N) — stores only Q,K,V,OQ, K, V, O, not S\mathbf{S}
  • IO: O(N2d2/M)O(N^2 d^2 / M) where MM is SRAM size — sub-quadratic IO when tiled optimally

Corollary 1. Enables sequence lengths impossible with materialized attention — critical for long-context LLMs with KV Cache.


Flash Attention 2 Improvements

FlashAttention-2 (Dao, 2023):

  • Better parallelization across sequence blocks
  • Reduced non-matmul FLOPs
  • ~2× speedup over FlashAttention-1
  • Supports variable sequence lengths, RoPE, GQA

Worked Examples

Example 1: Memory Savings

N=32N = 32K, FP16: naive S\mathbf{S} = 2 GB; Flash Attention: O(Nd)O(Nd) only.

Example 2: Online Softmax

Row [1,5,3][1, 5, 3] processed in blocks [1,5][1, 5] then [3][3] — same result as standard softmax.


Connection to the Broader Curriculum


Common Pitfalls and Misconceptions

Pitfall 1: Flash Attention is approximate (false — exact).

Pitfall 2: Reduces FLOPs (false — reduces memory IO).

Pitfall 3: Backward pass also requires recomputation (trade memory for compute).


Research Perspective

FlashAttention (Dao et al., 2022). FlashAttention-2/3. Ring attention for distributed long context. PagedAttention (vLLM).


Summary of Takeaways

  • MemoryO(N2)O(N^2)O(N)O(N)
  • FLOPsO(N2d)O(N^2 d)O(N2d)O(N^2 d)
  • Exact — Yes — Yes
  • Key idea — Materialize S\mathbf{S} — Tiling + online softmax

Next: Quantization Fundamentals


Exercises

Exercise 1. Compute HBM for S\mathbf{S} at N=64N = 64K, FP16.

Exercise 2. Derive online softmax update (1)–(3).

Exercise 3. Why is attention memory-bound?

Exercise 4. IO analysis for tiled matmul.

Exercise 5. Backward pass recomputation tradeoff.

Exercise 6. Compose with KV Cache.

Exercise 7. GQA reduces which tensor sizes?

Exercise 8. Compare to sparse attention approximations.