Phase 7 · Transformers/
Lesson 7.3core3 h⚡ live demo

Positional information: sinusoidal, learned, RoPE, ALiBi

Attention cannot see order — here is every way we give it one

Self-attention is permutation-equivariant: without extra information, "dog bites man" and "man bites dog" are the same input. How positions are injected decides whether a model can handle sequences longer than it saw in training — the question behind every "1M-token context" announcement. Rotary embeddings (RoPE) are used by essentially every open model you will fine-tune (8.2 Fine-tuning and PEFT) or serve (7.10 Efficient inference: KV cache, GQA, FlashAttention, speculative decoding, quantization), and context extension methods build directly on the math here (7.12 Beyond vanilla attention: state space models, long context and hybrids).

You'll be able to

  • Show that attention is permutation-equivariant and explain why positions must be injected
  • Write the sinusoidal encoding and prove its relative-shift (rotation) property
  • Derive how RoPE makes query–key scores depend only on relative position and implement it
  • Compare learned, sinusoidal, relative-bias, RoPE and ALiBi schemes on extrapolation and explain context-extension tricks

Key ideas

  1. Self-attention is permutation-equivariant; order must be injected either into the inputs or into the attention scores.
  2. Sinusoidal encodings are a bank of rotating 2-D clocks; a fixed offset k is a fixed rotation R_k, so relative position is linearly recoverable.
  3. Learned absolute position tables cannot represent positions beyond the training length.
Show 4 more key ideas
  1. RoPE rotates each query/key pair by an angle proportional to its position; the dot product then depends only on the relative offset n − m.
  2. ALiBi adds a per-head linear distance penalty to the scores and extrapolates to longer sequences without any position embeddings.
  3. Context extension (position interpolation, NTK scaling, YaRN, larger RoPE base) rescales positions or frequencies so unseen lengths map into the trained range.
  4. RoPE touches only q and k, never v — values carry content, positions only shape who attends to whom.

Suggested path through this lesson