BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / Paper implementations19 of 28 in topic

846. YaRN frequency ramp and attention temperature

HardPaper implementations Pro

Why it matters YaRN is how many open models (Qwen, DeepSeek, Mistral variants) ship 128k context. Knowing the ramp separates people who read the paper from people who copied a config flag.

Paper: "YaRN: Efficient Context Window Extension of Large Language Models", Peng, Quesnelle, Fan and Shippole, 2023 (ICLR 2024), arXiv 2309.00071.

YaRN does not scale every RoPE frequency the same way. For each dimension pair d (d = 0 .. dim/2 - 1):
- theta_d = base ** (-2d / dim) and wavelength lambda_d = 2 * pi / theta_d.
- r = L / lambda_d, where L is the original context length.
- ramp gamma(r) = 0 if r < alpha, 1 if r > beta, else (r - alpha) / (beta - alpha).
- new frequency h(theta_d) = (1 - gamma) * theta_d / s + gamma * theta_d, where s is the extension factor.

So long wavelengths (few rotations inside L) are interpolated fully, short wavelengths are left alone, and the ones in between are blended. YaRN also scales attention logits; the paper's recommended factor is sqrt(1/t) = 0.1 * ln(s) + 1.

Write yarn_frequencies(dim, base, scale, orig_len, alpha=1.0, beta=32.0).

Inputs: dim is an even int, base and scale are floats (scale >= 1), orig_len is an int.

Return a tuple (freqs, mscale): freqs is an ndarray of floats with shape (dim // 2,), mscale is a float. When scale == 1, freqs equal theta and mscale is 1.0. No rounding.

Functiondef yarn_frequencies(dim, base, scale, orig_len, alpha=1.0, beta=32.0)

Example 1

Input: yarn_frequencies(8, 10000.0, 4.0, 2048)

Output: ([1.0, 0.1, 0.003, 0.00025], 1.1386)

Example 2

Input: dim = 8, base = 10000.0, scale = 1.0, orig_len = 2048

Output: ([1.0, 0.1, 0.01, 0.001], 1.0)

Constraints

  • Python 3 with NumPy. Code runs in your browser.
  • Run checks the sample tests. Submit checks the full hidden test set (Pro).
Hint 1

Build theta, then wavelength = 2 pi / theta, then r = orig_len / wavelength.

Hint 2

gamma is (r - alpha) / (beta - alpha) clipped to [0, 1].

Hint 3

Blend: (1 - gamma) * theta / s + gamma * theta, and mscale = 0.1 * ln(s) + 1.

Common mistakes
  • Reversing the ramp so high frequencies get scaled.
  • Using the number of rotations per token instead of per context window.
  • Applying ln with base 10.
Understand the idea

High-frequency dimensions rotate many times inside the original window, so the model already knows every phase and they should not change. Low-frequency dimensions rotate less than once, so their new phases would be unseen and they must be interpolated by 1/s. YaRN measures this with r = L / wavelength and blends with a linear ramp between alpha and beta. With dim 8, base 10000 and L 2048, r is about [326, 32.6, 3.26, 0.33]: the first pair stays, the second is barely touched, the third is partly scaled and the last is divided by s.

The temperature 0.1 ln(s) + 1 is about 1.1386 for s = 4.

Shapes and data flow
freqs: (dim/2,). mscale: scalar.
Topics
Paper implementations
Similar questions
  • FlashAttention-style tiled causal attention forward (Dao et al., 2022)Hard
  • Mamba-style selective state-space scan with zero-order-hold discretizationHard
  • Implement recurrent retention from RetNetHard
  • Implement differential attention with the lambda reparameterizationHard
All Paper implementations problems
Report
846 / 903
Python 3solution.pyPython idle
1
2
3
4
5
6
import numpy as np
​
def yarn_frequencies(dim, base, scale, orig_len, alpha=1.0, beta=32.0):
# your code here
pass
​

Run checks these cases in your browser, no sign-in needed. Submit checks the full hidden test set with Pro.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS