Skip to main content

Simplify your ONNX model

Project description

ONNX Simplifier

PyPI version PyPI pyversions PyPI license PRs Welcome Discord Server Link

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 GitHub pages. 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

onnxsim-0.6.1.dev0-cp312-abi3-win_amd64.whl (1.6 MB view details)

Uploaded CPython 3.12+Windows x86-64

onnxsim-0.6.1.dev0-cp312-abi3-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (2.9 MB view details)

Uploaded CPython 3.12+manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

onnxsim-0.6.1.dev0-cp312-abi3-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl (2.6 MB view details)

Uploaded CPython 3.12+manylinux: glibc 2.26+ ARM64manylinux: glibc 2.28+ ARM64

onnxsim-0.6.1.dev0-cp310-cp310-win_amd64.whl (1.6 MB view details)

Uploaded CPython 3.10Windows x86-64

onnxsim-0.6.1.dev0-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (2.9 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

onnxsim-0.6.1.dev0-cp310-cp310-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl (2.6 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.26+ ARM64manylinux: glibc 2.28+ ARM64

File details

Details for the file onnxsim-0.6.1.dev0-cp312-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for onnxsim-0.6.1.dev0-cp312-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 6042dc925cd5ea0d63df23a3154b1ab0723c98275adb44864f08977d59310ed0
MD5 f2b47c801a09c340a6b74554da10fb18
BLAKE2b-256 c2f4b0e889657577743b597378e88a6ea1c5d9ca13bbdbbc61a0651f85ace551

See more details on using hashes here.

Provenance

The following attestation bundles were made for onnxsim-0.6.1.dev0-cp312-abi3-win_amd64.whl:

Publisher: build-and-test.yml on onnxsim/onnxsim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file onnxsim-0.6.1.dev0-cp312-abi3-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for onnxsim-0.6.1.dev0-cp312-abi3-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 e916b23ab5d9732949e40382a6a1b276795ca91d3b07ec79b9d441f74825ee9c
MD5 631f45144b01fc714a9a5768b5c74a2b
BLAKE2b-256 e14b32f46fd18f948ef7df029ab7cada0a06fc7b3418714af6ef94293abac4d1

See more details on using hashes here.

Provenance

The following attestation bundles were made for onnxsim-0.6.1.dev0-cp312-abi3-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: build-and-test.yml on onnxsim/onnxsim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file onnxsim-0.6.1.dev0-cp312-abi3-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for onnxsim-0.6.1.dev0-cp312-abi3-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 8f7b15b7e00a8e4f138b9cd2de5cc5846ba3a9a9a82e2c8e71e6a851ba1ec238
MD5 d82c6ade2f1b9513acecf86be84fc002
BLAKE2b-256 414192d0c6d08f7e01121527174cec5c0149aaa46b89cb0a69d49121d66d8dbb

See more details on using hashes here.

Provenance

The following attestation bundles were made for onnxsim-0.6.1.dev0-cp312-abi3-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl:

Publisher: build-and-test.yml on onnxsim/onnxsim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file onnxsim-0.6.1.dev0-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for onnxsim-0.6.1.dev0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 967757aae2316c0b25a85a6b35cf81bbbfac295f2e34f0ed09672b7a94b0705e
MD5 a11308325689d6f0362bcad788477819
BLAKE2b-256 3a863e6655c2d0d08b428761a1ddef5a2833d3bbcf18b5397804032c06da5cc7

See more details on using hashes here.

Provenance

The following attestation bundles were made for onnxsim-0.6.1.dev0-cp310-cp310-win_amd64.whl:

Publisher: build-and-test.yml on onnxsim/onnxsim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file onnxsim-0.6.1.dev0-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for onnxsim-0.6.1.dev0-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 b405ca1c4051e23d6c242b2fddd6665823e7932a4ed6b9f5131ed7d498382f03
MD5 4ee32ae143be0fd5b815ba0561b72e85
BLAKE2b-256 ff6a5632b89cfa579bc2e7a059fb30e851549f6e42b5960443798a79a41561a9

See more details on using hashes here.

Provenance

The following attestation bundles were made for onnxsim-0.6.1.dev0-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: build-and-test.yml on onnxsim/onnxsim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file onnxsim-0.6.1.dev0-cp310-cp310-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for onnxsim-0.6.1.dev0-cp310-cp310-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 0fc3e8bf2103240ad4359d6ce94fa579b41a3817b99c246abb280cab083f5637
MD5 26ecace5b0c13e8c28a27d6dcce480ab
BLAKE2b-256 44230eb1156d5d19674a344fb6d1f55eb6712ee81e681df6a93b83c33a423b47

See more details on using hashes here.

Provenance

The following attestation bundles were made for onnxsim-0.6.1.dev0-cp310-cp310-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl:

Publisher: build-and-test.yml on onnxsim/onnxsim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page