Attention heads forming orbits
One self-attention layer, penalized toward equivariance, learning modular addition
Each dot is one of the 32 heads of a single self-attention layer, drawn in a plane of its weight space while the layer learns modular addition. The experiment’s code is due to Georg Bökman.
1. Setup
- model: single 32-head self-attention layer sandwiched between an embedding and an unembedding
- task: modular addition with , . - symmetry: shifting all three inputs by shifts every output by
- loss: cross-entropy plus an equivariance loss
- result: 100 % train accuracy, 76 % on the held-out inputs, equivariance error
2. Prediction
The structure theorem for attention (preprint) says that the layer is exactly equivariant if and only if its heads can be organized into orbits of the group of shifts. That is, the shift by sends head to a head with where
- is the QK matrix of head
- is the OV matrix
- is the cyclic shift of by (heads with the same are merged, and only the sum of their enters).
The movie shows that a layer that is pressured towards equivariance arranges its heads in such orbits.
3. Movies
decomposes into irreps of labeled by frequencies. For QK matrices living in , we pick a pair of (query frequency , key frequency ) and look at a 2D subspace of isomorphic to or .
- top left: query frequency 0, key frequency 10. shift by rotates the dot by .
- bottom left: in output frequency 10 and input frequency 10. Shift by rotates a dot by (the factor 2 is the by which the output shifts).
- right column: QK and OV at frequency 6, where the rotation is by .
What to look for: a -orbit appears in every panel as a regular polygon centered at the origin, with one vertex per value of the rotation. takes 12 values, so an orbit on frequency 10 is a 12-gon, and takes 4, so an orbit on frequency 6 is a square. The 12 blue heads and the 4 orange heads are such orbits. This was checked on the full matrices (a polygon in one plane says nothing about the other blocks of and ): after any of the 24 shifts, each of these heads lands on another head of its set, to within 2 % in relative Frobenius norm. The gray dots are the other 16 heads.