Add W-TinyLFU eviction policy with cost-aware admission#680
Conversation
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: a1amit The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
|
Welcome @a1amit! It looks like this is your first PR to zilliztech/GPTCache 🎉 |
Implement a W-TinyLFU eviction policy that combines frequency-based admission filtering with cost-weighted eviction decisions, targeting LLM caching workloads where response regeneration costs vary widely. Architecture (following Caffeine's design): - Window LRU (1%): absorbs burst traffic - TinyLFU admission gate: Count-Min Sketch + Bloom doorkeeper - Segmented main LRU (99%): probation (20%) + protected (80%) Cost-aware extension: when enabled, admission multiplies frequency by response token count, preferring to retain expensive entries. Components: - count_min_sketch.py: 4-bit packed counters with periodic aging - doorkeeper.py: Bloom filter to reject one-hit-wonders - segmented_lru.py: two-tier LRU with promotion/demotion - wtinylfu_eviction.py: orchestrator implementing EvictionBase Registered as name="wtinylfu" in the eviction factory. Tunable via window_pct, probation_pct, cost_aware, and CMS parameters. No new external dependencies (uses numpy + stdlib only). 32 unit tests covering all components and algorithm properties. Usage examples added to examples/eviction/. Signed-off-by: Amit Abramovich <amitnoa.av@gmail.com>
7d2adb8 to
c2b3768
Compare
|
I'd appreciate it if one of you could take a look at this when you have a moment. thanks and have a great weekend! |
☑️ Command disallowed due to command restrictions in the Mergify configuration.Details
|
|
/assign @cxie |
|
@a1amit: GitHub didn't allow me to assign the following users: SimFG. Note that only zilliztech members, repo collaborators and people who have commented on this issue/PR can be assigned. Additionally, issues/PRs can only have 10 assignees at the same time. DetailsIn response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
|
Cost-aware was something that I was always interested in but lacked enough workload traces to verify a design. I wrote up my ideas which extends yours to possibly handle some edge cases better. A user tried it and it worked well in their workload, but did not share any data sets. @NadavKeren has been working on the problem with his own design which could be of interest: |
|
Hey @ben-manes , just got off work and saw your comment — thank you so much for taking the time to share your thoughts. Coming from the creator of Caffeine, that means a lot. I'm plannig to go through everything you wrote carefully when I find the time and incorporate what I can. If you're interested I can tag you again when I'm done. |
|
Hey @ben-manes , following up on your suggestions. I implemented the EWMA + z-score cost normalization you proposed in discussion #1744, along with Caffeine's hill climber for adaptive window sizing. What I built:
Key finding — multiplicative vs lexicographic scoring: I initially implemented your multiplicative proposal ( Results (cost-aware / no-cost ratio):
On synthetic (where cost correlates with popularity): improved +3-5%, and fixed a bug at cs=200 where nocost was beating cost-aware. On LMSYS (uncorrelated): cost-aware is neutral — doesn't help but doesn't hurt, which I think is the correct behavior for the lexicographic approach. Full evaluation report, raw data, and implementation on the branch: I also looked at @NadavKeren's pipeline cache work — the delayed hits concept is very relevant for LLM caches but would be an architectural change beyond the eviction policy. @Yiling-J's suggestion about a cost-aware window is another promising direction I haven't explored yet. If you have any further pointers or suggestions, I'd be happy to hear them. Thanks again for the pointers! |
|
Thanks, very insightful! Nadav’s simulator might be easy to try if you rewrite the trace to his format. |
- Add "Community Reception" section to the report documenting Ben Manes' engagement on PR zilliztech#680 (comment screenshots kept locally, not tracked) - Remove duplicate .pdf figures (PNG versions kept and used by report.tex) from figures_synthetic, figures_lmsys_080, figures_lmsys_085 - Regenerate report.pdf
|
@a1amit @ben-manes Sorry about being a little bit late for the party, but I do have some insights regarding this. Secondly, I am actively looking into creating a more sophisticated caching algorithm for this specific domain (based on a very similar architecture of GPTCache, that I find a bit limiting in this case), and I can tell that the adaptation of W-TinyLFU directly into this domain isn't trivial and especially doesn't work as one-to-one code translation on the policy side; however, I do have solutions for these problems that are still work in progress. If you want, we may continue this conversation directly; I would be happy to cooperate on this project. |
Summary
Adds a W-TinyLFU eviction policy (
name="wtinylfu") that combines frequency-based admission filtering with optional cost-weighted eviction, targeting LLM caching workloads where response regeneration costs vary by orders of magnitude.This addresses the roadmap item: "Support more complicated eviction policies".
Architecture (following Caffeine's design)
Cost-aware extension
When
cost_aware=True(default), the admission decision multiplies each candidate's frequency estimate by its response token count (set_cost()API), biasing eviction toward retaining expensive entries. This is an additive extension — the existingput/get/policyinterface is unchanged.Files changed
gptcache/manager/eviction/wtinylfu_eviction.pyEvictionBase)gptcache/manager/eviction/count_min_sketch.pygptcache/manager/eviction/doorkeeper.pygptcache/manager/eviction/segmented_lru.pygptcache/manager/eviction/manager.pytests/unit_tests/eviction/test_*.pyexamples/eviction/wtinylfu_eviction.pyexamples/README.mdREADME.mdUsage
Design decisions
numpy(already in requirements) + stdlibBenchmark results
Full benchmarks (synthetic Zipfian + LMSYS-Chat-1M real data, 3 trials each) are available in the deliverables folder(not part of the commit to reduce noise) (figures, raw JSON results). Key results vs. LRU baseline: +74% token savings on synthetic Zipfian workload (cs=50), +94% on real LMSYS-Chat-1M conversations (cs=50, threshold=0.80).
References
Test plan
pytest tests/unit_tests/eviction/ -v)examples/eviction/examples/README.mdupdatedREADME.mdroadmap updated