YaRN › Visualizing YaRN's Effect

Michael BrenndoerferUpdated June 28, 20253 min read
Language AI HandbookAll chapters
Part 18 of 67Long Context
Chapter 129YaRN › Visualizing YaRN's Effect
Reading section 10 of 16 · Visualizing YaRN's Effect · 3 minSection 10 of 16 · 3 min

Book contents

Section 10 of 16 · 3 min read

YaRN › Visualizing YaRN's EffectOne section · Sections in this chapter10 / 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

Let's visualize how YaRN affects the attention patterns compared to other methods. A good visualization helps build the intuition for why YaRN's two-part correction produces qualitatively better attention distributions.

In[22]:
Code
def compute_attention_matrix(
    seq_len, d_model, freqs, scale_attention=1.0, seed=42
):
    """Compute full attention weight matrix."""
    rng = np.random.default_rng(seed)

    Q = rng.standard_normal((seq_len, d_model))
    K = rng.standard_normal((seq_len, d_model))

    Q_rotated = np.array([apply_rope(Q[i], i, freqs) for i in range(seq_len)])
    K_rotated = np.array([apply_rope(K[i], i, freqs) for i in range(seq_len)])

    scores = Q_rotated @ K_rotated.T / np.sqrt(d_model)
    scores = scores * scale_attention

    weights = softmax(scores)
    return weights


# Compute attention patterns for each method
seq_len = 48
d_model = 64
scale = 16.0

# Recompute extension freqs at this scale so differences are pronounced
_pi_freqs_vis = compute_pi_frequencies(d_model, scale=scale)
_ntk_freqs_vis = compute_ntk_frequencies(d_model, scale=scale)

# YaRN with temperature
yarn_module = YaRNRoPE(d_model=d_model, original_context=2048, scale=scale)
yarn_attn = compute_attention_matrix(
    seq_len,
    d_model,
    yarn_module.adjusted_freqs,
    scale_attention=np.sqrt(yarn_module.temperature),
)

# Original (no extension)
original_attn = compute_attention_matrix(seq_len, d_model, original_freqs)

# Position Interpolation
pi_attn = compute_attention_matrix(seq_len, d_model, _pi_freqs_vis)

# NTK-aware
ntk_attn = compute_attention_matrix(seq_len, d_model, _ntk_freqs_vis)
Out[23]:
Visualization
Heatmap showing attention weights with diagonal emphasis.
Original RoPE attention pattern at 16x extension without any correction. This is the baseline distribution the model learned during training, with moderate contrast between attended and non-attended positions.
Heatmap showing more uniform attention distribution.
Position Interpolation attention at 16x extension. The pattern becomes more diffuse and uniform due to compressed position distances, indicating the model cannot sharply focus on specific tokens.
Heatmap showing moderate attention pattern preservation.
NTK-aware attention at 16x extension. Preserves more structure than PI by protecting high-frequency components, but still shows increased entropy from the global frequency adjustment.
Heatmap showing attention pattern similar to original.
YaRN attention at 16x extension. The combination of selective ramp-based interpolation and temperature scaling produces a pattern closer to the original, with better contrast and sharper focus on specific positions.

The attention heatmaps reveal the qualitative differences between methods at a 16x extension factor, where differences are most pronounced. Position Interpolation produces the most diffuse patterns: the uniform compression of all frequencies flattens the attention distribution across all key positions. NTK-aware scaling improves upon this by preserving high-frequency discrimination, resulting in slightly more structured patterns. YaRN, combining selective interpolation with temperature correction, produces patterns that most closely resemble the original RoPE baseline, with sharper peaks and clearer structure in the weight distribution. This visual comparison explains why YaRN requires less fine-tuning to recover model quality: it starts from a better position, and the model's existing weights need fewer updates to adapt.