Back to Vllm

Speculative Decoding

docs/features/speculative_decoding/README.md

0.20.19.5 KB
Original Source

Speculative Decoding

This document shows how to use Speculative Decoding with vLLM to reduce inter-token latency under medium-to-low QPS (query per second), memory-bound workloads.

To train your own draft models for optimized speculative decoding, see vllm-project/speculators for seamless training and integration with vLLM.

vLLM Speculation Methods

vLLM supports a variety of methods of speculative decoding. Model-based methods such as EAGLE, MTP, draft models, PARD and MLP provide the best latency reduction, while simpler methods such as n-gram and suffix decoding provide modest speedups without increasing workload during peak traffic.

Method Selection at a Glance

Use this qualitative table as a starting point for method selection. Real gains depend on your model family, traffic pattern, hardware, and sampling settings.

MethodLow QPS (latency focused)High QPS (throughput focused)Notes
EAGLEHigh gainMedium to high gainStrong general-purpose model-based method.
MTPHigh gainMedium to high gainBest when the target model has native MTP support.
Draft modelHigh gainMedium gainNeeds a separate draft model.
Parallel Draft ModelHigh gainMedium to high gainLow draft model latency.
MLP speculatorMedium to high gainMedium gainGood when compatible MLP speculators are available.
N-gramLow to medium gainMedium gainLightweight and easy to enable.
Suffix decodingLow to medium gainMedium gainNo extra draft model; dynamic speculation depth.

For reproducible measurements in your environment, use examples/offline_inference/spec_decode.py or the benchmark CLI guide.

--speculative-config schema

Use --speculative-config to pass speculative decoding settings as a JSON object on the CLI:

bash
vllm serve <target-model> \
  --speculative-config '{
    "method": "draft_model",
    "model": "<draft-model>",
    "num_speculative_tokens": 5
  }'

The same keys are accepted from Python via LLM(..., speculative_config={...}). The tables below highlight common user-facing keys accepted in this JSON object; they are not an exhaustive schema reference. For more details, see the generated engine arguments reference and the API docs for [vllm.config.SpeculativeConfig][].

Common keys

These keys are commonly used across speculative decoding setups, though some only apply to model-based methods such as draft_model, mtp, eagle3, and dflash.

KeyTypeDefaultAllowed values / meaning
methodstringNoneSpeculation method. Common values include draft_model, ngram, suffix, mtp, eagle3, and dflash. If omitted, vLLM infers the method from the provided configuration when possible.
modelstringNoneDraft model, EAGLE head, or auxiliary model identifier. For ngram, ngram_gpu, suffix, and mtp, this can often be omitted.
num_speculative_tokensinteger > 0NoneNumber of speculative tokens to propose per step. Required for methods that do not infer it from model metadata.
draft_tensor_parallel_sizeinteger >= 1NoneTensor parallel size for the draft model.
max_model_leninteger >= 1NoneMaximum context length for the draft model.
parallel_draftingbooleanfalseEnable parallel draft token generation. Only compatible with EAGLE and draft-model methods.
rejection_sample_methodstringstrictstrict, probabilistic, or synthetic.
synthetic_acceptance_ratefloatNoneAverage acceptance rate to target when rejection_sample_method is synthetic. Valid range is [0, 1].

Method-specific keys

N-gram

KeyTypeDefaultMeaning
prompt_lookup_maxinteger >= 15 if both lookup bounds are omitted; otherwise mirrors prompt_lookup_min when omittedMaximum n-gram window size.
prompt_lookup_mininteger >= 15 if both lookup bounds are omitted; otherwise mirrors prompt_lookup_max when omittedMinimum n-gram window size.

Example:

bash
vllm serve <target-model> \
  --speculative-config '{
    "method": "ngram",
    "num_speculative_tokens": 4,
    "prompt_lookup_min": 2,
    "prompt_lookup_max": 5
  }'

Suffix decoding

KeyTypeDefaultMeaning
suffix_decoding_max_tree_depthinteger24Maximum combined prefix-match and speculation tree depth.
suffix_decoding_max_cached_requestsinteger10000Maximum number of requests cached in the global suffix tree. Set 0 to disable the global cache.
suffix_decoding_max_spec_factorfloat1.0Caps speculative length as a multiple of prefix-match length.
suffix_decoding_min_token_probfloat0.1Minimum estimated token probability required to speculate a token.

Example:

bash
vllm serve <target-model> \
  --speculative-config '{
    "method": "suffix",
    "num_speculative_tokens": 8,
    "suffix_decoding_max_tree_depth": 24,
    "suffix_decoding_max_cached_requests": 10000,
    "suffix_decoding_max_spec_factor": 1.0,
    "suffix_decoding_min_token_prob": 0.1
  }'

Notes

  • --speculative-config expects a JSON object on the CLI. In YAML config files, use a nested mapping instead of an escaped JSON string.
  • tensor_parallel_size is not a valid key in speculative_config. Use draft_tensor_parallel_size instead.
  • Keys such as temperature and top_p are sampling parameters, not --speculative-config fields.
  • Internal fields such as target_model_config, draft_model_config, target_parallel_config, draft_parallel_config, and draft_load_config are populated by vLLM and are not intended to be set by users.

Lossless guarantees of Speculative Decoding

In vLLM, speculative decoding aims to enhance inference efficiency while maintaining accuracy. This section addresses the lossless guarantees of speculative decoding, breaking down the guarantees into three key areas:

  1. Theoretical Losslessness - Speculative decoding sampling is theoretically lossless up to the precision limits of hardware numerics. Floating-point errors might cause slight variations in output distributions, as discussed in Accelerating Large Language Model Decoding with Speculative Sampling

  2. Algorithmic Losslessness - vLLM’s implementation of speculative decoding is algorithmically validated to be lossless. Key validation tests include:

    • Rejection Sampler Convergence: Ensures that samples from vLLM’s rejection sampler align with the target distribution. View Test Code
    • Greedy Sampling Equality: Confirms that greedy sampling with speculative decoding matches greedy sampling without it. This verifies that vLLM's speculative decoding framework, when integrated with the vLLM forward pass and the vLLM rejection sampler, provides a lossless guarantee. Almost all of the tests in tests/spec_decode/e2e. verify this property using this assertion implementation
  3. vLLM Logprob Stability - vLLM does not currently guarantee stable token log probabilities (logprobs). This can result in different outputs for the same request across runs. For more details, see the FAQ section titled Can the output of a prompt vary across runs in vLLM? in the FAQs.

While vLLM strives to ensure losslessness in speculative decoding, variations in generated outputs with and without speculative decoding can occur due to following factors:

  • Floating-Point Precision: Differences in hardware numerical precision may lead to slight discrepancies in the output distribution.
  • Batch Size and Numerical Stability: Changes in batch size may cause variations in logprobs and output probabilities, potentially due to non-deterministic behavior in batched operations or numerical instability.

For mitigation strategies, please refer to the FAQ entry Can the output of a prompt vary across runs in vLLM? in the FAQs.

Known Feature Incompatibility

  1. Pipeline parallelism is not composible with speculative decoding as of vllm<=0.15.0
  2. Speculative decoding with a draft models is not supported in vllm<=0.10.0

Resources for vLLM contributors