Highlights

Contents

Part III
03

Positions: Order Is Invisible Until You Add It

The same words can mean different things in another order. Content comparisons need position information. Learned position rows provide it, but their fixed range and mixed representation create limitations.

YOU ARE HERE"raw text"tokenizerembeddings+ positionslearnedwavesRoPEDECODER BLOCK× NnormattentionQKVsoftmaxmaskheadsn × n+normMLP+residual streamKV cachefinal normLM headsoftmaxP(next)training: the loss sends a correction back to every weight

#Section 15The Blind Spot

“Dog bites man” is not news. “Man bites dog” is. The two sentences contain exactly the same three words, and the only thing that separates a boring headline from a front page is order. Any model of language must care about order. Attention on its own does not.

We have not built attention yet (that is Part VI), so take one property on trust for now and we will prove it there. Attention works by letting each token look at every other token and blend in information from the ones it finds relevant. The amount each token takes from another depends only on what the two tokens are, never on where they are. If you shuffle the input tokens, attention shuffles its outputs in exactly the same way and changes nothing else. Mathematicians call this being permutation equivariant: rearrange the input, and the output is rearranged identically.

WITHOUT POSITIONS, ATTENTION SEES A BAG OF WORDS“dog bites man”dogbitesman“man bites dog”manbitesdogidentical output for "dog"Each strip is what attention produces for that word. Swap the order and every word gets exactly the same strip back.
Figure 1.21The shuffle test. With no position information, the attention output for dog is identical in “dog bites man” and “man bites dog”. The model literally cannot tell who bit whom.

In other words, without help, attention sees a sentence as a bag of words. Every word knows which other words are present, but not where they are. So we must add position information somehow. The rest of this part, and the next two, are three different answers to “how?”.

#Section 16Two Kinds of “Where”

Before we stamp anything, we should decide what “position” means, because there are two quite different answers.

Absolute position is a fixed seat number counted from the start: “I am token number 4.” Relative position is a distance between two tokens: “the word I care about is three steps behind me.” In our sentence, it is at absolute position 4 and cat is at absolute position 1, so cat is three positions behind it.

TWO WAYS TO SAY WHERE A TOKEN ISThe0␠cat1␠sat2␠because3␠it4absolute: "I am at position 4"3 steps backrelative: "the word I need is 3 steps back"language mostlycares about gaps
Figure 1.22Absolute versus relative position. Absolute says “I sit at seat 4”. Relative says “the token I need is three seats back”. For language, the gap is usually what matters.

A relative gap can describe the same local relationship anywhere in a document. Encoding it directly makes that relationship available without relying only on absolute-position patterns.

#Section 17Learned Absolute Positions

The simplest fix borrows the idea from Part II. We already have a table with one row per token. Make a second table, called the position embedding table PP, with one row per position instead. Look up the row for each position and add it, number by number, to the token's vector:

xi=E[idi]+P[i]x_i = E[\text{id}_i] + P[i]

Add the token's embedding to its learned position row. Both tables receive training updates, but the position table has a fixed range.

Adding a position row changes the input for the same word at different locations. The figure illustrates the entry-by-entry addition.

LEARNED POSITIONS: ADD ROW i OF P TO THE TOKEN VECTOR (FIRST 6 OF 512 NUMBERS)E[8415] (␠cat)0.12-0.400.330.05-0.210.18+P[1] (position 1)0.020.10-0.050.200.07-0.11x₁0.14-0.300.280.25-0.140.07the same 512 slots now carry both "what" and "where"
Figure 1.23Adding a position. The token's row from E and the position's row from P are added entry by entry. The result carries both "which token" and "which seat" in the same 512 numbers.
THE POSITION TABLE P: ONE LEARNED ROW PER POSITION (FINCH-19: 1,024 × 512)01234567891011512 columns (24 drawn)…1023The␠cat␠sat␠because␠it␠was␠tiredrow 3 goes to whateversits at position 3
Figure 1.24The position table. Each position fetches its own row, whatever token happens to sit there: position 3 always gets row 3. Our seven tokens use rows 0 to 6.

Adding preserves model width; concatenating would enlarge downstream layers. The trade-off is that the same features now carry both content and position. RoPE instead changes the attention comparison.

GPT-2 used exactly this scheme with a table of 1,024 rows, as did BERT with 512, and Finch-19 copies GPT-2.

#Section 18What the Learned Table Ends Up Looking Like

Nobody told the model anything about how positions relate. Position 7 and position 8 got two independent random rows at the start, exactly as unrelated as position 7 and position 900. So what do the rows look like after training?

Compare position rows by cosine similarity. A bright diagonal is inevitable because each row matches itself; similarity near the diagonal reveals whether neighboring positions learned related representations.

AFTER TRAINING: COSINE SIMILARITY BETWEEN LEARNED POSITION ROWS (ILLUSTRATIVE)005510101515position jposition irow 8: brightest at 8,fading smoothly to 2 and 14nobody asked for this:the model discovered "nearby"
Figure 1.25Similarity between learned position rows after training. Nearby positions end up with similar vectors, and similarity fades smoothly with distance. (Illustrative of the pattern seen in GPT-2.)

That pattern was learned, not designed. The model discovered on its own that nearby positions should look alike, which is a crude form of relative position: if positions 7 and 8 have similar vectors, then "the token right before me" looks roughly the same wherever you are. The model is trying to reinvent relative position from absolute parts. That is a hint that we should give it relative position directly.

#Section 19Where Learned Positions Break

The learned table works. GPT-2 shipped with it and so did many models after. But it has two real weaknesses, and they are why newer models moved on.

The maximum-length wall

A learned position table has a hard final row. Extending it requires new rows and training, rather than merely evaluating the same rule at a later position.

THE MAXIMUM-LENGTH WALL: POSITION 1,024 HAS NO ROW TO FETCH1012101310141015101610171018101910201021102210231024102510261027end of P (row 1,023)trained positions 0 to 1,023no rows here
Figure 1.26The maximum-length wall. Positions past the end of the table have nothing to fetch. GPT-2 hit this wall at 1,024 tokens.

There is a softer version of the same problem inside the limit. If most training documents are short, the rows for high positions are trained on far fewer examples than the rows for low positions. The model then behaves a little worse near the end of long inputs, even before the wall.

The same relationship, learned many times

The second weakness is the one Section 16 warned about. Put the pair the cat at positions 2 and 3, then put it again at positions 102 and 103. The gap is one step both times, so the relationship is the same. But rows 2, 3, 102 and 103 of PP are four independent vectors. Nothing in their numbers says "these two pairs are one step apart in the same way". The model has to learn the "one step back" relationship separately at every offset it could appear at, and it only generalizes to offsets it has seen enough.

THE SAME PAIR AT TWO PLACES GETS UNRELATED POSITION VECTORSthe␠catpos 2pos 3P[2]P[3]the␠catpos 102pos 103P[102]P[103]gap = 1 both times, but nothing in common: "the then cat" is learned separately at every offset
Figure 1.27The same relationship at two places. The gap is one position each time, but the four position vectors have nothing in common, so the model gets no free generalization from one place to the other.

#Section 20Three Answers, Three Injection Points

The next two parts cover three main position schemes, distinguished by where they inject position into the model.

Learned absolute positions (this part) add a trained table PP to the token vectors once, at the very bottom, before the first layer. Sinusoidal positions (Part IV) also add a vector at the bottom, but compute it from a fixed formula of sine and cosine waves instead of learning it, so there are no parameters and no table to run out of. RoPE (Part V) does something different in kind. It never touches the input at all. Instead, inside every attention layer, it rotates the vectors used for comparing tokens by an angle that depends on their positions, in such a way that the comparison only sees the distance between them.

THREE POSITION SCHEMES, THREE INJECTION POINTSLearned (Part III)learned table, added oncetoken embeddingattention(q, k, v)+P[i]Sinusoidal (Part IV)fixed formula, added oncetoken embeddingattention(q, k, v)+PE(i)RoPE (Part V)rotation inside every layertoken embeddingattention(q, k, v)rotate q, k by i
Figure 1.28Where each scheme injects position. Learned and sinusoidal positions are added once at the bottom. RoPE rotates the query and key vectors inside every attention layer and leaves the token vectors alone.
Learned Sinusoidal RoPE
Where added at the bottom added at the bottom inside every attention layer
Parameters Tmax⁡×dmodelT_{\max} \times d_{\text{model}} (524,288 for Finch-19) none none
Past training length hard wall, no rows computable, but untrained computable, and can be stretched
Relative position learned indirectly implicit in the waves exact, by construction
Used by GPT-2, BERT original 2017 Transformer Llama, Mistral, Qwen, Gemma

Sharp edges of positions

Left padding shifts positions. When you batch prompts of different lengths for generation, you often pad short prompts on the left so that all of them end at the same place. The first real word of a padded prompt then sits at position 3 or 4 instead of 0, and the model thinks the text started later than it did. Compute position ids from the attention mask (count only real tokens), never from the raw index.

Packed documents need a decision. To avoid wasting compute on padding, training often packs several short documents end to end into one long row. Should the second document’s positions restart at 0, or continue from where the first ended? Both work, but the attention mask must agree with your choice, so that tokens from one document cannot see the previous one.

No positions is not quite zero information. A surprising result: a model with the causal mask of Part VIII and no position scheme at all can still infer some order, because each token can see a different number of earlier tokens, and it can learn to count them. This is a curiosity worth knowing for interviews, not a replacement for real positions.