Files
Model-Optimizer/examples/speculative_decoding/README.md
T

10 KiB
Raw Blame History

Speculative Decoding

Documentation

Speculative decoding accelerates auto-regressive generation in large language models (LLMs) by leveraging a lightweight draft model to predict the next γ tokens. The main LLM then verifies these candidate tokens in a single forward pass. If the draft model correctly predicts α tokens, the LLM can accept and generate α+1 tokens per verification step, significantly improving generation speed.

This folder contains an end-to-end runnable speculative decoding fine‑tuning pipeline in which Llama‑3.2‑1B (Hugging Face) is trained on the Daring‑Anteater dataset.

This example focuses on training with Hugging Face. To train with Megatron‑LM, see the Megatron‑LM example.

Contents

Section Description Jump To
Pre-Requisites Required & optional dependencies [Link]
Simplified Workflow Train, evaluate, and export eagle model with one-line command [Link]
Complete Workflow Full example with configurable training pipeline [Link]
Support Matrix Supported models for speculative decoding training [Link]
Speculation Module Checkpoints View pre-trained speculation modules ready to deploy! [Link]
Resources Extra links to relevant resources [Link]

Pre-Requisites

Install Modelopt with hf dependencies and other requirements for this example:

pip install -e ...
pip install -r requirements.txt

We use Daring-Anteater dataset in this example. Download by:

git clone https://huggingface.co/datasets/nvidia/Daring-Anteater

Getting Started: Simplified Workflow

bash train_eagle3_and_export.sh --base_model meta-llama/Llama-3.2-1B-Instruct --num_gpu 4

This one-line command runs a minimal example workflow of training and exporting an EAGLE draft model in Modelopt. Specifically, it

Complete Workflow

This section presents a more comprehensive example for customizing speculative decoding training with Modelopt, including optional steps to enhance training quality and efficiency.

(Optional) Data Synthesis

To achieve higher acceptance rates during speculative decoding, it is beneficial to use conversations generated by the base model as training data, ensuring that the draft model’s output distribution closely aligns with that of the base model.

To prepare such data, we launch an inference server with the base model:

pip install vllm
vllm serve meta-llama/Llama-3.2-1B-Instruct --api-key token-abc123 --port 8000  --tensor-parallel-size 1

Note: Add --quantization=modelopt flag for quantized models.

Then, we generate conversations with base model and prompts from Daring-Anteater:

python server_generate.py --data_path Daring-Anteater/train.jsonl --output_path synthetic/train.jsonl

To add a system prompt, use the --system_prompt <system_prompt_text> argument.

For large scale data generation, please see SLURM prepare data for SLURM support.

(Optional) Draft Vocabulary Compression

We can optionally use smaller vocab size for the draft model for faster training and inference. E.g. Llama3.2-1B has a vocab size of 128256. In this example, we construct a draft vocab mapping of size 32k by finding the most commonly appeared vocabs in our training set:

python calibrate_draft_vocab.py --model meta-llama/Llama-3.2-1B-Instruct --data Daring-Anteater/train.jsonl --draft_vocab_size 32000 --save_dir draft_vocab_cache

This will produce a d2t.pt file in save_dir, which is the mapping from draft token to target token. During inference, draft tokens can be mapped back to target tokens by target_token = draft_token + d2t[draft_token].

(Optional) Configuring Draft Model

For EAGLE‑1 and EAGLE‑3 we provide a default model architecture config in ModelOpt. You can override default settings by providing an additional JSON dict. In this example, we override draft_vocab_size in eagle_config.json:

{
    "draft_vocab_size": 32000
}

Training Draft Model with Modelopt

main.py provides an example for converting a HF base model for speculative decoding and training it. It consists of a few simple steps: First, load the base model and tokenizer from Hugging Face:

model = transformers.AutoModelForCausalLM.from_pretrained(
    "<path to your pretrained model>"
)

Then, load default eagle config and make necessary overwrites:

# Load default config
config = {
    "eagle1": EAGLE1_DEFAULT_CFG,
    "eagle3": EAGLE3_DEFAULT_CFG,
}[training_args.mode]["config"]

# overwrite config with custom config
config["eagle_architecture_config"].update({"<overwrite_keys>": "<overwrite_values>"})

# Mandatory: hidden size, vocab size and max position embeddings must match base model
config["eagle_architecture_config"].update(
    {
        "hidden_size": model.config.hidden_size,
        "vocab_size": model.config.vocab_size,
        "max_position_embeddings": model.config.max_position_embeddings,
    }
)

Then, we convert model to a speculative decoding model:

mtsp.convert(model, [("eagle", config)])

This will modify the model in-place with eagle training forward, making it compatible with HF trainer:

# Create a trainer
trainer = transformers.Trainer(model=model, tokenizer=tokenizer, args=training_args, **data_module)
trainer._move_model_to_device(model, trainer.args.device)

# Enable HF checkpointing so that the saved model will contain the speculative decoding module
mto.enable_huggingface_checkpointing()

trainer.train(resume_from_checkpoint=checkpoint)
trainer.save_state()
trainer.save_model("<path to the output directory>")

We omitted details like tokenizer initialization for simplicity. A complete training example is provided in main.py, along with a bash script to launch training with Hugging Face Accelerate in launch_train.sh, which can be run by:

./launch_train.sh --model $BASE_MODEL \
            --output_dir $OUTPUT_DIR \
            --data $DATA \
            --num_gpu $NUM_GPU \
            --num_epochs 10 \
            --eagle_config eagle_config.json #This is where we optionally overwrite default eagle configs

The saved modelopt checkpoint is similar in architecture to HF models. It can be further optimized through ModelOpt, e.g., PTQ and QAT.

Model Validation

After training draft model, we can evaluate the saved modelopt checkpoint on MT-bench by:

python ar_validate.py --model_path $OUTPUT_DIR

Alternatively, we can export the checkpoint and run evaluation on serving frameworks. See sections below.

Export

python export_hf_checkpoint.py --model_path $OUTPUT_DIR --export_path $EXPORT_PATH

This exports the model from a ModelOpt checkpoint to a deployment‑compatible format.

Deployment

The exported checkpoint can be deployed on TRT-LLM or SGLang.

TRT-LLM

To serve the checkpoint with trtllm, run trtllm-serve with:

trtllm-serve <base_model_checkpoint> --host 0.0.0.0 --port 8000 --backend pytorch --max_batch_size 32 --max_num_tokens 8192 --max_seq_len 8192 --extra_llm_api_options extra-llm-api-config.yml

, with extra-llm-api-config.yml being

enable_attention_dp: false
disable_overlap_scheduler: true
enable_autotuner: false

cuda_graph_config:
    max_batch_size: 1

speculative_config:
    decoding_type: Eagle
    max_draft_len: 3
    speculative_model_dir: <draft_model_checkpoint>

kv_cache_config:
    enable_block_reuse: false

Please refer to TRT-LLM Doc: Speculative Decoding for detailed usage.

SGLang

Please refer to SGLang Doc: Speculative Decoding for detailed usage.

Deploying Quantized model

See more details on deployment of quantized model to TRTLLM here.

Support Matrix

Model Medusa EAGLE1/2 EAGLE3
LLAMA 2 ✅ ✅ ✅
LLAMA 3, 3.1 ✅ ✅ ✅
Mistral ✅ ✅ ✅
Phi 3 ✅ ✅ ✅
QWen 1.5,2,2.5 ✅ ✅ ✅

Speculation Module Checkpoints

Ready-to-deploy speculation module checkpoints [🤗 Hugging Face - NVIDIA TensorRT Model Optimizer Collection] Deployable on TensorRT-LLM and SGLang!
More models coming soon!

Resources