Abstract
This paper introduces a structured memory which can be easily integrated into a neural network. The memory is very large by design and significantly increases the capacity of the architecture, by up to a billion parameters with a negligible computational overhead. Its design and access pattern is based on product keys, which enable fast and exact nearest neighbor search. The ability to increase the number of parameters while keeping the same computational budget lets the overall system strike a better trade-off between prediction accuracy and computation efficiency both at training and test time. This memory layer allows us to tackle very large scale language modeling tasks. In our experiments we consider a dataset with up to 30 billion words, and we plug our memory layer in a state-of-the-art transformer-based architecture. In particular, we found that a memory augmented model with only 12 layers outperforms a baseline transformer model with 24 layers, while being twice faster at inference time.
Introduction
In deep learning, increasing model capacity (the number of parameters) typically leads to improved performance on complex tasks. However, in standard dense architectures, capacity is inextricably linked to computational cost: more parameters generally mean more floating-point operations (FLOPs). This creates a bottleneck for training and real-time inference.
Researchers at Facebook AI Research (FAIR) addressed this by developing the Product Key Memory (PKM) layer. This "sparse" architecture allows for massive parameter counts—up to a billion—while maintaining a fixed computational budget. Unlike previous sparse models that relied on unstable approximate search, PKM utilizes a structured approach to enable exact, efficient nearest-neighbor retrieval.
Methodology: Product Key Memory
The PKM layer functions as a large-scale associative memory. The process involves three main stages: query generation, key selection, and value retrieval.
The Query Mechanism
When an input vector $x$ enters the layer, it is processed by a small "query network" (typically a linear layer followed by Batch Normalization) to produce a query vector $q$. This vector is then used to search the memory space.
Product Keys and Efficient Search
To manage millions of keys without $O(N)$ search complexity, the authors employ product quantization. Instead of a flat list, keys are represented as the Cartesian product of two smaller sub-key sets (codebooks).
For example, if each codebook contains 512 sub-keys, the model can represent $512^2 = 262,144$ total keys. During retrieval, the query $q$ is compared only against the sub-keys ($512 + 512 = 1,024$ comparisons), reducing complexity to $O(\sqrt{N})$. This allows for exact nearest neighbor search that is fully differentiable and integrated into backpropagation.
Value Lookup and Aggregation
Once the top-$k$ keys are identified, the model retrieves the corresponding trainable "value" vectors. The final output of the layer is a weighted sum of these values, where weights are determined by the similarity between the query and the selected keys. To increase information diversity, the authors utilize a multi-head attention mechanism similar to standard Transformers.
Experimental Setup and Datasets
Testing high-capacity models requires massive data to avoid immediate overfitting. While the One Billion Word corpus was initially used, the authors found it insufficient. They transitioned to CC-News, a massive 28-billion-word corpus (140 GB) extracted from Common Crawl. This scale provided the necessary signal to train models with a billion additional parameters effectively.
Key Insights
- The Batch Norm Trick: A critical finding was that adding Batch Normalization to the query network is essential. It ensures that queries are distributed evenly across the key space, preventing the model from collapsing into using only a tiny fraction of the available memory.
- Optimal Placement: The memory layer is most effective when placed in the middle-to-late stages of a Transformer (e.g., layers 4 or 5 of a 6-layer stack). Placing it too early or too late in the network architecture yields diminishing returns.
- Efficiency Gains: The bottleneck in these models remains the standard dense layers. By replacing some dense layers with PKM, the model becomes physically larger in parameters but computationally leaner.
Results and Performance
The PKM-augmented models demonstrated significant efficiency and performance gains:
- Depth vs. Performance: A 12-layer Transformer with a PKM layer outperformed a standard 24-layer Transformer baseline.
- Inference Speed: Because the 12-layer + PKM model has fewer sequential layers, it achieved twice the inference speed of the 24-layer baseline while maintaining superior accuracy.
- Systematic Scaling: Across various model dimensions (1024 and 1600), memory-augmented models consistently achieved lower perplexity than their memoryless counterparts for the same computational budget.
Conclusion
Product Key Memory layers offer a path toward massive model capacity without the proportional increase in computational cost. By leveraging structured product keys for exact search and utilizing Batch Normalization for memory coverage, these layers enable the training of billion-parameter models that are both faster and more accurate than deeper, traditional architectures. Future work may extend this mechanism beyond language modeling into domains like computer vision.
Summary updated