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.24.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.24-cp311-cp311-win_amd64.whl (1.2 MB view details)

Uploaded CPython 3.11Windows x86-64

onnx_simplifier-0.4.24-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.24-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.24-cp310-cp310-win_amd64.whl (1.2 MB view details)

Uploaded CPython 3.10Windows x86-64

onnx_simplifier-0.4.24-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.24-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.24-cp39-cp39-win_amd64.whl (1.2 MB view details)

Uploaded CPython 3.9Windows x86-64

onnx_simplifier-0.4.24-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.24-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.24-cp38-cp38-win_amd64.whl (1.2 MB view details)

Uploaded CPython 3.8Windows x86-64

onnx_simplifier-0.4.24-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.24-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.24-cp37-cp37m-win_amd64.whl (1.2 MB view details)

Uploaded CPython 3.7mWindows x86-64

onnx_simplifier-0.4.24-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.24.tar.gz.

File metadata

  • Download URL: onnx-simplifier-0.4.24.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.24.tar.gz
Algorithm Hash digest
SHA256 634d7f62787d64b369c6dfc48fee8bd9fe6ce62f87c670bd34844b2f6940a94f
MD5 81095e9c6f0acdfcf159fa65152085bb
BLAKE2b-256 a442899654fae8596260e151375e2b6862e01583853a0b0c135478d59d2a891f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for onnx_simplifier-0.4.24-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 8eff0a8c77b201f5544a38d65f16da50b19422b925d59b062c18592d225fe5ea
MD5 5ea2e1ce9d57865b533a95a0898f6eb3
BLAKE2b-256 dc428feba534633bbbc095ca01aacb5dcb6fde6219730b1c401f40fa7ef5ba22

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for onnx_simplifier-0.4.24-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 248d8d1c174bac6b8e1acfd43e7da110e367df22e2ea05aeead2342aad4c6372
MD5 a76e6c508c63c21468b027a964f68838
BLAKE2b-256 e913d6f340576e711208ffe7c77bf44adff7d69ca8496fe8372e1089cc1dc6c0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for onnx_simplifier-0.4.24-cp311-cp311-macosx_10_15_universal2.whl
Algorithm Hash digest
SHA256 95fe2259064d7b1986d2567187892b5d98444eba9faa9685735af844184e2fe0
MD5 c8bd4aa95b113ab154409785196408b3
BLAKE2b-256 d59c333d19a131fa846ff178a8bb30cc6425a2783c1c35a95595a643bbad4939

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for onnx_simplifier-0.4.24-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 548232683c6d29e4d672257bdad34ab2fdda400ba5611a98f04a3bb8be8102fe
MD5 cfa9efb8287b00c994a1950d351239c3
BLAKE2b-256 f5ca57f70def20a643ce93564bb03f23922f288f76b5e417cc8d2be2d10a870d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for onnx_simplifier-0.4.24-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b0a0b0ecb2e03be1100d40378ff395c09c3a65f6cb3cecce66d71d36eaa3382d
MD5 e85a51c6eee660d9cd473911370f7ba6
BLAKE2b-256 18920a8f658c89890d3533d7e50ca678ff09db47113a22842864d73853e12bff

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for onnx_simplifier-0.4.24-cp310-cp310-macosx_10_15_universal2.whl
Algorithm Hash digest
SHA256 f92bd86496d8becaf6b7eb791d6e822ebd52073fe5ae197385a9282c0dd6c651
MD5 b7b7160349970b44456d9159e9cb82d5
BLAKE2b-256 fb96548d4b619743220968f24b1ea01a010f7899f0f3bb406f64376d807bbe1f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for onnx_simplifier-0.4.24-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 103bbe6d7978c06fb5261629ee7c4148b61fb03ad96ebe8d713d647e6992833a
MD5 465faad3df6357b1ee2e91083687ff9b
BLAKE2b-256 76207cfc2cacb35e902a10171832811739764e7ac2ed5460eb73511384f0ba48

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for onnx_simplifier-0.4.24-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1a1bb85ca6fed4c2502419a8960db397836f0dcab62468686c24489a55a28baa
MD5 0b634521c924667adf3509ba74606ad5
BLAKE2b-256 d78e7bfcbc6eeae57a955fb36d865f2732e2cade82e37235477787839f7385d5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for onnx_simplifier-0.4.24-cp39-cp39-macosx_10_15_universal2.whl
Algorithm Hash digest
SHA256 8a14296220c8168b028d37a357b89162dde7efc4dc0abecd69a55afd49aedb68
MD5 e6b064a00f02df4944e959f889470409
BLAKE2b-256 41c0c7b18ea8c0142d9c3436593fe25458504b8dc7dbe18d9152b1bae3bcd62f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for onnx_simplifier-0.4.24-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 0b57767ba997443742bffadd0a43ca59844b66c05d049f2920d8b36bc0aad945
MD5 ca4284082335d3f4f1076903e12fad54
BLAKE2b-256 45bd057f5714016ceed30d11768d4d9e1cc16217f93ca19ed4fa7fd6c2a79fce

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for onnx_simplifier-0.4.24-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2b28b44736c5760e4598653450a5c175e60ad1d3683abc7a88cb73be28312231
MD5 8af321e3d33e66cdec44a8ff407a8dc1
BLAKE2b-256 4b50e1d8aba411077a88b1670d0861b48638d41258a0b096e5379c19c7c6c363

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for onnx_simplifier-0.4.24-cp38-cp38-macosx_10_15_universal2.whl
Algorithm Hash digest
SHA256 6ea6cb08f251bc1daf04e894bfae9f25463bbce01deb0b1bdd20b90e5877f7c9
MD5 6320fb50682fc8d9695d72e6b7078b83
BLAKE2b-256 dfe4253f68ccaed4fcee336d933b4697c4851e73252f866da2a142c35c32b199

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for onnx_simplifier-0.4.24-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 f10fb2e8cdde2b6bb07a145ac193bc04912aaa647d8e6b2600fd058006082683
MD5 53ffbedfaa217f0677c5346c9918917c
BLAKE2b-256 4d7d9899408eb383bef522b6aa0d710fdcf973ddb6cc00643afd24927276898a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for onnx_simplifier-0.4.24-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ca8311e133cf2c84bdd3980d0637f3bb2635e73cb09b3c97578f6dbfa195eac3
MD5 522e23156f7b743bb00618166cf47d48
BLAKE2b-256 ec076ae052a4cd546bd2c2e77918edafc590d6e1507acd069917f3731582269d

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