Doc-to-LoRA (D2L)
A hypernetwork that turns a document into LoRA weights at inference time, so an LLM remembers the facts without long prompts.
- Source repo
- SakanaAI/doc-to-lora
- Stars
- ★ 839
- Last updated
- 5mo ago
- License
- MIT
- Primary language
- Python
- FA score
- 32/100 · Major gaps
At a glance
- How it runs
- Works with
- Portable with changes
- Cost
- Free, no paid service needed
- Setup effort
- High · needs real infrastructure
- You'll need
- Typical use
- Researchers reproducing the paper's context-internalization results, running training and evaluation through scripts/main_exp.
- Not a fit if
- Teams that only want a hosted API and no local model weights
- Users needing a stable production interface — the Python API is non-batched only
- Hobbyists without GPU memory budget expecting a turnkey tool
- Source review
- 32/100 · Major gaps 1 safety controls not found
What does this agent do, and when should you use it?
Doc-to-LoRA is Sakana AI's reference implementation of a hypernetwork approach that lets an LLM "instantly internalize" a context. Instead of retrieving chunks or stuffing a prompt, it generates LoRA updates conditioned on the document and injects them into a base model, so later generations are shaped by that document. The repo ships a uv-based install script, downloadable pre-trained checkpoints from Hugging Face, a Python inference API, an interactive demo, and full data-prep/training/eval scripts for the main experiment and a NIAH (needle-in-a-haystack) experiment. The code sits on PyTorch and Hugging Face model loading, with entry points such as ModulatedPretrainedModel and get_tokenizer. It is aimed at researchers reproducing the paper or building on the method, not at teams wanting a plug-and-play service.
The repository implements ModulatedPretrainedModel, which loads a state dict (e.g. trained_d2l/gemma_demo/checkpoint-80000/pytorch_model.bin) and builds the model. Calling model.internalize(doc) reads a plain-text document and updates the model's LoRA parameters from it; a following model.generate(input_ids=..., max_new_tokens=512) produces answers influenced by the document, and model.reset() removes the internalized information. get_tokenizer fetches the tokenizer for the base model, and chat inputs are built with tokenizer.apply_chat_template. The README also documents demo/app.py for an interactive demo, webui/self_gen_viewer.py for inspecting self-generated data, and two script sets (scripts/main_exp, scripts/niah) covering data download/generation, training and evaluation.
- Researchers reproducing the paper's context-internalization results, running training and evaluation through scripts/main_exp.
- Teams evaluating long-context memory approaches who want to compare a demo's output with and without an internalized document (including hallucination without it).
- Engineers running needle-in-a-haystack memory benchmarks via the ordered scripts in scripts/niah (generate data, train, evaluate).
- Developers who need a fixed base model to absorb a small set of proprietary documents temporarily, using internalize/reset to inject and roll back.
- Teams building synthetic training data who want to inspect generated samples with webui/self_gen_viewer.py.
How do you install or deploy this agent?
Install uv, then run the repository's install script:
curl -LsSf https://astral.sh/uv/install.sh | sh
./install.shLog in to Hugging Face and download the pre-trained models into a local trained_d2l directory:
uv run huggingface-cli login
uv run huggingface-cli download SakanaAI/doc-to-lora --local-dir trained_d2l --include "*/"How do you use this agent?
From the project root, call the downloaded checkpoint from Python:
import torch
from ctx_to_lora.model_loading import get_tokenizer
from ctx_to_lora.modeling.hypernet import ModulatedPretrainedModel
checkpoint_path = "trained_d2l/gemma_demo/checkpoint-80000/pytorch_model.bin"
state_dict = torch.load(checkpoint_path, weights_only=False)
model = ModulatedPretrainedModel.from_state_dict(
state_dict, train=False, use_sequence_packing=False
)
model.reset()
tokenizer = get_tokenizer(model.base_model.name_or_path)
doc = open("data/sakana_wiki.txt", "r").read()
chat = [{"role": "user", "content": "Tell me about Sakana AI."}]
chat_ids = tokenizer.apply_chat_template(
chat,
add_special_tokens=False,
return_attention_mask=False,
add_generation_prompt=True,
return_tensors="pt",
).to(model.device)
model.internalize(doc)
outputs = model.generate(input_ids=chat_ids, max_new_tokens=512)
print(tokenizer.decode(outputs[0]))Launch the interactive demo:
uv run demo/app.pyExperiment scripts also run through uv from the repo root, for example:
uv run scripts/main_exp/0-download_data.sh
uv run scripts/main_exp/1-train.shWhat are this agent's strengths and limitations?
- Internalizes context as hypernetwork-generated LoRA weights instead of long prompts, with paired internalize/reset calls that make A/B comparison and rollback easy.
- Official pre-trained checkpoints are hosted at SakanaAI/doc-to-lora, so inference works without training from scratch.
- Ships two complete pipelines (main experiment and NIAH) with data prep, training and evaluation scripts to reproduce paper metrics.
- MIT licensed and modifiable for commercial integration; includes an interactive demo and a self-generated data viewer for quick inspection.
- The documented Python API only supports non-batched inputs; batched inference requires editing src/ctx_to_lora/modeling/hypernet.py.
- Only specific pre-trained checkpoints (e.g. a Gemma demo) are provided; switching base models means training your own hypernetwork.
- Local inference needs PyTorch plus downloaded model weights, with real VRAM and runtime requirements and a multi-step install.
- Positioned as a reference implementation, with no guidance on production serving or stability; batching/concurrency paths are thinly documented.
How does this agent compare with similar options?
Key facts side by side with the most closely related agents.
| Agent | Source review | Form / cost | Stars | Updated | Language | Full support on |
|---|---|---|---|---|---|---|
| Doc-to-LoRA (D2L) This agent | 32 · Major gaps | Library / SDKFree | ★ 839 | 5mo ago | Python | — |
| AgileRL | 71 · Some gaps | Library / SDKFreemium | ★ 964 | today | Python | — |
| Deep Researcher Agent | 52 · Major gaps | Agent plugin / skillFree + model costs | ★ 1.1k | 5mo ago | Python | Claude Code · OpenAI API · Claude API |
| ChatTTS Conversational Speech | 66 · Some gaps | Web appFree | ★ 40k | 6mo ago | Python | — |
How does FollowAgents rate this agent?
Why each dimension lost points
Evidence is limited to README, LICENSE, and pyproject.toml. The install path pipes a remote script via curl and runs a local install.sh with no checksum or confirmation step, so least_privilege and user_confirmation score low. Data flow only mentions downloading Hugging Face models and reading local documents; how document content is processed or stored is unspecified, so data_flow_transparency and sensitive_data_handling get 1. Dependencies are largely unpinned (peft, vllm pinned but others floating) with no lockfile or security audit, dependency_security 1. external_effects and rollback: only model.reset() clears internalized info, a limited rollback, 1. source_attribution has paper citation and authors, 2.
README example and pyproject name (ctx-to-lora) diverge from repo name (doc-to-lora), and the example uses torch.load(weights_only=False), a loading risk; self_consistency 1. Dependency availability relies on version constraints only, no lockfile or environment validation, 1. No failure messages in source or README, failure_messages 0.
README targets researchers with Python API, interactive demo, and experiment scripts, but does not state scenario boundaries, trigger conditions, or environment requirements (GPU, memory); audience_and_scenarios, capability_boundaries, trigger_precision, environment_fit all 1.
README is structured but thin; install notes depend on an external script without explaining install.sh, install_notes 1. Naming inconsistent between repo and package, naming_stability 1. Examples exist but no FAQ, examples_and_faq 1. known_limitations only notes non-batched interface and hallucination caveat, 1. LICENSE is full MIT, 2. No changelog, versioning_changelog 0. Maintenance responsibility only implied by authors/institution, 1.
Output usability depends on model weights and GPU; README example is plausible but unverified, output_usability 1. Marginal value is hypernetwork fact internalization but no baseline comparison, marginal_value 1. Cost-benefit involves large-model training and inference with no resource estimates, cost_benefit 1.
Paper link and citation support claim_traceability 1; no tests or independent reproduction evidence, cross_source_corroboration 1; README mixes facts with inference (e.g., 'without internalized info, the model will halucinate') without separation, fact_inference_separation 1.
- Not found in source: confirmation before actingTurn on (or add) a confirmation step before it acts, and try it in a sandbox or test environment before real data.
- Install path pipes a remote script via curl; review install.sh and the remote script before running.
- Example uses torch.load(weights_only=False), which risks code execution with untrusted weights.
- Many dependencies are unpinned with no lockfile, raising reproducibility concerns.
- Repo name and package name differ, which may confuse imports.
- No resource requirements (GPU/memory) or failure-handling guidance provided.