pyturboquant-cpu
CPU implementation of TurboQuant, a data-oblivious vector quantization algorithm for compressing high-dimensional vectors with near-optimal distortion.
Based on the paper: TurboQuant: Online Vector Quantization with Near-optimal Distortion Rate (Zandieh et al., ICLR 2026).
Installation
pip install pyturboquant-cpu
For development:
git clone https://github.com/pyturboquant/pyturboquant-cpu.git
cd pyturboquant-cpu
pip install -e ".[dev]"
Quick Start
MSE-Optimal Quantization
Minimizes mean-squared reconstruction error:
import numpy as np
from pyturboquant_cpu import quantize_mse, dequantize_mse
# Random vectors (e.g., KV cache embeddings)
vectors = np.random.randn(100, 128) # 100 vectors of dimension 128
# Quantize at 3 bits per coordinate
quantized = quantize_mse(vectors, bits=3, seed=42)
# Reconstruct
reconstructed = dequantize_mse(quantized)
# Check reconstruction quality
mse = np.mean(np.sum((vectors - reconstructed) ** 2, axis=1))
print(f"MSE: {mse:.4f}")
Inner-Product-Optimal Quantization
Provides unbiased inner product estimates — essential for attention mechanisms and nearest-neighbor search:
from pyturboquant_cpu import quantize_prod, dequantize_prod
# Quantize at 4 bits total (3 bits MSE + 1 bit QJL correction)
quantized = quantize_prod(vectors, bits=4, seed=42)
reconstructed = dequantize_prod(quantized)
# Inner products are unbiased: E[⟨y, x̃⟩] = ⟨y, x⟩
query = np.random.randn(128)
true_ip = vectors @ query
approx_ip = reconstructed @ query
print(f"Mean IP error: {np.mean(np.abs(true_ip - approx_ip)):.4f}")
How It Works
TurboQuant is a data-oblivious algorithm — it requires no training data or calibration:
- Random Rotation: Input vectors are multiplied by a random orthogonal matrix, transforming coordinates to follow a known Beta distribution
- Lloyd-Max Scalar Quantization: Each coordinate is independently quantized using a precomputed optimal codebook for the Beta distribution
- QJL Residual Correction (Prod mode only): A 1-bit Quantized Johnson-Lindenstrauss sketch of the residual removes inner-product bias
Theoretical Distortion Bounds
For unit vectors on S^{d-1}:
| Bits | MSE Distortion | Inner Product Distortion |
|---|---|---|
| 1 | ≈ 0.36 | ≈ 1.57/d |
| 2 | ≈ 0.117 | ≈ 0.56/d |
| 3 | ≈ 0.03 | ≈ 0.18/d |
| 4 | ≈ 0.009 | ≈ 0.047/d |
These are within a factor of ~2.7× of the information-theoretic lower bound.
API Reference
quantize_mse(vectors, bits, dim=None, seed=None)
Quantize vectors using TurboQuant_MSE (MSE-optimal).
- vectors: array of shape
(..., d)— input vectors - bits: int in
[1, 8]— bits per coordinate - seed: int or None — random seed for reproducibility
- Returns:
QuantizedMSEdataclass
dequantize_mse(quantized)
Reconstruct vectors from MSE quantization result.
- quantized:
QuantizedMSE— output ofquantize_mse - Returns: ndarray with same shape as original input
quantize_prod(vectors, bits, dim=None, seed=None)
Quantize vectors using TurboQuant_Prod (unbiased inner products).
- vectors: array of shape
(..., d)— input vectors - bits: int in
[2, 8]— total bits per coordinate - seed: int or None — random seed
- Returns:
QuantizedProddataclass
dequantize_prod(quantized)
Reconstruct vectors from Prod quantization result.
- quantized:
QuantizedProd— output ofquantize_prod - Returns: ndarray with same shape as original input
GPU Version
For GPU-accelerated quantization using PyTorch, see the companion package:
pip install pyturboquant-gpu
Citation
@article{zandieh2025turboquant,
title={TurboQuant: Online Vector Quantization with Near-optimal Distortion Rate},
author={Zandieh, Amir and Daliri, Majid and Hadian, Majid and Mirrokni, Vahab},
journal={arXiv preprint arXiv:2504.19874},
year={2025}
}
License
Apache 2.0
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 pyturboquant_cpu-0.1.0.tar.gz.
File metadata
- Download URL: pyturboquant_cpu-0.1.0.tar.gz
- Upload date:
- Size: 22.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ca3806fc689a311c115fbfd0acd1d2c1812392bade568fbf7486c00329feefa7
|
|
| MD5 |
2310d0a42964e8e6c0edbb05d292ce92
|
|
| BLAKE2b-256 |
f26c8e28159ec3b0f10c831d49796c66d2d2bd3dc69d74b2e87fd40fba77e9cb
|
Provenance
The following attestation bundles were made for pyturboquant_cpu-0.1.0.tar.gz:
Publisher:
publish.yml on ishandutta2007/pyturboquant-cpu
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pyturboquant_cpu-0.1.0.tar.gz -
Subject digest:
ca3806fc689a311c115fbfd0acd1d2c1812392bade568fbf7486c00329feefa7 - Sigstore transparency entry: 2579662400
- Sigstore integration time:
-
Permalink:
ishandutta2007/pyturboquant-cpu@34370b04fade5e424fad2e021f77f7fa432b06fd -
Branch / Tag:
refs/heads/main - Owner: https://github.com/ishandutta2007
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@34370b04fade5e424fad2e021f77f7fa432b06fd -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file pyturboquant_cpu-0.1.0-py3-none-any.whl.
File metadata
- Download URL: pyturboquant_cpu-0.1.0-py3-none-any.whl
- Upload date:
- Size: 16.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
281a87ac8425d84349322c63e37d303f3cbad42e3b2f45447cac5340c08900a5
|
|
| MD5 |
ef360fc0cdc80ba670622aab31242805
|
|
| BLAKE2b-256 |
a2b94cf1f0c5e9adb4c230223506aa2c15261014d13fe8cde87830fcd8a54b55
|
Provenance
The following attestation bundles were made for pyturboquant_cpu-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on ishandutta2007/pyturboquant-cpu
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pyturboquant_cpu-0.1.0-py3-none-any.whl -
Subject digest:
281a87ac8425d84349322c63e37d303f3cbad42e3b2f45447cac5340c08900a5 - Sigstore transparency entry: 2579662403
- Sigstore integration time:
-
Permalink:
ishandutta2007/pyturboquant-cpu@34370b04fade5e424fad2e021f77f7fa432b06fd -
Branch / Tag:
refs/heads/main - Owner: https://github.com/ishandutta2007
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@34370b04fade5e424fad2e021f77f7fa432b06fd -
Trigger Event:
workflow_dispatch
-
Statement type: