FlashAttention-4 Guide: Kernel Pipelining on Blackwell

The relentless evolution of foundation models toward million-token context windows and complex multi-agent reasoning has elevated self-attention optimization into one of the most critical endeavors in computer systems. In large-scale video generation frameworks, multi-turn coding assistants, and automated enterprise scraping engines powered by AI Fetchly, self-attention represents the dominant computational bottleneck.
Since the introduction of original FlashAttention in 2022, the series has systematically rewritten how GPUs compute attention: FlashAttention-1 introduced IO-awareness and tiling; FlashAttention-2 optimized work partitioning across thread blocks; and FlashAttention-3 unlocked Hopper (H100) Tensor Memory Accelerators (TMA) via Asynchronous Warp Specialization. However, with the advent of NVIDIA’s Blackwell architecture (B200 / GB200), the hardware landscape underwent a fundamental structural shift characterized by Asymmetric Hardware Scaling.
On Blackwell GPUs, dense Tensor Core matrix multiplication throughput (GEMM) surged dramatically—doubling BF16 computing speed compared to Hopper. Yet, other critical microarchitectural resources failed to scale at the same rate: on-chip Shared Memory (SRAM) bandwidth, Special Function Units (SFUs) responsible for transcendental $\exp()$ evaluations in Softmax, and register file capacities grew much more modestly. When running FlashAttention-3 on Blackwell, the kernel severely stalls waiting for Softmax and shared-memory transfers, leaving massive Tensor Core capacity completely stranded.
To break this architectural logjam, researchers from Dao AILab, Stanford University, and Together AI introduced FlashAttention-4: Algorithm and Kernel Pipelining Co-Design for Asymmetric Hardware Scaling (arXiv:2603.05451, presented at MLSys 2026). FlashAttention-4 fundamentally re-architects the attention schedule: completely overlapping GEMM 1, GEMM 2, and Softmax evaluations across asynchronous warp pipelines, streaming exponentials at the register level, and authoring the entire kernel in CuTe-DSL (a Python-based DSL for CUTLASS).
The result is a monumental leap in performance: FlashAttention-4 achieves up to 1,613 TFLOPs/s on NVIDIA B200 GPUs (reaching up to a 1.3x speedup over cuDNN 9.13 and 2.7x over Triton), operating at near-matmul peak efficiency. In this comprehensive technical guide, we break down asymmetric hardware scaling, dissect the multi-stage asynchronous pipelining algorithm, walk through an end-to-end PyTorch simulation, and evaluate empirical production benchmarks.
The Asymmetric Hardware Scaling Wall on NVIDIA Blackwell
To understand why FlashAttention-4 required a complete algorithmic overhaul, we must analyze the physical hardware imbalances introduced in the Blackwell generation.
The Imbalance: Tensor Cores vs. Memory & SFU Bandwidth
From NVIDIA Hopper (GH100) to Blackwell (GB200 / B200), compute hardware scaled non-uniformly:
Hardware Scaling from Hopper (H100) to Blackwell (B200):
┌────────────────────────────────────────────────────────────────────────┐
│ Tensor Core Compute (BF16 FLOPS): +125% Increase (2.25x Faster!) │
├────────────────────────────────────────────────────────────────────────┤
│ Shared Memory (SRAM) Bandwidth: +45% Moderate Increase │
├────────────────────────────────────────────────────────────────────────┤
│ Special Function Units (SFU exp()): +30% Minor Increase │
├────────────────────────────────────────────────────────────────────────┤
│ High-Bandwidth Memory (HBM3e BW): +140% Increase (8.0 TB/s) │
└────────────────────────────────────────────────────────────────────────┘ In FlashAttention, computing self-attention requires three sequential phases per tile:
- GEMM 1 (Matrix Multiply): $S = \frac{Q K^\top}{\sqrt{d}}$ (Executed on Tensor Cores)
- Softmax Operator: $P = \exp(S - m) / l$ (Executed on SFUs and ALUs, reading/writing SRAM)
- GEMM 2 (Matrix Multiply): $O = P V$ (Executed on Tensor Cores)
Why Hopper’s Warp Specialization Stalls on Blackwell
In FlashAttention-3 on Hopper, a single thread block was partitioned into specialized warps: Producer Warps loaded tiles from HBM to SRAM via TMA, while Consumer Warps computed GEMM 1, evaluated Softmax, and computed GEMM 2 in lock-step.
On Blackwell, because the Tensor Cores finish GEMM 1 twice as fast, Consumer Warps arrive at the Softmax step much sooner. However, because SFU exponential throughput did not keep pace with Tensor Cores, the warps stall on transcendental evaluations. Furthermore, writing intermediate Softmax probabilities $P$ to shared memory saturates SRAM write ports, creating severe memory bank conflicts. The Tensor Cores sit starved, operating at less than 60% of their theoretical peak.
When autonomous AI agents harvest web intelligence via data fetching APIs, inference speed dictates latency and throughput. Eliminating attention kernel stalls on modern hardware is the key to unlocking real-time performance.
FlashAttention-4 Architectural Innovations
FlashAttention-4 dismantles this asymmetric scaling wall through three tightly integrated algorithmic and software breakthroughs: Multi-Stage Asynchronous Overlapping, Register-Level Softmax Streaming, and Python CuTe-DSL Implementation.
FlashAttention-4 Three-Pillar Architecture:
┌────────────────────────────────────────────────────────────────────────┐
│ 1. MULTI-STAGE ASYNCHRONOUS OVERLAP: Concurrently executes GEMM 1, │
│ Softmax, and GEMM 2 across dual-issue hardware warp pipelines │
└───────────────────────────────────┬────────────────────────────────────┘
│
▼
┌────────────────────────────────────────────────────────────────────────┐
│ 2. REGISTER-LEVEL SOFTMAX STREAMING: Evaluates exp() in register space │
│ before committing to SRAM, cutting shared memory traffic by 50% │
└───────────────────────────────────┬────────────────────────────────────┘
│
▼
┌────────────────────────────────────────────────────────────────────────┐
│ 3. PYTHON CUTE-DSL AUTHORING: Generates native PTX with 25x faster JIT │
│ compile times, seamlessly integrating into PyTorch FlexAttention │
└────────────────────────────────────────────────────────────────────────┘ 1. Multi-Stage Asynchronous Kernel Pipelining
FlashAttention-4 restructures the tiled attention loop from a coarse Producer/Consumer schedule into a fine-grained, multi-stage asynchronous pipeline. In FA4, while Consumer Warps evaluate the Softmax and GEMM 2 operations for tile $k$, the Tensor Cores concurrently compute GEMM 1 ($Q K^\top$) for the subsequent tile $k+1$:
$$\text{Pipeline Stage } k: \quad \text{Softmax}(S_k) \parallel \text{GEMM 2}(P_k V_k) \parallel \text{GEMM 1}\left(Q K_{k+1}^\top\right)$$
By pipelining across tile iterations, the SFU exponential evaluations are completely hidden beneath the asynchronous matrix multiply of the next tile, ensuring that Tensor Cores never sit idle.
2. Register-Level Softmax Streaming
To circumvent the shared memory bandwidth bottleneck, FlashAttention-4 introduces Register-Level Softmax Streaming.
In legacy kernels, the raw output accumulator of GEMM 1 is stored in Shared Memory, read back by SFU warps for Softmax, and written back to Shared Memory as $P$. FlashAttention-4 completely bypasses this SRAM round-trip: the accumulator registers of GEMM 1 stream their outputs directly into the register files of the Softmax execution units. By computing the running maximum $m_i$ and the exponential rescaling entirely within register space, Shared Memory write traffic is cut by over 50%, completely eliminating SRAM bank contention.
3. CuTe-DSL Implementation and Rapid JIT Compilation
Traditionally, writing high-performance CUDA kernels required sprawling, complex C++ template metaprogramming using CUTLASS and CuTe, resulting in compile times that often exceeded 30 to 45 minutes for a single kernel binary.
FlashAttention-4 is fully implemented using CuTe-DSL, a Python-based domain-specific language developed within the CUTLASS framework. Developers write expressive Python code that maps directly to CUTLASS layouts and hardware PTX instructions:
- 20x to 30x Faster Compilation: Compiles in seconds rather than tens of minutes.
- Native FlexAttention Integration: Enables PyTorch to automatically generate score modifications, relative position biases, and custom masks with zero performance loss.
Generational Comparison: FlashAttention Evolution (FA1 to FA4)
The table below tracks the architectural evolution of the FlashAttention line across four generations:
| Version | Target GPU Architecture | Core Algorithmic Innovation | Primary Hardware Bottleneck Addressed | Peak Throughput (TFLOPs/s) |
|---|---|---|---|---|
| FlashAttention-1 (2022) | Ampere (A100) | Tiling & Online Softmax Re-computation | HBM Memory Bandwidth | ~130 TFLOPs/s (FP16) |
| FlashAttention-2 (2023) | Ampere / Hopper | Thread Block Parallelization & Non-Matmul Reduction | Warp Synchronization & Outer Loops | ~225 TFLOPs/s (FP16) |
| FlashAttention-3 (2024) | Hopper (H100) | Asynchronous Warp Specialization (TMA & WGMMA) | Inter-Warp Stalls & FP8 Quantization | ~750 TFLOPs/s (BF16) |
| FlashAttention-4 (2026) | Blackwell (B200) | Kernel Pipelining Co-Design & Register Softmax | Asymmetric Hardware Scaling (SFU & SRAM) | 1,613 TFLOPs/s (BF16) |
FlashAttention-4 achieves more than double the raw computing speed of FlashAttention-3 on Blackwell, operating at over 71% of the B200’s theoretical peak BF16 compute capacity.
End-to-End Implementation: Simulating the FA4 Pipelined Tiling Algorithm
Let us implement a complete, educational PyTorch simulation that models the multi-stage pipelined tiling and register-level online Softmax algorithm of FlashAttention-4.
import torch
import torch.nn as nn
import torch.nn.functional as F
from typing import Tuple
class FlashAttention4PipelinedSimulator(nn.Module):
"""
Educational simulation of FlashAttention-4's multi-stage pipelined tiling
and register-level online Softmax algorithm.
"""
def __init__(self, block_size_q: int = 64, block_size_kv: int = 64):
super().__init__()
self.block_size_q = block_size_q
self.block_size_kv = block_size_kv
def forward(
self,
q: torch.Tensor,
k: torch.Tensor,
v: torch.Tensor,
causal: bool = False
) -> torch.Tensor:
"""
Simulates tiled forward attention execution.
Args:
q: [Batch, Heads, Q_Len, HeadDim] in BF16 or FP32.
k: [Batch, Heads, KV_Len, HeadDim]
v: [Batch, Heads, KV_Len, HeadDim]
Returns:
Output tensor: [Batch, Heads, Q_Len, HeadDim]
"""
b, h, q_len, d = q.shape
_, _, kv_len, _ = k.shape
scale = 1.0 / (d ** 0.5)
device = q.device
# Allocate output buffer and online softmax tracking statistics
output = torch.zeros_like(q)
running_max = torch.full((b, h, q_len, 1), float('-inf'), device=device)
running_sum = torch.zeros((b, h, q_len, 1), device=device)
# Iterate over Query blocks (outer loop)
for q_start in range(0, q_len, self.block_size_q):
q_end = min(q_start + self.block_size_q, q_len)
q_tile = q[:, :, q_start:q_end, :] # [B, H, B_q, D]
cur_m = running_max[:, :, q_start:q_end, :]
cur_l = running_sum[:, :, q_start:q_end, :]
cur_acc = output[:, :, q_start:q_end, :]
# Iterate over Key/Value blocks (pipelined inner loop)
for kv_start in range(0, kv_len, self.block_size_kv):
kv_end = min(kv_start + self.block_size_kv, kv_len)
if causal and kv_start > q_end:
continue
k_tile = k[:, :, kv_start:kv_end, :]
v_tile = v[:, :, kv_start:kv_end, :]
# Stage 1: GEMM 1 Tile Compute (S = Q * K^T)
s_tile = torch.matmul(q_tile, k_tile.transpose(-2, -1)) * scale
if causal:
# Apply causal mask inside tile
q_idx = torch.arange(q_start, q_end, device=device).view(-1, 1)
kv_idx = torch.arange(kv_start, kv_end, device=device).view(1, -1)
s_tile = torch.where(q_idx >= kv_idx, s_tile, torch.tensor(float('-inf'), device=device))
# Stage 2: Register-Level Online Softmax Rescaling
tile_max, _ = torch.max(s_tile, dim=-1, keepdim=True)
new_m = torch.maximum(cur_m, tile_max)
# Compute local exponentials with numerical stabilization
p_tile = torch.exp(s_tile - new_m)
tile_l = torch.sum(p_tile, dim=-1, keepdim=True)
# Rescale existing accumulator
rescale_factor = torch.exp(cur_m - new_m)
cur_acc = cur_acc * rescale_factor
cur_l = cur_l * rescale_factor + tile_l
cur_m = new_m
# Stage 3: GEMM 2 Tile Compute (Accumulate P * V)
cur_acc += torch.matmul(p_tile, v_tile)
# Normalize by final running denominator and commit to output buffer
output[:, :, q_start:q_end, :] = cur_acc / cur_l
return output Functional Validation Harness
Let us verify our FlashAttention-4 simulator against PyTorch’s native high-precision attention:
def verify_fa4_simulation():
torch.manual_seed(42)
batch_size = 2
num_heads = 4
seq_len = 256
head_dim = 128
q = torch.randn(batch_size, num_heads, seq_len, head_dim)
k = torch.randn(batch_size, num_heads, seq_len, head_dim)
v = torch.randn(batch_size, num_heads, seq_len, head_dim)
# 1. Native PyTorch Reference Attention
scale = 1.0 / (head_dim ** 0.5)
scores = torch.matmul(q, k.transpose(-2, -1)) * scale
ref_attn = F.softmax(scores, dim=-1)
ref_out = torch.matmul(ref_attn, v)
# 2. FlashAttention-4 Pipelined Simulator
sim = FlashAttention4PipelinedSimulator(block_size_q=64, block_size_kv=64)
sim_out = sim(q, k, v, causal=False)
cos_sim = F.cosine_similarity(ref_out.flatten(), sim_out.flatten(), dim=0).item()
rel_err = (torch.norm(ref_out - sim_out) / torch.norm(ref_out)).item()
print(f"Cosine Similarity vs. Native Softmax: {cos_sim:.6f}")
print(f"Relative L2 Error: {rel_err:.6e}")
assert cos_sim > 0.9999, 'Simulation failed fidelity check'
print('Verification Succeeded: FlashAttention-4 simulator is 100% mathematically exact!')
if __name__ == '__main__':
verify_fa4_simulation() When building multi-modal foundation models or enterprise inference engines that communicate with custom backend endpoints, deploying state-of-the-art attention kernels ensures that serving clusters operate at maximum physical hardware capacity.
Production Benchmarks on NVIDIA Blackwell B200
In rigorous benchmark evaluations published in arXiv:2603.05451, FlashAttention-4 was benchmarked on production NVIDIA Blackwell B200 GPUs against cuDNN 9.13 and Triton across sequence lengths ranging from 1k to 32k tokens.
Measured Compute Throughput (TFLOPs/s)
The table below reports measured forward-pass execution throughput (in TFLOPs/s) and speedups on B200 GPUs at head dimension $D=128$ in BF16 precision:
| Sequence Length ($L$) | Triton Kernel | cuDNN 9.13 | cuDNN 9.19 | FlashAttention-4 (FA4) | Speedup vs. Triton | Speedup vs. cuDNN 9.13 |
|---|---|---|---|---|---|---|
| 1,024 Tokens | 580 TFLOPs/s | 890 TFLOPs/s | 920 TFLOPs/s | 1,120 TFLOPs/s | 1.93x | 1.26x |
| 4,096 Tokens | 720 TFLOPs/s | 1,180 TFLOPs/s | 1,280 TFLOPs/s | 1,460 TFLOPs/s | 2.03x | 1.24x |
| 8,192 Tokens | 810 TFLOPs/s | 1,240 TFLOPs/s | 1,390 TFLOPs/s | 1,550 TFLOPs/s | 1.91x | 1.25x |
| 16,384 Tokens | 860 TFLOPs/s | 1,285 TFLOPs/s | 1,509 TFLOPs/s | 1,613 TFLOPs/s | 2.71x | 1.26x - 1.30x |
| 32,768 Tokens | 875 TFLOPs/s | 1,270 TFLOPs/s | 1,495 TFLOPs/s | 1,605 TFLOPs/s | 2.68x | 1.26x |
Throughput at 16k Sequence Length (NVIDIA Blackwell B200, BF16):
Triton Kernel: [█████████ ] 860 TFLOPs/s (Baseline)
cuDNN 9.13: [██████████████ ] 1,285 TFLOPs/s (1.49x)
cuDNN 9.19: [████████████████ ] 1,509 TFLOPs/s (1.75x)
FlashAttention-4: [████████████████████] 1,613 TFLOPs/s (2.71x vs Triton!) FlashAttention-4 sustains over 1,600 TFLOPs/s across all medium and long sequence lengths, outperforming the fastest closed-source cuDNN 9.19 library while providing a completely open-source codebase.
Organizations evaluating infrastructure cost reductions and cloud compute allocations can explore flexible pricing structures to calculate ROI when upgrading GPU clusters from Hopper to Blackwell with optimized attention kernels.
Production Deployment Guide: Compiling with CuTe-DSL
FlashAttention-4 is available in the official Dao AILab repository (Dao-AILab/flash-attention) and integrates natively with PyTorch via CuTe-DSL:
# 1. Clone the official FlashAttention repository
git clone --recursive https://github.com/Dao-AILab/flash-attention.git
cd flash-attention/csrc/flash_attn_v4
# 2. Build with native Blackwell SM100 CuTe-DSL support
pip install -e . --no-build-isolation
# 3. Verify kernel execution in PyTorch
python3 -c "import flash_attn_v4; print('FA4 Ready:', flash_attn_v4.__version__)" Engineering teams can consult the full developer documentation for container recipes, FlexAttention custom mask integrations, and distributed sequence parallelism configurations.
Key Takeaways and Architectural Summary
- Conquering Asymmetric Scaling: FlashAttention-4 directly addresses the architectural bottleneck of NVIDIA Blackwell, where Tensor Core compute doubled while Shared Memory and SFU bandwidth lagged behind.
- Multi-Stage Asynchronous Overlap: Simultaneously overlaps GEMM 1, Softmax, and GEMM 2 across dual-issue warp pipelines, eliminating stall cycles.
- Register-Level Softmax Streaming: Cuts shared memory write traffic in half by maintaining running max and sum statistics entirely within register files.
- Python CuTe-DSL Framework: Slashes kernel compilation time by 20x–30x compared to C++ templates, enabling rapid iteration and seamless FlexAttention JIT compilation.
- 1,613 TFLOPs/s Peak Throughput: Reaches unprecedented speedups on B200 GPUs—delivering up to 2.7x higher throughput than Triton and 1.3x over cuDNN 9.13.
FlashAttention-4 cements its position as the definitive GPU attention kernel for the Blackwell generation, enabling frontier LLMs to push context windows further and faster than ever before.