Log inStart free
LESSON 30.10 · 6 STAGES · 2 CONTROLS · 3 CHECKSPRO · PREVIEW

KV-cache quantization

See a KV cache shrink to 8 or 4 bits, and what the rounding costs.


Pro lesson. Stage 1 is free to watch. Pro opens all 6 stages, 2 controls and 3 checks, in this lesson and every other.

Go Pro · $8.25/mo
EXPLAINER · 30.10 · KV CACHEFP16 · 8 TOKENS · 256 B
01
Stage 1 of 6 · The KV cache
The KV cache keeps a key and a value for each of 8 tokens. Key channel 4 is an outlier channel.
Explain it simply

You see two grids of squares. Each row is one token, and each square is one number: a big square is a big number. The keys on the left have one column, channel 4, with numbers about six times larger than the rest. Real models show the same thing in their keys, but not in their values.

Two grids of 8 tokens by 8 channels: keys on the left, values on the right. Key channel 4 holds numbers near 3.7; the other numbers stay near 0.6.

keys and values (hollow: negative, also for errors)scales, bitsrounding errorattention resultexact FP16, or a 0
keys Kvalues V123ch 4567812345678t1t2t3t4t5t6t7t82 × 8 tokens × 8 channels × 2 B = 256 B in FP16keys Kvalues V123ch 4567812345678t1t2t3t4t5t6t7t82 × 8 tokens × 8 channels × 2 B = 256 B in FP16
Toy: one attention head, 8 cached tokens × 8 channels, seeded numbers
STAGE 1 / 6
MATH · STAGE 1HOVER A SYMBOL OR A SHAPE
bytes=2⋅T⋅d⋅2 B\text{bytes} = 2 \cdot \sy{t}{data}{T} \cdot \sy{d}{data}{d} \cdot 2\,\text{B}
2⋅8⋅8⋅2 B=256 B2 \cdot 8 \cdot 8 \cdot 2\,\text{B} = 256\,\text{B}

Every key and value number takes 2 bytes.

Common mix-ups

MYTHOne INT4 scale per token works for keys too.

The fix, and 1 more mix-up with the diagram to show them, come with Pro.

Predict, then reveal
3 CHECKS · PRO
Q01At INT4, do per-token key scales make the output error grow or shrink?

Make your guess, then test it in the diagram. All 3 checks come with Pro.

PRO LESSON · YOU WATCHED STAGE 1 OF 6
Unlock the full explainer
  • 5 more stages and 2 live controls
  • 2 mix-up cards and 3 predict-then-reveal checks
  • Watch it as a narrated video, in three voices
  • Every Pro lesson and capstone · 656 in total

$99 a year, or $15 a month. Cancel any time.