Repository navigation
Conversation
Use a bounded sorted-key index while preserving LRU tie selection and public mapping mutations. Related to abetlen#280; long shared/nested prompt workloads benefit, while disjoint-key workloads can regress. Co-authored-by: GeneralTony007 <144252170+GeneralTony007@users.noreply.github.com>
Check ties, overwrites, eviction, mapping reorder/replacement, randomized scan equivalence, and NumPy integer boundaries. Co-authored-by: GeneralTony007 <144252170+GeneralTony007@users.noreply.github.com>
Add the user-visible cache change under Unreleased. PR reference follows once assigned. Co-authored-by: GeneralTony007 <144252170+GeneralTony007@users.noreply.github.com>
Complete the contribution guide's changelog format with abetlen#2410. Co-authored-by: GeneralTony007 <144252170+GeneralTony007@users.noreply.github.com>
This branch has not been deployed
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.
Related to #280. This is a dependency-free sorted-key alternative for multi-state RAM caches with long shared or nested prompt prefixes, not a pygtrie implementation.
Workload limits and tradeoffs
This is not a general faster-cache claim. In the synthetic 64-state, 2048-token benchmark below, disjoint-key
getregresses from 339 to 570 microseconds (about 68% slower), and alternating overwrite/get regresses from 826 to 1063 microseconds (about 29% slower). Shared/nested prefixes benefit, but this tradeoff may not suit every workload. No model-level inference speedup was measured.Every lookup takes an O(N) snapshot of public mapping order. Tie selection can scan O(N) ranks. Key/order changes trigger a lazy O(N log N) comparison sort, with token-length-dependent comparisons. Auxiliary O(N) key references/ranks are outside
capacity_bytes, which still measures serialized state bytes. Removed states are released normally; indexed key tuples may remain referenced until a subsequent lookup.Changes
cache_statekey/order changes and mapping replacement, including clear/pop/move_to_end. Keep a simple path for zero/one state.Validation
Python 3.11.17, Linux x86_64 CPU, GCC 11.4, CMake 3.22.1; exact pinned llama.cpp
7d7f0287530bc8981690f361d8c1627dbae1c99a, built withGGML_NATIVE=OFF,GGML_OPENMP=OFF,GGML_BLAS=OFF.python -m pytest tests -q: 50 passed, no skips, including the existing native/model-backed tests. Unchanged original suite: 26 passed.ruff check llama_cpp testsandruff format --check llama_cpp tests: passed.git diff --check: passed.Synthetic cache microbenchmark
Microseconds per operation, median of 7 batches, CPU0 affinity. Real
LlamaRAMCacheandLlamaState; 1-byte serialized states, 2048-token keys, 64 entries. This deliberately isolates Python cache overhead, not inference or large-state copying. The alternating case rotates overwrites through the stored keys before each get, so it includes lazy rebuild cost.Index-only nested-key allocation tracing at 64/128/256/512/1024 keys was 1576/3112/6376/19624/47336 bytes. These are Python traced allocations after forcing lazy index creation, excluding the preallocated token/state payloads, not process RSS.
Reproduce the same-workload timing comparison
Run from this branch with its native library built and test dependencies installed. The baseline module is extracted from the parent before this PR; only this script's baseline import is redirected. CPU0 was used for the reported run; choose an available CPU on another machine.
git show 05c124e33f7f33b7e66ef887274ad9ee6775099f:llama_cpp/llama_cache.py > /tmp/llama_cache_baseline.py OPENBLAS_NUM_THREADS=1 taskset -c 0 python /tmp/cache_benchmark.pySave this as
/tmp/cache_benchmark.py: