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

This version

0.4.7

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.7.tar.gz (18.1 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.7-cp310-cp310-win_amd64.whl (1.2 MB view details)

Uploaded CPython 3.10Windows x86-64

onnx_simplifier-0.4.7-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

onnx_simplifier-0.4.7-cp310-cp310-macosx_10_15_universal2.whl (3.1 MB view details)

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

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

Uploaded CPython 3.9Windows x86-64

onnx_simplifier-0.4.7-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

onnx_simplifier-0.4.7-cp39-cp39-macosx_10_15_universal2.whl (3.1 MB view details)

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

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

Uploaded CPython 3.8Windows x86-64

onnx_simplifier-0.4.7-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

onnx_simplifier-0.4.7-cp38-cp38-macosx_10_15_universal2.whl (3.1 MB view details)

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

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

Uploaded CPython 3.7mWindows x86-64

onnx_simplifier-0.4.7-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.7mmanylinux: glibc 2.17+ x86-64

onnx_simplifier-0.4.7-cp36-cp36m-win_amd64.whl (1.2 MB view details)

Uploaded CPython 3.6mWindows x86-64

onnx_simplifier-0.4.7-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.6mmanylinux: glibc 2.17+ x86-64

File details

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

File metadata

  • Download URL: onnx-simplifier-0.4.7.tar.gz
  • Upload date:
  • Size: 18.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for onnx-simplifier-0.4.7.tar.gz
Algorithm Hash digest
SHA256 2284ff775e000749ec1128cf89b1c10978c623389df8cf167e7e8ea4db99ccaf
MD5 1f23112156d004a4dfb1d36fa55b615a
BLAKE2b-256 2026146003f1b34a28ce8355072d918d3c19ec974b636bf286d241ffa6e4906e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for onnx_simplifier-0.4.7-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 7f69f2f58883dcbf3219448b4802aa7ed5c96c96ff8e7b4e1b01a3ab239e912a
MD5 10d1089722971646cad409bd3a9d5442
BLAKE2b-256 5237cd8749776eb2d0f2ea6b3037efe1e9cd5c0ad0331b0431c7445f3ac8b960

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for onnx_simplifier-0.4.7-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0915937c4f82dda89a97156541963af9024464da8bf87734f281b43c85e5d669
MD5 480a378be8dc671cc39d227bd0a1ac9f
BLAKE2b-256 7fc8440134ae49bd06673f986c649f0d0df82ed8b40f3c39a803b6fa8c772151

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for onnx_simplifier-0.4.7-cp310-cp310-macosx_10_15_universal2.whl
Algorithm Hash digest
SHA256 18ce686c516b4ac55d1f7c137c653fe0c529e65f2735fcad0d9d0108c697c840
MD5 f131b490d73cdc80715f81b42fe1d7d9
BLAKE2b-256 98d88646b5063be6f8352a82537b2771ea97e25aa716c9f86bf85554b1ab632e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for onnx_simplifier-0.4.7-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 f1bde9973876bfa1397c4c1444e2f29cf53a90996f27140cd41b4650de24f66d
MD5 74ace5f8f8faf2b860ced45e455e66be
BLAKE2b-256 35f99ef1abd0c1442bf440b10c832bd46e64259bcb4a6b2519a2e1e0eeb6f772

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for onnx_simplifier-0.4.7-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ec9b00a20a9271bba2f6aea59296c227e7addf61f2096677da939199b7abab02
MD5 55d315a8a4b74665db46f5710388638b
BLAKE2b-256 a9f0c3261f10f63b22bac1e0093689e152f273c375c6825a68c5623f706666f2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for onnx_simplifier-0.4.7-cp39-cp39-macosx_10_15_universal2.whl
Algorithm Hash digest
SHA256 4e37009ea76030c8376094adb5e4f74aa149df5388ebb85b4dc14c4f0418870b
MD5 b3230a253c62aba845728bcb978c6509
BLAKE2b-256 dee276bfb85debadaa64e29bbe120efd53f92be247bbe87741fe6247081bcf11

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for onnx_simplifier-0.4.7-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 f82fbf0b7e2f16dac22857b1886678fe8c579383ecfb50421dd20df72538ea62
MD5 cda622a43701b8282ac2329a00c28374
BLAKE2b-256 a89c95cdd439dd2412e050c7630713a4b5ec1e16a214f101f6b99aa8b7c5535f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for onnx_simplifier-0.4.7-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6c65b321e760112b0b73d0c578faf95313baa9ea99c7b954faecebb911d2b1c8
MD5 0fc94f20698bfd31a9a4bf76599767a2
BLAKE2b-256 a061bae80674d69fc2a557f8b139f14d84cd725a56794e0748c7ab34264131a3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for onnx_simplifier-0.4.7-cp38-cp38-macosx_10_15_universal2.whl
Algorithm Hash digest
SHA256 65f28348934a3012481c8713ff211a7b40ef41898e620a6b359f95bc0786abb7
MD5 3975aa61ba2f2cb3f2c556b63f54f21e
BLAKE2b-256 a0f8b3d316f5f4e1ed3733d6988a3398a67dcbb7ebb72d6751e44595f9e42fe7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for onnx_simplifier-0.4.7-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 1bfa705ac63af9d0c6f9ca7adef2ab3c918f2acd79fd3f8310fe4e431f4822e3
MD5 72ab0e3e01335257f11a4f957f15b761
BLAKE2b-256 4c0ab27c888adc01ffbcf86270782695e2468354cd0e808ed7ce1c46afe18fc3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for onnx_simplifier-0.4.7-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ddc6626d497b85cc80af33f1b44ae5886073bf9c6121f8ec41e463d8ec4feb8e
MD5 ab85d3062a96b693a4e62da832d7be73
BLAKE2b-256 6e77101d9eb56e324a52aa304e4feb52054cf9763839276bf629963c58055f85

See more details on using hashes here.

File details

Details for the file onnx_simplifier-0.4.7-cp36-cp36m-win_amd64.whl.

File metadata

File hashes

Hashes for onnx_simplifier-0.4.7-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 a02e4ab590edbc09544d31fe1a668defc105ebeaf506f74054c8c07777822bca
MD5 fca29b6f5731fa51d32440e9550017d1
BLAKE2b-256 89ac39d5f2062c2877f50f0d3299e27dfafc87f70a03c53e05c9dc71e93bb20e

See more details on using hashes here.

File details

Details for the file onnx_simplifier-0.4.7-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for onnx_simplifier-0.4.7-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 3e16173cac0292c2dc5e40e4ada61fab3ee1e4b439ec6be547438aa0270e50b1
MD5 0fa337395fc905bb0c14d6a2c57c3f0b
BLAKE2b-256 e0a87293de0cfa1f93059eb8fec042307f1cfa0ce415ed4a50bca0d322a9fe5d

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