Interactive Lab · Rotary Positional Embeddings
Attention on its own is order-blind: shuffle the tokens and the math is unchanged. RoPE fixes this with a beautifully simple trick, rotate each query and key vector by an angle proportional to its position. No learned lookup table, no added parameters. And it buys something deep: when a query at position m dots with a key at position n, the rotations combine so the score depends only on the relative distance n − m. Move the query anywhere and the whole attention pattern slides along with it. Watch below.
▶ The playground, position becomes rotation
RoPE is how modern LLMs (Llama, Mistral, Qwen) encode word order so attention knows what came before what. It's also the dial behind long-context models, raising the rotary base is how a model trained on 4K tokens is stretched to 128K. This is that mechanism.
The code that's actually running
Drag query position m and watch the lower chart: the attention peak follows the query, and the faint second query (9 positions later) traces an identical curve, just shifted by 9. Same shape, different centre, that's the whole point: RoPE encodes relative position, so a pattern learned at position 5 works unchanged at position 5,000. Absolute positional embeddings can't promise that. Then sweep the rotary base θ: a small base spins the vectors fast (fine detail, short reach), a big base spins them slowly (coarser, but distinguishes far-apart tokens), the reason long-context models crank the base way up.
The takeaway