Sub topic 2
The ONNX Format and Why It Is the Common Interchange Point
Assume you know basic Python and NumPy, have used a framework such as PyTorch, and understand that an image model takes tensors as input and produces tensors as output. This chapter focuses on the model representation used between training code and optimized inference runtimes.
What ONNX represents
ONNX, or Open Neural Network Exchange, is a file format for describing machine-learning computation graphs. A computation graph represents a model as connected operations, such as convolution, matrix multiplication, activation, and reshaping.
An ONNX model usually contains:
- Nodes: operations in the graph.
- Tensors: multidimensional arrays flowing between operations.
- Initializers: stored tensors such as learned weights and biases.
- Inputs and outputs: the public interface of the model.
- Operatorset imports: the versions that define how operations behave.
- Metadata: optional information such as producer and model version.
The file normally uses the .onnx extension and stores a serialized protobuf message. A runtime reads this description and chooses how to execute each operation.
Why ONNX is useful
Training frameworks and inference engines have different priorities. PyTorch provides flexible model authoring and automatic differentiation. ONNX provides a mostly framework-independent description of the forward pass. ONNX Runtime and TensorRT can then optimize that description for specific hardware.
This separation lets you change the serving runtime without rewriting the model in the training framework. It also makes graph transformations possible. A runtime may fold constants, remove unused nodes, combine adjacent operations, or select specialized GPU kernels.
ONNX is an interchange point, not a guarantee of universal compatibility. An operation must be supported by the target runtime, and its behavior must match the exported model. Custom layers, unusual control flow, unsupported data types, and dynamic dimensions can require extra configuration or replacement operations.
A small ONNX graph
The following script creates and validates a graph that adds a constant bias to a three-element input:
import onnx
from onnx import TensorProto, helper
x = helper.make_tensor_value_info("x", TensorProto.FLOAT, [3])
y = helper.make_tensor_value_info("y", TensorProto.FLOAT, [3])
bias = helper.make_tensor(
"bias",
TensorProto.FLOAT,
[3],
[1.0, 2.0, 3.0],
)
add_node = helper.make_node("Add", ["x", "bias"], ["y"])
graph = helper.make_graph(
[add_node],
"add_graph",
[x],
[y],
[bias],
)
model = helper.make_model(
graph,
opset_imports=[helper.make_opsetid("", 17)],
)
onnx.checker.check_model(model)
onnx.save(model, "add.onnx")
print("Saved a valid ONNX model")The operatorset version matters because operation definitions can change. Pin it during export and test the resulting file with the exact runtime versions used in deployment.
Exercise
Why does ONNX improve portability without guaranteeing that every runtime can execute every model?
Show answer
ONNX standardizes the graph and operator definitions, but runtimes may support different operators, data types, dynamic shapes, and custom extensions.