TurboQuant × ViT
KV-cache compression from recent LLM research (QJL, PolarQuant, TurboQuant), implemented from scratch and ported to a Vision Transformer with no retraining.
- smaller KV cache, PolarQuant 3-bit
- 10.7×
- KV memory saved, PolarQuant 3-bit
- 89%
- top-1, same as the FP32 baseline
- 83.98%
- cold start, down from ~40s
- ~2s
- Python
- PyTorch
- NumPy
- Streamlit

The problem
In a transformer, the key and value tensors cached for attention take a large share of memory. Language-model research has compressors for that cache. I wanted to know whether they carry over to a Vision Transformer without retraining it.
Pipeline
- Image
- Patch embedding
- Q, K, V projections
- Compress K and V
- Attention
- Class logits
How it works
A PyTorch hook replaces ViT-B/16 self-attention at inference time. It intercepts K and V after the linear projections, compresses and decompresses them once per layer, and computes attention against the reconstruction. Weights stay frozen, and the original forward pass is restored when the context manager exits.
QJL, PolarQuant and TurboQuant are written from scratch in NumPy and PyTorch. TurboQuant adds a 1-bit QJL sketch on PolarQuant's residual to correct inner products.
A Streamlit app runs the benchmark, compares attention heatmaps across methods layer by layer, and lets you try single images.

The decision
- I chose
- Report cold-start and warm steady-state latency separately.
- Instead of
- One average latency over all batches.
- Because
- The first batch includes CUDA warm-up and codebook construction. Averaging it in made the compressed methods look slower than they are in steady use, and hid how slow a cold start really was.
Results
| Method | Bits | Top-1 | KV memory | Compression |
|---|---|---|---|---|
| Original ViT | 32 | 83.98% | 221.63 MB | 1.00× |
| PolarQuant | 3 | 83.98% | 24.24 MB | 10.67× |
| TurboQuant | 3 | 83.98% | 31.17 MB | 9.14× |
| TurboQuant | 2 | 82.81% | 24.24 MB | 12.80× |
| QJL (reference) | 1 | 13.28% | 10.39 MB | 32.00× |
What broke, and the fix
Every inference took about 40 seconds on a cold start.
Each call rebuilt 24 compressors, one for K and one for V in each of 12 layers. A process-wide cache keyed by method, dimension, bits and seed now builds them once, and a cold start takes about 2 seconds.
The attention distortion metric crashed with tensors on different devices.
The reference attention had been forced onto the CPU. A device helper now keeps every tensor on the model's device.
The app froze or crashed on slow or offline networks.
A three-tier model loader tries the local cache, then a download under a time limit, then torchvision's ViT with adapted attention modules so the same hooks still work.
The default PyTorch wheel risked running out of memory on the free hosting tier.
Pinned the CPU-only wheel and capped the benchmark sliders when no GPU is detected.
What I would do next
- Run the benchmark on far more than 256 images. The lossless result holds on that slice, and I would not call it general until it holds on a full test set.
- Measure long sequences and large batches, where the KV cache is the real memory bottleneck.
- Fuse compression into the attention kernel. The Python path is slower than the uncompressed model, about 45 ms against 13 ms warm.