要点速览
- 小模型提议、大模型单次 forward pass 并行验证,输出分布与大模型独自生成一致,属无损加速。
- vLLM 加 draft/target 双 runner,改 scheduler/memory manager,兼容 continuous batching。
- 三类方案:独立 draft model、prompt lookup(n-gram)、附加头式 Medusa/EAGLE/MLPSpeculator。
- 低 QPS 最高加速:draft 方案 1.5x、n-gram 2.8x;高 QPS 反而减速 1.4x-1.8x。
- 路线图为 dynamic speculative decoding:按负载与 acceptance rate 调整提议 token 数。
解决什么问题
LLM 推理是自回归的:每生成一个 token 都需要一次完整的 forward pass,逐 token 串行导致延迟居高不下。这篇文章回答的问题是:vLLM 如何实现推测解码(小模型提议、大模型单次验证多个 token)来降低 token 生成延迟,它在 vLLM 系统中如何落地,以及在什么负载下有效、什么情况下反而变慢。
核心思路
核心是 draft+target 双模型协作:小 draft model 自回归提议若干候选 token,大 target model 在一次 forward pass 中并行验证,确认正确的 token、纠正第一个错误位置。vLLM 将该机制融入 continuous batching 架构,新增 Draft Runner 与 Target Runner,并改造 scheduler(单次 pass 处理多个 token slot)和 memory manager(同时管理两个模型的 KV cache)。文章随后梳理三类提议来源——独立 draft model、prompt lookup(n-gram)、附加层/头式的 Medusa/EAGLE/MLPSpeculator——给出代码级用法示例,并以 dynamic speculative decoding 作为高 QPS 场景的演进方向。
关键要点
- 推测解码为何既加速又无损 — 传统自回归生成中每个 token 需要一次独立 forward pass(如生成 T1、T2、T3 需三次)。推测解码让小 draft model 逐个提议 token,大 target model 在单次 forward pass 中并行验证:确认正确的、纠正错误的。文中示例:draft 提议 5 个 token,第 3 个出错,则该步产出前 3 个正确 token。由于最终输出与只用大模型的分布一致,这是无损的加速方法。
- vLLM 系统实现:双 runner + 架构改造 — vLLM 在 continuous batching 框架内加入两个组件:Draft Runner 执行小模型提议候选 token,Target Runner 运行大模型完成验证。为支持推测解码,scheduler 被改造为在单次 forward pass 中处理多个 token slot,实现多 token 的同时生成与验证;memory manager 则要同时管理 draft 与 target 两个模型的 KV cache,保证处理过程平滑。
- 三类推测解码方案 — 一是独立 draft model(最常用),如用 Llama 68M 为 Llama 2 70B 提议 token。二是 prompt lookup decoding(n-gram 匹配):以 prompt 中已有的 n-gram 为查找键提议后续 token,无需小模型,适合摘要、问答等答案与 prompt 重叠度高的场景。三是 Medusa/EAGLE/MLPSpeculator:在大模型自身上添加额外的层或头,一次 forward pass 预测多个位置的 token,免去独立 draft model。
- draft model 选型是关键难题 — draft model 必须足够小以避免显著开销,又足够准确以提供有意义的加速,两者需要权衡。此外推测解码要求 draft 与 target 共享同一 vocabulary——例如 Llama 3 因 vocabulary 大小差异而难以找到合适的 draft model,这正凸显了 draft-model-free 方法(n-gram、附加头)的价值。
- 低 QPS 加速、高 QPS 反而减速 — 在 QPS=1 的 Llama3-70B(4xH100)测试中,draft model 方案在 ShareGPT 上带来最高 1.5x 加速,n-gram 方案在 CNN/DailyMail 摘要数据集上最高 2.8x。但高 QPS 下系统已 compute-bound,提议与验证引入的额外计算反而拖慢系统:ShareGPT 上出现 1.4x slowdown,CNN/DailyMail 上达 1.8x slowdown。
- 路线图:dynamic speculative decoding — 为克服高 QPS 下的退化,vLLM 正在实现动态推测解码(对应论文 arXiv:2406.14066 与 RFC #4565):根据系统负载和 draft model 准确率自动调整每步提议的 token 数量。负载高时缩短提议长度,而平均 token acceptance rate 高时缩短幅度更小。未来用户可无条件开启推测解码,而不必担心它拖慢系统。
- 用法:三个关键配置 — 通过 LLM(...) 构造参数启用:speculative_model 指定提议来源(draft 模型名或 [ngram]),num_speculative_tokens 设定每步提议数(示例为 5),n-gram 方案还可配 ngram_prompt_lookup_max/min。当 draft 与 target 的张量并行度不同时可设 speculative_draft_tensor_parallel_size,例如 draft 用 1、target 用 4,让小模型占用更少资源、减少通信开销。
关键数据
| 指标 | 数值 | 条件 |
|---|---|---|
| token 生成加速比(draft model 方案) | 最高 1.5x | QPS=1,Llama3-70B,ShareGPT 数据集,4xH100,draft model 为 turboderp/Qwama-0.5B-Instruct |
| token 生成加速比(n-gram 方案) | 最高 2.8x | QPS=1,Llama3-70B,CNN/DailyMail 摘要数据集,4xH100,n-gram 提议 |
| 高 QPS 下的性能退化(ShareGPT) | 1.4x slowdown | 高 QPS 负载,Llama3-70B,ShareGPT 数据集,4xH100 |
| 高 QPS 下的性能退化(CNN/DailyMail) | 1.8x slowdown | 高 QPS 负载,Llama3-70B,CNN/DailyMail 数据集,4xH100 |
| draft/target 模型规模组合示例 | draft 68M,target 70B | 文中给出的典型组合:用 Llama 68M 为 Llama 2 70B 提议 token |
“up to 1.5x Speedup at QPS=1 Llama3-70B on ShareGPT with 4xH100 using draft model (turboderp/Qwama-0.5B-Instruct)”
“Similarly, prompt lookup decoding has shown speedups of up to 2.8x when applied to summarization datasets, such as CNN/DailyMail.”
“As high QPS, we see 1.4x slowdown Llama3-70B on ShareGPT with 4xH100, 1.8x slowdown Llama3-70B on CNN Dailymail with 4xH100”
前置知识
LLM 自回归(autoregressive)解码的基本流程:每个 token 一次 forward pass · KV cache 的概念与作用 · continuous batching(连续批处理)机制 · tensor parallelism(张量并行)与通信开销的基本概念 · QPS、吞吐(throughput)与延迟(latency)之间的关系 · draft model 与 target model 的分工(文中会展开,但预先了解更顺畅)
术语表
Speculative Decoding: 推测解码:用小模型快速提议多个候选 token,再由大模型在一次 forward pass 中并行验证,输出分布与只用大模型一致,是无损的加速方法。Draft Model: draft 模型:体积小、推理快的模型,负责自回归地逐个提议候选 token。Target Model: target 模型:被服务的原始大模型,负责在单次 forward pass 中验证并纠正 draft 的提议。Continuous Batching: 连续批处理:vLLM 的核心调度机制,将不同请求的 token 拼入同一 batch 持续处理,以提升系统吞吐。Prompt Lookup Decoding: 提示查找解码(n-gram matching):用 prompt 中已出现的 n-gram 作为查找键来提议后续 token,无需 draft model,适合摘要与问答等答案与 prompt 高度重叠的场景。Medusa: 在大模型上额外添加多个解码头(heads),一次 forward pass 同时预测多个未来位置的 token,无需独立 draft model。EAGLE: 一种基于 hidden states 反馈的推测解码方法,在 target 模型上附加轻量 drafter,是本系列后续文章的主角。MLPSpeculator: 与 Medusa 类似的附加式推测解码结构,通过额外的 MLP 层预测多个未来 token。KV Cache: 推理时缓存的 key/value 张量,避免对历史 token 重复计算注意力;推测解码需要同时管理 draft 与 target 两个模型的 KV cache。QPS: Queries Per Second,每秒请求数;文中用低/高 QPS 区分延迟敏感型与计算饱和型两种负载。Acceptance Rate: 接受率:draft 提议的 token 被 target 接受的比例,直接决定推测解码的加速幅度。Dynamic Speculative Decoding: 动态推测解码:根据系统负载与 draft 准确率自适应调整每步提议 token 数量的研究方向,用于避免高 QPS 下的性能退化。
How Speculative Decoding Boosts vLLM Performance by up to 2.8x
Speculative decoding in vLLM is a powerful technique that accelerates token generation by leveraging both small and large models in tandem. In this blog, we’ll break down speculative decoding in vLLM, how it works, and the performance improvements it brings.
This content is based on a session from our bi-weekly vLLM Office Hours, where we discuss techniques and updates to optimize vLLM performance. You can view the session slides here. If you prefer watching, you can view the full recording on YouTube. We’d love to see you attend future sessions - please register!
An Introduction to Speculative Decoding
Speculative decoding (Leviathan et al., 2023) is a key technique in reducing latency during token generation in large language models (LLMs). This approach leverages smaller models to handle simpler token predictions while utilizing larger models to verify or adjust those predictions. By doing this, speculative decoding accelerates generation without sacrificing accuracy, making it a lossless yet highly efficient method for optimizing LLM performance.
Why can speculative decoding reduce latency? Traditionally, LLMs generate tokens one at a time in an autoregressive manner. For example, given a prompt, the model generates three tokens T1, T2, T3, each requiring a separate forward pass. Speculative decoding transforms this process by allowing multiple tokens to be proposed and verified in one forward pass.
Here’s how the process works:
- Draft Model: A smaller, more efficient model proposes tokens one by one.
- Target Model Verification: The larger model verifies these tokens in a single forward pass. It confirms correct tokens and corrects any incorrect ones.
- Multiple Tokens in One Pass: Instead of generating one token per pass, this method processes multiple tokens simultaneously, reducing latency.
As shown in the picture above, the draft model proposes five tokens: ["I", "like", "cooking", "and", "traveling"]. These are then forwarded to the target model for parallel verification. In this example, the third token, "cooking" (should be "playing"), was proposed inaccurately. As a result, only the first three tokens, ["I", "like", "playing"], are generated in this step.
By using this approach, speculative decoding speeds up token generation, making it an effective method for both small-scale and large-scale language model deployments.
How Speculative Decoding Works in vLLM
In vLLM, speculative decoding is integrated with the system’s continuous batching architecture, where different requests are processed together in a single batch, enabling higher throughput. vLLM uses two key components to implement this:
- Draft Runner: This runner is responsible for executing the smaller model to propose candidate tokens.
- Target Runner: The target runner verifies the tokens by running the larger model.
vLLM's system is optimized to handle this process efficiently, allowing speculative decoding to work seamlessly with continuous batching, which increases the overall system performance.
Diagram illustrating how the draft and target runners interact within the vLLM batching system.
To implement speculative decoding in vLLM, two crucial components had to be modified:
- Scheduler: The scheduler was adjusted to handle multiple token slots within a single forward pass, enabling the simultaneous generation and verification of several tokens.
- Memory Manager: The memory manager now handles the KV cache for both the draft and target models, ensuring smooth processing during speculative decoding.
System architecture of speculative decoding in vLLM.
Types of Speculative Decoding Supported in vLLM
vLLM supports three types of speculative decoding, each tailored to different workloads and performance needs:
Draft Model-Based Speculative Decoding
This is the most commonly used form of speculative decoding, where a smaller model predicts the next tokens, and a larger model verifies them. A common example would be using a Llama 68M model to predict tokens for a Llama 2 70B model. This approach requires careful selection of the draft model to balance accuracy and overhead.
Choosing the correct draft model is essential for maximizing the efficiency of speculative decoding. The draft model needs to be small enough to avoid creating significant overhead but still accurate enough to provide a meaningful performance boost.
However, selecting the right draft model can be challenging. For example, in models like Llama 3, finding a suitable draft model is difficult due to differences in vocabulary size. Speculative decoding requires that the draft and target models share the same vocabulary, and in some cases, this can limit the use of speculative decoding. Therefore, in the following sections, we introduce several draft-model free speculative decoding methods.
Prompt Lookup Decoding
An example of prompt lookup decoding. Given the prompt, we build all 2-grams as the lookup key. The values are the three tokens following the lookup key. During generation, we will check if the current 2-gram matches any key. If so, we will propose the following tokens with the value.
Otherwise known as n-gram matching, this approach is effective for use cases like summarization and question-answering, where there is a significant overlap between the prompt and the answer. Instead of using a small model to propose tokens, the system speculates based on the information already available in the prompt. This works particularly well when the large model repeats parts of the prompt in its answers.
Medusa/Eagle/MLPSpeculator
Picture from https://github.com/FasterDecoding/Medusa.
In the example, three heads are used to propose tokens for the following three positions. Head 1 is proposing ["is", "\'", "the"] for the first position. Head 2 is proposing ["difficult", "is", "\'"] for the second position. Head 3 is proposing ["not", "difficult", "a"] for the third position. All heads take the output of the last transformer block as the input.
In this method, additional layers (or heads) are added to the large model itself, allowing it to predict multiple tokens in a single forward pass. This reduces the need for a separate draft model, instead leveraging the large model’s own capacity for parallel token generation. Though preliminary, this method shows promise for improving efficiency as more optimized kernels are developed.
Speculative Decoding Performance Insights: Speedups and Trade-offs
Speculative decoding offers significant performance benefits in low-QPS (queries per second) environments. For example, in testing on the ShareGPT dataset, vLLM demonstrated up to a 1.5x speedup in token generation when using draft model-based speculative decoding. Similarly, prompt lookup decoding has shown speedups of up to 2.8x when applied to summarization datasets, such as CNN/DailyMail.
Performance comparison showing spec decode delivering up to 1.5x Speedup at QPS=1 Llama3-70B on ShareGPT with 4xH100 using draft model (turboderp/Qwama-0.5B-Instruct) and up to 2.8x Speedup at QPS=1 Llama3-70B on CNN Dailymail with 4xH100 using n-grams.
However, in high-QPS environments, speculative decoding may introduce performance trade-offs. The extra compute required to propose and verify tokens can sometimes slow down the system when it is already compute-bound, as seen when the number of requests per second increases. In such cases, the overhead of speculative decoding can outweigh its benefits, leading to reduced performance.
As high QPS, we see 1.4x slowdown Llama3-70B on ShareGPT with 4xH100, 1.8x slowdown Llama3-70B on CNN Dailymail with 4xH100
On the Roadmap: Dynamic Adjustments for Better Performance
To overcome the limitations of speculative decoding in high-QPS settings, vLLM is working on implementing dynamic speculative decoding. Feel free to check out the paper for more detail. This is also one of the active research directions in vllm! This feature will allow vLLM to adjust the number of speculative tokens based on system load and the accuracy of the draft model. At a high level, dynamic speculative decoding shortens the proposed length when system load is high. However, the reduction is less pronounced when the average token acceptance rate is high as shown in the picture below.
In the future, the system will be able to automatically modify the degree of speculation at each step, ensuring speculative decoding is always beneficial, regardless of the workload. This will allow users to activate speculative decoding without worrying about whether it will slow down their system.
How to Use Speculative Decoding in vLLM
Setting up speculative decoding in vLLM is straightforward. When launching the vLLM server, you simply need to include the necessary flags to specify the speculative model, the number of tokens, and the tensor parallel size.
The following code configures vLLM in an offline mode to use speculative decoding with a draft model, speculating 5 tokens at a time:
from vllm import LLM
llm = LLM(
model="facebook/opt-6.7b",
speculative_model="facebook/opt-125m",
num_speculative_tokens=5,
)
outputs = llm.generate("The future of AI is")
for output in outputs:
print(f"Prompt: {output.prompt!r}, Generated text: {output.outputs[0].text!r}")
The following code configures vLLM to use speculative decoding where proposals are generated by matching n-grams in the prompt:
from vllm import LLM
llm = LLM(
model="facebook/opt-6.7b",
speculative_model="[ngram]",
num_speculative_tokens=5,
ngram_prompt_lookup_max=4,
ngram_prompt_lookup_min=1,
)
outputs = llm.generate("The future of AI is")
for output in outputs:
print(f"Prompt: {output.prompt!r}, Generated text: {output.outputs[0].text!r}")
At times, you may want the draft model to operate with a different tensor parallel size than the target model to improve efficiency. This allows the draft model to use fewer resources and has less communication overhead, leaving the more resource-intensive computations to the target model. In vLLM, you can configure the draft model to use a tensor parallel size of 1, while the target model uses a size of 4, as demonstrated in the example below.
from vllm import LLM
llm = LLM(
model="meta-llama/Meta-Llama-3.1-70B-Instruct",
tensor_parallel_size=4,
speculative_model="ibm-fms/llama3-70b-accelerator",
speculative_draft_tensor_parallel_size=1,
)
outputs = llm.generate("The future of AI is")
for output in outputs:
print(f"Prompt: {output.prompt!r}, Generated text: {output.outputs[0].text!r}")
Future updates (paper, RFC) will allow vLLM to automatically choose the number of speculative tokens, removing the need for manual configuration and simplifying the process even further.
Follow our docs on Speculative Decoding in vLLM to get started. Join our bi-weekly office hours to ask questions and give feedback.
Conclusion: The Future of Speculative Decoding in vLLM
Speculative decoding in vLLM delivers substantial performance improvements, especially in low-QPS environments. As dynamic adjustments are introduced, it will become a highly effective tool even in high-QPS settings, making it a versatile and essential feature for reducing latency and increasing efficiency in LLM inference.