Skip to content

Commit 2e6e432

Browse files
committed
fix bugs in ort examples
Signed-off-by: yuwenzho <[email protected]>
1 parent cbb69bf commit 2e6e432

File tree

7 files changed

+74
-8
lines changed

7 files changed

+74
-8
lines changed

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/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/roberta/quantization/ptq_dynamic/prepare_model.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,21 @@ 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+
import pdb;pdb.set_trace()
37+
line = "#" + line
38+
modified_lines.append(line)
39+
40+
with open(filepath, 'w') as file:
41+
file.writelines(modified_lines)
42+
2843
def prepare_model(input_model, output_model, task_name):
2944
print("\nexport model...")
3045
subprocess.run(
@@ -42,6 +57,10 @@ def prepare_model(input_model, output_model, task_name):
4257
text=True,
4358
)
4459

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

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

Lines changed: 20 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,21 @@ 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+
import pdb;pdb.set_trace()
37+
line = "#" + line
38+
modified_lines.append(line)
39+
40+
with open(filepath, 'w') as file:
41+
file.writelines(modified_lines)
42+
2843
def prepare_model(input_model, output_model, task_name):
2944
print("\nexport model...")
3045
subprocess.run(
@@ -42,6 +57,10 @@ def prepare_model(input_model, output_model, task_name):
4257
text=True,
4358
)
4459

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

neural_compressor/adaptor/ox_utils/operators/lstm.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,12 @@ def convert(self, convert_format):
128128

129129
kwargs = {}
130130
for attribute in node.attribute:
131-
kwargs.update(attribute_to_kwarg(attribute))
132-
kwargs["domain"] = ms_domain
131+
if attribute.name == "layout":
132+
continue
133+
kwarg = attribute_to_kwarg(attribute)
134+
kwargs.update(kwarg)
133135

134136
quant_lstm_name = node.name + "_quant"
135-
quant_lstm_node = onnx.helper.make_node("DynamicQuantizeLSTM", inputs, node.output, quant_lstm_name, **kwargs)
137+
quant_lstm_node = onnx.helper.make_node("DynamicQuantizeLSTM", inputs, node.output, quant_lstm_name, domain="com.microsoft", **kwargs)
136138
self.quantizer.remove_nodes.append(node)
137139
self.quantizer.new_nodes.append(quant_lstm_node)

0 commit comments

Comments
 (0)