Faster prompt lookup drafting in llama.cpp
TL;DR Highlight
Removing map copies and swapping data structures made llama.cpp prompt lookup drafting up to 42x faster.
Who Should Read
A backend developer running local LLM inference with llama.cpp who wants to know the drafting overhead of n-gram speculation.
Core Mechanics
- Prompt lookup is a very simple draft model
- Prompt lookup decoding (n-gram speculation) is a special case of speculative decoding that uses an n-gram model as the draft model.
- llama.cpp, vllm, and Hugging Face transformers support it.
- Three caches, tried in order
- llama.cpp keeps a context cache, a dynamic cache, and a static cache, and drafts the first token that passes thresholds for n=4,3,2,1.
- Dynamic is used only if context fails, and static alone if both fail.
- Removing inner map copies gave 4.5x-25.6x
- Inner maps were being copied unnecessarily on every drafting step, so they are now read by reference.
- Drafting got 4.5x to 25.6x faster depending on corpus size.
- The author says this is closer to a bug fix than an optimization.
- Outer map became a flat hash map
- Replaced std::unordered_map with ankerl::unordered_dense segmented_map, versus the previous step (copy removal).
- Static cache loading got 1.41-1.65x faster, drafting 1.02-1.13x faster, and memory use dropped 1.07-1.11x.
Community Reactions
- A question about real tokens-per-second gains
- A commenter asked how much this speeds up inference for end users in tk/s.
- The article does not answer this.
- Author follow-up on Lemire's extra optimization
- The author said Lemire added another optimization via PR.
- They said they would benchmark it and add it to the article, and later reported that Lemire absolutely crushed it.
- Criticism over the author's dispute with the llama.cpp community
- The author asked for advice on an interpersonal issue with the llama.cpp open source community.
- One commenter said that airing it here instead of resolving it privately was the wrong approach.
How to Apply
- Measure drafting time in your setup with llama-lookup-stats
- Build a static cache with llama.cpp's llama-lookup-create and replay a file with llama-lookup-stats.
- Check latency per drafted token, load time, and memory to find the bottleneck.
- Audit your own cache code for needless map copies
- Find places that return nested maps by value or copy them in loops, and switch to const references.
- This single change gave 4.5-25.6x in the article.
- Move read-only data to immutable structures
- Put lookup tables that never change after loading into a constmap-like immutable map with contiguous arrays.
- Measure load time and memory together to compare.
- Move early-exit checks ahead of scoring
- If your code scores all candidates before checking thresholds, check total count and the top candidate first.
- Skip the remaining computation when the top candidate fails.
Try It
# Run in a terminal after building llama.cpp (tools from the article; check options with --help)
# 1) Build a static n-gram cache
./llama-lookup-create --help
# 2) Replay a corpus and measure drafting stats
./llama-lookup-stats --help
# Next: run 3 times per corpus size (25/50/100/200MB) and compare the median draft latency, load time, and memory.Terminology
Related Papers
Mercury 3: Diffusion at scale
Scaling diffusion LLM 40x gives Mercury 3: 4–8x faster than speed-tier models, similar quality
Timestral: Mistral's time-series foundation models
A 209M time-series model ranks first on three benchmarks and runs on CPU.
DeepSeek V4.1 Flash 1017 (Max)
DeepSeek V4.1 Flash 1017 Max scores 45 on the Intelligence Index at 211 tok/s and $0.30 input.
If you don't run your own LLM proxy, how do you know whats going on?
Running your own LLM proxy shows which keys, models, and cache misses drive your usage.
DeepSeek: DeepSeek v4.1 Flash 1017
DeepSeek V4.1 Flash 1017: $0.05/M input, 1M context, built for agents
DeepSeek-v4.1-Flash-1017
DeepSeek V4.1 Flash minor update lifts coding and agent benchmarks while cutting run time and output length
Show HN: Llmspeed – feel how fast a local LLM runs before you buy the hardware