ElemedexA pocket encyclopedia of AI architectures

Glossary (TM) › Architectural parts

TM097

Multi-head attention

Also: attention heads

In Spanish: Atención multicabezal

Several attention operations run in parallel, each with its own projections, whose results are concatenated, so that different kinds of relation between tokens are gathered at once.

Instead of a single attention over full vectors, the transformer runs h attentions in parallel ("heads"), each with its own query, key and value matrices projecting into a smaller subspace; it then concatenates the results and projects them again (Vaswani et al., 2017, sec. 3.2.2):

MultiHead(Q,K,V) = Concat(head₁, …, headₕ)·W_O, headᵢ = Attention(Q·Wᵢ_Q, K·Wᵢ_K, V·Wᵢ_V)

The original transformer used 8 heads of 64 dimensions; BERT-base, 12; GPT-3, 96 heads in each of its 96 layers, that is, 9,216 (Vaswani et al., 2017, sec. 3.2.2; Devlin et al., 2019, p. 4173; Brown et al., 2020, sec. 2.1).

Why. A single softmax yields a single weighted average: if a token needs both its subject and its antecedent, the average blurs them. Several heads let the model attend to "different representation subspaces at different positions" (Vaswani et al., 2017, sec. 3.2.2). With one head, translation got 0.9 BLEU worse; with too many, it also degraded (Vaswani et al., 2017, sec. 6.2).

What each head might track. Take "The dog chased the squirrel because it was hungry." One head might link it to dog (whose hunger?); another, each word to the previous one; another, chased to its subject and object; another might "park" on a punctuation mark when there is nothing useful to look at. Something like this was found in BERT: K. Clark et al. (2019) found heads that track direct objects, determiners and coreference, and others that mostly attend to delimiters; Buckner (2024) shows a head that seems to resolve the anaphora in "she gave her a mint because her breath was bad" (2024, pp. 278–279). Other heads copy patterns from context, [a][b] … [a] → [b]: these are the so-called induction heads (Elhage et al., 2021; Buckner, 2024, pp. 281–282).

Warning. Nobody assigns these roles: they are the analyst's hypotheses. Visualizations are often hand-picked, many heads resist interpretation, and many can be pruned at little cost (Voita et al. (2019) removed 38 of 48 encoder heads for a loss of 0.15 BLEU). Moreover, the weights show where information came from, not what the model did with it (Buckner, 2024, p. 278).

Efficient variants (multi-query and grouped-query attention) share keys and values across heads to relieve the KV cache (Alammar & Grootendorst, 2024, pp. 98–100).

Sources

  • Alammar, J., & Grootendorst, M. (2024). Hands-on large language models: Language understanding and generation. O’Reilly Media. §
  • Brown, T. B., Mann, B., Ryder, N., Subbiah, M., Kaplan, J., Dhariwal, P., Neelakantan, A., Shyam, P., Sastry, G., Askell, A., Agarwal, S., Herbert-Voss, A., Krueger, G., Henighan, T., Child, R., Ramesh, A., Ziegler, D. M., Wu, J., Winter, C., . . . Amodei, D. (2020). Language models are few-shot learners. In Advances in Neural Information Processing Systems (Vol. 33, pp. 1877–1901). Curran Associates. https://proceedings.neurips.cc/paper_files/paper/2020/hash/1457c0d6bfcb4967418bfb8ac142f64a-Abstract.html §
  • Buckner, C. J. (2024). From deep learning to rational machines: What the history of philosophy can teach us about the future of artificial intelligence. Oxford University Press. https://doi.org/10.1093/oso/9780197653302.001.0001 §
  • Clark, K., Khandelwal, U., Levy, O., & Manning, C. D. (2019). What does BERT look at? An analysis of BERT’s attention. In T. Linzen, G. Chrupała, Y. Belinkov, & D. Hupkes (Eds.), Proceedings of the 2019 ACL Workshop BlackboxNLP: Analyzing and Interpreting Neural Networks for NLP (pp. 276–286). Association for Computational Linguistics. https://doi.org/10.18653/v1/W19-4828 §
  • Devlin, J., Chang, M.-W., Lee, K., & Toutanova, K. (2019). BERT: Pre-training of deep bidirectional transformers for language understanding. In J. Burstein, C. Doran, & T. Solorio (Eds.), Proceedings of the 2019 Conference of the North American Chapter of the Association for Computational Linguistics: Human Language Technologies, Volume 1 (Long and Short Papers) (pp. 4171–4186). Association for Computational Linguistics. https://doi.org/10.18653/v1/N19-1423 §
  • Elhage, N., Nanda, N., Olsson, C., Henighan, T., Joseph, N., Mann, B., Askell, A., Bai, Y., Chen, A., Conerly, T., DasSarma, N., Drain, D., Ganguli, D., Hatfield-Dodds, Z., Hernandez, D., Jones, A., Kernion, J., Lovitt, L., Ndousse, K., . . . Olah, C. (2021). A mathematical framework for transformer circuits. Transformer Circuits Thread. https://transformer-circuits.pub/2021/framework/index.html §
  • Vaswani, A., Shazeer, N., Parmar, N., Uszkoreit, J., Jones, L., Gomez, A. N., Kaiser, Ł., & Polosukhin, I. (2017). Attention is all you need. In Advances in Neural Information Processing Systems (Vol. 30). Curran Associates. https://proceedings.neurips.cc/paper_files/paper/2017/hash/3f5ee243547dee91fbd053c1c4a845aa-Abstract.html §
  • Voita, E., Talbot, D., Moiseev, F., Sennrich, R., & Titov, I. (2019). Analyzing multi-head self-attention: Specialized heads do the heavy lifting, the rest can be pruned. In A. Korhonen, D. Traum, & L. Màrquez (Eds.), Proceedings of the 57th Annual Meeting of the Association for Computational Linguistics (pp. 5797–5808). Association for Computational Linguistics. https://doi.org/10.18653/v1/P19-1580 §