Positional Encoding
Volume II, Chapter 8 — Part II (Chapter 12). Sinusoidal positional encodings from relative-shift desiderata, learned embeddings, RoPE, ALiBi, and length extrapolation theory for Transformers.
Prerequisites
Table of Contents
- Learning Objectives
- Prerequisites
- Notation
- Core Intuition
- Permutation Equivariance of Attention
- Desiderata for Positional Encodings
- Sinusoidal Encoding: Derivation
- Properties and Proofs
- Absolute Position Injection
- Learned Position Embeddings
- Rotary Position Embeddings (RoPE)
- ALiBi: Attention with Linear Biases
- Comparison and Extrapolation
- Worked Examples
- Connection to the Broader Curriculum
- Common Pitfalls and Misconceptions
- Research Perspective
- Summary of Takeaways
- Exercises
Learning Objectives
After reading this chapter, you should be able to:
- Prove that Self-Attention is permutation-equivariant and explain why position information must be injected.
- State desiderata for positional encodings (uniqueness, bounded norm, relative expressibility, extrapolation).
- Derive sinusoidal encodings from the relative-shift property using rotation matrices.
- Prove that depends only on .
- Contrast absolute (sinusoidal, learned) vs. relative (RoPE, ALiBi) position methods.
- Explain why RoPE dominates modern LLMs and how it connects to RoPE in LLMs.
Prerequisites
- Self-Attention — attention formula, Q/K/V
- Eigenvalues & Eigenvectors — rotation matrices (optional)
Notation
- — Sequence length
- — Model (embedding) dimension
- — Sinusoidal encoding at position
- — Positional encoding matrix
- — Position-augmented embeddings
Core Intuition
Self-Attention computes outputs as similarity-weighted sums of values. Dot products are invariant to permutations of token order — the model treats input as an unordered set. Language, code, and time series require order: "dog bites man" "man bites dog".
Positional encodings break permutation symmetry by making each token's representation depend on its index . Methods range from adding position vectors to embeddings (sinusoidal, learned) to encoding position in attention itself (RoPE, ALiBi).
Modern LLMs overwhelmingly use RoPE (RoPE), which embeds relative position directly in query–key products — compatible with KV Cache and length extrapolation.
Series context. Chapter 8, Part II (Chapter 12 in full curriculum) in Volume II.
Positional Encoding
Permutation Equivariance of Attention
Theorem 1 (Permutation Equivariance). Let be the permutation matrix for permutation . Then:
Proof. . Softmax is row-wise; permuting rows and columns consistently preserves structure. .
Corollary 1. Without position information, shuffling input tokens shuffles outputs identically — the model cannot use order.
Desiderata for Positional Encodings
Definition 1 (Positional Encoding). A function maps position to a -dimensional vector.
Desiderata:
- D1 (Uniqueness):
- D2 (Bounded norm): bounded as
- D3 (Relative shift): linear map with for all
- D4 (Smoothness): varies smoothly in
- D5 (Extrapolation): Defined for (training max length)
Sinusoidal Encoding: Derivation
Seek , satisfying D3.
Lemma 1 (Angle Addition).
The matrix is a rotation depending only on offset .
Definition 2 (Sinusoidal PE, Vaswani et al.).
Frequencies : low → high frequency (fine position); high → low frequency (coarse position).
Properties and Proofs
Proposition 1 (Constant Norm). for all .
Proof. .
Theorem 2 (Relative Position in Dot Product).
Proof. .
Attention dot products encode relative distance .
Absolute Position Injection
Definition 3 (Absolute PE). Input to the Transformer:
Position is added before the first attention layer. Subsequent layers propagate position implicitly through representations.
Learned Position Embeddings
Definition 4 (Learned PE). Learn matrix ; row is position .
- Parameters — 0 —
- Extrapolation — Partial — Fails beyond
- Relative structure — Via dot product — Not guaranteed
Used in BERT, GPT-2. Works well within training length; fails catastrophically beyond.
Rotary Position Embeddings (RoPE)
Definition 5 (RoPE). Apply position-dependent rotation to Q and K:
where is block-diagonal with rotations at frequencies .
Theorem 3 (Relative Position in RoPE).
Proof. (rotation composition).
Full treatment in RoPE. Used in LLaMA, Mistral, Gemma.
ALiBi: Attention with Linear Biases
Definition 6 (ALiBi). Add linear penalty to attention scores:
with head-specific slopes .
Properties: Zero extra parameters; explicit locality bias; extrapolates to longer sequences. Used in BLOOM, MPT.
Comparison and Extrapolation
- Sinusoidal — Via dot product — Partial — 0 — Original Transformer
- Learned — No — No — — BERT, GPT-2
- RoPE — Native — Yes (with scaling) — 0 — LLaMA, modern LLMs
- ALiBi — Bias — Yes — slopes — BLOOM
NTK-aware scaling extends RoPE to longer contexts by rescaling frequencies at inference — see RoPE.
Worked Examples
Example 1: PE Norm
For , for all — positions contribute fixed energy.
Example 2: Relative Dot Product
— depends only on distance 3.
Example 3: RoPE Attention
Positions 10 and 13 differ by 3; same attention score as positions 100 and 103.
Connection to the Broader Curriculum
- Self-Attention — base mechanism
- RoPE — deep dive for LLMs
- KV Cache — RoPE compatible with caching
- Scaling Laws — context length scaling
Common Pitfalls and Misconceptions
Pitfall 1: Assuming attention encodes order without PE.
Pitfall 2: Using learned PE beyond .
Pitfall 3: Confusing absolute PE addition with RoPE rotation.
Pitfall 4: Ignoring position in cross-attention (encoder–decoder).
Research Perspective
Sinusoidal PE (Vaswani et al., 2017). RoPE (Su et al., 2021) now standard. ALiBi (Press et al., 2022). Long-context research: YaRN, NTK scaling, position interpolation.
Summary of Takeaways
- Problem — Attention is permutation-equivariant
- Sinusoidal — Rotation-based relative shift
- Dot product — Depends on
- RoPE —
- Modern choice — RoPE for autoregressive LLMs
Next: Variational Autoencoders
Exercises
Exercise 1. Prove Theorem 1 (permutation equivariance).
Exercise 2. Derive (2) from angle addition formulas.
Exercise 3. Prove Theorem 2 (relative dot product).
Exercise 4. Prove Theorem 3 (RoPE relative attention).
Exercise 5. Compare parameter counts: learned PE vs. RoPE for , .
Exercise 6. Why does ALiBi use different slopes per head?
Exercise 7. Explain failure mode of learned PE at 2× training length.
Exercise 8. Connect RoPE rotations to Eigenvalues of rotation matrices.