From 589eb626f9f34db827dc50bfa425d0ec86fcdbfa Mon Sep 17 00:00:00 2001 From: Fangjun Kuang Date: Tue, 9 Apr 2024 14:55:58 +0800 Subject: [PATCH] Install onnxoptimizer --- .github/scripts/audioset/AT/run.sh | 2 ++ .github/scripts/docker/Dockerfile | 1 + docker/torch1.12.1-cuda11.3.dockerfile | 1 + docker/torch1.13.0-cuda11.6.dockerfile | 1 + docker/torch1.9.0-cuda10.2.dockerfile | 1 + docker/torch2.0.0-cuda11.7.dockerfile | 1 + docker/torch2.1.0-cuda11.8.dockerfile | 1 + docker/torch2.1.0-cuda12.1.dockerfile | 1 + docker/torch2.2.0-cuda11.8.dockerfile | 1 + docker/torch2.2.0-cuda12.1.dockerfile | 1 + docker/torch2.2.1-cuda11.8.dockerfile | 1 + docker/torch2.2.1-cuda12.1.dockerfile | 1 + docker/torch2.2.2-cuda11.8.dockerfile | 1 + docker/torch2.2.2-cuda12.1.dockerfile | 1 + egs/audioset/AT/zipformer/export-onnx.py | 10 ++++++---- requirements.txt | 9 +++++---- 16 files changed, 26 insertions(+), 8 deletions(-) diff --git a/.github/scripts/audioset/AT/run.sh b/.github/scripts/audioset/AT/run.sh index 81cafd64b..f09969ec9 100755 --- a/.github/scripts/audioset/AT/run.sh +++ b/.github/scripts/audioset/AT/run.sh @@ -2,6 +2,8 @@ set -ex +python3 -m pip install onnxoptimizer + log() { # This function is from espnet local fname=${BASH_SOURCE[1]##*/} diff --git a/.github/scripts/docker/Dockerfile b/.github/scripts/docker/Dockerfile index f64446e7e..b918a10d8 100644 --- a/.github/scripts/docker/Dockerfile +++ b/.github/scripts/docker/Dockerfile @@ -49,6 +49,7 @@ RUN pip install --no-cache-dir \ multi_quantization \ numba \ numpy \ + onnxoptimizer \ onnx \ onnxmltools \ onnxruntime \ diff --git a/docker/torch1.12.1-cuda11.3.dockerfile b/docker/torch1.12.1-cuda11.3.dockerfile index 33ecbf4d1..1c83d4877 100644 --- a/docker/torch1.12.1-cuda11.3.dockerfile +++ b/docker/torch1.12.1-cuda11.3.dockerfile @@ -55,6 +55,7 @@ RUN pip install --no-cache-dir \ onnx \ onnxruntime \ onnxmltools \ + onnxoptimizer \ multi_quantization \ typeguard \ numpy \ diff --git a/docker/torch1.13.0-cuda11.6.dockerfile b/docker/torch1.13.0-cuda11.6.dockerfile index b4d62b0bc..450513e6a 100644 --- a/docker/torch1.13.0-cuda11.6.dockerfile +++ b/docker/torch1.13.0-cuda11.6.dockerfile @@ -55,6 +55,7 @@ RUN pip install --no-cache-dir \ onnx \ onnxruntime \ onnxmltools \ + onnxoptimizer \ multi_quantization \ typeguard \ numpy \ diff --git a/docker/torch1.9.0-cuda10.2.dockerfile b/docker/torch1.9.0-cuda10.2.dockerfile index 4d2d3058a..36a533a31 100644 --- a/docker/torch1.9.0-cuda10.2.dockerfile +++ b/docker/torch1.9.0-cuda10.2.dockerfile @@ -69,6 +69,7 @@ RUN pip uninstall -y tqdm && \ onnx \ onnxruntime \ onnxmltools \ + onnxoptimizer \ multi_quantization \ typeguard \ numpy \ diff --git a/docker/torch2.0.0-cuda11.7.dockerfile b/docker/torch2.0.0-cuda11.7.dockerfile index 31ff09ac6..75335b932 100644 --- a/docker/torch2.0.0-cuda11.7.dockerfile +++ b/docker/torch2.0.0-cuda11.7.dockerfile @@ -56,6 +56,7 @@ RUN pip install --no-cache-dir \ onnx \ onnxruntime \ onnxmltools \ + onnxoptimizer \ multi_quantization \ typeguard \ numpy \ diff --git a/docker/torch2.1.0-cuda11.8.dockerfile b/docker/torch2.1.0-cuda11.8.dockerfile index 83b64a8d2..1d6deae0d 100644 --- a/docker/torch2.1.0-cuda11.8.dockerfile +++ b/docker/torch2.1.0-cuda11.8.dockerfile @@ -56,6 +56,7 @@ RUN pip install --no-cache-dir \ onnx \ onnxruntime \ onnxmltools \ + onnxoptimizer \ multi_quantization \ typeguard \ numpy \ diff --git a/docker/torch2.1.0-cuda12.1.dockerfile b/docker/torch2.1.0-cuda12.1.dockerfile index ec366a898..f6dcd2a2f 100644 --- a/docker/torch2.1.0-cuda12.1.dockerfile +++ b/docker/torch2.1.0-cuda12.1.dockerfile @@ -56,6 +56,7 @@ RUN pip install --no-cache-dir \ onnx \ onnxruntime \ onnxmltools \ + onnxoptimizer \ multi_quantization \ typeguard \ numpy \ diff --git a/docker/torch2.2.0-cuda11.8.dockerfile b/docker/torch2.2.0-cuda11.8.dockerfile index 143f0e066..72c1add60 100644 --- a/docker/torch2.2.0-cuda11.8.dockerfile +++ b/docker/torch2.2.0-cuda11.8.dockerfile @@ -56,6 +56,7 @@ RUN pip install --no-cache-dir \ onnx \ onnxruntime \ onnxmltools \ + onnxoptimizer \ multi_quantization \ typeguard \ numpy \ diff --git a/docker/torch2.2.0-cuda12.1.dockerfile b/docker/torch2.2.0-cuda12.1.dockerfile index c6d5a771f..ca81afcf4 100644 --- a/docker/torch2.2.0-cuda12.1.dockerfile +++ b/docker/torch2.2.0-cuda12.1.dockerfile @@ -56,6 +56,7 @@ RUN pip install --no-cache-dir \ onnx \ onnxruntime \ onnxmltools \ + onnxoptimizer \ multi_quantization \ typeguard \ numpy \ diff --git a/docker/torch2.2.1-cuda11.8.dockerfile b/docker/torch2.2.1-cuda11.8.dockerfile index d874134d7..713fa1a09 100644 --- a/docker/torch2.2.1-cuda11.8.dockerfile +++ b/docker/torch2.2.1-cuda11.8.dockerfile @@ -56,6 +56,7 @@ RUN pip install --no-cache-dir \ onnx \ onnxruntime \ onnxmltools \ + onnxoptimizer \ multi_quantization \ typeguard \ numpy \ diff --git a/docker/torch2.2.1-cuda12.1.dockerfile b/docker/torch2.2.1-cuda12.1.dockerfile index 6e4ef290a..5006a2180 100644 --- a/docker/torch2.2.1-cuda12.1.dockerfile +++ b/docker/torch2.2.1-cuda12.1.dockerfile @@ -56,6 +56,7 @@ RUN pip install --no-cache-dir \ onnx \ onnxruntime \ onnxmltools \ + onnxoptimizer \ multi_quantization \ typeguard \ numpy \ diff --git a/docker/torch2.2.2-cuda11.8.dockerfile b/docker/torch2.2.2-cuda11.8.dockerfile index bca40a065..78ac20bba 100644 --- a/docker/torch2.2.2-cuda11.8.dockerfile +++ b/docker/torch2.2.2-cuda11.8.dockerfile @@ -56,6 +56,7 @@ RUN pip install --no-cache-dir \ onnx \ onnxruntime \ onnxmltools \ + onnxoptimizer \ multi_quantization \ typeguard \ numpy \ diff --git a/docker/torch2.2.2-cuda12.1.dockerfile b/docker/torch2.2.2-cuda12.1.dockerfile index 4fb8946e7..655bcf933 100644 --- a/docker/torch2.2.2-cuda12.1.dockerfile +++ b/docker/torch2.2.2-cuda12.1.dockerfile @@ -56,6 +56,7 @@ RUN pip install --no-cache-dir \ onnx \ onnxruntime \ onnxmltools \ + onnxoptimizer \ multi_quantization \ typeguard \ numpy \ diff --git a/egs/audioset/AT/zipformer/export-onnx.py b/egs/audioset/AT/zipformer/export-onnx.py index a01311dfc..e2afeeffd 100755 --- a/egs/audioset/AT/zipformer/export-onnx.py +++ b/egs/audioset/AT/zipformer/export-onnx.py @@ -38,6 +38,7 @@ from typing import Dict import k2 import onnx +import onnxoptimizer import torch import torch.nn as nn from onnxruntime.quantization import QuantType, quantize_dynamic @@ -234,12 +235,13 @@ def export_audio_tagging_model_onnx( def optimize_model(filename): - # see https://github.com/microsoft/onnxruntime/issues/1899#issuecomment-534806537 - from onnx import optimizer - + # see + # https://github.com/microsoft/onnxruntime/issues/1899#issuecomment-534806537 + # and + # https://github.com/onnx/onnx/issues/582#issuecomment-937788108 passes = ["extract_constant_to_initializer", "eliminate_unused_initializer"] onnx_model = onnx.load(filename) - optimized_model = optimizer.optimize(onnx_model, passes) + optimized_model = onnxoptimizer.optimize(onnx_model, passes) onnx.save(optimized_model, filename) diff --git a/requirements.txt b/requirements.txt index 6bafa6aca..8410453f9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,13 +8,14 @@ pypinyin==0.50.0 tensorboard typeguard dill -onnx==1.15.0 -onnxruntime==1.16.3 +onnx>=1.15.0 +onnxruntime>=1.16.3 +onnxoptimizer # style check session: black==22.3.0 isort==5.10.1 -flake8==5.0.4 +flake8==5.0.4 # cantonese word segment support -pycantonese==3.4.0 \ No newline at end of file +pycantonese==3.4.0