From 683d95aed53c17affd44774d014eb261b8258da9 Mon Sep 17 00:00:00 2001 From: Ofir Gordon Date: Wed, 1 Jan 2025 18:30:35 +0200 Subject: [PATCH] fix tests --- .../get_target_platform_capabilities.py | 2 +- .../tpc_models/imx500_tpc/v1/tp_model.py | 1 - .../tflite_int8/imx500_int8_tp_model.py | 56 +------------------ .../tflite_int8/networks/conv2d_test.py | 7 +-- .../tflite_int8_exporter_base_test.py | 3 +- .../mixed_precision_activation_test.py | 2 +- .../model_tests/feature_models/qat_test.py | 3 +- tests/pytorch_tests/tpc_pytorch.py | 33 +---------- 8 files changed, 14 insertions(+), 93 deletions(-) diff --git a/model_compression_toolkit/target_platform_capabilities/tpc_models/get_target_platform_capabilities.py b/model_compression_toolkit/target_platform_capabilities/tpc_models/get_target_platform_capabilities.py index 75c454f6c..4a9a8a9b7 100644 --- a/model_compression_toolkit/target_platform_capabilities/tpc_models/get_target_platform_capabilities.py +++ b/model_compression_toolkit/target_platform_capabilities/tpc_models/get_target_platform_capabilities.py @@ -45,7 +45,7 @@ def get_target_platform_capabilities(fw_name: str, if target_platform_name == DEFAULT_TP_MODEL: return get_tp_model_imx500_v1() - assert target_platform_version == 'v1', \ + assert target_platform_version == 'v1' or target_platform_version is None, \ "The usage of get_target_platform_capabilities API is supported only with the default TPC ('v1')." if target_platform_name == IMX500_TP_MODEL: diff --git a/model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v1/tp_model.py b/model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v1/tp_model.py index 6bf19fadb..37c1a41ce 100644 --- a/model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v1/tp_model.py +++ b/model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v1/tp_model.py @@ -178,7 +178,6 @@ def generate_tp_model(default_config: OpQuantizationConfig, operator_set.append(schema.OperatorsSet(name=schema.OperatorSetNames.OPSET_GET_ITEM.value, qc_options=no_quantization_config)) operator_set.append(schema.OperatorsSet(name=schema.OperatorSetNames.OPSET_RESHAPE.value, qc_options=no_quantization_config)) operator_set.append(schema.OperatorsSet(name=schema.OperatorSetNames.OPSET_UNSQUEEZE.value, qc_options=no_quantization_config)) - operator_set.append(schema.OperatorsSet(name=schema.OperatorSetNames.OPSET_BATCH_NORM.value, qc_options=no_quantization_config)) operator_set.append(schema.OperatorsSet(name=schema.OperatorSetNames.OPSET_SIZE.value, qc_options=no_quantization_config)) operator_set.append(schema.OperatorsSet(name=schema.OperatorSetNames.OPSET_PERMUTE.value, qc_options=no_quantization_config)) operator_set.append(schema.OperatorsSet(name=schema.OperatorSetNames.OPSET_TRANSPOSE.value, qc_options=no_quantization_config)) diff --git a/tests/keras_tests/exporter_tests/tflite_int8/imx500_int8_tp_model.py b/tests/keras_tests/exporter_tests/tflite_int8/imx500_int8_tp_model.py index e40185c5e..46e612bec 100644 --- a/tests/keras_tests/exporter_tests/tflite_int8/imx500_int8_tp_model.py +++ b/tests/keras_tests/exporter_tests/tflite_int8/imx500_int8_tp_model.py @@ -34,6 +34,7 @@ import model_compression_toolkit as mct from model_compression_toolkit.target_platform_capabilities.schema.mct_current_schema import TargetPlatformModel, OpQuantizationConfig +from tests.common_tests.helpers.tpcs_for_tests.v1.tp_model import generate_tp_model tp = mct.target_platform @@ -62,60 +63,9 @@ def get_op_quantization_configs() -> Tuple[OpQuantizationConfig, List[OpQuantiza return eight_bits, mixed_precision_cfg_list, default_config -def generate_tp_model(default_config: OpQuantizationConfig, - base_config: OpQuantizationConfig, - mixed_precision_cfg_list: List[OpQuantizationConfig], - name: str) -> TargetPlatformModel: - default_configuration_options = schema.QuantizationConfigOptions(quantization_configurations=tuple( - [default_config])) - - mixed_precision_configuration_options = schema.QuantizationConfigOptions(quantization_configurations=tuple(mixed_precision_cfg_list), - base_config=base_config) - - operator_set, fusing_patterns = [], [] - - operator_set.append(schema.OperatorsSet(name="NoQuantization", - qc_options=default_configuration_options - .clone_and_edit(enable_activation_quantization=False) - .clone_and_edit_weight_attribute(enable_weights_quantization=False))) - - conv = schema.OperatorsSet(name="Conv", qc_options=mixed_precision_configuration_options) - fc = schema.OperatorsSet(name="FullyConnected", qc_options=mixed_precision_configuration_options) - - any_relu = schema.OperatorsSet(name="AnyReLU") - add = schema.OperatorsSet(name="Add") - sub = schema.OperatorsSet(name="Sub") - mul = schema.OperatorsSet(name="Mul") - div = schema.OperatorsSet(name="Div") - prelu = schema.OperatorsSet(name="PReLU") - swish = schema.OperatorsSet(name="Swish") - sigmoid = schema.OperatorsSet(name="Sigmoid") - tanh = schema.OperatorsSet(name="Tanh") - - operator_set.extend([conv, fc, any_relu, add, sub, mul, div, prelu, swish, sigmoid, tanh]) - - activations_after_conv_to_fuse = schema.OperatorSetConcat(operators_set=(any_relu, swish, prelu, sigmoid, tanh)) - activations_after_fc_to_fuse = schema.OperatorSetConcat(operators_set=(any_relu, swish, sigmoid)) - any_binary = schema.OperatorSetConcat(operators_set=(add, sub, mul, div)) - - fusing_patterns.append(schema.Fusing(operator_groups=(conv, activations_after_conv_to_fuse))) - fusing_patterns.append(schema.Fusing(operator_groups=(fc, activations_after_fc_to_fuse))) - fusing_patterns.append(schema.Fusing(operator_groups=(any_binary, any_relu))) - - generated_tpc = schema.TargetPlatformModel( - default_qco=default_configuration_options, - tpc_minor_version=None, - tpc_patch_version=None, - tpc_platform_type=None, - operator_set=tuple(operator_set), - fusing_patterns=tuple(fusing_patterns), - add_metadata=False, name=name) - return generated_tpc - - -def get_int8_tpc(edit_weights_params_dict={}, edit_act_params_dict={}) -> tp.TargetPlatformCapabilities: +def get_int8_tpc(edit_weights_params_dict={}, edit_act_params_dict={}) -> tp.TargetPlatformModel: default_tp_model = get_tp_model(edit_weights_params_dict, edit_act_params_dict) - return generate_keras_tpc(name='int8_tpc', tp_model=default_tp_model) + return default_tp_model def generate_keras_tpc(name: str, tp_model: schema.TargetPlatformModel): diff --git a/tests/keras_tests/exporter_tests/tflite_int8/networks/conv2d_test.py b/tests/keras_tests/exporter_tests/tflite_int8/networks/conv2d_test.py index 4b78382de..318149786 100644 --- a/tests/keras_tests/exporter_tests/tflite_int8/networks/conv2d_test.py +++ b/tests/keras_tests/exporter_tests/tflite_int8/networks/conv2d_test.py @@ -18,8 +18,7 @@ import tests.keras_tests.exporter_tests.constants as constants from model_compression_toolkit.core.keras.constants import KERNEL from model_compression_toolkit.target_platform_capabilities.target_platform import QuantizationMethod -from tests.common_tests.helpers.tpcs_for_tests.v1_pot.tp_model import get_tp_model as get_tp_model_pot -from tests.common_tests.helpers.tpcs_for_tests.v1.tp_model import get_tp_model as get_tp_model_symmetric +from tests.keras_tests.exporter_tests.tflite_int8.imx500_int8_tp_model import get_int8_tpc from tests.keras_tests.exporter_tests.tflite_int8.tflite_int8_exporter_base_test import TFLiteINT8ExporterBaseTest from tests.keras_tests.utils import get_layers_from_model_by_type @@ -35,7 +34,7 @@ def get_model(self): return self.get_one_layer_model(layers.Conv2D(6, 5)) def get_tpc(self): - return get_tp_model_symmetric() + return get_int8_tpc(edit_weights_params_dict={'weights_quantization_method': QuantizationMethod.SYMMETRIC}) def run_checks(self): # Fetch quantized weights from int8 model tensors @@ -69,7 +68,7 @@ def __init__(self): self.weights_diff_tolerance = 0 def get_tpc(self): - return get_tp_model_pot() + return get_int8_tpc(edit_weights_params_dict={'weights_quantization_method': QuantizationMethod.POWER_OF_TWO}) def run_checks(self): super(TestConv2DPOTTFLiteINT8Exporter, self).run_checks() diff --git a/tests/keras_tests/exporter_tests/tflite_int8/tflite_int8_exporter_base_test.py b/tests/keras_tests/exporter_tests/tflite_int8/tflite_int8_exporter_base_test.py index ec9f08a81..f148b2fbd 100644 --- a/tests/keras_tests/exporter_tests/tflite_int8/tflite_int8_exporter_base_test.py +++ b/tests/keras_tests/exporter_tests/tflite_int8/tflite_int8_exporter_base_test.py @@ -24,7 +24,6 @@ import model_compression_toolkit as mct import tests.keras_tests.exporter_tests.constants as constants -from tests.common_tests.helpers.tpcs_for_tests.v1.tp_model import get_tp_model from tests.keras_tests.exporter_tests.tflite_int8.imx500_int8_tp_model import get_int8_tpc @@ -74,7 +73,7 @@ def get_input_shape(self): return [(16, 16, 3)] def get_tpc(self): - return get_tp_model() + return get_int8_tpc() def __get_repr_dataset(self): for _ in range(1): 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 e302c9f46..76bcf8ee8 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 @@ -153,7 +153,7 @@ def get_tpc(self): mp_bitwidth_candidates_list=[(8, 8), (8, 4), (8, 2), (4, 8), (4, 4), (4, 2), (2, 8), (2, 4), (2, 2)], - custom_opsets=['Concat']), + custom_opsets=['Concat']), test_name='mixed_precision_activation_model', tpc_name='mixed_precision_activation_pytorch_test') diff --git a/tests/pytorch_tests/model_tests/feature_models/qat_test.py b/tests/pytorch_tests/model_tests/feature_models/qat_test.py index ecf1310bb..3a34b402f 100644 --- a/tests/pytorch_tests/model_tests/feature_models/qat_test.py +++ b/tests/pytorch_tests/model_tests/feature_models/qat_test.py @@ -316,7 +316,8 @@ def get_tpc(self): def run_test(self): self._gen_fixed_input() model_float = self.create_networks() - config = mct.core.CoreConfig(mct.core.QuantizationConfig(shift_negative_activation_correction=False)) + config = mct.core.CoreConfig(mct.core.QuantizationConfig(shift_negative_activation_correction=False, + custom_tpc_opset_to_layer={"Input": ([DummyPlaceHolder],)})) ru = mct.core.ResourceUtilization(weights_memory=50, activation_memory=40) qat_ready_model, quantization_info = mct.qat.pytorch_quantization_aware_training_init_experimental(model_float, self.representative_data_gen_experimental, diff --git a/tests/pytorch_tests/tpc_pytorch.py b/tests/pytorch_tests/tpc_pytorch.py index 8b5d232a7..055a1cffe 100644 --- a/tests/pytorch_tests/tpc_pytorch.py +++ b/tests/pytorch_tests/tpc_pytorch.py @@ -33,33 +33,6 @@ def get_weights_quantization_disabled_pytorch_tpc(name): return get_pytorch_test_tpc_dict(tp, name, name) -def get_mp_activation_pytorch_tpc_dict(tpc_model, test_name, tpc_name, custom_opsets_to_layer={}): - # TODO: this is a light implementation of the get_mp_activation_pytorch_tpc_dict function. - # the full implementation needs to be adjusted and reactivated once implementing custom layer opset support in TPC. - # it still should return a TP Model (!) but use custom opset to operators mapping to allow MCT construct the desired TPC. - - return {test_name: tpc_model} - -# def get_mp_activation_pytorch_tpc_dict(tpc_model, test_name, tpc_name, custom_opsets_to_layer={}): -# op_sets_to_layer_add = { -# "Input": [DummyPlaceHolder], -# } -# -# op_sets_to_layer_add.update(custom_opsets_to_layer) -# -# # we assume a standard tp model with standard operator sets names, -# # otherwise - need to generate the tpc per test and not with this generic function -# attr_mapping = {'Conv': {KERNEL_ATTR: DefaultDict(default_value=PYTORCH_KERNEL), -# BIAS_ATTR: DefaultDict(default_value=BIAS)}, -# 'FullyConnected': {KERNEL_ATTR: DefaultDict(default_value=PYTORCH_KERNEL), -# BIAS_ATTR: DefaultDict(default_value=BIAS)}} -# -# attach2pytorch = AttachTpcToPytorch() -# tpc = attach2pytorch.attach(tpc_model) -# return { -# test_name: generate_test_tpc(name=tpc_name, -# tp_model=tpc_model, -# base_tpc=tpc, -# op_sets_to_layer_add=op_sets_to_layer_add, -# attr_mapping=attr_mapping), -# } +def get_mp_activation_pytorch_tpc_dict(tpc_model, test_name, tpc_name): + # This is a legacy helper function that is kept for maintaining tests usability + return {test_name: tpc_model} \ No newline at end of file