← Journal
Run ONNX Runtime without tying the model to its execution environment

Run ONNX Runtime without tying the model to its execution environment

A record of running ONNX Runtime, an inference engine independent of training frameworks, with the network disconnected. A minimal model built without training code ran inference as-is.

Repository covered in this article

microsoft/onnxruntime

ONNX Runtime: cross-platform, high performance ML inferencing and training accelerator

21.1k C++ MIT License

The environment where you train a model may not use the same framework or version as the production environment where you run it. ONNX Runtime is an inference engine that runs models represented in ONNX format regardless of the framework used for training. I installed it and tested inference with the network disconnected.

ONNX Runtime loads models in ONNX (Open Neural Network Exchange) format and runs inference while optimizing for the execution environment, such as a CPU or GPU. Microsoft publishes the source under the MIT License (*1).

1. Install ONNX Runtime

FROM docker.io/library/python:3.12.13-slim
RUN pip install --no-cache-dir --quiet 'onnxruntime==1.27.0' 'onnx==1.22.0' 'numpy==2.5.1'
WORKDIR /work
podman build -t onnxruntime-sandbox .

2. Test inference without a trained model

I built a minimal model with the graph-construction API from the onnx package, without using a training framework, then ran it with ONNX Runtime. The model contains a matrix multiplication and an addition, the equivalent of a single dense layer. I ran it with the network disconnected.

import numpy as np
import onnx
import onnxruntime as ort
from onnx import TensorProto, helper

W = np.random.default_rng(0).normal(size=(8, 4)).astype(np.float32)
b = np.random.default_rng(1).normal(size=(4,)).astype(np.float32)

W_init = helper.make_tensor("W", TensorProto.FLOAT, W.shape, W.flatten().tolist())
b_init = helper.make_tensor("b", TensorProto.FLOAT, b.shape, b.flatten().tolist())
x = helper.make_tensor_value_info("x", TensorProto.FLOAT, [1, 8])
y = helper.make_tensor_value_info("y", TensorProto.FLOAT, [1, 4])

matmul_node = helper.make_node("MatMul", ["x", "W"], ["xw"])
add_node = helper.make_node("Add", ["xw", "b"], ["y"])
graph = helper.make_graph([matmul_node, add_node], "tiny_linear", [x], [y], initializer=[W_init, b_init])
model = helper.make_model(graph, opset_imports=[helper.make_opsetid("", 18)])
onnx.checker.check_model(model)
onnx.save(model, "/work/tiny_linear.onnx")

session = ort.InferenceSession("/work/tiny_linear.onnx", providers=["CPUExecutionProvider"])
input_data = np.array([[1, 2, 3, 4, 5, 6, 7, 8]], dtype=np.float32)
result = session.run(None, {"x": input_data})
print("output:", result[0].tolist())
podman run --rm --network none --memory 2g --cpus 2 --pids-limit 512 \
  -v "$(pwd)":/work -w /work onnxruntime-sandbox \
  python inference_demo.py

The -v option makes the specified directory available for reads and writes inside the container. Run the command from an empty directory dedicated to this experiment.

The command produced this output.

providers: ['CPUExecutionProvider']
input: [[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]]
output: [[-12.8413724899292, 5.778198719024658, -18.489063262939453, -2.9130420684814453]]
inference time: 3.694 ms

I installed no training framework, including PyTorch and TensorFlow. I built the model with ONNX's standard graph representation, and ONNX Runtime ran it as-is. The run used CPUExecutionProvider, and ONNX Runtime completed inference in 3.694 ms.

Separate model execution from the training environment

The longer you operate a system, the more work it takes to keep the training and execution environments identical. Once you convert a model to the common ONNX format, ONNX Runtime alone can run inference. This setup separates the constraints on the training side from those on the execution side.

Sources

*1 ONNX Runtime (GitHub), https://github.com/microsoft/onnxruntime, MIT License