NLP & Transformers

Positional Encodings & RoPE

Injecting word order awareness into permutation invariant self attention mechanisms.

🔴 advanced5 min readtransformers
Positional Encodings inject token order information into Transformer models. Because self attention computes pairwise similarities without regard to word sequence order, Transformers require explicit position signals. Original Transformers used Absolute Sinusoidal Positional Encodings. Modern Large Language Models use Rotary Position Embedding (RoPE), which rotates Query and Key vectors in complex 2D planes to naturally encode relative distance between tokens.

Why Transformers Need Positional Encodings

Self attention calculates pairwise dot products between token embeddings.

If you shuffle sentence word order, the set of pairwise dot products stays 100 percent identical.

To a raw Transformer without positional encodings:

"Dog bites man" AND "Man bites dog" are indistinguishable!

To make Transformers aware of word order, we must inject Position Signals into token representations.

┌──────────────────────────┬──────────────────────────┬──────────────────────────┐
│ 1. SINUSOIDAL (Vaswani)  │ 2. LEARNED ABSOLUTE      │ 3. ROTARY (RoPE)         │
├──────────────────────────┼──────────────────────────┼──────────────────────────┤
│ Uses sine and cosine     │ Assigns a trainable      │ Rotates Query and Key    │
│ wave functions of        │ embedding vector to each │ vectors in 2D planes by  │
│ different frequencies.   │ fixed position index.    │ position angles.         │
│ Added to input vectors.  │ Fails on longer context. │ Modern LLM standard!     │
└──────────────────────────┴──────────────────────────┴──────────────────────────┘

Absolute Sinusoidal Positional Encoding

Original Transformers added static sine and cosine wave values to token embeddings:

$$PE_{(pos, 2i)} = \sin\left( \frac{pos}{10000^{2i/d}} \right)$$

$$PE_{(pos, 2i+1)} = \cos\left( \frac{pos}{10000^{2i/d}} \right)$$

Higher dimensions oscillate slowly while lower dimensions oscillate rapidly, allowing the model to attend to relative positions via trigonometric identities.

Rotary Position Embedding (RoPE - Modern LLM Standard)

Modern LLMs (LLaMA 2, LLaMA 3, Mistral, PaLM) replace absolute additive encodings with Rotary Position Embedding (RoPE).

Instead of adding position vectors to input tokens at the bottom of the network, RoPE rotates Query and Key vectors inside every self attention layer!

  Query Vector q at Position m  ──► Rotate by Angle (m * theta) ──┐
                                                                 ├──► Dot Product q_m · k_n
  Key Vector k at Position n    ──► Rotate by Angle (n * theta) ──┘    Depends ONLY on Relative Distance (m - n)!

The Geometry of RoPE

RoPE pairs up adjacent 2D vector elements $(q_1, q_2)$ and rotates them in 2D planes:

$$R_{\Theta, m}^d q_m = \begin{pmatrix} \cos m\theta_1 & -\sin m\theta_1 & 0 & 0 \ \sin m\theta_1 & \cos m\theta_1 & 0 & 0 \ 0 & 0 & \cos m\theta_2 & -\sin m\theta_2 \ 0 & 0 & \sin m\theta_2 & \cos m\theta_2 \end{pmatrix} \begin{pmatrix} q_1 \ q_2 \ q_3 \ q_4 \end{pmatrix}$$

When computing Query Key dot product:

$$(R_{\Theta, m}^d q)^T (R_{\Theta, n}^d k) = q^T R_{\Theta, n - m}^d k$$

The inner product depends strictly on relative distance $n - m$!

Why RoPE Dominates

  1. Relative Distance Awareness: Attention naturally decays as token distance $n - m$ increases.
  2. Context Window Extension: Allows scaling context length from 4k to 128k tokens using Rotary Position Interpolation (YaRN) without retraining model weights from scratch.

Say this out loud

Positional Encodings inject word order awareness into self attention. Absolute sinusoidal encodings add sine and cosine waves to input embeddings. Rotary Position Embedding (RoPE) rotates Query and Key vectors in 2D planes by position angles inside attention layers. RoPE makes dot products depend strictly on relative token distance m - n, enabling long context window extensions in models like LLaMA.

Followups to expect

Check yourself

Question 1 of 3

Why are Positional Encodings mandatory in Transformer architectures?

More in NLP & Transformers

See all →
The Attention Mechanism5 minTransformer Architecture5 minTokenization & BPE5 min