Grokking, Visualized
Chapters
  1. The whole picture, first
  2. Counting on a clock
  3. The task as a dataset
  4. Vectors and matrices
  5. From scores to probabilities
  6. The model, step by step
  7. Simplifying the model
  8. How the model learns
  9. Grokking
  10. Angles and waves
  11. The Fourier basis
  12. Measuring fit and sparsity
  13. The Fourier multiplication algorithm
  14. Evidence 1: everything is periodic
  15. Evidence 2: neurons and heads
  16. Evidence 3: reading off the answer
  17. Evidence 4: interventions
  18. Progress measures and the three phases
  19. Watch it form
  20. Gradients and weight decay
  21. Is it robust? Seeds and controls
  22. The whole picture, again

Chapter 6 of 0–21

Simplifying the model

Which parts matter: ablations, the functional form and the effective weights.

Sources for this chapter
  • [3] section 1 “Functional form”
  • [1] App. A.1

Before asking what the trained network computes, it helps to find out which of its parts matter at all. The ARENA course does this first, deriving a simpler “functional form” of the network [3]; the paper reports the same facts in its appendix [1]. All numbers below are measured on the paper's trained network at step 40,001.

Ablations: switch a part off and see what breaks

An ablation removes or replaces one part of the computation and measures how much the output changes. A zero ablation replaces the part's output with zeros; a mean ablation replaces it with its average over all inputs, which removes only the information that depends on the input (zeroing can also remove a useful constant offset).

A skip connection is a path that carries a layer's input around the layer and adds it back — the “x +” in x^{(1)} = x^{(0)}_2 + \ldots and x^{(2)} = x^{(1)} + \ldots of chapter 5.

Worked example

What matters in the trained network (loss on all 12,769 inputs; unablated: 2.41×10⁻⁷; uniform guessing: 4.727):

Ablation Loss Interpretation
Zero the attention output 2.43×10¹ attention is essential (worse than guessing)
Zero the skip connection around the MLP 8.88×10⁻⁷ negligible (the paper reports accuracy unchanged)
Mean-ablate the skip connection around the MLP 7.25×10⁻⁷ negligible

Attention from “=” to itself is tiny and can be ignored: 0.44 %, 0.11 %, 0.44 %, 0.12 % for heads 0–3. The paper reports 24.3, 9.12 × 10⁻⁷ and 7.25 × 10⁻⁷ for the three ablations and 0.1–0.4 % for the attention [1] (App. A.1).

“our networks do not significantly use the skip connection around the MLP”— Nanda et al. 2023, Sec. 3, p. 3 · source

Consequence 1: logits come from the neurons through one matrix

If the skip connection around the MLP is ignored, x^{(2)} \approx W_{out}\,\mathrm{MLP}(a,b) (plus constants), so

\Logits(a,b) \approx W_U W_{out}\,\mathrm{MLP}(a,b) = W_L\,\mathrm{MLP}(a,b).

The paper calls W_L = W_U W_{out} (a 113 × 512 matrix) the neuron–logit map W_L: column m says how much one unit of neuron m's activation adds to each answer's logit. ARENA calls the same matrix (transposed) W_{logit}. A product of matrices that maps one meaningful quantity directly to another is an effective weight matrix; we will study W_L in the Fourier basis in chapter 15.

Consequence 2: each head is a two-way choice between a and b

Attention to “=” is negligible, so each head's softmax is effectively over two scores, which (chapter 4) is a sigmoid of their difference:

A^j_0 \approx \sigma\big(s_0 - s_1\big), \qquad A^j_1 \approx 1 - A^j_0 .

Moreover the query comes from the “=” position, whose vector x^{(0)}_2 is the same for every input. So the score of position 0 depends only on the token there: it is a fixed lookup table C_j over the 113 numbers, s_0 = C_j[a], and likewise s_1 \approx C_j[b] (up to the small difference between the positional embeddings of positions 0 and 1, see below). Hence

A^j_0(a, b) \approx \sigma\big(C_j[a] - C_j[b]\big).

This is the paper's App. C.1.3 derivation [1]; ARENA calls C_j the effective attention weights W_{attn} [3].

Average attention per head

Mean over all inputs of how much '=' attends to a, b and itself.

Loading…

How to read it
<p class="fig-expect"><strong>What to expect:</strong> About half to a and half to b; almost nothing to '='.</p>
<p class="fig-meta">
  <a href="../chapters/06-simplifying-the-model.html">Learn step by step →</a>
   · Source: <a class="cite" href="../references.html#nanda2023" title="Nanda et al. 2023">[1]</a> App. A.1; <a class="cite" href="../references.html#arena152" title="ARENA 1.5.2">[3]</a> section 3, “Development of commutativity”
  · Computed by <code>checkpoint_metrics.py</code> (<a href="https://github.com/BurnyCoder/visualizing-grokking-modular-addition">code</a>)
</p>

Here is that two-way choice in the real network. Each dot is one input: its attention to a against the difference of the two scores, with the sigmoid drawn on top (pick another head in the dashboard's control bar).

Attention as a sigmoid of two scores

For the selected head: attention from '=' to a on 800 sampled inputs against the score difference s₀ − s₁, with σ(s₀ − s₁) and the best straight line.

Loading…

How to read it · the math
A^j_0 \approx \sigma(s_0 - s_1) = \sigma\big(C_j[a] - C_j[b] + \text{const}\big)
<p class="fig-expect"><strong>What to expect:</strong> The points follow σ closely, because attention to '=' is negligible, and stay in its nearly straight middle part.</p>
<p class="fig-meta">
  <a href="../chapters/06-simplifying-the-model.html">Learn step by step →</a>
   · Source: <a class="cite" href="../references.html#nanda2023" title="Nanda et al. 2023">[1]</a> App. C.1.3
  · Computed by <code>web/js/model/grokmodel.js</code> (<a href="https://github.com/BurnyCoder/visualizing-grokking-modular-addition">code</a>)
</p>

And the choice on every input at once: for each head, the attention from “=” to a, to b and to itself over all 12,769 questions.

Every attention pattern on every input

For each head, the attention from '=' to a, to b and to '=' on all 12,769 inputs at the selected step.

Loading…

How to read it
<p class="fig-expect"><strong>What to expect:</strong> In the trained network the attention to a and to b form periodic stripes and the attention to '=' is close to 0 everywhere.</p>
<p class="fig-meta">
  <a href="../chapters/06-simplifying-the-model.html">Learn step by step →</a>
   · Source: <a class="cite" href="../references.html#nanda2023" title="Nanda et al. 2023">[1]</a> Fig. 4
  · Computed by <code>web/js/model/grokmodel.js</code> (<a href="https://github.com/BurnyCoder/visualizing-grokking-modular-addition">code</a>)
</p>

Consequence 3: the input is symmetric in a and b

The problem has commutativity: a + b = b + a. A network that has learned the task should treat a and b alike. In the trained network the positional embeddings of positions 0 and 1 are both small — length 0.12 and 0.12, against 1.18 for a typical token embedding — and point in similar directions (cosine similarity 0.62), whereas the “=” position's embedding is large (2.76). So whether a number sits at position 0 or 1 barely changes its vector, and a and b are processed (almost) identically:

Positional embeddings

The three learned vectors added at positions 0 (a), 1 (b) and 2 (=).

Loading…

How to read it
<p class="fig-expect"><strong>What to expect:</strong> Rows 0 and 1 stay small (paper run: norms <span class="num" data-num="paper.pos_norm0">0.12</span> and <span class="num" data-num="paper.pos_norm1">0.12</span>, against <span class="num" data-num="paper.we_norm_mean">1.18</span> for a typical token embedding) and partly aligned (cosine <span class="num" data-num="paper.pos_cos01">0.62</span>), so a and b are treated almost alike; row 2 (the “=” position) is large.</p>
<p class="fig-meta">
  <a href="../chapters/06-simplifying-the-model.html">Learn step by step →</a>
   · Source: <a class="cite" href="../references.html#arena152" title="ARENA 1.5.2">[3]</a> “Answer some initial questions”
  · Computed by <code>browser (GrokModel)</code> (<a href="https://github.com/BurnyCoder/visualizing-grokking-modular-addition">code</a>)
</p>

The simplified network

Putting it together, the trained network behaves like

\begin{aligned}\Logits(a,b) \approx W_L\,\ReLU\Big(W_{in}\big[\,&x_2 + \textstyle\sum_j W_O^j W_V^j\big(\sigma(C_j[a]{-}C_j[b])\,E_a\\ &+ (1-\sigma(\cdot))\,E_b\big)\big] + b_{in}\Big),\end{aligned}

where E_t is token t's embedding plus its positional vector. Everything that depends on the input enters through the embeddings E_a, E_b, the lookup tables C_j, a ReLU, and the neuron–logit map W_L. The rest of the site explains what each of these does.

Nuance

These simplifications are justified empirically for the trained network. They need not hold at every point of training: the dashboard's attention panels show attention to “=” at almost zero from step 101 on, then rising again around step 43,000 — a late change that neither the paper nor ARENA explains [3].