YaRN › Wavelength Analysis
Language AI HandbookAll chapters 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 of dimension pair is the inverse of the frequency, scaled by :
where:
- : the wavelength (in positions) for dimension pair , representing how many sequence positions correspond to one complete rotation cycle
- : the base frequency for dimension pair , measured in radians per position
- : the total embedding dimension (must be even, since RoPE operates on pairs)
- : the dimension pair index, ranging from to
- : the number of radians in a complete rotation (one full cycle)
- : the RoPE base constant, which controls the overall frequency range across dimensions
The second equality follows from substituting the definition of and simplifying: .
Why does this formula make sense? Notice that as increases (moving to higher dimension pair indices), the exponent grows from 0 toward 2, making grow from 1 toward . 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 ? If , the pair completes at least one full rotation during training, meaning the model has seen all possible rotation states. If , the pair completes less than one rotation, meaning the model has only seen a fraction of the rotation cycle. The ratio is central to YaRN's design: it tells us, for each dimension pair, how much of the rotation cycle the model has already explored.
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)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 . This ratio is the key to understanding which dimension pairs need interpolation:
- When , 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 , 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 . 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 . 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 do not need interpolation; pairs with large 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.