Homepage / Blog

When Attention Learns Group Composition

Architectural depth, reasoning depth, and the word problem of S5.

Model DepthCurriculum Learning

A small group, a hard problem

S5 = Sym({1, 2, 3, 4, 5})
Y = π1 ∘ π2 ∘ ··· ∘ πT
Input   (12345)(12345)(23451)
Output  (23451)

S5 is the smallest nonsolvable symmetric group. Its commutator subgroup is A5, a nonabelian simple group with [A5, A5] = A5.

Barrington’s theorem shows that the decision word problem for a fixed finite nonsolvable group—whether a word multiplies to the identity—is NC1-complete under AC0 reductions. We study a related task: given T permutations in S5, predict their full product. As T grows, the model must compose a longer sequence.

Expressivity: How much depth is needed?

A balanced tree composes pairs of permutations in parallel, evaluating a word of length T in O(log T) steps. Prior work shows that Transformers can realize this computation with O(log T) layers; see Merrill and Sabharwal and Liu et al..

D = O(log T)
016324864123456Word lengthDepth
Balanced binary composition tree: length 2ᴰ at depth D.

Can Training Find the Computation?

A balanced tree shows what a Transformer can represent, but not what training will discover. We test whether curriculum learning helps a fixed architecture learn longer permutation products.

016324864123456Word lengthDepth
Maximum observed solvable word length under curriculum training.

How Could Attention Compute Permutation Composition?

To interpret the attention patterns the model learns, we need alternatives to compare against. We give three ways to compute the same permutation composition; they differ in whether attention, the FFN, or both perform the selection. Each predicts a different pattern, giving us a reference for the learned attention.

(X ∘ Y)i = XYᵢ

Throughout, let ej denote the j-th one-hot vector in ℝ⁵.

1. Copy, then select

Attention copies the five entries of X into separate slots. The FFN then uses Yi to select XYᵢ.

(eX₁, …, eX₅, eYᵢ)   →   FFN   →   eXYᵢ
Mathematical construction

At position i, head k attends to the kth position of X and writes eXₖ into its own output slot. The residual stream preserves eYᵢ; the output projection concatenates these signals for the FFN.

A two-layer ReLU FFN computes 25 selection gates:

gkj = ReLU((eYᵢ)k + (eXₖ)j − 1)
oi = ∑k=15 ∑j=15 gkj ej = eXYᵢ

Only the gate with k = Yi and j = Xk is active. Under ideal copying, the FFN therefore computes the composition exactly. Five heads suffice for this construction; we do not claim that five are necessary.

2. Retrieve with attention

A single attention head can select XYᵢ directly; the selection itself happens in attention.

eYᵢ   →   Attention(X)   →   eXYᵢ
Mathematical construction

Assume each position and token value has a distinguishable representation. Linear projections can then form query eYᵢ, key ek for position k, and value eXₖ. We restrict attention to the five positions of X.

qi = eYᵢ,   kj = ej,   vj = eXⱼ
qi⊤kj = 𝟙[j = Yi]
HardAttn(qi, K, V) = eXYᵢ

With softmax and effective logit scale β > 0, the output is

oi = (exp(β)eXYᵢ + ∑j≠Yᵢ eXⱼ)/(exp(β)+4).

Because X is a permutation, its five values are distinct. The largest attention weight is on position Yi, so argmax returns XYᵢ. As β increases, the output approaches the one-hot vector eXYᵢ. A thresholding FFN can recover that exact vector for later composition when exp(β) > 4.

3. Split the work

For three permutations, attention retrieves XYᵢ while five other heads copy the entries of Z. The FFN then selects ZXYᵢ, completing the composition in one block.

(Z ∘ X ∘ Y)i = ZXYᵢ
Retrieval head
eXYᵢ
Five copying heads
(eZ₁, …, eZ₅)
↓   FFN   ↓
eZXYᵢ
Mathematical construction

Use six heads in parallel: head 0 retrieves XYᵢ with query eYᵢ, while heads 1–5 copy Z1, …, Z5. Give each head a separate output slot. With ideal hard attention, their concatenated output is

ai = (eXYᵢ, eZ₁, …, eZ₅) ∈ ℝ³⁰.

The output projection preserves these six slots. The same 25-gate FFN from Construction 1 now takes XYᵢ as the index and Zk as the candidate values:

gkj = ReLU(𝟙[XYᵢ = k] + 𝟙[Zk = j] − 1)
∑k,j gkjej = eZXYᵢ.

All six heads run in parallel, so this uses one attention block; the FFN performs the second selection. Softmax attention approximates the ideal copies and retrieval as its logits sharpen. Thus one block with six heads is sufficient, but this does not show that six heads are minimal.

These constructions produce the same output but predict different attention patterns. Final accuracy alone therefore cannot reveal which computation the model learned.

What Does the Model Learn Under a Tight Budget?

We train models with tight depth and head budgets, then compare their attention patterns with the predictions of the three constructions. Head ablation tests whether the heads associated with those patterns are necessary for solving the task.

Word lengthLayersHeads
212
313
321
Tight depth / head budget→Attention patterns→Lesion analysis

Head ablation tests which attention heads are functionally necessary.

Learned Attention Matches the Predicted Patterns

In both settings, learned attention closely follows the retrieval patterns predicted by the constructions.

Three permutations

Zᵢ = X[Y[Wᵢ]]
Learned attention
Learned attention: Three permutations
Layer 1 · Head 1
Theoretical retrieval
Theoretical retrieval: Three permutations

Five permutations

Zᵢ = P₁[P₂[P₃[P₄[P₅[i]]]]]
Learned attention
Learned attention: Five permutations
Layer 2 · Head 22
Theoretical retrieval
Theoretical retrieval: Five permutations

Attention atlas

All experiment collections

Conclusion

Under tight depth and head budgets, learned attention follows patterns predicted by explicit constructions. The results show that a Transformer can discover recognizable implementations of permutation composition, even when several implementations are possible.

Reference