6.A — Sinusoidal positional encoding (original AIAYN)
Goal: Define the sinusoidal positional encoding PE(pos, 2i) = sin(pos / 10000^{2i/d}); show that PE(pos + k) is a linear function of PE(pos); understand why this property motivates the design; place sinusoidal PE as a historical (now-superseded) ancestor of RoPE.
1. Why this appendix exists
The original Attention Is All You Need paper (Vaswani et al., 2017)[1] handled positions with an additive sinusoidal encoding: compute a deterministic vector from sinusoids of geometrically-progressing frequencies, and add it to the token embedding before the first block. The Transformer was thus position-aware at the input layer, and every subsequent layer inherited that signal through the residual stream (6.9).
This was the canonical positional encoding from 2017 to ~2020. Then learned PE (GPT-2 — Appendix 6.B) and rotary PE (6.7) displaced it. Sinusoidal PE survives in the literature as the historical reference design and in a few niche models (some encoder-decoder text-to-speech systems, some scientific Transformers). The math is short and instructive — particularly the property " is a linear function of " that motivated the design — and worth knowing if you read pre-2020 papers.
This appendix gives the definition, proves the linearity property, and identifies what made the design almost the right answer.
2. The formula
Let be the embedding dimension (must be even). For position and dimension index , define:
So is a vector built from pairs at angular frequencies .
Two key properties:
- The frequencies span orders of magnitude. For : (period , very fast — one full cycle every tokens). For : (period tokens — extremely slow). The intuition: low- pairs encode local position, high- pairs encode global position.
- The base is empirical. It produces a smooth distribution of wavelengths from to tokens. AIAYN doesn't motivate it further; it's a hyperparameter.
This is identical in structure to RoPE's frequency schedule of 6.7 §4 — same base , same geometric progression. The difference is in how the encoding is used: sinusoidal PE is added to the embedding; RoPE rotates the query and key inside attention.
2.1 How it is used
For an input sequence with token embeddings , the position-augmented input is
then enters the first block. Every subsequent layer sees position information only through this initial additive signal, transmitted along the residual stream.
The PE is not a parameter: it is deterministically computed from and never updated by gradient descent. (Contrast: learned PE — Appendix 6.B — is a parameter.)
3. The "linear in PE(pos)" property
AIAYN motivates the sinusoidal choice with one statement (§3.5):
"We chose this function because we hypothesized it would allow the model to easily learn to attend by relative positions, since for any fixed offset , can be represented as a linear function of ."
The proof is direct. Fix . We want to express as a linear map of .
3.1 The 2D pair
Consider the -th pair: . Using the angle-addition formulas:
In matrix form, with :
The 2D pair at position is obtained from the pair at position by a 2D rotation through angle — depending on , not on .
3.2 The full -dimensional map
Stacking the rotation for each of the pairs, define the block-diagonal matrix
(With the right reordering of the entries to match the dim-2/dim-2+1 layout above.) Then:
For any fixed offset , the position vector at is a linear (specifically, orthogonal) function of the position vector at , with the linear map independent of .
3.3 What this is good for
The idea behind AIAYN's choice was: if the model wants to compute attention based on relative offsets , it should be able to do so by learning a single linear projection — the same matrix at every absolute position. Without the linearity property, the model would have to learn a position-dependent relationship to encode offsets, which is harder.
But — and this is the part that motivated RoPE — this linearity property of the encoding does not translate cleanly into a linearity property of the attention dot product. The PE is added to the embedding before is applied, so
and involves cross-terms , , etc., which mix content and position in ways that do not reduce cleanly to a function of .
RoPE (6.7) fixes this by rotating and directly by position-dependent rotations after the projections, ensuring depends only on . The sinusoidal-PE-as-input is the "almost right" attempt that came first.
4. Why sinusoidal PE worked in practice (and where it failed)
4.1 What worked
The original AIAYN transformer used sinusoidal PE successfully for translation up to ~5000 token sequences. The design has three virtues:
- Determinism. No parameters; the encoding is the same at training and inference, at every length.
- Length extrapolation, in principle. Sinusoidal functions are defined at every real-valued position. If you train on length 1000 and infer at length 5000, is still computable.
- Wide spectrum. Frequencies span 4 orders of magnitude. The model has access to short-range and long-range positional information.
4.2 What failed
Empirically, sinusoidal PE does not extrapolate as well as the math suggests. Press et al. (2021, ALiBi paper) showed that sinusoidal-PE Transformers trained at length degrade sharply at lengths . The reason: although is well-defined at any , the learned attention patterns of the model are shaped during training by the position distribution it sees. Once , the model is in territory it has never trained on — even if the PE itself is well-defined.
This empirical failure is what motivated:
- Learned PE (Appendix 6.B), which dropped the math-driven design but matched empirical performance.
- Relative PE schemes (Shaw et al., 2018; T5), which encode in the score matrix directly.
- RoPE (6.7), which encodes at the dot-product level and preserves the "all positions defined" property.
- ALiBi (6.13 §3), which dropped the embedding altogether and used a fixed bias.
5. Sinusoidal PE versus RoPE: side by side
The mathematical similarity between sinusoidal PE and RoPE deserves explicit comparison, since they share the same frequencies :
| | Sinusoidal PE (AIAYN) | RoPE (Su et al., 2021) | |---|---|---| | Where applied | Input embedding: | Inside attention, on and after projection | | Operation | Addition | Rotation (orthogonal matrix multiplication) | | Effect on | Mixes position and content; not cleanly relative | depends only on | | Inference length | Defined at all positions but extrapolation is empirically poor | Defined at all positions; can be scaled (NTK / YaRN, 6.13 §4) | | Used by | Original Transformer (2017); some encoder-decoder models | LLaMA, Mistral, Qwen, Gemini, most 2024-onwards LLMs |
RoPE keeps the AIAYN spirit (sinusoidal frequencies; relative offsets are linear in position) and discards the part that didn't work (additive at input). The historical line is direct.
6. Summary
Sinusoidal PE is the original — and almost the right — design:
for a fixed orthogonal — the linearity property that motivated the design. In practice, the additive form mixed position and content in attention, and the empirical extrapolation behaviour was poor. RoPE inherits the frequencies and the relative-position promise but applies them as rotations inside attention, getting both properties.
Read AIAYN §3.5 with this appendix open; the two pages explain the design intent and you should now find the "linear function of PE(pos)" statement provable on inspection.
(a) For , write out the four-component vector for explicitly. Confirm that is .
(b) Verify the linearity property §3.2 for at . Construct explicitly and apply it to . Compare to .
(c) Argue that AIAYN's claim " is a linear function of " does not immediately imply that depends only on . (Hint: think about what and do to the additive content + position mixture.)
(d) Why is sinusoidal PE deterministic rather than learned? What is the parameter count of sinusoidal PE versus learned PE (Appendix 6.B)?
(e) Read AIAYN §3.5 (one page). State, in your own words, why the authors chose sinusoidal over learned. Was their reasoning vindicated by later research?
References
- [1]
Vaswani, A., Shazeer, N., Parmar, N., et al. (2017). "Attention is all you need". NeurIPS. arXiv:1706.03762 — The Transformer paper. Scaled dot-product attention (6.2), multi-head attention (6.4), encoder–decoder cross-attention (6.3), sinusoidal positional encoding (Appendix 6.A), the FFN sub-layer (6.8), post-LN block diagram (6.10), full architecture read (6.11).
↩