Skip to main content

Norm-Aware KV Cache Quantization

Installation

To install the package from PyPI, run the following command:

pip install kvq

Usage

  1. Initialization

    1.1. Creating a KVQ object using a configuration object:

    import torch
    from kvq import KVQConfig, KVQ
    
    
    config = KVQConfig(
        budget = 4,
        model="meta-llama/Llama-3.1-8B-Instruct"
        residual_length=32,
        group_size={"k": 64, "v": 64}, # Group size for keys and values
        axis={"k": 0, "v": 0}, # Axis along which to quantize
    )
    
    kv_cache = KVQ(config)
    
    text = "What is the meaning of life?"
    
    inputs = tokenizer(text, return_tensors="pt").to(model.device)
    
    outputs = model.generate(
        **inputs,
        max_new_tokens=256,
        past_key_values=kv_cache,
        use_cache=True,
        pad_token_id=tokenizer.eos_token_id,
    )

GitHub Repository

The source code is hosted on GitHub:

https://github.com/mohsenhariri/spectral-kv

Feel free to open issues, suggest improvements, or submit pull requests!

Citation

If you find our work useful or interesting, please consider citing our paper:

@article{hariri2025quantize,
title     = {Quantize What Counts: Bit Allocation Insights Informed by Spectral Gaps in Keys and Values},
author    = {Hariri, Mohsen and Luo, Alan and Nemati, Mohammadreza and Nguyen, Lam and Zhong, Shaochen and Wang, Qifan and Hu, Xia and Han, Xiaotian and Chaudhary, Vipin},
journal   = {arXiv preprint arXiv:2502.15075},
year      = {2025},
url       = {https://arxiv.org/abs/2502.15075v2},
}

Metadata

Release files for kvq 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for kvq 0.1.0
File Size Uploaded
kvq-0.1.0.tar.gz 28.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for kvq 0.1.0
File Interpreter ABI Platform
kvq-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 48.5 kB

Release files / kvq-0.1.0.tar.gz

Download URL kvq-0.1.0.tar.gz
Size 28.0 kB
Tags Source
SHA-256 checksum
How to use checksums
4cc5891ba4945b550e9d1cf84f68dc48a50ba41da1d7c557add323d4830d1c0f
BLAKE2b-256 checksum
How to use checksums
f293beab503a0abfceb4b3923284caa5deb0c338d9e42d99af27d5cd6bfac889
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.11

Release files / kvq-0.1.0-py3-none-any.whl

Download URL kvq-0.1.0-py3-none-any.whl
Size 20.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1e0eb38b0e7c176d247e4a982e10972d148a5f16c228128e8c53e13b60a0e105
BLAKE2b-256 checksum
How to use checksums
e6e1e9fd0cdc2deacb5caaea1e80549d24efb33542be83d840c39e8c51b84109
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.11

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page