Abstract
How do different architectural design choices influence the space of solutions that a transformer can implement and learn? We investigate these questions by characterizing the solutions simple transformer blocks can implement when challenged to solve the histogram task—counting the occurrences of each item in an input sequence. Our analysis reveals a strong inter-dependence between the model's predictive performance and the vocabulary and embedding sizes, the token-mixing mechanism, and the capacity of the feed-forward block. We characterize two different counting strategies: relation-based and inventory-based counting. The emergence of either strategy is heavily influenced by subtle synergies among hyperparameters and components, such as the inclusion of softmax in the attention mechanism.
The Problem: Division of Labor in Transformers
Despite the success of Large Language Models (LLMs), the "division of labor" between the attention mechanism (token-mixing) and the feed-forward network (MLP) remains poorly understood. This study uses the histogram task—counting occurrences of tokens in a sequence—as a lens to study this interaction. Even simple counting is often unreliable in massive models like Llama-3 when performed "in-weights." By analyzing a 1-layer Transformer, we can uncover the mechanistic blueprints that emerge during training.
Methodology
The research combines theoretical construction with empirical training:
- Architectural Variations: Testing 1-layer Transformers with Linear mixing vs. Dot-product attention, with and without Softmax, and with or without a "beginning-of-sequence" (BOS) token.
- Theoretical Proofs: Constructing weight configurations to prove that specific architectures can solve the task given sufficient embedding dimension ($d$) and MLP hidden size ($p$).
- Mechanistic Interpretability: Using SVD and attention map visualization on trained models to verify if they discover the predicted theoretical algorithms.
Two Strategies for Counting
The authors identify two distinct algorithmic paths:
- Relation-based Counting (RC): An efficient strategy where the model uses dot-product attention to compare tokens. If a match is found, it increments the count. This is highly parameter-efficient, requiring only a single hidden neuron ($p=1$) in the MLP.
- Inventory-based Counting (IC): A memory-heavy strategy where the MLP acts as a lookup table for every possible token in the alphabet. This requires a much wider MLP ($p \ge T$, where $T$ is the vocabulary size).
Results: The Role of Architecture and Phase Transitions
The study reveals clear "learning regimes" or phase transitions based on model capacity and architectural choices.
Key empirical findings include:
- Inductive Biases: Dot-product attention naturally favors Relation-based counting due to its comparative nature. In contrast, models using linear mixing (constant weights) are forced into the Inventory-based strategy, requiring $p \ge T$ to achieve high accuracy.
- The Softmax Filter: When the embedding dimension is small ($d < T$), tokens cannot be perfectly orthogonal. The Softmax operator acts as a non-linear filter, suppressing noise from non-matching tokens and enabling accurate counting in compressed spaces.
- BOS Token as an Anchor: A BOS token provides a dedicated subspace for aggregating count information, allowing the attention mechanism to use it as a proxy that the MLP then reads out. With both dot-product attention and a BOS token, models can achieve 100% accuracy with just $p=1$, provided $d$ is large enough.
Conclusion and Limitations
While 1-layer models provide clear insights, the complex hierarchies of deep LLMs may introduce more entangled strategies. Furthermore, counting is a permutation-equivariant task; real-world language tasks are order-dependent, and the interaction between these counting mechanisms and positional encodings remains a subject for future work. The findings highlight that even minor architectural tweaks, like adding softmax, can fundamentally shift the solutions a transformer learns.
Summary updated