From loading to execution

Taking ONNX Runtime as an example, the flow is as follows. First, the model file is loaded and converted into a graph in memory. Next, graph optimizations that do not depend on hardware are applied. The graph is then divided into parts and assigned according to the hardware available. Finally, each part is executed by kernels for the device it was assigned to. A kernel here is an optimized program that carries out one operation, such as a matrix product.

Why it is faster than a training framework

A training framework gives priority to flexible coding and to the bookkeeping needed for backpropagation. In inference the weights are fixed, so parts determined by constants alone can be computed in advance. Operations that matter only during training, such as dropout, can be removed. Consecutive operations, such as a convolution and the addition after it, are merged into one. Where needed, the runtime executes a model whose weights and activations have been quantized to lower precision. Measures like these reduce latency and memory.

Using different hardware

In ONNX Runtime, the execution components for each kind of device are specified in order of priority. Each node is assigned to the highest-priority component that can handle it. An operation that none of them supports is executed on the CPU. One model may therefore run across both a GPU and the CPU. Saving the model after optimization avoids repeating the work at every start-up. The saved model is tailored to the target device, however, so it may not be usable on a different one.

Try it: questions about Inference runtime

Why can inference runtimes such as TensorRT or ONNX Runtime run faster than using the training framework directly for inference?

  1. AThey optimize the graph for inference only, applying fusion, quantization and hardware-specific kernel selection
  2. BThey skip computation and give approximate answers
  3. CThey increase the number of model weights
Show answer and explanation

Answer: A. They optimize the graph for inference only, applying fusion, quantization and hardware-specific kernel selection

Training frameworks prioritize flexibility and recording for backpropagation, which is wasteful for inference alone. After loading a model, an inference runtime strips parts unneeded for inference, fuses operations, converts weights and activations to lower precision where appropriate and selects the fastest kernels for the target GPU or NPU. This greatly reduces latency and memory for the same model.

FAQ

Is it wrong to run inference directly in the framework used for training?

No, inference can be run that way. The machinery for training remains, however, so some waste is likely. Where latency or memory matters, moving to an inference runtime is the common practice.

Does using an inference runtime change the output?

Most graph optimizations are rewrites that preserve meaning, so the result is basically the same. Because the order of computation changes, small differences from floating-point rounding can appear. When quantization is used, accuracy may be affected.

Books on AI Hardware & Software Stack Ad

As an Amazon Associate I earn from qualifying purchases.

This page was drafted with AI and fact-checked by a separate AI review before publication. Our Editorial policy explains how we write and check content. If you spot an error, please tell us on our Contact page.

Back to the glossary