Clustering and Alignment: Understanding the Training Dynamics in Modular Addition

Tiberiu Mușat

TL;DR

While mechanistic interpretability has identified what algorithms neural networks learn on toy tasks like modular addition (e.g., circular "clock" or "pizza" representations), how these representations emerge during training has remained poorly understood. This paper models training dynamics as an interaction between embedding pair-sums governed by two intuitive forces—clustering and alignment—and proves that a simple particle simulation using these force equations spontaneously reproduces the exact geometric structures (circles and grids) formed by trained transformers.

What Problem Does This Paper Address?

In recent years, researchers discovered that transformers trained on modular arithmetic $a + b \pmod N$ reliably learn interpretable algorithms, such as arranging token embeddings along circles in representation space. However, existing work predominantly relies on post-hoc mechanistic analysis or coarse progress measures (such as grokking metrics) that track performance phases without explaining the micro-level gradient dynamics driving circuit formation.

This paper investigates the underlying optimization dynamics: Why do embedding vectors spontaneously organize into arithmetic progressions, grids, and circles, and what role does weight decay play in driving this geometry?

Methods

The author investigates a simplified single-layer transformer with constant attention (summing two 2D embeddings $x_a + x_b$) followed by an MLP layer ($H=32$ neurons, ReLU) predicting $c = a + b \pmod N$.

To explain the dynamics, the training process is modeled as the co-evolution of two systems—the embeddings and the classifier:

  1. Clustering Force:
  2. Non-matching sums ($i + j \not\equiv k + l \pmod N$) experience an inverse-distance repulsion force because the classifier must draw a separating decision boundary.
  3. Matching sums ($i + j \equiv k + l \pmod N$) experience an attraction force toward their common classification region center.
  4. Alignment Force: When weight decay penalizes MLP layer weights and biases, linear unit boundaries are constrained to pass near the origin. Under these constraints, neurons maximally reduce loss by aligning their weight vectors radially with pair-sums, producing a directional pull that groups embeddings of the same modular sum along rays (the "pizza" algorithm).
  5. Particle Simulation: The author implements a 2D particle simulation where particles correspond to embeddings and updates are computed purely using the analytical clustering and alignment force formulas (no actual neural network or backprop).

Datasets

The study evaluates modular addition with $N = 17$:

  • Dataset: All distinct pairs $(a, b) \in \{0, \dots, 16\}^2$, totaling $N(N + 1)/2 = 153$ unique pair combinations.
  • Split: 80% randomly selected for training and the remaining 20% held out for validation.
  • No external benchmark datasets were used.

Key Insights and Ideas

  • Representation Geometry Dictates Generalization: Embeddings naturally organize into circular rings, regular grids, or imperfect grids. Circles yield the highest generalization, while for non-circular structures, validation accuracy strongly drops as geometric "imperfections" increase.
  • Dual Role of Weight Decay in Dynamics: Rather than merely acting as an $L_2$ capacity penalty, weight decay actively shapes the representation dynamics:
  • By shrinking MLP weights, it keeps classification boundaries broad, preventing embeddings from getting trapped in local overfitting minima.
  • By shrinking MLP biases toward zero, it anchors decision boundaries at the origin, giving birth to the alignment force that yields circular representations.
  • Particle Equivalence: The fact that a stripped-down $N$-body particle simulation generates the identical spectrum of geometries (perfect circles, periodic grids, and defect-laden grids) demonstrates that complex optimization dynamics in modular arithmetic reduce to simple local attraction, repulsion, and radial alignment interactions.

Key Results

  • Generalization vs. Geometry: Across 100 random initializations trained with Adam for 2,000 epochs:
  • Circular solutions consistently outperform non-circular solutions (e.g., at weight decay $\gamma = 0.6$, circles achieve 65.9% average validation accuracy vs. 51.6% for non-circles).
  • For non-circles, the correlation between the number of grid imperfections and validation accuracy is strongly negative (ranging from $-0.64$ to $-0.92$ across weight decay values).
  • Weight Decay Regulates Structure: Increasing weight decay from $0.0 \to 1.0$ increases circle frequency from 0% to 18% and decreases average grid imperfections from $102.2 \pm 6.1$ down to $31.6 \pm 4.0$.
  • Simulation Validation: In the standalone particle simulation, varying the alignment force coefficient $f_a$ from $0.5$ to $2.0$ increases the number of emergent circles from 0 to 56 out of 100 runs, matching empirical observations from neural network training.

Limitations and Future Work

  • Architectural Simplicity: The setup relies on a 2D embedding space ($D=2$) and fixed uniform attention. While qualitative projections are shown for $D=3$ and $D=4$, formal validation in higher dimensions and standard multi-head transformer architectures remains open.
  • Heuristic Force Approximations: The force equations for clustering and alignment are phenomenological approximations rather than rigorous mathematical derivations directly integrated from the SGD/Adam update equations.
  • Future Directions: Proving these dynamics theoretically from first principles, testing whether cross-layer co-evolution via weight decay generalizes to larger real-world language modeling settings, and extending the framework to non-abelian or non-modular arithmetic tasks.

Summary updated

BibTeX
@inproceedings{musat2024clustering,
  author = {Tiberiu Mușat},
  title = {{Clustering and Alignment: Understanding the Training Dynamics in Modular Addition}},
  booktitle = {InterpretableAI @ NeurIPS},
  year = {2024},
  url = {https://openreview.net/forum?id=zjjtn7P8IG},
}
Figure