Pidoku

FlashAttention-2, -3 and -4 (2023–2026)

Module 30 Lesson 30.n 45 min Difficulty 4/5

Prerequisites 30.f, 4.c, 4.f, 13.l


Before you start: words you need#

WordPlain meaning
Non-matmul FLOPsWork that does not run on tensor cores: the exponentials, maxima, sums and rescaling in softmax. On an A100 each one costs about 16× a matmul FLOP.
OccupancyHow much of the GPU has work at once. Few thread blocks on a 100+-SM GPU means idle SMs (6.c).
Warp / warpgroupA warp is 32 threads that run together; a warpgroup is 4 warps, the unit Hopper’s tensor-core instruction (WGMMA) works on (4.c).
Warp specialisationGiving different warps different jobs, for example one group only loads data while others only compute (25.b).
TMATensor Memory Accelerator: Hopper/Blackwell hardware that copies tiles between global and shared memory on its own, asynchronously (25.a).
Asynchronous MMAA matrix-multiply instruction that starts and lets the thread do other work until it completes.
TMEMBlackwell’s tensor memory: a per-SM store next to the tensor cores that holds MMA results (25.a).
Incoherent processingMultiplying Q and K by a random orthogonal (Hadamard) matrix so outliers are spread out before low-precision rounding. The product Q·Kᵀ is unchanged.

1. The paper#

Three papers, each adapting FlashAttention (30.f) to a new GPU generation:

  • FlashAttention-2 — Tri Dao, “FlashAttention-2: Faster Attention with Better Parallelism and Work Partitioning”, ICLR 2024 (arXiv July 2023). arXiv:2307.08691 — Ampere (A100).
  • FlashAttention-3 — Jay Shah, Ganesh Bikshandi, Ying Zhang, Vijay Thakkar, Pradeep Ramani, Tri Dao, “FlashAttention-3: Fast and Accurate Attention with Asynchrony and Low-precision”, NeurIPS 2024 (arXiv July 2024). arXiv:2407.08608 — Hopper (H100).
  • FlashAttention-4 — Ted Zadouri, Markus Hoehnerbach, Jay Shah, Timmy Liu, Vijay Thakkar, Tri Dao, “FlashAttention-4: Algorithm and Kernel Pipelining Co-Design for Asymmetric Hardware Scaling”, MLSys 2026 (arXiv March 2026). arXiv:2603.05451 — Blackwell (B200).

Read together, they are a three-chapter lesson in how the bottleneck moves when hardware changes.


2. The problem#

FlashAttention-1 fixed memory traffic, but left the GPU underused, and each new GPU made a different part of the kernel the limit:

GenerationWhat limited the previous kernel
A100 (FA-2)FA-1 reached only 25–40% of peak FLOP/s: too many non-matmul FLOPs, too little parallelism for long sequences with small batches, and wasteful splitting of work between warps.
H100 (FA-3)FA-2 reached only about 35% of H100 peak. It did not use Hopper’s asynchronous tensor cores (WGMMA) and TMA, and had no FP8 path.
B200 (FA-4)Tensor-core throughput doubled again, but shared-memory bandwidth and the exponential unit (16 ops/clock/SM) did not. Softmax and shared-memory traffic became the bottleneck — “asymmetric hardware scaling”.

3. The idea#

FA-2: do less non-matmul work, and spread the work better.

  • Keep the output unscaled inside the loop and divide by the softmax sum once at the end, instead of rescaling at every block.
  • Parallelise over the sequence length as well as batch and heads, so long sequences with small batches still fill the GPU.
  • Swap the loops: outer loop over Q blocks, inner over K/V blocks, so each thread block owns its output rows and warps do not need to share partial results through shared memory.

FA-3: overlap everything, and add FP8.

  • Warp specialisation: producer warps issue TMA loads; consumer warpgroups issue WGMMA. They synchronise through barriers, so loading and computing overlap.
  • Ping-pong scheduling: two consumer warpgroups alternate, one doing softmax while the other does its matmuls, so the slow exponentials hide behind tensor-core work. Within one warpgroup, softmax of one block also overlaps with the matmul of the next.
  • FP8 with block quantization and incoherent processing (Hadamard rotation of Q and K) to keep accuracy while doubling tensor-core throughput.

FA-4: attack the units that did not scale.

  • Fully asynchronous pipelines around Blackwell’s tcgen05 MMA with results in TMEM, larger tiles, and a separate “correction” warpgroup that rescales outputs off the critical path.
  • Software exponentials: compute some of the exp2 calls with a polynomial on the ordinary FMA units instead of the special-function unit, raising exponential throughput.
  • Conditional rescaling: only rescale the running output when the running max has grown by more than a threshold (log2 256 = 8), which skips most rescales without changing the final result.
  • 2-CTA MMA and TMEM in the backward pass to cut shared-memory traffic and atomic adds.
  • Written entirely in CuTe DSL (Python), compiling 20–30× faster than C++ templates.
flowchart LR
  F1["FA-1 (2022): cut HBM traffic"] --> F2["FA-2 (2023, A100): fewer non-matmul FLOPs, sequence parallelism"]
  F2 --> F3["FA-3 (2024, H100): warp specialisation, TMA + WGMMA overlap, FP8"]
  F3 --> F4["FA-4 (2026, B200): async tcgen05 + TMEM, software exp, conditional rescale"]
  class F1,F2,F3,F4 compute

4. How it works#

The algorithm is the same tiling and online softmax as FA-1 (30.f). What changes is who does what, when, inside the kernel. Taught in detail in 13.l (concepts) and 25.f (kernel anatomy). Three patterns recur and are worth recognising in any modern kernel:

  1. Count every unit, not just FLOPs. FA-4’s analysis for one tile (M = N = d = 128) on B200 puts MMA time and exponential time at about the same number of cycles. Doubling tensor cores without doubling the exponential unit leaves softmax as the limit. The roofline (30.a) has to be drawn for each unit.
  2. Overlap is the main lever. FA-3 and FA-4 run loads, matmuls, softmax and rescaling in separate warps or warpgroups at the same time. The kernel’s speed is set by the slowest stage, not the sum of stages.
  3. Exact algebra still wins. Conditional rescaling, deferred normalisation and Hadamard rotations all change the order of work without changing the mathematical result.

For inference, the most-used variants are the varlen (variable-length, packed batch) forward kernels and paged-KV support, which let engines run a whole continuous batch (30.g) and a paged cache (30.o) in one launch, with split-KV for decode.


5. Results#

PaperGPUHeadline result (as stated in the paper)
FA-2A100About 2× FA-1; 50–73% of peak FLOP/s in the forward pass; up to 225 TFLOP/s per A100 (72% MFU) training GPT-style models end to end.
FA-3H1001.5–2.0× over FA-2 in FP16/BF16, up to 740 TFLOP/s (75% of peak); FP8 close to 1.2 PFLOP/s; FP8 with incoherent processing has 2.6× lower numerical error than baseline per-tensor FP8 attention.
FA-4B200BF16 up to 1.3× faster than cuDNN 9.13 and 2.7× faster than Triton; up to 1,613 TFLOP/s (71% utilisation); 20–30× faster compiles than C++ templates.

Note the comparison points. FA-4 is measured against cuDNN, which had by then adopted many of the earlier FlashAttention ideas — a much stronger baseline than FA-1’s PyTorch. A 1.3× win over a vendor library is a large result.


6. What held up and what did not#

Held up

  • Each generation’s ideas became standard. Loop order and sequence parallelism (FA-2), warp-specialised producer/consumer pipelines and ping-pong (FA-3), and pipelines built around asynchronous MMA and TMEM (FA-4) are now the templates for GEMMs, MoE kernels and MLA kernels too (25.b, 25.e, 25.h).
  • Python kernel DSLs for frontier kernels. FA-4 showed a top-performing kernel can be written in CuTe DSL; NVIDIA, Triton (Gluon) and others have moved the same way (25.d).
  • Co-design with hardware vendors. FA-3 and FA-4 are joint work with NVIDIA and Colfax; cuDNN and FlashAttention now leapfrog each other.

Did not, or changed

  • FP8 attention is used less than FP8 GEMMs. Accuracy concerns keep most production attention in BF16, with FP8 mainly for the KV cache (13.h) and some prefill paths.
  • Hardware-specific code multiplies. FA-3 runs only on Hopper, FA-4 targets Blackwell data-centre GPUs (SM100); older GPUs keep FA-2. Engines must pick per GPU (18.c).
  • Decode is often served by other kernels. For decode, FlashInfer (30.x), TensorRT-LLM’s XQA and trtllm-gen kernels, and FlashMLA for MLA models often lead; FlashAttention’s strength is prefill and training.

7. Where it lives in 2026#

  • Taught in depth: FlashAttention in 13.l, attention kernels on Hopper and Blackwell in 25.f, vLLM’s attention backends in 18.c.
  • The flash-attn package ships FA-2 and FA-3; FA-4 lives in its flash_attn/cute CuTe DSL code. vLLM v0.32.0’s FLASH_ATTN backend picks version 2, 3 or 4 per GPU, and defaults to FlashInfer on Blackwell data-centre GPUs.
  • FA-4 numbers quoted in this material: about 1.6 PFLOP/s BF16 on B200 (7.c, 13.l).

Checked on 13 November 2026.


8. Questions#

  1. FA-2 swapped FA-1’s loop order. What did that remove?
  2. Why is a non-matmul FLOP so much more expensive than a matmul FLOP on an A100 or H100?
  3. Explain warp specialisation and ping-pong scheduling in FA-3. What do they overlap?
  4. What is “asymmetric hardware scaling”, and which units became FA-4’s bottleneck?
  5. How can computing exp2 with a polynomial on FMA units make a kernel faster?
  6. Why is skipping a rescale when the max grows only a little still exact?
  7. FA-4 beats cuDNN by 1.3× but FA-1 beat PyTorch by 7.6×. Which is the more impressive result, and why?

9. Further reading#

↑↓ navigate↵ openesc close

100% drag to pan · scroll or pinch to zoom · double-click to zoom in