Skip to main content

ONNX Simplifier

PyPI version PyPI pyversions PyPI license PRs Welcome Discord

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

Custom operators

Models that contain custom operators, such as TensorRT plugins (BatchedNMS_TRT, EfficientNMS_TRT, ...), are supported. onnxsim keeps these ops unchanged and simplifies the rest of the graph around them. This works whether the custom op lives in a vendor-specific domain (e.g. TRT) or in the default ONNX domain, so you no longer need to manually move it into a custom domain to get past validation (issues #107 and #220).

If you describe your custom operator to ONNX with onnx.defs.register_schema, onnxsim picks that schema up automatically: onnxsim links its own copy of ONNX, so its operator registry is separate from the onnx Python module's, and every simplify call imports the schemas you registered into onnxsim's registry before validating the model (issue #326). You can also trigger the import explicitly with onnxsim.import_onnx_schemas(), or turn the automatic import off with onnxsim.simplify(model, import_custom_schemas=False) (CLI: --skip-schema-import).

import onnx
import onnxsim

# Teach ONNX about your custom operator.
onnx.defs.register_schema(my_op_schema)

# simplify() imports the schema into onnxsim automatically.
model_simp, check_ok = onnxsim.simplify(model)

If a registered schema also has a type/shape-inference function (set via onnx.defs.OpSchema.set_type_and_shape_inference_function), onnxsim registers a trampoline that calls it back through onnx.shape_inference.infer_node_outputs during simplification, so the custom operator's output shapes are inferred too. Custom operators without an inference function are still imported; shape inference simply flows past them.

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.

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.7.0.tar.gz (3.0 MB view details)

Uploaded Source

Built Distributions

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

onnxsim-0.7.0-cp314-cp314t-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.14tmanylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

onnxsim-0.7.0-cp312-abi3-win_amd64.whl (3.1 MB view details)

Uploaded CPython 3.12+Windows x86-64

onnxsim-0.7.0-cp312-abi3-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (2.7 MB view details)

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

onnxsim-0.7.0-cp312-abi3-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl (2.4 MB view details)

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

onnxsim-0.7.0-cp312-abi3-macosx_11_0_arm64.whl (2.6 MB view details)

Uploaded CPython 3.12+macOS 11.0+ ARM64

onnxsim-0.7.0-cp311-cp311-win_amd64.whl (3.1 MB view details)

Uploaded CPython 3.11Windows x86-64

onnxsim-0.7.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (2.7 MB view details)

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

onnxsim-0.7.0-cp311-cp311-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl (2.4 MB view details)

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

onnxsim-0.7.0-cp311-cp311-macosx_11_0_arm64.whl (2.6 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

onnxsim-0.7.0-cp310-cp310-win_amd64.whl (3.1 MB view details)

Uploaded CPython 3.10Windows x86-64

onnxsim-0.7.0-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (2.7 MB view details)

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

onnxsim-0.7.0-cp310-cp310-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl (2.4 MB view details)

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

onnxsim-0.7.0-cp310-cp310-macosx_11_0_arm64.whl (2.6 MB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

File details

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

File metadata

  • Download URL: onnxsim-0.7.0.tar.gz
  • Upload date:
  • Size: 3.0 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for onnxsim-0.7.0.tar.gz
Algorithm Hash digest
SHA256 9ee396257785ce07c5a468ba6cc0a8aba4eee0c6ed73f945deb7dc14b5c892c0
MD5 749db96c84330ce8d5fd0508d02fb8a4
BLAKE2b-256 29d40def0ec1f143963137419fc2cdfc8521724216861abd8ca2cc42f5d257c2

See more details on using hashes here.

Provenance

The following attestation bundles were made for onnxsim-0.7.0.tar.gz:

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.7.0-cp314-cp314t-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for onnxsim-0.7.0-cp314-cp314t-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 477f9c8d9894e144d698a1216d7e94eee6043be6ed4669440063daa6c1616a68
MD5 61aa5faba320fdce9e7b60247d3de1fc
BLAKE2b-256 94d727430466aaff6e97d4ed4d1d335281363167f33ff5bc5726c6a3906e896d

See more details on using hashes here.

Provenance

The following attestation bundles were made for onnxsim-0.7.0-cp314-cp314t-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.7.0-cp312-abi3-win_amd64.whl.

File metadata

  • Download URL: onnxsim-0.7.0-cp312-abi3-win_amd64.whl
  • Upload date:
  • Size: 3.1 MB
  • Tags: CPython 3.12+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for onnxsim-0.7.0-cp312-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 c4cfe7b2df2feeef8daf255537378d9028123d4ef627842ef19db9a2f6bd68e0
MD5 f2da909fd5fad892c7c14ec6b1536f66
BLAKE2b-256 92038c7913bc77c6f29fd122e92381fac85da5918a939efd68f396474072500b

See more details on using hashes here.

Provenance

The following attestation bundles were made for onnxsim-0.7.0-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.7.0-cp312-abi3-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for onnxsim-0.7.0-cp312-abi3-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 0aa412583982475f7a5b45882d46353447b15694f7870737e43c59ed2300f472
MD5 111d50a1c3f1e061be0cc43894c38a59
BLAKE2b-256 8988a771a9e5cfcd0210fdabdb242bb56857bd7beee59ba96f1178c1b702c17d

See more details on using hashes here.

Provenance

The following attestation bundles were made for onnxsim-0.7.0-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.7.0-cp312-abi3-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for onnxsim-0.7.0-cp312-abi3-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 a10fc08b853ce60d3b5b8076b26e2bda1322c1e01c808b8db908e73f9a047a9d
MD5 3dbc1bd03a47ed678120dc93d03f6a64
BLAKE2b-256 8abc743235ea06f3ef7aa55c515ed4e28316fc851d696d1acc686171529fb48f

See more details on using hashes here.

Provenance

The following attestation bundles were made for onnxsim-0.7.0-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.7.0-cp312-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for onnxsim-0.7.0-cp312-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ba0fc54e1f8f0bac1643ff8a3c00516428913ae799ab7717b771cf23baa46680
MD5 6e33f15545eed4e3707948b278ad9776
BLAKE2b-256 f065583263cca89c18abd68192a40a0be25506e67f1bc8d53df7edb1fa3f3a6a

See more details on using hashes here.

Provenance

The following attestation bundles were made for onnxsim-0.7.0-cp312-abi3-macosx_11_0_arm64.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.7.0-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: onnxsim-0.7.0-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 3.1 MB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for onnxsim-0.7.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 c807b308fd7249ee7a31949efb42781a969dc705078d7342d94e3a6f3d482a86
MD5 d09f551f1dbb55bbf21138bfebe4aa7c
BLAKE2b-256 0d065d516c4bdd77391e778206243e6d5538b6edda9084e364b7ab8d24143bbc

See more details on using hashes here.

Provenance

The following attestation bundles were made for onnxsim-0.7.0-cp311-cp311-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.7.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for onnxsim-0.7.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 ad35ab2066e44b5b839b553e59d135268faf3f7331f2c5e575d336128eaba171
MD5 651e8bd13b8aca5a44b05f5ef6326abc
BLAKE2b-256 f4f9de22837a77f0dabfb5075cecbc7c1f5a2d2f2a7f741d9fe33097882edb3b

See more details on using hashes here.

Provenance

The following attestation bundles were made for onnxsim-0.7.0-cp311-cp311-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.7.0-cp311-cp311-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for onnxsim-0.7.0-cp311-cp311-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 f1409944a76b93b2a20c43b9cd7405a36b7482e44a84317d9a6ee9808f7d5561
MD5 9f6a1155d9ab06f8282741d934b31acd
BLAKE2b-256 985e4251b34ce2305b10f818050317b83948ff39a68482bd0e756d2f77560b92

See more details on using hashes here.

Provenance

The following attestation bundles were made for onnxsim-0.7.0-cp311-cp311-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.7.0-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for onnxsim-0.7.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5902ccc6cc3f78249be3136a9e0e4c7c4d4b405cdb9a0390edcd814683b4fe50
MD5 5c0e689c11f86ab3f78b3fe273f74171
BLAKE2b-256 105040fb9043d5161a287e7df88b28c7b813cf893f9b1fa35317c0b9f67e73f7

See more details on using hashes here.

Provenance

The following attestation bundles were made for onnxsim-0.7.0-cp311-cp311-macosx_11_0_arm64.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.7.0-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: onnxsim-0.7.0-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 3.1 MB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for onnxsim-0.7.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 1803fbe03709896a90d0fce9203e1b0ae1e2b6c911126f45851658b39b776fea
MD5 0e127ffd346b6dbbc077e40d4e331370
BLAKE2b-256 c78a125afeed244cbc37427568d56748c0dd72c2a43f9d1805dc170da8fc9777

See more details on using hashes here.

Provenance

The following attestation bundles were made for onnxsim-0.7.0-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.7.0-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for onnxsim-0.7.0-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 26c12ae184d308c75c01644bdb1c53da52cb8e4738e0a2e325f1238fcee63ad9
MD5 4b131abbb120af8ab95547b43ee73fd8
BLAKE2b-256 b6b4dcd6f799f26f62b4242f3f0a2fa3a77b105349af1fc85aa053ce17f7675c

See more details on using hashes here.

Provenance

The following attestation bundles were made for onnxsim-0.7.0-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.7.0-cp310-cp310-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for onnxsim-0.7.0-cp310-cp310-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 a03b84c3e527a9516bf261194beb7f0a545183c53baabd347a2cb815327ecd4f
MD5 d6c62e67373e5d2a32243ccd9f0fa01c
BLAKE2b-256 b631898cbc058b77947aedc060a3fc631f5d15d4e877229a52d60960b27b2021

See more details on using hashes here.

Provenance

The following attestation bundles were made for onnxsim-0.7.0-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.

File details

Details for the file onnxsim-0.7.0-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for onnxsim-0.7.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e5c123acbee25b4d6b7aa8f273afb46d9adda1e2fa32f6d70cc9ea68345d63fb
MD5 cb3ff53cebea2ca45b8d6234f411207b
BLAKE2b-256 016d4eb3b9284bbcf43ec01e85a483241a85562d31d6065a5dd79ef24d01c9ee

See more details on using hashes here.

Provenance

The following attestation bundles were made for onnxsim-0.7.0-cp310-cp310-macosx_11_0_arm64.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 Sentry Error logging StatusPage Status page