Chapter 3: ONNX - The Universal Format

WIA-AI-008 Standard • Estimated reading time: 50 minutes

3.1 What is ONNX?

The Open Neural Network Exchange (ONNX) is an open format designed to represent deep learning models in a framework-agnostic manner. Created in 2017 by Microsoft and Facebook (now Meta), ONNX has become the de facto standard for model interoperability, supported by all major frameworks including PyTorch, TensorFlow, Keras, MXNet, and many others.

The Philosophy Behind ONNX

ONNX embodies the 弘益人間 (Benefit All Humanity) philosophy by removing barriers between different AI ecosystems. It enables:

3.2 ONNX Architecture

ONNX uses Protocol Buffers (protobuf) for serialization, defining models as directed acyclic graphs (DAGs).

Core Components

# ONNX model structure
ModelProto
├── ir_version: int64
├── opset_import: [OpsetIdProto]
├── producer_name: string
├── producer_version: string
├── domain: string
├── model_version: int64
├── doc_string: string
├── graph: GraphProto
│   ├── node: [NodeProto]
│   ├── name: string
│   ├── input: [ValueInfoProto]
│   ├── output: [ValueInfoProto]
│   ├── initializer: [TensorProto]
│   └── value_info: [ValueInfoProto]
└── metadata_props: [StringStringEntryProto]

3.3 Exporting to ONNX

PyTorch to ONNX

PyTorch has excellent built-in ONNX export capabilities:

import torch
import torch.nn as nn

# Define a simple model
class SimpleModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.conv1 = nn.Conv2d(3, 64, 3, padding=1)
        self.relu = nn.ReLU()
        self.pool = nn.MaxPool2d(2)
        self.fc = nn.Linear(64 * 112 * 112, 10)

    def forward(self, x):
        x = self.pool(self.relu(self.conv1(x)))
        x = x.view(x.size(0), -1)
        x = self.fc(x)
        return x

model = SimpleModel()
model.eval()

# Prepare dummy input
dummy_input = torch.randn(1, 3, 224, 224)

# Export to ONNX
torch.onnx.export(
    model,                                # Model being exported
    dummy_input,                          # Model input (or tuple for multiple inputs)
    "simple_model.onnx",                 # Where to save the model
    export_params=True,                   # Store trained parameters
    opset_version=15,                    # ONNX version
    do_constant_folding=True,            # Optimize by constant folding
    input_names=['input'],               # Input names
    output_names=['output'],             # Output names
    dynamic_axes={                        # Dynamic dimensions
        'input': {0: 'batch_size'},
        'output': {0: 'batch_size'}
    }
)

TensorFlow to ONNX

TensorFlow requires the tf2onnx tool:

# Install tf2onnx
# pip install tf2onnx

import tensorflow as tf
import tf2onnx

# Create a simple TensorFlow model
model = tf.keras.Sequential([
    tf.keras.layers.Conv2D(64, 3, padding='same', activation='relu', input_shape=(224, 224, 3)),
    tf.keras.layers.MaxPooling2D(2),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(10, activation='softmax')
])

# Save as SavedModel first
model.save('tf_model')

# Convert to ONNX via command line
# python -m tf2onnx.convert --saved-model tf_model --output model.onnx --opset 15

# Or programmatically
spec = (tf.TensorSpec((None, 224, 224, 3), tf.float32, name="input"),)
model_proto, _ = tf2onnx.convert.from_keras(model, input_signature=spec, opset=15)

with open("tf_model.onnx", "wb") as f:
    f.write(model_proto.SerializeToString())

Common Export Issues and Solutions

Issue Cause Solution
Unsupported operator Custom op not in ONNX opset Use supported alternatives or register custom op
Dynamic control flow If/while loops in model Use ONNX opset 13+ or refactor model
Shape inference failure Ambiguous tensor shapes Provide explicit shapes via dynamic_axes
Incorrect output values Numerical precision issues Check dtype conversions, use validation script

3.4 ONNX Runtime

ONNX Runtime is a high-performance inference engine optimized for ONNX models. It often outperforms native framework inference due to aggressive optimizations.

Basic Inference

import onnxruntime as ort
import numpy as np

# Create inference session
session = ort.InferenceSession("model.onnx")

# Get input and output names
input_name = session.get_inputs()[0].name
output_name = session.get_outputs()[0].name

# Prepare input data
input_data = np.random.randn(1, 3, 224, 224).astype(np.float32)

# Run inference
outputs = session.run([output_name], {input_name: input_data})

print(f"Output shape: {outputs[0].shape}")
print(f"Predictions: {outputs[0]}")

Performance Optimization

import onnxruntime as ort

# Configure session options for maximum performance
session_options = ort.SessionOptions()

# Enable graph optimizations
session_options.graph_optimization_level = ort.GraphOptimizationLevel.ORT_ENABLE_ALL

# Set number of threads
session_options.intra_op_num_threads = 4
session_options.inter_op_num_threads = 4

# Enable profiling (for debugging)
session_options.enable_profiling = True

# Use specific execution providers (GPU, TensorRT, etc.)
providers = [
    'CUDAExecutionProvider',      # NVIDIA GPU
    'TensorrtExecutionProvider',  # TensorRT
    'CPUExecutionProvider'        # CPU fallback
]

session = ort.InferenceSession(
    "model.onnx",
    sess_options=session_options,
    providers=providers
)

# Check which provider is being used
print(f"Providers: {session.get_providers()}")

3.5 ONNX Model Inspection and Validation

Inspecting Model Structure

import onnx

# Load ONNX model
model = onnx.load("model.onnx")

# Check model validity
onnx.checker.check_model(model)
print("✓ Model is valid")

# Print model information
print(f"IR Version: {model.ir_version}")
print(f"Producer: {model.producer_name} {model.producer_version}")
print(f"Domain: {model.domain}")
print(f"Model Version: {model.model_version}")
print(f"Doc: {model.doc_string}")

# Inspect graph
graph = model.graph
print(f"\nGraph name: {graph.name}")

# Print inputs
print("\nInputs:")
for input in graph.input:
    print(f"  - {input.name}: {input.type.tensor_type.elem_type}, shape: {[d.dim_value for d in input.type.tensor_type.shape.dim]}")

# Print outputs
print("\nOutputs:")
for output in graph.output:
    print(f"  - {output.name}: {output.type.tensor_type.elem_type}")

# Print operators
print(f"\nNodes: {len(graph.node)}")
op_types = {}
for node in graph.node:
    op_types[node.op_type] = op_types.get(node.op_type, 0) + 1

print("\nOperator distribution:")
for op_type, count in sorted(op_types.items(), key=lambda x: -x[1]):
    print(f"  {op_type}: {count}")

Numerical Validation

import torch
import onnxruntime as ort
import numpy as np

# Original PyTorch model
model_pt = SimpleModel()
model_pt.eval()

# Export to ONNX
torch.onnx.export(model_pt, dummy_input, "model.onnx")

# Create ONNX Runtime session
session = ort.InferenceSession("model.onnx")

# Prepare test inputs
test_input = torch.randn(10, 3, 224, 224)

# PyTorch inference
with torch.no_grad():
    pt_output = model_pt(test_input).numpy()

# ONNX Runtime inference
ort_output = session.run(None, {
    session.get_inputs()[0].name: test_input.numpy()
})[0]

# Compare outputs
max_diff = np.max(np.abs(pt_output - ort_output))
mean_diff = np.mean(np.abs(pt_output - ort_output))

print(f"Max difference: {max_diff}")
print(f"Mean difference: {mean_diff}")

# Assert numerical equivalence (within tolerance)
assert np.allclose(pt_output, ort_output, rtol=1e-3, atol=1e-5), "Outputs don't match!"
print("✓ Numerical validation passed")

3.6 ONNX Operators and Opsets

ONNX defines a standardized set of operators. Different opset versions support different operators and features.

Opset Evolution

Opset Released Key Features
9 2019 Stable baseline, broad support
11 2020 Better RNN support, dynamic shapes
13 2021 Control flow (If, Loop), advanced ops
15 2022 Improved quantization, training support
17 2023 Better transformer support, LayerNorm

Common ONNX Operators

# Core operators
Conv, ConvTranspose                    # Convolution layers
Gemm, MatMul                           # Dense layers
Relu, Sigmoid, Tanh, Softmax          # Activations
BatchNormalization, LayerNormalization # Normalization
MaxPool, AveragePool, GlobalAveragePool # Pooling
Add, Sub, Mul, Div                     # Element-wise ops
Concat, Split, Reshape, Transpose      # Tensor manipulation
Gather, Scatter                        # Indexing operations
If, Loop                               # Control flow (opset 13+)
Cast, Clip, Pad                        # Utility operations

3.7 Custom Operators in ONNX

When ONNX doesn't support a specific operation, you can define custom operators:

import torch
from torch.onnx import register_custom_op_symbolic

# Define custom operator behavior
@torch.onnx.symbolic_helper.parse_args('v', 'v', 'f')
def custom_op(g, input1, input2, alpha):
    return g.op("custom_domain::CustomOp", input1, input2, alpha_f=alpha)

# Register the symbolic function
register_custom_op_symbolic('custom_domain::CustomOp', custom_op, 14)

# Use in model export
class ModelWithCustomOp(nn.Module):
    def forward(self, x, y):
        # Your custom operation
        return custom_operation(x, y, alpha=0.5)

# Export will now include the custom op
torch.onnx.export(model, (x, y), "model_custom.onnx")

3.8 ONNX Model Optimization

ONNX provides tools to optimize models for better performance:

from onnxruntime.transformers import optimizer
from onnxruntime.transformers.fusion_options import FusionOptions

# Load model
model_path = "model.onnx"

# Configure optimization options
opt_options = FusionOptions('bert')
opt_options.enable_gelu = True
opt_options.enable_layer_norm = True
opt_options.enable_attention = True
opt_options.enable_skip_layer_norm = True
opt_options.enable_embed_layer_norm = True
opt_options.enable_bias_skip_layer_norm = True
opt_options.enable_bias_gelu = True

# Optimize
optimized_model = optimizer.optimize_model(
    model_path,
    model_type='bert',
    num_heads=12,
    hidden_size=768,
    optimization_options=opt_options
)

# Save optimized model
optimized_model.save_model_to_file("model_optimized.onnx")

print(f"Original nodes: {len(onnx.load(model_path).graph.node)}")
print(f"Optimized nodes: {len(optimized_model.model.graph.node)}")

Graph Optimizations

3.9 ONNX for Different Model Types

Vision Models

# ResNet, EfficientNet, Vision Transformers
import torchvision.models as models

resnet50 = models.resnet50(pretrained=True)
resnet50.eval()

dummy_input = torch.randn(1, 3, 224, 224)
torch.onnx.export(
    resnet50,
    dummy_input,
    "resnet50.onnx",
    opset_version=15,
    input_names=['image'],
    output_names=['probabilities'],
    dynamic_axes={'image': {0: 'batch'}, 'probabilities': {0: 'batch'}}
)

NLP Models

# BERT, GPT, T5
from transformers import BertModel, BertTokenizer

tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
model = BertModel.from_pretrained('bert-base-uncased')
model.eval()

# Create dummy inputs
text = "Hello, ONNX!"
inputs = tokenizer(text, return_tensors='pt')

# Export with multiple inputs
torch.onnx.export(
    model,
    (inputs['input_ids'], inputs['attention_mask']),
    "bert.onnx",
    opset_version=14,
    input_names=['input_ids', 'attention_mask'],
    output_names=['last_hidden_state'],
    dynamic_axes={
        'input_ids': {0: 'batch', 1: 'sequence'},
        'attention_mask': {0: 'batch', 1: 'sequence'},
        'last_hidden_state': {0: 'batch', 1: 'sequence'}
    }
)

Generative Models

# GANs, VAEs, Diffusion Models
class SimpleGAN(nn.Module):
    def __init__(self):
        super().__init__()
        self.generator = nn.Sequential(
            nn.Linear(100, 256),
            nn.ReLU(),
            nn.Linear(256, 784),
            nn.Tanh()
        )

    def forward(self, z):
        return self.generator(z)

gan = SimpleGAN()
gan.eval()

latent = torch.randn(1, 100)
torch.onnx.export(
    gan,
    latent,
    "gan_generator.onnx",
    opset_version=15,
    input_names=['latent_vector'],
    output_names=['generated_image']
)

3.10 ONNX Ecosystem and Tools

Key Tools

# Using onnx-simplifier
from onnxsim import simplify
import onnx

model = onnx.load("model.onnx")
simplified_model, check = simplify(model)

assert check, "Simplified model validation failed"
onnx.save(simplified_model, "model_simplified.onnx")

# Viewing with Netron
# Install: pip install netron
# Usage: netron model.onnx
# Opens interactive visualization in browser

Summary

Review Questions

  1. What problem does ONNX solve in the machine learning ecosystem?
  2. Describe the structure of an ONNX model. What are the main components?
  3. How do you export a PyTorch model to ONNX? Include dynamic axes in your answer.
  4. What is the difference between opset 9 and opset 15 in ONNX?
  5. Why might ONNX Runtime be faster than native PyTorch or TensorFlow inference?
  6. How do you validate that an ONNX model produces the same outputs as the original model?
  7. What are common issues when exporting models to ONNX, and how can they be resolved?
  8. Explain operator fusion and why it improves performance.
  9. How would you handle a custom operation that's not in the ONNX opset?
  10. Compare the process of exporting vision models vs NLP models to ONNX.

Korea Standardization Infrastructure Mapping

Korea operates a comprehensive standards governance system through inter-ministerial cooperation. National Standards Council (under Prime Minister's Office, per Framework Act on National Standards Article 5) coordinates KATS (Korean Agency for Technology and Standards), MFDS (Ministry of Food and Drug Safety), MOTIE (Ministry of Trade, Industry and Energy), MSIT (Ministry of Science and ICT), MOIS (Ministry of the Interior and Safety), MOE (Ministry of Environment), MOHW (Ministry of Health and Welfare), MND (Ministry of National Defense), MCST (Ministry of Culture, Sports and Tourism), MOFA (Ministry of Foreign Affairs), MOJ (Ministry of Justice), and FSC (Financial Services Commission). Accreditation and Testing: KOLAS (Korea Laboratory Accreditation Scheme) accredits 800+ testing laboratories. KAS (Korea Accreditation System) accredits 50+ certification bodies. KTC (Korea Testing Certification), KTR (Korea Testing & Research Institute), KTL (Korea Testing Laboratory), and KCL (Korea Conformity Laboratories) provide conformance testing. Telecom and Cyber: KCC (Korea Communications Commission), KCA (Korea Communications Agency), TTA (Telecommunications Technology Association), IITP (Institute for Information & Communications Technology Planning & Evaluation), NIPA (National IT Industry Promotion Agency), KISA (Korea Internet & Security Agency), KCMVP (Korea Cryptographic Module Validation Program), NIS (National Intelligence Service), NSR (National Security Research Institute), and NCSC (National Cyber Security Center). National R&D Centers: KIST, ETRI, KAIST, Seoul National University, Yonsei University, Korea University, POSTECH, UNIST, GIST, DGIST, KISTI, KIER, KIMM, KRICT, KFRI, KRIBB. International Standards Cooperation: ISO TC/SC Korean secretariats, IEC TC/SC Korean secretariats, ITU-T Study Group Korean chairs, 3GPP RAN/SA Korean chairs, IEEE 802 Korean chairs, W3C Korea office, OASIS Korea office, IETF Korea cooperation, OECD CSTP, UN ESCAP, APEC SCSC Korean cooperation. Korean Industrial Standards (KS) Catalog: KS X (Information) 25,000+, KS A (Basic) 15,000+, KS B (Machinery) 25,000+, KS C (Electrical) 18,000+, KS D (Metallurgy) 12,000+, KS E (Mining) 5,000+, KS F (Construction) 18,000+, KS H (Food) 8,000+, KS I (Environment) 5,000+, KS J (Biology) 3,000+, KS K (Textile) 15,000+, KS L (Ceramics) 7,000+, KS M (Chemistry) 12,000+, KS P (Medical) 5,000+, KS Q (Quality Mgmt) 4,000+, KS R (Transport) 12,000+, KS S (Service) 3,000+, KS T (Packaging) 4,000+, KS V (Shipbuilding) 5,000+, KS W (Aerospace) 3,000+ — totaling 220,000+ Korean Industrial Standards. Key Acts: Personal Information Protection Act (Act 19234, effective Sept 15, 2024), Electronic Government Act, Electronic Signature Act, Act on Promotion of Information and Communications Network Utilization and Information Protection, Information and Communications Infrastructure Protection Act, Data Industry Act, Public Data Act, AI Framework Act (Act 20212, effective July 2026), Industrial Technology Innovation Promotion Act, Framework Act on Science and Technology — 70+ Korean standardization-related laws.

Korea Digital Transformation Detailed Mapping

Korea operates digital transformation through a comprehensive governance system. Digital Government: Digital Platform Government Committee (established September 2022, under the President)·Ministry of the Interior and Safety Digital Government Bureau·e-Government Support Center·Gov.kr·National Citizen Service·KDIS (Korea Digital Information Society)·NIA (National Information Society Agency)·MOIS (Ministry of the Interior and Safety). K-DNS Infrastructure: Korea Internet & Security Agency (KISA) Korea Internet Center·KISA DNS Root Server·KRNIC (Korea Network Information Center)·BGP Korea·National Cyber Security Center (NCSC)·KCC (Korea Communications Commission)·MSIT (Ministry of Science and ICT)·NIA·NIPA. Korean Cloud Infrastructure: KT Cloud·NAVER Cloud (NCloud)·Samsung SDS Cloud·LG U+ Cloud·NHN Cloud·Kakao Enterprise Cloud·SK Telecom Cloud·KISA Cloud Security Assurance Program (CSAP)·KCMVP-validated cloud·ISMS-P (Information Security & Personal Information Management System). Korean Security Certifications: KISA ISMS-P certification·KCMVP (Korean Cryptographic Module Validation Program)·NIS (National Intelligence Service) "National Cryptographic Technology Operation Standards"·NCSC "National Cyber Security Strategy 2024-2028"·CC (Common Criteria) Korean evaluation bodies·EAL4·EAL5·KS X ISO/IEC 15408·19790·24759 Korean Profile. Korean Data Standards: NIA AI Hub·National Data Standardization Committee·Statistics Korea (KOSTAT)·MyData 4 Designated Combination Specialists (Samsung SDS, KICI, KOSTAT, KFTC)·National Institute of Korean Language·National Law Information Center·National Spatial Information Platform·National Spatial Data Center·Korean Spatial Information Standards. Finance and Fintech Standards: FSC (Financial Services Commission)·FSS (Financial Supervisory Service)·FIU (Financial Intelligence Unit)·BOK (Bank of Korea)·FSEC (Financial Security Institute)·KFTC (Korea Financial Telecommunications)·KSD (Korea Securities Depository)·KRX (Korea Exchange) 8-agency cooperation. 5G/6G Communications Infrastructure: 5G subscribers 35 million (2024)·5G base stations 350,000·6G commercialization target 2028·5G dedicated networks 16 operators·6G Acceleration Council (MSIT, 2024). K-Content: KOCCA (Korea Creative Content Agency)·MCST (Ministry of Culture, Sports and Tourism)·KCA (Korea Communications Agency)·Korea Culture Information Service Agency·Korean Film Archive·Korea Publishing Industry Promotion Agency. Data 3 Acts (Personal Information Protection Act·Credit Information Act·Telecommunications Network Act, 2020 enforcement)·Data Industry Act (2021)·Public Data Act (2013)·AI Framework Act (2026)·Digital Platform Government Framework Act (2024 proposed) — Korea digital transformation core legislation.