DEV Community

Cover image for ONNX Explained: The Standard Format for Machine Learning Models
Rijul Rajesh
Rijul Rajesh

Posted on

ONNX Explained: The Standard Format for Machine Learning Models

Hello, I'm Rijul. I'm building git-lrc, a micro AI code reviewer that runs on every commit. It's free and source-available on GitHub. Star git-lrc to help more developers discover the project. Do give it a try and share your feedback

Did you know there is an open standard for representing machine learning models in a common format?

This is called ONNX.

It provides a standard way to represent machine learning models so they can be used across different runtimes and environments.

Let's see what problem ONNX solves.

The Problem

Suppose you built a machine learning model using PyTorch.

You ran it locally and everything works.

Now you need to deploy it.

The environment where you deploy the model doesn't necessarily have PyTorch installed. You may also need to run the model on different hardware, such as a CPU, GPU, or mobile device.

In the past, this could mean making environment-specific changes or creating custom implementations.

ONNX provides a common representation for the model, making it easier to deploy the model across different runtimes and environments.


What Is ONNX?

ONNX stands for Open Neural Network Exchange.

It defines a standard way of representing machine learning models.

For example, imagine your model performs these operations:

  • Input
  • Matrix multiplication
  • Add bias
  • ReLU
  • Output

ONNX can represent this as a computational graph:

Input
  │
  ▼
MatMul
  │
  ▼
 Add
  │
  ▼
 Relu
  │
  ▼
Output
Enter fullscreen mode Exit fullscreen mode

The graph is made up of standardized ONNX operators that describe what the model does.

The resulting model can be serialized as a file such as:

model.onnx
Enter fullscreen mode Exit fullscreen mode

So, at its simplest, ONNX is a standard format for representing machine learning models.

But the ONNX file itself doesn't execute the model.

That's where ONNX Runtime comes in.


ONNX Runtime

ONNX Runtime can load an ONNX model and execute it.

It supports running models across different environments and hardware through its execution providers, including:

  • CPU
  • GPU
  • Mobile
  • Other supported hardware accelerators

So the basic flow looks like this:

PyTorch Model
     │
     ▼
Export to ONNX
     │
     ▼
 model.onnx
     │
     ▼
ONNX Runtime
     │
     ▼
Run the model
Enter fullscreen mode Exit fullscreen mode

Of course, ONNX doesn't automatically make every model runnable on every piece of hardware. The model's operators and the capabilities of the target runtime still need to be supported.

You can check out this Colab notebook, where we create an ONNX model from PyTorch and then use ONNX Runtime to run it.

Wrapping Up

So that's the basic idea behind ONNX and ONNX Runtime.

ONNX provides a common format for representing machine learning models, while ONNX Runtime provides a way to execute those models.

Together, they make it easier to take a model built in one environment and deploy it across different runtimes and supported hardware.

See you in the next article.

AI agents write code fast. They also silently remove logic, change behavior, and introduce bugs -- without telling you. You often find out in production.

git-lrc fixes this. It hooks into git commit and reviews every diff before it lands. 60-second setup. Completely free.

Any feedback or contributors are welcome! It's online, source-available, and ready for anyone to use.

Give it a ⭐ star on Github

Top comments (0)