NTK-aware Scaling › From Intuition to Formula: The NTK-aware Approach

Michael BrenndoerferUpdated July 1, 202514 min read
Language AI HandbookAll chapters
Part 18 of 67Long Context
Chapter 128NTK-aware Scaling › From Intuition to Formula: The NTK-aware Approach
Reading section 3 of 14 · From Intuition to Formula: The NTK-aware Approach · 14 minSection 3 of 14 · 14 min

Book contents

Section 3 of 14 · 14 min read

NTK-aware Scaling › From Intuition to Formula: The NTK-aware ApproachOne section · Sections in this chapter3 / 14 ↓
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

Now that we've seen the problem with uniform scaling, let's develop a solution from first principles. The journey from intuition to a working formula requires answering three questions: What property do we want? How can we achieve it mathematically? And does the result match our expectations?

Neural Tangent Kernel (NTK)

The Neural Tangent Kernel describes how neural networks behave during training in the infinite-width limit. In this context, "NTK-aware" refers to preserving the high-frequency components that the network needs to learn fine-grained distinctions between nearby positions. The term was coined in a 2023 Reddit post by user "bloc97" who made the connection between neural network frequency sensitivity and the mathematical properties of the NTK, motivating a scaling strategy that respects the model's learned frequency preferences.

Historical Context

NTK-aware scaling emerged not from a formal research paper but from a Reddit post in May 2023, written by a researcher working independently to extend LLaMA context windows. The poster observed that Position Interpolation's uniform frequency compression conflicted with what NTK theory predicts about how neural networks use frequency information. The insight spread quickly through the open-source community and was rapidly validated experimentally. Within weeks it became a standard technique incorporated into LLaMA derivative models including Code Llama and Mistral. This is a striking example of the open community driving fundamental technique improvements in parallel with formal academic channels.

The Core Insight: Frequency Bands Serve Different Purposes

The key realization is that not all frequencies are equal in importance. Think of RoPE's multi-frequency design like a ruler with both centimeter and millimeter markings. The millimeter marks (high frequencies) give you precision for small measurements, while the centimeter marks (low frequencies) help you measure longer distances quickly. Position Interpolation is like shrinking the entire ruler by 8x, making even the millimeter marks too small to read. What we really want is to keep the millimeter precision while adjusting the centimeter scale.

Another useful analogy: think of the frequency dimensions as instruments in an orchestra. The high-frequency dimensions are like the piccolo and violins, playing rapid, precise notes that define the immediate rhythmic detail of the music. The low-frequency dimensions are like the bass and cello. This provides the slow harmonic foundation that gives the piece its overall structure. Position Interpolation is like telling every instrument to play at 1/8 tempo, which ruins the fast parts even though the slow parts could tolerate it. A better approach would be to slow only the bass section while keeping the violins at full tempo.

Translating this intuition into concrete requirements:

  1. Preserve high frequencies: The fastest-rotating dimension pairs (small ii) should remain unchanged. These are our "millimeter marks" for local position distinctions.

  2. Scale low frequencies: The slowest-rotating dimension pairs (large ii) can be compressed by the full factor ss. These are our "centimeter marks" that need adjustment for longer contexts.

  3. Smooth transition: Intermediate dimensions should scale gradually between these extremes, not abruptly jump.

These three requirements are not arbitrary design choices. They follow from thinking about what the model learned during training. For a model trained on sequences up to LtrainL_{\text{train}} tokens, every positional relationship it has seen involves positions within that range. High-frequency dimensions complete many full rotations within that range and therefore carry meaningful, learned patterns. Low-frequency dimensions may only complete a fraction of a rotation, so extending the context just means continuing the pattern they were already set up to handle.

Designing the Solution: Modify the Base

How can we achieve dimension-dependent scaling? The original RoPE frequency formula is θi=1/b2i/d\theta_i = 1/b^{2i/d}. Position Interpolation modifies the output by dividing: θi′=θi/s\theta_i' = \theta_i / s. This gives uniform scaling because every frequency is divided by the same constant.

Instead, we'll modify the input: the base bb. This is the key creative step in the derivation. By operating on the base rather than the output, we can exploit the mathematical structure of the exponential to get dimension-dependent effects automatically.

If we replace bb with a larger base b′b', all frequencies decrease (slower rotation). The key insight is that the exponent 2i/d2i/d causes this decrease to affect different dimensions differently:

  • When i=0i = 0: θ0′=1/(b′)0=1\theta_0' = 1/(b')^0 = 1. No matter what b′b' is, the highest frequency remains 1.
  • When ii is large: θi′=1/(b′)2i/d\theta_i' = 1/(b')^{2i/d} shrinks more because the exponent is larger.

This is exactly the property we want. By choosing the right b′b', we can leave high frequencies untouched while scaling low frequencies by whatever factor we need. The mathematical structure of the exponential gives us the smooth, dimension-dependent transition between these extremes as a natural consequence, without any ad hoc weighting or interpolation scheme.

Notice the elegance here: we do not need to specify how each individual dimension should be scaled. We just need to find one number, b′b', and the exponential structure of the formula takes care of distributing the scaling correctly across all dimensions. This is a beautiful example of how choosing the right mathematical representation of a problem can make the solution fall out almost automatically.

Out[5]:
Visualization
2D scatter plot showing token positions rotated by high-frequency dimension under different scaling methods.
High-frequency dimension (i=0): Original RoPE rotates by 1 radian per position, creating widely spaced points. Position Interpolation compresses to 0.125 rad/pos, making adjacent positions nearly overlap. NTK-aware preserves the original spacing.
2D scatter plot showing token positions rotated by low-frequency dimension under different scaling methods.
Low-frequency dimension (i=31): All methods produce similar results here. NTK-aware and Position Interpolation both apply full compression to low-frequency dimensions.

This geometric view makes the difference concrete. In the high-frequency dimension (left), original RoPE spreads positions around the circle, allowing the model to distinguish them easily. Position Interpolation bunches them together near the starting point, reducing distinguishability. NTK-aware scaling preserves the original spread. In the low-frequency dimension (right), all methods behave similarly since both Position Interpolation and NTK-aware apply full compression to these slow-rotating dimensions.

Deriving the Formula

Let's work backward from our requirements to find the exact formula for b′b'. We want a scaling function where:

  • scale0=1\text{scale}_0 = 1 (no scaling at highest frequency)
  • scaled/2−1=s\text{scale}_{d/2-1} = s (full scaling at lowest frequency)

We write the modified base as b′=b⋅αb' = b \cdot \alpha for some multiplier α\alpha to be determined. This parameterization is useful because it lets us express the new base as a proportional increase over the old one, and we can reason about the multiplier α\alpha independently. Substituting into the frequency formula:

θi′=1(b⋅α)2i/d\theta_i' = \frac{1}{(b \cdot \alpha)^{2i/d}}

Using the exponent rule (ab)n=an⋅bn(ab)^n = a^n \cdot b^n, we can separate this:

θi′=1b2i/d⋅1α2i/d=θi⋅α−2i/d\theta_i' = \frac{1}{b^{2i/d}} \cdot \frac{1}{\alpha^{2i/d}} = \theta_i \cdot \alpha^{-2i/d}

The effective scaling factor for dimension ii becomes:

scalei=θiθi′=α2i/d\text{scale}_i = \frac{\theta_i}{\theta_i'} = \alpha^{2i/d}

Now we apply our constraints. At i=0i = 0, we need scale0=1\text{scale}_0 = 1:

scale0=α2⋅0/d=α0=1(as required)\text{scale}_0 = \alpha^{2 \cdot 0/d} = \alpha^0 = 1 \quad \text{(as required)}

This is automatically satisfied for any α>0\alpha > 0, since any number raised to zero equals one. Our first requirement is met regardless of what α\alpha we choose.

At i=d/2−1i = d/2 - 1 (the last dimension pair), we need scaled/2−1=s\text{scale}_{d/2-1} = s:

scaled/2−1=α2(d/2−1)/d=s\text{scale}_{d/2-1} = \alpha^{2(d/2 - 1)/d} = s

Simplifying the exponent step by step:

α(d−2)/d=s\alpha^{(d - 2)/d} = s

To solve for α\alpha, we raise both sides to the power d/(d−2)d/(d-2):

α=sd/(d−2)\alpha = s^{d/(d-2)}

This gives us the complete NTK-aware formula:

b′=b⋅sdd−2b' = b \cdot s^{\frac{d}{d - 2}}

where:

  • b′b': the modified base for NTK-aware scaling
  • bb: the original base (typically 10000)
  • ss: the context extension factor (e.g., s=8s = 8 to extend from 4k to 32k tokens)
  • dd: the embedding dimension
  • d/(d−2)d/(d-2): an exponent slightly greater than 1 (for d=64d = 64, this equals 64/62≈1.03264/62 \approx 1.032)

The exponent d/(d−2)d/(d-2) is worth examining closely. For large dd, this converges toward 1, meaning the formula approaches b′=b⋅sb' = b \cdot s. For small dd, it is significantly larger than 1. In practice, the head dimension dd used in modern transformers is typically 64 or 128, so the exponent is either ≈1.032\approx 1.032 or ≈1.016\approx 1.016. These are small but meaningful corrections that ensure the boundary condition at i=d/2−1i = d/2 - 1 is met exactly. If you were to mistakenly use b′=b⋅sb' = b \cdot s instead of the correct formula, the lowest-frequency dimension would be scaled by slightly less than ss, producing a small but systematic mismatch at the low-frequency end of the spectrum.

Understanding the Resulting Frequencies

Substituting the new base into the frequency formula reveals how each dimension is affected. Starting from the frequency definition, replacing bb with b⋅sd/(d−2)b \cdot s^{d/(d-2)}, and simplifying:

θi′=1(b′)2i/d=1(b⋅sd/(d−2))2i/d=θi⋅s−2i/(d−2)\theta_i' = \frac{1}{(b')^{2i/d}} = \frac{1}{(b \cdot s^{d/(d-2)})^{2i/d}} = \theta_i \cdot s^{-2i/(d-2)}

The effective scaling factor for each dimension, defined as the ratio of the original frequency to the new frequency, is:

scalei=θiθi′=s2id−2\text{scale}_i = \frac{\theta_i}{\theta_i'} = s^{\frac{2i}{d - 2}}

Let's verify this matches our design goals:

  • When i=0i = 0 (highest frequency): scale0=s0=1\text{scale}_0 = s^0 = 1. The highest frequencies are not scaled at all. ✓
  • When i=d/2−1i = d/2 - 1 (lowest frequency): scaled/2−1=s(d−2)/(d−2)=s\text{scale}_{d/2-1} = s^{(d-2)/(d-2)} = s. The lowest frequencies are scaled by the full factor. ✓
  • Intermediate dimensions: Scaling increases smoothly from 1 to ss as ii increases. ✓

This is precisely what we wanted: high frequencies preserved, low frequencies scaled, smooth transition between.

Notice also that the scaling exponent 2i/(d−2)2i/(d-2) grows roughly linearly with dimension index ii. This means the scaling factor is exponential in the dimension index, which mirrors the original exponential spacing of the frequencies. In other words, NTK-aware scaling respects the logarithmic structure of RoPE. It does not arbitrarily compress some frequencies more than others; it applies compression in a way that is proportional to the frequency's original "slowness."

A Worked Example

Let's make this concrete with typical values: d=64d = 64 dimensions, s=8s = 8 extension factor (4k to 32k tokens). This is a realistic scenario corresponding to extending LLaMA-2, which was trained with a 4,096-token context, to a 32,768-token context window.

First, compute the NTK exponent:

dd−2=6462≈1.032\frac{d}{d-2} = \frac{64}{62} \approx 1.032

The exponent is only slightly above 1, which means the modified base is only slightly larger than what you would get by simply multiplying bb by ss. This small correction is what makes the last dimension pair satisfy the boundary condition exactly.

The modified base becomes:

b′=10000⋅81.032=10000⋅8.72≈87,200b' = 10000 \cdot 8^{1.032} = 10000 \cdot 8.72 \approx 87,200

In practice, this means the model will behave as if it was trained with a base of 87,200 rather than 10,000. You can think of this as a kind of "stretching" of the frequency spectrum: by raising the base, we make all frequencies slightly smaller (slower rotation), but the effect is much stronger at the low-frequency end because those dimensions have a large exponent in the denominator.

Now we can compute scaling factors for specific dimensions:

NTK-aware scaling factors for d=64d=64, s=8s=8. High-frequency dimensions (small ii) are preserved while low-frequency dimensions receive full scaling.
Dimension iiExponent 2i/(d−2)2i/(d-2)scalei=8exp\text{scale}_i = 8^{\text{exp}}Interpretation
001.00No scaling (preserved)
80.2581.72Slight scaling
160.5162.95Moderate scaling
240.7745.06Significant scaling
311.008.00Full scaling

The gradient is smooth and continuous. High-frequency pairs (small ii) stay close to their original values, while low-frequency pairs (large ii) are compressed to fit the extended context. Notice that the "moderate scaling" region, around dimension pairs 12 to 20, corresponds to the intermediate frequencies that provide mid-range positional encoding, useful for tracking structure at the paragraph level rather than the word or document level. NTK-aware scaling applies intermediate compression here, which is intuitively the right thing to do: these dimensions can tolerate some compression, but not the full factor.

Out[6]:
Visualization
Line plot showing uniform scaling for Position Interpolation and progressive scaling for NTK-aware method across dimension indices.
Scaling factors across dimension pairs for Position Interpolation vs NTK-aware scaling. Position Interpolation applies uniform scaling (flat line), while NTK-aware scaling preserves high frequencies (left) and only compresses low frequencies (right).

The visualization confirms our mathematical analysis. The NTK-aware curve (squares) starts at 1 for the highest-frequency pairs and smoothly increases to the full scale factor of 8 for the lowest-frequency pairs. Meanwhile, Position Interpolation (circles) maintains a flat line at 8, treating all dimensions identically.