D2O: Dynamic Discriminative Operations for Efficient Long-Context Inference of Large Language Models

Zhongwei Wan, Xinjian Wu, Yu Zhang, Yi Xin, Chaofan Tao, Zhihong Zhu, Xin Wang, Siqi Luo, Jing Xiong, Longyue Wang, Mi Zhang

TL;DR

D2O (Dynamic Discriminative Operations) is a plug-and-play KV cache compression method that significantly reduces the memory footprint of Large Language Models (LLMs) during long-context inference. By dynamically allocating cache budgets across layers and using a token-merging "compensation mechanism" instead of permanent eviction, D2O achieves over 3x throughput speedups while maintaining high generation quality without any fine-tuning.

What Problem Does This Paper Address?

As LLMs are increasingly used for long-context tasks like document summarization and multi-turn dialogue, the memory required for the Key-Value (KV) cache becomes a massive bottleneck. The KV cache stores previous token representations to avoid redundant computations, but its size grows linearly with sequence length.

Existing solutions typically use "eviction" strategies—permanently dropping less important KV pairs based on attention scores. However, these methods suffer from two main flaws:

  1. Indiscriminate Layer Treatment: They often apply the same eviction budget to every layer, ignoring the fact that different layers have different attention patterns.
  2. Irreversible Information Loss: Once a token is evicted, its information is gone forever. If that token becomes relevant later in the generation process, the model may hallucinate or lose context.

Methods

D2O introduces a two-level approach to optimize the KV cache dynamically:

1. Layer-Level: Dynamic Allocation

The authors observed that shallow layers tend to have dense, global attention, while deeper layers exhibit sparser, localized patterns. Attention map density varies significantly between shallow and deep layers.

Figure 1: Attention map density comparisons of shallow layers (layer 0, 1) and deep layers (layer 16, 30, 31) of LLaMA-2-7B on the GSM8K dataset. We use the mean value of heads for each layer.

To exploit this, D2O uses an Inverse Variance Softmax strategy. It calculates the variance of attention scores for each layer; layers with low variance (dense attention) are allocated a larger cache budget, while layers with high variance (sparse attention) receive a smaller budget. This minimizes information loss where it matters most.

2. Token-Level: Compensation Mechanism

Instead of permanently discarding tokens, D2O implements a "compensation mechanism" via Dynamic Token Merging. D2O merges similar tokens instead of permanently discarding them.

Figure 2: Existing methods (a) vs. D2O (b) at token level.

When a token is slated for eviction, D2O checks its similarity to the tokens remaining in the cache. If the similarity exceeds a threshold—maintained by an Exponential Moving Average (EMA) to filter out noise—the evicted token is merged into its most similar counterpart using a weighted average. This preserves the "potential value" of evicted tokens at a minimal computational cost.

Datasets

The researchers evaluated D2O across a wide range of benchmarks and architectures (Llama-2/3, Falcon, Mistral):

  • Reasoning: GSM8K (math), CoQA (QA), and TruthfulQA.
  • Long-Context: LongBench, which includes tasks for single/multi-document QA, summarization, and code completion.
  • Retrieval: The Needle In A Haystack test, which measures a model's ability to find a specific fact hidden in a massive document.
  • Language Modeling: PG-19 for measuring perplexity over long sequences.
  • Dialogue: MT-bench for multi-turn conversation quality.

Key Insights and Ideas

  • Attention Density is Depth-Dependent: The finding that shallow layers engage more with syntactic structures (requiring more context) while deeper layers target specific semantic knowledge (allowing for more sparsity) is a crucial justification for non-uniform cache allocation. Variance of attention scores across layers for different models.

  • Merging as a Safety Net: Traditional eviction is a "hard" operation. By treating it as a "soft" merging operation, D2O mitigates the risk of losing a token that might be critical for a future reasoning step.

  • EMA Thresholding Prevents Feature Dispersion: Simply merging everything would lead to "feature dispersion" (blurring the distinct meanings of tokens). The EMA threshold ensures that only truly redundant or highly similar information is consolidated.
Figure 4: Variances of attention score across different layers for various models.

Key Results

  • Throughput Boost: D2O achieved up to 3.04x higher throughput than the original full-cache model by enabling larger batch sizes within the same memory constraints.
  • Superior Accuracy: On the LongBench benchmark with only a 20% KV cache budget, D2O consistently outperformed state-of-the-art baselines like H2O and StreamingLLM. For example, on the Llama-3-8B backbone, D2O maintained performance much closer to the "Full Cache" model than its competitors.
  • Fact Retrieval: In the "Needle In A Haystack" test, D2O showed significantly better retrieval capabilities at 100k+ context lengths compared to eviction-only methods, which often "lost the needle" due to aggressive pruning.

Limitations and Future Work

  • Extreme Compression: While D2O performs well at moderate compression (e.g., 20% budget), very high compression ratios still lead to performance degradation.
  • Integration with Other Techniques: The current work focuses on the KV cache structure itself. The authors suggest that future work could integrate D2O with other compression methods like quantization (reducing bit-precision) or distillation to push efficiency even further.
  • Computational Overhead: While the token merging is designed to be lightweight, it does introduce a small amount of additional computation during the prompt encoding phase compared to simple eviction.

Summary updated

BibTeX
@inproceedings{wan2025d,
  author = {Zhongwei Wan and Xinjian Wu and Yu Zhang and Yi Xin and Chaofan Tao and Zhihong Zhu and Xin Wang and Siqi Luo and Jing Xiong and Longyue Wang and Mi Zhang},
  title = {{D2O: Dynamic Discriminative Operations for Efficient Long-Context Inference of Large Language Models}},
  booktitle = {International Conference on Learning Representations},
  year = {2025},
  url = {https://openreview.net/forum?id=sHKRFzMRfe},
}
Figure