Explain FlashAttention's core idea. What does it optimise, and what doesn't it change?
Type it the way you would explain it to an interviewer. A few sentences is enough; you can't break anything.
Model answer: the part to lead with
FlashAttention's insight is that standard attention is bottlenecked not by FLOPs but by memory traffic: the naive implementation materialises the n×n score matrix in GPU HBM, writes it, reads it back for softmax, writes the probabilities, reads them again to multiply by V. GPU compute vastly outpaces HBM bandwidth, so the kernel spends its time waiting on memory. FlashAttention is an IO-aware, exact reimplementation - same math, radically less memory movement.
Full worked answer with diagram
FlashAttention's insight is that standard attention is bottlenecked not by FLOPs but by memory traffic: the naive implementation materialises the n×n score matrix in GPU HBM, writes it, reads it back for softmax, writes the probabilities, reads them again to multiply by V. GPU compute vastly outpaces HBM bandwidth, so the kernel spends its time waiting on memory. FlashAttention is an IO-aware, exact reimplementation - same math, radically less memory movement.
Three mechanisms:
- Tiling: split Q, K, V into blocks sized to fit in on-chip SRAM (orders of magnitude faster than HBM). Compute attention block-by-block, keeping intermediate products on-chip, fusing score computation, softmax, and the V-multiply into one kernel.
- Online softmax: softmax normally needs the full row (for the max and the normalizer) before any output. The streaming trick maintains a running maximum and running sum as blocks arrive, rescaling previous partial results when the max updates - mathematically exact, no full row ever needed. This is the piece that makes tiling possible.
- Recomputation in backward: instead of storing the n×n probability matrix for the backward pass, store only the row statistics (max, normalizer) and recompute attention blocks on the fly. Extra FLOPs, far less IO - a favourable trade on modern hardware.
Results: memory footprint for attention drops from O(n²) to O(n), wall-clock speedups of ~2-4× on typical shapes, and long-context training/inference becomes practical. FlashAttention-2/3 refine parallelization and exploit newer hardware (Hopper async execution, fp8), and FlashAttention-4 (2026) redesigns the kernel for Blackwell, where tensor-core throughput has outgrown the softmax and memory units around it.
What it does not change: the FLOP count is still O(n²) - prefill remains quadratic in compute; it's not sparse, linear, or approximate attention. And it doesn't shrink the KV cache - those are orthogonal problems solved by GQA/MLA/quantization. Common interview trap: calling FlashAttention an approximation. It's bit-for-bit the same attention (up to floating-point reordering), which is exactly why adoption was universal - no quality tradeoff to litigate.
Worth sketching. Contrasting the memory traffic in both versions makes it obvious the maths never changed.
flowchart LR
N["naive: write n x n scores to HBM"] --> RD["read back for softmax, write probs,<br/>read again for the V multiply"]
RD --> B["bottleneck is HBM traffic, not FLOPs"]
F["FlashAttention: tile Q, K, V into SRAM"] --> ON["online softmax carries a running<br/>max and sum, rescaling as blocks arrive"]
ON --> FUSE["scores, softmax and V multiply fused,<br/>no n x n matrix ever materialised"]
FUSE --> BW["backward recomputes tiles from row stats"]
BW --> SAME["identical numbers, O(n) memory,<br/>still O(n^2) FLOPs"]
What a good answer covers
- FlashAttention's insight is that standard attention is bottlenecked not by FLOPs but by memory traffic
- GPU compute vastly outpaces HBM bandwidth, so the kernel spends its time waiting on memory
- FlashAttention is an IO-aware, exact reimplementation - same math, radically less memory movement
- Tiling: split Q, K, V into blocks sized to fit in on-chip SRAM (orders of magnitude faster than HBM)
- Online softmax: softmax normally needs the full row (for the max and the normalizer) before any output
Key terms: memory traffic · IO-aware, exact · Tiling · Online softmax · Recomputation in backward · not
In the glossary: FlashAttention · HBM · KV cache · GQA · MLA · Quantization
Interviewer follow-ups to expect
Walk me through the online softmax rescaling. Why is recomputation faster than storing the matrix?
- Tiling: split Q, K, V into blocks sized to fit in on-chip SRAM (orders of magnitude faster than HBM). Compute attention block-by-block, keeping intermediate products on-chip, fusing score computation, softmax, and the V-multiply into one kernel.
- Online softmax: softmax normally needs the full row (for the max and the normalizer) before any output. The streaming trick maintains a running maximum and running sum as blocks arrive, rescaling previous partial results when the max updates - mathematically exact, no full row ever needed. This is the piece that makes tiling possible.
- Recomputation in backward: instead of storing the n×n probability matrix for the backward pass, store only the row statistics (max, normalizer) and recompute attention blocks on the fly. Extra FLOPs, far less IO - a favourable trade on modern hardware.
How does FlashAttention interact with causal masking?
Worked answer from AI-Engineer-Interview-Questions by Om (MIT), reformatted for LLM Prep · answer text last updated 6 November 2026 · scoring and review schedule by LLM Prep