Sub topic 1
What “fast” means: latency versus throughput
Prerequisites
Assume you know basic Python and NumPy, have used a deep learning framework such as PyTorch, and understand that an image model accepts tensors and produces predictions. You should also know that a GPU can process many examples in parallel.
Inference speed has two different measurements:
- Latency is the time needed to complete one request.
- Throughput is the number of requests completed during a unit of time.
A model can have low latency but modest throughput. A model can also have higher latency while processing many requests efficiently in a batch.
Latency
Latency matters when a user or another service waits for each prediction. For example, a camera application may need a result every 30 milliseconds to feel responsive.
Measure latency from the point where the model receives its input to the point where its output is ready. Decide whether this includes preprocessing, memory transfers, and postprocessing. These steps can dominate the model's own execution time.
Common latency values include:
- p50, the median request time. Half of requests finish faster.
- p95, the time that 95 percent of requests finish within.
- p99, the time that 99 percent of requests finish within.
Tail latency matters for interactive systems. An average of 10 milliseconds does not help if one request in every 100 takes 200 milliseconds.
Throughput
Throughput matters when a service processes a queue of images, videos, or requests. It is often measured in images per second.
Batching usually increases throughput. A batch is a group of inputs processed by one model call. The GPU can reuse work across the batch, so eight images may take less than eight separate calls.
Batching usually increases latency for an individual request because a request may wait for other requests to join its batch. It also requires more memory. Choose a batch size that meets both the service's latency target and its throughput target.
| Workload | Main goal | Typical choice |
|---|---|---|
| Interactive camera or API | Low p95 or p99 latency | Small, fixed batches |
| Offline image processing | High images per second | Larger batches |
| Mixed traffic | Both latency and throughput | Bounded dynamic batching |
Measure the target you actually need
Do not compare a single model call with a production service and call the result a benchmark. Fix the input shape, batch size, hardware, and preprocessing path. Warm up the runtime first because the initial calls may include memory allocation, kernel selection, or TensorRT engine construction.
Then record many measurements. Report p50, p95, and p99 latency, plus throughput. Test realistic batch sizes and input shapes. A faster batch-32 benchmark does not prove that batch-1 requests are faster.
Exercise
A model processes a batch of 16 images in 80 ms. What is its approximate throughput, and why might this not describe one user's latency?
Show answer
Its throughput is 16 ÷ 0.08 = 200 images per second. One user's request may wait for the batch to fill, so its latency can be much higher than 80 ms.