|
7 | 7 | Xinference supports multiple backends for different models. After the user specifies the model, |
8 | 8 | xinference will automatically select the appropriate backend. |
9 | 9 |
|
| 10 | +.. _llama_cpp_backend: |
| 11 | + |
10 | 12 | llama.cpp |
11 | 13 | ========= |
12 | 14 |
|
@@ -196,3 +198,120 @@ MLX |
196 | 198 | `MLX <https://github.com/ml-explore/mlx-examples/tree/main/llms>`_ provides efficient runtime |
197 | 199 | to run LLM on Apple silicon. It's recommended to use for Mac users when running on Apple silicon |
198 | 200 | if the model has MLX format support. |
| 201 | + |
| 202 | + |
| 203 | +.. _speculative_decoding: |
| 204 | + |
| 205 | +Speculative decoding |
| 206 | +==================== |
| 207 | +Some models ship a small paired drafter checkpoint that predicts several tokens |
| 208 | +ahead, which the target model then verifies in one pass. Output is unchanged, |
| 209 | +decoding gets faster. Gemma 4 calls this multi-token prediction (MTP) and |
| 210 | +publishes a ``*-it-assistant`` drafter for every variant. |
| 211 | + |
| 212 | +Pass ``--enable_mtp true`` at launch to download the drafter declared by the |
| 213 | +model spec and run it alongside the target model: |
| 214 | + |
| 215 | +.. code-block:: bash |
| 216 | +
|
| 217 | + xinference launch --model-name gemma-4 --model-engine vllm \ |
| 218 | + --model-format pytorch --size-in-billions 12 --quantization none \ |
| 219 | + --enable_mtp true |
| 220 | +
|
| 221 | +In the Web UI the same options live under *Advanced Configuration → Speculative |
| 222 | +Decoding*, which only appears for a format/size that actually ships a drafter. |
| 223 | + |
| 224 | +Optional parameters: |
| 225 | + |
| 226 | +* ``--num_speculative_tokens <n>``: how many tokens the drafter proposes per |
| 227 | + round, including the bonus token. Left unset, MLX reads it from the drafter, |
| 228 | + which runs at the depth it was trained for (``4`` for Gemma 4). Gemma 4 on |
| 229 | + llama.cpp, vLLM, and SGLang follows the same model-size recipe: ``2`` for |
| 230 | + E2B, ``4`` for E4B and 26B-A4B, and the lower end (``4``) of the recommended |
| 231 | + ``4-8`` range for 12B and 31B. Other llama.cpp models keep xllamacpp's own |
| 232 | + default. |
| 233 | +* ``--draft_quantization <quantization>``: which drafter conversion to use, when |
| 234 | + the spec declares more than one — the MLX build of Gemma 4 12B publishes |
| 235 | + eight. Defaults to the first declared, which is the least quantized one: a |
| 236 | + drafter is small and quantizing it costs acceptance rate, so pairing a |
| 237 | + quantized target with an unquantized drafter is the recommended setup. |
| 238 | +* ``--draft_model_path <path>``: use a local drafter instead of the one declared |
| 239 | + by the spec. Implies ``--enable_mtp true``. |
| 240 | + |
| 241 | +The drafter must match the target model family and size — it shares the target's |
| 242 | +KV cache, so a mismatched checkpoint is rejected rather than silently degrading. |
| 243 | + |
| 244 | +Engine support: |
| 245 | + |
| 246 | +.. list-table:: |
| 247 | + :header-rows: 1 |
| 248 | + :widths: 18 32 50 |
| 249 | + |
| 250 | + * - Engine |
| 251 | + - Requirement |
| 252 | + - Notes |
| 253 | + * - :ref:`vLLM <vllm_backend>` |
| 254 | + - ``vllm>=0.22.0``, ``transformers>=5.8.0`` |
| 255 | + - Translated into ``speculative_config`` with ``method: mtp``. An |
| 256 | + explicitly provided ``speculative_config`` is left untouched. Older |
| 257 | + vLLM treats the drafter as a generic draft model, while older |
| 258 | + Transformers does not recognize ``gemma4_assistant``; either case is |
| 259 | + rejected before engine initialization. Virtual-environment launches |
| 260 | + also synchronize ``flashinfer-cubin`` with ``flashinfer-python`` before |
| 261 | + starting vLLM, repairing stale environments that contain mismatched |
| 262 | + FlashInfer packages. |
| 263 | + * - :ref:`SGLang <sglang_backend>` |
| 264 | + - ``sglang==0.5.13.post1``, ``transformers==5.8.1`` |
| 265 | + - Translated into ``--speculative-algorithm NEXTN`` with the matching |
| 266 | + ``speculative_num_steps`` / ``speculative_num_draft_tokens``. An |
| 267 | + explicitly provided ``speculative_algorithm`` is left untouched. |
| 268 | + * - :ref:`MLX <mlx_backend>` |
| 269 | + - ``mlx-vlm>=0.5.0`` (``>=0.6.1`` for Gemma 4 12B) |
| 270 | + - Served by the MLX vision engine, the one that runs multimodal models |
| 271 | + such as Gemma 4. The drafter is validated against the target when the |
| 272 | + model loads. |
| 273 | + * - :ref:`llama.cpp <llama_cpp_backend>` |
| 274 | + - ``xllamacpp>=2026.6.9713`` |
| 275 | + - Translated into the ``draft-mtp`` speculative implementation. The |
| 276 | + drafter is a single gguf published inside the target's own repository, |
| 277 | + so its quantizations are its own (``BF16``, ``F16``, ``Q8_0`` for Gemma |
| 278 | + 4) and independent of the target's. Earlier llama.cpp builds do not |
| 279 | + know the ``gemma4-assistant`` architecture and cannot load it. |
| 280 | + |
| 281 | +Not supported by the Transformers engine: it runs its own continuous-batching |
| 282 | +loop rather than ``generate()``, so there is nowhere to attach a drafter. |
| 283 | + |
| 284 | +When it pays off |
| 285 | +---------------- |
| 286 | +Speculation is only worth it when a drafting step is cheap *relative to* a |
| 287 | +target decoding step. The drafter is a small dense model whose cost does not |
| 288 | +change with the target, so the ratio is what decides the outcome — and a |
| 289 | +mixture-of-experts target can land on the wrong side of it, because only its |
| 290 | +activated slice is read per token: |
| 291 | + |
| 292 | +.. list-table:: |
| 293 | + :header-rows: 1 |
| 294 | + :widths: 34 22 22 22 |
| 295 | + |
| 296 | + * - gemma-4 MLX, 4bit, M5 Pro |
| 297 | + - Without a drafter |
| 298 | + - With MTP |
| 299 | + - Accepted per round |
| 300 | + * - 31B (dense, 18.4 GB read per token) |
| 301 | + - 14.9 tok/s |
| 302 | + - **31.0 tok/s** (2.1x) |
| 303 | + - 2.08 of 4 |
| 304 | + * - 26B-A4B (MoE, ~2.2 GB read per token) |
| 305 | + - 73.2 tok/s |
| 306 | + - 65.9 tok/s (0.9x) |
| 307 | + - 1.40 of 4 |
| 308 | + |
| 309 | +The 0.83 GB drafter costs about 5% of a 31B decoding step but 39% of a |
| 310 | +26B-A4B one, so on the MoE the three drafting steps of a round already exceed |
| 311 | +one plain decoding step — and the round has to win that back from a lower |
| 312 | +acceptance rate. The MoE is still the faster model here in absolute terms; it |
| 313 | +simply has no headroom left for speculation. |
| 314 | + |
| 315 | +So measure before leaving it on, and note that the verification step itself is |
| 316 | +not the problem: a four-token forward costs only ~40% more than a single-token |
| 317 | +one on either model. |
0 commit comments