Rotary Position Embedding (RoPE) › RoPE Frequency Patterns

Michael BrenndoerferUpdated June 5, 20254 min read
Language AI HandbookAll chapters
Part 14 of 67Positional Encoding
Chapter 100Rotary Position Embedding (RoPE) › RoPE Frequency Patterns
Reading section 13 of 19 · RoPE Frequency Patterns · 4 minSection 13 of 19 · 4 min

Book contents

Section 13 of 19 · 4 min read

Rotary Position Embedding (RoPE) › RoPE Frequency PatternsOne section · Sections in this chapter13 / 19 ↓
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

The frequency choices determine which relative offsets each dimension pair can distinguish. Let's visualize how the multi-frequency structure creates unique position signatures:

In[30]:
Code
# Visualize how position affects embedding components
d_model = 64
max_pos = 100
frequencies = compute_rope_frequencies(d_model)

# For each position, compute the rotation for each dimension pair
# We'll track cos(pos * freq) and sin(pos * freq)
cos_components = np.zeros((max_pos, d_model // 2))
sin_components = np.zeros((max_pos, d_model // 2))

for pos in range(max_pos):
    cos_components[pos] = np.cos(pos * frequencies)
    sin_components[pos] = np.sin(pos * frequencies)
Out[31]:
Visualization
Heatmap showing cosine values with rapid oscillation at top, slow at bottom.
Cosine components of RoPE across positions and dimension pairs. Early pairs (top) oscillate rapidly, creating high-frequency position signals. Later pairs oscillate slowly, encoding longer-range position information.
Heatmap showing sine values with similar frequency patterns to cosine.
Sine components of RoPE. Together with cosine, they form the complete rotation state for each dimension pair. The combination of multiple frequencies creates a unique signature for each position.

The heatmaps reveal the multi-scale nature of RoPE. Low-index dimension pairs (top rows) cycle rapidly, distinguishing nearby positions. High-index pairs (bottom rows) change slowly. This provides a coarse position signal. This structure resembles sinusoidal position encodings since both use similar frequency patterns. The key difference is that sinusoidal encodings add position information to embeddings, while RoPE rotates the embeddings themselves.

Every position in the sequence maps to a unique column pattern across all the dimension pairs. No two positions within the context window produce the same combination of cosine and sine values (unless positions are so far apart that even the slowest frequency has repeated, which requires distances well beyond any practical context). This uniqueness is what allows the model to discriminate between any two positions in the sequence.

We can also visualize the effective rotation that accumulates over a long sequence by tracking a single unit vector in each pair as position increases. The resulting picture shows clearly why high-frequency pairs lose discriminative power at large distances while low-frequency pairs preserve it:

Out[32]:
Visualization
Line plot showing accumulated rotation in radians for eight dimension pairs as position increases to 200.
Accumulated rotation angle for each of the first eight dimension pairs over 200 positions. High-frequency pairs (low index) accumulate many full rotations quickly and cycle back, while low-frequency pairs (high index) advance slowly and remain in a unique orientation for much longer sequences. The multi-octave spread ensures that every position has a unique combined signature.