BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / Attention and transformers11 of 23 in topic

401. Scale RoPE frequencies with NTK-aware interpolation

MediumAttention and transformers

Why it matters Long-context variants of Llama-family models were extended with this trick before YaRN.

To stretch a RoPE model to a longer context, NTK-aware scaling raises the frequency base instead of squashing positions.

Write rope_freqs_ntk(d, base=10000.0, scale=1.0). The new base is base * scale^(d / (d - 2)). Return the d // 2 inverse frequencies new_base^(-2i / d) for i = 0 .. d/2 - 1.

Functiondef rope_freqs_ntk(d, base=10000.0, scale=1.0)

Example 1

Input: rope_freqs_ntk(4, 10000, 1)

Output: [1, 0.01]

Example 2

Input: d = 8, base = 10000, scale = 4

Output: [1.0, 0.062996, 0.003969, 0.00025]

Example 3

Input: float(rope_freqs_ntk(64, 10000, 8)[0])

Output: 1.0

Constraints

  • Python 3 with NumPy. Code runs in your browser.
  • Your code must pass all 4 tests to be accepted.
Hint 1

With scale 1 this reduces to standard RoPE frequencies.

Hint 2

Change the base, not the positions.

Hint 3

Exponent d / (d - 2).

Common mistakes
  • Scaling the base linearly.
  • Dividing positions instead, which is plain interpolation.
Understand the idea

RoPE rotates query and key pairs by angles that grow with position, using one frequency per pair. A model trained on 4k tokens has never seen the large angles of 16k tokens. NTK-aware scaling raises the frequency base so the slowest frequencies slow down by the scale factor, while the fastest ones, which encode local order, stay almost the same.

Worked example

d = 4, base 10000, scale 4. The exponent d / (d - 2) is 2, so the new base is 10000 * 4^2 = 160000. Inverse frequencies are new_base^(-2i / d) for i = 0, 1: i = 0 gives 1, and i = 1 gives 160000^(-0.5) = 1 / 400 = 0.0025. The original was 10000^(-0.5) = 0.01, so the slowest frequency dropped by exactly 4, and the fastest stayed at 1.

new_base = base * scale^(d / (d - 2)); return new_base^(-2i / d) for i = 0 .. d/2 - 1

With scale 1 you get standard RoPE. Just multiplying the base by scale under-stretches the low frequencies. Used in: extending Llama style models to longer context without much fine-tuning.

Topics
Attention and transformers
Similar questions
  • GPT decoder: causal self-attention for one headMedium
  • Split and merge attention headsMedium
  • Build the ALiBi attention bias for every headMedium
  • Apply a key padding mask in batched attentionMedium
All Attention and transformers problems
Report
401 / 903
Python 3solution.pyPython idle
1
2
3
4
5
6
import numpy as np
​
def rope_freqs_ntk(d, base=10000.0, scale=1.0):
# your code here
pass
​

Run checks these cases in your browser, no sign-in needed. Submit checks all 4 tests.

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