Quantize activations and persistent attention state deliberately
Separate transient activation ranges from the lifetime, layout and errors of the KV cache.
On this page
The question for today
What you’ll understand
- Distinguish transient activation quantization from stored key/value quantization.
- Account for KV payload using KV heads rather than query heads.
- Explain why context, grouping and scale lifetime belong in evaluation.
Weight compression does not automatically shrink every tensor needed for inference. Temporary activations and the persistent key/value cache have different lifetimes and range distributions. A serving system may quantize one, both or neither. Specify each tensor family separately, including when scales are chosen and where reconstructed values enter the attention computation, before estimating the memory and quality consequences of a proposed precision change.
Transient tensors and cached history are different
Static activation quantization uses parameters determined ahead of inference, commonly from calibration. Dynamic schemes compute some parameters from a runtime token, tensor or group and therefore pay a range-estimation and conversion cost. Both need compatible consumer kernels and an explicit outlier policy. A tensor’s distribution can differ between prefill and decode, so one successful short prompt does not validate all phases. Accumulation, softmax and normalization may remain at higher precision even when matrix operands use fewer bits.
The KV cache stores previously computed keys and values so later tokens can attend to history without recomputing every projection. It grows with stored tokens and uses the number of KV heads; grouped-query attention can have more query heads than KV heads. Counting query heads instead would overstate this payload for such a model. Real serving allocators also reserve pages, track sequence metadata and handle sharing or fragmentation, so a raw tensor formula is a lower-level accounting boundary rather than a complete capacity forecast.
Ranges, grouping and lifetime interact
A scale chosen for an existing cache block must remain associated with those stored codes. Replacing the scale after a new outlier arrives changes the meaning of old entries unless the block is requantized or represented with a compatible scheme. Per-token, per-channel and block-grouped formats impose different metadata and layout costs. KIVI’s studied design quantizes keys per channel and values per token, with grouped blocks and a higher-precision recent residual. Those empirical choices should not become a claim that every model has the same preferred grouping or a universally safe range.
Quantized keys perturb attention scores, while quantized values perturb the weighted sum. Score changes perturb softmax probabilities; even modest resulting output changes can alter a later token choice when its competing logits are close, after which generation can diverge. The complete real-product expansion includes both query and key perturbations if both are quantized. Even if a runtime uses a higher-precision softmax, its inputs have already changed. Evaluate long-context retrieval and domain-specific generation separately rather than accepting a cache format solely because it has low elementwise error.
Measure the complete serving footprint
Report packed KV bytes together with scale metadata, allocator overhead, live temporaries and any dequantized workspace. Measure peak resident memory at declared batch and context lengths, then measure latency and output quality under those same conditions. A fourfold code-payload reduction can deliver much less total-memory reduction when weights or metadata dominate. Include prefill and decode timings, because range estimation and cache conversion can change their costs differently. A cache release also needs a format identifier so sessions cannot read old codes with new interpretation rules.
To isolate a memory-bound ceiling argument, Reset the roofline lab and set both I=4 FLOP/byte and B=80 decimal GB/s. Predict 320 GFLOP/s. In a separate run Reset and set both I=8 and B=80; predict 640 GFLOP/s. The hypothetical byte boundary must include every activation, KV and metadata transfer being compared. Play traces the unchanged roof. This synthetic instrument demonstrates a ceiling from changed traffic, not an attention kernel, a cache allocator or a promised KV speedup.
Work it through
KV scales consume memory too
Consider one layer, one sequence, eight stored tokens, two KV heads and head dimension four. Keys and values initially use two bytes per element. Replace them with packed four-bit codes in groups of 16 elements, each carrying one two-byte scale and no zero point. Exclude all padding and allocator overhead.
Count elements as 2 times 1 times 1 times 8 times 2 times 4=128. The factor two includes both keys and values. At two bytes each, the original tensor payload occupies 256 bytes.
Four-bit codes for 128 elements use 64 bytes. There are eight groups of 16, so the two-byte scales add 16 bytes. The declared compressed representation totals 80 bytes.
The compression ratio is 256/80=3.2, rather than the fourfold code-only ratio. If the model had more query heads but the same two KV heads, this stored KV element count would remain 128 under the stated independent-head layout.
The ideal cache representation shrinks from 256 to 80 bytes. This example measures a precise payload-plus-scale boundary and does not include pages, shared prefixes, workspaces or numerical quality.
The living notebook
Explore the model
Interactive lab · roofline
Reduced declared traffic raises a ceiling
Reset → set I=4 and B=80 → predict 320 GFLOP/s. Separately Reset → set I=8 and B=80 → predict 640 GFLOP/s. Both stay below the fixed 1,000 GFLOP/s compute roof.
- Attainable ceiling
- 640 GFLOP/s
- Ridge intensity
- 12.5 FLOP/byte
- Ceiling regime
- Memory-bound ceiling
Synthetic roofline ceiling min(1000, B·I), in GFLOP/s, with B in decimal GB/s and I in FLOP/byte. Playback traces the roof without changing the selected kernel. This upper-bound model is not a measured throughput prediction, hardware comparison or price model. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.
Look forName all bytes crossing the modeled boundary. Playback traces an ideal ceiling and does not simulate KV quantization or guarantee achieved throughput.
Pause & reason
Check your understanding
Your turn
Try it yourself
Double the worked example to 16 stored tokens, keeping every other assumption. Compute original bytes, packed code bytes, scale bytes and the new ratio. Does this establish the full serving memory reduction?
Take a moment to make a prediction or work it out on paper. Open a hint when you need one.
Give me a hint
Element count doubles. Exact groups of 16 and their scale metadata double too; excluded allocations remain outside this calculation.
Compare with the solution
There are 256 elements: 512 original bytes, 128 four-bit code bytes and 16 groups times two bytes=32 scale bytes. The total is 160 bytes and the ratio remains 512/160=3.2. Full serving memory also includes weights, temporary activations, cache allocator overhead and any workspaces, so its reduction cannot be inferred from this ratio alone.
Keep the idea
Take it with you
- Activation and KV quantization have separate lifetimes and parameter policies.
- KV payload uses KV heads and includes both keys and values.
- Scale changes must preserve the interpretation of already stored codes.
Read further
Original sources and deeper explanations to follow at your own pace.
Optional progress, saved in this browser when available. Revisit any lesson at any time.
Lesson not marked complete.