Registry / ai-ml / onnxsim

onnxsim

JSON →
library0.7.3pypypi✓ verified 22d ago

ONNX Simplifier (onnxsim) is a Python library designed to reduce the complexity of ONNX models by inferring the computation graph and performing constant folding. This makes ONNX models more efficient for inference and deployment. It is actively maintained with frequent minor releases, currently at version 0.6.2.

pip install onnxsim
INSTALL
IMPORT
SIG · ONNXSIM
O
onnxsim
ai-mlpythonv0.7.3
Install
7.7s avg
Import
538ms
Disk
197MB
Pass rate
4/ 10
Env Coverage4 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.7.3 · 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
glibc
py 3.10
✕ build_error
✓ 8.1s
py 3.11
✕ build_error
✓ 7.8s
py 3.12
✕ build_error
✓ 7.4s
py 3.13
✕ build_error
✓ 7.4s
py 3.9
✕ build_error
✕ build_error
197MB installed
● package 197MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

simplify
from onnxsim import simplify

This quickstart demonstrates how to load an ONNX model, simplify it using `onnxsim.simplify`, and save the optimized model. A dummy PyTorch model is created and exported to ONNX for a complete runnable example.

import onnx from onnxsim import simplify import torch import torch.nn as nn import os # Create a dummy ONNX model for demonstration class SimpleNet(nn.Module): def __init__(self): super().__init__() self.fc = nn.Linear(10, 2) def forward(self, x): return self.fc(x) model = SimpleNet() dummy_input = torch.randn(1, 10) input_model_path = "dummy_model.onnx" output_model_path = "dummy_model_simplified.onnx" torch.onnx.export(model, dummy_input, input_model_path, input_names=['input'], output_names=['output']) # Load your predefined ONNX model onnx_model = onnx.load(input_model_path) # Convert model model_simp, check = simplify(onnx_model) assert check, "Simplified ONNX model could not be validated" # Save the simplified model onnx.save(model_simp, output_model_path) print(f"Model simplified and saved to {output_model_path}") # Clean up dummy models os.remove(input_model_path) os.remove(output_model_path)
onnxsim --version
Debug
Known issues
gotchaBuilding `onnxsim` from source (e.g., if pre-built wheels are unavailable for your specific platform/Python version) requires `cmake` and a C++ compiler. Installation may fail without these system-level dependencies.
fix
Ensure `cmake` and a C++ compiler (like GCC or MSVC) are installed on your system before attempting `pip install onnxsim`.
affects: All versions, especially when pre-built wheels are not used.
gotchaModels exceeding a protobuf size limit (typically 2GB) can cause errors during loading or simplification due to limitations in the underlying protobuf library used by ONNX.
fix
Consider splitting very large models into smaller subgraphs if possible, or investigate advanced techniques for handling large ONNX models outside the typical workflow.
affects: All versions
gotchaONNX models with graphs that are not topologically sorted may result in validation errors during simplification.
fix
Ensure the input ONNX model adheres to the ONNX specification regarding graph topology. Tools like Netron can help visualize and debug model graphs.
affects: All versions
deprecatedThe `--enable-fuse-bn` command-line argument for fusing batch normalization into convolutional layers is deprecated as this optimization is now enabled by default.
fix
Remove the `--enable-fuse-bn` flag. If you need to skip this optimization, use `--skip-fuse-bn` instead.
affects: >=0.4.x (specific version for default change is not exact, but in recent 0.6.x it's default)
gotchaWhen simplifying models with dynamic input shapes, you often need to explicitly provide the expected input shapes using the `--input-shape` argument in the CLI or `input_shapes` parameter in the `simplify` function.
fix
Pass a dictionary mapping input names to their expected (or example) shapes, e.g., `simplify(model, input_shapes={'input_name': [1, 3, 224, 224]})` or `onnxsim input.onnx output.onnx --input-shape input_name:1,3,224,224`.
affects: All versions
Upgrade
Version history
0.7.3latest on PyPI · released Aug 12, 2026
Audit
Dependencies
onnxrequiredCore dependency for ONNX model manipulation.
onnx-optimizerrequiredUsed for graph optimization passes within simplification process.
richoptionalUsed for rich text and progress bar in CLI, not strictly required for core simplification API.
Agent activity
26 hits · last 30 days
node
24
Amazon
1
Resources