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
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.
Rotation keeps lengths, shifts angles
The rotation matrix and its key identity
Same gap, same dot product
Check it numerically
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
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.
Bytewise
All courses