FlashAttention-2
FlashAttention solved attention's memory problem. FlashAttention-2 is what happened when the same kernel was examined again and found to be reaching only a quarter of what the GPU's matrix units could deliver — the memory traffic was gone, but the work was badly distributed.
Three changes, one goal: keep the matrix units busyThe rewrite attacks overhead rather than the algorithm:
- Fewer non-matmul operations — the rescaling that keeps the online softmax correct is deferred to the end of a block instead of applied at every step. Specialised matrix hardware runs matmuls far faster than ordinary arithmetic, so each non-matmul instruction is expensive in relative terms.
- Parallelism across the sequence length, not only batch and heads — which matters because long-context training runs with small batches.
- Work split differently inside each thread block, so warps share less through shared memory.
Roughly twice the throughput of the original kernel, reaching 50–73% of an A100's theoretical peak, with identical outputs: this is still exact attention, not an approximation. You never call it directly. It is the kernel behind PyTorch's scaled-dot-product attention, the flash_attention_2 option in Hugging Face Transformers, and most serving stacks — so a long-context fine-tune that fits on your hardware today and would not have a generation ago is usually sitting on it.
FlashAttention-2 is a rewrite of the FlashAttention kernel that roughly doubles its speed by cutting non-matrix operations, parallelising across sequence length as well as batch and heads, and dividing work better between warps. The output is identical — attention stays exact. It matters because it lifts long-context training and serving from a memory-bound crawl to near the GPU's arithmetic peak, and it is now the default attention path in most frameworks.
Think of a kitchen that has stopped wasting trips to the pantry but still has three cooks bumping into each other at one station. The ingredients arrive fine; the work just isn't shared out well. FlashAttention-2 is the reorganisation: the same recipe, the same dish, but the jobs divided so nobody stands idle. For the software behind a chat assistant, that means roughly twice the speed when it reads very long texts, at no cost in quality. Nothing about the answer changes — it simply arrives for half the electricity and half the wait.