Doc-to-LoRA: Learning to Instantly Internalize Contexts

Rujikorn Charakorn, Edoardo Cetin, Shinnosuke Uesaka, Robert Tjarko Lange

Large Language Models (LLMs) rely heavily on long input sequences for in-context learning, document understanding, and multi-step reasoning. However, this reliance introduces a significant bottleneck: the "quadratic tax" of Transformer attention. As prompts grow longer, memory consumption (via the KV-cache) spikes, and inference slows to a crawl.

Historically, researchers have explored two main workarounds, both with severe trade-offs:

  1. In-Context Learning (ICL): Placing the entire document in the prompt. This is simple but computationally expensive and memory-intensive.
  2. Context Distillation (CD): "Baking" the document's information directly into the model's weights. While this makes subsequent inference incredibly fast and memory-efficient, the distillation process itself requires thousands of slow backpropagation steps for every single new document.

Doc-to-LoRA (D2L) offers a breakthrough solution to this "update latency" problem. It provides the inference speed and memory efficiency of a distilled model, but with the near-instant setup time of a standard prompt.

The Doc-to-LoRA Approach

At its core, D2L is a lightweight hypernetwork—a secondary model designed to predict the weights of a larger target LLM. Instead of relying on gradient descent to distill a document, D2L uses a Perceiver-based architecture to read the document's hidden activations and output a set of LoRA (Low-Rank Adaptation) weights in a single forward pass.

Key technical innovations include:

  • Meta-Learning Objective: The hypernetwork is trained to minimize the KL-divergence between a "teacher" (the LLM with the full document in its prompt) and a "student" (the LLM using only the generated LoRA weights, without the document text).
  • Chunking Mechanism: To handle documents far longer than its training data, D2L breaks text into chunks, generates LoRA matrices for each, and concatenates them. This allows the model to scale well beyond its native context limits.
  • Weight-Space Mapping: D2L effectively compresses variable-length input activations into fixed-shape LoRA parameters, internalizing the document's knowledge directly into the neural layers.

Training and Evaluation

The researchers meta-trained D2L using a massive pipeline featuring a 900M-token subset of FineWeb-Edu alongside passage-grounded QA datasets (SQuAD, ROPES, DROP). This generated 3.2 million unique context-query pairs to teach the hypernetwork robust generalization.

Evaluation spanned synthetic tasks like Needle-in-a-Haystack (NIAH), real-world long-context benchmarks (LongBench), and even cross-modal experiments using Imagenette.

Key Results and Insights

  • Amortized Distillation (Sub-Second Speed): By investing in a one-time meta-training phase (5 days on 8 H200 GPUs), D2L reduces the time to internalize a new context to less than 1 second. Standard Context Distillation, by contrast, takes 40 to 100+ seconds per document.
  • Massive Memory Efficiency: For long documents, D2L reduces the additional memory required for inference from roughly 12 GB (for KV-cache) to less than 50 MB (for LoRA weights).
  • Bypassing the Context Window: Because information is stored in the weights rather than the prompt, the LLM can effectively "remember" massive documents. In the Needle-in-a-Haystack task, D2L maintained near-perfect accuracy up to 40,000 tokens—more than 4x the base model's native context window—despite only being trained on 256-token sequences.
  • Strong Performance: On the SQuAD benchmark, D2L achieved 82.5% of the performance of the full-context "gold standard" while consuming zero prompt tokens for the document.
  • Cross-Modal Weight Transfer: In a fascinating experiment, D2L successfully took activations from a Vision-Language Model (which processed an image) and generated LoRA weights for a text-only model. The text model was then able to accurately "classify" the image it never actually saw.

Limitations and Future Directions

While Doc-to-LoRA represents a major leap forward for efficient document processing, it has a few current limitations. The initial meta-training is computationally expensive, and the resulting hypernetwork is strictly tied to a specific base model (e.g., Gemma-2-2b); switching to a new architecture requires retraining from scratch.

Additionally, the authors note a "knowledge interference" challenge, where internalizing a new document can sometimes cause the model to temporarily ignore its general prior knowledge. Finally, while D2L recovers the vast majority of full-context performance, a small accuracy gap remains compared to raw In-Context Learning. Closing this gap and improving regularization against knowledge forgetting are the next frontiers for this promising architecture.

Summary updated

BibTeX
@inproceedings{charakorn2026doc,
  author = {Rujikorn Charakorn and Edoardo Cetin and Shinnosuke Uesaka and Robert Tjarko Lange},
  title = {{Doc-to-LoRA: Learning to Instantly Internalize Contexts}},
  booktitle = {International Conference on Machine Learning},
  year = {2026},
  url = {https://openreview.net/forum?id=iW1oBBO72S},
}
Figure