Skip to content

Make the drafter probabilistic and the target verify by rejection sampling for simple draft and MTP - #27694

Merged
ggerganov merged 9 commits into
ggml-org:masterfrom
praneshgo:pgonegandla/spec-draft-rejection-sampling
Oct 2, 2026
Merged

ggerganov merged 9 commits into
ggml-org:masterfrom
praneshgo:pgonegandla/spec-draft-rejection-sampling

Conversation

@praneshgo

@praneshgo praneshgo commented Aug 25, 2026 •

Copy link
Copy Markdown
Contributor

Overview

Today the model-based drafters throw away most of what the draft model knows. They run the sampler, discard its result, and take the top candidate, and the target then accepts that token only when its own draw lands on the same one. At temperature 0 that is the best available, since the target also always takes its top token. Above 0, they differ - the target samples while the draft still offers its single safest guess - and acceptance falls away as temperature rises.

This PR adds two things to make this better.

  • Make the target use rejection sampling instead of requiring an exact match.
  • Despite the above, if the draft still gives its top candidate only, it evaluates to min(1, p(x)/1), same as p(x). Hence to make q(x) land, we make the draft probabilistic as well. This time the effective probability that a candidate is chosen from draft is sum_x(q(x)*min(1, p(x)/q(x))).

The output distribution is preserved exactly. Accepting with min(1, p/q) and drawing the remainder from norm(max(0, p - q)) is the standard construction; the residual cancels the over-delivery on tokens where the draft is more generous than the target. This is same as the mechanism in Leviathan et al.

This PR applies to draft_simple and MTP only. Eagle3 is yet to be evaluated and DFlash is experimentally seen not to benefit with these changes. The likely reason for this is structural. DFlash reads every position of its block out of a single forward pass with no re-decode between them, so the positions are predicted jointly rather than conditioned on one another. Substituting a sampled token at one position leaves the rest of the block continuing from a token that was never drafted. The gain at that position is cancelled by the positions after it, which no longer follow from what was drafted — unlike draft-simple and draft-mtp, where each token is re-decoded and the substitution carries forward.

A flag --spec-draft-sampling is added, controlling how the draft is sampled. It takes greedy or probabilistic, defaulting to greedy. This matches vLLM's convention, where draft_sample_method takes the same two values and also defaults to greedy.

I have run sweeps with -bs enabled and disabled along with the changes in this PR. Based on the logs obtained, this PR does not affect the functionality of backend sampling, which ensures that the target sampling chain already happens on the GPU. With -bs on, I see the same accepted length and 1-2% change in throughput, which is consistent with what is observed on the master branch with -bs enabled.

Performance

Two machines, eight model configurations each, 11 temperatures from 0.0 to 1.0. Every run is 12 prompts of 256 tokens at --spec-draft-n-max 3, the shipped default.
Temperature 0.0 is the control. Both arms emit identical tokens there, so accepted length reads exactly +0.00% - the throughput figure on that row is the timing noise floor for that model.

RTX 5090

Llama-3.1-8B + 3B-Q8_0 - draft-simple

temp accepted length throughput
0.0 +0.00% +5.20%
0.1 +1.93% +4.21%
0.2 +0.15% +0.07%
0.3 +0.67% +1.29%
0.4 +3.19% +2.77%
0.5 +5.87% +6.12%
0.6 +11.52% +11.81%
0.7 +12.38% +12.57%
0.8 +14.35% +15.40%
0.9 +15.33% +16.61%
1.0 +15.34% +15.19%
mean 0.1-1.0 +8.07% +8.60%
mean 0.5-1.0 +12.47% +12.95%

Llama-3.1-8B + 3B-Q4_K_M - draft-simple

temp accepted length throughput
0.0 +0.00% +0.20%
0.1 +2.16% +1.23%
0.2 +0.70% +2.93%
0.3 +5.66% +3.97%
0.4 +4.26% +3.08%
0.5 +8.50% +8.54%
0.6 +9.46% +7.99%
0.7 +11.23% +11.54%
0.8 +8.20% +12.78%
0.9 +15.88% +16.07%
1.0 +12.67% +12.04%
mean 0.1-1.0 +7.87% +8.02%
mean 0.5-1.0 +10.99% +11.49%

Llama-3.1-8B + 3B-IQ2_XS - draft-simple

temp accepted length throughput
0.0 +0.00% -0.30%
0.1 -2.44% -2.99%
0.2 -0.61% -0.67%
0.3 +7.17% +7.46%
0.4 +7.39% +6.89%
0.5 +5.23% +4.99%
0.6 +4.09% +3.64%
0.7 +8.05% +8.21%
0.8 +7.89% +9.40%
0.9 +8.99% +10.28%
1.0 +8.05% +8.31%
mean 0.1-1.0 +5.38% +5.55%
mean 0.5-1.0 +7.05% +7.47%

Qwen3.5-4B - draft-mtp

temp accepted length throughput
0.0 +0.00% +0.00%
0.1 +0.25% +0.72%
0.2 +0.93% -0.06%
0.3 +0.36% +0.00%
0.4 +1.89% +2.02%
0.5 +4.74% +5.51%
0.6 +5.84% +5.12%
0.7 +5.93% +5.16%
0.8 +5.27% +4.62%
0.9 +4.57% +3.65%
1.0 +8.34% +8.95%
mean 0.1-1.0 +3.81% +3.57%
mean 0.5-1.0 +5.78% +5.50%

Qwen3.5-9B - draft-mtp

temp accepted length throughput
0.0 +0.00% +1.88%
0.1 +0.76% +0.12%
0.2 +3.28% +3.28%
0.3 +3.50% +1.79%
0.4 +4.60% +2.73%
0.5 +5.48% +4.28%
0.6 +4.91% +5.39%
0.7 +13.31% +12.46%
0.8 +7.90% +9.10%
0.9 +8.89% +5.47%
1.0 +11.98% +11.74%
mean 0.1-1.0 +6.46% +5.64%
mean 0.5-1.0 +8.75% +8.07%

Ornith-1.5-9B - draft-mtp

temp accepted length throughput
0.0 +0.00% -0.04%
0.1 -0.34% -0.38%
0.2 +0.63% +0.95%
0.3 +0.50% -3.90%
0.4 +2.47% +3.13%
0.5 +2.80% +2.35%
0.6 +1.20% +0.47%
0.7 +5.38% +4.95%
0.8 +8.18% +11.22%
0.9 +11.37% +9.90%
1.0 +7.91% +2.00%
mean 0.1-1.0 +4.01% +3.07%
mean 0.5-1.0 +6.14% +5.15%

Qwen3.6-27B - draft-mtp

temp accepted length throughput
0.0 +0.00% -3.22%
0.1 +2.21% -1.30%
0.2 -0.97% -3.49%
0.3 +2.40% +3.34%
0.4 +2.65% +4.73%
0.5 +3.09% +3.36%
0.6 +8.51% +8.18%
0.7 +7.63% +6.67%
0.8 +6.96% +7.13%
0.9 +8.03% +3.01%
1.0 +7.38% +9.49%
mean 0.1-1.0 +4.79% +4.11%
mean 0.5-1.0 +6.93% +6.31%

Qwen3.6-35B-A3B - draft-mtp

temp accepted length throughput
0.0 +0.00% -1.23%
0.1 +0.33% +1.63%
0.2 +0.87% +0.40%
0.3 +2.33% +2.39%
0.4 +5.11% +2.03%
0.5 +5.17% +4.52%
0.6 +5.93% +5.19%
0.7 +6.75% +4.84%
0.8 +4.23% +3.89%
0.9 +4.47% +8.15%
1.0 +8.54% +8.95%
mean 0.1-1.0 +4.37% +4.20%
mean 0.5-1.0 +5.85% +5.92%

DGX Spark

Llama-3.1-8B + 3B-Q8_0 - draft-simple

temp accepted length throughput
0.0 +0.00% -0.54%
0.1 +0.61% -0.27%
0.2 +2.43% +2.19%
0.3 +1.37% +1.09%
0.4 +3.41% +1.89%
0.5 +6.01% +5.70%
0.6 +6.24% +5.98%
0.7 +5.50% +4.69%
0.8 +10.90% +11.28%
0.9 +14.22% +15.36%
1.0 +15.68% +17.13%
mean 0.1-1.0 +6.64% +6.50%
mean 0.5-1.0 +9.76% +10.02%

Llama-3.1-8B + 3B-Q4_K_M - draft-simple

temp accepted length throughput
0.0 +0.00% -1.26%
0.1 +0.98% +1.06%
0.2 +1.96% +1.85%
0.3 +4.00% +3.38%
0.4 +0.81% +1.06%
0.5 +6.08% +6.18%
0.6 +7.21% +7.85%
0.7 +10.46% +10.18%
0.8 +16.97% +17.51%
0.9 +17.96% +18.31%
1.0 +17.38% +20.05%
mean 0.1-1.0 +8.38% +8.74%
mean 0.5-1.0 +12.68% +13.35%

Llama-3.1-8B + 3B-IQ2_XS - draft-simple

temp accepted length throughput
0.0 +0.00% +0.38%
0.1 -0.91% -1.30%
0.2 +0.25% +0.38%
0.3 +2.60% +3.24%
0.4 +2.79% +2.29%
0.5 +2.96% +2.49%
0.6 +5.18% +5.21%
0.7 +4.29% +4.10%
0.8 +13.87% +14.11%
0.9 +9.87% +10.32%
1.0 +12.63% +14.38%
mean 0.1-1.0 +5.35% +5.52%
mean 0.5-1.0 +8.13% +8.43%

Qwen3.5-4B - draft-mtp

temp accepted length throughput
0.0 +0.00% +0.00%
0.1 -1.86% -0.72%
0.2 +0.28% +0.31%
0.3 +7.29% +5.05%
0.4 +4.70% +4.64%
0.5 +6.48% +6.81%
0.6 +5.34% +5.30%
0.7 +4.64% +4.94%
0.8 +8.59% +12.05%
0.9 +9.22% +12.91%
1.0 +12.54% +11.18%
mean 0.1-1.0 +5.72% +6.25%
mean 0.5-1.0 +7.80% +8.86%

Qwen3.5-9B - draft-mtp

temp accepted length throughput
0.0 +0.00% +0.32%
0.1 +1.86% +3.78%
0.2 +3.33% +2.80%
0.3 +6.03% +5.42%
0.4 +5.97% +5.31%
0.5 +7.54% +7.19%
0.6 +8.56% +5.69%
0.7 +13.94% +11.51%
0.8 +8.86% +8.47%
0.9 +10.23% +9.45%
1.0 +11.43% +12.78%
mean 0.1-1.0 +7.78% +7.24%
mean 0.5-1.0 +10.09% +9.18%

Ornith-1.5-9B - draft-mtp

temp accepted length throughput
0.0 +0.00% -0.51%
0.1 +1.85% +5.03%
0.2 +2.19% +0.34%
0.3 +1.45% +1.20%
0.4 +2.80% +2.69%
0.5 +4.30% +1.35%
0.6 +4.81% +2.79%
0.7 +3.57% +3.70%
0.8 +7.19% +8.24%
0.9 +5.67% +5.35%
1.0 +8.54% +6.75%
mean 0.1-1.0 +4.24% +3.75%
mean 0.5-1.0 +5.68% +4.70%

Qwen3.6-27B - draft-mtp

temp accepted length throughput
0.0 +0.00% +0.36%
0.1 +0.66% +0.37%
0.2 +1.56% +1.50%
0.3 +1.05% +0.00%
0.4 +3.25% +3.75%
0.5 +3.51% +3.73%
0.6 +4.04% +5.22%
0.7 +2.61% +2.20%
0.8 +2.90% +2.61%
0.9 +4.06% +5.02%
1.0 +3.44% +3.89%
mean 0.1-1.0 +2.71% +2.83%
mean 0.5-1.0 +3.43% +3.78%

Qwen3.6-35B-A3B - draft-mtp

temp accepted length throughput
0.0 +0.00% +1.57%
0.1 +1.03% +0.67%
0.2 +2.15% +0.22%
0.3 +3.21% +2.84%
0.4 +4.05% +1.80%
0.5 +4.55% +4.03%
0.6 +6.95% +4.32%
0.7 +4.09% +2.31%
0.8 +5.98% +3.73%
0.9 +7.16% +6.15%
1.0 +3.62% +2.60%
mean 0.1-1.0 +4.28% +2.87%
mean 0.5-1.0 +5.39% +3.86%

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: AI was used to partially assist while making the code edits and to understand the contexts of the code base.

@praneshgo

Copy link
Copy Markdown
Contributor Author

@gaugarg-nv
@ORippler
Can you please review this PR? Thanks.

@ruixiang63

ruixiang63 commented Sep 1, 2026 •

Copy link
Copy Markdown
Member

Thanks for the PR. However, this doesn’t appear to implement rejection sampling in the GPU backend, so I wouldn’t expect the implementation to yield a decent performance improvement. Could we instead follow vLLM’s rejection-sampling approach? e.g. https://github.com/vllm-project/vllm/blob/1f1f628859dd0dc2a3b7f43810254884799619e6/vllm/v1/sample/rejection_sampler.py#L715 and https://github.com/vllm-project/vllm/blob/1f1f628859dd0dc2a3b7f43810254884799619e6/vllm/v1/sample/rejection_sampler.py#L774

@praneshgo

praneshgo commented Sep 2, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for the PR. However, this doesn’t appear to implement rejection sampling in the GPU backend, so I wouldn’t expect the implementation to yield a decent performance improvement. Could we instead follow vLLM’s rejection-sampling approach? e.g. https://github.com/vllm-project/vllm/blob/1f1f628859dd0dc2a3b7f43810254884799619e6/vllm/v1/sample/rejection_sampler.py#L715 and https://github.com/vllm-project/vllm/blob/1f1f628859dd0dc2a3b7f43810254884799619e6/vllm/v1/sample/rejection_sampler.py#L774

Hi @ruixiang63

The rejection sampling (accept/reject step) runs on the host and not in the GPU backend with this PR.

The gain here does not come from sampling being faster though. Argmax-and-match accepts a draft token only when the target's top pick happens to equal the draft's, while min(1, p/q) accepts it with probability p/q, so more tokens survive per verification step and the target runs fewer forward passes. This leads to increased acceptance length: it improves by +2.7% to +13.4% depending on the model, drafter and temperature.

I have run sweeps with -bs enabled and disabled along with the changes in this PR. Based on the logs obtained, this PR does not affect the functionality of backend sampling, which ensures that the target sampling chain already happens on the GPU. With -bs on, I see the same accepted length and 1-2% change in throughput, which is consistent with what is observed on the master branch with -bs enabled.

On the GPU kernels, I agree it is worth doing. However, I would prefer to take that up as a separate PR so the two changes can be reviewed and measured separately. Note the residual is built over the target's post-chain candidates, which in llama.cpp default to top_k 40 trimmed by top_p, against the draft's top_k 10 — so only a few hundred comparisons per position. This is different from vLLM where vocab_size options are present per token at both draft and target, resulting in many more comparisons. Given the lower number of comparisons per token in llama.cpp I would need to experimentally verify if implementing the rejection sampling in the GPU sampling would result in a considerable throughput gain, which I prefer to do as a separate PR.

@praneshgo
praneshgo force-pushed the pgonegandla/spec-draft-rejection-sampling branch from 48c375c to 88e706e Compare September 3, 2026 11:02
@praneshgo
praneshgo marked this pull request as ready for review September 3, 2026 11:14
@praneshgo
praneshgo requested review from a team as code owners September 3, 2026 11:14
@praneshgo

praneshgo commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor Author

@ruixiang63 , @gaugarg-nv , @ORippler can you please review this PR? Thanks.

FYI, I have started the GPU kernel for rejection sampling-based experimentation locally and will file a new PR for any corresponding changes. The scope of this PR is limited to rejection sampling (accept/reject step) happening on the CPU. For flexibility, I have also added a flag (defaults to greedy - same as vLLM behavior), when set to probabilistic, enables rejection sampling.

@praneshgo
praneshgo force-pushed the pgonegandla/spec-draft-rejection-sampling branch from 88e706e to b11dae7 Compare September 7, 2026 12:04
@ruixiang63

Copy link
Copy Markdown
Member

Could you share some data comparing DFlash/MTP with and without rejection sampling? I’d like to see the differences in acceptance rate and speedup.

@praneshgo

Copy link
Copy Markdown
Contributor Author

Could you share some data comparing DFlash/MTP with and without rejection sampling? I’d like to see the differences in acceptance rate and speedup.

draft_simple and draft_mtp data over different models and HW with percentage increase in acceptance length and throughput is already present in PR description under the performance tab. Also pasting them below in this comment. The PR does not apply to and does not affect DFlash, so its perf numbers and unchanged and not mentioned.

Performance

Two machines, eight model configurations each, 11 temperatures from 0.0 to 1.0. Every run is 12 prompts of 256 tokens at --spec-draft-n-max 3, the shipped default.
Temperature 0.0 is the control. Both arms emit identical tokens there, so accepted length reads exactly +0.00% - the throughput figure on that row is the timing noise floor for that model.

RTX 5090

Llama-3.1-8B + 3B-Q8_0 - draft-simple

temp accepted length throughput
0.0 +0.00% +5.20%
0.1 +1.93% +4.21%
0.2 +0.15% +0.07%
0.3 +0.67% +1.29%
0.4 +3.19% +2.77%
0.5 +5.87% +6.12%
0.6 +11.52% +11.81%
0.7 +12.38% +12.57%
0.8 +14.35% +15.40%
0.9 +15.33% +16.61%
1.0 +15.34% +15.19%
mean 0.1-1.0 +8.07% +8.60%
mean 0.5-1.0 +12.47% +12.95%

Llama-3.1-8B + 3B-Q4_K_M - draft-simple

temp accepted length throughput
0.0 +0.00% +0.20%
0.1 +2.16% +1.23%
0.2 +0.70% +2.93%
0.3 +5.66% +3.97%
0.4 +4.26% +3.08%
0.5 +8.50% +8.54%
0.6 +9.46% +7.99%
0.7 +11.23% +11.54%
0.8 +8.20% +12.78%
0.9 +15.88% +16.07%
1.0 +12.67% +12.04%
mean 0.1-1.0 +7.87% +8.02%
mean 0.5-1.0 +10.99% +11.49%

Llama-3.1-8B + 3B-IQ2_XS - draft-simple

temp accepted length throughput
0.0 +0.00% -0.30%
0.1 -2.44% -2.99%
0.2 -0.61% -0.67%
0.3 +7.17% +7.46%
0.4 +7.39% +6.89%
0.5 +5.23% +4.99%
0.6 +4.09% +3.64%
0.7 +8.05% +8.21%
0.8 +7.89% +9.40%
0.9 +8.99% +10.28%
1.0 +8.05% +8.31%
mean 0.1-1.0 +5.38% +5.55%
mean 0.5-1.0 +7.05% +7.47%

Qwen3.5-4B - draft-mtp

temp accepted length throughput
0.0 +0.00% +0.00%
0.1 +0.25% +0.72%
0.2 +0.93% -0.06%
0.3 +0.36% +0.00%
0.4 +1.89% +2.02%
0.5 +4.74% +5.51%
0.6 +5.84% +5.12%
0.7 +5.93% +5.16%
0.8 +5.27% +4.62%
0.9 +4.57% +3.65%
1.0 +8.34% +8.95%
mean 0.1-1.0 +3.81% +3.57%
mean 0.5-1.0 +5.78% +5.50%

Qwen3.5-9B - draft-mtp

temp accepted length throughput
0.0 +0.00% +1.88%
0.1 +0.76% +0.12%
0.2 +3.28% +3.28%
0.3 +3.50% +1.79%
0.4 +4.60% +2.73%
0.5 +5.48% +4.28%
0.6 +4.91% +5.39%
0.7 +13.31% +12.46%
0.8 +7.90% +9.10%
0.9 +8.89% +5.47%
1.0 +11.98% +11.74%
mean 0.1-1.0 +6.46% +5.64%
mean 0.5-1.0 +8.75% +8.07%

Ornith-1.5-9B - draft-mtp

temp accepted length throughput
0.0 +0.00% -0.04%
0.1 -0.34% -0.38%
0.2 +0.63% +0.95%
0.3 +0.50% -3.90%
0.4 +2.47% +3.13%
0.5 +2.80% +2.35%
0.6 +1.20% +0.47%
0.7 +5.38% +4.95%
0.8 +8.18% +11.22%
0.9 +11.37% +9.90%
1.0 +7.91% +2.00%
mean 0.1-1.0 +4.01% +3.07%
mean 0.5-1.0 +6.14% +5.15%

Qwen3.6-27B - draft-mtp

temp accepted length throughput
0.0 +0.00% -3.22%
0.1 +2.21% -1.30%
0.2 -0.97% -3.49%
0.3 +2.40% +3.34%
0.4 +2.65% +4.73%
0.5 +3.09% +3.36%
0.6 +8.51% +8.18%
0.7 +7.63% +6.67%
0.8 +6.96% +7.13%
0.9 +8.03% +3.01%
1.0 +7.38% +9.49%
mean 0.1-1.0 +4.79% +4.11%
mean 0.5-1.0 +6.93% +6.31%

Qwen3.6-35B-A3B - draft-mtp

temp accepted length throughput
0.0 +0.00% -1.23%
0.1 +0.33% +1.63%
0.2 +0.87% +0.40%
0.3 +2.33% +2.39%
0.4 +5.11% +2.03%
0.5 +5.17% +4.52%
0.6 +5.93% +5.19%
0.7 +6.75% +4.84%
0.8 +4.23% +3.89%
0.9 +4.47% +8.15%
1.0 +8.54% +8.95%
mean 0.1-1.0 +4.37% +4.20%
mean 0.5-1.0 +5.85% +5.92%

DGX Spark

Llama-3.1-8B + 3B-Q8_0 - draft-simple

temp accepted length throughput
0.0 +0.00% -0.54%
0.1 +0.61% -0.27%
0.2 +2.43% +2.19%
0.3 +1.37% +1.09%
0.4 +3.41% +1.89%
0.5 +6.01% +5.70%
0.6 +6.24% +5.98%
0.7 +5.50% +4.69%
0.8 +10.90% +11.28%
0.9 +14.22% +15.36%
1.0 +15.68% +17.13%
mean 0.1-1.0 +6.64% +6.50%
mean 0.5-1.0 +9.76% +10.02%

Llama-3.1-8B + 3B-Q4_K_M - draft-simple

temp accepted length throughput
0.0 +0.00% -1.26%
0.1 +0.98% +1.06%
0.2 +1.96% +1.85%
0.3 +4.00% +3.38%
0.4 +0.81% +1.06%
0.5 +6.08% +6.18%
0.6 +7.21% +7.85%
0.7 +10.46% +10.18%
0.8 +16.97% +17.51%
0.9 +17.96% +18.31%
1.0 +17.38% +20.05%
mean 0.1-1.0 +8.38% +8.74%
mean 0.5-1.0 +12.68% +13.35%

Llama-3.1-8B + 3B-IQ2_XS - draft-simple

temp accepted length throughput
0.0 +0.00% +0.38%
0.1 -0.91% -1.30%
0.2 +0.25% +0.38%
0.3 +2.60% +3.24%
0.4 +2.79% +2.29%
0.5 +2.96% +2.49%
0.6 +5.18% +5.21%
0.7 +4.29% +4.10%
0.8 +13.87% +14.11%
0.9 +9.87% +10.32%
1.0 +12.63% +14.38%
mean 0.1-1.0 +5.35% +5.52%
mean 0.5-1.0 +8.13% +8.43%

Qwen3.5-4B - draft-mtp

temp accepted length throughput
0.0 +0.00% +0.00%
0.1 -1.86% -0.72%
0.2 +0.28% +0.31%
0.3 +7.29% +5.05%
0.4 +4.70% +4.64%
0.5 +6.48% +6.81%
0.6 +5.34% +5.30%
0.7 +4.64% +4.94%
0.8 +8.59% +12.05%
0.9 +9.22% +12.91%
1.0 +12.54% +11.18%
mean 0.1-1.0 +5.72% +6.25%
mean 0.5-1.0 +7.80% +8.86%

Qwen3.5-9B - draft-mtp

temp accepted length throughput
0.0 +0.00% +0.32%
0.1 +1.86% +3.78%
0.2 +3.33% +2.80%
0.3 +6.03% +5.42%
0.4 +5.97% +5.31%
0.5 +7.54% +7.19%
0.6 +8.56% +5.69%
0.7 +13.94% +11.51%
0.8 +8.86% +8.47%
0.9 +10.23% +9.45%
1.0 +11.43% +12.78%
mean 0.1-1.0 +7.78% +7.24%
mean 0.5-1.0 +10.09% +9.18%

Ornith-1.5-9B - draft-mtp

temp accepted length throughput
0.0 +0.00% -0.51%
0.1 +1.85% +5.03%
0.2 +2.19% +0.34%
0.3 +1.45% +1.20%
0.4 +2.80% +2.69%
0.5 +4.30% +1.35%
0.6 +4.81% +2.79%
0.7 +3.57% +3.70%
0.8 +7.19% +8.24%
0.9 +5.67% +5.35%
1.0 +8.54% +6.75%
mean 0.1-1.0 +4.24% +3.75%
mean 0.5-1.0 +5.68% +4.70%

Qwen3.6-27B - draft-mtp

temp accepted length throughput
0.0 +0.00% +0.36%
0.1 +0.66% +0.37%
0.2 +1.56% +1.50%
0.3 +1.05% +0.00%
0.4 +3.25% +3.75%
0.5 +3.51% +3.73%
0.6 +4.04% +5.22%
0.7 +2.61% +2.20%
0.8 +2.90% +2.61%
0.9 +4.06% +5.02%
1.0 +3.44% +3.89%
mean 0.1-1.0 +2.71% +2.83%
mean 0.5-1.0 +3.43% +3.78%

Qwen3.6-35B-A3B - draft-mtp

temp accepted length throughput
0.0 +0.00% +1.57%
0.1 +1.03% +0.67%
0.2 +2.15% +0.22%
0.3 +3.21% +2.84%
0.4 +4.05% +1.80%
0.5 +4.55% +4.03%
0.6 +6.95% +4.32%
0.7 +4.09% +2.31%
0.8 +5.98% +3.73%
0.9 +7.16% +6.15%
1.0 +3.62% +2.60%
mean 0.1-1.0 +4.28% +2.87%
mean 0.5-1.0 +5.39% +3.86%

@ruixiang63

ruixiang63 commented Sep 8, 2026 •

Copy link
Copy Markdown
Member

Despite the above, if the draft still gives its top candidate only, it evaluates to min(1, p(x)/1), same as p(x). Hence to make q(x) land, we make the draft probabilistic as well. This time the effective probability that a candidate is chosen from draft is sum_x(q(x)*min(1, p(x)/q(x))).

I didn't get this. What will happen if both draft and target use greedy sampling? and if only draft uses greedy sampling?

Comment thread common/sampling.cpp

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
dst->cur_p = src->cur_p;
dst->rng = src->rng;

Need to copy new field here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this field here in the latest commit.

Comment on lines +3002 to 3004
slot.spec_draft_q.clear();

if (!slot.spec_draft.empty()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spec_draft_q should not be cleared for replay case when spec_draft is non-empty.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in the latest commit.

Comment thread common/sampling.cpp
Comment on lines +749 to +766
if (masked) {
cand.assign(cur_p->data, cur_p->data + cur_p->size);
llama_token_data_array arr = { cand.data(), cand.size(), -1, false };
llama_sampler_apply(gsmpl->grmr, &arr);
}

// a candidate the grammar rejects carries no probability, whatever the target thinks
auto p_of = [&](size_t k) {
return masked && cand[k].logit == -INFINITY ? 0.0f : cur_p->data[k].p;
};

// q_x is never 0 for a token the draft produced, but guard the divide
const float q_x = prob_of(q.data(), q.size(), draft[i]);

float p_x = 0.0f;
for (size_t k = 0; k < cur_p->size; ++k) {
if (cur_p->data[k].id == draft[i]) {
p_x = p_of(k);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we not need to renormalize distribution after masking?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Fixed this in the latest commit.

Comment thread common/speculative.cpp Outdated
}

cur.temp = dp.sampling->temp;
cur.seed = dp.sampling->seed;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this same seed as target? Can you check if using the same seed as target expected in rejection sampling?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch @gaugarg-nv. It was using the same seed as the target, which it should not. This is fixed in the latest commit. The draft's seed and the verifier's rng are now derived from the request seed with distinct constants, so every stream starts differently.

@gaugarg-nv

Copy link
Copy Markdown
Contributor

@ggerganov @danbev Could you please review this PR when you get some cycles?

This implements probabilistic sampling for draft as proposed by SpecDec paper and also implemented by vLLM.

@ggerganov ggerganov self-assigned this Sep 8, 2026
Comment thread common/sampling.cpp Resolved
@praneshgo

Copy link
Copy Markdown
Contributor Author

Latest commit addresses @gaugarg-nv's seed question and @danbev's rng-state finding. The verifier's rng and the draft sampler were both seeded from the request seed, and both are now mixed - LLAMA_DEFAULT_SEED is passed through to reseed randomly. This is experimentally confirmed, across 8 seeds the verifier and the draft no longer share a first uniform in any of them, and over 20 requests the draft no longer matches the target chain's stream.

That also surfaced another case. common_sampler_reset ran every draft round, restarting the draft's rng from word zero so each slot drew the same random number on every round of a generation. The reset now happens once per request in begin() and the per-round one is skipped when drafting probabilistically, so the stream runs across rounds while --seed still reproduces. Greedy keeps the per-round reset and is unchanged.

The perf tables are re-measured after making these changes, hence some differences from the previous tables may be expected.

Updated Performance

Two machines, 11 temperatures from 0.0 to 1.0, 12 prompts, each generating output up to 256 tokens at --spec-draft-n-max 3.
The two arms are interleaved per cell rather than run as separate phases, so clock drift affects both equally; one warm-up request per cell is discarded.
Temperature 0.0 is the control: both arms emit identical tokens, so accepted length reads exactly +0.00% and the throughput figure on that row is the noise floor.

RTX 5090

Llama-3.1-8B + 3B-Q4_K_M - draft-simple

temp accepted length throughput
0.0 +0.00% +0.88%
0.1 +2.28% +3.54%
0.2 -1.03% -0.62%
0.3 +0.56% -0.55%
0.4 +0.44% -0.18%
0.5 +4.67% +3.62%
0.6 +2.84% +2.02%
0.7 +9.99% +7.22%
0.8 +11.67% +11.44%
0.9 +14.09% +10.91%
1.0 +15.22% +14.65%
mean 0.1-1.0 +6.07% +5.21%
mean 0.5-1.0 +9.75% +8.31%

Llama-3.1-8B + 3B-Q8_0 - draft-simple

temp accepted length throughput
0.0 +0.00% -0.14%
0.1 -0.61% -2.29%
0.2 +0.50% -0.99%
0.3 +0.52% -0.42%
0.4 +2.52% +2.09%
0.5 +3.39% +4.25%
0.6 +3.41% +4.33%
0.7 +6.40% +6.28%
0.8 +7.18% +5.95%
0.9 +13.12% +11.18%
1.0 +11.08% +9.27%
mean 0.1-1.0 +4.75% +3.97%
mean 0.5-1.0 +7.43% +6.88%

Llama-3.1-8B + 3B-IQ2_XS - draft-simple

temp accepted length throughput
0.0 +0.00% +0.62%
0.1 +1.85% +4.73%
0.2 -1.70% -2.66%
0.3 +0.00% +0.48%
0.4 -1.61% -3.29%
0.5 +2.77% +2.90%
0.6 +0.66% +2.21%
0.7 +9.37% +9.39%
0.8 +5.05% +3.55%
0.9 +8.93% +11.38%
1.0 +8.66% +9.75%
mean 0.1-1.0 +3.40% +3.84%
mean 0.5-1.0 +5.90% +6.53%

Qwen3.5-4B - draft-mtp

temp accepted length throughput
0.0 +0.00% +0.14%
0.1 -1.23% -1.48%
0.2 +0.16% +0.45%
0.3 +1.03% +0.24%
0.4 +1.02% -0.64%
0.5 +4.41% +4.85%
0.6 +2.28% +3.19%
0.7 +3.38% +6.25%
0.8 +7.55% +6.91%
0.9 +6.14% +5.63%
1.0 +8.74% +7.75%
mean 0.1-1.0 +3.35% +3.31%
mean 0.5-1.0 +5.42% +5.76%

Qwen3.5-9B - draft-mtp

temp accepted length throughput
0.0 +0.00% -0.55%
0.1 -1.07% +1.11%
0.2 -0.29% -1.52%
0.3 +1.57% +1.54%
0.4 +2.62% +2.64%
0.5 +3.58% +4.36%
0.6 +5.88% +5.92%
0.7 +10.16% +11.41%
0.8 +3.47% +2.90%
0.9 +9.46% +6.51%
1.0 +15.47% +15.50%
mean 0.1-1.0 +5.08% +5.04%
mean 0.5-1.0 +8.00% +7.77%

Ornith-1.5-9B - draft-mtp

temp accepted length throughput
0.0 +0.00% -0.09%
0.1 +2.76% +3.95%
0.2 +0.27% +1.22%
0.3 -1.71% -1.79%
0.4 +1.49% +1.70%
0.5 +4.11% +4.91%
0.6 +3.98% +3.71%
0.7 -0.95% -1.11%
0.8 +3.95% +3.46%
0.9 +0.71% -1.38%
1.0 +3.55% +2.15%
mean 0.1-1.0 +1.82% +1.68%
mean 0.5-1.0 +2.56% +1.96%

Qwen3.6-27B - draft-mtp

temp accepted length throughput
0.0 +0.00% -0.19%
0.1 +0.59% +0.62%
0.2 +2.89% +0.46%
0.3 +2.14% +0.58%
0.4 +5.40% +4.97%
0.5 +4.26% +1.13%
0.6 +1.08% +3.30%
0.7 +4.63% +3.94%
0.8 +2.00% +3.50%
0.9 +5.01% +4.40%
1.0 +3.42% +3.10%
mean 0.1-1.0 +3.14% +2.60%
mean 0.5-1.0 +3.40% +3.23%

Qwen3.6-35B-A3B - draft-mtp

temp accepted length throughput
0.0 +0.00% -2.89%
0.1 -1.79% +0.89%
0.2 +1.55% +1.87%
0.3 +0.76% +3.14%
0.4 +1.88% +2.76%
0.5 +4.82% +3.90%
0.6 +4.59% +2.29%
0.7 +4.20% +1.82%
0.8 +0.53% +0.33%
0.9 +4.85% +3.74%
1.0 +0.68% +1.33%
mean 0.1-1.0 +2.21% +2.21%
mean 0.5-1.0 +3.28% +2.24%

DGX Spark

Llama-3.1-8B + 3B-Q8_0 - draft-simple

temp accepted length throughput
0.0 +0.00% +0.58%
0.1 +0.09% +0.02%
0.2 -1.66% -1.77%
0.3 +1.24% +1.49%
0.4 +0.79% +0.57%
0.5 +6.20% +5.92%
0.6 +2.21% +1.64%
0.7 +7.61% +7.50%
0.8 +7.48% +7.79%
0.9 +10.01% +10.14%
1.0 +9.96% +10.32%
mean 0.1-1.0 +4.39% +4.36%
mean 0.5-1.0 +7.25% +7.22%

Llama-3.1-8B + 3B-Q4_K_M - draft-simple

temp accepted length throughput
0.0 +0.00% -0.20%
0.1 -0.25% -1.43%
0.2 -0.32% -0.38%
0.3 +2.00% +1.63%
0.4 -0.44% -0.27%
0.5 +4.82% +3.58%
0.6 +2.51% +2.47%
0.7 +7.31% +7.50%
0.8 +7.26% +6.90%
0.9 +10.51% +10.83%
1.0 +11.11% +11.24%
mean 0.1-1.0 +4.45% +4.21%
mean 0.5-1.0 +7.25% +7.09%

Llama-3.1-8B + 3B-IQ2_XS - draft-simple

temp accepted length throughput
0.0 +0.00% -0.48%
0.1 +3.02% +2.68%
0.2 -1.06% -0.68%
0.3 +3.29% +3.79%
0.4 -0.21% -2.05%
0.5 +1.88% +3.16%
0.6 -3.13% -3.99%
0.7 +1.65% +0.50%
0.8 +3.76% +2.42%
0.9 +3.27% +2.78%
1.0 +0.67% +0.85%
mean 0.1-1.0 +1.31% +0.95%
mean 0.5-1.0 +1.35% +0.95%

Qwen3.5-4B - draft-mtp

temp accepted length throughput
0.0 +0.00% -0.45%
0.1 -1.02% -2.44%
0.2 +0.34% -0.59%
0.3 -0.33% -1.79%
0.4 +1.01% +0.88%
0.5 +0.33% +1.62%
0.6 +2.83% +3.73%
0.7 +6.33% +5.90%
0.8 +7.65% +7.81%
0.9 +8.63% +7.66%
1.0 +9.69% +9.58%
mean 0.1-1.0 +3.55% +3.24%
mean 0.5-1.0 +5.91% +6.05%

Qwen3.5-9B - draft-mtp

temp accepted length throughput
0.0 +0.00% -1.04%
0.1 +0.89% +0.79%
0.2 +1.86% -0.61%
0.3 +3.39% +2.16%
0.4 +2.21% +0.27%
0.5 +3.85% +0.56%
0.6 +3.74% +3.33%
0.7 +4.39% +4.37%
0.8 +8.12% +6.89%
0.9 +6.94% +8.16%
1.0 +6.24% +6.19%
mean 0.1-1.0 +4.16% +3.21%
mean 0.5-1.0 +5.55% +4.92%

Ornith-1.5-9B - draft-mtp

temp accepted length throughput
0.0 +0.00% -0.16%
0.1 -0.42% -0.27%
0.2 -0.38% -1.29%
0.3 +1.91% +2.72%
0.4 +3.11% +1.12%
0.5 +1.88% +3.17%
0.6 +2.45% +1.69%
0.7 +0.69% +2.19%
0.8 +1.76% +0.38%
0.9 +4.87% +5.41%
1.0 +6.63% +5.30%
mean 0.1-1.0 +2.25% +2.04%
mean 0.5-1.0 +3.05% +3.03%

Qwen3.6-27B - draft-mtp

temp accepted length throughput
0.0 +0.00% -0.23%
0.1 +0.20% +0.39%
0.2 +0.36% +0.30%
0.3 -0.08% -0.69%
0.4 +1.37% +1.29%
0.5 +1.85% +1.91%
0.6 +0.53% -0.00%
0.7 +1.90% +2.34%
0.8 +0.98% +0.81%
0.9 +1.43% +1.09%
1.0 +1.94% +1.89%
mean 0.1-1.0 +1.05% +0.93%
mean 0.5-1.0 +1.44% +1.34%

@praneshgo

Copy link
Copy Markdown
Contributor Author

Ran AIME 2026 (math-ai/aime26, apache-2.0) on two machines, 30 problems x 8 runs = 240 cells per arm, temperature 1.0, -np 4, --spec-draft-n-max 3.

arm draft verification
prob samples from the draft distribution rejection sampling, residual norm(max(0, p-q))
greedy argmax (master default) accept if the target's own sample matches the draft token

The target samples stochastically in both arms; the flag selects the draft sampler and the matching verification rule only.

trunc counts cells whose reasoning trace ran past the context ceiling before reaching an answer. These cells are excluded from the arm's score. Trace length may vary run to run, so the two arms may truncate on different cells.

RTX 5090, Qwen3.6-27B-Q4_K_M

arm pass fail trunc todo run score mean tok
prob 224 3 13 0 227 98.68% 30079
greedy 223 7 10 0 230 96.96% 30607

DGX Spark GB10, Qwen3.6-35B-A3B-UD-Q4_K_M

arm pass fail trunc todo run score mean tok
prob 226 14 0 0 240 94.17% 36067
greedy 228 10 2 0 238 95.80% 33996

Comment thread tools/server/server-context.cpp Outdated
Comment on lines +3915 to +3919
// drafters that fill no distribution fall back here, as does a chained draft
const bool use_rejection = slot.use_spec_rejection() &&
!slot.spec_draft.empty() &&
(slot.spec_is_replay ||
slot.spec_draft_q.size() == slot.spec_draft.size());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be simplified because above we assert GGML_ASSERT(n_draft > 0);:

Suggested change
// drafters that fill no distribution fall back here, as does a chained draft
const bool use_rejection = slot.use_spec_rejection() &&
!slot.spec_draft.empty() &&
(slot.spec_is_replay ||
slot.spec_draft_q.size() == slot.spec_draft.size());
// drafters that fill no distribution fall back here, as does a chained draft
const bool use_rejection = slot.use_spec_rejection() &&
(slot.spec_is_replay ||
slot.spec_draft_q.size() == slot.spec_draft.size());

Regarding slot.spec_draft_q.size() == slot.spec_draft.size() in which cases this wouldn't be true? If I understand correctly the logic, we probably need the following assert:

GGML_ASSERT(slot.spec_draft_q.empty() || (slot.spec_draft_q.size() == slot.spec_draft.size()));

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Updated as suggested in the latest commit.

Comment thread common/speculative.h Outdated
Comment on lines +76 to +77
// the target's config; only temp and seed are read, to retune the draft sampler
const common_params_sampling * sampling = nullptr;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pass only the data that is used (temp + seed) instead of the full sampling object.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated as suggested in the latest commit.

Comment thread common/speculative.cpp Outdated
@@ -294,7 +339,12 @@ struct common_speculative_impl_draft_simple : public common_speculative_impl {

n_drafting++;
drafting[seq_id] = true;
common_sampler_reset(smpls[seq_id].get());
spec_retune(smpls, smpls_cfg, llama_get_model(ctx_dft), seq_id, dp, params.probabilistic);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we do this in begin() instead of draft()? We don't expect sampling parameters to change in the middle of a generation.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The draft sampler is rebuilt only on the first draft of a request. For later drafts, spec_retune returns early if temp and seed are unchanged, and on the greedy path it is not called at all, since result_q gates it.

Moving the call into begin() would need the temperature and seed to reach common/ at generation start, since the draft params are filled per round afterwards. This involves function signature rewrites across multiple places for a relatively small change. The gate would also have to change: result_q is what currently tells the drafter that the caller wants rejection, and it is not set yet when begin() runs, so the temp > 0 check would have to be repeated there.

If we prefer that it be done in begin() despite the above, please let me know and I will make the change.

Comment thread common/sampling.h Outdated
@@ -85,6 +85,9 @@ llama_token common_sampler_sample(struct common_sampler * gsmpl, struct llama_co
//
std::vector<llama_token> common_sampler_sample_and_accept_n(struct common_sampler * gsmpl, struct llama_context * ctx, const std::vector<int> & idxs, const llama_tokens & draft, bool grammar_first = false);

// as above, but verifies by rejection sampling; draft_q holds the draft's candidates per token
std::vector<llama_token> common_sampler_sample_and_accept_n_rejection(struct common_sampler * gsmpl, struct llama_context * ctx, const std::vector<int> & idxs, const llama_tokens & draft, const std::vector<std::vector<llama_token_data>> & draft_q, bool is_replay = false, bool grammar_first = false);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if the is_replay logic needs to be introduced in this API.

I think we should just have an is_replay == true branch in the server-context.cpp logic and handle it there.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Updated as suggested in the latest commit.

abraham-imre pushed a commit to abraham-imre/llama.cpp that referenced this pull request Sep 23, 2026
…aft-simple, MTP)

Cherry-pick of ggml-org/llama.cpp PR ggml-org#27694 (ggml-org#27694)
Author: Pranesh Gonegandla (@praneshgo)

Reason for our setup: our live server runs MTP speculative decoding; at temperature>0 exact-match draft verification loses acceptance. Adds --spec-draft-sampling (greedy|probabilistic, default greedy = current behavior). Flag-gated, no default change.
Conflicts resolved manually against PR ggml-org#27210 (adaptive MTP depth): kept both the adaptive controller reset and the sampler reset in begin(); kept result_q recording with the adaptive n_cap check instead of fixed params.n_max.
abraham-imre pushed a commit to abraham-imre/llama.cpp that referenced this pull request Sep 23, 2026
Cherry-pick of ggml-org/llama.cpp PR ggml-org#27692 (ggml-org#27692)
Author: Felix Kranich (@kilofox)

Reason for our setup: cuts long-prompt TTFT by having a small draft model estimate which prompt tokens matter before target prefill (ICML 2025 speculative prefill). Opt-in feature; no default behavior change.
Conflicts resolved manually against PRs ggml-org#27694 and ggml-org#27210: kept both use_spec_rejection() and prompt_src()/n_prompt_src() in server slot; kept the adaptive-draft arg-parser tests.
Treggat pushed a commit to Treggat/llama.cpp-b70-patches that referenced this pull request Sep 25, 2026
…#27694)

Cherry-pick of the 7 commits of the open upstream PR ggml-org#27694 onto b11095. Opt-in via --spec-draft-sampling probabilistic; default greedy keeps the old path.

Assisted-by: Claude Opus 5.5
@praneshgo

Copy link
Copy Markdown
Contributor Author

@ggerganov I have updated the PR and addressed your previous comments.
Can you please take a look and review? Thanks.

@praneshgo
praneshgo requested a review from ggerganov September 30, 2026 05:37

@ggerganov ggerganov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rebase one more time on latest master.

Wizard815 pushed a commit to Wizard815/mx-llama.cpp-Rocm10 that referenced this pull request Oct 6, 2026
…27694)

* Make the drafter probabilistic and the target verify by rejection sampling

* Drop stale spec_draft_q before drafting

* Fallback to argmax sampling for grammar-constrained requests and adding flag for enabling probabilistic draft sampling. Default flag value is greedy.

* Support grammar-constrained requests in rejection sampling

* Fix - renormalize distribution after masking

* copy rng on sampler copy and re-accept drafted tokens on replay

* Fix draft sampler sharing the target's rng stream

* Simplify the rejection sampler's inputs and move replay to the server

* Truncate the draft candidates along with the draft

---------

Co-authored-by: praneshgo <[email protected]>
Co-authored-by: Pranesh Gonegandla <[email protected]>
(cherry picked from commit 1fb7ef3)
kmbandy added a commit to kmbandy/llama.cpp that referenced this pull request Oct 6, 2026
…bed0a85)

Resolution choices:
- DSpark --spec-draft-sampling (ours) and upstream ggml-org#27694 probabilistic draft
  sampling shared one CLI flag/env (--spec-draft-sampling,
  LLAMA_ARG_SPEC_DRAFT_SAMPLING), draft_params.temp and slot.spec_draft_q.
  One flag now: greedy | probabilistic (both implementations) | stochastic
  (DSpark only, back-compat). Ours renamed to dspark_temp/dspark_q and
  slot.spec_dspark_q; upstream keeps result_q/temp/seed and spec_draft_q.
- Verification order: synth > DSpark stochastic > upstream replay (only if the
  replayed round used rejection) > upstream rejection > sample-and-match, so
  default behaviour (no flags) is identical to master.
- sampling.cpp: keep <functional>. server.cpp: keep router-node early return,
  keep the "initializing ..." log before llama_backend_init.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
alainnothere added a commit to alainnothere/llama.cpp that referenced this pull request Oct 9, 2026
Two tailors in Lyon had been making the same coat for a year without meeting. One of them had sewn a hidden pocket for dice, because the customer gambled and liked to have them on him, and had sized it, lined it and tested it on his own hip. The other, who sold more coats, mailed over a pattern that spring with the same pocket drawn in, in the same place, in the same cloth, with a note that said the house of Vernet now considered the dice pocket standard. The first tailor laid the two patterns on the table and found that the seams crossed at fifteen points and that there was no honest way to have both pockets in one coat. He cut his out. He kept the stitch he'd invented for the lining, because the other pattern's lining tore on the Rhone wind and his didn't, and nobody from Vernet had ever stood on that quay.

The customer asked which pocket he'd got. The tailor said: theirs, with my thread, and the dice fit the same, and you will not find the seam because I moved it under the arm.

---

607 upstream commits (6d9c82e..06cad0b, b11480).

Dropped the fork's --spec-accept stochastic (f8a6903 / e9c788d /
60bc680) for upstream ggml-org#27694 --spec-draft-sampling probabilistic: same
rejection sampling, same three hook points, plus spec_retune() so the draft
samples at the target temperature. Running both side by side meant fifteen
conflicting hunks in common/speculative.cpp for one switch. test-spec-accept,
the stochastic/exact/fallback counters in server-common.h and
llama_sampler_grammar_is_active go with it. Server verify order is now synth,
replay, rejection, exact; the draft params block carries result_q/temp/seed
next to the tuner's n_draft_cur.

Kept: the keep vector on common_speculative_process, --spec-draft-auto,
--spec-draft-ctx-step, -lcd write-back, disk cache v4/v5 and checkpoint
spill, perf instrumentation, idempotent synchronize. server-http registers
upstream's callback lambda under path_prefix + path.

Upstream ggml-org#28498 writes n_rot_k/n_rot_v into the KV state blob, so disk-cache
files from the old binary fail with "incompatible key rotation" once, get
reprocessed and rewritten. Not a bug.

test-recurrent-state-rollback: upstream's dummy models (ggml-org#29133) are noisier
and Vulkan drifts ~1e-8 nmse between two contexts even without a restore, so
the composed/partial rollback tests bound nmse at 1e-4 and test_rollback at
1e-7 instead of asking for bit-exact logits. CPU stays bit-exact. 130/130
models pass on Vulkan.

Server pytests: 24 pass with the uncached upstream presets (tinylaya,
tinyopenjev) skipped; they cannot be fetched behind the proxy.
edwardyoon pushed a commit to edwardyoon/focus-llama that referenced this pull request Oct 10, 2026
…27694)

* Make the drafter probabilistic and the target verify by rejection sampling

* Drop stale spec_draft_q before drafting

* Fallback to argmax sampling for grammar-constrained requests and adding flag for enabling probabilistic draft sampling. Default flag value is greedy.

* Support grammar-constrained requests in rejection sampling

* Fix - renormalize distribution after masking

* copy rng on sampler copy and re-accept drafted tokens on replay

* Fix draft sampler sharing the target's rng stream

* Simplify the rejection sampler's inputs and move replay to the server

* Truncate the draft candidates along with the draft

---------

Co-authored-by: praneshgo <[email protected]>
Co-authored-by: Pranesh Gonegandla <[email protected]>
(cherry picked from commit 1fb7ef3)
tmac1973 added a commit to tmac1973/llama-toolchest that referenced this pull request Oct 15, 2026
…micro-batch (--spec-draft-n-ubatch) (#281)

--spec-draft-sampling (b11368, ggml-org/llama.cpp#27694) samples the
draft and verifies it by rejection, for a draft model and MTP.
--spec-draft-n-ubatch (b11630, #30459) gives the drafter's context a
micro-batch of its own, shrinking its compute buffer. Both are model
settings written only for builds that have them, spec-value settings
for benchmark sweeps, and counted where they matter: the estimate uses
the draft micro-batch for the draft compute buffer, and Autotune's
draft-settings stage measures probabilistic drafting on each draft or
MTP finalist when the profile samples above temperature 0.
vahpetr pushed a commit to vahpetr/llama.cpp-s1-fork that referenced this pull request Oct 19, 2026
…7694) [TAG_UP27694]

Manual port of upstream 1fb7ef3 ("spec : add probabilistic sampling for simple draft
and MTP", ggml-org/llama.cpp#27694); cherry-pick conflicted in common/speculative.{h,cpp}
and tools/server/server-context.cpp. Release train 63.5 (U7, R127), v63 candidate.

What it does (flag on, request temp > 0): draft-simple / draft-mtp sample the draft
from a draft sampler retuned to the request's temp/seed (top_k 10 + temp) and keep the
candidate set per drafted token; the server verifies by rejection sampling
(common_sampler_sample_and_accept_n_rejection: accept with min(1, p/q), else draw from
norm(max(0, p - q)); grammar-aware; the uniform draws come from a separate per-sampler
mt19937 so the target's dist RNG stream is untouched). Distribution-exact for the target.

Default = greedy. With the flag off the behaviour is byte-identical to the base:
- the drafters drop result_q -> argmax draft + per-round reset exactly as before;
- upstream's new reset in begin() is gated on the flag (fork deviation);
- common_sampler gains an rng member seeded from llama_sampler_get_seed (read-only),
  used only by the rejection path;
- server: the rejection branch needs non-empty spec_draft_q of the draft's size, which
  only a probabilistic drafter fills.

Fork adaptations:
- replay: upstream's server_accept_replay is NOT ported; the fork's
  server_sample_and_accept_replay already re-emits committed tokens without re-sampling,
  which is what the rejection round needs too (its committed tokens include the residual
  draw). The trace-only re-sample probe is off for rejection rounds (a kept token is not
  the target's own sample, it would count as a flip).
- the upstream GGML_ASSERT on spec_draft_q/spec_draft sizes became a fall-back to
  sample-and-match; the n_min rule clears result_q together with result in both drafters.
- [TAG_UP29638] EOG rule mirrored in the rejection verifier (no draft token after an
  accepted EOG).
- spec_untune(): a T = 0 request after a probabilistic one gets the default draft sampler
  back (top_k 10 {TOP_K}, MTP fast path), so p_min reads the same draft probabilities.
- draft params result_q/temp/seed are set after the fork's aggregate init (the fork
  struct has n_attempted/n_kept/keep_short in between).

Disk-cache key: unchanged. server_cache_param_canon lists draft fields explicitly and
does not include `probabilistic`; cache_compat_measure only runs process() on a
temporary speculative object (no draft() call), so the knob enters neither the
parameter digest nor the measured payload.

Tests (balkon CPU, stories15M MoE + stories15M-q4_0 draft-simple): new pytest
test_draft_sampling_probabilistic (T = 0 flag on == greedy, T 0.7 accepts drafts and a
fixed seed reproduces, T = 0 after T > 0 greedy again); unit/test_speculative.py 11/11;
flag off vs the base build: identical ids, draft counts and [spec-ids] rounds on 3 prompts
at T 0 and T 0.7 (seed 4242). The draft-mtp path has no CPU-sized test model: it is
exercised by the s1 pair gate.

EXACT with the flag off (default); behavioural (distribution-exact) with it on.

Review fixes (review-u7: M1, m1, m3; m2/m4 left as is):
- M1: flag off was not byte-identical for drafters that fill no candidates (ngram-*,
  eagle3, dflash) at T > 0 when the draft was truncated to n_max: the truncation grew the
  empty q to n_max empty sets, the size check passed and the rejection verifier ran with
  q = 0. Now (a) the server asks for candidates and picks the rejection verifier only when
  --spec-draft-sampling probabilistic is set, and (b) common_speculative_draft cuts q only
  when it exists and is longer than n_max (so flag on + ngram stays on sample-and-match).
  Upstream has the same latent bug. Prod (draft-mtp, capped at n_max) never truncated.
- m1: no rejection verifier with stateful samplers (mirostat v1/v2, adaptive-p): their
  state would be updated with the chain's own sample instead of the emitted token.
- m3: the trace-only replay re-sample probe is off only for replays of rounds that were
  actually verified by rejection (per-round slot.spec_round_rejection), not for every
  T > 0 replay under the flag.
- m2 (documented, not changed): with a grammar and grammar_first = false the flag-on
  verifier preserves norm(mask * chain(p)), while flag off resamples from
  chain(mask(logits)) on a grammar miss - upstream semantics; the pair gate G4 requires
  valid JSON, not the same distribution.
Re-tested on balkon CPU: test-arg-parser, test-sampling, unit/test_speculative.py 11/11;
ids A/B vs the base build: draft-simple flag off == pkg1 (6/6, [spec-ids] identical),
ngram-simple (size_m 16, truncations at T 0 and T 0.7) flag off == flag on == pkg1
(6/6, [spec-ids] identical), flag on + mirostat 1/2 == pkg1.

(manual port of upstream commit 1fb7ef3)
vahpetr pushed a commit to vahpetr/llama.cpp-s1-fork that referenced this pull request Oct 19, 2026
…7694) [TAG_UP27694]

Manual port of upstream 1fb7ef3 ("spec : add probabilistic sampling for simple draft
and MTP", ggml-org/llama.cpp#27694); cherry-pick conflicted in common/speculative.{h,cpp}
and tools/server/server-context.cpp. Release train 63.5 (U7, R127), v63 candidate.

What it does (flag on, request temp > 0): draft-simple / draft-mtp sample the draft
from a draft sampler retuned to the request's temp/seed (top_k 10 + temp) and keep the
candidate set per drafted token; the server verifies by rejection sampling
(common_sampler_sample_and_accept_n_rejection: accept with min(1, p/q), else draw from
norm(max(0, p - q)); grammar-aware; the uniform draws come from a separate per-sampler
mt19937 so the target's dist RNG stream is untouched). Distribution-exact for the target.

Default = greedy. With the flag off the behaviour is byte-identical to the base:
- the drafters drop result_q -> argmax draft + per-round reset exactly as before;
- upstream's new reset in begin() is gated on the flag (fork deviation);
- common_sampler gains an rng member seeded from llama_sampler_get_seed (read-only),
  used only by the rejection path;
- server: the rejection branch needs non-empty spec_draft_q of the draft's size, which
  only a probabilistic drafter fills.

Fork adaptations:
- replay: upstream's server_accept_replay is NOT ported; the fork's
  server_sample_and_accept_replay already re-emits committed tokens without re-sampling,
  which is what the rejection round needs too (its committed tokens include the residual
  draw). The trace-only re-sample probe is off for rejection rounds (a kept token is not
  the target's own sample, it would count as a flip).
- the upstream GGML_ASSERT on spec_draft_q/spec_draft sizes became a fall-back to
  sample-and-match; the n_min rule clears result_q together with result in both drafters.
- [TAG_UP29638] EOG rule mirrored in the rejection verifier (no draft token after an
  accepted EOG).
- spec_untune(): a T = 0 request after a probabilistic one gets the default draft sampler
  back (top_k 10 {TOP_K}, MTP fast path), so p_min reads the same draft probabilities.
- draft params result_q/temp/seed are set after the fork's aggregate init (the fork
  struct has n_attempted/n_kept/keep_short in between).

Disk-cache key: unchanged. server_cache_param_canon lists draft fields explicitly and
does not include `probabilistic`; cache_compat_measure only runs process() on a
temporary speculative object (no draft() call), so the knob enters neither the
parameter digest nor the measured payload.

Tests (balkon CPU, stories15M MoE + stories15M-q4_0 draft-simple): new pytest
test_draft_sampling_probabilistic (T = 0 flag on == greedy, T 0.7 accepts drafts and a
fixed seed reproduces, T = 0 after T > 0 greedy again); unit/test_speculative.py 11/11;
flag off vs the base build: identical ids, draft counts and [spec-ids] rounds on 3 prompts
at T 0 and T 0.7 (seed 4242). The draft-mtp path has no CPU-sized test model: it is
exercised by the s1 pair gate.

EXACT with the flag off (default); behavioural (distribution-exact) with it on.

Review fixes (review-u7: M1, m1, m3; m2/m4 left as is):
- M1: flag off was not byte-identical for drafters that fill no candidates (ngram-*,
  eagle3, dflash) at T > 0 when the draft was truncated to n_max: the truncation grew the
  empty q to n_max empty sets, the size check passed and the rejection verifier ran with
  q = 0. Now (a) the server asks for candidates and picks the rejection verifier only when
  --spec-draft-sampling probabilistic is set, and (b) common_speculative_draft cuts q only
  when it exists and is longer than n_max (so flag on + ngram stays on sample-and-match).
  Upstream has the same latent bug. Prod (draft-mtp, capped at n_max) never truncated.
- m1: no rejection verifier with stateful samplers (mirostat v1/v2, adaptive-p): their
  state would be updated with the chain's own sample instead of the emitted token.
- m3: the trace-only replay re-sample probe is off only for replays of rounds that were
  actually verified by rejection (per-round slot.spec_round_rejection), not for every
  T > 0 replay under the flag.
- m2 (documented, not changed): with a grammar and grammar_first = false the flag-on
  verifier preserves norm(mask * chain(p)), while flag off resamples from
  chain(mask(logits)) on a grammar miss - upstream semantics; the pair gate G4 requires
  valid JSON, not the same distribution.
Re-tested on balkon CPU: test-arg-parser, test-sampling, unit/test_speculative.py 11/11;
ids A/B vs the base build: draft-simple flag off == pkg1 (6/6, [spec-ids] identical),
ngram-simple (size_m 16, truncations at T 0 and T 0.7) flag off == flag on == pkg1
(6/6, [spec-ids] identical), flag on + mirostat 1/2 == pkg1.

(manual port of upstream commit 1fb7ef3)

(cherry picked from commit 116d2a2)
vahpetr pushed a commit to vahpetr/llama.cpp-s1-fork that referenced this pull request Oct 19, 2026
…7694) [TAG_UP27694]

Manual port of upstream 1fb7ef3 ("spec : add probabilistic sampling for simple draft
and MTP", ggml-org/llama.cpp#27694); cherry-pick conflicted in common/speculative.{h,cpp}
and tools/server/server-context.cpp. Release train 63.5 (U7, R127), v63 candidate.

What it does (flag on, request temp > 0): draft-simple / draft-mtp sample the draft
from a draft sampler retuned to the request's temp/seed (top_k 10 + temp) and keep the
candidate set per drafted token; the server verifies by rejection sampling
(common_sampler_sample_and_accept_n_rejection: accept with min(1, p/q), else draw from
norm(max(0, p - q)); grammar-aware; the uniform draws come from a separate per-sampler
mt19937 so the target's dist RNG stream is untouched). Distribution-exact for the target.

Default = greedy. With the flag off the behaviour is byte-identical to the base:
- the drafters drop result_q -> argmax draft + per-round reset exactly as before;
- upstream's new reset in begin() is gated on the flag (fork deviation);
- common_sampler gains an rng member seeded from llama_sampler_get_seed (read-only),
  used only by the rejection path;
- server: the rejection branch needs non-empty spec_draft_q of the draft's size, which
  only a probabilistic drafter fills.

Fork adaptations:
- replay: upstream's server_accept_replay is NOT ported; the fork's
  server_sample_and_accept_replay already re-emits committed tokens without re-sampling,
  which is what the rejection round needs too (its committed tokens include the residual
  draw). The trace-only re-sample probe is off for rejection rounds (a kept token is not
  the target's own sample, it would count as a flip).
- the upstream GGML_ASSERT on spec_draft_q/spec_draft sizes became a fall-back to
  sample-and-match; the n_min rule clears result_q together with result in both drafters.
- [TAG_UP29638] EOG rule mirrored in the rejection verifier (no draft token after an
  accepted EOG).
- spec_untune(): a T = 0 request after a probabilistic one gets the default draft sampler
  back (top_k 10 {TOP_K}, MTP fast path), so p_min reads the same draft probabilities.
- draft params result_q/temp/seed are set after the fork's aggregate init (the fork
  struct has n_attempted/n_kept/keep_short in between).

Disk-cache key: unchanged. server_cache_param_canon lists draft fields explicitly and
does not include `probabilistic`; cache_compat_measure only runs process() on a
temporary speculative object (no draft() call), so the knob enters neither the
parameter digest nor the measured payload.

Tests (balkon CPU, stories15M MoE + stories15M-q4_0 draft-simple): new pytest
test_draft_sampling_probabilistic (T = 0 flag on == greedy, T 0.7 accepts drafts and a
fixed seed reproduces, T = 0 after T > 0 greedy again); unit/test_speculative.py 11/11;
flag off vs the base build: identical ids, draft counts and [spec-ids] rounds on 3 prompts
at T 0 and T 0.7 (seed 4242). The draft-mtp path has no CPU-sized test model: it is
exercised by the s1 pair gate.

EXACT with the flag off (default); behavioural (distribution-exact) with it on.

Review fixes (review-u7: M1, m1, m3; m2/m4 left as is):
- M1: flag off was not byte-identical for drafters that fill no candidates (ngram-*,
  eagle3, dflash) at T > 0 when the draft was truncated to n_max: the truncation grew the
  empty q to n_max empty sets, the size check passed and the rejection verifier ran with
  q = 0. Now (a) the server asks for candidates and picks the rejection verifier only when
  --spec-draft-sampling probabilistic is set, and (b) common_speculative_draft cuts q only
  when it exists and is longer than n_max (so flag on + ngram stays on sample-and-match).
  Upstream has the same latent bug. Prod (draft-mtp, capped at n_max) never truncated.
- m1: no rejection verifier with stateful samplers (mirostat v1/v2, adaptive-p): their
  state would be updated with the chain's own sample instead of the emitted token.
- m3: the trace-only replay re-sample probe is off only for replays of rounds that were
  actually verified by rejection (per-round slot.spec_round_rejection), not for every
  T > 0 replay under the flag.
- m2 (documented, not changed): with a grammar and grammar_first = false the flag-on
  verifier preserves norm(mask * chain(p)), while flag off resamples from
  chain(mask(logits)) on a grammar miss - upstream semantics; the pair gate G4 requires
  valid JSON, not the same distribution.
Re-tested on balkon CPU: test-arg-parser, test-sampling, unit/test_speculative.py 11/11;
ids A/B vs the base build: draft-simple flag off == pkg1 (6/6, [spec-ids] identical),
ngram-simple (size_m 16, truncations at T 0 and T 0.7) flag off == flag on == pkg1
(6/6, [spec-ids] identical), flag on + mirostat 1/2 == pkg1.

(manual port of upstream commit 1fb7ef3)

(cherry picked from commit 116d2a2)
edwardyoon pushed a commit to edwardyoon/focus-llama that referenced this pull request Nov 19, 2026
…27694)

* Make the drafter probabilistic and the target verify by rejection sampling

* Drop stale spec_draft_q before drafting

* Fallback to argmax sampling for grammar-constrained requests and adding flag for enabling probabilistic draft sampling. Default flag value is greedy.

* Support grammar-constrained requests in rejection sampling

* Fix - renormalize distribution after masking

* copy rng on sampler copy and re-accept drafted tokens on replay

* Fix draft sampler sharing the target's rng stream

* Simplify the rejection sampler's inputs and move replay to the server

* Truncate the draft candidates along with the draft

---------

Co-authored-by: praneshgo <[email protected]>
Co-authored-by: Pranesh Gonegandla <[email protected]>
(cherry picked from commit 1fb7ef3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. server

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants