Data
What is ONNX?
ONNX (Open Neural Network Exchange) is an open model representation for exchanging machine-learning models between frameworks and inference runtimes. A typical `.onnx` file stores a Protocol Buffers ModelProto describing a computation graph, inputs and outputs, types and shapes, opset imports, and initializer tensors. Safetensors primarily stores weight tensors, whereas ONNX represents a model graph as well. Conversion does not make every runtime support all operators or model-specific features; check the IR/opset versions and implementation support.
When to choose this format
- Inference with runtimes that support the model's operators and opset
- Exchanging a model graph with inputs outputs and types
When to avoid this format
- Assuming every runtime supports every operator or preserves precision and performance
- Storing tensor weights alone without an inference graph or configuration
Common uses
- Exchanging machine-learning computation graphs and weights with supported runtimes
- Moving models between training frameworks and inference environments
Features
- Protocol Buffers ModelProto stores the graph and model information
- Nodes reference operators defined by domains and opset versions
- Initializer tensors can be embedded or stored in external data files
Advantages and disadvantages
Advantages
- Provides a common model graph for supported inference runtimes
- Includes graph and input/output type information for validation and optimization
- Large tensor data can be managed in external files
Disadvantages
- Operator opset type and optimization support differs among runtimes
- Converted outputs or performance may differ from the source framework
- External data files must remain paired with the .onnx model
Open and convert
How to open
Load it with an ONNX checker or compatible inference runtime. Before deployment, inspect IR and opset versions, operators, input/output types and shapes, and runtime support; compare outputs with the source model on representative inputs.
How to convert
Export using the source framework's ONNX exporter and validate the structure with an ONNX checker. Successful conversion does not guarantee numerical equivalence or execution on the target device. If external data is used, keep the model and every referenced file together in the distribution unit.
Cautions
- Do not omit rename or misplace sidecar files referenced by external tensor data
- External data locations can contain relative paths; inspect references in untrusted models and use maintained loaders
- Validate conversion preprocessing postprocessing and numerical precision even when the file is valid ONNX
FAQ
Is ONNX only a weights file?
No. ModelProto represents a computation graph and operator/type information, and can include initializer tensors. Large tensors may instead be stored as external data files.
Do ONNX and Safetensors serve the same purpose?
No. Safetensors mainly stores tensor weights; ONNX represents a model exchange graph for inference. They may be used together.
Will every inference runtime run any ONNX model?
No. Check whether the runtime supports the model's IR/opset, operators, data types, and target device.