Draftless Speculative Decoding: N-Gram Strategies in vLLM, SGLang, and llama.cpp

·5 min readlocal-aiai

TL;DR: Traditional speculative decoding requires a secondary draft model that consumes valuable VRAM. Engines like vLLM, SGLang, and llama.cpp bypass this requirement using draftless N-Gram architectures, utilizing context history to double throughput at virtually zero memory cost.

Speculative decoding has become a cornerstone of low-latency Large Language Model (LLM) serving. By predicting future tokens using a lightweight mechanism and verifying them in parallel on the target model, engines can dramatically increase tokens per second.

Historically, this required a small “draft model” (e.g., using an 8B model to speculatively decode a 70B model). However, managing two models introduces VRAM overhead and configuration friction. Enter draftless N-Gram speculative decoding: a class of algorithms that looks backward at the text already present in the prompt or generation history to guess what comes next.

While the concept is similar across frameworks, the algorithmic implementations in llama.cpp, vLLM, and SGLang differ profoundly in efficiency, capability, and multi-user performance.


1. llama.cpp — Global Hash Pools with ngram-mod

In llama.cpp, the classic ngram-simple approach scanned local text slots sequentially. The modern standard, ngram-mod (triggered via --spec-default), shifts this paradigm by building a global hash pool in system RAM.

Instead of parsing full text arrays on the fly, ngram-mod maps a rolling hash of a sequence of length NN to the single most probable next token.

Dynamic Iterative Hashing

Rather than proposing a static chunk of MM tokens, ngram-mod operates dynamically:

  1. It hashes the last NN tokens generated.
  2. It queries the global hash table for the next predicted token.
  3. If a match is found, it appends that token to the draft context, re-hashes the new sequence window, and fetches the next one.
  4. This loop repeats until the token budget is exhausted or a hash miss occurs.

Cross-User Benefits

Because this hash pool is shared across all execution slots in llama-server, it enables dynamic collaborative learning. If User A generates a structured piece of text, the global table learns those structural n-grams in real-time. When User B makes a completely separate request that relies on identical structural syntax, they immediately benefit from User A’s learned patterns, speeding up multi-user deployments.


2. vLLM — Prompt Lookup / Sequence Scan

vLLM implements N-Gram speculation natively using a Prompt Lookup mechanism (configured via --speculative-model=ngram).

Unlike llama.cpp, vLLM avoids a global hash table to prevent synchronization bottlenecks between continuous batches. Instead, it relies on a local window search directly within the request’s active sequence context.

vllm_ngram.py
from vllm import LLM
llm = LLM(
model="meta-llama/Llama-3.1-8B-Instruct",
speculative_config={
"method": "ngram",
"num_speculative_tokens": 5, # The exact number of tokens to speculate ahead
"prompt_lookup_max": 4, # The 'N' lookback window size
}
)
)

Execution Mechanics

When a request is processed, vLLM takes the last prompt_lookup_max tokens generated, scans backward through the existing KV Cache (both the prompt tokens and previously generated text), and locates matching strings.

Once a match is found, it copies a linear sequence of tokens directly following that match up to the length defined by num_speculative_tokens. This adds virtually 0 bytes of extra memory overhead, as it operates entirely within the pre-allocated KV cache.


3. SGLang — Dynamic Tries and Tree Verification

SGLang elevates draftless speculation by trading flat, linear sequences for complex data structures. Configured via --speculative-algorithm NGRAM, SGLang constructs a Dynamic Trie Structure (Prefix Tree) out of the runtime token streams.

Instead of predicting a single linear chain of tokens, SGLang uses a Breadth-First Search (BFS) to branch out across the Trie based on token frequency and historical recency. This generates a Draft Token Tree containing multiple plausible execution paths.

flowchart TD A[Current Suffix] --> B{Trie Search} B -->|Path A| C[token: fox] B -->|Path B| D[token: dog] C --> E[token: jumps] D --> F[token: runs] style B fill:#f9f,stroke:#333,stroke-width:2px

Parallel Tree Verification

The target LLM evaluates the entire token tree simultaneously in a single parallel GPU forward pass. If the model rejects one branch, it can still validate a parallel branch. This structural agility yields significantly higher token acceptance rates compared to the strict, linear sequences used by vLLM and llama.cpp.

Furthermore, SGLang supports compiling an External Corpus into a Suffix Automaton (SAM). This allows the speculator to draw draft predictions from massive reference textbooks or manuals entirely outside of the prompt context window.


Architectural Deep Dive

Featurellama.cpp (ngram-mod)vLLM (ngram)SGLang (NGRAM)
Data StructureGlobal Shared Hash TableLocal Context String SearchDynamic Trie Tree & Suffix Automaton
Draft ShapeLinear SequenceLinear SequenceToken Tree (Multi-branch)
Memory Overhead~16MB Static RAM0 bytes (Reads KV Cache)Dynamic Trie allocation
Best Used ForMulti-user chat serversRAG & Long-Context SummarizationComplex structured JSON & Code generation

When Do N-Gram Speculators Shine?

Draftless speculation is highly sensitive to text structure. Because it relies purely on historical repetition, performance gains scale alongside text predictability:

  • High Performance (Up to 2x Speedup): Code generation (highly repetitive syntax, keywords, and brackets), strict JSON schema adherence, and long-context RAG documentation where the model copies verbatim strings from the prompt.
  • Low Performance (Near 0% Speedup): Creative writing, conversational prose, and open-ended reasoning steps where token patterns rarely repeat identically.

References

  1. vLLM Speculative Decoding Documentation — vLLM Team — https://docs.vllm.ai/
  2. SGLang Speculative Execution Engine Design — SGLang Contributors — https://sgl-project.github.io/
  3. llama.cpp Server and Speculative Arguments — ggerganov, GitHub Repository — https://github.com/ggerganov/llama.cpp

This article was written by Gemini (Gemini 1.5 Pro | Google).