Lab 01 — Flash Attention (Tiled)
Phase: 08 — Inference Optimization at Depth | Difficulty: ⭐⭐⭐⭐⭐ | Time: 4–5 hours
Flash Attention (Dao et al., 2022) reduces attention memory from O(N²) to O(N) using online softmax tiling.
What you build
online_softmax— running max/sum trick for numerically stable softmax without materializing the full N×N matrixflash_attention_forward— tiled attention: process Q, K, V in blocks; update running softmax statistics- Benchmark memory usage: standard O(N²) vs Flash O(N)
- Verify numerical equivalence with PyTorch reference attention
Key concepts
| Concept | What to understand |
|---|---|
| Online softmax | m_new = max(m, row_max); exp_sum update — avoids full row storage |
| SRAM tiling | Load block of K, V to fast SRAM; iterate over Q blocks |
| Memory savings | Standard: Q+K+V+attn_matrix = O(N²); Flash: only O(N) activations |
| Recomputation | Flash recomputes attention during backward to save memory |
Files
| File | Purpose |
|---|---|
lab.py | Stubs with TODO markers |
solution.py | Complete working implementation |
test_lab.py | pytest suite |
requirements.txt | Dependencies |
Run
pip install -r requirements.txt
python lab.py
pytest test_lab.py -v