Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/.config/model_params_onnxrt.json
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@
"unet": {
"model_src_dir": "image_recognition/unet/quantization/ptq_static",
"dataset_location": "/tf_dataset2/datasets/imagenet/ImagenetRaw/ILSVRC2012_img_val",
"input_model": "/tf_dataset2/models/onnx/unet/model.onnx",
"input_model": "/tf_dataset2/models/onnx/unet/unet-export.onnx",
"main_script": "main.py",
"batch_size": 1
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pip install -r requirements.txt
## 2. Prepare Model

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

# Run
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Dataloader:
def __init__(self, batch_size):
self.batch_size = batch_size
shape = [[batch_size, 4, 64, 64], [batch_size], [batch_size, 77, 768]]
dtype = ['float32', 'int64', 'float32']
dtype = ['float32', 'float32', 'float32']
self.dataset = []
for idx in range(0, len(shape)):
tensor = np.random.uniform(size=shape[idx])
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import argparse
import os
import shutil
import subprocess


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

def move_and_rename_model(source_folder, destination_folder):
if not os.path.exists(source_folder):
raise RuntimeError("{} path is not exists".format(source_folder))
for file_name in os.listdir(source_folder):
source_file = os.path.join(source_folder, file_name)
destination_file = os.path.join(destination_folder, file_name)

if os.path.isdir(source_file):
continue

shutil.move(source_file, destination_file)

if file_name == "model.onnx":
new_file_name = "unet-export.onnx"
new_file_path = os.path.join(destination_folder, new_file_name)
os.rename(destination_file, new_file_path)

def prepare_model(input_model, output_model):
# Use [tf2onnx tool](https://github.com/onnx/tensorflow-onnx) to convert tflite to onnx model.
print("\nexport model...")

export_file = "prepare_unet"
subprocess.run(
[
"git",
Expand All @@ -34,11 +53,18 @@ def prepare_model(input_model, output_model):
"--model_path",
input_model,
"--output_path",
output_model,
export_file,
],
stdout=subprocess.PIPE,
text=True,
)

move_and_rename_model(os.path.join(export_file, "unet"), os.path.dirname(output_model))
try:
shutil.rmtree(export_file, ignore_errors=True)
except OSError as e:
raise e

assert os.path.exists(output_model), f"Export failed! {output_model} doesn't exist!"


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Supported model identifier from [huggingface.co](https://huggingface.co/):
| gpt2 |
| distilgpt2 |

Require transformers==3.2.0.
Require python <=3.8 and transformers==3.2.0.

```shell
python prepare_model.py --input_model=gpt2 --output_model=gpt2.onnx # or other supported model identifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Supported model identifier from [huggingface.co](https://huggingface.co/):
| gpt2 |
| distilgpt2 |

Require transformers==3.2.0.
Require python <=3.8 and transformers==3.2.0.

```shell
python prepare_model.py --input_model=gpt2 --output_model=gpt2.onnx # or other supported model identifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,6 @@ def export_onnx_model(args, model):

if args.input_model == 'Intel/bart-large-mrpc':
import os
os.system('python -m transformers.onnx --model=Intel/bart-large-mrpc --feature=sequence-classification bart-large-mrpc/')
os.system('python -m transformers.onnx --model=Intel/bart-large-mrpc --feature=sequence-classification --export_with_transformers bart-large-mrpc/')
else:
export_onnx_model(args, model)
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,6 @@ def export_onnx_model(args, model):

if args.input_model == 'Intel/bart-large-mrpc':
import os
os.system('python -m transformers.onnx --model=Intel/bart-large-mrpc --feature=sequence-classification bart-large-mrpc/')
os.system('python -m transformers.onnx --model=Intel/bart-large-mrpc --feature=sequence-classification --export_with_transformers bart-large-mrpc/')
else:
export_onnx_model(args, model)
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pip install -r requirements.txt
## 2. Prepare Model

Use `prepare_model.py` script for ONNX model conversion.
Require transformers==3.2.0.
Require python <=3.8 and transformers==3.2.0.

```shell
python prepare_model.py
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import argparse
import os
import re
import subprocess

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

def comment_out_line(filepath, code):
modified_lines = []

with open(filepath, 'r') as file:
lines = file.readlines()
file.seek(0)
for line in lines:
if re.match(code, line.strip()):
line = "#" + line
modified_lines.append(line)

with open(filepath, 'w') as file:
file.writelines(modified_lines)

def prepare_model(input_model, output_model, task_name):
print("\nexport model...")
subprocess.run(
Expand All @@ -42,6 +56,10 @@ def prepare_model(input_model, output_model, task_name):
text=True,
)

# remove transformers min version check
comment_out_line("my_transformers/examples/pytorch/text-classification/run_glue.py",
r"check_min_version\(.*\)")

subprocess.run(
[
"python",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import argparse
import os
import re
import subprocess

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

def comment_out_line(filepath, code):
modified_lines = []

with open(filepath, 'r') as file:
lines = file.readlines()
file.seek(0)
for line in lines:
if re.match(code, line.strip()):
line = "#" + line
modified_lines.append(line)

with open(filepath, 'w') as file:
file.writelines(modified_lines)

def prepare_model(input_model, output_model, task_name):
print("\nexport model...")
subprocess.run(
Expand All @@ -42,6 +56,10 @@ def prepare_model(input_model, output_model, task_name):
text=True,
)

# remove transformers min version check
comment_out_line("my_transformers/examples/pytorch/text-classification/run_glue.py",
r"check_min_version\(.*\)")

subprocess.run(
[
"python",
Expand Down
10 changes: 7 additions & 3 deletions neural_compressor/adaptor/ox_utils/operators/lstm.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,14 @@ def convert(self, convert_format):

kwargs = {}
for attribute in node.attribute:
kwargs.update(attribute_to_kwarg(attribute))
kwargs["domain"] = ms_domain
if attribute.name == "layout":
continue
kwarg = attribute_to_kwarg(attribute)
kwargs.update(kwarg)

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