Memory Decoder: A Pretrained, Plug-and-Play Memory for Large Language Models

Jiaqi Cao, Jiarui Wang, Rubin Wei, Qipeng Guo, Kai Chen, Bowen Zhou, Zhouhan Lin

Abstract

Adapting Large Language Models (LLMs) to specialized domains typically involves a trade-off between the high computational costs of Domain Adaptive Pretraining (DAPT) and the high inference latency of Retrieval-Augmented Generation (RAG). This paper introduces Memory Decoder (MemDec), a compact, pretrained transformer that acts as a plug-and-play memory component. By training a small model (0.1B–0.5B parameters) to imitate the output distribution of a non-parametric retriever, MemDec can be integrated with any LLM sharing the same tokenizer. Experimental results demonstrate that MemDec significantly improves domain-specific performance across various model scales with minimal latency overhead, while avoiding the catastrophic forgetting common in full-model retraining.

Introduction

General-purpose LLMs often struggle with specialized knowledge in fields such as law, medicine, or finance. Current adaptation strategies are limited: DAPT is resource-intensive and must be repeated for every model size, while RAG introduces significant latency due to expensive nearest-neighbor searches in external datastores. The authors propose Memory Decoder (MemDec) to fill this gap, providing a solution that is both model-agnostic within a tokenizer family and computationally efficient during deployment.

Comparison of Domain Adaptive Pretraining (DAPT), Retrieval-Augmented Generation (RAG), and the proposed Memory Decoder (MemDec) approach.

Methods

The core innovation of MemDec is treating domain adaptation as an imitation task. Instead of querying a massive database at runtime, a small "Memory Decoder" is trained to mimic the behavior of a traditional k-nearest neighbor (kNN) retriever.

Pretraining and Distribution Alignment

The training process involves building a traditional kNN datastore for a target domain and using it to generate training signals. The MemDec is optimized using a hybrid loss function:

  • Forward KL Divergence: This forces the MemDec's output to align with the sparse, "peaky" probability distributions of the kNN retriever. This "mode-seeking" nature is critical for capturing specific knowledge tokens.
  • Cross-Entropy: This ensures the model maintains linguistic coherence.

Plug-and-Play Inference

During inference, the MemDec runs in parallel with the base LLM. Their output distributions are combined via a simple weighted interpolation. Because the MemDec is small and avoids database searches, it introduces significantly less overhead than traditional retrieval methods. Furthermore, a single MemDec trained for a domain can be attached to any model size (e.g., from 0.5B to 72B) within the same model family.

Key Results

The authors evaluated MemDec across specialized domains including biomedicine (MIMIC-III), finance, and law (Asylex).

Performance and Scaling

MemDec reduced perplexity by an average of 6.17 points across specialized domains. Notably, as shown in Figure 5, a 0.5B parameter MemDec can improve the performance of a 0.5B base model to exceed that of a standalone 72B model in specific domains, demonstrating exceptional knowledge compression.

Perplexity vs. Parameter Count for base Qwen2.5 models compared to those augmented with LoRA and the 0.5B Memory Decoder.

Efficiency and Generalization

  • Latency: MemDec introduced only 1.28x latency overhead, compared to 2.17x for traditional kNN-LM methods.
  • General Intelligence: Unlike DAPT, which often degrades performance on general reasoning tasks, MemDec actually improved average scores across nine standard NLP benchmarks, proving it preserves the base model's core intelligence while adding specialized expertise.

Conclusion

Memory Decoder represents a significant step toward efficient LLM customization. By decoupling domain-specific knowledge into a small, portable module, researchers have created a system that is both high-performing and highly flexible. Future work will explore cross-tokenizer transfer and the potential for stacking multiple domain-specific decoders for multi-expert systems.

Summary updated

BibTeX
@inproceedings{cao2025memory,
  author = {Jiaqi Cao and Jiarui Wang and Rubin Wei and Qipeng Guo and Kai Chen and Bowen Zhou and Zhouhan Lin},
  title = {{Memory Decoder: A Pretrained, Plug-and-Play Memory for Large Language Models}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2025},
  url = {https://openreview.net/forum?id=ARJpQtLXfe},
}
Figure