How Language Models Know Where a Token Is
Self-attention compares tokens with other tokens. In its basic form, it knows what their vectors contain, but it does not receive a separate coordinate telling it where each token appeared. Order matters: “dog bites person” and “person bites dog” contain the same words and describe different events. A language model therefore needs some way to make position visible to its attention layers.
There are several ways to do this. Older systems added a position vector to each token embedding. Many current models rotate the query and key vectors inside attention. Some newer designs rotate only some dimensions, skip explicit positions in selected layers, or add a learned bias to attention scores. These are choices about where order enters the computation, not merely different names for the same embedding.
Start with the attention score
A token ID selects a learned embedding. Each attention layer then projects its current token representation into a query, key, and value. A query-key dot product determines how strongly one position attends to another; softmax converts scores into weights for the values. If position is introduced before those projections, it can influence all three vectors. If introduced inside attention, it can act directly on the comparison between query and key.
For an unmasked attention layer without position information, permuting the input tokens permutes its outputs in the same way. The layer has no independent notion of “third token.” A causal decoder already has an asymmetric mask—each token sees only its prefix—so it has some indirect information about order. Explicit position methods give the model a more direct signal.
Learned absolute positions
GPT-2 uses two learned lookup tables. One maps a token ID to its token vector; the other maps a position index to a position vector. It adds the two vectors element by element before the first transformer block. Two occurrences of the same word begin with the same token vector, but their distinct position vectors make their initial representations different.
This design is easy to understand and implement. Its fixed position table also creates a hard boundary: a model trained with 1,024 entries has no learned entry for position 1,024. Extending the context means changing the table and training the new positions. The table’s parameter count grows linearly with maximum length, even though every token uses just one row at a time.
The original Transformer instead used a fixed vector made from sine and cosine waves of different frequencies. It still added position to the token representation before attention, but did not learn a row per position. For pair i in a vector of width d, the coordinates are sin(m / 10000^(2i/d)) and cos(m / 10000^(2i/d)) at position m. Fast waves distinguish nearby positions; slow waves vary across longer spans. The formula can produce values at positions beyond those seen in training, though that mathematical ability alone does not promise good long-context behavior.
Rotary position embeddings move order into attention
RoPE takes a different route. After the layer creates queries and keys, it groups features into pairs and rotates each pair by an angle that depends on token position. Values are not rotated. For a pair (a, b) at position m, with frequency θ, the transformed pair is:
a' = a cos(mθ) − b sin(mθ)
b' = a sin(mθ) + b cos(mθ)
A rotation preserves the pair’s length. What changes is its direction. The query at position m and key at position n receive rotations of mθ and nθ; their relative angle changes by (m−n)θ. Consequently, their dot product depends on their separation. If both positions move forward by the same amount while the underlying query and key stay fixed, their pairwise contribution stays the same.
For a small numerical picture, suppose two identical unit vectors are rotated at 20 degrees per token. Three positions apart, their dot product is cos(60°) = 0.5. Two positions apart, it becomes cos(40°) ≈ 0.766. The same content vectors now receive different compatibility scores because their positions differ. A real head contains many pairs and sums their contributions, so one cosine does not describe its whole attention score.
Why multiple frequencies?
Rotating every pair at the same rate would give a narrow, repetitive signal. Standard RoPE uses a geometric sequence of frequencies: θᵢ = b^(−2i/d), with head width d and a chosen base b (often 10,000). An eight-feature head then has four pairs; with base 10,000 their frequencies are 1, 0.1, 0.01, and 0.001 radians per position. Different pairs change at different rates as tokens move apart.
Implementations can pair adjacent features or pair the first half of a head with the second half. Both conventions can express the same rotation after a matching feature permutation, but the pairing, cosine tables, and weight layout must agree. Mixing conventions with unchanged weights silently changes attention scores.
In code, an implementation usually builds cosine and sine tables for positions and pair frequencies, broadcasts them across heads and batches, and rotates queries and keys immediately before the attention dot product. Cached keys must retain the rotation for their original positions; a new query uses its current position. A model that changes its RoPE scaling or position indexing also changes how new queries compare with those cached keys.
Context length is a learned behavior
RoPE can calculate an angle at an arbitrary position, whereas a learned absolute table has a finite set of rows. That does not mean a model trained on short sequences can reliably use an arbitrarily long prompt. At much larger positions, its attention patterns may be unfamiliar; high-frequency rotations wrap repeatedly, and the model has not necessarily learned to retrieve or reason over the longer span. Context extension methods rescale or interpolate positions and normally require evaluation or additional training. A declared context window should be checked with retrieval, reasoning, and short-context regression tests.
Variants in current architectures
Partial RoPE rotates only some dimensions of each query and key head. The untouched dimensions still participate in attention. This reduces the size of the cosine and sine tables and some rotation work, while the KV cache still stores complete key and value vectors. It is a design tradeoff, not a universally better replacement for full RoPE. Sebastian Raschka’s architecture survey notes MiniMax-M3 as an example with 64 rotary dimensions in a 128-dimensional head.
NoPE means a layer has no explicit positional encoding. In a causal decoder, token zero can see one position, token one can see two, and so on. A layer can potentially infer something about prefix length from that changing visible set. For example, averaging one marked value across a prefix yields 1/n at prefix length n. This is an illustrative mechanism, not a claim that every trained model uses it. Some models alternate RoPE and NoPE layers; a NoPE layer after a RoPE layer can also receive position-dependent hidden states from earlier computation. SmolLM3, for example, skips RoPE in every fourth block.
Relative score bias adds a position-dependent term to the query-key score before softmax. T5 used relative position biases, and newer variants can make the bias depend on the query state as well as distance. This acts at the score itself rather than rotating the vectors that produce it.
There is no single universal 2026 design. In Raschka’s surveyed open-weight architecture gallery, RoPE remains the most common choice, while partial rotation and mixtures of RoPE and NoPE appear more often in newer entries. Those counts describe that gallery, not every released model.
A useful way to remember the differences
Ask where position enters. Learned absolute and fixed sinusoidal methods modify the token representation before attention. RoPE modifies queries and keys so their dot product reflects relative offsets. A score bias modifies the attention logit directly. NoPE relies on other structure, especially the causal mask and earlier layers. Those entry points explain the engineering consequences: whether maximum position requires a table row, what happens to cached keys, how much per-position state is stored, and what must be evaluated when context length grows.