TL;DR
D-LLM is a novel dynamic inference framework that allows Large Language Models (LLMs) to adaptively skip transformer layers on a per-token basis. By pairing this with a clever KV-cache eviction strategy, it reduces computational costs and memory overhead by up to 50% while maintaining competitive performance across diverse reasoning and generation tasks.
The Problem: "One-Size-Fits-All" Computation
Standard Large Language Models are computationally "flat"—they spend the exact same amount of processing power on a simple article like "the" as they do on a complex, context-heavy word like "quantum." This uniform approach is highly inefficient, especially when deploying models on resource-constrained hardware like edge devices or mobile phones.
While techniques like pruning and quantization reduce model size, they are usually static; once the model is compressed, it stays that way for every input. Dynamic inference (skipping layers for "easy" tokens) is a promising alternative, but it faces a major technical hurdle in LLMs: the KV-cache. Modern LLMs store "Key" and "Value" vectors for every token to avoid recomputing them during generation. If a token skips a layer, it leaves a "hole" in the cache, breaking the attention mechanism for all subsequent tokens.
Methods: Dynamic Inference and Cache Eviction
The authors introduce D-LLM, which treats the transformer stack like a series of optional checkpoints, dynamically allocating compute where it is needed most.
- Dynamic Decision Modules: The authors insert a tiny, two-layer MLP before each transformer layer. This module evaluates the current token embedding and outputs a binary decision to either "execute" the next layer or "skip" it entirely.
- Differentiable Training: To train these discrete "yes/no" modules, the framework utilizes the Gumbel-Softmax trick and a Straight-Through Estimator. This allows the model to learn which tokens require deeper processing through standard backpropagation.
- KV-Cache Eviction Strategy: This is the core innovation solving the cache "hole" problem. Instead of trying to fill the gaps left by skipped layers, D-LLM simply evicts those entries. It uses a sparse causal mask to ensure that subsequent tokens only attend to the KV pairs that were actually computed. To maintain stability, the first few tokens (known as "attention sinks") are always executed to preserve global context.
- Parameter-Efficient Adaptation: Rather than retraining a massive model from scratch, the authors use LoRA (Low-Rank Adaptation) to train the decision modules and fine-tune the model for dynamic execution.
Key Insights and Ideas
- Not All Tokens are Equal: Visualizations of the model's routing decisions show that it naturally learns to skip layers for articles and punctuation, while dedicating more depth to nouns, verbs, and mathematical symbols.
- Layer Specialization: Interestingly, different layers specialize in different grammatical categories. For example, some layers are heavily utilized for numbers and math symbols, while others are "subject-heavy."
- The "Aha" Moment on Memory: Most dynamic methods focus solely on reducing FLOPs (compute). D-LLM’s eviction strategy means that skipped layers don't just save time; they save physical memory. By not storing KV pairs for skipped layers, the memory footprint of the KV-cache—a major bottleneck for long-context windows—is slashed significantly.
- Task-Adaptive Depth: The model automatically adjusts its "average depth" based on task difficulty. Complex math problems consistently trigger more layer executions than simple commonsense questions.
Key Results
The researchers evaluated D-LLM using LLaMA2-7B and LLaMA3-8B across nine benchmarks, including Q&A (Alpaca, SAMSum), Math Solving (GSM8K, MaWPS), and Commonsense Reasoning (BoolQ, PIQA, SIQA, OBQA, MMLU).
- Massive Efficiency Gains: D-LLM reduced computational costs (FLOPs) and KV-cache storage by 45% on Q&A and math tasks, and by up to 50% on commonsense reasoning.
- Superior Trade-offs: Compared to other state-of-the-art methods like Mixture-of-Depths (MoD) and Shortened-LLaMA, D-LLM achieved better accuracy at the same computational budget.
- Performance Retention: On LLaMA3-8B, D-LLM actually outperformed the baseline LoRA-tuned model on several datasets while using significantly fewer resources.
- Low Overhead: The decision modules are incredibly lightweight, adding only about 0.9% to the total parameter count and costing a mere 0.1ms of latency per block.
Limitations and Future Work
- Granularity: The current approach operates strictly at the token level. The authors suggest that exploring different granularities, such as skipping entire sentences or specific sub-tasks, could yield further benefits.
- Generalizability: While tested on nine benchmarks, the generalizability of these dynamic policies to broader, "in-the-wild" tasks still requires further verification.
- Hyperparameter Sensitivity: The balance between speed and accuracy is controlled by a hyperparameter. Finding the "sweet spot" requires careful tuning for different hardware targets.
Summary updated