Skip to content

Position Interpolation: Context Extension Without Retraining

Summary

Position interpolation is a group of scaling methods for positional encoding that allows models to work with longer contexts than during training without full retraining.

The Problem of Exceeding Training Limits

Models with RoPE are trained on a fixed maximum length. When presented with longer sequences, positional phases exceed the range observed during training, leading to a sharp decline in quality: attention is distributed incorrectly, perplexity increases.

Main Methods

Method Idea Training
Linear Interpolation Divide positions by a scaling factor Not required
NTK-aware scaling Non-linearly adjust the base of RoPE by frequencies Not required
YaRN Combination of NTK and attention temperature scaling Short phase
Dynamic NTK Recalculate the base under the actual input length Not required

Linear Interpolation

Position p is replaced by p / s, where s is the scaling factor. All positions are compressed into the original range, but high-frequency components lose resolution.

def scale_positions(positions, scale):
    return positions / scale

NTK-aware scaling

The base of RoPE theta is adjusted according to a formula dependent on the scaling factor:

theta_new = theta * scale ** (dim / (dim - 2))

This preserves resolution at high frequencies and compresses low ones, leading to less degradation on short sequences.

YaRN

YaRN combines frequency-dependent scaling with softmax attention temperature correction. Requires short fine-tuning on long examples but provides better quality at the target length.

Application Scheme

flowchart TD
    A[Request of length L] --> B{L > L_train?}
    B -->|no| C[Standard RoPE]
    B -->|yes| D[Choose method]
    D --> E[Linear interpolation]
    D --> F[NTK-aware]
    D --> G[YaRN]
    E --> H[Inference]
    F --> H
    G --> H

Impact on Quality

  • Short sequences may degrade due to phase compression.
  • Tasks requiring precise positional search (retrieval within context) suffer more than generation.
  • The effect of lost-in-the-middle is amplified with aggressive scaling.

Practical Notes

  • Scaling factors above 4–8 without retraining are usually unacceptable.
  • The method is set at the inference-engine level, not in the prompt.
  • Changing the method alters the output distribution — re-evaluation is required.
  • Compatibility with KV-cache is maintained as only position calculation changes.

Related materials: context-window, attention, rope, context-rot.