Understanding Factual Recall in Transformers via Associative Memories

Eshaan Nichani, Jason D. Lee, Alberto Bietti · DOI

Large Language Models (LLMs) often function as massive, searchable knowledge bases. When a model is asked for a specific fact, such as George Washington’s birth year, it retrieves "1732" with high consistency. However, the underlying mechanism by which a sequence of matrix multiplications stores and retrieves such information has remained largely opaque. The paper “Understanding Factual Recall in Transformers via Associative Memories” provides a rigorous theoretical and empirical investigation into the machinery of memory within the Transformer architecture.

The Mechanics of Memory

While empirical evidence suggests that a model's capacity to store facts scales linearly with its parameter count, a formal explanation of this mechanism was previously missing. The process of factual recall involves two primary challenges:

  1. Filtering: The model must distinguish relevant subject (e.g., "George Washington") and relation (e.g., "birth year") tokens from surrounding "noise" tokens.
  2. Storage: Once identified, the subject-relation pair must be mapped to a unique, correct answer.

This research bridges the gap between high-level observations of LLM behavior and the low-level linear algebra of transformer weights by framing transformer components as associative memories.

Theoretical Framework: Transformers as Associative Memories

The researchers model transformer layers as Associative Memories—mathematical structures that store pairs of input-output embeddings. By analyzing the capacity of these structures, the authors proved that both linear and Multi-Layer Perceptron (MLP) associative memories possess a storage capacity that scales linearly with their parameter count, subject to logarithmic factors.

A key contribution is the mathematical construction of a one-layer transformer (comprising self-attention and an MLP) capable of solving factual recall tasks with 100% accuracy. This construction demonstrates that the Query and Key matrices in the attention layer act as a filter, learning to "attend" specifically to subject and relation tokens while suppressing noise.

Experimental Setup: Synthetic Factual Recall

To isolate the mechanics of memory from the complexities of natural language, the study utilizes a synthetic factual recall task.

The synthetic factual recall task requires the model to identify a subject (s) and relation (r) among noise tokens (z) to predict the correct answer (a*) after an EOS token.

In this setup:

  • The Dictionary: Facts are defined as mappings: (Subject, Relation) $\to$ Answer.
  • The Prompts: Sequences contain a subject token and a relation token hidden among variable noise tokens, ending with an "EOS" (End of Sequence) token.
  • The Goal: The model must predict the correct answer token immediately following the EOS.

Key Insights: Dual-Store and Learning Dynamics

The research reveals that transformers are highly flexible "dual-store" systems. They can dynamically trade off storage between Attention Value matrices and MLP weights. If one component is constrained, the other can compensate to maintain memorization capacity.

Furthermore, the authors identified a distinct "hallucination" stage during training. Models do not learn facts instantaneously; instead, they first learn the general conditional distribution of a relation. For instance, if the relation is "Capital City," the model initially learns to output any capital city before it learns which specific city belongs to which country.

Results: Scaling Laws

The paper confirms that the number of facts $N$ a model can store is $\Theta(\text{Parameters})$. For a model with width $m$ and dimension $d$, the capacity is approximately $md$.

Empirical results confirm that storage capacity N scales linearly with the number of parameters (d^2 for linear memory and md for MLP memory), matching theoretical predictions.

Information-theoretic lower bounds provided in the paper suggest that these associative memory constructions are optimal up to logarithmic factors. Empirical validation shows a clear three-stage learning process: initialization, a "hallucination" plateau where relation-only distributions are mastered, and final convergence to near-zero loss.

Limitations and Future Directions

While providing a strong theoretical foundation, the study focuses on shallow, one-layer architectures and uses random, near-orthogonal embeddings. Real-world LLMs are significantly deeper, and natural language embeddings are often highly correlated, which may influence actual capacity limits. Additionally, the optimization analysis utilizes a simplified linear attention model. Future research is needed to determine if these specific associative memory structures can be identified within massive, pre-trained models like GPT-4 or Llama-3 through mechanistic interpretability techniques.

Summary updated

BibTeX
@inproceedings{nichani2025understanding,
  author = {Eshaan Nichani and Jason D. Lee and Alberto Bietti},
  title = {{Understanding Factual Recall in Transformers via Associative Memories}},
  booktitle = {International Conference on Learning Representations},
  year = {2025},
  url = {https://openreview.net/forum?id=hwSmPOAmhk},
}
Figure