Transformers, the architecture underlying large language models, do not naturally perceive word order. The sentences “The dog chased the fox” and “The fox chased the dog” contain the same words but express opposite meanings. Positional embeddings, or positional encodings, tell the model the position of each token, that is, each unit of text it processes, within the sentence.
The core of a transformer is the attention mechanism: each token examines all the others to determine which are relevant. This calculation relies on dot products between vectors, an operation that does not account for the order in which the vectors were processed. A code example provided by the source shows this: by swapping two words in a sentence, the representation of “fox” remains identical in both versions, and the test returns “True”. Without positional encoding, the model does not know whether the fox is chasing or being chased.
The causal mask, used in GPT-type models, limits each token to viewing only the tokens that precede it, which introduces a form of order. However, this signal remains imprecise: it does not distinguish between a gap of one token and a gap of ten tokens. This is why many models add explicit positional information.
Three successive approaches
The first solution involves assigning a unique vector to each position, learned during training, and then adding it to the token’s vector. GPT-2 works this way, but it only has 1,024 position vectors: position 1,025 does not exist, and the context length must be fixed before training. The cost also grows rapidly. For GPT-2 Medium, the position weights represent about 0.3% of the model with 1,024 tokens, and over 22% with around 100,000 tokens.
The original transformer paper, published in 2017, proposes another approach: generating positions from sine and cosine functions, with no parameters to learn. The source compares this process to a watch with three hands, whose different speeds allow the time to be read. According to the author, this system allows a position to be calculated for any length and turns a constant offset into a simple linear transformation. However, it has only been partially effective at capturing relative distances.
RoPE, for Rotary Position Embeddings, appeared in 2021 with work on RoFormer and has become established in recent large language models. This process adds nothing to the token embeddings: it rotates the query and key vectors in each attention layer, according to the token’s position. In the source’s example, an angle of 20 degrees per position gives a score of 13 for positions 5 and 2, as for 105 and 102, because the gap is the same. Bringing the key at position 3 closer raises the score to 19.92.
Models that modify the recipe
According to the source, RoPE remains dominant, but several models change its usage. MiniMax-M3 applies rotation to only 64 of the 128 features in each attention head, SmolLM3 skips RoPE in one out of four layers, and Inkling uses a learned relative bias. T5, released in 2019, employs a simpler relative bias based on the distance between tokens. In layers without encoding, the position remains deducible thanks to the causal mask and previous layers. The developers of SmolLM3 claim this combination improves performance on long contexts.
The source also notes several misconceptions. RoPE does not rotate the values, only the queries and keys. The conventions for pairing features differ: the original LLaMA pairs adjacent features, while the Qwen3-style code works in “split-half”. A misalignment when loading weights therefore renders the model inoperative.
