Abstract: Large language models (LLMs) demonstrate proficiency across numerous computational tasks, yet their inner workings remain unclear. In theory, the combination of causal self-attention and multilayer perceptron layers allows every token to access and compute information based on all preceding tokens. In practice, to what extent are such operations present? In this paper, on mental math tasks (i.e., direct math calculation via next-token prediction without explicit reasoning), we investigate this question in three steps: inhibiting input-specific token computations in the initial layers, restricting the routes of information transfer across token positions in the next few layers, and forcing all computation to happen at the last token in the remaining layers. With two proposed techniques, Context-Aware Mean Ablation (CAMA) and Attention-Based Peeking (ABP), we identify an All-for-One subgraph (AF1) with high accuracy on a wide variety of mental math tasks, where meaningful computation occurs very late (in terms of layer depth) and only at the last token, which receives information of other tokens in few specific middle layers. Experiments on a variety of models and arithmetic expressions show that this subgraph is sufficient and necessary for high model performance, transfers across different models, and works on a variety of input styles.
Introduction
In the theoretical framework of Transformer architectures, every token acts as a potential processor. Through the interplay of causal self-attention and multi-layer perceptrons (MLPs), a model could distribute complex calculations across its entire depth and all available token positions. However, the degree to which models actually utilize this distributed capacity for direct arithmetic—termed "mental math"—remains a subject of active research. This paper investigates whether LLMs employ a compositional strategy or a more centralized approach when solving arithmetic problems in a single forward pass.
Methodology
To dissect the internal logic of models like Llama-3-8B, the researchers developed two novel probing techniques:
- Context-Aware Mean Ablation (CAMA): Unlike standard zero-ablation, CAMA replaces a token's activation with its expected value calculated across a diverse dataset. This effectively "blinds" the model to specific operand values (e.g., it recognizes a token is a number but not that it is "42") while preserving the broader task context.
- Attention-Based Peeking (ABP): This technique modifies the attention mask to restrict information flow. It is used to determine exactly when and where the last token must "peek" at previous operands to successfully complete a calculation.
The All-for-One (AF1) Subgraph
By applying these techniques, the authors identified a sparse, highly centralized circuit dubbed the All-for-One (AF1) subgraph. This subgraph reveals that mental math in LLMs typically follows a rigid three-stage process:
- Waiting Stage (Layers 0 to $L_{wait}$): Tokens perform task-general computations to establish context (e.g., identifying the operation) but do not process input-specific operand values.
- Transfer Stage (Layers $L_{wait}$ to $L_{transfer}$): A brief window where the last token attends to the positions of previous operands to "grab" their specific values.
- Last-Token Computation: The final token performs the actual arithmetic calculation within its own residual stream in the remaining layers before predicting the answer.
Key Insights and Results
Centralization and Sparsity
The study found that the "waiting" period is remarkably long. In Llama-3-8B, the model can wait until layer 15—nearly halfway through its depth—before it requires specific operand information. Furthermore, the transfer stage is extremely brief, often spanning only two layers. Within these layers, the circuit is incredibly sparse; for Llama-3, removing 60 out of 64 attention heads in the transfer layers still preserved 95% of the model's original accuracy.
Lack of Compositionality
One of the most striking findings is the lack of intermediate computation. In a three-operand task like $A + B + C$, the model does not compute $(A + B)$ at the second token position and then add $C$ at the end. Instead, all three operands are transferred directly to the final token position for a single, centralized calculation.
Performance and Faithfulness
The AF1 subgraph achieved over 99% faithfulness for addition and subtraction tasks. This indicates that the sparse circuit behaves almost identically to the full, dense model. Logit lens analysis confirmed that the correct answer typically emerges in the vocabulary space around layer 24, reinforcing the finding that the heavy lifting occurs late in the network.
Discussion and Limitations
While the AF1 subgraph is robust across various input templates (symbolic, verbal, and question-answering), its performance degrades on complex word problems and Python-style code. This suggests that while "mental math" utilizes this specific centralized circuit, more complex reasoning tasks likely require additional semantic processing layers. Additionally, the current findings are somewhat dependent on the tokenizer; if a single number is split into multiple tokens, the AF1 subgraph would require extension to handle multi-token communication.
Conclusion
This research demonstrates that LLMs solve mental math through a highly centralized "All-for-One" mechanism rather than a distributed, compositional one. The discovery that large portions of the model are essentially "waiting" or redundant for these tasks opens new avenues for mechanistic interpretability and suggests potential for significant inference optimizations by skipping unnecessary computations in early layers.
Summary updated