Memorization in Attention-only Transformers

Léo Dana, Muni Sreenivas Pydi, Yann Chevaleyre

Abstract

Recent research has explored the memorization capacity of multi-head attention, but these findings are constrained by unrealistic limitations on the context size. We present a novel proof for language-based Transformers that extends the current hypothesis to any context size. Our approach improves upon the state-of-the-art by achieving more effective exact memorization with an attention layer, while also introducing the concept of approximate memorization of distributions. Through experimental validation, we demonstrate that our proposed bounds more accurately reflect the true memorization capacity of language models, and provide a precise comparison with prior work.

Introduction

In the architecture of Large Language Models (LLMs), Multi-Layer Perceptrons (MLPs) are traditionally viewed as the "knowledge base," while Attention layers serve as "routers" for information flow. However, the theoretical limits of what an attention layer can independently store have remained poorly understood. Previous research suggested that an attention layer's memorization capacity was strictly limited by the embedding dimension ($d$), requiring the context window ($S$) to be smaller than $d$.

This paper challenges that constraint, providing a new theoretical framework to explain how Transformers memorize information within their attention layers even when the context window is massive compared to the embedding dimension—a scenario common in modern models like GPT-4 or Gemini.

Methodology

The study distinguishes between two primary types of memorization:

  1. The Association Task: Exact memorization required to predict a specific next-token given a sequence.
  2. The Distribution Task: Approximate memorization of the probability distribution of next-tokens, reflecting the probabilistic nature of real language.

To analyze these, the authors employ a novel proof technique using Taylor expansions of the softmax function. This allows the attention mechanism to represent complex mappings even when input vectors are not linearly independent. Empirically, the researchers trained "Attention-only Transformers" (AoT) on synthetic bigram tasks with dictionary sizes ($N$) of 50 and 200. These datasets used uniform priors to ensure that model accuracy reflected true associative memory rather than frequency-based guessing.

Key Results and Insights

Breaking Theoretical Barriers

The most significant finding is that context size does not need to be smaller than the embedding dimension for effective memorization. By leveraging the non-linearity of the softmax function, attention heads can effectively act as high-capacity look-up tables.

New Capacity Bounds

The authors established a new theoretical bound for memorization capacity ($T_0$), proving that a single-layer AoT can memorize at least $H d_h + d$ associations, where $H$ is the number of heads and $d_h$ is the head dimension. This represents a strict improvement over previous state-of-the-art bounds.

Comparison of memorization capacity bounds and parameter counts across different theoretical frameworks.

Scaling Laws

Empirical testing verified that memorization capacity scales linearly with the number of heads ($H$) and quadratically with the head dimension ($d_h$). Furthermore, the experiments suggest that an optimal configuration for maximizing memorization per parameter occurs when the embedding dimension equals the head dimension ($d = d_h$).

Empirical validation showing accuracy scaling quadratically with the head dimension (dh).

Optimization Advantage

Interestingly, the study found that Attention-only models were easier to optimize for memorization tasks than MLPs. While MLPs possess high theoretical capacity, they proved "harder to reach" via standard training methods like Adam compared to the more direct mapping provided by the attention mechanism.

Discussion and Limitations

While this work provides a breakthrough in understanding attention-based memory, several areas remain for future exploration:

  • Scale: Empirical validation was limited to small dictionary sizes and short sequences (bigrams). It remains to be seen if these scaling laws hold for trillion-parameter models.
  • Depth: The core results focus on a single attention layer; the dynamics of multi-layer hierarchies may introduce new complexities.
  • The Optimization Gap: There remains a significant gap between what a model can theoretically represent and what it can practically learn via Stochastic Gradient Descent (SGD).

Summary updated

BibTeX
@inproceedings{dana2025memorization,
  author = {Léo Dana and Muni Sreenivas Pydi and Yann Chevaleyre},
  title = {{Memorization in Attention-only Transformers}},
  booktitle = {International Conference on Artificial Intelligence and Statistics},
  year = {2025},
  url = {https://openreview.net/forum?id=hyALFH7E9J},
}
Figure