TL;DR
This paper investigates how small transformer models learn to count tokens in a sequence, identifying two distinct strategies: relation-based counting (efficiently using attention) and inventory-based counting (using the MLP as a lookup table). The authors demonstrate that minor architectural choices, like adding a beginning-of-sequence (BOS) token or a softmax layer, fundamentally shift which algorithm the model learns and how robust it is to small embedding dimensions.
What Problem Does This Paper Address?
Despite their massive scale, modern Large Language Models (LLMs) often fail at seemingly trivial tasks like counting characters or words in a string without using "Chain-of-Thought" prompting. This paper seeks to understand the mechanistic "why" behind this failure by stripping the transformer down to its most basic components.
The authors focus on the histogram task: given a sequence of tokens, the model must output the count of each token at its respective position. Solving this requires two fundamental operations: comparison (is this token the same as that one?) and aggregation (how many total matches are there?). By studying 1-layer transformer blocks in a controlled setting, the researchers explore how architectural inductive biases—such as embedding size ($d$), MLP hidden layer size ($p$), and the type of attention mechanism—dictate the model's ability to learn these operations.
Methods
The researchers analyzed several 1-layer transformer architectures, varying the token-mixing mechanism:
- Linear Mixing (lin): A simple learnable matrix $A$ that mixes tokens.
- Dot-Product Mixing (dot): The standard attention mechanism where mixing depends on the input tokens.
- Softmax (+sftm): Applying softmax to the mixing matrix.
- BOS Token (bos): Adding a special "beginning-of-sequence" token.
They combined empirical training (using Adam on synthetic datasets) with theoretical constructions. The latter involves manually designing weight configurations that achieve 100% accuracy, proving that certain architectures can solve the task even if they are difficult to train.
Datasets
The study uses a synthetic histogram task dataset. Sequences of length $L=10$ are generated from an alphabet of size $T=32$. To ensure the models don't just memorize common counts, the authors used a specific sampling strategy (detailed in Appendix D) to create a close-to-uniform distribution over possible count values. They also tested LLMs like Mistral-7B and Llama-3.1-8B on similar counting tasks to establish a baseline of "real-world" failure.
Key Insights and Ideas
- Two Counting Strategies:
- Relation-Based Counting (RC): The model uses attention to compare tokens pairwise. This is highly efficient, requiring only a single hidden neuron ($p=1$) in the MLP to process the result.
- Inventory-Based Counting (IC): The model uses the MLP as an "inventory" or lookup table. It counts every possible token in the alphabet and then extracts the relevant count. This is memory-intensive, requiring at least as many hidden neurons as there are tokens in the alphabet ($p \ge T$).
- The Softmax Paradox: While softmax is standard in transformers, it can actually hinder counting. In dot-product attention, softmax normalization can "wash out" the counting signal. However, adding a BOS token solves this by providing a stable reference point (a "counter subspace") that the attention mechanism can use to track counts.
- Robustness in the "Small $d$" Regime: When the embedding dimension $d$ is smaller than the alphabet size $T$, tokens cannot be perfectly orthogonal. The authors show that softmax and dot-product attention help the model ignore the "noise" created by this non-orthogonality, allowing for perfect counting even with very compressed embeddings.
Key Results
- Architectural Efficiency: As shown in Figure 2, architectures capable of Relation-Based Counting (like
bos+sftm) are significantly more parameter-efficient than those forced to use Inventory-Based Counting (likelin). - BOS Token Necessity: For dot-product attention with softmax, a BOS token is essential for the $p=1$ (efficient) regime. Without it, the model fails to count unless the MLP is large enough to implement the inventory strategy ($p \ge T$).
- LLM Failure: Empirical tests on Mistral and Llama (Figures 6 & 7) show they struggle to count accurately beyond 5 occurrences, often defaulting to a prediction of "3" for positional queries. This suggests that even 8B parameter models haven't fully mastered the efficient counting circuits described in this paper.
Limitations and Future Work
The study is limited to 1-layer, non-autoregressive models. Real-world transformers are deep and use causal masking, which might lead to even more complex interdependencies between layers. While the authors show their findings generalize somewhat to 2-layer models, the "algorithmic alignment" of deeper stacks remains an open question.
Future work could investigate if similar "memory vs. algorithm" trade-offs exist for other basic tasks like sorting or logic gates, and whether these findings can be used to design more robust architectural primitives for large-scale LLMs.
Summary updated