Catalog PyPI
Back to packageRELEASE DETAILS

0.1.0

PyPI

adaptivekv

Adaptive KV-cache compression for LLM inference via dynamic token eviction and bit allocation.

inferencekv-cachellmmemory-optimizationquantizationtransformers
Published20 Aug 2026
Direct dependencies14
Transitive dependenciesNot calculated

README

Version 0.1.0 documentation

AdaptiveKV: Dynamic Bit-Allocation for KV-Cache Compression (v0.1.0)

License Python 3.10+ Status


📌 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:

  1. Severe precision loss on critical, highly-attended tokens ("attention sinks" and prompt anchors).
  2. 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 (LlamaForCausalLM research configuration and OPTForCausalLM).

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 SchemeStorage (KB)Comp RatioMemory Saved (%)Quantization MSECosine Sim (Quality)Token Agreement (%)Total Latency (ms)
FP16 Baseline65,536.01.00x0.0%0.0000001.0000100.0%365.50 $\pm$ 12.4
Fixed 4-bit17,408.03.76x73.4%0.0100880.995196.9%310.20 $\pm$ 9.8
Fixed 3-bit13,312.04.92x79.7%0.0463320.977790.6%328.40 $\pm$ 11.2
Fixed 2-bit9,216.07.11x85.9%0.2524010.892478.1%312.80 $\pm$ 8.5
AdaptiveKV (Threshold)15,847.54.14x75.8%0.0557890.973393.8%439.60 $\pm$ 15.3
AdaptiveKV (Budget 25%)19,456.03.37x70.3%0.0100880.995196.9%3103.19 $\pm$ 84.1 (Experimental)
Random Allocation (Ablation)13,312.04.92x79.7%0.1894210.921581.3%445.10 $\pm$ 14.1

For full scientific documentation, see FINAL_VALIDATION_REPORT.md.


⚡ Limitations

  1. CPU Execution Overhead: Budget-constrained optimization currently incurs higher CPU overhead during prefill due to iterative marginal gain ranking in Python loops (~3100+ ms).
  2. 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.

Downloads & popularity

Awaiting observations

No metric observations yet

Download counts and popularity will appear here as the collector receives them.

Declared dependencies 14

Native requirements
14 direct targetsNot calculated transitive count
Package / targetRequirementRelationship
torch
Environment
{
  "extras": "",
  "marker": null,
  "requires_python": ">=3.10",
  "evaluation": "NOT_EVALUATED"
}
torch>=2.1.0runtime
transformers
Environment
{
  "extras": "",
  "marker": null,
  "requires_python": ">=3.10",
  "evaluation": "NOT_EVALUATED"
}
transformers>=4.36.0runtime
numpy
Environment
{
  "extras": "",
  "marker": null,
  "requires_python": ">=3.10",
  "evaluation": "NOT_EVALUATED"
}
numpy>=1.24.0runtime
pytest
Environment
{
  "extras": "",
  "marker": "extra == \"dev\"",
  "requires_python": ">=3.10",
  "evaluation": "NOT_EVALUATED"
}
pytest>=7.4.0; extra == "dev"runtime
pytest-cov
Environment
{
  "extras": "",
  "marker": "extra == \"dev\"",
  "requires_python": ">=3.10",
  "evaluation": "NOT_EVALUATED"
}
pytest-cov>=4.1.0; extra == "dev"runtime
pytest-benchmark
Environment
{
  "extras": "",
  "marker": "extra == \"dev\"",
  "requires_python": ">=3.10",
  "evaluation": "NOT_EVALUATED"
}
pytest-benchmark>=4.0.0; extra == "dev"runtime
ruff
Environment
{
  "extras": "",
  "marker": "extra == \"dev\"",
  "requires_python": ">=3.10",
  "evaluation": "NOT_EVALUATED"
}
ruff>=0.1.0; extra == "dev"runtime
mypy
Environment
{
  "extras": "",
  "marker": "extra == \"dev\"",
  "requires_python": ">=3.10",
  "evaluation": "NOT_EVALUATED"
}
mypy>=1.7.0; extra == "dev"runtime
pre-commit
Environment
{
  "extras": "",
  "marker": "extra == \"dev\"",
  "requires_python": ">=3.10",
  "evaluation": "NOT_EVALUATED"
}
pre-commit>=3.5.0; extra == "dev"runtime
build
Environment
{
  "extras": "",
  "marker": "extra == \"dev\"",
  "requires_python": ">=3.10",
  "evaluation": "NOT_EVALUATED"
}
build>=1.0.0; extra == "dev"runtime
twine
Environment
{
  "extras": "",
  "marker": "extra == \"dev\"",
  "requires_python": ">=3.10",
  "evaluation": "NOT_EVALUATED"
}
twine>=4.0.0; extra == "dev"runtime
datasets
Environment
{
  "extras": "",
  "marker": "extra == \"bench\"",
  "requires_python": ">=3.10",
  "evaluation": "NOT_EVALUATED"
}
datasets>=2.15.0; extra == "bench"runtime
accelerate
Environment
{
  "extras": "",
  "marker": "extra == \"bench\"",
  "requires_python": ">=3.10",
  "evaluation": "NOT_EVALUATED"
}
accelerate>=0.25.0; extra == "bench"runtime
psutil
Environment
{
  "extras": "",
  "marker": "extra == \"bench\"",
  "requires_python": ">=3.10",
  "evaluation": "NOT_EVALUATED"
}
psutil>=5.9.0; extra == "bench"runtime

Distribution files

2 collected
adaptivekv-0.1.0-py3-none-any.whl

bdist_wheel · 35.2 KB · Python py3

Source file
adaptivekv-0.1.0.tar.gz

sdist · 87.99 KB · Python source

Source file

Repository

PENDING
venkateshaddanki287-eng/adaptivekv

Shared repository data · GitHub

License detected in repository
Not collected

Metadata history

Meaningful changes
Current collected state Current

Observed since 20 Oct, 19:06 UTC

No superseded metadata states collected. Unchanged observations do not create new revisions.

Record identity

package_version:fc30ae19e70967d8f7bcfa6cc8d3d86d258bd29304b64315335b975d71e7166e

Other collected versions

View all collected versions →