TensorRT-LLMs/1.1.0rc5/features/speculative-decoding.html
2025-09-17 05:52:48 +00:00

939 lines
63 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html lang="en" data-content_root="../" >
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Speculative Decoding &#8212; TensorRT-LLM</title>
<script data-cfasync="false">
document.documentElement.dataset.mode = localStorage.getItem("mode") || "";
document.documentElement.dataset.theme = localStorage.getItem("theme") || "";
</script>
<!--
this give us a css class that will be invisible only if js is disabled
-->
<noscript>
<style>
.pst-js-only { display: none !important; }
</style>
</noscript>
<!-- Loaded before other Sphinx assets -->
<link href="../_static/styles/theme.css?digest=8878045cc6db502f8baf" rel="stylesheet" />
<link href="../_static/styles/pydata-sphinx-theme.css?digest=8878045cc6db502f8baf" rel="stylesheet" />
<link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=8f2a1f02" />
<link rel="stylesheet" type="text/css" href="../_static/styles/nvidia-sphinx-theme.css?v=df3ac72c" />
<link rel="stylesheet" type="text/css" href="../_static/copybutton.css?v=76b2166b" />
<link rel="stylesheet" type="text/css" href="../_static/autodoc_pydantic.css" />
<link rel="stylesheet" type="text/css" href="../_static/togglebutton.css?v=13237357" />
<link rel="stylesheet" type="text/css" href="../_static/custom.css?v=95073da6" />
<!-- So that users can add custom icons -->
<script src="../_static/scripts/fontawesome.js?digest=8878045cc6db502f8baf"></script>
<!-- Pre-loaded scripts that we'll load fully later -->
<link rel="preload" as="script" href="../_static/scripts/bootstrap.js?digest=8878045cc6db502f8baf" />
<link rel="preload" as="script" href="../_static/scripts/pydata-sphinx-theme.js?digest=8878045cc6db502f8baf" />
<script src="../_static/documentation_options.js?v=5929fcd5"></script>
<script src="../_static/doctools.js?v=9a2dae69"></script>
<script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
<script src="../_static/clipboard.min.js?v=a7894cd8"></script>
<script src="../_static/copybutton.js?v=65e89d2a"></script>
<script>let toggleHintShow = 'Click to show';</script>
<script>let toggleHintHide = 'Click to hide';</script>
<script>let toggleOpenOnPrint = 'true';</script>
<script src="../_static/togglebutton.js?v=4a39c7ea"></script>
<script>var togglebuttonSelector = '.toggle, .admonition.dropdown';</script>
<script>var togglebuttonSelector = '.toggle, .admonition.dropdown';</script>
<script>DOCUMENTATION_OPTIONS.pagename = 'features/speculative-decoding';</script>
<script>
DOCUMENTATION_OPTIONS.theme_version = '0.16.1';
DOCUMENTATION_OPTIONS.theme_switcher_json_url = './_static/switcher.json';
DOCUMENTATION_OPTIONS.theme_switcher_version_match = '1.1.0rc5';
DOCUMENTATION_OPTIONS.show_version_warning_banner =
false;
</script>
<link rel="icon" href="../_static/favicon.png"/>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="Checkpoint Loading" href="checkpoint-loading.html" />
<link rel="prev" title="Sampling" href="sampling.html" />
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<meta name="docsearch:language" content="en"/>
<meta name="docsearch:version" content="1.1.0rc5" />
</head>
<body data-bs-spy="scroll" data-bs-target=".bd-toc-nav" data-offset="180" data-bs-root-margin="0px 0px -60%" data-default-mode="">
<div id="pst-skip-link" class="skip-link d-print-none"><a href="#main-content">Skip to main content</a></div>
<div id="pst-scroll-pixel-helper"></div>
<button type="button" class="btn rounded-pill" id="pst-back-to-top">
<i class="fa-solid fa-arrow-up"></i>Back to top</button>
<dialog id="pst-search-dialog">
<form class="bd-search d-flex align-items-center"
action="../search.html"
method="get">
<i class="fa-solid fa-magnifying-glass"></i>
<input type="search"
class="form-control"
name="q"
placeholder="Search the docs ..."
aria-label="Search the docs ..."
autocomplete="off"
autocorrect="off"
autocapitalize="off"
spellcheck="false"/>
<span class="search-button__kbd-shortcut"><kbd class="kbd-shortcut__modifier">Ctrl</kbd>+<kbd>K</kbd></span>
</form>
</dialog>
<div class="pst-async-banner-revealer d-none">
<aside id="bd-header-version-warning" class="d-none d-print-none" aria-label="Version warning"></aside>
</div>
<header class="bd-header navbar navbar-expand-lg bd-navbar d-print-none">
<div class="bd-header__inner bd-page-width">
<button class="pst-navbar-icon sidebar-toggle primary-toggle" aria-label="Site navigation">
<span class="fa-solid fa-bars"></span>
</button>
<div class="col-lg-3 navbar-header-items__start">
<div class="navbar-item">
<a class="navbar-brand logo" href="../index.html">
<img src="../_static/nvidia-logo-horiz-rgb-blk-for-screen.svg" class="logo__image only-light" alt="TensorRT-LLM - Home"/>
<img src="../_static/nvidia-logo-horiz-rgb-wht-for-screen.svg" class="logo__image only-dark pst-js-only" alt="TensorRT-LLM - Home"/>
<p class="title logo__title">TensorRT-LLM</p>
</a></div>
</div>
<div class="col-lg-9 navbar-header-items">
<div class="me-auto navbar-header-items__center">
<div class="navbar-item">
<div class="version-switcher__container dropdown pst-js-only">
<button id="pst-version-switcher-button-2"
type="button"
class="version-switcher__button btn btn-sm dropdown-toggle"
data-bs-toggle="dropdown"
aria-haspopup="listbox"
aria-controls="pst-version-switcher-list-2"
aria-label="Version switcher list"
>
Choose version <!-- this text may get changed later by javascript -->
<span class="caret"></span>
</button>
<div id="pst-version-switcher-list-2"
class="version-switcher__menu dropdown-menu list-group-flush py-0"
role="listbox" aria-labelledby="pst-version-switcher-button-2">
<!-- dropdown will be populated by javascript on page load -->
</div>
</div></div>
</div>
<div class="navbar-header-items__end">
<div class="navbar-item navbar-persistent--container">
<button class="btn search-button-field search-button__button pst-js-only" title="Search" aria-label="Search" data-bs-placement="bottom" data-bs-toggle="tooltip">
<i class="fa-solid fa-magnifying-glass"></i>
<span class="search-button__default-text">Search</span>
<span class="search-button__kbd-shortcut"><kbd class="kbd-shortcut__modifier">Ctrl</kbd>+<kbd class="kbd-shortcut__modifier">K</kbd></span>
</button>
</div>
<div class="navbar-item">
<button class="btn btn-sm nav-link pst-navbar-icon theme-switch-button pst-js-only" aria-label="Color mode" data-bs-title="Color mode" data-bs-placement="bottom" data-bs-toggle="tooltip">
<i class="theme-switch fa-solid fa-sun fa-lg" data-mode="light" title="Light"></i>
<i class="theme-switch fa-solid fa-moon fa-lg" data-mode="dark" title="Dark"></i>
<i class="theme-switch fa-solid fa-circle-half-stroke fa-lg" data-mode="auto" title="System Settings"></i>
</button></div>
</div>
</div>
<div class="navbar-persistent--mobile">
<button class="btn search-button-field search-button__button pst-js-only" title="Search" aria-label="Search" data-bs-placement="bottom" data-bs-toggle="tooltip">
<i class="fa-solid fa-magnifying-glass"></i>
<span class="search-button__default-text">Search</span>
<span class="search-button__kbd-shortcut"><kbd class="kbd-shortcut__modifier">Ctrl</kbd>+<kbd class="kbd-shortcut__modifier">K</kbd></span>
</button>
</div>
<button class="pst-navbar-icon sidebar-toggle secondary-toggle" aria-label="On this page">
<span class="fa-solid fa-outdent"></span>
</button>
</div>
</header>
<div class="bd-container">
<div class="bd-container__inner bd-page-width">
<dialog id="pst-primary-sidebar-modal"></dialog>
<div id="pst-primary-sidebar" class="bd-sidebar-primary bd-sidebar">
<a class="navbar-brand logo" href="../index.html">
<img src="../_static/nvidia-logo-horiz-rgb-blk-for-screen.svg" class="logo__image only-light" alt="TensorRT-LLM - Home"/>
<img src="../_static/nvidia-logo-horiz-rgb-wht-for-screen.svg" class="logo__image only-dark pst-js-only" alt="TensorRT-LLM - Home"/>
<p class="title logo__title">TensorRT-LLM</p>
</a>
<div class="sidebar-header-items sidebar-primary__section">
<div class="sidebar-header-items__center">
<div class="navbar-item">
<div class="version-switcher__container dropdown pst-js-only">
<button id="pst-version-switcher-button-3"
type="button"
class="version-switcher__button btn btn-sm dropdown-toggle"
data-bs-toggle="dropdown"
aria-haspopup="listbox"
aria-controls="pst-version-switcher-list-3"
aria-label="Version switcher list"
>
Choose version <!-- this text may get changed later by javascript -->
<span class="caret"></span>
</button>
<div id="pst-version-switcher-list-3"
class="version-switcher__menu dropdown-menu list-group-flush py-0"
role="listbox" aria-labelledby="pst-version-switcher-button-3">
<!-- dropdown will be populated by javascript on page load -->
</div>
</div></div>
</div>
<div class="sidebar-header-items__end">
<div class="navbar-item">
<button class="btn btn-sm nav-link pst-navbar-icon theme-switch-button pst-js-only" aria-label="Color mode" data-bs-title="Color mode" data-bs-placement="bottom" data-bs-toggle="tooltip">
<i class="theme-switch fa-solid fa-sun fa-lg" data-mode="light" title="Light"></i>
<i class="theme-switch fa-solid fa-moon fa-lg" data-mode="dark" title="Dark"></i>
<i class="theme-switch fa-solid fa-circle-half-stroke fa-lg" data-mode="auto" title="System Settings"></i>
</button></div>
</div>
</div>
<div class="sidebar-primary-items__start sidebar-primary__section">
<div class="sidebar-primary-item">
<nav class="bd-docs-nav bd-links"
aria-label="Table of Contents">
<p class="bd-links__title" role="heading" aria-level="1">Table of Contents</p>
<div class="bd-toc-item navbar-nav"><p aria-level="2" class="caption" role="heading"><span class="caption-text">Getting Started</span></p>
<ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="../overview.html">Overview</a></li>
<li class="toctree-l1"><a class="reference internal" href="../quick-start-guide.html">Quick Start Guide</a></li>
<li class="toctree-l1 has-children"><a class="reference internal" href="../installation/index.html">Installation</a><details><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul>
<li class="toctree-l2"><a class="reference internal" href="../installation/containers.html">Pre-built release container images on NGC</a></li>
<li class="toctree-l2"><a class="reference internal" href="../installation/linux.html">Installing on Linux via <code class="docutils literal notranslate"><span class="pre">pip</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="../installation/build-from-source-linux.html">Building from Source Code on Linux</a></li>
</ul>
</details></li>
</ul>
<p aria-level="2" class="caption" role="heading"><span class="caption-text">Deployment Guide</span></p>
<ul class="nav bd-sidenav">
<li class="toctree-l1 has-children"><a class="reference internal" href="../examples/llm_api_examples.html">LLM Examples</a><details><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul>
<li class="toctree-l2"><a class="reference internal" href="../examples/llm_inference.html">Generate text</a></li>
<li class="toctree-l2"><a class="reference internal" href="../examples/llm_inference_async.html">Generate text asynchronously</a></li>
<li class="toctree-l2"><a class="reference internal" href="../examples/llm_inference_async_streaming.html">Generate text in streaming</a></li>
<li class="toctree-l2"><a class="reference internal" href="../examples/llm_inference_distributed.html">Distributed LLM Generation</a></li>
<li class="toctree-l2"><a class="reference internal" href="../examples/llm_guided_decoding.html">Generate text with guided decoding</a></li>
<li class="toctree-l2"><a class="reference internal" href="../examples/llm_logits_processor.html">Control generated text using logits processor</a></li>
<li class="toctree-l2"><a class="reference internal" href="../examples/llm_multilora.html">Generate text with multiple LoRA adapters</a></li>
<li class="toctree-l2"><a class="reference internal" href="../examples/llm_speculative_decoding.html">Speculative Decoding</a></li>
<li class="toctree-l2"><a class="reference internal" href="../examples/llm_kv_cache_connector.html">KV Cache Connector</a></li>
<li class="toctree-l2"><a class="reference internal" href="../examples/llm_runtime.html">Runtime Configuration Examples</a></li>
<li class="toctree-l2"><a class="reference internal" href="../examples/llm_sampling.html">Sampling Techniques Showcase</a></li>
<li class="toctree-l2"><a class="reference internal" href="../examples/llm_mgmn_llm_distributed.html">Run LLM-API with pytorch backend on Slurm</a></li>
<li class="toctree-l2"><a class="reference internal" href="../examples/llm_mgmn_trtllm_bench.html">Run trtllm-bench with pytorch backend on Slurm</a></li>
<li class="toctree-l2"><a class="reference internal" href="../examples/llm_mgmn_trtllm_serve.html">Run trtllm-serve with pytorch backend on Slurm</a></li>
</ul>
</details></li>
<li class="toctree-l1 has-children"><a class="reference internal" href="../examples/trtllm_serve_examples.html">Online Serving Examples</a><details><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul>
<li class="toctree-l2"><a class="reference internal" href="../examples/curl_chat_client.html">Curl Chat Client</a></li>
<li class="toctree-l2"><a class="reference internal" href="../examples/curl_chat_client_for_multimodal.html">Curl Chat Client For Multimodal</a></li>
<li class="toctree-l2"><a class="reference internal" href="../examples/curl_completion_client.html">Curl Completion Client</a></li>
<li class="toctree-l2"><a class="reference internal" href="../examples/deepseek_r1_reasoning_parser.html">Deepseek R1 Reasoning Parser</a></li>
<li class="toctree-l2"><a class="reference internal" href="../examples/genai_perf_client.html">Genai Perf Client</a></li>
<li class="toctree-l2"><a class="reference internal" href="../examples/genai_perf_client_for_multimodal.html">Genai Perf Client For Multimodal</a></li>
<li class="toctree-l2"><a class="reference internal" href="../examples/openai_chat_client.html">OpenAI Chat Client</a></li>
<li class="toctree-l2"><a class="reference internal" href="../examples/openai_chat_client_for_multimodal.html">OpenAI Chat Client for Multimodal</a></li>
<li class="toctree-l2"><a class="reference internal" href="../examples/openai_completion_client.html">OpenAI Completion Client</a></li>
<li class="toctree-l2"><a class="reference internal" href="../examples/openai_completion_client_for_lora.html">Openai Completion Client For Lora</a></li>
<li class="toctree-l2"><a class="reference internal" href="../examples/openai_completion_client_json_schema.html">OpenAI Completion Client with JSON Schema</a></li>
</ul>
</details></li>
<li class="toctree-l1"><a class="reference internal" href="../examples/dynamo_k8s_example.html">Dynamo K8s Example</a></li>
<li class="toctree-l1 has-children"><a class="reference internal" href="../deployment-guide/index.html">Model Recipes</a><details><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul>
<li class="toctree-l2"><a class="reference internal" href="../deployment-guide/quick-start-recipe-for-deepseek-r1-on-trtllm.html">Quick Start Recipe for DeepSeek R1 on TensorRT LLM - Blackwell &amp; Hopper Hardware</a></li>
<li class="toctree-l2"><a class="reference internal" href="../deployment-guide/quick-start-recipe-for-llama3.3-70b-on-trtllm.html">Quick Start Recipe for Llama3.3 70B on TensorRT LLM - Blackwell &amp; Hopper Hardware</a></li>
<li class="toctree-l2"><a class="reference internal" href="../deployment-guide/quick-start-recipe-for-llama4-scout-on-trtllm.html">Quick Start Recipe for Llama4 Scout 17B on TensorRT LLM - Blackwell &amp; Hopper Hardware</a></li>
<li class="toctree-l2"><a class="reference internal" href="../deployment-guide/quick-start-recipe-for-gpt-oss-on-trtllm.html">Quick Start Recipe for GPT-OSS on TensorRT-LLM - Blackwell Hardware</a></li>
</ul>
</details></li>
</ul>
<p aria-level="2" class="caption" role="heading"><span class="caption-text">Models</span></p>
<ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="../models/supported-models.html">Supported Models</a></li>
<li class="toctree-l1"><a class="reference internal" href="../models/adding-new-model.html">Adding a New Model</a></li>
</ul>
<p aria-level="2" class="caption" role="heading"><span class="caption-text">CLI Reference</span></p>
<ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="../commands/trtllm-bench.html">trtllm-bench</a></li>
<li class="toctree-l1"><a class="reference internal" href="../commands/trtllm-eval.html">trtllm-eval</a></li>
<li class="toctree-l1 has-children"><a class="reference internal" href="../commands/trtllm-serve/index.html">trtllm-serve</a><details><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul>
<li class="toctree-l2"><a class="reference internal" href="../commands/trtllm-serve/trtllm-serve.html">trtllm-serve</a></li>
<li class="toctree-l2"><a class="reference internal" href="../commands/trtllm-serve/run-benchmark-with-trtllm-serve.html">Run benchmarking with <code class="docutils literal notranslate"><span class="pre">trtllm-serve</span></code></a></li>
</ul>
</details></li>
</ul>
<p aria-level="2" class="caption" role="heading"><span class="caption-text">API Reference</span></p>
<ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="../llm-api/index.html">LLM API Introduction</a></li>
<li class="toctree-l1"><a class="reference internal" href="../llm-api/reference.html">API Reference</a></li>
</ul>
<p aria-level="2" class="caption" role="heading"><span class="caption-text">Features</span></p>
<ul class="current nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="feature-combination-matrix.html">Feature Combination Matrix</a></li>
<li class="toctree-l1"><a class="reference internal" href="attention.html">Multi-Head, Multi-Query, and Group-Query Attention</a></li>
<li class="toctree-l1"><a class="reference internal" href="disagg-serving.html">Disaggregated Serving (Beta)</a></li>
<li class="toctree-l1"><a class="reference internal" href="kvcache.html">KV Cache System</a></li>
<li class="toctree-l1"><a class="reference internal" href="long-sequence.html">Long Sequences</a></li>
<li class="toctree-l1"><a class="reference internal" href="lora.html">LoRA (Low-Rank Adaptation)</a></li>
<li class="toctree-l1"><a class="reference internal" href="multi-modality.html">Multimodal Support in TensorRT LLM</a></li>
<li class="toctree-l1"><a class="reference internal" href="overlap-scheduler.html">Overlap Scheduler</a></li>
<li class="toctree-l1"><a class="reference internal" href="paged-attention-ifb-scheduler.html">Paged Attention, IFB, and Request Scheduling</a></li>
<li class="toctree-l1"><a class="reference internal" href="parallel-strategy.html">Parallelism in TensorRT LLM</a></li>
<li class="toctree-l1"><a class="reference internal" href="quantization.html">Quantization</a></li>
<li class="toctree-l1"><a class="reference internal" href="sampling.html">Sampling</a></li>
<li class="toctree-l1 current active"><a class="current reference internal" href="#">Speculative Decoding</a></li>
<li class="toctree-l1"><a class="reference internal" href="checkpoint-loading.html">Checkpoint Loading</a></li>
<li class="toctree-l1"><a class="reference internal" href="auto_deploy/auto-deploy.html">AutoDeploy (Prototype)</a></li>
</ul>
<p aria-level="2" class="caption" role="heading"><span class="caption-text">Developer Guide</span></p>
<ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="../architecture/overview.html">Architecture Overview</a></li>
<li class="toctree-l1"><a class="reference internal" href="../developer-guide/perf-analysis.html">Performance Analysis</a></li>
<li class="toctree-l1"><a class="reference internal" href="../developer-guide/perf-benchmarking.html">TensorRT LLM Benchmarking</a></li>
<li class="toctree-l1"><a class="reference internal" href="../developer-guide/ci-overview.html">Continuous Integration Overview</a></li>
<li class="toctree-l1"><a class="reference internal" href="../developer-guide/dev-containers.html">Using Dev Containers</a></li>
</ul>
<p aria-level="2" class="caption" role="heading"><span class="caption-text">Blogs</span></p>
<ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="../blogs/tech_blog/blog10_ADP_Balance_Strategy.html">ADP Balance Strategy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../blogs/tech_blog/blog11_GPT_OSS_Eagle3.html">Running GPT-OSS-120B with Eagle3 Speculative Decoding on GB200/B200 (TensorRT LLM)</a></li>
<li class="toctree-l1"><a class="reference internal" href="../blogs/tech_blog/blog1_Pushing_Latency_Boundaries_Optimizing_DeepSeek-R1_Performance_on_NVIDIA_B200_GPUs.html">Pushing Latency Boundaries: Optimizing DeepSeek-R1 Performance on NVIDIA B200 GPUs</a></li>
<li class="toctree-l1"><a class="reference internal" href="../blogs/tech_blog/blog2_DeepSeek_R1_MTP_Implementation_and_Optimization.html">DeepSeek R1 MTP Implementation and Optimization</a></li>
<li class="toctree-l1"><a class="reference internal" href="../blogs/tech_blog/blog3_Optimizing_DeepSeek_R1_Throughput_on_NVIDIA_Blackwell_GPUs.html">Optimizing DeepSeek R1 Throughput on NVIDIA Blackwell GPUs: A Deep Dive for Developers</a></li>
<li class="toctree-l1"><a class="reference internal" href="../blogs/tech_blog/blog4_Scaling_Expert_Parallelism_in_TensorRT-LLM.html">Scaling Expert Parallelism in TensorRT LLM (Part 1: Design and Implementation of Large-scale EP)</a></li>
<li class="toctree-l1"><a class="reference internal" href="../blogs/tech_blog/blog5_Disaggregated_Serving_in_TensorRT-LLM.html">Disaggregated Serving in TensorRT LLM</a></li>
<li class="toctree-l1"><a class="reference internal" href="../blogs/tech_blog/blog6_Llama4_maverick_eagle_guide.html">How to launch Llama4 Maverick + Eagle3 TensorRT LLM server</a></li>
<li class="toctree-l1"><a class="reference internal" href="../blogs/tech_blog/blog7_NGram_performance_Analysis_And_Auto_Enablement.html">N-GramSpeculativeDecodingin TensorRT LLM</a></li>
<li class="toctree-l1"><a class="reference internal" href="../blogs/tech_blog/blog8_Scaling_Expert_Parallelism_in_TensorRT-LLM_part2.html">Scaling Expert Parallelism in TensorRT LLM (Part 2: Performance Status and Optimization)</a></li>
<li class="toctree-l1"><a class="reference internal" href="../blogs/tech_blog/blog9_Deploying_GPT_OSS_on_TRTLLM.html">Running a High Performance GPT-OSS-120B Inference Server with TensorRT LLM</a></li>
<li class="toctree-l1"><a class="reference internal" href="../blogs/Best_perf_practice_on_DeepSeek-R1_in_TensorRT-LLM.html">How to get best performance on DeepSeek-R1 in TensorRT LLM</a></li>
<li class="toctree-l1"><a class="reference internal" href="../blogs/H200launch.html">H200 achieves nearly 12,000 tokens/sec on Llama2-13B with TensorRT LLM</a></li>
<li class="toctree-l1"><a class="reference internal" href="../blogs/XQA-kernel.html">New XQA-kernel provides 2.4x more Llama-70B throughput within the same latency budget</a></li>
<li class="toctree-l1"><a class="reference internal" href="../blogs/H100vsA100.html">H100 has 4.6x A100 Performance in TensorRT LLM, achieving 10,000 tok/s at 100ms to first token</a></li>
</ul>
<p aria-level="2" class="caption" role="heading"><span class="caption-text">Quick Links</span></p>
<ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference external" href="https://github.com/NVIDIA/TensorRT-LLM/releases">Releases</a></li>
<li class="toctree-l1"><a class="reference external" href="https://github.com/NVIDIA/TensorRT-LLM">Github Code</a></li>
<li class="toctree-l1"><a class="reference external" href="https://github.com/NVIDIA/TensorRT-LLM/issues?q=is%3Aissue%20state%3Aopen%20label%3Aroadmap">Roadmap</a></li>
</ul>
<p aria-level="2" class="caption" role="heading"><span class="caption-text">Use TensorRT Engine</span></p>
<ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="../legacy/tensorrt_quickstart.html">LLM API with TensorRT Engine</a></li>
</ul>
</div>
</nav></div>
</div>
<div class="sidebar-primary-items__end sidebar-primary__section">
</div>
</div>
<main id="main-content" class="bd-main" role="main">
<div class="bd-content">
<div class="bd-article-container">
<div class="bd-header-article d-print-none">
<div class="header-article-items header-article__inner">
<div class="header-article-items__start">
<div class="header-article-item">
<nav aria-label="Breadcrumb" class="d-print-none">
<ul class="bd-breadcrumbs">
<li class="breadcrumb-item breadcrumb-home">
<a href="../index.html" class="nav-link" aria-label="Home">
<i class="fa-solid fa-home"></i>
</a>
</li>
<li class="breadcrumb-item active" aria-current="page"><span class="ellipsis">Speculative Decoding</span></li>
</ul>
</nav>
</div>
</div>
</div>
</div>
<div id="searchbox"></div>
<article class="bd-article">
<section id="speculative-decoding">
<h1>Speculative Decoding<a class="headerlink" href="#speculative-decoding" title="Link to this heading">#</a></h1>
<p>There are two flavors of speculative decoding currently supported in the PyTorch backend:</p>
<ul class="simple">
<li><p>The “one model” implementation a variant which inserts a drafter directly into the model code as a submodule.</p></li>
<li><p>The “two model” implementation a variant which produces draft tokens in the <code class="docutils literal notranslate"><span class="pre">PyExecutor</span></code>. The draft tokens are attached to requests before they are passed
into the target models <code class="docutils literal notranslate"><span class="pre">ModelEngine</span></code>.</p></li>
</ul>
<p>In general, the one model implementation is faster. Its able to achieve better performance in extreme low latency
scenarios because it can launch the entire drafting loop as a single CUDA graph. The trade off is flexibility. The one model implementation
does not support dynamic draft lengths. Additionally, only a subset of models/speculative decoding algorithms support the one model implementation.
The table below enumerates all of the algorithm/model combinations that are supported.</p>
<div class="pst-scrollable-table-container"><table class="table">
<thead>
<tr class="row-odd"><th class="head"><p>Speculative Decoding Algorithm</p></th>
<th class="head"><p>Model</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p>EAGLE 3</p></td>
<td><p>Llama 4 Maverick</p></td>
</tr>
<tr class="row-odd"><td><p>MTP</p></td>
<td><p>Deepseek V3/R1</p></td>
</tr>
<tr class="row-even"><td><p>EAGLE-style MTP</p></td>
<td><p>Deepseek V3/R1</p></td>
</tr>
</tbody>
</table>
</div>
<p>The two model implementation supports the following speculative decoding algorithms:</p>
<div class="pst-scrollable-table-container"><table class="table">
<thead>
<tr class="row-odd"><th class="head"><p>Speculative Decoding Algorithm</p></th>
<th class="head"><p>Model</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p>EAGLE 3</p></td>
<td><p>Llama 4 Maverick, Llama 3.1 8B, Llama 3.3 70B</p></td>
</tr>
<tr class="row-odd"><td><p>Draft/target</p></td>
<td><p>All models</p></td>
</tr>
<tr class="row-even"><td><p>NGram</p></td>
<td><p>All models</p></td>
</tr>
<tr class="row-odd"><td><p>User-provided</p></td>
<td><p>All models</p></td>
</tr>
</tbody>
</table>
</div>
<section id="quick-start">
<h2>Quick Start<a class="headerlink" href="#quick-start" title="Link to this heading">#</a></h2>
<p>For all speculation algorithms, when speculation is enabled, a single sequence of draft tokens with length <code class="docutils literal notranslate"><span class="pre">max_draft_len</span></code> is created for every request. There is currently no way to dynamically disable speculation, thus speed ups are only observable at low batch sizes.</p>
<section id="draft-target">
<h3>Draft/Target<a class="headerlink" href="#draft-target" title="Link to this heading">#</a></h3>
<p>Draft/target is the simplest form of speculative decoding. In this approach, an arbitrary draft model is used to produce draft tokens. It is important to make sure that the draft and target models were trained with the same tokenizer, else the acceptance rate is extremely low and performance is regressed.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span><span class="w"> </span><span class="nn">tensorrt_llm.llmapi</span><span class="w"> </span><span class="kn">import</span> <span class="n">DraftTargetDecodingConfig</span>
<span class="n">speculative_config</span> <span class="o">=</span> <span class="n">DraftTargetDecodingConfig</span><span class="p">(</span>
<span class="n">max_draft_len</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">speculative_model</span><span class="o">=</span><span class="s2">&quot;/path/to/draft_model&quot;</span><span class="p">)</span>
<span class="n">llm</span> <span class="o">=</span> <span class="n">LLM</span><span class="p">(</span><span class="s2">&quot;/path/to/target_model&quot;</span><span class="p">,</span> <span class="n">speculative_config</span><span class="o">=</span><span class="n">speculative_config</span><span class="p">,</span> <span class="n">disable_overlap_scheduler</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</pre></div>
</div>
</section>
<section id="eagle-3">
<h3>EAGLE 3<a class="headerlink" href="#eagle-3" title="Link to this heading">#</a></h3>
<p>The EAGLE 3 algorithm is described in the paper <a class="reference external" href="https://arxiv.org/pdf/2503.01840">EAGLE-3: Scaling up Inference Acceleration of Large Language Models via Training-Time Test</a>.
TRT-LLM supports a modified version of the algorithm presented in the paper: tree structures for draft sequences are not supported. Instead, each request uses a single sequence of draft tokens with length <code class="docutils literal notranslate"><span class="pre">max_draft_len</span></code>.</p>
<p>The following draft model checkpoints can be used for EAGLE 3:</p>
<ul class="simple">
<li><p>Llama 3 variants: <a class="reference external" href="https://huggingface.co/yuhuili">use the checkpoints from the authors of the original EAGLE 3 paper</a>.</p></li>
<li><p>Llama 4 Maverick: <a class="reference external" href="https://huggingface.co/nvidia/Llama-4-Maverick-17B-128E-Eagle3">use the checkpoint from the NVIDIA HuggingFace repository</a>.</p></li>
</ul>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span><span class="w"> </span><span class="nn">tensorrt_llm.llmapi</span><span class="w"> </span><span class="kn">import</span> <span class="n">EagleDecodingConfig</span>
<span class="c1"># Enable to use the faster one-model implementation for Llama 4.</span>
<span class="n">eagle3_one_model</span> <span class="o">=</span> <span class="kc">False</span>
<span class="n">speculative_config</span> <span class="o">=</span> <span class="n">EagleDecodingConfig</span><span class="p">(</span>
<span class="n">max_draft_len</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">speculative_model</span><span class="o">=</span><span class="s2">&quot;/path/to/draft_model&quot;</span><span class="p">,</span> <span class="n">eagle3_one_model</span><span class="o">=</span><span class="n">eagle3_one_model</span><span class="p">)</span>
<span class="c1"># Only need to disable overlap scheduler if eagle3_one_model is False.</span>
<span class="n">llm</span> <span class="o">=</span> <span class="n">LLM</span><span class="p">(</span><span class="s2">&quot;/path/to/target_model&quot;</span><span class="p">,</span> <span class="n">speculative_config</span><span class="o">=</span><span class="n">speculative_config</span><span class="p">,</span> <span class="n">disable_overlap_scheduler</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</pre></div>
</div>
</section>
<section id="ngram">
<h3>NGram<a class="headerlink" href="#ngram" title="Link to this heading">#</a></h3>
<p>The NGram method is an implementation of <a class="reference external" href="https://github.com/apoorvumang/prompt-lookup-decoding">this Prompt Lookup Decoding algorithm</a>.</p>
<p>When the NGram algorithm is used, TRT-LLM will maintain a map from token prefixes to candidate draft sequences. For example, the 3-gram [“The “, “ future “, “ is”] could map to the draft sequence [” bright”, “ because”]. The prefixes are token sequences that are extracted from the prompt and the tokens generated by the target model. The NGram pool and matching procedure can be tuned with the following options:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">max_draft_len</span></code>: Maximum draft candidate length.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">max_matching_ngram_size</span></code>: Maximum prompt suffix length to match with keys in the pool.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">is_public_pool</span></code>: If true, a single ngram pool is shared for all requests. Otherwise, each request has its own ngram pool.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">is_keep_all</span></code>: If true, draft candidates will be retained in the pool forever. Otherwise, only the largest draft candidate is retained.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">is_use_oldest</span></code>: If true, the oldest draft candidate is always proposed for a given match. Otherwise, the newest draft candidate is used. Only applicable if <code class="docutils literal notranslate"><span class="pre">is_keep_all</span> <span class="pre">==</span> <span class="pre">True</span></code> because <code class="docutils literal notranslate"><span class="pre">is_keep_all</span> <span class="pre">==</span> <span class="pre">False</span></code> means well only ever have a single value for each key.</p></li>
</ul>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span><span class="w"> </span><span class="nn">tensorrt_llm.llmapi</span><span class="w"> </span><span class="kn">import</span> <span class="n">NGramDecodingConfig</span>
<span class="n">speculative_config</span> <span class="o">=</span> <span class="n">NGramDecodingConfig</span><span class="p">(</span>
<span class="n">max_draft_len</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">max_matching_ngram_size</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span> <span class="n">is_public_pool</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="n">llm</span> <span class="o">=</span> <span class="n">LLM</span><span class="p">(</span><span class="s2">&quot;/path/to/target_model&quot;</span><span class="p">,</span> <span class="n">speculative_config</span><span class="o">=</span><span class="n">speculative_config</span><span class="p">,</span> <span class="n">disable_overlap_scheduler</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</pre></div>
</div>
</section>
<section id="mtp">
<h3>MTP<a class="headerlink" href="#mtp" title="Link to this heading">#</a></h3>
<p>MTP is currently only supported by Deepseek. MTP can be tuned with the following configuration options:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">max_draft_len</span></code>: Maximum draft candidate length.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">num_nextn_predict_layers</span></code>: Number of MTP modules to use. Currently must match <code class="docutils literal notranslate"><span class="pre">max_draft_len</span></code>.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">use_relaxed_acceptance_for_thinking</span></code>: If true, use relaxed decoding for reasoning models in the thinking phase. In this mode, speculation requirements are relaxed for the thinking phase - a draft token may be accepted if it appears in a candidate set constructed with <code class="docutils literal notranslate"><span class="pre">relaxed_topk</span></code> and <code class="docutils literal notranslate"><span class="pre">relaxed_delta</span></code>.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">relaxed_topk</span></code>: The top K tokens are sampled from the target models logits to create the initial candidate set for relaxed decoding.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">relaxed_delta</span></code>: Used to further filter the top K candidate set for relaxed decoding. We remove tokens <code class="docutils literal notranslate"><span class="pre">t</span></code> for which <code class="docutils literal notranslate"><span class="pre">log(P(top</span> <span class="pre">1</span> <span class="pre">token))</span> <span class="pre">-</span> <span class="pre">log(P(t))</span> <span class="pre">&gt;</span> <span class="pre">relaxed_delta</span></code>.</p></li>
</ul>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span><span class="w"> </span><span class="nn">tensorrt_llm.llmapi</span><span class="w"> </span><span class="kn">import</span> <span class="n">MTPDecodingConfig</span>
<span class="n">speculative_config</span> <span class="o">=</span> <span class="n">MTPDecodingConfig</span><span class="p">(</span>
<span class="n">max_draft_len</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">num_nextn_predict_layers</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
<span class="n">llm</span> <span class="o">=</span> <span class="n">LLM</span><span class="p">(</span><span class="s2">&quot;/path/to/deepseek_model&quot;</span><span class="p">,</span> <span class="n">speculative_config</span><span class="o">=</span><span class="n">speculative_config</span><span class="p">)</span>
</pre></div>
</div>
</section>
<section id="user-provided-drafting">
<h3>User-provided drafting<a class="headerlink" href="#user-provided-drafting" title="Link to this heading">#</a></h3>
<p>A completely user-defined drafting method can be supplied with a <code class="docutils literal notranslate"><span class="pre">UserProvidedDecodingConfig</span></code> that includes</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">max_draft_len</span></code>: Maximum draft candidate length.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">drafter</span></code>: An object of type <code class="docutils literal notranslate"><span class="pre">Drafter</span></code> that implements the <code class="docutils literal notranslate"><span class="pre">prepare_draft_tokens</span></code> method (see <a class="reference internal" href="#developer-guide"><span class="std std-ref">Developer Guide</span></a> 7.)</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">resource_manager</span></code>: An optional <code class="docutils literal notranslate"><span class="pre">ResourceManager</span></code> object (see <a class="reference internal" href="#developer-guide"><span class="std std-ref">Developer Guide</span></a> 4.)</p></li>
</ul>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span><span class="w"> </span><span class="nn">tensorrt_llm.llmapi</span><span class="w"> </span><span class="kn">import</span> <span class="n">UserProvidedDecodingConfig</span>
<span class="n">speculative_config</span> <span class="o">=</span> <span class="n">UserProvidedDecodingConfig</span><span class="p">(</span>
<span class="n">max_draft_len</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">drafter</span><span class="o">=</span><span class="n">MyDrafter</span><span class="p">())</span>
<span class="n">llm</span> <span class="o">=</span> <span class="n">LLM</span><span class="p">(</span><span class="s2">&quot;/path/to/target_model&quot;</span><span class="p">,</span> <span class="n">speculative_config</span><span class="o">=</span><span class="n">speculative_config</span><span class="p">)</span>
</pre></div>
</div>
</section>
</section>
<section id="usage-with-trtllm-bench-and-trtllm-serve">
<h2>Usage with <code class="docutils literal notranslate"><span class="pre">trtllm-bench</span></code> and <code class="docutils literal notranslate"><span class="pre">trtllm-serve</span></code><a class="headerlink" href="#usage-with-trtllm-bench-and-trtllm-serve" title="Link to this heading">#</a></h2>
<p>Speculative decoding options must be specified via <code class="docutils literal notranslate"><span class="pre">--extra_llm_api_options</span> <span class="pre">config.yaml</span></code> for both <code class="docutils literal notranslate"><span class="pre">trtllm-bench</span></code> and <code class="docutils literal notranslate"><span class="pre">trtllm-serve</span></code>. All speculative decoding options can be specified in this YAML file. An additional <code class="docutils literal notranslate"><span class="pre">decoding_type</span></code> option is used to specify the type of speculation to use. The available options are:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">MTP</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">Eagle</span></code> (for EAGLE 3)</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">NGram</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">DraftTarget</span></code></p></li>
</ul>
<p>The rest of the argument names/valid values are the same as in their corresponding configuration class described in the Quick Start section. For example, a YAML configuration could look like this:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">disable_overlap_scheduler</span><span class="p">:</span> <span class="n">true</span>
<span class="n">speculative_config</span><span class="p">:</span>
<span class="n">decoding_type</span><span class="p">:</span> <span class="n">Eagle</span>
<span class="n">max_draft_len</span><span class="p">:</span> <span class="mi">4</span>
<span class="n">speculative_model</span><span class="p">:</span> <span class="o">/</span><span class="n">path</span><span class="o">/</span><span class="n">to</span><span class="o">/</span><span class="n">draft</span><span class="o">/</span><span class="n">model</span>
</pre></div>
</div>
</section>
<section id="developer-guide">
<h2>Developer Guide<a class="headerlink" href="#developer-guide" title="Link to this heading">#</a></h2>
<p>This section describes the components of a speculative decoding algorithm. All of the interfaces are defined in <a class="reference external" href="https://github.com/NVIDIA/TensorRT-LLM/blob/main/tensorrt_llm/_torch/speculative/interface.py"><code class="docutils literal notranslate"><span class="pre">_torch/speculative/interface.py</span></code></a>.</p>
<ol class="arabic simple">
<li><p><code class="docutils literal notranslate"><span class="pre">SpeculativeDecodingMode</span></code>: this is a simple <code class="docutils literal notranslate"><span class="pre">IntEnum</span></code>, one for each supported algorithm. There are a few
nontrivial methods, however.</p></li>
</ol>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">needs_kv_cache_rewind</span></code>. See “KV Cache Rewind” below. In general, this is true for all two model speculative
decoding algorithms.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">extend_ctx</span></code>: If true, the speculative decoding dispatches requests with <code class="docutils literal notranslate"><span class="pre">py_draft_tokens</span></code> attached to them
to the <em>prefill</em> version of the attention kernels. This usually needs to be true. The exception is when youre on
Blackwell using the TensorRT LLM attention backend. In that case, use the generation kernels for better performance.
This optimized kernel has one limitation; all draft lengths must be the same (or padding must be used) in this case.</p></li>
</ul>
<blockquote>
<div><p><em>These may be refactored in the future to reduce the difficulty of adding a new speculative
decoding algorithm. <code class="docutils literal notranslate"><span class="pre">extend_ctx</span></code> in particular is problematic. Ideally, we would move all of the kernel dispatching logic
to a lower level of abstraction.</em></p>
</div></blockquote>
<ol class="arabic simple" start="2">
<li><p><code class="docutils literal notranslate"><span class="pre">SpecMetadata</span></code>: Defines all metadata that should be passed to the model during the forward pass to facilitate speculative decoding.
Each speculative decoding algorithm defines a subclass of <code class="docutils literal notranslate"><span class="pre">SpecMetadata</span></code>. Similar to <code class="docutils literal notranslate"><span class="pre">AttentionMetadata</span></code>, each <code class="docutils literal notranslate"><span class="pre">CUDAGraphRunner</span></code> owns
its own <code class="docutils literal notranslate"><span class="pre">SpecMetadata</span></code>, and CUDA-graph compatible <code class="docutils literal notranslate"><span class="pre">SpecMetadata</span></code> objects may be created by invoking <code class="docutils literal notranslate"><span class="pre">create_cuda_graph_metadata(batch_size)</span></code>.
<code class="docutils literal notranslate"><span class="pre">SpecMetadata</span></code> has many fields. Many of them are exclusively used by the one model implementation. For the two model implementation, the
main purpose of <code class="docutils literal notranslate"><span class="pre">SpecMetadata</span></code> is to facilitate the capture of hidden states. In EAGLE 3, we need to capture hidden states from the
target model to use as draft model inputs. The <code class="docutils literal notranslate"><span class="pre">SpecMetadata</span></code> stores a list of layers to capture and the model calls
<code class="docutils literal notranslate"><span class="pre">maybe_capture_hidden_states(layer_id,</span> <span class="pre">hidden_states,</span> <span class="pre">residual)</span></code> during its forward pass. If the layer ID is in the list of layers to capture,
the hidden states are saved. For CUDA graph compatibility, these may be saved in pre-allocated buffers.</p></li>
</ol>
<p><code class="docutils literal notranslate"><span class="pre">SpecMetadata</span></code> is derived from a <code class="docutils literal notranslate"><span class="pre">SpecConfig</span></code> object in <code class="docutils literal notranslate"><span class="pre">_torch/speculative/utils.py</span></code>. There are a few other optional components created in
this file too:</p>
<ol class="arabic simple" start="4">
<li><p><code class="docutils literal notranslate"><span class="pre">ResourceManager</span></code>: Create a custom resource manager to prepare and free resources before and after target forward passes; see
the section on <code class="docutils literal notranslate"><span class="pre">ResourceManager</span></code> in <code class="docutils literal notranslate"><span class="pre">arch.md</span></code>. This is used by the n-gram method to manage its pool. The one model implementation also uses
<code class="docutils literal notranslate"><span class="pre">ResourceManager</span></code>s to manage hidden states.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">Sampler</span></code>: Each speculative decoding algorithm can optionally create its own sampler. This is mostly used by the one model implementation.
The default <code class="docutils literal notranslate"><span class="pre">TorchSampler</span></code> is used as a fallback if no custom sampler is provided. EAGLE 3 two model also has a simple custom decoder to handle
differences in the draft/target model vocab sizes.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">Worker</span></code>: This is exclusive to the one-model implementation. The <code class="docutils literal notranslate"><span class="pre">Worker</span></code> is the object that gets injected into the target model as a
submodule.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">Drafter</span></code>: All of the logic required to actually produce draft tokens should be implemented in a <code class="docutils literal notranslate"><span class="pre">Drafter</span></code> subclass. There is a single
abstract method, <code class="docutils literal notranslate"><span class="pre">prepare_draft_tokens</span></code>. It takes a set of requests (a <code class="docutils literal notranslate"><span class="pre">ScheduledRequests</span></code> object) and returns nothing. The <a class="reference external" href="https://github.com/NVIDIA/TensorRT-LLM/blob/main/tensorrt_llm/_torch/pyexecutor/py_executor.py#L162"><code class="docutils literal notranslate"><span class="pre">PyExecutor</span></code></a> expects
draft tokens to be attached to the <code class="docutils literal notranslate"><span class="pre">py_draft_tokens</span></code> field of request that speculation is to be done for.</p></li>
</ol>
</section>
<section id="two-model-speculative-decoding-architecture">
<h2>Two Model Speculative Decoding Architecture<a class="headerlink" href="#two-model-speculative-decoding-architecture" title="Link to this heading">#</a></h2>
<p>Two-model based speculation implementations do not support overlap scheduler. It will be disabled automatically.</p>
<p>In this approach, there are two new steps to the <code class="docutils literal notranslate"><span class="pre">PyExecutor</span></code>s <code class="docutils literal notranslate"><span class="pre">_executor_loop</span></code>.</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">_prepare_draft_requests</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">_prepare_draft_tokens</span></code></p></li>
</ul>
<section id="prepare-draft-requests">
<h3><code class="docutils literal notranslate"><span class="pre">_prepare_draft_requests</span></code><a class="headerlink" href="#prepare-draft-requests" title="Link to this heading">#</a></h3>
<p>This stage occurs for all speculative decoding algorithms before scheduling. The purpose
of this stage is to make the KV cache and scheduler aware of the fact that speculative decoding
will occur. Draft tokens take up extra KV cache pages and count towards the executors
<code class="docutils literal notranslate"><span class="pre">max_num_tokens</span></code> limit. Thus, we need a way to tell the scheduler that drafting will occur
<strong>before we do the scheduling</strong>.</p>
<p>To achieve this, we simply attach the maximum number of draft tokens to each request. The
scheduler and KV cache manager will automatically account for tokens attached to the
<code class="docutils literal notranslate"><span class="pre">py_draft_tokens</span></code> attribute.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">for</span> <span class="n">req</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">active_requests</span><span class="p">:</span>
<span class="n">req</span><span class="o">.</span><span class="n">py_draft_tokens</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="n">max_draft_len</span>
</pre></div>
</div>
</section>
<section id="prepare-draft-tokens">
<h3><code class="docutils literal notranslate"><span class="pre">_prepare_draft_tokens</span></code><a class="headerlink" href="#prepare-draft-tokens" title="Link to this heading">#</a></h3>
<p>This stage occurs after scheduling and KV cache allocation. The purpose of this stage
is to attach draft tokens to the <code class="docutils literal notranslate"><span class="pre">py_draft_tokens</span></code> attribute. This occurs by calling <code class="docutils literal notranslate"><span class="pre">self.drafter.prepare_draft_tokens</span></code>;
each speculative decoding algorithm should have a concrete instance of the <code class="docutils literal notranslate"><span class="pre">Drafter</span></code> class associated with it that defines
the drafting logic.</p>
<p>In addition to producing all “real” draft tokens, <code class="docutils literal notranslate"><span class="pre">_prepare_draft_tokens</span></code> currently must also pad
all <code class="docutils literal notranslate"><span class="pre">py_draft_tokens</span></code> to the maximum draft length. This is a CUDA graph limitation - the target
model captures its CUDA graphs using the maximum number of draft tokens on each request.</p>
</section>
<section id="verification-and-sampling">
<h3>Verification and Sampling<a class="headerlink" href="#verification-and-sampling" title="Link to this heading">#</a></h3>
<p>Once the draft tokens are obtained, the target model runs a forward pass through the usual flow.
Everything is the same, except that the logits for all the draft tokens are returned and passed
to the sampler.</p>
<p>Currently, only greedy sampling is supported for speculative decoding. A draft token is accepted if
matches the previously decoded token exactly. For example, suppose there is a generation request
<code class="docutils literal notranslate"><span class="pre">[t,</span> <span class="pre">d1,</span> <span class="pre">d2,</span> <span class="pre">d3]</span></code>, where <code class="docutils literal notranslate"><span class="pre">d1</span></code>, <code class="docutils literal notranslate"><span class="pre">d2</span></code>, and <code class="docutils literal notranslate"><span class="pre">d3</span></code> are drat tokens. Suppose the token after <code class="docutils literal notranslate"><span class="pre">t</span></code> is <code class="docutils literal notranslate"><span class="pre">d1</span></code>
(determined with the <code class="docutils literal notranslate"><span class="pre">argmax</span></code> of the logits). <code class="docutils literal notranslate"><span class="pre">d1</span></code> is then accepted. If the token after <code class="docutils literal notranslate"><span class="pre">d1</span></code> is <code class="docutils literal notranslate"><span class="pre">d2</span></code>,
then <code class="docutils literal notranslate"><span class="pre">d2</span></code> can be accepted. And so on until draft tokens cannot be accepted anymore.</p>
</section>
<section id="kv-cache-rewind">
<h3>KV Cache Rewind<a class="headerlink" href="#kv-cache-rewind" title="Link to this heading">#</a></h3>
<p>KV cache space allocated to rejected tokens is freed before the next iteration. This is achieved by setting
the <code class="docutils literal notranslate"><span class="pre">request.py_rewind_len</span></code> attribute to <code class="docutils literal notranslate"><span class="pre">num_draft_tokens_allocated</span> <span class="pre">-</span> <span class="pre">num_accepted_tokens</span></code>. The pages are
freed as part of the <code class="docutils literal notranslate"><span class="pre">resource_manager.free_resources</span></code> routine.</p>
<p>The purpose of KV cache rewind is to avoid complicated page reuse logic in the KV cache managers <code class="docutils literal notranslate"><span class="pre">prepare_resources</span></code>
function. In practice, this is very cheap since the blocks are just marked as available; no memory is actually freed.</p>
</section>
</section>
</section>
</article>
<footer class="prev-next-footer d-print-none">
<div class="prev-next-area">
<a class="left-prev"
href="sampling.html"
title="previous page">
<i class="fa-solid fa-angle-left"></i>
<div class="prev-next-info">
<p class="prev-next-subtitle">previous</p>
<p class="prev-next-title">Sampling</p>
</div>
</a>
<a class="right-next"
href="checkpoint-loading.html"
title="next page">
<div class="prev-next-info">
<p class="prev-next-subtitle">next</p>
<p class="prev-next-title">Checkpoint Loading</p>
</div>
<i class="fa-solid fa-angle-right"></i>
</a>
</div>
</footer>
</div>
<dialog id="pst-secondary-sidebar-modal"></dialog>
<div id="pst-secondary-sidebar" class="bd-sidebar-secondary bd-toc"><div class="sidebar-secondary-items sidebar-secondary__inner">
<div class="sidebar-secondary-item">
<div
id="pst-page-navigation-heading-2"
class="page-toc tocsection onthispage">
<i class="fa-solid fa-list"></i> On this page
</div>
<nav class="bd-toc-nav page-toc" aria-labelledby="pst-page-navigation-heading-2">
<ul class="visible nav section-nav flex-column">
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#quick-start">Quick Start</a><ul class="nav section-nav flex-column">
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#draft-target">Draft/Target</a></li>
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#eagle-3">EAGLE 3</a></li>
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#ngram">NGram</a></li>
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#mtp">MTP</a></li>
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#user-provided-drafting">User-provided drafting</a></li>
</ul>
</li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#usage-with-trtllm-bench-and-trtllm-serve">Usage with <code class="docutils literal notranslate"><span class="pre">trtllm-bench</span></code> and <code class="docutils literal notranslate"><span class="pre">trtllm-serve</span></code></a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#developer-guide">Developer Guide</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#two-model-speculative-decoding-architecture">Two Model Speculative Decoding Architecture</a><ul class="nav section-nav flex-column">
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#prepare-draft-requests"><code class="docutils literal notranslate"><span class="pre">_prepare_draft_requests</span></code></a></li>
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#prepare-draft-tokens"><code class="docutils literal notranslate"><span class="pre">_prepare_draft_tokens</span></code></a></li>
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#verification-and-sampling">Verification and Sampling</a></li>
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#kv-cache-rewind">KV Cache Rewind</a></li>
</ul>
</li>
</ul>
</nav></div>
</div></div>
</div>
<footer class="bd-footer-content">
</footer>
</main>
</div>
</div>
<!-- Scripts loaded after <body> so the DOM is not blocked -->
<script defer src="../_static/scripts/bootstrap.js?digest=8878045cc6db502f8baf"></script>
<script defer src="../_static/scripts/pydata-sphinx-theme.js?digest=8878045cc6db502f8baf"></script>
<footer class="bd-footer">
<div class="bd-footer__inner bd-page-width">
<div class="footer-items__start">
<div class="footer-item">
<a class="footer-brand logo" href="https://www.nvidia.com">
<img src="../_static/nvidia-logo-horiz-rgb-1c-blk-for-screen.svg" class="logo__image only-light" alt="NVIDIA"/>
<img src="../_static/nvidia-logo-horiz-rgb-1c-wht-for-screen.svg" class="logo__image only-dark" alt="NVIDIA"/>
</a></div>
<div class="footer-item">
<div class="footer-links">
<a class="external" href="https://www.nvidia.com/en-us/about-nvidia/privacy-policy/">Privacy Policy</a>
|
<a class="external" href="https://www.nvidia.com/en-us/about-nvidia/privacy-center/">Manage My Privacy</a>
|
<a class="external" href="https://www.nvidia.com/en-us/preferences/start/">Do Not Sell or Share My Data</a>
|
<a class="external" href="https://www.nvidia.com/en-us/about-nvidia/terms-of-service/">Terms of Service</a>
|
<a class="external" href="https://www.nvidia.com/en-us/about-nvidia/accessibility/">Accessibility</a>
|
<a class="external" href="https://www.nvidia.com/en-us/about-nvidia/company-policies/">Corporate Policies</a>
|
<a class="external" href="https://www.nvidia.com/en-us/product-security/">Product Security</a>
|
<a class="external" href="https://www.nvidia.com/en-us/contact/">Contact</a>
</div>
</div>
<div class="footer-item">
<p class="copyright">
Copyright © 2025, NVidia.
<br/>
</p>
</div>
<div class="footer-item">
<div class="extra_footer">
<p>Last updated on September 15, 2025.</p>
<p>This page is generated by TensorRT-LLM commit <a href="https://github.com/NVIDIA/TensorRT-LLM/tree/0c9430e">0c9430e</a>.</p>
</div></div>
</div>
</div>
</footer>
</body>
</html>