From 872ccf81708b7fa044b1048c0a9685b48566f2cc Mon Sep 17 00:00:00 2001 From: RAYNAL JR Date: Wed, 31 Jan 2024 04:08:42 -0500 Subject: [PATCH 1/3] Fix Python 3.9 Windows file permission error in Fakely TFlite exporter (#936) replacing the use of NamedTemporaryFile for creating a temporary file for export with mkstemp command and ensuring file removal afterward. --- .../keras/fakely_quant_tflite_exporter.py | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/model_compression_toolkit/exporter/model_exporter/keras/fakely_quant_tflite_exporter.py b/model_compression_toolkit/exporter/model_exporter/keras/fakely_quant_tflite_exporter.py index 33263c3f7..f3a65fdcb 100644 --- a/model_compression_toolkit/exporter/model_exporter/keras/fakely_quant_tflite_exporter.py +++ b/model_compression_toolkit/exporter/model_exporter/keras/fakely_quant_tflite_exporter.py @@ -13,6 +13,7 @@ # limitations under the License. # ============================================================================== import os +from pathlib import Path import tempfile from typing import Callable @@ -56,14 +57,20 @@ def export(self): """ # Use Keras exporter to quantize model's weights before converting it to TFLite. - # Since exporter saves the model, we use a tmp path for saving, and then we delete it automatically. - with tempfile.NamedTemporaryFile(suffix=DEFAULT_KERAS_EXPORT_EXTENTION) as tmp_file: - FakelyQuantKerasExporter(self.model, - self.is_layer_exportable_fn, - tmp_file.name, - verbose=False).export() + # Since exporter saves the model, we use a tmp path for saving, and then we delete it. + handle, tmp_file = tempfile.mkstemp(DEFAULT_KERAS_EXPORT_EXTENTION) + # Close handle right away, the file is going to be reopenned by Keras exporter + os.close(handle) + try: + custom_objects = FakelyQuantKerasExporter(self.model, + self.is_layer_exportable_fn, + tmp_file, + verbose=False).export() - model = keras_load_quantized_model(tmp_file.name) + model = keras_load_quantized_model(tmp_file) + # Ensures artifact is removed even in case of error + finally: + Path(tmp_file).unlink(missing_ok=True) self.exported_model = tf.lite.TFLiteConverter.from_keras_model(model).convert() Logger.info(f'Exporting FQ tflite model to: {self.save_model_path}') From 61efdfcc0f87e9afacbeb332b32cddfd4dc16449 Mon Sep 17 00:00:00 2001 From: Reuven <44209964+reuvenperetz@users.noreply.github.com> Date: Wed, 31 Jan 2024 14:58:40 +0200 Subject: [PATCH 2/3] Change default onnx opset to 17 when exporting in torch 1.13 or above (#937) Change default onnx opset to 17 when exprting in torch 1.13 or above. --------- Co-authored-by: reuvenp --- .../pytorch/fakely_quant_onnx_pytorch_exporter.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/model_compression_toolkit/exporter/model_exporter/pytorch/fakely_quant_onnx_pytorch_exporter.py b/model_compression_toolkit/exporter/model_exporter/pytorch/fakely_quant_onnx_pytorch_exporter.py index 99887efbe..1ed3701ed 100644 --- a/model_compression_toolkit/exporter/model_exporter/pytorch/fakely_quant_onnx_pytorch_exporter.py +++ b/model_compression_toolkit/exporter/model_exporter/pytorch/fakely_quant_onnx_pytorch_exporter.py @@ -26,8 +26,11 @@ # ONNX opset version 16 is supported from PyTorch 1.12 if version.parse(torch.__version__) < version.parse("1.12"): OPSET_VERSION = 15 -else: +elif version.parse("1.12.0") <= version.parse(torch.__version__) < version.parse("1.13.0"): OPSET_VERSION = 16 +else: + # ONNX opset version 17 is supported from PyTorch 1.13 + OPSET_VERSION = 17 class FakelyQuantONNXPyTorchExporter(BasePyTorchExporter): From b62ed4e7c2bf37702f8e0aef79b97d0a3cc76810 Mon Sep 17 00:00:00 2001 From: Reuven <44209964+reuvenperetz@users.noreply.github.com> Date: Wed, 31 Jan 2024 15:29:53 +0200 Subject: [PATCH 3/3] Remove old per-channel configuration from QuantizationConfig (#919) --------- Co-authored-by: reuvenp --- .../quantization/quantization_config.py | 18 +++------- .../feature_networks/bn_folding_test.py | 3 +- .../feature_networks/gptq/gptq_test.py | 4 +-- .../feature_networks/input_scaling_test.py | 3 +- .../linear_collapsing_test.py | 3 +- .../matmul_substitution_test.py | 3 +- .../feature_networks/mixed_precision_tests.py | 20 ++++------- .../multi_inputs_to_node_test.py | 5 ++- .../multiple_inputs_node_tests.py | 3 +- .../nested_model_multiple_inputs_test.py | 5 ++- .../nested_model_multiple_outputs_test.py | 5 ++- .../network_editor/node_filter_test.py | 15 ++++---- .../residual_collapsing_test.py | 3 +- .../reused_layer_mixed_precision_test.py | 10 ++---- .../scale_equalization_test.py | 4 +-- .../shift_neg_activation_test.py | 6 ++-- .../feature_networks/split_conv_bug_test.py | 5 ++- .../feature_networks/test_kmeans_quantizer.py | 9 ++--- .../weights_mixed_precision_tests.py | 35 ++++++------------- .../function_tests/test_get_gptq_config.py | 3 +- ...st_kl_error_quantization_configurations.py | 11 +++--- .../test_quantization_configurations.py | 16 ++++----- ...t_symmetric_threshold_selection_weights.py | 3 +- .../test_uniform_range_selection_weights.py | 3 +- .../models_tests/test_networks_runner.py | 5 ++- .../function_tests/get_gptq_config_test.py | 3 +- .../model_tests/base_pytorch_test.py | 3 +- .../constant_conv_substitution_test.py | 3 +- .../model_tests/feature_models/gptq_test.py | 3 +- .../feature_models/linear_collapsing_test.py | 3 +- .../feature_models/lut_quantizer_test.py | 6 ++-- .../mixed_precision_activation_test.py | 10 ++---- .../mixed_precision_bops_test.py | 10 ++---- .../mixed_precision_weights_test.py | 10 ++---- .../feature_models/old_api_test.py | 10 ++---- .../permute_substitution_test.py | 3 +- .../feature_models/relu_bound_test.py | 6 ++-- .../residual_collapsing_test.py | 3 +- .../shift_negative_activation_test.py | 4 +-- .../symmetric_activation_test.py | 5 ++- .../feature_models/uniform_activation_test.py | 5 ++- .../keras/ptq/example_keras_imagenet.ipynb | 2 -- tutorials/quick_start/keras_fw/quant.py | 6 ++-- tutorials/quick_start/pytorch_fw/quant.py | 6 ++-- 44 files changed, 106 insertions(+), 195 deletions(-) diff --git a/model_compression_toolkit/core/common/quantization/quantization_config.py b/model_compression_toolkit/core/common/quantization/quantization_config.py index 3b2508c4c..a2aa4f89b 100644 --- a/model_compression_toolkit/core/common/quantization/quantization_config.py +++ b/model_compression_toolkit/core/common/quantization/quantization_config.py @@ -50,7 +50,6 @@ def __init__(self, weights_error_method: QuantizationErrorMethod = QuantizationErrorMethod.MSE, relu_bound_to_power_of_2: bool = False, weights_bias_correction: bool = True, - weights_per_channel_threshold: bool = True, weights_second_moment_correction: bool = False, input_scaling: bool = False, softmax_shift: bool = False, @@ -73,7 +72,6 @@ def __init__(self, relu_bound_to_power_of_2 (bool): Whether to use relu to power of 2 scaling correction or not. weights_bias_correction (bool): Whether to use weights bias correction or not. weights_second_moment_correction (bool): Whether to use weights second_moment correction or not. - weights_per_channel_threshold (bool): Whether to quantize the weights per-channel or not (per-tensor). input_scaling (bool): Whether to use input scaling or not. softmax_shift (bool): Whether to use softmax shift or not. shift_negative_activation_correction (bool): Whether to use shifting negative activation correction or not. @@ -90,11 +88,11 @@ def __init__(self, One may create a quantization configuration to quantize a model according to. For example, to quantize a model's weights and activation using thresholds, such that weights threshold selection is done using MSE, activation threshold selection is done using NOCLIPPING (min/max), - enabling relu_bound_to_power_of_2, weights_bias_correction, and quantizing the weights per-channel, + enabling relu_bound_to_power_of_2, weights_bias_correction, one can instantiate a quantization configuration: >>> import model_compression_toolkit as mct - >>> qc = mct.core.QuantizationConfig(activation_error_method=mct.core.QuantizationErrorMethod.NOCLIPPING,weights_error_method=mct.core.QuantizationErrorMethod.MSE,relu_bound_to_power_of_2=True,weights_bias_correction=True,weights_per_channel_threshold=True) + >>> qc = mct.core.QuantizationConfig(activation_error_method=mct.core.QuantizationErrorMethod.NOCLIPPING, weights_error_method=mct.core.QuantizationErrorMethod.MSE, relu_bound_to_power_of_2=True, weights_bias_correction=True) The QuantizationConfig instanse can then be passed to @@ -107,7 +105,6 @@ def __init__(self, self.relu_bound_to_power_of_2 = relu_bound_to_power_of_2 self.weights_bias_correction = weights_bias_correction self.weights_second_moment_correction = weights_second_moment_correction - self.weights_per_channel_threshold = weights_per_channel_threshold self.activation_channel_equalization = activation_channel_equalization self.input_scaling = input_scaling self.softmax_shift = softmax_shift @@ -126,11 +123,6 @@ def __repr__(self): # Default quantization configuration the library use. -DEFAULTCONFIG = QuantizationConfig(QuantizationErrorMethod.MSE, - QuantizationErrorMethod.MSE, - relu_bound_to_power_of_2=False, - weights_bias_correction=True, - weights_second_moment_correction=False, - weights_per_channel_threshold=True, - input_scaling=False, - softmax_shift=False) +DEFAULTCONFIG = QuantizationConfig(QuantizationErrorMethod.MSE, QuantizationErrorMethod.MSE, + relu_bound_to_power_of_2=False, weights_bias_correction=True, + weights_second_moment_correction=False, input_scaling=False, softmax_shift=False) diff --git a/tests/keras_tests/feature_networks_tests/feature_networks/bn_folding_test.py b/tests/keras_tests/feature_networks_tests/feature_networks/bn_folding_test.py index 581765574..5d8d71a93 100644 --- a/tests/keras_tests/feature_networks_tests/feature_networks/bn_folding_test.py +++ b/tests/keras_tests/feature_networks_tests/feature_networks/bn_folding_test.py @@ -70,8 +70,7 @@ def get_tpc(self): def get_quantization_config(self): return mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.NOCLIPPING, - mct.core.QuantizationErrorMethod.NOCLIPPING, - False, False, True) + mct.core.QuantizationErrorMethod.NOCLIPPING, False, False) def compare(self, quantized_model, float_model, input_x=None, quantization_info=None): # check the conv weights after the bn folding diff --git a/tests/keras_tests/feature_networks_tests/feature_networks/gptq/gptq_test.py b/tests/keras_tests/feature_networks_tests/feature_networks/gptq/gptq_test.py index 56bb4f9e3..582f395b9 100644 --- a/tests/keras_tests/feature_networks_tests/feature_networks/gptq/gptq_test.py +++ b/tests/keras_tests/feature_networks_tests/feature_networks/gptq/gptq_test.py @@ -82,9 +82,7 @@ def get_tpc(self): def get_quantization_config(self): return mct.core.QuantizationConfig(activation_error_method=mct.core.QuantizationErrorMethod.NOCLIPPING, weights_error_method=mct.core.QuantizationErrorMethod.NOCLIPPING, - relu_bound_to_power_of_2=True, - weights_bias_correction=False, - weights_per_channel_threshold=self.per_channel) + relu_bound_to_power_of_2=True, weights_bias_correction=False) def get_gptq_config(self): return GradientPTQConfig(5, optimizer=tf.keras.optimizers.Adam( diff --git a/tests/keras_tests/feature_networks_tests/feature_networks/input_scaling_test.py b/tests/keras_tests/feature_networks_tests/feature_networks/input_scaling_test.py index 1a00fe174..ab89b020f 100644 --- a/tests/keras_tests/feature_networks_tests/feature_networks/input_scaling_test.py +++ b/tests/keras_tests/feature_networks_tests/feature_networks/input_scaling_test.py @@ -39,8 +39,7 @@ def get_tpc(self): def get_quantization_config(self): return mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.NOCLIPPING, - mct.core.QuantizationErrorMethod.NOCLIPPING, - input_scaling=True) + mct.core.QuantizationErrorMethod.NOCLIPPING, input_scaling=True) def compare(self, quantized_model, float_model, input_x=None, quantization_info=None): fi = 2 if isinstance(float_model.layers[1], layers.ZeroPadding2D) else 1 diff --git a/tests/keras_tests/feature_networks_tests/feature_networks/linear_collapsing_test.py b/tests/keras_tests/feature_networks_tests/feature_networks/linear_collapsing_test.py index 05169b0c6..dddc07353 100644 --- a/tests/keras_tests/feature_networks_tests/feature_networks/linear_collapsing_test.py +++ b/tests/keras_tests/feature_networks_tests/feature_networks/linear_collapsing_test.py @@ -48,8 +48,7 @@ def get_tpc(self): def get_quantization_config(self): return mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.NOCLIPPING, - mct.core.QuantizationErrorMethod.NOCLIPPING, - False, False, True) + mct.core.QuantizationErrorMethod.NOCLIPPING, False, False) def compare(self, quantized_model, float_model, input_x=None, quantization_info=None): y = float_model.predict(input_x) diff --git a/tests/keras_tests/feature_networks_tests/feature_networks/matmul_substitution_test.py b/tests/keras_tests/feature_networks_tests/feature_networks/matmul_substitution_test.py index 57f98d513..ab833e151 100644 --- a/tests/keras_tests/feature_networks_tests/feature_networks/matmul_substitution_test.py +++ b/tests/keras_tests/feature_networks_tests/feature_networks/matmul_substitution_test.py @@ -43,8 +43,7 @@ def get_tpc(self): def get_quantization_config(self): return mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.NOCLIPPING, - mct.core.QuantizationErrorMethod.NOCLIPPING, - False, False, True) + mct.core.QuantizationErrorMethod.NOCLIPPING, False, False) def create_networks(self): inputs = tf.keras.layers.Input(shape=self.get_input_shapes()[0][1:]) diff --git a/tests/keras_tests/feature_networks_tests/feature_networks/mixed_precision_tests.py b/tests/keras_tests/feature_networks_tests/feature_networks/mixed_precision_tests.py index 9935f5a1c..5ccffda42 100644 --- a/tests/keras_tests/feature_networks_tests/feature_networks/mixed_precision_tests.py +++ b/tests/keras_tests/feature_networks_tests/feature_networks/mixed_precision_tests.py @@ -73,13 +73,9 @@ def get_tpc(self): name="mixed_precision_activation_test") def get_quantization_config(self): - return mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.MSE, - mct.core.QuantizationErrorMethod.MSE, - relu_bound_to_power_of_2=False, - weights_bias_correction=True, - weights_per_channel_threshold=True, - input_scaling=False, - activation_channel_equalization=False) + return mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.MSE, mct.core.QuantizationErrorMethod.MSE, + relu_bound_to_power_of_2=False, weights_bias_correction=True, + input_scaling=False, activation_channel_equalization=False) def get_mixed_precision_v2_config(self): return mct.core.MixedPrecisionQuantizationConfigV2(num_of_images=1) @@ -428,13 +424,9 @@ def get_input_shapes(self): return [[self.val_batch_size, 224, 244, 3] for _ in range(self.num_of_inputs)] def get_quantization_config(self): - return mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.MSE, - mct.core.QuantizationErrorMethod.MSE, - relu_bound_to_power_of_2=False, - weights_bias_correction=True, - weights_per_channel_threshold=True, - input_scaling=False, - activation_channel_equalization=False) + return mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.MSE, mct.core.QuantizationErrorMethod.MSE, + relu_bound_to_power_of_2=False, weights_bias_correction=True, + input_scaling=False, activation_channel_equalization=False) def get_mixed_precision_v2_config(self): return mct.core.MixedPrecisionQuantizationConfigV2(num_of_images=self.num_of_inputs) diff --git a/tests/keras_tests/feature_networks_tests/feature_networks/multi_inputs_to_node_test.py b/tests/keras_tests/feature_networks_tests/feature_networks/multi_inputs_to_node_test.py index be1c89677..13931d0f0 100644 --- a/tests/keras_tests/feature_networks_tests/feature_networks/multi_inputs_to_node_test.py +++ b/tests/keras_tests/feature_networks_tests/feature_networks/multi_inputs_to_node_test.py @@ -32,9 +32,8 @@ def get_tpc(self): return get_16bit_tpc("multi_input_test") def get_quantization_config(self): - return mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.MSE, - mct.core.QuantizationErrorMethod.MSE, - True, True, True, input_scaling=True) + return mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.MSE, mct.core.QuantizationErrorMethod.MSE, + True, True, input_scaling=True) def get_input_shapes(self): return [[self.val_batch_size, 224, 224, 3]] diff --git a/tests/keras_tests/feature_networks_tests/feature_networks/multiple_inputs_node_tests.py b/tests/keras_tests/feature_networks_tests/feature_networks/multiple_inputs_node_tests.py index d0aaa77ba..a5efa3c83 100644 --- a/tests/keras_tests/feature_networks_tests/feature_networks/multiple_inputs_node_tests.py +++ b/tests/keras_tests/feature_networks_tests/feature_networks/multiple_inputs_node_tests.py @@ -34,8 +34,7 @@ def get_tpc(self): def get_quantization_config(self): return mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.NOCLIPPING, - mct.core.QuantizationErrorMethod.NOCLIPPING, - True, False, True) + mct.core.QuantizationErrorMethod.NOCLIPPING, True, False) def get_input_shapes(self): return [[self.val_batch_size, 224, 244, 3]] diff --git a/tests/keras_tests/feature_networks_tests/feature_networks/nested_networks/nested_model_multiple_inputs_test.py b/tests/keras_tests/feature_networks_tests/feature_networks/nested_networks/nested_model_multiple_inputs_test.py index 24caabebb..6d31f6228 100644 --- a/tests/keras_tests/feature_networks_tests/feature_networks/nested_networks/nested_model_multiple_inputs_test.py +++ b/tests/keras_tests/feature_networks_tests/feature_networks/nested_networks/nested_model_multiple_inputs_test.py @@ -41,9 +41,8 @@ def get_tpc(self): return get_16bit_tpc("nested_multi_inputs_test") def get_quantization_config(self): - return mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.MSE, - mct.core.QuantizationErrorMethod.MSE, - True, True, True) + return mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.MSE, mct.core.QuantizationErrorMethod.MSE, + True, True) def get_input_shapes(self): return [[self.val_batch_size, 236, 236, 3]] diff --git a/tests/keras_tests/feature_networks_tests/feature_networks/nested_networks/nested_model_multiple_outputs_test.py b/tests/keras_tests/feature_networks_tests/feature_networks/nested_networks/nested_model_multiple_outputs_test.py index 57d398894..8eddda42c 100644 --- a/tests/keras_tests/feature_networks_tests/feature_networks/nested_networks/nested_model_multiple_outputs_test.py +++ b/tests/keras_tests/feature_networks_tests/feature_networks/nested_networks/nested_model_multiple_outputs_test.py @@ -41,9 +41,8 @@ def get_tpc(self): return get_16bit_tpc("nested_multi_outputs_test") def get_quantization_config(self): - return mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.MSE, - mct.core.QuantizationErrorMethod.MSE, - True, True, True) + return mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.MSE, mct.core.QuantizationErrorMethod.MSE, + True, True) def get_input_shapes(self): return [[self.val_batch_size, 236, 236, 3]] diff --git a/tests/keras_tests/feature_networks_tests/feature_networks/network_editor/node_filter_test.py b/tests/keras_tests/feature_networks_tests/feature_networks/network_editor/node_filter_test.py index 39872bdbd..22d77295f 100644 --- a/tests/keras_tests/feature_networks_tests/feature_networks/network_editor/node_filter_test.py +++ b/tests/keras_tests/feature_networks_tests/feature_networks/network_editor/node_filter_test.py @@ -63,9 +63,8 @@ def get_tpc(self): return generate_keras_tpc(name="scope_filter_test", tp_model=tp_model) def get_quantization_config(self): - return mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.MSE, - mct.core.QuantizationErrorMethod.MSE, - False, False, True) + return mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.MSE, mct.core.QuantizationErrorMethod.MSE, + False, False) def get_debug_config(self): # first rule is to check that the scope filter catches the 2 convs with @@ -143,9 +142,8 @@ def get_tpc(self): return generate_keras_tpc(name="name_filter_test", tp_model=tp_model) def get_quantization_config(self): - return mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.MSE, - mct.core.QuantizationErrorMethod.MSE, - False, False, True) + return mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.MSE, mct.core.QuantizationErrorMethod.MSE, + False, False) def get_debug_config(self): network_editor = [EditRule(filter=NodeNameFilter(self.node_to_change_name), @@ -215,9 +213,8 @@ def get_tpc(self): return generate_keras_tpc(name="type_filter_test", tp_model=tp_model) def get_quantization_config(self): - return mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.MSE, - mct.core.QuantizationErrorMethod.MSE, - False, False, False) + return mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.MSE, mct.core.QuantizationErrorMethod.MSE, + False, False) def get_debug_config(self): network_editor = [EditRule(filter=NodeTypeFilter(self.type_to_change), diff --git a/tests/keras_tests/feature_networks_tests/feature_networks/residual_collapsing_test.py b/tests/keras_tests/feature_networks_tests/feature_networks/residual_collapsing_test.py index 0fda39bdc..2429cff5b 100644 --- a/tests/keras_tests/feature_networks_tests/feature_networks/residual_collapsing_test.py +++ b/tests/keras_tests/feature_networks_tests/feature_networks/residual_collapsing_test.py @@ -41,8 +41,7 @@ def get_tpc(self): def get_quantization_config(self): return mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.NOCLIPPING, - mct.core.QuantizationErrorMethod.NOCLIPPING, - False, False, True) + mct.core.QuantizationErrorMethod.NOCLIPPING, False, False) def compare(self, quantized_model, float_model, input_x=None, quantization_info=None): y = float_model.predict(input_x) diff --git a/tests/keras_tests/feature_networks_tests/feature_networks/reused_layer_mixed_precision_test.py b/tests/keras_tests/feature_networks_tests/feature_networks/reused_layer_mixed_precision_test.py index 4c4855885..1aa1a38c9 100644 --- a/tests/keras_tests/feature_networks_tests/feature_networks/reused_layer_mixed_precision_test.py +++ b/tests/keras_tests/feature_networks_tests/feature_networks/reused_layer_mixed_precision_test.py @@ -43,13 +43,9 @@ def get_tpc(self): name="reused_layer_mp_test") def get_quantization_config(self): - return mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.MSE, - mct.core.QuantizationErrorMethod.MSE, - relu_bound_to_power_of_2=True, - weights_bias_correction=True, - weights_per_channel_threshold=True, - input_scaling=True, - activation_channel_equalization=True) + return mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.MSE, mct.core.QuantizationErrorMethod.MSE, + relu_bound_to_power_of_2=True, weights_bias_correction=True, + input_scaling=True, activation_channel_equalization=True) def get_mixed_precision_v2_config(self): return MixedPrecisionQuantizationConfigV2() diff --git a/tests/keras_tests/feature_networks_tests/feature_networks/scale_equalization_test.py b/tests/keras_tests/feature_networks_tests/feature_networks/scale_equalization_test.py index 22c5d88d7..605793938 100644 --- a/tests/keras_tests/feature_networks_tests/feature_networks/scale_equalization_test.py +++ b/tests/keras_tests/feature_networks_tests/feature_networks/scale_equalization_test.py @@ -55,8 +55,8 @@ def get_tpc(self): def get_quantization_config(self): return mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.MSE, mct.core.QuantizationErrorMethod.MSE, - relu_bound_to_power_of_2=False, weights_bias_correction=False, - weights_per_channel_threshold=True, activation_channel_equalization=True) + relu_bound_to_power_of_2=False, weights_bias_correction=False, + activation_channel_equalization=True) def create_networks(self): inputs = layers.Input(shape=self.get_input_shapes()[0][1:]) diff --git a/tests/keras_tests/feature_networks_tests/feature_networks/shift_neg_activation_test.py b/tests/keras_tests/feature_networks_tests/feature_networks/shift_neg_activation_test.py index 5048dfe10..c6b175048 100644 --- a/tests/keras_tests/feature_networks_tests/feature_networks/shift_neg_activation_test.py +++ b/tests/keras_tests/feature_networks_tests/feature_networks/shift_neg_activation_test.py @@ -53,10 +53,8 @@ def get_tpc(self): def get_quantization_config(self): return mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.MSE, mct.core.QuantizationErrorMethod.MSE, - False, False, weights_per_channel_threshold=True, - shift_negative_activation_correction=True, - shift_negative_ratio=np.inf, - shift_negative_params_search=self.param_search) + False, False, shift_negative_activation_correction=True, + shift_negative_ratio=np.inf, shift_negative_params_search=self.param_search) def create_networks(self): inputs = layers.Input(shape=self.get_input_shapes()[0][1:]) diff --git a/tests/keras_tests/feature_networks_tests/feature_networks/split_conv_bug_test.py b/tests/keras_tests/feature_networks_tests/feature_networks/split_conv_bug_test.py index ae6554069..c4673c3dc 100644 --- a/tests/keras_tests/feature_networks_tests/feature_networks/split_conv_bug_test.py +++ b/tests/keras_tests/feature_networks_tests/feature_networks/split_conv_bug_test.py @@ -33,9 +33,8 @@ def get_tpc(self): return get_16bit_tpc("split_conv_bug_test") def get_quantization_config(self): - return mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.MSE, - mct.core.QuantizationErrorMethod.MSE, - True, True, True, input_scaling=True) + return mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.MSE, mct.core.QuantizationErrorMethod.MSE, + True, True, input_scaling=True) def get_input_shapes(self): return [[self.val_batch_size, 224, 224, 3]] diff --git a/tests/keras_tests/feature_networks_tests/feature_networks/test_kmeans_quantizer.py b/tests/keras_tests/feature_networks_tests/feature_networks/test_kmeans_quantizer.py index 2742b7de5..8e4160012 100644 --- a/tests/keras_tests/feature_networks_tests/feature_networks/test_kmeans_quantizer.py +++ b/tests/keras_tests/feature_networks_tests/feature_networks/test_kmeans_quantizer.py @@ -71,9 +71,8 @@ def get_tpc(self): return generate_keras_tpc(name="kmean_quantizer_test", tp_model=tp) def get_quantization_config(self): - return mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.MSE, - mct.core.QuantizationErrorMethod.MSE, - False, False, True) + return mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.MSE, mct.core.QuantizationErrorMethod.MSE, + False, False) def get_input_shapes(self): return [[self.val_batch_size, 16, 16, self.num_conv_channels]] @@ -144,9 +143,7 @@ def __init__(self, def get_quantization_config(self): return mct.core.QuantizationConfig(activation_error_method=mct.core.QuantizationErrorMethod.MSE, weights_error_method=mct.core.QuantizationErrorMethod.MSE, - relu_bound_to_power_of_2=False, - weights_bias_correction=False, - weights_per_channel_threshold=False) + relu_bound_to_power_of_2=False, weights_bias_correction=False) def compare(self, quantized_model, float_model, input_x=None, quantization_info=None): # check that the two conv's weights have different values since they where quantized diff --git a/tests/keras_tests/feature_networks_tests/feature_networks/weights_mixed_precision_tests.py b/tests/keras_tests/feature_networks_tests/feature_networks/weights_mixed_precision_tests.py index 2af6075d0..1dca5e6a4 100644 --- a/tests/keras_tests/feature_networks_tests/feature_networks/weights_mixed_precision_tests.py +++ b/tests/keras_tests/feature_networks_tests/feature_networks/weights_mixed_precision_tests.py @@ -37,13 +37,9 @@ def __init__(self, unit_test, val_batch_size=1): super().__init__(unit_test, val_batch_size=val_batch_size, experimental_exporter=True) def get_quantization_config(self): - return mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.MSE, - mct.core.QuantizationErrorMethod.MSE, - relu_bound_to_power_of_2=True, - weights_bias_correction=True, - weights_per_channel_threshold=True, - input_scaling=True, - activation_channel_equalization=True) + return mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.MSE, mct.core.QuantizationErrorMethod.MSE, + relu_bound_to_power_of_2=True, weights_bias_correction=True, + input_scaling=True, activation_channel_equalization=True) def get_mixed_precision_v2_config(self): return mct.core.MixedPrecisionQuantizationConfigV2(num_of_images=1) @@ -77,9 +73,8 @@ def get_tpc(self): def get_quantization_config(self): return mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.MSE, mct.core.QuantizationErrorMethod.MSE, - relu_bound_to_power_of_2=True, weights_bias_correction=True, - weights_per_channel_threshold=False, input_scaling=True, - activation_channel_equalization=True) + relu_bound_to_power_of_2=True, weights_bias_correction=True, + input_scaling=True, activation_channel_equalization=True) def get_mixed_precision_v2_config(self): return mct.core.MixedPrecisionQuantizationConfigV2() @@ -355,13 +350,9 @@ def get_tpc(self): name="mp_dw_test") def get_quantization_config(self): - return mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.MSE, - mct.core.QuantizationErrorMethod.MSE, - relu_bound_to_power_of_2=False, - weights_bias_correction=False, - weights_per_channel_threshold=True, - input_scaling=False, - activation_channel_equalization=False) + return mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.MSE, mct.core.QuantizationErrorMethod.MSE, + relu_bound_to_power_of_2=False, weights_bias_correction=False, + input_scaling=False, activation_channel_equalization=False) def get_mixed_precision_v2_config(self): return mct.core.MixedPrecisionQuantizationConfigV2() @@ -372,13 +363,9 @@ def __init__(self, unit_test): super().__init__(unit_test) def get_quantization_config(self): - return mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.MSE, - mct.core.QuantizationErrorMethod.MSE, - relu_bound_to_power_of_2=True, - weights_bias_correction=True, - weights_per_channel_threshold=True, - input_scaling=False, - activation_channel_equalization=False) + return mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.MSE, mct.core.QuantizationErrorMethod.MSE, + relu_bound_to_power_of_2=True, weights_bias_correction=True, + input_scaling=False, activation_channel_equalization=False) def get_mixed_precision_v2_config(self): return mct.core.MixedPrecisionQuantizationConfigV2(num_of_images=1) diff --git a/tests/keras_tests/function_tests/test_get_gptq_config.py b/tests/keras_tests/function_tests/test_get_gptq_config.py index 613bd714b..52512813e 100644 --- a/tests/keras_tests/function_tests/test_get_gptq_config.py +++ b/tests/keras_tests/function_tests/test_get_gptq_config.py @@ -63,8 +63,7 @@ def random_datagen_experimental(): class TestGetGPTQConfig(unittest.TestCase): def setUp(self): - self.qc = QuantizationConfig(QuantizationErrorMethod.MSE, - QuantizationErrorMethod.MSE, + self.qc = QuantizationConfig(QuantizationErrorMethod.MSE, QuantizationErrorMethod.MSE, weights_bias_correction=False) # disable bias correction when working with GPTQ self.cc = CoreConfig(quantization_config=self.qc) diff --git a/tests/keras_tests/function_tests/test_kl_error_quantization_configurations.py b/tests/keras_tests/function_tests/test_kl_error_quantization_configurations.py index 7d6ae92bf..33680befd 100644 --- a/tests/keras_tests/function_tests/test_kl_error_quantization_configurations.py +++ b/tests/keras_tests/function_tests/test_kl_error_quantization_configurations.py @@ -62,11 +62,8 @@ def representative_data_gen(): tpc = generate_keras_tpc(name="kl_quant_config_weights_test", tp_model=tp_model) qc = mct.core.QuantizationConfig(activation_error_method=mct.core.QuantizationErrorMethod.NOCLIPPING, - weights_error_method=error_method, - relu_bound_to_power_of_2=False, - weights_bias_correction=True, - weights_per_channel_threshold=per_channel, - input_scaling=False) + weights_error_method=error_method, relu_bound_to_power_of_2=False, + weights_bias_correction=True, input_scaling=False) core_config = mct.core.CoreConfig(quantization_config=qc) q_model, quantization_info = mct.ptq.keras_post_training_quantization_experimental(model, @@ -85,8 +82,8 @@ def representative_data_gen(): tpc = generate_keras_tpc(name="kl_quant_config_activation_test", tp_model=tp) qc = mct.core.QuantizationConfig(activation_error_method=error_method, - relu_bound_to_power_of_2=relu_bound_to_power_of_2, - shift_negative_activation_correction=False) + relu_bound_to_power_of_2=relu_bound_to_power_of_2, + shift_negative_activation_correction=False) core_config = mct.core.CoreConfig(quantization_config=qc) q_model, quantization_info = mct.ptq.keras_post_training_quantization_experimental(model, diff --git a/tests/keras_tests/function_tests/test_quantization_configurations.py b/tests/keras_tests/function_tests/test_quantization_configurations.py index 7e58c88fa..3e4980ebe 100644 --- a/tests/keras_tests/function_tests/test_quantization_configurations.py +++ b/tests/keras_tests/function_tests/test_quantization_configurations.py @@ -72,11 +72,8 @@ def representative_data_gen(): tpc = generate_keras_tpc(name="quant_config_weights_test", tp_model=tp) qc = mct.core.QuantizationConfig(activation_error_method=mct.core.QuantizationErrorMethod.NOCLIPPING, - weights_error_method=error_method, - relu_bound_to_power_of_2=False, - weights_bias_correction=bias_correction, - weights_per_channel_threshold=per_channel, - input_scaling=input_scaling) + weights_error_method=error_method, relu_bound_to_power_of_2=False, + weights_bias_correction=bias_correction, input_scaling=input_scaling) core_config = mct.core.CoreConfig(quantization_config=qc) q_model, quantization_info = mct.ptq.keras_post_training_quantization_experimental(model, representative_data_gen, @@ -93,11 +90,10 @@ def representative_data_gen(): tpc = generate_keras_tpc(name="quant_config_activation_test", tp_model=tp) qc = mct.core.QuantizationConfig(activation_error_method=error_method, - weights_error_method=mct.core.QuantizationErrorMethod.NOCLIPPING, - relu_bound_to_power_of_2=relu_bound_to_power_of_2, - weights_bias_correction=False, - weights_per_channel_threshold=False, - shift_negative_activation_correction=shift_negative_correction) + weights_error_method=mct.core.QuantizationErrorMethod.NOCLIPPING, + relu_bound_to_power_of_2=relu_bound_to_power_of_2, + weights_bias_correction=False, + shift_negative_activation_correction=shift_negative_correction) core_config = mct.core.CoreConfig(quantization_config=qc) q_model, quantization_info = mct.ptq.keras_post_training_quantization_experimental(model, diff --git a/tests/keras_tests/function_tests/test_symmetric_threshold_selection_weights.py b/tests/keras_tests/function_tests/test_symmetric_threshold_selection_weights.py index 66b5f451d..d6c40f1e2 100644 --- a/tests/keras_tests/function_tests/test_symmetric_threshold_selection_weights.py +++ b/tests/keras_tests/function_tests/test_symmetric_threshold_selection_weights.py @@ -94,8 +94,7 @@ def test_weights_symmetric_threshold_selection_kl(self): self.run_test_for_threshold_method(QuantizationErrorMethod.KL, per_channel=False) def run_test_for_threshold_method(self, threshold_method, per_channel=True): - qc = QuantizationConfig(weights_error_method=threshold_method, - weights_per_channel_threshold=per_channel) + qc = QuantizationConfig(weights_error_method=threshold_method) in_model = create_network() graph = prepare_graph_with_quantization_parameters(in_model, KerasImplementation(), DEFAULT_KERAS_INFO, diff --git a/tests/keras_tests/function_tests/test_uniform_range_selection_weights.py b/tests/keras_tests/function_tests/test_uniform_range_selection_weights.py index 4ad831129..4f7c044bb 100644 --- a/tests/keras_tests/function_tests/test_uniform_range_selection_weights.py +++ b/tests/keras_tests/function_tests/test_uniform_range_selection_weights.py @@ -93,8 +93,7 @@ def test_weights_uniform_range_selection_kl(self): self.run_test_for_threshold_method(QuantizationErrorMethod.KL, per_channel=False) def run_test_for_threshold_method(self, threshold_method, per_channel=True): - qc = QuantizationConfig(weights_error_method=threshold_method, - weights_per_channel_threshold=per_channel) + qc = QuantizationConfig(weights_error_method=threshold_method) in_model = create_network() graph = prepare_graph_with_quantization_parameters(in_model, KerasImplementation(), DEFAULT_KERAS_INFO, diff --git a/tests/keras_tests/models_tests/test_networks_runner.py b/tests/keras_tests/models_tests/test_networks_runner.py index e7791a55b..f39936fae 100644 --- a/tests/keras_tests/models_tests/test_networks_runner.py +++ b/tests/keras_tests/models_tests/test_networks_runner.py @@ -34,9 +34,8 @@ tp = mct.target_platform QUANTIZATION_CONFIG = mct.core.QuantizationConfig(activation_error_method=mct.core.QuantizationErrorMethod.MSE, - weights_error_method=mct.core.QuantizationErrorMethod.MSE, - relu_bound_to_power_of_2=False, weights_bias_correction=False, - weights_per_channel_threshold=True) + weights_error_method=mct.core.QuantizationErrorMethod.MSE, + relu_bound_to_power_of_2=False, weights_bias_correction=False) TWO_BIT_QUANTIZATION = generate_keras_tpc(name="two_bit_network_test", tp_model=generate_test_tp_model({'weights_n_bits': 2, diff --git a/tests/pytorch_tests/function_tests/get_gptq_config_test.py b/tests/pytorch_tests/function_tests/get_gptq_config_test.py index cea3c3efc..51686e944 100644 --- a/tests/pytorch_tests/function_tests/get_gptq_config_test.py +++ b/tests/pytorch_tests/function_tests/get_gptq_config_test.py @@ -65,8 +65,7 @@ def __init__(self, unit_test, quantization_method=QuantizationMethod.SYMMETRIC, self.quantization_parameters_learning = quantization_parameters_learning def run_test(self): - qc = QuantizationConfig(QuantizationErrorMethod.MSE, - QuantizationErrorMethod.MSE, + qc = QuantizationConfig(QuantizationErrorMethod.MSE, QuantizationErrorMethod.MSE, weights_bias_correction=False) # disable bias correction when working with GPTQ cc = CoreConfig(quantization_config=qc) diff --git a/tests/pytorch_tests/model_tests/base_pytorch_test.py b/tests/pytorch_tests/model_tests/base_pytorch_test.py index f70fa8085..927cc5b27 100644 --- a/tests/pytorch_tests/model_tests/base_pytorch_test.py +++ b/tests/pytorch_tests/model_tests/base_pytorch_test.py @@ -67,8 +67,7 @@ def get_tpc(self): def get_core_configs(self): base_quant_config = mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.NOCLIPPING, - mct.core.QuantizationErrorMethod.NOCLIPPING, - False, True, True) + mct.core.QuantizationErrorMethod.NOCLIPPING, False, True) base_core_config = mct.core.CoreConfig(quantization_config=base_quant_config, debug_config=self.get_debug_config()) return { diff --git a/tests/pytorch_tests/model_tests/feature_models/constant_conv_substitution_test.py b/tests/pytorch_tests/model_tests/feature_models/constant_conv_substitution_test.py index eb7201319..984d0d529 100644 --- a/tests/pytorch_tests/model_tests/feature_models/constant_conv_substitution_test.py +++ b/tests/pytorch_tests/model_tests/feature_models/constant_conv_substitution_test.py @@ -39,8 +39,7 @@ def get_tpc(self): def get_quantization_config(self): return mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.NOCLIPPING, - mct.core.QuantizationErrorMethod.NOCLIPPING, - False, False, True) + mct.core.QuantizationErrorMethod.NOCLIPPING, False, False) def compare(self, quantized_model, float_model, input_x=None, quantization_info=None): in_torch_tensor = to_torch_tensor(input_x[0]) diff --git a/tests/pytorch_tests/model_tests/feature_models/gptq_test.py b/tests/pytorch_tests/model_tests/feature_models/gptq_test.py index 9978d7cce..7d9946696 100644 --- a/tests/pytorch_tests/model_tests/feature_models/gptq_test.py +++ b/tests/pytorch_tests/model_tests/feature_models/gptq_test.py @@ -68,8 +68,7 @@ def __init__(self, unit_test, experimental_exporter=True, weights_bits=8, weight def get_quantization_config(self): return mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.NOCLIPPING, - mct.core.QuantizationErrorMethod.NOCLIPPING, - weights_per_channel_threshold=self.per_channel) + mct.core.QuantizationErrorMethod.NOCLIPPING) def create_networks(self): return TestModel() diff --git a/tests/pytorch_tests/model_tests/feature_models/linear_collapsing_test.py b/tests/pytorch_tests/model_tests/feature_models/linear_collapsing_test.py index 2b8b8e710..3bc0a6d66 100644 --- a/tests/pytorch_tests/model_tests/feature_models/linear_collapsing_test.py +++ b/tests/pytorch_tests/model_tests/feature_models/linear_collapsing_test.py @@ -39,8 +39,7 @@ def get_tpc(self): def get_quantization_config(self): return mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.NOCLIPPING, - mct.core.QuantizationErrorMethod.NOCLIPPING, - False, False, True) + mct.core.QuantizationErrorMethod.NOCLIPPING, False, False) def compare(self, quantized_model, float_model, input_x=None, quantization_info=None): in_torch_tensor = to_torch_tensor(input_x[0]) diff --git a/tests/pytorch_tests/model_tests/feature_models/lut_quantizer_test.py b/tests/pytorch_tests/model_tests/feature_models/lut_quantizer_test.py index 7c94c40af..05850102f 100644 --- a/tests/pytorch_tests/model_tests/feature_models/lut_quantizer_test.py +++ b/tests/pytorch_tests/model_tests/feature_models/lut_quantizer_test.py @@ -97,9 +97,9 @@ def get_core_configs(self): network_editor = [EditRule(filter=NodeNameFilter(self.node_to_change_name), action=ChangeCandidatesWeightsQuantizationMethod( weights_quantization_method=self.quant_method))] - return {'lut_quantizer_test': mct.core.CoreConfig(quantization_config=mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.MSE, - mct.core.QuantizationErrorMethod.MSE), - debug_config=mct.core.DebugConfig(network_editor=network_editor))} + return {'lut_quantizer_test': mct.core.CoreConfig(quantization_config=mct.core.QuantizationConfig( + mct.core.QuantizationErrorMethod.MSE, mct.core.QuantizationErrorMethod.MSE), + debug_config=mct.core.DebugConfig(network_editor=network_editor))} def create_inputs_shape(self): return [[self.val_batch_size, 3, 16, 16], [self.val_batch_size, 3, 16, 16]] diff --git a/tests/pytorch_tests/model_tests/feature_models/mixed_precision_activation_test.py b/tests/pytorch_tests/model_tests/feature_models/mixed_precision_activation_test.py index bbd1932cc..e3739bd8a 100644 --- a/tests/pytorch_tests/model_tests/feature_models/mixed_precision_activation_test.py +++ b/tests/pytorch_tests/model_tests/feature_models/mixed_precision_activation_test.py @@ -45,13 +45,9 @@ def get_tpc(self): tpc_name='mixed_precision_activation_pytorch_test') def get_core_configs(self): - qc = mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.MSE, - mct.core.QuantizationErrorMethod.MSE, - weights_bias_correction=True, - weights_per_channel_threshold=True, - activation_channel_equalization=False, - relu_bound_to_power_of_2=False, - input_scaling=False) + qc = mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.MSE, mct.core.QuantizationErrorMethod.MSE, + relu_bound_to_power_of_2=False, weights_bias_correction=True, + input_scaling=False, activation_channel_equalization=False) mpc = mct.core.MixedPrecisionQuantizationConfigV2(num_of_images=1) return {"mixed_precision_activation_model": mct.core.CoreConfig(quantization_config=qc, mixed_precision_config=mpc)} diff --git a/tests/pytorch_tests/model_tests/feature_models/mixed_precision_bops_test.py b/tests/pytorch_tests/model_tests/feature_models/mixed_precision_bops_test.py index 9e0279dbf..1a76f007c 100644 --- a/tests/pytorch_tests/model_tests/feature_models/mixed_precision_bops_test.py +++ b/tests/pytorch_tests/model_tests/feature_models/mixed_precision_bops_test.py @@ -113,13 +113,9 @@ def get_tpc(self): tpc_name='mixed_precision_bops_pytorch_test') def get_core_configs(self): - qc = mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.MSE, - mct.core.QuantizationErrorMethod.MSE, - weights_bias_correction=True, - weights_per_channel_threshold=True, - activation_channel_equalization=False, - relu_bound_to_power_of_2=False, - input_scaling=False) + qc = mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.MSE, mct.core.QuantizationErrorMethod.MSE, + relu_bound_to_power_of_2=False, weights_bias_correction=True, + input_scaling=False, activation_channel_equalization=False) mpc = MixedPrecisionQuantizationConfigV2(num_of_images=1) return {"mixed_precision_bops_model": mct.core.CoreConfig(quantization_config=qc, mixed_precision_config=mpc)} diff --git a/tests/pytorch_tests/model_tests/feature_models/mixed_precision_weights_test.py b/tests/pytorch_tests/model_tests/feature_models/mixed_precision_weights_test.py index 2cf42b5ee..df9c1187f 100644 --- a/tests/pytorch_tests/model_tests/feature_models/mixed_precision_weights_test.py +++ b/tests/pytorch_tests/model_tests/feature_models/mixed_precision_weights_test.py @@ -42,13 +42,9 @@ def get_tpc(self): ftp_name='mixed_precision_pytorch_test') def get_core_configs(self): - qc = mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.MSE, - mct.core.QuantizationErrorMethod.MSE, - weights_bias_correction=True, - weights_per_channel_threshold=True, - activation_channel_equalization=False, - relu_bound_to_power_of_2=False, - input_scaling=False) + qc = mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.MSE, mct.core.QuantizationErrorMethod.MSE, + relu_bound_to_power_of_2=False, weights_bias_correction=True, + input_scaling=False, activation_channel_equalization=False) mpc = mct.core.MixedPrecisionQuantizationConfigV2(num_of_images=1) return {"mixed_precision_model": mct.core.CoreConfig(quantization_config=qc, mixed_precision_config=mpc)} diff --git a/tests/pytorch_tests/model_tests/feature_models/old_api_test.py b/tests/pytorch_tests/model_tests/feature_models/old_api_test.py index 44ebc2b5c..1a1cdd2d6 100644 --- a/tests/pytorch_tests/model_tests/feature_models/old_api_test.py +++ b/tests/pytorch_tests/model_tests/feature_models/old_api_test.py @@ -64,13 +64,9 @@ def get_mp_tpc(self): ftp_name='mixed_precision_pytorch_test') def get_mp_quant_config(self): - qc = mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.MSE, - mct.core.QuantizationErrorMethod.MSE, - weights_bias_correction=True, - weights_per_channel_threshold=True, - activation_channel_equalization=False, - relu_bound_to_power_of_2=False, - input_scaling=False) + qc = mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.MSE, mct.core.QuantizationErrorMethod.MSE, + relu_bound_to_power_of_2=False, weights_bias_correction=True, + input_scaling=False, activation_channel_equalization=False) return mct.core.MixedPrecisionQuantizationConfig(qc, num_of_images=1) def get_kpi(self): diff --git a/tests/pytorch_tests/model_tests/feature_models/permute_substitution_test.py b/tests/pytorch_tests/model_tests/feature_models/permute_substitution_test.py index 48862c485..ebf6ed68b 100644 --- a/tests/pytorch_tests/model_tests/feature_models/permute_substitution_test.py +++ b/tests/pytorch_tests/model_tests/feature_models/permute_substitution_test.py @@ -38,8 +38,7 @@ def get_tpc(self): def get_quantization_config(self): return mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.NOCLIPPING, - mct.core.QuantizationErrorMethod.NOCLIPPING, - False, False, True) + mct.core.QuantizationErrorMethod.NOCLIPPING, False, False) def compare(self, quantized_model, float_model, input_x=None, quantization_info=None): in_torch_tensor = to_torch_tensor(input_x[0]) diff --git a/tests/pytorch_tests/model_tests/feature_models/relu_bound_test.py b/tests/pytorch_tests/model_tests/feature_models/relu_bound_test.py index ef007bc76..ef368d464 100644 --- a/tests/pytorch_tests/model_tests/feature_models/relu_bound_test.py +++ b/tests/pytorch_tests/model_tests/feature_models/relu_bound_test.py @@ -107,8 +107,7 @@ def get_tpc(self): ftp_name='relu_bound_pytorch_test') def get_core_configs(self): - quant_config = QuantizationConfig(QuantizationErrorMethod.MSE, - QuantizationErrorMethod.MSE, + quant_config = QuantizationConfig(QuantizationErrorMethod.MSE, QuantizationErrorMethod.MSE, relu_bound_to_power_of_2=True) return {"8bit_relu_bound": mct.core.CoreConfig(quantization_config=quant_config)} @@ -155,8 +154,7 @@ def get_tpc(self): ftp_name='relu_bound_pytorch_test') def get_core_configs(self): - quant_config = QuantizationConfig(QuantizationErrorMethod.MSE, - QuantizationErrorMethod.MSE, + quant_config = QuantizationConfig(QuantizationErrorMethod.MSE, QuantizationErrorMethod.MSE, relu_bound_to_power_of_2=True) return {"8bit_relu_bound": mct.core.CoreConfig(quantization_config=quant_config)} diff --git a/tests/pytorch_tests/model_tests/feature_models/residual_collapsing_test.py b/tests/pytorch_tests/model_tests/feature_models/residual_collapsing_test.py index 8c5d2c42a..94adb0d0b 100644 --- a/tests/pytorch_tests/model_tests/feature_models/residual_collapsing_test.py +++ b/tests/pytorch_tests/model_tests/feature_models/residual_collapsing_test.py @@ -39,8 +39,7 @@ def get_tpc(self): def get_quantization_config(self): return mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.NOCLIPPING, - mct.core.QuantizationErrorMethod.NOCLIPPING, - False, False, True) + mct.core.QuantizationErrorMethod.NOCLIPPING, False, False) def compare(self, quantized_model, float_model, input_x=None, quantization_info=None): in_torch_tensor = to_torch_tensor(input_x[0]) diff --git a/tests/pytorch_tests/model_tests/feature_models/shift_negative_activation_test.py b/tests/pytorch_tests/model_tests/feature_models/shift_negative_activation_test.py index bbb444eac..1f89fe638 100644 --- a/tests/pytorch_tests/model_tests/feature_models/shift_negative_activation_test.py +++ b/tests/pytorch_tests/model_tests/feature_models/shift_negative_activation_test.py @@ -66,8 +66,8 @@ def get_core_configs(self): quantization_config=mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.NOCLIPPING, mct.core.QuantizationErrorMethod.NOCLIPPING, shift_negative_activation_correction=True, - shift_negative_params_search=True, - shift_negative_ratio=np.inf)), + shift_negative_ratio=np.inf, + shift_negative_params_search=True)), } def create_inputs_shape(self): diff --git a/tests/pytorch_tests/model_tests/feature_models/symmetric_activation_test.py b/tests/pytorch_tests/model_tests/feature_models/symmetric_activation_test.py index 765fe4844..3ecf16b1f 100644 --- a/tests/pytorch_tests/model_tests/feature_models/symmetric_activation_test.py +++ b/tests/pytorch_tests/model_tests/feature_models/symmetric_activation_test.py @@ -54,9 +54,8 @@ def get_tpc(self): def get_core_configs(self): qc = mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.NOCLIPPING, - mct.core.QuantizationErrorMethod.NOCLIPPING, - shift_negative_activation_correction=True, - shift_negative_ratio=np.inf) + mct.core.QuantizationErrorMethod.NOCLIPPING, + shift_negative_activation_correction=True, shift_negative_ratio=np.inf) return {'act_8bit': mct.core.CoreConfig(quantization_config=qc)} def create_feature_network(self, input_shape): diff --git a/tests/pytorch_tests/model_tests/feature_models/uniform_activation_test.py b/tests/pytorch_tests/model_tests/feature_models/uniform_activation_test.py index 75426dede..d391bf154 100644 --- a/tests/pytorch_tests/model_tests/feature_models/uniform_activation_test.py +++ b/tests/pytorch_tests/model_tests/feature_models/uniform_activation_test.py @@ -40,9 +40,8 @@ def get_tpc(self): def get_core_configs(self): qc = mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.NOCLIPPING, - mct.core.QuantizationErrorMethod.NOCLIPPING, - shift_negative_activation_correction=True, - shift_negative_ratio=np.inf) + mct.core.QuantizationErrorMethod.NOCLIPPING, + shift_negative_activation_correction=True, shift_negative_ratio=np.inf) return {'act_2bit': mct.core.CoreConfig(quantization_config=qc)} def create_feature_network(self, input_shape): diff --git a/tutorials/notebooks/keras/ptq/example_keras_imagenet.ipynb b/tutorials/notebooks/keras/ptq/example_keras_imagenet.ipynb index 50bb0b83e..802371863 100644 --- a/tutorials/notebooks/keras/ptq/example_keras_imagenet.ipynb +++ b/tutorials/notebooks/keras/ptq/example_keras_imagenet.ipynb @@ -340,13 +340,11 @@ "\n", "# Set the following quantization configurations:\n", "# Choose the desired QuantizationErrorMethod for the quantization parameters search.\n", - "# Enable per-channel weights quantization.\n", "# Enable weights bias correction induced by quantization.\n", "# Enable shift negative corrections for improving 'signed' non-linear functions quantization (such as swish, prelu, etc.) \n", "# Set the threshold to filter outliers with z_score of 16. \n", "q_config = mct.QuantizationConfig(activation_error_method=QuantizationErrorMethod.MSE,\n", " weights_error_method=QuantizationErrorMethod.MSE,\n", - " weights_per_channel_threshold=True,\n", " weights_bias_correction=True,\n", " shift_negative_activation_correction=True,\n", " z_threshold=16)\n", diff --git a/tutorials/quick_start/keras_fw/quant.py b/tutorials/quick_start/keras_fw/quant.py index 6a6d9e520..7195f1300 100644 --- a/tutorials/quick_start/keras_fw/quant.py +++ b/tutorials/quick_start/keras_fw/quant.py @@ -95,11 +95,13 @@ def quantize(model: tf.keras.Model, mp_wcr = args.get(MP_WEIGHTS_COMPRESSION, None) if mp_wcr: mp_conf = MixedPrecisionQuantizationConfigV2() - core_conf = CoreConfig(quantization_config=mct.core.QuantizationConfig(shift_negative_activation_correction=True), + core_conf = CoreConfig(quantization_config=mct.core.QuantizationConfig( + shift_negative_activation_correction=True), mixed_precision_config=mp_conf) target_kpi = get_target_kpi(model, mp_wcr, representative_data_gen, core_conf, tpc) else: - core_conf = CoreConfig(quantization_config=mct.core.QuantizationConfig(shift_negative_activation_correction=True)) + core_conf = CoreConfig(quantization_config=mct.core.QuantizationConfig( + shift_negative_activation_correction=True)) target_kpi = None # Quantize model diff --git a/tutorials/quick_start/pytorch_fw/quant.py b/tutorials/quick_start/pytorch_fw/quant.py index 0d91d8b30..1dd66b6e2 100644 --- a/tutorials/quick_start/pytorch_fw/quant.py +++ b/tutorials/quick_start/pytorch_fw/quant.py @@ -97,11 +97,13 @@ def quantize(model: nn.Module, mp_wcr = args.get(MP_WEIGHTS_COMPRESSION, None) if mp_wcr: mp_conf = MixedPrecisionQuantizationConfigV2() - core_conf = CoreConfig(quantization_config=mct.core.QuantizationConfig(shift_negative_activation_correction=True), + core_conf = CoreConfig(quantization_config=mct.core.QuantizationConfig( + shift_negative_activation_correction=True), mixed_precision_config=mp_conf) target_kpi = get_target_kpi(model, mp_wcr, representative_data_gen, core_conf, tpc) else: - core_conf = CoreConfig(quantization_config=mct.core.QuantizationConfig(shift_negative_activation_correction=True)) + core_conf = CoreConfig(quantization_config=mct.core.QuantizationConfig( + shift_negative_activation_correction=True)) target_kpi = None # Quantize model