feat: Expose Multi-Sequence Speculative Decoding & MTP to Public C API - #27788
Open
zsogitbe wants to merge 1 commit into
Open
feat: Expose Multi-Sequence Speculative Decoding & MTP to Public C API#27788zsogitbe wants to merge 1 commit into
zsogitbe wants to merge 1 commit into
Conversation
|
Hi @zsogitbe, thanks for your contribution! Per our contribution guidelines, the automated PR checker found the following issue(s) that need your attention:
Please note that maintainers reserve the right to make final decisions on PRs. If you believe there is a mistake, please comment below. |
zsogitbe
marked this pull request as ready for review
August 27, 2026 09:50
Author
I have updated the main PR description to fully comply with the required template. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context: I am submitting this on behalf of the LLamaSharp maintainer team (cc @martindevans). We needed a way to cleanly expose speculative decoding and MTP to our C# users without bloating the upstream core. This standalone C API has been heavily battle-tested across both C++ and C# environments.
Overview
This PR introduces a standalone, dependency-free C API for multi-sequence speculative decoding and Multi-Token Prediction (MTP). It extracts the core speculative execution loop from the internal
common/speculative.cpputilities into a public-facing implementation (src/llama-speculative.cpp). This enables downstream bindings (such asllama-cpp-python,LLamaSharp, etc.) to easily integrate draft-simple and Draft-MTP capabilities via native pointers, without needing to recreate complex draft/verify loops or context routing mechanics.Additional information
Key Architectural Changes
include/llama.h): Introducedllama_speculative_init,llama_speculative_decode,llama_speculative_free, and thellama_speculative_resultstruct.llama_set_embeddings_nextnandllama_get_embeddings_nextn_ithto the public header. These are strictly required to route hidden states across MTP projection heads for architectures like DeepSeek-R1 and Qwen.src/llama-speculative.cpp): Implemented a core multi-sequence engine relying solely onllama.hand the C++ standard library, keeping the footprint minimal and avoidingcommondependencies.batch->n_seq_idto draft and verify multiple independent sequences concurrently, mapping accepted tokens back to their origin sequence ID.llama_state_seqbackup mechanism prior to verification. If a sequence is rejected, the engine performs a hard memory wipe and transparent re-decodes accepted tokens. This specifically prevents fatalX < YM-RoPE position collision crashes on hybrid recurrent architectures.Validation
tests/test-speculative.cppand integrated it intoCMakeLists.txtusing thetest-download-modelfixture.LLamaSharpfeat: Native Speculative Decoding & MTP (Multi-Token Prediction) - #1431. The API and underlying engine have been exhaustively tested across both C++ and C# environments. This includes the native CI test provided here, alongside extensive managed C# unit tests validating multi-sequence multiplexing, KV cache deduplication, and real-world interactive Tokens-Per-Second (TPS) benchmarks.)Requirements
ANNEX: Model Selection & Evaluation
1. Model Compatibility & Selection Rules
Dual-Model Speculation: The target and draft models must share the exact same tokenizer architecture and vocabulary size to prevent immediate cache desynchronization crashes. Crucially, the larger the target model and the smaller/faster the draft model, the higher the resulting speedup.
Multi-Token Prediction (MTP): This requires a single model with pre-trained speculative projection layers (
nextn_predict_layers >= 1). The more draft heads the model has, the higher the potential speedup, as it can verify more tokens per native API call without context-switching overhead. The draft budget must match the available heads.2. Real-World Benchmark Examples
3. Hardware & Workload Dynamics
100% VRAM Offloading: Both models (or the full MTP model) must fit entirely within GPU VRAM. If layers spill over to the CPU, parallel batch verification turns into serialized matrix multiplication, causing a severe net slowdown.
The Size Threshold: Speculative decoding is an optimization for memory-bandwidth-bound workloads. Speedups reliably appear on mid-to-large models (8B, 14B, 32B, 70B) where reading massive weight matrices per token is the actual bottleneck.
Task Predictability: Deterministic tasks (code completion, JSON extraction) achieve high draft acceptance rates (>70%), maximizing throughput. Creative writing and high-temperature sampling trigger frequent draft rejections, wasting compute cycles.
4. Quick Selection & Viability Matrix
nextn_predict_layers >= 1)