FlashAttention's Core C++ CUDA Optimizations
FlashAttention revolutionizes VRAM usage by employing tiling and recomputation, avoiding the storage of large intermediate attention matrices in High Bandwidth Memory (HBM). Instead, it loads blocks of Query, Key, and Value data into fast on-chip SRAM, computes attention within SRAM, and discards intermediate results, significantly reducing HBM reads/writes.
The top 3
- 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.
- Kernel Fusion's Memory Bandwidth Gains: FlashAttention uses kernel fusion to combine multiple attention operations (like QKᵀ matmul, softmax, and value multiplication) into a single CUDA kernel, eliminating redundant memory accesses and avoiding writing large intermediate results to HBM.
- Online Softmax for Reduced Memory Footprint: Instead of storing the entire N×N attention matrix, FlashAttention employs an 'online softmax' mechanism, recomputing attention values during the backward pass and maintaining running statistics in fast registers, which reduces memory usage from quadratic to linear with sequence length.
Sources
Open the full topic