YaRN › The YaRN Solution
Language AI HandbookAll chapters YaRN › The YaRN SolutionOne section · Sections in this chapter4 / 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
YaRN combines two complementary mechanisms to enable high-quality context extension. Neither mechanism is sufficient on its own: the frequency adjustment alone does not fix the entropy problem, and the temperature correction alone does not fix the frequency distortion. But together, they address both sources of degradation in a way that is both principled and computationally lightweight.
The first mechanism is ramp-based frequency interpolation. Rather than treating all dimension pairs equally (like PI) or smoothly transitioning across all pairs using a single global parameter (like NTK), YaRN uses a ramp function that applies no interpolation to high-frequency pairs, full interpolation to low-frequency pairs, and a smooth linear transition in between. The threshold between "needs interpolation" and "does not need interpolation" is based on whether the pair's rotation wavelength fits within the original training context. This makes the interpolation decision interpretable and motivated by the actual frequency behavior of each dimension.
The second mechanism is attention temperature scaling. YaRN introduces a scaling factor applied to attention scores before softmax, where is computed from the extension factor via an empirically derived formula. This scaling increases the spread of attention scores, counteracting the compression effect of interpolation. When (no extension), and no scaling is applied, preserving the original behavior exactly.
The combined approach modifies both the rotation frequencies and the attention computation. For frequencies, YaRN applies a dimension-specific adjustment. The core idea is to compute a modified frequency for each dimension pair :
where:
- : the adjusted frequency for dimension pair after YaRN modification
- : the original RoPE frequency for dimension pair , computed as
- : the interpolation factor, a value between and that depends on the wavelength ratio for this dimension
- : the wavelength-to-context ratio for dimension pair , comparing this pair's rotation period to the training context length
- : the wavelength for dimension pair , measuring how many sequence positions correspond to one full rotation
- : the original training context length (e.g., 2048 or 4096 tokens)
Why does this formula make sense? Notice that when , the adjusted frequency equals the original frequency: no change. When , the adjusted frequency is scaled down by exactly the extension factor, which is equivalent to Position Interpolation applied to just this dimension. The ramp function interpolates between these extremes based on how much this particular frequency needs correction.
For attention scores, YaRN introduces a temperature correction:
where:
- : the temperature-adjusted attention score between positions and
- : the temperature scaling factor, where increases score magnitude and sharpens the attention distribution
- : the temperature parameter, computed from the extension factor via
- , : the YaRN-rotated query and key vectors at positions and
- : the dimension of key vectors, used for the standard scaling
The scaling factor appears before the softmax, not after. Multiplying pre-softmax scores by a constant sharpens the distribution (for ) in the same way that reducing temperature does in generative sampling. YaRN uses this mechanism to counteract the entropy increase from frequency interpolation.
Let's develop each component in detail.