Introduction
While Transformers have revolutionized natural language processing, they famously struggle with basic arithmetic. They are often viewed as "black boxes" that are difficult to interpret when they do succeed. This paper tackles two main questions: Can a transformer learn a non-trivial mathematical operation like the Greatest Common Divisor (GCD)? And if so, can we fully explain how it makes its predictions?
The author focuses on GCD because it is a fundamental operation in number theory that requires understanding divisibility—a task more complex than simple addition but more structured than arbitrary reasoning. The study demonstrates that small transformers can indeed be trained to compute the GCD of two integers. However, they do not learn the standard Euclidean algorithm. Instead, they develop a "sieve-like" strategy that relies heavily on the number base used for representation and a phenomenon called "grokking" to master small prime numbers.
Methodology
The researcher trained 4-layer transformers to perform a "translation" task: taking a pair of integers $(a, b)$ as input and outputting their GCD.
Key methodological choices included:
- Number Representation: Integers were encoded in various bases ($B$), ranging from prime bases like 2 and 11 to highly composite bases like 30, 210, and 420.
- Training Distributions: The study compared standard uniform sampling (where most numbers are large) against log-uniform sampling (which provides a more balanced mix of small and large numbers).
- Explainability via Probing: Rather than looking at internal weights, the author characterized the model by analyzing its "failure modes"—specifically, what it predicts when it gets the answer wrong.
The study used synthetically generated data, including a training set of integer pairs up to $10^6$, a natural test set of uniformly sampled pairs, and a stratified test set where GCDs are uniformly distributed between 1 and 100 to ensure the model is tested on rare, large GCDs.
Key Insights: The Sieve Strategy and Grokking
The model doesn't "calculate" the GCD in a traditional sense. Instead, it learns to recognize divisibility. It first masters divisors of the base $B$. For example, in base 10, it is easy to see if a number is divisible by 2 or 5 by looking at the last digit. The model learns these "easy" GCDs first.
Primes that do not divide the base are learned through "grokking"—sudden jumps in accuracy after long periods of stagnation. The model eventually "clicks" and begins to recognize these patterns.
The author found that model predictions can be summarized by three simple rules:
- Predictions are deterministic for a given GCD.
- The model only correctly predicts GCDs that are products of "learned" divisors.
- For any input, the model predicts the largest learned divisor that divides both inputs.
The Impact of Distribution and Base
Training distributions significantly impact performance. Training on log-uniform operands (more small numbers) boosts performance, suggesting that "curriculum-like" distributions are crucial for learning mathematical rules. By using log-uniform distributions for both operands and outcomes, the model correctly predicted 91 out of the first 100 GCDs, a massive leap from the 13 predicted under standard uniform training.
Furthermore, highly composite bases (like 420) allow the model to achieve much higher accuracy (up to 96.8%) compared to prime bases (like 11, which hit only 61.8%).
Interestingly, while balancing the distribution of outcomes (making large GCDs as common as small ones) improves accuracy, it creates an Explainability Paradox. The model's logic becomes less deterministic and more "chaotic" as it tries to memorize individual cases, making it harder to explain. Surprisingly, model size had little impact; a 1-layer transformer performed nearly as well as a 24-layer one, suggesting the bottleneck is the data distribution and number representation.
Limitations and Future Work
The primary limitation is that the model does not learn the recursive Euclidean algorithm that humans use. Because it relies on a sieve-like approach, it cannot generalize to GCDs it hasn't "grokked" or that aren't composed of small prime factors it has seen.
Future work could explore whether these distributional tricks can help larger Language Models (LLMs) master more complex arithmetic, or if different architectures could eventually learn the recursive logic of true mathematical algorithms.
Summary updated