Skip to content

Commit af0aea4

Browse files
authored
Fix bugs in ONNXRT examples (#1438)
Signed-off-by: yuwenzho <yuwen.zhou@intel.com>
1 parent 4f2c35d commit af0aea4

File tree

12 files changed

+80
-14
lines changed

12 files changed

+80
-14
lines changed

examples/.config/model_params_onnxrt.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@
486486
"unet": {
487487
"model_src_dir": "image_recognition/unet/quantization/ptq_static",
488488
"dataset_location": "/tf_dataset2/datasets/imagenet/ImagenetRaw/ILSVRC2012_img_val",
489-
"input_model": "/tf_dataset2/models/onnx/unet/model.onnx",
489+
"input_model": "/tf_dataset2/models/onnx/unet/unet-export.onnx",
490490
"main_script": "main.py",
491491
"batch_size": 1
492492
},

examples/onnxrt/image_recognition/unet/quantization/ptq_static/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pip install -r requirements.txt
1616
## 2. Prepare Model
1717

1818
```bash
19-
python prepare_model.py --input_model='CompVis/stable-diffusion-v1-4' --output_model='.'
19+
python prepare_model.py --input_model='CompVis/stable-diffusion-v1-4' --output_model='unet-export.onnx'
2020
```
2121

2222
# Run

examples/onnxrt/image_recognition/unet/quantization/ptq_static/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class Dataloader:
3232
def __init__(self, batch_size):
3333
self.batch_size = batch_size
3434
shape = [[batch_size, 4, 64, 64], [batch_size], [batch_size, 77, 768]]
35-
dtype = ['float32', 'int64', 'float32']
35+
dtype = ['float32', 'float32', 'float32']
3636
self.dataset = []
3737
for idx in range(0, len(shape)):
3838
tensor = np.random.uniform(size=shape[idx])

examples/onnxrt/image_recognition/unet/quantization/ptq_static/prepare_model.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import argparse
22
import os
3+
import shutil
34
import subprocess
45

56

@@ -9,10 +10,28 @@ def parse_arguments():
910
parser.add_argument("--output_model", type=str, required=True)
1011
return parser.parse_args()
1112

13+
def move_and_rename_model(source_folder, destination_folder):
14+
if not os.path.exists(source_folder):
15+
raise RuntimeError("{} path is not exists".format(source_folder))
16+
for file_name in os.listdir(source_folder):
17+
source_file = os.path.join(source_folder, file_name)
18+
destination_file = os.path.join(destination_folder, file_name)
19+
20+
if os.path.isdir(source_file):
21+
continue
22+
23+
shutil.move(source_file, destination_file)
24+
25+
if file_name == "model.onnx":
26+
new_file_name = "unet-export.onnx"
27+
new_file_path = os.path.join(destination_folder, new_file_name)
28+
os.rename(destination_file, new_file_path)
1229

1330
def prepare_model(input_model, output_model):
1431
# Use [tf2onnx tool](https://github.com/onnx/tensorflow-onnx) to convert tflite to onnx model.
1532
print("\nexport model...")
33+
34+
export_file = "prepare_unet"
1635
subprocess.run(
1736
[
1837
"git",
@@ -34,11 +53,18 @@ def prepare_model(input_model, output_model):
3453
"--model_path",
3554
input_model,
3655
"--output_path",
37-
output_model,
56+
export_file,
3857
],
3958
stdout=subprocess.PIPE,
4059
text=True,
4160
)
61+
62+
move_and_rename_model(os.path.join(export_file, "unet"), os.path.dirname(output_model))
63+
try:
64+
shutil.rmtree(export_file, ignore_errors=True)
65+
except OSError as e:
66+
raise e
67+
4268
assert os.path.exists(output_model), f"Export failed! {output_model} doesn't exist!"
4369

4470

examples/onnxrt/nlp/huggingface_model/language_modeling/quantization/ptq_dynamic/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Supported model identifier from [huggingface.co](https://huggingface.co/):
2121
| gpt2 |
2222
| distilgpt2 |
2323

24-
Require transformers==3.2.0.
24+
Require python <=3.8 and transformers==3.2.0.
2525

2626
```shell
2727
python prepare_model.py --input_model=gpt2 --output_model=gpt2.onnx # or other supported model identifier

examples/onnxrt/nlp/huggingface_model/language_modeling/quantization/ptq_static/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Supported model identifier from [huggingface.co](https://huggingface.co/):
2121
| gpt2 |
2222
| distilgpt2 |
2323

24-
Require transformers==3.2.0.
24+
Require python <=3.8 and transformers==3.2.0.
2525

2626
```shell
2727
python prepare_model.py --input_model=gpt2 --output_model=gpt2.onnx # or other supported model identifier

examples/onnxrt/nlp/huggingface_model/text_classification/quantization/ptq_dynamic/prepare_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,6 @@ def export_onnx_model(args, model):
8989

9090
if args.input_model == 'Intel/bart-large-mrpc':
9191
import os
92-
os.system('python -m transformers.onnx --model=Intel/bart-large-mrpc --feature=sequence-classification bart-large-mrpc/')
92+
os.system('python -m transformers.onnx --model=Intel/bart-large-mrpc --feature=sequence-classification --export_with_transformers bart-large-mrpc/')
9393
else:
9494
export_onnx_model(args, model)

examples/onnxrt/nlp/huggingface_model/text_classification/quantization/ptq_static/prepare_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,6 @@ def export_onnx_model(args, model):
8888

8989
if args.input_model == 'Intel/bart-large-mrpc':
9090
import os
91-
os.system('python -m transformers.onnx --model=Intel/bart-large-mrpc --feature=sequence-classification bart-large-mrpc/')
91+
os.system('python -m transformers.onnx --model=Intel/bart-large-mrpc --feature=sequence-classification --export_with_transformers bart-large-mrpc/')
9292
else:
9393
export_onnx_model(args, model)

examples/onnxrt/nlp/onnx_model_zoo/gpt2/quantization/ptq_dynamic/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pip install -r requirements.txt
1515
## 2. Prepare Model
1616

1717
Use `prepare_model.py` script for ONNX model conversion.
18-
Require transformers==3.2.0.
18+
Require python <=3.8 and transformers==3.2.0.
1919

2020
```shell
2121
python prepare_model.py

examples/onnxrt/nlp/roberta/quantization/ptq_dynamic/prepare_model.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import argparse
2-
import os
2+
import re
33
import subprocess
44

55
import torch
@@ -25,6 +25,20 @@ def parse_arguments():
2525
help='Maximum length of the sentence pairs')
2626
return parser.parse_args()
2727

28+
def comment_out_line(filepath, code):
29+
modified_lines = []
30+
31+
with open(filepath, 'r') as file:
32+
lines = file.readlines()
33+
file.seek(0)
34+
for line in lines:
35+
if re.match(code, line.strip()):
36+
line = "#" + line
37+
modified_lines.append(line)
38+
39+
with open(filepath, 'w') as file:
40+
file.writelines(modified_lines)
41+
2842
def prepare_model(input_model, output_model, task_name):
2943
print("\nexport model...")
3044
subprocess.run(
@@ -42,6 +56,10 @@ def prepare_model(input_model, output_model, task_name):
4256
text=True,
4357
)
4458

59+
# remove transformers min version check
60+
comment_out_line("my_transformers/examples/pytorch/text-classification/run_glue.py",
61+
r"check_min_version\(.*\)")
62+
4563
subprocess.run(
4664
[
4765
"python",

0 commit comments

Comments
 (0)