Back to paper library
arXiv 2024NeurIPS 2024Language & TransformersPaper notes

FlashAttention-3

Each to Its Own Task

Original paper

FlashAttention-3: Fast and Accurate Attention with Asynchrony and Low-precision

Paper authors

Jay Shah · Ganesh Bikshandi · Ying Zhang · Vijay Thakkar · Pradeep Ramani · Tri Dao

Publication
NeurIPS 2024Conference
First submitted to arXiv
arXiv:2407.08608
Cite this paper · BibTeX

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

@misc{transformersflashattention32024,
  title = {{FlashAttention-3: Fast and Accurate Attention with Asynchrony and Low-precision}},
  author = {Jay Shah and Ganesh Bikshandi and Ying Zhang and Vijay Thakkar and Pradeep Ramani and Tri Dao},
  year = {2024},
  eprint = {2407.08608},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2407.08608}
}
On this page

FlashAttention-2 gave every SM on the GPU something to do and pushed attention to around 70% of theoretical peak on the A100. The previous note ended with a question. If everyone looks busy, does that mean the whole production line is efficient?

On the H100, the answer came quickly. FlashAttention-2 uses only 35% of the H100's compute, while a well-tuned matrix multiplication on the same card reaches 80% to 90%.

The H100 (Hopper architecture) has a few things the A100 lacks: dedicated hardware for moving data, Tensor Cores that can compute asynchronously on their own, and FP8 support. FlashAttention-2's algorithm was designed around a synchronous model, "finish one step, then start the next", and uses none of these new features.

This paper, by Jay Shah and Ganesh Bikshandi of Colfax Research, Ying Zhang of Meta, Vijay Thakkar and Pradeep Ramani of NVIDIA, and Tri Dao of the FlashAttention series, redesigns the algorithm specifically for Hopper.

Defining the problem

The exponential costs more than you'd think

The attention forward pass is mostly two matrix multiplications with a softmax in between. First compute S=QK⊤S = QK^\top, apply softmax to each row to get PP, then compute O=PVO = PV.

People usually keep their eyes on the matmuls, and the exponential inside softmax looks like a minor supporting role. A quick calculation on the H100 says otherwise.

The H100 SXM5 does FP16 matrix multiplication at 989 TFLOPS, while the units for special functions such as the exponential manage only 3.9 TFLOPS, a gap of about 256×.

With head dimension d=128d = 128, each element of the score matrix takes 2d2d operations for QK⊤QK^\top and another 2d2d for PVPV, 4d=5124d = 512 matmul operations in all, for a single exponential.

The work differs by 512×, the speed by 256×, so the exponential takes half as long as the matmuls. In FP8, the matmuls get twice as fast while the exponential stays the same, and the two take equally long.

If the two take turns, the Tensor Cores spend nearly a third of their time waiting for the exponentials to finish.

What Hopper can do

The good news is that the exponential and the matmuls use different hardware units, so they could run at the same time all along. The H100 provides the tools to make them do so:

  • TMA (Tensor Memory Accelerator): dedicated hardware for moving data between HBM and shared memory. Once the instruction is issued, you can forget about it; it signals when the transfer is done.
  • WGMMA: a Tensor Core instruction issued per warpgroup (4 warps, 128 threads). It can read operands directly from shared memory, and it is asynchronous, so after issuing it the threads can go do something else first.
  • setmaxnreg: reallocates registers dynamically between warpgroups, giving more to the ones that compute more.
  • FP8 Tensor Core: twice the throughput of FP16.

The hard part is that softmax has to wait for the result of the first matmul, and the second matmul has to wait for the result of softmax. How to run dependent work at the same time is what this paper is about.

Solving the problem

Some load, some compute

The first technique is warp specialization. The warps in a thread block are split into two roles.

The producer warpgroup only issues TMA instructions, moving QQ, KjK_j and VjV_j from HBM into shared memory. Issuing instructions takes only one thread, so it gives up most of its registers.

The consumer warpgroup only computes, doing the matmuls with WGMMA and the softmax itself, and it gets the larger share of registers.

Between them sits a circular buffer with ss stages. The producer notifies the consumer each time it fills a stage, and the consumer hands the stage back when it is done with it. Data movement and computation each go their own way, with no need for both to stop and wait.

Playing ping-pong

Original Figure 1. Ping-pong scheduling of two warpgroups, with time on the horizontal axis. Warpgroup 1 runs GEMM0, Softmax, GEMM1, GEMM0, Softmax… in turn, and Warpgroup 2 is offset by half a beat; in each time slice marked by dashed lines, while one warpgroup is doing a GEMM, the other is doing Softmax. The same color marks the same iteration.
Original Figure 1, Shah et al., from PDF page 6, CC BY 4.0.

With two consumer warpgroups, you can stagger them. A barrier forces warpgroup 1's matmuls (this iteration's PVPV and the next iteration's QK⊤QK^\top) to be scheduled before warpgroup 2's, so while warpgroup 1 does softmax, the Tensor Cores are busy with warpgroup 2's matmuls. In the next time slice the two swap.

The authors admit that the real schedule is not as tidy as the figure, but for the FP16 forward pass with d=128d = 128 and sequence length 8192, speed goes from 570 to 620–640 TFLOPS.

Overlap inside one warpgroup

Original Figure 2. The two-stage pipeline within a single warpgroup, with time on the horizontal axis and three rows, WGMMA0, Softmax and WGMMA1. Iteration 0's WGMMA0 and Softmax run first, one after the other. In each stretch after that, the next iteration's WGMMA0 runs first and then that iteration's Softmax, while WGMMA1 works on the previous iteration; for example, iteration 1's Softmax overlaps iteration 0's WGMMA1. At the end, iteration N−1's WGMMA1 finishes alone.
Original Figure 2, Shah et al., from PDF page 6, CC BY 4.0.

Even within a single warpgroup, work can overlap across iterations. The key is not to line up the three tasks of the same iteration one behind another.

In iteration jj, first issue Snext=QKj⊤S_\text{next} = Q K_j^\top without waiting for it to finish; then issue the previous iteration's O+=P~curVj−1O \mathrel{+}= \tilde{P}_\text{cur} V_{j-1}, again without waiting. Then wait for SnextS_\text{next} to come back and immediately run softmax on it. At this point the Tensor Cores are still computing the previous iteration's PVPV, and the softmax hides underneath it.

The price is registers. To span iterations, an extra copy of SnextS_\text{next} has to be kept, Br×BcB_r \times B_c more floats per thread block. Large tiles are already competing for registers, so one more pipeline stage means a smaller tile, and the trade-off between the two has to be settled by measurement.

In the appendix, the authors also tried a three-stage version, hoping to hide softmax under both the preceding and the following matmul, and it ended up slower than two stages. The compiler overlapped softmax with only one of the matmuls, and registers got even tighter, forcing a smaller tile.

Another headache is the compiler. NVCC reorders instructions on its own and can scramble a carefully arranged order. The authors read the assembly (SASS) to confirm that the overlap really happens.

To see how much each technique contributes, the authors measured non-causal FP16 attention with batch 4, sequence length 8448, 16 heads and d=128d = 128:

SettingTime (ms)TFLOPs/s
FlashAttention-33.538661
No two-stage pipeline4.021582
No warp specialization4.105570

Compiled from Table 2 of the paper.

Remove either technique and it gets more than 12% slower.

The backward pass uses the same division of labor, with one extra role. The dQdQ computed by each thread block must eventually be added into the same dQdQ in HBM, and with many blocks competing to write the same locations, they easily get in each other's way. FlashAttention-3 assigns a separate warp just to write dQdQ back, so the other warps don't wait for it and can move straight on to the next matmul.

FP8: layout and precision

FP8 can make the matmuls twice as fast again, but it brings two headaches.

The first is data layout. FP8 WGMMA only accepts k-major operands, meaning contiguous along the inner-product dimension. For the second matmul, PVPV, the inner-product dimension is the sequence length, but VV is usually contiguous along the head dimension. TMA can't transpose data while moving it, so you have to transpose it yourself.

The authors weighed three options. Transposing as part of the previous step (rotary embedding, for example) is hard to turn into a general-purpose library; running a separate transpose kernel is too wasteful when inference is already bound by memory bandwidth. They settled on transposing inside the kernel, with the producer warpgroup doing it en route using LDSM and STSM instructions. From the second iteration on, the transpose hides within the time of the two WGMMAs.

In addition, the FP32 accumulator that an FP8 matmul produces is laid out in registers differently from the input layout the next matmul expects. The authors rearrange it with byte permute instructions and have the VV transpose write out the matching order, so the two sides line up.

The second headache is precision. FP8 (e4m3) has only 3 mantissa bits, and the activations of large models often contain a few unusually large outliers. The usual approach shares one scaling factor across the whole tensor, so a single outlier squeezes every other value into a tiny range. The authors use two techniques:

  1. Block quantization: split QQ, KK and VV into blocks of the same size as the ones FlashAttention computes on, each with its own scaling factor.
  2. Incoherent processing: before quantization, multiply both QQ and KK by the same random orthogonal matrix MM. Since MM⊤=IMM^\top = I, (QM)(KM)⊤=QK⊤(QM)(KM)^\top = QK^\top and the attention result is unchanged; but every element becomes a random sum of many original elements, which flattens out the outliers. MM is a random ±1 diagonal matrix times a Hadamard matrix, costs O(dlog⁡d)O(d \log d), and can be done together with rotary embedding.

For the test, the authors simulated activations with outliers by adding extra noise with a standard deviation of 10 to 0.1% of the elements, then compared the root-mean-square error against a result computed in FP64:

MethodError (RMSE)
Standard implementation, FP163.2e-4
FlashAttention-2, FP161.9e-4
FlashAttention-3, FP161.9e-4
Standard implementation, FP82.4e-2
FlashAttention-3, FP89.1e-3
No block quantization9.3e-3
No incoherent processing2.4e-2

Compiled from Table 3 of the paper.

The FP8 version's error is 2.6 times lower than the standard approach's. Look closely at the last two rows, though. Removing block quantization makes almost no difference, while removing incoherent processing falls straight back to the standard approach's level. That 2.6× is almost entirely the random orthogonal matrix's doing.

In FP16, FlashAttention-2 and -3 are both more accurate than the standard implementation, because softmax's intermediate results stay in FP32.

Discussion

Speed

Original Figure 5 (c)(d). Attention forward speed (TFLOPs/s) with head dim 128 on an H100 80GB SXM5, without a causal mask on the left and with one on the right, for sequence lengths from 512 to 16k. The five implementations are standard attention (blue), FlashAttention-2 (orange), Triton (green), cuDNN (red) and FlashAttention-3 (purple). On the left, FlashAttention-3 rises from 467 at 512 to 648 at 16k; cuDNN is at 497 and 574 at 512 and 1k, slightly above FlashAttention-3, and is overtaken from 2k on; FlashAttention-2 stays between 309 and 370; standard attention manages only 74 to 139 and runs out of memory at 16k. On the right, FlashAttention-3 rises from 292 to 616, and cuDNN goes from 315 to 539.
Original Figure 5 (c)(d), Shah et al., from PDF page 10, CC BY 4.0.

In FP16, the forward pass is 1.5 to 2.0 times faster than FlashAttention-2, and the backward pass 1.5 to 1.75 times faster. The fastest setting (d=256d = 256, sequence length 16k) reaches 756 TFLOPs/s, about three quarters of the H100's FP16 theoretical peak of 989.

Against NVIDIA's own cuDNN, the two trade wins at sequence lengths around 1k; a little longer, and FlashAttention-3 leads all the way.

The FP8 forward pass gets close to 1.2 PFLOPs/s. Against cuDNN's FP8, though, it leads only at d=64d = 64; at dd of 128 and 256 it ties without a causal mask and falls behind with one. The authors' explanation is that the FP8 version doesn't have a persistent kernel and load balancing yet, while the FP16 version does.

All these speeds are converted with a formula, counting 4⋅N2⋅d⋅heads4 \cdot N^2 \cdot d \cdot \text{heads} operations for the forward pass, half that for causal, and 2.5 times as many for the backward pass.

Limitations and where it stands

The authors list three limitations. LLM inference hasn't been specially optimized yet, FP8 has no persistent kernel yet, and the effect of low-precision attention on large-scale training is still unclear.

The code is in the hopper directory of flash-attention, released under BSD-3-Clause. It has to be installed separately as flash_attn_3 and supports only the H100 and H800. As of November 2026, the official README still carries the heading "FlashAttention-3 beta release" and describes it as a version for testing before it is integrated into the main repository, and FP8 is still forward-only. (Over two years in beta. Call it long-term support)

Not that nobody is looking after it. As of November 2026, the hopper directory is still getting bug fixes.

Conclusion

Attention on the H100 was slow not because anyone was slacking, but because everyone was waiting for whoever came before them. FlashAttention-3 gives the hardware that moves data, multiplies matrices and computes exponentials each its own task, then staggers their work with scheduling.

In the FP8 part, the precision trick is of more practical use than the speed. If you want low-precision attention in your own inference pipeline, multiplying by a random orthogonal matrix costs very little, yet almost all of the error reduction comes from it.

On Blackwell, the Tensor Cores got a lot faster again, while the exponential and memory did not keep up. The next note, [26.03] FlashAttention-4: Fixing the Weakest Link, picks up from there.