Skip to content

Commit

Permalink
Fix the rest; fix training import order
Browse files Browse the repository at this point in the history
  • Loading branch information
justinchuby committed Jan 16, 2023
1 parent 213cfb6 commit 4b84d82
Show file tree
Hide file tree
Showing 17 changed files with 71 additions and 68 deletions.
14 changes: 6 additions & 8 deletions onnxruntime/python/tools/transformers/bert_perf_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def onnxruntime_inference_with_io_binding(session, all_inputs, output_names, tes
results = []
latency_list = []
device = "cuda" if test_setting.use_gpu else "cpu"
for test_case_id, inputs in enumerate(all_inputs):
for inputs in all_inputs:
result = session.run(output_names, inputs)
results.append(result)
outputs = {}
Expand Down Expand Up @@ -201,7 +201,7 @@ def onnxruntime_inference(session, all_inputs, output_names):

results = []
latency_list = []
for test_case_id, inputs in enumerate(all_inputs):
for inputs in all_inputs:
start_time = timeit.default_timer()
result = session.run(output_names, inputs)
latency = timeit.default_timer() - start_time
Expand Down Expand Up @@ -240,14 +240,12 @@ def run_one_test(model_setting, test_setting, perf_results, all_inputs, intra_op

all_latency_list = []
if test_setting.use_io_binding:
for i in range(test_setting.test_times):
results, latency_list = onnxruntime_inference_with_io_binding(
session, all_inputs, output_names, test_setting
)
for _ in range(test_setting.test_times):
_, latency_list = onnxruntime_inference_with_io_binding(session, all_inputs, output_names, test_setting)
all_latency_list.extend(latency_list)
else:
for i in range(test_setting.test_times):
results, latency_list = onnxruntime_inference(session, all_inputs, output_names)
for _ in range(test_setting.test_times):
_, latency_list = onnxruntime_inference(session, all_inputs, output_names)
all_latency_list.extend(latency_list)

# latency in miliseconds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@

sys.path.append(os.path.join(os.path.dirname(__file__), "..", ".."))

from benchmark_helper import (
from benchmark_helper import ( # noqa: E402
Precision,
create_onnxruntime_session,
get_ort_environment_variables,
prepare_environment,
setup_logger,
)
from quantize_helper import QuantizeHelper
from quantize_helper import QuantizeHelper # noqa: E402

logger = logging.getLogger("")

Expand Down
40 changes: 21 additions & 19 deletions onnxruntime/python/tools/transformers/models/gpt2/gpt2_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import tempfile
import time
from pathlib import Path
from typing import Dict, List, Tuple, Union
from typing import Collection, Dict, List, Tuple, Union

import numpy
import onnx
Expand All @@ -22,12 +22,12 @@

sys.path.append(os.path.join(os.path.dirname(__file__), "..", ".."))

from benchmark_helper import Precision
from float16 import float_to_float16_max_diff
from fusion_options import AttentionMaskFormat
from io_binding_helper import IOBindingHelper
from onnx_model import OnnxModel
from torch_onnx_export_helper import torch_onnx_export
from benchmark_helper import Precision # noqa: E402
from float16 import float_to_float16_max_diff # noqa: E402
from fusion_options import AttentionMaskFormat # noqa: E402
from io_binding_helper import IOBindingHelper # noqa: E402
from onnx_model import OnnxModel # noqa: E402
from torch_onnx_export_helper import torch_onnx_export # noqa: E402

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -551,24 +551,26 @@ def optimize_onnx(
@staticmethod
def auto_mixed_precision(
onnx_model: OnnxModel,
op_block_list: List[str] = [
"Add",
"LayerNormalization",
"SkipLayerNormalization",
"FastGelu",
"EmbedLayerNormalization",
],
op_block_list: Collection[str] = frozenset(
(
"Add",
"LayerNormalization",
"SkipLayerNormalization",
"FastGelu",
"EmbedLayerNormalization",
)
),
):
"""Convert GPT-2 model to mixed precision.
It detects whether original model has fp16 weights, and set parameters for float16 conversion automatically.
Args:
onnx_model (OnnxModel): optimized ONNX model
op_block_list (List[str], optional): operators to compute in fp32. Defaults to ["Add", "LayerNormalization",
"SkipLayerNormalization", "FastGelu", "EmbedLayerNormalization"]
onnx_model: Optimized ONNX model
op_block_list: Operators to compute in fp32. Defaults to {"Add", "LayerNormalization",
"SkipLayerNormalization", "FastGelu", "EmbedLayerNormalization"}.
Returns:
parameters(dict): a dictionary of parameters used in float16 conversion
A dictionary of parameters used in float16 conversion.
"""
op_full_set = set([node.op_type for node in onnx_model.nodes()])
op_full_set = set(node.op_type for node in onnx_model.nodes())
fp32_op_set = set(op_block_list)
fp16_op_set = op_full_set.difference(fp32_op_set)
logger.info(f"fp32 op: {fp32_op_set} fp16 op: {fp16_op_set}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

sys.path.append(os.path.join(os.path.dirname(__file__), "..", ".."))

from benchmark_helper import Precision
from benchmark_helper import Precision # noqa: E402

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -151,7 +151,7 @@ def __init__(
0,
hidden_size // num_attention_heads,
]
for i in range(num_layer):
for _ in range(num_layer):
empty_past = torch.empty(past_shape).type(torch.float16 if is_fp16 else torch.float32)
self.past.append(empty_past.to(device))

Expand Down Expand Up @@ -194,7 +194,7 @@ def add_tensor(input_tensors, torch_tensor, name):
f.write(tensor.SerializeToString())

output_names = [output.name for output in session.get_outputs()]
for i, name in enumerate(output_names):
for i, _ in enumerate(output_names):
tensor = numpy_helper.from_array(
output[i] if isinstance(output[i], numpy.ndarray) else output[i].clone().cpu().numpy()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

sys.path.append(os.path.join(os.path.dirname(__file__), "..", ".."))

from benchmark_helper import create_onnxruntime_session
from benchmark_helper import create_onnxruntime_session # noqa: E402

NON_ZERO_VALUE = str(1)
ZERO_VALUE = str(0)
Expand Down Expand Up @@ -137,8 +137,8 @@ def post_processing(outputs_path, outputs_path_other):
dummy_inputs_fp32 = dummy_inputs_fp16.to_fp32()

# Get GPT-2 model from huggingface using convert_to_onnx.py
os.system("python convert_to_onnx.py -m gpt2 --output gpt2_fp32.onnx -o -p fp32 --use_gpu")
os.system("python convert_to_onnx.py -m gpt2 --output gpt2_fp16.onnx -o -p fp16 --use_gpu")
os.system("python convert_to_onnx.py -m gpt2 --output gpt2_fp32.onnx -o -p fp32 --use_gpu") # noqa: DUO106
os.system("python convert_to_onnx.py -m gpt2 --output gpt2_fp16.onnx -o -p fp16 --use_gpu") # noqa: DUO106

# Specify the directory to dump the node's I/O
outputs_path_fp32_gpu = "./fp32_gpu"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def group_by_self_or_cross(present_key_values):
"""
present_self = []
present_cross = []
for i, present_layer_i in enumerate(present_key_values):
for present_layer_i in present_key_values:
assert len(present_layer_i) == 4, f"Expected to have four items. Got {len(present_layer_i)}"
(
present_key_self,
Expand Down
43 changes: 24 additions & 19 deletions onnxruntime/python/tools/transformers/models/t5/t5_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import os
import sys
from pathlib import Path
from typing import Dict, List, Union
from typing import Collection, Dict, Union

import torch
from t5_decoder import T5Decoder, T5DecoderHelper, T5DecoderInit
Expand All @@ -19,9 +19,9 @@
from onnxruntime import InferenceSession

sys.path.append(os.path.join(os.path.dirname(__file__), "..", ".."))
from float16 import float_to_float16_max_diff
from onnx_model import OnnxModel
from optimizer import optimize_model
from float16 import float_to_float16_max_diff # noqa: E402
from onnx_model import OnnxModel # noqa: E402
from optimizer import optimize_model # noqa: E402

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -150,26 +150,31 @@ def export_onnx(
@staticmethod
def auto_mixed_precision(
onnx_model: OnnxModel,
op_block_list: List[str] = [
"Pow",
"ReduceMean",
"Add",
"Sqrt",
"Div",
"Mul",
"Softmax",
"Relu",
],
op_block_list: Collection[str] = frozenset(
(
"Pow",
"ReduceMean",
"Add",
"Sqrt",
"Div",
"Mul",
"Softmax",
"Relu",
)
),
):
"""Convert model to mixed precision.
It detects whether original model has fp16 precision weights, and set parameters for float16 conversion automatically.
It detects whether original model has fp16 precision weights,
and set parameters for float16 conversion automatically.
Args:
onnx_model (OnnxModel): optimized ONNX model
op_block_list (List[str], optional): . Defaults to ["Pow", "ReduceMean", "Add", "Sqrt", "Div", "Mul", "Softmax", "Relu"]
onnx_model: Optimized ONNX model
op_block_list: Defaults to {"Pow", "ReduceMean", "Add", "Sqrt", "Div", "Mul", "Softmax", "Relu"}
Returns:
parameters(dict): a dictionary of parameters used in float16 conversion
parameters: A dictionary of parameters used in float16 conversion
"""
op_full_set = set([node.op_type for node in onnx_model.nodes()])
op_full_set = set(node.op_type for node in onnx_model.nodes())
fp32_op_set = set(op_block_list)
fp16_op_set = op_full_set.difference(fp32_op_set)
logger.info(f"fp32 op: {fp32_op_set} fp16 op: {fp16_op_set}")
Expand Down
2 changes: 1 addition & 1 deletion onnxruntime/python/tools/transformers/onnx_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from transformers import AutoConfig, AutoTokenizer, LxmertConfig, TransfoXLConfig

sys.path.append(os.path.join(os.path.dirname(__file__), "models", "gpt2"))
from gpt2_helper import PRETRAINED_GPT2_MODELS, GPT2ModelNoPastState, TFGPT2ModelNoPastState
from gpt2_helper import PRETRAINED_GPT2_MODELS, GPT2ModelNoPastState, TFGPT2ModelNoPastState # noqa: E402

os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"

Expand Down
2 changes: 1 addition & 1 deletion onnxruntime/python/tools/transformers/onnx_model_bart.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def fuse(self, normalize_node, input_name_to_nodes, output_name_to_node):
return

other_inputs = []
for i, input in enumerate(normalize_node.input):
for input in normalize_node.input:
if input not in output_name_to_node:
continue
if input == qkv_nodes[0].output[0]:
Expand Down
1 change: 0 additions & 1 deletion onnxruntime/python/tools/transformers/onnx_model_bert.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@ def use_dynamic_axes(self, dynamic_batch_dim="batch_size", dynamic_seq_len="max_
casted=True
) + self.get_graph_inputs_from_fused_nodes(casted=False)

{}
for input in self.model.graph.input:
if input.name in bert_graph_inputs:
dim_proto = input.type.tensor_type.shape.dim[0]
Expand Down
3 changes: 1 addition & 2 deletions onnxruntime/python/tools/transformers/onnx_model_tnlr.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
import logging
from typing import Union

import numpy as np
from fusion_attention import AttentionMask, FusionAttention
from fusion_utils import NumpyHelper
from onnx import NodeProto, TensorProto, helper, numpy_helper
from onnx_model import OnnxModel
from onnx_model_bert import BertOnnxModel

import numpy as np

logger = logging.getLogger(__name__)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
# --------------------------------------------------------------------------

import torch
from packaging.version import Version

TrainingMode = torch.onnx.TrainingMode
from packaging.version import Version


def torch_onnx_export(
Expand Down
4 changes: 2 additions & 2 deletions tools/ci_build/github/android/build_aar_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ def _build_aar(args):
# clean, build, and publish to a local directory
subprocess.run(gradle_command + ["clean"], env=temp_env, shell=use_shell, check=True, cwd=JAVA_ROOT) # noqa: DUO116
subprocess.run(gradle_command + ["build"], env=temp_env, shell=use_shell, check=True, cwd=JAVA_ROOT) # noqa: DUO116
subprocess.run(
subprocess.run( # noqa: DUO116
gradle_command + ["publish"], env=temp_env, shell=use_shell, check=True, cwd=JAVA_ROOT
) # noqa: DUO116
)


def parse_args():
Expand Down
4 changes: 2 additions & 2 deletions tools/doc/rename_folders.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def rename_folder(root):
Returns the list of renamed folders.
"""
found = []
for r, dirs, files in os.walk(root):
for r, dirs, _ in os.walk(root):
for name in dirs:
if name.startswith("_"):
found.append((r, name))
Expand All @@ -35,7 +35,7 @@ def replace_files(root, renamed):
subs = {r[1]: r[2] for r in renamed}
reg = re.compile('(\\"[a-zA-Z0-9\\.\\/\\?\\:@\\-_=#]+\\.([a-zA-Z]){2,6}' '([a-zA-Z0-9\\.\\&\\/\\?\\:@\\-_=#])*\\")')

for r, dirs, files in os.walk(root):
for r, _, files in os.walk(root):
for name in files:
if os.path.splitext(name)[-1] != ".html":
continue
Expand Down
2 changes: 1 addition & 1 deletion tools/python/example_operator_perf_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def run_test():
# run the model and measure time after 'iters' calls
while total < num_seconds:
start = time.time_ns()
for i in range(iters):
for _ in range(iters):
# ignore the outputs as we're not validating them in a performance test
sess.run(None, inputs)
end = time.time_ns()
Expand Down
2 changes: 1 addition & 1 deletion tools/python/gen_contrib_doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ def main(output_path: str, domain_filter: [str]):
fout.write(s)

for _, namemap in supportmap:
for n, schema, versions in namemap:
for n, schema, _ in namemap:
s = ' * {}<a href="#{}">{}</a>\n'.format(
support_level_str(schema.support_level),
format_name_with_domain(domain, n),
Expand Down
2 changes: 1 addition & 1 deletion tools/python/util/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def run(
def output(is_stream_captured):
return subprocess.PIPE if is_stream_captured else (subprocess.DEVNULL if quiet else None)

completed_process = subprocess.run(
completed_process = subprocess.run( # noqa: DUO116
cmd,
cwd=cwd,
check=check,
Expand Down

0 comments on commit 4b84d82

Please sign in to comment.