NTK-Aware Scaling: Extending Context Without Fine-Tuning

6 minute read

Published:

TL;DR: RoPE encodes position through rotation frequencies \(\theta_i\). When you extend context beyond training length, high-frequency dimensions fail (they have seen all their cycles). NTK-Aware Scaling replaces the base (10000) with a larger value, spreading frequencies out so all dimensions remain useful at longer contexts, often with no additional training.
Paper: "Scaling Laws of RoPE-based Extrapolation"  ·  arXiv:2310.05209
Authors: Xiaoran Liu, Hang Yan, Shuo Zhang, Chenxin An, Xipeng Qiu, Dahua Lin
Venue: ICLR 2024  ·  📄 Read the paper
First page of the NTK scaling paper
Paper preview, Scaling Laws of RoPE-based Extrapolation (Liu et al., 2024).

The Context Extension Problem

RoPE (Rotary Position Embedding) encodes the position of each token by rotating query and key vectors at dimension-specific frequencies. A model trained with RoPE on sequences up to length \(L\) learns to use those frequencies, but when you try to run it on sequences longer than \(L\), the model sees rotation angles it has never encountered.

Naïve position interpolation (scaling positions linearly: \(\mathrm{pos} \to \mathrm{pos} \times L/L'\)) works but degrades high-frequency dimensions catastrophically, they change too fast across the rescaled positions, destroying local structure.

RoPE Frequencies: A Quick Recap

In RoPE, dimension pair \(i\) of a \(d_k\)-dimensional key or query is rotated by:

\[ \theta_i = \frac{1}{\mathrm{base}^{2i/d}}, \qquad i = 0, 1, \dots, d/2 - 1 \]

With \(\mathrm{base} = 10000\) (the original RoPE default), frequencies range from \(1\) (low-frequency, long-range position signal) to \(1/10000^{d/d} \approx 0.0001\) (high-frequency, fine-grained local signal).

High-frequency dimensions complete many rotation cycles within a short context window. Low-frequency dimensions rotate slowly across the full context.

Visual Intuition: Frequency Saturation

RoPE frequency dimensions at training length vs 4× extended context RoPE dimension index (low freq → high freq) rotation cycles at context length original L=2k context saturated! high-freq dims see too many cycles 4× extension (no fix) NTK-scaled (base↑) Training budget (2k) Naive 4× extension (breaks high-freq) NTK scaling (stretches all dims proportionally)
At 4× context extension without NTK scaling (red), high-frequency RoPE dimensions complete far more cycles than during training, their rotation angles enter unseen regimes (red zone). NTK scaling (purple dashed) raises the base value so all dimensions are stretched proportionally, keeping every dimension within a familiar regime.

What Breaks at Long Context

When context length exceeds training length, two problems arise:

  1. High-frequency dimensions have seen all their cycles, they wrap around and lose uniqueness. Two distant positions may map to nearly the same rotation angle.

  2. Attention patterns based on relative angles degrade, the model’s learned sense of “close” vs “far” tokens breaks down.

The NTK-Aware Scaling Insight

Proposed independently by /u/bloc97 on Reddit (2023) and connected to Neural Tangent Kernel theory, NTK-Aware Scaling replaces the base \(\theta\) with a larger value:

\[ \mathrm{base}_{\mathrm{new}} = \mathrm{base} \cdot \left( \frac{L'}{L} \right)^{d / (d-2)} \]

Where:

  • \(L\) = original training context length
  • \(L'\) = desired new context length
  • \(d\) = head dimension

For example, extending LLaMA (trained at \(L = 2048\)) to \(L' = 8192\):

\[ \mathrm{base}_{\mathrm{new}} = 10000 \cdot \left( \frac{8192}{2048} \right)^{128/126} \approx 10000 \cdot 4^{1.016} \approx 41400 \]

This larger base stretches all frequencies proportionally. High-frequency dimensions that previously completed a full cycle within \(L\) tokens now complete their cycle within \(L'\) tokens, no dimension becomes “saturated” at the new length.

Why NTK? The NTK connection comes from viewing the Transformer as a kernel machine in function space. When you change context length, you are effectively changing the kernel's support. The frequency scaling ensures the kernel remains well-conditioned, similar in spirit to how NTK theory analyzes function space behaviour under parameter changes.
RoPE as the basis that NTK scaling modifies
NTK-aware scaling is a way to retune RoPE so its frequency spectrum remains useful at longer context lengths.

NTK vs Linear Interpolation

MethodHigh-freq dimsLow-freq dimsFine-tuning needed
Linear interpolationSeverely degradedGoodOften needed
NTK scalingPreservedGoodUsually not needed

Linear interpolation scales positions but keeps frequencies fixed, the high-frequency dimensions see too many cycles per unit position. NTK scaling changes the frequencies to match the new scale.

Worked Example: Computing the NTK Base

Model: LLaMA-2 7B, trained at \(L = 4096\), head dimension \(d = 128\), original \(\mathrm{base} = 10{,}000\).

Target: extend to \(L' = 32{,}768\) (8× extension)

\[ \begin{aligned} \mathrm{base}_{\mathrm{new}} &= 10{,}000 \times \left( \frac{32768}{4096} \right)^{128 / (128-2)} \\ &= 10{,}000 \times 8^{128/126} \\ &= 10{,}000 \times 8^{1.016} \\ &= 10{,}000 \times 8.36 \\ &\approx \mathbf{83{,}600} \end{aligned} \]

The new base of ~83,600 means every RoPE frequency \(\theta_i = 1/\mathrm{base}^{2i/d}\) is reduced by a factor of ~8×, spreading cycles proportionally over 8× more tokens.

For dimension \(i = 0\) (lowest frequency):

  • Original: \(\theta_0 = 1/10{,}000^{0} = 1.0\) (full rotation per token, highest freq)
  • After NTK: \(\theta_0 = 1/83{,}600^{0} = 1.0\) (unchanged, already handles short range fine)

For dimension \(i = 63\) (highest frequency of the pair, near \(d/2\)):

  • Original: \(\theta_{63} = 1/10{,}000^{126/128} \approx 1/7{,}244 \approx 0.000138\)
  • After NTK: \(\theta_{63} = 1/83{,}600^{126/128} \approx 1/60{,}600 \approx 0.0000165\)

The highest-frequency dimension now completes its cycle every ~60,600 tokens instead of ~7,244, scaled with the 8× target extension.

Dynamic NTK Scaling

A practical variant applies NTK scaling dynamically at inference time, adjusting the base only for sequences that exceed the training length:

def get_ntk_base(seq_len, training_len=2048, base=10000, dim=128):
    if seq_len <= training_len:
        return base
    scale = seq_len / training_len
    return base * (scale ** (dim / (dim - 2)))

This is zero-cost for short sequences and automatically extends context for long ones. LLaMA.cpp and many inference engines implement this by default.

Limitations

  • NTK scaling degrades gradually as \(L'/L\) increases. At 8× extension (e.g., 2k → 16k), quality noticeably drops without at least a small amount of fine-tuning.
  • It is a post-hoc fix, not a principled training strategy. For best long-context performance, fine-tuning with the new scale (or using YaRN) is recommended.
  • It does not address the attention sink problem, very long sequences still have attention pattern degradation.

Summary

PropertyValue
Core ideaRescale RoPE base to stretch frequencies to longer contexts
Fine-tuningNot required for moderate extension (2-4×)
Quality at 8×Degrades; short fine-tune recommended
ImplementationSingle hyperparameter change (new base value)
Relation to linear interpolationComplementary, fixes what interpolation breaks

NTK-Aware Scaling is the simplest way to extend the context of an existing RoPE model. For more sophisticated extension, see YaRN.

References