All courses

2D rotation matrices

Write the 2×2 matrix that rotates by angle θ. Show that rotating two vectors by angles a and b changes their dot product only through the difference a−b. This property is what later lets attention scores depend on how far apart two tokens are.

Builds on: Attention in plain words

▶ Watch this lesson Start the whole course

What the animation says

A score that only sees distance

In attention, a query is scored against each key with a dot product. We want that score to depend only on how far apart the two tokens are. Token seven looking at token five should score exactly like token one hundred seven looking at one hundred five. One idea: rotate each vector by an angle set by its position. Now spin the whole picture, both arrows, by the same extra amount. What happens to the dot product?

Only the gap matters

It stays the same. A dot product is one length, times the other length, times the cosine of the angle between them. Rotation never changes a length. It only turns direction. Rotate q by angle a and k by angle b. The angle from k to q grows by exactly a minus b. So the score depends on the two angles only through their gap: a minus b. But order counts. q by sixty and k by zero, versus q by zero and k by sixty: same score for every q and k?

The rotation matrix

They differ. With q at thirty degrees and k at zero, one pair scores zero, the other about point eight seven. Now the machinery. With row vectors, x times R of phi rotates x counterclockwise by phi. Row one is where the x-axis arrow lands. Row two is where the y-axis arrow lands. Transposing R undoes it: R transposed is R of minus phi. And two rotations add their angles. Put together: rotated q dotted with rotated k equals q, times R of a minus b, times k transposed.

Same gap, same score

Check with numbers. Start with q and k both equal to one, zero. Case one: q by sixty, k by zero. q lands at one half, root three over two. Dot product: one half. Case two: q by ninety, k by thirty. q lands at zero, one; k at root three over two, one half. Again one half. Both gaps are sixty degrees, and the cosine of sixty is one half. Your turn. q is rotated by one hundred fifty degrees. Drag k to keep the gap at sixty.

Check it in code

Now with random vectors, in NumPy. This function builds R in row form: cosine and sine on top, minus sine and cosine below. This loop rotates q and k by three pairs of angles, each with a gap of zero point seven. All three lines print the same number. And this last line, rotating only q by zero point seven, prints that number too. So rotation turns a gap in angle into a fixed score. Later, that angle comes from token position.

Can a match score depend only on distance?

In attention, a query is compared with each key at its own or an earlier position, and the match score is their dot product. Suppose we want that score to depend only on **how far apart** the two tokens are, so that token 7 looking at token 5 behaves like token 107 looking at token 105. One idea is to **rotate** each vector by an angle set by its position. If the dot product of two rotated vectors depends only on the *difference* of their angles, then shifting both positions by the same amount leaves the score unchanged. Does rotation have that property?

Rotation keeps lengths, shifts angles

A dot product is $|u|\,|v|\cos(\text{angle between them})$. Rotating a vector spins it and leaves its length unchanged. Rotate $u$ by $a$ and $v$ by $b$. The angle from $v$ to $u$, measured counterclockwise, then grows by exactly $a-b$. Cosine does not care about the sign of that angle, so the dot product depends on the two angles only through their **difference**. If you rotate both vectors by the same extra amount, nothing changes, just as spinning a whole drawing keeps every angle inside it.

The rotation matrix and its key identity

With row vectors, $xR(\varphi)$ rotates $x$ counterclockwise by angle $\varphi$: $$R(\varphi)=\begin{pmatrix}\cos\varphi & \sin\varphi\\ -\sin\varphi & \cos\varphi\end{pmatrix}$$ Row 1 is where $(1,0)$ lands. Row 2 is where $(0,1)$ lands. Column-vector textbooks write the transpose. Two facts hold: $R(a)R(b)=R(a+b)$ and $R(\varphi)^\top=R(-\varphi)$. For row vectors, $x\cdot y=xy^\top$ and $(vR(b))^\top=R(b)^\top v^\top$. Therefore $$(uR(a))\cdot(vR(b))=uR(a)R(b)^\top v^\top=u\,R(a-b)\,v^\top.$$

Same gap, same dot product

Take $u=v=(1,0)$. **Case 1:** $a=60^\circ$, $b=0^\circ$. Then $uR(60^\circ)=(\tfrac12,\tfrac{\sqrt3}{2})$ and $v$ stays $(1,0)$. The dot product is $\tfrac12$. **Case 2:** $a=90^\circ$, $b=30^\circ$. Then $uR(90^\circ)=(0,1)$ and $vR(30^\circ)=(\tfrac{\sqrt3}{2},\tfrac12)$. The dot product is $0+\tfrac12=\tfrac12$. Both cases have a gap of $a-b=60^\circ$, and both give $\cos 60^\circ=\tfrac12$.

Check it numerically

Random 2D vectors are rotated by three angle pairs that all have $a-b=0.7$. Every pair prints the same number, and that number matches rotating only $q$ by $0.7$.
import numpy as np
def R(phi):
    c, s = np.cos(phi), np.sin(phi)
    return np.array([[c, s], [-s, c]])  # row form: x @ R(phi)

rng = np.random.default_rng(0)
q, k = rng.normal(size=2), rng.normal(size=2)
for a, b in [(0.9, 0.2), (2.0, 1.3), (5.7, 5.0)]:
    print(round((q @ R(a)) @ (k @ R(b)), 6))  # all equal
print(round(q @ R(0.7) @ k, 6))              # same value

Remember this

$R(\varphi)=\begin{pmatrix}\cos\varphi&\sin\varphi\\-\sin\varphi&\cos\varphi\end{pmatrix}$ rotates row vectors. Rotating $u$ by $a$ and $v$ by $b$ gives the dot product $u\,R(a-b)\,v^\top$, which depends only on the difference $a-b$. Later, this is how a query–key match can be made to depend on how far apart two tokens are.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.