Registry / ai-ml / onnxoptimizer

onnxoptimizer

JSON →
library0.4.2pypypi✓ verified 80d ago

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.

pip install onnxoptimizer
INSTALL
IMPORT
SIG · ONNXOPTIMIZER
O
onnxoptimizer
ai-mlpythonv0.4.2
Install
6.9s avg
Import
554ms
Disk
196MB
Pass rate
5/ 10
Env Coverage5 / 10
glibc
3.93.13
musl
3.93.13
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
musl
py 3.103.920 runs
build_error
glibc
py 3.103.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.
fix
Install `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.
fix
Ensure `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.
fix
Carefully 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.
Agent activity
4 hits · last 30 days
node
4
Resources
onnxoptimizer — pip install onnxoptimizer · libregistry