Top 3 FlashAttention CUDA Optimizations

FlashAttention's primary C++ CUDA optimizations include an IO-aware tiling strategy that loads blocks of Q, K, V into fast on-chip SRAM to minimize slow High Bandwidth Memory (HBM) transfers, significantly reducing memory traffic.

The top 3

  1. Tiling for SRAM Efficiency: Minimizing HBM Transfers: FlashAttention breaks attention computation into small blocks (tiles) that fit into fast on-chip SRAM, drastically reducing data movement to and from slow High Bandwidth Memory (HBM) and improving throughput by 2-4x for models like GPT-2.
  2. Kernel Fusion: Unifying Operations for Peak GPU Efficiency: By merging QKᵀ matrix multiplication, softmax, and PV multiplication into a single CUDA kernel, FlashAttention eliminates costly intermediate writes to HBM and reduces kernel launch overhead, keeping all operations within the GPU's fast SRAM.
  3. Strategic Recomputation: Trading FLOPs for Memory Bandwidth: Instead of storing large intermediate attention matrices (like the full softmax matrix) in HBM, FlashAttention recomputes them on-the-fly, particularly during the backward pass. This leverages abundant compute for significant memory bandwidth savings, reducing memory usage from quadratic to linear.

Sources

Open the full topic