ONNX Simplifier

0.6.2 · active · verified Mon Apr 13

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.

Warnings

Install

Imports

Quickstart

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)

view raw JSON →