Home Knowledge Base FlashAttention-2

FlashAttention-2 is an optimized implementation of the attention mechanism that achieves 2× the speed of the original FlashAttention — reaching up to 230 TFLOPS/s on NVIDIA A100 GPUs (73% of theoretical peak), through better work partitioning across GPU thread blocks, improved parallelism along the sequence length dimension, and elimination of redundant floating-point operations in the online softmax computation, making it the standard attention implementation for all production LLM training and inference.

What Is FlashAttention-2?

FlashAttention-2 Improvements Over v1

Optimizationv1 Problemv2 SolutionSpeedup
Non-matmul FLOPsRescaling operations during online softmaxEliminate rescaling by restructuring the algorithm~15%
Sequence ParallelismParallelized only across batch and headsAlso parallelize across sequence length dimension~50% on long sequences
Warp PartitioningSuboptimal work distribution between warpsBetter partition between thread warps, reducing shared memory reads/writes~20%
Causal MaskingApplied mask to all tilesSkip computation for fully masked tiles~2× for causal (autoregressive)

Performance Comparison

ImplementationTFLOPS/s (A100)% of PeakMemoryExact?
Standard PyTorch~3010%O(N²)Yes
FlashAttention v1~12039%O(N)Yes
FlashAttention-2~23073%O(N)Yes
FlashAttention-3~300+ (H100)75%+O(N)Yes
Theoretical Peak312 (A100 BF16)100%

How FlashAttention-2 Works (Tiled Algorithm)

StepActionMemory Level
1. Load Q tile from HBM to SRAMLoad Q block (Br × d)HBM → SRAM
2. Load K, V tiles sequentiallyLoad K, V blocks (Bc × d)HBM → SRAM
3. Compute S = Q × K^T (tile)Matrix multiply in SRAMSRAM only
4. Online softmax (no rescaling in v2)Compute softmax incrementallySRAM only
5. Compute O = softmax(S) × VAccumulate output tileSRAM only
6. Write output tile to HBMStore final resultSRAM → HBM
7. Repeat for all K, V tilesIterate through sequenceOverlapped loads

Adoption

FrameworkIntegration Status
PyTorch 2.0+Built-in via torch.nn.functional.scaled_dot_product_attention
Hugging Face TransformersDefault for supported models (attn_implementation="flash_attention_2")
vLLMDefault attention backend for LLM serving
DeepSpeedIntegrated for training

FlashAttention-2 is the standard attention implementation for modern LLMs — delivering exact attention computation at 73% of GPU peak throughput through IO-aware tiling, optimized warp scheduling, and sequence-length parallelism, enabling 2-4× faster training and longer context lengths without any approximation or quality loss compared to standard attention.

flashattention-2optimization

Explore 500+ Semiconductor & AI Topics

From EUV lithography to CUDA optimization — search the full knowledge base or chat with our AI assistant.