main
md 112 lines 3.37 KB
Rendered Raw
1 # Draft Models
2
3 The following code configures vLLM in an offline mode to use speculative decoding with a draft model, speculating 5 tokens at a time.
4
5 ```python
6 from vllm import LLM, SamplingParams
7
8 prompts = ["The future of AI is"]
9 sampling_params = SamplingParams(temperature=0.8, top_p=0.95)
10
11 llm = LLM(
12 model="Qwen/Qwen3-8B",
13 tensor_parallel_size=1,
14 speculative_config={
15 "model": "Qwen/Qwen3-0.6B",
16 "num_speculative_tokens": 5,
17 "method": "draft_model",
18 },
19 )
20 outputs = llm.generate(prompts, sampling_params)
21
22 for output in outputs:
23 prompt = output.prompt
24 generated_text = output.outputs[0].text
25 print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")
26 ```
27
28 To perform the equivalent launch in online mode, use the following server-side code:
29
30 ```bash
31 vllm serve Qwen/Qwen3-4B-Thinking-2507 \
32 --host 0.0.0.0 \
33 --port 8000 \
34 --seed 42 \
35 -tp 1 \
36 --max-model-len 2048 \
37 --gpu-memory-utilization 0.8 \
38 --speculative-config '{"model": "Qwen/Qwen3-0.6B", "num_speculative_tokens": 5, "method": "draft_model"}'
39 ```
40
41 The code used to request completions as a client remains unchanged:
42
43 ??? code
44
45 ```python
46 from openai import OpenAI
47
48 # Modify OpenAI's API key and API base to use vLLM's API server.
49 openai_api_key = "EMPTY"
50 openai_api_base = "http://localhost:8000/v1"
51
52 client = OpenAI(
53 # defaults to os.environ.get("OPENAI_API_KEY")
54 api_key=openai_api_key,
55 base_url=openai_api_base,
56 )
57
58 models = client.models.list()
59 model = models.data[0].id
60
61 # Completion API
62 stream = False
63 completion = client.completions.create(
64 model=model,
65 prompt="The future of AI is",
66 echo=False,
67 n=1,
68 stream=stream,
69 )
70
71 print("Completion results:")
72 if stream:
73 for c in completion:
74 print(c)
75 else:
76 print(completion)
77 ```
78
79 ## Draft Model Method with heterogeneous vocabs
80
81 By default, vLLM requires the draft and target models to share the same vocabulary. Setting `use_heterogeneous_vocab: true` enables the **Token-Level Intersection (TLI)** algorithm, which allows draft models from a different model family with a different tokenizer.
82
83 Currently, `use_heterogeneous_vocab` requires `draft_sample_method='greedy'` (the default). Probabilistic draft sampling is not yet supported and will be added in a
84 future release.
85
86 ```python
87 from vllm import LLM, SamplingParams
88
89 llm = LLM(
90 model="Qwen/Qwen3-8B",
91 speculative_config={
92 "method": "draft_model",
93 "model": "HuggingFaceTB/SmolLM2-135M-Instruct",
94 "num_speculative_tokens": 3,
95 "use_heterogeneous_vocab": True,
96 },
97 gpu_memory_utilization=0.5,
98 )
99 outputs = llm.generate(prompts, sampling_params)
100
101 for output in outputs:
102 prompt = output.prompt
103 generated_text = output.outputs[0].text
104 print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")
105 ```
106
107 !!! warning
108 Note: Please use `--speculative-config` to set all configurations related
109 to speculative decoding. The previous method of specifying the model
110 through `--speculative-model` and adding related parameters such as
111 `--num-speculative-tokens` separately has been deprecated. For supported
112 keys and examples, see the [`--speculative-config` schema](README.md#--speculative-config-schema).