Interpreting Grokked Transformers in Complex Modular Arithmetic

Hiroki Furuta, Gouki Minegishi, Yusuke Iwasawa, Yutaka Matsuo

Abstract

Grokking has been actively explored to reveal the mystery of delayed generalization. Identifying interpretable algorithms inside the grokked models is a suggestive hint to understanding its mechanism. In this work, beyond the simplest and well-studied modular addition, we observe the internal circuits learned through grokking in complex modular arithmetic via interpretable reverse engineering, which highlights the significant difference in their dynamics: subtraction poses a strong asymmetry on Transformer; multiplication requires cosine-biased components at all the frequencies in a Fourier domain; polynomials often result in the superposition of the patterns from elementary arithmetic, but clear patterns do not emerge in challenging cases; grokking can easily occur even in higher-degree formulas with basic symmetric and alternating expressions. We also introduce the novel progress measure for modular arithmetic; Fourier Frequency Sparsity and Fourier Coefficient Ratio, which not only indicate the late generalization but also characterize distinctive internal representations of grokked models per modular operation. Our empirical analysis emphasizes the importance of holistic evaluation among various combinations.

Introduction

"Grokking" describes the phenomenon where a neural network suddenly transitions from memorization to perfect generalization long after training accuracy has saturated. While previous research has successfully reverse-engineered this process for simple modular addition—finding that models learn to utilize Fourier transforms and trigonometric identities—it remained unclear if these mechanistic insights generalize to more complex operations. This work investigates whether Transformers employ similar "tricks" for multiplication or higher-order polynomials like $a^2 + ab + b^2 \pmod p$, bridging the gap between toy addition tasks and the structured complexity of real-world data.

Methods and Datasets

The study utilizes single-layer causal Transformers trained on synthetic modular arithmetic datasets with a prime modulus ($p=97$). Operations range from elementary arithmetic (addition, subtraction, multiplication) to linear and polynomial expressions (e.g., $2a+b$, $a^2+b^2$).

To analyze the internal representations, the authors employed:

  • Mechanistic Interpretability: Analyzing weights via Discrete Fourier Transforms to identify frequency attention.
  • Pre-Grokked Ablation: Testing if a model that has already grokked one task can "jumpstart" another by freezing its embedding or Transformer layers.
  • Novel Progress Measures: Introducing Fourier Frequency Density (FFD) to measure representation sparsity and Fourier Coefficient Ratio (FCR) to measure sine/cosine bias.

Mechanistic Interpretability of Operations

A core finding is the Superposition Principle: for complex operations like $ab + b$, the model does not develop an entirely new circuit but rather a representation that is a superposition of the circuits used for addition and multiplication.

The internal logic varies significantly by operation:

  • Addition: Highly sparse in the Fourier domain, utilizing only a few key frequencies.
  • Multiplication: Leverages all frequencies equally but exhibits a strong cosine bias.
  • Subtraction: Imposes a strong asymmetry on the model's internal map, making it harder to transfer knowledge from symmetric operations like addition.
Fourier analysis of internal circuits for addition, subtraction, and multiplication, revealing distinct sparsity and bias patterns.

Key Results

The proposed FFD and FCR metrics serve as "grokking thermometers," decreasing precisely when test accuracy begins to climb, thus signaling the phase of circuit formation.

The study also highlights the role of Factorizability. Models grok "factorizable" polynomials (e.g., $(a+b)^n$) easily by leveraging existing addition circuits. In contrast, "non-factorizable" polynomials (e.g., $a^2 + ab + b^2$) often fail to generalize even with high training data fractions. Furthermore, the authors observed Co-Grokking in multi-task settings, where training on a mixture of similar operations accelerates the generalization of all tasks simultaneously.

Accuracy curves comparing grokking from scratch versus using pre-grokked components across various modular tasks.

Conclusion

While this work identifies clear trends in the Fourier basis for modular arithmetic, deriving exact analytical "pseudocode" for every complex polynomial remains a challenge. Future research should investigate how these modular circuits interact within deeper, multi-layer models and whether they serve as the fundamental building blocks for the emergent reasoning in large-scale language models.

Summary updated

Abstract

Grokking has been actively explored to reveal the mystery of delayed generalization. Identifying interpretable algorithms inside the grokked models is a suggestive hint to understanding its mechanism. In this work, beyond the simplest and well-studied modular addition, we observe the internal circuits learned through grokking in complex modular arithmetic via interpretable reverse engineering, which highlights the significant difference in their dynamics: subtraction poses a strong asymmetry on Transformer; multiplication requires cosine-biased components at all the frequencies in a Fourier domain; polynomials often result in the superposition of the patterns from elementary arithmetic, but clear patterns do not emerge in challenging cases; grokking can easily occur even in higher-degree formulas with basic symmetric and alternating expressions. We also introduce the novel progress measure for modular arithmetic; Fourier Frequency Sparsity and Fourier Coefficient Ratio, which not only indicate the late generalization but also characterize distinctive internal representations of grokked models per modular operation. Our empirical analysis emphasizes the importance of holistic evaluation among various combinations.
BibTeX
@article{furuta2024interpreting,
  author = {Hiroki Furuta and Gouki Minegishi and Yusuke Iwasawa and Yutaka Matsuo},
  title = {{Interpreting Grokked Transformers in Complex Modular Arithmetic}},
  journal = {Accepted by TMLR},
  year = {2024},
  url = {https://openreview.net/forum?id=MzSf70uXJO},
}
Figure