RoPE Dot Products Depend on Relative Offset

Absolute query and key rotations combine into a dot product determined by their relative offset. Derive the identity, test simultaneous shifts, and preserve position indices across cached and full-prefix execution.

RoPE uses absolute indices to choose two rotations, but their query-key dot product can be written using the difference between those indices. This is the mechanism's central relative-position property.

Derive the Identity

Rotate query qq at position mm and key kk at position nn:

qm=R(mθ)q,kn=R(nθ)k.q_m=R(m\theta)q,\qquad k_n=R(n\theta)k.

Because a rotation matrix is orthogonal and R(α)TR(β)=R(βα)R(\alpha)^TR(\beta)=R(\beta-\alpha),

qmTkn=qTR(mθ)TR(nθ)k=qTR((nm)θ)k.q_m^Tk_n =q^TR(m\theta)^TR(n\theta)k =q^TR((n-m)\theta)k.

The score contribution from this coordinate pair depends on relative offset nmn-m, using the key-minus-query convention.

Calculate a Small Case

Let q=k=[1,0]Tq=k=[1,0]^T and θ=π/4\theta=\pi/4.

Query position mmKey position nnOffset nmn-mDot product
0001
0112/2\sqrt{2}/2
1320
4512/2\sqrt{2}/2

The pairs (0,1)(0,1) and (4,5)(4,5) have the same offset and therefore the same RoPE contribution for the same unrotated qq and kk.

Shift Both Indices Together

If both positions increase by cc,

(n+c)(m+c)=nm.(n+c)-(m+c)=n-m.

The pairwise RoPE dot product is unchanged. This does not prove that the whole model is invariant to shifting a sequence. Causal and padding boundaries, special tokens, token content, finite context, numerical precision, and later operations remain part of the computation.

Test relative RoPE logits

The program compares position pairs with equal offsets and exposes an inconsistent key-position bug.

Command/Ctrl + Enter. Python runs in your browser.

Ready to run.

Cached Decoding Must Continue the Position Count

During generation, previously computed keys retain the rotations associated with their original indices. A new query and key must use the next absolute index under the same convention. Restarting the new token at position 0 can leave every tensor shape valid while changing its logits.

Chapter 8 will build the key-value cache. For now, retain this invariant:

Cached and uncached execution must assign the same position index to every query and key representing the same token occurrence.

A Relative Score Property Is Not a Universal Length Guarantee

RoPE can calculate rotations at indices beyond those seen during training. Performance at those indices depends on learned behavior, frequency schedules, precision, scaling methods, task, data, and evaluation. The algebraic identity does not settle those empirical questions.

Q1. Use the relative RoPE identity

For one RoPE pair, q=k=[1,0]Tq=k=[1,0]^T and θ=π/4\theta=\pi/4. The query position is m=7m=7 and key position is n=9n=9. What is their rotated dot product?

Compute it first, then check your number.

Hint
Use cos((nm)θ)\cos((n-m)\theta) for these two unrotated vectors.
Solution
nm=2n-m=2, giving cos(2π/4)=cos(π/2)=0\cos(2\pi/4)=\cos(\pi/2)=0.
Not attempted
Review

Not marked done.

Reference

Pause and reflect

In your own words, note what you understood, what remains unclear, or what you want to revisit. The note stays with this lesson.

0 of 1 exercises marked done

Review

Not marked done.

LLM PrimerRoPE Dot Products Depend on Relative Offsethttps://llmprimer.com/transformers/position-and-sequence-order/rope-dot-products-depend-on-relative-offset© 2026 LLM Primer