The host side: CPU overhead, tokenisation, sampling and streaming
Everything an inference server does on the CPU between GPU steps (scheduling, preparing inputs, sampling, detokenising and streaming), when it becomes the bottleneck (small models, fast GPUs, big batches), how engines hide it with asynchronous scheduling and GPU-side sampling, and how to spot it in a profile. With a runnable step model and measured Python costs.
By Sandip Maity, Applied AI Engineer
The GPU lessons treat a decode step as reading weights and cache from memory. Between those steps a server also does work on the CPU: deciding which sequences run, building the inputs, choosing the next token, turning tokens back into text and sending it to clients. On a big model that work hides in the shadow of a 40 ms step. On a small model on a fast GPU, it can be most of the step.
You need Python 3 and numpy.
#The loop around the GPU
Every decode step of an engine such as vLLM or SGLang does roughly this:
- Schedule: pick which sequences run, admit new ones, preempt if memory is short (scheduling).
- Prepare inputs: token IDs, positions, block tables for paged attention, sampling parameters, copied to the GPU.
- Run the model on the GPU.
- Sample: apply temperature, penalties, top-k and top-p, grammar masks for structured output, and pick tokens.
- Post-process: check stop conditions, detokenise new tokens into text, compute log-probabilities if asked.
- Stream: send each sequence's new text to its client.
Steps 1, 2, 5 and 6 are host work. Step 4 can be either, and in modern engines it runs on the GPU.
Step time
7.78 ms
GPU busy
61%
Bigger share
GPU
Four decode steps. Overlapped scheduling prepares the next step on the CPU while the GPU runs the current one.
#When the CPU becomes the bottleneck
Decode time on the GPU is roughly weight bytes divided by memory bandwidth (roofline). Assume the host adds 3 ms per step:
import json, time
import numpy as np
# 1. When does the CPU become the bottleneck? GPU step time from bandwidth, CPU time per step as an assumption.
def gpu_step_ms(params_b, bytes_per_param=2, hbm_tbs=3.35):
return params_b * 1e9 * bytes_per_param / (hbm_tbs * 1e12) * 1e3
CPU_MS = 3.0 # scheduling, input preparation, sampling bookkeeping, detokenising and streaming, per step
print("1. Decode step time, CPU work run serially versus overlapped with the GPU")
for p in (1, 8, 70):
g = gpu_step_ms(p)
serial, overlap = g + CPU_MS, max(g, CPU_MS)
print(f" {p:>3}B model: GPU {g:5.2f} ms serial {serial:5.2f} ms ({g/serial:4.0%} GPU busy) overlapped {overlap:5.2f} ms")1. Decode step time, CPU work run serially versus overlapped with the GPU
1B model: GPU 0.60 ms serial 3.60 ms ( 17% GPU busy) overlapped 3.00 ms
8B model: GPU 4.78 ms serial 7.78 ms ( 61% GPU busy) overlapped 4.78 ms
70B model: GPU 41.79 ms serial 44.79 ms ( 93% GPU busy) overlapped 41.79 msThe same 3 ms is a 7% tax on a 70B model, 63% on an 8B model, and turns a 1B model into a CPU benchmark. Faster GPUs make it worse: halve the GPU time and the CPU share grows. Overlapping the two (next section) brings the 8B model back to its GPU speed. It cannot save the 1B model, where the CPU alone takes longer than the GPU; that needs less host work, not just overlap.
How much host work is there? Here is a plain-Python sketch of building block tables, packing them for the GPU and formatting one streaming event per sequence:
# 2. Measure some host work per decode step in plain Python
def host_step(batch, block_size=16, ctx=2000):
t0 = time.perf_counter()
tables = [list(range(i * 200, i * 200 + ctx // block_size)) for i in range(batch)] # block tables
positions = [ctx] * batch
flat = np.array([b for t in tables for b in t], dtype=np.int32) # pack for the GPU
toks = np.random.randint(0, 128000, size=batch)
chunks = [json.dumps({"choices": [{"delta": {"content": f"tok{t}"}}]}) for t in toks] # one SSE event per stream
return (time.perf_counter() - t0) * 1e3, flat.size + len(chunks) + len(positions)
print("\n2. Measured host work per step (this machine, plain Python and numpy)")
for batch in (8, 64, 256):
ms = min(host_step(batch)[0] for _ in range(20))
print(f" batch {batch:>3}: {ms:5.2f} ms")2. Measured host work per step (this machine, plain Python and numpy)
batch 8: 0.03 ms
batch 64: 0.24 ms
batch 256: 0.88 msYour numbers will differ. The pattern will not: host work grows linearly with batch size, while GPU decode time grows slowly with batch (weights are read once for everyone). So the bigger the batch you push for throughput, the larger the CPU's share. A real engine does much more than this sketch: scheduling policy, prefix-cache lookups, stop strings, detokenisation, metrics, and an HTTP server, often all in Python, on the same process.
Pro content
Keep reading with Inference Academy Pro
Unlock the rest of this lesson, plus every advanced lesson, hands-on lab, the larger playground models and completion certificates.
Still to come in this lesson
- How engines hide it
- Sampling is not free
- Tokenisation and the API server
- Spotting it in a profile
- What you can now do
- Common mistakes
- Exercise
- Going deeper
You will be able to answer
- In the lesson's model, a 1B model's decode step takes 0.6 ms on the GPU and the CPU adds 3 ms. How busy is the GPU if the two run one after the other?
- What does asynchronous (overlapped) scheduling do?
- Why is top-p sampling more expensive than plain temperature sampling?
- How do you recognise host overhead in an Nsight Systems timeline?
Preparing for an interview?
- vLLM, SGLang and llama.cpp internals interview questions (5 questions with model answers)
- Structured output interview questions (constrained decoding) (6 questions with model answers)