onnxruntime-directml 1.20.0__cp313-cp313-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 +6508 -0
- onnxruntime/__init__.py +78 -0
- onnxruntime/backend/__init__.py +6 -0
- onnxruntime/backend/backend.py +174 -0
- onnxruntime/backend/backend_rep.py +53 -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/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 +1108 -0
- onnxruntime/capi/onnxruntime_providers_shared.dll +0 -0
- onnxruntime/capi/onnxruntime_pybind11_state.pyd +0 -0
- onnxruntime/capi/onnxruntime_validation.py +150 -0
- onnxruntime/capi/version_info.py +2 -0
- onnxruntime/datasets/__init__.py +17 -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 +16 -0
- onnxruntime/quantization/base_quantizer.py +532 -0
- onnxruntime/quantization/calibrate.py +1245 -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/mixed_precision_overrides_utils.py +413 -0
- onnxruntime/quantization/execution_providers/qnn/preprocess.py +307 -0
- onnxruntime/quantization/execution_providers/qnn/quant_config.py +387 -0
- onnxruntime/quantization/fusions/__init__.py +3 -0
- onnxruntime/quantization/fusions/fusion.py +311 -0
- onnxruntime/quantization/fusions/fusion_gelu.py +272 -0
- onnxruntime/quantization/fusions/fusion_layernorm.py +135 -0
- onnxruntime/quantization/matmul_4bits_quantizer.py +1480 -0
- onnxruntime/quantization/matmul_bnb4_quantizer.py +240 -0
- onnxruntime/quantization/onnx_model.py +580 -0
- onnxruntime/quantization/onnx_quantizer.py +1008 -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 +258 -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 +166 -0
- onnxruntime/quantization/operators/lstm.py +117 -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 +100 -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 +1187 -0
- onnxruntime/quantization/quant_utils.py +891 -0
- onnxruntime/quantization/quantize.py +748 -0
- onnxruntime/quantization/registry.py +106 -0
- onnxruntime/quantization/shape_inference.py +187 -0
- onnxruntime/quantization/tensor_quant_overrides.py +516 -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 +377 -0
- onnxruntime/tools/file_utils.py +46 -0
- onnxruntime/tools/logger.py +11 -0
- onnxruntime/tools/make_dynamic_shape_fixed.py +72 -0
- onnxruntime/tools/mobile_helpers/__init__.py +0 -0
- onnxruntime/tools/mobile_helpers/coreml_supported_mlprogram_ops.md +33 -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 +739 -0
- onnxruntime/tools/offline_tuning.py +169 -0
- onnxruntime/tools/onnx_model_utils.py +413 -0
- onnxruntime/tools/onnx_randomizer.py +85 -0
- onnxruntime/tools/onnxruntime_test.py +164 -0
- onnxruntime/tools/optimize_onnx_model.py +55 -0
- onnxruntime/tools/ort_format_model/__init__.py +25 -0
- onnxruntime/tools/ort_format_model/operator_type_usage_processors.py +663 -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 +84 -0
- onnxruntime/tools/ort_format_model/utils.py +62 -0
- onnxruntime/tools/pytorch_export_contrib_ops.py +108 -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/reduced_build_config_parser.py +202 -0
- onnxruntime/tools/symbolic_shape_infer.py +3016 -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 +944 -0
- onnxruntime/transformers/benchmark_helper.py +646 -0
- onnxruntime/transformers/bert_perf_test.py +634 -0
- onnxruntime/transformers/bert_test_data.py +642 -0
- onnxruntime/transformers/compare_bert_results.py +246 -0
- onnxruntime/transformers/constants.py +47 -0
- onnxruntime/transformers/convert_generation.py +3124 -0
- onnxruntime/transformers/convert_tf_models_to_pytorch.py +205 -0
- onnxruntime/transformers/convert_to_packing_mode.py +387 -0
- onnxruntime/transformers/dynamo_onnx_helper.py +104 -0
- onnxruntime/transformers/float16.py +501 -0
- onnxruntime/transformers/fusion_attention.py +1235 -0
- onnxruntime/transformers/fusion_attention_clip.py +257 -0
- onnxruntime/transformers/fusion_attention_sam2.py +534 -0
- onnxruntime/transformers/fusion_attention_unet.py +1304 -0
- onnxruntime/transformers/fusion_attention_vae.py +301 -0
- onnxruntime/transformers/fusion_bart_attention.py +640 -0
- onnxruntime/transformers/fusion_base.py +137 -0
- onnxruntime/transformers/fusion_bias_add.py +58 -0
- onnxruntime/transformers/fusion_biasgelu.py +66 -0
- onnxruntime/transformers/fusion_biassplitgelu.py +111 -0
- onnxruntime/transformers/fusion_conformer_attention.py +143 -0
- onnxruntime/transformers/fusion_embedlayer.py +811 -0
- onnxruntime/transformers/fusion_fastgelu.py +360 -0
- onnxruntime/transformers/fusion_gelu.py +259 -0
- onnxruntime/transformers/fusion_gelu_approximation.py +25 -0
- onnxruntime/transformers/fusion_gemmfastgelu.py +122 -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 +179 -0
- onnxruntime/transformers/fusion_layernorm.py +465 -0
- onnxruntime/transformers/fusion_nhwc_conv.py +100 -0
- onnxruntime/transformers/fusion_options.py +340 -0
- onnxruntime/transformers/fusion_qordered_attention.py +421 -0
- onnxruntime/transformers/fusion_qordered_gelu.py +119 -0
- onnxruntime/transformers/fusion_qordered_layernorm.py +123 -0
- onnxruntime/transformers/fusion_qordered_matmul.py +217 -0
- onnxruntime/transformers/fusion_quickgelu.py +74 -0
- onnxruntime/transformers/fusion_reshape.py +173 -0
- onnxruntime/transformers/fusion_rotary_attention.py +1592 -0
- onnxruntime/transformers/fusion_shape.py +110 -0
- onnxruntime/transformers/fusion_simplified_layernorm.py +159 -0
- onnxruntime/transformers/fusion_skip_group_norm.py +255 -0
- onnxruntime/transformers/fusion_skiplayernorm.py +209 -0
- onnxruntime/transformers/fusion_transpose.py +168 -0
- onnxruntime/transformers/fusion_utils.py +307 -0
- onnxruntime/transformers/huggingface_models.py +167 -0
- onnxruntime/transformers/import_utils.py +20 -0
- onnxruntime/transformers/io_binding_helper.py +442 -0
- onnxruntime/transformers/large_model_exporter.py +395 -0
- onnxruntime/transformers/machine_info.py +221 -0
- onnxruntime/transformers/metrics.py +164 -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 +561 -0
- onnxruntime/transformers/models/gpt2/gpt2_helper.py +1032 -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 +703 -0
- onnxruntime/transformers/models/llama/benchmark_all.py +488 -0
- onnxruntime/transformers/models/llama/benchmark_e2e.py +606 -0
- onnxruntime/transformers/models/llama/convert_to_onnx.py +1027 -0
- onnxruntime/transformers/models/llama/dist_settings.py +57 -0
- onnxruntime/transformers/models/llama/llama_inputs.py +503 -0
- onnxruntime/transformers/models/llama/llama_parity.py +309 -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 +77 -0
- onnxruntime/transformers/models/phi2/__init__.py +12 -0
- onnxruntime/transformers/models/phi2/convert_to_onnx.py +576 -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 +625 -0
- onnxruntime/transformers/models/sam2/convert_to_onnx.py +260 -0
- onnxruntime/transformers/models/sam2/image_decoder.py +273 -0
- onnxruntime/transformers/models/sam2/image_encoder.py +186 -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 +322 -0
- onnxruntime/transformers/models/sam2/sam2_image_onnx_predictor.py +280 -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 +1429 -0
- onnxruntime/transformers/models/stable_diffusion/benchmark_controlnet.py +426 -0
- onnxruntime/transformers/models/stable_diffusion/demo_txt2img.py +102 -0
- onnxruntime/transformers/models/stable_diffusion/demo_txt2img_xl.py +268 -0
- onnxruntime/transformers/models/stable_diffusion/demo_utils.py +778 -0
- onnxruntime/transformers/models/stable_diffusion/diffusion_models.py +1319 -0
- onnxruntime/transformers/models/stable_diffusion/diffusion_schedulers.py +1181 -0
- onnxruntime/transformers/models/stable_diffusion/engine_builder.py +296 -0
- onnxruntime/transformers/models/stable_diffusion/engine_builder_ort_cuda.py +388 -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 +350 -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 +278 -0
- onnxruntime/transformers/models/t5/past_helper.py +150 -0
- onnxruntime/transformers/models/t5/t5_decoder.py +438 -0
- onnxruntime/transformers/models/t5/t5_encoder.py +171 -0
- onnxruntime/transformers/models/t5/t5_encoder_decoder_init.py +299 -0
- onnxruntime/transformers/models/t5/t5_helper.py +272 -0
- onnxruntime/transformers/models/whisper/__init__.py +12 -0
- onnxruntime/transformers/models/whisper/benchmark.py +610 -0
- onnxruntime/transformers/models/whisper/benchmark_all.py +528 -0
- onnxruntime/transformers/models/whisper/convert_to_onnx.py +536 -0
- onnxruntime/transformers/models/whisper/whisper_chain.py +329 -0
- onnxruntime/transformers/models/whisper/whisper_decoder.py +402 -0
- onnxruntime/transformers/models/whisper/whisper_encoder.py +164 -0
- onnxruntime/transformers/models/whisper/whisper_encoder_decoder_init.py +306 -0
- onnxruntime/transformers/models/whisper/whisper_helper.py +524 -0
- onnxruntime/transformers/models/whisper/whisper_openai_helper.py +84 -0
- onnxruntime/transformers/onnx_exporter.py +717 -0
- onnxruntime/transformers/onnx_model.py +1569 -0
- onnxruntime/transformers/onnx_model_bart.py +142 -0
- onnxruntime/transformers/onnx_model_bert.py +481 -0
- onnxruntime/transformers/onnx_model_bert_keras.py +475 -0
- onnxruntime/transformers/onnx_model_bert_tf.py +589 -0
- onnxruntime/transformers/onnx_model_clip.py +40 -0
- onnxruntime/transformers/onnx_model_conformer.py +33 -0
- onnxruntime/transformers/onnx_model_gpt2.py +101 -0
- onnxruntime/transformers/onnx_model_phi.py +930 -0
- onnxruntime/transformers/onnx_model_sam2.py +138 -0
- onnxruntime/transformers/onnx_model_t5.py +791 -0
- onnxruntime/transformers/onnx_model_tnlr.py +227 -0
- onnxruntime/transformers/onnx_model_unet.py +259 -0
- onnxruntime/transformers/onnx_model_vae.py +43 -0
- onnxruntime/transformers/onnx_utils.py +55 -0
- onnxruntime/transformers/optimizer.py +612 -0
- onnxruntime/transformers/profiler.py +725 -0
- onnxruntime/transformers/quantize_helper.py +76 -0
- onnxruntime/transformers/shape_infer_helper.py +122 -0
- onnxruntime/transformers/shape_optimizer.py +401 -0
- onnxruntime/transformers/torch_onnx_export_helper.py +74 -0
- onnxruntime_directml-1.20.0.dist-info/METADATA +187 -0
- onnxruntime_directml-1.20.0.dist-info/RECORD +305 -0
- onnxruntime_directml-1.20.0.dist-info/WHEEL +5 -0
- onnxruntime_directml-1.20.0.dist-info/entry_points.txt +2 -0
- onnxruntime_directml-1.20.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
# -------------------------------------------------------------------------
|
|
2
|
+
# Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
# Licensed under the MIT License. See License.txt in the project root for
|
|
4
|
+
# license information.
|
|
5
|
+
# --------------------------------------------------------------------------
|
|
6
|
+
|
|
7
|
+
import argparse
|
|
8
|
+
import logging
|
|
9
|
+
import os
|
|
10
|
+
from typing import List, Tuple
|
|
11
|
+
|
|
12
|
+
import numpy as np
|
|
13
|
+
import numpy.typing as npt
|
|
14
|
+
import onnx
|
|
15
|
+
from onnx.onnx_pb import GraphProto, ModelProto, NodeProto, TensorProto
|
|
16
|
+
|
|
17
|
+
from onnxruntime.capi._pybind_state import quantize_matmul_bnb4
|
|
18
|
+
|
|
19
|
+
from .onnx_model import ONNXModel
|
|
20
|
+
from .quant_utils import attribute_to_kwarg
|
|
21
|
+
|
|
22
|
+
logger = logging.getLogger(__name__)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class MatMulBnb4Quantizer:
|
|
26
|
+
"""Perform 4b quantization of constant MatMul weights using FP4 or NF4 data type"""
|
|
27
|
+
|
|
28
|
+
##################
|
|
29
|
+
# quantization types, must be consistent with native code type
|
|
30
|
+
# Bnb_DataType_t defined in blockwise_quant_block_bnb4.h
|
|
31
|
+
|
|
32
|
+
# 4b floating point with bias of 3
|
|
33
|
+
FP4 = 0
|
|
34
|
+
|
|
35
|
+
# 4b NormalFloat
|
|
36
|
+
NF4 = 1
|
|
37
|
+
|
|
38
|
+
def __init__(self, model: ModelProto, quant_type: int, block_size: int, nodes_to_exclude=None):
|
|
39
|
+
nodes_to_exclude = nodes_to_exclude or []
|
|
40
|
+
assert quant_type in [MatMulBnb4Quantizer.FP4, MatMulBnb4Quantizer.NF4]
|
|
41
|
+
self.model = ONNXModel(model)
|
|
42
|
+
self.quant_type = quant_type
|
|
43
|
+
self.block_size = block_size
|
|
44
|
+
self.nodes_to_exclude = set(nodes_to_exclude)
|
|
45
|
+
|
|
46
|
+
@staticmethod
|
|
47
|
+
def __get_initializer(name, graph_path: List[GraphProto]) -> Tuple[TensorProto, GraphProto]:
|
|
48
|
+
for gid in range(len(graph_path) - 1, -1, -1):
|
|
49
|
+
graph = graph_path[gid]
|
|
50
|
+
for tensor in graph.initializer:
|
|
51
|
+
if tensor.name == name:
|
|
52
|
+
return tensor, graph
|
|
53
|
+
return None, None
|
|
54
|
+
|
|
55
|
+
def bnb4_block_quant(self, fpweight: npt.ArrayLike) -> np.ndarray:
|
|
56
|
+
"""4b quantize fp32/fp16 weight"""
|
|
57
|
+
|
|
58
|
+
if len(fpweight.shape) != 2:
|
|
59
|
+
raise ValueError("Current bnb4 block quantization only supports 2D tensors!")
|
|
60
|
+
# need to copy since the transposed weight still has the original memory layout
|
|
61
|
+
# Linear4bit quantizes its weight data which is the transposed weight
|
|
62
|
+
fpweight_t = fpweight.transpose().copy()
|
|
63
|
+
|
|
64
|
+
rows, cols = fpweight.shape
|
|
65
|
+
numel = rows * cols
|
|
66
|
+
block_size = self.block_size
|
|
67
|
+
num_blocks = (numel + block_size - 1) // block_size
|
|
68
|
+
quantized_numel = (numel + 1) // 2
|
|
69
|
+
|
|
70
|
+
packed = np.zeros(quantized_numel, dtype="uint8")
|
|
71
|
+
absmax = np.zeros(num_blocks, dtype=fpweight.dtype)
|
|
72
|
+
# block wise quantization, fpweight_t is flattened and divided into blocks
|
|
73
|
+
quantize_matmul_bnb4(packed, fpweight_t, absmax, block_size, self.quant_type, cols, rows)
|
|
74
|
+
|
|
75
|
+
return (packed, absmax)
|
|
76
|
+
|
|
77
|
+
def _bnb4_matmul_node_weight(self, node: NodeProto, graph_stack: List[GraphProto]) -> NodeProto:
|
|
78
|
+
"""If the node is MatMul with fp32 const weight, quantize the weight with int4, and return the new node"""
|
|
79
|
+
|
|
80
|
+
if node.op_type != "MatMul":
|
|
81
|
+
return node # only care about MatMul for now
|
|
82
|
+
|
|
83
|
+
logger.debug(f"start to quantize {node.name} ...")
|
|
84
|
+
if node.name in self.nodes_to_exclude:
|
|
85
|
+
logger.debug(f"exclude to quantize {node.name} as specified by nodes_to_exclude...")
|
|
86
|
+
return node
|
|
87
|
+
|
|
88
|
+
inputB = node.input[1] # noqa: N806
|
|
89
|
+
B, Bs_graph = MatMulBnb4Quantizer.__get_initializer(inputB, graph_stack) # noqa: N806
|
|
90
|
+
if B is None:
|
|
91
|
+
logger.debug("MatMul doesn't have const weight. Skip to quantize")
|
|
92
|
+
return node # only care about constant weight
|
|
93
|
+
|
|
94
|
+
B_array = onnx.numpy_helper.to_array(B) # noqa: N806
|
|
95
|
+
if len(B_array.shape) != 2:
|
|
96
|
+
logger.debug("MatMul weight is not 2D. Skip to quantize")
|
|
97
|
+
return node # can only process 2-D matrix
|
|
98
|
+
|
|
99
|
+
packed, absmax = self.bnb4_block_quant(B_array)
|
|
100
|
+
B_quant = onnx.numpy_helper.from_array(packed) # noqa: N806
|
|
101
|
+
B_quant.name = B.name + "_Bnb4"
|
|
102
|
+
for input in Bs_graph.input:
|
|
103
|
+
if input.name == inputB:
|
|
104
|
+
Bs_graph.input.remove(input)
|
|
105
|
+
break
|
|
106
|
+
|
|
107
|
+
absmax_tensor = onnx.numpy_helper.from_array(absmax)
|
|
108
|
+
absmax_tensor.name = B.name + "_absmax"
|
|
109
|
+
|
|
110
|
+
Bs_graph.initializer.extend([B_quant, absmax_tensor])
|
|
111
|
+
|
|
112
|
+
kwargs = {}
|
|
113
|
+
rows, cols = B_array.shape
|
|
114
|
+
kwargs["K"] = rows
|
|
115
|
+
kwargs["N"] = cols
|
|
116
|
+
kwargs["block_size"] = self.block_size
|
|
117
|
+
kwargs["quant_type"] = self.quant_type
|
|
118
|
+
|
|
119
|
+
matmul_bnb4_node = onnx.helper.make_node(
|
|
120
|
+
"MatMulBnb4",
|
|
121
|
+
inputs=[node.input[0], B_quant.name, absmax_tensor.name],
|
|
122
|
+
outputs=[node.output[0]],
|
|
123
|
+
name=node.name + "_Bnb4" if node.name else "",
|
|
124
|
+
domain="com.microsoft",
|
|
125
|
+
**kwargs,
|
|
126
|
+
)
|
|
127
|
+
|
|
128
|
+
logger.debug(f"complete quantization of {node.name} ...")
|
|
129
|
+
|
|
130
|
+
return matmul_bnb4_node
|
|
131
|
+
|
|
132
|
+
def _process_subgraph(self, graph_stack: List[GraphProto]):
|
|
133
|
+
new_nodes = []
|
|
134
|
+
graph = graph_stack[-1]
|
|
135
|
+
|
|
136
|
+
for node in graph.node:
|
|
137
|
+
graph_attrs = [
|
|
138
|
+
attr
|
|
139
|
+
for attr in node.attribute
|
|
140
|
+
if attr.type == onnx.AttributeProto.GRAPH or attr.type == onnx.AttributeProto.GRAPHS
|
|
141
|
+
]
|
|
142
|
+
if len(graph_attrs):
|
|
143
|
+
kwargs = {}
|
|
144
|
+
for attr in node.attribute:
|
|
145
|
+
if attr.type == onnx.AttributeProto.GRAPH:
|
|
146
|
+
# recursive call to take care of sub-graph
|
|
147
|
+
graph_stack.append(attr.g)
|
|
148
|
+
kv = {attr.name: self._process_subgraph(graph_stack)}
|
|
149
|
+
elif attr.type == onnx.AttributeProto.GRAPHS:
|
|
150
|
+
value = []
|
|
151
|
+
for subgraph in attr.graphs:
|
|
152
|
+
# recursive call to take care of sub-graph
|
|
153
|
+
graph_stack.append(subgraph)
|
|
154
|
+
value.extend([self._process_subgraph(graph_stack)])
|
|
155
|
+
kv = {attr.name: value}
|
|
156
|
+
else:
|
|
157
|
+
kv = attribute_to_kwarg(attr)
|
|
158
|
+
kwargs.update(kv)
|
|
159
|
+
node = onnx.helper.make_node( # noqa: PLW2901
|
|
160
|
+
node.op_type, node.input, node.output, name=node.name, **kwargs
|
|
161
|
+
)
|
|
162
|
+
|
|
163
|
+
new_nodes.append(self._bnb4_matmul_node_weight(node, graph_stack))
|
|
164
|
+
|
|
165
|
+
graph.ClearField("node")
|
|
166
|
+
graph.node.extend(new_nodes)
|
|
167
|
+
graph_stack.pop()
|
|
168
|
+
return graph
|
|
169
|
+
|
|
170
|
+
def process(self):
|
|
171
|
+
# use a stack to keep track of sub-graphs
|
|
172
|
+
graph_stack = [self.model.graph()]
|
|
173
|
+
opset_import = self.model.opset_import()
|
|
174
|
+
|
|
175
|
+
has_ms_domain = False
|
|
176
|
+
for opset in opset_import:
|
|
177
|
+
if opset.domain == "com.microsoft":
|
|
178
|
+
has_ms_domain = True
|
|
179
|
+
if not has_ms_domain:
|
|
180
|
+
opset_import.extend([onnx.helper.make_opsetid("com.microsoft", 1)])
|
|
181
|
+
|
|
182
|
+
self._process_subgraph(graph_stack)
|
|
183
|
+
self.model.clean_initializers()
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
def parse_args():
|
|
187
|
+
parser = argparse.ArgumentParser(
|
|
188
|
+
description="""Blockwise FP4/NF4 quantization for MatMul 2D weight matrices.
|
|
189
|
+
|
|
190
|
+
A weight matrix is partitioned into blocks, where each block is a contiguous
|
|
191
|
+
subset inside the flattened transposed weight matrix. Each block is quantized
|
|
192
|
+
into a set of 4b integers with an absolute value scaling factor.
|
|
193
|
+
"""
|
|
194
|
+
)
|
|
195
|
+
|
|
196
|
+
parser.add_argument("--input_model", required=True, help="Path to the input model file")
|
|
197
|
+
parser.add_argument("--output_model", required=True, help="Path to the output model file")
|
|
198
|
+
parser.add_argument(
|
|
199
|
+
"--quant_type",
|
|
200
|
+
required=False,
|
|
201
|
+
default=1,
|
|
202
|
+
choices=[MatMulBnb4Quantizer.FP4, MatMulBnb4Quantizer.NF4],
|
|
203
|
+
help="Quantization data type. 0: FP4, 1: NF4",
|
|
204
|
+
)
|
|
205
|
+
parser.add_argument(
|
|
206
|
+
"--block_size",
|
|
207
|
+
required=False,
|
|
208
|
+
default=64,
|
|
209
|
+
help="Block size for blockwise quantization. Note: bnb.nn.Linear4bit only uses block_size=64",
|
|
210
|
+
)
|
|
211
|
+
parser.add_argument("-v", "--verbose", required=False, action="store_true")
|
|
212
|
+
parser.set_defaults(verbose=False)
|
|
213
|
+
parser.add_argument(
|
|
214
|
+
"--nodes_to_exclude",
|
|
215
|
+
nargs="+",
|
|
216
|
+
type=str,
|
|
217
|
+
required=False,
|
|
218
|
+
default=[],
|
|
219
|
+
help="Specify the nodes to be excluded from quantization with node names",
|
|
220
|
+
)
|
|
221
|
+
|
|
222
|
+
return parser.parse_args()
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
if __name__ == "__main__":
|
|
226
|
+
args = parse_args()
|
|
227
|
+
if args.verbose:
|
|
228
|
+
logger.setLevel(logging.DEBUG)
|
|
229
|
+
|
|
230
|
+
input_model_path = args.input_model
|
|
231
|
+
output_model_path = args.output_model
|
|
232
|
+
|
|
233
|
+
if os.path.exists(output_model_path):
|
|
234
|
+
logger.error(f"file {output_model_path} already exists")
|
|
235
|
+
raise Exception(f"file {output_model_path} already exists")
|
|
236
|
+
|
|
237
|
+
model = onnx.load(input_model_path)
|
|
238
|
+
quant = MatMulBnb4Quantizer(model, args.quant_type, args.block_size, nodes_to_exclude=args.nodes_to_exclude)
|
|
239
|
+
quant.process()
|
|
240
|
+
quant.model.save_model_to_file(output_model_path, True)
|