ElemedexA pocket encyclopedia of AI architectures

Glossary (TM) › Architectural parts

TM100

Positional encoding

Also: positional embeddings, position embeddings, RoPE

In Spanish: Codificación posicional

Information about each token's place in the sequence, added to or applied to its vector because attention by itself cannot tell order apart.

Self-attention compares every token with every other, but it does not know in which order they came: without extra information, it treats the input as a set. "Dog bites man" and "Man bites dog" contain exactly the same tokens. Since the Transformer has no recurrence and no convolution, its authors had to "inject" position into each vector (Vaswani et al., 2017, sec. 3.5).

The original solution was to add to each embedding a vector of sines and cosines of different frequencies:

PE(pos, 2i) = sin(pos / 10000^(2i/d)), PE(pos, 2i+1) = cos(pos / 10000^(2i/d))

Each dimension is a wave; together they work like an odometer with many wheels. The authors chose this form because a shift by k positions is a linear function, which should make it easy to attend to relative positions. They also tried learned positions, with nearly identical results (Vaswani et al., 2017, secs. 3.5 and 6.2).

Learned positions. GPT-1 and BERT add to each token one learned vector per position, which fixes a maximum length: 512 tokens in both cases (Radford et al., 2018, sec. 4.1; Devlin et al., 2019, pp. 4174, 4183). Vision Transformer (ViT) does the same for image patches.

RoPE. Today rotary position embeddings dominate: instead of being added to the input, they rotate the query and key vectors by an angle proportional to position, right before they are compared, so that the score depends on the distance between tokens (Alammar & Grootendorst, 2024, pp. 102–104). LLaMA and its successors use them (Touvron, Lavril, et al., 2023, sec. 2.2). This also matters because training packs several documents into one window, and the model should not be told that a document's first token is number 50 (Alammar & Grootendorst, 2024, p. 103).

A linguistic angle. Here word order is not part of the structure of the computation, as in a grammar or a recurrent network: it is one more feature, added to each word's vector, that the model must learn to use. Buckner (2024) notes that splitting each vector into query, key and value and adding positional encodings were the two innovations that allowed recurrence to be abandoned (2024, p. 272). Whether syntactic compositionality can be rebuilt from that is an open empirical question.

Sources

  • Alammar, J., & Grootendorst, M. (2024). Hands-on large language models: Language understanding and generation. O’Reilly Media. §
  • 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 §
  • 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 §