Learn: Positional Information (Sinusoidal → RoPE → ALiBi)
What you're learning
Self-attention has no idea what order its tokens are in — you must inject that. This lesson walks the four canonical ways to do it (sinusoidal, learned, RoPE, ALiBi), building from the intuition of why order matters, to the math of each scheme, to why RoPE became the modern default and how it (and ALiBi) unlock long-context extrapolation. By the end you should be able to derive the RoPE relative-position identity from scratch and explain the absolute-vs-relative distinction mechanistically.
This lesson assumes 01-scaled-dot-product-attention and 02-multi-head-attention-and-the-block — you should already be comfortable with , the softmax, and the multi-head block. Here we ask the question those lessons deferred: where does position come from?
1. Learning map
graph TD A["Self-attention is<br/>permutation-equivariant<br/>(no order info)"] --> B["Must inject position"] B --> C["Absolute<br/>(tag each position)"] B --> D["Relative<br/>(what matters is offset m-n)"] C --> C1["Sinusoidal<br/>(Vaswani 2017)"] C --> C2["Learned table<br/>(GPT/BERT)"] C2 --> C3["⚠ can't extrapolate<br/>past L_max"] C1 --> D D --> E["RoPE<br/>rotate Q,K by position<br/>(Su 2021)"] D --> F["ALiBi<br/>linear distance penalty<br/>(Press 2021)"] E --> G["Modern default<br/>+ context extension"] F --> H["Strong length<br/>extrapolation"] G --> I["→ long-context race<br/>(lesson 09)"] H --> I style A fill:#a44,color:#fff style E fill:#44a,color:#fff style F fill:#483,color:#fff style I fill:#a83,color:#fff
2. Intuition: attention can’t see order
Recall the core of lesson 01: for a single head, the output of position is . Look closely — nowhere does the index appear, only the content vectors . The sum is over a set of key/value pairs; sets have no order.
Formally, self-attention is permutation-equivariant: permute the input tokens by any , and the outputs come out permuted the same way, with identical values. There is no way for the mechanism to distinguish
“dog bites man” from “man bites dog”
because both contain the same bag of token embeddings dog, bites, man — only the positions differ, and attention is blind to positions.1 That is fatal for language: meaning lives in order. So we must inject positional information into the representations before (or during) attention.
Two philosophies for doing so:
- Absolute — give each position its own signature (a vector, or a learned tag) and stamp it onto the token there. The model learns “position 5-ness,” “position 6-ness,” etc.
- Relative — the linguistically meaningful quantity is usually the offset between a query at and a key at (“the adjective two words back”), not the absolute slot. A relative scheme makes the attention score depend on directly.
The distinction that organizes everything below
Absolute schemes answer “where am I?”; relative schemes answer “how far apart are we?”. Sinusoidal and learned encodings are absolute (though sinusoidal has a latent relative structure). RoPE and ALiBi are relative. The field moved decisively toward relative because it generalizes to lengths and offsets never seen in training — the crux of long context.
Quiz: Why does adding a positional signal before the attention matmul break permutation-equivariance, and what property of the raw attention sum made the model order-blind in the first place?
Answer
What made it blind: the output with depends only on the content of the vectors, never on the indices . Permuting the tokens permutes the terms of the sum but leaves each term’s value unchanged — a sum is invariant to the order of its addends — so the operation is equivariant to any permutation. Why injection fixes it: once you add a position-dependent vector to token (or rotate/bias by ), two tokens with identical content at different positions now carry different vectors, so (and hence ) changes when you reorder them. The set becomes effectively a sequence: each element’s representation now encodes where it sits.
3. Math
Setup: token embeddings ; queries/keys (head dim ; ignore multi-head indexing).
3.1 Sinusoidal absolute (Vaswani et al., 2017)
The original Transformer adds a fixed (non-learned) positional vector to each token embedding: . The signature is a bank of sinusoids at geometrically-spaced frequencies. For position and dimension pair index :1
Frequencies sweep from (fast, wavelength ) down to (slow, wavelength ). Think of it as a binary-clock-in-continuous-form: low dims tick fast, high dims tick slowly, so the full vector uniquely identifies position over a huge range.
Why sinusoids specifically? Because a shift by a fixed offset is a linear map on each 2D pair — the rotation identity
means is a linear function of . So the model can, in principle, learn to attend by relative offset. This latent relative structure is exactly the seed RoPE later makes explicit and exact. It’s absolute in form, weakly relative in spirit — and being parameter-free, it can be evaluated at any , so it nominally extrapolates (though in practice performance still degrades well past the training length).
3.2 Learned absolute (GPT / BERT style)
Instead of a fixed formula, learn a position embedding table and add the row for each slot: , with trained by backprop like any other parameter. This is what original GPT and BERT use. It’s maximally flexible — the model discovers whatever positional geometry the data wants — and costs one embedding lookup.
Learned absolute encodings cannot extrapolate past
The table has exactly rows. At inference, a position has no row — the encoding is literally undefined, so the model simply cannot run on longer sequences without adding untrained parameters. Even within range, rows for rarely-seen large positions are undertrained. This hard length ceiling is the central practical failure of learned absolute encodings and a primary reason the field moved to relative schemes (RoPE/ALiBi) that are defined for any offset. If you only remember one drawback of any scheme in this lesson, remember this one.
3.3 RoPE — Rotary Position Embedding (Su et al., 2021)
Idea: don’t add a position vector — rotate each query and key by an angle proportional to its position, so that when you take the dot product , the two rotations combine into a single rotation by the offset . Position enters multiplicatively, and the score comes out depending only on relative distance — while each vector is still stamped with its absolute position individually.
Work in 2D first. Split the head dimension into coordinate pairs; each pair gets a frequency (base by default, same bank as sinusoidal). Define the per-pair rotation matrix
and let be the block-diagonal rotation that spins every pair of (or ) by its own angle . RoPE replaces and .2
The relative-position identity. The attention logit is the dot product of the rotated vectors:
Now use the defining property of 2D rotations: , and rotations by angles add, . Therefore, block-by-block,
and the logit collapses to
(up to the sign convention on the offset; ). The absolute positions and vanished — only their difference survives. That is the whole trick: absolute rotations applied independently to and produce a score that is a function of relative offset, because the dot product multiplies by and rotations compose by adding angles.
Why RoPE is the modern default
- Relative for free, no extra params, no extra terms. It’s applied inside (a fixed rotation, zero learned parameters), so it needs no bias-add and no attention-score modification — it drops into any attention/FlashAttention kernel.
- Norm-preserving. Rotations are orthogonal, so — RoPE never changes vector magnitudes, only phases. Clean and numerically benign.
- Multi-frequency = multi-scale. Fast pairs resolve nearby offsets, slow pairs carry long-range position — one vector spans local and global structure.
- Context extension. Because position is a continuous angle, you can extend the usable context at (or after) train time by manipulating the frequencies — e.g. linear position interpolation (scale ) or NTK-aware / YaRN base- scaling (raise ) so the fastest pairs don’t alias at unseen distances. This is why nearly every modern open LLM (LLaMA, Qwen, Mistral, DeepSeek, …) uses RoPE and ships long-context variants by retuning . We unpack this in 09-long-context-race.
Quiz: Why does RoPE yield a relative score even though the rotation is applied using each token's absolute position? (The one to really nail.)
Answer
Because the two absolute rotations meet inside a dot product, and the dot product transposes one of them. RoPE forms and independently using absolute positions . The logit is . For rotation matrices, and angles add, so — a rotation by the difference of the two absolute angles. The absolute parts cancel exactly the way cancels: each vector carries absolute phase, but the interaction keeps only the phase difference. So each token is still individually tagged with its absolute position (useful, and it’s what lets you interpolate positions), yet every attention logit depends only on . Absolute stamping, relative interaction.
3.4 ALiBi — Attention with Linear Biases (Press et al., 2021)
RoPE bakes relativity into . ALiBi takes an even simpler route: leave (and embeddings) untouched — inject no positional vectors at all — and instead add a static, non-learned linear penalty to the attention scores that grows with distance. For a query at and key at (causal, so ):3
where is the distance and is a fixed per-head slope. The bias is at zero distance and becomes increasingly negative the further back the key is — a soft recency prior that says “attend less to far-away tokens, linearly.” Slopes are set as a fixed geometric sequence across heads (e.g. for 8 heads, : ), so different heads look back at different length scales — some sharply local, some nearly flat/global.
Because the bias is a pure function of the offset (defined for any distance) with no learned or stored position representation, ALiBi is inherently relative and extrapolates strongly: the original paper trained on length 1024 and matched or beat sinusoidal/RoPE baselines when evaluated at much longer lengths (e.g. 2048+), with essentially no length ceiling.3 The trade-off vs. RoPE: the linear penalty is a strong inductive bias toward recency rather than a fully general position representation, and it’s most natural for decoder (causal) attention.
Two ways to be relative
RoPE and ALiBi both make the score depend on , but at different points in the computation. RoPE modifies the operands ( are rotated) before the matmul — it’s a rich, phase-based relative encoding. ALiBi modifies the scores after the matmul with a scalar additive term — it’s a cheap, monotone recency bias. RoPE preserves full directional information at each offset; ALiBi throws all offset structure into a single slope per head. RoPE dominates as the general-purpose default; ALiBi remains a clean, extrapolation-friendly choice and inspired many learned-relative-bias variants (e.g. T5’s relative bias).
4. Illustration: RoPE as position-dependent 2D rotation
Each 2D coordinate pair of (and ) is spun by an angle proportional to its position; higher-frequency pairs spin faster. The dot product then depends only on the angle between the rotated query and key — i.e. on .
Figure 1 — Left: within one 2D coordinate pair, the query is rotated by and the key by ; their dot product depends only on the angle between them, — the relative offset. Right: the head dimension is split into pairs, each with its own frequency , so fast pairs encode fine local offsets and slow pairs encode coarse global position. Absolute rotation per token, relative interaction under the dot product.
5. FLOPs & notes: cheap to compute, huge for long context
The headline: every positional scheme here adds negligible FLOPs, yet the choice dominates long-context behavior.
- Sinusoidal / learned absolute: one add of a length- vector per token — , a rounding error next to the attention matmuls. Learned costs one extra embedding lookup.
- RoPE: rotating and is, per 2D pair, 4 multiplies + 2 adds (or via the complex/half-split trick, a handful of elementwise ops). Total elementwise work on only — no change to the score/output matmuls, and no extra HBM matrix. It’s typically fused into the attention kernel (FlashAttention exposes a RoPE-on-the-fly path), so its runtime cost is essentially free.
- ALiBi: adding a scalar to each of the scores is adds — same order as the softmax’s own elementwise work, still trivial beside the matmul FLOPs, and it stores no bias matrix (the bias is computed from indices on the fly).
Negligible FLOPs, decisive consequences
Do not mistake “cheap to compute” for “unimportant.” The positional scheme sets the model’s length-generalization ceiling. Learned-absolute hard-caps at . Sinusoidal degrades past training length. RoPE’s behavior at unseen distances is governed by the base (base ): the slowest pair must not complete a full turn within the context, or distant positions alias — which is exactly why long-context recipes raise the RoPE base (NTK-aware scaling) or interpolate positions (linear PI, YaRN) so the frequency bank still resolves the extended range. ALiBi sidesteps this with its parameter-free recency bias. The FLOPs are a footnote; the extrapolation semantics are the whole story.
This is the doorway to 09-long-context-race, where RoPE-base scaling, position interpolation, YaRN, and ALiBi/relative-bias variants are the main characters in the race to million-token context. It also connects back to the quadratic cost of attention itself — the reason long context is hard in the first place — which is the subject of the next lesson.
Quiz: Learned-absolute costs one lookup; RoPE costs a per-token rotation; ALiBi costs an add. All are negligible vs. the matmuls. So on what axis do they actually differ, and which fails first when you double the inference length past training?
Answer
They differ on length generalization, not compute. Doubling inference length past training: learned-absolute fails hardest and first — positions have no embedding row at all, so it can’t even run without new parameters. Sinusoidal runs (the formula is defined for any ) but quality degrades beyond the trained range. RoPE runs and degrades more gracefully, and — crucially — can be made to extrapolate by scaling its base or interpolating positions (NTK/YaRN/PI), because position is a continuous rotation angle. ALiBi extrapolates most readily out-of-the-box thanks to its parameter-free monotone distance penalty. So the ranking on “survives length extrapolation” is roughly ALiBi ≈ RoPE(+scaling) > sinusoidal > learned-absolute — the inverse of how much each hard-codes a fixed position table.
Quiz: Sinusoidal encoding is called "absolute," yet the Transformer paper motivates it partly by a relative argument. What is that argument, and how does RoPE turn the same idea from "possible in principle" into "exact by construction"?
Answer
The sinusoidal relative argument: for any fixed offset , is a linear function of — each 2D pair maps under a shift by the fixed rotation . So a linear projection in the attention could, in principle, learn to detect relative offsets. But it’s only latent: the encoding is added to the embedding and the model must learn to exploit the structure; nothing forces the score to depend solely on . RoPE makes it exact: instead of adding sinusoids and hoping the model uses the linearity, it applies the rotation directly to and . Then the dot product depends on by construction, for every query-key pair, with no learning required. RoPE is the sinusoidal relative property promoted from an emergent possibility to a structural guarantee.
6. Practice problems
Practice problem 1: derive the RoPE offset identity for one 2D pair
Problem: Take a single 2D coordinate pair with frequency . Let . Show that depends on only through , and identify the resulting expression.
Worked solution: Expand . Rotation matrices are orthogonal with , and they compose additively, . Hence . So the product equals — a function of the offset only. Writing it out: with and ,
i.e. a linear combination of the dot product and the 2D cross-product, weighted by and of the relative angle . The absolute positions have completely cancelled. Stacking such pairs (each with its own ) gives full-dimensional RoPE, with the total logit — a sum of relative-offset terms across frequencies.
Practice problem 2: choose a RoPE base for a target context length
Problem: You train with RoPE base , head dim , on context length , and want to serve without retraining aliasing badly. (a) Which frequency pair is most at risk of aliasing at long range, and why? (b) Qualitatively, what does raising the base (NTK-aware scaling) do, and what does linear position interpolation () do instead? (c) Which pairs does each method mainly protect?
Worked solution: (a) The slowest pair, , with . Its wavelength is tokens — barely longer than . If any pair’s phase exceeds what was seen in training (each pair’s max trained angle is ), the model meets rotations it never learned to read; the slow pairs, which carry coarse global position, are where extrapolation breaks. (Conversely the fastest pairs already wrap many times even within , so they’re “used to” full-circle phases.) (b) Raising shrinks every , i.e. slows all rotations, so a given position advances less phase — the highest-frequency pairs in particular stop completing extra turns at long range (NTK-aware scaling stretches the frequency spectrum, targeting the fast end while barely touching the slow end). (c) Linear PI rescales position itself ( with ), so every pair’s phase is uniformly compressed back into the trained range — it protects all pairs uniformly but blurs fine local resolution. NTK/base scaling stretches the spectrum so the fast pairs don’t over-rotate while preserving low-frequency (global) resolution better than uniform PI. In practice YaRN blends the two (interpolate low freqs, extrapolate high freqs). Full treatment in 09-long-context-race.
7. Primary source & going deeper
- Read the RoPE paper first (primary source): Su, Lu, Pan, Murtadha, Wen, Liu — “RoFormer: Enhanced Transformer with Rotary Position Embedding” — arXiv:2104.09864.2 Work §3.2–3.4: the 2D formulation, the general block-diagonal rotation, and the relative-position derivation you reproduced above. Their complex-number view ( as ) is the cleanest way to see why the phases subtract.
- Sinusoidal: Vaswani et al., “Attention Is All You Need” — arXiv:1706.03762, §3.5.1
- ALiBi: Press, Smith, Lewis — “Train Short, Test Long: Attention with Linear Biases Enables Input Length Extrapolation” — arXiv:2108.12409.3
Ask the teacher
This lesson compresses a lot. If any step didn’t land — the rotation-composition algebra, why raising the RoPE base helps, how ALiBi slopes are chosen, or how RoPE is fused into a FlashAttention kernel — ask me for a follow-up. Good targets: “walk me through YaRN vs. NTK vs. linear PI,” “show RoPE in the complex-exponential formulation,” or “why does ALiBi struggle in bidirectional/encoder attention?“
8. What’s next
- 04-quadratic-bottleneck-and-io-aware (next) — now that tokens know where they are, confront why attention is expensive: the score matrix, the memory wall, and the IO-aware (FlashAttention) fix. Positional schemes ride inside that kernel (RoPE fused into ; ALiBi as a score bias), so the two lessons interlock.
- 09-long-context-race (forward tease) — RoPE-base scaling, position interpolation, YaRN, and ALiBi/relative-bias variants as the levers in the million-token-context race.
- 02-multi-head-attention-and-the-block (back) — reread the head/block structure knowing that RoPE is applied per head to after projection, before the score matmul.
Reference topic: attention-mechanisms | Glossary: attention-glossary | Concepts: rope, alibi, positional-encoding | Filed: 2026-09-10
References
Footnotes
-
[established] Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N. Gomez, Łukasz Kaiser, Illia Polosukhin, “Attention Is All You Need” — arXiv:1706.03762 (NeurIPS 2017). arxiv.org/abs/1706.03762. §3.5 defines the sinusoidal positional encoding , , and motivates it via the linear-shift (relative-offset) property. Source of the permutation-equivariance framing. ↩ ↩2 ↩3
-
[established] Jianlin Su, Yu Lu, Shengfeng Pan, Ahmed Murtadha, Bo Wen, Yunfeng Liu, “RoFormer: Enhanced Transformer with Rotary Position Embedding” — arXiv:2104.09864 (2021; Neurocomputing 2024). arxiv.org/abs/2104.09864. Source of the rotary embedding: block-diagonal rotation applied to , the per-pair frequencies , and the relative-position identity . The primary source for this lesson. ↩ ↩2
-
[established] Ofir Press, Noah A. Smith, Mike Lewis, “Train Short, Test Long: Attention with Linear Biases Enables Input Length Extrapolation” — arXiv:2108.12409 (ICLR 2022). arxiv.org/abs/2108.12409. Source of the additive linear-distance bias , the fixed geometric per-head slopes, and the train-short/test-long length-extrapolation results (train at 1024, evaluate at 2048+).
↩ ↩2 ↩3