TurboQuant — KV Cache Compression Skill
A skill for implementing, using, and explaining Google's TurboQuant algorithm — a data-oblivious
vector quantization framework that achieves 6x memory reduction and up to 8x speedup for LLM
KV caches with zero accuracy loss.
What TurboQuant Does
TurboQuant compresses the key-value (KV) cache in transformer-based LLMs. During inference,
the KV cache grows linearly with sequence length and becomes the primary memory bottleneck
for long-context generation. TurboQuant compresses each cache vector from 32-bit floats down
to 3-4 bits per coordinate — without retraining, without calibration data, and without
measurable accuracy loss.
How It Works (Two Stages)
Stage 1: PolarQuant (MSE Minimization)
Random Orthogonal Rotation: Multiply the input vector by a random orthogonal matrix Π.
This "isotropizes" the vector — after rotation, each coordinate of a unit vector follows
a Beta(d/2, d/2) distribution (shifted to [-1, 1]), regardless of the original
vector's structure.
Lloyd-Max Scalar Quantization: Quantize each rotated coordinate independently using
a Lloyd-Max quantizer optimized for the Beta(d/2, d/2) distribution. Because the
distribution is known analytically (data-oblivious), the codebook is computed once offline
and reused for all vectors.
Stage 2: QJL (Unbiased Inner Products)
Residual Computation: Compute the quantization residual r = y - ŷ (difference between
rotated vector and its quantized reconstruction).
1-bit Sign Quantization: Project the residual through a random Rademacher matrix S
and store only the signs: sign(S @ r). This uses the Quantized Johnson-Lindenstrauss
transform to preserve inner product information in just 1 bit per dimension.
Inner Product Estimation: To compute <query, key>, combine the PolarQuant
reconstruction with a QJL correction term that uses the stored signs to unbias the estimate.
When to Use Each Variant
| Variant |
Use Case |
Bits |
Accuracy |
TurboQuant_mse |
Reconstruction (nearest neighbor search) |
b bits |
MSE-optimal |
TurboQuant_prod |
Inner products (attention computation) |
b + 1 bits |
Unbiased IP |
For KV cache compression in transformers, use TurboQuant_prod — attention requires inner
products between queries and keys, and the QJL correction ensures these estimates are unbiased.
Implementation Reference
A complete Python implementation is bundled at scripts/turboquant.py. It includes:
build_lloyd_max_codebook() — Offline codebook construction via Lloyd-Max iteration
generate_rotation_matrix() — Random orthogonal matrix via QR decomposition
TurboQuant class — Compress, decompress, and estimate inner products
TurboQuantKVCache class — Simulated KV cache with compressed storage
run_self_test() — Validation suite with MSE, cosine similarity, and IP correlation metrics
Quick Start
from scripts.turboquant import TurboQuant, TurboQuantConfig
config = TurboQuantConfig(dimension=128, bits=3, qjl_enabled=True)
tq = TurboQuant(config)
# Compress a vector
compressed = tq.compress(my_vector)
# Decompress (MSE reconstruction)
reconstructed = tq.decompress(compressed)
# Estimate inner product (unbiased, with QJL correction)
ip_estimate = tq.inner_product(query_vector, compressed)
# Check compression ratio
print(f"Compression: {tq.compression_ratio():.1f}x")
KV Cache Usage
from scripts.turboquant import TurboQuantKVCache, TurboQuantConfig
config = TurboQuantConfig(dimension=128, bits=3, qjl_enabled=True)
cache = TurboQuantKVCache(config)
# During generation — append each new KV pair
cache.append(key_vector, value_vector)
# Compute attention scores for a query
scores = cache.attention_scores(query_vector)
Key Mathematical Properties
- Data-oblivious: No calibration data needed. The codebook depends only on dimension
d
and bit-width b, not on the data distribution.
- Near-optimal distortion: Achieves rate-distortion performance within constant factors
of the theoretical optimum for Euclidean vectors.
- Unbiased inner products: The QJL stage ensures
E[<q, k̂>] = <q, k> — critical for
attention computation where biased estimates shift the softmax distribution.
- O(d) compression/decompression: Linear in dimension. No codebook search.
Performance Benchmarks (from the paper)
| Metric |
Result |
| KV cache compression |
3 bits/value, 6x reduction |
| Attention speedup (H100) |
Up to 8x on 4-bit keys |
| Needle-in-Haystack (104k tokens) |
100% retrieval accuracy at 4x compression |
| Accuracy loss |
Zero measurable loss on LongBench, ZeroSCROLLS, RULER, L-Eval |
Technical Details
For deeper mathematical treatment, see references/algorithm_details.md:
- Proof sketch for why random rotation produces Beta-distributed coordinates
- Lloyd-Max convergence properties
- QJL guarantee: the Johnson-Lindenstrauss lemma for quantized projections
- Bit-rate analysis and comparison to Product Quantization
Dependencies
- Python 3.10+
- NumPy
- SciPy (for
beta distribution and minimize_scalar)
Paper Reference
Zandieh et al., "TurboQuant: Online Vector Quantization with Near-optimal Distortion Rate,"
ICLR 2026. arXiv: 2504.19874.
1---2name: turboquant3description: Implement, use, or explain TurboQuant — Google's data-oblivious vector quantization algorithm for LLM KV cache compression (ICLR 2026). Use this skill when the user asks about KV cache compression, TurboQuant, PolarQuant, QJL (Quantized Johnson-Lindenstrauss), Lloyd-Max quantization for high-dimensional vectors, reducing LLM memory usage, compressing attention keys/values, or implementing any component of the TurboQuant pipeline. Also trigger when the user mentions vector quantization for inference optimization, 3-4 bit KV cache quantization, or inner product preserving compression.4---56# TurboQuant — KV Cache Compression Skill78A skill for implementing, using, and explaining Google's TurboQuant algorithm — a data-oblivious9vector quantization framework that achieves 6x memory reduction and up to 8x speedup for LLM10KV caches with zero accuracy loss.1112## What TurboQuant Does1314TurboQuant compresses the key-value (KV) cache in transformer-based LLMs. During inference,15the KV cache grows linearly with sequence length and becomes the primary memory bottleneck16for long-context generation. TurboQuant compresses each cache vector from 32-bit floats down17to 3-4 bits per coordinate — without retraining, without calibration data, and without18measurable accuracy loss.1920## How It Works (Two Stages)2122### Stage 1: PolarQuant (MSE Minimization)23241. **Random Orthogonal Rotation**: Multiply the input vector by a random orthogonal matrix `Π`.25 This "isotropizes" the vector — after rotation, each coordinate of a unit vector follows26 a `Beta(d/2, d/2)` distribution (shifted to `[-1, 1]`), regardless of the original27 vector's structure.28292. **Lloyd-Max Scalar Quantization**: Quantize each rotated coordinate independently using30 a Lloyd-Max quantizer optimized for the `Beta(d/2, d/2)` distribution. Because the31 distribution is known analytically (data-oblivious), the codebook is computed once offline32 and reused for all vectors.3334### Stage 2: QJL (Unbiased Inner Products)35363. **Residual Computation**: Compute the quantization residual `r = y - ŷ` (difference between37 rotated vector and its quantized reconstruction).38394. **1-bit Sign Quantization**: Project the residual through a random Rademacher matrix `S`40 and store only the signs: `sign(S @ r)`. This uses the Quantized Johnson-Lindenstrauss41 transform to preserve inner product information in just 1 bit per dimension.42435. **Inner Product Estimation**: To compute `<query, key>`, combine the PolarQuant44 reconstruction with a QJL correction term that uses the stored signs to unbias the estimate.4546## When to Use Each Variant4748| Variant | Use Case | Bits | Accuracy |49|---------|----------|------|----------|50| `TurboQuant_mse` | Reconstruction (nearest neighbor search) | b bits | MSE-optimal |51| `TurboQuant_prod` | Inner products (attention computation) | b + 1 bits | Unbiased IP |5253For KV cache compression in transformers, use `TurboQuant_prod` — attention requires inner54products between queries and keys, and the QJL correction ensures these estimates are unbiased.5556## Implementation Reference5758A complete Python implementation is bundled at `scripts/turboquant.py`. It includes:5960- `build_lloyd_max_codebook()` — Offline codebook construction via Lloyd-Max iteration61- `generate_rotation_matrix()` — Random orthogonal matrix via QR decomposition62- `TurboQuant` class — Compress, decompress, and estimate inner products63- `TurboQuantKVCache` class — Simulated KV cache with compressed storage64- `run_self_test()` — Validation suite with MSE, cosine similarity, and IP correlation metrics6566### Quick Start6768```python69from scripts.turboquant import TurboQuant, TurboQuantConfig7071config = TurboQuantConfig(dimension=128, bits=3, qjl_enabled=True)72tq = TurboQuant(config)7374# Compress a vector75compressed = tq.compress(my_vector)7677# Decompress (MSE reconstruction)78reconstructed = tq.decompress(compressed)7980# Estimate inner product (unbiased, with QJL correction)81ip_estimate = tq.inner_product(query_vector, compressed)8283# Check compression ratio84print(f"Compression: {tq.compression_ratio():.1f}x")85```8687### KV Cache Usage8889```python90from scripts.turboquant import TurboQuantKVCache, TurboQuantConfig9192config = TurboQuantConfig(dimension=128, bits=3, qjl_enabled=True)93cache = TurboQuantKVCache(config)9495# During generation — append each new KV pair96cache.append(key_vector, value_vector)9798# Compute attention scores for a query99scores = cache.attention_scores(query_vector)100```101102## Key Mathematical Properties103104- **Data-oblivious**: No calibration data needed. The codebook depends only on dimension `d`105 and bit-width `b`, not on the data distribution.106- **Near-optimal distortion**: Achieves rate-distortion performance within constant factors107 of the theoretical optimum for Euclidean vectors.108- **Unbiased inner products**: The QJL stage ensures `E[<q, k̂>] = <q, k>` — critical for109 attention computation where biased estimates shift the softmax distribution.110- **O(d) compression/decompression**: Linear in dimension. No codebook search.111112## Performance Benchmarks (from the paper)113114| Metric | Result |115|--------|--------|116| KV cache compression | 3 bits/value, 6x reduction |117| Attention speedup (H100) | Up to 8x on 4-bit keys |118| Needle-in-Haystack (104k tokens) | 100% retrieval accuracy at 4x compression |119| Accuracy loss | Zero measurable loss on LongBench, ZeroSCROLLS, RULER, L-Eval |120121## Technical Details122123For deeper mathematical treatment, see `references/algorithm_details.md`:124- Proof sketch for why random rotation produces Beta-distributed coordinates125- Lloyd-Max convergence properties126- QJL guarantee: the Johnson-Lindenstrauss lemma for quantized projections127- Bit-rate analysis and comparison to Product Quantization128129## Dependencies130131- Python 3.10+132- NumPy133- SciPy (for `beta` distribution and `minimize_scalar`)134135## Paper Reference136137Zandieh et al., "TurboQuant: Online Vector Quantization with Near-optimal Distortion Rate,"138ICLR 2026. arXiv: 2504.19874.