AI State of the Art
SOTA/Advanced Attention Mechanisms/19 min
Section 76

Flash Attention 3 and Hardware Optimizations

Listen as a podcast →

Flash Attention 3: squeezing the most out of the Hopper GPU

Flash Attention 2 showed the way for hardware-aware optimizations. Flash Attention 3 pushes this philosophy even further by exploiting specific features of NVIDIA's Hopper GPU architecture (H100, H200). Where FA2 reached 50-73% of theoretical throughput on A100, FA3 reaches 75% on H100, approaching the physical limits of the hardware.

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

The stakes are clear: on training clusters that cost hundreds of millions, every percentage point of GPU utilization gained is worth millions.

The Hopper's new weapons

Tensor Memory Accelerator (TMA)

The H100 introduces the TMA (Tensor Memory Accelerator), a dedicated hardware unit for memory transfers between HBM (High Bandwidth Memory, the main memory, large but slow) and SRAM (Static RAM, the on-chip memory, small but ultra-fast). Before TMA, these transfers used the same units that do the computations, creating a queue.

With TMA, it's like having an automated conveyor belt bringing ingredients to the kitchen while the chef cooks. The chef no longer needs to go fetch ingredients from the stockroom themselves.

Diagram of a 2D tensor in memory, drawn as a green rectangle labelled 2D Tensor in Mem, with a yellow padding strip on the right. The sides are annotated tensor height and tensor width, the starting address at the bottom left is labelled base addr, and the total width including padding is labelled tensor stride. Inside the green rectangle, a small pink rectangle labelled region to copy is measured by its block height and block width.
What the TMA can do on its own. You describe the geometry of the tensor in memory once (height, width, stride, base address) plus the rectangle to copy, and it fetches that block from HBM without tying up the compute threads. This descriptor is what makes asynchronous loading possible.Source : FlashAttention-3 blog post (Tri Dao et al., Together AI)
Asynchronous pipeline

On the Hopper architecture, memory transfers and computation can be truly simultaneous thanks to TMA. Flash Attention 3 exploits this capability to create a pipeline where data loading, computation, and result writing overlap in time, nearly eliminating memory-related wait times.

Warp Groups and Warp Specialization

Hopper introduces warp groups (groups of 4 warps) and a powerful concept: warp specialization. Instead of all warps doing the same thing (load then compute then write), different warps specialize in different tasks.

Flash Attention 3 exploits this specialization:

  • Producer warps: responsible for loading data from HBM to SRAM via TMA
  • Consumer warps: responsible for computation (matmul on Tensor Cores)

It's the principle of the industrial assembly line: instead of each worker doing all tasks sequentially, each one specializes. The pipeline runs continuously, with no downtime.

Timeline diagram with two rows, warpgroup 1 on top and warpgroup 2 below, along a horizontal time axis. Each row chains blocks labelled GEMM0, softmax, GEMM1, GEMM0, softmax, and so on. The blocks of the two warpgroups are offset: when warpgroup 1 is running a GEMM, warpgroup 2 is running a softmax, and vice versa. Vertical dashed lines mark the synchronization points.
Ping-pong scheduling between two warp groups. The GEMM blocks (General Matrix Multiply, the matrix multiplications) run on the Tensor Cores, the softmax blocks on the regular units. By offsetting the two warp groups, whenever one is occupying the Tensor Cores the other is doing its softmax: both kinds of unit stay busy instead of waiting for each other.Source : FlashAttention-3 paper (figure 3)

FP8 Tensor Cores

The H100 has Tensor Cores capable of FP8 computation (8 bits instead of 16), offering 2x theoretical throughput over FP16/BF16. Using only 8 bits per number is risky in terms of precision, but it doubles compute capacity.

FP8 attention

Flash Attention 3 offers an FP8 mode that performs matrix multiplications QKTQK^T and PVPV in FP8, with FP32 accumulation to preserve numerical precision. Softmax operations remain in FP32 (they're too sensitive to rounding errors). This hybrid approach offers roughly a 1.6x speedup over FP16 (1,200 TFLOP/s versus 740), with minimal quality degradation.

Algorithm architecture

3-stage pipeline

The core of Flash Attention 3 is a 3-stage hardware pipeline running in parallel:

  1. Stage 1 (Producer): the TMA loads the next K/V block from HBM to SRAM
  2. Stage 2 (Consumer): Tensor Cores compute S=QKTS = QK^T and the accumulation O+=PVO += PV
  3. Stage 3 (Producer): the TMA writes partial results to HBM if needed

While stage 2 computes on block ii, stage 1 is already loading block i+1i+1 and stage 3 is writing results from block i−1i-1. Each stage advances independently, like three workers on an assembly line.

Pipelined softmax

The online softmax (invented for Flash Attention 1) is integrated into this pipeline:

  • The statistics mm (maximum) and ℓ\ell (sum of exponentials) are updated incrementally at each block
  • The rescaling correction is amortized and fused with the following matmuls, minimizing non-matmul operations
Timeline diagram with three rows titled figure 2, 2-stage WGMMA-softmax pipelining. The top row, WGMMA0, chains blocks numbered 0, 1, 2 up to N minus 1. The middle row, softmax, chains the same numbers shifted one step to the right. The bottom row, WGMMA1, is shifted one further step. A time axis runs along the bottom.
The overlap inside a single warp group. While the softmax of block 1 runs, the first matrix multiplication of block 2 has already been launched and the second one of block 0 is finishing. The three stages are each shifted by one step, so no unit ever waits for the previous stage's result.Source : FlashAttention-3 paper (figure 2), via Wentao Guo
Overlapping: the key to everything

FA3's performance relies on a simple principle: never let any GPU unit sit idle. While Tensor Cores compute on block ii, the TMA loads block i+1i+1 and writes results from block i−1i-1. This pipeline almost entirely hides memory latency, which is traditionally the attention bottleneck.

Performance results

Benchmarks on H100

The paper's numbers, in TFLOP/s (tera floating-point operations per second):

ConfigurationThroughput (TFLOP/s)% of theoretical
FA2 on H100 (FP16)~370-430~40%
FA3 on H100 (FP16)up to ~740~75%
FA3 on H100 (FP8)close to 1,200~60% (of FP8 theoretical)
Theoretical H100 FP16989100%
Theoretical H100 FP81979100%

On H100 in FP16, FA3 offers a 1.5-2.0x speedup over FA2. In FP8, we approach 1.2 petaflop/s (1,200 TFLOP/s), a number that would have seemed unrealistic a few years ago for a single attention computation.

FP8 numerical precision

The challenges of FP8 for attention

FP8 is not a universal format. It comes in two variants:

  • E4M3: 4 exponent bits, 3 mantissa bits (decent range, modest precision)
  • E5M2: 5 exponent bits, 2 mantissa bits (large range, very low precision)

The specific problem with attention: after softmax, the value distribution is very peaked. Most values are close to 0, a few are close to 1. This uneven distribution is hard to capture in just 8 bits.

Precision considerations

Flash Attention 3's FP8 mode keeps softmax in FP32 and uses FP32 accumulation for matmuls to avoid numerical drift. Only the Q, K, V inputs are in FP8 for multiplications. Sensitive intermediate operations remain in high precision. Experiments show minimal quality degradation for both training and inference.

Per-block quantization

To improve FP8 precision, Flash Attention 3 implements per-block quantization: each data block has its own scale factor. Instead of a single global factor trying to capture the full dynamic range, each block adapts locally. This is more precise and costs almost nothing in overhead.

Complementary optimizations

Optimized causal attention

Flash Attention 3 refines masked block skipping by exploiting warp group granularity. The chunking is finer than in FA2, allowing more unnecessary computations to be skipped in causal masking.

Support for various attention patterns

  • Sliding window: native skipping of out-of-window blocks
  • Block-sparse: custom attention patterns via block masks
  • Cross-attention: optimized for encoder-decoder models

Backward pass on Hopper

The backward pass (gradient computation) benefits from the same optimizations: asynchronous pipeline, warp specialization, and softmax rematerialization integrated into the pipeline. The speedup is similar to the forward pass, which is crucial since the backward is often more expensive.

Hardware perspectives

Post-Hopper GPUs

Future GPU architectures (Blackwell B100/B200) introduce additional capabilities that future versions can exploit:

  • FP4 Tensor Cores: 2x throughput over FP8, even more precision/speed trade-offs
  • Increased HBM bandwidth: the B200's HBM3E reaches about 8 TB/s (versus 3.35 TB/s on H100), reducing the memory bottleneck
  • Improved TMA: extended transfer capabilities for even deeper pipelines
Hardware-algorithm co-design

Flash Attention 3 illustrates a fundamental trend: hardware-algorithm co-design. Algorithms are designed based on specific hardware capabilities, and in turn, hardware evolves to better support critical algorithms. This co-optimization loop is the primary path to continue improving Transformer efficiency.

Key takeaways

Flash Attention 3 pushes the limits of attention efficiency by fully exploiting Hopper GPU capabilities. The asynchronous pipeline eliminates memory wait times, warp specialization maximizes GPU occupancy, and FP8 support doubles compute throughput. Combined, these optimizations allow approaching the physical limits of the hardware, confirming that hardware-aware optimization is the royal road to LLM performance improvement.

Check your understanding0 / 4

1. What does the H100's TMA (Tensor Memory Accelerator) bring, which Flash Attention 3 exploits?

2. What is the warp specialization that Flash Attention 3 exploits?

3. How does Flash Attention 3's FP8 mode sharply increase throughput without sacrificing quality?

4. Why is the softmax output hard to represent in FP8, and what is FA3's countermeasure?