Skip to content

Commit

Permalink
Remove folder "target_platform"
Browse files Browse the repository at this point in the history
  • Loading branch information
liord committed Jan 13, 2025
1 parent aefa143 commit 05e1eaa
Show file tree
Hide file tree
Showing 18 changed files with 67 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import model_compression_toolkit as mct
import model_compression_toolkit.target_platform_capabilities.schema.mct_current_schema as schema
from mct_quantizers import QuantizationMethod
from model_compression_toolkit.constants import FLOAT_BITWIDTH
from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, BIAS_ATTR, QNNPACK_TP_MODEL
from model_compression_toolkit.target_platform_capabilities.schema.mct_current_schema import TargetPlatformCapabilities, \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import model_compression_toolkit as mct
import model_compression_toolkit.target_platform_capabilities.schema.mct_current_schema as schema
from mct_quantizers import QuantizationMethod
from model_compression_toolkit.constants import FLOAT_BITWIDTH
from model_compression_toolkit.target_platform_capabilities.constants import BIAS_ATTR, KERNEL_ATTR, TFLITE_TP_MODEL
from model_compression_toolkit.target_platform_capabilities.schema.mct_current_schema import TargetPlatformCapabilities, Signedness, \
Expand Down
1 change: 1 addition & 0 deletions tests/common_tests/helpers/tpcs_for_tests/v2/tpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import model_compression_toolkit as mct
import model_compression_toolkit.target_platform_capabilities.schema.mct_current_schema as schema
from mct_quantizers import QuantizationMethod
from model_compression_toolkit.constants import FLOAT_BITWIDTH
from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, BIAS_ATTR, WEIGHTS_N_BITS, \
IMX500_TP_MODEL
Expand Down
1 change: 1 addition & 0 deletions tests/common_tests/helpers/tpcs_for_tests/v3/tpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import model_compression_toolkit as mct
import model_compression_toolkit.target_platform_capabilities.schema.mct_current_schema as schema
from mct_quantizers import QuantizationMethod
from model_compression_toolkit.constants import FLOAT_BITWIDTH
from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, BIAS_ATTR, WEIGHTS_N_BITS, \
IMX500_TP_MODEL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
from tests.keras_tests.feature_networks_tests.base_keras_feature_test import BaseKerasFeatureNetworkTest
from tests.common_tests.helpers.tensors_compare import cosine_similarity
from tests.keras_tests.utils import get_layers_from_model_by_type
from mct_quantizers import KerasQuantizationWrapper

from mct_quantizers import KerasQuantizationWrapper, QuantizationMethod

keras = tf.keras
layers = keras.layers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import tensorflow as tf

import model_compression_toolkit as mct
from mct_quantizers import KerasActivationQuantizationHolder
from mct_quantizers import KerasActivationQuantizationHolder, QuantizationMethod
from model_compression_toolkit.core.common.network_editors.actions import EditRule, \
ChangeCandidatesWeightsQuantizationMethod
from model_compression_toolkit.core.common.network_editors.node_filters import NodeNameFilter
Expand Down Expand Up @@ -68,7 +68,7 @@ def get_debug_config(self):
network_editor=[EditRule(filter=NodeNameFilter(self.node_to_change_name),
action=ChangeCandidatesWeightsQuantizationMethod(
weights_quantization_method=
mct.QuantizationMethod.POWER_OF_TWO,
QuantizationMethod.POWER_OF_TWO,
attr_name=KERNEL))])

def get_input_shapes(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
import tensorflow as tf

import model_compression_toolkit as mct
from mct_quantizers import QuantizationTarget, KerasActivationQuantizationHolder, KerasQuantizationWrapper
from mct_quantizers import QuantizationTarget, KerasActivationQuantizationHolder, KerasQuantizationWrapper, \
QuantizationMethod
from mct_quantizers.common.base_inferable_quantizer import QuantizerID
from mct_quantizers.common.get_all_subclasses import get_all_subclasses
from mct_quantizers.keras.quantizers import BaseKerasInferableQuantizer
Expand All @@ -45,8 +46,8 @@

class QuantizationAwareTrainingTest(BaseKerasFeatureNetworkTest):
def __init__(self, unit_test, layer, weight_bits=2, activation_bits=4, finalize=False,
weights_quantization_method=mct.QuantizationMethod.POWER_OF_TWO,
activation_quantization_method=mct.QuantizationMethod.POWER_OF_TWO,
weights_quantization_method=QuantizationMethod.POWER_OF_TWO,
activation_quantization_method=QuantizationMethod.POWER_OF_TWO,
test_loading=False):
self.layer = layer
self.weight_bits = weight_bits
Expand Down Expand Up @@ -163,8 +164,8 @@ def compare(self, quantized_model, float_model, loaded_model, input_x=None, quan

class QATWrappersTest(BaseKerasFeatureNetworkTest):
def __init__(self, unit_test, layer, weight_bits=2, activation_bits=4, finalize=True,
weights_quantization_method=mct.QuantizationMethod.POWER_OF_TWO,
activation_quantization_method=mct.QuantizationMethod.POWER_OF_TWO,
weights_quantization_method=QuantizationMethod.POWER_OF_TWO,
activation_quantization_method=QuantizationMethod.POWER_OF_TWO,
training_method=TrainingMethod.STE,
per_channel=True,
test_loading=False):
Expand Down
1 change: 1 addition & 0 deletions tests/keras_tests/function_tests/test_custom_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import model_compression_toolkit as mct
import model_compression_toolkit.target_platform_capabilities.schema.mct_current_schema as schema
from mct_quantizers import QuantizationMethod
from model_compression_toolkit.core import CoreConfig, QuantizationConfig
from model_compression_toolkit.target_platform_capabilities.schema.mct_current_schema import Signedness
from model_compression_toolkit.target_platform_capabilities.constants import BIAS_ATTR, KERNEL_ATTR
Expand Down
17 changes: 9 additions & 8 deletions tests/keras_tests/function_tests/test_hmse_error_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import model_compression_toolkit as mct
import model_compression_toolkit.target_platform_capabilities.schema.mct_current_schema as schema
from mct_quantizers import QuantizationMethod
from model_compression_toolkit import DefaultDict
from model_compression_toolkit.core import QuantizationConfig
from model_compression_toolkit.constants import THRESHOLD, RANGE_MAX, NUM_QPARAM_HESSIAN_SAMPLES
Expand Down Expand Up @@ -136,44 +137,44 @@ def _run_node_verification(node_type):
_run_node_verification(layers.Dense)

def test_pot_threshold_selection_hmse_per_channel(self):
self._setup_with_args(quant_method=mct.QuantizationMethod.POWER_OF_TWO, per_channel=True)
self._setup_with_args(quant_method=QuantizationMethod.POWER_OF_TWO, per_channel=True)
calculate_quantization_params(self.graph, fw_impl=self.keras_impl, repr_data_gen_fn=representative_dataset,
hessian_info_service=self.his, num_hessian_samples=1)
self._verify_params_calculation_execution(THRESHOLD)

def test_pot_threshold_selection_hmse_per_tensor(self):
self._setup_with_args(quant_method=mct.QuantizationMethod.POWER_OF_TWO, per_channel=False)
self._setup_with_args(quant_method=QuantizationMethod.POWER_OF_TWO, per_channel=False)
calculate_quantization_params(self.graph, fw_impl=self.keras_impl, repr_data_gen_fn=representative_dataset,
hessian_info_service=self.his, num_hessian_samples=1)
self._verify_params_calculation_execution(THRESHOLD)

def test_symmetric_threshold_selection_hmse_per_channel(self):
self._setup_with_args(quant_method=mct.QuantizationMethod.SYMMETRIC, per_channel=True)
self._setup_with_args(quant_method=QuantizationMethod.SYMMETRIC, per_channel=True)
calculate_quantization_params(self.graph, fw_impl=self.keras_impl, repr_data_gen_fn=representative_dataset,
hessian_info_service=self.his, num_hessian_samples=1)
self._verify_params_calculation_execution(THRESHOLD)

def test_symmetric_threshold_selection_hmse_per_tensor(self):
self._setup_with_args(quant_method=mct.QuantizationMethod.SYMMETRIC, per_channel=False)
self._setup_with_args(quant_method=QuantizationMethod.SYMMETRIC, per_channel=False)
calculate_quantization_params(self.graph, fw_impl=self.keras_impl, repr_data_gen_fn=representative_dataset,
hessian_info_service=self.his, num_hessian_samples=1)
self._verify_params_calculation_execution(THRESHOLD)

def test_usniform_threshold_selection_hmse_per_channel(self):
self._setup_with_args(quant_method=mct.QuantizationMethod.UNIFORM, per_channel=True)
self._setup_with_args(quant_method=QuantizationMethod.UNIFORM, per_channel=True)
calculate_quantization_params(self.graph, fw_impl=self.keras_impl, repr_data_gen_fn=representative_dataset,
hessian_info_service=self.his, num_hessian_samples=1)
self._verify_params_calculation_execution(RANGE_MAX)

def test_uniform_threshold_selection_hmse_per_tensor(self):
self._setup_with_args(quant_method=mct.QuantizationMethod.UNIFORM, per_channel=False)
self._setup_with_args(quant_method=QuantizationMethod.UNIFORM, per_channel=False)
calculate_quantization_params(self.graph, fw_impl=self.keras_impl, repr_data_gen_fn=representative_dataset,
hessian_info_service=self.his, num_hessian_samples=1)
self._verify_params_calculation_execution(RANGE_MAX)

def test_threshold_selection_hmse_no_gptq(self):
with self.assertRaises(ValueError) as e:
self._setup_with_args(quant_method=mct.QuantizationMethod.SYMMETRIC, per_channel=True,
self._setup_with_args(quant_method=QuantizationMethod.SYMMETRIC, per_channel=True,
running_gptq=False)
self.assertTrue('The HMSE error method for parameters selection is only supported when running GPTQ '
'optimization due to long execution time that is not suitable for basic PTQ.' in
Expand All @@ -200,7 +201,7 @@ def _generate_bn_quantization_tpc(quant_method, per_channel):

return tpc

self._setup_with_args(quant_method=mct.QuantizationMethod.SYMMETRIC, per_channel=True,
self._setup_with_args(quant_method=QuantizationMethod.SYMMETRIC, per_channel=True,
tpc_fn=_generate_bn_quantization_tpc, model_gen_fn=no_bn_fusion_model_gen)
calculate_quantization_params(self.graph, fw_impl=self.keras_impl, repr_data_gen_fn=representative_dataset,
hessian_info_service=self.his, num_hessian_samples=1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
from mct_quantizers import QuantizationMethod
from model_compression_toolkit.target_platform_capabilities.tpc_models.imx500_tpc.latest import generate_keras_tpc
from model_compression_toolkit.core.keras.default_framework_info import DEFAULT_KERAS_INFO
import unittest
Expand All @@ -38,9 +39,9 @@ def test_run_quantization_config_mbv1(self):
def representative_data_gen():
yield [x]

quantizer_methods = [mct.QuantizationMethod.POWER_OF_TWO,
mct.QuantizationMethod.SYMMETRIC,
mct.QuantizationMethod.UNIFORM]
quantizer_methods = [QuantizationMethod.POWER_OF_TWO,
QuantizationMethod.SYMMETRIC,
QuantizationMethod.UNIFORM]

quantization_error_methods = [mct.core.QuantizationErrorMethod.KL]
relu_bound_to_power_of_2 = [True, False]
Expand Down
21 changes: 11 additions & 10 deletions tests/keras_tests/function_tests/test_layer_fusing.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from model_compression_toolkit.core.keras.default_framework_info import DEFAULT_KERAS_INFO
from model_compression_toolkit.core.keras.keras_implementation import KerasImplementation
from model_compression_toolkit.core.common.quantization.quantization_config import CustomOpsetLayers
from model_compression_toolkit.target_platform_capabilities.targetplatform2framework import LayerFilterParams
from model_compression_toolkit.target_platform_capabilities.targetplatform2framework.attach2keras import \
AttachTpcToKeras
from model_compression_toolkit.target_platform_capabilities.tpc_models.imx500_tpc.latest import \
Expand Down Expand Up @@ -199,14 +200,14 @@ def test_layer_fusing_2(self):

qc = QuantizationConfig(custom_tpc_opset_to_layer={"Conv": CustomOpsetLayers([Conv2D]),
"AnyReLU": CustomOpsetLayers([tf.nn.relu,
tp.LayerFilterParams(ReLU, negative_slope=0.0),
tp.LayerFilterParams(Activation,
LayerFilterParams(ReLU, negative_slope=0.0),
LayerFilterParams(Activation,
activation="relu")]),
"Swish": CustomOpsetLayers([tf.nn.swish, tp.LayerFilterParams(Activation,
"Swish": CustomOpsetLayers([tf.nn.swish, LayerFilterParams(Activation,
activation="swish")]),
"Sigmoid": CustomOpsetLayers([tf.nn.sigmoid, tp.LayerFilterParams(Activation,
"Sigmoid": CustomOpsetLayers([tf.nn.sigmoid, LayerFilterParams(Activation,
activation="sigmoid")]),
"Tanh": CustomOpsetLayers([tf.nn.tanh, tp.LayerFilterParams(Activation,
"Tanh": CustomOpsetLayers([tf.nn.tanh, LayerFilterParams(Activation,
activation="tanh")])})

fusion_graph = prepare_graph_with_configs(model, KerasImplementation(), DEFAULT_KERAS_INFO,
Expand All @@ -221,8 +222,8 @@ def test_layer_fusing_3(self):

qc = QuantizationConfig(custom_tpc_opset_to_layer={"Conv": CustomOpsetLayers([Conv2D]),
"AnyReLU": CustomOpsetLayers([tf.nn.relu,
tp.LayerFilterParams(ReLU, negative_slope=0.0),
tp.LayerFilterParams(Activation,
LayerFilterParams(ReLU, negative_slope=0.0),
LayerFilterParams(Activation,
activation="relu")])})

fusion_graph = prepare_graph_with_configs(model, KerasImplementation(), DEFAULT_KERAS_INFO,
Expand All @@ -240,11 +241,11 @@ def test_layer_fusing_4(self):
"Conv": CustomOpsetLayers([Conv2D]),
"FullyConnected": CustomOpsetLayers([Dense]),
"AnyReLU": CustomOpsetLayers([tf.nn.relu,
tp.LayerFilterParams(ReLU, negative_slope=0.0),
tp.LayerFilterParams(Activation,
LayerFilterParams(ReLU, negative_slope=0.0),
LayerFilterParams(Activation,
activation="relu")]),
"Add": CustomOpsetLayers([tf.add, Add]),
"Swish": CustomOpsetLayers([tf.nn.swish, tp.LayerFilterParams(Activation, activation="swish")]),
"Swish": CustomOpsetLayers([tf.nn.swish, LayerFilterParams(Activation, activation="swish")]),
})

fusion_graph = prepare_graph_with_configs(model, KerasImplementation(), DEFAULT_KERAS_INFO,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from tensorflow.keras import layers

import model_compression_toolkit as mct
from mct_quantizers import QuantizationMethod
from model_compression_toolkit.target_platform_capabilities.tpc_models.imx500_tpc.latest import generate_keras_tpc
from model_compression_toolkit.core.keras.default_framework_info import DEFAULT_KERAS_INFO
from tests.common_tests.helpers.generate_test_tpc import generate_test_tpc
Expand All @@ -41,9 +42,9 @@ def test_run_quantization_config(self):
def representative_data_gen():
yield [x]

quantizer_methods = [mct.QuantizationMethod.POWER_OF_TWO,
mct.QuantizationMethod.SYMMETRIC,
mct.QuantizationMethod.UNIFORM]
quantizer_methods = [QuantizationMethod.POWER_OF_TWO,
QuantizationMethod.SYMMETRIC,
QuantizationMethod.UNIFORM]

quantization_error_methods = [mct.core.QuantizationErrorMethod.MSE,
mct.core.QuantizationErrorMethod.NOCLIPPING,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from keras.layers import Conv2D, Conv2DTranspose

import model_compression_toolkit as mct
from mct_quantizers import QuantizationMethod
from model_compression_toolkit.core import QuantizationConfig, QuantizationErrorMethod
from model_compression_toolkit.constants import THRESHOLD
from model_compression_toolkit.core.keras.constants import KERNEL
Expand Down Expand Up @@ -58,7 +59,7 @@ def representative_dataset():

def get_tpc(per_channel):
tp = generate_test_tpc(edit_params_dict={
'weights_quantization_method': mct.QuantizationMethod.SYMMETRIC,
'weights_quantization_method': QuantizationMethod.SYMMETRIC,
'weights_per_channel_threshold': per_channel})
tpc = generate_keras_tpc(name="symmetric_threshold_selection_test", tpc=tp)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from keras.layers import Conv2D, Conv2DTranspose

import model_compression_toolkit as mct
from mct_quantizers import QuantizationMethod
from model_compression_toolkit.core import QuantizationConfig, QuantizationErrorMethod
from model_compression_toolkit.constants import RANGE_MIN, RANGE_MAX
from model_compression_toolkit.core.keras.constants import KERNEL
Expand Down Expand Up @@ -57,7 +58,7 @@ def representative_dataset():

def get_tpc(per_channel):
tp = generate_test_tpc({
'weights_quantization_method': mct.QuantizationMethod.UNIFORM,
'weights_quantization_method': QuantizationMethod.UNIFORM,
'weights_per_channel_threshold': per_channel})
tpc = generate_keras_tpc(name="uniform_range_selection_test", tpc=tp)

Expand Down
5 changes: 3 additions & 2 deletions tests/keras_tests/tpc_keras.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from packaging import version
import tensorflow as tf

from mct_quantizers import QuantizationMethod
from model_compression_toolkit.defaultdict import DefaultDict
from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, KERAS_KERNEL, BIAS_ATTR, BIAS, \
KERAS_DEPTHWISE_KERNEL
Expand All @@ -35,8 +36,8 @@


def get_tpc(name, weight_bits=8, activation_bits=8,
weights_quantization_method=mct.QuantizationMethod.POWER_OF_TWO,
activation_quantization_method=mct.QuantizationMethod.POWER_OF_TWO,
weights_quantization_method=QuantizationMethod.POWER_OF_TWO,
activation_quantization_method=QuantizationMethod.POWER_OF_TWO,
per_channel=True):
tpc = generate_test_tpc({'weights_n_bits': weight_bits,
'activation_n_bits': activation_bits,
Expand Down
Loading

0 comments on commit 05e1eaa

Please sign in to comment.