# Speculative Decoding [![Documentation](https://img.shields.io/badge/Docs-TensorRT--Model--Optimizer-blue?logo=readthedocs&style=flat-square)](https://nvidia.github.io/TensorRT-Model-Optimizer/guides/5_speculative_decoding.html) 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](https://github.com/NVIDIA/Megatron-LM/tree/main/examples/post_training/modelopt). ## Contents
| **Section** | **Description** | **Jump To** | | :------------: | :------------: | :------------: | | Pre-Requisites | Required & optional dependencies | \[[Link](#pre-requisites)\] | | Simplified Workflow | Train, evaluate, and export eagle model with one-line command | \[[Link](#getting-started-simplified-workflow)\] | | Complete Workflow | Full example with configurable training pipeline | \[[Link](#complete-workflow)\] | | Support Matrix | Supported models for speculative decoding training | \[[Link](#support-matrix)\] | | Speculation Module Checkpoints | View pre-trained speculation modules ready to deploy! | \[[Link](#speculation-module-checkpoints)\] | | Resources | Extra links to relevant resources | \[[Link](#resources)\] |
## Pre-Requisites Install Modelopt with `hf` dependencies and other requirements for this example: ```bash pip install -e ... pip install -r requirements.txt ``` We use [Daring-Anteater](https://huggingface.co/datasets/nvidia/Daring-Anteater) dataset in this example. Download by: ```bash git clone https://huggingface.co/datasets/nvidia/Daring-Anteater ``` ## Getting Started: Simplified Workflow ```bash 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 - Initializes the draft model with [default settings](https://github.com/NVIDIA/TensorRT-Model-Optimizer/blob/main/modelopt/torch/speculative/eagle/default_config.py#L18) - Fine-tunes the model on the [Daring-Anteater](https://huggingface.co/datasets/nvidia/Daring-Anteater) dataset - Evaluates the acceptance rate on [MT-Bench](https://huggingface.co/datasets/HuggingFaceH4/mt_bench_prompts) - Exports a checkpoint ready for deployment ## 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: ```bash 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: ```bash python server_generate.py --data_path Daring-Anteater/train.jsonl --output_path synthetic/train.jsonl ``` To add a system prompt, use the `--system_prompt ` argument. For large scale data generation, please see [SLURM prepare data](SLURM_prepare_data.md) 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: ```bash 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](https://github.com/NVIDIA/TensorRT-Model-Optimizer/blob/main/modelopt/torch/speculative/config.py#L37) 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`: ```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: ```python model = transformers.AutoModelForCausalLM.from_pretrained( "" ) ``` Then, load default eagle config and make necessary overwrites: ```python # 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({"": ""}) # 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: ```python mtsp.convert(model, [("eagle", config)]) ``` This will modify the model in-place with eagle training forward, making it compatible with HF trainer: ```python # 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("") ``` 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: ```bash ./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: ```bash python ar_validate.py --model_path $OUTPUT_DIR ``` Alternatively, we can export the checkpoint and run evaluation on serving frameworks. See sections below. ### Export ```bash 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: ```bash trtllm-serve --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 ```yaml 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: kv_cache_config: enable_block_reuse: false ``` Please refer to [TRT-LLM Doc: Speculative Decoding](https://nvidia.github.io/TensorRT-LLM/examples/llm_speculative_decoding.html) for detailed usage. #### SGLang Please refer to [SGLang Doc: Speculative Decoding](https://docs.sglang.ai/advanced_features/speculative_decoding.html#EAGLE-3-Decoding) for detailed usage. #### Deploying Quantized model See more details on deployment of quantized model to TRTLLM [here](../llm_ptq/README.md). ## 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](https://huggingface.co/collections/nvidia/model-optimizer-66aa84f7966b3150262481a4)\] Deployable on [TensorRT-LLM](https://github.com/NVIDIA/TensorRT-LLM) and [SGLang](https://github.com/sgl-project/sglang)!\ More models coming soon! ## Resources - 📅 [Roadmap](https://github.com/NVIDIA/TensorRT-Model-Optimizer/issues/146) - 📖 [Documentation](https://nvidia.github.io/TensorRT-Model-Optimizer) - 🎯 [Benchmarks](../benchmark.md) - 💡 [Release Notes](https://nvidia.github.io/TensorRT-Model-Optimizer/reference/0_changelog.html) - 🐛 [File a bug](https://github.com/NVIDIA/TensorRT-Model-Optimizer/issues/new?template=1_bug_report.md) - ✨ [File a Feature Request](https://github.com/NVIDIA/TensorRT-Model-Optimizer/issues/new?template=2_feature_request.md)