More for Keys, Less for Values: Adaptive KV Cache Quantization ☝️🔑👇🔢
Project description
More for Keys, Less for Values: Adaptive KV Cache Quantization ☝️🔑👇🔢
Installation
To install the package from PyPI, run the following command:
pip install kvq
Usage
Initialization
1.1. Creating a KVQ object using a configuration object:
import torch from kvq import KVQ, KVQCacheConfig config = KVQCacheConfig( nbits_k=4, nbits_v=2, axis_key=0, axis_value=0, q_group_size=64, residual_length=128, compute_dtype=torch.bfloat16, backend="quanto", device=model.device, ) kvq = KVQ(config)1.2. Creating a KVQ object directly from a dictionary:
kvq_dict = { "nbits_k": 4, "nbits_v": 2, "axis_key": 0, "axis_value": 0, "q_group_size": 64, "residual_length": 128, "compute_dtype": torch.float16, "backend": "quanto", "device": model.device, } kvq = KVQ(kvq_dict)Using KVQ during text generation with a transformer model
# Assume 'model' is a transformer-like model (e.g. Llama, Mistral, ...) # that supports caching past key-value states. outputs = model.generate( **inputs, max_new_tokens=1024, use_cache=True, past_key_values=kvq, ) print(outputs)
GitHub Repository
The source code is hosted on GitHub:
https://github.com/mohsenhariri/kvq
Feel free to open issues, suggest improvements, or submit pull requests!
Citation
If you find our method useful, please kindly cite our paper:
Mohsen Hariri, Lam Nguyen, Sixu Chen, Shaochen Zhong, Qifan Wang, Xia Hu, Xiaotian Han, Vipin Chaudhary, “More for Keys, Less for Values: Adaptive KV Cache Quantization”, https://arxiv.org/abs/2502.15075
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file kvq-0.0.2.tar.gz.
File metadata
- Download URL: kvq-0.0.2.tar.gz
- Upload date:
- Size: 19.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
46e4be09d14ef9911871761a0b95a7ee4bcb0f7462b270a4ab6eb704bb18f870
|
|
| MD5 |
34f28b6285e46a6393014efc4e7423b3
|
|
| BLAKE2b-256 |
a65e1c4406b2d24abe2cc91a98f4f3a66908b15ec0d74d4e0222a62c98390887
|
File details
Details for the file kvq-0.0.2-py3-none-any.whl.
File metadata
- Download URL: kvq-0.0.2-py3-none-any.whl
- Upload date:
- Size: 18.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
01403dfce64659625a20ce0261c5fbe3e1f34323ac3bc390dfcbeb7c50f63ea2
|
|
| MD5 |
a808f11e421b6a5847100e7115d800d4
|
|
| BLAKE2b-256 |
3c13f5310455be020c8c56baebd1d575422de7f8fab9291a821824e78d9ffd05
|