mct-nightly 1.7.1.31122022.post351__py3-none-any.whl → 1.8.0.1042023.post423__py3-none-any.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.
- {mct_nightly-1.7.1.31122022.post351.dist-info → mct_nightly-1.8.0.1042023.post423.dist-info}/METADATA +16 -16
- {mct_nightly-1.7.1.31122022.post351.dist-info → mct_nightly-1.8.0.1042023.post423.dist-info}/RECORD +193 -150
- {mct_nightly-1.7.1.31122022.post351.dist-info → mct_nightly-1.8.0.1042023.post423.dist-info}/WHEEL +1 -1
- model_compression_toolkit/__init__.py +13 -14
- model_compression_toolkit/core/common/back2framework/base_model_builder.py +1 -1
- model_compression_toolkit/core/common/collectors/base_collector.py +7 -4
- model_compression_toolkit/core/common/collectors/statistics_collector.py +2 -2
- model_compression_toolkit/core/common/constants.py +9 -4
- model_compression_toolkit/core/common/framework_implementation.py +32 -30
- model_compression_toolkit/core/common/graph/base_graph.py +8 -6
- model_compression_toolkit/core/common/logger.py +10 -2
- model_compression_toolkit/core/common/matchers/base_matcher.py +3 -3
- model_compression_toolkit/core/common/mixed_precision/mixed_precision_quantization_config.py +2 -1
- model_compression_toolkit/core/common/mixed_precision/mixed_precision_search_facade.py +2 -2
- model_compression_toolkit/core/common/mixed_precision/mixed_precision_search_manager.py +2 -2
- model_compression_toolkit/core/common/mixed_precision/search_methods/linear_programming.py +6 -1
- model_compression_toolkit/core/common/model_validation.py +2 -1
- model_compression_toolkit/core/common/quantization/node_quantization_config.py +3 -1
- model_compression_toolkit/core/common/quantization/quantization_params_fn_selection.py +7 -4
- model_compression_toolkit/core/common/quantization/quantization_params_generation/lut_kmeans_params.py +4 -2
- model_compression_toolkit/core/common/quantization/quantization_params_generation/qparams_activations_computation.py +14 -17
- model_compression_toolkit/core/common/quantization/quantizers/quantizers_helpers.py +9 -2
- model_compression_toolkit/core/common/quantization/quantizers/uniform_quantizers.py +5 -4
- model_compression_toolkit/core/common/quantization/set_node_quantization_config.py +3 -3
- model_compression_toolkit/core/common/substitutions/batchnorm_reconstruction.py +7 -0
- model_compression_toolkit/core/common/substitutions/batchnorm_refusing.py +13 -8
- model_compression_toolkit/core/common/substitutions/shift_negative_activation.py +17 -12
- model_compression_toolkit/core/common/substitutions/weights_activation_split.py +1 -1
- model_compression_toolkit/core/common/target_platform/current_tp_model.py +3 -1
- model_compression_toolkit/core/common/target_platform/targetplatform2framework/attribute_filter.py +17 -4
- model_compression_toolkit/core/common/target_platform/targetplatform2framework/operations_to_layers.py +2 -4
- model_compression_toolkit/core/common/target_platform/targetplatform2framework/target_platform_capabilities.py +3 -5
- model_compression_toolkit/core/keras/back2framework/instance_builder.py +12 -21
- model_compression_toolkit/core/keras/back2framework/keras_model_builder.py +40 -14
- model_compression_toolkit/core/keras/back2framework/model_gradients.py +51 -27
- model_compression_toolkit/core/keras/constants.py +1 -0
- model_compression_toolkit/core/keras/graph_substitutions/substitutions/multi_head_attention_decomposition.py +2 -1
- model_compression_toolkit/core/keras/kpi_data_facade.py +2 -2
- model_compression_toolkit/core/keras/quantization_facade.py +3 -3
- model_compression_toolkit/core/keras/quantizer/fake_quant_builder.py +15 -9
- model_compression_toolkit/core/keras/quantizer/input_layer_quantize_transform.py +2 -1
- model_compression_toolkit/core/keras/quantizer/lut_fake_quant.py +1 -1
- model_compression_toolkit/core/keras/reader/common.py +3 -2
- model_compression_toolkit/core/pytorch/back2framework/instance_builder.py +14 -1
- model_compression_toolkit/core/pytorch/back2framework/model_gradients.py +88 -46
- model_compression_toolkit/core/pytorch/back2framework/pytorch_model_builder.py +27 -12
- model_compression_toolkit/core/pytorch/back2framework/quantization_wrapper/wrapper_quantize_config.py +2 -3
- model_compression_toolkit/core/pytorch/constants.py +5 -0
- model_compression_toolkit/core/pytorch/graph_substitutions/substitutions/multi_head_attention_decomposition.py +9 -14
- model_compression_toolkit/core/pytorch/graph_substitutions/substitutions/reshape_with_static_shapes.py +16 -2
- model_compression_toolkit/core/pytorch/kpi_data_facade.py +2 -2
- model_compression_toolkit/core/pytorch/quantization_facade.py +2 -2
- model_compression_toolkit/core/pytorch/quantizer/fake_quant_builder.py +7 -5
- model_compression_toolkit/core/pytorch/quantizer/lut_fake_quant.py +1 -1
- model_compression_toolkit/core/tpc_models/get_target_platform_capabilities.py +6 -2
- model_compression_toolkit/{exporter/model_wrapper/keras/quantize_configs → core/tpc_models/imx500_tpc}/__init__.py +1 -1
- model_compression_toolkit/core/tpc_models/imx500_tpc/latest/__init__.py +24 -0
- model_compression_toolkit/core/tpc_models/imx500_tpc/target_platform_capabilities.py +45 -0
- model_compression_toolkit/core/tpc_models/imx500_tpc/v1/__init__.py +16 -0
- model_compression_toolkit/core/tpc_models/imx500_tpc/v1/tp_model.py +156 -0
- model_compression_toolkit/core/tpc_models/imx500_tpc/v1/tpc_keras.py +101 -0
- model_compression_toolkit/core/tpc_models/imx500_tpc/v1/tpc_pytorch.py +95 -0
- model_compression_toolkit/exporter/__init__.py +5 -0
- model_compression_toolkit/exporter/model_exporter/__init__.py +0 -12
- model_compression_toolkit/exporter/model_exporter/fw_agonstic/exporter.py +1 -1
- model_compression_toolkit/exporter/model_exporter/keras/fakely_quant_keras_exporter.py +12 -39
- model_compression_toolkit/exporter/model_exporter/keras/keras_export_facade.py +39 -27
- model_compression_toolkit/exporter/model_exporter/pytorch/fakely_quant_onnx_pytorch_exporter.py +10 -2
- model_compression_toolkit/exporter/model_exporter/pytorch/fakely_quant_torchscript_pytorch_exporter.py +6 -2
- model_compression_toolkit/exporter/model_exporter/pytorch/pytorch_export_facade.py +48 -35
- model_compression_toolkit/exporter/model_exporter/tflite/fakely_quant_tflite_exporter.py +3 -2
- model_compression_toolkit/exporter/model_exporter/tflite/int8_tflite_exporter.py +180 -0
- model_compression_toolkit/exporter/model_exporter/tflite/tflite_export_facade.py +44 -26
- model_compression_toolkit/exporter/model_wrapper/__init__.py +4 -4
- model_compression_toolkit/exporter/model_wrapper/keras/builder/fully_quantized_model_builder.py +34 -137
- model_compression_toolkit/exporter/model_wrapper/keras/builder/node_to_quantizer.py +143 -0
- model_compression_toolkit/exporter/model_wrapper/keras/builder/node_to_quantizers.py +46 -0
- model_compression_toolkit/exporter/model_wrapper/keras/validate_layer.py +56 -22
- model_compression_toolkit/exporter/model_wrapper/pytorch/builder/fully_quantized_model_builder.py +29 -112
- model_compression_toolkit/exporter/model_wrapper/pytorch/builder/node_to_quantizer.py +83 -79
- model_compression_toolkit/exporter/model_wrapper/pytorch/builder/node_to_quantizers.py +47 -0
- model_compression_toolkit/exporter/model_wrapper/pytorch/validate_layer.py +44 -0
- model_compression_toolkit/gptq/__init__.py +6 -0
- model_compression_toolkit/gptq/common/gptq_config.py +57 -127
- model_compression_toolkit/gptq/common/gptq_constants.py +20 -6
- model_compression_toolkit/gptq/common/gptq_graph.py +22 -0
- model_compression_toolkit/gptq/common/gptq_training.py +32 -26
- model_compression_toolkit/gptq/keras/gptq_loss.py +1 -1
- model_compression_toolkit/gptq/keras/gptq_training.py +73 -39
- model_compression_toolkit/gptq/keras/graph_info.py +24 -43
- model_compression_toolkit/gptq/keras/quantization_facade.py +10 -18
- model_compression_toolkit/gptq/keras/quantizer/__init__.py +2 -1
- model_compression_toolkit/gptq/keras/quantizer/base_keras_gptq_quantizer.py +112 -0
- model_compression_toolkit/gptq/keras/quantizer/quant_utils.py +13 -14
- model_compression_toolkit/gptq/keras/quantizer/quantization_builder.py +78 -0
- model_compression_toolkit/gptq/keras/quantizer/regularization_factory.py +45 -0
- model_compression_toolkit/gptq/keras/{optimizers → quantizer/soft_rounding}/__init__.py +1 -1
- model_compression_toolkit/gptq/keras/quantizer/soft_rounding/soft_quantizer_reg.py +112 -0
- model_compression_toolkit/gptq/keras/quantizer/soft_rounding/symmetric_soft_quantizer.py +256 -0
- model_compression_toolkit/gptq/keras/quantizer/ste_rounding/symmetric_ste.py +68 -168
- model_compression_toolkit/gptq/pytorch/gptq_training.py +78 -39
- model_compression_toolkit/gptq/pytorch/graph_info.py +81 -0
- model_compression_toolkit/gptq/pytorch/quantization_facade.py +12 -18
- model_compression_toolkit/gptq/pytorch/quantizer/__init__.py +5 -1
- model_compression_toolkit/gptq/pytorch/quantizer/base_pytorch_gptq_quantizer.py +92 -0
- model_compression_toolkit/gptq/pytorch/quantizer/quant_utils.py +10 -119
- model_compression_toolkit/gptq/pytorch/quantizer/quantization_builder.py +75 -0
- model_compression_toolkit/gptq/pytorch/quantizer/regularization_factory.py +45 -0
- model_compression_toolkit/{exporter/model_wrapper/keras/quantizers → gptq/pytorch/quantizer/soft_rounding}/__init__.py +1 -1
- model_compression_toolkit/gptq/pytorch/quantizer/soft_rounding/soft_quantizer_reg.py +115 -0
- model_compression_toolkit/gptq/pytorch/quantizer/soft_rounding/symmetric_soft_quantizer.py +244 -0
- model_compression_toolkit/gptq/pytorch/quantizer/soft_rounding/uniform_soft_quantizer.py +196 -0
- model_compression_toolkit/gptq/pytorch/quantizer/ste_rounding/symmetric_ste.py +182 -0
- model_compression_toolkit/ptq/keras/quantization_facade.py +3 -3
- model_compression_toolkit/ptq/pytorch/quantization_facade.py +7 -6
- model_compression_toolkit/qat/common/qat_config.py +68 -0
- model_compression_toolkit/qat/keras/quantization_facade.py +55 -48
- model_compression_toolkit/qat/keras/quantizer/__init__.py +3 -0
- model_compression_toolkit/qat/keras/quantizer/base_keras_qat_quantizer.py +49 -0
- model_compression_toolkit/qat/keras/quantizer/quant_utils.py +48 -0
- model_compression_toolkit/qat/keras/quantizer/quantization_builder.py +77 -0
- model_compression_toolkit/qat/keras/quantizer/ste_rounding/symmetric_ste.py +283 -0
- model_compression_toolkit/qat/keras/quantizer/ste_rounding/uniform_ste.py +158 -46
- model_compression_toolkit/qat/pytorch/quantization_facade.py +190 -11
- model_compression_toolkit/qat/pytorch/quantizer/__init__.py +17 -0
- model_compression_toolkit/qat/pytorch/quantizer/base_pytorch_qat_quantizer.py +49 -0
- model_compression_toolkit/qat/pytorch/quantizer/quantization_builder.py +74 -0
- model_compression_toolkit/qat/pytorch/quantizer/quantizer_utils.py +136 -0
- model_compression_toolkit/qat/pytorch/quantizer/ste_rounding/symmetric_ste.py +204 -0
- model_compression_toolkit/qat/pytorch/quantizer/ste_rounding/uniform_ste.py +190 -0
- model_compression_toolkit/quantizers_infrastructure/__init__.py +23 -0
- model_compression_toolkit/{gptq/keras/quantizer/configs → quantizers_infrastructure/inferable_infrastructure}/__init__.py +1 -1
- model_compression_toolkit/{gptq/keras/quantizer/gumbel_rounding → quantizers_infrastructure/inferable_infrastructure/common}/__init__.py +1 -1
- model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/common/base_inferable_quantizer.py +87 -0
- model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/common/constants.py +41 -0
- model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/common/get_all_subclasses.py +31 -0
- model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/common/get_quantizers.py +53 -0
- model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/common/quant_utils.py +49 -0
- model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/keras/__init__.py +14 -0
- model_compression_toolkit/{qunatizers_infrastructure → quantizers_infrastructure/inferable_infrastructure}/keras/load_model.py +26 -8
- model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/keras/quantize_wrapper.py +345 -0
- model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/keras/quantizer_utils.py +85 -0
- model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/keras/quantizers/__init__.py +27 -0
- model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/keras/quantizers/activation_inferable_quantizers/__init__.py +14 -0
- model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/keras/quantizers/activation_inferable_quantizers/activation_lut_pot_inferable_quantizer.py +148 -0
- model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/keras/quantizers/activation_inferable_quantizers/activation_pot_inferable_quantizer.py +65 -0
- model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/keras/quantizers/activation_inferable_quantizers/activation_symmetric_inferable_quantizer.py +86 -0
- model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/keras/quantizers/activation_inferable_quantizers/activation_uniform_inferable_quantizer.py +111 -0
- model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/keras/quantizers/base_keras_inferable_quantizer.py +56 -0
- model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/keras/quantizers/constants.py +25 -0
- model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/keras/quantizers/weights_inferable_quantizers/__init__.py +14 -0
- model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/keras/quantizers/weights_inferable_quantizers/weights_lut_pot_inferable_quantizer.py +79 -0
- model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/keras/quantizers/weights_inferable_quantizers/weights_lut_symmetric_inferable_quantizer.py +179 -0
- model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/keras/quantizers/weights_inferable_quantizers/weights_pot_inferable_quantizer.py +67 -0
- model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/keras/quantizers/weights_inferable_quantizers/weights_symmetric_inferable_quantizer.py +87 -0
- model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/keras/quantizers/weights_inferable_quantizers/weights_uniform_inferable_quantizer.py +163 -0
- model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/keras/validation_functions.py +66 -0
- model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/pytorch/__init__.py +14 -0
- model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/pytorch/quantize_wrapper.py +269 -0
- model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/pytorch/quantizer_utils.py +152 -0
- model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/pytorch/quantizers/__init__.py +35 -0
- model_compression_toolkit/{exporter/model_wrapper/pytorch/quantizers → quantizers_infrastructure/inferable_infrastructure/pytorch/quantizers/activation_inferable_quantizers}/__init__.py +1 -1
- model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/pytorch/quantizers/activation_inferable_quantizers/activation_lut_pot_inferable_quantizer.py +97 -0
- model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/pytorch/quantizers/activation_inferable_quantizers/activation_pot_inferable_quantizer.py +62 -0
- model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/pytorch/quantizers/activation_inferable_quantizers/activation_symmetric_inferable_quantizer.py +83 -0
- model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/pytorch/quantizers/activation_inferable_quantizers/activation_uniform_inferable_quantizer.py +100 -0
- model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/pytorch/quantizers/base_lut_symmetric_inferable_quantizer.py +95 -0
- model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/pytorch/quantizers/base_pytorch_inferable_quantizer.py +48 -0
- model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/pytorch/quantizers/base_symmetric_inferable_quantizer.py +70 -0
- model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/pytorch/quantizers/base_uniform_inferable_quantizer.py +57 -0
- model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/pytorch/quantizers/constants.py +26 -0
- model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/pytorch/quantizers/weights_inferable_quantizers/__init__.py +14 -0
- model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/pytorch/quantizers/weights_inferable_quantizers/weights_lut_pot_inferable_quantizer.py +77 -0
- model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/pytorch/quantizers/weights_inferable_quantizers/weights_lut_symmetric_inferable_quantizer.py +106 -0
- model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/pytorch/quantizers/weights_inferable_quantizers/weights_pot_inferable_quantizer.py +66 -0
- model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/pytorch/quantizers/weights_inferable_quantizers/weights_symmetric_inferable_quantizer.py +104 -0
- model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/pytorch/quantizers/weights_inferable_quantizers/weights_uniform_inferable_quantizer.py +109 -0
- model_compression_toolkit/quantizers_infrastructure/trainable_infrastructure/__init__.py +14 -0
- model_compression_toolkit/quantizers_infrastructure/trainable_infrastructure/common/__init__.py +14 -0
- model_compression_toolkit/quantizers_infrastructure/trainable_infrastructure/common/base_trainable_quantizer.py +200 -0
- model_compression_toolkit/quantizers_infrastructure/trainable_infrastructure/common/get_quantizer_config.py +116 -0
- model_compression_toolkit/quantizers_infrastructure/trainable_infrastructure/common/get_quantizers.py +65 -0
- model_compression_toolkit/quantizers_infrastructure/trainable_infrastructure/common/quant_utils.py +36 -0
- model_compression_toolkit/quantizers_infrastructure/trainable_infrastructure/common/trainable_quantizer_config.py +97 -0
- model_compression_toolkit/quantizers_infrastructure/trainable_infrastructure/keras/__init__.py +14 -0
- model_compression_toolkit/quantizers_infrastructure/trainable_infrastructure/keras/base_keras_quantizer.py +90 -0
- model_compression_toolkit/quantizers_infrastructure/trainable_infrastructure/keras/config_serialization.py +80 -0
- model_compression_toolkit/quantizers_infrastructure/trainable_infrastructure/keras/quantizer_utils.py +48 -0
- model_compression_toolkit/quantizers_infrastructure/trainable_infrastructure/pytorch/__init__.py +14 -0
- model_compression_toolkit/quantizers_infrastructure/trainable_infrastructure/pytorch/base_pytorch_quantizer.py +66 -0
- model_compression_toolkit/exporter/model_wrapper/keras/builder/quantize_config_to_node.py +0 -66
- model_compression_toolkit/exporter/model_wrapper/keras/builder/quantizer_to_node.py +0 -134
- model_compression_toolkit/exporter/model_wrapper/keras/extended_quantize_wrapper.py +0 -81
- model_compression_toolkit/exporter/model_wrapper/keras/quantize_configs/activation_quantize_config.py +0 -81
- model_compression_toolkit/exporter/model_wrapper/keras/quantize_configs/weights_activation_quantize_config.py +0 -128
- model_compression_toolkit/exporter/model_wrapper/keras/quantize_configs/weights_quantize_config.py +0 -107
- model_compression_toolkit/exporter/model_wrapper/keras/quantizers/fq_quantizer.py +0 -99
- model_compression_toolkit/exporter/model_wrapper/keras/quantizers/weights_uniform_quantizer.py +0 -105
- model_compression_toolkit/exporter/model_wrapper/pytorch/builder/node_to_quantize_config.py +0 -61
- model_compression_toolkit/exporter/model_wrapper/pytorch/quantizers/fq_quantizer.py +0 -59
- model_compression_toolkit/exporter/model_wrapper/pytorch/quantizers/uniform_weights_quantizer.py +0 -67
- model_compression_toolkit/exporter/model_wrapper/pytorch/wrappers_quantize_configs/activation_quantize_config.py +0 -52
- model_compression_toolkit/exporter/model_wrapper/pytorch/wrappers_quantize_configs/no_quantization_quantize_config.py +0 -46
- model_compression_toolkit/exporter/model_wrapper/pytorch/wrappers_quantize_configs/weights_activation_quantize_config.py +0 -54
- model_compression_toolkit/exporter/model_wrapper/pytorch/wrappers_quantize_configs/weights_quantize_config.py +0 -52
- model_compression_toolkit/gptq/keras/gptq_model_builder.py +0 -104
- model_compression_toolkit/gptq/keras/optimizers/sam_optimizer.py +0 -119
- model_compression_toolkit/gptq/keras/quantizer/config_factory.py +0 -62
- model_compression_toolkit/gptq/keras/quantizer/configs/base_quantizer_gptq_config.py +0 -65
- model_compression_toolkit/gptq/keras/quantizer/configs/weight_quantizer_gptq_config.py +0 -269
- model_compression_toolkit/gptq/keras/quantizer/gumbel_rounding/base_gumbel_rounding.py +0 -263
- model_compression_toolkit/gptq/keras/quantizer/gumbel_rounding/gumbel_softmax.py +0 -75
- model_compression_toolkit/gptq/keras/quantizer/gumbel_rounding/symmetric_gumbel.py +0 -266
- model_compression_toolkit/gptq/keras/quantizer/gumbel_rounding/uniform_gumbel.py +0 -247
- model_compression_toolkit/gptq/keras/quantizer/kernel_functions.py +0 -50
- model_compression_toolkit/gptq/keras/quantizer/ste_rounding/uniform_ste.py +0 -49
- model_compression_toolkit/gptq/pytorch/gptq_graph_info.py +0 -94
- model_compression_toolkit/gptq/pytorch/gptq_model_builder.py +0 -113
- model_compression_toolkit/gptq/pytorch/quantizer/gptq_quantizer.py +0 -71
- model_compression_toolkit/gptq/pytorch/quantizer/gumbel_rounding/__init__.py +0 -14
- model_compression_toolkit/gptq/pytorch/quantizer/gumbel_rounding/base_gumbel_weights_quantizer.py +0 -157
- model_compression_toolkit/gptq/pytorch/quantizer/gumbel_rounding/sym_gumbel_weights_quantizer.py +0 -150
- model_compression_toolkit/gptq/pytorch/quantizer/gumbel_rounding/uniform_gumbel_weights_quantizer.py +0 -143
- model_compression_toolkit/gptq/pytorch/quantizer/quantizer_wrapper.py +0 -103
- model_compression_toolkit/gptq/pytorch/quantizer/ste_rounding/ste_weights_quantizer.py +0 -103
- model_compression_toolkit/qat/keras/qat_model_builder.py +0 -105
- model_compression_toolkit/qat/keras/quantizer/quantization_dispatcher_builder.py +0 -56
- model_compression_toolkit/qat/keras/quantizer/ste_rounding/symmetirc_ste.py +0 -145
- model_compression_toolkit/qunatizers_infrastructure/__init__.py +0 -8
- model_compression_toolkit/qunatizers_infrastructure/common/__init__.py +0 -14
- model_compression_toolkit/qunatizers_infrastructure/common/base_quantizer.py +0 -123
- model_compression_toolkit/qunatizers_infrastructure/common/node_quantization_dispatcher.py +0 -65
- model_compression_toolkit/qunatizers_infrastructure/keras/__init__.py +0 -14
- model_compression_toolkit/qunatizers_infrastructure/keras/base_keras_quantizer.py +0 -75
- model_compression_toolkit/qunatizers_infrastructure/keras/config_serialization.py +0 -83
- model_compression_toolkit/qunatizers_infrastructure/keras/keras_node_quantization_dispatcher.py +0 -74
- model_compression_toolkit/qunatizers_infrastructure/keras/quantize_wrapper.py +0 -194
- model_compression_toolkit/qunatizers_infrastructure/pytorch/__init__.py +0 -0
- {mct_nightly-1.7.1.31122022.post351.dist-info → mct_nightly-1.8.0.1042023.post423.dist-info}/LICENSE.md +0 -0
- {mct_nightly-1.7.1.31122022.post351.dist-info → mct_nightly-1.8.0.1042023.post423.dist-info}/top_level.txt +0 -0
- /model_compression_toolkit/{exporter/model_wrapper/pytorch/wrappers_quantize_configs → qat/pytorch/quantizer/ste_rounding}/__init__.py +0 -0
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
# Copyright 2023 Sony Semiconductor Israel, Inc. All rights reserved.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
# ==============================================================================
|
|
15
|
+
import warnings
|
|
16
|
+
from typing import List
|
|
17
|
+
|
|
18
|
+
import numpy as np
|
|
19
|
+
|
|
20
|
+
from model_compression_toolkit.core.common.logger import Logger
|
|
21
|
+
from model_compression_toolkit.core.common.constants import FOUND_TF
|
|
22
|
+
|
|
23
|
+
from model_compression_toolkit.core.common.target_platform import QuantizationMethod
|
|
24
|
+
from model_compression_toolkit.quantizers_infrastructure import QuantizationTarget
|
|
25
|
+
from model_compression_toolkit.quantizers_infrastructure.inferable_infrastructure.common.base_inferable_quantizer import mark_quantizer
|
|
26
|
+
from model_compression_toolkit.quantizers_infrastructure.inferable_infrastructure.common.constants import MULTIPLIER_N_BITS, EPS
|
|
27
|
+
|
|
28
|
+
if FOUND_TF:
|
|
29
|
+
import tensorflow as tf
|
|
30
|
+
from model_compression_toolkit.quantizers_infrastructure.inferable_infrastructure.keras.quantizers.base_keras_inferable_quantizer \
|
|
31
|
+
import BaseKerasInferableQuantizer
|
|
32
|
+
from model_compression_toolkit.quantizers_infrastructure.inferable_infrastructure.keras.quantizer_utils import lut_quantizer
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
@mark_quantizer(quantization_target=QuantizationTarget.Activation,
|
|
36
|
+
quantization_method=[QuantizationMethod.LUT_POT_QUANTIZER],
|
|
37
|
+
quantizer_type=None)
|
|
38
|
+
class ActivationLutPOTInferableQuantizer(BaseKerasInferableQuantizer):
|
|
39
|
+
"""
|
|
40
|
+
Class for quantizing activations using lut power-of-two quantizer
|
|
41
|
+
"""
|
|
42
|
+
|
|
43
|
+
def __init__(self,
|
|
44
|
+
num_bits: int,
|
|
45
|
+
cluster_centers: np.ndarray,
|
|
46
|
+
threshold: List[float],
|
|
47
|
+
signed: bool,
|
|
48
|
+
multiplier_n_bits: int = MULTIPLIER_N_BITS,
|
|
49
|
+
eps: float = EPS):
|
|
50
|
+
"""
|
|
51
|
+
Initialize the quantizer with the specified parameters.
|
|
52
|
+
|
|
53
|
+
Args:
|
|
54
|
+
num_bits: number of bits to use for quantization
|
|
55
|
+
cluster_centers: the cluster centers to assign the activations
|
|
56
|
+
threshold: threshold for quantizing activations
|
|
57
|
+
signed: whether or not to use signed quantization
|
|
58
|
+
multiplier_n_bits: Number of bits that determines the quantization range
|
|
59
|
+
eps: Small value for numerical stability in division
|
|
60
|
+
"""
|
|
61
|
+
# Call the superclass constructor with the given parameters, along with the target of Activation
|
|
62
|
+
# quantization
|
|
63
|
+
super(ActivationLutPOTInferableQuantizer, self).__init__()
|
|
64
|
+
|
|
65
|
+
assert isinstance(threshold, list), f'Expected threshold to be of type list but is {type(threshold)}'
|
|
66
|
+
assert all([isinstance(x, (float, np.float32, tf.float32)) for x in
|
|
67
|
+
threshold]), f'Expected threshold list to contain float or np.float values but found ' \
|
|
68
|
+
f'{[type(x) for x in threshold]}'
|
|
69
|
+
|
|
70
|
+
# In activation per-channel quantization is not supported thus we expect a single threshold value.
|
|
71
|
+
assert len(threshold) == 1, f'In per-tensor quantization threshold should be of ' \
|
|
72
|
+
f'length 1 but is {len(threshold)}'
|
|
73
|
+
|
|
74
|
+
is_threshold_pot = np.all([int(np.log2(x)) == np.log2(x) for x in threshold])
|
|
75
|
+
assert is_threshold_pot, f'Expected threshold to be power of 2 but is {threshold}'
|
|
76
|
+
|
|
77
|
+
self.threshold = threshold[0]
|
|
78
|
+
|
|
79
|
+
assert len(np.unique(cluster_centers)) <= 2 ** num_bits, \
|
|
80
|
+
f'Expected num of cluster centers to be less or equal than {2 ** num_bits} ' \
|
|
81
|
+
f'but got {len(cluster_centers)}'
|
|
82
|
+
|
|
83
|
+
assert not np.any(cluster_centers - cluster_centers.astype(int)), f'Expected cluster centers to be integers'
|
|
84
|
+
|
|
85
|
+
if signed:
|
|
86
|
+
assert np.all((-1 * (2 ** (multiplier_n_bits - int(signed))) <= cluster_centers) &
|
|
87
|
+
(cluster_centers <= (2 ** (multiplier_n_bits - int(signed)) - 1))), \
|
|
88
|
+
f'Expected cluster centers in the quantization range'
|
|
89
|
+
else:
|
|
90
|
+
assert np.all(cluster_centers <= (2 ** multiplier_n_bits)), \
|
|
91
|
+
f'Expected cluster centers in the quantization range'
|
|
92
|
+
|
|
93
|
+
# num_bits must be less than multiplier_n_bits
|
|
94
|
+
assert num_bits <= multiplier_n_bits, f'Look-Up-Table bit configuration has {num_bits} bits. It must be ' \
|
|
95
|
+
f'less then {multiplier_n_bits}'
|
|
96
|
+
if num_bits == multiplier_n_bits:
|
|
97
|
+
warnings.warn("Num of bits equal to multiplier n bits, Please be aware LUT quantizier may be "
|
|
98
|
+
"inefficient in that case, consider using SymmetricInferableQuantizer instead")
|
|
99
|
+
|
|
100
|
+
# If unsigned activation quantization, all cluster_centers must have the same sign
|
|
101
|
+
if not signed:
|
|
102
|
+
assert np.all(cluster_centers >= 0), f'Expected unsigned cluster centers in unsigned activation ' \
|
|
103
|
+
f'quantization '
|
|
104
|
+
|
|
105
|
+
self.num_bits = num_bits
|
|
106
|
+
self.cluster_centers = cluster_centers
|
|
107
|
+
self.signed = signed
|
|
108
|
+
self.multiplier_n_bits = multiplier_n_bits
|
|
109
|
+
self.eps = eps
|
|
110
|
+
|
|
111
|
+
def __call__(self, inputs: tf.Tensor) -> tf.Tensor:
|
|
112
|
+
"""
|
|
113
|
+
Quantize the given inputs using the quantizer parameters.
|
|
114
|
+
|
|
115
|
+
Args:
|
|
116
|
+
inputs: input tensor to quantize
|
|
117
|
+
|
|
118
|
+
Returns:
|
|
119
|
+
quantized tensor.
|
|
120
|
+
"""
|
|
121
|
+
assert inputs.dtype == tf.float32, f'Input tensor was expected to be a float tensor but is of type ' \
|
|
122
|
+
f'{inputs.dtype}'
|
|
123
|
+
|
|
124
|
+
return lut_quantizer(inputs, cluster_centers=self.cluster_centers, signed=self.signed,
|
|
125
|
+
threshold=self.threshold, multiplier_n_bits=self.multiplier_n_bits, eps=self.eps)
|
|
126
|
+
|
|
127
|
+
def get_config(self):
|
|
128
|
+
"""
|
|
129
|
+
Return a dictionary with the configuration of the quantizer.
|
|
130
|
+
|
|
131
|
+
Returns:
|
|
132
|
+
Dictionary with the following keys: 'num_bits', 'cluster_centers', 'threshold', 'signed',
|
|
133
|
+
'multiplier_n_bits', 'eps'
|
|
134
|
+
"""
|
|
135
|
+
return {'num_bits': self.num_bits,
|
|
136
|
+
'cluster_centers': self.cluster_centers,
|
|
137
|
+
'threshold': self.threshold,
|
|
138
|
+
'signed': self.signed,
|
|
139
|
+
'multiplier_n_bits': self.multiplier_n_bits,
|
|
140
|
+
'eps': self.eps}
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
else:
|
|
144
|
+
class ActivationLutPOTInferableQuantizer: # pragma: no cover
|
|
145
|
+
def __init__(self, *args, **kwargs):
|
|
146
|
+
Logger.error('Installing tensorflow and tensorflow_model_optimization is mandatory '
|
|
147
|
+
'when using ActivationLutPOTInferableQuantizer. '
|
|
148
|
+
'Could not find Tensorflow package.')
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# Copyright 2023 Sony Semiconductor Israel, Inc. All rights reserved.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
# ==============================================================================
|
|
15
|
+
from typing import List
|
|
16
|
+
|
|
17
|
+
import numpy as np
|
|
18
|
+
|
|
19
|
+
from model_compression_toolkit.core.common.logger import Logger
|
|
20
|
+
from model_compression_toolkit.core.common.constants import FOUND_TF
|
|
21
|
+
|
|
22
|
+
from model_compression_toolkit.core.common.target_platform import QuantizationMethod
|
|
23
|
+
from model_compression_toolkit.quantizers_infrastructure import QuantizationTarget
|
|
24
|
+
from model_compression_toolkit.quantizers_infrastructure.inferable_infrastructure.common.base_inferable_quantizer import mark_quantizer
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
if FOUND_TF:
|
|
28
|
+
from model_compression_toolkit.quantizers_infrastructure.inferable_infrastructure.keras.quantizers.activation_inferable_quantizers.activation_symmetric_inferable_quantizer \
|
|
29
|
+
import ActivationSymmetricInferableQuantizer
|
|
30
|
+
|
|
31
|
+
@mark_quantizer(quantization_target=QuantizationTarget.Activation,
|
|
32
|
+
quantization_method=[QuantizationMethod.POWER_OF_TWO],
|
|
33
|
+
quantizer_type=None)
|
|
34
|
+
class ActivationPOTInferableQuantizer(ActivationSymmetricInferableQuantizer):
|
|
35
|
+
"""
|
|
36
|
+
Class for quantizing activations using power-of-two quantizer
|
|
37
|
+
"""
|
|
38
|
+
|
|
39
|
+
def __init__(self,
|
|
40
|
+
num_bits: int,
|
|
41
|
+
threshold: List[float],
|
|
42
|
+
signed: bool):
|
|
43
|
+
"""
|
|
44
|
+
Initialize the quantizer with the specified parameters.
|
|
45
|
+
|
|
46
|
+
Args:
|
|
47
|
+
num_bits: number of bits to use for quantization
|
|
48
|
+
threshold: threshold for quantizing activations
|
|
49
|
+
signed: whether or not to use signed quantization
|
|
50
|
+
"""
|
|
51
|
+
# Call the superclass constructor with the given parameters, along with the target of Activation
|
|
52
|
+
# quantization
|
|
53
|
+
super(ActivationPOTInferableQuantizer, self).__init__(num_bits=num_bits,
|
|
54
|
+
threshold=threshold,
|
|
55
|
+
signed=signed)
|
|
56
|
+
|
|
57
|
+
is_threshold_pot = np.all([int(np.log2(x)) == np.log2(x) for x in self.threshold.flatten()])
|
|
58
|
+
assert is_threshold_pot, f'Expected threshold to be power of 2 but is {self.threshold}'
|
|
59
|
+
|
|
60
|
+
else:
|
|
61
|
+
class ActivationPOTInferableQuantizer: # pragma: no cover
|
|
62
|
+
def __init__(self, *args, **kwargs):
|
|
63
|
+
Logger.error('Installing tensorflow and tensorflow_model_optimization is mandatory '
|
|
64
|
+
'when using ActivationPOTInferableQuantizer. '
|
|
65
|
+
'Could not find Tensorflow package.')
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# Copyright 2023 Sony Semiconductor Israel, Inc. All rights reserved.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
# ==============================================================================
|
|
15
|
+
from typing import List
|
|
16
|
+
|
|
17
|
+
import numpy as np
|
|
18
|
+
|
|
19
|
+
from model_compression_toolkit.core.common.constants import FOUND_TF
|
|
20
|
+
|
|
21
|
+
from model_compression_toolkit.core.common.target_platform import QuantizationMethod
|
|
22
|
+
from model_compression_toolkit.quantizers_infrastructure.inferable_infrastructure.common.base_inferable_quantizer import mark_quantizer, \
|
|
23
|
+
QuantizationTarget
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
if FOUND_TF:
|
|
27
|
+
import tensorflow as tf
|
|
28
|
+
from model_compression_toolkit.quantizers_infrastructure.inferable_infrastructure.keras.quantizers.activation_inferable_quantizers.activation_uniform_inferable_quantizer \
|
|
29
|
+
import ActivationUniformInferableQuantizer
|
|
30
|
+
|
|
31
|
+
@mark_quantizer(quantization_target=QuantizationTarget.Activation,
|
|
32
|
+
quantization_method=[QuantizationMethod.SYMMETRIC],
|
|
33
|
+
quantizer_type=None)
|
|
34
|
+
class ActivationSymmetricInferableQuantizer(ActivationUniformInferableQuantizer):
|
|
35
|
+
|
|
36
|
+
"""
|
|
37
|
+
Class for quantizing activations using a symmetric quantizer
|
|
38
|
+
"""
|
|
39
|
+
|
|
40
|
+
def __init__(self,
|
|
41
|
+
num_bits: int,
|
|
42
|
+
threshold: List[float],
|
|
43
|
+
signed: bool):
|
|
44
|
+
"""
|
|
45
|
+
Initialize the quantizer with the specified parameters.
|
|
46
|
+
|
|
47
|
+
Args:
|
|
48
|
+
num_bits: number of bits to use for quantization
|
|
49
|
+
threshold: threshold for quantizing activations
|
|
50
|
+
signed: whether or not to use signed quantization
|
|
51
|
+
"""
|
|
52
|
+
assert isinstance(threshold, list), f'Expected threshold to be of type list but is {type(threshold)}'
|
|
53
|
+
assert all([isinstance(x, (float, np.float32, tf.float32)) for x in
|
|
54
|
+
threshold]), f'Expected threshold list to contain float or np.float values but found ' \
|
|
55
|
+
f'{[type(x) for x in threshold]}'
|
|
56
|
+
|
|
57
|
+
self.threshold = np.asarray(threshold)
|
|
58
|
+
self.signed = signed
|
|
59
|
+
|
|
60
|
+
delta = self.threshold / (2 ** (num_bits - int(self.signed)))
|
|
61
|
+
# In activation quantization is per-tensor only - thus we pass the threshold as a list with a len of 1
|
|
62
|
+
min_range = list(-self.threshold) if self.signed else [0.0]
|
|
63
|
+
max_range = list(self.threshold - delta)
|
|
64
|
+
|
|
65
|
+
super(ActivationSymmetricInferableQuantizer, self).__init__(num_bits=num_bits,
|
|
66
|
+
min_range=min_range,
|
|
67
|
+
max_range=max_range)
|
|
68
|
+
|
|
69
|
+
def get_config(self):
|
|
70
|
+
"""
|
|
71
|
+
Return a dictionary with the configuration of the quantizer.
|
|
72
|
+
|
|
73
|
+
Returns:
|
|
74
|
+
Dictionary with the following keys: 'num_bits', 'signed', 'threshold'
|
|
75
|
+
"""
|
|
76
|
+
return {'num_bits': self.num_bits,
|
|
77
|
+
'signed': self.signed,
|
|
78
|
+
'threshold': self.threshold}
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
else:
|
|
82
|
+
class ActivationSymmetricInferableQuantizer: # pragma: no cover
|
|
83
|
+
def __init__(self, *args, **kwargs):
|
|
84
|
+
raise Exception('Installing tensorflow and tensorflow_model_optimization is mandatory '
|
|
85
|
+
'when using ActivationSymmetricInferableQuantizer. '
|
|
86
|
+
'Could not find Tensorflow package.')
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# Copyright 2023 Sony Semiconductor Israel, Inc. All rights reserved.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
# ==============================================================================
|
|
15
|
+
from typing import List
|
|
16
|
+
|
|
17
|
+
import numpy as np
|
|
18
|
+
|
|
19
|
+
from model_compression_toolkit.core.common.logger import Logger
|
|
20
|
+
from model_compression_toolkit.core.common.constants import FOUND_TF
|
|
21
|
+
from model_compression_toolkit.core.common.target_platform import QuantizationMethod
|
|
22
|
+
from model_compression_toolkit.quantizers_infrastructure import QuantizationTarget
|
|
23
|
+
from model_compression_toolkit.quantizers_infrastructure.inferable_infrastructure.common.base_inferable_quantizer import mark_quantizer
|
|
24
|
+
from model_compression_toolkit.quantizers_infrastructure.inferable_infrastructure.common.quant_utils import \
|
|
25
|
+
adjust_range_to_include_zero
|
|
26
|
+
from model_compression_toolkit.quantizers_infrastructure.inferable_infrastructure.keras.validation_functions import \
|
|
27
|
+
validate_uniform_min_max_ranges, validate_adjusted_min_max_ranges
|
|
28
|
+
|
|
29
|
+
if FOUND_TF:
|
|
30
|
+
import tensorflow as tf
|
|
31
|
+
from model_compression_toolkit.quantizers_infrastructure.inferable_infrastructure.keras.quantizers.base_keras_inferable_quantizer import BaseKerasInferableQuantizer
|
|
32
|
+
|
|
33
|
+
@mark_quantizer(quantization_target=QuantizationTarget.Activation,
|
|
34
|
+
quantization_method=[QuantizationMethod.UNIFORM],
|
|
35
|
+
quantizer_type=None)
|
|
36
|
+
class ActivationUniformInferableQuantizer(BaseKerasInferableQuantizer):
|
|
37
|
+
"""
|
|
38
|
+
Class for quantizing activations using an uniform quantizer
|
|
39
|
+
"""
|
|
40
|
+
|
|
41
|
+
def __init__(self,
|
|
42
|
+
num_bits: int,
|
|
43
|
+
min_range: List[float],
|
|
44
|
+
max_range: List[float],
|
|
45
|
+
):
|
|
46
|
+
"""
|
|
47
|
+
Initialize the quantizer with the specified parameters.
|
|
48
|
+
|
|
49
|
+
Args:
|
|
50
|
+
num_bits: number of bits to use for quantization
|
|
51
|
+
min_range: min range for quantizing activations
|
|
52
|
+
max_range: max range for quantizing activations
|
|
53
|
+
"""
|
|
54
|
+
super(ActivationUniformInferableQuantizer, self).__init__()
|
|
55
|
+
|
|
56
|
+
# Validate ranges properties
|
|
57
|
+
validate_uniform_min_max_ranges(min_range,
|
|
58
|
+
max_range)
|
|
59
|
+
|
|
60
|
+
# In activation per-channel quantization is not supported thus we expect a single min/max value.
|
|
61
|
+
assert len(min_range) == 1, f'In per-tensor quantization min_range should be of length 1 but is {len(min_range)}'
|
|
62
|
+
assert len(max_range) == 1, f'In per-tensor quantization max_range should be of length 1 but is {len(max_range)}'
|
|
63
|
+
|
|
64
|
+
self.num_bits = num_bits
|
|
65
|
+
|
|
66
|
+
# Convert min/max to numpy arrays
|
|
67
|
+
min_range, max_range = np.asarray(min_range), np.asarray(max_range)
|
|
68
|
+
_min_range, _max_range = adjust_range_to_include_zero(min_range, max_range, num_bits)
|
|
69
|
+
validate_adjusted_min_max_ranges(min_range=min_range,
|
|
70
|
+
max_range=max_range,
|
|
71
|
+
adj_min=_min_range,
|
|
72
|
+
adj_max=_max_range)
|
|
73
|
+
|
|
74
|
+
self.max_range = _max_range[0]
|
|
75
|
+
self.min_range = _min_range[0]
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def __call__(self, inputs:tf.Tensor) -> tf.Tensor:
|
|
79
|
+
"""
|
|
80
|
+
Quantize the given inputs using the quantizer parameters.
|
|
81
|
+
|
|
82
|
+
Args:
|
|
83
|
+
inputs: input tensor to quantize
|
|
84
|
+
|
|
85
|
+
Returns:
|
|
86
|
+
quantized tensor.
|
|
87
|
+
"""
|
|
88
|
+
assert inputs.dtype==tf.float32, f'Input tensor was expected to be a float tensor but is of type {inputs.dtype}'
|
|
89
|
+
|
|
90
|
+
return tf.quantization.fake_quant_with_min_max_vars(inputs,
|
|
91
|
+
min=self.min_range,
|
|
92
|
+
max=self.max_range,
|
|
93
|
+
num_bits=self.num_bits)
|
|
94
|
+
|
|
95
|
+
def get_config(self):
|
|
96
|
+
"""
|
|
97
|
+
Return a dictionary with the configuration of the quantizer.
|
|
98
|
+
|
|
99
|
+
Returns:
|
|
100
|
+
Dictionary with the following keys: 'num_bits', 'min_range', 'max_range'
|
|
101
|
+
"""
|
|
102
|
+
return {'num_bits': self.num_bits,
|
|
103
|
+
'min_range': self.min_range,
|
|
104
|
+
'max_range': self.max_range}
|
|
105
|
+
|
|
106
|
+
else:
|
|
107
|
+
class ActivationUniformInferableQuantizer: # pragma: no cover
|
|
108
|
+
def __init__(self, *args, **kwargs):
|
|
109
|
+
Logger.error('Installing tensorflow and tensorflow_model_optimization is mandatory '
|
|
110
|
+
'when using ActivationUniformInferableQuantizer. '
|
|
111
|
+
'Could not find Tensorflow package.')
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# Copyright 2023 Sony Semiconductor Israel, Inc. All rights reserved.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
# ==============================================================================
|
|
15
|
+
from abc import abstractmethod
|
|
16
|
+
|
|
17
|
+
from model_compression_toolkit.core.common.constants import FOUND_TF
|
|
18
|
+
from model_compression_toolkit.quantizers_infrastructure import BaseInferableQuantizer
|
|
19
|
+
|
|
20
|
+
if FOUND_TF:
|
|
21
|
+
import tensorflow as tf
|
|
22
|
+
|
|
23
|
+
class BaseKerasInferableQuantizer(BaseInferableQuantizer):
|
|
24
|
+
def __init__(self):
|
|
25
|
+
"""
|
|
26
|
+
This class is a base quantizer for Keras quantizers for inference only.
|
|
27
|
+
"""
|
|
28
|
+
super(BaseKerasInferableQuantizer, self).__init__()
|
|
29
|
+
|
|
30
|
+
@abstractmethod
|
|
31
|
+
def get_config(self):
|
|
32
|
+
"""
|
|
33
|
+
Return a dictionary with the configuration of the quantizer.
|
|
34
|
+
"""
|
|
35
|
+
raise NotImplemented(f'{self.__class__.__name__} did not implement get_config') # pragma: no cover
|
|
36
|
+
|
|
37
|
+
@abstractmethod
|
|
38
|
+
def __call__(self, inputs: tf.Tensor):
|
|
39
|
+
"""
|
|
40
|
+
Quantize the given inputs using the quantizer parameters.
|
|
41
|
+
|
|
42
|
+
Args:
|
|
43
|
+
inputs: input tensor to quantize
|
|
44
|
+
|
|
45
|
+
Returns:
|
|
46
|
+
quantized tensor.
|
|
47
|
+
"""
|
|
48
|
+
raise NotImplemented(f'{self.__class__.__name__} did not implement __call__') # pragma: no cover
|
|
49
|
+
else:
|
|
50
|
+
class BaseKerasInferableQuantizer: # pragma: no cover
|
|
51
|
+
def __init__(self, *args, **kwargs):
|
|
52
|
+
raise Exception('Installing tensorflow and tensorflow_model_optimization is mandatory '
|
|
53
|
+
'when using BaseKerasInferableQuantizer. '
|
|
54
|
+
'Could not find Tensorflow package.')
|
|
55
|
+
|
|
56
|
+
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# Copyright 2023 Sony Semiconductor Israel, Inc. All rights reserved.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
# ==============================================================================
|
|
15
|
+
|
|
16
|
+
# Inferable keras quantizer signature parameters:
|
|
17
|
+
NUM_BITS = 'num_bits'
|
|
18
|
+
SIGNED = 'signed'
|
|
19
|
+
THRESHOLD = 'threshold'
|
|
20
|
+
PER_CHANNEL = 'per_channel'
|
|
21
|
+
MIN_RANGE = 'min_range'
|
|
22
|
+
MAX_RANGE = 'max_range'
|
|
23
|
+
CHANNEL_AXIS = 'channel_axis'
|
|
24
|
+
INPUT_RANK = 'input_rank'
|
|
25
|
+
CLUSTER_CENTERS = 'cluster_centers'
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# Copyright 2023 Sony Semiconductor Israel, Inc. All rights reserved.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
# ==============================================================================
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# Copyright 2023 Sony Semiconductor Israel, Inc. All rights reserved.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
# ==============================================================================
|
|
15
|
+
from typing import List
|
|
16
|
+
|
|
17
|
+
import numpy as np
|
|
18
|
+
|
|
19
|
+
from model_compression_toolkit.core.common.constants import FOUND_TF
|
|
20
|
+
from model_compression_toolkit.core.common.target_platform import QuantizationMethod
|
|
21
|
+
from model_compression_toolkit.quantizers_infrastructure.inferable_infrastructure.common.base_inferable_quantizer import mark_quantizer, \
|
|
22
|
+
QuantizationTarget
|
|
23
|
+
from model_compression_toolkit.quantizers_infrastructure.inferable_infrastructure.common.constants import MULTIPLIER_N_BITS, EPS
|
|
24
|
+
|
|
25
|
+
if FOUND_TF:
|
|
26
|
+
from model_compression_toolkit.quantizers_infrastructure.inferable_infrastructure.keras.quantizers.weights_inferable_quantizers.\
|
|
27
|
+
weights_lut_symmetric_inferable_quantizer import WeightsLUTSymmetricInferableQuantizer
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@mark_quantizer(quantization_target=QuantizationTarget.Weights,
|
|
31
|
+
quantization_method=[QuantizationMethod.LUT_POT_QUANTIZER],
|
|
32
|
+
quantizer_type=None)
|
|
33
|
+
class WeightsLUTPOTInferableQuantizer(WeightsLUTSymmetricInferableQuantizer):
|
|
34
|
+
"""
|
|
35
|
+
Class for quantizing weights using a lut power-of-two quantizer
|
|
36
|
+
"""
|
|
37
|
+
|
|
38
|
+
def __init__(self,
|
|
39
|
+
num_bits: int,
|
|
40
|
+
cluster_centers: np.ndarray,
|
|
41
|
+
threshold: List[float],
|
|
42
|
+
per_channel: bool,
|
|
43
|
+
channel_axis: int = None,
|
|
44
|
+
input_rank: int = None,
|
|
45
|
+
multiplier_n_bits: int = MULTIPLIER_N_BITS,
|
|
46
|
+
eps: float = EPS):
|
|
47
|
+
"""
|
|
48
|
+
Initialize the quantizer with the specified parameters.
|
|
49
|
+
|
|
50
|
+
Args:
|
|
51
|
+
num_bits: number of bits to use for quantization
|
|
52
|
+
cluster_centers: the cluster centers to assign the weights
|
|
53
|
+
threshold: threshold for quantizing weights
|
|
54
|
+
per_channel: whether to use per-channel quantization
|
|
55
|
+
channel_axis: axis along which to apply per-channel quantization
|
|
56
|
+
input_rank: number of dimensions of input tensor the quantizer quantizes
|
|
57
|
+
multiplier_n_bits: Number of bits that determines the quantization range
|
|
58
|
+
eps: Small value for numerical stability in division
|
|
59
|
+
"""
|
|
60
|
+
|
|
61
|
+
super(WeightsLUTPOTInferableQuantizer, self).__init__(num_bits=num_bits,
|
|
62
|
+
cluster_centers=cluster_centers,
|
|
63
|
+
threshold=threshold,
|
|
64
|
+
per_channel=per_channel,
|
|
65
|
+
channel_axis=channel_axis,
|
|
66
|
+
input_rank=input_rank,
|
|
67
|
+
multiplier_n_bits=multiplier_n_bits,
|
|
68
|
+
eps=eps)
|
|
69
|
+
|
|
70
|
+
is_threshold_pot = np.all([int(np.log2(x)) == np.log2(x) for x in self.threshold.flatten()])
|
|
71
|
+
assert is_threshold_pot, f'Expected threshold to be power of 2 but is {self.threshold}'
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
else:
|
|
75
|
+
class WeightsLUTPOTInferableQuantizer: # pragma: no cover
|
|
76
|
+
def __init__(self, *args, **kwargs):
|
|
77
|
+
raise Exception('Installing tensorflow and tensorflow_model_optimization is mandatory '
|
|
78
|
+
'when using WeightsLUTPOTInferableQuantizer. '
|
|
79
|
+
'Could not find Tensorflow package.')
|