Expand description
§tq-kv: Extreme KV Cache Compression for LLMs
Pure Rust implementation of Google’s TurboQuant algorithm (ICLR 2026). Compresses KV cache keys to 2-4 bits with up to 15x compression and 0.997 cosine similarity. Zero C/C++ dependencies.
§Algorithm
- Randomized Hadamard Transform — decorrelates outliers, O(d log d)
- Lloyd-Max Codebook Quantization — optimal centroids for Gaussian, O(d)
- Fused Attention — pre-rotate query, centroid table lookup (no decompress)
§Quick Start
use tq_kv::{TurboQuantConfig, compress_keys, decompress_keys};
let config = TurboQuantConfig::extreme(); // 2-bit, ~15x compression
let head_dim = 128;
let kv_data: Vec<f32> = vec![0.1; head_dim]; // one vector
let compressed = compress_keys(&kv_data, head_dim, &config);
println!("Ratio: {:.1}x", compressed.compression_ratio());
let restored = decompress_keys(&compressed, &config);§Incremental KV Cache
use tq_kv::*;
let config = TurboQuantConfig::extreme();
let dim = 128;
let signs = hadamard::generate_signs(dim, config.rotation_seed);
let mut cache = CompressedKeys::new_empty(config.bits, dim, config.rotation_seed);
let key = vec![0.1f32; dim];
let (packed, norm) = compress_single_key_with_signs(&key, dim, &config, &signs);
cache.append_raw(&packed, norm);Modules§
- codebook
- Lloyd-Max codebook quantization (2/3/4-bit optimal centroids). Lloyd-Max Optimal Codebook for Gaussian-distributed coordinates.
- compaction
- KV cache compaction — reduce token count via attention matching. KV Cache Compaction — reduce token count while preserving attention behavior.
- hadamard
- Fast Walsh-Hadamard Transform for decorrelation. Fast Walsh-Hadamard Transform (WHT)
Structs§
- Compressed
Keys - Paper-faithful compressed key cache. Only keys are compressed; values stay in fp16.
- Compressed
Values - Compressed value cache using per-vector absmax quantization.
- Compressed
Values4 Bit - Compressed value cache using per-group 4-bit absmax quantization.
- Compressed
Vectors - Compressed vector collection.
- Compression
Stats - Compression quality statistics.
- Decay
Tier - A single decay tier: tokens older than
age_thresholdget compressed tobits. - SparseV
Stats - Statistics from a sparse V multiply: how many positions were active vs skipped.
- Temporal
Decay Config - Temporal decay configuration.
- Turbo
Quant Config - TurboQuant configuration.
Enums§
- QjlMode
- QJL activation mode.
Functions§
- calibrate_
channel_ scales - Calibrate per-channel scaling factors from a batch of key vectors.
- calibrate_
codebook - Calibrate codebook from a batch of key vectors.
- calibrate_
codebook_ with_ rotation - Calibrate codebook with optional custom rotation matrix. If rotation_matrix is Some, uses that instead of randomized Hadamard. This ensures the codebook is fitted to the same rotation used at runtime.
- calibrate_
rotation - Calibrate optimal rotation matrix from key vectors (SpinQuant PCA approach).
- compress_
keys - Compress key vectors using paper-faithful Lloyd-Max codebook.
- compress_
single_ key - Compress a single key vector. For incremental KV cache. Returns: (packed_indices, corrected_norm)
- compress_
single_ key_ grouped - Compress a single key with per-group quantization.
- compress_
single_ key_ with_ signs - Compress a single key vector with pre-computed signs. Saves signs allocation in the hot loop.
- compress_
vectors Deprecated - V1 API: Compress vectors using PolarQuant pipeline.
Deprecated — use
compress_keys(V2 Lloyd-Max) instead for better compression ratio and speed. - decompress_
keys - Decompress keys back to f32.
- decompress_
keys_ grouped - Decompress keys with per-group norms.
- decompress_
vectors Deprecated - V1 API: Decompress PolarQuant data back to f32 vectors.
- evaluate
Deprecated - V1 API: Evaluate PolarQuant compression quality.
- evaluate_
keys - Evaluate V2 compression quality.
- fused_
attention_ scores - Batch fused attention scores: compute all attention scores between a pre-rotated query and all keys in a compressed cache.
- fused_
dot_ product - Compute attention score between pre-rotated query and compressed key.
- fused_
dot_ product_ with_ centroids - Fused dot product with pre-computed centroid table. Eliminates Codebook construction overhead in the hot loop.
- pre_
rotate_ query - Pre-rotate a query vector for fused attention.
- pre_
rotate_ query_ with_ matrix - Pre-rotate query with a custom rotation matrix (SpinQuant/PCA).
- pre_
rotate_ query_ with_ signs - Pre-rotate query with pre-computed signs (alloc-free hot path).
- softmax_
bias_ correction - Softmax bias correction (Bondarenko, arXiv:2309.01729).
- sparse_
attn_ v_ mul - Sparse attention-value multiply: only accumulate V rows where attention weight > threshold.
- sparse_
attn_ v_ mul_ compressed_ 4bit - Fused sparse attention-value multiply on 4-bit compressed values.
- sparse_
attn_ v_ mul_ compressed_ 8bit - Fused sparse attention-value multiply on 8-bit compressed values.
- sparse_
v_ stats - Count how many positions would be active for a given threshold.