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.

Sources

Open the full topic