liveBook · Manning

Chapter 5: Exploring ONNX

What you will learn in chapter 5 of Domain-Specific Small Language Models

Introduces ONNX as a framework relevant to model optimization, quantization, and portability.

After this chapter you will be able to

What it builds on

Section by section

Introduction

Introduces ONNX as a framework relevant to model optimization, quantization, and portability.

Key terms: ONNX framework, model optimization, quantization, portability

5.1 The ONNX format

This section explains why ONNX makes trained models portable, how its format represents models, and what can be done after conversion.

Key terms: ONNX, interoperability, portability, lock-in, ONNX graph, operators, typed tensor, custom operators, convert to ONNX, ONNX Runtime

5.2 ONNX operators and types

This section explains ONNX operators and tensor types, then uses a linear regression model to show how an ONNX graph is built, checked, serialized, and inspected.

Key terms: operators, tensors, element type, shape, MatMul, Add, make_tensor_value_info, make_node, make_graph, make_model

5.3 The ONNX runtime

This section explains what ONNX Runtime does, the basic sequence for running ONNX inference, and a browser-based serving option.

Key terms: ONNX format, ONNX Runtime, ORT, hardware acceleration, inference, InferenceSession, get_inputs, get_outputs, input preprocessing, output postprocessing

5.4 ONNX runtime providers

This section explains how ONNX Runtime execution providers use deployment hardware, how providers differ in support and maturity, and how to inspect and prioritize those available.

Key terms: ORT, execution providers, nodes, subgraphs, stable execution providers, preview, NVIDIA CUDA, available providers, Python list, CUDA provider

5.5 ONNX for LLMs on CPU

This section walks through exporting a BERT model to ONNX, validating it, and running and benchmarking CPU inference.

Key terms: bert-base-uncased, PyTorch’s ONNX export function, constant folding, variable-length axes, check_model, allclose, rtol, atol, ONNX Runtime session, NumPy arrays

5.6 ONNX for LLMs on GPU

This section explains the changes needed to convert and run an ONNX model on an NVIDIA GPU rather than a CPU.

Key terms: ONNX inference session, CUDA execution provider, onnxruntime for GPU, GPU memory, hardware acceleration, convert the model to ONNX

5.6.1 ONNX for GPT on GPU

This section exports GPT-2 to ONNX for GPU inference and explains how to optimize the exported graph offline or when an inference session starts.

Key terms: GPT-2 small, return_attention_mask, inference session, CUDA runtime provider, ONNX Runtime, Transformer optimizer, optimize_model, graph optimization level, constant folding, graph partitioning

5.6.2 I/O binding

This section explains why ONNX Runtime I/O binding can reduce CPU–GPU data-transfer overhead and outlines how to bind inputs and outputs for GPU inference.

Key terms: ONNX Runtime, ORT, IOBinding, execution providers, run_with_iobinding, CUDA, input, output

5.7 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 4: Running inference Chapter 6: Quantizing for your production environment →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.