Skip to main content
The Open Neural Network Exchange (ONNX) format is an open standard for representing machine learning models. ONNX Runtime uses this format as its primary input for inference and training.

What is ONNX?

ONNX provides a common format for representing deep learning models, enabling interoperability between different frameworks:
  • Framework Agnostic: Export from PyTorch, TensorFlow, scikit-learn, and more
  • Standardized Operators: Well-defined operator specifications with versioning
  • Portable: Run models across different hardware and platforms
  • Extensible: Support for custom operators and domains

ONNX Format Structure

An ONNX model consists of several key components:

ModelProto

The top-level container for an ONNX model:
The ir_version field indicates the ONNX IR (Intermediate Representation) version, currently at version 9.

GraphProto

Represents the computational graph:

NodeProto

Defines individual operators in the graph:

Model Components

Nodes (Operators)

Nodes represent operations in the computation graph:

Initializers (Constants)

Initializers store constant tensors like model weights:
  • Embedded directly in the model file
  • Can be stored externally for large models
  • Typically used for learned parameters

Inputs and Outputs

Define the model’s interface:
ONNX supports various tensor element types:

Operator Sets (OpSets)

ONNX uses versioned operator sets to ensure compatibility:
ONNX Runtime supports multiple OpSet versions simultaneously. Models are compatible as long as the runtime supports the required OpSet version.

OpSet Evolution

Operator definitions evolve across versions:
  • New operators: Added in newer OpSets
  • Updated semantics: Changes to existing operators
  • Deprecated operators: Old operators may be removed
  • Attribute changes: New or modified operator attributes

ORT Format

ONNX Runtime also supports its own optimized format (ORT format):

ONNX Format

  • Standard ONNX protobuf format
  • Portable across runtimes
  • Human-readable (with tools)
  • Larger file size

ORT Format

  • Optimized for ONNX Runtime
  • Faster loading time
  • Smaller file size
  • Pre-applied optimizations

Converting to ORT Format

ORT format models are version-specific. Models saved in one ONNX Runtime version may not load in different versions.

External Data

Large models can store tensors externally:

External Data Configuration

External data is useful for:
  • Models larger than 2GB (protobuf limit)
  • Faster git operations (diff, clone)
  • Separate weight management

Subgraphs and Control Flow

ONNX supports control flow operators with subgraphs:

If Operator

Loop Operator

Implements iterative computation:

Model Metadata

Models can include custom metadata:

Inspecting ONNX Models

Best Practices

Define dynamic dimensions with names instead of -1:
Always optimize models before deployment:
  • Use graph optimizations
  • Consider quantization
  • Convert to ORT format for production
Use the model_version field to track model versions:
Add documentation strings and metadata:

Next Steps

Execution Providers

Learn how execution providers accelerate model inference

Graph Optimizations

Understand optimization techniques for better performance

Sessions

Deep dive into InferenceSession configuration

Custom Operators

Learn how to add custom operators