NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Draft on the Fly: Adaptive Self-Speculative Decoding using Cosine Similarity

Michael R. Metel, Peng Lu, Boxing Chen, Mehdi Rezagholizadeh, Ivan Kobyzev

Introduction

Large Language Models (LLMs) have revolutionized natural language processing, yet their deployment remains hindered by the high latency of autoregressive inference. In this paradigm, tokens are generated sequentially, often bottlenecked by memory bandwidth rather than computational capacity. Speculative decoding has emerged as a promising solution, utilizing a smaller "draft" model to predict multiple tokens that a larger "target" model then verifies in parallel. However, traditional speculative decoding introduces its own set of challenges, including the need for specialized, fine-tuned draft models and complex offline optimization processes to determine which layers to skip in self-speculative setups.

To address these limitations, researchers have introduced Adaptive Self-Speculative Decoding (ASD). This "plug-and-play" method eliminates the need for separate draft models or time-consuming pre-computation by dynamically creating a draft model on the fly based on the input context.

Methodology: The Adaptive Draft Model Generator (ADMG)

The core innovation of ASD is the Adaptive Draft Model Generator (ADMG). Unlike static methods that rely on a fixed set of skipped layers determined through hours of Bayesian optimization, ADMG tailors the draft architecture to each specific input prompt during the initial processing phase.

Cosine Similarity Thresholding

The primary mechanism for layer selection is Cosine Similarity (CS) Thresholding. During the prompt processing (prefill) stage, the model calculates the cosine similarity between hidden states before and after each attention layer. High cosine similarity indicates that a layer's transformation is nearly an identity mapping for the current context, suggesting the layer is redundant and can be safely bypassed during the drafting phase.

The ADMG Algorithm

The ADMG algorithm integrates three heuristic rules to construct the draft model:

  1. CS Thresholding: Attention layers with an Average Cosine Similarity (ACS) above a predefined threshold $\alpha$ (typically 0.985) are removed.
  2. Periodic Removal: Every $m^{th}$ attention and MLP layer is removed to provide a baseline structural reduction.
  3. Safety Buffer: The final $n$ layers are always preserved, as they are critical for projecting internal representations back into the vocabulary space.

This approach ensures that the "calibration" is essentially free, occurring during the standard inference flow, and is perfectly aligned with the task at hand.

Experimental Evaluation

The researchers evaluated ASD using Llama-2-13B, Llama-2-13B-Chat, and CodeLlama-13B across several benchmarks, including CNN/DM and XSum for summarization, and HumanEval for code generation.

Performance on Summarization and General Tasks

ASD demonstrated significant inference speedups, ranging from 1.1x to over 1.4x compared to standard autoregressive generation. As shown in Table 1, ASD remains highly competitive with the state-of-the-art "Draft & Verify" (D&V) method, despite requiring zero offline optimization time.

Table 1: Speedup comparison between the proposed ASD and the Draft & Verify (D&V) baseline on Llama-2-13B models across CNN/DM and XSum datasets.

Superiority in Specialized Domains

One of the most compelling results was observed with CodeLlama-13B. In this domain, ASD actually outperformed the D&V baseline. This suggests that the adaptive nature of ASD is particularly beneficial for out-of-distribution tasks where static calibration sets might fail to capture the specific redundancies of the input.

Table 2: Speedup performance on CodeLlama-13B, where ASD outperforms the D&V baseline.

Key Insights

  • Layer Redundancy: The research confirms that attention layers are generally more redundant and computationally expensive than MLP layers. In Llama-2-13B, attention layers take approximately 1.74x longer to compute, making them ideal targets for skipping.
  • Context-Aware Calibration: By using the input prompt as the calibration set, ASD avoids the "friction" of traditional methods. While the baseline D&V method requires roughly 11 hours of optimization per model, ASD is truly "plug-and-play" with zero setup time.
  • Heuristic Efficiency: Surprisingly, simple heuristic rules based on cosine similarity perform on par with complex black-box optimization techniques, highlighting the inherent redundancy in modern transformer architectures.

Conclusion

Adaptive Self-Speculative Decoding (ASD) offers a streamlined, efficient path to accelerating LLM inference. By leveraging real-time cosine similarity metrics to skip redundant layers, it achieves state-of-the-art speedups without the overhead of specialized draft models or extensive pre-computation. This makes it an ideal candidate for production environments where rapid deployment and adaptability are paramount.

Summary updated

BibTeX
@inproceedings{metel2024draft,
  author = {Michael R. Metel and Peng Lu and Boxing Chen and Mehdi Rezagholizadeh and Ivan Kobyzev},
  title = {{Draft on the Fly: Adaptive Self-Speculative Decoding using Cosine Similarity}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2024},
  doi = {10.18653/v1/2024.findings-emnlp.124},
}
Figure