Inference from scratch · Part 1

Start with the simplest version that works.

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.

5.8 tok/s
average generation speed
62 → 408 ms
first token vs. last token
5.9 min
to generate 2,048 tokens

Every token is slower than the last

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.

0100200300400ms6212505007501511k1.25k1.5k1.75k2k4082,048
Early on, the GPU is bored. For the first ~500 tokens it stays flat at ~60 ms. Up to 80% of that is the GPU waiting for Python to hand it the next tiny operation. What does that mean?
Then attention takes over. By token 2,000, attention alone is ~78% of each step, and the GPU is busy 98% of the time.

How busy is the GPU?

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.

0%50%100%20%132%25074%5007501k1.25k1.5k1.75k2k98%2,048
What does "waiting on Python" mean?

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".

CPU GPU send op 1 send op 2 send op 3 idle idle

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.

Inside one token

Pick a token to see where its time went, as a share of that token's total.

Attention
1%
Feed-forward (MLP)
11%
Q/K/V/O projections
3%
Norms + RoPE
2%
Output layer
3%
Waiting on Python
80%
Total for this token62.4 ms

Every operation, every token

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 blockFeed-forward blockOutput
TokenEmbeddingNormQ projK projV projRoPEAttentionO projNormGate + UpSiLU × mulDownFinal normLM headSamplingGPU busyWaiting on PythonTotal
10.0040.370.630.250.260.390.480.640.354.730.082.200.022.070.0312.5749.8062.37
2500.010.620.890.230.230.632.270.870.567.150.323.220.042.090.0319.3640.6359.99
5000.031.111.710.490.511.2315.371.711.1510.431.015.350.062.080.0442.6615.3257.98
7500.041.371.670.490.501.5346.151.721.3513.252.117.170.092.080.0480.114.4684.57
1,0000.041.732.330.880.871.94102.482.341.6916.553.348.740.112.080.04145.744.81150.55
1,2500.062.133.260.960.922.33150.393.292.1723.094.9111.820.132.080.04208.305.32213.62
1,5000.072.583.580.970.962.82204.293.612.6128.677.1313.400.162.080.04273.787.38281.16
1,7500.072.974.221.141.103.29260.224.273.1031.488.1515.740.192.080.04338.986.24345.22
2,0000.083.504.321.561.583.87324.674.333.6731.759.8317.040.232.090.05409.746.53416.27
2,0480.083.374.481.511.533.79312.964.383.7233.7210.6217.760.222.100.04401.586.20407.78
Questions or ideas? Join the discussion on LinkedIn.