{"id":5347,"library":"onnxsim","title":"ONNX Simplifier","description":"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.","status":"active","version":"0.6.2","language":"en","source_language":"en","source_url":"https://github.com/onnxsim/onnxsim","tags":["onnx","deep-learning","model-optimization","simplification","inference"],"install":[{"cmd":"pip install onnxsim","lang":"bash","label":"Install latest stable version"}],"dependencies":[{"reason":"Core dependency for ONNX model manipulation.","package":"onnx","optional":false},{"reason":"Used for graph optimization passes within simplification process.","package":"onnx-optimizer","optional":false},{"reason":"Used for rich text and progress bar in CLI, not strictly required for core simplification API.","package":"rich","optional":true}],"imports":[{"symbol":"simplify","correct":"from onnxsim import simplify"}],"quickstart":{"code":"import onnx\nfrom onnxsim import simplify\nimport torch\nimport torch.nn as nn\nimport os\n\n# Create a dummy ONNX model for demonstration\nclass SimpleNet(nn.Module):\n    def __init__(self):\n        super().__init__()\n        self.fc = nn.Linear(10, 2)\n    def forward(self, x):\n        return self.fc(x)\n\nmodel = SimpleNet()\ndummy_input = torch.randn(1, 10)\ninput_model_path = \"dummy_model.onnx\"\noutput_model_path = \"dummy_model_simplified.onnx\"\n\ntorch.onnx.export(model, dummy_input, input_model_path,\n                  input_names=['input'], output_names=['output'])\n\n# Load your predefined ONNX model\nonnx_model = onnx.load(input_model_path)\n\n# Convert model\nmodel_simp, check = simplify(onnx_model)\n\nassert check, \"Simplified ONNX model could not be validated\"\n\n# Save the simplified model\nonnx.save(model_simp, output_model_path)\nprint(f\"Model simplified and saved to {output_model_path}\")\n\n# Clean up dummy models\nos.remove(input_model_path)\nos.remove(output_model_path)","lang":"python","description":"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."},"warnings":[{"fix":"Ensure `cmake` and a C++ compiler (like GCC or MSVC) are installed on your system before attempting `pip install onnxsim`.","message":"Building `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.","severity":"gotcha","affected_versions":"All versions, especially when pre-built wheels are not used."},{"fix":"Consider splitting very large models into smaller subgraphs if possible, or investigate advanced techniques for handling large ONNX models outside the typical workflow.","message":"Models 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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure the input ONNX model adheres to the ONNX specification regarding graph topology. Tools like Netron can help visualize and debug model graphs.","message":"ONNX models with graphs that are not topologically sorted may result in validation errors during simplification.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Remove the `--enable-fuse-bn` flag. If you need to skip this optimization, use `--skip-fuse-bn` instead.","message":"The `--enable-fuse-bn` command-line argument for fusing batch normalization into convolutional layers is deprecated as this optimization is now enabled by default.","severity":"deprecated","affected_versions":">=0.4.x (specific version for default change is not exact, but in recent 0.6.x it's default)"},{"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`.","message":"When 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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-13T00:00:00.000Z","next_check":"2026-07-12T00:00:00.000Z"}