TL;DR
Dynamic Memory Compression (DMC) is an innovative method that adaptively compresses the Key-Value (KV) cache of pre-trained Large Language Models (LLMs) during inference without adding any new parameters. By retrofitting models like Llama 2 via a brief continued pre-training phase, DMC achieves up to a 7× throughput increase on an NVIDIA H100 GPU while fully preserving downstream task performance.
What Problem Does This Paper Address?
Transformer-based LLMs are notoriously memory-bound during auto-regressive generation. To avoid recomputing past token representations at every step, these models maintain a "KV cache." However, this cache grows linearly with both the sequence length and the batch size, creating a massive memory bottleneck. This bottleneck severely limits the maximum batch size and context length a system can handle.
Existing solutions fall short in various ways. Grouped Query Attention (GQA) reduces the number of KV heads but causes significant performance degradation if applied to a pre-trained model post-hoc. On the other hand, token eviction policies (like $\text{H}_2\text{O}$ or TOVA) dynamically drop less important tokens from the cache. While they save memory, dropping tokens hurts accuracy, and these methods are fundamentally incompatible with highly optimized attention kernels like FlashAttention because they require materializing the full attention matrix to calculate eviction scores.
Methods
DMC introduces an online, adaptive approach to KV cache compression. Instead of dropping tokens entirely or applying a uniform compression ratio across the entire model, DMC dynamically decides at each time step whether to append the new token's KV values to the cache or accumulate them by computing a weighted average with the top-most item already in the cache.
To avoid adding new parameters to the model, the authors cleverly re-purpose the very first neuron of the key ($k_t$) and query ($q_t$) vectors to predict a binary decision score ($\alpha_t$) and an importance weight ($\omega_t$).
Training a model to make these discrete append/accumulate decisions introduces two major technical challenges:
- Gradient Estimation: Discrete decisions are non-differentiable. DMC bypasses this during training by using a continuous relaxation via the Gumbel-sigmoid trick.
- Parallelization Mismatch: During inference, the cache physically shrinks over time. However, training must be parallelized across the entire token sequence. To simulate inference-time compression during parallel training, the authors introduce a custom additive attention mask that blocks queries from attending to intermediate, non-final states within a compressed segment.
A global, one-sided loss function ($\ell_{CR}$) nudges the model to meet a target overall compression ratio while giving individual heads and layers the freedom to learn their own optimal compression strategies.
Datasets
- Retrofitting / Continued Pre-training: The models were fine-tuned on a diverse corpus containing sections of The Pile (including BookCorpus2, Books3, Pile-CC, Gutenberg, NIH ExPorter, OpenWebText2, Stack Exchange, and Wikipedia), ArXiv, Pushshift Reddit, mC4, Common Crawl dumps (2017-2023), CC-News, PubMed Central, BigScience Workshop datasets, and The Stack.
- Evaluation Benchmarks: Downstream performance was measured using MMLU (for factuality and general knowledge), HumanEval (for Python code generation), and a Commonsense QA (CS-QA) suite averaged across six tasks (PIQA, BoolQ, Arc-C, Arc-E, HellaSwag, and WinoGrande).
Key Insights and Ideas
- Layer-wise Preferences: The model naturally learns to compress deeper layers much more aggressively than the initial layers. The authors hypothesize that early layers lack the rich contextual information needed to make safe compression decisions.
- Compounding Benefits with GQA: DMC can be layered directly on top of architectures already using GQA. For example, Llama 2 70B is pre-trained with 8× GQA; adding a 2× DMC compression layer yields a massive 16× total cache reduction with zero performance loss.
- Linguistic Alignment: In the earliest layers, DMC naturally learns to merge subwords back into complete words, effectively reversing the tokenizer's fragmentation for highly redundant tokens.
- Logarithmic Scaling: The achieved compression ratio scales logarithmically with sequence length, meaning that the longer the input text, the more efficiently DMC compresses it.
Key Results
- Throughput Gains: At 4× cache compression, DMC increases inference throughput by 350% to 390% for Llama 2 7B and 13B on NVIDIA H100 and A100 GPUs. At 8× compression, throughput gains reach up to 700%.
- Performance Retention: At 4× compression, DMC maintains virtually identical downstream performance to the baseline Llama 2 models, significantly outperforming post-hoc GQA, $\text{H}_2\text{O}$, and TOVA.
- Sample Efficiency: DMC requires very little data to retrofit models—only ~2% of the original pre-training data volume is needed for 2× compression, and ~8% for 8× compression.
Limitations and Future Work
- Retrofitting Only: The authors note that trying to train an LLM with DMC from scratch failed. They describe a "vicious cycle" where poor initial token representations lead to bad segmentation decisions, which in turn prevent the model from learning good representations. Future work will explore Expectation-Maximization (EM) style training to alternate between representation modeling and cache segmenting.
- Hardware Implementation Complexity: Storing variable-length KV caches across different heads requires dynamic memory allocation tools like PagedAttention to avoid memory fragmentation and overhead.
Summary updated