onnxruntime-directml 1.24.1__cp314-cp314-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- onnxruntime/LICENSE +21 -0
- onnxruntime/Privacy.md +21 -0
- onnxruntime/ThirdPartyNotices.txt +6121 -0
- onnxruntime/__init__.py +418 -0
- onnxruntime/backend/__init__.py +6 -0
- onnxruntime/backend/backend.py +175 -0
- onnxruntime/backend/backend_rep.py +52 -0
- onnxruntime/capi/DirectML.dll +0 -0
- onnxruntime/capi/__init__.py +4 -0
- onnxruntime/capi/_ld_preload.py +7 -0
- onnxruntime/capi/_pybind_state.py +33 -0
- onnxruntime/capi/build_and_package_info.py +2 -0
- onnxruntime/capi/convert_npz_to_onnx_adapter.py +48 -0
- onnxruntime/capi/onnxruntime.dll +0 -0
- onnxruntime/capi/onnxruntime_collect_build_info.py +47 -0
- onnxruntime/capi/onnxruntime_inference_collection.py +1440 -0
- onnxruntime/capi/onnxruntime_providers_shared.dll +0 -0
- onnxruntime/capi/onnxruntime_pybind11_state.pyd +0 -0
- onnxruntime/capi/onnxruntime_validation.py +154 -0
- onnxruntime/capi/version_info.py +2 -0
- onnxruntime/datasets/__init__.py +18 -0
- onnxruntime/datasets/logreg_iris.onnx +0 -0
- onnxruntime/datasets/mul_1.onnx +0 -0
- onnxruntime/datasets/sigmoid.onnx +13 -0
- onnxruntime/quantization/CalTableFlatBuffers/KeyValue.py +78 -0
- onnxruntime/quantization/CalTableFlatBuffers/TrtTable.py +90 -0
- onnxruntime/quantization/CalTableFlatBuffers/__init__.py +0 -0
- onnxruntime/quantization/__init__.py +19 -0
- onnxruntime/quantization/base_quantizer.py +529 -0
- onnxruntime/quantization/calibrate.py +1267 -0
- onnxruntime/quantization/execution_providers/qnn/__init__.py +2 -0
- onnxruntime/quantization/execution_providers/qnn/fusion_lpnorm.py +132 -0
- onnxruntime/quantization/execution_providers/qnn/fusion_spacetodepth.py +162 -0
- onnxruntime/quantization/execution_providers/qnn/mixed_precision_overrides_utils.py +413 -0
- onnxruntime/quantization/execution_providers/qnn/preprocess.py +353 -0
- onnxruntime/quantization/execution_providers/qnn/quant_config.py +389 -0
- onnxruntime/quantization/fusions/__init__.py +4 -0
- onnxruntime/quantization/fusions/fusion.py +311 -0
- onnxruntime/quantization/fusions/fusion_gelu.py +272 -0
- onnxruntime/quantization/fusions/fusion_layernorm.py +146 -0
- onnxruntime/quantization/fusions/replace_upsample_with_resize.py +96 -0
- onnxruntime/quantization/matmul_bnb4_quantizer.py +239 -0
- onnxruntime/quantization/matmul_nbits_quantizer.py +1638 -0
- onnxruntime/quantization/neural_compressor/__init__.py +1 -0
- onnxruntime/quantization/neural_compressor/onnx_model.py +1251 -0
- onnxruntime/quantization/neural_compressor/util.py +80 -0
- onnxruntime/quantization/neural_compressor/weight_only.py +932 -0
- onnxruntime/quantization/onnx_model.py +600 -0
- onnxruntime/quantization/onnx_quantizer.py +1163 -0
- onnxruntime/quantization/operators/__init__.py +2 -0
- onnxruntime/quantization/operators/activation.py +119 -0
- onnxruntime/quantization/operators/argmax.py +18 -0
- onnxruntime/quantization/operators/attention.py +73 -0
- onnxruntime/quantization/operators/base_operator.py +26 -0
- onnxruntime/quantization/operators/binary_op.py +72 -0
- onnxruntime/quantization/operators/concat.py +62 -0
- onnxruntime/quantization/operators/conv.py +260 -0
- onnxruntime/quantization/operators/direct_q8.py +78 -0
- onnxruntime/quantization/operators/embed_layernorm.py +121 -0
- onnxruntime/quantization/operators/gather.py +64 -0
- onnxruntime/quantization/operators/gavgpool.py +62 -0
- onnxruntime/quantization/operators/gemm.py +172 -0
- onnxruntime/quantization/operators/lstm.py +121 -0
- onnxruntime/quantization/operators/matmul.py +231 -0
- onnxruntime/quantization/operators/maxpool.py +34 -0
- onnxruntime/quantization/operators/norm.py +40 -0
- onnxruntime/quantization/operators/pad.py +172 -0
- onnxruntime/quantization/operators/pooling.py +67 -0
- onnxruntime/quantization/operators/qdq_base_operator.py +22 -0
- onnxruntime/quantization/operators/resize.py +34 -0
- onnxruntime/quantization/operators/softmax.py +74 -0
- onnxruntime/quantization/operators/split.py +63 -0
- onnxruntime/quantization/operators/where.py +87 -0
- onnxruntime/quantization/preprocess.py +141 -0
- onnxruntime/quantization/qdq_loss_debug.py +389 -0
- onnxruntime/quantization/qdq_quantizer.py +1477 -0
- onnxruntime/quantization/quant_utils.py +1051 -0
- onnxruntime/quantization/quantize.py +953 -0
- onnxruntime/quantization/registry.py +110 -0
- onnxruntime/quantization/shape_inference.py +204 -0
- onnxruntime/quantization/static_quantize_runner.py +256 -0
- onnxruntime/quantization/tensor_quant_overrides.py +520 -0
- onnxruntime/tools/__init__.py +10 -0
- onnxruntime/tools/check_onnx_model_mobile_usability.py +47 -0
- onnxruntime/tools/convert_onnx_models_to_ort.py +380 -0
- onnxruntime/tools/file_utils.py +47 -0
- onnxruntime/tools/logger.py +11 -0
- onnxruntime/tools/make_dynamic_shape_fixed.py +73 -0
- onnxruntime/tools/mobile_helpers/__init__.py +0 -0
- onnxruntime/tools/mobile_helpers/coreml_supported_mlprogram_ops.md +53 -0
- onnxruntime/tools/mobile_helpers/coreml_supported_neuralnetwork_ops.md +43 -0
- onnxruntime/tools/mobile_helpers/nnapi_supported_ops.md +58 -0
- onnxruntime/tools/mobile_helpers/usability_checker.py +738 -0
- onnxruntime/tools/offline_tuning.py +169 -0
- onnxruntime/tools/onnx_model_utils.py +416 -0
- onnxruntime/tools/onnx_randomizer.py +85 -0
- onnxruntime/tools/onnxruntime_test.py +164 -0
- onnxruntime/tools/optimize_onnx_model.py +56 -0
- onnxruntime/tools/ort_format_model/__init__.py +27 -0
- onnxruntime/tools/ort_format_model/operator_type_usage_processors.py +653 -0
- onnxruntime/tools/ort_format_model/ort_flatbuffers_py/__init__.py +0 -0
- onnxruntime/tools/ort_format_model/ort_flatbuffers_py/fbs/ArgType.py +7 -0
- onnxruntime/tools/ort_format_model/ort_flatbuffers_py/fbs/ArgTypeAndIndex.py +67 -0
- onnxruntime/tools/ort_format_model/ort_flatbuffers_py/fbs/Attribute.py +337 -0
- onnxruntime/tools/ort_format_model/ort_flatbuffers_py/fbs/AttributeType.py +18 -0
- onnxruntime/tools/ort_format_model/ort_flatbuffers_py/fbs/Checkpoint.py +125 -0
- onnxruntime/tools/ort_format_model/ort_flatbuffers_py/fbs/DeprecatedKernelCreateInfos.py +120 -0
- onnxruntime/tools/ort_format_model/ort_flatbuffers_py/fbs/DeprecatedNodeIndexAndKernelDefHash.py +68 -0
- onnxruntime/tools/ort_format_model/ort_flatbuffers_py/fbs/DeprecatedSessionState.py +96 -0
- onnxruntime/tools/ort_format_model/ort_flatbuffers_py/fbs/DeprecatedSubGraphSessionState.py +72 -0
- onnxruntime/tools/ort_format_model/ort_flatbuffers_py/fbs/Dimension.py +71 -0
- onnxruntime/tools/ort_format_model/ort_flatbuffers_py/fbs/DimensionValue.py +80 -0
- onnxruntime/tools/ort_format_model/ort_flatbuffers_py/fbs/DimensionValueType.py +8 -0
- onnxruntime/tools/ort_format_model/ort_flatbuffers_py/fbs/EdgeEnd.py +32 -0
- onnxruntime/tools/ort_format_model/ort_flatbuffers_py/fbs/FloatProperty.py +67 -0
- onnxruntime/tools/ort_format_model/ort_flatbuffers_py/fbs/Graph.py +320 -0
- onnxruntime/tools/ort_format_model/ort_flatbuffers_py/fbs/InferenceSession.py +88 -0
- onnxruntime/tools/ort_format_model/ort_flatbuffers_py/fbs/IntProperty.py +67 -0
- onnxruntime/tools/ort_format_model/ort_flatbuffers_py/fbs/KernelTypeStrArgsEntry.py +91 -0
- onnxruntime/tools/ort_format_model/ort_flatbuffers_py/fbs/KernelTypeStrResolver.py +78 -0
- onnxruntime/tools/ort_format_model/ort_flatbuffers_py/fbs/MapType.py +71 -0
- onnxruntime/tools/ort_format_model/ort_flatbuffers_py/fbs/Model.py +223 -0
- onnxruntime/tools/ort_format_model/ort_flatbuffers_py/fbs/ModuleState.py +141 -0
- onnxruntime/tools/ort_format_model/ort_flatbuffers_py/fbs/Node.py +317 -0
- onnxruntime/tools/ort_format_model/ort_flatbuffers_py/fbs/NodeEdge.py +126 -0
- onnxruntime/tools/ort_format_model/ort_flatbuffers_py/fbs/NodeType.py +7 -0
- onnxruntime/tools/ort_format_model/ort_flatbuffers_py/fbs/NodesToOptimizeIndices.py +160 -0
- onnxruntime/tools/ort_format_model/ort_flatbuffers_py/fbs/OpIdKernelTypeStrArgsEntry.py +91 -0
- onnxruntime/tools/ort_format_model/ort_flatbuffers_py/fbs/OperatorSetId.py +67 -0
- onnxruntime/tools/ort_format_model/ort_flatbuffers_py/fbs/OptimizerGroup.py +117 -0
- onnxruntime/tools/ort_format_model/ort_flatbuffers_py/fbs/ParameterOptimizerState.py +91 -0
- onnxruntime/tools/ort_format_model/ort_flatbuffers_py/fbs/PropertyBag.py +152 -0
- onnxruntime/tools/ort_format_model/ort_flatbuffers_py/fbs/RuntimeOptimizationRecord.py +105 -0
- onnxruntime/tools/ort_format_model/ort_flatbuffers_py/fbs/RuntimeOptimizationRecordContainerEntry.py +91 -0
- onnxruntime/tools/ort_format_model/ort_flatbuffers_py/fbs/RuntimeOptimizations.py +79 -0
- onnxruntime/tools/ort_format_model/ort_flatbuffers_py/fbs/SequenceType.py +58 -0
- onnxruntime/tools/ort_format_model/ort_flatbuffers_py/fbs/Shape.py +78 -0
- onnxruntime/tools/ort_format_model/ort_flatbuffers_py/fbs/SparseTensor.py +114 -0
- onnxruntime/tools/ort_format_model/ort_flatbuffers_py/fbs/StringProperty.py +67 -0
- onnxruntime/tools/ort_format_model/ort_flatbuffers_py/fbs/StringStringEntry.py +67 -0
- onnxruntime/tools/ort_format_model/ort_flatbuffers_py/fbs/Tensor.py +203 -0
- onnxruntime/tools/ort_format_model/ort_flatbuffers_py/fbs/TensorDataType.py +26 -0
- onnxruntime/tools/ort_format_model/ort_flatbuffers_py/fbs/TensorTypeAndShape.py +71 -0
- onnxruntime/tools/ort_format_model/ort_flatbuffers_py/fbs/TypeInfo.py +83 -0
- onnxruntime/tools/ort_format_model/ort_flatbuffers_py/fbs/TypeInfoValue.py +9 -0
- onnxruntime/tools/ort_format_model/ort_flatbuffers_py/fbs/ValueInfo.py +84 -0
- onnxruntime/tools/ort_format_model/ort_flatbuffers_py/fbs/__init__.py +6 -0
- onnxruntime/tools/ort_format_model/ort_model_processor.py +86 -0
- onnxruntime/tools/ort_format_model/types.py +85 -0
- onnxruntime/tools/ort_format_model/utils.py +61 -0
- onnxruntime/tools/pytorch_export_contrib_ops.py +129 -0
- onnxruntime/tools/pytorch_export_helpers.py +131 -0
- onnxruntime/tools/qdq_helpers/__init__.py +0 -0
- onnxruntime/tools/qdq_helpers/optimize_qdq_model.py +37 -0
- onnxruntime/tools/qnn/add_trans_cast.py +292 -0
- onnxruntime/tools/qnn/gen_qnn_ctx_onnx_model.py +364 -0
- onnxruntime/tools/qnn/preprocess.py +165 -0
- onnxruntime/tools/reduced_build_config_parser.py +203 -0
- onnxruntime/tools/remove_initializer_from_input.py +37 -0
- onnxruntime/tools/symbolic_shape_infer.py +3094 -0
- onnxruntime/tools/update_onnx_opset.py +31 -0
- onnxruntime/transformers/__init__.py +8 -0
- onnxruntime/transformers/affinity_helper.py +40 -0
- onnxruntime/transformers/benchmark.py +942 -0
- onnxruntime/transformers/benchmark_helper.py +643 -0
- onnxruntime/transformers/bert_perf_test.py +629 -0
- onnxruntime/transformers/bert_test_data.py +641 -0
- onnxruntime/transformers/compare_bert_results.py +256 -0
- onnxruntime/transformers/constants.py +47 -0
- onnxruntime/transformers/convert_generation.py +3605 -0
- onnxruntime/transformers/convert_tf_models_to_pytorch.py +205 -0
- onnxruntime/transformers/convert_to_packing_mode.py +385 -0
- onnxruntime/transformers/dynamo_onnx_helper.py +205 -0
- onnxruntime/transformers/float16.py +501 -0
- onnxruntime/transformers/fusion_attention.py +1189 -0
- onnxruntime/transformers/fusion_attention_clip.py +340 -0
- onnxruntime/transformers/fusion_attention_sam2.py +533 -0
- onnxruntime/transformers/fusion_attention_unet.py +1307 -0
- onnxruntime/transformers/fusion_attention_vae.py +300 -0
- onnxruntime/transformers/fusion_bart_attention.py +435 -0
- onnxruntime/transformers/fusion_base.py +141 -0
- onnxruntime/transformers/fusion_bias_add.py +57 -0
- onnxruntime/transformers/fusion_biasgelu.py +66 -0
- onnxruntime/transformers/fusion_biassplitgelu.py +110 -0
- onnxruntime/transformers/fusion_conformer_attention.py +222 -0
- onnxruntime/transformers/fusion_constant_fold.py +144 -0
- onnxruntime/transformers/fusion_embedlayer.py +810 -0
- onnxruntime/transformers/fusion_fastgelu.py +492 -0
- onnxruntime/transformers/fusion_gelu.py +258 -0
- onnxruntime/transformers/fusion_gelu_approximation.py +25 -0
- onnxruntime/transformers/fusion_gemmfastgelu.py +121 -0
- onnxruntime/transformers/fusion_gpt_attention.py +546 -0
- onnxruntime/transformers/fusion_gpt_attention_megatron.py +355 -0
- onnxruntime/transformers/fusion_gpt_attention_no_past.py +260 -0
- onnxruntime/transformers/fusion_group_norm.py +180 -0
- onnxruntime/transformers/fusion_layernorm.py +489 -0
- onnxruntime/transformers/fusion_mha_mmdit.py +667 -0
- onnxruntime/transformers/fusion_nhwc_conv.py +99 -0
- onnxruntime/transformers/fusion_options.py +340 -0
- onnxruntime/transformers/fusion_qordered_attention.py +420 -0
- onnxruntime/transformers/fusion_qordered_gelu.py +118 -0
- onnxruntime/transformers/fusion_qordered_layernorm.py +122 -0
- onnxruntime/transformers/fusion_qordered_matmul.py +216 -0
- onnxruntime/transformers/fusion_quickgelu.py +74 -0
- onnxruntime/transformers/fusion_reshape.py +173 -0
- onnxruntime/transformers/fusion_rotary_attention.py +1591 -0
- onnxruntime/transformers/fusion_shape.py +109 -0
- onnxruntime/transformers/fusion_simplified_layernorm.py +165 -0
- onnxruntime/transformers/fusion_skip_group_norm.py +254 -0
- onnxruntime/transformers/fusion_skiplayernorm.py +209 -0
- onnxruntime/transformers/fusion_transpose.py +167 -0
- onnxruntime/transformers/fusion_utils.py +321 -0
- onnxruntime/transformers/huggingface_models.py +74 -0
- onnxruntime/transformers/import_utils.py +20 -0
- onnxruntime/transformers/io_binding_helper.py +487 -0
- onnxruntime/transformers/large_model_exporter.py +395 -0
- onnxruntime/transformers/machine_info.py +230 -0
- onnxruntime/transformers/metrics.py +163 -0
- onnxruntime/transformers/models/bart/__init__.py +12 -0
- onnxruntime/transformers/models/bart/export.py +98 -0
- onnxruntime/transformers/models/bert/__init__.py +12 -0
- onnxruntime/transformers/models/bert/eval_squad.py +329 -0
- onnxruntime/transformers/models/gpt2/__init__.py +12 -0
- onnxruntime/transformers/models/gpt2/benchmark_gpt2.py +413 -0
- onnxruntime/transformers/models/gpt2/convert_to_onnx.py +566 -0
- onnxruntime/transformers/models/gpt2/gpt2_helper.py +1031 -0
- onnxruntime/transformers/models/gpt2/gpt2_parity.py +513 -0
- onnxruntime/transformers/models/gpt2/gpt2_tester.py +501 -0
- onnxruntime/transformers/models/gpt2/parity_check_helper.py +146 -0
- onnxruntime/transformers/models/llama/__init__.py +12 -0
- onnxruntime/transformers/models/llama/benchmark.py +700 -0
- onnxruntime/transformers/models/llama/benchmark_all.py +488 -0
- onnxruntime/transformers/models/llama/benchmark_e2e.py +608 -0
- onnxruntime/transformers/models/llama/convert_to_onnx.py +1064 -0
- onnxruntime/transformers/models/llama/dist_settings.py +57 -0
- onnxruntime/transformers/models/llama/llama_inputs.py +504 -0
- onnxruntime/transformers/models/llama/llama_parity.py +343 -0
- onnxruntime/transformers/models/llama/llama_torch.py +47 -0
- onnxruntime/transformers/models/llama/quant_kv_dataloader.py +108 -0
- onnxruntime/transformers/models/longformer/__init__.py +12 -0
- onnxruntime/transformers/models/longformer/benchmark_longformer.py +821 -0
- onnxruntime/transformers/models/longformer/convert_to_onnx.py +413 -0
- onnxruntime/transformers/models/longformer/generate_test_data.py +347 -0
- onnxruntime/transformers/models/longformer/longformer_helper.py +76 -0
- onnxruntime/transformers/models/phi2/__init__.py +12 -0
- onnxruntime/transformers/models/phi2/convert_to_onnx.py +590 -0
- onnxruntime/transformers/models/phi2/inference_example.py +414 -0
- onnxruntime/transformers/models/sam2/__init__.py +12 -0
- onnxruntime/transformers/models/sam2/benchmark_sam2.py +638 -0
- onnxruntime/transformers/models/sam2/convert_to_onnx.py +270 -0
- onnxruntime/transformers/models/sam2/image_decoder.py +272 -0
- onnxruntime/transformers/models/sam2/image_encoder.py +236 -0
- onnxruntime/transformers/models/sam2/mask_decoder.py +208 -0
- onnxruntime/transformers/models/sam2/nvtx_helper.py +33 -0
- onnxruntime/transformers/models/sam2/prompt_encoder.py +189 -0
- onnxruntime/transformers/models/sam2/sam2_demo.py +321 -0
- onnxruntime/transformers/models/sam2/sam2_image_onnx_predictor.py +279 -0
- onnxruntime/transformers/models/sam2/sam2_utils.py +147 -0
- onnxruntime/transformers/models/stable_diffusion/__init__.py +12 -0
- onnxruntime/transformers/models/stable_diffusion/benchmark.py +1519 -0
- onnxruntime/transformers/models/stable_diffusion/benchmark_controlnet.py +426 -0
- onnxruntime/transformers/models/stable_diffusion/demo_txt2img.py +103 -0
- onnxruntime/transformers/models/stable_diffusion/demo_txt2img_xl.py +269 -0
- onnxruntime/transformers/models/stable_diffusion/demo_utils.py +778 -0
- onnxruntime/transformers/models/stable_diffusion/diffusion_models.py +1318 -0
- onnxruntime/transformers/models/stable_diffusion/diffusion_schedulers.py +1179 -0
- onnxruntime/transformers/models/stable_diffusion/engine_builder.py +295 -0
- onnxruntime/transformers/models/stable_diffusion/engine_builder_ort_cuda.py +387 -0
- onnxruntime/transformers/models/stable_diffusion/engine_builder_ort_trt.py +288 -0
- onnxruntime/transformers/models/stable_diffusion/engine_builder_tensorrt.py +395 -0
- onnxruntime/transformers/models/stable_diffusion/engine_builder_torch.py +108 -0
- onnxruntime/transformers/models/stable_diffusion/optimize_pipeline.py +590 -0
- onnxruntime/transformers/models/stable_diffusion/ort_optimizer.py +136 -0
- onnxruntime/transformers/models/stable_diffusion/pipeline_stable_diffusion.py +831 -0
- onnxruntime/transformers/models/stable_diffusion/trt_utilities.py +12 -0
- onnxruntime/transformers/models/t5/__init__.py +12 -0
- onnxruntime/transformers/models/t5/convert_to_onnx.py +318 -0
- onnxruntime/transformers/models/t5/t5_decoder.py +437 -0
- onnxruntime/transformers/models/t5/t5_encoder.py +70 -0
- onnxruntime/transformers/models/t5/t5_encoder_decoder_init.py +361 -0
- onnxruntime/transformers/models/t5/t5_helper.py +302 -0
- onnxruntime/transformers/models/whisper/__init__.py +12 -0
- onnxruntime/transformers/models/whisper/benchmark.py +585 -0
- onnxruntime/transformers/models/whisper/benchmark_all.py +526 -0
- onnxruntime/transformers/models/whisper/convert_to_onnx.py +609 -0
- onnxruntime/transformers/models/whisper/whisper_chain.py +334 -0
- onnxruntime/transformers/models/whisper/whisper_decoder.py +464 -0
- onnxruntime/transformers/models/whisper/whisper_encoder.py +164 -0
- onnxruntime/transformers/models/whisper/whisper_encoder_decoder_init.py +371 -0
- onnxruntime/transformers/models/whisper/whisper_helper.py +1035 -0
- onnxruntime/transformers/models/whisper/whisper_inputs.py +380 -0
- onnxruntime/transformers/models/whisper/whisper_jump_times.py +477 -0
- onnxruntime/transformers/onnx_exporter.py +719 -0
- onnxruntime/transformers/onnx_model.py +1636 -0
- onnxruntime/transformers/onnx_model_bart.py +141 -0
- onnxruntime/transformers/onnx_model_bert.py +488 -0
- onnxruntime/transformers/onnx_model_bert_keras.py +474 -0
- onnxruntime/transformers/onnx_model_bert_tf.py +588 -0
- onnxruntime/transformers/onnx_model_clip.py +42 -0
- onnxruntime/transformers/onnx_model_conformer.py +32 -0
- onnxruntime/transformers/onnx_model_gpt2.py +101 -0
- onnxruntime/transformers/onnx_model_mmdit.py +112 -0
- onnxruntime/transformers/onnx_model_phi.py +929 -0
- onnxruntime/transformers/onnx_model_sam2.py +137 -0
- onnxruntime/transformers/onnx_model_t5.py +985 -0
- onnxruntime/transformers/onnx_model_tnlr.py +226 -0
- onnxruntime/transformers/onnx_model_unet.py +258 -0
- onnxruntime/transformers/onnx_model_vae.py +42 -0
- onnxruntime/transformers/onnx_utils.py +55 -0
- onnxruntime/transformers/optimizer.py +620 -0
- onnxruntime/transformers/past_helper.py +149 -0
- onnxruntime/transformers/profile_result_processor.py +358 -0
- onnxruntime/transformers/profiler.py +434 -0
- onnxruntime/transformers/quantize_helper.py +76 -0
- onnxruntime/transformers/shape_infer_helper.py +121 -0
- onnxruntime/transformers/shape_optimizer.py +400 -0
- onnxruntime/transformers/torch_onnx_export_helper.py +74 -0
- onnxruntime_directml-1.24.1.dist-info/METADATA +216 -0
- onnxruntime_directml-1.24.1.dist-info/RECORD +322 -0
- onnxruntime_directml-1.24.1.dist-info/WHEEL +5 -0
- onnxruntime_directml-1.24.1.dist-info/entry_points.txt +2 -0
- onnxruntime_directml-1.24.1.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,474 @@
|
|
|
1
|
+
# -------------------------------------------------------------------------
|
|
2
|
+
# Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
# Licensed under the MIT License.
|
|
4
|
+
# --------------------------------------------------------------------------
|
|
5
|
+
|
|
6
|
+
import logging
|
|
7
|
+
|
|
8
|
+
import onnx
|
|
9
|
+
from onnx import numpy_helper
|
|
10
|
+
from onnx_model_bert_tf import BertOnnxModelTF
|
|
11
|
+
|
|
12
|
+
logger = logging.getLogger(__name__)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class BertOnnxModelKeras(BertOnnxModelTF):
|
|
16
|
+
def __init__(self, model, num_heads, hidden_size):
|
|
17
|
+
super().__init__(model, num_heads, hidden_size)
|
|
18
|
+
|
|
19
|
+
def match_mask_path(self, add_or_sub_before_softmax):
|
|
20
|
+
mask_nodes = self.match_parent_path(
|
|
21
|
+
add_or_sub_before_softmax,
|
|
22
|
+
["Mul", "Sub", "Reshape", "Cast"],
|
|
23
|
+
[1, None, 1, 0],
|
|
24
|
+
)
|
|
25
|
+
if mask_nodes is not None:
|
|
26
|
+
return mask_nodes
|
|
27
|
+
|
|
28
|
+
mask_nodes = self.match_parent_path(
|
|
29
|
+
add_or_sub_before_softmax,
|
|
30
|
+
["Mul", "Sub", "Cast", "Slice", "Unsqueeze"],
|
|
31
|
+
[1, 1, 1, 0, 0],
|
|
32
|
+
)
|
|
33
|
+
if mask_nodes is not None:
|
|
34
|
+
return mask_nodes
|
|
35
|
+
|
|
36
|
+
mask_nodes = self.match_parent_path(
|
|
37
|
+
add_or_sub_before_softmax,
|
|
38
|
+
["Mul", "Sub", "Cast", "Unsqueeze", "Unsqueeze"],
|
|
39
|
+
[1, None, 1, 0, 0],
|
|
40
|
+
)
|
|
41
|
+
return mask_nodes
|
|
42
|
+
|
|
43
|
+
def check_attention_input(self, matmul_q, matmul_k, matmul_v, parent, output_name_to_node):
|
|
44
|
+
reshape_nodes = []
|
|
45
|
+
|
|
46
|
+
for x in [matmul_q, matmul_k, matmul_v]:
|
|
47
|
+
root_input = x.input[0]
|
|
48
|
+
root_node = output_name_to_node[root_input]
|
|
49
|
+
if root_node == parent:
|
|
50
|
+
continue
|
|
51
|
+
if root_node.op_type == "Reshape" and root_node.input[0] == parent.output[0]:
|
|
52
|
+
reshape_nodes.append(root_node)
|
|
53
|
+
continue
|
|
54
|
+
logger.debug(f"Check attention input failed:{root_input}, {parent.output[0]}")
|
|
55
|
+
return False, []
|
|
56
|
+
|
|
57
|
+
return True, reshape_nodes
|
|
58
|
+
|
|
59
|
+
def fuse_attention(self):
|
|
60
|
+
self.input_name_to_nodes()
|
|
61
|
+
output_name_to_node = self.output_name_to_node()
|
|
62
|
+
|
|
63
|
+
nodes_to_remove = []
|
|
64
|
+
attention_count = 0
|
|
65
|
+
|
|
66
|
+
skip_layer_norm_nodes = self.get_nodes_by_op_type("SkipLayerNormalization")
|
|
67
|
+
for normalize_node in skip_layer_norm_nodes:
|
|
68
|
+
# SkipLayerNormalization has two inputs, and one of them is the root input for attention.
|
|
69
|
+
parent = self.get_parent(normalize_node, 0)
|
|
70
|
+
if parent is None or parent.op_type not in [
|
|
71
|
+
"SkipLayerNormalization",
|
|
72
|
+
"EmbedLayerNormalization",
|
|
73
|
+
]:
|
|
74
|
+
if parent.op_type == "Add":
|
|
75
|
+
parent = self.get_parent(normalize_node, 1)
|
|
76
|
+
if parent is None or parent.op_type not in [
|
|
77
|
+
"SkipLayerNormalization",
|
|
78
|
+
"EmbedLayerNormalization",
|
|
79
|
+
]:
|
|
80
|
+
logger.debug(f"First input for skiplayernorm: {parent.op_type if parent is not None else None}")
|
|
81
|
+
continue
|
|
82
|
+
else:
|
|
83
|
+
logger.debug(f"First input for skiplayernorm: {parent.op_type if parent is not None else None}")
|
|
84
|
+
continue
|
|
85
|
+
else:
|
|
86
|
+
# TODO: shall we add back the checking of children op types.
|
|
87
|
+
pass
|
|
88
|
+
|
|
89
|
+
qkv_nodes = self.match_parent_path(
|
|
90
|
+
normalize_node,
|
|
91
|
+
["Add", "Reshape", "MatMul", "Reshape", "Transpose", "MatMul"],
|
|
92
|
+
[None, 0, 0, 0, 0, 0],
|
|
93
|
+
)
|
|
94
|
+
if qkv_nodes is None:
|
|
95
|
+
logger.debug("Failed to match qkv nodes")
|
|
96
|
+
continue
|
|
97
|
+
(
|
|
98
|
+
add,
|
|
99
|
+
extra_reshape_0,
|
|
100
|
+
matmul,
|
|
101
|
+
reshape_qkv,
|
|
102
|
+
transpose_qkv,
|
|
103
|
+
matmul_qkv,
|
|
104
|
+
) = qkv_nodes
|
|
105
|
+
logger.debug("Matched qkv nodes")
|
|
106
|
+
|
|
107
|
+
v_nodes = self.match_parent_path(
|
|
108
|
+
matmul_qkv,
|
|
109
|
+
["Transpose", "Reshape", "Add", "Reshape", "MatMul"],
|
|
110
|
+
[1, 0, 0, 0, 0],
|
|
111
|
+
)
|
|
112
|
+
if v_nodes is None:
|
|
113
|
+
logger.debug("Failed to match v path")
|
|
114
|
+
continue
|
|
115
|
+
(transpose_v, reshape_v, add_v, extra_reshape_1, matmul_v) = v_nodes
|
|
116
|
+
|
|
117
|
+
qk_nodes = self.match_parent_path(matmul_qkv, ["Softmax", "Sub", "MatMul"], [0, 0, 0])
|
|
118
|
+
if qk_nodes is not None:
|
|
119
|
+
(softmax_qk, sub_qk, matmul_qk) = qk_nodes
|
|
120
|
+
q_nodes = self.match_parent_path(
|
|
121
|
+
matmul_qk,
|
|
122
|
+
["Mul", "Transpose", "Reshape", "Add", "Reshape", "MatMul"],
|
|
123
|
+
[0, None, 0, 0, 0, 0],
|
|
124
|
+
)
|
|
125
|
+
if q_nodes is not None:
|
|
126
|
+
(
|
|
127
|
+
mul_q,
|
|
128
|
+
transpose_q,
|
|
129
|
+
reshape_q,
|
|
130
|
+
add_q,
|
|
131
|
+
extra_reshape_2,
|
|
132
|
+
matmul_q,
|
|
133
|
+
) = q_nodes
|
|
134
|
+
|
|
135
|
+
else:
|
|
136
|
+
qk_nodes = self.match_parent_path(matmul_qkv, ["Softmax", "Add", "Mul", "MatMul"], [0, 0, 0, None])
|
|
137
|
+
if qk_nodes is None:
|
|
138
|
+
qk_nodes = self.match_parent_path(matmul_qkv, ["Softmax", "Add", "Div", "MatMul"], [0, 0, 0, None])
|
|
139
|
+
if qk_nodes is None:
|
|
140
|
+
logger.debug("Failed to match qk path")
|
|
141
|
+
continue
|
|
142
|
+
(softmax_qk, add_qk, mul_qk, matmul_qk) = qk_nodes
|
|
143
|
+
|
|
144
|
+
q_nodes = self.match_parent_path(
|
|
145
|
+
matmul_qk,
|
|
146
|
+
["Transpose", "Reshape", "Add", "Reshape", "MatMul"],
|
|
147
|
+
[0, 0, 0, 0, 0],
|
|
148
|
+
)
|
|
149
|
+
if q_nodes is not None:
|
|
150
|
+
(transpose_q, reshape_q, add_q, extra_reshape_2, matmul_q) = q_nodes
|
|
151
|
+
|
|
152
|
+
if q_nodes is None:
|
|
153
|
+
logger.debug("Failed to match q path")
|
|
154
|
+
continue
|
|
155
|
+
|
|
156
|
+
k_nodes = self.match_parent_path(
|
|
157
|
+
matmul_qk,
|
|
158
|
+
["Transpose", "Reshape", "Add", "Reshape", "MatMul"],
|
|
159
|
+
[1, 0, 0, 0, 0],
|
|
160
|
+
)
|
|
161
|
+
if k_nodes is None:
|
|
162
|
+
logger.debug("Failed to match k path")
|
|
163
|
+
continue
|
|
164
|
+
(transpose_k, reshape_k, add_k, extra_reshape_3, matmul_k) = k_nodes
|
|
165
|
+
|
|
166
|
+
mask_nodes = self.match_mask_path(qk_nodes[1])
|
|
167
|
+
if mask_nodes is None:
|
|
168
|
+
logger.debug("Failed to match mask path")
|
|
169
|
+
continue
|
|
170
|
+
if not self.has_constant_input(mask_nodes[1], 1):
|
|
171
|
+
logger.debug("Sub node expected to have an input with constant value 1.0.")
|
|
172
|
+
continue
|
|
173
|
+
|
|
174
|
+
is_same_root, reshape_nodes = self.check_attention_input(
|
|
175
|
+
matmul_q, matmul_k, matmul_v, parent, output_name_to_node
|
|
176
|
+
)
|
|
177
|
+
if is_same_root:
|
|
178
|
+
mask_index = self.attention_mask.process_mask(mask_nodes[-1].input[0])
|
|
179
|
+
logger.debug("Create an Attention node.")
|
|
180
|
+
attention_node = self.attention_fusion.create_attention_node(
|
|
181
|
+
mask_index=mask_index,
|
|
182
|
+
q_matmul=matmul_q,
|
|
183
|
+
k_matmul=matmul_k,
|
|
184
|
+
v_matmul=matmul_v,
|
|
185
|
+
q_add=add_q,
|
|
186
|
+
k_add=add_k,
|
|
187
|
+
v_add=add_v,
|
|
188
|
+
num_heads=self.num_heads,
|
|
189
|
+
hidden_size=self.hidden_size,
|
|
190
|
+
first_input=parent.output[0],
|
|
191
|
+
output=reshape_qkv.output[0],
|
|
192
|
+
)
|
|
193
|
+
if attention_node is None:
|
|
194
|
+
continue
|
|
195
|
+
|
|
196
|
+
self.add_node(attention_node)
|
|
197
|
+
attention_count += 1
|
|
198
|
+
|
|
199
|
+
nodes_to_remove.extend([reshape_qkv, transpose_qkv, matmul_qkv])
|
|
200
|
+
nodes_to_remove.extend(qk_nodes)
|
|
201
|
+
nodes_to_remove.extend(q_nodes)
|
|
202
|
+
nodes_to_remove.extend(k_nodes)
|
|
203
|
+
nodes_to_remove.extend(v_nodes)
|
|
204
|
+
nodes_to_remove.extend(mask_nodes)
|
|
205
|
+
nodes_to_remove.extend(reshape_nodes)
|
|
206
|
+
nodes_to_remove.append(extra_reshape_0)
|
|
207
|
+
self.replace_node_input(add, extra_reshape_0.output[0], matmul.output[0])
|
|
208
|
+
else:
|
|
209
|
+
logger.debug("Root node not matched.")
|
|
210
|
+
continue
|
|
211
|
+
self.remove_nodes(nodes_to_remove)
|
|
212
|
+
self.update_graph()
|
|
213
|
+
logger.info(f"Fused Attention count:{attention_count}")
|
|
214
|
+
|
|
215
|
+
def preprocess(self):
|
|
216
|
+
self.process_embedding()
|
|
217
|
+
self.fuse_mask()
|
|
218
|
+
self.skip_reshape()
|
|
219
|
+
|
|
220
|
+
def skip_reshape(self):
|
|
221
|
+
self.input_name_to_nodes()
|
|
222
|
+
self.output_name_to_node()
|
|
223
|
+
|
|
224
|
+
count = 0
|
|
225
|
+
reshape_nodes = self.get_nodes_by_op_type("Reshape")
|
|
226
|
+
for reshape_node in reshape_nodes:
|
|
227
|
+
parent = self.get_parent(reshape_node, 0)
|
|
228
|
+
if parent is not None and parent.op_type == "Reshape":
|
|
229
|
+
reshape_node.input[0] = parent.input[0]
|
|
230
|
+
count += 1
|
|
231
|
+
|
|
232
|
+
if count > 0:
|
|
233
|
+
logger.info(f"Skip consequent Reshape count: {count}")
|
|
234
|
+
|
|
235
|
+
def fuse_embedding(self, node, output_name_to_node):
|
|
236
|
+
assert node.op_type == "LayerNormalization"
|
|
237
|
+
logger.debug(f"start fusing embedding from node with output={node.output[0]}...")
|
|
238
|
+
word_embed_path = self.match_parent_path(node, ["Add", "Add", "Gather"], [0, 0, 0], output_name_to_node)
|
|
239
|
+
if word_embed_path is None:
|
|
240
|
+
logger.debug("failed to match word_embed_path")
|
|
241
|
+
return False
|
|
242
|
+
|
|
243
|
+
skip_node, add_node, gather_node = word_embed_path
|
|
244
|
+
|
|
245
|
+
word_initializer = self.get_initializer(gather_node.input[0])
|
|
246
|
+
if word_initializer is None:
|
|
247
|
+
logger.debug("failed to get word initializer")
|
|
248
|
+
return False
|
|
249
|
+
|
|
250
|
+
temp = numpy_helper.to_array(word_initializer)
|
|
251
|
+
if len(temp.shape) == 2:
|
|
252
|
+
logger.info(f"Found word embedding. name:{word_initializer.name}, shape:{temp.shape}")
|
|
253
|
+
word_embedding = word_initializer.name
|
|
254
|
+
else:
|
|
255
|
+
logger.info(f"Failed to find word embedding. name:{word_initializer.name}, shape:{temp.shape}")
|
|
256
|
+
return False
|
|
257
|
+
|
|
258
|
+
pos_initializer = self.get_initializer(add_node.input[1])
|
|
259
|
+
if pos_initializer is not None:
|
|
260
|
+
temp = numpy_helper.to_array(pos_initializer)
|
|
261
|
+
if len(temp.shape) == 3 and temp.shape[0] == 1:
|
|
262
|
+
tensor = numpy_helper.from_array(temp.reshape((temp.shape[1], temp.shape[2])), "position_embedding")
|
|
263
|
+
self.add_initializer(tensor)
|
|
264
|
+
logger.info(f"Found position embedding. name:{pos_initializer.name}, shape:{temp.shape[1:]}")
|
|
265
|
+
position_embedding = "position_embedding"
|
|
266
|
+
else:
|
|
267
|
+
logger.info(f"Failed to find position embedding. name:{pos_initializer.name}, shape:{temp.shape}")
|
|
268
|
+
return False
|
|
269
|
+
else:
|
|
270
|
+
pos_embed_path = self.match_parent_path(add_node, ["Gather", "Slice"], [1, 1], output_name_to_node)
|
|
271
|
+
if pos_embed_path is None:
|
|
272
|
+
logger.debug("failed to match pos_embed_path")
|
|
273
|
+
return False
|
|
274
|
+
|
|
275
|
+
pos_gather, pos_slice = pos_embed_path
|
|
276
|
+
pos_initializer = self.get_initializer(pos_gather.input[0])
|
|
277
|
+
if pos_initializer is None:
|
|
278
|
+
logger.debug("failed to get pos initializer")
|
|
279
|
+
return False
|
|
280
|
+
|
|
281
|
+
temp = numpy_helper.to_array(pos_initializer)
|
|
282
|
+
if len(temp.shape) == 2:
|
|
283
|
+
logger.info(f"Found word embedding. name:{pos_initializer.name}, shape:{temp.shape}")
|
|
284
|
+
position_embedding = pos_initializer.name
|
|
285
|
+
else:
|
|
286
|
+
logger.info(f"Failed to find position embedding. name:{pos_initializer.name}, shape:{temp.shape}")
|
|
287
|
+
return False
|
|
288
|
+
|
|
289
|
+
gather = self.get_parent(skip_node, 1, output_name_to_node)
|
|
290
|
+
if gather is None or gather.op_type != "Gather":
|
|
291
|
+
logger.debug("failed to get gather")
|
|
292
|
+
return False
|
|
293
|
+
|
|
294
|
+
segment_initializer = self.get_initializer(gather.input[0])
|
|
295
|
+
if segment_initializer is None:
|
|
296
|
+
logger.debug("failed to get segment initializer")
|
|
297
|
+
return False
|
|
298
|
+
|
|
299
|
+
temp = numpy_helper.to_array(segment_initializer)
|
|
300
|
+
if len(temp.shape) == 2:
|
|
301
|
+
logger.info(f"Found segment embedding. name:{segment_initializer.name}, shape:{temp.shape}")
|
|
302
|
+
segment_embedding = segment_initializer.name
|
|
303
|
+
else:
|
|
304
|
+
logger.info(f"Failed to find segment embedding. name:{segment_initializer.name}, shape:{temp.shape}")
|
|
305
|
+
return False
|
|
306
|
+
|
|
307
|
+
logger.info("Create Embedding node")
|
|
308
|
+
self.create_embedding_subgraph(node, word_embedding, segment_embedding, position_embedding)
|
|
309
|
+
return True
|
|
310
|
+
|
|
311
|
+
def process_embedding(self):
|
|
312
|
+
"""
|
|
313
|
+
Automatically detect word, segment and position embeddings.
|
|
314
|
+
"""
|
|
315
|
+
logger.info("start processing embedding layer...")
|
|
316
|
+
output_name_to_node = self.output_name_to_node()
|
|
317
|
+
for node in self.nodes():
|
|
318
|
+
if node.op_type == "LayerNormalization":
|
|
319
|
+
if self.fuse_embedding(node, output_name_to_node):
|
|
320
|
+
return
|
|
321
|
+
break
|
|
322
|
+
|
|
323
|
+
def fuse_mask(self):
|
|
324
|
+
nodes_to_remove = []
|
|
325
|
+
for node in self.nodes():
|
|
326
|
+
if node.op_type == "Mul" and self.has_constant_input(node, -10000):
|
|
327
|
+
mask_path = self.match_parent_path(node, ["Sub", "Cast", "Slice", "Unsqueeze"], [0, 1, 0, 0])
|
|
328
|
+
if mask_path is None:
|
|
329
|
+
continue
|
|
330
|
+
sub_node, cast_node, slice_node, unsqueeze_node = mask_path
|
|
331
|
+
|
|
332
|
+
mask_input_name = self.attention_mask.get_first_mask()
|
|
333
|
+
if unsqueeze_node.input[0] != mask_input_name:
|
|
334
|
+
print(f"Cast input {unsqueeze_node.input[0]} is not mask input {mask_input_name}")
|
|
335
|
+
continue
|
|
336
|
+
|
|
337
|
+
unsqueeze_added_1 = onnx.helper.make_node(
|
|
338
|
+
"Unsqueeze",
|
|
339
|
+
inputs=[mask_input_name],
|
|
340
|
+
outputs=["mask_fuse_unsqueeze1_output"],
|
|
341
|
+
name="Mask_UnSqueeze_1",
|
|
342
|
+
axes=[1],
|
|
343
|
+
)
|
|
344
|
+
|
|
345
|
+
unsqueeze_added_2 = onnx.helper.make_node(
|
|
346
|
+
"Unsqueeze",
|
|
347
|
+
inputs=["mask_fuse_unsqueeze1_output"],
|
|
348
|
+
outputs=["mask_fuse_unsqueeze2_output"],
|
|
349
|
+
name="Mask_UnSqueeze_2",
|
|
350
|
+
axes=[2],
|
|
351
|
+
)
|
|
352
|
+
|
|
353
|
+
# self.replace_node_input(cast_node, cast_node.input[0], 'mask_fuse_unsqueeze2_output')
|
|
354
|
+
cast_node_2 = onnx.helper.make_node(
|
|
355
|
+
"Cast",
|
|
356
|
+
inputs=["mask_fuse_unsqueeze2_output"],
|
|
357
|
+
outputs=["mask_fuse_cast_output"],
|
|
358
|
+
)
|
|
359
|
+
cast_node_2.attribute.extend([onnx.helper.make_attribute("to", 1)])
|
|
360
|
+
self.replace_node_input(sub_node, sub_node.input[1], "mask_fuse_cast_output")
|
|
361
|
+
|
|
362
|
+
nodes_to_remove.extend([slice_node, unsqueeze_node, cast_node])
|
|
363
|
+
self.add_node(unsqueeze_added_1)
|
|
364
|
+
self.add_node(unsqueeze_added_2)
|
|
365
|
+
self.add_node(cast_node_2)
|
|
366
|
+
|
|
367
|
+
self.remove_nodes(nodes_to_remove)
|
|
368
|
+
|
|
369
|
+
# Prune graph is done after removing nodes to remove island nodes.
|
|
370
|
+
if len(nodes_to_remove) > 0:
|
|
371
|
+
self.prune_graph()
|
|
372
|
+
|
|
373
|
+
logger.info("Fused mask" if len(nodes_to_remove) > 0 else "Failed to fuse mask")
|
|
374
|
+
|
|
375
|
+
def remove_extra_reshape(self):
|
|
376
|
+
skiplayernorm_nodes = self.get_nodes_by_op_type("SkipLayerNormalization")
|
|
377
|
+
reshape_removed = 0
|
|
378
|
+
for skiplayernorm_node in skiplayernorm_nodes:
|
|
379
|
+
path = self.match_parent_path(
|
|
380
|
+
skiplayernorm_node,
|
|
381
|
+
[
|
|
382
|
+
"Add",
|
|
383
|
+
"Reshape",
|
|
384
|
+
"MatMul",
|
|
385
|
+
"Reshape",
|
|
386
|
+
"Gelu",
|
|
387
|
+
"Add",
|
|
388
|
+
"Reshape",
|
|
389
|
+
"MatMul",
|
|
390
|
+
"SkipLayerNormalization",
|
|
391
|
+
],
|
|
392
|
+
[0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
393
|
+
)
|
|
394
|
+
if path is None:
|
|
395
|
+
continue
|
|
396
|
+
|
|
397
|
+
(
|
|
398
|
+
add_1,
|
|
399
|
+
reshape_1,
|
|
400
|
+
matmul_1,
|
|
401
|
+
reshape_2,
|
|
402
|
+
gelu,
|
|
403
|
+
add_2,
|
|
404
|
+
reshape_3,
|
|
405
|
+
matmul_2,
|
|
406
|
+
skiplayernorm,
|
|
407
|
+
) = path
|
|
408
|
+
add_2.input[0] = matmul_2.output[0]
|
|
409
|
+
self.remove_node(reshape_3)
|
|
410
|
+
matmul_1.input[0] = gelu.output[0]
|
|
411
|
+
self.remove_node(reshape_2)
|
|
412
|
+
add_1.input[0] = matmul_1.output[0]
|
|
413
|
+
self.remove_node(reshape_1)
|
|
414
|
+
reshape_removed += 3
|
|
415
|
+
|
|
416
|
+
return reshape_removed
|
|
417
|
+
|
|
418
|
+
def remove_extra_reshape_2(self):
|
|
419
|
+
skiplayernorm_nodes = self.get_nodes_by_op_type("SkipLayerNormalization")
|
|
420
|
+
reshape_removed = 0
|
|
421
|
+
for skiplayernorm_node in skiplayernorm_nodes:
|
|
422
|
+
path = self.match_parent_path(
|
|
423
|
+
skiplayernorm_node,
|
|
424
|
+
[
|
|
425
|
+
"Add",
|
|
426
|
+
"Reshape",
|
|
427
|
+
"MatMul",
|
|
428
|
+
"Reshape",
|
|
429
|
+
"Gelu",
|
|
430
|
+
"Add",
|
|
431
|
+
"Reshape",
|
|
432
|
+
"MatMul",
|
|
433
|
+
"Reshape",
|
|
434
|
+
"SkipLayerNormalization",
|
|
435
|
+
],
|
|
436
|
+
[None, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
437
|
+
)
|
|
438
|
+
if path is None:
|
|
439
|
+
continue
|
|
440
|
+
|
|
441
|
+
(
|
|
442
|
+
add_1,
|
|
443
|
+
reshape_1,
|
|
444
|
+
matmul_1,
|
|
445
|
+
reshape_2,
|
|
446
|
+
gelu,
|
|
447
|
+
add_2,
|
|
448
|
+
reshape_3,
|
|
449
|
+
matmul_2,
|
|
450
|
+
reshape_4,
|
|
451
|
+
skiplayernorm,
|
|
452
|
+
) = path
|
|
453
|
+
|
|
454
|
+
matmul_2.input[0] = skiplayernorm.output[0]
|
|
455
|
+
self.remove_node(reshape_4)
|
|
456
|
+
|
|
457
|
+
add_2.input[0] = matmul_2.output[0]
|
|
458
|
+
self.remove_node(reshape_3)
|
|
459
|
+
|
|
460
|
+
matmul_1.input[0] = gelu.output[0]
|
|
461
|
+
self.remove_node(reshape_2)
|
|
462
|
+
|
|
463
|
+
add_1.input[0] = matmul_1.output[0]
|
|
464
|
+
self.remove_node(reshape_1)
|
|
465
|
+
|
|
466
|
+
reshape_removed += 4
|
|
467
|
+
|
|
468
|
+
return reshape_removed
|
|
469
|
+
|
|
470
|
+
def postprocess(self):
|
|
471
|
+
reshape_removed = self.remove_extra_reshape() + self.remove_extra_reshape_2()
|
|
472
|
+
logger.info(f"Remove {reshape_removed} Reshape nodes.")
|
|
473
|
+
|
|
474
|
+
self.prune_graph()
|