Positional encoding

Positional encoding is how a transformer is told the order of the tokens it reads; a widely used modern form, rotary position embedding (RoPE), rotates query and key vectors by angles that depend on each token's position.

Attention, the core of a transformer, compares every token with every other, but on its own it has no sense of order. Without extra information, 'dog bites man' and 'man bites dog' would look the same. The 2017 transformer had no recurrence or convolution to supply order, so it added a positional encoding to each token's embedding at the bottom of the network.

The original encoding used sine and cosine waves of different frequencies, giving each position a unique pattern. The authors chose it because they expected it to let the model reason about relative positions and to cope with sequences longer than any seen in training. They also tried learned position embeddings, a separate trainable vector for each position, and found nearly identical results.

Many open language models now use rotary position embedding, or RoPE, proposed by Jianlin Su and colleagues in April 2021. Rather than adding a position vector, RoPE rotates each token's query and key vectors by an angle set by its position. When two tokens are compared, the rotations combine so that the result depends on how far apart they are, and the link between tokens weakens as the distance grows. Meta's LLaMA, published in February 2023, dropped absolute position embeddings in favour of RoPE at every layer, and DeepSeek's multi-head latent attention includes a separate rotary component.

Positional encoding is central to the long context windows announced in model launches. A model trained on inputs of a certain length meets unfamiliar positions beyond it. In June 2023 Shouyuan Chen and colleagues proposed position interpolation: instead of stretching into unseen positions, they squeezed the position numbers of a long input into the range the model had been trained on. That extended LLaMA models from 7 to 65 billion parameters to 32,768 tokens of context with 1,000 or fewer steps of fine-tuning.

The practical upshot is that an advertised context window depends partly on how positions are encoded and how the model was trained to handle them. Pushing a model past the lengths it was trained on without such adjustments can produce extreme attention scores that disrupt it, which is why long-context claims are best checked with tests on real documents.

Common questions

Why do transformers need positional encoding?

Attention compares tokens with each other but has no built-in notion of order, so without position information a transformer would treat a sentence like a bag of words. Positional encoding adds that information, so the model knows which word came first.

What is RoPE?

RoPE, short for rotary position embedding, encodes position by rotating each token's query and key vectors by an angle that depends on its position. When two tokens are compared, the result depends on their distance apart. It was proposed in 2021 and is used in many open models, including Meta's LLaMA family.

How do models extend their context window?

A common approach is to adjust the positional encoding and then train briefly on longer inputs. Position interpolation, proposed in June 2023, compresses the positions of a long input into the range the model already knows; it extended LLaMA models to 32,768 tokens with 1,000 or fewer fine-tuning steps.

Finding more

Search for "rotary position embedding explained", "RoPE scaling" or "context window extension" to follow how models handle long inputs. The original papers on arXiv, model cards that state the positional scheme and context length, and inference engine documentation on RoPE scaling settings are reliable sources. For claims about long-context ability, look for independent evaluations rather than the advertised maximum length.

Working out what this means for your business?

Aileron Shift builds the systems behind this site: AI newsrooms that research, write and publish on their own, marketing automation and WordPress hosting for companies that need to move faster than their competitors.