Context Distillation as Latent Memory Management

Ziyang Zheng, Zeju Li, Xiangyu Wen, Jianyuan Zhong, Junhua Huang, Lei Chen, Mingxuan Yuan, Qiang XuarXiv 2026

TL;DR

Context distillation compresses long documents into LLM parameters to eliminate heavy context windows, but managing a large collection of these "parameterized memories" remains a challenge. This paper introduces a modular framework that stores each context as an independent, "hot-swappable" LoRA adapter. Combined with a two-stage retrieval system and an entropy-based self-gating mechanism, it safely and efficiently activates the correct memory at inference time with up to a 1000× reduction in FLOPs compared to standard in-context learning.

What Problem Does This Paper Address?

In-context learning (ICL) allows LLMs to adapt to new documents simply by placing them in the prompt. However, this approach is temporary and computationally expensive: the model must re-read the entire document for every single query, leading to high latency, massive memory usage, and generation instability.

Context Distillation (CD) offers a solution by training a model to internalize the context directly into its weights. But how do we manage hundreds or thousands of these distilled memories?

Existing CD methods suffer from two main flaws:

  1. The Oracle Assumption: They assume an unrealistic scenario where an external system perfectly knows which distilled memory to load for a given query.
  2. The Cumulative Flaw: Methods that try to sequentially bake multiple contexts into a single adapter suffer from catastrophic forgetting and "noise assimilation," where irrelevant past contexts degrade the model's general capabilities.

This paper addresses the challenge of latent memory management: how to store, retrieve, and safely activate multiple distilled memories in realistic, non-oracle settings.

Comparison of cumulative context distillation and the proposed modular framework.

Methods

The authors abandon the cumulative paradigm in favor of a modular, bank-like architecture:

  • Modular LoRA Memory Bank: Each document or context is distilled into its own independent LoRA adapter ($\Delta\theta_i$).
  • Cache-Sharing Distillation (The "Hot-Swap" Property): Standard adapter switching requires recomputing the key-value (KV) cache for the prompt prefix, which is slow. The authors introduce a dual cache-sharing pipeline. The query's prefix KV-cache is computed using the frozen base model before any adapter is activated. The LoRA adapter is only turned on during response generation. This ensures all adapters are perfectly compatible with the same base-model KV-cache, allowing instant adapter hot-swapping.
  • Two-Stage Retrieval System: When a query arrives, an External Retriever uses dense embeddings to find the top-$K$ candidate adapters. Then, an Internal Router (a lightweight feed-forward network) fine-ranks them using the first-token hidden states and predictive entropy.
  • Self-Gating System: To prevent a retrieved adapter from corrupting the model's response to general knowledge queries, the system evaluates the Shannon entropy of the first generated token. If the entropy is low, the model is confident and continues with the adapter. If it is high, the system instantly deactivates the adapter and falls back to the base model.
Figure 2: Overview of our training stage and inference stage. Training: Teacher model precomputes the KV-cache of target document for reuse during training, while student computes the prefix KV-cache with Base Model to enable the Hot-swap property for management. Inference: Given a query, we first retrieve the most relevant latent memory ∆θi, and then use Self-Gating to decide whether to continue with θ + ∆θi or fallback to θ. Owing to the cache-sharing distillation introduced during training, memory management remains efficient.

Datasets

The framework was evaluated using the following datasets:

  • NarrativeQA: Used for long-document context-specific reading comprehension.
  • SQuAD: Filtered to the top 300 most frequently queried documents to evaluate short-to-medium document question answering.
  • CommonsenseQA: A context-agnostic dataset used to test whether the self-gating mechanism successfully prevents distilled memories from interfering with general knowledge.

Training queries were synthetically generated by prompting an instruction-tuned model to create distinct questions highly relevant to each source document.

Key Insights and Ideas

  • Cumulative Distillation is Mathematically Flawed: The authors prove that sequential cumulative distillation is mathematically equivalent to conditioning a base model on all accumulated contexts simultaneously. This means it inherently inherits the teacher model's sensitivity to irrelevant noise and long-context degradation.
  • The Entropy Gap: Distilled models inherit a distinct "entropy gap" from their teachers. When a LoRA adapter encounters a query relevant to its injected knowledge, its first-token predictive entropy is significantly lower than when it encounters an irrelevant query. This provides a built-in, computationally trivial confidence signal for gating.
  • Decoupling Prefix Caching: Forcing the student model to remain compatible with base-model KV-caches during training eliminates the inference-time overhead of switching adapters, making modular parameter management practical at scale.

Key Results

  • Superior Performance in Realistic Settings: In non-oracle evaluations (where the correct adapter is unknown), the proposed method vastly outperformed cumulative baselines. On NarrativeQA, it achieved 24.54 ROUGE-1 (using Qwen2.5-0.5B) compared to scores below 11 for TempLora and InfiniteICL.
  • Effective Gating: When tested on a hybrid mix of context-specific and general queries, always activating an adapter caused CommonsenseQA accuracy to drop significantly. With Self-Gating enabled, accuracy was largely preserved (improving from 37.30% to 45.62% on the 0.5B model and up to 79.44% on the 7B model).
  • Massive Efficiency Gains: At a context length of 4,000 tokens, the modular cache-sharing approach reduced inference FLOPs by 414.7× under Top-3 retrieval and 1081.4× under Top-5 retrieval compared to standard explicit ICL. It also achieved an 8.4× training speedup and a 4.1× reduction in peak training memory.

Limitations and Future Work

  • The Performance Gap: While highly efficient, context distillation still exhibits a performance gap compared to full, explicit In-Context Learning (ICL) under oracle settings. The authors suggest exploring on-policy distillation methods to close this gap.
  • Storage Footprint: Storing a separate LoRA adapter for every single document introduces a linear storage overhead. Although LoRAs are small, managing thousands of them can become costly. Future work will investigate even more parameter-efficient modules, such as BitFit, to minimize the memory footprint.

Summary updated

BibTeX
@misc{zheng2026context,
  author = {Ziyang Zheng and Zeju Li and Xiangyu Wen and Jianyuan Zhong and Junhua Huang and Lei Chen and Mingxuan Yuan and Qiang Xu},
  title = {{Context Distillation as Latent Memory Management}},
  year = {2026},
  eprint = {2605.28889},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2605.28889},
}
Figure