onnxruntime/onnxruntime/python/tools/transformers
Tianlei Wu 95f9628776
Fix transformers optimizations for GPT-NeoX (#16743)
### Description
Fix some issues found in GPT-NeoX graph fusion:
(1) GPT-NeoX uses float16 weights. The step of using onnxruntime with
opt_level==1 uses CPU provider. Since most operators does not have fp16
in CPU EP, so extra Cast nodes are added to up cast to fp32.
(2) Add is shared by two LayerNormalization children, and
SkipLayerNormalization might cause invalid graph.
(3) Reshape fusion might miss since some part only check initializer but
not Constant.

This PR adds a check whether model uses FP16, and output a warning when
use_gpu is not True, and use GPU provider for graph optimization when use_gpu=True.
2023-07-18 10:29:08 -07:00
..
models Add Stable Diffusion Benchmark on A100-PCIE-80GB (#16702) 2023-07-14 10:37:00 -07:00
notebooks Fix typos in sources: operater, tranform, neccessary, trainig (#14907) 2023-03-13 22:45:04 -07:00
__init__.py Adopt linrtunner as the linting tool - take 2 (#15085) 2023-03-24 15:29:03 -07:00
affinity_helper.py
benchmark.py VIT model handling in the Benchmark.sh file (#15045) 2023-04-07 20:17:29 -07:00
benchmark_helper.py Bump ruff in CI (#15533) 2023-04-17 10:11:44 -07:00
bert_perf_test.py Allow bert_perf_test.py to load/save tuning results (#15096) 2023-03-26 18:03:08 +08:00
bert_test_data.py Enable pylint and numpy rules (#15218) 2023-03-27 20:37:53 -07:00
compare_bert_results.py Adopt linrtunner as the linting tool - take 2 (#15085) 2023-03-24 15:29:03 -07:00
constants.py Add tool to support packing mode for BERT model (#15283) 2023-03-31 08:46:47 -07:00
convert_generation.py Speed Up Whisper Export (#16504) 2023-06-30 12:22:30 -07:00
convert_tf_models_to_pytorch.py Bump ruff in CI (#15533) 2023-04-17 10:11:44 -07:00
convert_to_packing_mode.py Bump ruff in CI (#15533) 2023-04-17 10:11:44 -07:00
dev_benchmark.cmd
Dev_Guide.md
float16.py update stable diffusion script and doc (#15846) 2023-05-09 15:29:13 -07:00
fusion_attention.py [Whisper] add a fusion option to split input bias from MHA/DMHA (#16049) 2023-06-16 10:29:48 +08:00
fusion_attention_unet.py Attention fusion for UNet onnx model export from PyTorch 2.* (#16629) 2023-07-11 14:35:48 -07:00
fusion_attention_vae.py Stable Diffusion CUDA Optimizations Part 3 (#14646) 2023-02-14 12:46:50 -08:00
fusion_bart_attention.py [Whisper] add a fusion option to split input bias from MHA/DMHA (#16049) 2023-06-16 10:29:48 +08:00
fusion_base.py Fix transformers optimizations for GPT-NeoX (#16743) 2023-07-18 10:29:08 -07:00
fusion_bias_add.py Adopt linrtunner as the linting tool - take 2 (#15085) 2023-03-24 15:29:03 -07:00
fusion_biasgelu.py
fusion_biassplitgelu.py Adopt linrtunner as the linting tool - take 2 (#15085) 2023-03-24 15:29:03 -07:00
fusion_embedlayer.py Bump ruff in CI (#15533) 2023-04-17 10:11:44 -07:00
fusion_fastgelu.py
fusion_gelu.py
fusion_gelu_approximation.py Adopt linrtunner as the linting tool - take 2 (#15085) 2023-03-24 15:29:03 -07:00
fusion_gemmfastgelu.py
fusion_gpt_attention.py GPT-2 attention fusion for transformers >= 4.27 (#16461) 2023-06-23 15:38:35 -07:00
fusion_gpt_attention_megatron.py Adopt linrtunner as the linting tool - take 2 (#15085) 2023-03-24 15:29:03 -07:00
fusion_gpt_attention_no_past.py Adopt linrtunner as the linting tool - take 2 (#15085) 2023-03-24 15:29:03 -07:00
fusion_group_norm.py Stable Diffusion CUDA Optimizations Part 3 (#14646) 2023-02-14 12:46:50 -08:00
fusion_layernorm.py Fix transformers optimizations for GPT-NeoX (#16743) 2023-07-18 10:29:08 -07:00
fusion_nhwc_conv.py Stable Diffusion CUDA optimizations Part 2 (#14597) 2023-02-07 07:49:15 -08:00
fusion_options.py [Whisper] add a fusion option to split input bias from MHA/DMHA (#16049) 2023-06-16 10:29:48 +08:00
fusion_qordered_attention.py Adopt linrtunner as the linting tool - take 2 (#15085) 2023-03-24 15:29:03 -07:00
fusion_qordered_gelu.py Adopt linrtunner as the linting tool - take 2 (#15085) 2023-03-24 15:29:03 -07:00
fusion_qordered_layernorm.py Adopt linrtunner as the linting tool - take 2 (#15085) 2023-03-24 15:29:03 -07:00
fusion_qordered_matmul.py Adopt linrtunner as the linting tool - take 2 (#15085) 2023-03-24 15:29:03 -07:00
fusion_reshape.py Fix transformers optimizations for GPT-NeoX (#16743) 2023-07-18 10:29:08 -07:00
fusion_shape.py Adopt linrtunner as the linting tool - take 2 (#15085) 2023-03-24 15:29:03 -07:00
fusion_skiplayernorm.py Fix transformers optimizations for GPT-NeoX (#16743) 2023-07-18 10:29:08 -07:00
fusion_transpose.py Update stable diffusion benchmark for TensorRT EP (#16560) 2023-07-10 09:51:03 -07:00
fusion_utils.py Adopt linrtunner as the linting tool - take 2 (#15085) 2023-03-24 15:29:03 -07:00
huggingface_models.py VIT model handling in the Benchmark.sh file (#15045) 2023-04-07 20:17:29 -07:00
io_binding_helper.py Adopt linrtunner as the linting tool - take 2 (#15085) 2023-03-24 15:29:03 -07:00
machine_info.py Adopt linrtunner as the linting tool - take 2 (#15085) 2023-03-24 15:29:03 -07:00
onnx_exporter.py VIT model handling in the Benchmark.sh file (#15045) 2023-04-07 20:17:29 -07:00
onnx_model.py Fix transformers optimizations for GPT-NeoX (#16743) 2023-07-18 10:29:08 -07:00
onnx_model_bart.py [Whisper] add a fusion option to split input bias from MHA/DMHA (#16049) 2023-06-16 10:29:48 +08:00
onnx_model_bert.py Whisper Model Optimization (#15473) 2023-04-18 17:13:54 -07:00
onnx_model_bert_keras.py Adopt linrtunner as the linting tool - take 2 (#15085) 2023-03-24 15:29:03 -07:00
onnx_model_bert_tf.py Enable pylint and numpy rules (#15218) 2023-03-27 20:37:53 -07:00
onnx_model_clip.py Stable Diffusion CUDA Optimizations Part 3 (#14646) 2023-02-14 12:46:50 -08:00
onnx_model_gpt2.py Adopt linrtunner as the linting tool - take 2 (#15085) 2023-03-24 15:29:03 -07:00
onnx_model_t5.py stabilize fusion script with a seperate create_attention_node() (#15670) 2023-04-25 13:07:58 -07:00
onnx_model_tnlr.py Adopt linrtunner as the linting tool - take 2 (#15085) 2023-03-24 15:29:03 -07:00
onnx_model_unet.py Doc for Stable Diffusion CUDA Optimizations (#14830) 2023-03-01 19:29:30 -08:00
onnx_model_vae.py Doc for Stable Diffusion CUDA Optimizations (#14830) 2023-03-01 19:29:30 -08:00
optimizer.py Fix transformers optimizations for GPT-NeoX (#16743) 2023-07-18 10:29:08 -07:00
profiler.py Adopt linrtunner as the linting tool - take 2 (#15085) 2023-03-24 15:29:03 -07:00
pytest.ini
quantize_helper.py Adopt linrtunner as the linting tool - take 2 (#15085) 2023-03-24 15:29:03 -07:00
README.md Transformers tool - update readme to link to docs page (#14964) 2023-03-21 11:56:19 -07:00
requirements.txt Add support for full ViT optimization (#15289) 2023-04-04 14:05:24 -07:00
run_benchmark.sh Add PyTorch 2.0 to ORT transformer benchmarking (#14300) 2023-01-20 12:50:53 -08:00
shape_infer_helper.py Adopt linrtunner as the linting tool - take 2 (#15085) 2023-03-24 15:29:03 -07:00
shape_optimizer.py Adopt linrtunner as the linting tool - take 2 (#15085) 2023-03-24 15:29:03 -07:00
torch_onnx_export_helper.py Adopt linrtunner as the linting tool - take 2 (#15085) 2023-03-24 15:29:03 -07:00

Usage instructions: Transformers Tool Usage Documentation

Supported Models

The following models from Huggingface Transformers have been tested using the optimizer:

PyTorch:

  • BERT
  • DistilBERT
  • DistilGPT2
  • RoBERTa
  • ALBERT
  • GPT-2 (GPT2Model, GPT2LMHeadModel)

Tensorflow:

  • BERT

Most optimizations require exact match of a subgraph. Any layout change in subgraph might cause some optimization not working. Note that different versions of training or export tool might lead to different graph layouts. It is recommended to use latest released version of PyTorch and Transformers.

Models not in the list may only be partially optimized or not optimized at all.

Optimizer Options

  • input: input model path
  • output: output model path
  • model_type: (defaul: bert) There are 4 model types: bert (exported by PyTorch), gpt2 (exported by PyTorch), and bert_tf (BERT exported by tf2onnx), bert_keras (BERT exported by keras2onnx) respectively.
  • num_heads: (default: 12) Number of attention heads. BERT-base and BERT-large has 12 and 16 respectively.
  • hidden_size: (default: 768) BERT-base and BERT-large has 768 and 1024 hidden nodes respectively.
  • input_int32: (optional) Exported model ususally uses int64 tensor as input. If this flag is specified, int32 tensors will be used as input, and it could avoid un-necessary Cast nodes and get better performance.
  • float16: (optional) By default, model uses float32 in computation. If this flag is specified, half-precision float will be used. This option is recommended for NVidia GPU with Tensor Core like V100 and T4. For older GPUs, float32 is likely faster.
  • use_gpu: (optional) When opt_level > 1, please set this flag for GPU inference.
  • opt_level: (optional) Set a proper graph optimization level of OnnxRuntime: 0 - disable all (default), 1 - basic, 2 - extended, 99 - all. If the value is positive, OnnxRuntime will be used to optimize graph first.
  • verbose: (optional) Print verbose information when this flag is specified.

Benchmark Results

These benchmarks were executed on V100 machines using the optimizer with IO binding enabled.

We tested on Tesla V100-PCIE-16GB GPU (CPU is Intel Xeon(R) E5-2690 v4) for different batch size (b) and sequence length (s). Below result is average latency of per inference in miliseconds.

bert-base-uncased (BertModel)

The model has 12 layers and 768 hidden, with input_ids as input.

engine version precision b s=8 s=16 s=32 s=64 s=128 s=256 s=512
torchscript 1.5.1 fp32 1 7.92 8.78 8.91 9.18 9.56 9.39 12.83
onnxruntime 1.4.0 fp32 1 1.38 1.42 1.67 2.15 3.11 5.37 10.74
onnxruntime 1.4.0 fp16 1 1.30 1.29 1.31 1.33 1.45 1.95 3.36
onnxruntime 1.4.0 fp32 4 1.51 1.93 2.98 5.01 9.13 17.95 38.15
onnxruntime 1.4.0 fp16 4 1.27 1.35 1.43 1.83 2.66 4.40 9.76

run_benchmark.sh is used to get the results.

gpt2 (GPT2LMHeadModel)

The model has 12 layers and 768 hidden, with input_ids, position_ids, attention_mask and past state as inputs.

engine version precision b s=4 s=8 s=32 s=128
torchscript 1.5.1 fp32 1 5.80 5.77 5.82 5.78
onnxruntime 1.4.0 fp32 1 1.42 1.42 1.43 1.47
onnxruntime 1.4.0 fp16 1 1.54 1.54 1.58 1.64
onnxruntime 1.4.0 fp32 8 1.83 1.84 1.90 2.13
onnxruntime 1.4.0 fp16 8 1.74 1.75 1.81 2.09
onnxruntime 1.4.0 fp32 32 2.19 2.21 2.45 3.34
onnxruntime 1.4.0 fp16 32 1.66 1.71 1.85 2.73
onnxruntime 1.4.0 fp32 128 4.15 4.37 5.15 8.61
onnxruntime 1.4.0 fp16 128 2.47 2.58 3.26 6.16

Since past state is used, sequence length in input_ids is 1. For example, s=4 means the past sequence length is 4 and the total sequence length is 5.

benchmark_gpt2.py is used to get the results like the following commands:

python -m onnxruntime.transformers.models.gpt2.benchmark_gpt2 --use_gpu -m gpt2 -o -v -b 1 8 32 128 -s 4 8 32 128 -p fp32
python -m onnxruntime.transformers.models.gpt2.benchmark_gpt2 --use_gpu -m gpt2 -o -v -b 1 8 32 128 -s 4 8 32 128 -p fp16