We used CUDA 12.1 and Python 3.10
cd KVzip
pip install -r requirements.txt
pip install flash-attn==2.7.4.post1 --no-build-isolation
make i
./model/quant_model.from model import ModelKVzip
model = ModelKVzip("Qwen/Qwen2.5-7B-Instruct-1M")
context = "This is my basic profile. My name is Kim living in Seoul. My major is computer science."
queries = ["What is my name?", "Do I live in Seoul?"]
kv = model.prefill(context, load_score=False) # prefill KV cache + importance scoring
kv.prune(ratio=0.3) # compression ratio, evict 70% KV
for q in queries:
query_ids = model.apply_template(q)
output = model.generate(query_ids, kv=kv, update_cache=False) # efficient inference
print(q, output)
model/load.py, including LLaMA3, Qwen2.5/3, Gemma3.load_score=True to eliminate compression overhead. This enables context-independent KV eviction, with a trade-off in compression ratio of ratio=0.6.update_cache=True to enable multi-turn inference, retaining full interaction histories throughout the inference.python -B test.py -m [model_name] -d [data_name] --kv_type evict --ratio 0.3
-d squad. For long-context testing, use -d scbench_kv.
data/load.py.model/load.py, e.g., llama3.1-8b, qwen2.5-7b (or Qwen/Qwen2.5-7B-Instruct-1M).--kv_type retain).--level head flag with --ratio 0.6 (recommended).
./utils/head_score.python -B test.py -m [model_name] -d scbench_qa_eng --save_head_score
./utils/head_score.-d scbench_repoqa. This allows the model to use the max head scores from both natural and coding languages, which improves performance.python -B eval.py -m [model_name] -d [data_name] --kv_type retain --num 100
./results/[data_name].data/load.py.
python -B -m results.parse -m [model_name] -d [data_name]
To integrate KVzip for a new model, you will need to update the following files:
attention/attn.pymodel/monkeypatch.pymodel/template.py@article{kim2025kvzip,
title={KVzip: Query-Agnostic KV Cache Compression with Context Reconstruction},
author={Kim, Jang-Hyun and Kim, Jinuk and Kwon, Sangwoo and Lee, Jae W and Yun, Sangdoo and Song, Hyun Oh},
journal={Advances in Neural Information Processing Systems},
year={2025}
}
MIT License
52 commits
8 commits
Python
93.1%
Cuda
5.9%
We used CUDA 12.1 and Python 3.10
cd KVzip
pip install -r requirements.txt
pip install flash-attn==2.7.4.post1 --no-build-isolation
make i
./model/quant_model.from model import ModelKVzip
model = ModelKVzip("Qwen/Qwen2.5-7B-Instruct-1M")
context = "This is my basic profile. My name is Kim living in Seoul. My major is computer science."
queries = ["What is my name?", "Do I live in Seoul?"]
kv = model.prefill(context, load_score=False) # prefill KV cache + importance scoring
kv.prune(ratio=0.3) # compression ratio, evict 70% KV
for q in queries:
query_ids = model.apply_template(q)
output = model.generate(query_ids, kv=kv, update_cache=False) # efficient inference
print(q, output)
model/load.py, including LLaMA3, Qwen2.5/3, Gemma3.load_score=True to eliminate compression overhead. This enables context-independent KV eviction, with a trade-off in compression ratio of ratio=0.6.update_cache=True to enable multi-turn inference, retaining full interaction histories throughout the inference.python -B test.py -m [model_name] -d [data_name] --kv_type evict --ratio 0.3
-d squad. For long-context testing, use -d scbench_kv.
data/load.py.model/load.py, e.g., llama3.1-8b, qwen2.5-7b (or Qwen/Qwen2.5-7B-Instruct-1M).--kv_type retain).--level head flag with --ratio 0.6 (recommended).
./utils/head_score.python -B test.py -m [model_name] -d scbench_qa_eng --save_head_score
./utils/head_score.-d scbench_repoqa. This allows the model to use the max head scores from both natural and coding languages, which improves performance.python -B eval.py -m [model_name] -d [data_name] --kv_type retain --num 100
./results/[data_name].data/load.py.
python -B -m results.parse -m [model_name] -d [data_name]
To integrate KVzip for a new model, you will need to update the following files:
attention/attn.pymodel/monkeypatch.pymodel/template.py@article{kim2025kvzip,
title={KVzip: Query-Agnostic KV Cache Compression with Context Reconstruction},
author={Kim, Jang-Hyun and Kim, Jinuk and Kwon, Sangwoo and Lee, Jae W and Yun, Sangdoo and Song, Hyun Oh},
journal={Advances in Neural Information Processing Systems},
year={2025}
}
MIT License
52 commits
8 commits
Python
93.1%
Cuda
5.9%