Does using h heads with head dimension d_model/h increase computational cost compared to a single head of size d_model?
Last updated
No extra matmul FLOPs: projections cost
4·N·d²either way, and QKᵀ costsh·N²·(d/h) = N²·d, same as one head.What grows is the
h·N²score count: softmax, masking and dropout do h× more elementwise work, and stored attention weights use h× memory (avoided byFlashAttention).Small heads mean many small matmuls with low hardware efficiency, so
d_kis usually kept around 64 to 128.
- https://inamdaraditya.medium.com/multi-head-attention-from-scratch-a-complete-guide-to-the-engine-that-powers-every-transformer-8e78c5511e11
- https://apxml.com/courses/foundations-transformers-architecture/chapter-6-advanced-architectural-variants-analysis/self-attention-complexity
- https://mbrenndoerfer.com/writing/multi-head-attention-transformers