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


Download files

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

Source Distribution

onnxsim-0.4.36.tar.gz (21.0 MB view details)

Uploaded Source

Built Distributions

onnxsim-0.4.36-cp311-cp311-win_amd64.whl (1.3 MB view details)

Uploaded CPython 3.11 Windows x86-64

onnxsim-0.4.36-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.3 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

onnxsim-0.4.36-cp311-cp311-macosx_10_15_universal2.whl (3.5 MB view details)

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

onnxsim-0.4.36-cp310-cp310-win_amd64.whl (1.3 MB view details)

Uploaded CPython 3.10 Windows x86-64

onnxsim-0.4.36-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.3 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

onnxsim-0.4.36-cp310-cp310-macosx_10_15_universal2.whl (3.5 MB view details)

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

onnxsim-0.4.36-cp39-cp39-win_amd64.whl (1.3 MB view details)

Uploaded CPython 3.9 Windows x86-64

onnxsim-0.4.36-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.3 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

onnxsim-0.4.36-cp39-cp39-macosx_10_15_universal2.whl (3.5 MB view details)

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

onnxsim-0.4.36-cp38-cp38-win_amd64.whl (1.3 MB view details)

Uploaded CPython 3.8 Windows x86-64

onnxsim-0.4.36-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.3 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

onnxsim-0.4.36-cp38-cp38-macosx_10_15_universal2.whl (3.5 MB view details)

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

onnxsim-0.4.36-cp37-cp37m-win_amd64.whl (1.3 MB view details)

Uploaded CPython 3.7m Windows x86-64

onnxsim-0.4.36-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.3 MB view details)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

File details

Details for the file onnxsim-0.4.36.tar.gz.

File metadata

  • Download URL: onnxsim-0.4.36.tar.gz
  • Upload date:
  • Size: 21.0 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.18

File hashes

Hashes for onnxsim-0.4.36.tar.gz
Algorithm Hash digest
SHA256 6e0ee9d6d4a83042bdef7319fbe58352d9fda5f253386be2b267c7c27f0638ee
MD5 9082117772abf4598364d63e59c4672d
BLAKE2b-256 ce9ef34238413ebeda9a3a8802feeaa5013934455466b9ab390b48ad9c7e184f

See more details on using hashes here.

File details

Details for the file onnxsim-0.4.36-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for onnxsim-0.4.36-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 91fb32def04f2f89d5f76527c852332366957752e5e61ac25be0b2d7bb410f89
MD5 30555dbc99267f6c30d2fe84c9540deb
BLAKE2b-256 c15caa277f45b0d8253027d1ce3269952e116b476985e5fb497e00ebd917ce29

See more details on using hashes here.

File details

Details for the file onnxsim-0.4.36-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for onnxsim-0.4.36-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 fa7596e6b806ed19077f7652788a50ee576c172b4d16d421f0593aef1a6fa4c4
MD5 1e24ddc01e915354be6077879919eeb9
BLAKE2b-256 db9422aab761b3d416bce02020d9ca98dc692427c2717b0325952e30ce41f83b

See more details on using hashes here.

File details

Details for the file onnxsim-0.4.36-cp311-cp311-macosx_10_15_universal2.whl.

File metadata

File hashes

Hashes for onnxsim-0.4.36-cp311-cp311-macosx_10_15_universal2.whl
Algorithm Hash digest
SHA256 150b9a3a409af2f3161af3fecda2113e0e6e296fb015b5205a9ddf645765acad
MD5 c29864f09ee90bf93413f14405982a1c
BLAKE2b-256 2ea3f6cad8499f375a3acc8a7837721f82860244656cf62984cf80ebe187cc68

See more details on using hashes here.

File details

Details for the file onnxsim-0.4.36-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for onnxsim-0.4.36-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 f92bec8c6c0d4f8463e10021277711d2faac900e4eb890238001b3eadb5c03bc
MD5 caf0f4c427d4d5ccf3e171b4ae4a40c0
BLAKE2b-256 0ec56c93b354684b3fc4b520a23be3e4db5870b35dde9e9e2a1f41018ba369e8

See more details on using hashes here.

File details

Details for the file onnxsim-0.4.36-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for onnxsim-0.4.36-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ce87837f8975beebdcc98cc01d6d13e84b10900eb2c14035ce1066c3d670d96d
MD5 4be351e6160a75e303940aa1a6f2eeaa
BLAKE2b-256 d96e80c77b5c6ec079994295e6e685097fa42732a1e7c5a22fe9c5c4ca1aac74

See more details on using hashes here.

File details

Details for the file onnxsim-0.4.36-cp310-cp310-macosx_10_15_universal2.whl.

File metadata

File hashes

Hashes for onnxsim-0.4.36-cp310-cp310-macosx_10_15_universal2.whl
Algorithm Hash digest
SHA256 7498e7b9584c4b354b455564dfba66d460ce2c205b71dae169cfa9b6704e03fd
MD5 b576df1aa87a40d1ba980ae23c2b5c19
BLAKE2b-256 9b550a0a248636cccccb7d4ed9189446e00017f411d0e13d1dd4af419ee4d529

See more details on using hashes here.

File details

Details for the file onnxsim-0.4.36-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: onnxsim-0.4.36-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 1.3 MB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.18

File hashes

Hashes for onnxsim-0.4.36-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 1a23f32bc6ab3829ee69d9b737e4cfeb457d91720c4040788dabbc88e11dfacc
MD5 5703fe9086804fb4a025b99840b39664
BLAKE2b-256 adc416260e99ed9f82e33d4061a953d927bd174e0c67868e4e15ae5335cd1713

See more details on using hashes here.

File details

Details for the file onnxsim-0.4.36-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for onnxsim-0.4.36-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e937abb8e20a6609f27ae19639d21dc5e8621c4a5e44ebbafab9292451f75497
MD5 ac25e5b1299802f28ab7729dccf2e5be
BLAKE2b-256 0cc321c4e4a17c0dae64c0f0c02186baa198d7d5563af9a27361d2fdd75a8e04

See more details on using hashes here.

File details

Details for the file onnxsim-0.4.36-cp39-cp39-macosx_10_15_universal2.whl.

File metadata

File hashes

Hashes for onnxsim-0.4.36-cp39-cp39-macosx_10_15_universal2.whl
Algorithm Hash digest
SHA256 6ae38cb9a23b759202d8ecc6c25392a2cecd156499b036fc48720d048833bece
MD5 5f0ca276d57517ecb47867e9315ddf2f
BLAKE2b-256 203b2230a57ee0425649b16bf8ba0a17400c9aa003f049052833055ff3bcbfa8

See more details on using hashes here.

File details

Details for the file onnxsim-0.4.36-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: onnxsim-0.4.36-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 1.3 MB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.18

File hashes

Hashes for onnxsim-0.4.36-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 f17c2b81ad97e7bb91f6f049282b7d9bc8e4dc93d23d2bdf59e8bf3f5b3e63c7
MD5 919c464a846627b36e333f7c13b32030
BLAKE2b-256 2cbc6f4248187a13cec1782854d4fb339c32d8976821fb74694d809f7a4bb3eb

See more details on using hashes here.

File details

Details for the file onnxsim-0.4.36-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for onnxsim-0.4.36-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 19c881a9c9af4e7238c856da8b677e158f9a74f3eeaedbdb447fb109a4d08ce3
MD5 82f288db317ef799fd9eed2f51bb4437
BLAKE2b-256 30e625dd3bd1dbb30561662d8c2e9e5aa014a2e704d1a81053fe9e935d5fe6be

See more details on using hashes here.

File details

Details for the file onnxsim-0.4.36-cp38-cp38-macosx_10_15_universal2.whl.

File metadata

File hashes

Hashes for onnxsim-0.4.36-cp38-cp38-macosx_10_15_universal2.whl
Algorithm Hash digest
SHA256 369815cd292ae1b3cb37d5692428abf69ed3917c7794d895155c4d4d1f8b374e
MD5 0c4560a86d88c9c29b89b0cd66effd52
BLAKE2b-256 3a933b27939bcbde20d04088770b6cda2e0c4211335732329aa7cd36f78f89ca

See more details on using hashes here.

File details

Details for the file onnxsim-0.4.36-cp37-cp37m-win_amd64.whl.

File metadata

  • Download URL: onnxsim-0.4.36-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 1.3 MB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.18

File hashes

Hashes for onnxsim-0.4.36-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 7c0aa3de159fc9c4560b62ddccc0d1146fef035aeba5eb3797f6550889a2f96a
MD5 479a83fc4e3fcc6340c68e5c9dea8b84
BLAKE2b-256 e3c079fc142a53a27e477f0f82d2311600e343a6da700e5407af03dc389ce8b9

See more details on using hashes here.

File details

Details for the file onnxsim-0.4.36-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for onnxsim-0.4.36-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9c616b1e48e92d916e762bae405563f5b2fd8d0718fbbafef2ceb81570202716
MD5 40e3e2fe494e26101d6e2b246d5d312f
BLAKE2b-256 03c0e64f4d92bee6596620417f1d388bee7a73ecc8e0caf173c844d7bd8e22d0

See more details on using hashes here.

Supported by

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