YaRN › The YaRN Ramp Function
Language AI HandbookAll chapters YaRN › The YaRN Ramp FunctionOne section · Sections in this chapter6 / 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
Now we can design a function that decides how much interpolation each dimension pair receives. We want three behaviors, each motivated by the wavelength analysis above.
First, for pairs with small (short wavelengths, many rotations during training): apply no interpolation, leaving them at their original frequencies. These pairs are perfectly capable of distinguishing positions across the extended context without any modification.
Second, for pairs with large (long wavelengths, few rotations during training): apply full interpolation, scaling them by just like Position Interpolation would. These pairs must be slowed down so that the extended context positions fall within the rotation states the model has already learned.
Third, for pairs with intermediate : apply partial interpolation, blending smoothly between the two extremes. This smooth transition prevents discontinuities and allows the model to adapt gradually from the no-interpolation regime to the full-interpolation regime.
This is exactly what a ramp function achieves. Think of it as a dimmer switch rather than an on-off switch: instead of suddenly jumping from "no interpolation" to "full interpolation," it slides smoothly across a transition zone. YaRN defines as a piecewise function:
where:
- : the wavelength-to-context ratio for dimension pair , comparing the rotation period to the training context length
- : the wavelength for dimension pair
- : the original training context length (e.g., 2048 or 4096 tokens)
- : the extension scale factor (e.g., 4 for extending from 2048 to 8192 positions)
- : the lower threshold (typically 1.0), below which no interpolation is applied to the dimension pair
- : the upper threshold (typically 32.0), above which full Position-Interpolation-style scaling is applied
- : the interpolation weight in the ramp region, ranging linearly from 0 (at ) to 1 (at )
- : the output interpolation factor, ranging from 1 (no interpolation, frequency unchanged) to (full interpolation, frequency divided by the extension factor)
Let's unpack the middle case, which is the most interesting and distinguishes YaRN from simpler approaches. The weight measures where falls within the transition region . At the left edge (), we have , so the expression becomes : the pair receives no interpolation. At the right edge (), we have , giving : the pair receives full interpolation. For values between the edges, we get a linear blend of the two extremes. This creates a smooth ramp rather than an abrupt step, which helps the model adapt more gracefully across dimension pairs.
Why does this formula make sense? Consider what means for the modified frequency: , unchanged. The pair rotates at its original speed, and positions in the extended context will be seen as extrapolated values. For high-frequency pairs with , this is fine because they complete so many rotations that the model has already generalized to all rotation states. Consider what means: , which is exactly what Position Interpolation would apply if used globally. For low-frequency pairs with , this scaling ensures that position in the extended context maps to the same rotation state as position in the original context.
The default thresholds have intuitive interpretations rooted in the wavelength analysis. When , dimension pairs with wavelength less than the context length (those completing at least one full rotation during training) are left unchanged. The model has visited all possible rotation states for these pairs and can handle any position without modification. When , dimension pairs with wavelength more than 32 times the context length (completing less than 3% of a single rotation during training) receive full interpolation. These pairs are so slow-moving that they must be slowed down by the full extension factor to stay within familiar territory. The ramp between and provides a smooth transition for the intermediate pairs that fall between these extremes. These values were empirically validated across multiple model architectures including LLaMA-2 and Mistral, and they strike a balance between preserving high-frequency information and ensuring stable extrapolation for low-frequency pairs.
To summarize, the ramp function creates three distinct regions:
-
Short wavelengths (): These pairs already rotate many times within the original context. They don't need interpolation because they can naturally handle extended positions.
-
Long wavelengths (): These pairs rotate slowly and need full Position Interpolation treatment to avoid operating outside their training distribution.
-
Middle wavelengths (): These pairs receive partial interpolation, smoothly blending between the two extremes.
def yarn_gamma(wavelength, context_length, scale, alpha=1.0, beta=32.0):
"""Compute YaRN interpolation factor for a given wavelength."""
r = wavelength / context_length
if r < alpha:
# Short wavelength: no interpolation
return 1.0
elif r > beta:
# Long wavelength: full interpolation
return 1.0 / scale
else:
# Ramp region: smooth transition
t = (r - alpha) / (beta - alpha)
# Linear interpolation between 1 and 1/scale
return (1 - t) * 1.0 + t * (1.0 / scale)
def compute_yarn_frequencies(
d_model, base=10000, context_length=2048, scale=4.0, alpha=1.0, beta=32.0
):
"""Compute YaRN-adjusted frequencies."""
original_freqs = compute_rope_frequencies(d_model, base)
wavelengths = compute_wavelengths(d_model, base)
gamma_values = np.array(
[yarn_gamma(w, context_length, scale, alpha, beta) for w in wavelengths]
)
# Apply gamma as a frequency multiplier
# gamma < 1 means we slow down the rotation (interpolation)
yarn_freqs = original_freqs * gamma_values
return yarn_freqs, gamma_valuesLet's visualize the ramp function and its effect on frequencies:
# Compute YaRN frequencies
d_model = 64
context_length = 512
scale = 4.0
yarn_freqs, gamma_values = compute_yarn_frequencies(
d_model, context_length=context_length, scale=scale
)
original_freqs = compute_rope_frequencies(d_model)
wavelengths = compute_wavelengths(d_model)
# Compute wavelength ratios
r_values = wavelengths / context_length

The ramp function creates a piecewise linear transition on the log-wavelength scale. The left panel shows how drops from 1 to as wavelength increases, with the smooth ramp connecting the two flat regions. The right panel shows the resulting frequency spectrum: YaRN preserves high frequencies at the left side of the dimension range (matching the original), then transitions smoothly to interpolated low frequencies on the right. This dimension-specific adjustment is the key innovation that sets YaRN apart from both PI and NTK-aware scaling.