ONNX Optimizer

0.4.2 · active · verified Thu Apr 16

ONNX Optimizer is a Python library that provides a C++ library for performing arbitrary optimizations on ONNX models, offering a growing list of prepackaged optimization passes. It aims to share optimization work between various ONNX backend implementations, making models more efficient for inference. The project is actively maintained with frequent minor releases, often every few months.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to load an ONNX model (here, a dummy one is created), apply optimizations using `onnxoptimizer.optimize()`, and then save the resulting optimized model. It also includes an optional step to verify the optimized model using ONNX Runtime.

import onnx
import onnxoptimizer

# Create a dummy ONNX model for demonstration
# In a real scenario, you would load an existing .onnx file
from onnx import helper
from onnx import TensorProto

graph_def = helper.make_graph(
    [
        helper.make_node("Add", ["input1", "input2"], ["output"]), # Example operation
    ],
    "simple_graph",
    [
        helper.make_tensor_value_info("input1", TensorProto.FLOAT, [1, 2]),
        helper.make_tensor_value_info("input2", TensorProto.FLOAT, [1, 2]),
    ],
    [
        helper.make_tensor_value_info("output", TensorProto.FLOAT, [1, 2]),
    ],
)

model = helper.make_model(graph_def, producer_name='test-model')

# Save the dummy model (optional, for verification)
onnx.save(model, "original_model.onnx")
print("Original model saved to original_model.onnx")

# Optimize the model
# You can specify passes, e.g., ['fuse_bn_into_conv']
# By default, a set of common fusion and elimination passes are used.
optimized_model = onnxoptimizer.optimize(model)

# Save the optimized model
onnx.save(optimized_model, "optimized_model.onnx")
print("Optimized model saved to optimized_model.onnx")

# Optional: Verify the optimized model (requires onnxruntime)
try:
    import onnxruntime as ort
    sess_options = ort.SessionOptions()
    _ = ort.InferenceSession("optimized_model.onnx", sess_options)
    print("Optimized model successfully loaded by ONNX Runtime.")
except ImportError:
    print("onnxruntime not installed. Cannot verify optimized model.")
except Exception as e:
    print(f"Error loading optimized model with ONNX Runtime: {e}")

view raw JSON →