DSAI4207 · Lecture 6 · Generation, Decoding & Inference

KV Cache Lab

Why isn’t token 100 a hundred times more work than token 1?

An interactive companion to the lecture. One generated sentence, six ideas — causal attention, wasted recomputation, the KV cache, its memory price, prefill vs. decode latency, and the trade-off between them — each with something to drag, step through, or predict. Explore at your own pace by scrolling, or start the Guided tour for a full-screen, one-step-per-page class presentation.

  • 6 ideas
  • 5 predict & check questions
  • 23 guided steps
  • press F for fullscreen
00The question

Same sentence, two ways to generate it.

An LLM writes one token at a time. Press Step and watch how much work each approach does at every token. Both produce exactly the same text.

Run the race
Without a cachere-project the whole prefix at every step
0K/V projections so far
With a KV cachekeep old K/V, project only the new token
0K/V projections so far
computed this stepreused from the cachenot generated yet

Recall the generation loop from the Decoding Lab: every new token is appended and the loop runs again. The KV cache is what keeps that loop cheap.

  1. AttentionEvery token looks back
  2. WasteThe past never changes
  3. KV cacheKeep it, reuse it
  4. MemoryWhat it costs
  5. LatencyPrefill, then decode
1
01Causal attention

Every new token looks back at all of its history.

Token i may attend to tokens 1…i and nothing after. Each row of the score matrix is one query; each filled cell is one query–key score.

✎ Pause & predict

The context doubles from 8 to 16 tokens. The number of attention scores grows by about…

Click a row
Scores · rows = queries, columns = keys
36query–key scores for 8 tokens
n = 8
2n = 16
Takeaway

Attention work grows with the square of the context.

2
02Recomputing the past

Old keys and values never change.

Without a cache, every step re-projects K and V for the whole prefix — even though token 3’s K and V are identical at step 3 and at step 12. Step through and click a token to see it.

✎ Pause & predict

At step 9, how many of the 8 earlier tokens have K/V that changed since step 8?

Step through it
Work grid · rows = tokens, columns = generation steps
0K/V projections computed
needed0repeated0
Takeaway

Without a cache, almost all of the work is repeated work.

3
03The KV cache

Keep the past. Compute only the new row.

Store each token’s K and V once (hatched = reused). At each step project just the new token, append its K and V, and let the new query read all cached keys. The text is identical; the work is not.

✎ Pause & predict

With a KV cache, is the attention step itself free?

Toggle the cache
Work grid
0K/V projections computed
this step: projections0attention scores0
Takeaway

The cache skips old work — not the new token’s attention.

4
04The price of the cache

The cache trades compute for memory.

Every cached token stores a K and a V in every layer. Memory grows with context length and with the number of sequences (or beam paths) kept alive. Pick a model size and drag.

✎ Pause & predict

We double the context and double the batch. KV memory becomes…

Fill the GPU
24 GiB80 GiB
2.0 GiB
Advanced: cache precision (bytes per number)

Quantising the cache to fewer bytes shrinks the bars above proportionally — one common way to fit longer contexts.

Takeaway

KV memory ∝ context length × active sequences.

5
05Prefill and decode

Prefill once, then one token at a time.

The prompt is processed in parallel (prefill) and produces token 1. Every later token is a separate decode step. Three numbers describe a request: TTFT, inter-token latency (ITL), and end-to-end latency (E2E).

✎ Pause & predict

Does a KV cache make the first token (TTFT) arrive faster?

Read the timeline

Toy timings with the right shapes (prefill ∝ prompt, cached step ≈ constant, uncached step ∝ context) — not benchmarks.

Takeaway

The cache speeds up decode (ITL, E2E) — TTFT still pays for the prompt.

6
06Putting it together

Benefit and cost both grow with context.

Benefit and cost both grow with context length. Slide the context and watch the decode speed-up (teal) climb alongside the KV memory (purple) against the GPU’s limit.

Slide the context

Illustrative constants: a = 0.25 ms/token (parallel prefill), d₀ = 18 ms (reading the weights each step), d₁ = 0.0035 ms/token (reading the cache). Shapes, not benchmarks.

Takeaway

A KV cache turns a compute problem into a memory problem.

Long chat or document?n large

Decode is fast, but one sequence’s cache can take gigabytes — memory is the limit.

Many users at once?batch × n

Memory multiplies with every active sequence; serving systems budget the cache like a resource.

Wide beam search?B paths

Each beam path keeps its own history, so B paths cost about B× the cache.