ElemedexA pocket encyclopedia of AI architectures

Glossary (TM) › Architectural parts

TM108

Transformer block

Also: transformer layer, decoder layer

In Spanish: Bloque transformer

The unit repeated dozens of times in a transformer: an attention layer and a feedforward network, each with a residual connection and normalization.

A transformer is, above all, a stack of blocks identical in shape (with different weights). Each block has two sublayers: (1) multi-head attention, which moves information between positions, and (2) a feedforward (MLP) block network, which processes each position separately (Vaswani et al., 2017, sec. 3.1; Alammar & Grootendorst, 2024, p. 86). Each sublayer is wrapped in a residual connection and a layer normalization. In the original version:

x ← LayerNorm(x + Attention(x)), x ← LayerNorm(x + FFN(x))

All sublayers return vectors of the same dimension they receive (512 in the original), which is what makes stacking possible (Vaswani et al., 2017, sec. 3.1). The original decoder blocks add a third sublayer of cross-attention.

How many. Six on each side in the Transformer; 12 in GPT-1 and BERT-base; 96 in GPT-3 (Radford et al., 2018, sec. 4.1; Devlin et al., 2019, p. 4173; Brown et al., 2020, sec. 2.1). In Pokédex diagrams, that number is the specimen's "height".

How it changed. The 2017 block survives almost intact, with tweaks: normalization moves before each sublayer (pre-norm: x ← x + Attention(LN(x))), which stabilizes training; LayerNorm is replaced by RMSNorm; the feedforward network's ReLU, by SwiGLU; and attention gains rotary positions and grouped queries (Alammar & Grootendorst, 2024, pp. 101–102; Touvron, Lavril, et al., 2023, sec. 2.2). In mixture of experts models, the feedforward network is multiplied into several "experts" (Mixture of experts (Mixtral)).

How to think about it. There are two complementary readings. One is an assembly line: each block transforms the representation and hands it on, like the "abstraction sandwiches" that Buckner (2024) describes for convolutional networks (2024, p. 127). The other, favored by mechanistic interpretability, sees a single residual stream running through the stack, which every sublayer reads from and writes to (Elhage et al., 2021). The first suggests hierarchy; the second, a shared workspace. Neither is compulsory: they are two ways of looking at the same sum of matrices.

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 §
  • 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 §
  • Radford, A., Narasimhan, K., Salimans, T., & Sutskever, I. (2018). Improving language understanding by generative pre-training [Technical report]. OpenAI. https://cdn.openai.com/research-covers/language-unsupervised/language_understanding_paper.pdf §
  • Touvron, H., Lavril, T., Izacard, G., Martinet, X., Lachaux, M.-A., Lacroix, T., Rozière, B., Goyal, N., Hambro, E., Azhar, F., Rodriguez, A., Joulin, A., Grave, E., & Lample, G. (2023). LLaMA: Open and efficient foundation language models (arXiv:2302.13971). arXiv. https://doi.org/10.48550/arXiv.2302.13971 §
  • 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 §