Language Models Use Trigonometry to Do Addition — part 1/4
Cleaned-text code points [0, 15679). Bibliography omitted here; source.md includes it.
# Language Models Use Trigonometry to Do Addition
###### Abstract
Mathematical reasoning is an increasingly important indicator of large language model (LLM) capabilities, yet we lack understanding of how LLMs process even simple mathematical tasks. To address this, we reverse engineer how three mid-sized LLMs compute addition. We first discover that numbers are represented in these LLMs as a generalized helix, which is strongly causally implicated for the tasks of addition and subtraction, and is also causally relevant for integer division, multiplication, and modular arithmetic. We then propose that LLMs compute addition by manipulating this generalized helix using the “Clock” algorithm: to solve a + b a+b , the helices for a a and b b are manipulated to produce the a + b a+b answer helix which is then read out to model logits. We model influential MLP outputs, attention head outputs, and even individual neuron preactivations with these helices and verify our understanding with causal interventions. By demonstrating that LLMs represent numbers on a helix and manipulate this helix to perform addition, we present the first representation-level explanation of an LLM’s mathematical capability.
###### Keywords:
\printAffiliationsAndNoticeMODIFIED
## 1 Introduction
Large language models (LLMs) display surprising and significant aptitude for mathematical reasoning ( Ahn et al., 2024 ; Satpute et al., 2024 ) , which is increasingly seen as a benchmark for LLM capabilities ( OpenAI, ; Glazer et al., 2024 ) . Despite LLMs’ mathematical proficiency, we have limited understanding of how LLMs process even simple mathematical tasks like addition. Understanding mathematical reasoning is valuable for ensuring LLMs’ reliability, interpretability, and alignment in high-stakes applications.
In this study, we reverse engineer how GPT-J, Pythia-6.9B, and Llama3.1-8B compute the addition problem a + b a+b for a , b ∈ [ 0 , 99 ] a,b\in[0,99] . Remarkably, we find that LLMs use a form of the “Clock” algorithm to compute addition, which was previously proposed by Nanda et al. (2023a) as a mechanistic explanation of how one layer transformers compute modular addition (and later named by Zhong et al. (2023) ).
To compute a + b a+b , all three LLMs represent a a and b b as a helix on their tokens and construct helix ( a + b ) \mathrm{helix}(a+b) on the last token, which we verify with causal interventions. We then focus on how GPT-J implements the Clock algorithm by investigating MLPs, attention heads, and even specific neurons. We find that these components can be understood as either constructing the a + b a+b helix by manipulating the a a and b b helices, or using the a + b a+b helix to produce the answer in the model’s logits. We visualize this procedure in Fig. 1 as rotating the dial of a clock.
Our work is in the spirit of mechanistic interpretability (MI), which attempts to reverse engineer the functionality of machine learning models. However, most LLM MI research focuses either on identifying circuits, which are the minimal set of model components required for computations, or understanding features, which are the representations of concepts in LLMs. A true mechanistic explanation requires understanding both how an LLM represents a feature and how downstream components manipulate that feature to complete a task. To our knowledge, we are the first work to present this type of description of an LLM’s mathematical capability, identifying that LLMs represent numbers as helices and compute addition by manipulating these helices with the interpretable Clock algorithm.
## 2 Related Work
Circuits. Within mechanistic interpretability, circuits research attempts to understand the key model components (MLPs and attention heads) that are required for specific functionalities ( Olah et al., 2020 ; Elhage et al., 2021 ) . For example, Olsson et al. (2022) found that in-context learning is primarily driven by induction attention heads, and Wang et al. (2023) identified a sparse circuit of attention heads that GPT-2 uses to complete the indirect object of a sentence. Understanding how multilayer perceptrons (MLPs) affect model computation has been more challenging, with Nanda et al. (2023b) attempting to understand how MLPs are used in factual recall, and Hanna et al. (2023) investigating MLP outputs while studying the greater-than operation in GPT-2.
Features. Another branch of MI focuses on understanding how models represent human-interpretable concepts, known as features. Most notably, the Linear Representation Hypothesis posits that LLMs store features as linear directions ( Park et al., 2023 ; Elhage et al., 2022 ) , culminating in the introduction of sparse autoencoders (SAEs) that decompose model activations into sparse linear combinations of features ( Huben et al., 2024 ; Bricken et al., 2023 ; Templeton et al., 2024 ; Gao et al., 2024 ; Rajamanoharan et al., 2024 ) . However, recent work from Engels et al. (2024) found that some features are represented as non-linear manifolds, for example the days of the week lie on a circle. Levy & Geva (2024) and Zhu et al. (2025) model LLMs’ representations of numbers as a circle in base 10 and as a line respectively, although with limited causal results. Recent work has bridged features and circuits research, with Marks et al. (2024) constructing circuits from SAE features and Makelov et al. (2024) using attention-based SAEs to identify the features used in Wang et al. (2023) ’s IOI task.
Reverse engineering addition. Liu et al. (2022) first discovered that one layer transformers generalize on the task of modular addition when they learn circular representations of numbers. Following this, Nanda et al. (2023a) introduced the “Clock” algorithm as a description of the underlying angular addition mechanisms these transformers use to generalize. However, Zhong et al. (2023) found the “Pizza” algorithm as a rivaling explanation for some transformers, illustrating the complexity of decoding even small models. Stolfo et al. (2023) identifies the circuit used by LLMs in addition problems, and Nikankin et al. (2024) claims that LLMs use heuristics implemented by specific neurons rather than a definite algorithm to compute arithmetic. Zhou et al. (2024) analyze a fine-tuned GPT-2 and found that Fourier components in numerical representations are critical for addition, while providing preliminary results that larger base LLMs might use similar features.
## 3 Problem Setup
Models As in Nikankin et al. (2024) , we analyze 3 LLMs: GPT-J (6B parameters) ( Wang & Komatsuzaki, 2021 ) , Pythia-6.9B ( Biderman et al., 2023 ) , and Llama3.1-8B ( Grattafiori et al., 2024 ) . All three models are autoregressive transformers which process tokens x 0 , … , x n x_{0},...,x_{n} to produce probability distributions over the likely next token x n + 1 x_{n+1} ( Vaswani et al., 2017 ) . The i i th token is embedded as L L hidden state vectors (also known as the residual stream), where L L is the number of layers in the transformer. Each hidden state is the sum of multilayer perceptron ( MLP \mathrm{MLP} ) and attention ( attn \mathrm{attn} ) layers.
h i l \displaystyle h^{l}_{i} = h i l − 1 + a i l + m i l , \displaystyle=h^{l-1}_{i}+a^{l}_{i}+m^{l}_{i}, (1) a i l \displaystyle a^{l}_{i} = attn l ( h 1 l − 1 , h 2 l − 1 , … , h i l − 1 ) , \displaystyle=\mathrm{attn}^{l}\left(h^{l-1}_{1},h^{l-1}_{2},\dots,h^{l-1}_{i}\right), m i l \displaystyle m^{l}_{i} = MLP l ( a i ( l ) + h i l − 1 ) . \displaystyle=\mathrm{MLP}^{l}(a^{(l)}_{i}+h^{l-1}_{i}).
GPT-J and Pythia-6.9B use simple MLP implementations, namely MLP ( x ) = σ ( x W up ) W down \mathrm{MLP}(x)=\sigma\left({xW_{\mathrm{up}}}\right)W_{\mathrm{down}} , where σ ( x ) \sigma(x) is the sigmoid function. Llama3.1-8B uses a gated MLP, MLP ( x ) = σ ( x W gate ) ∘ ( x W in ) W out \mathrm{MLP}(x)=\sigma\left(xW_{\mathrm{gate}}\right)\circ\left(xW_{\mathrm{in}}\right)W_{\mathrm{out}} , where ∘ \circ represents the Hadamard product ( Liu et al., 2021 ) . GPT-J tokenizes the numbers [ 0,361 ] [0,361] (with a space) as a single token, Pythia-6.9B tokenizes [ 0,557 ] [0,557] as a single token, and Llama3.1-8B tokenizes [ 0,999 ] [0,999] as a single token. We focus on the single-token regime for simplicity.
Data To ensure that answers require only a single token for all models, we construct problems a + b a+b for integers a , b ∈ [ 0 , 99 ] a,b\in[0,99] . We evaluate all three models on these 10,000 addition problems, and find that all models can competently complete the task: GPT-J achieves 80.5% accuracy, Pythia-6.9B achieves 77.2% accuracy, and Llama3.1-8B achieves 98.0% accuracy. For the prompts used and each model’s performance heatmap by a a and b b , see Appendix A . Despite Llama3.1-8B’s impressive performance, in the main paper we focus our analysis on GPT-J because its simple MLP allows for easier neuron interpretation. We report similar results for Pythia-6.9B and Llama3.1-8B in the Appendix.
## 4 LLMs Represent Numbers as a Helix
To generate a ground up understanding of how LLMs compute a + b a+b , we first aim to understand how LLMs represent numbers. To identify representational trends, we run GPT-J on the single-token integers a ∈ [ 0,360 ] a\in[0,360] . We do not use a = 361 a=361 because 360 360 has more integer divisors, allowing for a simpler analysis of periodic structure. We conduct analysis on h 360 0 h^{0}_{360} , which is the residual stream following layer 0 with shape [ 360 , model _ dim ] [360,\mathrm{model\_dim}] . We choose to use the output of layer 0 rather than directly analyzing the embeddings because prior work has shown that processing in layer 0 is influential for numerical tasks ( Nikankin et al., 2024 ) .
### 4.1 Investigating Numerical Structure
Linear structure . To investigate structure in numerical representations, we perform a PCA ( F.R.S., 1901 ) on h 360 0 h^{0}_{360} and find that the first principal component (PC1) for a ∈ [ 0,360 ] a\in[0,360] has a sharp discontinuity at a = 100 a=100 (Fig. 15 , Appendix B ), which implies that GPT-J uses a distinct representation for three-digit integers. Instead, in the bottom of Fig. 2 , we plot PC1 for h 99 0 h^{0}_{99} and find that it is well approximated by a line in a a . Additionally, when plotting the Euclidean distance between a a and a + δ n a+\delta n for a ∈ [ 0 , 9 ] a\in[0,9] (Fig. 14 , Appendix B ), we see that the distance is locally linear. The existence of linear structure is unsurprising - numbers are semantically linear, and LLMs often represent concepts linearly.
Periodic Structure. We center and apply a Fourier transform to h 360 0 h^{0}_{360} with respect to the number a a being represented and the model _ dim \mathrm{model\_dim} . In Fig. 2 , we average the resulting spectra across model _ dim \mathrm{model\_dim} and observe a sparse Fourier domain with high-frequency components at T = [ 2 , 5 , 10 ] T=[2,5,10] . Additionally, when we compare the residual streams of all pairs of integers a 1 a_{1} and a 2 a_{2} , we see that there is distinct periodicity in both their Euclidean distance and cosine similarity (Fig. 13 , Appendix B ). These Fourier features were also identified by Zhou et al. (2024) , and although initially surprising, are sensible. The units digit of numbers in base 10 is periodic ( T = 10 T=10 ), and it is reasonable that qualities like evenness ( T = 2 T=2 ) are useful for tasks.
### 4.2 Parameterizing the Structure as a Helix
To account for both the periodic and linear structure in numbers, we propose that numbers can be modeled helically. Namely, we posit that h a l h^{l}_{a} , the residual stream immediately preceding layer l l for some number a a , can be modeled as h a l \displaystyle h^{l}_{a} = helix ( a ) = C B ( a ) T , \displaystyle=\mathrm{helix}(a)=CB(a)^{T}, (2) B ( a ) \displaystyle B(a) = [ a , cos ( 2 π T 1 a ) , sin ( 2 π T 1 a ) , \displaystyle=\big[a,\cos\left(\frac{2\pi}{T_{1}}a\right),\sin\left(\frac{2\pi}{T_{1}}a\right), … , cos ( 2 π T k a ) , sin ( 2 π T k a ) ] . \displaystyle\dots,\cos\left(\frac{2\pi}{T_{k}}a\right),\sin\left(\frac{2\pi}{T_{k}}a\right)\big].
C C is a matrix applied to the basis of functions B ( a ) B(a) , where B ( a ) B(a) uses k k Fourier features with periods T = [ T 1 , … T k ] T=[T_{1},\dots T_{k}] . The k = 1 k=1 case represents a regular helix; for k > 1 k>1 , the independent Fourier features share a single linear direction. We refer to this structure as a generalized helix, or simply a helix for brevity.
We identify four major Fourier features: T = [ 2 , 5 , 10 , 100 ] T=[2,5,10,100] . We use the periods T = [ 2 , 5 , 10 ] T=[2,5,10] because they have significant high frequency components in Fig. 2 . We are cautious of low frequency Fourier components, and use T = 100 T=100 both because of its significant magnitude, and by applying the inductive bias that our number system is base 10.
### 4.3 Fitting a Helix
We fit our helical form to the residual streams on top of the a a token for our a + b a+b dataset. In practice, we first use PCA to project the residual stream at each layer to 100 dimensions. To ensure we do not overfit with Fourier features, we consider all combinations of k k Fourier features, with k ∈ [ 1 , 4 ] k\in[1,4] . If we use k k Fourier features, the helical fit uses 2 k + 1 2k+1 basis functions (one linear component, 2 k 2k periodic components). We then use linear regression to find some coefficient matrix C PCA C_{\mathrm{PCA}} of shape 100 × 2 k + 1 100\times 2k+1 that best satisfies PCA ( h a l ) = C PCA B ( a ) T \mathrm{PCA}(h^{l}_{a})=C_{\mathrm{PCA}}B(a)^{T} . Finally, we use the inverse PCA transformation to project C PCA C_{\mathrm{PCA}} back into the model’s full residual stream dimensionality to find C C .
We visualize the quality of our fit for layer 0 when using all k = 4 k=4 Fourier features with T = [ 2 , 5 , 10 , 100 ] T=[2,5,10,100] in Fig. 3 . To do so, we calculate C † h C^{\dagger}h , where C † C^{\dagger} is the Moore-Penrose pseudo-inverse of C C . Thus, C † h C^{\dagger}h represents the projection of the residual stream into the helical subspace. When analyzing the columns of C C , we find that the Fourier features increase in magnitude with period and are mostly orthogonal (Appendix C.1 ).
### 4.4 Evaluating the Quality of the Helical Fit
We want to causally demonstrate that the model actually uses the fitted helix. To do so, we employ activation patching. Activation patching isolates the contribution of specific model components towards answer tokens ( Meng et al., 2022 ; Heimersheim & Nanda, 2024 ) . Specifically, to evaluate the contribution of some residual stream h a l h_{a}^{l} on the a a token, we first store h a , clean l h_{a,\text{clean}}^{l} when the model is run on a “clean” prompt a + b a+b . We then run the model on the corrupted prompt a ′ + b a^{\prime}+b and store the model logits for the clean answer of a + b a+b . Finally, we patch in the clean h a , clean l h_{a,\text{clean}}^{l} on the corrupted prompt a ′ + b a^{\prime}+b and calculate L D a l = logit patched ( a + b ) − logit corrupted ( a + b ) LD_{a}^{l}=\mathrm{logit_{patched}}(a+b)-\mathrm{logit_{corrupted}}(a+b) , where L D a l LD_{a}^{l} is the logit difference for h a l h_{a}^{l} . By averaging over 100 pairs of clean and corrupted prompts, we can evaluate h a l h_{a}^{l} ’s ability to restore model behavior to the clean answer a + b a+b . To reduce noise, all patching experiments only use prompts the model can successfully complete.