Search

Search papers and lessons

KIVI: A Tuning-Free Asymmetric 2bit Quantization for KV Cache

Zirui Liu, Jiayi Yuan, Hongye Jin et al. · 2024

KIVI stores a language model's cached keys and values in 2 bits per number, grouping keys per channel and values per token, which cuts peak memory by about 2.6 times with little loss in accuracy.

On this page

  • Colour = topic mix
  • Discrete math
  • Calculus
  • Neural network basics
  • Linear algebra
  • Probability
  • Architectures
  • ReadReady to readNot yet

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:

  1. 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.
  2. 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 ss is the size of one step on the integer grid, and a zero-point zz 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.

  1. Input. The group is x=[0.0, 0.9, 1.4, 3.0]x = [0.0,\ 0.9,\ 1.4,\ 3.0], stored as 16-bit floats. Check: four numbers, each 16 bits, so 64 bits in total.
  2. 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 s=(3.0−0.0)/3=1s = (3.0 - 0.0)/3 = 1. Check: the levels are 0, 1, 2 and 3, and the largest one lands on 3.0.
  3. Set the start. The zero-point is the smallest value, z=0.0z = 0.0. Check: level 0 sits at zz.
  4. Map each number to a level. Compute (x−z)/s(x - z)/s 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 [0,1,1,3][0, 1, 1, 3]. Check: each code is between 0 and 3, so it fits in 2 bits.
  5. 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.
  6. Reconstruct when the numbers are read. Each value comes back as code⋅s+z\text{code} \cdot s + z, giving [0,1,1,3][0, 1, 1, 3]. Check: code 1 gives 1⋅1+0=11 \cdot 1 + 0 = 1.
  7. 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.

−1−0.500.511.52−1−0.500.511.52original valuestored value
  • original value
  • after 2-bit rounding
With a step of 1 and levels from -1 to 2, every input lands on the nearest level. The staircase is the rounding error.

Worked examples

Example
1. A tiny group by hand

Take x=[−2, 0, 1.5, 4]x = [-2,\ 0,\ 1.5,\ 4] at 2 bits.

  1. The minimum is −2-2 and the maximum is 44, so s=(4−(−2))/3=2s = (4-(-2))/3 = 2.
  2. The zero-point is z=−2z = -2.
  3. For x=0x = 0: (0−(−2))/2=1(0-(-2))/2 = 1, so the code is 1.
  4. For x=1.5x = 1.5: (1.5−(−2))/2=1.75(1.5-(-2))/2 = 1.75, which rounds to 2.
  5. The reconstruction for code 2 is 2⋅2+(−2)=22 \cdot 2 + (-2) = 2, so 1.51.5 comes back as 22, an error of 0.50.5.
  6. Check: the bound is s/2=1s/2 = 1, and 0.5≤10.5 \le 1.

The interpretation: each number now costs 2 bits, and the one number that was not on the grid moved to the nearest grid point.

Example
2. Per token or per channel: the same numbers, two groupings

Take the key values for one token, row 1, with three channels: [0.5, −0.5, 40][0.5,\ -0.5,\ 40]. The third channel is an outlier. Now take the first channel alone across three tokens: [0.5, −0.5, 0.1][0.5,\ -0.5,\ 0.1].

  1. Per-token group (row 1). The range runs from −0.5-0.5 to 4040, so s=40.5/3=13.5s = 40.5/3 = 13.5. The levels are −0.5-0.5, 1313, 26.526.5 and 4040.
  2. The number 0.50.5 is (0.5−(−0.5))/13.5=0.074(0.5 - (-0.5))/13.5 = 0.074 steps above the start, which rounds to 0. It comes back as −0.5-0.5, an error of 1.01.0.
  3. The outlier 4040 and the value −0.5-0.5 are exact, because they sit on levels.
  4. Per-channel group (channel 1). The range runs from −0.5-0.5 to 0.50.5, so s=1/3s = 1/3. The levels are −0.5-0.5, −0.167-0.167, 0.1670.167 and 0.50.5.
  5. The number 0.10.1 is (0.1−(−0.5))/(1/3)=1.8(0.1 - (-0.5))/(1/3) = 1.8 steps up, which rounds to 2, giving 0.1670.167, an error of 0.0670.067.
  6. The values 0.50.5 and −0.5-0.5 are exact.
  7. Check: the per-channel error 0.0670.067 is well under its bound s/2=0.167s/2 = 0.167, and the per-token error 1.01.0 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.

Example
3. Realistic: how much memory does 2 bits save?

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 2⋅4096⋅32=262,1442 \cdot 4096 \cdot 32 = 262{,}144 numbers. At 16 bits, that is 262,144⋅2=524,288262{,}144 \cdot 2 = 524{,}288 bytes, about 0.52 MB per token. The per-token size follows from the architecture alone.

  1. Batch 64 sequences of 512 tokens each is 64⋅512=32,76864 \cdot 512 = 32{,}768 tokens.
  2. The 16-bit cache is 32,768⋅524,288=17,179,869,18432{,}768 \cdot 524{,}288 = 17{,}179{,}869{,}184 bytes, about 17.2 GB.
  3. The weights are about 7⋅109⋅2=147 \cdot 10^{9} \cdot 2 = 14 GB at 16 bits.
  4. The 16-bit total is about 14+17.2=31.214 + 17.2 = 31.2 GB.
  5. At 2 bits the cache is one eighth of that, about 17.2/8=2.117.2/8 = 2.1 GB. Scales, zero-points and the window add a little more.
  6. The 2-bit total is about 14+2.1=16.114 + 2.1 = 16.1 GB.
  7. The ratio is 31.2/16.1=1.9331.2/16.1 = 1.93.

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: 128⋅512=65,536128 \cdot 512 = 65{,}536 tokens, and 65,536⋅524,288=34,359,738,36865{,}536 \cdot 524{,}288 = 34{,}359{,}738{,}368 bytes, about 34.4 GB.

The math

Definition: Group-wise round-to-nearest quantization
For a group of numbers XX and BB bits, set z=min⁡Xz = \min X and s=(max⁡X−min⁡X)/(2B−1)s = (\max X - \min X)/(2^{B}-1). The codes are Q(X)=round((X−z)/s)Q(X) = \text{round}\big((X - z)/s\big), and the reconstructed values are X′=Q(X)⋅s+zX' = Q(X)\cdot s + z.

Step 2 in symbols

With B=2B = 2 the denominator is 22−1=32^{2}-1 = 3. For the example group, s=(3.0−0.0)/3=1s = (3.0 - 0.0)/3 = 1.

Step 4 in symbols

For one number xx, let t=(x−z)/st = (x - z)/s. Then the code is round(t)\text{round}(t). For x=0.9x = 0.9: t=0.9/1=0.9t = 0.9/1 = 0.9, and round(0.9)=1\text{round}(0.9) = 1.

Step 7 in symbols

Rounding moves a number tt by at most half: ∣t−round(t)∣≤12|t - \text{round}(t)| \le \tfrac{1}{2}. Multiplying by ss, the reconstruction error for any number in the group satisfies

∣x−x′∣=s ∣t−round(t)∣≤s2.|x - x'| = s\,|t - \text{round}(t)| \le \frac{s}{2}.

The bound scales with the group's range, because ss is the range divided by 3. A group that spans a huge outlier has a large ss 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 [AXV]i=∑jAij[XV]j[AX_V]_{i} = \sum_j A_{ij} [X_V]_{j}, a weighted sum over tokens. Because the weights AijA_{ij} 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 ss 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
It stores the attention keys and values for every past token, in every layer. Each new token then needs only its own key and value computed; the attention for it reads the stored rows, so past tokens are not recomputed on every step.
QuestionA group of cached numbers is [−2, 0, 1.5, 4][-2,\ 0,\ 1.5,\ 4] at 2 bits. What is the reconstructed value of 1.5, and what is its error?Show answer
The scale is s=(4−(−2))/3=2s = (4-(-2))/3 = 2 and the zero-point is z=−2z = -2. Then t=(1.5+2)/2=1.75t = (1.5+2)/2 = 1.75, which rounds to 2. The reconstruction is 2⋅2+(−2)=22 \cdot 2 + (-2) = 2. The error is 2−1.5=0.52 - 1.5 = 0.5, which is within the bound s/2=1s/2 = 1.
QuestionWhy does KIVI quantize keys per channel but values per token?Show answer
Keys have a few fixed channels with very large magnitudes. Grouping per channel confines each outlier to its own channel, so the normal channels keep a fine grid. Values have no outlier channels, and the attention output is a sparse weighted sum over tokens, so grouping per token keeps each token's error inside its own row, and the few tokens with large weights stay accurate.
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
Not much. The cache is 2⋅512=1,0242 \cdot 512 = 1{,}024 tokens times about 0.52 MB per token, roughly 0.54 GB. The weights are about 14 GB and do not change. The total goes from about 14.5 GB to about 14.1 GB, a ratio near 1.03. The saving only matters when the cache is much larger than the weights, which needs a larger batch or a longer context.

Sources

Read the full paper

Zirui Liu, Jiayi Yuan, Hongye Jin et al., 2024. arXiv:2402.02750