↑↓ move Enter open ⌘K find from any page 256 of 256 tabs
Layer 1 of 24 Query heads KV1 KV2 trained to 32,768 · YaRN s = 4 → 131,072 · β 32 / 1 · √(1/t) = 1.0000
Loading recorded attention…

What YaRN is

A RoPE model is trained up to some context length, 32,768 tokens for Qwen2.5-0.5B, and its attention has only ever seen the rotation angles that distances up to that length produce. Feed it a longer input and the slow pairs of every query and key (the RoPE tab's frequencies) turn further than they ever did in training. YaRN (Yet another RoPE extensioN, Peng et al., 2023) changes the frequencies so that a longer context maps back onto angles the model knows, without retraining the weights, and adds a small attention temperature. Peng et al. also fine-tune briefly on long data after the change; this page, like Qwen's cards, applies it to the released weights as they are.

Qwen's model cards for Qwen2.5-7B-Instruct and larger document one YaRN setting for inputs past 32,768 tokens: rope_scaling with type yarn, factor 4 and original_max_position_embeddings 32,768. This page applies that same setting to Qwen2.5-0.5B (whose own card lists a 32,768-token context) through transformers' rotary module, and records the model with and without it on inputs of 8K, 32K and 64K tokens.

Interpolate, extrapolate or ramp

Position Interpolation (Chen et al.) divides every frequency by the factor , so position is turned exactly as position was. That keeps every angle inside the trained range, but it also squeezes the fast pairs, which the model uses to tell neighbouring tokens apart. YaRN's “NTK-by-parts” rule treats pairs by how many turns they make in the original context , where is the wavelength:

with and . Pairs that turn more than 32 times in 32,768 tokens have already seen every angle many times, so they keep (extrapolation). Pairs that never complete a turn are divided by 4 (interpolation). Transformers rounds the boundaries outward to whole pairs, which for Qwen's base of gives pairs 0–11 kept, 12–19 ramped and 20–31 divided. The Frequencies panel draws all 32 wavelengths after YaRN, coloured by band, with RoPE's wavelengths as grey ticks where they differ.

Angles never seen

The small dial under the bars shows the problem for one pair at a time. In training, pair reaches at most . For a fast pair that is many full turns, so a longer input brings nothing new. For a slow pair it is a fraction of a turn, and at 64K tokens RoPE turns it twice as far, into angles the model has never seen. With YaRN the same pair is turned by , and 64K tokens stay inside half of the trained range.

The temperature

Spreading attention over four times as many keys flattens the softmax. YaRN compensates by multiplying every score by a constant before the softmax, which it writes as with . For that is , so scores grow by . Transformers implements it by multiplying cos and sin by : both the query and the key carry the factor, and the model's code needs no change.

Past 32K, recorded

The Pass key grid runs Mohtashami and Jaggi's passkey test: a five-digit number hidden in repeated filler (“The grass is green. The sky is blue. …”), and at the end “What is the pass key? The pass key is”. There are 15 prompts, at 8,192, 32,768 and 65,536 tokens with the key 10–90 % of the way into the filler, each run with RoPE as trained and with YaRN; the model wrote 8 tokens greedily. At 8K and 32K, inside the trained context, both settings answer all 10 prompts. At 64K, YaRN answers all five. RoPE answers four and fails once: with the key 58,956 tokens before the question it wrote “"green".”. With the key 45,863 tokens back it still answered, so going past the trained length does not break everything at once.

The head card and Where the last token looks show what went wrong in that run. With YaRN, head 14 of layer 14 puts 91 % of the last token's attention on the pass-key sentence, and head 4 of layer 17 puts 78 %. With RoPE, no head in any layer puts more than 16 % there, and head 4 of layer 17 puts 99 % on token 0 instead. Over all layers and heads, the last token's attention has a mean entropy of 4.31 nats with RoPE and 3.03 with YaRN: the stretched frequencies and the temperature keep it focused.

What it costs

Qwen's cards warn that YaRN as transformers and vLLM apply it is static: the frequencies are rescaled at every length, “potentially impacting performance on shorter texts”. The Loss along a book chart measures that on 65,536 tokens of The Adventures of Sherlock Holmes, as the mean next-token loss of each 4,096-token window. Up to 32,768 tokens YaRN is worse than plain RoPE in every window: 2.80 against 2.73 nats on tokens 4,096–32,768. Past 32K the gap narrows, and in the last window YaRN is ahead, 2.790 against 2.802. Most of a book's words can be predicted from the last few hundred tokens, which is why the average loss barely moves while the pass-key test, which needs one token from far back, fails.

The third line keeps YaRN's frequencies but switches the temperature off (attention_factor 1.0). On this model, used without fine-tuning, it has a lower loss than full YaRN in every window (2.77 against 2.80 on tokens 4,096–32,768). Peng et al. chose the temperature's formula by fitting LLaMA models' perplexity; it is not tuned for Qwen2.5-0.5B.

Where it came from

Position Interpolation (Chen et al., June 2023) showed that dividing all of RoPE's frequencies lets a LLaMA model read past its trained context after a short fine-tune. Soon after, “NTK-aware” scaling, which raises the base instead so the fast pairs change least, was proposed in an online post, followed by the “NTK-by-parts” and dynamic variants. YaRN (Peng et al., 2023) combined NTK-by-parts with the temperature and extended LLaMA 2 to 128K tokens with far less training than earlier methods. Later work such as LongRoPE searches a separate factor for every pair. RoPE itself is the RoPE tab; the Sinks tab shows another way past a fixed window, keeping the first tokens and a recent window in the cache.

Papers

  1. YaRN: Efficient Context Window Extension of Large Language Models Bowen Peng, Jeffrey Quesnelle, Honglu Fan, Enrico Shippole · ICLR 2024 Introduces YaRN: NTK-by-parts interpolation of RoPE's frequencies plus the attention temperature.
  2. Extending Context Window of Large Language Models via Positional Interpolation Shouyuan Chen, Sherman Wong, Liangjian Chen, Yuandong Tian · arXiv 2023 Position Interpolation, which divides every RoPE frequency by the extension factor.
  3. Landmark Attention: Random-Access Infinite Context Length for Transformers Amirkeivan Mohtashami, Martin Jaggi · NeurIPS 2023 The passkey-retrieval prompt this page runs at 8K, 32K and 64K tokens.
  4. RoFormer: Enhanced Transformer with Rotary Position Embedding Jianlin Su, Yu Lu, Shengfeng Pan, Ahmed Murtadha, Bo Wen, Yunfeng Liu · Neurocomputing 2024 Rotary position embeddings, whose frequencies YaRN rescales (the RoPE tab).
  5. Qwen2.5 Technical Report Qwen Team (An Yang et al.) · arXiv 2024 The model family this page records; Qwen extends its context with YaRN.
  6. LongRoPE: Extending LLM Context Window Beyond 2 Million Tokens Yiran Ding et al. · ICML 2024 Searches a separate rescaling factor for each RoPE pair instead of YaRN's three bands.

See also