Positional Encoding

Since transformers are order-invariant (i.e., self-attention treats all positions symmetrically), positional encodings are added to token embeddings to inject information about sequence order. Given token embeddings , the model forms position-aware representations

where assigns a distinct encoding to each position .

An effective positional encoding should satisfy four criteria:

  1. Uniqueness: each position receives a unique vector , ensuring that the model can distinguish token order.
  2. Boundedness: encodings remain on the same scale as the original features (e.g., ), which stabilizes training.
  3. Distance-awareness: the representation should make relative distances between positions easy to infer - an inductive bias important for both language and vision models.
  4. Scalability: the scheme should extend gracefully to arbitrary sequence lengths, since inputs vary in size.

The original transformer uses sinusoidal encodings, varying between even and odd positions:

since sinusoids make relative position linearly recoverable: if a query at position wants to attend to a key at position , then there exists a rotation matrix such that .

Another famously used positional encoding method is Rotary Positional Embeddings (RoPE), which does not add a position vector to embeddings, but instead rotates the query and key representations in attention by a position-dependent angle.

  • The key idea is to treat pairs of coordinates as a 2D plane and rotate them; then attention scores depend naturally on relative offsets. For a head dimension (assumed even), define frequencies

    and for token position , rotate the coordinates by angle . Writing for the two coordinates of a vector at position , RoPE applies
x’_{m,2i} &= x_{m,2i}\cos(m\theta_i) - x_{m,2i+1}\sin(m\theta_i),\\ x’_{m,2i+1} &= x_{m,2i}\sin(m\theta_i) + x_{m,2i+1}\cos(m\theta_i). \end{aligned}$$ In practice this is implemented without explicit matrices by precomputing $\cos(m\theta_i)$ and $\sin(m\theta_i)$ and using a “rotate-half” operation that swaps the two halves of the vector with a sign flip. RoPE is especially attractive in decoder-only LMs because it builds in a strong **relative-position** inductive bias while remaining simple to compute and parameter-free.