---
title: "You could have designed state of the art positional encoding"
description: "Rotary Positional Encoding (RoPE) can be derived step by step from simple ideas."
url: "https://swe.quest/articles/you-could-have-designed-state-of-the-art-positional-encoding-af87c15753b60969"
canonical: "https://huggingface.co/blog/designing-positional-encoding"
source: "Hugging Face"
published: "2024-11-25"
difficulty: "hard"
kind: "Deep dive"
reading_minutes: 15
tags: ["llms","machine-learning"]
---

# You could have designed state of the art positional encoding

A post on [Hugging Face](https://swe.quest/sources/hugging-face.md), published 2024-11-25, 15 min read, Deep dive. Original: [https://huggingface.co/blog/designing-positional-encoding](https://huggingface.co/blog/designing-positional-encoding)

## Summary

Rotary Positional Encoding (RoPE) can be derived step by step from simple ideas. The derivation starts from the permutation-equivariance of self-attention, demonstrated with Llama 3.2 1B embeddings, and lists five desirable properties for a positional scheme. It then iterates from integer to binary to sinusoidal encodings, showing that sinusoidal encoding already encodes relative offsets as a rotation matrix. The final step moves to a multiplicative rotation of queries and keys inside the QK^T dot product, which preserves vector norms and yields RoPE as used in modern transformers.

Written by swe.quest, not by the author.

**Difficulty:** Hard: written for specialists in the field

## Topics

- [LLMs](https://swe.quest/tags/llms.md) (AI & ML)
- [Machine learning](https://swe.quest/tags/machine-learning.md) (AI & ML)

## Prerequisites

- self-attention
- basic linear algebra
- trigonometry

## Keywords

RoPE, RoFormer, sinusoidal positional encoding, Llama 3.2, Attention Is All You Need, PyTorch

## Related posts

- [Using group theory to explore the space of positional encodings for attention](https://swe.quest/articles/using-group-theory-to-explore-the-space-of-positional-encodings-for-attention-24091e992dee90df.md): Jane Street Tech Blog · 2026-04-22 · Hard · 11 min read · LLMs, Machine learning
  Requiring a positional encoding for attention to be linear in queries and keys, translation-invariant (depending only on relative position) and continuous forces it to be a one-parameter matrix group A(t)=exp(tX).
- [Differential Transformer V2](https://swe.quest/articles/differential-transformer-v2-308ff82f46821bd8.md): Hugging Face · 2026-01-20 · Hard · 10 min read · LLMs, Model training, Inference & serving, Machine learning
  Differential Transformer V2 reworks differential attention by doubling query heads while keeping key-value heads fixed, subtracting paired heads within the same GQA group using a per-token, per-head sigmoid-gated λ.
- [The N Implementation Details of RLHF with PPO](https://swe.quest/articles/the-n-implementation-details-of-rlhf-with-ppo-ed05a4d20cc991ad.md): Hugging Face · 2023-10-24 · Hard · 24 min read · Model training, LLMs, Python, Machine learning
  A from-scratch PyTorch reproduction of OpenAI's 2019 lm-human-preferences RLHF codebase closely matches the original learning curves on stylistic tasks like sentiment and descriptiveness.
- [Nyströmformer: Approximating self-attention in linear time and memory via the Nyström method](https://swe.quest/articles/nystromformer-approximating-self-attention-in-linear-time-and-memory-via-the-fd6613fb9ac42d67.md): Hugging Face · 2022-08-02 · Hard · 6 min read · LLMs, Machine learning, Algorithms, Python
  Nyströmformer replaces quadratic softmax self-attention with an O(n) approximation built on the Nyström method, which reconstructs a matrix from a sampled subset of its rows and columns via a Moore-Penrose pseudoinverse.
- [Introducing RWKV - An RNN with the advantages of a transformer](https://swe.quest/articles/introducing-rwkv-an-rnn-with-the-advantages-of-a-transformer-61894d925597865d.md): Hugging Face · 2023-05-15 · Easy · 10 min read · LLMs, Machine learning, Inference & serving
  RWKV is an attention-free language model architecture, derived from Apple's Attention Free Transformer, that trains in parallel like a GPT-style transformer but runs inference as an RNN.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
