Back to paper library
arXiv 2026MLSys 2026Language & TransformersPaper notes

FlashAttention-4

Fixing the Weakest Link

Original paper

FlashAttention-4: Algorithm and Kernel Pipelining Co-Design for Asymmetric Hardware Scaling

Paper authors

Ted Zadouri · Markus Hoehnerbach · Jay Shah · Timmy Liu · Vijay Thakkar · Tri Dao

Publication
MLSys 2026Conference
First submitted to arXiv
arXiv:2603.05451
Cite this paper · BibTeX

The citation identifies the linked source version. It cites the original paper and its authors.

@misc{transformersflashattention42026,
  title = {{FlashAttention-4: Algorithm and Kernel Pipelining Co-Design for Asymmetric Hardware Scaling}},
  author = {Ted Zadouri and Markus Hoehnerbach and Jay Shah and Timmy Liu and Vijay Thakkar and Tri Dao},
  year = {2026},
  eprint = {2603.05451},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2603.05451}
}
On this page

On the H100, FlashAttention-3 staggered matmul and softmax so that the exponential hid underneath the Tensor Core work.

On Blackwell, that trick is no longer enough. The B200's FP16/BF16 Tensor Cores deliver 2.25 PFLOPS, against about 1 PFLOPS on the H100, more than double. But the special function units that compute exponentials can still do only 16 per SM per clock, and shared memory bandwidth didn't double either.

The faster the Tensor Cores run, the more the other units look like the weakest links in the chain. That is what the "asymmetric hardware scaling" in the paper's title is about.

The more immediate problem is that FlashAttention-3 doesn't run on the B200 at all. It uses Hopper-specific matmul instructions that Blackwell doesn't support.

This generation's authors are Ted Zadouri and Tri Dao of Princeton and Together AI, Markus Hoehnerbach of Meta, Jay Shah of Colfax Research, Timmy Liu of NVIDIA, and Vijay Thakkar of Meta and Georgia Tech.

Defining the problem

What Blackwell changed

Blackwell's Tensor Cores differ from Hopper's in three ways:

  • Tensor memory (TMEM): each SM gets an extra 256 KB of memory just for matmul accumulators. The Tensor Cores write their results straight into it, without taking up registers. The register crunch of the Hopper era finally eases here.
  • Larger tiles: one matmul instruction handles a 128 × N block, versus 64 × N on Hopper.
  • Fully asynchronous: once a matmul is issued, the result writes itself into TMEM, and the threads don't have to deal with it at all.

There is also a 2-CTA mode, in which two thread blocks can team up and do a matmul as one larger tile.

How long one tile takes

Using a roofline approach, the authors estimate how many clock cycles one tile needs on each of three hardware resources.

Take M=N=d=128M = N = d = 128, where MM and NN are the lengths of the query and key blocks and dd is the head dimension. The forward pass has two matmuls, QK⊤QK^\top and PVPV, each 2MNd2MNd operations, for 4×1283≈8.44 \times 128^3 \approx 8.4 million in total. The B200's Tensor Cores do 8192 per SM per clock, so that takes 1024 cycles.

The exponential has to be computed once for each of the M×N=16384M \times N = 16384 scores, at 16 per clock, which also comes to 1024 cycles.

Bar chart titled "Clock cycles for one tile on B200". 128 × 128 × 128 tile: Matmul 1024, Shared memory 768, Exponential 1024. 256 × 128 × 128 tile: Matmul 2048, Shared memory 1536, Exponential 2048.
Drawn for this note from the roofline estimates in Section 3.1.1 of the paper.

On the H100, the exponential took about half as long as the matmuls, leaving room to hide it. On the B200 the two take equally long, and shared memory reads take only a little less. However fast the Tensor Cores get, the tile as a whole can't go below 1024 cycles.

The chart sets out the three things to do next: overlap matmul and softmax as much as possible, find a way to raise exponential throughput, and cut non-matmul work that isn't needed.

Solving the problem

Rearranging the pipeline

The forward schedule continues FlashAttention-3's ping-pong. Each thread block computes two output tiles at once, and while one is in a matmul, the other does softmax.

What changes is how finely the work is divided. Blackwell's accumulators live in TMEM, and one tile is 128 × 128. FlashAttention-4 uses two softmax warpgroups of 128 threads each, so exactly one thread handles one full row. Finding the row maximum needs no data exchange between threads; each thread just scans its own 128 numbers.

Also, because PP is handed to the Tensor Cores through TMEM, rescaling the output OO can be split off to a separate correction warpgroup, and the softmax warpgroups don't have to stop for it. Add one warpgroup dedicated to driving the Tensor Cores and TMA, and a thread block holds four warpgroups doing three kinds of work.

With one thread holding a whole row of 128 numbers, registers are tight. BF16 input takes 128 registers, and the output may need another 64. The authors write PP out in two parts, first the leading three quarters, which also starts the corresponding matmul, then the last quarter separately, to ease the register pressure.

Exponentials on the FMA units

If the exponential unit is only so fast, get other units to help. The authors emulate 2x2^x on the floating-point multiply-add (FMA) units and let them work at the same time as the hardware exponential unit (MUFU).

Diagram. At the top is 2 to the power x, which splits into two boxes. Left, "Integer part": 2 to the power ⌊x⌋, "Written directly into the exponent field". Right, "Fractional part f": 1 + p₁f + p₂f² + p₃f³, "Cubic polynomial on FMA units". Below them, "Their product is the answer". At the bottom is "One softmax row" of 16 cells; 3 are orange, meaning they switch to the polynomial, and the other blue cells go to the hardware exponential unit (MUFU). The legend reads "10%–25% use the polynomial".
Drawn for this note from Section 3.1.3 of the paper.

The method splits the exponential into two halves:

2x=2⌊x⌋⋅2x−⌊x⌋2^x = 2^{\lfloor x \rfloor} \cdot 2^{x - \lfloor x \rfloor}

The integer part is cheap. A floating-point number already uses its exponent field to say which power of 2 it holds, so you just shift ⌊x⌋\lfloor x \rfloor and add it into the exponent field, with integer operations only.

The fractional part lies in [0,1)[0, 1) and is approximated by a cubic polynomial, which Horner's method breaks into three multiply-adds. The coefficients come from Sollya, a numerical tool, chosen to minimize the relative error over the whole interval.

Is that accurate enough? In FP32, the cubic polynomial's maximum relative error is 8.8×10−58.8 \times 10^{-5}, about 600 times that of the hardware instruction, which sounds bad. But the softmax output in attention ends up stored in BF16, and BF16's own quantization error is about 3.9×10−33.9 \times 10^{-3}, far larger than the polynomial's. After rounding to BF16, 99% of inputs come within one unit in the last place of the hardware result.

Not everything switches to the polynomial, though. Emulation uses more registers and has longer latency than the hardware instruction, so switching everything over would run out of registers instead. In practice only 10% to 25% of the elements in each row take the polynomial, the rest still go to the hardware, and the ratio is tuned by measurement for each tile configuration.

Fewer rescales

FlashAttention's online softmax works its way through the scores one block at a time. Every time a larger score shows up, the output OO accumulated so far has to be multiplied by emj−1−mje^{m_{j-1} - m_j} to rescale it, which is yet more non-matmul work.

The authors' observation is that this rescaling can be done lazily. As long as you remember which maximum is currently in use and normalize everything at the last step with the true maximum and sum, the intermediate results come out right in the end even if they weren't rescaled along the way. The only worry is numerical overflow, so a rescale actually happens only when the new maximum exceeds the one in use by more than a threshold τ\tau.

τ\tau is usually set to 8. The attention scores are exponentiated in base 2, so a gap of 8 means values at most 28=2562^8 = 256 times larger, which is still quite safe for an FP32 accumulator.

Line chart. The horizontal axis is "K/V blocks processed" (1 to 10); the vertical axis is "Row max (base 2)" (0 to 25). The blue dots ("Max seen in this block") are each block's maximum, roughly 3, 5.5, 9, 10.5, 12.5, 12, 21, 22, 23.5 and 22.5 in order. The orange step line is the "Max actually used"; it starts at 3, jumps to 12.5 only at block 5 and to 21 at block 7, and both places are marked "Rescale". A note below reads "Illustrative numbers. Rescaling at every increase: 7 times; only for gaps over 8: 2 times."
Drawn for this note from the conditional rescaling rule in Section 3.1.4 of the paper; the block values are illustrative.

The chart uses made-up numbers. The maximum keeps growing, and rescaling every time it grows would take 7 rescales. If it rescales only when the gap exceeds 8, it does so just once at block 5 and once at block 7.

In the implementation, if even one thread in a warp needs to rescale, the whole warp rescales together, so threads don't go down different branches.

Backward: shared memory is the bottleneck

The backward pass has five matmuls, computing SS, dPdP, dVdV, dQdQ and dKdK. Estimated the same way, at M=N=d=128M = N = d = 128 shared memory reads take about 30% more time than the matmuls, and this time shared memory is the weakest link.

FlashAttention-3 kept all its accumulators in registers, so the five matmuls had to run more or less one after another. FlashAttention-4 puts more intermediate results in TMEM and overlaps the previous iteration's dQdQ and dKdK matmuls with this iteration's softmax.

Then there is the 2-CTA mode. Two thread blocks combine into one 256-row tile, and each thread block only has to load half of the second operand. That roughly halves this part of the shared memory reads and shrinks the gap to the matmuls to about 5%.

The dQdQ step needs separate handling. It sums along the KV direction, which is exactly where the two thread blocks are split, yet each thread block needs the complete sum. The authors have the two thread blocks each exchange half of dSdS through the cluster's distributed shared memory. As a side benefit, each thread block only writes back half of dQdQ, which also halves the expensive global atomic adds.

Determinism and scheduling

The order of atomic adds isn't fixed, so the same input can give slightly different gradients from run to run. Reinforcement learning training often needs reproducible results, so the authors also provide a deterministic mode that uses a semaphore lock to make every thread block add in a fixed order.

A fixed order easily leaves blocks waiting on each other. In the causal case, the authors dispatch the KV blocks in reverse and walk the query blocks down from the diagonal, so no thread block has to queue for its first dQdQ write. The deterministic backward pass reaches up to 75% of the speed of the non-deterministic 1-CTA version.

The causal mask itself also causes load imbalance, since tiles near the diagonal have less work. The authors apply a classic scheduling rule from 1969, longest processing time first (LPT), and process heads in batches sized to the L2 cache. With BF16 and d=128d = 128, standard multi-head attention gets 4% to 8% faster and MQA 7% to 14% faster. The schedule isn't tied to Blackwell; it was also shown to help on FlashAttention-3.

Written in Python

FlashAttention-4 is written entirely in CuTe-DSL, NVIDIA's GPU programming language embedded in Python. It has the same structure as the C++ version of CUTLASS and can drop down to PTX directly when needed.

The most obvious benefit is compile time. Compiling one kernel takes FlashAttention-3 55 seconds for the forward pass and 45 for the backward; FlashAttention-4 needs only 2.5 and 1.4 seconds, 22 and 32 times faster. The previous two generations had to precompile hundreds of kernels to cover the many attention variants, so the gap matters in practice. People have already built FlexAttention and block-sparse attention versions on top of it.

Discussion

On the B200 with BF16, FlashAttention-4's forward pass is 1.1 to 1.3 times faster than cuDNN 9.13 and 2.1 to 2.7 times faster than Triton, peaking at 1613 TFLOPs/s, about 71% of theoretical peak. From sequence length 4k up, it beats every baseline regardless of head dimension and with or without causal masking; the lead is larger for causal, which the authors credit to the LPT schedule. The backward pass is also consistently faster for long sequences and causal attention.

The setup is similar to the previous two generations, with sequence lengths from 1k to 32k and the batch adjusted to 32k tokens in total; they also added the (192, 128) head dimension combination used in DeepSeek V3.

The cuDNN baseline comes with a caveat. The authors explain in the appendix that starting with cuDNN 9.13 and 9.14, they worked with the cuDNN team to bring some of FlashAttention-4's techniques into cuDNN. The paper's figure caption also says that newer cuDNN versions have taken in many of the paper's techniques and run about as fast as FlashAttention-4.

So that 1.3× was set before the competition had learned all the tricks.

The code is in the flash_attn/cute directory of flash-attention, released under BSD-3-Clause. It installs with pip install flash-attn-4 and supports Hopper and Blackwell. As of November 2026, the version on PyPI is 4.0.0b40, still in beta, and new fixes keep landing in the directory.

Conclusion

The first three generations of FlashAttention all tried to make the Tensor Cores wait less, from moving less data and dividing the work well to hiding softmax under the matmuls. On Blackwell, the matmuls got so fast that the other units can no longer be hidden, and the exponential and shared memory now set the speed limit instead.

Most of this paper's tricks work on those supporting units, using the FMA units to share the exponential load, skipping rescales whenever possible, and letting two thread blocks share one operand. None of them looks impressive, but at this hardware ratio, what they save is exactly the bottleneck.

If hardware keeps going this way, the next GPU generation will probably double the Tensor Cores again. Which link will need fixing then is anyone's guess.