§ ONNX Runtime for Engineers
9 lessons · every lesson has a Colab notebook

What is ONNX Runtime?

Nine lessons on running trained models with ONNX Runtime. Each lesson links to a Colab notebook for hands-on practice. The core exercises use a CPU; GPU work is optional.

Before you begin

You should be comfortable with Python functions and arrays. Inference means using a trained model to calculate an answer. A graph describes the operations used in that calculation. An operator is one kind of operation, such as addition or matrix multiplication.

Read the plain-language introduction, try its question, then open the notebook. Keep your measured results separate from the illustrative simulations on this page.

Technical references: ONNX Runtime Python API and quantization guidance.

How to use this guide

Read a lesson, then press its Run in Colab button. The notebooks install their dependencies and contain CPU exercises. Setup and run times vary. The interactive diagrams illustrate ideas; use notebook measurements to evaluate performance on your chosen hardware.

The one-sentence version

ONNX Runtime (ORT) is a cross-platform inference engine that executes ONNX graphs — it is a runtime, not a framework. If PyTorch and TensorFlow are languages plus compilers, then ONNX is the bytecode and ORT is the VM that runs that bytecode on CPUs, GPUs, NPUs, phones, and browsers.

Definition

ONNX (Open Neural Network Exchange) is a file format: a typed, directed acyclic graph of operators plus their weights, defined as a protobuf schema and an operator specification with versioned opsets.
ONNX Runtime is an engine that loads that file, optimizes the graph, partitions it across hardware backends, and executes it — with bindings for C++, C#, Java, Python, JavaScript/WASM, Rust, and mobile.

Two things follow from that split, and they explain almost everything else in this guide:

  1. The artifact is the contract. Training frameworks stop at the file boundary. Deployment starts there. You can train in PyTorch, export once, and execute the same bytes in a C++ service, a browser tab, and an Android app.
  2. The runtime owns everything after the file. Optimization, kernel selection, memory planning, device placement — all deployment-time concerns, all invisible until they slow you down. This guide makes them visible.

01 · What ONNX Runtime Actually Is

Introduction

A saved model does not run itself. Given a floating-point input with shape one by four, an ONNX Runtime session validates the named tensor, executes graph nodes, and returns a named output.

Learning goal

Explain the complete runtime session lifecycle and separate inference responsibilities clearly from framework training responsibilities.

Before you start

Python functions, arrays and shapes, and a basic idea of training versus inference.

Lesson plan

  1. Follow one request through session creation, input validation, execution, and output.
  2. Inspect a minimal session call and identify names, shapes, and data types.
  3. Compare what a training framework owns with what an inference runtime owns.

Trace one request. A client sends a float tensor shaped (1,4). An inference session validates its name and shape, runs the stored graph on available hardware, and returns the named output tensor. The runtime executes learned computation; it does not train the model.

A session, not a process

You do not "run" a model in ORT. You create an InferenceSession once, and call it many times. Session creation is where the runtime does its expensive work:

  1. Parse and validate the graph (structure, opset, types)
  2. Optimize it — fusion, constant folding, layout transforms (lesson 04)
  3. Partition and assign kernels across Execution Providers with CPU fallback (lesson 05)
  4. Plan memory for activations, keyed by input shape (lesson 06)

Per inference call, what remains is copying inputs, launching kernels, and copying outputs. Everything else is amortized. This is why "startup was fast, throughput is fine" and "throughput is fine, latency has overhead" are different problems with different fixes.

Example · a model with no framework

The notebook builds a linear layer with onnx.helper — literally two nodes, MatMul and Add — and executes it. No PyTorch, no autograd, no Python classes at run time. This is the smallest possible demonstration that ONNX + ORT is a complete execution path on its own.

import onnxruntime as ort

sess = ort.InferenceSession("tiny_linear.onnx", providers=["CPUExecutionProvider"])
y = sess.run(None, {"X": x})[0]        # None = fetch every output
▶ Run notebook 01 in Colab 01-what-onnx-runtime-is.ipynb CPU runtime · ~20 seconds

Framework vs. runtime: who owns what

ConcernPyTorch / TF (train)ONNX Runtime (deploy)
Autograd, optimizersYesTraining exists but is niche — inference is the point
Graph optimizationCompile-time for the frameworkLoad-time for the target hardware
Kernel selectionFixed kernels per device backendChooses among CPU / CUDA / TRT / NPU per subgraph
Runtime dependenciesHeavy (Python, CUDA stack)Small native library; Python optional
ArtifactPickle + codeOne .onnx file, versioned by opset