-
Notifications
You must be signed in to change notification settings - Fork 258
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support > 2GB onnx model and update graph sort (#1359)
- Loading branch information
1 parent
fcfafc5
commit 8d83cc8
Showing
13 changed files
with
371 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
examples/onnxrt/image_recognition/unet/quantization/ptq/main.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
# pylint:disable=redefined-outer-name,logging-format-interpolation | ||
|
||
|
||
import logging | ||
import argparse | ||
|
||
import numpy as np | ||
import onnx | ||
|
||
logger = logging.getLogger(__name__) | ||
logging.basicConfig(format = '%(asctime)s - %(levelname)s - %(name)s - %(message)s', | ||
datefmt = '%m/%d/%Y %H:%M:%S', | ||
level = logging.WARN) | ||
|
||
if __name__ == "__main__": | ||
logger.info("Evaluating ONNXRuntime full precision accuracy and performance:") | ||
parser = argparse.ArgumentParser( | ||
formatter_class=argparse.ArgumentDefaultsHelpFormatter | ||
) | ||
parser.add_argument( | ||
'--model_path', | ||
type=str, | ||
help="Pre-trained mobilenet_v3 model on onnx file" | ||
) | ||
parser.add_argument( | ||
'--benchmark', | ||
action='store_true', \ | ||
default=False | ||
) | ||
parser.add_argument( | ||
'--tune', | ||
action='store_true', \ | ||
default=False, | ||
help="whether quantize the model" | ||
) | ||
parser.add_argument( | ||
'--config', | ||
type=str, | ||
help="config yaml path" | ||
) | ||
parser.add_argument( | ||
'--output_model', | ||
type=str, | ||
help="output model path" | ||
) | ||
parser.add_argument( | ||
'--mode', | ||
type=str, | ||
default='performance', | ||
help="benchmark mode of performance or accuracy" | ||
) | ||
args = parser.parse_args() | ||
if args.benchmark: | ||
from neural_compressor.experimental import Benchmark, common | ||
evaluator = Benchmark(args.config) | ||
evaluator.model = common.Model(args.model_path) | ||
evaluator(args.mode) | ||
|
||
if args.tune: | ||
from neural_compressor.experimental import Quantization, common | ||
|
||
quantize = Quantization(args.config) | ||
quantize.model = common.Model(args.model_path) | ||
q_model = quantize() | ||
q_model.save(args.output_model) | ||
|
31 changes: 31 additions & 0 deletions
31
examples/onnxrt/image_recognition/unet/quantization/ptq/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Evaluate performance of ONNX Runtime(unet) | ||
|
||
This is an experimental example to quantize unet model. We use dummy data to do quantization and evaluation, so the accuracy is not guaranteed. | ||
|
||
### Environment | ||
onnx: 1.12.0 | ||
onnxruntime: 1.12.1 | ||
|
||
### Prepare model | ||
|
||
```bash | ||
git clone https://github.com/huggingface/diffusers.git | ||
cd diffusers/scripts/ | ||
python convert_stable_diffusion_checkpoint_to_onnx.py --model_path "CompVis/stable-diffusion-v1-4" --output_path /workdir/output_path | ||
``` | ||
|
||
### Quantization | ||
|
||
```bash | ||
bash run_tuning.sh --input_model=/workdir/output_path/unet/model.onnx \ | ||
--config=unet.yaml \ | ||
--output_model=path/to/save | ||
``` | ||
|
||
### Benchmark | ||
|
||
```bash | ||
bash run_benchmark.sh --input_model=/workdir/output_path/unet/model.onnx \ | ||
--config=unet.yaml \ | ||
--mode=performance | ||
``` |
3 changes: 3 additions & 0 deletions
3
examples/onnxrt/image_recognition/unet/quantization/ptq/requirements.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
onnx==1.12.0 | ||
onnxruntime==1.12.0 | ||
onnxruntime-extensions; python_version < '3.10' |
41 changes: 41 additions & 0 deletions
41
examples/onnxrt/image_recognition/unet/quantization/ptq/run_benchmark.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#!/bin/bash | ||
set -x | ||
|
||
function main { | ||
init_params "$@" | ||
run_benchmark | ||
|
||
} | ||
|
||
# init params | ||
function init_params { | ||
|
||
for var in "$@" | ||
do | ||
case $var in | ||
--config=*) | ||
config=$(echo $var |cut -f2 -d=) | ||
;; | ||
--input_model=*) | ||
input_model=$(echo $var |cut -f2 -d=) | ||
;; | ||
--mode=*) | ||
mode=$(echo $var |cut -f2 -d=) | ||
;; | ||
esac | ||
done | ||
|
||
} | ||
|
||
# run_benchmark | ||
function run_benchmark { | ||
|
||
python main.py \ | ||
--model_path ${input_model} \ | ||
--config ${config} \ | ||
--mode=${mode} \ | ||
--benchmark | ||
|
||
} | ||
|
||
main "$@" |
39 changes: 39 additions & 0 deletions
39
examples/onnxrt/image_recognition/unet/quantization/ptq/run_tuning.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#!/bin/bash | ||
set -x | ||
|
||
function main { | ||
init_params "$@" | ||
run_tuning | ||
|
||
} | ||
|
||
# init params | ||
function init_params { | ||
|
||
for var in "$@" | ||
do | ||
case $var in | ||
--config=*) | ||
config=$(echo $var |cut -f2 -d=) | ||
;; | ||
--input_model=*) | ||
input_model=$(echo $var |cut -f2 -d=) | ||
;; | ||
--output_model=*) | ||
output_model=$(echo $var |cut -f2 -d=) | ||
;; | ||
esac | ||
done | ||
|
||
} | ||
|
||
# run_tuning | ||
function run_tuning { | ||
python main.py \ | ||
--model_path ${input_model} \ | ||
--output_model ${output_model} \ | ||
--config ${config} \ | ||
--tune | ||
} | ||
|
||
main "$@" |
57 changes: 57 additions & 0 deletions
57
examples/onnxrt/image_recognition/unet/quantization/ptq/unet.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# | ||
# Copyright (c) 2021 Intel Corporation | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
version: 1.0 | ||
|
||
model: # mandatory. used to specify model specific information. | ||
name: unet | ||
framework: onnxrt_qlinearops # mandatory. supported values are tensorflow, pytorch, pytorch_ipex, onnxrt_integer, onnxrt_qlinear or mxnet; allow new framework backend extension. | ||
|
||
quantization: # optional. tuning constraints on model-wise for advance user to reduce tuning space. | ||
approach: post_training_static_quant # optional. default value is post_training_static_quant. | ||
calibration: | ||
dataloader: | ||
batch_size: 1 | ||
dataset: | ||
dummy: | ||
shape: [[1, 4, 64, 64], [1], [1, 77, 768]] | ||
dtype: ['float32', 'int64', 'float32'] | ||
|
||
evaluation: # optional. required if user doesn't provide eval_func in neural_compressor.Quantization. | ||
accuracy: # optional. required if user doesn't provide eval_func in neural_compressor.Quantization. | ||
dataloader: | ||
batch_size: 1 | ||
dataset: | ||
dummy: | ||
shape: [[1, 4, 64, 64], [1], [1, 77, 768]] | ||
dtype: ['float32', 'int64', 'float32'] | ||
|
||
performance: # optional. used to benchmark performance of passing model. | ||
warmup: 10 | ||
iteration: 500 | ||
configs: | ||
cores_per_instance: 4 | ||
num_of_instance: 7 | ||
dataloader: | ||
batch_size: 1 | ||
dataset: | ||
dummy: | ||
shape: [[1, 4, 64, 64], [1], [1, 77, 768]] | ||
dtype: ['float32', 'int64', 'float32'] | ||
|
||
tuning: | ||
exit_policy: | ||
performance_only: True | ||
random_seed: 9527 # optional. random seed for deterministic tuning. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.