Skip to main content

hippocache 🦛

Smart, plug-and-play KV cache compression for Transformer inference, written in Rust.

hippocache provides drop-in KV cache implementations with multiple retention and compression strategies, designed to fit into LLM generation loops to reduce memory footprints without sacrificing model perplexity.


🚀 Features

  • 🦀 Rust Engine: High-performance token merging and eviction operations implemented using ndarray and PyO3.
  • 🔌 Drop-in Integration: Fits seamlessly into standard LLM autoregressive generation loops.
  • 📊 Smart Compression: Includes advanced strategies like cosine-similarity-based sequence merging (merge and hybrid).
  • 📈 Stats & Metrics: Built-in tracking for compression ratios, merges, and evictions.

📦 Installation

Install the pre-compiled package directly via pip:

pip install hippocache

⚡ Quickstart

Using hippocache is simple. Below is a realistic example demonstrating how to integrate KVCache into a token generation loop:

import random
from hippocache import KVCache

# 1. Initialize the KV Cache
# d_k and d_v represent the key and value head dimensions
cache = KVCache(
    d_k=64,
    d_v=64,
    strategy="hybrid",
    static_k=3,         # Retain the first 3 tokens (e.g., prompt context)
    threshold=0.90,     # Cosine similarity merge threshold
    merge_every=4       # Check and merge adjacent tokens every 4 steps
)

# 2. Simulate a generation loop
for step in range(20):
    # Mock key & value vectors for the new token
    new_k = [random.uniform(-1.0, 1.0) for _ in range(64)]
    new_v = [random.uniform(-1.0, 1.0) for _ in range(64)]
    
    # Append to cache (merges/evictions happen automatically in Rust)
    cache.append(new_k, new_v)
    
    # Query attention over the compressed cache
    q = [random.uniform(-1.0, 1.0) for _ in range(64)]
    attention_output = cache.attend(q)
    
    print(f"Step {step+1:02d} | Cache Size: {cache.current_len()} | Stats: {cache.stats()}")

# 3. Retrieve final stats
final_stats = cache.stats()
print("\nFinal Generation Stats:")
print(f"  Strategy:          {final_stats['strategy']}")
print(f"  Compression Ratio: {final_stats['compression_ratio']:.2f} (lower is better)")
print(f"  Total Appends:     {final_stats['total_appends']}")
print(f"  Total Merges:      {final_stats['total_merges']}")

🛠 Caching Strategies

Strategy Config Parameters Description
full None Keeps all key-value states in memory (default).
sliding window_size Retains only the last $N$ tokens, evicting older states.
static static_k, recent_k Keeps the first $K$ prefix tokens forever + the last $R$ recent tokens.
merge threshold, merge_every Compares adjacent key tensors using cosine similarity and merges them if similarity exceeds the threshold.
hybrid static_k, threshold, merge_every Keeps the first $K$ prefix tokens forever and performs similarity merges on the rest of the sequence.

📊 Benchmark Results

Evaluated on TinyLlama-1.1B (Layer 0, Head 0) across various compression strategies and parameters.

HippoCache Compression Analysis

1. Strategy Comparison

Evaluating the tradeoff between cache size and quality loss (measured by KL Divergence / proxy perplexity and L2 attention divergence).

Strategy Comparison - Cache Size vs Quality

Strategy Config Cache Size (Tokens) Size Reduction (%) L2 Attention Divergence KL Divergence (Quality Loss)
full (Default) 16 0% 0.0000 0.0000
merge threshold=0.90 12 25% 0.0031 0.67 × 10⁻⁵
hybrid static_k=3, threshold=0.90 12 25% 0.0032 0.74 × 10⁻⁵
static static_k=3, recent_k=6 9 43.8% 0.0051 1.95 × 10⁻⁵
sliding window_size=8 8 50% 0.0076 3.75 × 10⁻⁵

2. Compression vs. Quality (Merge Threshold Sweep)

Adjusting the cosine similarity threshold allows fine-grained control over the compression vs. attention quality tradeoff.

Threshold Sweep - Variance Across Heads

Merge Threshold Cache Size (Tokens) Cache Compression L2 Attention Divergence Recommendation
0.99 16 / 16 0% 0.0000 Lossless baseline
0.97 15 / 16 6.2% 0.0018 Ultra-conservative
0.95 15 / 16 6.2% 0.0018 Ultra-conservative
0.92 14 / 16 12.5% 0.0032 Conservative
0.90 12 / 16 25.0% 0.0030 Sweet spot (recommended)
0.85 8 / 16 50.0% 0.0051 Moderate compression
0.80 4 / 16 75.0% 0.0140 High compression, quality degradation

📜 License

MIT License. See LICENSE for details.

Metadata

Release files for hippocache 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for hippocache 0.1.2
File Interpreter ABI Platform Actions
hippocache-0.1.2-cp312-cp312-manylinux_2_34_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.34+ x86-64 Details for hippocache-0.1.2-cp312-cp312-manylinux_2_34_x86_64.whl

Release files / hippocache-0.1.2-cp312-cp312-manylinux_2_34_x86_64.whl

Download URL hippocache-0.1.2-cp312-cp312-manylinux_2_34_x86_64.whl
Size 279.1 kB
Tags CPython 3.12 Linux glibc 2.34+ x86-64
SHA-256 checksum
How to use checksums
6c99b337821cc635a3c0d7160d7c1215ec17a28c5e49d207f343ed70ae3db69d
BLAKE2b-256 checksum
How to use checksums
40db4f69693492edf6481625ddebb0bf64131e068103bc0682ea2871f57ffa09
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Security for hippocache 0.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.2 This release

1 release file No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page