llama.cpp icon indicating copy to clipboard operation
llama.cpp copied to clipboard

Feature Request: Early-exit speculative drafting via Shannon entropy (comparison with p_min)

Open wizardwithcodehazard opened this issue 2 months ago • 4 comments

Prerequisites

  • [x] I am running the latest code. Mention the version if possible as well.
  • [x] I carefully followed the README.md.
  • [x] I searched using keywords relevant to my issue to make sure that I am creating a new issue that is not already open (or closed).
  • [x] I reviewed the Discussions, and have a new and useful enhancement to share.

Feature Description

Add an uncertainty-based early-exit threshold for speculative decoding (--spec-draft-entropy-max) using the Shannon entropy of draft candidates:

H = -sum(p * log2(p))

When the entropy of the draft model's output distribution exceeds this threshold, speculative drafting halts early for that sequence. This prevents generating low-confidence draft tokens that are likely to be rejected by the target model, saving forward passes and increasing throughput.

Motivation

Speculative decoding often loses performance on reasoning or divergent tasks when the draft model generates speculative tokens with low confidence.

Currently, llama.cpp provides --spec-draft-p-min to prune drafting based on top-1 probability (cur_p->data[0].p). However, top-1 probability only checks the argmax and ignores whether probability mass is tightly focused or dispersed across many tokens.

I benchmarked Shannon entropy gating head-to-head against --spec-draft-p-min 0.6 on an AMD Ryzen 7 7735HS (8 CPU threads) using Qwen2.5-3B (target) and Qwen2.5-0.5B (draft) with K=8 draft budget over 128+ tokens:

1. Code Generation (longest common subsequence, 134 tokens)

Strategy Speed Drafted Accepted Acceptance Rate
Static Speculation (no gate) 11.71 t/s 192 110 57.29%
--spec-draft-p-min 0.6 14.83 t/s 138 109 78.99%
Entropy Gate (entropy 1.5) 16.50 t/s 130 115 88.46%
  • +1.67 t/s (+11.3% speedup) over p_min 0.6 and +40.9% over static speculation.
  • Draft acceptance reached 88.46% (only 15 rejected drafts out of 130).

2. Divergent Reasoning / Riddle (130 tokens)

Strategy Speed Drafted Accepted Acceptance Rate
Static Speculation (no gate) 6.70 t/s 324 90 27.78%
--spec-draft-p-min 0.6 12.91 t/s 102 73 71.57%
Entropy Gate (entropy 1.5) 13.19 t/s 78 66 84.62%
  • 1.97x wall-clock speedup over static baseline.
  • Gating was more selective: drafted 78 tokens instead of 102, achieving 84.62% acceptance vs 71.57% for p_min 0.6.

Possible Implementation

I have a working, tested branch ready: https://github.com/wizardwithcodehazard/llama.cpp/tree/feat/entropy-gated-speculation

  1. Add float entropy_max = 0.0f to common_params_speculative_draft in common/common.h.
  2. Add --spec-draft-entropy-max / --draft-entropy-max in common/arg.cpp, clamping negative values with std::max(0.0f, ...).
  3. In common/speculative.cpp, compute Shannon entropy over cur_p->data[k].p (with p > 1e-12f) after sampling and exit drafting early if entropy > params.entropy_max.
  4. Applied across all candidate-sampling implementations: draft_simple, eagle3, dflash, and mtp.
  5. Passes 44/44 unit tests in ctest.

Would the maintainers be open to reviewing a PR for this?

(Implementation and local benchmarking assisted by AI, reviewed and verified manually).

wizardwithcodehazard avatar Oct 02 '26 18:10 wizardwithcodehazard

Some constructive feedback: It sounds better than p-min, but is a sample size of 1 for target-drafter pair and two use cases enough to justify a new early-exit option? I suppose the maintainers would like to see more evidence to consider this.

Maybe also if the interface was more extensible/generic it would be easier to accept (e.g. --spec-draft-halt-metric [entropy,other] --spec-draft-halt-metric-max ) would allow other methods to be added without introducing new arguments.

Also is the entropy computed across the whole vocabulary? maybe the logarithm is quite expensive as an operation and an alternative like Gini impurity/diversity index (1 - sum(p^2)) could be less expensive but with similar properties? This is used in Decision trees exactly because entropy calculation is heavier.

Finally, llama-server and llama-cli --help currently list an argument: --spec-draft-p-split, --draft-p-split P with description "speculative decoding split probability (default: 0.10)", which from what I can tell looking at the code is not wired anywhere (so currently misleading). I imagine that the intent of this flag was to stop when the difference of the top-1 from the next token is lower than that. Maybe as a proxy it could have a similar effect to min-entropy (not global uncertainty across all possible tokens but uncertainty that the top-1 is better than the next best). I'm mentioning that because a PR may be accepted more easily if it adds an implementation for an already documented argument (which is currently misleading as it does nothing), than introducing a new one.

Just my thoughts, but I liked the idea in general and I may experiment with your patch locally. Thanks.

henfiber avatar Oct 04 '26 02:10 henfiber

Thanks for your feedback!

I did get into things u said but it wouldn't be that heavy. In llama.cpp, the candidates list is already filtered down by the sampler to just the top candidates (usually only 5 to 20 tokens, not the whole 150k vocab). Doing log2 on like 10 floats takes a few nanoseconds, while a single draft model forward pass takes around 15 milliseconds, so the log overhead is basically unnoticeable. Gini impurity (1 - sum(p^2)) is a cool idea though and behaves pretty similarly while staying between 0 and 1, so we could easily support that too if people want it.

Also checked the code for --spec-draft-p-split and it turns out it actually isn't dead code. It is used in examples/speculative/speculative.cpp for tree speculation to decide when to split branches in the KV cache tree, so hijacking it would break tree decoding.

Good point on the interface though, grouping it under something like --spec-draft-halt-metric [none | p-min | entropy] to avoid adding too many flags makes sense if maintainers prefer that. And yeah, I will run benchmarks on another pair like LLaMA 3.2 3B + 1B as well to test across more architectures.

Also do let me know what results you get :)

wizardwithcodehazard avatar Oct 04 '26 07:10 wizardwithcodehazard

You're right about --spec-draft-p-split, it is used by that experiment, although it is unused in llama-server and llama-cli, so it should not be listed in --help. But since it has a different meaning it wouldn't make sense to hijack it.

By the way, a few days ago a probabilistic sampling mode was introduced (see #27694 ) as an alternative to the previous greedy-only mode. You may test it with --spec-draft-sampling probabilistic. It may make sense to test how the entropy-based halt logic would play with this mode.

henfiber avatar Oct 04 '26 17:10 henfiber

This issue was closed because it has been inactive for 14 days since being marked as stale.

github-actions[bot] avatar Nov 18 '26 00:11 github-actions[bot]