Abstract
Speculative decoding accelerates Large Language Model (LLM) inference by using a small "drafter" model to predict tokens that are subsequently verified by a larger "target" model. However, as vocabularies scale to 128k tokens and beyond, the drafter's final output projection becomes a significant computational bottleneck. We present DynaSpec, a context-aware dynamic shortlisting mechanism that uses a lightweight meta-classifier to route contexts to specific token clusters. This approach eliminates the output layer bottleneck while recovering 98.4% of the full-vocabulary acceptance rate. DynaSpec achieves up to a 2.25x throughput increase on Llama-3-8B, particularly excelling in technical domains like code generation where static vocabulary methods often fail.
The Bottleneck of Massive Vocabularies
Speculative decoding is a standard technique for speeding up LLM inference. However, modern models like Llama-3 and Qwen-2 utilize massive vocabularies ($|V| \ge 128k$). In small drafter models, the final linear projection to the vocabulary space—an $O(|V|d)$ operation—can consume more time than all preceding transformer layers combined.
Previous attempts to mitigate this involved static vocabulary truncation (shortlisting the most frequent tokens). While efficient, static lists are brittle; they frequently miss domain-specific or rare tokens (e.g., code identifiers), leading to draft rejections by the target model and neutralizing speed gains.
Methodology: Dynamic Shortlisting
DynaSpec replaces static truncation with a "coarse-to-fine" dynamic routing system designed to be both accurate and computationally efficient.
1. Vocabulary Partitioning and Routing
The entire vocabulary is partitioned into $M$ disjoint clusters using spherical k-means on the target model's LM-head weights. This ensures semantic similarity within clusters. A tiny MLP meta-classifier observes the current context and predicts which clusters are most likely to contain the next token. The drafter then only computes logits for tokens within these selected clusters.
2. Position-Aware Budgeting
DynaSpec employs a "position-aware budget" strategy. Since speculative decoding is sequential—a single rejection invalidates all subsequent tokens—the system allocates a larger cluster budget for the initial tokens in a draft sequence. The budget decays for later tokens, optimizing the trade-off between draft accuracy and computational cost.
3. Systems Optimization
To prevent the meta-classifier from introducing latency, it runs on a separate CUDA stream, overlapping its execution with the drafter's attention layers. Furthermore, a custom fused kernel for the shortlisted matrix multiplication minimizes memory overhead during the final projection.
Experimental Evaluation
DynaSpec was evaluated using Llama-3-8B and Qwen-2-7B across several benchmarks, including Spec-Bench (MT, RAG, QA, Math, Summarization) and HumanEval (Code).
Key Results
- Acceptance Rate Recovery: DynaSpec recovered 98.4% of the acceptance rate of a full-vocabulary drafter, significantly outperforming static methods which typically reach ~93.6%.
- Throughput Gains: As shown in the performance data, DynaSpec achieves a 2.25x throughput increase over the target-only baseline, a notable improvement over the 1.93x gain seen with static approaches.
- Domain Robustness: The most dramatic improvements occurred in the "Code" dataset. While static lists often fail to capture unique identifiers, DynaSpec's context-awareness allows it to dynamically locate rare tokens, resulting in a substantial tokens/sec improvement over static baselines.
Conclusion
DynaSpec demonstrates that context-aware dynamic routing is a viable solution to the vocabulary bottleneck in speculative decoding. By focusing computation on semantically relevant clusters, it maintains high acceptance rates across diverse domains without the overhead of full-vocabulary projection. Future work may explore integrating these routed sparse heads directly into the training phase of small language models to further optimize inference pipelines.
Summary updated