5.2 Kernels and FlashAttention
1. Learn
Section titled “1. Learn”- Stanford CS336: Language Modeling from Scratchlecture 6 "Kernels, Triton" (April 17) and the assignment 2 (systems) handout
- Dao et al., FlashAttentionsections 2–3 and algorithm 1
2. Bridge
Section titled “2. Bridge”The problem. Written as three separate GPU kernels, attention writes the N × N score matrix to GPU memory (HBM), reads it back for the softmax, writes the probabilities and reads them again for the product with V. For long sequences those 4N² numbers dominate, and the operation is memory-bound (5.1). The fix is not fewer FLOPs but fewer bytes moved (Dao et al. 2022, §2).
Tiling and the online softmax. A GPU has a little very fast on-chip memory (SRAM). FlashAttention loads a tile of queries and a tile of keys and values into it and computes that tile’s scores. The difficulty is the softmax, which needs the whole row. The way out is to keep, for every query row, the running maximum m and the running sum l of exp(score − m). When a new tile raises the maximum, the sum and the partial output are rescaled by exp(m_old − m_new) (Milakov & Gimelshein 2018). The result is exact attention, and the N × N matrix never reaches HBM.
The backward pass needs the probabilities again. Rather than storing them, FlashAttention saves only the logsumexp L of every row and recomputes P = exp(S − L) tile by tile, trading cheap FLOPs for expensive bytes. FlashAttention-2 puts the query tiles in the outer loop, so that each one can run in parallel and write its output once (Dao 2023, §3).
Triton is a Python language for GPU kernels in which a program computes one block of the output and the compiler handles the threads within it. It is how you will write the kernel, as in CS336 Assignment 2 (Tillet et al. 2019).
3. Explore
Section titled “3. Explore”First, the algorithm in NumPy, in the browser or locally: the online softmax, then the tiled forward and backward passes, checked against plain attention and finite differences.
stubs/m5_2_flash.pyLoading starter file…
Prefer your own editor? Edit exercises/stubs/m5_2_flash.py and run mlc check 5.2. The tests are identical; import the results on the progress page.
Then write it in Triton. Open notebooks/5.2-triton-flashattention.ipynb on Kaggle with a T4 GPU. Triton officially supports only GPUs of compute capability 8.0 and newer; kernels like these generally run on the T4 (7.5) anyway, but not on the P100. Fill in a fused row-softmax kernel and the inner loop of a FlashAttention-2 forward kernel, a line-by-line translation of your NumPy code. The notebook wraps it in an autograd function, checks outputs and gradients against plain attention with and without the causal mask, and times it against plain attention and PyTorch’s scaled_dot_product_attention up to 8,192 tokens. Without a GPU, Triton’s interpreter runs the same kernels on the CPU at small sizes, so you can debug on a laptop.
5. Check
Section titled “5. Check”- not yet: All tests pass (0/?)
- not yet: Checkpoint quiz ≥ 80%
Learner log: time spent and points of confusion
Every confusion you note becomes a fix before the module is marked stable. Export it from the progress page and attach it to an issue.