Grokking › A Worked Example: Tracing the Algorithm Circuit

Michael BrenndoerferNovember 11, 20254 min read
Language AI HandbookAll chapters
Part 30 of 67Emergent Capabilities
Chapter 219Grokking › A Worked Example: Tracing the Algorithm Circuit
Reading section 5 of 13 · A Worked Example: Tracing the Algorithm Circuit · 4 minSection 5 of 13 · 4 min

Book contents

Section 5 of 13 · 4 min read

Grokking › A Worked Example: Tracing the Algorithm CircuitOne section · Sections in this chapter5 / 13 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

To make the circuit formation hypothesis concrete, let us trace through step by step what happens when the network learns to compute (5+7) mod 17=12(5 + 7) \bmod 17 = 12 using the circular embedding.

Step 1: Embedding the operands. Assume the network has learned the circular embedding for p=17p = 17. We need to find the angles for 5 and 7. The angular spacing between consecutive numbers is 2π17≈0.3696\frac{2\pi}{17} \approx 0.3696 radians. We compute each angle by multiplying this spacing by the number:

angle(5)=2π×517=10π17≈1.847 radiansangle(7)=2π×717=14π17≈2.586 radians\begin{aligned} \text{angle}(5) &= \frac{2\pi \times 5}{17} = \frac{10\pi}{17} \approx 1.847 \text{ radians} \\ \text{angle}(7) &= \frac{2\pi \times 7}{17} = \frac{14\pi}{17} \approx 2.586 \text{ radians} \end{aligned}

Step 2: Computing the embedded vectors. Each number maps to a (x,y)(x, y) pair on the unit circle:

embedding(5)=(cos⁡(1.847),sin⁡(1.847))≈(−0.271,0.963)embedding(7)=(cos⁡(2.586),sin⁡(2.586))≈(−0.841,0.540)\begin{aligned} \text{embedding}(5) &= (\cos(1.847), \sin(1.847)) \approx (-0.271, 0.963) \\ \text{embedding}(7) &= (\cos(2.586), \sin(2.586)) \approx (-0.841, 0.540) \end{aligned}

Step 3: Adding the angles. The attention mechanism combines these embeddings by adding their angles. This is the geometric analog of modular addition:

angle(5+7)=2π×517+2π×717=2π×1217≈4.433 radians\begin{aligned} \text{angle}(5 + 7) &= \frac{2\pi \times 5}{17} + \frac{2\pi \times 7}{17} \\ &= \frac{2\pi \times 12}{17} \\ &\approx 4.433 \text{ radians} \end{aligned}

Step 4: Converting back to a token. The output layer maps this angle back to a token index by finding which number 0 through 16 corresponds to angle 4.433 radians. Since 2π×1217≈4.433\frac{2\pi \times 12}{17} \approx 4.433, this maps to token 12, which is the correct answer.

Step 5: Verifying the result. We can verify directly: 5+7=125 + 7 = 12, and 12<1712 < 17, so (5+7) mod 17=12(5 + 7) \bmod 17 = 12. The circular computation produces the right answer.

This angle corresponds to number 12 on the unit circle, which is the correct answer. The output layer maps this angular position back to the token 12. The entire computation requires only a few dozen parameters to stand for: the circular embedding layer, the attention weights that implement angular addition, and the output layer that maps back to token indices.

By contrast, the memorization solution for this single example requires storing that the input (5, 7) maps to output 12. With 9409 total input pairs, full memorization of a 50% training split requires storing roughly 4700 such associations. Even with compact representations, this is inherently more parameter-intensive than the algorithmic solution, which applies the same rotation operation to every input. This quantitative difference in compactness is what weight decay exploits. Each step of optimization penalizes large parameters, and the memorization circuit requires larger or more numerous parameters than the algorithm circuit. Over thousands of training steps, this penalty accumulates, making the memorization circuit increasingly costly relative to the algorithm circuit. Eventually, the algorithm circuit wins out and generalization emerges.

Worked example with a wraparound case. Now let us trace a case where the sum exceeds the modulus, to see how the circular representation handles it. Consider (13+8) mod 17(13 + 8) \bmod 17. The ordinary sum is 21, and 21=1×17+421 = 1 \times 17 + 4, so the correct answer is 4.

In the circular representation:

angle(13+8)=2π×1317+2π×817=2π×2117=2π×(17+4)17=2π+2π×417\begin{aligned} \text{angle}(13 + 8) &= \frac{2\pi \times 13}{17} + \frac{2\pi \times 8}{17} \\ &= \frac{2\pi \times 21}{17} \\ &= \frac{2\pi \times (17 + 4)}{17} \\ &= 2\pi + \frac{2\pi \times 4}{17} \end{aligned}

The 2π2\pi term is a full rotation around the circle, which brings us back to where we started. The remaining 2π×417\frac{2\pi \times 4}{17} corresponds to position 4 on the circle. The circular representation handles the wraparound automatically, without any special casing. The memorization solution, by contrast, must have separately memorized that (13,8)(13, 8) maps to 4. If this particular pair was in the test set and not the training set, the memorization solution would fail. The circular representation succeeds on any pair because the wraparound is implicit in the geometry.