> ## Documentation Index
> Fetch the complete documentation index at: https://miles.radixark.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Speculative Decoding

> Draft + target speculative rollout, with online SFT for MTP-style drafts.

Speculative decoding accelerates rollout by letting a lightweight draft model
generate ahead a few tokens and then verifying them with a single batched
forward of the target model. When the draft is correct the target produces N
tokens for the cost of one forward pass.

## Enabling speculative decoding

For models with built-in MTP (Multi-Token Prediction) layers (GLM-4.7,
DeepSeek-V3, DeepSeek-R1):

```bash theme={null}
SGLANG_ARGS+=(
   --sglang-speculative-algorithm EAGLE
   --sglang-speculative-num-steps 3
   --sglang-speculative-eagle-topk 1
   --sglang-speculative-num-draft-tokens 4
)
```

These are passthrough flags forwarded to SGLang. Miles auto-enables
`enable_draft_weights_cpu_backup` so SGLang can run training without
MTP weights resident on GPU
(`miles/backends/sglang_utils/sglang_engine.py`).

For an externally trained draft model (for example, trained with
[SpecForge](https://docs.sglang.ai/SpecForge/)):

```bash theme={null}
SGLANG_ARGS+=(
   --sglang-speculative-draft-model-path /data/draft_model/
)
```

Full reference: [SGLang speculative decoding docs](https://docs.sglang.ai/advanced_features/speculative_decoding.html).

## Drift over a long RL run

As RL training progresses, the target model's distribution shifts away from the
draft. Fewer draft tokens pass verification, and over many steps speculative
decoding can become a net negative because the wasted draft compute outweighs
the verified speedup.

Miles supports training the draft alongside the target through online MTP-SFT.

## Online SFT for MTP-style draft models

```bash theme={null}
PERF_ARGS+=(
   --mtp-num-layers 1
   --enable-mtp-training
   --mtp-loss-scaling-factor 0.2
)
```

| Flag | Notes |
| - | - |
| `--mtp-num-layers` | MTP layers the model has (1 matches GLM/DeepSeek release defaults). Built only with `--enable-mtp-training`. |
| `--enable-mtp-training` | Build and train the MTP layers alongside the policy (Megatron only). Without it no MTP layer is built, so exported checkpoints carry no MTP weights. |
| `--mtp-loss-scaling-factor` | Weight of the MTP loss in the combined gradient (default `0.2`). |

> [!NOTE]
>
> **Checkpoint must contain MTP weights.** Pass `--mtp-num-layers 1` when running `convert_hf_to_torch_dist.py`.
> Without it the resulting `torch_dist` checkpoint will not contain the MTP
> layer to train.

## External draft model SFT

Training an external (non-MTP) draft model online is not yet supported in
Miles. The current path is to retrain the external draft offline every N
rollouts and reload it.

## Pairs with

* [Low Precision RL](https://miles.radixark.com/docs/advanced/low-precision.md). Draft and target both quantized the
  same way.
* [INT4 QAT](https://miles.radixark.com/docs/advanced/int4-qat.md). A quantized draft is cheaper to verify.
* [R3](https://miles.radixark.com/docs/advanced/miles-router.md). R3 captures routing for the verified tokens emitted
  by the target.

## When to skip

* Rollout-bound on dense models below \~13 B. The verification overhead can
  outweigh the benefit.
* Already at high draft acceptance and the bottleneck is verification compute,
  not generation.

## Reading

* SpecForge: [SGLang docs](https://docs.sglang.ai/SpecForge/).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.