ONNX Optimizer
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
-
subprocess.CalledProcessError: Command '['/usr/bin/cmake', '--build', '.', '--', '-j', '4']' returned non-zero exit status 2.
cause This error typically occurs during installation from source (when `pip` tries to build the wheel) if the `cmake` build system or other necessary C++ build tools (like `protobuf` development headers) are not present on the system.fixEnsure `cmake` is installed and properly configured, and that `protobuf` development files are available. For example, on Ubuntu: `sudo apt-get update && sudo apt-get install cmake build-essential libprotobuf-dev protobuf-compiler`. -
AttributeError: module 'onnx' has no attribute 'optimizer' or ImportError: cannot import name 'optimizer' from 'onnx'
cause Attempting to import the optimizer directly from the `onnx` package, which is no longer the correct path for `onnx` versions 1.9.0 and newer. The optimizer functionality was moved to the `onnxoptimizer` package.fixInstall the `onnxoptimizer` package separately (`pip install onnxoptimizer`) and change your import statements from `from onnx import optimizer` to `import onnxoptimizer`. -
[ONNXRuntimeError] : 2 : INVALID_ARGUMENT : Invalid rank for input: float_input Got: 1 Expected: 2 Please fix either the inputs/outputs or the model.
cause While not directly an `onnxoptimizer` error, this is a common issue when using optimized ONNX models with `onnxruntime` if the input shape provided to the runtime session does not match the expectations of the optimized model. This could indicate a misunderstanding of the model's new input shape after optimization, or an issue with how the model was optimized.fixVerify the expected input shapes of the optimized ONNX model using `onnx.load("optimized_model.onnx").graph.input` and ensure your input data conforms to these shapes. Use tools like Netron to visually inspect the optimized model graph.
Warnings
- breaking The 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.
- gotcha Building `onnxoptimizer` from source (e.g., when pre-built wheels are unavailable or for specific development needs) requires `protobuf` to be installed on your system.
- gotcha Some advanced or custom graph structures might not be optimized as expected. The optimizer relies on exact subgraph matching for certain transformations.
Install
-
pip install onnxoptimizer
Imports
- optimize
from onnx import optimizer
import onnxoptimizer optimized_model = onnxoptimizer.optimize(model)
Quickstart
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}")