FlashAttention & Sparse Attention: Transformer FLOPs Optimization

FlashAttention and sparse attention mechanisms significantly reduce FLOPs in transformer LLMs by optimizing the attention layer. FlashAttention achieves 2-4x speedups by reordering computations and using tiling to reduce memory reads/writes between GPU HBM and SRAM. Block-sparse FlashAttention further improves this, being 2-4x faster than FlashAttention, by leveraging sparsity to reduce operations for very long sequences (up to 64K tokens).

The top 3

  1. FlashAttention's Memory Breakthrough for Transformers: FlashAttention revolutionized Transformer efficiency by treating attention as an I/O problem, using tiling and recomputation to keep computations in fast on-chip SRAM. This avoids storing large intermediate matrices in slower HBM, reducing memory usage from quadratic to linear with sequence length and enabling contexts up to 64K tokens.
  2. Sparse vs. FlashAttention: Divergent Optimization Strategies: FlashAttention performs exact attention, optimizing its hardware-efficient computation for memory. In contrast, Sparse Attention approximates attention by selectively ignoring less relevant token pairs, fundamentally reducing the number of FLOPs from quadratic to linear or O(N log N) complexity, changing the attention pattern itself.
  3. FlashAttention 2: Key Speed & Efficiency Gains: FlashAttention-2 improves upon its predecessor by re-engineering for better GPU parallelism and work partitioning, achieving up to 2x faster performance than FlashAttention-1, and 9x faster than standard PyTorch attention. It optimizes non-matmul FLOPs and uses better warp-level communication, reaching 50-73% of A100's theoretical peak FLOPs/s.

Sources

Open the full topic