Skip to content

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
Benchmark dashboard comparing accuracy, KV-cache memory and latency across compression methods.

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

  1. Image
  2. Patch embedding
  3. Q, K, V projections
  4. Compress K and V
  5. Attention
  6. Class logits
K and V are compressed in flight by a hook; the model's weights never change. The highlighted stage is where the key decision sits.

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.

Benchmark dashboard with accuracy against compression, KV-cache memory per method, latency, and bits against accuracy.
Benchmark dashboard, rendered from the committed results.

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

CIFAR-10, 256 test images, ViT-B/16, RTX 3050, batch 16.
MethodBitsTop-1KV memoryCompression
Original ViT3283.98%221.63 MB1.00×
PolarQuant383.98%24.24 MB10.67×
TurboQuant383.98%31.17 MB9.14×
TurboQuant282.81%24.24 MB12.80×
QJL (reference)113.28%10.39 MB32.00×

What broke, and the fix

  1. 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.

  2. 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.

  3. 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.

  4. 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.