liveBook · Manning

Torch.onnx.export

As explained in Quantization and Fast Inference

torch.onnx.export is a PyTorch API that exports a PyTorch model to the ONNX format.

Export converts a live PyTorch nn.Module into an ONNX graph. The dynamo flag selects between TorchScript tracing and the newer torch.export-based capture path. Choose deliberately: dynamo=True became the default in PyTorch 2.9, but reported failures include several model families, and legacy dynamic_axes support is incomplete on that path. section 6.4

Listing 6.6 Export ResNet-18 to ONNX
torch.onnx.export(
    model,
    dummy_input,                                       #A
    "resnet18_fp32.onnx",
    opset_version=17,                                  #B
    input_names=["input"],
    output_names=["output"],
    dynamic_axes={"input": {0: "batch"},
                  "output": {0: "batch"}},             #C
    dynamo=False,                                      #D
)

A ResNet-18 export configured with the legacy exporter, opset 17, and a dynamic batch dimension. section 6.4

How Quantization and Fast Inference covers it

How deep it goes
Compares both exporter mechanisms and documents compatibility caveats, while deferring hardware-targeted optimization.
Examples
Uses a ResNet-18 export configured for opset 17, named I/O, and a dynamic batch axis.
What it assumes
Assumes a live PyTorch nn.Module and example inputs for tracing or graph capture.
What you can do after
Apply torch.onnx.export with example inputs and explicit exporter settings to produce an ONNX graph.
How it connects
Connects PyTorch export with ONNX, TorchScript, torch.export, TorchDynamo, and ONNX Runtime.

Also explained in

Compare how 2 books explain torch.onnx.export →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Quantization and Fast Inference. The section links point to the parts of the book it draws on.