Skip to content
Inference Academy

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

14 min · verified 2026-111 animated diagramPro
Share

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:

  1. Schedule: pick which sequences run, admit new ones, preempt if memory is short (scheduling).
  2. Prepare inputs: token IDs, positions, block tables for paged attention, sampling parameters, copied to the GPU.
  3. Run the model on the GPU.
  4. Sample: apply temperature, penalties, top-k and top-p, grammar masks for structured output, and pick tokens.
  5. Post-process: check stop conditions, detokenise new tokens into text, compute log-probabilities if asked.
  6. 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.

Where does a decode step's time go: GPU or CPU?
Interactive
CPU
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:

python
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")
text
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 ms

The 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:

python
# 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")
text
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 ms

Your 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

  1. How engines hide it
  2. Sampling is not free
  3. Tokenisation and the API server
  4. Spotting it in a profile
  5. What you can now do
  6. Common mistakes
  7. Exercise
  8. 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?