Modern PE (Deep Dive)

P10.positional-encoding.03 · Audience: it-ml · Prerequisites: Modern Positional Encodings

Customise the corpus these labs use

Beyond sinusoidal PE: how RoPE and ALiBi address its limitations in modern large language models — with the actual matrices, computed live.

Step 1 / 4Why Sinusoidal PE Has Limits
🗣️ From a linguist's perspective: long-discourse reference
An interpreter tracks reference across an hour of speech by relative recency — “the treaty mentioned a moment ago” — not by absolute minute marks. A system trained only on absolute timestamps up to minute 32 has no reliable reading for minute 200.
ⓘ Concept: Two limitations of sinusoidal PE
1. Out-of-distribution lengths. Beyond the training length the sinusoids keep producing well-defined values the model never learned to interpret. 2. Absolute, not relative. The encoding says “this is position 7”, while language mostly needs “these two words are 3 apart”.

Why it matters — These two gaps — poor length extrapolation and absolute-only position — are precisely what RoPE and ALiBi were designed to close.

Recall — the sinusoidal formulas (P1)
PE[pos,2i]=sin ⁣(pos100002i/d),PE[pos,2i+1]=cos ⁣(pos100002i/d)\text{PE}[\text{pos}, 2i] = \sin\!\left(\tfrac{\text{pos}}{10000^{2i/d}}\right),\qquad \text{PE}[\text{pos}, 2i+1] = \cos\!\left(\tfrac{\text{pos}}{10000^{2i/d}}\right)
🎓 Interview deep-dive

Extending context length. ALiBi extrapolates natively. RoPE models stretch further via position interpolation (compress positions into the trained range), NTK-aware scaling, or YaRN. Orthogonally, sparse/sliding-window attention (Longformer, BigBird, Mistral's SWA) and ring attention cut the O(n²) cost. The recurring trade-off: long-range faithfulness vs compute and memory.

📚 Go further

Check your understanding

Name one limitation of fixed sinusoidal PE on sequences longer than training.

Which method rotates Q and K inside the attention dot product?

Does ALiBi make attention to distant tokens more or less likely?

Which method has no learned parameters AND a linear distance penalty?

After RoPE, does the Q·K dot product depend on absolute or relative position?

Ask the mentor about this module

Ask a question about this content. The mentor explains and grounds its answer in what you are studying; asking is recorded as a learning signal, not a grade.

Ctrl/Cmd + Enter to send

Try it yourself

A scratch console for this page's ideas — ungraded, nothing you run here is recorded.

Scratch console

A scratch console with the scientific stack (pandas, numpy, scikit-learn). Runs on the server — no network, resource-limited and measured.

Output appears here.

Where next?