Expand description
TurboQuant KV cache compression — CPU reference implementation.
Implements the TurboQuant_mse algorithm:
- Walsh-Hadamard rotation for incoherence
- Per-head norm extraction
- Lloyd-Max scalar quantization against N(0,1) codebooks
This module is CPU-only math — no Metal GPU dispatch.
Structs§
- Turbo
Quant Config - Configuration for TurboQuant quantization.
Enums§
- BitWidth
- Quantization bit-width for TurboQuant.
Constants§
- CODEBOOK_
2BIT - 2-bit Lloyd-Max centroids for N(0,1): 4 reconstruction levels.
- CODEBOOK_
3BIT - 3-bit Lloyd-Max centroids for N(0,1): 8 reconstruction levels.
- CODEBOOK_
4BIT - 4-bit Lloyd-Max centroids for N(0,1): 16 reconstruction levels.
- CODEBOOK_
HB_ 5BIT - 5-bit Lloyd-Max centroids for N(0,1): 32 reconstruction levels.
Mirrors
flash_attn_vec_tq_hb.metal::CODEBOOK_HB_5BIT. - CODEBOOK_
HB_ 6BIT - 6-bit Lloyd-Max centroids for N(0,1): 64 reconstruction levels.
Mirrors
flash_attn_vec_tq_hb.metal::CODEBOOK_HB_6BIT. - CODEBOOK_
HB_ 8BIT - 8-bit Lloyd-Max centroids for N(0,1): 256 reconstruction levels.
Range: [-5.0652659, +5.0652659]. Symmetry error: 3.41e-10.
Mirrors
flash_attn_vec_tq_hb.metal::CODEBOOK_HB_8BITandhadamard_quantize_kv_fast.metal::CODEBOOK_8BIT. - TBQ_
SIGNS_ 256 - D1 sign mask for the SRHT pre-multiplication, D=256 path.
- TBQ_
SIGNS_ 512 - D1 sign mask for the SRHT pre-multiplication, D=512 path.
Functions§
- apply_
d1_ sign_ mask_ inplace - Apply D1 sign mask in-place per the SRHT convention.
- compute_
lloyd_ max_ beta_ codebook - Compute Lloyd-Max codebook for Beta((d-1)/2, (d-1)/2) scaled to [-1, 1].
- compute_
lloyd_ max_ codebook - Compute Lloyd-Max codebook for N(0,1) with the given number of levels.
- fwht_
inplace - In-place normalized Fast Walsh-Hadamard Transform.
- hb_
centroid - HB codebook lookup helper.
- hb_
nearest_ centroid - HB nearest-centroid encoder (CPU-side mirror of the Metal encoder kernel).
- turboquant_
dequantize - Dequantize a TurboQuant-compressed head vector.
- turboquant_
hb_ encode_ d256 - Higher-bit (5/6/8-bit) CPU encoder for D=256 — byte-equivalent mirror of
hadamard_quantize_kv_fast.metal::hadamard_quantize_kv_hb<256>. - turboquant_
quantize - Quantize a single head vector using TurboQuant_mse.