Towards Empirical Interpretation of Internal Circuits and Properties in Grokked Transformers on Modular Polynomials

Hiroki Furuta, Gouki Minegishi, Yusuke Iwasawa, Yutaka Matsuo

TL;DR

This paper extends the study of "grokking"—the phenomenon where models suddenly generalize long after overfitting—from simple modular addition to complex operations like multiplication and polynomials. The authors demonstrate that Transformers learn distinctive internal "circuits" based on Fourier representations for these tasks and introduce two new metrics, Fourier Frequency Density (FFD) and Fourier Coefficient Ratio (FCR), to track this late-stage generalization.

What Problem Does This Paper Address?

Grokking has fascinated the machine learning community because it defies standard intuition: a model first memorizes the training data (reaching 100% train accuracy but 0% test accuracy) and then, after much more training, suddenly "clicks" and achieves perfect generalization.

While previous research successfully "reverse-engineered" how Transformers solve modular addition using trigonometric identities and Fourier bases, it remained unclear if these findings were a special case. This paper asks: Do these Fourier-based internal representations hold for more complex math? Can we find common features that transfer between different operations? And does training on multiple tasks at once help or hinder this mysterious process?

Methods

The researchers trained small, single-layer causal Transformers on various modular arithmetic tasks (modulo $p=97$). To look under the hood, they employed several key techniques:

  • Mechanistic Interpretability: They analyzed the weights and logits using Discrete Fourier Transforms to see which frequencies the model prioritized.
  • New Progress Measures: They introduced Fourier Frequency Density (FFD) to measure the sparsity of representations (key for addition) and Fourier Coefficient Ratio (FCR) to measure sinusoidal bias (key for multiplication).
  • Pre-Grokked Modules: As shown in Figure 1, they tested transferability by taking a module (like the embedding layer) from a model that had already "grokked" one task and freezing it while training a new model on a different task.
Figure 1: Grokking has been inves- tigated with training from scratch. To shed light on the dynamics in- side Transformer, we introduce the notion of pre-grokked models, which are pre-trained on a similar task un- til grokking and used to replace ran- domly initialized modules without any parameter updates (i.e. frozen). We use pre-grokked embedding and Transformer in the later section.

Datasets

The study uses synthetic algorithmic datasets consisting of pairs of integers $(a, b)$ and a target $c$ defined by a modular operation $(a \circ b) \pmod p$.

  • Elementary Arithmetic: Addition, subtraction, and multiplication.
  • Modular Polynomials: A wide range of expressions including univariate terms ($a^2 + b$), cross terms ($ab + a + b$), and higher-degree factorizable polynomials like $(a+b)^n$.
  • Multi-task Mixtures: Combined datasets of different operations to study "co-grokking."

Key Insights and Ideas

  • Factorizability is the "Secret Sauce": One of the most striking findings is that polynomials that can be factored into simpler terms (like $(a+b)^2$) grok much more easily than non-factorizable ones (like $a^2 + ab + b^2$). Factorization seems to provide a "pathway" for the model to reuse simpler circuits.
  • Subtraction’s Hidden Asymmetry: Mathematically, subtraction is just addition with a negative sign. However, the Transformer sees it differently. While addition learns symmetric representations, subtraction imposes a strong asymmetry in the model's internal "neuron-logit map," making it surprisingly difficult to transfer knowledge between the two.
  • Representational Superposition: For complex polynomials, the model doesn't just learn a brand-new algorithm. Instead, it often discovers a superposition of the Fourier representations used for elementary addition and multiplication.
  • Co-Grokking: In multi-task settings, the model can experience "co-grokking," where generalization for all tasks happens simultaneously. Interestingly, training on a mixture of "easy" and "hard" tasks can sometimes accelerate generalization for the harder task.

Key Results

The authors found that their new metrics, FFD and FCR, act as excellent "thermometers" for grokking, decreasing sharply exactly when test accuracy begins to climb.

As seen in Figure 2, transferability is highly specific. Pre-grokked embeddings for addition and subtraction accelerate each other, but multiplication requires a fundamentally different representation. Furthermore, transferring a pre-grokked Transformer block for subtraction is notoriously difficult, even when transferring to another subtraction task with a different initialization.

Figure 2: Test accuracy in modular elementary arithmetic (addition, subtraction, and multiplication) with pre-grokked models (embedding and Transformer). The x-axis is the logarithmic scale. Because of the task simplicity, grokking always occurs in elementary arithmetic. However, in certain combinations, pre-grokked models hinder grokking even with a r = 0.9 fraction. For pre-grokked embedding, addition and subtraction accelerate grokking each other (fig[0:2, 0:2]), while multiplication and those do not show synergy (+: fig[2, 0] and [0, 2], −: fig[2, 1] and [1, 2]). In contrast, for pre-grokked Transformer, subtraction is challenging in both directions, even transferring subtraction models into subtraction itself (fig[1, 4]). With small r, addition and multiplication accelerate each other (fig[0, 5] and [2, 3]).

Quantitatively, the study shows that while simple tasks grok within $10^3$ steps, complex polynomials can require upwards of $10^5$ steps, and some non-factorizable expressions fail to generalize entirely within the experimental window.

Limitations and Future Work

The authors acknowledge that their work is limited to synthetic, small-scale models. While these provide a "clean" laboratory for studying grokking, the gap between modular polynomials and the massive, real-world datasets used by Large Language Models (LLMs) remains significant.

Future research directions include:

  • Deriving exact analytical solutions for the internal circuits of multiplication and polynomials (currently, the interpretations are largely empirical).
  • Investigating if similar "circuit formation" phases occur during the training of LLMs on natural language.
  • Exploring how these findings relate to "emergent capabilities" in larger models.

Summary updated

Abstract

Grokking has been actively explored to reveal the mystery of delayed generalization and identifying interpretable representations and algorithms inside the grokked models is a suggestive hint to understanding its mechanism. Grokking on modular addition has been known to implement Fourier representation and its calculation circuits with trigonometric identities in Transformers. Considering the periodicity in modular arithmetic, the natural question is to what extent these explanations and interpretations hold for the grokking on other modular operations beyond addition. For a closer look, we first hypothesize that any modular operations can be characterized with distinctive Fourier representation or internal circuits, grokked models obtain common features transferable among similar operations, and mixing datasets with similar operations promotes grokking. Then, we extensively examine them by learning Transformers on complex modular arithmetic tasks, including polynomials. Our Fourier analysis and novel progress measure for modular arithmetic, Fourier Frequency Density and Fourier Coefficient Ratio, characterize distinctive internal representations of grokked models per modular operation; for instance, polynomials often result in the superposition of the Fourier components seen in elementary arithmetic, but clear patterns do not emerge in challenging non-factorizable polynomials. In contrast, our ablation study on the pre-grokked models reveals that the transferability among the models grokked with each operation can be only limited to specific combinations, such as from elementary arithmetic to linear expressions. Moreover, some multi-task mixtures may lead to co-grokking -- where grokking simultaneously happens for all the tasks -- and accelerate generalization, while others may not find optimal solutions. We provide empirical steps towards the interpretability of internal circuits.
BibTeX
@article{furuta2024towards,
  author = {Hiroki Furuta and Gouki Minegishi and Yusuke Iwasawa and Yutaka Matsuo},
  title = {{Towards Empirical Interpretation of Internal Circuits and Properties in Grokked Transformers on Modular Polynomials}},
  journal = {Accepted by TMLR},
  year = {2024},
  url = {https://openreview.net/forum?id=MzSf70uXJO},
}
Figure