Transformers from the ground up
Positional information
Attention is permutation-invariant, so position has to be injected. Sinusoidal encodings, learned tables and rotary embeddings are three answers to the same problem.
Attention has no order
Shuffle the tokens of a sequence and shuffle the attention rows and columns to match. The output is the same, up to that shuffle. Attention compares content with content. Nothing in the dot product knows whether a token came first or fifth. That is useful for sets. It is a bug for language, where “dog bites man” and “man bites dog” contain the same tokens.
The fix is to add position information to the token vectors before the first block. The model then sees a vector that says both what the token is and where it sits. Every later layer inherits that signal through the residual stream. The code therefore has to be applied only once, at the input.
Absolute encodings
The original transformer used a fixed sinusoidal table. Sine and cosine waves fill the vector. Each dimension oscillates at a different rate: fast for the early pairs, slow for the later ones. The pattern is fixed in advance and needs no weights. It gives every position a unique fingerprint that the model can learn to read.
A learned table is the simpler alternative. It is an embedding matrix with one row per position, trained like the token embeddings. It fits the training data well. It cannot extrapolate beyond the longest sequence it saw, because there is no row for position 4,097. A sinusoidal table can in principle reach further. A model trained on short sequences still rarely generalises to long ones without other changes.
The sinusoidal table has a useful property. A shift in position is a linear change to the code. That does not make the model relative on its own. It does give the weights a consistent pattern to learn. The rates are spaced geometrically. The fastest pair completes a cycle every few positions. The slowest pair changes very little across a whole sequence.
A learned position table
import torch
from torch import nn
positions = torch.arange(seq_len).unsqueeze(1)
table = nn.Embedding(max_positions, d_model)
x = token_embeddings + table(positions)
print(x.shape) # (batch, tokens, d_model)A fixed position fingerprint
Print the encodings for four positions, then compute the dot product between position zero and each of the others and describe how the similarity decays.
Relative and rotary
Absolute codes tell a token where it is. Relative methods tell it how far away another token is. That is often the more useful fact. Rotary position embedding, or RoPE, takes the query and key vectors. It rotates each pair of dimensions by an angle that depends on the position. The dot product of a rotated query and key then depends on their offset. It does not depend on their absolute positions.
The rotation is applied inside attention, not added to the input. The residual stream therefore stays free of position vectors. RoPE is the default in many current language models. It is often combined with a scaling rule that stretches the rates for longer contexts. ALiBi takes a different route. It subtracts a distance penalty from the attention scores, with no position vectors at all.
RoPE is usually applied per head. It comes after the query and key projections and before the dot product. That placement matters. The values are not rotated, so the content that gets mixed is unchanged. The residual stream never sees a position vector. The rotation is a property of the comparison, not of the vector itself.
When you pack several documents into one sequence, reset or offset the position ids at each boundary. A model that sees document two starting at position 900 has been told something false about where its first token sits.
What position does not fix
A position code gives the model a coordinate. It does not guarantee that the model will use distance correctly. Attention can still spread across a long context. A model trained on short sequences may ignore positions far outside its experience. Extending the context window usually needs more than a bigger table. It needs training on longer inputs, or a deliberate stretch of the rates.
Position also depends on the task. Copying a token from earlier is a relative operation. Answering a question about the third paragraph is closer to an absolute one. Models learn both. A change in context length often shows up here first, as a quiet drop in quality rather than an error.
Long-context testing is its own discipline. A model can pass a needle-in-a-haystack test and still fail to use the middle of a long document. Finding a fact and reasoning with it are different demands. Position codes set the stage. The test has to check whether the model reads the whole stage.
Rotate instead of add
Run the relative scores, then confirm that shifting both positions by the same amount leaves the dot product unchanged.
Check what you learned
One question at a time. A wrong answer explains itself — take another go.
Part 1 of 5
Why does a transformer need positional information?
