Repository navigation
common : read ngram cache parts by reference instead of copying - #30061
Merged
ggerganov merged 1 commit intoOct 16, 2026
Merged
Conversation
Contributor
|
Nice! Did you measure e2e speed-up using something like speed-bench (available in tools/ in this repo) |
ggerganov
approved these changes
Oct 12, 2026
Contributor
Author
|
Great question @am17an! I ran it yesterday based on your advice. I started the server with and then I used speed-bench like you suggested in the following way: With my setup i.e qwen2.5 0.5B with Q4K_M on my Macbook M4 Pro, the decode for the baseline was 247 tokens per sec. The decode with this change was 250.9 tokens per second i.e about a 1.6%-ish increase end to end? Also saw an analogous decrease in latency. I am assuming this is because drafting isn't the bottleneck for most workloads. Thank you! Also thank you @ggerganov for approving! |
am17an
approved these changes
Oct 15, 2026
edwardyoon
pushed a commit
to edwardyoon/focus-llama
that referenced
this pull request
Oct 24, 2026
auRose94
pushed a commit
to auRose94/llama.cpp
that referenced
this pull request
Oct 28, 2026
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.
Overview
The n-gram caches in llama.cpp are currently implemented as nested std::unordered_maps. An outer map sends each n-gram to an inner map of the tokens that follow it and their counts. This is a small performance fix. I found that the inner maps were being copied unnecessarily in multiple places on every drafting step. I created this simple PR to read them by reference instead. This immediately made drafting 4.5x to 25.6x faster depending on the size of the corpus (see figure below). The latency is the average time spent drafting per drafted token.
Additional information
llama-lookup-stats -m qwen2.5-0.5b-instruct-q4_k_m.gguf -f wiki.test.raw -lcs <static cache> -c 4096 --spec-draft-n-max 8 -ngl 0llama-lookup-stats, 4096-token context)llama-lookup-stats, not end-to-end generationBenchmark code and full results: https://github.com/jadidbourbaki/ngram-cache-bench
Requirements