AI State of the Art
SOTA/Inference & Optimization/24 min
Section 127

ONNX Runtime: the portable compiler

Listen as a podcast →

The problem: every engine is married to some hardware

TensorRT-LLM runs only on NVIDIA graphics cards. llama.cpp targets the central processor first. Every inference engine is tied to a hardware family, and switching hardware means redoing the deployment work from scratch.

ONNX (Open Neural Network Exchange) attacks this with a move that is a classic in computing: instead of connecting every training framework to every piece of hardware, you invent a pivot format in the middle. Each framework learns to export to that one format; each execution engine learns to read it. Instead of N × M bridges to write, you need N + M.

Hourglass diagram titled Interoperability. On the left, the Caffe2, PyTorch, TensorFlow, Keras, MXNet and Microsoft CNTK logos, with arrows converging on a central ONNX logo, under the label export to onnx. On the right, arrows leave the ONNX logo towards ONNX Runtime, Caffe2, PyTorch, TensorFlow, Keras, MXNet and CNTK, under the label load from onnx.
The hourglass shape of a pivot format: everything converges on ONNX on the left, everything leaves it on the right. The bottleneck is deliberate, it is what saves you from writing one bridge per framework-hardware pair.Source : Medium article by Sergy R.

Two things are worth keeping apart, because they are routinely conflated:

  • ONNX is a file format. It describes the model as a graph of operations (this matrix multiplication, that addition, that activation function) with the weights attached. It is a description; it does not run.
  • ONNX Runtime is the engine that reads the file and runs it. That is where the interesting work happens.

ONNX Runtime is also a compiler, of a different kind

Like TensorRT-LLM (see TensorRT-LLM), ONNX Runtime transforms the graph before running it: it merges several operations into one, drops useless nodes, reorders computations. The difference is in when that transformation happens and how far it goes.

TensorRT-LLM compiles once, ahead of time, and produces a frozen program valid only for one specific card. ONNX Runtime does most of its work when the model is loaded, on every start, discovering the hardware actually present on the machine. It gives up the most aggressive optimizations (the ones that require knowing exact dimensions in advance) and in exchange gains the ability to run more or less anywhere.

Execution providers

The mechanism that makes this possible is called an execution provider: a module that knows how to run pieces of the graph on a given kind of hardware.

ProviderTarget hardwareWhat it uses underneath
CUDANVIDIA graphics cardsNVIDIA compute libraries
TensorRTNVIDIA graphics cardsTensorRT compilation, more aggressive
ROCmAMD graphics cardsAMD's counterpart to the NVIDIA libraries
DirectMLany graphics card on Windowsthe operating system's graphics interface, DirectX 12
OpenVINOIntel processors, cards and acceleratorsIntel libraries
CPUany central processorthe processor's vector instruction sets

Two acronyms in that last row deserve unpacking, because they explain why inference on a central processor became viable at all. AVX-512 (Advanced Vector Extensions) is an instruction set that applies one operation to sixteen numbers at a time instead of one. VNNI (Vector Neural Network Instructions) adds instructions dedicated to the single most frequent operation in neural networks: multiply two 8-bit integers and accumulate the result. A recent processor is not a graphics card, but it is no longer laughable.

Architecture diagram. At the top, an ONNX model box feeds an In-Memory Graph box, which feeds a Graph Partitioner. The Graph Partitioner also receives an arrow from a Provider Registry. It feeds a parallel and distributed Graph Runner, which distributes work to an Execution Provider band containing three groups: CPU Provider with MLAS by default, XNNPACK and DNN; GPU Provider with CUDA and TensorRT; Other Providers with NPU. Green Input tensors and Result boxes frame the whole. An inset at the bottom details the MLAS and KleidiAI kernels optimized for aarch64 CPUs.
What happens between the .onnx file and the result. The graph partitioner queries the registry of providers available on the machine, then cuts the graph into pieces: each piece goes to the provider that can run it fastest. That cut is what lets the same file run on an NVIDIA card, a processor or a dedicated accelerator, and mix all three when none of them can do everything.Source : Arm Learning Paths, ONNX Runtime architecture

The thing to take from that diagram: the split is partial. If a provider cannot run some operation in the graph, ONNX Runtime does not give up, it hands that operation to another provider, usually the central processor. The model still runs. That is robust, but it is also a performance trap: a single unsupported operator in the middle of the graph forces a round trip between card and processor on every pass, and can cost more than everything else combined.

NPUs, ARM, and why this is getting interesting

NPU stands for neural processing unit: a chip specialized in matrix multiplication, built for the past few years into laptop and phone processors. ARM is the processor architecture in phones and recent Macs. Neither runs code written for NVIDIA. A pivot format is the only sane way to target that whole set of devices without rewriting the deployment each time.

Converting a model to ONNX

The Optimum library, published by Hugging Face, bridges models on the Hugging Face Hub and ONNX. In practice one command-line call exports a model to the format:

optimum-cli export onnx --model mistralai/Mistral-7B-v0.1 ./mistral-onnx

The tool traces the model (it runs it once on dummy data to observe which operations get called and in what order) then writes the corresponding graph to a .onnx file. That tracing mechanism explains the main limitation of the export, described below.

What the tool does, in three steps

Optimum converts the graph, applies the hardware-independent graph optimizations along the way, and can also quantize the weights on request. It performs no miracles: it converts correctly only the architectures it already knows about.

Reducing precision with ONNX Runtime

How quantization works and what it costs in quality are covered in Quantization: the concepts. What is specific to ONNX Runtime is that there are two paths of very different difficulty.

Dynamic quantization. Weights are converted to 8-bit integers (one byte per weight, against two in FP16, so a file half the size) and the engine works out the scale of the intermediate values at run time, request by request. No preparation is needed: hand over the file, get a quantized file back. It is the simplest path and the least effective.

Static quantization. First a small, representative dataset is pushed through the model to measure the typical magnitude of intermediate values (the calibration step) then those scales are frozen into the file. You have to supply that dataset, which presupposes knowing what real inputs look like. In exchange the engine has nothing left to compute on the fly and the result is better.

Down to 4 bits. ONNX Runtime supports 4-bit weights (half a byte per weight) through a dedicated operator named MatMulNBits, which unpacks the weights at compute time. It can read models produced by the methods described in AWQ and GPTQ.

Where ONNX Runtime is the right call

The same model on heterogeneous hardware. One file deployed on a server with an NVIDIA card, on an Intel workstation, on an ARM phone. This is the use case the format exists for.

Embedded applications rather than servers. When the model is a component inside software installed on the user's machine, rather than a service behind a network interface, ONNX Runtime integrates more simply than a serving engine: it is a library the application calls, not a server to administer.

Windows and DirectML. On Windows, the DirectML provider goes through DirectX 12, the operating system's graphics interface, and therefore works on any graphics card (NVIDIA, AMD or Intel) with no vendor-specific code. Microsoft presents this stack as the one serving its own AI features built into Windows.

Diagram titled ONNX Runtime. At the top, an Application frame containing an ONNX logo and binary digits, linked by an arrow to a central ONNX Runtime block. That block carries a row of labels CPU-EPs, GPU-EPs, NPU-EPs, ORT Web and Azure-EP. Arrows go down to two frames: Device, containing CPU, GPU and NPU icons, and Cloud, containing Azure logos.
The diagram Microsoft published when announcing AI in Windows 11: one application, one ONNX model, and the engine deciding whether to run on the machine's processor, graphics card or neural accelerator, or to send the computation to the cloud. Same execution-provider logic as the previous diagram, seen from the product side.Source : Windows Developer Blog (Microsoft)

The limits, and they are structural

What resists export

The ONNX format describes a fixed graph of operations. But export works by tracing: you observe one execution and infer the graph. Anything in the model code that depends on the data at run time (a loop whose number of iterations varies, a conditional branch, a hand-written operation with no counterpart in the ONNX catalogue) traces badly or not at all. Recent or unconventional architectures are therefore the hardest to convert, and this is a permanent condition: the format runs behind the research.

The lag on text-serving features

ONNX Runtime is a general-purpose engine: it runs a vision model, a classifier or a language model equally well. Engines specialized in text generation (vLLM, TensorRT-LLM) have only one model family to serve, and implement the tricks specific to that case first: paged attention cache (see PagedAttention), continuous batching (see Continuous batching), speculative decoding (see Speculative decoding).

This is a scheduling gap rather than an impossibility: the ONNX Runtime GenAI extension is progressively bringing key-value cache management, token sampling strategies and batching to the format. But the invariant holds, and it is worth remembering in place of any current feature list: a new language-model serving technique appears first in a specialized engine, then reaches the general-purpose one.

Do not look for a comparison number here

Measurements of the form "engine X is twice as fast as engine Y" are everywhere. They mean nothing outside their conditions: card model, model size, sequence lengths, how many requests are processed together, what precision, and above all against which baseline. The same speedup claim can be measured against an already-optimized system or against a naive loop, and the two numbers are not comparable. There is no reliable, durable table of speeds in this field; there are only measurements taken on your own deployment.

The real selection criterion

What durably separates these tools is not their speed at some point in time, it is what they are tied to.

What the result is tied toWhat you get
TensorRT-LLMone NVIDIA card model, one version, one frozen configurationmaximum speed on that hardware
vLLMthe graphics-card ecosystem, flexible at run timethe most advanced text-serving features
ONNX Runtimenothing in particular: the file is portabledeployment on heterogeneous hardware

The detailed comparison of serving engines is in Serving frameworks; the file formats themselves are covered in Model conversions.

What to take away

ONNX is a pivot format; ONNX Runtime is the engine that reads it and optimizes it for whatever hardware is present. It is the exact opposite of TensorRT-LLM's bet: where that one sacrifices portability for speed, ONNX Runtime sacrifices some speed to depend on no vendor at all. The choice does not turn on a performance number but on a simple question: is the target hardware known and stable, or do you need to be able to change it?

Check your understanding0 / 5

1. What is the difference between ONNX and ONNX Runtime?

2. Why does a pivot format like ONNX simplify multi-hardware deployment?

3. A single operator in the graph is not supported by the GPU provider. What does ONNX Runtime do, and with what consequence?

4. Why are recent or unconventional architectures the hardest to export to ONNX?

5. Dynamic versus static quantization with ONNX Runtime: what does switching to static change?