Same Llama 3.2 1B, same NVIDIA L4, one change: a KV cache. In Part 1, every new token re-ran the whole model over the entire sequence. Now each token computes only what's new, and reuses everything else.
Time per token, without and with the cache. Without it, each token is slower than the last. With it, the 2,048th token costs about the same as the 1st.
Attention compares each token's query (q) with the key (k) of every token up to and including itself. Here's the work for generating token 6.
The rows above the newest one never change: earlier tokens can't look ahead at later ones, so their results were final the moment they were produced. That's why the cache stores K and V for every past token, and nothing from the old queries.
The same breakdown as Part 1, with the cache. The GPU's work per token shrank to ~12 ms. The grey part, the GPU idle while Python sends it the next operation, did not shrink. Later in the sequence it even grew, because it's no longer hidden behind long stretches of GPU work.
These are the 10 profiled tokens, which also carry the profiler's own overhead (~58 ms each). Across the whole run, tokens averaged ~39 ms, and the GPU sat idle 70% of the time.
Part 1's GPU was busy 98% of the time. Now it's busy about 20%. Busy wasn't good there: it was busy redoing work. The cache removed that work, and exposed the next bottleneck. Each token still launches hundreds of small operations from Python, one at a time, and the GPU finishes each one before the next arrives.
With one token at a time, there's very little math left. What's left is reading the model.
The measured GPU time is about 12 ms per token, not far above that floor. So decoding is now limited by how fast the GPU can read memory, not how fast it can multiply. That's the opposite of the prompt (prefill) step, where many tokens share one read of the weights.
The idea of a KV cache is simple. These are the parts that only became obvious once I wrote one.
Each step feeds just one token through the model. Its query is the only one that matters: older queries already produced their tokens, and their rows of the attention matrix will never change. So you cache keys and values, never queries.
The mask exists so a token can't see the future. During generation, the single new token is the latest one; everything in the cache is its past. There's nothing to mask. The mask only matters for the prompt, when many tokens go in at once.
if num_input_tokens > 1: # prompt only
scores = scores + causal_mask
Llama encodes position by rotating queries and keys (RoPE). The cache stores keys already rotated, so they're never touched again. The new token must be rotated using its real position in the sequence, not position 0. Get that wrong and nothing crashes; the model just quietly gets worse.
cos = cos_cached[start_pos : start_pos + n] # not [0 : n]
Llama 3.2 uses grouped-query attention: 32 query heads share 8 key/value heads. Store the 8 and expand them only when computing attention. That makes the cache 4× smaller: 32 KB per token across all 16 layers. 2,048 tokens is just 64 MB, but the full 128K context would be 4 GB, which is why serving systems obsess over cache memory.
Appending with torch.cat copies the whole cache on every token, in every layer. Pre-allocating one buffer and writing each new key and value into its slot keeps the cache update at about 0.04 ms per token, the "KV write" column below.
self.k[layer, :, start_pos : end_pos] = xk # in place, no copy
All 16 layers, each operation summed across layers, in the order they run. Start with the speedup view, then flip to the raw milliseconds. Token 1 is the prompt, the same work either way, so everything there is about 1×.
| Attention block | Feed-forward block | Output | |||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Token | Embedding | Norm | Q proj | K proj | V proj | RoPE | KV write | Attention | O proj | Norm | Gate + Up | SiLU × mul | Down | Final norm | LM head | Sampling | GPU busy | Waiting on Python | Total |
| 1 | ≈ | 1× | 1× | 1.2× | 1.2× | 1× | new | 1.2× | 1× | 1× | 1× | 1.6× | 1× | 1× | 1× | 1× | 1× | 0.86× | 0.9× |
| 250 | 5.0× | 2.3× | 1.5× | 1.4× | 1.4× | 2.0× | new | 6.0× | 1.5× | 2.1× | 1.7× | 8.0× | 1.5× | 2.0× | 1× | 1× | 1.7× | 0.82× | 1.0× |
| 500 | 15× | 4.1× | 2.9× | 3.1× | 3.2× | 4.0× | new | 30× | 2.9× | 4.3× | 2.4× | 25× | 2.6× | 3.0× | 1× | 1.3× | 3.7× | 0.28× | 0.9× |
| 750 | 20× | 5.1× | 2.9× | 3.1× | 3.1× | 4.9× | new | 77× | 3.0× | 5.0× | 3.1× | 53× | 3.4× | 4.5× | 1× | 1.3× | 6.9× | 0.10× | 1.5× |
| 1,000 | 13× | 6.4× | 4.0× | 5.5× | 5.4× | 6.3× | new | 135× | 4.0× | 6.3× | 3.8× | 84× | 4.2× | 5.5× | 1× | 1.3× | 12× | 0.11× | 2.6× |
| 1,250 | 20× | 7.9× | 5.6× | 6.0× | 5.8× | 7.5× | new | 173× | 5.7× | 8.0× | 5.4× | 123× | 5.7× | 6.5× | 1× | 1.3× | 18× | 0.11× | 3.6× |
| 1,500 | 23× | 9.6× | 6.2× | 6.1× | 6.0× | 9.1× | new | 200× | 6.2× | 9.7× | 6.7× | 178× | 6.4× | 8.0× | 1× | 1.3× | 23× | 0.16× | 4.8× |
| 1,750 | 23× | 11× | 7.3× | 7.1× | 6.9× | 11× | new | 243× | 7.4× | 11× | 7.3× | 204× | 7.5× | 9.5× | 1× | 1.3× | 28× | 0.13× | 5.9× |
| 2,000 | 27× | 13× | 7.4× | 9.8× | 9.9× | 12× | new | 264× | 7.5× | 14× | 7.4× | 246× | 8.2× | 12× | 1× | 1.7× | 34× | 0.14× | 7.2× |
| 2,048 | 27× | 12× | 7.7× | 9.4× | 9.6× | 12× | new | 235× | 7.6× | 14× | 7.8× | 266× | 8.5× | 11× | 1× | 1.3× | 33× | 0.13× | 7.0× |
| Attention block | Feed-forward block | Output | |||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Token | Embedding | Norm | Q proj | K proj | V proj | RoPE | KV write | Attention | O proj | Norm | Gate + Up | SiLU × mul | Down | Final norm | LM head | Sampling | GPU busy | Waiting on Python | Total |
| 1 | 0.008 | 0.34 | 0.59 | 0.21 | 0.21 | 0.36 | 0.04 | 0.41 | 0.62 | 0.32 | 4.51 | 0.05 | 2.19 | 0.02 | 2.07 | 0.03 | 11.98 | 57.54 | 69.52 |
| 250 | 0.002 | 0.27 | 0.58 | 0.16 | 0.16 | 0.31 | 0.04 | 0.38 | 0.58 | 0.27 | 4.30 | 0.04 | 2.09 | 0.02 | 2.07 | 0.03 | 11.29 | 49.30 | 60.59 |
| 500 | 0.002 | 0.27 | 0.58 | 0.16 | 0.16 | 0.31 | 0.04 | 0.52 | 0.58 | 0.27 | 4.30 | 0.04 | 2.09 | 0.02 | 2.07 | 0.03 | 11.44 | 54.51 | 65.95 |
| 750 | 0.002 | 0.27 | 0.58 | 0.16 | 0.16 | 0.31 | 0.04 | 0.60 | 0.58 | 0.27 | 4.30 | 0.04 | 2.09 | 0.02 | 2.07 | 0.03 | 11.53 | 45.97 | 57.50 |
| 1,000 | 0.003 | 0.27 | 0.58 | 0.16 | 0.16 | 0.31 | 0.04 | 0.76 | 0.58 | 0.27 | 4.30 | 0.04 | 2.09 | 0.02 | 2.07 | 0.03 | 11.69 | 45.47 | 57.16 |
| 1,250 | 0.003 | 0.27 | 0.58 | 0.16 | 0.16 | 0.31 | 0.04 | 0.87 | 0.58 | 0.27 | 4.30 | 0.04 | 2.09 | 0.02 | 2.07 | 0.03 | 11.80 | 46.84 | 58.64 |
| 1,500 | 0.003 | 0.27 | 0.58 | 0.16 | 0.16 | 0.31 | 0.04 | 1.02 | 0.58 | 0.27 | 4.30 | 0.04 | 2.09 | 0.02 | 2.07 | 0.03 | 11.95 | 46.33 | 58.28 |
| 1,750 | 0.003 | 0.27 | 0.58 | 0.16 | 0.16 | 0.31 | 0.04 | 1.07 | 0.58 | 0.27 | 4.30 | 0.04 | 2.09 | 0.02 | 2.07 | 0.03 | 11.99 | 46.83 | 58.82 |
| 2,000 | 0.003 | 0.27 | 0.58 | 0.16 | 0.16 | 0.31 | 0.04 | 1.23 | 0.58 | 0.27 | 4.30 | 0.04 | 2.09 | 0.02 | 2.07 | 0.03 | 12.17 | 45.37 | 57.54 |
| 2,048 | 0.003 | 0.27 | 0.58 | 0.16 | 0.16 | 0.31 | 0.04 | 1.33 | 0.58 | 0.27 | 4.30 | 0.04 | 2.09 | 0.02 | 2.07 | 0.03 | 12.26 | 46.23 | 58.49 |
| Attention block | Feed-forward block | Output | |||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Token | Embedding | Norm | Q proj | K proj | V proj | RoPE | KV write | Attention | O proj | Norm | Gate + Up | SiLU × mul | Down | Final norm | LM head | Sampling | GPU busy | Waiting on Python | Total |
| 1 | 0.004 | 0.37 | 0.63 | 0.25 | 0.26 | 0.39 | — | 0.48 | 0.64 | 0.35 | 4.73 | 0.08 | 2.20 | 0.02 | 2.07 | 0.03 | 12.60 | 49.77 | 62.37 |
| 250 | 0.01 | 0.62 | 0.89 | 0.23 | 0.23 | 0.63 | — | 2.27 | 0.87 | 0.56 | 7.15 | 0.32 | 3.22 | 0.04 | 2.09 | 0.03 | 19.40 | 40.59 | 59.99 |
| 500 | 0.03 | 1.11 | 1.71 | 0.49 | 0.51 | 1.23 | — | 15.37 | 1.71 | 1.15 | 10.43 | 1.01 | 5.35 | 0.06 | 2.08 | 0.04 | 42.70 | 15.28 | 57.98 |
| 750 | 0.04 | 1.37 | 1.67 | 0.49 | 0.50 | 1.53 | — | 46.15 | 1.72 | 1.35 | 13.25 | 2.11 | 7.17 | 0.09 | 2.08 | 0.04 | 80.10 | 4.47 | 84.57 |
| 1,000 | 0.04 | 1.73 | 2.33 | 0.88 | 0.87 | 1.94 | — | 102.48 | 2.34 | 1.69 | 16.55 | 3.34 | 8.74 | 0.11 | 2.08 | 0.04 | 145.70 | 4.85 | 150.55 |
| 1,250 | 0.06 | 2.13 | 3.26 | 0.96 | 0.92 | 2.33 | — | 150.39 | 3.29 | 2.17 | 23.09 | 4.91 | 11.82 | 0.13 | 2.08 | 0.04 | 208.30 | 5.32 | 213.62 |
| 1,500 | 0.07 | 2.58 | 3.58 | 0.97 | 0.96 | 2.82 | — | 204.29 | 3.61 | 2.61 | 28.67 | 7.13 | 13.40 | 0.16 | 2.08 | 0.04 | 273.80 | 7.36 | 281.16 |
| 1,750 | 0.07 | 2.97 | 4.22 | 1.14 | 1.10 | 3.29 | — | 260.22 | 4.27 | 3.10 | 31.48 | 8.15 | 15.74 | 0.19 | 2.08 | 0.04 | 339.00 | 6.22 | 345.22 |
| 2,000 | 0.08 | 3.50 | 4.32 | 1.56 | 1.58 | 3.87 | — | 324.67 | 4.33 | 3.67 | 31.75 | 9.83 | 17.04 | 0.23 | 2.09 | 0.05 | 409.70 | 6.57 | 416.27 |
| 2,048 | 0.08 | 3.37 | 4.48 | 1.51 | 1.53 | 3.79 | — | 312.96 | 4.38 | 3.72 | 33.72 | 10.62 | 17.76 | 0.22 | 2.10 | 0.04 | 401.60 | 6.18 | 407.78 |
By the last token, attention is 235× cheaper and the feed-forward block about 10×, because each step now pushes one token through the model instead of the whole sequence. "Waiting on Python" is the one column that gets worse: it was hidden behind the GPU's busy work before, and now it's exposed. The no-cache numbers are the same run as Part 1.
Python is the bottleneck now. Fusing many small operations into fewer, bigger GPU kernels, and writing my first CUDA kernel. Read Part 3 →