# Speculative decoding

> A draft model predicts several next tokens so a target language model can check them in one pass. When the guesses are good, the target can commit multiple tokens per pass.

A draft model predicts several next tokens so a target language model can check them in one pass. When the guesses are good, the target can commit multiple tokens per pass.

## Result

> 39.1% paired-latency reduction on one NVIDIA A10 for a 260K-parameter toy target and a zero-cost n-gram draft; Qwen 0.5B/1.5B was 37.4% slower

## Modules

* [`knotkit/distribution`](/modules/distribution/): Computes probabilities, quantiles, moments, and seeded draws for named and caller-defined distributions
* [`knotkit/prng`](/modules/prng/): Generates seeded pseudorandom numbers and unbiased integers for reproducible simulations and tests

## Figures

* With up to 16 guesses per batch, a matching draft needs 121 rounds for 2,048 output tokens.
* A biased draft needs 533 rounds and keeps only 17.8% of its guesses.
* CPU simulation with three possible tokens and seed 1729. Each round represents one verification batch.

Hover or focus a chart for exact values. Drawn by [`demo.ts`](https://github.com/jkomyno/knotkit/blob/main/problems/speculative-decoding/demo.ts) at build time, from the fixtures the benchmark uses; it reproduces the README figure.

## Reproduce

```bash
git clone https://github.com/jkomyno/knotkit.git && cd knotkit && bun install
bun run demo:problem:speculative-decoding
```

[`problems/speculative-decoding/README.md`](https://github.com/jkomyno/knotkit/blob/main/problems/speculative-decoding/README.md) states the fixture, the seed, and the result table.
