Engram-PEFT: Efficient Parameter-Efficient Fine-Tuning with Engram
Project description
Engram-PEFT
[English] | 中文
[!IMPORTANT] This is an unofficial implementation of the DeepSeek Engram paper (arXiv:2601.07372). DeepSeek-AI official demo is here.
Engram-PEFT is a high-performance, 100% paper-aligned implementation of the DeepSeek Engram architecture. It provides a Parameter-Efficient Fine-Tuning (PEFT) interface to inject conditional memory into any Transformer-based LLM.
Engram decouples static knowledge storage from dynamic reasoning using a sparse retrieval mechanism, allowing models to scale their factual memory without increasing inference FLOPs or interfering with core logic.
🚀 Quick Start
Installation
pip install engram-peft
To run examples or contribute to development, install the project with development dependencies:
# Using uv (recommended)
uv sync --all-groups
# Using pip
pip install -e ".[dev]"
5-Minute Example
from transformers import AutoModelForCausalLM, AutoTokenizer
from engram_peft import EngramConfig, get_engram_model
# 1. Load base model
base_model = AutoModelForCausalLM.from_pretrained("TinyLlama/TinyLlama-1.1B-intermediate-step-1431k-3T")
tokenizer = AutoTokenizer.from_pretrained("TinyLlama/TinyLlama-1.1B-intermediate-step-1431k-3T")
# 2. Inject Engram layers (aligned with arXiv:2601.07372)
config = EngramConfig(target_layers=[2, 11, 20])
model = get_engram_model(base_model, config, tokenizer)
# 3. Quick check on trainable parameters
model.print_trainable_parameters()
# trainable params: 86,938,368 || all params: 1,186,986,752 || trainable%: 7.3243
📊 Performance Comparison
| Method | Params Added | Speed (s/step) | Training Loss | Eval Loss | VRAM (nvtop) |
|---|---|---|---|---|---|
| LoRA (r=16) | ~2.25 M | 0.2738 s | 1.231 | 0.989 | 9.35 GiB |
| Engram-PEFT | 545.4 M | 0.2961 s | 1.263 | 1.017 | 10.82 GiB |
[!TIP] Performance Insight: In our latest 3000-step benchmark on RTX 4090D, LoRA achieved slightly better loss and speed. However, Engram-PEFT provides 240x more parameter capacity (545M) for knowledge storage with only a ~8% latency penalty, making it ideal for tasks requiring massive factual recall.
Loss Curve Comparison
* Engram employs sparse lookup; only a tiny fraction of parameters (approx. 1%) are active and receive gradient updates per step. For a detailed breakdown of performance, computation, and memory, see our Performance Analysis.
🛠 Features
- 100% Paper Alignment: Implements Appendix A Table 5 parameters and the official DeepSeek gating/hashing logic.
- CPU Prefetching & Precomputation:
EngramDataCollatorpre-calculates multi-head hash indices on the CPU. By usingnum_workers > 0, these indices are prefetched in parallel with training, ensuring zero hashing overhead on the GPU. - Tokenizer Compression: Built-in NFKC and lowercase normalization for 23% vocabulary reduction.
- Cross-Model Weight Migration: A unique feature (see
weight_transfer.py) that allows migrating Engram weights between different models (e.g., Llama to Qwen) using character-level alignment on a corpus—effectively "recycling" learned knowledge. - Zero-Invasive: Injects via forward hooks; no modification to your base model architecture required.
- Peft-like API: Familiar methods like
print_trainable_parameters()andsave_pretrained(). - Named Adapters: Industry-standard named adapter management (add/set/unload) for modular knowledge packs.
- Automated Training: Native
EngramTrainerwith built-in sparse Adam support and automatic sync of optimizer hyperparameters.
📖 Documentation
For full details, see our documentation:
- Tutorials: Quickstart and domain knowledge injection.
- API Reference: Detailed class and function documentation.
- Paper Alignment: How we match the DeepSeek research.
🎯 Citation
If you use this implementation in your research, please cite the original DeepSeek paper:
@article{deepseek2026engram,
title={Conditional Memory via Scalable Lookup: A New Axis of Sparsity for Large Language Models},
author={DeepSeek-AI},
journal={arXiv preprint arXiv:2601.07372},
year={2026}
}
License
Apache License 2.0. See LICENSE for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file engram_peft-1.0.4.tar.gz.
File metadata
- Download URL: engram_peft-1.0.4.tar.gz
- Upload date:
- Size: 44.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3dc3dc0b1192910acec8fba4f77cefcdb13f82d39bab7bafe6e23c2c3524d9e5
|
|
| MD5 |
485902389304eea3bbb9baf5e854544c
|
|
| BLAKE2b-256 |
108639c44f2eea18c032817cd2e906e53fae6dca0d3033b50720b5601e1ecffb
|
File details
Details for the file engram_peft-1.0.4-py3-none-any.whl.
File metadata
- Download URL: engram_peft-1.0.4-py3-none-any.whl
- Upload date:
- Size: 34.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
90824185add408661b68f37d9b47fab857936f045f0abb661b2d0e9e339b0b36
|
|
| MD5 |
8943f2b39055e4138ba8b24459fe7889
|
|
| BLAKE2b-256 |
f0d8b75cc0166995943ae9379b033e39440391914883af8f52d60ee17da2b0a7
|