Initialisation du repository de Beta
This commit is contained in:
commit
14985f6dbb
9469 changed files with 1903273 additions and 0 deletions
|
|
@ -0,0 +1,12 @@
|
|||
# -------------------------------------------------------------------------
|
||||
# Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
# Licensed under the MIT License.
|
||||
# --------------------------------------------------------------------------
|
||||
import os.path
|
||||
import sys
|
||||
|
||||
sys.path.append(os.path.dirname(__file__))
|
||||
|
||||
transformers_dir = os.path.normpath(os.path.join(os.path.dirname(__file__), "..", ".."))
|
||||
if transformers_dir not in sys.path:
|
||||
sys.path.append(transformers_dir)
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
|
@ -0,0 +1,318 @@
|
|||
# -------------------------------------------------------------------------
|
||||
# Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
# Licensed under the MIT License. See License.txt in the project root for
|
||||
# license information.
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
import argparse
|
||||
import copy
|
||||
import logging
|
||||
import os
|
||||
|
||||
import torch
|
||||
from benchmark_helper import (
|
||||
Precision,
|
||||
create_onnxruntime_session,
|
||||
prepare_environment,
|
||||
setup_logger,
|
||||
)
|
||||
from onnx.shape_inference import infer_shapes_path
|
||||
from t5_helper import PRETRAINED_MT5_MODELS, PRETRAINED_T5_MODELS, T5Helper
|
||||
from transformers import MT5Config, T5Config
|
||||
|
||||
logger = logging.getLogger("")
|
||||
|
||||
|
||||
def parse_arguments():
|
||||
parser = argparse.ArgumentParser()
|
||||
|
||||
pretrained_models = PRETRAINED_T5_MODELS + PRETRAINED_MT5_MODELS
|
||||
parser.add_argument(
|
||||
"-m",
|
||||
"--model_name_or_path",
|
||||
required=False,
|
||||
default=PRETRAINED_T5_MODELS[0],
|
||||
type=str,
|
||||
help="Model path, or pretrained model name in the list: " + ", ".join(pretrained_models),
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
"--model_type",
|
||||
required=False,
|
||||
type=str,
|
||||
default="t5",
|
||||
choices=["t5", "mt5"],
|
||||
help="Model type: either t5 (default) or mt5",
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
"--cache_dir",
|
||||
required=False,
|
||||
type=str,
|
||||
default=os.path.join(".", "cache_models"),
|
||||
help="Directory to cache pre-trained models",
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
"--output",
|
||||
required=False,
|
||||
type=str,
|
||||
default=os.path.join(".", "onnx_models"),
|
||||
help="Output directory",
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
"-o",
|
||||
"--optimize_onnx",
|
||||
required=False,
|
||||
action="store_true",
|
||||
help="Use optimizer.py to optimize onnx model",
|
||||
)
|
||||
parser.set_defaults(optimize_onnx=False)
|
||||
|
||||
parser.add_argument("--use_gpu", required=False, action="store_true", help="use GPU for inference")
|
||||
parser.set_defaults(use_gpu=False)
|
||||
|
||||
parser.add_argument(
|
||||
"-p",
|
||||
"--precision",
|
||||
required=False,
|
||||
type=str,
|
||||
default=Precision.FLOAT32.value,
|
||||
choices=[Precision.FLOAT32.value, Precision.FLOAT16.value],
|
||||
help="Precision of model to run. fp32 for full precision, fp16 for half precision",
|
||||
)
|
||||
|
||||
parser.add_argument("--verbose", required=False, action="store_true")
|
||||
parser.set_defaults(verbose=False)
|
||||
|
||||
parser.add_argument("-e", "--use_external_data_format", required=False, action="store_true")
|
||||
parser.set_defaults(use_external_data_format=False)
|
||||
|
||||
parser.add_argument(
|
||||
"-s",
|
||||
"--use_decoder_start_token",
|
||||
required=False,
|
||||
action="store_true",
|
||||
help="Use config.decoder_start_token_id. Otherwise, add an extra graph input for decoder_input_ids.",
|
||||
)
|
||||
parser.set_defaults(use_decoder_start_token=False)
|
||||
|
||||
parser.add_argument(
|
||||
"-w",
|
||||
"--overwrite",
|
||||
required=False,
|
||||
action="store_true",
|
||||
help="overwrite existing ONNX model",
|
||||
)
|
||||
parser.set_defaults(overwrite=False)
|
||||
|
||||
parser.add_argument(
|
||||
"--disable_auto_mixed_precision",
|
||||
required=False,
|
||||
action="store_true",
|
||||
help="do not use auto mixed precision conversion",
|
||||
)
|
||||
parser.set_defaults(disable_auto_mixed_precision=False)
|
||||
|
||||
parser.add_argument(
|
||||
"--force_fp16_io",
|
||||
required=False,
|
||||
action="store_true",
|
||||
help="Force to convert all float inputs and outputs to fp16 when precision is fp16.",
|
||||
)
|
||||
parser.set_defaults(force_fp16_io=False)
|
||||
|
||||
parser.add_argument(
|
||||
"--use_int64_inputs",
|
||||
required=False,
|
||||
action="store_true",
|
||||
help="Use int64 instead of int32 for input_ids, position_ids and attention_mask.",
|
||||
)
|
||||
parser.set_defaults(use_int64_inputs=False)
|
||||
|
||||
parser.add_argument(
|
||||
"--state_dict_path",
|
||||
type=str,
|
||||
default="",
|
||||
help="filepath to load pre-trained model with custom state dictionary (e.g. pytorch_model.bin)",
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
"--encoder_decoder_init",
|
||||
required=False,
|
||||
action="store_true",
|
||||
help="Combine encoder and decoder kv cache initialization into one model. It is legacy format that will be deprecated.",
|
||||
)
|
||||
parser.set_defaults(encoder_decoder_init=False)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
return args
|
||||
|
||||
|
||||
def export_onnx_models(
|
||||
model_name_or_path: str,
|
||||
cache_dir: str,
|
||||
output_dir: str,
|
||||
use_gpu: bool = False,
|
||||
use_external_data_format: bool = False,
|
||||
optimize_onnx: bool = False,
|
||||
precision: str = Precision.FLOAT32.value,
|
||||
verbose: bool = False,
|
||||
use_decoder_start_token: bool = False,
|
||||
overwrite: bool = False,
|
||||
disable_auto_mixed_precision: bool = False,
|
||||
use_int32_inputs: bool = True,
|
||||
model_type: str = "t5",
|
||||
state_dict_path: str = "",
|
||||
encoder_decoder_init: bool = False,
|
||||
force_fp16_io: bool = False,
|
||||
shape_infer_before_optimization: bool = False,
|
||||
):
|
||||
assert precision in [Precision.FLOAT32.value, Precision.FLOAT16.value], (
|
||||
f"Invalid precision: {precision}. Use 'fp32' or 'fp16'."
|
||||
)
|
||||
device = torch.device("cuda:0" if use_gpu else "cpu")
|
||||
|
||||
models = T5Helper.load_model(
|
||||
model_name_or_path,
|
||||
cache_dir,
|
||||
device,
|
||||
model_type,
|
||||
state_dict_path,
|
||||
encoder_decoder_init=encoder_decoder_init,
|
||||
)
|
||||
config: T5Config | MT5Config = models["decoder"].config
|
||||
|
||||
if (not use_external_data_format) and (config.num_layers > 24):
|
||||
logger.info("Try use_external_data_format when model size > 2GB")
|
||||
|
||||
output_paths = []
|
||||
for name, model in models.items():
|
||||
model.to(device)
|
||||
filename_suffix = "_" + name
|
||||
|
||||
onnx_path = T5Helper.get_onnx_path(
|
||||
output_dir,
|
||||
model_name_or_path,
|
||||
suffix=filename_suffix,
|
||||
new_folder=False,
|
||||
)
|
||||
|
||||
if overwrite or not os.path.exists(onnx_path):
|
||||
logger.info(f"Exporting ONNX model to {onnx_path}")
|
||||
# We have to clone model before exporting onnx, otherwise verify_onnx will report large difference.
|
||||
cloned_model = copy.deepcopy(model).to(device)
|
||||
T5Helper.export_onnx(
|
||||
cloned_model,
|
||||
device,
|
||||
onnx_path,
|
||||
verbose,
|
||||
use_external_data_format,
|
||||
use_decoder_input_ids=not use_decoder_start_token,
|
||||
use_int32_inputs=use_int32_inputs,
|
||||
)
|
||||
else:
|
||||
logger.info(f"Skip exporting: existed ONNX model {onnx_path}")
|
||||
|
||||
# Optimize ONNX graph.
|
||||
# The precision shall be compared with string value. It is because the Precision enum loaded from local file
|
||||
# (like by transformers test in CI pipeline) are not same as Precision enum from package.
|
||||
if optimize_onnx or precision != Precision.FLOAT32.value:
|
||||
onnx_shape_path = None
|
||||
if shape_infer_before_optimization:
|
||||
onnx_shape_path = T5Helper.get_onnx_path(
|
||||
output_dir,
|
||||
model_name_or_path,
|
||||
suffix=filename_suffix + "_shape",
|
||||
new_folder=False,
|
||||
)
|
||||
infer_shapes_path(onnx_path, onnx_shape_path)
|
||||
|
||||
output_path = T5Helper.get_onnx_path(
|
||||
output_dir,
|
||||
model_name_or_path,
|
||||
suffix=filename_suffix + "_" + str(precision),
|
||||
new_folder=False,
|
||||
)
|
||||
|
||||
if overwrite or not os.path.exists(output_path):
|
||||
logger.info(f"Optimizing model to {output_path}")
|
||||
T5Helper.optimize_onnx(
|
||||
onnx_shape_path or onnx_path,
|
||||
output_path,
|
||||
precision == Precision.FLOAT16.value,
|
||||
config.num_heads,
|
||||
config.hidden_size,
|
||||
use_external_data_format,
|
||||
auto_mixed_precision=not disable_auto_mixed_precision,
|
||||
use_gpu=use_gpu,
|
||||
force_fp16_io=force_fp16_io,
|
||||
)
|
||||
else:
|
||||
logger.info(f"Skip optimizing: existed ONNX model {output_path}")
|
||||
else:
|
||||
output_path = onnx_path
|
||||
|
||||
ort_session = create_onnxruntime_session(
|
||||
output_path,
|
||||
use_gpu=use_gpu,
|
||||
verbose=verbose,
|
||||
)
|
||||
if ort_session is None:
|
||||
break
|
||||
|
||||
with torch.no_grad():
|
||||
max_diff = T5Helper.verify_onnx(model, ort_session, device, use_int32_inputs)
|
||||
logger.info(f"PyTorch and OnnxRuntime results max difference = {max_diff}")
|
||||
|
||||
# The threshold cannot apply to fp16 model, which need a larger threshold.
|
||||
if precision == Precision.FLOAT32.value and max_diff > 1e-4:
|
||||
logger.warning("PyTorch and OnnxRuntime results are NOT close")
|
||||
|
||||
output_paths.append(output_path)
|
||||
|
||||
return output_paths
|
||||
|
||||
|
||||
def main():
|
||||
args = parse_arguments()
|
||||
|
||||
setup_logger(args.verbose)
|
||||
|
||||
logger.info(f"Arguments:{args}")
|
||||
|
||||
cache_dir = args.cache_dir
|
||||
output_dir = args.output if not args.output.endswith(".onnx") else os.path.dirname(args.output)
|
||||
prepare_environment(cache_dir, output_dir, args.use_gpu)
|
||||
|
||||
if args.precision != Precision.FLOAT32.value:
|
||||
assert args.optimize_onnx, "fp16/int8 requires --optimize_onnx"
|
||||
|
||||
if args.precision == Precision.FLOAT16.value:
|
||||
assert args.use_gpu, "fp16 requires --use_gpu"
|
||||
|
||||
output_paths = export_onnx_models(
|
||||
args.model_name_or_path,
|
||||
cache_dir,
|
||||
output_dir,
|
||||
args.use_gpu,
|
||||
args.use_external_data_format,
|
||||
args.optimize_onnx,
|
||||
args.precision,
|
||||
args.verbose,
|
||||
args.use_decoder_start_token,
|
||||
args.overwrite,
|
||||
args.disable_auto_mixed_precision,
|
||||
not args.use_int64_inputs,
|
||||
args.model_type,
|
||||
encoder_decoder_init=args.encoder_decoder_init,
|
||||
force_fp16_io=args.force_fp16_io,
|
||||
)
|
||||
|
||||
logger.info(f"Done! Outputs: {output_paths}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
|
@ -0,0 +1,437 @@
|
|||
# -------------------------------------------------------------------------
|
||||
# Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
# Licensed under the MIT License. See License.txt in the project root for
|
||||
# license information.
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
import logging
|
||||
import os
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
import numpy
|
||||
import onnx
|
||||
import torch
|
||||
from io_binding_helper import TypeHelper
|
||||
from onnx_model import OnnxModel
|
||||
from past_helper import PastKeyValuesHelper
|
||||
from t5_encoder import T5EncoderInputs
|
||||
from torch_onnx_export_helper import torch_onnx_export
|
||||
from transformers import MT5Config, T5Config
|
||||
|
||||
from onnxruntime import InferenceSession
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class T5DecoderInit(torch.nn.Module):
|
||||
"""A T5 decoder with LM head to create initial past key values.
|
||||
This model is only called once during starting decoding.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
decoder: torch.nn.Module,
|
||||
lm_head: torch.nn.Module,
|
||||
config: T5Config | MT5Config,
|
||||
decoder_start_token_id: int | None = None,
|
||||
):
|
||||
super().__init__()
|
||||
self.decoder = decoder
|
||||
self.lm_head = lm_head
|
||||
self.config = config
|
||||
self.decoder_start_token_id = (
|
||||
decoder_start_token_id if decoder_start_token_id is not None else self.config.decoder_start_token_id
|
||||
)
|
||||
self.tie_word_embeddings = (
|
||||
self.config.tie_word_embeddings if hasattr(self.config, "tie_word_embeddings") else True
|
||||
)
|
||||
|
||||
def forward(
|
||||
self,
|
||||
decoder_input_ids: torch.Tensor,
|
||||
encoder_attention_mask: torch.Tensor,
|
||||
encoder_hidden_states: torch.FloatTensor,
|
||||
):
|
||||
if decoder_input_ids is None:
|
||||
batch_size = encoder_attention_mask.shape[0]
|
||||
decoder_input_ids = (
|
||||
torch.ones(
|
||||
(batch_size, 1),
|
||||
dtype=torch.long,
|
||||
device=encoder_attention_mask.device,
|
||||
)
|
||||
* self.decoder_start_token_id
|
||||
)
|
||||
|
||||
decoder_outputs = self.decoder(
|
||||
input_ids=decoder_input_ids,
|
||||
encoder_hidden_states=encoder_hidden_states,
|
||||
encoder_attention_mask=encoder_attention_mask,
|
||||
use_cache=True,
|
||||
return_dict=True,
|
||||
)
|
||||
|
||||
sequence_output = decoder_outputs.last_hidden_state
|
||||
present_key_values = decoder_outputs.past_key_values
|
||||
|
||||
if self.tie_word_embeddings:
|
||||
sequence_output = sequence_output * (self.config.d_model**-0.5)
|
||||
|
||||
lm_logits = self.lm_head(sequence_output)
|
||||
past_self, past_cross = PastKeyValuesHelper.group_by_self_or_cross(present_key_values)
|
||||
return lm_logits, past_self, past_cross
|
||||
|
||||
|
||||
class T5Decoder(torch.nn.Module):
|
||||
"""A T5 decoder with LM head and past key values"""
|
||||
|
||||
def __init__(self, decoder, lm_head, config):
|
||||
super().__init__()
|
||||
self.decoder = decoder
|
||||
self.lm_head = lm_head
|
||||
self.config = config
|
||||
self.tie_word_embeddings = (
|
||||
self.config.tie_word_embeddings if hasattr(self.config, "tie_word_embeddings") else True
|
||||
)
|
||||
|
||||
def forward(self, decoder_input_ids, encoder_attention_mask, *past):
|
||||
num_decoder_layers = self.config.num_decoder_layers
|
||||
past_key_values = PastKeyValuesHelper.group_by_layer(past, num_decoder_layers)
|
||||
|
||||
# This is a hack since only the third dimension of encoder_hidden_states is used here
|
||||
dummy_encoder_hidden_states = encoder_attention_mask.unsqueeze(2)
|
||||
decoder_outputs = self.decoder(
|
||||
input_ids=decoder_input_ids,
|
||||
past_key_values=past_key_values,
|
||||
encoder_hidden_states=dummy_encoder_hidden_states,
|
||||
encoder_attention_mask=encoder_attention_mask,
|
||||
use_cache=True,
|
||||
return_dict=True,
|
||||
)
|
||||
|
||||
sequence_output = decoder_outputs.last_hidden_state
|
||||
present_key_values = decoder_outputs.past_key_values
|
||||
|
||||
if self.tie_word_embeddings:
|
||||
sequence_output = sequence_output * (self.config.d_model**-0.5)
|
||||
|
||||
lm_logits = self.lm_head(sequence_output)
|
||||
present_self, _ = PastKeyValuesHelper.group_by_self_or_cross(present_key_values)
|
||||
|
||||
# Do not return present_cross since they are identical to corresponding past_cross input
|
||||
return lm_logits, present_self
|
||||
|
||||
|
||||
class T5DecoderInputs:
|
||||
def __init__(
|
||||
self,
|
||||
decoder_input_ids,
|
||||
encoder_attention_mask,
|
||||
past_key_values=None,
|
||||
):
|
||||
self.decoder_input_ids: torch.LongTensor = decoder_input_ids
|
||||
self.encoder_attention_mask: torch.LongTensor = encoder_attention_mask
|
||||
self.past_key_values: list[torch.FloatTensor] | list[torch.HalfTensor] | None = past_key_values
|
||||
|
||||
@staticmethod
|
||||
def create_dummy(
|
||||
config: T5Config | MT5Config,
|
||||
batch_size: int,
|
||||
encode_sequence_length: int,
|
||||
past_decode_sequence_length: int,
|
||||
device: torch.device,
|
||||
float16: bool = False,
|
||||
use_int32_inputs: bool = False,
|
||||
): # -> T5DecoderInputs:
|
||||
"""Create dummy inputs for T5Decoder.
|
||||
|
||||
Args:
|
||||
decoder: decoder
|
||||
batch_size (int): batch size
|
||||
encode_sequence_length (int): sequence length of input_ids for encoder
|
||||
past_decode_sequence_length (int): past sequence length of input_ids for decoder
|
||||
device (torch.device): device of output tensors
|
||||
float16 (bool): whether the model uses float32 or float16 in input
|
||||
use_int32_inputs(bool): whether use int32 instead of int64 for some inputs
|
||||
|
||||
Returns:
|
||||
T5DecoderInputs: dummy inputs for decoder
|
||||
"""
|
||||
num_attention_heads: int = config.num_heads
|
||||
num_layers: int = config.num_decoder_layers
|
||||
vocab_size: int = config.vocab_size
|
||||
|
||||
# Do not use head_size = hidden_size / num_attention_heads here.
|
||||
# For example, mt5-small, d_model=512 and num_heads=6
|
||||
head_size: int = config.d_kv
|
||||
|
||||
sequence_length: int = 1 # fixed for decoding
|
||||
decoder_input_ids = torch.randint(
|
||||
low=0,
|
||||
high=vocab_size - 1,
|
||||
size=(batch_size, sequence_length),
|
||||
dtype=(torch.int32 if use_int32_inputs else torch.int64),
|
||||
device=device,
|
||||
)
|
||||
|
||||
encoder_inputs = T5EncoderInputs.create_dummy(
|
||||
batch_size,
|
||||
encode_sequence_length,
|
||||
vocab_size,
|
||||
device,
|
||||
use_int32_inputs=use_int32_inputs,
|
||||
)
|
||||
|
||||
float_type = torch.float16 if float16 else torch.float32
|
||||
|
||||
if past_decode_sequence_length > 0:
|
||||
self_attention_past_shape = [
|
||||
batch_size,
|
||||
num_attention_heads,
|
||||
past_decode_sequence_length,
|
||||
head_size,
|
||||
]
|
||||
cross_attention_past_shape = [
|
||||
batch_size,
|
||||
num_attention_heads,
|
||||
encode_sequence_length,
|
||||
head_size,
|
||||
]
|
||||
|
||||
past = []
|
||||
for _ in range(2 * num_layers):
|
||||
past.append(torch.rand(self_attention_past_shape, dtype=float_type, device=device))
|
||||
|
||||
for _ in range(2 * num_layers):
|
||||
past.append(torch.rand(cross_attention_past_shape, dtype=float_type, device=device))
|
||||
else:
|
||||
past = None
|
||||
|
||||
return T5DecoderInputs(decoder_input_ids, encoder_inputs.attention_mask, past)
|
||||
|
||||
def to_list(self) -> list:
|
||||
input_list = [
|
||||
self.decoder_input_ids,
|
||||
self.encoder_attention_mask,
|
||||
]
|
||||
if self.past_key_values:
|
||||
input_list.extend(self.past_key_values)
|
||||
return input_list
|
||||
|
||||
def to_fp32(self):
|
||||
past = [p.to(dtype=torch.float32) for p in self.past_key_values] if self.past_key_values else None
|
||||
return T5DecoderInputs(
|
||||
self.decoder_input_ids.clone(),
|
||||
self.encoder_attention_mask.clone(),
|
||||
past,
|
||||
)
|
||||
|
||||
|
||||
class T5DecoderHelper:
|
||||
@staticmethod
|
||||
def export_onnx(
|
||||
decoder: T5Decoder | T5DecoderInit,
|
||||
device: torch.device,
|
||||
onnx_model_path: str,
|
||||
verbose: bool = True,
|
||||
use_external_data_format: bool = False,
|
||||
use_int32_inputs: bool = False,
|
||||
):
|
||||
"""Export decoder to ONNX
|
||||
|
||||
Args:
|
||||
decoder (Union[T5Decoder, T5DecoderNoPastState]): decoder object
|
||||
device (torch.device): device of decoder object
|
||||
onnx_model_path (str): onnx path
|
||||
verbose (bool, optional): print verbose information. Defaults to True.
|
||||
use_external_data_format (bool, optional): use external data format or not. Defaults to False.
|
||||
use_int32_inputs (bool, optional): use int32 inputs
|
||||
"""
|
||||
assert isinstance(decoder, (T5Decoder, T5DecoderInit))
|
||||
|
||||
inputs = T5DecoderInputs.create_dummy(
|
||||
decoder.config,
|
||||
batch_size=2,
|
||||
encode_sequence_length=3,
|
||||
past_decode_sequence_length=5 if isinstance(decoder, T5Decoder) else 0,
|
||||
device=device,
|
||||
use_int32_inputs=use_int32_inputs,
|
||||
)
|
||||
input_list = inputs.to_list()
|
||||
|
||||
num_decoder_layers = decoder.config.num_decoder_layers
|
||||
|
||||
past_names = PastKeyValuesHelper.get_past_names(num_decoder_layers, present=False)
|
||||
present_names = PastKeyValuesHelper.get_past_names(num_decoder_layers, present=True)
|
||||
present_self_names = present_names[: 2 * num_decoder_layers]
|
||||
|
||||
input_past_names = past_names if isinstance(decoder, T5Decoder) else []
|
||||
output_present_names = present_self_names if isinstance(decoder, T5Decoder) else present_names
|
||||
output_names = ["logits", *output_present_names]
|
||||
|
||||
# Shape of input tensors (sequence_length==1):
|
||||
# input_ids: (batch_size, sequence_length)
|
||||
# encoder_attention_mask: (batch_size, encode_sequence_length)
|
||||
# past_self_*: (batch_size, num_heads, past_decode_sequence_length, head_size)
|
||||
# past_cross_*: (batch_size, num_heads, encode_sequence_length, head_size)
|
||||
|
||||
# Shape of output tensors:
|
||||
# logits: (batch_size, sequence_length, vocab_size)
|
||||
# past_self_*: (batch_size, num_heads, past_decode_sequence_length + sequence_length, head_size)
|
||||
# past_cross_*: (batch_size, num_heads, encode_sequence_length, head_size)
|
||||
|
||||
input_names = ["input_ids"]
|
||||
input_names.append("encoder_attention_mask")
|
||||
input_names.extend(input_past_names)
|
||||
|
||||
dynamic_axes = {
|
||||
"input_ids": {
|
||||
0: "batch_size",
|
||||
# 1: 'sequence_length'
|
||||
},
|
||||
"encoder_attention_mask": {0: "batch_size", 1: "encode_sequence_length"},
|
||||
"encoder_hidden_states": {0: "batch_size", 1: "encode_sequence_length"},
|
||||
"logits": {
|
||||
0: "batch_size",
|
||||
# 1: 'sequence_length'
|
||||
},
|
||||
}
|
||||
|
||||
for name in input_past_names:
|
||||
dynamic_axes[name] = {
|
||||
0: "batch_size",
|
||||
2: "past_decode_sequence_length" if "self" in name else "encode_sequence_length",
|
||||
}
|
||||
|
||||
for name in output_present_names:
|
||||
if "cross" in name:
|
||||
dynamic_axes[name] = {0: "batch_size", 2: "encode_sequence_length"}
|
||||
else: # self attention past state
|
||||
if isinstance(decoder, T5Decoder):
|
||||
dynamic_axes[name] = {
|
||||
0: "batch_size",
|
||||
2: "past_decode_sequence_length + 1",
|
||||
}
|
||||
else:
|
||||
dynamic_axes[name] = {
|
||||
0: "batch_size",
|
||||
# 2: 'sequence_length'
|
||||
}
|
||||
|
||||
Path(onnx_model_path).parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmp_dir_name:
|
||||
temp_onnx_model_path = os.path.join(tmp_dir_name, "decoder.onnx")
|
||||
Path(temp_onnx_model_path).parent.mkdir(parents=True, exist_ok=True)
|
||||
torch_onnx_export(
|
||||
decoder,
|
||||
args=tuple(input_list),
|
||||
f=temp_onnx_model_path if use_external_data_format else onnx_model_path,
|
||||
export_params=True,
|
||||
input_names=input_names,
|
||||
output_names=output_names,
|
||||
dynamic_axes=dynamic_axes,
|
||||
opset_version=12,
|
||||
do_constant_folding=True,
|
||||
use_external_data_format=use_external_data_format,
|
||||
verbose=verbose,
|
||||
)
|
||||
|
||||
if use_external_data_format:
|
||||
model = onnx.load_model(temp_onnx_model_path, load_external_data=True)
|
||||
OnnxModel.save(
|
||||
model,
|
||||
onnx_model_path,
|
||||
save_as_external_data=True,
|
||||
all_tensors_to_one_file=True,
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def onnxruntime_inference(ort_session, inputs: T5DecoderInputs):
|
||||
"""Run inference of ONNX model."""
|
||||
logger.debug("start onnxruntime_inference")
|
||||
|
||||
ort_inputs = {
|
||||
"input_ids": numpy.ascontiguousarray(inputs.decoder_input_ids.cpu().numpy()),
|
||||
"encoder_attention_mask": numpy.ascontiguousarray(inputs.encoder_attention_mask.cpu().numpy()),
|
||||
}
|
||||
|
||||
if inputs.past_key_values:
|
||||
assert len(inputs.past_key_values) % 4 == 0
|
||||
num_layers = int(len(inputs.past_key_values) / 4)
|
||||
past_names = PastKeyValuesHelper.get_past_names(num_layers)
|
||||
for i, past_tensor in enumerate(inputs.past_key_values):
|
||||
ort_inputs[past_names[i]] = numpy.ascontiguousarray(past_tensor.cpu().numpy())
|
||||
|
||||
ort_outputs = ort_session.run(None, ort_inputs)
|
||||
return ort_outputs
|
||||
|
||||
@staticmethod
|
||||
def verify_onnx(
|
||||
model: T5Decoder | T5DecoderInit,
|
||||
ort_session: InferenceSession,
|
||||
device: torch.device,
|
||||
use_int32_inputs: bool,
|
||||
max_cases: int = 4,
|
||||
):
|
||||
"""Compare the result from PyTorch and OnnxRuntime to verify the ONNX model is good."""
|
||||
float16: bool = TypeHelper.get_input_type(ort_session, "past_key_self_0") == "tensor(float16)"
|
||||
|
||||
test_cases = [(4, 11, 3), (1, 2, 5), (3, 1, 1), (8, 5, 2)]
|
||||
test_cases_max_diff = []
|
||||
for (
|
||||
batch_size,
|
||||
encode_sequence_length,
|
||||
past_decode_sequence_length,
|
||||
) in test_cases[:max_cases]:
|
||||
if isinstance(model, T5DecoderInit):
|
||||
past_decode_sequence_length = 0 # noqa: PLW2901
|
||||
|
||||
inputs = T5DecoderInputs.create_dummy(
|
||||
model.config,
|
||||
batch_size,
|
||||
encode_sequence_length,
|
||||
past_decode_sequence_length,
|
||||
device=device,
|
||||
float16=float16,
|
||||
use_int32_inputs=use_int32_inputs,
|
||||
)
|
||||
|
||||
# We use fp32 PyTroch model as baseline even when ONNX model is fp16
|
||||
input_list = inputs.to_fp32().to_list()
|
||||
|
||||
# Run inference of PyTorch model
|
||||
with torch.no_grad():
|
||||
torch_outputs = model(*input_list)
|
||||
|
||||
ort_outputs = T5DecoderHelper.onnxruntime_inference(ort_session, inputs)
|
||||
num_decoder_layers = model.config.num_decoder_layers
|
||||
|
||||
max_diff = numpy.amax(numpy.abs(torch_outputs[0].cpu().numpy() - ort_outputs[0]))
|
||||
max_diff_all = max_diff
|
||||
logger.debug(f"logits max_diff={max_diff}")
|
||||
|
||||
for i in range(2 * num_decoder_layers):
|
||||
max_diff = numpy.amax(numpy.abs(torch_outputs[1][i].cpu().numpy() - ort_outputs[1 + i]))
|
||||
logger.debug(f"self attention past state {i} max_diff={max_diff}")
|
||||
max_diff_all = max(max_diff_all, max_diff)
|
||||
|
||||
if isinstance(model, T5DecoderInit):
|
||||
for i in range(2 * num_decoder_layers):
|
||||
max_diff = numpy.amax(
|
||||
numpy.abs(torch_outputs[2][i].cpu().numpy() - ort_outputs[1 + 2 * num_decoder_layers + i])
|
||||
)
|
||||
logger.debug(f"cross attention past state {i} max_diff={max_diff}")
|
||||
max_diff_all = max(max_diff_all, max_diff)
|
||||
|
||||
test_cases_max_diff.append(max_diff_all)
|
||||
logger.info(
|
||||
"batch_size=%s, encode_sequence_length=%s, past_decode_sequence_length=%s, max_diff=%s",
|
||||
batch_size,
|
||||
encode_sequence_length,
|
||||
past_decode_sequence_length,
|
||||
max_diff_all,
|
||||
)
|
||||
|
||||
return max_diff_all
|
||||
|
|
@ -0,0 +1,70 @@
|
|||
# -------------------------------------------------------------------------
|
||||
# Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
# Licensed under the MIT License.
|
||||
# -------------------------------------------------------------------------
|
||||
|
||||
import logging
|
||||
import random
|
||||
|
||||
import torch
|
||||
from transformers import MT5Config, T5Config
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class T5Encoder(torch.nn.Module):
|
||||
"""T5 encoder outputs only the last hidden state"""
|
||||
|
||||
def __init__(self, encoder, config: T5Config | MT5Config):
|
||||
super().__init__()
|
||||
self.encoder = encoder
|
||||
self.config = config
|
||||
|
||||
def forward(self, input_ids, attention_mask):
|
||||
return self.encoder(input_ids, attention_mask)[0]
|
||||
|
||||
|
||||
class T5EncoderInputs:
|
||||
def __init__(self, input_ids, attention_mask):
|
||||
self.input_ids: torch.LongTensor = input_ids
|
||||
self.attention_mask: torch.LongTensor = attention_mask
|
||||
|
||||
@staticmethod
|
||||
def create_dummy(
|
||||
batch_size: int,
|
||||
sequence_length: int,
|
||||
vocab_size: int,
|
||||
device: torch.device,
|
||||
use_int32_inputs: bool = False,
|
||||
): # -> T5EncoderInputs
|
||||
"""Create dummy inputs for T5 encoder.
|
||||
|
||||
Args:
|
||||
batch_size (int): batch size
|
||||
sequence_length (int): sequence length
|
||||
vocab_size (int): vocabulary size
|
||||
device (torch.device): device of output tensors
|
||||
|
||||
Returns:
|
||||
T5EncoderInputs: dummy inputs for encoder
|
||||
"""
|
||||
dtype = torch.int32 if use_int32_inputs else torch.int64
|
||||
|
||||
input_ids = torch.randint(
|
||||
low=0,
|
||||
high=vocab_size - 1,
|
||||
size=(batch_size, sequence_length),
|
||||
dtype=dtype,
|
||||
device=device,
|
||||
)
|
||||
|
||||
attention_mask = torch.ones([batch_size, sequence_length], dtype=dtype, device=device)
|
||||
if sequence_length >= 2:
|
||||
for i in range(batch_size):
|
||||
padding_position = random.randint(0, sequence_length - 1)
|
||||
attention_mask[i, :padding_position] = 0
|
||||
return T5EncoderInputs(input_ids, attention_mask)
|
||||
|
||||
def to_list(self) -> list:
|
||||
input_list = [v for v in [self.input_ids, self.attention_mask] if v is not None]
|
||||
return input_list
|
||||
|
|
@ -0,0 +1,361 @@
|
|||
# -------------------------------------------------------------------------
|
||||
# Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
# Licensed under the MIT License.
|
||||
# -------------------------------------------------------------------------
|
||||
|
||||
import logging
|
||||
import os
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
import numpy
|
||||
import onnx
|
||||
import torch
|
||||
from onnx_model import OnnxModel
|
||||
from past_helper import PastKeyValuesHelper
|
||||
from t5_decoder import T5DecoderInit
|
||||
from t5_encoder import T5Encoder, T5EncoderInputs
|
||||
from torch_onnx_export_helper import torch_onnx_export
|
||||
from transformers import MT5Config, T5Config
|
||||
|
||||
from onnxruntime import InferenceSession
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class T5EncoderDecoderInit(torch.nn.Module):
|
||||
"""A combination of T5Encoder and T5DecoderInit."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
encoder: torch.nn.Module,
|
||||
decoder: torch.nn.Module,
|
||||
lm_head: torch.nn.Linear,
|
||||
config: T5Config | MT5Config,
|
||||
decoder_start_token_id: int | None = None,
|
||||
output_cross_only: bool = False,
|
||||
):
|
||||
super().__init__()
|
||||
self.config: T5Config | MT5Config = config
|
||||
self.t5_encoder = T5Encoder(encoder, config)
|
||||
self.t5_decoder_init = T5DecoderInit(decoder, lm_head, config, decoder_start_token_id)
|
||||
self.output_cross_only = output_cross_only
|
||||
|
||||
def forward(
|
||||
self,
|
||||
encoder_input_ids: torch.Tensor,
|
||||
encoder_attention_mask: torch.Tensor,
|
||||
decoder_input_ids: torch.Tensor | None = None,
|
||||
):
|
||||
encoder_hidden_states: torch.FloatTensor = self.t5_encoder(encoder_input_ids, encoder_attention_mask)
|
||||
|
||||
lm_logits, past_self, past_cross = self.t5_decoder_init(
|
||||
decoder_input_ids, encoder_attention_mask, encoder_hidden_states
|
||||
)
|
||||
|
||||
if self.output_cross_only:
|
||||
return past_cross
|
||||
else:
|
||||
return lm_logits, encoder_hidden_states, past_self, past_cross
|
||||
|
||||
|
||||
class T5EncoderDecoderInitInputs:
|
||||
def __init__(self, encoder_input_ids, encoder_attention_mask, decoder_input_ids=None):
|
||||
self.encoder_input_ids: torch.LongTensor = encoder_input_ids
|
||||
self.encoder_attention_mask: torch.LongTensor = encoder_attention_mask
|
||||
self.decoder_input_ids: torch.LongTensor | None = decoder_input_ids
|
||||
|
||||
@staticmethod
|
||||
def create_dummy(
|
||||
config: T5Config | MT5Config,
|
||||
batch_size: int,
|
||||
encode_sequence_length: int,
|
||||
use_decoder_input_ids: int,
|
||||
device: torch.device,
|
||||
use_int32_inputs: bool = False,
|
||||
): # -> T5EncoderDecoderInitInputs:
|
||||
encoder_inputs: T5EncoderInputs = T5EncoderInputs.create_dummy(
|
||||
batch_size,
|
||||
encode_sequence_length,
|
||||
config.vocab_size,
|
||||
device,
|
||||
use_int32_inputs=use_int32_inputs,
|
||||
)
|
||||
decoder_input_ids = None
|
||||
if use_decoder_input_ids:
|
||||
dtype = torch.int32 if use_int32_inputs else torch.int64
|
||||
decoder_input_ids = torch.ones((batch_size, 1), dtype=dtype, device=device) * config.decoder_start_token_id
|
||||
|
||||
return T5EncoderDecoderInitInputs(encoder_inputs.input_ids, encoder_inputs.attention_mask, decoder_input_ids)
|
||||
|
||||
def to_list(self) -> list:
|
||||
input_list = [self.encoder_input_ids, self.encoder_attention_mask]
|
||||
if self.decoder_input_ids is not None:
|
||||
input_list.append(self.decoder_input_ids)
|
||||
return input_list
|
||||
|
||||
|
||||
class T5EncoderDecoderInitHelper:
|
||||
@staticmethod
|
||||
def export_onnx(
|
||||
model: T5EncoderDecoderInit,
|
||||
device: torch.device,
|
||||
onnx_model_path: str,
|
||||
use_decoder_input_ids: bool = True,
|
||||
verbose: bool = True,
|
||||
use_external_data_format: bool = False,
|
||||
use_int32_inputs: bool = False,
|
||||
):
|
||||
"""Export decoder to ONNX
|
||||
|
||||
Args:
|
||||
model (T5EncoderDecoderInit): the model to export
|
||||
device (torch.device): device of decoder object
|
||||
onnx_model_path (str): onnx path
|
||||
verbose (bool, optional): print verbose information. Defaults to True.
|
||||
use_external_data_format (bool, optional): use external data format or not. Defaults to False.
|
||||
use_int32_inputs (bool, optional): use int32 instead of int64 for integer inputs. Defaults to False.
|
||||
"""
|
||||
assert isinstance(model, T5EncoderDecoderInit)
|
||||
|
||||
# Do not exclude decoder in torch onnx export so that cross can show up.
|
||||
output_cross_only = model.output_cross_only
|
||||
model.output_cross_only = False
|
||||
|
||||
inputs = T5EncoderDecoderInitInputs.create_dummy(
|
||||
model.config,
|
||||
batch_size=2,
|
||||
encode_sequence_length=3,
|
||||
use_decoder_input_ids=use_decoder_input_ids,
|
||||
device=device,
|
||||
use_int32_inputs=use_int32_inputs,
|
||||
)
|
||||
input_list = inputs.to_list()
|
||||
|
||||
present_names = PastKeyValuesHelper.get_past_names(model.config.num_decoder_layers, present=True)
|
||||
|
||||
output_names = ["logits", "encoder_hidden_states", *present_names]
|
||||
|
||||
# Shape of input tensors (sequence_length==1):
|
||||
# input_ids: (batch_size, sequence_length)
|
||||
# encoder_attention_mask: (batch_size, encode_sequence_length)
|
||||
# encoder_hidden_states: (batch_size, encode_sequence_length, hidden_size)
|
||||
# past_self_*: (batch_size, num_heads, past_decode_sequence_length, head_size)
|
||||
# past_cross_*: (batch_size, num_heads, encode_sequence_length, head_size)
|
||||
|
||||
# Shape of output tensors:
|
||||
# logits: (batch_size, sequence_length, vocab_size)
|
||||
# past_self_*: (batch_size, num_heads, past_decode_sequence_length + sequence_length, head_size)
|
||||
# past_cross_*: (batch_size, num_heads, encode_sequence_length, head_size)
|
||||
|
||||
input_names = ["encoder_input_ids", "encoder_attention_mask"]
|
||||
|
||||
# ONNX exporter might mark dimension like 'present_value_self_1_dim_2' in shape inference.
|
||||
# We use a workaround here: first use dim_param "1" for sequence_length, and later change to dim_value.
|
||||
sequence_length = "1"
|
||||
num_heads = str(model.config.num_heads)
|
||||
hidden_size = str(model.config.d_model)
|
||||
head_size = str(model.config.d_kv)
|
||||
|
||||
dynamic_axes = {
|
||||
"encoder_input_ids": {0: "batch_size", 1: "encode_sequence_length"},
|
||||
"encoder_attention_mask": {0: "batch_size", 1: "encode_sequence_length"},
|
||||
"encoder_hidden_states": {
|
||||
0: "batch_size",
|
||||
1: "encode_sequence_length",
|
||||
2: hidden_size,
|
||||
},
|
||||
"logits": {
|
||||
0: "batch_size",
|
||||
1: sequence_length,
|
||||
},
|
||||
}
|
||||
|
||||
if use_decoder_input_ids:
|
||||
input_names.append("decoder_input_ids")
|
||||
dynamic_axes["decoder_input_ids"] = {
|
||||
0: "batch_size",
|
||||
1: sequence_length,
|
||||
}
|
||||
|
||||
for name in present_names:
|
||||
if "cross" in name:
|
||||
dynamic_axes[name] = {
|
||||
0: "batch_size",
|
||||
1: num_heads,
|
||||
2: "encode_sequence_length",
|
||||
3: head_size,
|
||||
}
|
||||
|
||||
else: # self attention past state
|
||||
dynamic_axes[name] = {
|
||||
0: "batch_size",
|
||||
1: num_heads,
|
||||
2: sequence_length,
|
||||
3: head_size,
|
||||
}
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmp_dir_name:
|
||||
temp_onnx_model_path = os.path.join(tmp_dir_name, "encoder_decoder_init.onnx")
|
||||
Path(temp_onnx_model_path).parent.mkdir(parents=True, exist_ok=True)
|
||||
torch_onnx_export(
|
||||
model,
|
||||
args=tuple(input_list),
|
||||
f=temp_onnx_model_path,
|
||||
export_params=True,
|
||||
input_names=input_names,
|
||||
output_names=output_names,
|
||||
dynamic_axes=dynamic_axes,
|
||||
opset_version=12,
|
||||
do_constant_folding=True,
|
||||
use_external_data_format=use_external_data_format,
|
||||
verbose=verbose,
|
||||
)
|
||||
|
||||
# Restore output_cross_only setting.
|
||||
model.output_cross_only = output_cross_only
|
||||
|
||||
# Workaround as mentioned earlier: change numeric dim_param to dim_value
|
||||
exported_model: onnx.ModelProto = onnx.load(temp_onnx_model_path)
|
||||
for tensor in exported_model.graph.output:
|
||||
for dim_proto in tensor.type.tensor_type.shape.dim:
|
||||
if dim_proto.HasField("dim_param") and dim_proto.dim_param in [
|
||||
sequence_length,
|
||||
num_heads,
|
||||
hidden_size,
|
||||
head_size,
|
||||
]:
|
||||
dim_value = int(dim_proto.dim_param)
|
||||
dim_proto.Clear()
|
||||
dim_proto.dim_value = dim_value
|
||||
|
||||
if output_cross_only:
|
||||
# Rewrite onnx graph to only keep present_[key|value]_cross_* outputs.
|
||||
onnx_model = OnnxModel(exported_model)
|
||||
output_name_to_node = onnx_model.output_name_to_node()
|
||||
|
||||
for output in exported_model.graph.output:
|
||||
if "cross" in output.name:
|
||||
assert output.name in output_name_to_node
|
||||
|
||||
transpose_node = output_name_to_node[output.name]
|
||||
assert transpose_node and transpose_node.op_type == "Transpose"
|
||||
|
||||
permutation = OnnxModel.get_node_attribute(transpose_node, "perm")
|
||||
assert isinstance(permutation, list)
|
||||
assert permutation == [0, 2, 1, 3]
|
||||
|
||||
matched_nodes = onnx_model.match_parent_path(
|
||||
transpose_node,
|
||||
["Reshape", "MatMul"],
|
||||
[0, 0],
|
||||
output_name_to_node,
|
||||
)
|
||||
assert matched_nodes is not None
|
||||
|
||||
reshape_node, matmul_node = matched_nodes
|
||||
assert "encoder_hidden_states" in matmul_node.input
|
||||
|
||||
if not onnx_model.get_initializer("cross_reshape_shape"):
|
||||
shape_tensor = onnx.helper.make_tensor(
|
||||
name="cross_reshape_shape",
|
||||
data_type=onnx.TensorProto.INT64,
|
||||
dims=[4],
|
||||
vals=[0, 0, int(num_heads), int(head_size)],
|
||||
raw=False,
|
||||
)
|
||||
onnx_model.add_initializer(shape_tensor)
|
||||
|
||||
reshape_node.input[1] = "cross_reshape_shape"
|
||||
|
||||
cross_outputs = [output.name for output in exported_model.graph.output if "cross" in output.name]
|
||||
onnx_model.prune_graph(cross_outputs, allow_remove_graph_inputs=True)
|
||||
|
||||
OnnxModel.save(
|
||||
exported_model,
|
||||
onnx_model_path,
|
||||
save_as_external_data=use_external_data_format,
|
||||
all_tensors_to_one_file=True,
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def onnxruntime_inference(ort_session, inputs: T5EncoderDecoderInitInputs):
|
||||
"""Run inference of ONNX model."""
|
||||
logger.debug("start onnxruntime_inference")
|
||||
|
||||
ort_inputs = {
|
||||
"encoder_input_ids": numpy.ascontiguousarray(inputs.encoder_input_ids.cpu().numpy()),
|
||||
"encoder_attention_mask": numpy.ascontiguousarray(inputs.encoder_attention_mask.cpu().numpy()),
|
||||
}
|
||||
if inputs.decoder_input_ids is not None:
|
||||
ort_inputs["decoder_input_ids"] = numpy.ascontiguousarray(inputs.decoder_input_ids.cpu().numpy())
|
||||
|
||||
ort_outputs = ort_session.run(None, ort_inputs)
|
||||
return ort_outputs
|
||||
|
||||
@staticmethod
|
||||
def verify_onnx(
|
||||
model: T5EncoderDecoderInit,
|
||||
ort_session: InferenceSession,
|
||||
device: torch.device,
|
||||
use_int32_inputs: bool,
|
||||
max_cases: int = 4,
|
||||
):
|
||||
"""Compare the result from PyTorch and OnnxRuntime to verify the ONNX model is good."""
|
||||
ort_inputs = ort_session.get_inputs()
|
||||
use_decoder_input_ids = len(ort_inputs) == 3
|
||||
|
||||
test_cases = [(4, 11), (1, 2), (3, 1), (8, 5)]
|
||||
test_cases_max_diff = []
|
||||
for batch_size, encode_sequence_length in test_cases[:max_cases]:
|
||||
inputs = T5EncoderDecoderInitInputs.create_dummy(
|
||||
model.config,
|
||||
batch_size,
|
||||
encode_sequence_length,
|
||||
use_decoder_input_ids=use_decoder_input_ids,
|
||||
device=device,
|
||||
use_int32_inputs=use_int32_inputs,
|
||||
)
|
||||
|
||||
ort_outputs = T5EncoderDecoderInitHelper.onnxruntime_inference(ort_session, inputs)
|
||||
|
||||
# Run inference of PyTorch model
|
||||
input_list = inputs.to_list()
|
||||
torch_outputs = model(*input_list)
|
||||
|
||||
num_decoder_layers = model.config.num_decoder_layers
|
||||
|
||||
if not model.output_cross_only:
|
||||
assert torch_outputs[0].cpu().numpy().shape == ort_outputs[0].shape
|
||||
max_diff = numpy.amax(numpy.abs(torch_outputs[0].cpu().numpy() - ort_outputs[0]))
|
||||
logger.debug(f"logits max_diff={max_diff}")
|
||||
max_diff_all = max_diff
|
||||
|
||||
assert torch_outputs[1].cpu().numpy().shape == ort_outputs[1].shape
|
||||
max_diff = numpy.amax(numpy.abs(torch_outputs[1].cpu().numpy() - ort_outputs[1]))
|
||||
logger.debug(f"encoder_hidden_states max_diff={max_diff}")
|
||||
max_diff_all = max(max_diff_all, max_diff)
|
||||
|
||||
for i in range(2 * num_decoder_layers):
|
||||
max_diff = numpy.amax(numpy.abs(torch_outputs[2][i].cpu().numpy() - ort_outputs[2 + i]))
|
||||
logger.debug(f"self attention past state {i} max_diff={max_diff}")
|
||||
|
||||
for i in range(2 * num_decoder_layers):
|
||||
max_diff = numpy.amax(
|
||||
numpy.abs(torch_outputs[3][i].cpu().numpy() - ort_outputs[2 + 2 * num_decoder_layers + i])
|
||||
)
|
||||
logger.debug(f"cross attention past state {i} max_diff={max_diff}")
|
||||
max_diff_all = max(max_diff_all, max_diff)
|
||||
else:
|
||||
max_diff_all = -float("inf")
|
||||
for i in range(2 * num_decoder_layers):
|
||||
max_diff = numpy.amax(numpy.abs(torch_outputs[i].cpu().numpy() - ort_outputs[i]))
|
||||
logger.debug(f"cross attention past state {i} max_diff={max_diff}")
|
||||
max_diff_all = max(max_diff_all, max_diff)
|
||||
|
||||
test_cases_max_diff.append(max_diff_all)
|
||||
logger.info(
|
||||
f"batch_size={batch_size} encode_sequence_length={encode_sequence_length}, max_diff={max_diff_all}"
|
||||
)
|
||||
|
||||
return max(test_cases_max_diff)
|
||||
|
|
@ -0,0 +1,302 @@
|
|||
# -------------------------------------------------------------------------
|
||||
# Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
# Licensed under the MIT License.
|
||||
# -------------------------------------------------------------------------
|
||||
|
||||
import logging
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
import torch
|
||||
from float16 import float_to_float16_max_diff
|
||||
from onnx_model import OnnxModel
|
||||
from optimizer import optimize_model
|
||||
from t5_decoder import T5Decoder, T5DecoderHelper
|
||||
from t5_encoder_decoder_init import T5EncoderDecoderInit, T5EncoderDecoderInitHelper
|
||||
from transformers import MT5ForConditionalGeneration, T5ForConditionalGeneration
|
||||
|
||||
from onnxruntime import InferenceSession
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
PRETRAINED_T5_MODELS = ["t5-small", "t5-base", "t5-large", "t5-3b", "t5-11b"]
|
||||
PRETRAINED_MT5_MODELS = [
|
||||
"google/mt5-small",
|
||||
"google/mt5-base",
|
||||
"google/mt5-large",
|
||||
"google/mt5-xl",
|
||||
"google/mt5-xxl",
|
||||
]
|
||||
|
||||
|
||||
class T5Helper:
|
||||
@staticmethod
|
||||
def get_onnx_path(
|
||||
output_dir: str,
|
||||
model_name_or_path: str,
|
||||
suffix: str = "",
|
||||
new_folder: bool = False,
|
||||
) -> str:
|
||||
"""Build onnx path
|
||||
|
||||
Args:
|
||||
output_dir (str): output directory
|
||||
model_name_or_path (str): pretrained model name, or path to the model checkpoint
|
||||
suffix (str, optional): suffix like "_encoder" or "_decoder_fp16" will be appended to file name. Defaults to None.
|
||||
new_folder (bool, optional): create a new directory for the model. Defaults to False.
|
||||
|
||||
Returns:
|
||||
str: path of onnx model
|
||||
"""
|
||||
model_name = model_name_or_path
|
||||
if os.path.isdir(model_name_or_path):
|
||||
model_name = Path(model_name_or_path).parts[-1]
|
||||
else:
|
||||
model_name.split("/")[-1]
|
||||
|
||||
model_name += suffix
|
||||
|
||||
directory = os.path.join(output_dir, model_name) if new_folder else output_dir
|
||||
return os.path.join(directory, model_name + ".onnx")
|
||||
|
||||
@staticmethod
|
||||
def load_model(
|
||||
model_name_or_path: str,
|
||||
cache_dir: str,
|
||||
device: torch.device,
|
||||
model_type: str = "t5",
|
||||
state_dict_path: str = "",
|
||||
encoder_decoder_init: bool = False,
|
||||
) -> dict[str, T5EncoderDecoderInit | T5Decoder]:
|
||||
"""Load model given a pretrained name or path, then build models for ONNX conversion.
|
||||
|
||||
Args:
|
||||
model_name_or_path (str): pretrained model name or path
|
||||
cache_dir (str): cache directory
|
||||
device (torch.device): device to run the model
|
||||
model_type (str, optional): model type "t5" or "mt5"
|
||||
state_dict_path(str, optional): state dictionary path
|
||||
encoder_decoder_init (bool, optional): combine encoder and decoder kv cache initialization into one model.
|
||||
Returns:
|
||||
Dict[str, torch.nn.Module]: mapping from name to modules for ONNX conversion.
|
||||
"""
|
||||
if model_type == "t5":
|
||||
model = T5ForConditionalGeneration.from_pretrained(model_name_or_path, cache_dir=cache_dir)
|
||||
elif model_type == "mt5":
|
||||
model = MT5ForConditionalGeneration.from_pretrained(model_name_or_path, cache_dir=cache_dir)
|
||||
else:
|
||||
raise ValueError("only support mode_type=t5 or mt5")
|
||||
|
||||
if state_dict_path:
|
||||
model.load_state_dict(torch.load(state_dict_path))
|
||||
|
||||
decoder = T5Decoder(model.decoder, model.lm_head, model.config)
|
||||
decoder.eval().to(device)
|
||||
|
||||
encoder = T5EncoderDecoderInit(
|
||||
model.encoder,
|
||||
model.decoder,
|
||||
model.lm_head,
|
||||
model.config,
|
||||
decoder_start_token_id=None,
|
||||
output_cross_only=not encoder_decoder_init,
|
||||
)
|
||||
|
||||
encoder_name = "encoder_decoder_init" if encoder_decoder_init else "encoder"
|
||||
return {encoder_name: encoder, "decoder": decoder}
|
||||
|
||||
@staticmethod
|
||||
def export_onnx(
|
||||
model: T5Decoder | T5EncoderDecoderInit,
|
||||
device: torch.device,
|
||||
onnx_model_path: str,
|
||||
verbose: bool = True,
|
||||
use_external_data_format: bool = False,
|
||||
use_decoder_input_ids: bool = True,
|
||||
use_int32_inputs: bool = False,
|
||||
):
|
||||
if isinstance(model, T5EncoderDecoderInit):
|
||||
T5EncoderDecoderInitHelper.export_onnx(
|
||||
model,
|
||||
device,
|
||||
onnx_model_path,
|
||||
use_decoder_input_ids,
|
||||
verbose,
|
||||
use_external_data_format,
|
||||
use_int32_inputs,
|
||||
)
|
||||
else:
|
||||
T5DecoderHelper.export_onnx(
|
||||
model,
|
||||
device,
|
||||
onnx_model_path,
|
||||
verbose,
|
||||
use_external_data_format,
|
||||
use_int32_inputs,
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def auto_mixed_precision(
|
||||
onnx_model: OnnxModel,
|
||||
op_block_list: list[str] | None = None,
|
||||
force_fp16_logits: bool = False,
|
||||
use_symbolic_shape_infer: bool = True,
|
||||
):
|
||||
"""Convert model to mixed precision.
|
||||
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): operators need to run in fp32.
|
||||
force_fp16_logits (bool, optional): force logits and last MatMul node to be in float16. Defaults to False.
|
||||
use_symbolic_shape_infer (bool, optional): use symbolic shape inference to convert float to float16. Defaults to True.
|
||||
Returns:
|
||||
parameters(dict): a dictionary of parameters used in float16 conversion
|
||||
"""
|
||||
if op_block_list is None:
|
||||
op_block_list = [
|
||||
"SimplifiedLayerNormalization",
|
||||
"SkipSimplifiedLayerNormalization",
|
||||
"Relu",
|
||||
"Add",
|
||||
]
|
||||
|
||||
op_full_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}")
|
||||
|
||||
# logits is the first output
|
||||
logits_output_name = onnx_model.graph().output[0].name
|
||||
|
||||
# We use the weight in last MatMul node to detect whether the model is stored with float16 weights from training.
|
||||
is_weight_fp16_precision = False
|
||||
output_name_to_node = onnx_model.output_name_to_node()
|
||||
assert logits_output_name in output_name_to_node
|
||||
node = output_name_to_node[logits_output_name]
|
||||
last_matmul_node = None
|
||||
if node.op_type == "MatMul":
|
||||
last_matmul_node = node
|
||||
logger.info(f"Found last MatMul node for logits: {node.name}")
|
||||
initializer = None
|
||||
for input in node.input:
|
||||
initializer = onnx_model.get_initializer(input)
|
||||
if initializer is not None:
|
||||
break
|
||||
|
||||
# when the max difference of value after converting float to float16 is lower than a threshold (1e-6),
|
||||
# we can deduce that the weights are stored in float16 precision.
|
||||
max_diff = float_to_float16_max_diff(initializer)
|
||||
logger.debug(f"max diff of converting weights in last MatMul node {node.name}: {max_diff}")
|
||||
is_weight_fp16_precision = max_diff < 1e-6
|
||||
else:
|
||||
logger.warning(f"Failed to find MatMul node for logits. Found {node.op_type} of node {node.name}")
|
||||
|
||||
keep_io_types = []
|
||||
node_block_list = []
|
||||
if (not is_weight_fp16_precision) and (last_matmul_node is not None) and not force_fp16_logits:
|
||||
# When original weight is float32 precision, keep logits and last MatMul in float32 could get better precision.
|
||||
keep_io_types = [logits_output_name]
|
||||
node_block_list = [last_matmul_node.name]
|
||||
|
||||
if "Add" not in op_block_list:
|
||||
input_name_to_nodes = onnx_model.input_name_to_nodes()
|
||||
fp32_add = 0
|
||||
changed = True
|
||||
add_nodes = onnx_model.get_nodes_by_op_type("Add")
|
||||
while changed:
|
||||
changed = False
|
||||
for node in add_nodes:
|
||||
if node.name not in node_block_list:
|
||||
parents = onnx_model.get_parents(node, output_name_to_node)
|
||||
children = onnx_model.get_children(node, input_name_to_nodes)
|
||||
blocked_children = [
|
||||
child for child in children if child.op_type in op_block_list or child in node_block_list
|
||||
]
|
||||
blocked_parents = [
|
||||
parent for parent in parents if parent.op_type in op_block_list or parent in node_block_list
|
||||
]
|
||||
# If any child or parent is in fp32, we place the Add node to fp32.
|
||||
if (len(blocked_children) + len(blocked_parents)) > 0:
|
||||
node_block_list.append(node.name)
|
||||
fp32_add += 1
|
||||
changed = True
|
||||
fp16_add = len(add_nodes) - fp32_add
|
||||
logger.info(f"node counter of Add operator: fp32={fp32_add} fp16={fp16_add}")
|
||||
|
||||
logger.info(f"node_block_list: {node_block_list}")
|
||||
|
||||
parameters = {
|
||||
"keep_io_types": keep_io_types,
|
||||
"op_block_list": op_block_list,
|
||||
"node_block_list": node_block_list,
|
||||
"force_fp16_initializers": is_weight_fp16_precision,
|
||||
}
|
||||
|
||||
logger.info(f"auto_mixed_precision parameters: {parameters}")
|
||||
if use_symbolic_shape_infer:
|
||||
onnx_model.convert_float_to_float16(use_symbolic_shape_infer=True, **parameters)
|
||||
else:
|
||||
# Workaround when symbolic shape inference fails.
|
||||
# Need enable shape_infer_before_optimization in convert_to_onnx.py as well.
|
||||
from float16 import convert_float_to_float16 # noqa: PLC0415
|
||||
|
||||
convert_float_to_float16(
|
||||
onnx_model.model,
|
||||
disable_shape_infer=True,
|
||||
**parameters,
|
||||
)
|
||||
|
||||
return parameters
|
||||
|
||||
@staticmethod
|
||||
def optimize_onnx(
|
||||
onnx_model_path: str,
|
||||
optimized_model_path: str,
|
||||
is_float16: bool,
|
||||
num_attention_heads: int,
|
||||
hidden_size: int,
|
||||
use_external_data_format: bool = False,
|
||||
auto_mixed_precision: bool = True,
|
||||
use_gpu: bool = False,
|
||||
force_fp16_io: bool = False,
|
||||
):
|
||||
"""Optimize ONNX model with an option to convert it to use mixed precision."""
|
||||
|
||||
from fusion_options import FusionOptions # noqa: PLC0415
|
||||
|
||||
optimization_options = None
|
||||
if is_float16:
|
||||
optimization_options = FusionOptions("t5")
|
||||
# SkipLayerNormalization is faster but might bring accuracy drop since it uses fp16 accumulation.
|
||||
optimization_options.enable_skip_layer_norm = not auto_mixed_precision
|
||||
|
||||
m = optimize_model(
|
||||
onnx_model_path,
|
||||
model_type="t5",
|
||||
num_heads=num_attention_heads,
|
||||
hidden_size=hidden_size,
|
||||
opt_level=0,
|
||||
optimization_options=optimization_options,
|
||||
use_gpu=use_gpu,
|
||||
)
|
||||
|
||||
if is_float16:
|
||||
if auto_mixed_precision:
|
||||
T5Helper.auto_mixed_precision(m, force_fp16_logits=force_fp16_io)
|
||||
else:
|
||||
m.convert_model_float32_to_float16(cast_input_output=force_fp16_io)
|
||||
|
||||
m.save_model_to_file(optimized_model_path, use_external_data_format, all_tensors_to_one_file=True)
|
||||
|
||||
@staticmethod
|
||||
def verify_onnx(
|
||||
model: T5Decoder | T5EncoderDecoderInit,
|
||||
ort_session: InferenceSession,
|
||||
device: torch.device,
|
||||
use_int32_inputs: bool,
|
||||
):
|
||||
"""Compare the result from PyTorch and OnnxRuntime to verify the ONNX model is good."""
|
||||
if isinstance(model, T5EncoderDecoderInit):
|
||||
return T5EncoderDecoderInitHelper.verify_onnx(model, ort_session, device, use_int32_inputs)
|
||||
|
||||
return T5DecoderHelper.verify_onnx(model, ort_session, device, use_int32_inputs)
|
||||
Loading…
Add table
Add a link
Reference in a new issue