FlashAttention-2: Faster Attention with Better Parallelism and Work Partitioning
TL;DR. FlashAttention already moves the minimum data between HBM and SRAM, yet it reached only 25–40% of the A100’s matmul peak. FlashAttention-2 shows the remaining loss is in how work is split across thread blocks and warps. Three changes roughly double speed and bring attention close to GEMM efficiency.
Key idea
Section titled “Key idea”The IO pattern stays the same; the scheduling changes.
- Fewer non-matmul FLOPs. Matmul throughput can be up to 16× higher than non-matmul throughput. FA2 keeps the output unscaled until the last block and stores only the logsumexp for the backward pass.
- Parallelise over sequence length. FA1 used one thread block per (batch, head). With long sequences the batch is small and SMs sit idle. FA2 also splits the query dimension across thread blocks, with queries in the outer loop.
- Split Q, not K, across warps. The “split-K” scheme forced warps to reduce partial results through shared memory. Splitting queries removes that synchronisation.
Causal masking also gets cheaper: blocks entirely above the diagonal are skipped, about 1.7–1.8× faster than without the mask.
Results
Section titled “Results”FlashAttention-2 is 1.7–3.0× faster than FlashAttention, 1.3–2.5× faster than the Triton version and 3–10× faster than standard PyTorch attention, reaching up to 230 TFLOPs/s (73% of A100 peak) (§4.1). End to end (Table 1), a GPT-3-style 2.7B model at 8K context trains at 225 TFLOPs/s per A100 (72% model FLOPs utilisation), against 175 with FlashAttention and 80 without it. The same code reaches up to 335 TFLOPs/s on H100 without Hopper-specific instructions.
Connections
Section titled “Connections”- Before: FlashAttention for the tiling and online softmax; Making Deep Learning Go Brrrr for why matmul and non-matmul FLOPs cost different amounts.
- Alongside: GQA, which FA2 supports natively, and PagedAttention for the serving side.
- Why it matters for IR: cheaper long-context training is what makes 8K–16K context rerankers and RAG readers trainable on a small cluster. RULER is a reminder that a trainable window is not the same as a usable one.
Questions to think through
These questions have no single right answer. Write or say your answer before you open the hints. Mark a question once you could defend your answer in a reading group.
- Derive / compute
You train with one sequence of 16K tokens and 16 heads of dimension 128 on an A100 (108 SMs). How many thread blocks does FlashAttention-1's batch × heads parallelism launch, and how many does FlashAttention-2 launch if it also splits the sequence into row blocks of 128? Then use the paper's FLOP formula to estimate the causal forward plus backward attention FLOPs per layer.
What a strong answer covers
- FA1: batch × heads = 1 × 16 = 16 thread blocks, so at most 16 of 108 SMs are busy (about 15%). The paper says this scheduling works well only when the count is large, around 80 or more.
- FA2: 16K / 128 = 128 row blocks per head, × 16 heads = 2048 thread blocks, enough to fill all SMs many times over. (The block size of 128 is an assumption for the exercise.)
- Forward FLOPs = 4·N²·d·heads = 4 × 16,384² × 128 × 16 ≈ 2.2 TFLOP; the causal mask halves it to ≈ 1.1 TFLOP.
- Backward = 2.5 × forward (5 matmuls instead of 2, because of recomputation) ≈ 2.75 TFLOP, so about 3.85 TFLOP per layer per sequence, or roughly 17 ms at 230 TFLOPs/s.
- Mechanism
An A100 does 312 TFLOPs/s of fp16 matmul but only 19.5 TFLOPs/s of other FP32 operations. Explain the two changes FlashAttention-2 makes to the online softmax to exploit this, and why it splits work across warps by queries rather than by keys.
What a strong answer covers
- A non-matmul FLOP is about 16× more expensive, so even a small number of rescaling operations shows up in runtime.
- Change 1: keep the running output unscaled and divide by ℓ once at the end, instead of rescaling by diag(ℓ)⁻¹ after every key block.
- Change 2: save only the logsumexp L = m + log ℓ for the backward pass instead of both m and ℓ.
- Split-K (FA1) gives each warp a slice of K and V, so warps must write partial results to shared memory, synchronise and add them. Splitting Q gives each warp its own rows of the output, so no cross-warp reduction is needed in the forward pass.
- The loop order also flips: query blocks become the outer loop, so each thread block owns a set of output rows end to end.
- Critique
FlashAttention-2 reports 50–73% of A100 peak and up to 335 TFLOPs/s on H100 "without special instructions". Critique what these numbers do and do not tell you about the speed of your own long-context RAG fine-tuning job, and design a measurement that would.
What a strong answer covers
- The kernel numbers are attention-only, for specific head dimensions (64, 128) and masks. End-to-end model FLOPs utilisation (72% at best in Table 1) depends on MLPs, communication and data loading too.
- The H100 figure uses no Hopper features (TMA, 4th-gen Tensor Cores). The paper expects 1.5–2× more, which later kernels target, so the H100 number is a lower bound for that hardware generation.
- FLOPs/s are computed with a formula (4·N²·d·heads, ×2.5 for backward), so recomputation FLOPs are not counted as useful work.
- Measurement: profile one training step at your real sequence-length distribution (padded or packed), report tokens per second and peak memory, and break time into attention, MLP and communication.