Source and scope. This is a reading note, not a translation. It begins with Jianlin Su’s post “Tracing the Origins of Sinusoidal Position Encoding”, then re-derives the argument alongside the original Transformer and RoPE papers. I will distinguish exact identities, useful sufficient conditions, and explanations that rely on approximations.
The Transformer’s sinusoidal position encoding can look like an empirically chosen table of trigonometric values:
with
The usual explanation says that a Transformer has no order information, so different sine and cosine values are added to distinguish different positions. That is true, but it leaves the interesting question unanswered. If distinctness were the only goal, random vectors, one-hot codes, or a learned embedding table would all work. Why sinusoids?
The thread developed here is a stronger—yet still only sufficient, not necessary—requirement:
If the interaction between two absolute position vectors depends only on their displacement, the absolute coordinates expose a relative position signal. The point of sin/cos is not merely to manufacture different numbers. It maps translation along a sequence to rotation in a representation space.
1. What exactly is missing from self-attention?
Remove masks, position biases, and position encodings. For an input sequence
and any permutation matrix , pure self-attention obeys
This is permutation equivariance: permuting the input tokens simply permutes the outputs in the same way. The layer can compare content, but it has no coordinates for “the third token” or “two steps to the left.” A decoder’s causal mask already supplies a directional constraint; the statement above concerns self-attention with no position-dependent signal at all.
The original Transformer adds a position vector to the token at position :
Requiring only sets a low bar. A more useful question is whether the model can easily recognise that
have the same relative structure. Their absolute coordinates differ, but both offsets are . We can express that goal through an interaction kernel:
This is not the sole definition of a good position encoding. It is a clear, tractable design target that fits the bilinear interactions used by attention.
2. Start with the simplest interaction: an inner product
At the centre of an attention score is a bilinear form,
To isolate the positional structure, consider a simpler problem: can we choose such that
The condition immediately gives translation invariance. For any integer ,
Pairs separated by the same distance therefore have the same positional similarity. The goal is not to make their positions equal, but to make their relationship equal.
3. A two-dimensional solution: place positions on a unit circle
Consider
where is a fixed angular velocity. The inner product between two positions is
The second line is just the cosine subtraction identity. Thus
which no longer depends on and separately.
The same construction follows from the complex number . If must depend only on and the magnitude of every is fixed, the phase grows linearly with , producing uniform motion around the unit circle. This makes circular encoding a simple and natural family of solutions. It does not establish sin/cos as the unique possibility under every set of assumptions.
The canonical Transformer formula orders each pair as , while the derivation above uses . Swapping the two coordinates changes neither the inner product nor the rotation structure.
4. The intuition: every pair of dimensions is a clock
Think of as a clock hand. Advancing one token rotates the hand by :
If two tokens are positions apart, their phase difference is always . The pairs and have completely different absolute coordinates, but both pairs of hands are separated by .
The circle therefore creates the correspondence
Sin and cos are not decoration here. They are the coordinates of a two-dimensional rotation and the paired basis functions that make the subtraction identity work.
5. Why do we need many frequencies?
One clock is periodic. If , then
so a single frequency cannot distinguish positions one full period apart. A -dimensional encoding places clocks with different speeds side by side:
When one frequency returns to its initial phase, the others will usually not reset at the same position. Combining frequencies therefore reduces periodic aliasing and gives several resolutions of positional change. It is not an unconditional guarantee of no collisions at every length and under finite numerical precision.
The frequencies also span different scales:
- Larger values produce substantial phase changes between nearby positions, making them sensitive to local displacement.
- Smaller values evolve slowly and preserve low-frequency structure across a wider interval.
This resembles the multiscale logic of Fourier features, but no single dimension should be read as exclusively responsible for one fixed distance. The phase pattern is distributed across dimensions, and the trained model decides how to use it.
6. Why arrange the frequencies geometrically?
The original Transformer uses
Equivalently, the wavelength for each coordinate pair is
The wavelengths therefore follow an approximate geometric progression from to . Geometric spacing uses a finite number of dimensions to cover a wide range of logarithmic scales, instead of concentrating most coordinates in a narrow absolute interval.
Two questions should remain separate:
- Why sin/cos? Rotations and the angle-subtraction identity make relative displacement appear naturally in interactions.
- Why the base 10000? It is an engineering choice governing frequency range and resolution, not a constant uniquely forced by the identity above.
The stated motivation in the original Transformer paper is that, for any fixed offset , is a linear function of . The authors also hoped that a fixed function would extrapolate to lengths not seen during training. Their translation experiment found learned absolute embeddings and fixed sinusoidal encodings performed similarly. Being able to evaluate an encoding at a longer index does not, by itself, guarantee reliable length extrapolation by the full model.
7. Does the inner product decay with distance?
The inner product of the full encodings is
Let and normalise by the number of frequency pairs:
For small , the phases remain relatively aligned and many cosine terms reinforce one another. As distance grows, the phases spread out and positive and negative terms cancel. Over practical ranges this often creates oscillatory decorrelation.
The qualification matters:
With finitely many frequencies, is a finite cosine sum. It is not guaranteed to decrease monotonically, and in general it should not be claimed to converge strictly to zero as .
The “long-range decay” discussed in Su’s post comes from a continuous, large-dimensional approximation. With ,
where is the cosine integral. The envelope of this oscillatory integral tends to zero. But that describes the trend of a continuous limit, not the exact behaviour of a finite sum. A defensible conclusion is that the geometric frequencies provide useful local similarity and an oscillatory decorrelation prior over practical distance ranges.
The three figures below are taken directly from Jianlin Su’s original post. The first shows the continuous integral approximation for as relative distance changes; the next two compare several choices of over short- and long-distance ranges. They illustrate that the shape and rate of decay depend on the frequency schedule; they are not a proof that a finite-dimensional position kernel decreases strictly monotonically.
8. What role does the Taylor expansion play?
The geometric motivation for sinusoidal PE is already complete. A Taylor expansion addresses a different question: why focus on a bilinear interaction between two position vectors in the first place?
Suppose the network computes
In a local approximation that treats as a small perturbation, a first-order term involves only one position, for example
The first cross term containing both and appears at second order:
If, purely to obtain a tractable starting point, we further approximate , the problem reduces to
This route supplies motivation, not an assumption-free proof. It relies on at least three choices: treating position vectors as sufficiently small perturbations; expecting a local second-order expansion to capture the relevant interaction; and approximating by the identity or a diagonal-dominant structure. For a general , dependence on alone need not survive. Stating these assumptions is more accurate than presenting the Taylor expansion as the inevitable origin of sin/cos.
9. A deeper view: translation as a group representation
Define the two-dimensional rotation matrix
The circular encoding satisfies
and
The map therefore represents translation in the additive group as a two-dimensional rotation. Relative position appears through
The group-theoretic vocabulary is optional. The intuitive statement is simple: whether the current position is 5 or 100, “move three tokens forward” applies the same rotation operator. The absolute coordinate changes; the relative transformation does not.
10. From here to RoPE: one conceptual step, a different mechanism
Traditional sinusoidal PE adds a position vector to a token representation:
RoPE instead applies position-dependent rotations directly to content-derived Queries and Keys. Let and denote the unrotated vectors:
Their attention score is
Position enters the score explicitly through the relative offset , while the full score still depends on token content through and . The RoFormer paper implements this structure with blockwise two-dimensional rotations inside attention.
“Only one step from sinusoidal PE to RoPE” is therefore a useful conceptual bridge, not a claim that the mechanisms are equivalent. The former is additive absolute encoding at the input; the latter is multiplicative rotation in Q/K space. Their shared mathematical core is phase difference under composition of rotations.
11. What does this derivation establish—and what does it not?
It establishes directly that:
- Paired sin/cos coordinates describe a two-dimensional rotation.
- The inner product of a matched frequency pair is exactly .
- A fixed positional offset is a linear rotation independent of the absolute position.
- Multiple frequencies extend this structure across several scales.
- RoPE makes relative displacement appear through in the Q/K inner product.
It does not establish that:
- Sinusoidal PE is the unique or optimal position encoding.
- The base 10000 is theoretically mandatory.
- A finite-dimensional inner product is strictly monotonic in distance or must converge to zero.
- Evaluating the formula beyond the training length guarantees extrapolation.
- The actual attention score of additive sinusoidal PE depends only on relative position.
That final limitation is important. The kernel shows a structure supplied by the encoding and a possibility the model can exploit; it is not a complete description of a trained network’s behaviour.
12. The argument on one page
The entire chain can be compressed to
This goes one level deeper than “use sine and cosine to assign different numbers to positions.” Sinusoidal encoding turns relative displacement into a phase difference; RoPE then places that rotation structure directly inside the attention score.
References and figure provenance
- Vaswani, A. et al. (2017). Attention Is All You Need, §3.5.
- Su, J. (2021). Tracing the Origins of Sinusoidal Position Encoding. This post supplies the reading starting point and the Taylor/continuous-integral perspective; the derivation and finite-dimensional qualifications here are independently reorganised.
- Su, J. et al. (2021). RoFormer: Enhanced Transformer with Rotary Position Embedding.
- Figures 1–3 are original figures by the author, generated from the equations in this article and released under CC BY 4.0. No third-party image is reproduced. The numerical curve uses the exact finite sum with .