A from-scratch Llama 3.2 1B on one NVIDIA L4 GPU. No KV cache, no fused kernels, no tricks. I built it, plus a profiler on top, mostly by chatting with Antigravity. Here's where the time goes.
Time to generate one token, sampled every 250 steps. Without a KV cache, the model recomputes attention over the whole sequence at every step, so it keeps getting slower.
Share of each token's time the GPU was actually running work. The rest of the time it sat idle, waiting for Python to send the next operation.
The GPU can't decide what to do next. The CPU (Python, plus PyTorch underneath) walks through the model one operation at a time and hands each one to the GPU: "now multiply this", "now normalize that".
Each hand-off takes the CPU a few microseconds. Early on, the GPU's jobs are so small it finishes them faster than the CPU can send the next one, so it sits idle in between. Those gaps are what this page calls "waiting on Python".
Busy isn't automatically good. Here the GPU is busy because it keeps redoing work it has already done. Remove that work with a KV cache (Part 2) and each token gets fast again, but now Python can't keep up, and the GPU is back to waiting.
Pick a token to see where its time went, as a share of that token's total.
The full profile. Milliseconds per token, with each operation summed across all 16 transformer layers, in the order they run. Attention grows fastest. Feed-forward grows too, because without a KV cache every past token runs through the whole model again on every step. Only the LM head stays flat: it only ever looks at the newest token.
| Attention block | Feed-forward block | Output | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Token | Embedding | Norm | Q proj | K proj | V proj | RoPE | 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.57 | 49.80 | 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.36 | 40.63 | 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.66 | 15.32 | 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.11 | 4.46 | 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.74 | 4.81 | 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.78 | 7.38 | 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 | 338.98 | 6.24 | 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.74 | 6.53 | 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.58 | 6.20 | 407.78 |
The KV cache. Remember the past instead of recomputing it, and that orange wedge stops growing. Read Part 2 →
Questions or ideas? Join the discussion on LinkedIn.