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

Overview

D2O (Dynamic Discriminative Operations) is a novel, "plug-and-play" Key-Value (KV) cache compression method designed to significantly reduce the memory footprint of Large Language Models (LLMs) during long-context inference. By dynamically allocating cache budgets across layers and utilizing a similarity-based merging strategy for tokens, D2O achieves over 3x throughput improvements while maintaining high generation quality—all without requiring any model fine-tuning.

The Bottleneck: KV Cache in Long-Context LLMs

As LLMs are increasingly tasked with processing massive sequences—such as entire books, codebases, or extensive chat histories—they encounter a severe hardware bottleneck: the KV cache. This cache stores previous token representations to accelerate text generation, but its memory requirements scale linearly with sequence length. For instance, a 30B parameter model processing a single batch can require up to 180 GB of KV cache, rapidly exceeding standard GPU memory limits.

Existing solutions typically rely on "eviction" strategies, which discard less important tokens based on their attention scores. However, these methods suffer from two critical flaws:

  1. Layer-Level Blindness: They apply uniform compression across all layers. In reality, shallow layers (handling syntax) and deep layers (handling semantics) exhibit vastly different attention patterns.
  2. Token-Level Information Loss: Once a token is evicted, its context is permanently destroyed. This leads to "context loss," causing the model to forget earlier details or hallucinate.

The D2O Approach

To address these flaws, D2O operates at two distinct levels to minimize information loss while maximizing compression.

1. Layer-Level: Dynamic Budget Allocation

The authors observed that shallow layers exhibit "dense" attention (attending broadly to many tokens), while deep layers show "sparse" or "staircase" patterns (focusing narrowly on specific segments).

D2O introduces an inverse variance softmax metric to evaluate this density dynamically. Layers with low variance (dense attention) are allocated a larger share of the overall KV cache budget. Conversely, layers with high variance (sparse attention) can be compressed much more aggressively without degrading performance.

2. Token-Level: Compensation Mechanism

Instead of permanently discarding tokens that fall below the attention threshold, D2O implements a "recall and merge" strategy.

Comparison of standard eviction versus D2O's similarity-based merging.

The system maintains an Exponential Moving Average (EMA) threshold to evaluate token similarity. When a token is slated for eviction, D2O checks if it is sufficiently similar to a token already preserved in the cache. If a match is found, the information is merged (using a weighted merging technique inspired by Graph Attention Networks) rather than deleted. This ensures the "essence" of evicted tokens survives within the remaining cache.

Crucially, D2O is entirely non-trainable, meaning it can be seamlessly integrated into existing open-source models like Llama-3 or Mistral without expensive retraining.

Key Insights

  • Attention is Not a Monolith: Visualizing layer-wise attention reveals that shallow layers act like a broad-spectrum radar, while deep layers act like a sniper scope. Forcing a uniform compression ratio across both is inherently inefficient.
  • Merging Outperforms Pruning: By treating eviction as a clustering or merging problem, D2O retains a "ghost" of discarded information, preventing the model from losing the thread of long inputs.
  • Dynamic Thresholding: Utilizing an EMA for similarity thresholds allows the model to adapt to the specific density and style of the current input text in real-time.

Evaluation and Results

The authors evaluated D2O across a rigorous suite of benchmarks, including GSM8K, CoQA, TruthfulQA, LongBench, and PG-19.

  • Throughput: D2O achieves up to 3.04x higher throughput compared to full-cache models by enabling larger batch sizes and drastically reducing memory overhead.
  • Memory Efficiency: The model maintains high performance while retaining only 20% of the original KV cache.
  • Retrieval Accuracy: On the challenging Needle In A Haystack test (8192 budget, 100k context), D2O achieved 91.88% accuracy, significantly outperforming competing methods like H2O (72.34%) and StreamingLLM (51.34%).
  • Reasoning Preservation: Unlike many compression methods that degrade a model's logical capabilities, D2O actually surpassed the full-cache model's performance on TruthfulQA. This suggests the similarity-based eviction strategy may effectively filter out input "noise."

Limitations and Future Work

While highly effective, D2O presents a few areas for future refinement:

  • Computational Overhead: The similarity calculation and merging process introduce a minor latency penalty per token compared to naive eviction, though this is heavily offset by the massive gains in batch size capacity.
  • Synergistic Integration: Future research could combine D2O with orthogonal compression techniques, such as quantization (reducing bit-precision) or distillation, to push efficiency limits even further.
  • Architectural Generalization: While proven on standard architectures like Llama, Mistral, and Falcon, further study is required to evaluate D2O's interaction with alternative attention mechanisms, such as sliding window or linear attention.

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