Skip to main content

Salience-Aware Tiered KV Cache Compression for Long-Context Inference

Project description

TTKV - Three Tiered Key Value Cache Compression

PyPI version Python 3.9+ License: MIT

Three-tier progressive compression (full → 4:1 → 16:1) with structural survival floor.

TTKV is a PyTorch library that enables long-context inference on consumer GPUs by compressing the KV cache rather than evicting tokens. It solves the "slow-burn problem" where critical tokens mentioned early get evicted before the model attends to them.

Key Features

  • 🎯 Three-Tier Compression: Uncompressed (Tier 0) → 4:1 (Tier 1) → 16:1 (Tier 2)
  • 🛡️ Structural Survival Floor: Rare-but-critical tokens survive until attended
  • 🧠 Attention-Guided Scoring: Uses model's own attention patterns
  • Consumer GPU Ready: Enables 16K context on RTX 3060 (12GB)
  • 🔌 Transformers Compatible: Works with HuggingFace models

Installation

pip install ttkv

Optional Dependencies

# For 4-bit quantization support
pip install ttkv[quantization]

# For visualization
pip install ttkv[viz]

# Development dependencies
pip install ttkv[dev]

Quick Start

from ttkv import TieredKVCache, CacheConfig
import torch

# Configure the cache
config = CacheConfig(
    hidden_dim=768,
    num_heads=12,
    tier0_size=256,      # Recent tokens (uncompressed)
    tier1_size=2048,     # Middle tier
    tier1_compression=4,  # 4:1 compression
    tier2_compression=16, # 16:1 compression
    tau_threshold=0.9
)

# Create cache
cache = TieredKVCache(config)

# Use with your model
k = torch.randn(1, 12, 8192, 64)  # [batch, heads, seq, head_dim]
v = torch.randn(1, 12, 8192, 64)
retention = torch.rand(1, 8192)    # Salience scores
positions = torch.arange(8192).unsqueeze(0)

cache.add(k, v, retention, positions)

# Get compressed cache
k_comp, v_comp, pos_comp = cache.get_compressed_cache()
stats = cache.get_stats()
print(f"Compression: {stats['compression_ratio']:.2f}x")

Performance

Context GPU Baseline TTKV Compression
16K tokens RTX 3060 (12GB) OOM ✅ Works 7.36x

Quality: <0.2% perplexity increase at 7.36x compression on GPT-2.

Documentation

API Reference

Core Components

CacheConfig

Configuration for tiered KV cache.

config = CacheConfig(
    hidden_dim=768,
    num_heads=12,
    head_dim=64,
    tier0_size=256,
    tier1_size=2048,
    tier1_compression=4,
    tier2_compression=16,
    tau_threshold=0.8
)

TieredKVCache

Main cache implementation.

cache = TieredKVCache(config)
cache.add(k, v, retention, positions)
k_comp, v_comp, pos_comp = cache.get_compressed_cache()
stats = cache.get_stats()

Attention-Guided Components

AttentionGuidedScorer

Learns token importance from attention patterns.

from ttkv import AttentionGuidedScorer

scorer = AttentionGuidedScorer(ema_decay=0.95, structural_floor=0.1)
scorer.update_from_attention(attention_weights, query_position, token_id)
salience = scorer.get_salience_scores(seq_len)

AttentionGuidedWrapper

Full wrapper for models.

from ttkv import AttentionGuidedWrapper

wrapper = AttentionGuidedWrapper(model, tokenizer, cache_config)
result, stats = wrapper.generate_with_attention_guidance(input_ids)

Project Structure

ttkv/
├── src/ttkv/
│   ├── __init__.py          # Package exports
│   ├── core.py              # TieredKVCache, CacheConfig
│   ├── attention_scorer.py  # Attention-guided scoring
│   └── type_prior.py        # Structural priors
├── tests/
│   ├── core/               # Core functionality tests
│   └── experiments/        # Paper experiments
├── paper/
│   └── main.tex            # LaTeX source
└── README.md               # This file

Citation

If you use TTKV in your research:

@software{ttkv2024,
  title={TTKV: Salience-Aware Tiered KV Cache Compression},
  author={Pérez Muñiz, Zabdiel},
  year={2024},
  url={https://github.com/zabwie/ttkv}
}

License

MIT License - see LICENSE file.

Acknowledgments

Contact

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ttkv-0.1.0.tar.gz (15.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ttkv-0.1.0-py3-none-any.whl (14.0 kB view details)

Uploaded Python 3

File details

Details for the file ttkv-0.1.0.tar.gz.

File metadata

  • Download URL: ttkv-0.1.0.tar.gz
  • Upload date:
  • Size: 15.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for ttkv-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9d53c5c26b492dd5ffda1c566e6e540f3a02b7d31ecf36e7c40f7a9eca9fd135
MD5 d06f759bd968e01661b673dbf9494ca7
BLAKE2b-256 8474f40e0f96a5ab35103bd545d346a67d4696aaa12a0ea9c3224b7c5b08661f

See more details on using hashes here.

File details

Details for the file ttkv-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: ttkv-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 14.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for ttkv-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 90714cdd8fb7b93b1f68671a0939a24ff16b2c9fdc1c9a9f5313d1ce208e9b1f
MD5 688c4dbff9ebefa4da0916fdada5c08e
BLAKE2b-256 68f81ccadd601afcd0fded79b0583700ed70d987ebaf411d2b1a119979a4c8db

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page