liveBook · Manning

ONNX

As explained in Domain-Specific Small Language Models

A serialized machine-learning model represented in the ONNX format for portable execution outside the training framework.

ONNX (Open Neural Network Exchange) is an open standard for representing machine-learning models across frameworks and hardware platforms. A model trained in one framework can be exported to ONNX for inference without making the training framework the only deployment option. The format describes the model; ONNX Runtime is a separate, optional way to execute it. section 5.1

An ONNX model is a computation graph built from operators such as matrix multiplication, addition, and neural-network layers. Its tensors have defined element types and shapes, and its graph is serialized using protobuf. The format supports common numerical types as well as specialized types such as bfloat16. This structure makes a model portable, but a valid export still needs an output comparison against the original model. section 5.2 section 5.7

A practical deployment path separates conversion, execution, and optimization:

  • Export a trained model with a supported tool. PyTorch’s torch.onnx.export exposes settings such as operator-set version, input and output names, and dynamic axes; Transformers and Optimum offer less manual conversion paths. section 5.5 section 6.3
  • Check the exported schema, then compare inference outputs with the original model on test inputs. A successful schema check alone does not establish matching behavior. section 5.5
  • Choose a runtime and hardware provider. ONNX Runtime supports CPU and GPU execution providers; graph optimizations, quantization, and GPU I/O binding are further options when performance matters. section 5.6 section 5.7

A small graph and a production-scale export

For Y = XA + B, an ONNX graph uses MatMul followed by Add; Python helpers declare typed inputs and outputs, assemble the graph, check it, and serialize it. For question answering, a BERT model is exported with variable batch and sequence axes, checked, and compared with its original outputs on SQuAD samples before CPU inference is benchmarked. The latter workflow tests deployment behavior, not just whether a file can be saved. section 5.2 section 5.5

Portability does not mean every deployment has the same cost. ONNX Runtime can optimize a graph during conversion or session initialization, and transformer-specific optimization can be applied to an exported model. On a GPU, default CPU placement of inputs and outputs can add transfers; IOBinding allows data to remain on the execution device. Measure latency and check accuracy after changes such as 8-bit quantization. section 5.6.1 section 5.6.2 section 6.3

How Domain-Specific Small Language Models covers it

How deep it goes
Defines the format through typed tensors and operators, then works through graph construction, export, validation, and CPU/GPU execution.
Examples
Uses a MatMul–Add linear-regression graph and exported BERT and GPT-2 models to illustrate construction and inference.
What it assumes
Assumes familiarity with trained models and Python ML workflows while defining ONNX operators, tensors, and the format–runtime distinction.
What you can do after
Build it from scratch with ONNX helpers, or apply it via PyTorch export and ONNX Runtime to validate and run models.
How it connects
Connects model interoperability to ONNX Runtime, execution providers, graph optimization, quantization, and I/O binding.

Also explained in

Compare how 7 books explain ONNX →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Domain-Specific Small Language Models. The section links point to the parts of the book it draws on.