KV cache lab / 01

Inference-only research prototype

PairFold-KV

Fold two attention entries into a mean and signed residual. Keep the original softmax equation. Store less than half the cache. Change no model weights.

Cache reduction2.13×
Local CPU TPS9.16
Top-1 agreement32/32

01 / The idea

Two tokens become one structured pair.

This is a change of coordinates, not token eviction. At full precision, it reproduces the original two-token softmax attention exactly.

Two cached tokens
k₁, v₁token A
k₂, v₂token B
Pair coordinates
mmean
(1 + 2) / 2
rresidual
(1 − 2) / 2
At decode
mean scorea = q · mₖ
residual scorex = q · rₖ
original logitsa+x, a−x
// recover both logits without rebuilding either token k₁ = mₖ + rₖ    k₂ = mₖ − rₖ v₁ = mᵥ + rᵥ    v₂ = mᵥ − rᵥ
logit₁ = a + x logit₂ = a − x
pair output = (w₁+w₂)mᵥ + (w₁−w₂)rᵥ

The mean says what the two entries share. The residual says how they differ. A future query can still choose between them through the sign and magnitude of x.

The identity itself is lossless. Our implementation becomes approximate only when it quantizes the mean and residual components.

There is no learned codebook and no fine-tuning step. PairFold is not VQ.

02 / The codec

Spend bits where attention notices.

Value residuals proved more sensitive than key residuals, so the selected format is deliberately asymmetric.

mₖINT8key mean
rₖINT4key residual
mᵥINT8value mean
rᵥINT8value residual
Two BF16 K/V tokens
uncompressed
512 B
One PairFold entry
packed
232 B

The packed count includes four FP16 scales. The implementation also keeps the newest 16 tokens exact, which lowers the measured prompt reduction from the 2.21× asymptote to 2.13×.

03 / The kernel

Read the packed cache once.

The fast path is a fused AVX2 and OpenMP kernel. It never creates a decoded cache or a full attention matrix.

01LoadRead the query and packed INT4/INT8 pair.
02ScoreDequantize in registers and calculate a+x and a−x.
03NormalizeUpdate the running maximum and denominator.
04AccumulateCombine mean and residual values directly into the output.
(w₁ + w₂) · meanᵥ  +  (w₁ − w₂) · residualᵥ

04 / Results

The slowdown disappeared on CPU.

The clean run used DistilGPT-2, a 512-token prompt, 64 generated tokens, 12 CPU threads, and five alternating baseline and PairFold measurements.

MetricDefaultPairFold
Median decode speed7.75 TPS9.16 TPS
Prompt cache9.00 MiB4.23 MiB
Cache after 64 tokens10.13 MiB4.74 MiB
Top-1 agreementreference32 / 32
Top-5 overlapreference96.88%

Attention time, all six layers

Default PyTorch SDPA
0.55 ms
PairFold, separate operations
10.09 ms
PairFold, fused packed kernel
1.29 ms
Cache reduction2.13×
Ratio of median TPS1.18×
Mean logit KL0.00217

05 / Interpretation

A useful result, with a small evidence base.

The fused experiment answers one question well. PairFold's algebra does not require a 30% decode penalty. It does not yet answer the GPU or frontier-model question.

What the test supports

  • No model-weight changes are required.
  • The unquantized pair identity matches ordinary softmax attention.
  • Direct packed attention can remove the unpacking bottleneck.
  • K4/V8 retained all 32 top-1 predictions in this sample.

What remains unknown

  • The benchmark used one 82M-parameter model.
  • The shared CPU produced noisy absolute TPS measurements.
  • The speed baseline used FP32 while cache bytes use a BF16 deployment baseline.
  • PairFold still has linear memory and compute in context length.

06 / Next test

Move it to an H200.

The decisive experiment is a fused CUDA or Triton paged-attention kernel on a 7B model and a 30B-class GQA model. It should cover 4K through 128K contexts, several batch sizes, and uncontended as well as shared-GPU measurements.

That run needs to report cache bytes, tokens per second, inter-token latency, kernel time, quality, and GPU utilization together. A memory win that destroys decode speed is not a win.

go / no-go: >2× cache reduction at ≥0.95× baseline TPS