🚀 Engram-PEFT: An unofficial implementation of DeepSeek Engram. Inject high-capacity conditional memory into LLMs via sparse retrieval PEFT without increasing inference FLOPs / DeepSeek Engram 架构的非官方实现。通过参数高效微调 (PEFT) 为大语言模型注入超大规模条件记忆,支持稀疏更新且不增加推理开销。
Python
49
172 commits
updated Sep 7, 2026
[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 PEFT-style interface to inject conditional memory into any Transformer-based LLM, while also supporting stacked-adapter and full-finetuning workflows through explicit train_mode controls.
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.
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]"
NPU (Ascend) Support: For training on Huawei Ascend NPU, install torch-npu:
pip install torch-npu --index-url https://repo.huaweicloud.com/repository/pypi/simple
Engram-PEFT automatically detects NPU availability and switches all AMP settings accordingly. On NPU, bfloat16 is generally not supported — use engram_dtype="float16" in your EngramConfig.
For distributed training on NPU (torchrun DDP, DeepSpeed ZeRO‑1/2), use the built-in backend detection:
from engram_peft.utils.device import get_distributed_backend
backend = get_distributed_backend() # "hccl" on NPU, "nccl" on CUDA
See NPU Distributed Training in the docs for details.
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,
train_mode="engram_only",
)
# 3. Quick check on trainable parameters
model.print_trainable_parameters()
# trainable params: ... (backbone: 0, engram: ...) || all params: ... || trainable%: ...
You can also trigger training through a YAML configuration file without writing Python scripts:
# 1. Generate a full, documented configuration template
engram-peft config-template --output training_config.yaml
The generated YAML is structured into five main sections:
model_name_or_path: Base model identifier.engram_config: Core hyperparameters for Engram layers.lora_config: (Optional) PEFT LoRA settings for hybrid adaptation.training_args: Standard transformers.TrainingArguments.data_args: Dataset settings and tokenization logic.engram-peft train --config training_config.yaml
The CLI automatically generates a ready-to-run inference.py script in your output_dir.
uv run python outputs/tinyllama-lora-engram/inference.py
engram-peft train --config training_config.yaml --overrides "training_args.learning_rate=5e-5"
| Method | Params Added | Speed (s/step) | Training Loss | Eval Loss | Peak Memory (JSON) |
|---|---|---|---|---|---|
| LoRA (r=16) | ~2.25 M | 0.2738 s | 1.231 | 0.9890 | 8.07 GB |
| Engram-PEFT | 545.4 M | 0.2961 s | 1.263 | 1.0165 | 9.38 GB |
| LoRA+Engram | ~547.7 M | 0.3360 s | 1.214 | 0.9656 | 10.33 GB |
| Full Finetune+Engram | ~545.4 M | 0.3818 s | 1.111 | 1.0944 | 15.32 GB |
[!TIP] Performance Insight: In our latest benchmark (Test 8 & 9, TinyLlama-1.1B, 3000 steps), LoRA+Engram achieved the best convergence (lowest eval loss), outperforming standalone LoRA by ~2.3%, Engram by ~5.0%, and Full Finetune+Engram by ~12.2%. Engram-PEFT provides 240x more parameter capacity (545M) for knowledge storage with minimal latency penalty. Use LoRA+Engram to leverage both structural adaptation and high-capacity sparse memory. Full Finetune+Engram, while more memory-intensive, shows competitive performance but requires significantly more GPU resources and exhibits potential overfitting tendencies.

* Engram employs sparse lookup; only a tiny fraction of parameters (approx. 1%) are active and receive gradient updates per step. To reproduce these benchmarks on your own hardware, run uv run python examples/compare_engram_lora.py --all. For a detailed breakdown of performance, computation, and memory, see our Performance Analysis.
EngramDataCollator pre-calculates multi-head hash indices on the CPU. By using num_workers > 0, these indices are prefetched in parallel with training, ensuring zero hashing overhead on the GPU.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.print_trainable_parameters() and save_pretrained().EngramTrainer fully supports trainer.train(resume_from_checkpoint=True) for seamless mid-training recovery, correctly restoring LoRA adapters and Engram weights from composite checkpoints.train_mode="engram_only", "preserve_trainable", and "full_finetune" make backbone behavior predictable.EngramTrainer with built-in sparse Adam support and automatic sync of optimizer hyperparameters.inference.py scripts for immediate verification.engine="qwen_ple" and table_spec="PLE_QWEN_V1", including deterministic Qwen PLE hashing and automatic EngramDB Store-I disk-table installation via table_source="engramdb:store".text_config attributes for state-of-the-art models.push_to_hub and loading directly from Hub IDs via from_pretrained, fully aligned with the PEFT ecosystem.EngramCompatibleSFTTrainer that handles model preparation, hash precomputation, and sparse gradient clipping automatically for seamless instruction tuning.compute_dtype.get_distributed_backend() returns "hccl" on NPU, "nccl" on CUDA).torchrun (DDP) and DeepSpeed ZeRO-1/2 on both CUDA and NPU. Distributed sparse embeddings are supported under DDP; DeepSetup mode automatically falls back to dense embeddings for ZeRO optimizer compatibility.For full details, see our documentation:
# Engram-only PEFT
model = get_engram_model(base_model, config, tokenizer, train_mode="engram_only")
# Keep LoRA / existing trainable params
model = get_engram_model(model, config, tokenizer, train_mode="preserve_trainable")
# Full finetuning + Engram
model = get_engram_model(base_model, config, tokenizer, train_mode="full_finetune")
from torch.optim import AdamW
from engram_peft import get_optimizer
optimizer = get_optimizer(
model,
backbone_learning_rate=5e-5,
engram_dense_learning_rate=4e-4,
engram_sparse_learning_rate=2e-3,
backbone_optimizer=AdamW,
)
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}
}
We welcome contributions! Please see our Contributing Guide for details on our tiered development workflow (L1-L4) and testing standards.
Apache License 2.0. See LICENSE for details.
168 commits
4 commits
Python
99.4%
🚀 Engram-PEFT: An unofficial implementation of DeepSeek Engram. Inject high-capacity conditional memory into LLMs via sparse retrieval PEFT without increasing inference FLOPs / DeepSeek Engram 架构的非官方实现。通过参数高效微调 (PEFT) 为大语言模型注入超大规模条件记忆,支持稀疏更新且不增加推理开销。
Python
49
172 commits
updated Sep 7, 2026
[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 PEFT-style interface to inject conditional memory into any Transformer-based LLM, while also supporting stacked-adapter and full-finetuning workflows through explicit train_mode controls.
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.
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]"
NPU (Ascend) Support: For training on Huawei Ascend NPU, install torch-npu:
pip install torch-npu --index-url https://repo.huaweicloud.com/repository/pypi/simple
Engram-PEFT automatically detects NPU availability and switches all AMP settings accordingly. On NPU, bfloat16 is generally not supported — use engram_dtype="float16" in your EngramConfig.
For distributed training on NPU (torchrun DDP, DeepSpeed ZeRO‑1/2), use the built-in backend detection:
from engram_peft.utils.device import get_distributed_backend
backend = get_distributed_backend() # "hccl" on NPU, "nccl" on CUDA
See NPU Distributed Training in the docs for details.
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,
train_mode="engram_only",
)
# 3. Quick check on trainable parameters
model.print_trainable_parameters()
# trainable params: ... (backbone: 0, engram: ...) || all params: ... || trainable%: ...
You can also trigger training through a YAML configuration file without writing Python scripts:
# 1. Generate a full, documented configuration template
engram-peft config-template --output training_config.yaml
The generated YAML is structured into five main sections:
model_name_or_path: Base model identifier.engram_config: Core hyperparameters for Engram layers.lora_config: (Optional) PEFT LoRA settings for hybrid adaptation.training_args: Standard transformers.TrainingArguments.data_args: Dataset settings and tokenization logic.engram-peft train --config training_config.yaml
The CLI automatically generates a ready-to-run inference.py script in your output_dir.
uv run python outputs/tinyllama-lora-engram/inference.py
engram-peft train --config training_config.yaml --overrides "training_args.learning_rate=5e-5"
| Method | Params Added | Speed (s/step) | Training Loss | Eval Loss | Peak Memory (JSON) |
|---|---|---|---|---|---|
| LoRA (r=16) | ~2.25 M | 0.2738 s | 1.231 | 0.9890 | 8.07 GB |
| Engram-PEFT | 545.4 M | 0.2961 s | 1.263 | 1.0165 | 9.38 GB |
| LoRA+Engram | ~547.7 M | 0.3360 s | 1.214 | 0.9656 | 10.33 GB |
| Full Finetune+Engram | ~545.4 M | 0.3818 s | 1.111 | 1.0944 | 15.32 GB |
[!TIP] Performance Insight: In our latest benchmark (Test 8 & 9, TinyLlama-1.1B, 3000 steps), LoRA+Engram achieved the best convergence (lowest eval loss), outperforming standalone LoRA by ~2.3%, Engram by ~5.0%, and Full Finetune+Engram by ~12.2%. Engram-PEFT provides 240x more parameter capacity (545M) for knowledge storage with minimal latency penalty. Use LoRA+Engram to leverage both structural adaptation and high-capacity sparse memory. Full Finetune+Engram, while more memory-intensive, shows competitive performance but requires significantly more GPU resources and exhibits potential overfitting tendencies.

* Engram employs sparse lookup; only a tiny fraction of parameters (approx. 1%) are active and receive gradient updates per step. To reproduce these benchmarks on your own hardware, run uv run python examples/compare_engram_lora.py --all. For a detailed breakdown of performance, computation, and memory, see our Performance Analysis.
EngramDataCollator pre-calculates multi-head hash indices on the CPU. By using num_workers > 0, these indices are prefetched in parallel with training, ensuring zero hashing overhead on the GPU.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.print_trainable_parameters() and save_pretrained().EngramTrainer fully supports trainer.train(resume_from_checkpoint=True) for seamless mid-training recovery, correctly restoring LoRA adapters and Engram weights from composite checkpoints.train_mode="engram_only", "preserve_trainable", and "full_finetune" make backbone behavior predictable.EngramTrainer with built-in sparse Adam support and automatic sync of optimizer hyperparameters.inference.py scripts for immediate verification.engine="qwen_ple" and table_spec="PLE_QWEN_V1", including deterministic Qwen PLE hashing and automatic EngramDB Store-I disk-table installation via table_source="engramdb:store".text_config attributes for state-of-the-art models.push_to_hub and loading directly from Hub IDs via from_pretrained, fully aligned with the PEFT ecosystem.EngramCompatibleSFTTrainer that handles model preparation, hash precomputation, and sparse gradient clipping automatically for seamless instruction tuning.compute_dtype.get_distributed_backend() returns "hccl" on NPU, "nccl" on CUDA).torchrun (DDP) and DeepSpeed ZeRO-1/2 on both CUDA and NPU. Distributed sparse embeddings are supported under DDP; DeepSetup mode automatically falls back to dense embeddings for ZeRO optimizer compatibility.For full details, see our documentation:
# Engram-only PEFT
model = get_engram_model(base_model, config, tokenizer, train_mode="engram_only")
# Keep LoRA / existing trainable params
model = get_engram_model(model, config, tokenizer, train_mode="preserve_trainable")
# Full finetuning + Engram
model = get_engram_model(base_model, config, tokenizer, train_mode="full_finetune")
from torch.optim import AdamW
from engram_peft import get_optimizer
optimizer = get_optimizer(
model,
backbone_learning_rate=5e-5,
engram_dense_learning_rate=4e-4,
engram_sparse_learning_rate=2e-3,
backbone_optimizer=AdamW,
)
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}
}
We welcome contributions! Please see our Contributing Guide for details on our tiered development workflow (L1-L4) and testing standards.
Apache License 2.0. See LICENSE for details.
168 commits
4 commits
Python
99.4%