onnx-simplifier 0.4.36


pip install onnx-simplifier

  Latest version

Released: Mar 04, 2024

Project Links

Meta
Author: ONNX Simplifier Authors
Requires Python: >=3.7

Classifiers

Development Status
  • 4 - Beta

Intended Audience
  • Developers

License
  • OSI Approved :: Apache Software License

Programming Language
  • Python :: 3 :: Only
  • Python :: 3.7
  • Python :: 3.8
  • Python :: 3.9
  • Python :: 3.10
  • Python :: 3.11

Topic
  • Scientific/Engineering
  • Software Development

ONNX Simplifier

PyPI version PyPI pyversions PyPI license PRs Welcome

ONNX is great, but sometimes too complicated.

Background

One day I wanted to export the following simple reshape operation to ONNX:

import torch


class JustReshape(torch.nn.Module):
    def __init__(self):
        super(JustReshape, self).__init__()

    def forward(self, x):
        return x.view((x.shape[0], x.shape[1], x.shape[3], x.shape[2]))


net = JustReshape()
model_name = 'just_reshape.onnx'
dummy_input = torch.randn(2, 3, 4, 5)
torch.onnx.export(net, dummy_input, model_name, input_names=['input'], output_names=['output'])

The input shape in this model is static, so what I expected is

simple_reshape

However, I got the following complicated model instead:

complicated_reshape

Our solution

ONNX Simplifier is presented to simplify the ONNX model. It infers the whole computation graph and then replaces the redundant operators with their constant outputs (a.k.a. constant folding).

Web version

We have published ONNX Simplifier on convertmodel.com. It works out of the box and doesn't need any installation. Note that it runs in the browser locally and your model is completely safe.

Python version

pip3 install -U pip && pip3 install onnxsim

Then

onnxsim input_onnx_model output_onnx_model

For more advanced features, try the following command for help message

onnxsim -h

Demonstration

An overall comparison between a complicated model and its simplified version:

Comparison between old model and new model

In-script workflow

If you would like to embed ONNX simplifier python package in another script, it is just that simple.

import onnx
from onnxsim import simplify

# load your predefined ONNX model
model = onnx.load(filename)

# convert model
model_simp, check = simplify(model)

assert check, "Simplified ONNX model could not be validated"

# use model_simp as a standard ONNX model object

You can see more details of the API in onnxsim/onnx_simplifier.py

Projects Using ONNX Simplifier

Chat

We created a Chinese QQ group for ONNX!

ONNX QQ Group (Chinese): 1021964010, verification code: nndab. Welcome to join!

For English users, I'm active on the ONNX Slack. You can find and chat with me (daquexian) there.

0.4.36 Mar 04, 2024
0.4.35 Oct 21, 2023
0.4.34 Oct 21, 2023
0.4.33 Jun 26, 2023
0.4.31 Jun 05, 2023
0.4.30 Jun 04, 2023
0.4.28 May 01, 2023
0.4.27 May 01, 2023
0.4.26 Apr 29, 2023
0.4.25 Apr 28, 2023
0.4.24 Apr 21, 2023
0.4.23 Apr 21, 2023
0.4.22 Apr 19, 2023
0.4.21 Apr 19, 2023
0.4.20 Apr 17, 2023
0.4.19 Mar 21, 2023
0.4.17 Feb 07, 2023
0.4.15 Feb 05, 2023
0.4.13 Jan 06, 2023
0.4.10 Nov 15, 2022
0.4.9 Nov 10, 2022
0.4.8 Aug 27, 2022
0.4.7 Aug 09, 2022
0.4.6 Aug 05, 2022
0.4.5 Jul 30, 2022
0.4.4 Jul 29, 2022
0.4.3 Jul 26, 2022
0.4.2 Jul 24, 2022
0.4.1 Jul 18, 2022
0.4.0 Jul 07, 2022
0.3.10 May 14, 2022
0.3.9 May 06, 2022
0.3.8 Apr 27, 2022
0.3.7 Mar 05, 2022
0.3.6 May 31, 2021
0.3.5 Apr 18, 2021
0.3.4 Apr 04, 2021
0.3.3 Feb 27, 2021
0.3.2 Feb 17, 2021
0.3.1 Feb 14, 2021
0.3.0 Feb 06, 2021
0.2.28 Feb 03, 2021
0.2.27 Feb 02, 2021
0.2.26 Feb 01, 2021
0.2.25 Feb 01, 2021
0.2.24 Jan 30, 2021
0.2.23 Jan 25, 2021
0.2.22 Jan 14, 2021
0.2.21 Jan 04, 2021
0.2.20 Dec 25, 2020
0.2.19 Nov 10, 2020
0.2.18 Oct 27, 2020
0.2.17 Oct 25, 2020
0.2.16 Sep 07, 2020
0.2.15 Aug 31, 2020
0.2.14 Aug 21, 2020
0.2.13 Aug 15, 2020
0.2.12 Aug 02, 2020
0.2.11 Aug 02, 2020
0.2.10 Jul 01, 2020
0.2.9 Apr 27, 2020
0.2.8 Apr 11, 2020
0.2.7 Mar 28, 2020
0.2.6 Mar 23, 2020
0.2.5 Mar 21, 2020
0.2.4 Jan 29, 2020
0.2.3 Jan 28, 2020
0.2.2 Nov 06, 2019
0.2.1 Nov 01, 2019
0.2.0 Oct 31, 2019
0.1.9 Oct 28, 2019
0.1.8 Aug 06, 2019
0.1.6 Jul 29, 2019
0.1.5 May 23, 2019
0.1.4 May 11, 2019
0.1.3 May 04, 2019
0.1.2 Apr 15, 2019
0.1.1 Apr 02, 2019
0.1.0 Apr 02, 2019
0.0.0 Jul 29, 2019
Extras: None
Dependencies:
onnx
rich