Feed 0% source
AI/ML AI-generated

Assign and Add: A Mechanistic Study of Compositional Arithmetic

Generated by a local model (nvidia/Gemma-4-26B-A4B-NVFP4) from a scientific paper, claim-checked against the full text. Provenance is open by design.

Unlocking Compositionality: How Transformers Learn to Bind Variables and Perform Arithmetic

AI models learn complex tasks in stages. First, they learn how to do math. Then, they learn how to remember which numbers belong to which labels. Finally, they combine these two skills to solve new problems they have not seen during training.

In the field of mechanistic interpretability (the study of reverse-engineering the internal weights of neural networks), a major open question is how "compositionality" emerges. We know that large language models (LLMs) can tackle complex tasks by combining smaller, atomic skills. However, the precise mechanism by which these disparate circuits integrate remains elusive. Specifically, we do not fully understand how a model moves from simple arithmetic to performing that same arithmetic on variables dynamically assigned in the context.

This paper provides a mechanistic answer. The authors demonstrate that compositional generalization is not a monolithic emergence. Instead, it is a result of integrating two independent, pre-learned modules: a modular addition circuit and a variable assignment mechanism.

The Problem

Current understanding of LLM generalization often treats it as a black box. While we know models can perform out-of-distribution (OOD) tasks (tasks involving data distributions different from the training set), we lack a clear picture of how functional circuits actually integrate. Specifically, we need to know how associative recall (retrieving information) and algorithmic reasoning (performing math) shake hands in the residual stream (the primary communication channel between transformer layers).

When a model encounters a sequence like c=1, b=17, a=42, b+19=?, it must perform two distinct operations. It must bind the variable b to the value 17. It must then feed that value into an addition circuit. If these circuits fail to compose, the model fails the task. This happens even if the model is a "math expert" or a "memory expert" in isolation. Previous research has looked at these phenomena separately. There has been little clarity on the training dynamics that force these two different types of logic to merge.

How It Works

The researchers use a 2-layer transformer trained on a controlled task of modular addition (modulo $N=59$) combined with variable assignment. The architecture is intentionally stripped down. It uses a single attention head and omits the first-layer MLP (multi-layer perceptron, a type of feed-forward neural network) to make circuit discovery tractable.

The composition happens through a specific interplay of two circuits:

  1. The Variable Assignment Module: This is implemented via a modified induction head (a circuit that helps models copy information). Traditionally, induction heads implement a copying mechanism. Here, the authors find that the first layer develops a "previous-token head" behavior. Constant tokens look back at the variable assigned to them. This effectively remaps the variable's identity into the residual stream .
Figure 4
Figure 4. Attention patterns for an example sequence. Left (Layer 1): The = token attends to the two immediately preceding positions representing the operands (orange boxes). Simultaneously, constant tokens in positions 1–11 act as previous-token heads, attending to their assigned variables (red boxes).
  1. The Modular Addition Module: This is located in the second-layer MLP. The authors show that the MLP learns to represent modular arithmetic using Fourier features (periodic, sinusoidal representations). These allow the network to compute sums through constructive interference .

The "magic" of composition lies in the routing. The second-layer attention (the QK circuit, which determines where information is sent) acts as the glue. It identifies the correct operands—whether they are raw constants or retrieved variable values. It then routes them to the second-layer MLP. The authors prove that the same MLP module is used regardless of whether the input is a direct constant or an indirect variable. This confirms the model has truly composed the two skills.

Numbers

The authors report that the model achieves over 98% test accuracy on complex 2-variable sequences. This means it succeeds even on unseen combinations of variables and numbers. However, this capability is gated by specific data requirements.

The paper finds that generalization is highly sensitive to the density of the training distribution: * Arithmetic Density: To generalize to the entire 0-variable (pure constant) set, the model requires training on at least $f \approx 0.25$ of all possible addition pairs [Figure 3b]. This means the model needs to see roughly a quarter of all possible math pairs to master basic arithmetic. * Task Complexity: To successfully generalize to 2-variable sequences, the model requires a relative frequency of $r \approx 0.2$ in the training set [Figure 3a]. This indicates the model needs a minimum threshold of complex examples to learn the composition.

Crucially, the training dynamics reveal a clear hierarchy of learning. The model first masters 0-variable modular addition. Then, it builds the structure for variable assignment. Finally, it enters a "refinement" phase to resolve difficult routing cases .

Figure 2
Figure 2. Accuracies during training partitioned by evaluation set. Add-restricted sets contain sequences with held-out addition pairs as discussed in Section 3, and var-restricted sets contain sequences with held-out variable positions.

This suggests that arithmetic competence is a prerequisite for variable binding in this architecture.

What's Missing

While the mechanistic insights are deep, the scope is narrow.

First, the study relies on a highly controlled, low-complexity synthetic task. This is necessary for the mathematical proofs. However, it is a far cry from the messy, high-dimensional reasoning required in natural language. We do not know if these clean "circuit handshakes" survive the introduction of linguistic ambiguity or massive scale.

Second, the theoretical analysis assumes a "disentangled" transformer setup with orthogonal embeddings (vectors that are perpendicular and do not overlap). This is a significant idealization. In a real-world production model, embeddings are dense and highly correlated. The assumption that the gradient is dominated by a single associative-memory term might not hold when the noise from other features is higher.

Finally, the paper does not address scaling laws. It shows what happens in a 2-layer, 128-dimension model. It leaves open whether these circuits become more or less modular as the model depth and width increase.

Should You Prototype This

Not yet.

If you are looking for a way to improve the reasoning capabilities of a production LLM, this paper does not provide a new loss function or a better architecture to ship. It is a foundational piece of mechanistic interpretability. It explains why current models behave the way they do.

However, if you are building specialized symbolic reasoners, the methodology is invaluable. The researchers released their code under the MIT License (available via MechCompose). If you want to test if your own small-scale models are developing these modular circuits, the codebase is a solid starting point. For now, treat this as a map of the terrain, not a blueprint for a new engine.

Figures from the paper

Figure 1
Figure 1. Examples of tokenized sequences formed of variables and constants. The final token in blue represents the target answer, and is not present in the training sequence. Note that the third sequence will be in the test set, since it has b in the first position of the addition.
Figure 3
Figure 3. Data requirements for generalization. (a) Test accuracy on 2-variable sequences as a function of their relative frequency in the training set. The model requires a relative frequency of r ≈0.2 to successfully generalize.
Figure 5
Figure 5. Residual stream similarities. (a) Cosine similarities between the pre-MLP residual stream vectors of different sequences. ”Matched” pairs contain the same underlying addition operation (e.g., b 3 + b 4 = versus + 3 4 =), whereas ”Mismatched” pairs do not.
Figure 6
Figure 6. Layer 1 QK matrix. The previous-token head is formed by a combination of effects: constant tokens exhibit a strong preference for variables (purple outline); sequence positions 0–11, where variable assignments occur, also prefer variables (orange outline); and the positional interaction term
Novelty
0.0/10
Overall
0.0/10
#mechanistic interpretability#compositionality#transformer circuits#training dynamics
How this was made
Generation

Model: nvidia/Gemma-4-26B-A4B-NVFP4
Persona: habr_engineer
Refinement: 0
Pipeline: forge-1.0

Verification

Evaluator: nvidia/Gemma-4-26B-A4B-NVFP4
Score: 94% (passed)
Claims verified: 13 / 13

Translation

Model: nvidia/Gemma-4-26B-A4B-NVFP4

Hardware & cost

NVIDIA GB10 · 128 GB unified · NVFP4 · 100% local · $0 cloud
Tokens: 85,501
Wall-time: 365.2s
Tokens/s: 234.1

Next up

Unlocking Concept Binding: Why CLIP Fails and How Transformers Can Succeed

8.7/10· 5 min