TL;DR
KIVI is a way to shrink the cache of keys and values that a language model keeps while it writes text. It stores each cached number in 2 bits instead of 16, and it groups the numbers differently for keys and for values, because their numbers are shaped differently. The paper reports about 2.6 times lower peak memory for Llama-2-7B, up to 4 times larger batches, and 2.35 to 3.47 times more throughput, with almost no loss in accuracy and no retraining.
Builds on: KV Cache, Post-Training Quantization (and the attention it serves, Attention Mechanism) · Leads to: later work on compressing the cache.
Why it mattered
Serving a large language model is expensive mostly because of GPU memory and GPU time. When many requests run at once (see continuous batching), the KV cache fills that memory. The paper's own example: for the OPT-175B model at batch size 512, prompt length 512 and output length 32, the cache alone requires 1.2 TB, 3.8 times the size of the model's weights (the paper cites Sheng et al., 2023, for this figure).
Earlier answers had costs. Using fewer key and value heads, as in Multi-Query Attention, needs training or fine-tuning. Evicting tokens from the cache throws information away. KIVI keeps every token and only lowers the precision of the numbers. The paper also notes that it changes only the precision of the stored numbers, and that system-level memory tools are orthogonal to its method, so both can be applied together.
The idea of quantizing the cache was not new. FlexGen, cited in the paper, used 4-bit group-wise quantization for both keys and values. SmoothQuant could compress the cache to 8 bits with small losses but, according to the paper's citation of Zhao et al. (2024), fell apart at 4 bits or less. The per-channel treatment of keys was found independently around the same time in the concurrent KVQuant work, which the paper cites as Hooper et al. (2024). KIVI appeared on arXiv in February 2024 under the identifier 2402.02750, and the paper's header names ICML as the venue. Its contribution is the measurement: the outlier pattern in keys and the sparse attention weights on values explain why the two should be grouped differently, and a streaming design lets the scheme run one token at a time.
The problem
Recall from KV Cache that during decoding each new token adds one key row and one value row for every layer and head. The cache grows with batch size times sequence length times model width, and the GPU has to read all of it from GPU memory for every token it writes.
Two obstacles stood in the way:
- Accurate weight quantizers such as GPTQ: Accurate Post-Training Quantization for Generative Pre-trained Transfor... solve an optimization problem for each block of weights. The cache is a stream that grows one token at a time, so the paper treats those methods as unsuitable and uses plain round-to-nearest.
- Plain round-to-nearest works at 4 bits but breaks at 2 bits, and the direction in which numbers are grouped decides how badly. In the paper's Table 1 for Llama-2-13B on the CoQA benchmark, the 16-bit model scores 66.37. With 2-bit keys and values both quantized per token it scores 52.93. With both quantized per channel it scores 2.88. Keys per channel and values per token scores 63.53.
Some terms used from here on. A channel is one of the model width's feature dimensions, so a key cache has one column per channel and one row per token. A scale is the size of one step on the integer grid, and a zero-point is where the grid starts. A group is the set of numbers that share one scale and zero-point. Quantizing per token means a group is one row (one token, all channels). Quantizing per channel means a group is one column (one channel, many tokens). An outlier channel is one whose values are far larger than the others.
How it works
Start with the basic operation on one tiny group of four numbers at 2 bits. Two bits give four levels, numbered 0, 1, 2 and 3.
- Input. The group is , stored as 16-bit floats. Check: four numbers, each 16 bits, so 64 bits in total.
- Find the range. The smallest value is 0.0 and the largest is 3.0. Three gaps between four levels must cover that range, so each gap is . Check: the levels are 0, 1, 2 and 3, and the largest one lands on 3.0.
- Set the start. The zero-point is the smallest value, . Check: level 0 sits at .
- Map each number to a level. Compute and round to the nearest whole number. For 0.9 that is 0.9, which rounds to 1. For 1.4 it is 1.4, which rounds to 1. For 3.0 it is 3, which is the top level. The stored codes are . Check: each code is between 0 and 3, so it fits in 2 bits.
- Store the codes. Memory keeps the four 2-bit codes plus one scale and one zero-point for the whole group. Check: the codes take 8 bits in total, against 64 bits before.
- Reconstruct when the numbers are read. Each value comes back as , giving . Check: code 1 gives .
- Measure the error. The values came back as 0.0, 1.0, 1.0 and 3.0, so the errors are 0, 0.1, 0.4 and 0. Check: the largest error, 0.4, is below half a step, which is 0.5. Rounding can never be off by more than half a step.
The idea is: fewer bits per number, and a scale and zero-point per group that keep the error inside half a step.
KIVI wraps this step in a streaming structure. Figure 3 of the paper shows the flow.
flowchart LR A[new key and value for one token] --> B[full-precision recent window] B -->|window reaches R tokens| C[quantize the oldest group] C --> D[2-bit grouped cache] B --> E[attention reads both parts] D --> E
New keys and values land in a full-precision window of R tokens. When the window is full, a group is quantized and appended to the 2-bit cache. Attention reads both parts, so the newest tokens are always exact. The paper uses group size 32 and window length R equal to 128.
- original value
- after 2-bit rounding
Worked examples
Take at 2 bits.
- The minimum is and the maximum is , so .
- The zero-point is .
- For : , so the code is 1.
- For : , which rounds to 2.
- The reconstruction for code 2 is , so comes back as , an error of .
- Check: the bound is , and .
The interpretation: each number now costs 2 bits, and the one number that was not on the grid moved to the nearest grid point.
Take the key values for one token, row 1, with three channels: . The third channel is an outlier. Now take the first channel alone across three tokens: .
- Per-token group (row 1). The range runs from to , so . The levels are , , and .
- The number is steps above the start, which rounds to 0. It comes back as , an error of .
- The outlier and the value are exact, because they sit on levels.
- Per-channel group (channel 1). The range runs from to , so . The levels are , , and .
- The number is steps up, which rounds to 2, giving , an error of .
- The values and are exact.
- Check: the per-channel error is well under its bound , and the per-token error is far above the per-channel error.
The interpretation: the outlier in the row stretches the grid so much that the normal channel is wiped out. Grouping per channel keeps the outlier away from the normal channels. This is the paper's argument for keys, and its Table 2 shows the same effect at scale: per-token keys have about 4.9 times the attention-score error of per-channel keys for Llama-2-13B.
Take Llama-2-7B. It has 32 layers and a model width of 4096, with keys and values each 4096 numbers wide. Per token, the cache holds numbers. At 16 bits, that is bytes, about 0.52 MB per token. The per-token size follows from the architecture alone.
- Batch 64 sequences of 512 tokens each is tokens.
- The 16-bit cache is bytes, about 17.2 GB.
- The weights are about GB at 16 bits.
- The 16-bit total is about GB.
- At 2 bits the cache is one eighth of that, about GB. Scales, zero-points and the window add a little more.
- The 2-bit total is about GB.
- The ratio is .
The interpretation: the saving is about 1.9 times, not 8 times, because the weights do not shrink and they dominate at this batch size. The saving grows toward 8 times as the cache grows to dominate memory, which is the regime of big batches and long contexts that the paper targets.
Try it: batch 128 sequences of 512 tokens. How large is the 16-bit cache alone? Answer: tokens, and bytes, about 34.4 GB.
The math
Step 2 in symbols
With the denominator is . For the example group, .
Step 4 in symbols
For one number , let . Then the code is . For : , and .
Step 7 in symbols
Rounding moves a number by at most half: . Multiplying by , the reconstruction error for any number in the group satisfies
The bound scales with the group's range, because is the range divided by 3. A group that spans a huge outlier has a large and a large error bound for every number in it. That is the reason grouping matters.
Why the direction matters
For the attention output, the paper writes , a weighted sum over tokens. Because the weights are sparse, a few tokens carry most of the sum. If a value group is one token, the error from quantizing that token stays inside that token's row, and the rows that matter are the ones with the largest weights. For keys, an outlier channel forces a large on any group that spans channels, which is why keys are grouped per channel instead.
Results
Quality, normal context. On the LM-Eval tasks, KIVI at 2 bits stays close to 16-bit. For Llama-2-7B, CoQA goes from 63.88 to 63.05 and GSM8K from 13.50 to 12.74. For Mistral-7B, GSM8K goes from 38.36 to 36.01. The paper reports at most about a 2 percent drop for the Llama and Mistral families. Falcon-7B uses multi-query attention with a single KV head, which is already compressed, so its 2-bit KIVI scores 57.48 on CoQA against 59.83 at 16 bits, and 4-bit KIVI scores 59.67.
Quality, long context. On LongBench, the average over its tasks is 44.52 at 16 bits and 44.27 for KIVI-2 on Llama2-7B. On Llama2-13B it is 44.85 and 44.69, and on Mistral-7B it is 46.58 and 45.85. The paper also reports that KIVI keeps needle-in-a-haystack retrieval working at 2 bits on Llama-3-8B-Instruct and Mistral-7B-Instruct-v0.2.
Efficiency. On a single A100 80GB, with a ShareGPT-style workload that averages 161 prompt tokens and 338 output tokens, KIVI reduces peak memory on Llama-2-7B by about 2.6 times, allows up to 4 times the batch size, and reaches 2.35 to 3.47 times the throughput of the 16-bit baseline. The paper says these speedups should grow with longer contexts and that fusing the cache quantization into earlier operations would help further.
Ablations. Group size 32 and 64 give similar GSM8K scores on Llama2-13B (20.77 and 21.00), while 128 drops to 17.29. For the residual length, 32, 96 and 128 score 20.62, 20.55 and 20.77 on GSM8K for Llama2-13B, and 64 is the lowest at 19.86.
Limits. The efficiency evaluation covers one model on one GPU. The accuracy claims rest on a few model families. The paper did not use closed-ended benchmarks such as MMLU, because they need only one decoding step and so do not exercise the cache much.
Common mistakes and tradeoffs
It is tempting to think that 2 bits means the whole serving memory shrinks 8 times. In fact only the cached numbers shrink 8 times. The weights, the scales, the zero-points and the full-precision window stay, so Example 3 gives about 1.9 times at batch 64. The gain approaches 8 times only when the cache dominates.
It is tempting to think that keys and values should be quantized the same way. In fact they should not. Table 1 shows that keys per channel and values per token scores 63.53 on CoQA for Llama-2-13B, while both per token scores 52.93 and both per channel scores 2.88. The mixed choice is the point of the paper.
It is tempting to think that the whole cache can be 2 bits all the time. In fact, a fake 2-bit cache without the full-precision window loses a lot on math. In the paper's Table 3, Llama-2-7B scores 13.50 on GSM8K at 16 bits and 5.76 with keys per channel and values per token quantized fully. KIVI, which keeps the recent window in full precision, scores 12.74. The window matters most for hard generation tasks.
It is tempting to think that smaller groups are always better. In fact, smaller groups store more scales, so the choice is a tradeoff between accuracy and overhead, and the paper finds 32 to be a good setting.
When to use it: long contexts, large batches, and decoding that is limited by reading the cache, with no time for retraining. When not to use it: short contexts where the weights dominate memory, or models that already share heads. Falcon-7B uses multi-query attention with one key and value head, and the paper needs 4-bit KIVI to keep its accuracy, since 2-bit loses more there. Alternatives include multi-query attention itself (which needs training), token eviction such as Attention Sink (which drops tokens), and weight quantization such as AWQ: Activation-aware Weight Quantization for LLM Compression and Acceleration (which compresses weights, not the cache). The paper says KIVI is fully compatible with weight-only quantization, so they can be combined.
Reading guide
- Section 1, Introduction. The motivation and the three findings on key and value distributions. Read it with KV Cache in mind.
- Section 2, Background. The prefill and decoding equations for keys, values and attention. Needs KV Cache and Attention Mechanism.
- Section 3.1, Preliminary study. The round-to-nearest formula and Table 1. Needs Post-Training Quantization; the examples above walk through the same formula.
- Section 3.2, Why the directions differ. The outlier figure and Table 2, plus the sparse-attention argument. Needs Softmax.
- Section 3.3, Algorithm and system support. The residual window, the streaming append rule and the fused kernels. Read after the diagram in How it works.
- Section 4, Experiments. Tables 3 to 5 and the efficiency study. Needs the Results section above and Multi-Query Attention to read the Falcon rows.
- Appendices. Algorithm 1 gives the full prefill and decoding procedure; Appendix B gives the needle-in-a-haystack setup.
Check yourself
QuestionWhat does the KV cache store, and why does a decoder keep it instead of recomputing?Show answer
QuestionA group of cached numbers is at 2 bits. What is the reconstructed value of 1.5, and what is its error?Show answer
QuestionWhy does KIVI quantize keys per channel but values per token?Show answer
QuestionA team serves a 7B model with batch 2 and 512 tokens per sequence, in 16-bit precision. Would moving the cache to 2 bits make much difference to total memory? Explain.Show answer
Sources
- arXiv abstract page: https://arxiv.org/abs/2402.02750
- Source code: https://github.com/jy-yuan/KIVI