Course contents 18 / 37

18 / 37 · Transformers

Positional representations

Self-attention is permutation-equivariant, so order has to be added to the input. The note proves the equivariance in one line, derives the sinusoidal encoding and its shift property, computes a small example, and compares fixed, learned and rotary positions.

On this page 10

The Transformer note added a positional signal PP to the token embeddings before the first block and deferred what PP is. This note starts from why the signal is necessary, which takes one line of algebra, then builds the encoding used in the original paper and the alternatives that replaced it.

Notation. X∈Rn×dX \in \mathbb R^{n \times d} the embedded sequence; Π\Pi an n×nn \times n permutation matrix; pos∈{0,…,n−1}\mathrm{pos} \in \{0, \dots, n - 1\} a position; P∈Rn×dP \in \mathbb R^{n \times d} the positional matrix with rows PposP_{\mathrm{pos}}.

Baseline: no positions#

Apply self-attention to a permuted input ΠX\Pi X. The projections are per row, so Q,K,VQ, K, V become ΠQ,ΠK,ΠV\Pi Q, \Pi K, \Pi V. Then

softmax⁡((ΠQ)(ΠK)⊤/dk) ΠV=softmax⁡(Π QK⊤Π⊤/dk) ΠV=Π softmax⁡(QK⊤/dk) V,\operatorname{softmax}\big((\Pi Q)(\Pi K)^\top / \sqrt{d_k}\big)\,\Pi V = \operatorname{softmax}\big(\Pi\, Q K^\top \Pi^\top / \sqrt{d_k}\big)\,\Pi V = \Pi\,\operatorname{softmax}\big(QK^\top/\sqrt{d_k}\big)\,V ,

using Π⊤Π=I\Pi^\top \Pi = I and the fact that a row-wise softmax commutes with permuting rows and columns together. The output of the permuted input is the permuted output: self-attention is permutation-equivariant. The feed-forward layer, LayerNorm and residuals act per position and are equivariant too. The argument holds for unmasked attention, or for a mask permuted along with the tokens, as a padding mask is. A causal mask is attached to positions, not to tokens, so it breaks arbitrary permutations: in a decoder the first token attends only to itself, whatever word it is. An encoder has no causal mask, so an encoder without positional input computes, for “dog bites man” and “man bites dog”, the same three vectors in a different order, and a classifier that pools them cannot tell the sentences apart. A recurrent network never had this problem, because it read tokens in sequence; the price of parallelism is that order must be put back by hand.

Sinusoidal encoding#

Vaswani et al. (2017) define, for position pos\mathrm{pos} and dimension index ii,

Ppos, 2i=sin⁡ ⁣(pos100002i/d),Ppos, 2i+1=cos⁡ ⁣(pos100002i/d),i=0,…,d/2−1.P_{\mathrm{pos},\,2i} = \sin\!\Big(\frac{\mathrm{pos}}{10000^{2i/d}}\Big), \qquad P_{\mathrm{pos},\,2i+1} = \cos\!\Big(\frac{\mathrm{pos}}{10000^{2i/d}}\Big), \qquad i = 0, \dots, d/2 - 1 .

Each pair of dimensions (2i,2i+1)(2i, 2i + 1) is a sine and cosine of the same angle ωi⋅pos\omega_i \cdot \mathrm{pos} with ωi=10000−2i/d\omega_i = 10000^{-2i/d}. The frequencies form a geometric progression: dimension pair 0 has wavelength 2π2\pi positions, the last pair, i=d/2−1i = d/2 - 1, has wavelength 2π⋅100001−2/d2\pi \cdot 10000^{1 - 2/d}, which approaches 10000⋅2π10000 \cdot 2\pi as dd grows: about 19,869 positions for d=16d = 16 and 60,611 for d=512d = 512. Low dimensions change quickly with position and resolve neighbours; high dimensions change slowly and encode coarse location.

Figure

Sinusoidal positional encoding: four of the sixteen dimensions

Sinusoidal positional encoding curvesFour sine curves over positions 0 to 60. The blue curve for dimension 0 oscillates rapidly, the red for dimension 2 more slowly, the green for dimension 6 completes about half a cycle, and the amber for dimension 14 rises almost linearly and stays near zero.02040601−1positiondim 0, λ ≈ 6.3dim 2, λ ≈ 19.9dim 6, λ ≈ 199dim 14, λ ≈ 19869Sinusoidal positional encoding curvesFour sine curves over positions 0 to 60. The blue curve for dimension 0 oscillates rapidly, the red for dimension 2 more slowly, the green for dimension 6 completes about half a cycle, and the amber for dimension 14 rises almost linearly and stays near zero.02040601−1positiondim 0, λ ≈ 6.3dim 2, λ ≈ 19.9dim 6, λ ≈ 199dim 14, λ ≈ 19869
d = 16. Each even dimension 2i is sin(pos / 10000^(2i/d)); the odd dimension next to it is the cosine with the same frequency. Dimension 0 has wavelength 2π ≈ 6.3 positions and repeats every few tokens; dimension 14 has wavelength 19869 positions and barely moves over the sequence. Any single dimension repeats, so two positions can share its value, but no two positions in range share the whole vector. Vertical dashed lines mark positions 0, 20 and 40.

Worked example#

d=4d = 4, so two frequency pairs: ω0=1\omega_0 = 1 and ω1=10000−1/2=0.01\omega_1 = 10000^{-1/2} = 0.01.

possin⁡(pos)\sin(\mathrm{pos})cos⁡(pos)\cos(\mathrm{pos})sin⁡(0.01 pos)\sin(0.01\,\mathrm{pos})cos⁡(0.01 pos)\cos(0.01\,\mathrm{pos})
00101
10.8410.5400.0101.000
20.909−0.4160.0201.000
30.141−0.9900.0301.000

Rows are distinct, bounded in [−1,1][-1, 1], and the slow pair changes by 0.010.01 per step, so positions 1 and 2 are closer in the slow dimensions than positions 1 and 30. The vector is added to the token embedding (scaled by d\sqrt d so that the two have comparable magnitude), and the same table is reused for every sequence; it is added once, before the first block.

Properties#

Relative shift is a rotation. For a fixed offset kk, the pair at position pos+k\mathrm{pos} + k is a linear function of the pair at pos\mathrm{pos}, independent of pos\mathrm{pos}:

(sin⁡ω(pos+k)cos⁡ω(pos+k))=(cos⁡ωksin⁡ωk−sin⁡ωkcos⁡ωk)(sin⁡ω poscos⁡ω pos),\begin{pmatrix} \sin\omega(\mathrm{pos} + k) \\ \cos\omega(\mathrm{pos} + k) \end{pmatrix} = \begin{pmatrix} \cos\omega k & \sin\omega k \\ -\sin\omega k & \cos\omega k \end{pmatrix} \begin{pmatrix} \sin\omega\,\mathrm{pos} \\ \cos\omega\,\mathrm{pos} \end{pmatrix},

which is the angle-addition identity. The authors hypothesized that this lets a linear projection in attention pick out relative positions; the dot product Ppos⋅Ppos+kP_{\mathrm{pos}} \cdot P_{\mathrm{pos}+k} indeed depends on kk only. Whether the trained model uses this is empirical.

No parameters and no maximum length in principle. The formula evaluates at any position. In practice models are trained on sequences up to some length and do worse beyond it, because the attention patterns learned on short sequences do not transfer to combinations of positional vectors never seen in training.

Repetition within a dimension is not a collision. Any single sine repeats, so positions pos\mathrm{pos} and pos+2π/ωi\mathrm{pos} + 2\pi/\omega_i share dimension 2i2i. The other dimensions have incommensurable periods, so two positions in any practical range do not share the whole vector.

Learned absolute positions#

Replace the formula by a table P∈Rnmax⁡×dP \in \mathbb R^{n_{\max} \times d} of free parameters, nn.Embedding(max_len, d), trained with everything else. BERT does this with nmax⁡=512n_{\max} = 512, GPT-2 with 1,024, and the translation model of the next note with 32. It is simpler, performs about the same as sinusoids on tasks of the training length (Vaswani et al., 2017, Table 3, row E), and has a hard maximum: a position beyond nmax⁡n_{\max} has no row. The table costs nmax⁡×dn_{\max} \times d parameters, 0.4 M for BERT-base.

Relative and rotary positions#

Both schemes above encode absolute position and leave attention to infer relative distances. Later models encode the relative offset directly, because what matters for most linguistic relations is how far apart two tokens are, not where in the document they sit.

Relative position biases add a learned scalar bj−ib_{j-i} to the score of query ii and key jj (T5, which shares one scalar among offsets grouped into buckets that widen with distance), or a fixed penalty proportional to ∣j−i∣|j - i| (ALiBi, Press, Smith and Lewis, 2022), with no positional vector at the input at all.

Rotary position embeddings (RoPE, Su et al., 2024) move the sinusoids from the input to the attention scores. Each query and key of a head, of width dkd_k, is split into dk/2d_k/2 two-dimensional pairs, and pair ii at position pos\mathrm{pos} is rotated by angle ωi pos\omega_i\,\mathrm{pos}, with ωi=10000−2i/dk\omega_i = 10000^{-2i/d_k}. Because a dot product of two rotated vectors depends only on the difference of the rotation angles, the score qm⋅knq_m \cdot k_n depends on the content of the two tokens and on m−nm - n, not on mm and nn separately. The shift property of the sinusoidal scheme becomes exact and built into every attention layer. Llama 3.2 1B, the model of the GPT and instruction tuning notes, uses RoPE. The rotation has no parameters, and the base 1000010000 can be increased to stretch a trained model to longer contexts.

SchemeWhere appliedParametersMax lengthEncodes
SinusoidalAdded to input0None in principleAbsolute, with shift structure
Learned tableAdded to inputnmax⁡dn_{\max} dnmax⁡n_{\max}Absolute
Relative bias / ALiBiAttention scoresO(nmax⁡)O(n_{\max}) or 0Extrapolates somewhatRelative offset
RoPEQueries and keys0Trained length, extendableRelative offset in the dot product

What changed#

Recurrent modelTransformer + positions
Source of orderSequential readingAn explicit positional signal
CostImplicit00 to nmax⁡dn_{\max} d parameters
Parallel over positionsNoYes
Length generalizationAny length, weak memoryDepends on the scheme

Limitations#

  • Absolute schemes do not extrapolate. A learned table has a hard limit; sinusoids have a soft one.
  • Added signals compete with content. In the input-addition schemes the positional vector shares the dd dimensions with the token vector; the model must learn to separate them.
  • Long contexts stress every scheme. Which positional method generalizes best to lengths beyond training is an active empirical question, and the answer interacts with the data and the attention implementation.
  • Positions are indices, not structure. None of the schemes knows about sentences, paragraphs or syntax; those have to be learned from the tokens.

What you should remember#

  • Unmasked self-attention is permutation-equivariant: Attn⁡(ΠX)=Π Attn⁡(X)\operatorname{Attn}(\Pi X) = \Pi\,\operatorname{Attn}(X). Order must be added; a causal mask breaks the symmetry, since it is fixed by position.
  • Sinusoidal: sin⁡\sin and cos⁡\cos of pos⋅10000−2i/d\mathrm{pos} \cdot 10000^{-2i/d}, wavelengths in geometric progression from 2π2\pi to 2π⋅100001−2/d2\pi \cdot 10000^{1 - 2/d}, just under 10000⋅2π10000 \cdot 2\pi for large dd; a shift by kk is a fixed rotation of each pair.
  • Learned tables are simpler and bounded by nmax⁡n_{\max}.
  • RoPE rotates queries and keys so that scores depend on relative offset; no parameters, used by current decoder-only models.
  • The scheme sets how the model handles lengths it was not trained on.

Notebook#

week05_transformer.ipynb. Cell numbers count every cell, markdown included, from 1. Quoted numbers come from the run saved with the course; cells changed since then for current library versions have no saved output.

  • Cell 34 states the sinusoidal formulas and the wavelength range, and the dropout applied to the sum of embeddings and positions.
  • Cell 35 implements PositionalEncoder: the frequencies are computed as exp⁡(−2iln⁡10000/d)\exp(-2i \ln 10000 / d) for even 2i2i, the table of shape [1, max_length, d] is filled with sines in even columns and cosines in odd columns and stored as a non-trainable parameter, and forward adds the first T rows to the input and applies dropout. Cell 36 points to rotary embeddings as the modern alternative.

week04_lstm_attention_transformer.ipynb, cell 58, uses a learned table nn.Embedding(max_len, hidden_size) with max_len = 32, indexed by arange(T) % max_len so that longer inputs wrap around rather than fail.

week08_modern_LLM.ipynb, cells 18–19, prints the module trees of the Llama 3.2 1B checkpoints; RoPE appears in them as the rotary_emb module.

Next#

With attention, the block and positions in place, the architecture is complete. The next note returns to the German–English task where the course left the LSTM at BLEU 0.152, trains a Transformer with the same parameter budget, measures what cross-attention, depth and beam search each contribute, and reads the attention maps.

References#