Learning to grok: Emergence of in-context learning and skill composition in modular arithmetic tasks

Tianyu He, Darshil Doshi, Aritra Das, Andrey Gromov · DOI

Introduction

Large Language Models (LLMs) possess the remarkable ability to perform in-context learning (ICL), solving entirely new tasks by observing a few examples within a prompt. However, the underlying mechanics of how these models transition from simple pattern matching to the composition of complex skills remain largely opaque. This paper investigates this "black box" by exploring the phenomenon of "grokking"—a sudden shift from memorization to generalized reasoning—within the controlled environment of modular arithmetic.

While previous research has documented grokking in simple addition, this study extends the inquiry to task distributions. By training Transformers on various linear modular functions, the authors demonstrate how models develop a generalized "algorithmic engine" capable of solving unseen problems through the composition of learned skills.

Experimental Framework

The researchers utilized a GPT-style Transformer architecture trained on a collection of linear modular functions defined by the equation:

$$z = ax + by \pmod p$$

In this setup, each "task" is defined by a unique pair of coefficients $(a, b)$. For a prime number $p=29$, the total task space consists of $p^2 = 841$ possible functions. The model is presented with sequences of triples $(x, y, z)$ and must predict the value of $z$ for a given $(x, y)$ based on the preceding examples in the context.

To understand the drivers of generalization, the authors varied several parameters:

  • Task Diversity ($n_{i.d.}$): The number of unique tasks included in the training set.
  • Data Density ($\alpha$): The amount of data provided per task.
  • Model Depth: Architectures with 2, 4, and 6 transformer blocks were compared to assess how depth influences skill composition.

The Four Phases of Learning

The study identifies a distinct "phase diagram" of model behavior as task diversity increases. Models do not simply "learn" or "not learn"; they progress through four evolutionary stages:

  1. ID Memorization: The model only succeeds on specific examples it has seen during training within known tasks.
  2. ID Generalization: The model can solve seen tasks using new, unseen number pairs.
  3. OOD Memorization: The model begins to solve unseen (Out-of-Distribution) tasks, but only when the specific number pairs in the prompt have appeared in other contexts during training.
  4. OOD Generalization: The true "grokking" moment. The model develops a universal understanding of the underlying logic, allowing it to solve unseen tasks with entirely unseen number pairs.

Geometric and Algorithmic Insights

The "Circle-of-Circles" Representation

One of the paper's most significant findings is the discovery of a structured internal geometry. Rather than treating numbers as abstract tokens, the model maps them onto circular patterns. This "Circle-of-Circles" representation is mathematically optimal for modular arithmetic, as it allows the model to treat addition and multiplication as rotations within a periodic space. This geometric alignment is a prerequisite for the model to transition from memorization to calculation.

Algorithmic Shifting

The strategy the model employs is not static; it changes based on the information available in the prompt. In deeper models, the researchers observed an "algorithmic shift":

  • Ratio Matching: With only 1 or 2 examples, the model attempts to find a direct scalar relationship between inputs and outputs.
  • Modular Regression: When provided with more context (e.g., 16+ examples), the model shifts to a more sophisticated approach, essentially performing a form of modular regression to solve the system of linear equations on the fly.

Key Results and Robustness

  • Architectural Requirements: The study found that a minimum of two transformer blocks is required for OOD generalization. A single-layer model lacks the depth necessary to compose the skills required for generalized modular arithmetic.
  • Critical Mass: For the $p=29$ dataset, models typically required exposure to at least 256 different tasks before the transition to OOD generalization occurred.
  • Transient Generalization: Interestingly, in deeper models, generalization can be "transient." If training continues too long, the model's memorization circuits may eventually overtake its generalization circuits, leading to a decline in OOD performance. This suggests that early stopping is a vital component of training for generalization.
  • Noise Resilience: Deeper models (4+ layers) demonstrated significant robustness, maintaining accuracy even when up to 40% of the in-context examples were corrupted with incorrect labels. This indicates the model learns a consensus-based algorithm rather than a simple lookup table.

Conclusion and Limitations

This research provides a "clean room" observation of how Transformers move from rote memorization to algorithmic reasoning. While the use of synthetic modular arithmetic data allows for precise interpretability, it remains a simplification of the complexities found in natural language. Future work is needed to determine if the circular representations and phase transitions observed here are universal features of Transformer models across more diverse and less structured datasets. Nonetheless, the study offers a compelling framework for understanding how model depth and task diversity drive the emergence of in-context learning.

Summary updated

BibTeX
@inproceedings{he2024learning,
  author = {Tianyu He and Darshil Doshi and Aritra Das and Andrey Gromov},
  title = {{Learning to grok: Emergence of in-context learning and skill composition in modular arithmetic tasks}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2024},
  url = {https://openreview.net/forum?id=aVh9KRZdRk},
}
Figure