Skip to content

Commit c05f3e2

Browse files
authored
Blog: Add Speculators Training Deep Dive Blog (#134)
1 parent 37efc09 commit c05f3e2

File tree

6 files changed

+213
-0
lines changed

6 files changed

+213
-0
lines changed
Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
---
2+
layout: post
3+
title: "Diving into speculative decoding training support for vLLM with Speculators v0.3.0"
4+
author: "Fynn Schmitt-Ulms, Helen Zhao, Rahul Tuli and Dipika Sikka (Red Hat AI Model Optimization Team)"
5+
image: /assets/figures/2025-12-13-speculators-v030/cropped_workflow.png
6+
---
7+
8+
## Key Highlights
9+
10+
- Speculative decoding serves as an optimization to improve inference performance; however, training a unique draft model for each LLM can be difficult and time-consuming, while production-ready training utilities for generating models for vLLM are scarce
11+
- [Speculators v0.3.0](https://github.com/vllm-project/speculators/releases/tag/v0.3.0) provides end-to-end training support for Eagle3 draft models that can seamlessly run with vLLM
12+
- Support for training includes offline data generation using vLLM as well as training capabilities for single- and multi-layer draft models, for both MoE and non-MoE verifiers
13+
14+
Over the past decade, LLMs have expanded rapidly in both scale and capability, bringing with it increasing demands on inference performance. As LLMs generate tokens sequentially—with each token requiring a full forward pass through billions of parameters—the cost of generation scales quickly. As model sizes continue to rise, this sequential computation becomes a significant bottleneck, making today’s LLMs incredibly capable yet often slow.
15+
16+
One promising optimization to alleviate this challenge is speculative decoding, which accelerates generation by allowing smaller draft models to propose tokens that the larger model can quickly verify.
17+
18+
This blog will explore speculative decoding as an optimization technique, introduce the [Speculators](https://github.com/vllm-project/speculators) library, and dive into it and its recent [v0.3.0 release](https://github.com/vllm-project/speculators/releases/tag/v0.3.0). Speculators provides researchers, engineers, and ML practitioners the tools to generate speculative decoding models end-to-end with seamless vLLM integration.
19+
20+
## What is speculative decoding?
21+
22+
Speculative decoding allows LLMs to generate multiple tokens in a single forward pass. It works by utilizing a small “draft” model in conjunction with the full sized “verifier” model (i.e, the original LLM that you are trying to serve). The draft model which is cheap and fast to run (often just a single transformer block) does the heavy lifting and auto-regressively predicts several tokens. The verifier model processes these tokens in parallel. For each token, the verifier determines if it agrees with the draft’s prediction or not. If the verifier rejects a token, the rest of the sequence is discarded, otherwise the tokens are included in the verifier model’s response.
23+
24+
The advantages of this approach are:
25+
1. The final response comes from the same distribution as using the verifier model alone, which ensures there is no degradation in model performance using speculative decoding.
26+
2. The verifier model is able to generate multiple tokens in parallel.
27+
3. Because the draft model is small, it usually produces minimal overhead to run
28+
29+
Altogether this can reduce model latency by 1.5-3x resulting in significantly faster generation.
30+
31+
## Using speculative decoding models in vLLM
32+
vLLM and Speculators make running speculative decoding models as easy as serving any other model with vllm serve. In particular, speculative decoding performs best in low-throughput scenarios, where GPUs are not fully saturated and can take advantage of the verifier model’s parallel token generation. It is also important for the draft model to align closely with the verifier model, which is why we train draft models specific to each verifier. However, having to train an LLM-specific draft model can be difficult and time consuming. Fortunately, the Speculators library simplifies this training process and enables users to produce draft models with seamless integration into vLLM.
33+
34+
## Creating new draft models
35+
36+
The current SOTA for speculative decoding algorithms is Eagle3 [(Zhang et al., 2025)](https://arxiv.org/abs/2503.01840).
37+
38+
Eagle3 draft models take the hidden states from three layers of the verifier model as input, capturing the verifier’s latent features. Combined with the token ids, these hidden states are passed through the smaller draft model, which auto-regressively generates draft tokens.
39+
40+
This means that training an Eagle3 draft model requires a dataset of sample sequences with the following components:
41+
1. Verifier model hidden states (from three intermediate layers)
42+
2. Token ids
43+
3. Loss mask (used to train only on model responses, ignoring user prompts)
44+
4. Verifier model output probabilities (the training target for the draft model)
45+
46+
### Data Generation
47+
48+
Extracting these values directly from vLLM is non-trivial. Fortunately, Speculators v0.3.0 supports offline training data generation through a hidden states generator, which produces hidden state tensors from standard LLM text datasets. These hidden state tensors are then saved to disk for later use in the training process.
49+
50+
There are three main parts of data generation: preprocessing, hidden states generation and saving.
51+
52+
![data_generation_overview](/assets/figures/2025-12-13-speculators-v030/data_generation.png)
53+
54+
Preprocessing takes in a raw dataset,
55+
1. Reformats and normalizes conversation turns
56+
2. Applies the model’s chat template
57+
3. Tokenizes the conversation
58+
4. Calculates loss masks based on assistant response spans
59+
5. Saves it to disk along with the token IDs
60+
6. Collects statistics on token frequencies, which are saved to disk for later use
61+
62+
The loss masks ensure training focuses only on machine generated tokens. For reasoning models which typically only insert thinking tokens in the last response, Speculators provides an extra flag to randomly drop turns of the conversation to ensure the model trains on a variety of conversation lengths.
63+
64+
The hidden states generator takes advantage of the vLLM plug-in system through a custom worker extension. It patches the model’s forward pass to intercept and capture intermediate hidden states during the prefill phase. The generator uses vLLM’s multiprocess executor for efficient batch inference and supports tensor parallelism for larger models. This process is illustrated in the diagram below.
65+
66+
![hidden_state_generator](/assets/figures/2025-12-13-speculators-v030/hidden_state_generator.png)
67+
68+
During the saving phase, each processed sample is saved as an individual .pt file on disk, containing:
69+
- `input_ids`: tokenized input sequences
70+
- `hidden_states`: list of tensors on per captured layer
71+
- `loss_mask`: binary mask indicating trainable tokens
72+
73+
The generator uses asynchronous I/O with ThreadPoolExecutor to parallelize disk write while hidden states generation continues, maximizing throughput.
74+
75+
Along with the data files, two additional files are saved to disk:
76+
- `data_config.json`, which contains metadata about the data generation
77+
- `token_freq.pt`, which contains information about token frequencies
78+
79+
The frequency data stored in token_freq.pt is used to build additional target-to-draft (t2d) and draft-to-target (d2t) files. These files act as mappings between the verifier’s full vocabulary and the smaller vocabulary of the draft model. This reduced “draft” vocabulary improves the efficiency of the draft model by including only the most frequently occurring tokens.
80+
81+
The following scripts can be used to enable offline data generation:
82+
- [`data_generation_offline.py`](https://github.com/vllm-project/speculators/blob/main/scripts/data_generation_offline.py): preprocesses data, saves token-frequency distribution, and generates hidden states
83+
- [`build_vocab_mapping.py`](https://github.com/vllm-project/speculators/blob/main/scripts/build_vocab_mapping.py): builds t2d and d2t tensors
84+
85+
### Training
86+
87+
Speculators v0.3.0 supports training Eagle3 draft models. Training takes as input the generated samples and vocabulary mapping files from the previous steps, along with model configuration information and initializes a new Eagle3DraftModel instance. This model is then trained using a technique introduced by the Eagle3 authors called “train-time-testing”. Train-time-testing simulates the multi-step draft sampling process during training to ensure the model learns to predict not just the first token, but also subsequent ones.
88+
89+
![flex_attention](/assets/figures/2025-12-13-speculators-v030/flex_attention.png)
90+
91+
Diagram from Eagle3 [(Zhang et al., 2025)](https://arxiv.org/abs/2503.01840) paper.
92+
93+
The diagram above shows the train-time-testing process, and the attention mask at each step. For every prefix, the draft model generates a next token (blue). Then for every prefix plus first generation step, the model generates a second token (yellow), and so on.
94+
95+
Train-time-testing is challenging to implement because the attention mask is sparse which typical attention implementations struggle to handle in a compute and memory efficient way. That is why Speculators uses FlexAttention [(He et al., 2024)](https://arxiv.org/abs/2412.05496) for attention computations. FlexAttention splits the attention mask into blocks and only computes attention in non-empty regions. Combined with `torch.compile`, this speeds up computation while drastically reducing the activation VRAM required for the backward pass.
96+
97+
Another important feature for any training implementation is batching. Batching samples for LLM training is made more complicated by the fact that sequences are typically different lengths. There are two approaches to this problem, the first is to make the sequences the same length using some combination of truncation and padding. This works well for datasets with uniform lengths, but can lead to wasted compute on datasets that need a lot of padding. Instead, Speculators v0.3.0 uses the second approach, which is to concatenate sequences along the “sequence” dimension, and then configure the attention masks to treat them as separate sequences. This integrates well with the FlexAttention implementation, and results in better performance, particularly when combined with an intelligent batch sampling algorithm which efficiently packs samples into batches that are close to the max sequence length.
98+
99+
Together these components make Speculators Eagle3 model training fast and memory efficient, all of which can be applied using a single [train.py](https://github.com/vllm-project/speculators/blob/main/scripts/train.py) script.
100+
101+
## Running Speculators models in vLLM
102+
103+
Once training is complete, the library generates a complete model artifact with an extended config.json file that includes a speculators_config. Models can then be run seamlessly in vLLM using a simple vllm serve command:
104+
105+
```bash
106+
vllm serve RedHatAI/Llama-3.1-8B-Instruct-speculator.eagle3
107+
```
108+
109+
When running this command, vLLM will read the speculative decoding settings (e.g., the name of the verifier model) stored in the `speculators_config`. This information is used to load both the draft model and the verifier model into the same server and set up speculative decoding. The `speculators_config` provides a standardized configuration format, enabling a self-contained model that knows how it should run while making deployment of speculative decoding models as simple as running any other LLM. For further details on the `speculators_config`, [see an example below](#speculators_config).
110+
111+
While the simplified one-command deployment is perfect for getting started, vLLM also provides a long-form syntax when you need more control. This is useful for:
112+
113+
- Using a different verifier model than the one in the config
114+
- Tuning for speculative decoding parameters, such as the number of speculative tokens
115+
116+
The long-form command serves the base (verifier) model and specifies the speculator via the `--speculative-config` flag.
117+
This flexibility is crucial for experimentation and optimization. For example, you might want to swap in a quantized version of the verifier to further improve performance:
118+
119+
```bash
120+
vllm serve RedHatAI/Qwen3-8B-FP8-dynamic \
121+
--tensor-parallel-size 1 \
122+
--gpu-memory-utilization 0.9 \
123+
--speculative-config '{"model": "RedHatAI/Qwen3-8B-speculator.eagle3", "num_speculative_tokens": 5, "method": "eagle3"}'
124+
```
125+
126+
In this example, we're using the FP8-quantized Qwen3-8B as the verifier (instead of the default BF16 version referenced in the `speculators_config`) and increasing the number of speculative tokens from the default 3 to 5 for potentially higher throughput.
127+
128+
## vLLM Integration: Production-Ready Speculative Decoding
129+
The tight integration between Speculators and vLLM transforms speculative decoding from a research technique into a production-ready feature. vLLM's support for Eagle3 enables seamless deployment across diverse model architectures and configurations:
130+
131+
**vLLM serving and Speculators training**:
132+
- Llama (3.1, 3.2, 3.3): 8B to 70B parameters
133+
- Qwen3: 8B, 14B, 32B parameters
134+
- Qwen3 MoE: 235B-A22B parameters (mixture-of-experts)
135+
- GPT-OSS: 20B, 120B parameters
136+
137+
**vLLM serving only**:
138+
- Multimodal: Llama 4 vision-language models
139+
140+
## What’s Next?
141+
142+
Speculators will be focusing on the following next set of features:
143+
- Online data generation (generate hidden states while training, with no intermediate caching to disk)
144+
- Data generation support for Vision Language models
145+
- Regenerating verifier responses (replace the dataset “assistant” response with one generated by the verifier for better aligned training data)
146+
147+
## Get involved!
148+
149+
Interested in learning more about speculative decoding? Check out the [Speculators repository](https://github.com/vllm-project/speculators) and help grow the repository by checking out [First Good Issues](https://github.com/vllm-project/speculators/issues)!
150+
151+
For additional resources, documentation, and slack channels, check out:
152+
- **Speculators Documentation**: https://docs.vllm.ai/projects/Speculators/en/latest/
153+
- **vLLM slack channels**: `#speculators`, `#feat-spec-decode`
154+
- **Data Generation and Training Scripts**: https://github.com/vllm-project/speculators/blob/main/scripts/README.md
155+
- **End-to-end examples**: https://github.com/vllm-project/Speculators/tree/main/examples/data_generation_and_training
156+
- For a list of already trained Speculators models, check out the [Red Hat AI Hub](https://huggingface.co/collections/RedHatAI/speculator-models)
157+
158+
## Appendix
159+
160+
### Eagle3 Algorithm
161+
162+
![Eagle3 Algorithm](/assets/figures/2025-12-13-speculators-v030/EAGLE3.png)
163+
164+
### `speculators_config`:
165+
166+
```yaml
167+
{
168+
"architectures": ["Eagle3Speculator"],
169+
"auto_map": {"": "eagle3.Eagle3SpeculatorConfig"},
170+
"Speculators_model_type": "eagle3",
171+
"Speculators_version": "0.3.0",
172+
173+
"draft_vocab_size": 10000,
174+
"transformer_layer_config": {
175+
"num_hidden_layers": 1,
176+
"hidden_size": 4096,
177+
...
178+
},
179+
180+
"Speculators_config": {
181+
"algorithm": "eagle3",
182+
"proposal_methods": [{
183+
"proposal_type": "greedy",
184+
"speculative_tokens": 3,
185+
...
186+
}],
187+
"verifier": {
188+
"name_or_path": "meta-llama/Llama-3.1-8B-Instruct",
189+
"architectures": ["LlamaForCausalLM"]
190+
}
191+
}
192+
}
193+
```
194+
195+
This config defines the speculator as a complete model with:
196+
197+
- Model Identity:
198+
- `architectures`: The speculator's model class (e.g., Eagle3Speculator)
199+
- `auto_map`: Custom model loading for Hugging Face compatibility
200+
- `Speculators_model_type`: The specific speculator implementation
201+
- Draft Model Architecture:
202+
- `transformer_layer_config`: Full specification of the draft model's transformer layers
203+
- `draft_vocab_size`: Reduced vocabulary size for efficient draft generation (typically 10k-32k tokens)
204+
- Model-specific configuration options
205+
- Speculative Decoding Configuration:
206+
- `algorithm`: The spec decoding algorithm (EAGLE3)
207+
- `proposal_methods`: Token generation strategies with parameters
208+
- `speculative_tokens`: Number of draft tokens to generate per step
209+
- `verifier_accept_k`: How many top-k predictions to consider during verification
210+
- `accept_tolerance`: Probability threshold for accepting draft tokens
211+
- `verifier`: Which verifier model to use and validate against
212+
- `name_or_path`: HuggingFace model ID or local path
213+
- `architectures`: Expected verifier architecture for compatibility checks
464 KB
Loading
821 KB
Loading
1.44 MB
Loading
48.1 KB
Loading
104 KB
Loading

0 commit comments

Comments
 (0)