Skip to main content

Norm-Aware KVQuant: Precision Where It Counts

Project description

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 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.bfloat16,
        "backend": "quanto",
        "device": model.device,
    }
    kvq = KVQ(kvq_dict)
  2. 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 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},
}

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

kvq-0.0.4.tar.gz (21.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kvq-0.0.4-py3-none-any.whl (20.2 kB view details)

Uploaded Python 3

File details

Details for the file kvq-0.0.4.tar.gz.

File metadata

  • Download URL: kvq-0.0.4.tar.gz
  • Upload date:
  • Size: 21.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for kvq-0.0.4.tar.gz
Algorithm Hash digest
SHA256 d28c85a9d703ec7f1259f9d6da9a4e59633944ccbcd151b45e8e3c2d4828371f
MD5 a9a6d0f0d3f55dafc04d2dd53b0a3d22
BLAKE2b-256 88ada9e9b50d8cfaecf1130ed24809f63c8e8c238e97b440292db8d7ea191587

See more details on using hashes here.

File details

Details for the file kvq-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: kvq-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 20.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for kvq-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 d4e0484a493e5e710b203f47d608c338ac4d6d761e733cc3233fadcf9e17a451
MD5 d927f0fa960fb4160b8d1a9249232c4a
BLAKE2b-256 bcbfca4e7a5cf82790718fe3267a450f27d526c0b942ef78f9236d5f3841835e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page