Hypernetworks that update LLMs to remember factual information
Doc-to-LoRA (D2L): Learning to Instantly Internalize Contexts
:sparkles:Interactive Web | :newspaper:X | :scroll:Paper | :hugs:Hugging Face | :octocat:GitHubA reference implementation of Doc-to-LoRA (D2L).
๐ ๏ธ Installation
curl -LsSf https://astral.sh/uv/install.sh | sh
./install.sh
๐ค Pre-Trained Models
uv run huggingface-cli login
uv run huggingface-cli download SakanaAI/doc-to-lora --local-dir trained_d2l --include "*/"
๐ Python API Usage
# caveat: this interface only supports non-batched inputs
for batched inference please see src/ctxtolora/modeling/hypernet.py
import torch
from ctxtolora.modelloading import gettokenizer from ctxtolora.modeling.hypernet import ModulatedPretrainedModel
model loading
checkpointpath = "trainedd2l/gemmademo/checkpoint-80000/pytorchmodel.bin"
statedict = torch.load(checkpointpath, weights_only=False)
model = ModulatedPretrainedModel.fromstatedict(
statedict, train=False, usesequence_packing=False
)
model.reset()
tokenizer = gettokenizer(model.basemodel.nameorpath)
prepare data
doc = open("data/sakana_wiki.txt", "r").read()
chat = [{"role": "user", "content": "Tell me about Sakana AI."}]
chatids = tokenizer.applychat_template(
chat,
addspecialtokens=False,
returnattentionmask=False,
addgenerationprompt=True,
return_tensors="pt",
).to(model.device)
calls after internalization will be influenced by internalized info
model.internalize(doc)
outputs = model.generate(inputids=chatids, maxnewtokens=512) print(tokenizer.decode(outputs[0]))
remove internalized info
model.reset()
without internalized info, the model will halucinate
outputs = model.generate(inputids=chatids, maxnewtokens=512)
print(tokenizer.decode(outputs[0]))
๐ฎ Interactive Demo
uv run demo/app.py
Video Demo
๐งช Experimental Scripts
To run any of the following scripts, useuv run $PATHTOSCRIPT from the root of this project.
| Experiment | Data prep | Training | Evaluation | Notes | | ------------------------------------ | ------------------------------------- | ----------------------------- | ---------------------------- | ----------------------------------------------------------------------------------------------------------------------------------- | | Main experiment | scripts/mainexp/0-downloaddata.sh | scripts/mainexp/1-train.sh | scripts/mainexp/eval/*.sh | Downloading data is fastest; regenerate only if you need fresh synthetic data. Evaluation scripts reproduce the main paper metrics. | | NIAH | scripts/niah/0-gendata.sh | scripts/niah/1-train.sh | scripts/niah/2-eval.sh | Run the scripts in order; data generation only needs to happen once |
๐ฌ Self-Generated Data Viewer
After downloading/generating the data, we can see samples of the data using this script.uv run webui/selfgenviewer.py
See more info at webui/SELFGEN_VIEWER.md.
๐ Citation
@inproceedings{charakorn2026doctolora,
title ={Doc-to-Lo{RA}: Learning to Instantly Internalize Contexts},
author ={Rujikorn Charakorn and Edoardo Cetin and Shinnosuke Uesaka and Robert Tjarko Lange},
booktitle ={Forty-third International Conference on Machine Learning},
year ={2026},
url ={https://openreview.net/forum?id=iW1oBBO72S}
}