Position Interpolation › A Worked Example

Michael BrenndoerferUpdated July 6, 20254 min read
Language AI HandbookAll chapters
Part 18 of 67Long Context
Chapter 127Position Interpolation › A Worked Example
Reading section 4 of 12 · A Worked Example · 4 minSection 4 of 12 · 4 min

Book contents

Section 4 of 12 · 4 min read

Position Interpolation › A Worked ExampleOne section · Sections in this chapter4 / 12 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Before writing code, let's work through a concrete numerical example to solidify the concept. This will make the subsequent implementation much easier to follow.

Suppose we have a model with dmodel=4d_{\text{model}} = 4 (two dimension pairs, which is unrealistically small but easy to trace), base=10000\text{base} = 10000, trained on sequences up to Ltrain=8L_{\text{train}} = 8 positions. We now want to process a sequence of Ltarget=16L_{\text{target}} = 16 positions.

The scale factor is:

s=LtrainLtarget=816=0.5s = \frac{L_{\text{train}}}{L_{\text{target}}} = \frac{8}{16} = 0.5

The base frequencies for our two dimension pairs (i=0i = 0 and i=1i = 1 with d=4d = 4) are:

θ0=1100000/4=1100000=1.0 rad/position\theta_0 = \frac{1}{10000^{0/4}} = \frac{1}{10000^{0}} = 1.0 \text{ rad/position} θ1=1100002/4=1100=0.01 rad/position\theta_1 = \frac{1}{10000^{2/4}} = \frac{1}{100} = 0.01 \text{ rad/position}

Now consider position m=12m = 12 in the extended sequence. Under standard RoPE (no interpolation), the rotation angles would be:

  • Dimension pair 0: 12×1.0=12.012 \times 1.0 = 12.0 radians
  • Dimension pair 1: 12×0.01=0.1212 \times 0.01 = 0.12 radians

The training maximum (at position 7) was:

  • Dimension pair 0: 7×1.0=7.07 \times 1.0 = 7.0 radians
  • Dimension pair 1: 7×0.01=0.077 \times 0.01 = 0.07 radians

Position 12 exceeds the training maximum for both dimension pairs. The fast-rotating dimension pair 0 is especially far outside the training range (12.0 vs 7.0 radians).

Under Position Interpolation, we scale the position first: m′=12×0.5=6.0m' = 12 \times 0.5 = 6.0. The rotation angles become:

  • Dimension pair 0: 6.0×1.0=6.06.0 \times 1.0 = 6.0 radians (within training max of 7.0)
  • Dimension pair 1: 6.0×0.01=0.066.0 \times 0.01 = 0.06 radians (within training max of 0.07)

Both angles now fall within the range the model saw during training. The model can apply its learned interpretation of these rotation patterns to process what is effectively a mid-sequence position. The trade-off is that position 12 in the extended sequence "feels like" position 6 in the original sequence, meaning the model perceives it as occupying roughly the middle of the context rather than three-quarters of the way through. Fine-tuning corrects this calibration.

Out[7]:
Visualization
Two-panel bar chart showing rotation angles for dimension pairs 0 and 1 at positions 0 through 15 for standard RoPE versus position interpolation.
Worked example comparing rotation angles for standard RoPE and Position Interpolation with a tiny model (d=4, train_length=8, target_length=16). The dashed red lines mark the training maximum. Under standard RoPE, positions beyond 8 exceed the training maximum for the fast-rotating dimension pair 0. Under Position Interpolation, all positions in the extended range map within the training bounds.