QK^T Dominance: Over 60% of Transformer Compute in Long Sequences

In contexts with very long sequences, especially in high-resolution vision tasks, the QK^T matrix computation and the subsequent attention matrix can consume over 60% of a Transformer's total computational resources, making it the primary bottleneck for scaling.

The top 3

  1. QKᵀ's N² Secret: Why Self-Attention Bloats: The quadratic computational cost in Transformer self-attention stems from the Query-Key (QKᵀ) matrix multiplication. For an input sequence of N tokens, this operation produces an N×N attention score matrix, leading to approximately O(N²dₖ) floating-point operations, where dₖ is the dimension of the keys.
  2. Long Context Barrier: QKᵀ's Memory & Compute Limits: The quadratic scaling of QKᵀ's computational and memory requirements severely limits Large Language Models (LLMs) from efficiently processing very long sequences. For contexts exceeding ~4096 tokens, the N×N attention matrix often surpasses GPU memory capacity, becoming the primary bottleneck. Doubling the sequence length quadruples both memory and compute costs.
  3. FlashAttention: Halving Memory for QKᵀ Efficiency: FlashAttention dramatically reduces memory usage and speeds up QKᵀ computations by optimizing data movement between GPU memory levels through techniques like tiling and recomputation, achieving 2-4x speedups without changing the core quadratic complexity.

Sources

Open the full topic