Skip to main content

Simplify your ONNX model

Project description

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.

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

onnx-simplifier-0.4.21.tar.gz (19.2 MB view details)

Uploaded Source

Built Distributions

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

onnx_simplifier-0.4.21-cp311-cp311-win_amd64.whl (1.2 MB view details)

Uploaded CPython 3.11Windows x86-64

onnx_simplifier-0.4.21-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.1 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

onnx_simplifier-0.4.21-cp311-cp311-macosx_10_15_universal2.whl (3.3 MB view details)

Uploaded CPython 3.11macOS 10.15+ universal2 (ARM64, x86-64)

onnx_simplifier-0.4.21-cp310-cp310-win_amd64.whl (1.2 MB view details)

Uploaded CPython 3.10Windows x86-64

onnx_simplifier-0.4.21-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.1 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

onnx_simplifier-0.4.21-cp310-cp310-macosx_10_15_universal2.whl (3.3 MB view details)

Uploaded CPython 3.10macOS 10.15+ universal2 (ARM64, x86-64)

onnx_simplifier-0.4.21-cp39-cp39-win_amd64.whl (1.2 MB view details)

Uploaded CPython 3.9Windows x86-64

onnx_simplifier-0.4.21-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.1 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

onnx_simplifier-0.4.21-cp39-cp39-macosx_10_15_universal2.whl (3.3 MB view details)

Uploaded CPython 3.9macOS 10.15+ universal2 (ARM64, x86-64)

onnx_simplifier-0.4.21-cp38-cp38-win_amd64.whl (1.2 MB view details)

Uploaded CPython 3.8Windows x86-64

onnx_simplifier-0.4.21-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.1 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

onnx_simplifier-0.4.21-cp38-cp38-macosx_10_15_universal2.whl (3.3 MB view details)

Uploaded CPython 3.8macOS 10.15+ universal2 (ARM64, x86-64)

onnx_simplifier-0.4.21-cp37-cp37m-win_amd64.whl (1.2 MB view details)

Uploaded CPython 3.7mWindows x86-64

onnx_simplifier-0.4.21-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.1 MB view details)

Uploaded CPython 3.7mmanylinux: glibc 2.17+ x86-64

File details

Details for the file onnx-simplifier-0.4.21.tar.gz.

File metadata

  • Download URL: onnx-simplifier-0.4.21.tar.gz
  • Upload date:
  • Size: 19.2 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for onnx-simplifier-0.4.21.tar.gz
Algorithm Hash digest
SHA256 10c6ced32b12b1dbb24c7af61af372b89518d46711f3bb8e55e4f3cb2743e72f
MD5 d5108023debb43d4ccfa02a3fd643a21
BLAKE2b-256 8641d2ab73deedb4c27d3ab73416a5a4b17611339ae59f528d67170efdcb01ec

See more details on using hashes here.

File details

Details for the file onnx_simplifier-0.4.21-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for onnx_simplifier-0.4.21-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 f9cc0d5a897abe82cbb4a574807d5f07c9844aad0dbedff5b7fcd7fb132a1881
MD5 75e1f6461dfd0c0664f6126782d133a9
BLAKE2b-256 a783b9eb6b25d0ab6b7de7653522ac71924a805c98e6b59afaf723ebc7ba2037

See more details on using hashes here.

File details

Details for the file onnx_simplifier-0.4.21-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for onnx_simplifier-0.4.21-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a6ae16fff3f65fee04d6d8a126bc43cece1d907797c5046268247d10c4efe854
MD5 9b3ea9b0399112811c7e84ca27ca5e3a
BLAKE2b-256 6a6d6a1f3272bc5d9fa5e29970578b1496139e01b5eadcfa1fae7d50967f4308

See more details on using hashes here.

File details

Details for the file onnx_simplifier-0.4.21-cp311-cp311-macosx_10_15_universal2.whl.

File metadata

File hashes

Hashes for onnx_simplifier-0.4.21-cp311-cp311-macosx_10_15_universal2.whl
Algorithm Hash digest
SHA256 26b17a7755b246bc3a49cab688691e0c408961fdd8d0ab4fece4271a878624c0
MD5 5d7ea1a6c2694c43a6098dcfb2751359
BLAKE2b-256 c55bb8e283ce4646b68f502942e19306ee7221956f535a85953d1b0d6951e6cb

See more details on using hashes here.

File details

Details for the file onnx_simplifier-0.4.21-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for onnx_simplifier-0.4.21-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 2fd1b9dc69f77522ac4135c51cea94d54aa6b441e674e91f96d6e6efb3dfac19
MD5 361744144df4bd3acfe3bce3b6d43f38
BLAKE2b-256 ee8947ec0d76d30313da506249c9f6edacf1bebad4a62951267b09143617dd72

See more details on using hashes here.

File details

Details for the file onnx_simplifier-0.4.21-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for onnx_simplifier-0.4.21-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 19a993fb057fed0a74252052cf0ca02d5c1019481c013a54eae78791c9afe69d
MD5 6dfa0456cd81dd246c3e608132480c79
BLAKE2b-256 744fb3aedf2733a51fa56c6c84e1fc22a0074cb29a76f2b7108cb3b3dba0fd91

See more details on using hashes here.

File details

Details for the file onnx_simplifier-0.4.21-cp310-cp310-macosx_10_15_universal2.whl.

File metadata

File hashes

Hashes for onnx_simplifier-0.4.21-cp310-cp310-macosx_10_15_universal2.whl
Algorithm Hash digest
SHA256 96e9a6db7b9b00b51b6b24efe85939f7c3fb1baecfa4fc864c48d6519c2cf948
MD5 2bee77d3ddb57b4ccbadaf3d22b3e089
BLAKE2b-256 1fa735e6f842846038f54c146c478dad6a71ddd7c73ceb1048f663cd7d2bfd5c

See more details on using hashes here.

File details

Details for the file onnx_simplifier-0.4.21-cp39-cp39-win_amd64.whl.

File metadata

File hashes

Hashes for onnx_simplifier-0.4.21-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 0a5005417dba0b8f3a6bbbc5d46888cff502001c682faf6f17e3ee223df8d3f0
MD5 a29585c7a55314bd978b700659072637
BLAKE2b-256 926c0e8c1c1f50de42e92ca86f1aceb9d1be6945720abec3c9c454a2344334dc

See more details on using hashes here.

File details

Details for the file onnx_simplifier-0.4.21-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for onnx_simplifier-0.4.21-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0050637a44f454d886448f8c506646bb46fc0e76ff55110d4f329594028ac681
MD5 2e645791d4d67cf01272f06c905cbaad
BLAKE2b-256 baace18c7eeb4c2ab2073540226fbef06b9b938b328f9ac07513e8c85459ac1e

See more details on using hashes here.

File details

Details for the file onnx_simplifier-0.4.21-cp39-cp39-macosx_10_15_universal2.whl.

File metadata

File hashes

Hashes for onnx_simplifier-0.4.21-cp39-cp39-macosx_10_15_universal2.whl
Algorithm Hash digest
SHA256 f325101b3dd043571d744747a3c6de680b8d0da2caa5df7aafd28a2396aedbb0
MD5 60e3b1c1ef3c94b6c28151f73a63b2e3
BLAKE2b-256 b8e80b52a648f42d39299655afdc08c8da01f3464b42c93b87f19136712c6993

See more details on using hashes here.

File details

Details for the file onnx_simplifier-0.4.21-cp38-cp38-win_amd64.whl.

File metadata

File hashes

Hashes for onnx_simplifier-0.4.21-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 a054176296f1ef6f7c04c8f55108121a8d516578e11bd36237933f8af1e52585
MD5 81081510633c2e70fcc16809f57cc6ff
BLAKE2b-256 b9b5e0892c72946e3e6fd1025a6b948fda4553f7385cacf9e1582bcf3bbee91d

See more details on using hashes here.

File details

Details for the file onnx_simplifier-0.4.21-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for onnx_simplifier-0.4.21-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 578b8e3d822e77c957b18c45e09a6c014a24bda080b79e206659958a84ed6954
MD5 e3ca8a991d5fcbae7d1e4f0613a91c7b
BLAKE2b-256 8452fb14d45b08cf221b874a687fd57b508c98f47d865ea0ac36cd793af265d3

See more details on using hashes here.

File details

Details for the file onnx_simplifier-0.4.21-cp38-cp38-macosx_10_15_universal2.whl.

File metadata

File hashes

Hashes for onnx_simplifier-0.4.21-cp38-cp38-macosx_10_15_universal2.whl
Algorithm Hash digest
SHA256 a69f5f79c55ac61932e6a7224155728374a827d7390a30e0382ce9ffb9388128
MD5 a946a98bc0b6c25c8213db073900dbc8
BLAKE2b-256 90d0523bbcb8a565143c02b3f133d184e0dfc2ca35d276544dd989083dfcec43

See more details on using hashes here.

File details

Details for the file onnx_simplifier-0.4.21-cp37-cp37m-win_amd64.whl.

File metadata

File hashes

Hashes for onnx_simplifier-0.4.21-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 228e773ddc7c50028c8b67d7ff05685d059a954ced8d3381c3637fd4af68af80
MD5 ade8489c50403f1534bf08a00cadd68e
BLAKE2b-256 1ece9ee2215b894aeb487c2bfff016b80b5725acbf43ba238151adff7cbb5954

See more details on using hashes here.

File details

Details for the file onnx_simplifier-0.4.21-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for onnx_simplifier-0.4.21-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 db10452a593815647d14f376f72faf02107138933751a94b98dd837baf7b8b5c
MD5 3ab779f0e8cf9b3c5c5852605bce9a82
BLAKE2b-256 26641ad9f27ff7d8101a6ad4ff0974fc3312250129146c1939e8d1bb5276d23f

See more details on using hashes here.

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