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.
Prerequisites
Table of Contents
- Learning Objectives
- Prerequisites
- Notation
- Core Intuition
- Memory Hierarchy and the IO Bottleneck
- Standard Attention Memory Analysis
- Tiling Strategy
- Online Softmax
- Flash Attention Algorithm
- Complexity Analysis
- Flash Attention 2 Improvements
- Worked Examples
- Connection to the Broader Curriculum
- Common Pitfalls and Misconceptions
- Research Perspective
- Summary of Takeaways
- Exercises
Learning Objectives
After reading this chapter, you should be able to:
- Explain the GPU memory hierarchy (SRAM vs. HBM) and IO bottleneck in attention.
- Prove standard attention requires HBM accesses for sequence length .
- Describe tiling that keeps blocks of Q, K, V in SRAM.
- Derive online softmax for computing attention without materializing full .
- State Flash Attention's FLOPs with memory.
Prerequisites
- Self-Attention —
- KV Cache — inference context
Notation
- — Sequence length and head dimension
- — Attention factor matrices
- — Query and key/value tile sizes
- — On-chip SRAM capacity
- — Online softmax running max and sum
Core Intuition
Self-Attention is in memory for the attention matrix . For K, storing in FP16 requires 8 GB — prohibitive.
Flash Attention (Dao et al., 2022) computes exact attention (not approximation) with 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
Memory Hierarchy and the IO Bottleneck
Definition 1 (Memory Levels).
- HBM (High Bandwidth Memory): Large (80 GB), high latency, main GPU memory
- SRAM (Shared Memory): Small (20–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:
- Compute — write to HBM
- Softmax on — read/write
- Compute — read , write
Theorem 1. Naive attention materialization requires HBM memory for and IO per forward pass.
For long sequences, memory — not FLOPs — is the bottleneck.
Tiling Strategy
Definition 3 (Block Tiling). Partition into blocks of size and that fit in SRAM.
Idea. Compute attention block by block without ever forming full :
For each block of queries , iterate over key/value blocks , accumulating partial attention output.
Online Softmax
Problem. Softmax requires global max and sum over entire row — seemingly needs full row.
Theorem 2 (Online Softmax). Softmax can be computed incrementally over blocks using running max and running sum :
Given previous blocks with max , sum , and output accumulator , for new block with local max , local exp-sum , local contribution :
Proof. Rescale previous partial softmax when discovering new maximum.
This enables exact softmax over tiled blocks without storing full row.
Flash Attention Algorithm
Algorithm (FlashAttention Forward).
Input: , block sizes
For each query block :
- Load to SRAM
- Initialize , ,
- For each key/value block :
- Load to SRAM
- Compute (in SRAM)
- Update via online softmax (1)–(3)
- Write to HBM
Key property: blocks never written to HBM — only final .
Complexity Analysis
Theorem 3 (Flash Attention Complexity).
- FLOPs: — same as standard attention (exact computation)
- HBM Memory: — stores only , not
- IO: where 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
K, FP16: naive = 2 GB; Flash Attention: only.
Example 2: Online Softmax
Row processed in blocks then — same result as standard softmax.
Connection to the Broader Curriculum
- Self-Attention — exact algorithm preserved
- KV Cache — composable
- Data Parallelism — orthogonal axis
- Quantization — complementary optimization
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
- Memory — —
- FLOPs — —
- Exact — Yes — Yes
- Key idea — Materialize — Tiling + online softmax
Next: Quantization Fundamentals
Exercises
Exercise 1. Compute HBM for at K, 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.