Install & Compatibility
Where this runs
tested against v0.4.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.920 runs
build_error
glibcpy 3.10–3.920 runs
installs and imports cleanly · install 6.9s · import 0.554s · 193MB
196MB installed
● package 196MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
optimize
✓ from onnxoptimizer import optimize
✗ import onnxoptimizer
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}")
Debug
Known issues
breakingThe ONNX optimizer functionality was moved from the `onnx` package to a separate `onnxoptimizer` package. Direct imports like `from onnx import optimizer` will fail for `onnx` versions 1.9.0 and later.fixInstall `onnxoptimizer` as a separate package (`pip install onnxoptimizer`) and update imports to `import onnxoptimizer` or `from onnxoptimizer import optimize`.
affects: onnx >= 1.9.0
gotchaBuilding `onnxoptimizer` from source (e.g., when pre-built wheels are unavailable or for specific development needs) requires `protobuf` to be installed on your system.fixEnsure `protobuf` is installed before attempting to build from source (e.g., `pip install protobuf` or `sudo apt-get install libprotobuf-dev protobuf-compiler`).
affects: All versions when building from source
gotchaSome advanced or custom graph structures might not be optimized as expected. The optimizer relies on exact subgraph matching for certain transformations.fixCarefully inspect the optimized graph using tools like Netron. If expected optimizations are not applied, consider simplifying the model or implementing custom passes if necessary.
affects: All versions
Upgrade
Version history
0.4.2latest on PyPI · released Jan 7, 2026
Audit
Dependencies
onnxrequiredRequired for loading, manipulating, and saving ONNX model files which onnxoptimizer operates on.
protobufoptionalRequired for building onnxoptimizer from source if pre-built wheels are not available for your system or architecture.