Attention reads a crowd, not a queue. That is both the superpower of the transformer and the reason every language model ever shipped needed a hack to know that "dog bites man" and "man bites dog" are different sentences. That hack is positional encoding, one of the oldest core ideas in the architecture, and one that has changed shape three times since 2017.
The problem attention created for itself
Recurrent networks processed text one token at a time, left to right. Word order was built into the computation: the model literally could not see the fourth word before the third. The cost was slow training, because each step had to wait for the last.
The transformer removed the queue. Self-attention lets every token look at every other token in one parallel pass, which is why the architecture could scale on GPUs at all. But attention operates on a set of vectors, not a sequence. Permute the inputs and the same relationships get computed, just relabeled: "cat sat mat" produces the same attention pattern as "mat sat cat." The model, as written, has no way to tell them apart (Vaswani et al., 2017).
The transformer paper called this out directly and solved it the way engineers solve a missing signal: inject one. Before the first layer sees anything, each token embedding gets combined with a vector that says where the token sits in the sequence. Same words, different positions, different inputs to attention. Order is back.

The original trick: sine waves
The 2017 paper offered two ways to do this. The first is now famous: fixed sinusoidal encodings. Each position gets a vector of sines and cosines at different frequencies. Dimension 0 of position 7 looks nothing like dimension 0 of position 8, but neighboring positions have nearby vectors, and the lowest frequencies change slowly enough that position 900 is still distinguishable from position 901.
The authors picked sinusoids for three reasons. Every position gets a unique value, so the model can tell tokens apart by where they sit. The geometry is smooth, so similar positions get similar encodings and the model generalizes more easily. And the relationship between any two positions is linear, which means the model can learn to attend based on relative offsets like "one token back" without ever being taught them. As a bonus, the formula computes an encoding for any position at inference time; nothing was learned, so nothing can run out.
The second option in the paper was to make the positions learnable, a lookup table trained like any other embedding. The authors found both worked nearly identically on translation benchmarks and chose the sine version for its extrapolation potential. They did not know that the industry would mostly pick the learned table first.
Learned tables, and the wall they built
GPT-2 and BERT both went with learned positional embeddings: a trainable table with one vector per position, 1 through 1,024 for GPT-2 and 1 through 512 for BERT (Radford et al., 2019; Devlin et al., 2019). Content and position information get added together and the model sorts the mixture out during training.
Learned tables are flexible, but they carry a hard consequence. A lookup table only knows the positions it was trained on, so the maximum context length became a fixed property of the model weights. Ask GPT-2 for token 1,025 and there is no vector to give it. Much of the early pain around long context, and much of the long drama of context-window extension tricks, traces back to this design choice. Follow-up work also showed that learned positional encodings are surprisingly sensitive: the patterns they converge to depend heavily on how the table was initialized (Ito, Cocchi and Klinger, 2024).
Vision transformers inherit the whole problem in two dimensions. The original ViT cuts an image into patches, flattens them into a sequence, and adds learned 1D positional embeddings, which bakes in the assumption that image size is fixed (Dosovitskiy et al., 2020). Larger or differently-shaped inputs need resampling of the position grid.
The relative turn: rotations and penalties
By 2020 the field started asking a better question. Language depends mostly on relative distance, not absolute index: "not good" behaves like "not good" wherever it appears. Shaw et al. (2018) had already proposed attending over learned representations of the distance between two tokens instead of their absolute positions. The idea went mainstream through two descendants.
The first is rotary positional embedding, or RoPE. Instead of adding anything, RoPE rotates the query and key vectors by an angle proportional to each token position. When the rotated query meets the rotated key inside the attention dot product, the absolute positions algebraically cancel and only the relative offset survives (Su et al., 2021). Nothing is stored, so there is no table to run out of, and the decay with distance is built into the geometry. RoPE is why LLaMA and most open models since 2023 handle a few thousand tokens of context without a trained maximum length written into their weights (Touvron et al., 2023).
The second is Attention with Linear Biases, or ALiBi, which skips embeddings entirely. It adds a fixed penalty to attention scores proportional to distance between tokens, and gives each attention head a different penalty slope. Some heads look at neighbors, others scan far. The 2021 paper showed a model trained on short sequences could keep working on inputs eight times longer (Press, Smith and Lewis, 2021).

Why the window is still brittle
Neither trick made long context free. Position-based extension became its own research cottage. YaRN rescales the frequencies inside RoPE so a model trained at 4,000 tokens can be adapted to 128,000 with modest fine-tuning rather than from-scratch retraining (Peng et al., 2023). Randomized positional encodings, which train with positions drawn from a Gaussian distribution rather than counted 1 through N, measurably improve length generalization on algorithmic tasks (Ruoss et al., 2023). And the exponential-decay variant of sinusoids has been used to bias attention toward locality while a compressive memory absorbs the long range (He et al., 2024).
There is also behavioral evidence that position signals are underused rather than missing. The 2023 "Lost in the Middle" study found that models reading long documents answer best when the relevant fact sits at the very beginning or the very end of the context, and degrade sharply in the middle, a U-shaped curve that grows worse as windows grow (Liu et al., 2023). The encodings tell the model where every token is. Attention has not fully learned to care evenly.
What to remember
- Self-attention is order-blind by construction; positional encoding is what makes "dog bites man" different from "man bites dog."
- The transformer offered fixed sine waves and learned tables. The 2017 paper preferred sines; 2018-2019 models shipped learned tables, and their fixed size became the context window ceiling.
- Modern models mostly encode relative position: RoPE rotates queries and keys so distance falls out of the dot product; ALiBi just penalizes far-away tokens.
- Every window-extension technique in the last three years, YaRN included, is really a positional-encoding trick in disguise.
- Position information is injected, but not equally used: the lost-in-the-middle effect is a property of how attention reads positions, not whether the model has them.
Sources
- Vaswani et al., 2017, Attention Is All You Need: https://arxiv.org/abs/1706.03762
- Radford et al., 2019, Language Models are Unsupervised Multitask Learners: https://cdn.openai.com/better-language-models/language_models_are_unsupervised_multitask_learners.pdf
- Devlin et al., 2019, BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding: https://arxiv.org/abs/1810.04805
- Dosovitskiy et al., 2020, An Image is Worth 16x16 Words: https://arxiv.org/abs/2010.11929
- Shaw et al., 2018, Self-Attention with Relative Position Representations: https://arxiv.org/abs/1803.02155
- Su et al., 2021, RoFormer: Enhanced Transformer with Rotary Position Embedding: https://arxiv.org/abs/2104.09864
- Touvron et al., 2023, LLaMA: Open and Efficient Foundation Language Models: https://arxiv.org/abs/2302.13971
- Press, Smith and Lewis, 2021, Train Short, Test Long: Attention with Linear Biases: https://arxiv.org/abs/2108.12409
- Peng et al., 2023, YaRN: Efficient Context Window Extension of Large Language Models: https://arxiv.org/abs/2309.00071
- Ruoss et al., 2023, Randomized Positional Encodings Boost Length Generalization of Transformers: https://arxiv.org/abs/2305.16843
- He et al., 2024, Leave No Context Behind: Efficient Infinite Context Transformers: https://arxiv.org/abs/2404.07143
- Liu et al., 2023, Lost in the Middle: How Language Models Use Long Contexts: https://arxiv.org/abs/2307.03172
- Ito, Cocchi and Klinger, 2024, Learning positional encodings in transformers depends on initialization: https://arxiv.org/abs/2406.08272
- Positional Encoding in Transformer-Based Time Series Models: A Survey (2025): https://arxiv.org/abs/2502.12370
