onnxsim runs a folding fixed point until your ONNX graph stops changing
Simplify your onnx model
At a glance
- What is it?
- onnxsim rewrites exported ONNX graphs by folding constant subgraphs until nothing moves, then checks the result against the original. Useful when a torch export arrives as an unreadable wall of nodes; not a general graph optimizer, and building it from source needs cmake.
- Who is it for?
- Adopt onnxsim when you control the export pipeline and can afford the build, and when the deployment artifact carries operators no engine wants. It is a post-export cleaner, so skip it if you need graph work at request time, if your interpreter is older than 3.11, or if your model is a fused vendor graph you do not control.
- Can I use it commercially?
- Yes. Apache-2.0 is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
- Is it still maintained?
- Yes. The repository received new commits within the last day.
- What is it written in?
- Mainly Python, according to GitHub's language statistics.
Answers come from the project's GitHub data, last synced on November 23, 2026, and from our analysis. They are not legal advice.
Star history
- onnxsim/onnxsim4,421 ★ · +23
| Repository | 2026-09-11 | 2026-11-23 |
|---|---|---|
| onnxsim/onnxsim | 4.4k | 4.4k |
23 Stars in the last 73 days, measured from the daily GitHub snapshot. Compare with other projects
Editorial analysis
The reshape that came back as a wall of nodes
The founding example in the README is a PyTorch module whose whole forward pass is one line: x.view((x.shape[0], x.shape[1], x.shape[3], x.shape[2])). Exporting it with torch.onnx.export should give back a Reshape node. What the author actually got was a complicated graph, even though the input shape in that model is static. onnxsim exists to close that gap, so the audience is narrow and specific: anyone who exports a model from PyTorch, Detectron2, a diffusers pipeline or a Hugging Face transformers model and then finds the deployment artifact carrying operators no inference engine benefits from.
The project is not a general purpose graph optimizer you bolt onto arbitrary ONNX. It is a post-export cleaner that assumes the graph was produced by a tracer and that much of it is arithmetic on values fixed at build time. Its language is Python, with requires-python set to >=3.11, and the tree was last pushed on 2026-10-30.
What the fixed point actually iterates on
At the center is a loop, not a single pass. onnxsim runs shape inference, graph optimization and constant folding, then runs them again, until the model stops changing between iterations. That termination condition is the whole design. A single folding pass can expose a new constant, a fused BatchNorm can expose a constant reshape, and shape inference can make a branch provably dead only after the optimizer has already rewritten what feeds it.
Three layers feed each other. Constant folding evaluates the constant parts of the graph and replaces redundant operators with their computed outputs. Graph optimization runs onnx-optimizer fusions and eliminations, fuse BatchNorm into Conv being the named example. Shape inference propagates tensor shapes through the graph, including partial shape evaluation through ONNX data propagation, which is what unlocks more folding. Because the loop repeats, a simplification that only becomes legal late in the sequence still gets taken.
Two switches control how far it will go. Initializers count as constants by default, so weights get baked in. Passing --initializers-as-non-constants, and initializers_as_constants=False in Python, keeps weights as tunable tensors. That matters for nodes rooted only at initializers, and for value-baking fusions such as BatchNorm folded into Conv, which are then left alone. The optimizer set is likewise adjustable: --skip-optimization takes a list of passes to turn off, --enable-optimization requests a pass outside the default set, and both list-style flags exist as --list-default-optimizers and --list-other-optimizers.
Installing from PyPI and running the first simplify
The package name on the index is onnxsim, and the repository is a normal Python source tree with pyproject.toml, setup.py and a CMake-driven build through nanobind. Only one hard dependency is declared, onnx, with requires-python set to >=3.11. Optional extras carry the rest: onnxruntime for constant folding and correctness checking, rich for the terminal reports, and sympy for the MACs and FLOPs reporting. Install the onnxsim package from PyPI with pip.
Once the console script is on your PATH, ask the tool what it would do before it does it. This prints the optimizer passes that run by default, which is the fastest way to see whether a fusion you care about is in the set:
onnxsim --list-default-optimizersThe companion flag --list-other-optimizers prints the passes that exist but are off unless you ask for them. Diff the two lists against the operators in your export, and if something you need is in the second list, request it with --enable-optimization rather than editing the model by hand.
The shortest real use takes an input model path and an output model path, followed optionally by a positive integer check_n that compares both models on that many random inputs. Leave the check off and you have only a smaller graph, not evidence that it still computes the same thing. From Python the same work is import onnxsim followed by onnxsim.simplify on a loaded model.
check_n is the only safety net, and its inputs are uniform by default
Correctness checking is opt-in, positional, and cheap. Pass the check_n argument and onnxsim validates the simplified model against the original on that many random inputs, with --check-rtol and --check-atol for the tolerances. What you should look for is a mismatch, and if you get one the simplification is wrong for that input distribution rather than for the model in general.
The distribution is set by --input-fill, or input_fill= in Python: random, ones, zeros or arange. Random is the default and fills uniform [0, 1). This is the detail worth pausing on. A folded model that bakes a clamp, a mask or a saturating activation into a constant can agree with the original across a thousand uniform samples and still diverge on production inputs, which are rarely uniform in [0, 1). Switching --input-fill to arange is cheap insurance that the default does not give you.
The same family of caveats covers dynamic shapes. A model with symbolic dimensions is pinned for simplification and checking with --overwrite-input-shape, and the shape used for the check comes from --test-input-shape. Fix a shape and you are testing a concrete graph, so a shape-dependent fold that holds for the pinned dimension can be wrong for another.
Subgraphs, functions and vendor domains are three separate escape hatches
Control flow hides constant subgraphs. If/Loop/Scan bodies are only simplified when you pass --include-subgraph.
Model-defined functions hide a second layer. --inline-functions, and inline_functions=True in Python, flatten local FunctionProto definitions into the main graph before simplification starts, so the optimizer, shape inference and folding can see through the call. Schema-defined built-in functions are left alone by design.
Custom operators are the third. onnxsim keeps custom ops unchanged, whether they are TensorRT plugins, a vendor domain, or a custom op inside the default ONNX domain, and it picks up schemas registered through onnx.defs.register_schema automatically. This is the property that makes it usable on real production exports, where a deployment graph mixes standard operators with hardware-specific ones.
Conversion rides along with the same pass. --target-opset upgrades or downgrades the opset while simplifying. --emit-mlir hands the result to torch-mlir or onnx-mlir, both optional backends, and --emit-coreml converts through a built-in ONNX-to-MIL translator and coremltools. Subgraph simplification, opset conversion and format export are separate flags, not stages you get by default.
cmake and nanobind decide how much gets compiled on install
Setup cost is not zero. The build requires setuptools>=77, wheel and nanobind>=2.12.0, and setup.py resolves cmake through shutil.which('cmake') before handing work to CMakeLists.txt.
Build weight is configurable through environment variables read in that same setup.py. ONNXSIM_NON_CORE_FEATURES=0 skips compiling roughly fifty quantization and pruning entry points, GPTQ, AWQ, SmoothQuant, Wanda, structured and magnitude pruning among them, none of which are reachable from Simplify(). For a CI job that only needs the folding loop, that is the difference between a short build and a long one.
Two records disagree on one line here: the GitHub repository metadata records Apache-2.0, while the license field in pyproject.toml reads MIT AND (Apache-2.0 OR BSD-2-Clause). That is a discrepancy in the files rather than a legal opinion, and the packaging metadata is the one an installer reads, so settle it inside your own shipping process before redistributing anything derived from the package.
Declared Python support narrows things too. The classifiers list 3.11, 3.12 and 3.13, along with Programming Language :: Python :: 3 :: Only, so an inference environment pinned to an older interpreter is simply out of range for this package.
Where onnxslim and onnxoptimizer divide the work
onnxsim's graph optimization passes run onnx-optimizer's fusions and eliminations. If your problem is a single bad fusion or a specific elimination, going directly at onnxoptimizer is the smaller dependency, and you keep the ability to run one pass instead of a fixed point.
Beyond that, onnxsim's own approach is worth naming as the difference: a repeatable fixed point with shape inference in the loop, a built-in numerical check against the original, and a set of export bridges for transformers, diffusers, Detectron2, SAM 2 and DriveTransformer models, plus safetensors and GGUF archive round-trips. A pure fusion library does none of that. What it also does not do is run at inference time: the documented commands operate on a model file you pass in, not on a serving process.
Editorial conclusion
Adopt onnxsim when you control the export pipeline and can afford the build, and when the deployment artifact carries operators no engine wants. It is a post-export cleaner, so skip it if you need graph work at request time, if your interpreter is older than 3.11, or if your model is a fused vendor graph you do not control. Verify first that simplify() leaves your custom domain nodes alone on one real export, and run check_n with --input-fill arange instead of the uniform default before you trust the folded weights.
Frequently asked questions
what is onnxsim
onnxsim is a Python tool that simplifies exported ONNX models. It infers the whole computation graph and replaces redundant operators with their constant outputs, a process also called constant folding.
how to install onnxsim
The package is published on PyPI as onnxsim and requires Python 3.11 or newer. It declares onnx as its only hard dependency, with onnxruntime, rich and sympy available as optional extras.
what does onnxsim do
It runs a fixed point of shape inference, graph optimization and constant folding until the model stops changing. Around that it can change the opset, emit MLIR or Core ML, and export to safetensors or GGUF.
how to use onnxsim
From a shell, pass an input model and an output path, optionally followed by a check_n integer that compares the simplified model against the original on that many random inputs. In Python, call onnxsim.simplify on a loaded model instead.
Official sources
Add this badge to your README
If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.
[](https://hysenlabs.com/projects/onnxsim-onnxsim)Star history chart for your README
[](https://hysenlabs.com/projects/onnxsim-onnxsim)Was this useful?