This software project accompanies the research paper Pretraining with Hierarchical Memories: Separating Long-tail and Common Knowledge
The impressive performance gains of modern language models currently rely on scaling parameters: larger models store more world knowledge and reason better. Yet compressing all world knowledge into parameters is unnecessary, as only a fraction is used per prompt, and impractical for edge devices with limited inference-time memory and compute. We address this shortcoming by a memory-augmented architecture and a pretraining strategy aligned with existing hardware paradigms. We introduce small language models that access large hierarchical parametric memory banks encoding world knowledge. During pretraining and inference, we fetch a small, context-dependent memory block and add it to the model. Our pretraining learns to store long-tail world knowledge in the memory parameters, while the small language model acts as an anchor capturing common knowledge and general reasoning abilities. Through trillion-token-scale experiments, we show significant gains: a 160M-parameters model augmented with an 18M-parameters memory fetched from a 4.6B memory bank obtains comparable performance to a regular model with more than 2x the parameters. Through extensive experiments, we study the optimal type and size of parametric memories in transformers, scaling them to over 21B parameters. We find that our proposed hierarchical feed-forward memories work robustly across transformer architectures, whether added during pretraining or post-hoc.
- Install requirements:
pip install -r requirements.txt- Apply the llmfoundry patch (only necessary for the evaluation script):
patch -p1 -d /path/to/site-packages < memory_models_llmfoundry_v20.patchhuggingface_memory_wrapper.py shows how to attach a memory bank to an arbitrary HuggingFace model. The file includes a stand-alone example in the __main__ block that demonstrates:
- Creating a memory-augmented model
- Testing memory functionality
- Saving and loading checkpoints with embedded configuration
Run the example:
python huggingface_memory_wrapper.pyThe memory system has two main components:
-
Memory Bank: Stores additional parameters in a hierarchical structure. For example:
- Level 1: Large parameter blocks (128 tokens), few clusters (16)
- Level 2: Medium blocks (32 tokens), medium clusters (32)
- Level 3: Small blocks (8 tokens), many clusters (128)
Note: These are toy example sizes. The exact sizes depend on the anchor model size and the desired trade-off between inference speed and memory bank size. See the paper for scaling guidelines.
-
Memory Retriever: Decides which memory to use from each level based on the input context. The selected memories are added to the feed-forward network of each layer. This retriever logic can be implemented via text embedders and clustering of the pretraining data, see below.
Memory retrieval maps text to cluster IDs using centroids generated via CRISP clustering. This mapping is essential for both training and evaluation.
Generating centroids (one-time, from training corpus):
- Embed corpus with sentence transformer (e.g.,
all-MiniLM-L6-v2) - Run hierarchical KMeans clustering
- Save centroids (e.g.,
centroids.npy) - The same centroids file assigns cluster IDs to examples in both training and evaluation.
An example script for training such a clustering is provided as train_kmeans.py.
python train_kmeans.py \
--config configs/example_clustering_config.yamlIt will save the clustering file in ./clustering/model/centroids.npy. Note that the number of clusters in configs/example_clustering_config.yaml are toy-sized, meant to run through in roughly 1 hour. You should choose the number of levels and clusters, and the correct number of train documents per cluster from the paper to reproduce the findings.
Train your model and memory bank via train_memory.py:
python train_memory.py \
--model-name Qwen/Qwen2.5-0.5B-Instruct \
--train-yaml configs/example_train_config.yaml \
--centroids-file /path/to/centroids.npyThis will output a joint_model_checkpoint.pt. Note that this train script only uses exemplary hyperparameters and optimizers, you should tune them to your setup when using it. If you leave out the centroids-file, training uses generic memory (last cluster index for each level).
Evaluate your model using eval_memory.py:
python eval_memory.py \
--checkpoint ./joint_model_checkpoint.pt \
--eval-yaml configs/example_eval_config.yaml \
--centroids-file /path/to/centroids.npyThe eval config specifies tasks, datasets, and metrics. See example_eval_config.yaml for configuration examples. Results are saved to ./eval_output/. For multi-GPU evaluation, use torchrun. If you leave out the centroids-file, evaluation uses generic memory (last cluster index for each level).
If you found this work useful, we'd be happy if you cited us:
@misc{pouransari2026pretraininghierarchicalmemoriesseparating,
title={Pretraining with hierarchical memories: separating long-tail and common knowledge},
author={Hadi Pouransari and David Grangier and C Thomas and Michael Kirchhof and Oncel Tuzel},
year={2026},
booktitle = {Proceedings of the International Conference on Learning Representations (ICLR)},
url={https://arxiv.org/abs/2510.02375}
}