Attention as a Hypernetwork

Simon Schug, Seijin Kobayashi, Yassir Akram, Joao Sacramento, Razvan Pascanu

Transformers exhibit a remarkable ability to solve novel combinations of familiar problems—a hallmark of human intelligence known as compositional generalization. Historically, neural networks have struggled with this, yet modern large language models demonstrate a surprising capacity for it, especially during in-context learning.

But what specific mechanisms inside the Transformer architecture allow it to flexibly recombine learned skills? In "Attention as a Hypernetwork," researchers hypothesize that the secret lies in the multi-head attention layer, specifically in how multiple heads interact to form reusable computational modules. By reformulating multi-head attention, they reveal that it is mathematically equivalent to a "hypernetwork," where attention scores across heads act as a latent code configuring specific operations.

Multi-Head Attention as a Hypernetwork

The authors start with a mathematical proof: multi-head attention can be rewritten as a linear hypernetwork. A hypernetwork is a neural network that generates the weights for another network (the "value network").

Multi-head attention reformulated as a linear hypernetwork

In this view, the attention scores across the different heads form a low-dimensional "latent code." Instead of just deciding where to look (acting as a pointer), this latent code dictates exactly what operation the value network should apply to a given token.

To test if this hypernetwork mechanism drives compositional generalization, the authors introduce a modified attention layer called HYLA (Hypernetwork Linear Attention).

  • The Tweak: In standard attention, the implicit value network is purely linear. HYLA adds a non-linearity (a ReLU activation) to this value network and normalizes attention scores across the heads instead of the keys.
  • The Goal: This makes the hypernetwork mechanism more expressive, allowing it to compose more complex operations without adding any new learnable parameters.

Testing Compositional Reasoning

To rigorously test compositional generalization, the authors utilized tasks with strictly controlled training and testing distributions:

  • Fuzzy Logic Functions: A synthetic in-context learning task where models deduce and apply combinations of logical operators (AND, OR, NOT).
  • SRAVEN (Symbolic Raven's): A newly introduced benchmark based on the Raven's Progressive Matrices intelligence test. Unlike image-based versions, SRAVEN is purely symbolic, isolating the core difficulty of abstract reasoning—finding underlying rules—without the overhead of visual processing.
  • C4 Dataset: A standard web-text dataset (130 billion tokens) used to test if HYLA scales to real-world language modeling.

Key Insights and Results

The hypernetwork perspective yields several fascinating insights into how Transformers process information:

  • A Vocabulary of Rules: When visualizing the attention scores (latent codes) while the network solved SRAVEN, the authors found that the codes formed distinct clusters. Strikingly, these clusters corresponded to the underlying rules (e.g., addition, progression) the model was applying, not just the output values. The network effectively learns a reusable vocabulary of operations.
  • Multiple Heads are Crucial: Reducing a Transformer to a single attention head breaks the hypernetwork mechanism (it can only rescale weights, not compose them). Consequently, the model's ability to generalize to unseen problem compositions plummets.
  • Non-Linearity Boosts Reasoning: Adding a ReLU to the implicit value network (HYLA) acts as a strong inductive bias for compositionality. On the Fuzzy Logic task, HYLA vastly outperformed standard baselines on out-of-distribution (OOD) tasks, achieving an $R^2$ score of ~81%, compared to standard softmax attention (~63%).
  • Stronger Abstract Reasoning: On a 4-layer model trained on 20M SRAVEN instances, HYLA achieved ~69% OOD accuracy, beating both softmax (~56%) and linear attention (~56%). Furthermore, given enough data and model size (e.g., 16 layers, 40M instances), all Transformer variants eventually learned to solve over 80% of unseen SRAVEN rule combinations.
  • Language Modeling Scalability: On the massive C4 dataset, HYLA significantly outperformed standard linear attention and nearly closed the performance gap with standard softmax attention.

Limitations and Future Directions

To perfectly control "unseen" compositions, the authors deliberately focused on models trained from scratch on synthetic and symbolic tasks. While this rigorously proves the mechanism, it leaves open questions about how these structures manifest in the wild.

The most exciting next step is to examine massive, pretrained LLMs to see if these beautifully structured, rule-based latent codes naturally emerge during diverse, real-world training. Additionally, exploring different pooling operators—like those used in Graph Neural Networks—could further upgrade the attention mechanism, paving the way for even stronger compositional reasoning in future models.

Summary updated

BibTeX
@inproceedings{schug2025attention,
  author = {Simon Schug and Seijin Kobayashi and Yassir Akram and Joao Sacramento and Razvan Pascanu},
  title = {{Attention as a Hypernetwork}},
  booktitle = {International Conference on Learning Representations},
  year = {2025},
  url = {https://openreview.net/forum?id=V4K9h1qNxE},
}
Figure