In the realm of artificial intelligence, advancing language models with memory capabilities has opened new horizons. This blog will guide you through the process of implementing the LongMem model as detailed in the paper Augmenting Language Models with Long-Term Memory. By the end of this guide, you will be able to set up the necessary environment, install components, and evaluate your model with ease.
Environment Setup
Setting up the environment correctly is crucial for the smooth operation of the LongMem model. Here’s how you can do it step-by-step:
- Torch Installation: Follow the official installation guide. We recommend using
torch=1.8.0and ensure you select the version that is compatible with your CUDA driver. - Install Faiss-GPU: Depending on your GPU, install Faiss-GPU using the following commands:
- Nvidia V100:
pip install faiss-gpu - Nvidia A100/A6000:
conda install faiss-gpu cudatoolkit=11.0 -c pytorch
Note: The A100 is not officially supported, so encountering errors can happen. For troubleshooting, refer to this GitHub issue.
- Nvidia V100:
- Install Fairseq: Execute
pip install --editable .fairseqto install Fairseq and its dependencies. Python 3.8 is recommended for stability. - Additional Packages: Finally, run
pip install -r requirements.txtto install all other necessary packages.
Understanding the Project Structure
To visualize the LongMem implementation, let’s use an analogy: Consider LongMem as a library filled with various books (modules) organized into specific sections (scripts). Each part plays a vital role in how the library functions. Here’s a breakdown:
- Pre-trained LLM Class: Located in fairseq/fairseq/models/new_gpt.py, this module forms the backbone of the model.
- Transformer Decoder with SideNetwork: Found in fairseq/fairseq/models/sidenet/transformer_decoder_sidenet.py, it enriches the standard decoder with additional capabilities.
- Memory Bank and Retrieval: This module, located at fairseq/fairseq/modules/dynamic_memory_with_chunk.py, acts like a librarian organizing and retrieving information when needed.
- Joint Attention for Memory Fusion: Available at fairseq/fairseq/modules/joint_multihead_attention_sum.py, it helps improve the attention mechanism to integrate memory efficiently.
Training and Evaluation
Here’s how to carry out memory-augmented adaptation training:
- Data Collection and Preprocessing: Download the Pile dataset from the official release. The data is organized into different JSON lines. For sampling, you can check preprocess/filter_shard_tnlg.py.
- Run Training: Execute the training script:
bash train_scripts/train_longmem.sh.
For evaluation purposes, download the checkpoints for the pre-trained GPT2-medium model and LongMem model and store them in a ‘checkpoints’ directory. Use the following commands to evaluate:
python eval_scripts/eval_longmem_icl.py --path path_to_gpt2_pretrained_modelpython eval_scripts/eval_longmem_icl.py --path path_to_longmem_model --pretrained-model-path path_to_gpt2_pretrained_model
Troubleshooting
If you encounter issues during installation or operation, here are some troubleshooting tips:
- Ensure all dependencies are correctly installed in the right versions. Double-check the installation commands.
- If Faiss installation fails, refer back to the GitHub issue for possible solutions.
- For Python compatibility issues, try switching back to Python 3.8 for improved stability.
For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.
Conclusion
At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

