11.4 Positional encodings: sinusoidal, learned
Multi-head attention gives the same output for “321+654” and “123+456” shuffled into any order of keys: permuting the keys changes nothing. For addition that is fatal, because which digit is the ones digit is entirely a matter of position. The RNNs of the earlier chapters knew order for free, by reading one token at a time. The transformer reads all tokens at once and has to be told.
Concepts from other sections (7)
This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.
- Unit circle: the circle of radius 1 about the origin; the point at angle is . Read more: 6.2 Trigonometry, rotations, complex numbers, and Euler’s formula in Mathematics
- Geometric sequence: a sequence that multiplies by the same ratio at every step, . Read more: 6.3 Sequences, geometric series, and frequency ladders in Mathematics
- RoPE: rotary position embedding, which rotates queries and keys by position to encode relative position. Read more: 5.3 Model landscape and lifecycle
- embedding table: the matrix whose row is the learned vector for token , shared across every window. Read more: 8.2 Bengio’s neural probabilistic language model
- Half layout: pair is ; used by Hugging Face Llama-family checkpoints. Read more: 13.3 Rotary Position Embeddings
- absolute position: , the position of a query row in the whole sequence rather than in the current call. Read more: 22.5 FlashAttention forward in C
- ALiBi: a per-head bias that falls linearly with query-key distance, used instead of rotations. Read more: 13.4 Context Extension: PI, NTK, YaRN, Llama 3 Scaling, and ALiBi
This section builds the two ways this book tells it. The first is the fixed sinusoidal table of the 2017 transformer, used by the encoder-decoder Transformer at the end of this chapter, whose shift property is the seed of RoPE in the modern block. The second is the learned table of GPT-2 and BERT. You will see why the frequencies form a geometric ladder, prove that moving positions is a fixed rotation, and learn why the angles must be computed in float64.
The section draws on Mathematics: rotate_pairs from Trigonometry, rotations, complex numbers, and Euler’s formula, geometric from Sequences, geometric series, and frequency ladders, normal_init from Variance propagation and initialization, and PCG32 from Modular arithmetic, hashing, and PCG32. From this book it uses the Tensor, the embedding op from The op library and gradcheck_all, and Module from Module system and basic layers. The full solution is python/tinyllm/xfmr/pos.py (sinusoidal_freqs, sinusoidal_pe, shift_pe, SinusoidalPE, LearnedPE), and its contract is course/contracts/py/tinyllm/xfmr/pos.pyi.
Learning objectives
- Build the sinusoidal table with interleaved sin and cos columns and the frequency ladder .
- Prove that is a fixed block rotation of and implement
shift_pewith one call.- Explain why angles are computed in float64 and rounded to float32 once.
- Build a learned position table as an embedding whose rows get gradient only when used, and honour
offsetin both modules.
The notation
Section titled “The notation”| Symbol | Meaning | Type / shape |
|---|---|---|
| position, counted from 0 | int | |
| model width, even | int | |
| pair index, | int | |
| base, 10000 in the paper | float | |
| angular frequency of pair (radians per position) | float64[d/2] | |
| the encoding of position | float32[d] | |
| the 2D rotation by | float64[2, 2] |
Why add a vector at all
Section titled “Why add a vector at all”Attention is permutation equivariant, so order must enter through the input. The cheapest place is to add a vector that depends only on the position: . Adding (not concatenating) keeps the width that every later layer already expects.
The vector should stay bounded however long the sequence is, so raw is out. Points on a unit circle, , are bounded and distinct for nearby as long as is small enough not to wrap. But one circle cannot separate both neighbours and distant positions: a fast wraps, a slow one barely moves. The answer is circles at a geometric ladder of speeds.
The sinusoidal table
Section titled “The sinusoidal table”
The frequencies form a geometric sequence with first term 1 and ratio (the geometric helper). Pair 0 turns one radian per position and repeats every positions; the last pair turns about radians per position and repeats every , about 63 000 positions for . Like a clock with hands of geometric speeds, the fast pairs tell neighbours apart and the slow ones tell far positions apart; geometric spacing gives every scale of distance the same number of pairs.
Each pair lies on the unit circle, so for every : unlike adding itself, the signal never grows with the sequence. The columns are interleaved, sin at even columns and cos at odd ones. That is the layout of the paper and of rotate_pairs, which makes the shift law below a single call.
There is a precision trap. The angle for is about radians. float32 keeps about 7 significant digits, so the angle is off by roughly rad before the sine is even taken. The table is therefore computed in float64 and rounded to float32 at the end.
A shift is a rotation
Section titled “A shift is a rotation”The table’s most important property comes from the angle-addition formulas:
So is with pair turned by : one block-diagonal rotation that depends on and not on . shift_pe(pe, k, d) is rotate_pairs(pe, -k * omega).
Two consequences follow. A linear layer can learn to “look back” the same way at every position. And depends only on the distance. RoPE applies exactly this rotation to the queries and keys instead of adding it to the input.
Learned positions
Section titled “Learned positions”GPT-2 and BERT take the other route and learn a table (max_len rows), adding row at position . It is an embedding indexed by position, initialized , and only the rows actually used get gradient. It can learn any pattern, but it knows nothing about positions past : a model trained on 1024 tokens has no row 1024.
Offsets
Section titled “Offsets”Both modules add positions offset .. offset + T - 1 to an input of length . During training offset is 0. When decoding one token at a time with a KV cache, the new token sits at position offset, the number of tokens before it, and must get that row, not row 0.
Working it by hand
Section titled “Working it by hand”With and , the ladder is , and the first three rows are:
| 0 | 0 | 1 | 0 | 1 |
| 1 | 0.841471 | 0.540302 | 0.010000 | 0.999950 |
| 2 | 0.909297 | -0.416147 | 0.019999 | 0.999800 |
Now check the shift on pair 0 with . Applying to gives , which is the row.
Designing the API
Section titled “Designing the API”def sinusoidal_freqs(d: int, base: float = 10000.0) -> NDArray # float64 [d/2]def sinusoidal_pe(T: int, d: int, base: float = 10000.0, offset: int = 0) -> NDArray # float32 [T, d]def shift_pe(pe, k: float, d: int, base: float = 10000.0) -> NDArrayclass SinusoidalPE(Module): # no parameters; forward(x, offset=0) = x + table[offset : offset + T]class LearnedPE(Module): # weight [max_len, d]; positions(T, offset); forward(x, offset=0)The fixed table is data, not a parameter: storing it as a plain array keeps it out of the gradient and the state_dict. The learned table is the opposite, a parameter read through embedding, so that only the rows used get gradient. Both modules take offset so the same module serves training (offset 0) and cached decoding.
Building it step by step
Section titled “Building it step by step”The ladder
Section titled “The ladder”The ladder is the geometric sequence, with the validation that keeps the table meaningful:
def _check_d(d: int) -> None: # Pairs need an even width; an odd last column would have no partner. if not isinstance(d, (int, np.integer)) or d < 2 or d % 2: raise ValueError(f"d must be a positive even integer, got {d}")
def sinusoidal_freqs(d: int, base: float = 10000.0) -> NDArray: _check_d(d) # base <= 1 would make the ladder flat or increasing: no slow pairs. if not (np.isfinite(base) and base > 1.0): raise ValueError(f"base must be finite and > 1, got {base}") # omega_i = base^(-2i/d): first term 1, ratio base^(-2/d) (M00.3). return geometric(1.0, float(base) ** (-2.0 / d), d // 2)The ratio is , not : with terms the last one is , so the slowest pair repeats every positions.
The table
Section titled “The table”The table is an outer product of positions and frequencies, then sin and cos written into interleaved columns:
def sinusoidal_pe(T: int, d: int, base: float = 10000.0, offset: int = 0) -> NDArray: if T < 0 or offset < 0: raise ValueError(f"T and offset must be non-negative, got {T}, {offset}") w = sinusoidal_freqs(d, base) # Angles in float64: at p = 10^4 a float32 angle is off by about 5e-4 rad. # Positions start at `offset` (0 in training), counted from 0, not 1. ang = np.arange(offset, offset + T, dtype=np.float64)[:, None] * w[None, :] pe = np.empty((T, d), dtype=np.float64) pe[:, 0::2] = np.sin(ang) # PE(p, 2i) pe[:, 1::2] = np.cos(ang) # PE(p, 2i + 1) return pe.astype(np.float32) # round once, after the trigThe shift
Section titled “The shift”The shift is one call, because the layout matches rotate_pairs:
def shift_pe(pe: ArrayLike, k: float, d: int, base: float = 10000.0) -> NDArray: x = np.asarray(pe) if x.shape[-1] != d: raise ValueError(f"pe has width {x.shape[-1]}, expected d = {d}") # The pair (sin a, cos a) turned by -k*omega is (sin(a + k omega), # cos(a + k omega)): a rotation (M00.2) that depends on k, not on p. return rotate_pairs(x, -float(k) * sinusoidal_freqs(d, base))rotate_pairs(x, θ) maps a pair to . With , and the first entry is : the minus sign is what moves forward in position. Each pair gets its own angle ; one shared angle would scramble the slow pairs.
The fixed module
Section titled “The fixed module”The fixed module precomputes max_len rows once and slices:
class SinusoidalPE(Module): def __init__(self, max_len: int, d: int, base: float = 10000.0) -> None: super().__init__() # ... # A constant, not a parameter: no gradient, not in the state_dict. self.table = sinusoidal_pe(max_len, d, base)
def forward(self, x: Tensor, offset: int = 0) -> Tensor: T = x.shape[-2] # ... width check ... if offset < 0 or offset + T > self.max_len: # Refuse instead of wrapping: a wrapped slice would silently reuse rows. raise ValueError(...) # Tensor + ndarray: the gradient flows to x and nowhere else. return x + self.table[offset : offset + T].astype(x.dtype)The learned module
Section titled “The learned module”The learned module is an embedding table indexed by position:
class LearnedPE(Module): def __init__(self, max_len: int, d: int, std: float = 0.02, rng: Any = None) -> None: super().__init__() # ... r = rng if rng is not None else PCG32(0).substream("init") self.max_len, self.d = max_len, d # N(0, std^2) from the seeded stream (M07.3): the same init in every language. self.weight = Tensor(normal_init((max_len, d), std, r), requires_grad=True)
def positions(self, T: int, offset: int = 0) -> Tensor: # ... range check against max_len ... # Rows offset .. offset+T-1 of the table, with their gradient. return F.embedding(self.weight, np.arange(offset, offset + T))
def forward(self, x: Tensor, offset: int = 0) -> Tensor: # ... width check ... return x + self.positions(x.shape[-2], offset)F.embedding is a gather whose backward scatters into exactly the rows read, so rows past the sequence length stay at their initial values. positions is public because GPT-2 and BERT want the position rows on their own. The full solution is pos.py.
Why this design
Section titled “Why this design”Building the table is once; each forward is a slice and an add, . The learned table costs parameters and its gradient touches only rows.
fairseq and some HF code use a half layout: all sines first, then all cosines. That is equally valid but needs a different rotation helper. Interleaving matches the paper and the rotation helper, so the shift law reuses existing code.
Computing rows per call would allow unbounded positions, but costs trig on every step; a fixed max_len matches the learned table’s contract and makes “past the table” a clear error. Computing in float32 would be faster but wrong at long positions; the cost of float64 is paid once.
There is one tradeoff accepted here: neither encoding extrapolates past max_len. The rotation property is the seed of RoPE, which removes that limit for relative distances.
What can go wrong
Section titled “What can go wrong”A correct solution must reproduce the worked table (frequencies (1, 0.01) and the three rows) so everyone agrees on layout and ladder, and the worked shift (turning PE(1) by gives PE(2)) to pin down the rotation’s sign. At long positions, the table at positions up to 65535, for two settings, must match a 50-digit mpmath reference, so long contexts keep exact positions. The properties argued above must hold: one fixed for every , negative included (the relative-position property RoPE builds on); ; every pair has norm 1; and offset gives the tail of a longer table, which the module honours.
For the modules, the sinusoidal module has an empty state_dict and passes gradient through to ; the learned module adds rows offset.. and only those rows get gradient; and its init is a seeded with default std 0.02, the same in every language. Odd , a base , and positions past the table must raise, so nothing wraps around silently. The model suite test_pos.py uses the formula itself as the oracle (Python’s math in float64, including positions ).
The common mistakes and how the code avoids each:
| Pitfall | Symptom | How the solution avoids it |
|---|---|---|
| sin and cos concatenated (or swapped) instead of interleaved | the shift law fails; checkpoints from other code disagree | writes pe[:, 0::2] = sin, pe[:, 1::2] = cos |
| exponent instead of | the ladder ends at : far positions alias | ratio base ** (-2.0 / d) over d // 2 terms |
| angles in float32 | rows past a few thousand off in the 4th decimal | angles built from a float64 arange, cast to float32 only at the end |
| the shift turned the wrong way, or by the same angle for every pair | shift_pe goes back in position, or scrambles pairs | passes the per-pair vector -k * omega to rotate_pairs |
| positions counted from 1 | every row one late; HF and torch disagree | np.arange(offset, offset + T) starts at 0 when offset = 0 |
offset ignored | incremental decoding gives every new token position 0 | both modules slice rows offset .. offset + T - 1 |
| the fixed table registered as a parameter | it trains and lands in the checkpoint | self.table is a plain ndarray |
| learned rows read as constants | wpe never learns | rows gathered with F.embedding(self.weight, ...) |
std ignored by the learned table | every table starts at 0.02 | normal_init((max_len, d), std, r) uses the argument |
| an odd width accepted | the last feature has no partner | _check_d rejects odd or tiny d |
Where this leads
Section titled “Where this leads”The encoder-decoder Transformer applies SinusoidalPE to both the source and the target embeddings; this is how the chapter’s addition model reads digit positions. LearnedPE is GPT-2’s wpe and BERT’s position embedding. RoPE rotates queries and keys by the same ladder instead of adding a vector, and Whisper decoding calls this code through its declared contract.
The production equivalents:
| Solution piece | Production equivalent | What it adds | Where to look |
|---|---|---|---|
sinusoidal_pe | fairseq SinusoidalPositionalEmbedding | the half layout (all sin, then all cos) and a padding offset | fairseq/modules/sinusoidal_positional_embedding.py |
shift_pe | RoPE | the rotation applied inside attention, so scores depend on only | RoPE; Su et al. 2021 |
LearnedPE | HF GPT2Model.wpe, BertEmbeddings.position_embeddings | absolute positions capped at n_positions | transformers/models/gpt2/modeling_gpt2.py |
| no position at all | ALiBi, NoPE | a distance penalty on scores, or causality alone, for length extrapolation | Press et al. 2022; Kazemnejad et al. 2023 |
Further reading
Section titled “Further reading”- Vaswani et al., “Attention Is All You Need” (2017), section 3.5.
- Kazemnejad, “Transformer Architecture: The Positional Encoding” (2019).
- Su et al., “RoFormer” (2021), for where the rotation goes next.
Key terms
Section titled “Key terms”- positional encoding: a vector depending only on position, added to the token embedding so attention can see order.
- frequency ladder: the geometric sequence of angular speeds, from 1 down to about .
- interleaved layout: sin at column and cos at column , the pair layout of the paper.
- shift law: equals with each pair rotated by , independent of .
- learned position table: an embedding of
max_lenrows indexed by position, trained like any parameter. - offset: the absolute position of the first input token, nonzero during cached decoding.
Summary
Section titled “Summary”- Attention sees its keys as a set, so position must be added to the input: .
- Pair of is the point with , a geometric ladder from 1 radian per position down to about .
- Moving positions is a fixed rotation of every pair, the same for every , so depends only on .
- Angles must be computed in float64: at a float32 angle is already off in the fourth decimal.
- A learned table is just an embedding indexed by position: it adds rows
offset ..and only those rows get gradient.
Review questions
Section titled “Review questions”- Why must the ratio of the ladder be and not ?
Answer
There are $d/2$ frequencies. With ratio $b^{-2/d}$ the last is about $b^{-1}$, so the slowest pair has period $2\pi b$. With $b^{-1/d}$ the ladder stops at about $b^{-1/2}$, the slowest period is only $2\pi\sqrt b \approx 630$ positions, and far positions alias. - Why is the shift a rotation by rather than ?
Answer
The pair is stored as (sin, cos), not (cos, sin). `rotate_pairs` with angle $\theta$ sends $(\sin a, \cos a)$ to $(\sin(a - \theta), \cos(a - \theta))$, so moving forward by $k\omega$ needs $\theta = -k\omega$. - Why is
SinusoidalPE.tablea plain array and not aTensorwithrequires_grad?Answer
It is a fixed function of position. As a parameter it would be updated by the optimizer (no longer sinusoidal, so the shift law breaks) and saved in every checkpoint for no reason. - What goes wrong in cached decoding if
offsetis ignored?Answer
Each new token is fed alone, so $T = 1$, and without `offset` it always gets row 0. Every generated token then looks like the first token of the sequence. - Why does
LearnedPEread its rows throughF.embeddinginstead of slicingweight.data?Answer
Slicing the raw array would give a constant with no path back to `weight`, so the table would never learn. `embedding` records the gather and scatters the gradient into the rows used.