onnxslim 0.1.38__tar.gz → 0.1.75__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- onnxslim-0.1.75/PKG-INFO +146 -0
- onnxslim-0.1.75/README.md +112 -0
- onnxslim-0.1.75/VERSION +1 -0
- {onnxslim-0.1.38 → onnxslim-0.1.75}/onnxslim/__init__.py +0 -1
- {onnxslim-0.1.38 → onnxslim-0.1.75}/onnxslim/argparser.py +65 -13
- {onnxslim-0.1.38 → onnxslim-0.1.75}/onnxslim/cli/_main.py +56 -31
- {onnxslim-0.1.38 → onnxslim-0.1.75}/onnxslim/core/__init__.py +27 -9
- {onnxslim-0.1.38 → onnxslim-0.1.75}/onnxslim/core/optimization/__init__.py +72 -16
- {onnxslim-0.1.38 → onnxslim-0.1.75}/onnxslim/core/optimization/dead_node_elimination.py +32 -19
- {onnxslim-0.1.38 → onnxslim-0.1.75}/onnxslim/core/optimization/subexpression_elimination.py +11 -21
- {onnxslim-0.1.38 → onnxslim-0.1.75}/onnxslim/core/optimization/weight_tying.py +19 -0
- {onnxslim-0.1.38 → onnxslim-0.1.75}/onnxslim/core/pattern/__init__.py +71 -50
- {onnxslim-0.1.38 → onnxslim-0.1.75}/onnxslim/core/pattern/elimination/__init__.py +2 -0
- onnxslim-0.1.75/onnxslim/core/pattern/elimination/concat.py +61 -0
- {onnxslim-0.1.38 → onnxslim-0.1.75}/onnxslim/core/pattern/elimination/reshape.py +3 -3
- onnxslim-0.1.75/onnxslim/core/pattern/elimination/reshape_as.py +64 -0
- {onnxslim-0.1.38 → onnxslim-0.1.75}/onnxslim/core/pattern/elimination/slice.py +7 -7
- {onnxslim-0.1.38 → onnxslim-0.1.75}/onnxslim/core/pattern/elimination/unsqueeze.py +14 -5
- {onnxslim-0.1.38 → onnxslim-0.1.75}/onnxslim/core/pattern/fusion/__init__.py +2 -0
- onnxslim-0.1.75/onnxslim/core/pattern/fusion/concat_reshape.py +50 -0
- {onnxslim-0.1.38 → onnxslim-0.1.75}/onnxslim/core/pattern/fusion/convadd.py +3 -3
- {onnxslim-0.1.38 → onnxslim-0.1.75}/onnxslim/core/pattern/fusion/convbn.py +9 -9
- onnxslim-0.1.75/onnxslim/core/pattern/fusion/convmul.py +69 -0
- onnxslim-0.1.75/onnxslim/core/pattern/fusion/gemm.py +330 -0
- onnxslim-0.1.75/onnxslim/core/pattern/fusion/padconv.py +89 -0
- {onnxslim-0.1.38 → onnxslim-0.1.75}/onnxslim/core/pattern/fusion/reduce.py +21 -10
- {onnxslim-0.1.38 → onnxslim-0.1.75}/onnxslim/core/pattern/registry.py +3 -1
- {onnxslim-0.1.38 → onnxslim-0.1.75}/onnxslim/misc/tabulate.py +12 -10
- onnxslim-0.1.75/onnxslim/third_party/_sympy/functions.py +205 -0
- onnxslim-0.1.75/onnxslim/third_party/_sympy/numbers.py +397 -0
- onnxslim-0.1.75/onnxslim/third_party/_sympy/printers.py +491 -0
- onnxslim-0.1.75/onnxslim/third_party/_sympy/solve.py +172 -0
- onnxslim-0.1.75/onnxslim/third_party/_sympy/symbol.py +102 -0
- {onnxslim-0.1.38 → onnxslim-0.1.75}/onnxslim/third_party/onnx_graphsurgeon/exporters/onnx_exporter.py +105 -53
- {onnxslim-0.1.38 → onnxslim-0.1.75}/onnxslim/third_party/onnx_graphsurgeon/graph_pattern/graph_pattern.py +12 -13
- {onnxslim-0.1.38 → onnxslim-0.1.75}/onnxslim/third_party/onnx_graphsurgeon/importers/onnx_importer.py +33 -27
- {onnxslim-0.1.38 → onnxslim-0.1.75}/onnxslim/third_party/onnx_graphsurgeon/ir/function.py +13 -12
- {onnxslim-0.1.38 → onnxslim-0.1.75}/onnxslim/third_party/onnx_graphsurgeon/ir/graph.py +19 -15
- {onnxslim-0.1.38 → onnxslim-0.1.75}/onnxslim/third_party/onnx_graphsurgeon/ir/node.py +58 -9
- {onnxslim-0.1.38 → onnxslim-0.1.75}/onnxslim/third_party/onnx_graphsurgeon/ir/tensor.py +26 -15
- onnxslim-0.1.75/onnxslim/third_party/onnx_graphsurgeon/util/__init__.py +0 -0
- {onnxslim-0.1.38 → onnxslim-0.1.75}/onnxslim/third_party/onnx_graphsurgeon/util/misc.py +9 -8
- {onnxslim-0.1.38 → onnxslim-0.1.75}/onnxslim/third_party/symbolic_shape_infer.py +270 -178
- onnxslim-0.1.75/onnxslim/utils.py +794 -0
- onnxslim-0.1.75/onnxslim/version.py +1 -0
- onnxslim-0.1.75/onnxslim.egg-info/PKG-INFO +146 -0
- {onnxslim-0.1.38 → onnxslim-0.1.75}/onnxslim.egg-info/SOURCES.txt +11 -2
- onnxslim-0.1.75/onnxslim.egg-info/requires.txt +5 -0
- onnxslim-0.1.75/pyproject.toml +9 -0
- {onnxslim-0.1.38 → onnxslim-0.1.75}/setup.py +1 -1
- onnxslim-0.1.38/PKG-INFO +0 -89
- onnxslim-0.1.38/README.md +0 -69
- onnxslim-0.1.38/VERSION +0 -1
- onnxslim-0.1.38/onnxslim/core/pattern/fusion/gemm.py +0 -174
- onnxslim-0.1.38/onnxslim/core/pattern/fusion/padconv.py +0 -78
- onnxslim-0.1.38/onnxslim/core/utils.py +0 -28
- onnxslim-0.1.38/onnxslim/misc/font.py +0 -3
- onnxslim-0.1.38/onnxslim/utils.py +0 -631
- onnxslim-0.1.38/onnxslim/version.py +0 -1
- onnxslim-0.1.38/onnxslim.egg-info/PKG-INFO +0 -89
- onnxslim-0.1.38/onnxslim.egg-info/requires.txt +0 -3
- {onnxslim-0.1.38 → onnxslim-0.1.75}/LICENSE +0 -0
- {onnxslim-0.1.38 → onnxslim-0.1.75}/MANIFEST.in +0 -0
- {onnxslim-0.1.38 → onnxslim-0.1.75}/onnxslim/__main__.py +0 -0
- {onnxslim-0.1.38 → onnxslim-0.1.75}/onnxslim/cli/__init__.py +0 -0
- {onnxslim-0.1.38 → onnxslim-0.1.75}/onnxslim/core/pattern/fusion/gelu.py +0 -0
- {onnxslim-0.1.38 → onnxslim-0.1.75}/onnxslim/misc/__init__.py +0 -0
- {onnxslim-0.1.38 → onnxslim-0.1.75}/onnxslim/third_party/__init__.py +0 -0
- {onnxslim-0.1.38/onnxslim/third_party/onnx_graphsurgeon/ir → onnxslim-0.1.75/onnxslim/third_party/_sympy}/__init__.py +0 -0
- {onnxslim-0.1.38 → onnxslim-0.1.75}/onnxslim/third_party/onnx_graphsurgeon/__init__.py +0 -0
- {onnxslim-0.1.38 → onnxslim-0.1.75}/onnxslim/third_party/onnx_graphsurgeon/exporters/__init__.py +0 -0
- {onnxslim-0.1.38 → onnxslim-0.1.75}/onnxslim/third_party/onnx_graphsurgeon/exporters/base_exporter.py +0 -0
- {onnxslim-0.1.38 → onnxslim-0.1.75}/onnxslim/third_party/onnx_graphsurgeon/graph_pattern/__init__.py +0 -0
- {onnxslim-0.1.38 → onnxslim-0.1.75}/onnxslim/third_party/onnx_graphsurgeon/importers/__init__.py +0 -0
- {onnxslim-0.1.38 → onnxslim-0.1.75}/onnxslim/third_party/onnx_graphsurgeon/importers/base_importer.py +0 -0
- {onnxslim-0.1.38/onnxslim/third_party/onnx_graphsurgeon/util → onnxslim-0.1.75/onnxslim/third_party/onnx_graphsurgeon/ir}/__init__.py +0 -0
- {onnxslim-0.1.38 → onnxslim-0.1.75}/onnxslim/third_party/onnx_graphsurgeon/logger/__init__.py +0 -0
- {onnxslim-0.1.38 → onnxslim-0.1.75}/onnxslim/third_party/onnx_graphsurgeon/logger/logger.py +0 -0
- {onnxslim-0.1.38 → onnxslim-0.1.75}/onnxslim/third_party/onnx_graphsurgeon/util/exception.py +0 -0
- {onnxslim-0.1.38 → onnxslim-0.1.75}/onnxslim.egg-info/dependency_links.txt +0 -0
- {onnxslim-0.1.38 → onnxslim-0.1.75}/onnxslim.egg-info/entry_points.txt +0 -0
- {onnxslim-0.1.38 → onnxslim-0.1.75}/onnxslim.egg-info/top_level.txt +0 -0
- {onnxslim-0.1.38 → onnxslim-0.1.75}/onnxslim.egg-info/zip-safe +0 -0
- {onnxslim-0.1.38 → onnxslim-0.1.75}/setup.cfg +0 -0
onnxslim-0.1.75/PKG-INFO
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: onnxslim
|
|
3
|
+
Version: 0.1.75
|
|
4
|
+
Summary: OnnxSlim: A Toolkit to Help Optimize Onnx Model
|
|
5
|
+
Home-page: https://github.com/inisis/OnnxSlim
|
|
6
|
+
Author: inisis
|
|
7
|
+
Author-email: desmond.yao@buaa.edu.cn
|
|
8
|
+
License: MIT
|
|
9
|
+
Project-URL: Bug Tracker, https://github.com/inisis/OnnxSlim/issues
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
14
|
+
Requires-Python: >=3.6
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
License-File: LICENSE
|
|
17
|
+
Requires-Dist: onnx
|
|
18
|
+
Requires-Dist: sympy>=1.13.3
|
|
19
|
+
Requires-Dist: packaging
|
|
20
|
+
Requires-Dist: colorama
|
|
21
|
+
Requires-Dist: ml_dtypes
|
|
22
|
+
Dynamic: author
|
|
23
|
+
Dynamic: author-email
|
|
24
|
+
Dynamic: classifier
|
|
25
|
+
Dynamic: description
|
|
26
|
+
Dynamic: description-content-type
|
|
27
|
+
Dynamic: home-page
|
|
28
|
+
Dynamic: license
|
|
29
|
+
Dynamic: license-file
|
|
30
|
+
Dynamic: project-url
|
|
31
|
+
Dynamic: requires-dist
|
|
32
|
+
Dynamic: requires-python
|
|
33
|
+
Dynamic: summary
|
|
34
|
+
|
|
35
|
+
# OnnxSlim
|
|
36
|
+
|
|
37
|
+
<p align="center">
|
|
38
|
+
<a href="https://pypi.org/project/onnxslim">
|
|
39
|
+
<img src="https://img.shields.io/pypi/v/onnxslim?color=blue" />
|
|
40
|
+
</a>
|
|
41
|
+
<a href="https://pypi.org/project/onnxslim">
|
|
42
|
+
<img src="https://static.pepy.tech/badge/onnxslim/week" />
|
|
43
|
+
</a>
|
|
44
|
+
<a href="https://pypi.org/project/onnxslim">
|
|
45
|
+
<img src="https://static.pepy.tech/badge/onnxslim/month" />
|
|
46
|
+
</a>
|
|
47
|
+
<a href="https://pypi.org/project/onnxslim">
|
|
48
|
+
<img src="https://static.pepy.tech/badge/onnxslim" />
|
|
49
|
+
</a>
|
|
50
|
+
<a href="https://github.com/inisis/onnxslim/actions/workflows/ci.yaml">
|
|
51
|
+
<img src="https://github.com/inisis/onnxslim/actions/workflows/ci.yml/badge.svg" />
|
|
52
|
+
</a>
|
|
53
|
+
<a href="https://codecov.io/gh/inisis/onnxslim" >
|
|
54
|
+
<img src="https://codecov.io/gh/inisis/onnxslim/branch/main/graph/badge.svg?token=C69ZH6802N"/>
|
|
55
|
+
</a>
|
|
56
|
+
<a href="https://muhammadrizwanmunawar.medium.com/boost-onnx-load-speed-by-10-15-with-onnxslims-python-package-d401eb8c2e69">
|
|
57
|
+
<img src="https://img.shields.io/badge/Blog-OnnxSlim?style=flat&label=OnnxSlim" />
|
|
58
|
+
</a>
|
|
59
|
+
<a href="https://deepwiki.com/inisis/OnnxSlim"><img src="https://img.shields.io/badge/DeepWiki-inisis%2FOnnxSlim-blue.svg?logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACwAAAAyCAYAAAAnWDnqAAAAAXNSR0IArs4c6QAAA05JREFUaEPtmUtyEzEQhtWTQyQLHNak2AB7ZnyXZMEjXMGeK/AIi+QuHrMnbChYY7MIh8g01fJoopFb0uhhEqqcbWTp06/uv1saEDv4O3n3dV60RfP947Mm9/SQc0ICFQgzfc4CYZoTPAswgSJCCUJUnAAoRHOAUOcATwbmVLWdGoH//PB8mnKqScAhsD0kYP3j/Yt5LPQe2KvcXmGvRHcDnpxfL2zOYJ1mFwrryWTz0advv1Ut4CJgf5uhDuDj5eUcAUoahrdY/56ebRWeraTjMt/00Sh3UDtjgHtQNHwcRGOC98BJEAEymycmYcWwOprTgcB6VZ5JK5TAJ+fXGLBm3FDAmn6oPPjR4rKCAoJCal2eAiQp2x0vxTPB3ALO2CRkwmDy5WohzBDwSEFKRwPbknEggCPB/imwrycgxX2NzoMCHhPkDwqYMr9tRcP5qNrMZHkVnOjRMWwLCcr8ohBVb1OMjxLwGCvjTikrsBOiA6fNyCrm8V1rP93iVPpwaE+gO0SsWmPiXB+jikdf6SizrT5qKasx5j8ABbHpFTx+vFXp9EnYQmLx02h1QTTrl6eDqxLnGjporxl3NL3agEvXdT0WmEost648sQOYAeJS9Q7bfUVoMGnjo4AZdUMQku50McDcMWcBPvr0SzbTAFDfvJqwLzgxwATnCgnp4wDl6Aa+Ax283gghmj+vj7feE2KBBRMW3FzOpLOADl0Isb5587h/U4gGvkt5v60Z1VLG8BhYjbzRwyQZemwAd6cCR5/XFWLYZRIMpX39AR0tjaGGiGzLVyhse5C9RKC6ai42ppWPKiBagOvaYk8lO7DajerabOZP46Lby5wKjw1HCRx7p9sVMOWGzb/vA1hwiWc6jm3MvQDTogQkiqIhJV0nBQBTU+3okKCFDy9WwferkHjtxib7t3xIUQtHxnIwtx4mpg26/HfwVNVDb4oI9RHmx5WGelRVlrtiw43zboCLaxv46AZeB3IlTkwouebTr1y2NjSpHz68WNFjHvupy3q8TFn3Hos2IAk4Ju5dCo8B3wP7VPr/FGaKiG+T+v+TQqIrOqMTL1VdWV1DdmcbO8KXBz6esmYWYKPwDL5b5FA1a0hwapHiom0r/cKaoqr+27/XcrS5UwSMbQAAAABJRU5ErkJggg==" alt="DeepWiki"></a>
|
|
60
|
+
</p>
|
|
61
|
+
|
|
62
|
+
OnnxSlim can help you slim your onnx model, with less operators, but same accuracy, better inference speed.
|
|
63
|
+
|
|
64
|
+
- 🚀 2025/05/17: OnnxSlim is merged into [optimum](https://github.com/huggingface/optimum) 🤗🤗🤗
|
|
65
|
+
- 🚀 2025/04/30: Rank 1st in the [AICAS 2025 LLM inference optimization challenge](https://tianchi.aliyun.com/competition/entrance/532289/customize588)
|
|
66
|
+
- 🚀 2025/01/28: Achieved 1M downloads
|
|
67
|
+
- 🚀 2024/06/23: OnnxSlim is merged into [transformers.js](https://github.com/huggingface/transformers.js) 🤗🤗🤗
|
|
68
|
+
- 🚀 2024/06/02: OnnxSlim is merged into [ultralytics](https://github.com/ultralytics/ultralytics) ❤️❤️❤️
|
|
69
|
+
- 🚀 2024/04/30: Rank 1st in the [AICAS 2024 LLM inference optimization challenge](https://tianchi.aliyun.com/competition/entrance/532170/customize440) held by Arm and T-head
|
|
70
|
+
- 🚀 2024/01/25: OnnxSlim is merged to [mnn-llm](https://github.com/wangzhaode/mnn-llm), performance increased by 5%
|
|
71
|
+
|
|
72
|
+
# Benchmark
|
|
73
|
+
|
|
74
|
+

|
|
75
|
+
|
|
76
|
+
# Installation
|
|
77
|
+
|
|
78
|
+
## Using Prebuilt
|
|
79
|
+
|
|
80
|
+
```bash
|
|
81
|
+
pip install onnxslim
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## Install From Source
|
|
85
|
+
|
|
86
|
+
```bash
|
|
87
|
+
pip install git+https://github.com/inisis/OnnxSlim@main
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Install From Local
|
|
91
|
+
|
|
92
|
+
```bash
|
|
93
|
+
git clone https://github.com/inisis/OnnxSlim && cd OnnxSlim/
|
|
94
|
+
pip install .
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
# How to use
|
|
98
|
+
|
|
99
|
+
## Bash
|
|
100
|
+
|
|
101
|
+
```bash
|
|
102
|
+
onnxslim your_onnx_model slimmed_onnx_model
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
<div align=left><img src="https://raw.githubusercontent.com/inisis/onnxslim/main/images/onnxslim.gif"></div>
|
|
106
|
+
|
|
107
|
+
## Inscript
|
|
108
|
+
|
|
109
|
+
```inscript
|
|
110
|
+
import onnx
|
|
111
|
+
import onnxslim
|
|
112
|
+
|
|
113
|
+
model = onnx.load("model.onnx")
|
|
114
|
+
slimmed_model = onnxslim.slim(model)
|
|
115
|
+
onnx.save(slimmed_model, "slimmed_model.onnx")
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
For more usage, see onnxslim -h or refer to our [examples](./examples)
|
|
119
|
+
|
|
120
|
+
# Projects using OnnxSlim
|
|
121
|
+
|
|
122
|
+
- <img src="https://avatars.githubusercontent.com/u/131524?s=48&v=4" width="22" height="22"/>[Mozilla/smart_autofill](https://github.com/mozilla/smart_autofill)
|
|
123
|
+
- <img src="https://avatars.githubusercontent.com/u/1961952?s=48&v=4" width="22" height="22"/>[alibaba/MNN](https://github.com/alibaba/MNN)
|
|
124
|
+
- <img src="https://avatars.githubusercontent.com/u/23534030?s=48&v=4" width="22" height="22"/>[PaddlePaddle/PaddleOCR](https://github.com/PaddlePaddle/PaddleOCR)
|
|
125
|
+
- <img src="https://avatars.githubusercontent.com/u/25720743?s=48&v=4" width="22" height="22"/>[huggingface/transformers.js](https://github.com/huggingface/transformers.js)
|
|
126
|
+
- <img src="https://avatars.githubusercontent.com/u/25720743?s=48&v=4" width="22" height="22"/>[huggingface/optimum](https://github.com/huggingface/optimum)
|
|
127
|
+
- <img src="https://avatars.githubusercontent.com/u/86091366?s=48&v=4" width="22" height="22"/>[THU-MIG/yolov10](https://github.com/THU-MIG/yolov10)
|
|
128
|
+
- <img src="https://avatars.githubusercontent.com/u/26833451?s=48&v=4" width="22" height="22"/>[ultralytics/ultralytics](https://github.com/ultralytics/ultralytics)
|
|
129
|
+
- <img src="https://avatars.githubusercontent.com/u/109945100?s=48&v=4" width="22" height="22"/>[ModelScope/FunASR](https://github.com/modelscope/FunASR)
|
|
130
|
+
- <img src="https://avatars.githubusercontent.com/u/1961952?s=48&v=4" width="22" height="22"/>[alibaba/MNN-LLM](https://github.com/wangzhaode/mnn-llm)
|
|
131
|
+
- <img src="https://avatars.githubusercontent.com/u/126587470?s=48&v=4" width="22" height="22"/>[deepghs/imgutils](https://github.com/deepghs/imgutils)
|
|
132
|
+
- <img src="https://avatars.githubusercontent.com/u/48153283?s=48&v=4" width="22" height="22"/>[sunsmarterjie/yolov12](https://github.com/sunsmarterjie/yolov12)
|
|
133
|
+
- <img src="https://avatars.githubusercontent.com/u/147458884?s=48&v=4" width="22" height="22"/>[nndeploy/nndeploy](https://github.com/nndeploy/nndeploy)
|
|
134
|
+
- <img src="https://avatars.githubusercontent.com/u/111754012?s=48&v=4" width="22" height="22"/>[CVCUDA/CV-CUDA](https://github.com/CVCUDA/CV-CUDA)
|
|
135
|
+
|
|
136
|
+
# References
|
|
137
|
+
|
|
138
|
+
> - [onnx-graphsurgeon](https://github.com/NVIDIA/TensorRT/tree/main/tools/onnx-graphsurgeon)
|
|
139
|
+
> - [Polygraphy](https://github.com/NVIDIA/TensorRT/tree/main/tools/Polygraphy/polygraphy)
|
|
140
|
+
> - [onnx-simplifier](https://github.com/daquexian/onnx-simplifier)
|
|
141
|
+
> - [tabulate](https://github.com/astanin/python-tabulate)
|
|
142
|
+
> - [onnxruntime](https://github.com/microsoft/onnxruntime)
|
|
143
|
+
|
|
144
|
+
# Contact
|
|
145
|
+
|
|
146
|
+
Discord: https://discord.gg/nRw2Fd3VUS QQ Group: `873569894`
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
# OnnxSlim
|
|
2
|
+
|
|
3
|
+
<p align="center">
|
|
4
|
+
<a href="https://pypi.org/project/onnxslim">
|
|
5
|
+
<img src="https://img.shields.io/pypi/v/onnxslim?color=blue" />
|
|
6
|
+
</a>
|
|
7
|
+
<a href="https://pypi.org/project/onnxslim">
|
|
8
|
+
<img src="https://static.pepy.tech/badge/onnxslim/week" />
|
|
9
|
+
</a>
|
|
10
|
+
<a href="https://pypi.org/project/onnxslim">
|
|
11
|
+
<img src="https://static.pepy.tech/badge/onnxslim/month" />
|
|
12
|
+
</a>
|
|
13
|
+
<a href="https://pypi.org/project/onnxslim">
|
|
14
|
+
<img src="https://static.pepy.tech/badge/onnxslim" />
|
|
15
|
+
</a>
|
|
16
|
+
<a href="https://github.com/inisis/onnxslim/actions/workflows/ci.yaml">
|
|
17
|
+
<img src="https://github.com/inisis/onnxslim/actions/workflows/ci.yml/badge.svg" />
|
|
18
|
+
</a>
|
|
19
|
+
<a href="https://codecov.io/gh/inisis/onnxslim" >
|
|
20
|
+
<img src="https://codecov.io/gh/inisis/onnxslim/branch/main/graph/badge.svg?token=C69ZH6802N"/>
|
|
21
|
+
</a>
|
|
22
|
+
<a href="https://muhammadrizwanmunawar.medium.com/boost-onnx-load-speed-by-10-15-with-onnxslims-python-package-d401eb8c2e69">
|
|
23
|
+
<img src="https://img.shields.io/badge/Blog-OnnxSlim?style=flat&label=OnnxSlim" />
|
|
24
|
+
</a>
|
|
25
|
+
<a href="https://deepwiki.com/inisis/OnnxSlim"><img src="https://img.shields.io/badge/DeepWiki-inisis%2FOnnxSlim-blue.svg?logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACwAAAAyCAYAAAAnWDnqAAAAAXNSR0IArs4c6QAAA05JREFUaEPtmUtyEzEQhtWTQyQLHNak2AB7ZnyXZMEjXMGeK/AIi+QuHrMnbChYY7MIh8g01fJoopFb0uhhEqqcbWTp06/uv1saEDv4O3n3dV60RfP947Mm9/SQc0ICFQgzfc4CYZoTPAswgSJCCUJUnAAoRHOAUOcATwbmVLWdGoH//PB8mnKqScAhsD0kYP3j/Yt5LPQe2KvcXmGvRHcDnpxfL2zOYJ1mFwrryWTz0advv1Ut4CJgf5uhDuDj5eUcAUoahrdY/56ebRWeraTjMt/00Sh3UDtjgHtQNHwcRGOC98BJEAEymycmYcWwOprTgcB6VZ5JK5TAJ+fXGLBm3FDAmn6oPPjR4rKCAoJCal2eAiQp2x0vxTPB3ALO2CRkwmDy5WohzBDwSEFKRwPbknEggCPB/imwrycgxX2NzoMCHhPkDwqYMr9tRcP5qNrMZHkVnOjRMWwLCcr8ohBVb1OMjxLwGCvjTikrsBOiA6fNyCrm8V1rP93iVPpwaE+gO0SsWmPiXB+jikdf6SizrT5qKasx5j8ABbHpFTx+vFXp9EnYQmLx02h1QTTrl6eDqxLnGjporxl3NL3agEvXdT0WmEost648sQOYAeJS9Q7bfUVoMGnjo4AZdUMQku50McDcMWcBPvr0SzbTAFDfvJqwLzgxwATnCgnp4wDl6Aa+Ax283gghmj+vj7feE2KBBRMW3FzOpLOADl0Isb5587h/U4gGvkt5v60Z1VLG8BhYjbzRwyQZemwAd6cCR5/XFWLYZRIMpX39AR0tjaGGiGzLVyhse5C9RKC6ai42ppWPKiBagOvaYk8lO7DajerabOZP46Lby5wKjw1HCRx7p9sVMOWGzb/vA1hwiWc6jm3MvQDTogQkiqIhJV0nBQBTU+3okKCFDy9WwferkHjtxib7t3xIUQtHxnIwtx4mpg26/HfwVNVDb4oI9RHmx5WGelRVlrtiw43zboCLaxv46AZeB3IlTkwouebTr1y2NjSpHz68WNFjHvupy3q8TFn3Hos2IAk4Ju5dCo8B3wP7VPr/FGaKiG+T+v+TQqIrOqMTL1VdWV1DdmcbO8KXBz6esmYWYKPwDL5b5FA1a0hwapHiom0r/cKaoqr+27/XcrS5UwSMbQAAAABJRU5ErkJggg==" alt="DeepWiki"></a>
|
|
26
|
+
</p>
|
|
27
|
+
|
|
28
|
+
OnnxSlim can help you slim your onnx model, with less operators, but same accuracy, better inference speed.
|
|
29
|
+
|
|
30
|
+
- 🚀 2025/05/17: OnnxSlim is merged into [optimum](https://github.com/huggingface/optimum) 🤗🤗🤗
|
|
31
|
+
- 🚀 2025/04/30: Rank 1st in the [AICAS 2025 LLM inference optimization challenge](https://tianchi.aliyun.com/competition/entrance/532289/customize588)
|
|
32
|
+
- 🚀 2025/01/28: Achieved 1M downloads
|
|
33
|
+
- 🚀 2024/06/23: OnnxSlim is merged into [transformers.js](https://github.com/huggingface/transformers.js) 🤗🤗🤗
|
|
34
|
+
- 🚀 2024/06/02: OnnxSlim is merged into [ultralytics](https://github.com/ultralytics/ultralytics) ❤️❤️❤️
|
|
35
|
+
- 🚀 2024/04/30: Rank 1st in the [AICAS 2024 LLM inference optimization challenge](https://tianchi.aliyun.com/competition/entrance/532170/customize440) held by Arm and T-head
|
|
36
|
+
- 🚀 2024/01/25: OnnxSlim is merged to [mnn-llm](https://github.com/wangzhaode/mnn-llm), performance increased by 5%
|
|
37
|
+
|
|
38
|
+
# Benchmark
|
|
39
|
+
|
|
40
|
+

|
|
41
|
+
|
|
42
|
+
# Installation
|
|
43
|
+
|
|
44
|
+
## Using Prebuilt
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
pip install onnxslim
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Install From Source
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
pip install git+https://github.com/inisis/OnnxSlim@main
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Install From Local
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
git clone https://github.com/inisis/OnnxSlim && cd OnnxSlim/
|
|
60
|
+
pip install .
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
# How to use
|
|
64
|
+
|
|
65
|
+
## Bash
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
onnxslim your_onnx_model slimmed_onnx_model
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
<div align=left><img src="https://raw.githubusercontent.com/inisis/onnxslim/main/images/onnxslim.gif"></div>
|
|
72
|
+
|
|
73
|
+
## Inscript
|
|
74
|
+
|
|
75
|
+
```inscript
|
|
76
|
+
import onnx
|
|
77
|
+
import onnxslim
|
|
78
|
+
|
|
79
|
+
model = onnx.load("model.onnx")
|
|
80
|
+
slimmed_model = onnxslim.slim(model)
|
|
81
|
+
onnx.save(slimmed_model, "slimmed_model.onnx")
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
For more usage, see onnxslim -h or refer to our [examples](./examples)
|
|
85
|
+
|
|
86
|
+
# Projects using OnnxSlim
|
|
87
|
+
|
|
88
|
+
- <img src="https://avatars.githubusercontent.com/u/131524?s=48&v=4" width="22" height="22"/>[Mozilla/smart_autofill](https://github.com/mozilla/smart_autofill)
|
|
89
|
+
- <img src="https://avatars.githubusercontent.com/u/1961952?s=48&v=4" width="22" height="22"/>[alibaba/MNN](https://github.com/alibaba/MNN)
|
|
90
|
+
- <img src="https://avatars.githubusercontent.com/u/23534030?s=48&v=4" width="22" height="22"/>[PaddlePaddle/PaddleOCR](https://github.com/PaddlePaddle/PaddleOCR)
|
|
91
|
+
- <img src="https://avatars.githubusercontent.com/u/25720743?s=48&v=4" width="22" height="22"/>[huggingface/transformers.js](https://github.com/huggingface/transformers.js)
|
|
92
|
+
- <img src="https://avatars.githubusercontent.com/u/25720743?s=48&v=4" width="22" height="22"/>[huggingface/optimum](https://github.com/huggingface/optimum)
|
|
93
|
+
- <img src="https://avatars.githubusercontent.com/u/86091366?s=48&v=4" width="22" height="22"/>[THU-MIG/yolov10](https://github.com/THU-MIG/yolov10)
|
|
94
|
+
- <img src="https://avatars.githubusercontent.com/u/26833451?s=48&v=4" width="22" height="22"/>[ultralytics/ultralytics](https://github.com/ultralytics/ultralytics)
|
|
95
|
+
- <img src="https://avatars.githubusercontent.com/u/109945100?s=48&v=4" width="22" height="22"/>[ModelScope/FunASR](https://github.com/modelscope/FunASR)
|
|
96
|
+
- <img src="https://avatars.githubusercontent.com/u/1961952?s=48&v=4" width="22" height="22"/>[alibaba/MNN-LLM](https://github.com/wangzhaode/mnn-llm)
|
|
97
|
+
- <img src="https://avatars.githubusercontent.com/u/126587470?s=48&v=4" width="22" height="22"/>[deepghs/imgutils](https://github.com/deepghs/imgutils)
|
|
98
|
+
- <img src="https://avatars.githubusercontent.com/u/48153283?s=48&v=4" width="22" height="22"/>[sunsmarterjie/yolov12](https://github.com/sunsmarterjie/yolov12)
|
|
99
|
+
- <img src="https://avatars.githubusercontent.com/u/147458884?s=48&v=4" width="22" height="22"/>[nndeploy/nndeploy](https://github.com/nndeploy/nndeploy)
|
|
100
|
+
- <img src="https://avatars.githubusercontent.com/u/111754012?s=48&v=4" width="22" height="22"/>[CVCUDA/CV-CUDA](https://github.com/CVCUDA/CV-CUDA)
|
|
101
|
+
|
|
102
|
+
# References
|
|
103
|
+
|
|
104
|
+
> - [onnx-graphsurgeon](https://github.com/NVIDIA/TensorRT/tree/main/tools/onnx-graphsurgeon)
|
|
105
|
+
> - [Polygraphy](https://github.com/NVIDIA/TensorRT/tree/main/tools/Polygraphy/polygraphy)
|
|
106
|
+
> - [onnx-simplifier](https://github.com/daquexian/onnx-simplifier)
|
|
107
|
+
> - [tabulate](https://github.com/astanin/python-tabulate)
|
|
108
|
+
> - [onnxruntime](https://github.com/microsoft/onnxruntime)
|
|
109
|
+
|
|
110
|
+
# Contact
|
|
111
|
+
|
|
112
|
+
Discord: https://discord.gg/nRw2Fd3VUS QQ Group: `873569894`
|
onnxslim-0.1.75/VERSION
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
0.1.75
|
|
@@ -1,9 +1,34 @@
|
|
|
1
1
|
import argparse
|
|
2
2
|
import dataclasses
|
|
3
|
+
from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser
|
|
3
4
|
from dataclasses import dataclass, field
|
|
4
|
-
from typing import List, Optional, Type
|
|
5
|
-
|
|
6
|
-
import
|
|
5
|
+
from typing import List, Optional, Type, Union, get_args, get_origin, TypedDict, Dict, Literal
|
|
6
|
+
|
|
7
|
+
from .core.optimization import OptimizationSettings
|
|
8
|
+
from .core.pattern.registry import DEFAULT_FUSION_PATTERNS
|
|
9
|
+
from .version import __version__
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class OnnxSlimKwargs(TypedDict, total=False):
|
|
13
|
+
model_check: bool
|
|
14
|
+
input_shapes: Dict[str, List[int]]
|
|
15
|
+
inputs: List[str]
|
|
16
|
+
outputs: List[str]
|
|
17
|
+
no_shape_infer: bool
|
|
18
|
+
skip_optimizations: List[str]
|
|
19
|
+
dtype: Literal["float16", "float32", "uint8", "int8"]
|
|
20
|
+
skip_fusion_patterns: List[str]
|
|
21
|
+
size_threshold: int
|
|
22
|
+
inspect: bool
|
|
23
|
+
dump_to_disk: bool
|
|
24
|
+
save_as_external_data: bool
|
|
25
|
+
model_check_inputs: Optional[List[str]]
|
|
26
|
+
verbose: bool
|
|
27
|
+
|
|
28
|
+
def _get_inner_type(arg_type):
|
|
29
|
+
if get_origin(arg_type) is Union:
|
|
30
|
+
return next((t for t in get_args(arg_type) if t is not type(None)), str)
|
|
31
|
+
return arg_type
|
|
7
32
|
|
|
8
33
|
|
|
9
34
|
@dataclass
|
|
@@ -31,14 +56,24 @@ class OptimizationArguments:
|
|
|
31
56
|
"""
|
|
32
57
|
|
|
33
58
|
no_shape_infer: bool = field(default=False, metadata={"help": "whether to disable shape_infer, default false."})
|
|
34
|
-
|
|
35
|
-
default=
|
|
59
|
+
skip_optimizations: Optional[List[str]] = field(
|
|
60
|
+
default=None,
|
|
61
|
+
metadata={
|
|
62
|
+
"help": "whether to skip some optimizations",
|
|
63
|
+
"choices": list(OptimizationSettings.keys()),
|
|
64
|
+
},
|
|
36
65
|
)
|
|
37
66
|
skip_fusion_patterns: Optional[List[str]] = field(
|
|
38
67
|
default=None,
|
|
39
68
|
metadata={
|
|
40
69
|
"help": "whether to skip the fusion of some patterns",
|
|
41
|
-
"choices": list(
|
|
70
|
+
"choices": list(DEFAULT_FUSION_PATTERNS.keys()),
|
|
71
|
+
},
|
|
72
|
+
)
|
|
73
|
+
size_threshold: int = field(
|
|
74
|
+
default=None,
|
|
75
|
+
metadata={
|
|
76
|
+
"help": "size threshold in bytes, size larger than this value will not be folded, default None, which means fold all constants",
|
|
42
77
|
},
|
|
43
78
|
)
|
|
44
79
|
|
|
@@ -109,8 +144,11 @@ class CheckerArguments:
|
|
|
109
144
|
verbose: bool = field(default=False, metadata={"help": "verbose mode, default False."})
|
|
110
145
|
|
|
111
146
|
|
|
112
|
-
class ArgumentParser:
|
|
113
|
-
def __init__(self, *argument_dataclasses: Type):
|
|
147
|
+
class OnnxSlimArgumentParser(ArgumentParser):
|
|
148
|
+
def __init__(self, *argument_dataclasses: Type, **kwargs):
|
|
149
|
+
if "formatter_class" not in kwargs:
|
|
150
|
+
kwargs["formatter_class"] = ArgumentDefaultsHelpFormatter
|
|
151
|
+
super().__init__(**kwargs)
|
|
114
152
|
self.argument_dataclasses = argument_dataclasses
|
|
115
153
|
self.parser = argparse.ArgumentParser(
|
|
116
154
|
description="OnnxSlim: A Toolkit to Help Optimizer Onnx Model",
|
|
@@ -120,13 +158,19 @@ class ArgumentParser:
|
|
|
120
158
|
|
|
121
159
|
def _add_arguments(self):
|
|
122
160
|
for dataclass_type in self.argument_dataclasses:
|
|
161
|
+
if dataclass_type is ModelArguments:
|
|
162
|
+
continue
|
|
123
163
|
for field_name, field_def in dataclass_type.__dataclass_fields__.items():
|
|
124
|
-
arg_type = field_def.type
|
|
164
|
+
arg_type = _get_inner_type(field_def.type)
|
|
125
165
|
default_value = field_def.default if field_def.default is not field_def.default_factory else None
|
|
126
166
|
help_text = field_def.metadata.get("help", "")
|
|
127
|
-
nargs = "+" if arg_type ==
|
|
167
|
+
nargs = "+" if get_origin(arg_type) == list else None
|
|
128
168
|
choices = field_def.metadata.get("choices", None)
|
|
129
|
-
|
|
169
|
+
if choices and default_value is not None and default_value not in choices:
|
|
170
|
+
raise ValueError(
|
|
171
|
+
f"Invalid default value '{default_value}' for argument '{field_name}'. Must be one of {choices}."
|
|
172
|
+
)
|
|
173
|
+
arg_type = get_args(arg_type)[0] if get_args(arg_type) else arg_type
|
|
130
174
|
if arg_type == bool:
|
|
131
175
|
self.parser.add_argument(
|
|
132
176
|
f"--{field_name.replace('_', '-')}",
|
|
@@ -137,7 +181,7 @@ class ArgumentParser:
|
|
|
137
181
|
else:
|
|
138
182
|
self.parser.add_argument(
|
|
139
183
|
f"--{field_name.replace('_', '-')}",
|
|
140
|
-
type=arg_type
|
|
184
|
+
type=arg_type,
|
|
141
185
|
default=default_value,
|
|
142
186
|
nargs=nargs,
|
|
143
187
|
choices=choices,
|
|
@@ -147,9 +191,17 @@ class ArgumentParser:
|
|
|
147
191
|
# Add positional arguments separately for ModelArguments
|
|
148
192
|
self.parser.add_argument("input_model", help="input onnx model")
|
|
149
193
|
self.parser.add_argument("output_model", nargs="?", default=None, help="output onnx model")
|
|
150
|
-
self.parser.add_argument("-v", "--version", action="version", version=
|
|
194
|
+
self.parser.add_argument("-v", "--version", action="version", version=__version__)
|
|
151
195
|
|
|
152
196
|
def parse_args_into_dataclasses(self):
|
|
197
|
+
# Pre-parse arguments to check for `--inspect`
|
|
198
|
+
pre_parsed_args, _ = self.parser.parse_known_args()
|
|
199
|
+
if pre_parsed_args.inspect:
|
|
200
|
+
for action in self.parser._actions:
|
|
201
|
+
if action.dest == "input_model":
|
|
202
|
+
action.nargs = "+"
|
|
203
|
+
break
|
|
204
|
+
|
|
153
205
|
args = self.parser.parse_args()
|
|
154
206
|
args_dict = vars(args)
|
|
155
207
|
|
|
@@ -1,14 +1,17 @@
|
|
|
1
|
-
from
|
|
1
|
+
from __future__ import annotations
|
|
2
2
|
|
|
3
3
|
import onnx
|
|
4
4
|
|
|
5
|
+
from onnxslim.argparser import OnnxSlimKwargs
|
|
5
6
|
|
|
6
|
-
|
|
7
|
+
|
|
8
|
+
def slim(model: str | onnx.ModelProto | list[str | onnx.ModelProto], *args, **kwargs: OnnxSlimKwargs):
|
|
7
9
|
import os
|
|
8
10
|
import time
|
|
9
11
|
from pathlib import Path
|
|
10
12
|
|
|
11
13
|
from onnxslim.core import (
|
|
14
|
+
OptimizationSettings,
|
|
12
15
|
convert_data_format,
|
|
13
16
|
freeze,
|
|
14
17
|
input_modification,
|
|
@@ -18,6 +21,7 @@ def slim(model: Union[str, onnx.ModelProto], *args, **kwargs):
|
|
|
18
21
|
shape_infer,
|
|
19
22
|
)
|
|
20
23
|
from onnxslim.utils import (
|
|
24
|
+
TensorInfo,
|
|
21
25
|
check_onnx,
|
|
22
26
|
check_point,
|
|
23
27
|
check_result,
|
|
@@ -27,6 +31,7 @@ def slim(model: Union[str, onnx.ModelProto], *args, **kwargs):
|
|
|
27
31
|
print_model_info_as_table,
|
|
28
32
|
save,
|
|
29
33
|
summarize_model,
|
|
34
|
+
update_outputs_dims,
|
|
30
35
|
)
|
|
31
36
|
|
|
32
37
|
output_model = args[0] if len(args) > 0 else kwargs.get("output_model", None)
|
|
@@ -35,10 +40,12 @@ def slim(model: Union[str, onnx.ModelProto], *args, **kwargs):
|
|
|
35
40
|
inputs = kwargs.get("inputs", None)
|
|
36
41
|
outputs = kwargs.get("outputs", None)
|
|
37
42
|
no_shape_infer = kwargs.get("no_shape_infer", False)
|
|
38
|
-
|
|
43
|
+
skip_optimizations = kwargs.get("skip_optimizations", None)
|
|
39
44
|
dtype = kwargs.get("dtype", None)
|
|
40
45
|
skip_fusion_patterns = kwargs.get("skip_fusion_patterns", None)
|
|
41
|
-
|
|
46
|
+
size_threshold = kwargs.get("size_threshold", None)
|
|
47
|
+
size_threshold = int(size_threshold) if size_threshold else None
|
|
48
|
+
kwargs.get("inspect", False)
|
|
42
49
|
dump_to_disk = kwargs.get("dump_to_disk", False)
|
|
43
50
|
save_as_external_data = kwargs.get("save_as_external_data", False)
|
|
44
51
|
model_check_inputs = kwargs.get("model_check_inputs", None)
|
|
@@ -48,24 +55,37 @@ def slim(model: Union[str, onnx.ModelProto], *args, **kwargs):
|
|
|
48
55
|
|
|
49
56
|
MAX_ITER = int(os.getenv("ONNXSLIM_MAX_ITER")) if os.getenv("ONNXSLIM_MAX_ITER") else 10
|
|
50
57
|
|
|
51
|
-
|
|
52
|
-
model_name = Path(model).name
|
|
53
|
-
model = onnx.load(model)
|
|
54
|
-
else:
|
|
55
|
-
model_name = "OnnxModel"
|
|
58
|
+
start_time = time.time()
|
|
56
59
|
|
|
57
|
-
|
|
60
|
+
def get_info(model, inspect=False):
|
|
61
|
+
if isinstance(model, str):
|
|
62
|
+
model_name = Path(model).name
|
|
63
|
+
model = onnx.load(model)
|
|
64
|
+
else:
|
|
65
|
+
model_name = "OnnxModel"
|
|
58
66
|
|
|
59
|
-
|
|
67
|
+
freeze(model)
|
|
68
|
+
|
|
69
|
+
if not inspect:
|
|
70
|
+
return model_name, model
|
|
71
|
+
|
|
72
|
+
model_info = summarize_model(model, model_name)
|
|
73
|
+
|
|
74
|
+
return model_info
|
|
60
75
|
|
|
61
|
-
if
|
|
62
|
-
|
|
76
|
+
if isinstance(model, list):
|
|
77
|
+
model_info_list = [get_info(m, inspect=True) for m in model]
|
|
63
78
|
|
|
64
|
-
if inspect:
|
|
65
|
-
print_model_info_as_table(model_name, [float_info])
|
|
66
79
|
if dump_to_disk:
|
|
67
|
-
dump_model_info_to_disk(
|
|
68
|
-
|
|
80
|
+
[dump_model_info_to_disk(info) for info in model_info_list]
|
|
81
|
+
|
|
82
|
+
print_model_info_as_table(model_info_list)
|
|
83
|
+
|
|
84
|
+
return
|
|
85
|
+
else:
|
|
86
|
+
model_name, model = get_info(model)
|
|
87
|
+
if output_model:
|
|
88
|
+
original_info = summarize_model(model, model_name)
|
|
69
89
|
|
|
70
90
|
if inputs:
|
|
71
91
|
model = input_modification(model, inputs)
|
|
@@ -79,14 +99,17 @@ def slim(model: Union[str, onnx.ModelProto], *args, **kwargs):
|
|
|
79
99
|
if model_check:
|
|
80
100
|
input_data_dict, raw_onnx_output, model = check_onnx(model, model_check_inputs)
|
|
81
101
|
|
|
102
|
+
output_info = {TensorInfo(o).name: TensorInfo(o).shape for o in model.graph.output}
|
|
103
|
+
|
|
82
104
|
if not no_shape_infer:
|
|
83
105
|
model = shape_infer(model)
|
|
84
106
|
|
|
85
|
-
|
|
107
|
+
OptimizationSettings.reset(skip_optimizations)
|
|
108
|
+
if OptimizationSettings.enabled():
|
|
86
109
|
graph_check_point = check_point(model)
|
|
87
110
|
while MAX_ITER > 0:
|
|
88
111
|
logger.debug(f"iter: {MAX_ITER}")
|
|
89
|
-
model = optimize(model, skip_fusion_patterns)
|
|
112
|
+
model = optimize(model, skip_fusion_patterns, size_threshold)
|
|
90
113
|
if not no_shape_infer:
|
|
91
114
|
model = shape_infer(model)
|
|
92
115
|
graph = check_point(model)
|
|
@@ -101,21 +124,23 @@ def slim(model: Union[str, onnx.ModelProto], *args, **kwargs):
|
|
|
101
124
|
if dtype:
|
|
102
125
|
model = convert_data_format(model, dtype)
|
|
103
126
|
|
|
127
|
+
model = update_outputs_dims(model, output_dims=output_info)
|
|
128
|
+
|
|
104
129
|
if model_check:
|
|
105
130
|
slimmed_onnx_output, model = onnxruntime_inference(model, input_data_dict)
|
|
106
|
-
check_result(raw_onnx_output, slimmed_onnx_output)
|
|
131
|
+
if not check_result(raw_onnx_output, slimmed_onnx_output):
|
|
132
|
+
return None
|
|
107
133
|
|
|
108
134
|
if not output_model:
|
|
109
135
|
return model
|
|
110
136
|
|
|
111
|
-
slimmed_info = summarize_model(model)
|
|
137
|
+
slimmed_info = summarize_model(model, output_model)
|
|
112
138
|
save(model, output_model, model_check, save_as_external_data, slimmed_info)
|
|
113
139
|
|
|
114
140
|
end_time = time.time()
|
|
115
141
|
elapsed_time = end_time - start_time
|
|
116
142
|
print_model_info_as_table(
|
|
117
|
-
|
|
118
|
-
[float_info, slimmed_info],
|
|
143
|
+
[original_info, slimmed_info],
|
|
119
144
|
elapsed_time,
|
|
120
145
|
)
|
|
121
146
|
|
|
@@ -123,26 +148,26 @@ def slim(model: Union[str, onnx.ModelProto], *args, **kwargs):
|
|
|
123
148
|
def main():
|
|
124
149
|
"""Entry point for the OnnxSlim toolkit, processes command-line arguments and passes them to the slim function."""
|
|
125
150
|
from onnxslim.argparser import (
|
|
126
|
-
ArgumentParser,
|
|
127
151
|
CheckerArguments,
|
|
128
152
|
ModelArguments,
|
|
129
153
|
ModificationArguments,
|
|
154
|
+
OnnxSlimArgumentParser,
|
|
130
155
|
OptimizationArguments,
|
|
131
156
|
)
|
|
132
157
|
|
|
133
|
-
argument_parser =
|
|
158
|
+
argument_parser = OnnxSlimArgumentParser(
|
|
159
|
+
ModelArguments, OptimizationArguments, ModificationArguments, CheckerArguments
|
|
160
|
+
)
|
|
134
161
|
model_args, optimization_args, modification_args, checker_args = argument_parser.parse_args_into_dataclasses()
|
|
135
162
|
|
|
136
|
-
if checker_args.inspect and model_args.output_model:
|
|
137
|
-
argument_parser.error("--inspect and output_model are mutually exclusive")
|
|
138
|
-
|
|
139
163
|
if not checker_args.inspect and checker_args.dump_to_disk:
|
|
140
164
|
argument_parser.error("dump_to_disk can only be used with --inspect")
|
|
141
165
|
|
|
142
|
-
if not optimization_args.no_shape_infer
|
|
143
|
-
from onnxslim.utils import check_onnx_compatibility
|
|
166
|
+
if not optimization_args.no_shape_infer:
|
|
167
|
+
from onnxslim.utils import check_onnx_compatibility, is_onnxruntime_available
|
|
144
168
|
|
|
145
|
-
|
|
169
|
+
if is_onnxruntime_available():
|
|
170
|
+
check_onnx_compatibility()
|
|
146
171
|
|
|
147
172
|
slim(
|
|
148
173
|
model_args.input_model,
|