AdaptiveKV: Dynamic Bit-Allocation for KV-Cache Compression (v0.1.0)
📌 What is AdaptiveKV?
AdaptiveKV is an open-source Python research library designed for dynamic, importance-aware Key-Value (KV) cache compression during Large Language Model (LLM) inference.
❓ The Problem
In autoregressive transformer inference, storing key and value states for long context sequences consumes vast amounts of GPU/CPU memory (e.g. tens of gigabytes). Standard uniform quantization applies the same fixed bit-width (e.g. all 2-bit or all 4-bit) across all tokens. This leads to two major flaws:
- Severe precision loss on critical, highly-attended tokens ("attention sinks" and prompt anchors).
- Wasted memory precision on low-importance tokens.
💡 How It Works
AdaptiveKV dynamically evaluates the attention importance of KV blocks during prefill and decoding iterations. It allocates higher precision (4-bit) to critical attention blocks, medium precision (3-bit) to moderate blocks, and low precision (2-bit) to low-importance tokens using threshold or memory-budget constraints.
🔬 Experimental Research Results & Conclusion
[!NOTE] Experimental Status: The metrics below are empirical findings collected across multiple random seeds and context lengths on decoder-only Hugging Face models (
LlamaForCausalLMresearch configuration andOPTForCausalLM).
Scientifically Defensible Conclusion:
"AdaptiveKV provides evidence of a favorable quality-memory trade-off under the evaluated settings by leveraging token attention importance."
Benchmark Results (Context = 2048 Tokens, 3 Seeds):
| Compression Scheme | Storage (KB) | Comp Ratio | Memory Saved (%) | Quantization MSE | Cosine Sim (Quality) | Token Agreement (%) | Total Latency (ms) |
|---|---|---|---|---|---|---|---|
| FP16 Baseline | 65,536.0 | 1.00x | 0.0% | 0.000000 | 1.0000 | 100.0% | 365.50 $\pm$ 12.4 |
| Fixed 4-bit | 17,408.0 | 3.76x | 73.4% | 0.010088 | 0.9951 | 96.9% | 310.20 $\pm$ 9.8 |
| Fixed 3-bit | 13,312.0 | 4.92x | 79.7% | 0.046332 | 0.9777 | 90.6% | 328.40 $\pm$ 11.2 |
| Fixed 2-bit | 9,216.0 | 7.11x | 85.9% | 0.252401 | 0.8924 | 78.1% | 312.80 $\pm$ 8.5 |
| AdaptiveKV (Threshold) | 15,847.5 | 4.14x | 75.8% | 0.055789 | 0.9733 | 93.8% | 439.60 $\pm$ 15.3 |
| AdaptiveKV (Budget 25%) | 19,456.0 | 3.37x | 70.3% | 0.010088 | 0.9951 | 96.9% | 3103.19 $\pm$ 84.1 (Experimental) |
| Random Allocation (Ablation) | 13,312.0 | 4.92x | 79.7% | 0.189421 | 0.9215 | 81.3% | 445.10 $\pm$ 14.1 |
For full scientific documentation, see FINAL_VALIDATION_REPORT.md.
⚡ Limitations
- CPU Execution Overhead: Budget-constrained optimization currently incurs higher CPU overhead during prefill due to iterative marginal gain ranking in Python loops (~3100+ ms).
- Hardware Backends: Evaluated primarily on CPU execution backends; GPU VRAM allocation scaling requires CUDA kernel extensions.
💻 Installation
git clone https://github.com/venkateshaddanki287-eng/adaptivekv.git
cd adaptivekv
pip install -e .
🚀 Quick Start Example
import torch
from adaptivekv import AdaptiveKVConfig, AllocationConfig
from adaptivekv.cache import AdaptiveKVCache
# Configure AdaptiveKV with Threshold strategy
config = AdaptiveKVConfig(
allocation=AllocationConfig(
strategy="threshold",
bits=(2, 3, 4)
)
)
# Initialize AdaptiveKVCache for LLM generation
cache = AdaptiveKVCache(config)
🔁 Reproducibility Commands
# 1. Run unit test suite
pytest
# 2. Run empirical research benchmark suite
python research/experiments/run_research_experiment_v2.py
# 3. Generate SVG research figures
python research/generate_figures.py
# 4. Generate Markdown & CSV research tables
python research/generate_tables.py
🏗️ Architecture Overview
adaptivekv/
├── src/adaptivekv/
│ ├── quantizer.py # 2-bit, 3-bit, 4-bit uint8 bit-packing engine
│ ├── cache.py # Hugging Face transformers Cache implementation
│ ├── integration.py # AutoModel integration adapters
│ ├── importance.py # Attention, magnitude, and recency analyzers
│ ├── allocator.py # Threshold & budget allocators
│ ├── metrics.py # Quality & storage metrics
│ └── cli.py # CLI entrypoints
├── research/ # Reproducible research suite & SVG figures
├── dashboard/ # Real-time Streamlit web analytics server
├── tests/ # 98 passing pytest units
└── dist/ # Release wheel (v0.1.0)
📄 License & Citation
Licensed under the Apache 2.0 License. See CITATION.cff for software citation format.
Metadata
Release files for adaptivekv 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| adaptivekv-0.1.0.tar.gz | 90.1 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| adaptivekv-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 126.1 kB
Release files / adaptivekv-0.1.0.tar.gz
| Download URL | adaptivekv-0.1.0.tar.gz |
|---|---|
| Size | 90.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6a4f76c61ee4cbf665ddac55498f7de421018900c29f8c75458a36123426e402
|
|
BLAKE2b-256 checksum How to use checksums |
648f556fe063ec0a9f5f21468fd55dcf8f4c974512af6ba7b30de5bcf83d68cc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.10.5
|
Release files / adaptivekv-0.1.0-py3-none-any.whl
| Download URL | adaptivekv-0.1.0-py3-none-any.whl |
|---|---|
| Size | 36.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a1db8cd1e3c428a805d45b1f064943f362896f78416727b2d5cbba230ba8bdcb
|
|
BLAKE2b-256 checksum How to use checksums |
b71e02038c66b7736a6594c7172d005bdd0f5d13518269bb13d100ecf914eca7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.10.5
|
Security for adaptivekv 0.1.0
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.