YaRN › Wavelength Analysis

Michael BrenndoerferUpdated June 28, 20255 min read
Language AI HandbookAll chapters
Part 18 of 67Long Context
Chapter 129YaRN › Wavelength Analysis
Reading section 5 of 16 · Wavelength Analysis · 5 minSection 5 of 16 · 5 min

Book contents

Section 5 of 16 · 5 min read

YaRN › Wavelength AnalysisOne section · Sections in this chapter5 / 16 ↓
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

To understand why some dimension pairs need interpolation while others don't, we need to think about rotation in terms of wavelength rather than frequency. While frequency tells us how fast something rotates (radians per position), wavelength tells us how far we must travel for one complete cycle (positions per full rotation). Wavelength provides a more intuitive picture because we can directly compare it to the context length: if a wavelength is shorter than the context, the pair completes multiple full rotations during training; if longer, it completes only a fraction of one rotation.

Think of a dimension pair's rotation as a clock hand sweeping around a circle. A high-frequency pair is like a second hand: it completes many full rotations in the span of the training context. Every possible angle has been visited many times. The model knows how to use all parts of the rotation cycle to distinguish positions. A low-frequency pair is like an hour hand: it moves very slowly, covering only a small arc during training. The model has only seen a fraction of the rotation cycle, and it uses that small arc to encode position. If we extend the context beyond training, the second-hand pair has no problem: it just keeps spinning. But the hour-hand pair is now asked to cover territory it has never visited, and extrapolation fails.

The wavelength λi\lambda_i of dimension pair ii is the inverse of the frequency, scaled by 2π2\pi:

λi=2πθi=2π⋅100002i/d\lambda_i = \frac{2\pi}{\theta_i} = 2\pi \cdot 10000^{2i/d}

where:

  • λi\lambda_i: the wavelength (in positions) for dimension pair ii, representing how many sequence positions correspond to one complete rotation cycle
  • θi=1/100002i/d\theta_i = 1/10000^{2i/d}: the base frequency for dimension pair ii, measured in radians per position
  • dd: the total embedding dimension (must be even, since RoPE operates on pairs)
  • ii: the dimension pair index, ranging from 00 to d/2−1d/2 - 1
  • 2π2\pi: the number of radians in a complete rotation (one full cycle)
  • 1000010000: the RoPE base constant, which controls the overall frequency range across dimensions

The second equality follows from substituting the definition of θi\theta_i and simplifying: 2π/(1/100002i/d)=2π⋅100002i/d2\pi / (1/10000^{2i/d}) = 2\pi \cdot 10000^{2i/d}.

Why does this formula make sense? Notice that as ii increases (moving to higher dimension pair indices), the exponent 2i/d2i/d grows from 0 toward 2, making 100002i/d10000^{2i/d} grow from 1 toward 100002=10810000^2 = 10^8. So wavelengths grow exponentially with dimension pair index: the first pairs have very short wavelengths (high frequencies), and the last pairs have very long wavelengths (low frequencies). This exponential spread is what gives RoPE its ability to encode position at multiple scales simultaneously.

The critical question becomes: how does each wavelength compare to the training context length LL? If λi<L\lambda_i < L, the pair completes at least one full rotation during training, meaning the model has seen all possible rotation states. If λi>L\lambda_i > L, the pair completes less than one rotation, meaning the model has only seen a fraction of the rotation cycle. The ratio ri=λi/Lr_i = \lambda_i / L is central to YaRN's design: it tells us, for each dimension pair, how much of the rotation cycle the model has already explored.

In[9]:
Code
def compute_wavelengths(d_model, base=10000):
    """Compute wavelengths for each dimension pair."""
    frequencies = compute_rope_frequencies(d_model, base)
    wavelengths = 2 * np.pi / frequencies
    return wavelengths


# Analyze wavelengths relative to context length
d_model = 64
base = 10000
original_context = 2048  # Original training context
extension_factor = 4.0
extended_context = original_context * extension_factor

wavelengths = compute_wavelengths(d_model, base)
Out[10]:
Console
Original context: 2048 positions
Extended context: 8192 positions

Pair     Wavelength      vs Original L        vs Extended L       
---------------------------------------------------------------
0        6.3             0.0031               0.0008              
4        19.9            0.0097               0.0024              
8        62.8            0.0307               0.0077              
16       628.3           0.3068               0.0767              
24       6283.2          3.0680               0.7670              
31       47117.2         23.0065              5.7516

The "vs Original L" column shows the wavelength ratio ri=λi/Lr_i = \lambda_i / L. This ratio is the key to understanding which dimension pairs need interpolation:

  • When ri<1r_i < 1, the wavelength is shorter than the context, meaning the pair completes multiple full rotations during training. These pairs have learned the full rotation cycle and can handle extended positions without modification.
  • When ri>1r_i > 1, the wavelength exceeds the context, meaning the pair does not complete even one rotation during training. These pairs operate in a limited portion of the rotation cycle and need interpolation to avoid extrapolation.

Let's trace through the table to build concrete intuition. Dimension pair 0 has a wavelength of about 6 positions and r0≈0.003r_0 \approx 0.003. It completes roughly 340 full rotations within the training context, so it has thoroughly learned how to use rotation for position encoding across its entire cycle. When we extend to 8,192 positions, pair 0 still completes over 1,300 rotations. No problem here: it is operating well within familiar territory.

At the other extreme, dimension pair 31 has a wavelength of around 60,000 positions and r31≈29r_{31} \approx 29. During training, it completes only about 3% of a single rotation. The model has learned to encode position using just this small arc of the rotation cycle. If we extend the context by 4x without interpolation, we would ask pair 31 to cover 12% of its rotation cycle, using rotation states it has never encountered during training. This is extrapolation, and it systematically degrades model quality.

The pattern is clear: pairs with small rir_i do not need interpolation; pairs with large rir_i do. The question is where to draw the line, and whether the transition should be abrupt or gradual. An abrupt threshold would create a discontinuity in how different dimension pairs are treated, which could introduce artifacts in the attention computation. A smooth transition, on the other hand, allows adjacent dimension pairs to behave similarly, which YaRN achieves with its ramp function.