TL;DR
FlashAttention-4 introduces an algorithm and kernel co-design specifically optimized for NVIDIA's new Blackwell GPU architecture. By addressing "asymmetric hardware scaling"—where matrix math units have doubled in speed but memory and specialized math units have not—it achieves up to a 1.3× speedup over cuDNN and reaches 71% theoretical GPU utilization, all while slashing kernel compilation times by 20-30×.
What Problem Does This Paper Address?
The attention mechanism is the primary computational bottleneck for large language models (LLMs), scaling quadratically with sequence length. While previous iterations like FlashAttention-3 successfully optimized attention for NVIDIA Hopper (H100) GPUs, the hardware landscape has rapidly shifted to the Blackwell architecture (B200/GB200).
Blackwell introduces a unique challenge: asymmetric hardware scaling. While its tensor cores double the raw FP16/BF16 math throughput compared to Hopper, other vital components—such as shared memory bandwidth and the specialized multifunction units (MUFU) that calculate exponential functions—have scaled much more slowly or remained unchanged. As a result, the performance bottleneck has shifted away from matrix multiplication toward memory traffic and softmax operations. Simply porting older attention kernels to Blackwell leaves massive amounts of performance on the table.
Methods
FlashAttention-4 rewrites the playbook for Blackwell by introducing several hardware-aware algorithmic innovations:
-
Redesigned Asynchronous Pipelines: The forward and backward passes are restructured into new software pipelines that exploit Blackwell's fully asynchronous matrix multiply-accumulate (MMA) operations and larger 128×128 tile sizes. This maximizes the overlap between tensor core math, softmax calculations, and memory transfers.
Figure 1: FlashAttention-4 forward pipeline. The superscript H denotes the matrices corresponding to the ”high” Q tile, and superscript L denotes matrices corresponding to the ”low” Q tile. Each Q tile corresponds to 128 query tokens. -
Software-Emulated Exponentials: To bypass the slow hardware MUFU bottleneck, the authors implement a software emulation of the exponential function ($2^x$) using polynomial approximations executed on standard, highly parallel floating-point FMA (Fused Multiply-Add) units.
- Conditional Softmax Rescaling: Online softmax requires frequent rescaling of previous blocks when a new maximum value is found. FlashAttention-4 introduces a "slack" threshold ($\tau$); it skips intermediate rescaling operations if the change in the running maximum is small, significantly reducing non-matmul operations without sacrificing numerical accuracy.
- 2-CTA MMA Mode for the Backward Pass: In the backward pass, the authors leverage Blackwell's cooperative 2-CTA mode, where pairs of thread blocks share data loading responsibilities. This halves the shared memory traffic for certain operands and restructures the gradient computation to cut global atomic reductions in half.
- Python-Embedded DSL: Instead of using traditional, notoriously slow C++ template metaprogramming, the entire kernel is implemented in CuTe-DSL embedded directly within Python, compiling just-in-time (JIT) down to GPU assembly.
Datasets
As a systems and hardware-optimization paper, FlashAttention-4 does not train on standard machine learning datasets (like Wikipedia or ImageNet). Instead, it evaluates performance using synthetic attention workloads designed to benchmark realistic LLM configurations (such as those matching the DeepSeek-V3 architecture). The benchmarks vary sequence lengths from 1k to 32k tokens and test head dimensions of 64, 128, and 192 using BF16 precision.
Key Insights and Ideas
-
The Shifting Bottleneck: Hardware evolution is rarely uniform. When compute capabilities outpace memory and specialized units, software engineers must treat hardware as an unbalanced pipeline and creatively offload work to underutilized units (e.g., using standard FMA math units to calculate exponentials).
Roofline analysis showing that MMA compute and exponential units are primary bottlenecks. -
Partial Emulation is the Sweet Spot: While software-emulated exponentials increase throughput, they also consume registers. The authors discovered that fully emulating every exponential causes register spills; instead, emulating only a tuned subset (10–25%) of the entries per row yields the best performance.
Accuracy of 2^x polynomial emulation compared to hardware MUFU. -
Ditching C++ Templates for Python: High-performance CUDA programming has traditionally been locked behind complex C++ templates. This paper demonstrates that a Python-embedded domain-specific language can achieve maximum hardware expressivity and performance while drastically improving developer velocity.
Key Results
- Compute Performance: On B200 GPUs using BF16, FlashAttention-4 achieves up to a 1.3× speedup over cuDNN 9.13 and up to a 2.7× speedup over Triton.
- Hardware Utilization: The kernel reaches up to 1613 TFLOPs/s, which translates to an impressive 71% of the theoretical maximum utilization of the Blackwell GPU.
- Compilation Speed: Transitioning to CuTe-DSL in Python reduced kernel compilation times by 20–30× compared to FlashAttention-3 (e.g., dropping the forward pass compile time from 55 seconds down to just 2.5 seconds).
Limitations and Future Work
The primary limitation of FlashAttention-4 is its tight coupling with the architectural specifics of NVIDIA's Blackwell generation (such as its unique Tensor Memory layout and 2-CTA cooperative mode). Consequently, these specific kernels are not backward-compatible with Hopper or Ampere GPUs. Additionally, the exact fraction of exponential operations to emulate must still be tuned empirically based on the specific tile sizes used. The authors note that these co-design principles will need to be continuously adapted as future accelerator designs continue to scale compute and memory asymmetrically.
Summary updated