Near-optimal KV-cache compression for HuggingFace transformers using Lloyd-Max + QJL quantization
Project description
TurboQuant
Near-optimal KV-cache compression for HuggingFace transformers. Reduces KV cache memory by ~8x at 2 bits with attention quality within ~2.7x of the Shannon limit.
Installation
pip install turboquant-explained
Install PyTorch separately for your hardware first:
- CPU:
pip install torch- CUDA 12.x:
pip install torch --index-url https://download.pytorch.org/whl/cu121- See pytorch.org for all variants.
Quick Start
from transformers import AutoModelForCausalLM, AutoTokenizer
import turboquant
model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-3.1-8B-Instruct")
tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-3.1-8B-Instruct")
# 2-bit keys + values → ~8x memory reduction
cache = turboquant.patch_model(model, b_key=2, b_value=2)
inputs = tokenizer("Hello, world!", return_tensors="pt").to("cuda")
output = model.generate(**inputs, past_key_values=cache, max_new_tokens=200)
print(tokenizer.decode(output[0]))
patch_model reads head_dim from model.config automatically — no manual configuration needed.
Memory savings at a glance
| Bit-width | Memory vs FP16 | Typical use |
|---|---|---|
| b=2 | ~1/8 | Long contexts, aggressive compression |
| b=3 | ~3/16 | Balanced quality / savings |
| b=4 | ~1/4 | Near-lossless |
For implementation details and theory, see the full README on GitHub.
Project details
Release history Release notifications | RSS feed
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 turboquant_explained-0.1.1.tar.gz.
File metadata
- Download URL: turboquant_explained-0.1.1.tar.gz
- Upload date:
- Size: 35.9 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5d78bbddea67d78c9b4e6717b7c269e4b563816da6a1547279b6b028ea1cd2c3
|
|
| MD5 |
ac4e6e095d53724599d5c2810317bac8
|
|
| BLAKE2b-256 |
8df0237ddda6286d5821f36f23a87558c660b03a26c50bbee703de1760917cb2
|
File details
Details for the file turboquant_explained-0.1.1-py3-none-any.whl.
File metadata
- Download URL: turboquant_explained-0.1.1-py3-none-any.whl
- Upload date:
- Size: 27.1 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
50cd2c73e93094b17fe862152c0c5f000713ca7c04eb9c7fdd7f399271a92426
|
|
| MD5 |
98022867aa71dcb2c6ccc0f6e533e408
|
|
| BLAKE2b-256 |
53ad9e7461e082bcf2a2d68ab8bda1869d3faa0f2ebbec08e60d1d13ab55ffaf
|