TECHNOLOGY

Read PyTorch benchmark block times and per-call measurements correctly

In this article
THE QUICK TAKE

Keep number_per_run beside raw block times, and compare normalized calls only when setup, mutable state and execution controls match.

A microbenchmark export shows a larger raw duration after an optimization, but its displayed per-call result is smaller. Before declaring a contradiction, inspect how many statement executions each timed block contains. PyTorch benchmark utilities adjust repetition to reduce timer overhead; a block duration and an individual-call estimate are different quantities.

Preserve the repetition count

Keep number_per_run with raw_times and the task definition. A raw time covers a block of repeated statements. The Measurement interface supports normalized summaries, while merging measurements normalizes repetition rather than assuming every raw duration describes one call. Comparing blocks with different repetition counts can reverse the apparent ordering even when each normalized measurement is internally consistent.

Understand automatic blocking

The blocked timing method increases block size during estimation to make measurement overhead sufficiently small, then collects repeated blocks. An optimized statement may receive more repetitions in each block. That is a measurement strategy, not evidence that production requests were automatically batched. Preserve the method and runtime target so later analysis does not reconstruct an unsupported request-throughput claim from the export.

Check setup and mutable state

Python timeit separates setup from the timed statement. Repetition does not imply that every call receives a fresh tensor, model or allocator state. An in-place operation can progressively change its input; a cache can become populated during a block. Decide whether those semantics represent the intended operation, then construct initialization and reset boundaries accordingly. Do not divide away a changing workload and call it an equivalent sample.

Retain execution controls

PyTorch’s Timer provides relevant warmup, thread control and accelerator synchronization behavior. Record the selected thread setting and applicable device support rather than transferring assumptions from a basic Python timer. Setup outside timing can still influence residency and cache state inside timing. Validate representative outputs separately, since a neatly normalized number does not establish that the optimized statement performs the same useful computation.

Interpret the normalized result

Compare per-call estimates for matched statements and state conditions, while retaining block-level variability. Repeated executions inside a block are not automatically independent service requests. A microbenchmark can isolate an operator regression, but it does not reproduce arrival queues, request cancellation or end-to-end token delivery. Keep that boundary visible when using the result to explain a serving change.

Primary references

PyTorch Benchmark Timer and Measurement.

PyTorch Benchmark Recipe.

Python timeit Setup and Repetition Semantics.

Related reading: A GPU inference benchmark you can trust; GPU benchmark confidence intervals: what counts as an observation.

Earn rental income from GPUs

Have idle GPUs or looking to fund AI infrastructure? Retrivium finds a rental platform, sets up the server and helps you assess income and costs with a pilot.

Explore GPU hosting
← All articles