TL;DR
A language model can only read as many tokens as its context window, and the window it was trained with is fixed. Chen and colleagues showed how to make a model trained on 2048 tokens read up to 32768 tokens. Instead of letting the position numbers run past the range the model has seen, they shrink every position number so the longest input still falls inside the old range, then fine-tune for about 1000 steps. The extended LLaMA models kept most of their short-context quality and used the long context well.
Builds on: Rotary Position Embedding, Transformer, Fine-Tuning, Linear Interpolation · Leads to: long-context fine-tuning recipes for open models that use the same rescaling idea
Why it mattered
Long inputs are now routine: whole books, codebases, long chats and retrieval results placed in the prompt. A model that stops at 2048 tokens has to chop these up. Training a new model with a long window from scratch costs a full pre-training run, which is expensive. Position Interpolation (PI) was a cheap middle path: take a model that already exists, change how positions are fed in, and train briefly.
The paper appeared in June 2023, a few months after the LLaMA models, which used RoPE and had 2048-token windows. A concurrent blog post (SuperHOT, by kaiokendev) had described a similar interpolation just before it, and the authors acknowledge that work. Open-source projects on LLaMA then picked up the same rescaling with short fine-tunes.
The problem
Start with what is already known. A transformer with RoPE does not store a position for each token. It rotates each query and key vector by an angle that depends on the token's position, and the attention score between two tokens depends only on how far apart they are. The paper calls that distance the span . The position scheme is therefore built to encode gaps rather than absolute places.
Here is the problem. Suppose the model was pretrained with a window . Its attention scores were only ever seen for spans from 0 to 2048. If you feed it a 4096-token input and keep the positions as they are, the model meets spans it has never seen. This is extrapolation: predicting outside the range of the training data.
The authors measured what happens. With direct extrapolation, the Perplexity (a measure of how surprised the model is by the next token) climbs above , which is the level of an untrained model. The attention score curves, as functions of , look well-behaved inside the trained range and can jump to very large values outside it.
Plain fine-tuning on longer text was the obvious fix, and it was slow. After more than 10000 training batches, the effective context of one 7B model grew only from 2048 to 2560 tokens.
How it works
Take a tiny instance first. The pretrained window is and the new window is , so the new inputs are twice as long. Position Interpolation works in six steps.
flowchart LR A["Input tokens, positions 0 to new window minus 1"] --> B["Scale each position by old window over new window"] B --> C["Scaled positions now lie in 0 to old window"] C --> D["Apply RoPE rotation to query and key"] D --> E["Attention scores, as in the pretrained model"] E --> F["Short fine-tune on the longer window"]
-
Start with positions 0 to 7. The new input has eight tokens, numbered . The pretrained model only knows positions 0 to 3. If you leave the numbers alone, the largest span is 7, which is outside 0 to 4.
-
Scale every position by . Here . The positions become 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5. Check: , and is less than .
-
Feed the scaled positions into RoPE. The rotation angle for a token is now computed from instead of . Nothing else in the network changes: the weights, the attention formula and the architecture are the same.
-
Check the largest span. The largest span is now , which lies inside the pretrained range . The attention scores the model computes are the kind it saw in pretraining.
-
Pick up the in-between positions. Positions such as 0.5 and 1.5 never existed in pretraining. They are fine because the attention score is a smooth function of the span, so a value between two known grid points lands between their values. This is the sense in which the method is an interpolation.
-
Fine-tune briefly. Train with next-token prediction on long text, using the scaled positions, for about 1000 steps. The model only has to adjust to the new scale. It does not have to learn new facts.
Two sanity checks. The scaling is applied to every token, so the maximum span is always at most . And the scaling changes no parameter counts, so existing training and serving code can be reused.
Worked examples
Setting: , , , and (the coefficient symbols are defined in The math below).
- The scale factor is .
- Scale the largest position: .
- The largest span is therefore , which is inside .
- Take the grid cell that contains . Then .
- Plug into the bound: .
- Interpretation: the straight-line guess for the score at span 3.5 is off by at most about 0.027, with these and values.
Answer: the largest span is 3.5, and the interpolation error is at most about 0.027.
Setting: , , (the head size used by LLaMA 7B), and .
- The scale factor is .
- Take a token at position . Its scaled position is , which is inside .
- Take a key at . Its scaled position is . The scaled span is .
- The unscaled span would have been , far outside the trained range. The scaled span is inside it.
- The worst-case interpolation error for any span inside one grid cell is .
- Interpretation: with these values the straight-line guess is within about 0.434 of the true score for any span inside one unit-length grid cell, however long the new window is.
Answer: the scaled span is 1975, and the worst-case error from interpolation is about 0.434.
Setting: the same and , but now and , a 16-fold extension.
- The scale factor is .
- A query at and a key at have unscaled span . Far outside the trained range.
- Scale both positions: and . The scaled span is , inside .
- Interpolation guarantees an error of at most .
- Extrapolation, applied to the unscaled span , has bound . Using the paper's numerical finding , the bound is at least .
- The ratio is consistent with the paper's "about 600 times smaller" figure, which uses the same bounds with rounded constants.
Try it: a window goes from to . What is the scale factor, and what is the scaled position of token 12000? Answer: the scale factor is , and , which is inside .
The math
The method takes a position in the new window and maps it to before the usual RoPE rotation. Write for the RoPE map that rotates an embedding vector at position , and let be the extended version.
Given a pretrained window and a target window , Position Interpolation replaces the RoPE map by
The scale factor is less than or equal to 1, so every scaled position lies in .
Step 2 in symbols
For RoPE, the attention score between a query at position and a key at position depends only on the span . Under interpolation the span becomes . Write for the pretrained score as a function of span . Then the extended score is
Since , the scaled span is always in .
Step 3 in symbols: why the score is smooth
The paper writes the score as a sum over frequency pairs :
Here is the head dimension, is the imaginary unit from complex numbers, and each is a complex coefficient set by the query and key vectors. Each term is a rotating vector, so is a sum of sines and cosines in . Those are smooth, and their derivatives are bounded.
Step 5 in symbols: the interpolation bound
Let be the straight line through two known grid values and , with and . This is the idea from Linear Interpolation:
The paper's Theorem 2.1 bounds the error of this straight-line guess:
On an interval of length 1, the product is at most , so the bound becomes .
Step 6 in symbols: the extrapolation bound for comparison
Extrapolation has its own bound, from the RoPE analysis:
The paper reports that numerically for every they checked, and it is often much larger. So the extrapolation bound is at least . Dividing that by the interpolation bound gives a ratio of about . That ratio is the paper's headline stability argument.
Results
Perplexity on long text (PG-19 books, 7B model). Direct fine-tuning to 8192 tokens gave perplexity 7.21 at 2048 tokens and 7.69 at 8192, so the longer window made it worse. Position Interpolation to 8192 gave 7.13 at 2048 and 6.95 at 8192. Extended to 32768, the model reached 6.77 at 32768. The 13B and 33B models showed the same pattern, and the 65B model went from 5.49 for the original model at 2048 to 5.42 at 2048 and 5.37 at 8192 after PI.
Passkey retrieval. A random five-digit passkey is hidden in a long document, and the model must repeat it. For a 7B model fine-tuned directly, the effective window grew only from 2048 to 2560 tokens even after 10000 steps. Every Position Interpolation model at 8192, 16384 and 32768 tokens reached its target window by 200 steps. The one exception is the 7B model at 32768, which dipped to 18432 at 600 steps before returning to 32768.
Short-context benchmarks. Within the original 2048 window, the extended models lose a little. For the 7B model, BoolQ falls from 76.1 at 2048 to 73.2 at 8192 and 64.7 at 32768, while PIQA moves from 78.9 to 78.2 and 77.2. The authors report comparable results on most tasks, with degradation of up to about 2 percent, and single out BoolQ, which needs attention to word order in a short passage.
Long document summarization (GovReport). A 7B model extended to 16384 tokens, with minimal hyper-parameter tuning, reached ROUGE-1 60.0, against 58.7 for CoLT5 Base and 61.3 for CoLT5 XL. ROUGE-2 was lower than both CoLT5 models.
Limits. Only LLaMA models were tested, and only with RoPE. The fine-tuning data was the Pile, with a RedPajama check. Regularising the query and key magnitudes, which the theory suggests could also stabilise extrapolation, is left for future work.
Misreadings to avoid
It is tempting to think that a longer window means the model uses more of it. In fact the model only accepts more tokens, and whether it retrieves a fact from far back depends on attention. This is why the authors measured the effective window separately with the passkey test.
It is tempting to think that interpolation adds new positions. In fact it packs the new range into the old one, so neighbouring tokens are now closer in position than before. A small loss on short inputs is expected: the paper reports 0.01 to 0.05 perplexity on short proof-pile text.
It is tempting to think that fine-tuning is what makes the method work. In fact the model already has some language-modelling ability right after scaling, with perplexity below 20 at 8192 tokens before any training, against above for direct extrapolation. Fine-tuning pushes it further.
Use PI when you have a pretrained RoPE model, a modest budget, and a target window that is a moderate multiple of the original. Do not use it when the model uses a different position scheme, since the argument relies on RoPE's smooth score, or when short-context quality must be exact. Alternatives include training with long windows from the start, which is costly, and extrapolation methods such as ALiBi, which need a model trained with them. The paper also notes that LoRA fine-tuning worked in community tests, though the paper itself does not test it.
Reading guide
Read the paper in this order, with the lessons it needs at each step:
- Section 1, Introduction. Needs Transformer and Language Model for the context-window idea, and Rotary Position Embedding for why LLaMA extrapolates poorly.
- Section 2.1, RoPE background. Needs Rotary Position Embedding, Complex Numbers and Trigonometric Functions. The derivation of the score as a sum of cosines uses Euler's Formula.
- Section 2.2, Direct extrapolation. Needs Positional Encoding and Perplexity to read the failure numbers.
- Section 2.3, Position Interpolation and Theorem 2.1. Needs Linear Interpolation for the straight-line guess and its error.
- Section 3, Experiments. Needs Fine-Tuning for the training setup and Perplexity for the tables.
- Appendix A and B. Taylor expansion and the bound on ; read after Section 2.3.
Chain of ideas in this lesson: a RoPE score depends only on span and is smooth in it; extrapolation asks for spans never seen, where the score can blow up; scaling every position by keeps all spans inside the trained range; the straight-line error is about 600 times smaller than the extrapolation bound; about 1000 steps of fine-tuning finish the adaptation.
Check yourself
QuestionA model was pretrained with a 2048-token window. Under Position Interpolation, to what range are the scaled positions mapped when the new window is 8192 tokens?Show answer
The scale factor is . Every position in becomes , so the scaled positions lie in , exactly the range the model was pretrained on.
QuestionPosition Interpolation uses . A query is at position 6000 and a key at position 2000. What is the scaled span, and why is that good?Show answer
Scaled positions are and . The scaled span is . The unscaled span would have been , which is outside the pretrained range; the scaled span of is inside it, so the score is one the model already knows how to produce.
QuestionWhy does the paper prefer interpolation to extrapolation, even though both are formulas for the same attention score?Show answer
Interpolation estimates a score between two grid points that the model already handles, and its error is bounded by , which is small. Extrapolation evaluates the trigonometric sum at spans beyond training, where the trigonometric basis can produce large values; its bound is at least in the paper's numerical check. The ratio is about 600.
QuestionApplied scenario: a team extends a RoPE model from 4096 to 16384 tokens with Position Interpolation and fine-tunes it for 1000 steps. Their short-prompt benchmark drops slightly. Is this a bug?Show answer
Probably not. Interpolation packs the longer range into the old one, so short inputs see positions that are a little more crowded than before. The paper observed small degradations within the original window and reports them as expected. The team should measure the drop against their needs, and check that the long-context gains are present.
Sources
- Chen, Wong, Chen, Tian (2023), Extending Context Window of Large Language Models via Positional Interpolation, arXiv:2306.15595. https://arxiv.org/abs/2306.15595
- DOI for the same paper: https://doi.org/10.48550/arXiv.2306.15595