FastMem: Fast Memorization of Prompt Improves Context Awareness of Large Language Models

Junyi Zhu, Shuochen Liu, Yu Yu, Bo Tang, Yibo Yan, Zhiyu Li, Feiyu Xiong, Tong Xu, Matthew B. Blaschko

Abstract

Large language models (LLMs) excel in generating coherent text, but they often struggle with context awareness, leading to inaccuracies in tasks requiring faithful adherence to provided information. We introduce FastMem, a novel method designed to enhance instruction fine-tuned LLMs' context awareness through fast memorization of the prompt. FastMem maximizes the likelihood of the prompt before inference by fine-tuning only the last Feed-Forward Network (FFN) module. This targeted approach ensures efficient optimization without overfitting, significantly improving the model's ability to comprehend and accurately follow the context. Our experiments demonstrate substantial gains in reading comprehension, text summarization and adherence to output structures. For instance, FastMem improves the accuracy of Llama 3-8B-Inst on the NQ-SWAP dataset from 59.1% to 71.6%, and reduces the output structure failure rate of Qwen 1.5-4B-Chat from 34.9% to 25.5%.

Introduction

Large Language Models (LLMs) possess vast internal "parametric knowledge" acquired during pre-training. However, this knowledge can lead to a "stubbornness" problem known as knowledge conflict. When a user provides a prompt containing information that contradicts the model's internal data—such as a news article about an event occurring after the model's training cutoff—the model often prioritizes its outdated internal knowledge over the provided context, leading to hallucinations.

Comparison between conventional methods and FastMem in handling knowledge conflicts.

This lack of context awareness also manifests as a failure to follow specific formatting instructions or a tendency to "drift" during long-form tasks. We propose FastMem, a technique that gives LLMs a form of "short-term memory" by forcing them to memorize the prompt immediately before generating a response.

Methodology

FastMem implements a "just-in-time" optimization strategy where the model treats the input prompt as a mini-training set. The process is divided into two phases: Memorization Training and Context-Aware Inference.

The two-phase architecture of FastMem, including memorization training and context-aware inference.

Last-FFN Adaptation

To maintain inference speed and prevent model degradation, FastMem only updates the parameters of the last Feed-Forward Network (FFN) module. FFNs are hypothesized to act as the model's key-value memory; by updating only the final layer, FastMem achieves targeted knowledge adjustment without the overhead of full fine-tuning.

Objective Function

The optimization minimizes a loss function combining Next Token Prediction (NTP) on the prompt with a KL Divergence regularization term. The KL term ensures the model's general reasoning and linguistic capabilities remain intact by preventing excessive deviation from its original state.

Integration with Decoding Strategies

FastMem can operate as a standalone enhancement or be integrated with advanced decoding strategies like Contrastive Decoding (CD) or DoLa (Decoding by Contrasting Layers) to further sharpen the model's focus on the provided context.

Experimental Evaluation

The effectiveness of FastMem was evaluated across several benchmarks designed to test context adherence and knowledge conflict resolution:

  • NQ-SWAP: A Natural Questions variant where entity names are swapped to create direct conflicts with internal knowledge.
  • MemoTrap: A dataset designed to "trap" models into following common-sense priors instead of counter-intuitive prompt instructions.
  • Standard Benchmarks: Tasks including CNN-DM, XSUM, and HotpotQA were used to ensure general performance stability.

Key Results

  • Conflict Resolution: On NQ-SWAP, FastMem boosted Llama 3-8B-Instruct's accuracy from 59.1% to 71.6%.
  • Instruction Adherence: Accuracy on MemoTrap jumped from 77.9% to 97.2%.
  • Structural Integrity: For Qwen 1.5-4B, the failure rate in producing requested output structures dropped from 34.9% to 25.5%.
  • Efficiency: The memorization phase is highly efficient, taking approximately 1.6 seconds on an A100 GPU for a Llama 3-8B model, with zero additional peak memory overhead.

Discussion and Insights

A key finding is the correlation between perplexity and performance; models are more likely to ignore prompts they find "surprising." FastMem explicitly lowers this perplexity through memorization. Furthermore, the choice of the last FFN layer proved to be a "Goldilocks" zone—tuning earlier layers often led to overfitting that degraded reasoning.

While FastMem significantly improves faithfulness, it introduces a "Garbage In, Garbage Out" risk: the model will more faithfully reproduce errors if the prompt itself is noisy or incorrect. Future work will explore scaling to longer contexts and the potential use of Low-Rank Adaptation (LoRA) for even greater stability.

Summary updated

Abstract

Large language models (LLMs) excel in generating coherent text, but they often struggle with context awareness, leading to inaccuracies in tasks requiring faithful adherence to provided information. We introduce FastMem, a novel method designed to enhance instruction fine-tuned LLMs' context awareness through fast memorization of the prompt. FastMem maximizes the likelihood of the prompt before inference by fine-tuning only the last Feed-Forward Network (FFN) module. This targeted approach ensures efficient optimization without overfitting, significantly improving the model's ability to comprehend and accurately follow the context. Our experiments demonstrate substantial gains in reading comprehension, text summarization and adherence to output structures. For instance, FastMem improves the accuracy of Llama 3-8B-Inst on the NQ-SWAP dataset from 59.1% to 71.6%, and reduces the output structure failure rate of Qwen 1.5-4B-Chat from 34.9% to 25.5%. Extensive experimental results highlight FastMem's potential to offer a robust solution to enhance the reliability and accuracy of LLMs in various applications. Our code is available at: https://github.com/IAAR-Shanghai/FastMem
BibTeX
@inproceedings{zhu2024fastmem,
  author = {Junyi Zhu and Shuochen Liu and Yu Yu and Bo Tang and Yibo Yan and Zhiyu Li and Feiyu Xiong and Tong Xu and Matthew B. Blaschko},
  title = {{FastMem: Fast Memorization of Prompt Improves Context Awareness of Large Language Models}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2024},
  url = {https://openreview.net/forum?id=Ud3CKCZRuN},
}
Figure