common : store the ngram caches in an unordered_dense map - #5
Open
jadidbourbaki wants to merge 3 commits into
Open
jadidbourbaki wants to merge 3 commits into
jadidbourbaki wants to merge 3 commits into
Conversation
This was referenced Sep 26, 2026
jadidbourbaki
added this pull request to stack #8
September 26, 2026 16:43
jadidbourbaki
removed this pull request from stack #8
September 26, 2026 16:47
jadidbourbaki
added this pull request to stack #9
September 26, 2026 16:47
jadidbourbaki
removed this pull request from stack #9
September 26, 2026 17:07
jadidbourbaki
added this pull request to stack #11
September 26, 2026 17:07
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.
Stacked on #2.
I replaced the outer
std::unordered_mapof the n-gram caches with anankerl::unordered_dense::segmented_map. The inner maps are unchanged.A segmented map grows in blocks of 4096 bytes. A plain
ankerl::unordered_dense::mapkeeps its entries in one vector that doubles as it fills. The 541 MB static cache has 8.9 million 2-grams, so the last doubling held both vectors at once and raised the peak memory by about 0.65 GB.I vendored unordered_dense v5.0.1 by
@martinusundervendor/ankerland pinned it inscripts/sync_vendor.py.Results
I borrowed the benchmark setup from ggml-org/llama.cpp#5479, which builds the static cache from WikiText-103 and runs with a context of 4096 tokens. Since this PR makes no algorithmic changes to lookup decoding, the dataset mainly matters for the acceptance rate, which stays identical. The metrics that change are the latency per drafted token, the load time of the static cache, and the memory used by the static cache.
I ran
llama-lookup-statson WikiText-103 test with static caches built from prefixes of WikiText-103 train. Each value is the median of 3 runs on the CPU of an Apple M4 Pro with 14 cores and 48 GB of memory, running macOS 26.5.1. A corpus size of 0 means no static cache. #2 is the baseline.The benchmark code and full tables are in ngram-cache-bench.