Compression · E49 · Engineering practice

Design a KV-compression benchmark before designing a codec

Reconstruction error, attention error and memory accounting test different promises. A benchmark should retain all three.

NumPy codec interfaceCaptured transformer activationsMetric/accounting utilities
A codec has several budgets: bytes, reconstruction error and downstream behavior. The tradeoff curve is illustrative, not a measured frontier.
Figure 1. One codec. Several promises.. A codec has several budgets: bytes, reconstruction error and downstream behavior. The tradeoff curve is illustrative, not a measured frontier. Illustrative tradeoff; no benchmark curve. Original vector illustration.

Follow the information

From input to outcome

The original and reconstructed states are compared under a fixed attention query. Distortion, metadata and estimated storage are accounted for separately; this is not an end-to-end language-model quality result.

The original and reconstructed states are compared under a fixed attention query. Distortion, metadata and estimated storage are accounted for separately; this is not an end-to-end language-model quality result.
Figure 2. Information flow. Solid arrows carry observations, tensors or artifacts; other routes are explicitly labelled. Signal shapes, matrices and network icons are schematic, not measured samples or literal neuron counts. Open full-size SVG ↗ On narrow screens, scroll the diagram horizontally.

Read this alongside Figure 1: A codec has several budgets: bytes, reconstruction error and downstream behavior. The tradeoff curve is illustrative, not a measured frontier. The module map and layer-level figures below expand the operations in this route.

Design a KV-compression benchmark before designing a codec: system and evaluation mapPer-head K / V: Token trajectories → Candidate codec: Quantization / low rank / temporal → Reconstructed K / V: Declared approximation → Attention proxy: Fixed Q comparison → Accounting: Distortion + estimated bytes. A high-level module map; comparison branches and training details are explained in the article.COMPRESSION / E49 / MODULE MAP01 INPUTPer-head K / VToken trajectories02 MODULECandidate codecQuantization / low rank / temporal03 MODULEReconstructed K / VDeclared approximation04 MODULEAttention proxyFixed Q comparison05 OUTPUTAccountingDistortion + estimated bytes
Source-grounded module map. Boxes summarize operations, not individual neurons; comparison arms and training paths are detailed below. On a small screen, scroll the diagram horizontally.
Per-head K / V — Token trajectories

The architecture in context

The system we are building

The prototype asks whether activation trajectories can be represented more compactly than dense tensors. It compares simple quantization and low-rank baselines with temporal operator models and later piecewise or graph-based representations. Its central engineering contribution is an explicit comparator interface, not an assumption that a spline-like representation must win.

Who does what in the stack

NumPy codec interface
Compares reconstruction methods on identical tensors.
Captured transformer activations
Supply a fixed evaluation corpus.
Metric/accounting utilities
Separate distortion from resource claims.

The benchmark evaluates tensor reconstruction, an attention-output proxy and estimated representation size. Those measures are deliberately separate. A representation can fit K and V well in Frobenius norm yet damage an important attention direction, or require more metadata than its coefficients suggest.

Framework responsibility map. Each row maps a library or custom component to its job; rows are not a sequential inference graph.
Framework responsibility map. Each row maps a library or custom component to its job; rows are not a sequential inference graph. Open full-size SVG ↗

Open up the implementation

Compare codecs at the same reconstruction interface

A concrete operation-level view of this implementation; no unobserved neural architecture is implied.
A concrete operation-level view of this implementation; no unobserved neural architecture is implied. Open full-size SVG ↗

Every codec must return a tensor with the same axes and dtype contract before downstream error is meaningful. A factorization’s coefficient count is not necessarily its serialized byte count. Metadata, scales, indices, padding and retained bases can dominate small tensors.

The mathematical contract

e=∥X−X^∥F∥X∥Fe=\frac{\|X-\widehat X\|_F}{\|X\|_F}

A method with low tensor error may disturb important attention directions; a good proxy score may still fail in generation. Keep codec fitting cost, decoding cost and downstream quality separate. This archive explores representations, not an integrated accelerated autoregressive cache.

Implementation and resource card

Capacity / budget
Methods include quantization, low-rank and dynamical/polynomial approximations. Their rank/order/bit budget is not a shared neural parameter count.
Execution evidence
This revision inspects and explains the archived implementation. It does not rerun the original workload. No unrecorded convergence time, throughput or accelerator result is supplied.
Current reproduction context
Current workstation, supplied by the author: Apple M4, 128 GB unified RAM, 40 GPU cores and 16 CPU cores. This is context for prospective reproduction, not attribution of every archived run. Python and framework versions are not fully locked for these historical sources; declarations, when available, are identified separately.

From explanation to a reproducible check

Round-trip a tensor with known rank and a tensor with isolated outliers. Account for a real byte buffer where available; otherwise label storage an estimate. Do not compare estimated bits to another method’s actual resident allocation as if they were equivalent.

Preserve input identities, configuration and failure records with the result. A successful numerical check only establishes the operation it exercises: it does not certify an entire dataset, model or deployed system. Reproduce the interface on a small deterministic input before optimizing throughput or increasing workload size.

A closer look at the implementation

The code that carries the idea

The companion excerpt is the simple quantization baseline. It reconstructs a float tensor and estimates a packed payload from a nominal bit depth. That is useful as a distortion control, but it is not itself a serialized compressed cache.

Python · file · lines 6–14
def uniform_quantize(x: np.ndarray, bits: int = 8):
    qmax = (1 << (bits - 1)) - 1
    max_abs = np.max(np.abs(x))
    scale = 1.0 if max_abs == 0 else max_abs / qmax
    zero_point = 0
    q = np.clip(np.round(x / scale), -qmax - 1, qmax).astype(np.int32)
    x_hat = (q.astype(np.float32) * scale).astype(np.float32)
    bytes_used = q.size * bits / 8 + 8  # scale + zero-point metadata
    return x_hat, {"bits": bits, "scale": float(scale), "zero_point": int(zero_point), "bytes": float(bytes_used)}

Verbatim archive excerpt from quant.py (companion source E50). Context-dependent historical code, not a standalone runnable program. Comments retain their original wording; the article distinguishes implemented behavior from stale or overbroad comments.

The boundary that matters

The README describes a broader sequence of experiments than the selected CSV alone contains. No end-to-end generation quality, measured cache latency or deployed memory reduction follows from a proxy comparison. E48 and E51 identify additional boundaries in query capture and masking.

Keep building

Other posts of interest