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

845. Position Interpolation for RoPE angles

EasyPaper implementations

Why it matters Long-context fine-tunes of LLaMA-style models started with this trick. Interviewers ask why interpolation beats extrapolation for RoPE.

Paper: "Extending Context Window of Large Language Models via Positional Interpolation", Chen, Wong, Chen and Tian, 2023, arXiv 2306.15595.

A model trained with RoPE on L tokens breaks on longer inputs because it never saw large rotation angles. Position Interpolation rescales positions instead: position m is fed to RoPE as m * L / L', where L' is the new context length. RoPE itself uses theta_i = base ** (-2i / dim) for i = 0 .. dim/2 - 1, and the angle for position p is p * theta_i.

Write pi_rope_angles(positions, dim, base, train_len, target_len).

Inputs:
- positions: a list or ndarray of integer positions, shape (n,). Use np.asarray(positions, dtype=float).
- dim: an even int, the head dimension.
- base: a float, usually 10000.0.
- train_len, target_len: ints. If target_len <= train_len, do not scale (scale factor 1).

Return an ndarray of floats with shape (n, dim // 2), where entry [j, i] is (positions[j] * train_len / target_len) * theta_i. No rounding.

Functiondef pi_rope_angles(positions, dim, base, train_len, target_len)

Example 1

Input: pi_rope_angles([0, 1, 4], 4, 10000.0, 2048, 8192)

Output: [[0.0, 0.0], [0.25, 0.0025], [1.0, 0.01]]

Example 2

Input: positions = [3], dim = 6, base = 100.0, train_len = 1000, target_len = 2000

Output: [[1.5, 0.323165, 0.069624]]

Example 3

Input: positions = [5, 10], dim = 4, base = 10000.0, train_len = 4096, target_len = 4096

Output: [[5.0, 0.05], [10.0, 0.1]]

Constraints

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

Compute the scale factor first and clamp it to 1 when the target is not longer.

Hint 2

theta_i = base ** (-2 * i / dim) for i in range(dim // 2).

Hint 3

np.outer(scaled_positions, theta) gives the whole table at once.

Common mistakes
  • Scaling theta instead of the positions gives a different (NTK-style) method.
  • Using range(dim) instead of range(dim // 2).
  • Scaling when the target is shorter than the training length.
Understand the idea

Extrapolation asks the model to handle angles it never saw. Interpolation squeezes the new range [0, L') into the old range [0, L), so every angle is one the model already knows, just at a finer spacing. With L = 2048 and L' = 8192 the scale is 0.25, so position 4 becomes 1.0 and its angles with dim 4 are [1.0, 0.01].

Shapes and data flow
positions: (n,). theta: (dim/2,). Output: (n, dim/2) via an outer product.
Topics
Paper implementations
Similar questions
  • word2vec skip-gram negative sampling loss (Mikolov et al., 2013)Easy
  • Plan a training run: compute-optimal model size and tokens (Chinchilla rule)Easy
  • Evaluate the Chinchilla scaling lawEasy
  • Build a global magnitude pruning maskEasy
All Paper implementations problems
Report
845 / 903
Python 3solution.pyPython idle
1
2
3
4
5
6
import numpy as np
​
def pi_rope_angles(positions, dim, base, train_len, target_len):
# your code here
pass
​

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

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