Part XII — Modern Sequence Architectures: RoPE, FlashAttention & State-Space Models · Chapter 3

Context window scaling (YaRN & LongRoPE)

Hook

A model trained on sequences up to length 8 gets handed a prompt of length 64. Every RoPE rotation angle past position 8 is one it has never seen produced during training. Retraining on longer sequences fixes it, but that's expensive — is there a way to reuse the same weights and just reinterpret the positions?

Intuition

Push the scaling factor up and watch two numbers: the fast dimension's effective position never budges, but the slow dimension's collapses back down toward the trained range. The two frequencies aren't treated the same way at all — and that asymmetry is the entire idea.

Formalize

YaRN checks each RoPE frequency's wavelength — how many positions it takes to complete one full rotation — against the length the model was actually trained on, and only rescales the ones that need it:

poseff(pos,i)={pos/swavelength(i)>Lposotherwise\text{pos}_{\text{eff}}(pos, i) = \begin{cases} pos / s & \text{wavelength}(i) > L \\ pos & \text{otherwise} \end{cases}
  • pospos — the token's true position in the (extended) sequence.
  • ii — which frequency this is; wavelength(i)=2π/rate(i)\text{wavelength}(i) = 2\pi / \text{rate}(i).
  • LL — the original trained context length.
  • ss — the scaling factor: how many times longer the new context is meant to be.
  1. A fast dimension has already lapped the trained range many times

    If its wavelength is shorter than LL, it completed full rotations inside training. One more turn past LL looks, geometrically, exactly like turns it already learned from — safe to extrapolate as-is.

  2. A slow dimension never even finished one turn during training

    Its wavelength exceeds LL, so every angle past LL is new territory. Dividing its position by ss before rotating maps the extended range of positions back down into the angles the model actually trained on.

  3. Real YaRN ramps smoothly between the two, this demo snaps

    Production YaRN blends interpolation and extrapolation across a band of intermediate wavelengths so there's no hard seam. With only two frequencies here, the boundary is exact and hand-checkable — the idea is identical either way.

Play
at scale 8x, the slow dimension's effective position lands exactly on 8 — the edge of the trained context

The dashed line is the fast dimension: flat, because it's never rescaled. The bold curve is the slow dimension's effective position at position 64 as the scaling factor slides from 1x (no rescaling — the raw, unsafe extrapolation) to 8x. By 8x it lands exactly on 8 — the length the model actually trained on.

Worked example

Trained context L=8L=8; extended position pos=64pos=64 (an 8x-longer sequence); fast rate =1=1, slow rate =0.01=0.01 (so wavelengths 2π6.28<82\pi\approx6.28 < 8 and 200π628>8200\pi\approx628 > 8):

  1. No scaling (s = 1): the slow dimension extrapolates raw

    Angle =64×0.01=0.64= 64 \times 0.01 = 0.64 radians. The largest slow-dimension angle seen in training is at pos=L=8pos=L=8: 8×0.01=0.088\times0.01=0.08. Ratio: 0.64/0.08=8×0.64/0.08=8\times larger than any slow-dimension angle seen in training.

  2. Scale by 8x: divide position by 8 before rotating

    Effective position =64/8=8=64/8=8. Angle =8×0.01=0.08=8\times0.01=0.08 — precisely the boundary angle the model trained up to.

  3. The fast dimension is untouched throughout

    Angle =64×1=64=64\times1=64 radians regardless of ss — it already completed more than 10 full turns inside the trained range, so extrapolating further changes nothing about how familiar the angle looks.

Checkpoint

At position 64, find the scaling factor, among the candidates, that brings the slow dimension's effective position back down to within 0.5 of the trained context length (8).

Pick a scaling factor to try it
Summary
poseff(pos,i)={pos/swavelength(i)>Lposotherwise\text{pos}_{\text{eff}}(pos, i) = \begin{cases} pos / s & \text{wavelength}(i) > L \\ pos & \text{otherwise} \end{cases}

YaRN stretches a trained context window without touching a single weight, by rescaling positions rather than the model — and only for the RoPE frequencies whose wavelength is long enough that extrapolation would otherwise land them in angles never seen during training. Fast-rotating dimensions are left alone because they've already lapped the trained range many times over. It's a purely inference-time fix for the mismatch RoPE creates once a prompt runs longer than training — but it says nothing about the cost of actually processing that longer prompt, which is where the next chapter picks up.