↑↓ move Enter open ⌘K find from any page 256 of 256 tabs
Loading the recording…

What KV-cache quantization is

The KV cache grows by one row per layer for every token, and in long contexts or large batches it outweighs the model's weights (the KV cache tab). KV-cache quantization stores the cached keys and values as small integers with a few floating-point scales, and dequantizes them when a query reads them. Unlike weight quantization it has to work online: rows arrive one at a time during decoding, and there is no calibration step.

This page stores the KV cache tab's cache, Qwen2.5-0.5B-Instruct answering the guest Wi-Fi question, as 8-, 4- or 2-bit integers, KIVI's way (keys per channel, values per token) or with keys per token for contrast. Each scheme runs teacher-forced on the full-precision answer, so every step compares with the full cache, and free-running, to see where its own answer drifts.

Min–max quantization

Each group of numbers that shares a scale is quantized asymmetrically, from its minimum to its maximum:

with bits per code and and stored in float16. The choice that matters is which numbers share a group. A cached key or value of one head is a 64-number row per token, so a group can run along a row (per token: one scale for a token's 64 channels) or down a column (per channel: one scale for one channel over several tokens). The cache panel (The cache as int4, or int8 or int2 as the toolbar picks) switches between the full-precision cache, the integers, the dequantized cache and the error, for the four layers recorded in full (1, 9, 17 and 24).

Why keys go per channel

In the full-precision keys a few channels are far larger than the rest in every token: the vertical stripes in the cache. Per token, one scale must span those outliers, and the small channels round to a handful of levels. Per channel, each column gets its own range. Values have no such fixed channels, and each attention output is a weighted average of value rows, so a value's error stays within its own token; values go per token.

The Why keys go per channel chart measures the relative error in all 24 layers. Averaged over the layers, the keys' error is 0.00183 per channel against 0.0071 per token at 8 bits, 0.0311 against 0.120 at 4 bits and 0.156 against 0.574 at 2 bits; the values' error, per token in both schemes, is 0.00622, 0.106 and about 0.53.

Streaming the cache

Per-channel keys need several tokens to form a group, but decoding produces one token at a time. KIVI keeps the newest rows in full precision until a group of 16 is complete, then quantizes the group. The prefill attends in full precision; every decode step reads the dequantized rows plus the full-precision tail. In the 69-row cache at the end of the answer, 64 rows are integers and 5 are still bfloat16.

The Attention panel of each step sets one query head's weights over the full cache against those over the dequantized cache, with their total variation distance; clicking the weights column opens every key's pair. The Next token panel compares the two next-token distributions and their KL divergence in nats.

What the answers show

At 8 bits both schemes give the full cache's answer, "The password for the guest Wi-Fi is "maple-river-42".". At 4 bits both answer "The guest Wi-Fi password is "maple-river-42".", leaving the full answer right after "The", where the full cache itself was close to a tie: " password" 52 %, " guest" 43 %. At 2 bits the per-channel cache keeps all 17 tokens of the answer and the password, then writes " This password is a random combination of" where the full cache ended the answer (end of turn 72 %, " This" 17 %); the per-token cache breaks right after "The" and writes nonsense. Keys per channel are what make 2 bits usable.

The Memory panel counts the bytes for these 69 rows: 828 KiB in bfloat16, and 504, 312 and 216 KiB at 8, 4 and 2 bits with keys per channel, 1.6, 2.7 and 3.8 times smaller. At this length the float16 scales and zeros and the full-precision tail weigh a lot; over long contexts the per-channel ratio approaches 1.7, 3.0 and 4.9 times.

Where it came from

FlexGen (2023) compressed the KV cache to 4 bits with group-wise quantization to fit large batches on one GPU. KIVI (2024) studied the distribution of keys and values, found the outlier channels in keys, and proposed per-channel keys, per-token values and a full-precision residual for a tuning-free 2-bit cache. KVQuant (2024) also quantizes keys per channel, before RoPE is applied, with non-uniform levels and separately stored outliers. Other tabs shrink the cache differently: KV eviction keeps fewer rows, and grouped-query attention (the GQA tab) and MLA (the MLA tab) store fewer or smaller rows per token.

Papers

  1. KIVI: A Tuning-Free Asymmetric 2bit Quantization for KV Cache Zirui Liu, Jiayi Yuan, Hongye Jin, Shaochen Zhong et al. · ICML 2024 The scheme on this page: keys per channel in groups of tokens, values per token, the newest rows kept in full precision.
  2. KVQuant: Towards 10 Million Context Length LLM Inference with KV Cache Quantization Coleman Hooper, Sehoon Kim, Hiva Mohammadzadeh, Michael W. Mahoney, Yakun Sophia Shao, Kurt Keutzer, Amir Gholami · NeurIPS 2024 Per-channel keys quantized before RoPE, non-uniform levels and dense-and-sparse outliers for caches below 4 bits.
  3. FlexGen: High-Throughput Generative Inference of Large Language Models with a Single GPU Ying Sheng, Lianmin Zheng, Binhang Yuan, Zhuohan Li et al. · ICML 2023 Group-wise 4-bit compression of weights and the attention cache, an early KV-cache quantizer.
  4. Qwen2.5 Technical Report An Yang et al. (Qwen Team) · arXiv 2024 The Qwen2.5 models, including the 0.5B instruct model whose cache this page quantizes.

See also