Skip to main content
Quantization is a trade-off. Before shipping, measure on your model and your deployment hardware:
  1. Latency: is inference actually faster?
  2. Size: did the file shrink?
benchmark() runs locally, on your own machine, not on the platform. That is deliberate: latency numbers only mean something on the hardware you deploy to. Local benchmarks are free and do not consume plan quota.
benchmark() measures speed and size only. Even on real inputs it never checks whether the quantized model is still correct. See Accuracy is a separate job.

Built-in benchmark

Point the benchmark at two ONNX files and the data you want them timed on. It cycles through your eval_data across the timed runs, times both models on whichever ONNX Runtime provider is available on your machine, and reports the hardware it ran on.
quantized_model is the path to a .onnx file. If you unpacked a downloaded bundle, that is <bundle>/model.quantized.onnx — see the manifest for what else the bundle contains.

Notes on the input

  • Pass an iterable of samples: NumPy arrays, PyTorch tensors, or anything np.asarray can turn into a float32 array.
  • Every sample must match the shape the model expects. Batch size comes from your data: to time batch 32, pass samples of shape (32, 3, 640, 640).
  • A 3-D sample (C, H, W) gets a leading batch axis added, so (3, 224, 224) is fed as (1, 3, 224, 224).
You can reuse the same preprocessing function you used for calibrating the model. If no data samples are available and the network makes the input go through a fixed number of layers, latency benchmarking is easier with synthetic data - more on this in the next section.

Synthetic input

Without data at hand, set use_synthetic=True and omit eval_data. The benchmark then reads the input shape from the model itself and synthesizes random inputs.
If any axis is symbolic (batch, sequence, …), that quantity is set to 1. Use input_shape when the inferred shape is not the one you deploy at.

Terminal output

With verbose=True (the default) the benchmark prints each run’s latency and tracks the running average:

The output is a BenchmarkResult

Benchmark on hardware that matches your deployment target.

Accuracy is a separate job

The benchmark method does not report accuracy. To sign off on a quantized model, please run your task metric on a held-out labeled set, with the same preprocessing step.

Next steps

Calibration

Build a representative image set for ptq_static.

Quantization methods

Pick the right PTQ method or QAT.