Rúben GalvãoThe Rabbit Hole

The Rabbit Hole / AI and machine learning / Language models / Giving tokens a sense of position

RoPE scaling: stretching context beyond the training length

Position interpolation, NTK-aware scaling and YaRN extend rotary embeddings. What changes when an 8,000-token model reads 60,000 tokens?

A model learned to read 8,192 tokens at a time. Now we hand it a 60,000-token contract and ask about a clause near the end.

Attention can compare any two tokens. So why does the answer fall apart?

Angles it has never seen

With RoPE, each pair of query and key coordinates turns as the position grows, every pair at its own speed.

The fast pairs went round many times during training, so the model has seen every angle they can make. The slow pairs only turned part of the way.

At position 60,000 the slow pair points where training never wentThree rotary pairs of Llama 3.1 at position 60,000 with no change. The fast and medium pairs reached every angle in training. The slow pair reached only 25 degrees of a turn in training, and its arrow now points far outside that.Pair 1FastNext token 57° onPair 25MediumSeen in trainingPair 49SlowNever seen in training
The green area is every angle a pair reached in 8,192 training positions. Only the slow pair's arrow has left it.

At position 60,000, the slow pair points somewhere the model has never been. It has no idea what that angle means.

Squeeze everything back

So divide every position by 8. Position 60,000 then turns like position 7,500, and every arrow lands inside the green.

That fixes the slow pair. Now look at the fast one.

Slowing every pair crowds neighbouring tokens together in the fast pairThe same three pairs with every speed divided by 8. All three arrows are inside the angles seen in training, but in the fast pair the next token is only 7 degrees on instead of 57.Pair 1FastNext token only 7° onPair 25MediumSeen in trainingPair 49SlowSeen in training
Every arrow is back in the green, but in the fast pair the next token is now only 7° on, not 57°.

The fast pairs tell the model which tokens sit right next to each other. Squeezed eight times, neighbours look almost alike, and word order up close blurs.

Slow only the pairs that need it

The fast pairs were never the problem: they had already seen every angle. Only the slow pairs ran past what training showed them.

So leave the fast pairs alone, and slow down only the pairs that never finished a turn in training. Try all three below, and move the token.

Read past the training length

Interactive

Trained on 8,192 positions. Green: angles seen in training. Arrow: this token. Dashed: the next token.

Pair 1FastNext token 57° onPair 25MediumSeen in trainingPair 49SlowSeen in training

With only the slow pairs slowed, every arrow stays in the green and neighbours keep their spacing.

This is what Llama 3.1 does to read 128K tokens after training on 8K. Pairs too slow to finish a turn in 8,192 positions run 8 times slower. Pairs that turn at least four times keep their speed, and the pairs in between blend the two.

YaRN uses the same split. It also sharpens attention slightly, because scores spread thinner over so many more tokens. Either way, the model still trains on some long text afterwards. For YaRN that was about 400 steps, rather than starting again.

Reaching 60,000 tokens is one thing. Every new token still compares itself with all of them. Next, let it read only the nearby ones.

Sources and assumptions
  • Position interpolation, Chen and colleagues at Meta, 2023: dividing every position to fit the trained window, extending LLaMA to 32,768 tokens within 1,000 fine-tuning steps.
  • NTK-aware scaling (bloc97, 2023) changes the rotary base instead, which slows the slow pairs most and the fast pairs hardly at all. YaRN, Peng and colleagues, 2023, refines it into the split used here (pairs with a turn longer than the training length are slowed fully, pairs that turn more than 32 times are left alone, a ramp between) and scales queries and keys by 0.1 ln(s) + 1 to sharpen attention. It needed about 400 training steps.
  • Llama 3.1 configuration: rotary base 500,000, trained length 8,192, factor 8, low-frequency factor 1 and high-frequency factor 4, for a 131,072-token context.
  • Llama 4 also scales its queries up slightly as the position grows, in the layers that use no rotary positions.

The dials use Llama 3.1’s real speeds for pairs 1, 25 and 49 of the 64 in a 128-wide head. A pair counts as “seen” when its angle, within one turn, is no larger than the largest angle it reached in 8,192 positions; pair 49 does not complete a turn within 65,536 positions, so its unseen angles stay unseen. The figures and the widget show position 60,032, the nearest step of the slider to 60,000.

← Browse all subjectsBack to the beginning ↑