mct-nightly 2.0.0.20240410.422__py3-none-any.whl → 2.0.0.20240412.408__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.
Files changed (36) hide show
  1. {mct_nightly-2.0.0.20240410.422.dist-info → mct_nightly-2.0.0.20240412.408.dist-info}/METADATA +2 -2
  2. {mct_nightly-2.0.0.20240410.422.dist-info → mct_nightly-2.0.0.20240412.408.dist-info}/RECORD +36 -27
  3. model_compression_toolkit/__init__.py +1 -1
  4. model_compression_toolkit/constants.py +4 -0
  5. model_compression_toolkit/core/common/graph/base_graph.py +3 -2
  6. model_compression_toolkit/core/common/quantization/quantization_params_generation/qparams_computation.py +2 -2
  7. model_compression_toolkit/exporter/model_exporter/keras/fakely_quant_keras_exporter.py +6 -1
  8. model_compression_toolkit/exporter/model_exporter/keras/fakely_quant_tflite_exporter.py +5 -0
  9. model_compression_toolkit/exporter/model_exporter/keras/int8_tflite_exporter.py +5 -0
  10. model_compression_toolkit/exporter/model_exporter/pytorch/fakely_quant_onnx_pytorch_exporter.py +29 -11
  11. model_compression_toolkit/exporter/model_exporter/pytorch/pytorch_export_facade.py +1 -1
  12. model_compression_toolkit/exporter/model_wrapper/keras/builder/fully_quantized_model_builder.py +1 -1
  13. model_compression_toolkit/exporter/model_wrapper/pytorch/builder/fully_quantized_model_builder.py +2 -2
  14. model_compression_toolkit/gptq/keras/gptq_training.py +17 -15
  15. model_compression_toolkit/gptq/keras/quantization_facade.py +6 -1
  16. model_compression_toolkit/gptq/keras/quantizer/regularization_factory.py +2 -1
  17. model_compression_toolkit/gptq/pytorch/gptq_training.py +18 -16
  18. model_compression_toolkit/gptq/pytorch/quantization_facade.py +6 -1
  19. model_compression_toolkit/gptq/pytorch/quantizer/regularization_factory.py +2 -1
  20. model_compression_toolkit/metadata.py +29 -0
  21. model_compression_toolkit/ptq/keras/quantization_facade.py +6 -2
  22. model_compression_toolkit/ptq/pytorch/quantization_facade.py +6 -1
  23. model_compression_toolkit/target_platform_capabilities/target_platform/target_platform_model.py +4 -1
  24. model_compression_toolkit/target_platform_capabilities/target_platform/targetplatform2framework/target_platform_capabilities.py +1 -0
  25. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/target_platform_capabilities.py +12 -2
  26. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v2/__init__.py +16 -0
  27. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v2/tp_model.py +210 -0
  28. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v2/tpc_keras.py +129 -0
  29. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v2/tpc_pytorch.py +111 -0
  30. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v2_lut/__init__.py +16 -0
  31. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v2_lut/tp_model.py +207 -0
  32. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v2_lut/tpc_keras.py +129 -0
  33. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v2_lut/tpc_pytorch.py +110 -0
  34. {mct_nightly-2.0.0.20240410.422.dist-info → mct_nightly-2.0.0.20240412.408.dist-info}/LICENSE.md +0 -0
  35. {mct_nightly-2.0.0.20240410.422.dist-info → mct_nightly-2.0.0.20240412.408.dist-info}/WHEEL +0 -0
  36. {mct_nightly-2.0.0.20240410.422.dist-info → mct_nightly-2.0.0.20240412.408.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,29 @@
1
+ # Copyright 2024 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
+ from typing import Dict
17
+ from model_compression_toolkit.constants import MCT_VERSION, TPC_VERSION
18
+
19
+
20
+ def get_versions_dict(tpc) -> Dict:
21
+ """
22
+
23
+ Returns: A dictionary with TPC and MCT versions.
24
+
25
+ """
26
+ # imported inside to avoid circular import error
27
+ from model_compression_toolkit import __version__ as mct_version
28
+ tpc_version = f'{tpc.name}.{tpc.version}'
29
+ return {MCT_VERSION: mct_version, TPC_VERSION: tpc_version}
@@ -28,6 +28,7 @@ from model_compression_toolkit.core.common.mixed_precision.mixed_precision_quant
28
28
  from model_compression_toolkit.target_platform_capabilities.target_platform.targetplatform2framework import TargetPlatformCapabilities
29
29
  from model_compression_toolkit.core.runner import core_runner
30
30
  from model_compression_toolkit.ptq.runner import ptq_runner
31
+ from model_compression_toolkit.metadata import get_versions_dict
31
32
 
32
33
  if FOUND_TF:
33
34
  from model_compression_toolkit.core.keras.default_framework_info import DEFAULT_KERAS_INFO
@@ -38,6 +39,7 @@ if FOUND_TF:
38
39
  from model_compression_toolkit.exporter.model_wrapper import get_exportable_keras_model
39
40
 
40
41
  from model_compression_toolkit import get_target_platform_capabilities
42
+ from mct_quantizers.keras.metadata import add_metadata
41
43
  DEFAULT_KERAS_TPC = get_target_platform_capabilities(TENSORFLOW, DEFAULT_TP_MODEL)
42
44
 
43
45
 
@@ -164,8 +166,10 @@ if FOUND_TF:
164
166
  fw_impl,
165
167
  fw_info)
166
168
 
167
- return get_exportable_keras_model(graph_with_stats_correction)
168
-
169
+ exportable_model, user_info = get_exportable_keras_model(graph_with_stats_correction)
170
+ if target_platform_capabilities.tp_model.add_metadata:
171
+ exportable_model = add_metadata(exportable_model, get_versions_dict(target_platform_capabilities))
172
+ return exportable_model, user_info
169
173
 
170
174
 
171
175
  else:
@@ -29,6 +29,7 @@ from model_compression_toolkit.core.runner import core_runner
29
29
  from model_compression_toolkit.ptq.runner import ptq_runner
30
30
  from model_compression_toolkit.core.analyzer import analyzer_model_quantization
31
31
  from model_compression_toolkit.core.common.quantization.quantize_graph_weights import quantize_graph_weights
32
+ from model_compression_toolkit.metadata import get_versions_dict
32
33
 
33
34
 
34
35
  if FOUND_TORCH:
@@ -38,6 +39,7 @@ if FOUND_TORCH:
38
39
  from torch.nn import Module
39
40
  from model_compression_toolkit.exporter.model_wrapper.pytorch.builder.fully_quantized_model_builder import get_exportable_pytorch_model
40
41
  from model_compression_toolkit import get_target_platform_capabilities
42
+ from mct_quantizers.pytorch.metadata import add_metadata
41
43
 
42
44
  DEFAULT_PYTORCH_TPC = get_target_platform_capabilities(PYTORCH, DEFAULT_TP_MODEL)
43
45
 
@@ -139,7 +141,10 @@ if FOUND_TORCH:
139
141
  fw_impl,
140
142
  fw_info)
141
143
 
142
- return get_exportable_pytorch_model(graph_with_stats_correction)
144
+ exportable_model, user_info = get_exportable_pytorch_model(graph_with_stats_correction)
145
+ if target_platform_capabilities.tp_model.add_metadata:
146
+ exportable_model = add_metadata(exportable_model, get_versions_dict(target_platform_capabilities))
147
+ return exportable_model, user_info
143
148
 
144
149
 
145
150
  else:
@@ -60,15 +60,18 @@ class TargetPlatformModel(ImmutableClass):
60
60
 
61
61
  def __init__(self,
62
62
  default_qco: QuantizationConfigOptions,
63
+ add_metadata: bool = False,
63
64
  name="default_tp_model"):
64
65
  """
65
66
 
66
67
  Args:
67
68
  default_qco (QuantizationConfigOptions): Default QuantizationConfigOptions to use for operators that their QuantizationConfigOptions are not defined in the model.
69
+ add_metadata (bool): Whether to add metadata to the model or not.
68
70
  name (str): Name of the model.
69
71
  """
70
72
 
71
73
  super().__init__()
74
+ self.add_metadata = add_metadata
72
75
  self.name = name
73
76
  self.operator_set = []
74
77
  assert isinstance(default_qco, QuantizationConfigOptions)
@@ -191,7 +194,7 @@ class TargetPlatformModel(ImmutableClass):
191
194
 
192
195
  """
193
196
  opsets_names = [op.name for op in self.operator_set]
194
- if (len(set(opsets_names)) != len(opsets_names)):
197
+ if len(set(opsets_names)) != len(opsets_names):
195
198
  Logger.critical(f'Operator Sets must have unique names.')
196
199
 
197
200
  def get_default_config(self) -> OpQuantizationConfig:
@@ -29,6 +29,7 @@ from model_compression_toolkit.target_platform_capabilities.target_platform.op_q
29
29
  from model_compression_toolkit.target_platform_capabilities.target_platform.operators import OperatorsSetBase
30
30
  from model_compression_toolkit.target_platform_capabilities.target_platform.target_platform_model import TargetPlatformModel
31
31
  from model_compression_toolkit.target_platform_capabilities.target_platform.targetplatform2framework.current_tpc import _current_tpc
32
+ from model_compression_toolkit.constants import MCT_VERSION, TPC_VERSION
32
33
 
33
34
 
34
35
  class TargetPlatformCapabilities(ImmutableClass):
@@ -25,11 +25,15 @@ if FOUND_TF:
25
25
  from model_compression_toolkit.target_platform_capabilities.tpc_models.imx500_tpc.v1.tpc_keras import get_keras_tpc as get_keras_tpc_v1
26
26
  from model_compression_toolkit.target_platform_capabilities.tpc_models.imx500_tpc.v1_lut.tpc_keras import get_keras_tpc as get_keras_tpc_v1_lut
27
27
  from model_compression_toolkit.target_platform_capabilities.tpc_models.imx500_tpc.v1_pot.tpc_keras import get_keras_tpc as get_keras_tpc_v1_pot
28
+ from model_compression_toolkit.target_platform_capabilities.tpc_models.imx500_tpc.v2.tpc_keras import get_keras_tpc as get_keras_tpc_v2
29
+ from model_compression_toolkit.target_platform_capabilities.tpc_models.imx500_tpc.v2_lut.tpc_keras import get_keras_tpc as get_keras_tpc_v2_lut
28
30
 
29
31
  # Keras: TPC versioning
30
32
  keras_tpc_models_dict = {'v1': get_keras_tpc_v1(),
31
33
  'v1_lut': get_keras_tpc_v1_lut(),
32
34
  'v1_pot': get_keras_tpc_v1_pot(),
35
+ 'v2': get_keras_tpc_v2(),
36
+ 'v2_lut': get_keras_tpc_v2_lut(),
33
37
  LATEST: get_keras_tpc_latest()}
34
38
 
35
39
  ###############################
@@ -42,13 +46,19 @@ if FOUND_TORCH:
42
46
  get_pytorch_tpc as get_pytorch_tpc_v1
43
47
  from model_compression_toolkit.target_platform_capabilities.tpc_models.imx500_tpc.v1_pot.tpc_pytorch import \
44
48
  get_pytorch_tpc as get_pytorch_tpc_v1_pot
45
- from model_compression_toolkit.target_platform_capabilities.tpc_models.imx500_tpc.v1_lut.tpc_pytorch import get_pytorch_tpc as get_pytorch_tpc_v1_lut
46
-
49
+ from model_compression_toolkit.target_platform_capabilities.tpc_models.imx500_tpc.v1_lut.tpc_pytorch import \
50
+ get_pytorch_tpc as get_pytorch_tpc_v1_lut
51
+ from model_compression_toolkit.target_platform_capabilities.tpc_models.imx500_tpc.v2.tpc_pytorch import \
52
+ get_pytorch_tpc as get_pytorch_tpc_v2
53
+ from model_compression_toolkit.target_platform_capabilities.tpc_models.imx500_tpc.v2_lut.tpc_pytorch import \
54
+ get_pytorch_tpc as get_pytorch_tpc_v2_lut
47
55
 
48
56
  # Pytorch: TPC versioning
49
57
  pytorch_tpc_models_dict = {'v1': get_pytorch_tpc_v1(),
50
58
  'v1_lut': get_pytorch_tpc_v1_lut(),
51
59
  'v1_pot': get_pytorch_tpc_v1_pot(),
60
+ 'v2': get_pytorch_tpc_v2(),
61
+ 'v2_lut': get_pytorch_tpc_v2_lut(),
52
62
  LATEST: get_pytorch_tpc_latest()}
53
63
 
54
64
  tpc_dict = {TENSORFLOW: keras_tpc_models_dict,
@@ -0,0 +1,16 @@
1
+ # Copyright 2024 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
+ __version__ = 'v2'
@@ -0,0 +1,210 @@
1
+ # Copyright 2024 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, Tuple
16
+
17
+ import model_compression_toolkit as mct
18
+ from model_compression_toolkit.constants import FLOAT_BITWIDTH
19
+ from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, BIAS_ATTR, WEIGHTS_N_BITS
20
+ from model_compression_toolkit.target_platform_capabilities.target_platform import OpQuantizationConfig, \
21
+ TargetPlatformModel
22
+ from model_compression_toolkit.target_platform_capabilities.target_platform.op_quantization_config import \
23
+ AttributeQuantizationConfig
24
+
25
+ tp = mct.target_platform
26
+
27
+
28
+ def get_tp_model() -> TargetPlatformModel:
29
+ """
30
+ A method that generates a default target platform model, with base 8-bit quantization configuration and 8, 4, 2
31
+ bits configuration list for mixed-precision quantization.
32
+ NOTE: in order to generate a target platform model with different configurations but with the same Operators Sets
33
+ (for tests, experiments, etc.), use this method implementation as a test-case, i.e., override the
34
+ 'get_op_quantization_configs' method and use its output to call 'generate_tp_model' with your configurations.
35
+ This version enables metadata by default
36
+
37
+ Returns: A TargetPlatformModel object.
38
+
39
+ """
40
+ base_config, mixed_precision_cfg_list, default_config = get_op_quantization_configs()
41
+ return generate_tp_model(default_config=default_config,
42
+ base_config=base_config,
43
+ mixed_precision_cfg_list=mixed_precision_cfg_list,
44
+ name='imx500_tp_model')
45
+
46
+
47
+ def get_op_quantization_configs() -> Tuple[OpQuantizationConfig, List[OpQuantizationConfig], OpQuantizationConfig]:
48
+ """
49
+ Creates a default configuration object for 8-bit quantization, to be used to set a default TargetPlatformModel.
50
+ In addition, creates a default configuration objects list (with 8, 4 and 2 bit quantization) to be used as
51
+ default configuration for mixed-precision quantization.
52
+
53
+ Returns: An OpQuantizationConfig config object and a list of OpQuantizationConfig objects.
54
+
55
+ """
56
+
57
+ # TODO: currently, we don't want to quantize any attribute but the kernel by default,
58
+ # to preserve the current behavior of MCT, so quantization is disabled for all other attributes.
59
+ # Other quantization parameters are set to what we eventually want to quantize by default
60
+ # when we enable multi-attributes quantization - THIS NEED TO BE MODIFIED IN ALL TP MODELS!
61
+
62
+ # define a default quantization config for all non-specified weights attributes.
63
+ default_weight_attr_config = AttributeQuantizationConfig(
64
+ weights_quantization_method=tp.QuantizationMethod.POWER_OF_TWO,
65
+ weights_n_bits=8,
66
+ weights_per_channel_threshold=False,
67
+ enable_weights_quantization=False, # TODO: this will changed to True once implementing multi-attributes quantization
68
+ lut_values_bitwidth=None)
69
+
70
+ # define a quantization config to quantize the kernel (for layers where there is a kernel attribute).
71
+ kernel_base_config = AttributeQuantizationConfig(
72
+ weights_quantization_method=tp.QuantizationMethod.SYMMETRIC,
73
+ weights_n_bits=8,
74
+ weights_per_channel_threshold=True,
75
+ enable_weights_quantization=True,
76
+ lut_values_bitwidth=None)
77
+
78
+ # define a quantization config to quantize the bias (for layers where there is a bias attribute).
79
+ bias_config = AttributeQuantizationConfig(
80
+ weights_quantization_method=tp.QuantizationMethod.POWER_OF_TWO,
81
+ weights_n_bits=FLOAT_BITWIDTH,
82
+ weights_per_channel_threshold=False,
83
+ enable_weights_quantization=False,
84
+ lut_values_bitwidth=None)
85
+
86
+ # Create a quantization config.
87
+ # A quantization configuration defines how an operator
88
+ # should be quantized on the modeled hardware:
89
+
90
+ # We define a default config for operation without kernel attribute.
91
+ # This is the default config that should be used for non-linear operations.
92
+ eight_bits_default = tp.OpQuantizationConfig(
93
+ default_weight_attr_config=default_weight_attr_config,
94
+ attr_weights_configs_mapping={},
95
+ activation_quantization_method=tp.QuantizationMethod.POWER_OF_TWO,
96
+ activation_n_bits=8,
97
+ enable_activation_quantization=True,
98
+ quantization_preserving=False,
99
+ fixed_scale=None,
100
+ fixed_zero_point=None,
101
+ simd_size=32)
102
+
103
+ # We define an 8-bit config for linear operations quantization, that include a kernel and bias attributes.
104
+ linear_eight_bits = tp.OpQuantizationConfig(
105
+ default_weight_attr_config=default_weight_attr_config,
106
+ attr_weights_configs_mapping={KERNEL_ATTR: kernel_base_config, BIAS_ATTR: bias_config},
107
+ activation_quantization_method=tp.QuantizationMethod.POWER_OF_TWO,
108
+ activation_n_bits=8,
109
+ enable_activation_quantization=True,
110
+ quantization_preserving=False,
111
+ fixed_scale=None,
112
+ fixed_zero_point=None,
113
+ simd_size=32)
114
+
115
+ # To quantize a model using mixed-precision, create
116
+ # a list with more than one OpQuantizationConfig.
117
+ # In this example, we quantize some operations' weights
118
+ # using 2, 4 or 8 bits, and when using 2 or 4 bits, it's possible
119
+ # to quantize the operations' activations using LUT.
120
+ four_bits = linear_eight_bits.clone_and_edit(attr_to_edit={KERNEL_ATTR: {WEIGHTS_N_BITS: 4}},
121
+ simd_size=linear_eight_bits.simd_size * 2)
122
+ two_bits = linear_eight_bits.clone_and_edit(attr_to_edit={KERNEL_ATTR: {WEIGHTS_N_BITS: 2}},
123
+ simd_size=linear_eight_bits.simd_size * 4)
124
+
125
+ mixed_precision_cfg_list = [linear_eight_bits, four_bits, two_bits]
126
+
127
+ return linear_eight_bits, mixed_precision_cfg_list, eight_bits_default
128
+
129
+
130
+ def generate_tp_model(default_config: OpQuantizationConfig,
131
+ base_config: OpQuantizationConfig,
132
+ mixed_precision_cfg_list: List[OpQuantizationConfig],
133
+ name: str) -> TargetPlatformModel:
134
+ """
135
+ Generates TargetPlatformModel with default defined Operators Sets, based on the given base configuration and
136
+ mixed-precision configurations options list.
137
+
138
+ Args
139
+ default_config: A default OpQuantizationConfig to set as the TP model default configuration.
140
+ base_config: An OpQuantizationConfig to set as the TargetPlatformModel base configuration for mixed-precision purposes only.
141
+ mixed_precision_cfg_list: A list of OpQuantizationConfig to be used as the TP model mixed-precision
142
+ quantization configuration options.
143
+ name: The name of the TargetPlatformModel.
144
+
145
+ Returns: A TargetPlatformModel object.
146
+
147
+ """
148
+ # Create a QuantizationConfigOptions, which defines a set
149
+ # of possible configurations to consider when quantizing a set of operations (in mixed-precision, for example).
150
+ # If the QuantizationConfigOptions contains only one configuration,
151
+ # this configuration will be used for the operation quantization:
152
+ default_configuration_options = tp.QuantizationConfigOptions([default_config])
153
+
154
+ # Create a TargetPlatformModel and set its default quantization config.
155
+ # This default configuration will be used for all operations
156
+ # unless specified otherwise (see OperatorsSet, for example):
157
+ generated_tpm = tp.TargetPlatformModel(default_configuration_options, add_metadata=True, name=name)
158
+
159
+ # To start defining the model's components (such as operator sets, and fusing patterns),
160
+ # use 'with' the TargetPlatformModel instance, and create them as below:
161
+ with generated_tpm:
162
+ # Create an OperatorsSet to represent a set of operations.
163
+ # Each OperatorsSet has a unique label.
164
+ # If a quantization configuration options is passed, these options will
165
+ # be used for operations that will be attached to this set's label.
166
+ # Otherwise, it will be a configure-less set (used in fusing):
167
+
168
+ generated_tpm.set_simd_padding(is_simd_padding=True)
169
+
170
+ # May suit for operations like: Dropout, Reshape, etc.
171
+ default_qco = tp.get_default_quantization_config_options()
172
+ tp.OperatorsSet("NoQuantization",
173
+ default_qco.clone_and_edit(enable_activation_quantization=False)
174
+ .clone_and_edit_weight_attribute(enable_weights_quantization=False))
175
+
176
+ # Create Mixed-Precision quantization configuration options from the given list of OpQuantizationConfig objects
177
+ mixed_precision_configuration_options = tp.QuantizationConfigOptions(mixed_precision_cfg_list,
178
+ base_config=base_config)
179
+
180
+ # Define operator sets that use mixed_precision_configuration_options:
181
+ conv = tp.OperatorsSet("Conv", mixed_precision_configuration_options)
182
+ fc = tp.OperatorsSet("FullyConnected", mixed_precision_configuration_options)
183
+
184
+ # Define operations sets without quantization configuration
185
+ # options (useful for creating fusing patterns, for example):
186
+ any_relu = tp.OperatorsSet("AnyReLU")
187
+ add = tp.OperatorsSet("Add")
188
+ sub = tp.OperatorsSet("Sub")
189
+ mul = tp.OperatorsSet("Mul")
190
+ div = tp.OperatorsSet("Div")
191
+ prelu = tp.OperatorsSet("PReLU")
192
+ swish = tp.OperatorsSet("Swish")
193
+ sigmoid = tp.OperatorsSet("Sigmoid")
194
+ tanh = tp.OperatorsSet("Tanh")
195
+
196
+ # Combine multiple operators into a single operator to avoid quantization between
197
+ # them. To do this we define fusing patterns using the OperatorsSets that were created.
198
+ # To group multiple sets with regard to fusing, an OperatorSetConcat can be created
199
+ activations_after_conv_to_fuse = tp.OperatorSetConcat(any_relu, swish, prelu, sigmoid, tanh)
200
+ activations_after_fc_to_fuse = tp.OperatorSetConcat(any_relu, swish, sigmoid)
201
+ any_binary = tp.OperatorSetConcat(add, sub, mul, div)
202
+
203
+ # ------------------- #
204
+ # Fusions
205
+ # ------------------- #
206
+ tp.Fusing([conv, activations_after_conv_to_fuse])
207
+ tp.Fusing([fc, activations_after_fc_to_fuse])
208
+ tp.Fusing([any_binary, any_relu])
209
+
210
+ return generated_tpm
@@ -0,0 +1,129 @@
1
+ # Copyright 2024 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 tensorflow as tf
16
+ from packaging import version
17
+
18
+ from model_compression_toolkit.defaultdict import DefaultDict
19
+ from model_compression_toolkit.constants import FOUND_SONY_CUSTOM_LAYERS
20
+ from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, KERAS_DEPTHWISE_KERNEL, \
21
+ KERAS_KERNEL, BIAS_ATTR, BIAS
22
+
23
+ if FOUND_SONY_CUSTOM_LAYERS:
24
+ from sony_custom_layers.keras.object_detection.ssd_post_process import SSDPostProcess
25
+
26
+ if version.parse(tf.__version__) >= version.parse("2.13"):
27
+ from keras.src.layers import Conv2D, DepthwiseConv2D, Dense, Reshape, ZeroPadding2D, Dropout, \
28
+ MaxPooling2D, Activation, ReLU, Add, Subtract, Multiply, PReLU, Flatten, Cropping2D, LeakyReLU, Permute, \
29
+ Conv2DTranspose
30
+ else:
31
+ from keras.layers import Conv2D, DepthwiseConv2D, Dense, Reshape, ZeroPadding2D, Dropout, \
32
+ MaxPooling2D, Activation, ReLU, Add, Subtract, Multiply, PReLU, Flatten, Cropping2D, LeakyReLU, Permute, \
33
+ Conv2DTranspose
34
+
35
+ from model_compression_toolkit.target_platform_capabilities.tpc_models.imx500_tpc.v2.tp_model import get_tp_model
36
+ import model_compression_toolkit as mct
37
+ from model_compression_toolkit.target_platform_capabilities.tpc_models.imx500_tpc.v2 import __version__ as TPC_VERSION
38
+
39
+ tp = mct.target_platform
40
+
41
+
42
+ def get_keras_tpc() -> tp.TargetPlatformCapabilities:
43
+ """
44
+ get a Keras TargetPlatformCapabilities object with default operation sets to layers mapping.
45
+
46
+ Returns: a Keras TargetPlatformCapabilities object for the given TargetPlatformModel.
47
+ """
48
+ imx500_tpc_tp_model = get_tp_model()
49
+ return generate_keras_tpc(name='imx500_tpc_keras_tpc', tp_model=imx500_tpc_tp_model)
50
+
51
+
52
+ def generate_keras_tpc(name: str, tp_model: tp.TargetPlatformModel):
53
+ """
54
+ Generates a TargetPlatformCapabilities object with default operation sets to layers mapping.
55
+
56
+ Args:
57
+ name: Name of the TargetPlatformCapabilities.
58
+ tp_model: TargetPlatformModel object.
59
+
60
+ Returns: a TargetPlatformCapabilities object for the given TargetPlatformModel.
61
+ """
62
+
63
+ keras_tpc = tp.TargetPlatformCapabilities(tp_model, name=name, version=TPC_VERSION)
64
+
65
+ no_quant_list = [Reshape,
66
+ tf.reshape,
67
+ Permute,
68
+ tf.transpose,
69
+ Flatten,
70
+ Cropping2D,
71
+ ZeroPadding2D,
72
+ Dropout,
73
+ MaxPooling2D,
74
+ tf.split,
75
+ tf.quantization.fake_quant_with_min_max_vars,
76
+ tf.math.argmax,
77
+ tf.shape,
78
+ tf.math.equal,
79
+ tf.gather,
80
+ tf.cast,
81
+ tf.unstack,
82
+ tf.compat.v1.gather,
83
+ tf.nn.top_k,
84
+ tf.__operators__.getitem,
85
+ tf.image.combined_non_max_suppression,
86
+ tf.compat.v1.shape]
87
+
88
+ if FOUND_SONY_CUSTOM_LAYERS:
89
+ no_quant_list.append(SSDPostProcess)
90
+
91
+ with keras_tpc:
92
+ tp.OperationsSetToLayers("NoQuantization", no_quant_list)
93
+ tp.OperationsSetToLayers("Conv",
94
+ [Conv2D,
95
+ DepthwiseConv2D,
96
+ Conv2DTranspose,
97
+ tf.nn.conv2d,
98
+ tf.nn.depthwise_conv2d,
99
+ tf.nn.conv2d_transpose],
100
+ # we provide attributes mapping that maps each layer type in the operations set
101
+ # that has weights attributes with provided quantization config (in the tp model) to
102
+ # its framework-specific attribute name.
103
+ # note that a DefaultDict should be provided if not all the layer types in the
104
+ # operation set are provided separately in the mapping.
105
+ attr_mapping={
106
+ KERNEL_ATTR: DefaultDict({
107
+ DepthwiseConv2D: KERAS_DEPTHWISE_KERNEL,
108
+ tf.nn.depthwise_conv2d: KERAS_DEPTHWISE_KERNEL}, default_value=KERAS_KERNEL),
109
+ BIAS_ATTR: DefaultDict(default_value=BIAS)})
110
+ tp.OperationsSetToLayers("FullyConnected", [Dense],
111
+ attr_mapping={KERNEL_ATTR: DefaultDict(default_value=KERAS_KERNEL),
112
+ BIAS_ATTR: DefaultDict(default_value=BIAS)})
113
+ tp.OperationsSetToLayers("AnyReLU", [tf.nn.relu,
114
+ tf.nn.relu6,
115
+ tf.nn.leaky_relu,
116
+ ReLU,
117
+ LeakyReLU,
118
+ tp.LayerFilterParams(Activation, activation="relu"),
119
+ tp.LayerFilterParams(Activation, activation="leaky_relu")])
120
+ tp.OperationsSetToLayers("Add", [tf.add, Add])
121
+ tp.OperationsSetToLayers("Sub", [tf.subtract, Subtract])
122
+ tp.OperationsSetToLayers("Mul", [tf.math.multiply, Multiply])
123
+ tp.OperationsSetToLayers("Div", [tf.math.divide])
124
+ tp.OperationsSetToLayers("PReLU", [PReLU])
125
+ tp.OperationsSetToLayers("Swish", [tf.nn.swish, tp.LayerFilterParams(Activation, activation="swish")])
126
+ tp.OperationsSetToLayers("Sigmoid", [tf.nn.sigmoid, tp.LayerFilterParams(Activation, activation="sigmoid")])
127
+ tp.OperationsSetToLayers("Tanh", [tf.nn.tanh, tp.LayerFilterParams(Activation, activation="tanh")])
128
+
129
+ return keras_tpc
@@ -0,0 +1,111 @@
1
+ # Copyright 2024 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
+ import operator
17
+
18
+ import torch
19
+ from torch import add, sub, mul, div, flatten, reshape, split, unsqueeze, dropout, sigmoid, tanh, chunk, unbind, topk, \
20
+ gather, equal, transpose, permute, argmax, squeeze
21
+ from torch.nn import Conv2d, Linear, BatchNorm2d, ConvTranspose2d
22
+ from torch.nn import Dropout, Flatten, Hardtanh
23
+ from torch.nn import ReLU, ReLU6, PReLU, SiLU, Sigmoid, Tanh, Hardswish, LeakyReLU
24
+ from torch.nn.functional import relu, relu6, prelu, silu, hardtanh, hardswish, leaky_relu
25
+
26
+ from model_compression_toolkit.defaultdict import DefaultDict
27
+ from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, BIAS_ATTR, PYTORCH_KERNEL, \
28
+ BIAS
29
+ from model_compression_toolkit.target_platform_capabilities.tpc_models.imx500_tpc.v2.tp_model import get_tp_model
30
+ import model_compression_toolkit as mct
31
+ from model_compression_toolkit.target_platform_capabilities.tpc_models.imx500_tpc.v2 import __version__ as TPC_VERSION
32
+
33
+ tp = mct.target_platform
34
+
35
+
36
+ def get_pytorch_tpc() -> tp.TargetPlatformCapabilities:
37
+ """
38
+ get a Pytorch TargetPlatformCapabilities object with default operation sets to layers mapping.
39
+
40
+ Returns: a Pytorch TargetPlatformCapabilities object for the given TargetPlatformModel.
41
+ """
42
+ imx500_tpc_tp_model = get_tp_model()
43
+ return generate_pytorch_tpc(name='imx500_tpc_pytorch_tpc', tp_model=imx500_tpc_tp_model)
44
+
45
+
46
+ def generate_pytorch_tpc(name: str, tp_model: tp.TargetPlatformModel):
47
+ """
48
+ Generates a TargetPlatformCapabilities object with default operation sets to layers mapping.
49
+ Args:
50
+ name: Name of the TargetPlatformModel.
51
+ tp_model: TargetPlatformModel object.
52
+ Returns: a TargetPlatformCapabilities object for the given TargetPlatformModel.
53
+ """
54
+
55
+ pytorch_tpc = tp.TargetPlatformCapabilities(tp_model,
56
+ name=name,
57
+ version=TPC_VERSION)
58
+
59
+ # we provide attributes mapping that maps each layer type in the operations set
60
+ # that has weights attributes with provided quantization config (in the tp model) to
61
+ # its framework-specific attribute name.
62
+ # note that a DefaultDict should be provided if not all the layer types in the
63
+ # operation set are provided separately in the mapping.
64
+ pytorch_linear_attr_mapping = {KERNEL_ATTR: DefaultDict(default_value=PYTORCH_KERNEL),
65
+ BIAS_ATTR: DefaultDict(default_value=BIAS)}
66
+
67
+ with pytorch_tpc:
68
+ tp.OperationsSetToLayers("NoQuantization", [Dropout,
69
+ Flatten,
70
+ dropout,
71
+ flatten,
72
+ split,
73
+ operator.getitem,
74
+ reshape,
75
+ unsqueeze,
76
+ BatchNorm2d,
77
+ chunk,
78
+ unbind,
79
+ torch.Tensor.size,
80
+ permute,
81
+ transpose,
82
+ equal,
83
+ argmax,
84
+ gather,
85
+ topk,
86
+ squeeze])
87
+
88
+ tp.OperationsSetToLayers("Conv", [Conv2d, ConvTranspose2d],
89
+ attr_mapping=pytorch_linear_attr_mapping)
90
+ tp.OperationsSetToLayers("FullyConnected", [Linear],
91
+ attr_mapping=pytorch_linear_attr_mapping)
92
+ tp.OperationsSetToLayers("AnyReLU", [torch.relu,
93
+ ReLU,
94
+ ReLU6,
95
+ LeakyReLU,
96
+ relu,
97
+ relu6,
98
+ leaky_relu,
99
+ tp.LayerFilterParams(Hardtanh, min_val=0),
100
+ tp.LayerFilterParams(hardtanh, min_val=0)])
101
+
102
+ tp.OperationsSetToLayers("Add", [operator.add, add])
103
+ tp.OperationsSetToLayers("Sub", [operator.sub, sub])
104
+ tp.OperationsSetToLayers("Mul", [operator.mul, mul])
105
+ tp.OperationsSetToLayers("Div", [operator.truediv, div])
106
+ tp.OperationsSetToLayers("PReLU", [PReLU, prelu])
107
+ tp.OperationsSetToLayers("Swish", [SiLU, silu, Hardswish, hardswish])
108
+ tp.OperationsSetToLayers("Sigmoid", [Sigmoid, sigmoid])
109
+ tp.OperationsSetToLayers("Tanh", [Tanh, tanh])
110
+
111
+ return pytorch_tpc
@@ -0,0 +1,16 @@
1
+ # Copyright 2024 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
+ __version__ = 'v2_lut'