liveBook · Manning

ONNX

As explained in Deep Learning with PyTorch, Second Edition

A vendor-neutral standard and file format for representing neural network computations so models can be executed in different runtimes and devices.

ONNX, or Open Neural Network Exchange, is a vendor-neutral format for describing neural-network and machine-learning computations. It lets a model move beyond its original framework and run with optimized processors, specialized hardware, cloud services, or applications that do not use Python. section 17.0 section 1.4.1

A deep-learning model can be treated as a program built from operations such as matrix multiplication, convolution, ReLU, and tanh. ONNX serializes those operations and their parameters so an ONNX-compatible runtime can re-execute them. section 17.2.1

A basic ONNX deployment workflow is:

  • Run the model with a dummy input whose shape and type match the expected input; the values themselves are not important. section 17.2.1
  • Call torch.onnx.export to trace the model’s computations and write an ONNX file. section 17.2.1
  • Load the file with a compatible runtime such as ONNX Runtime and execute it with NumPy input data. section 17.2.1
  • Check that the model’s operations are supported by both the ONNX standard and the target runtime. section 17.2.1
onnx_model_path = "simple_model.onnx"
dummy_input = torch.randn(1, 10)
torch.onnx.export(       #1
    model,
    dummy_input,
    onnx_model_path
)

This short export listing shows the required model, correctly shaped dummy input, and output path. section 17.2.1

import onnxruntime
sess = onnxruntime.InferenceSession("simple_model.onnx")   #1
batch = dummy_input.numpy()
input_name = sess.get_inputs()[0].name
pred_onnx, = sess.run(None, {input_name: batch})

This inference listing loads the exported file with ONNX Runtime, discovers its input name, and runs a prediction. section 17.2.1

How Deep Learning with PyTorch, Second Edition covers it

How deep it goes
Explains operation serialization, export tracing, dummy inputs, and runtime execution, but stops short of detailed ONNX internals.
Examples
Uses a simple exported model, a random tensor with shape one-by-ten, an ONNX file, and an ONNX Runtime prediction.
What it assumes
Assumes a PyTorch model and an input tensor with known shape and type; teaches the export and inference steps directly.
What you can do after
Apply it via torch.onnx.export and ONNX Runtime to export a model and execute it outside PyTorch.
How it connects
Connects PyTorch, ONNX Runtime, model serialization, neural networks, cloud deployment, embedded hardware, and AI accelerators.

Also explained in

Compare how 7 books explain ONNX →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with PyTorch, Second Edition. The section links point to the parts of the book it draws on.