Doc-to-LoRA: Learning to Instantly Internalize Contexts
Reading guide · Based on sections of the original paper
Why read this
Practitioners weighing KV-cache cost against per-document fine-tuning for document QA, personalization or on-device use will find concrete latency, memory and accuracy trade-offs measured on Gemma-2-2B.
What the authors found
- On SQuAD with gemma-2-2b-it, the authors report that D2L outperforms the in-parameter baselines and reaches 82.5% of the in-context ROUGE-L. It internalizes a context in under a second. Oracle CD takes about 40 s and CD with generated queries takes over 100 s and over 40 GB of VRAM. D2L's update memory is under 2 GB. 5.1.1 Efficient and Effective Internalization on Reading Comprehension Tasks
- On 2WikiMultihopQA, with documents up to 32K tokens and a longest training sample of 2,344 tokens, batched D2L scores 0.857 normalized performance in 0.21 s. CD with 25 generated queries scores 0.745 and takes 465 s; oracle CD scores 0.901. 5.1.2 Instant Zero-Shot Internalization of Long-Context Information
- On a RULER-style needle-in-a-haystack task, D2L was trained on contexts of at most 256 tokens. Its retrieval stays near-perfect up to 40K tokens, while the base model fails beyond its 8K window. This used a simplified setup: an NTP loss and the same query at training and evaluation. 4 Implanting Synthetic Needle-in-a-Haystack Information
Main limitation
When an unrelated context is internalized, D2L lowers SQuAD performance below both the base model and CD: 0.096 versus 0.201 and 0.211. The long-document gains are also uneven. On MultiFieldQA, D2L scores about 0.48 relative to truncated ICL, below CD with 25 generated queries at 0.528. C.4 Knowledge Interference
Where to start
C.4 Knowledge Interference · Check the knowledge-interference test and the MultiFieldQA/QASPER tables, where D2L's advantage over CD narrows or reverses.
Abstract
Long input sequences are central to in-context learning, document understanding, and multi-step reasoning of Large Language Models (LLMs). However, the quadratic attention cost of Transformers makes inference memory-intensive and slow. While context distillation (CD) can transfer information into model parameters, per-prompt distillation is impractical due to training costs and latency. To address these limitations, we propose Doc-to-LoRA (D2L), a lightweight hypernetwork that meta-learns to perform approximate CD within a single forward pass. Given an unseen prompt, D2L generates a LoRA adapter for a target LLM, enabling subsequent queries to be answered without re-consuming the original context, reducing latency and KV-cache memory consumption during inference of the target LLM. On a long-context needle-in-a-haystack task, D2L successfully learns to map contexts into adapters that store the needle information, achieving near-perfect zero-shot accuracy at sequence lengths exceeding the target LLM's native context window by more than 4x. On real-world QA datasets with limited compute, D2L outperforms standard CD while significantly reducing peak memory consumption and update latency. We envision that D2L can facilitate rapid adaptation of LLMs, opening up the possibility of frequent knowledge updates and personalized chat behavior.