onnx-cse is a tiny library for common subexpression elimination (CSE) from ONNX models.
The onnx_cse package provides a single function:
from onnx_cse import eliminate_common_subexpressions
import onnx
model = onnx.load_model("model.onnx")
# Update model in-place
eliminate_common_subexpressions(model)It is written in pure Python with minimal dependencies and focuses on being safe, fast, and simple. It differs from similar tools such as onnxoptimizer, onnx-simplifier, and onnxruntimes own CSE-pass in the following ways:
onnx-cse does one thing (CSE) but does it well.
The entire library is less then a couple of hundred lines of code and easy to understand for anybody interested.
From personal experience onnx-cse handily outperforms onnxruntime's CSE pass on very large graphs with small weights (~10k nodes with nested subgraphs) while onnxoptimizer fails to finish its operation on such graphs at all.
onnx-cse eliminates subexpressions in subgraphs if they can be replaced with expressions found in the enclosing scope.
pip install onnx-cse
Using pixi:
pixi add onnx-cse
or using conda:
conda install onnx-cse
You can install the package in development mode using:
git clone https://github.com/cbourjau/onnx-cse
cd onnx-cse
pixi run pre-commit-install
pixi run postinstall
pixi run test