mct-nightly 2.2.0.20250106.546__py3-none-any.whl → 2.2.0.20250107.15510__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 (80) hide show
  1. {mct_nightly-2.2.0.20250106.546.dist-info → mct_nightly-2.2.0.20250107.15510.dist-info}/METADATA +1 -1
  2. {mct_nightly-2.2.0.20250106.546.dist-info → mct_nightly-2.2.0.20250107.15510.dist-info}/RECORD +43 -78
  3. model_compression_toolkit/__init__.py +1 -1
  4. model_compression_toolkit/core/__init__.py +1 -1
  5. model_compression_toolkit/core/common/graph/memory_graph/compute_graph_max_cut.py +1 -1
  6. model_compression_toolkit/core/common/graph/memory_graph/cut.py +5 -2
  7. model_compression_toolkit/core/common/graph/memory_graph/max_cut_astar.py +25 -25
  8. model_compression_toolkit/core/common/quantization/quantization_config.py +19 -1
  9. model_compression_toolkit/core/keras/back2framework/keras_model_builder.py +1 -33
  10. model_compression_toolkit/core/keras/graph_substitutions/substitutions/conv_funcs_to_layer.py +2 -2
  11. model_compression_toolkit/core/keras/resource_utilization_data_facade.py +11 -1
  12. model_compression_toolkit/core/pytorch/graph_substitutions/substitutions/matmul_decomposition.py +499 -0
  13. model_compression_toolkit/core/pytorch/pytorch_implementation.py +3 -0
  14. model_compression_toolkit/core/pytorch/resource_utilization_data_facade.py +11 -3
  15. model_compression_toolkit/gptq/keras/quantization_facade.py +10 -1
  16. model_compression_toolkit/gptq/pytorch/quantization_facade.py +10 -1
  17. model_compression_toolkit/pruning/keras/pruning_facade.py +8 -2
  18. model_compression_toolkit/pruning/pytorch/pruning_facade.py +8 -2
  19. model_compression_toolkit/ptq/keras/quantization_facade.py +10 -1
  20. model_compression_toolkit/ptq/pytorch/quantization_facade.py +9 -1
  21. model_compression_toolkit/qat/__init__.py +5 -2
  22. model_compression_toolkit/qat/keras/quantization_facade.py +9 -1
  23. model_compression_toolkit/qat/pytorch/quantization_facade.py +9 -1
  24. model_compression_toolkit/target_platform_capabilities/schema/mct_current_schema.py +1 -1
  25. model_compression_toolkit/target_platform_capabilities/schema/v1.py +63 -55
  26. model_compression_toolkit/target_platform_capabilities/target_platform/targetplatform2framework/attach2fw.py +29 -18
  27. model_compression_toolkit/target_platform_capabilities/target_platform/targetplatform2framework/attach2keras.py +78 -57
  28. model_compression_toolkit/target_platform_capabilities/target_platform/targetplatform2framework/attach2pytorch.py +69 -54
  29. model_compression_toolkit/target_platform_capabilities/target_platform/targetplatform2framework/operations_to_layers.py +2 -4
  30. model_compression_toolkit/target_platform_capabilities/target_platform/targetplatform2framework/target_platform_capabilities.py +0 -10
  31. model_compression_toolkit/target_platform_capabilities/tpc_io_handler.py +93 -0
  32. model_compression_toolkit/target_platform_capabilities/tpc_models/get_target_platform_capabilities.py +46 -28
  33. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/latest/__init__.py +6 -5
  34. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v1/tp_model.py +51 -19
  35. model_compression_toolkit/target_platform_capabilities/tpc_models/qnnpack_tpc/latest/__init__.py +8 -4
  36. model_compression_toolkit/target_platform_capabilities/tpc_models/qnnpack_tpc/v1/tp_model.py +19 -9
  37. model_compression_toolkit/target_platform_capabilities/tpc_models/tflite_tpc/latest/__init__.py +7 -4
  38. model_compression_toolkit/target_platform_capabilities/tpc_models/tflite_tpc/v1/tp_model.py +46 -32
  39. model_compression_toolkit/xquant/keras/keras_report_utils.py +11 -3
  40. model_compression_toolkit/xquant/pytorch/pytorch_report_utils.py +10 -2
  41. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/target_platform_capabilities.py +0 -98
  42. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v1/tpc_keras.py +0 -129
  43. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v1/tpc_pytorch.py +0 -108
  44. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v1_lut/__init__.py +0 -16
  45. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v1_lut/tp_model.py +0 -217
  46. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v1_lut/tpc_keras.py +0 -130
  47. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v1_lut/tpc_pytorch.py +0 -109
  48. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v1_pot/__init__.py +0 -16
  49. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v1_pot/tp_model.py +0 -215
  50. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v1_pot/tpc_keras.py +0 -130
  51. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v1_pot/tpc_pytorch.py +0 -110
  52. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v2/__init__.py +0 -16
  53. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v2/tp_model.py +0 -222
  54. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v2/tpc_keras.py +0 -132
  55. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v2/tpc_pytorch.py +0 -110
  56. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v2_lut/__init__.py +0 -16
  57. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v2_lut/tp_model.py +0 -219
  58. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v2_lut/tpc_keras.py +0 -132
  59. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v2_lut/tpc_pytorch.py +0 -109
  60. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3/__init__.py +0 -16
  61. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3/tp_model.py +0 -246
  62. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3/tpc_keras.py +0 -135
  63. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3/tpc_pytorch.py +0 -113
  64. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3_lut/__init__.py +0 -16
  65. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3_lut/tp_model.py +0 -230
  66. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3_lut/tpc_keras.py +0 -132
  67. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3_lut/tpc_pytorch.py +0 -110
  68. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v4/__init__.py +0 -16
  69. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v4/tp_model.py +0 -332
  70. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v4/tpc_keras.py +0 -140
  71. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v4/tpc_pytorch.py +0 -122
  72. model_compression_toolkit/target_platform_capabilities/tpc_models/qnnpack_tpc/target_platform_capabilities.py +0 -55
  73. model_compression_toolkit/target_platform_capabilities/tpc_models/qnnpack_tpc/v1/tpc_keras.py +0 -89
  74. model_compression_toolkit/target_platform_capabilities/tpc_models/qnnpack_tpc/v1/tpc_pytorch.py +0 -78
  75. model_compression_toolkit/target_platform_capabilities/tpc_models/tflite_tpc/target_platform_capabilities.py +0 -55
  76. model_compression_toolkit/target_platform_capabilities/tpc_models/tflite_tpc/v1/tpc_keras.py +0 -118
  77. model_compression_toolkit/target_platform_capabilities/tpc_models/tflite_tpc/v1/tpc_pytorch.py +0 -100
  78. {mct_nightly-2.2.0.20250106.546.dist-info → mct_nightly-2.2.0.20250107.15510.dist-info}/LICENSE.md +0 -0
  79. {mct_nightly-2.2.0.20250106.546.dist-info → mct_nightly-2.2.0.20250107.15510.dist-info}/WHEEL +0 -0
  80. {mct_nightly-2.2.0.20250106.546.dist-info → mct_nightly-2.2.0.20250107.15510.dist-info}/top_level.txt +0 -0
@@ -1,332 +0,0 @@
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
- import model_compression_toolkit.target_platform_capabilities.schema.v1 as schema
19
- from model_compression_toolkit.constants import FLOAT_BITWIDTH
20
- from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, BIAS_ATTR, WEIGHTS_N_BITS, \
21
- IMX500_TP_MODEL
22
- from model_compression_toolkit.target_platform_capabilities.schema.mct_current_schema import TargetPlatformModel, \
23
- Signedness, \
24
- AttributeQuantizationConfig, OpQuantizationConfig
25
-
26
- tp = mct.target_platform
27
-
28
- OPSET_NO_QUANTIZATION = "NoQuantization"
29
- OPSET_QUANTIZATION_PRESERVING = "QuantizationPreserving"
30
- OPSET_DIMENSION_MANIPULATION_OPS_WITH_WEIGHTS = "DimensionManipulationOpsWithWeights"
31
- OPSET_DIMENSION_MANIPULATION_OPS = "DimensionManipulationOps"
32
- OPSET_SPLIT_OPS = "SplitOps"
33
- OPSET_MERGE_OPS = "MergeOps"
34
- OPSET_CONV = "Conv"
35
- OPSET_FULLY_CONNECTED = "FullyConnected"
36
- OPSET_BATCH_NORM = "BatchNorm"
37
- OPSET_ANY_RELU = "AnyReLU"
38
- OPSET_ADD = "Add"
39
- OPSET_SUB = "Sub"
40
- OPSET_MUL = "Mul"
41
- OPSET_DIV = "Div"
42
- OPSET_MIN_MAX = "MinMax"
43
- OPSET_PRELU = "PReLU"
44
- OPSET_SWISH = "Swish"
45
- OPSET_SIGMOID = "Sigmoid"
46
- OPSET_TANH = "Tanh"
47
- OPSET_GELU = "Gelu"
48
- OPSET_HARDSIGMOID = "HardSigmoid"
49
- OPSET_HARDSWISH = "HardSwish"
50
-
51
-
52
- def get_tp_model() -> TargetPlatformModel:
53
- """
54
- A method that generates a default target platform model, with base 8-bit quantization configuration and 8, 4, 2
55
- bits configuration list for mixed-precision quantization.
56
- NOTE: in order to generate a target platform model with different configurations but with the same Operators Sets
57
- (for tests, experiments, etc.), use this method implementation as a test-case, i.e., override the
58
- 'get_op_quantization_configs' method and use its output to call 'generate_tp_model' with your configurations.
59
- This version enables metadata by default.
60
-
61
- Returns: A TargetPlatformModel object.
62
-
63
- """
64
- base_config, mixed_precision_cfg_list, default_config = get_op_quantization_configs()
65
- return generate_tp_model(default_config=default_config,
66
- base_config=base_config,
67
- mixed_precision_cfg_list=mixed_precision_cfg_list,
68
- name='imx500_tp_model')
69
-
70
-
71
- def get_op_quantization_configs() -> \
72
- Tuple[OpQuantizationConfig, List[OpQuantizationConfig], OpQuantizationConfig]:
73
- """
74
- Creates a default configuration object for 8-bit quantization, to be used to set a default TargetPlatformModel.
75
- In addition, creates a default configuration objects list (with 8, 4 and 2 bit quantization) to be used as
76
- default configuration for mixed-precision quantization.
77
-
78
- Returns: An OpQuantizationConfig config object and a list of OpQuantizationConfig objects.
79
-
80
- """
81
-
82
- # TODO: currently, we don't want to quantize any attribute but the kernel by default,
83
- # to preserve the current behavior of MCT, so quantization is disabled for all other attributes.
84
- # Other quantization parameters are set to what we eventually want to quantize by default
85
- # when we enable multi-attributes quantization - THIS NEED TO BE MODIFIED IN ALL TP MODELS!
86
-
87
- # define a default quantization config for all non-specified weights attributes.
88
- default_weight_attr_config = AttributeQuantizationConfig(
89
- weights_quantization_method=tp.QuantizationMethod.POWER_OF_TWO,
90
- weights_n_bits=8,
91
- weights_per_channel_threshold=False,
92
- enable_weights_quantization=False,
93
- # TODO: this will changed to True once implementing multi-attributes quantization
94
- lut_values_bitwidth=None)
95
-
96
- # define a quantization config to quantize the kernel (for layers where there is a kernel attribute).
97
- kernel_base_config = AttributeQuantizationConfig(
98
- weights_quantization_method=tp.QuantizationMethod.SYMMETRIC,
99
- weights_n_bits=8,
100
- weights_per_channel_threshold=True,
101
- enable_weights_quantization=True,
102
- lut_values_bitwidth=None)
103
-
104
- # define a quantization config to quantize the bias (for layers where there is a bias attribute).
105
- bias_config = AttributeQuantizationConfig(
106
- weights_quantization_method=tp.QuantizationMethod.POWER_OF_TWO,
107
- weights_n_bits=FLOAT_BITWIDTH,
108
- weights_per_channel_threshold=False,
109
- enable_weights_quantization=False,
110
- lut_values_bitwidth=None)
111
-
112
- # Create a quantization config.
113
- # A quantization configuration defines how an operator
114
- # should be quantized on the modeled hardware:
115
-
116
- # We define a default config for operation without kernel attribute.
117
- # This is the default config that should be used for non-linear operations.
118
- eight_bits_default = OpQuantizationConfig(
119
- default_weight_attr_config=default_weight_attr_config,
120
- attr_weights_configs_mapping={},
121
- activation_quantization_method=tp.QuantizationMethod.POWER_OF_TWO,
122
- activation_n_bits=8,
123
- supported_input_activation_n_bits=8,
124
- enable_activation_quantization=True,
125
- quantization_preserving=False,
126
- fixed_scale=None,
127
- fixed_zero_point=None,
128
- simd_size=32,
129
- signedness=Signedness.AUTO)
130
-
131
- # We define an 8-bit config for linear operations quantization, that include a kernel and bias attributes.
132
- linear_eight_bits = OpQuantizationConfig(
133
- default_weight_attr_config=default_weight_attr_config,
134
- attr_weights_configs_mapping={KERNEL_ATTR: kernel_base_config, BIAS_ATTR: bias_config},
135
- activation_quantization_method=tp.QuantizationMethod.POWER_OF_TWO,
136
- activation_n_bits=8,
137
- supported_input_activation_n_bits=8,
138
- enable_activation_quantization=True,
139
- quantization_preserving=False,
140
- fixed_scale=None,
141
- fixed_zero_point=None,
142
- simd_size=32,
143
- signedness=Signedness.AUTO)
144
-
145
- # To quantize a model using mixed-precision, create
146
- # a list with more than one OpQuantizationConfig.
147
- # In this example, we quantize some operations' weights
148
- # using 2, 4 or 8 bits, and when using 2 or 4 bits, it's possible
149
- # to quantize the operations' activations using LUT.
150
- four_bits = linear_eight_bits.clone_and_edit(attr_to_edit={KERNEL_ATTR: {WEIGHTS_N_BITS: 4}},
151
- simd_size=linear_eight_bits.simd_size * 2)
152
- two_bits = linear_eight_bits.clone_and_edit(attr_to_edit={KERNEL_ATTR: {WEIGHTS_N_BITS: 2}},
153
- simd_size=linear_eight_bits.simd_size * 4)
154
-
155
- mixed_precision_cfg_list = [linear_eight_bits, four_bits, two_bits]
156
-
157
- return linear_eight_bits, mixed_precision_cfg_list, eight_bits_default
158
-
159
-
160
- def generate_tp_model(default_config: OpQuantizationConfig,
161
- base_config: OpQuantizationConfig,
162
- mixed_precision_cfg_list: List[OpQuantizationConfig],
163
- name: str) -> TargetPlatformModel:
164
- """
165
- Generates TargetPlatformModel with default defined Operators Sets, based on the given base configuration and
166
- mixed-precision configurations options list.
167
-
168
- Args
169
- default_config: A default OpQuantizationConfig to set as the TP model default configuration.
170
- base_config: An OpQuantizationConfig to set as the TargetPlatformModel base configuration for mixed-precision purposes only.
171
- mixed_precision_cfg_list: A list of OpQuantizationConfig to be used as the TP model mixed-precision
172
- quantization configuration options.
173
- name: The name of the TargetPlatformModel.
174
-
175
- Returns: A TargetPlatformModel object.
176
-
177
- """
178
- # Create a QuantizationConfigOptions, which defines a set
179
- # of possible configurations to consider when quantizing a set of operations (in mixed-precision, for example).
180
- # If the QuantizationConfigOptions contains only one configuration,
181
- # this configuration will be used for the operation quantization:
182
- default_configuration_options = schema.QuantizationConfigOptions(quantization_configurations=tuple([default_config]))
183
- default_config_input16 = default_config.clone_and_edit(supported_input_activation_n_bits=(8, 16))
184
- default_config_options_16bit = schema.QuantizationConfigOptions(quantization_configurations=tuple([default_config_input16,
185
- default_config_input16.clone_and_edit(
186
- activation_n_bits=16,
187
- signedness=Signedness.SIGNED)]),
188
- base_config=default_config_input16)
189
-
190
- qpreseving_config = default_config.clone_and_edit(enable_activation_quantization=False,
191
- quantization_preserving=True,
192
- supported_input_activation_n_bits=(8, 16))
193
-
194
- qpreseving_config_options = schema.QuantizationConfigOptions(quantization_configurations=tuple([qpreseving_config,
195
- qpreseving_config.clone_and_edit(
196
- activation_n_bits=16,
197
- signedness=Signedness.SIGNED)]),
198
- base_config=qpreseving_config)
199
-
200
- # Create a QuantizationConfigOptions for quantizing constants in functional ops.
201
- # Constant configuration is similar to the default eight bit configuration except for PoT
202
- # quantization method for the constant.
203
- # Since the constants are not named attributes of the layer, we use the default_weight_attr_config to
204
- # define the desired quantization properties for them.
205
- const_config = default_config.clone_and_edit(
206
- default_weight_attr_config=default_config.default_weight_attr_config.clone_and_edit(
207
- enable_weights_quantization=True, weights_per_channel_threshold=True,
208
- weights_quantization_method=tp.QuantizationMethod.POWER_OF_TWO))
209
- const_configuration_options = schema.QuantizationConfigOptions(quantization_configurations=tuple([const_config]))
210
-
211
- # 16 bits inputs and outputs. Currently, only defined for consts since they are used in operators that
212
- # support 16 bit as input and output.
213
- const_config_input16 = const_config.clone_and_edit(
214
- supported_input_activation_n_bits=(8, 16))
215
- const_config_input16_output16 = const_config_input16.clone_and_edit(
216
- activation_n_bits=16, signedness=Signedness.SIGNED)
217
- const_configuration_options_inout16 = schema.QuantizationConfigOptions(
218
- quantization_configurations=tuple([const_config_input16_output16,
219
- const_config_input16]),
220
- base_config=const_config_input16)
221
-
222
- const_config_input16_per_tensor = const_config.clone_and_edit(
223
- supported_input_activation_n_bits=(8, 16),
224
- default_weight_attr_config=default_config.default_weight_attr_config.clone_and_edit(
225
- enable_weights_quantization=True, weights_per_channel_threshold=False,
226
- weights_quantization_method=tp.QuantizationMethod.POWER_OF_TWO)
227
- )
228
- const_config_input16_output16_per_tensor = const_config_input16_per_tensor.clone_and_edit(
229
- activation_n_bits=16, signedness=Signedness.SIGNED)
230
- const_configuration_options_inout16_per_tensor = schema.QuantizationConfigOptions(quantization_configurations=tuple(
231
- [const_config_input16_output16_per_tensor,
232
- const_config_input16_per_tensor]),
233
- base_config=const_config_input16_per_tensor)
234
-
235
- qpreserving_const_config = const_config.clone_and_edit(enable_activation_quantization=False,
236
- quantization_preserving=True,
237
- default_weight_attr_config=const_config.default_weight_attr_config.clone_and_edit(
238
- weights_per_channel_threshold=False))
239
- qpreserving_const_config_options = schema.QuantizationConfigOptions(quantization_configurations=tuple([qpreserving_const_config]))
240
-
241
- mp_cfg_list_16bit = [mp_cfg.clone_and_edit(activation_n_bits=16, signedness=Signedness.SIGNED)
242
- for mp_cfg in mixed_precision_cfg_list]
243
-
244
- # Create Mixed-Precision quantization configuration options from the given list of OpQuantizationConfig objects
245
- mixed_precision_configuration_options = schema.QuantizationConfigOptions(quantization_configurations=tuple(
246
- mixed_precision_cfg_list + mp_cfg_list_16bit),
247
- base_config=base_config)
248
-
249
- # Create an OperatorsSet to represent a set of operations.
250
- # Each OperatorsSet has a unique label.
251
- # If a quantization configuration options is passed, these options will
252
- # be used for operations that will be attached to this set's label.
253
- # Otherwise, it will be a configure-less set (used in fusing):
254
- operator_set = []
255
- fusing_patterns = []
256
- # May suit for operations like: Dropout, Reshape, etc.
257
- operator_set.append(schema.OperatorsSet(name=OPSET_NO_QUANTIZATION,
258
- qc_options=default_configuration_options.clone_and_edit(
259
- enable_activation_quantization=False)
260
- .clone_and_edit_weight_attribute(enable_weights_quantization=False)))
261
- operator_set.append(schema.OperatorsSet(name=OPSET_QUANTIZATION_PRESERVING,
262
- qc_options=default_configuration_options.clone_and_edit(
263
- enable_activation_quantization=False,
264
- quantization_preserving=True)
265
- .clone_and_edit_weight_attribute(enable_weights_quantization=False)))
266
- operator_set.append(
267
- schema.OperatorsSet(name=OPSET_DIMENSION_MANIPULATION_OPS_WITH_WEIGHTS,
268
- qc_options=qpreserving_const_config_options))
269
- operator_set.append(schema.OperatorsSet(name=OPSET_DIMENSION_MANIPULATION_OPS,
270
- qc_options=default_configuration_options.clone_and_edit(
271
- enable_activation_quantization=False,
272
- quantization_preserving=True,
273
- supported_input_activation_n_bits=(8, 16))
274
- .clone_and_edit_weight_attribute(enable_weights_quantization=False)))
275
-
276
- operator_set.append(schema.OperatorsSet(name=OPSET_SPLIT_OPS, qc_options=qpreseving_config_options))
277
- operator_set.append(schema.OperatorsSet(name=OPSET_MERGE_OPS, qc_options=const_configuration_options_inout16_per_tensor))
278
-
279
- # Define operator sets that use mixed_precision_configuration_options:
280
- conv = schema.OperatorsSet(name=OPSET_CONV, qc_options=mixed_precision_configuration_options)
281
- fc = schema.OperatorsSet(name=OPSET_FULLY_CONNECTED, qc_options=mixed_precision_configuration_options)
282
-
283
- operator_set.append(schema.OperatorsSet(name=OPSET_BATCH_NORM, qc_options=default_config_options_16bit))
284
-
285
- # Note: Operations sets without quantization configuration are useful for creating fusing patterns
286
- any_relu = schema.OperatorsSet(name=OPSET_ANY_RELU, qc_options=default_config_options_16bit)
287
- add = schema.OperatorsSet(name=OPSET_ADD, qc_options=const_configuration_options_inout16)
288
- sub = schema.OperatorsSet(name=OPSET_SUB, qc_options=const_configuration_options_inout16)
289
- mul = schema.OperatorsSet(name=OPSET_MUL, qc_options=const_configuration_options_inout16)
290
- div = schema.OperatorsSet(name=OPSET_DIV, qc_options=const_configuration_options)
291
- min_max = schema.OperatorsSet(name=OPSET_MIN_MAX, qc_options=const_configuration_options_inout16)
292
- prelu = schema.OperatorsSet(name=OPSET_PRELU, qc_options=default_config_options_16bit)
293
- swish = schema.OperatorsSet(name=OPSET_SWISH, qc_options=default_config_options_16bit)
294
- sigmoid = schema.OperatorsSet(name=OPSET_SIGMOID, qc_options=default_config_options_16bit)
295
- tanh = schema.OperatorsSet(name=OPSET_TANH, qc_options=default_config_options_16bit)
296
- gelu = schema.OperatorsSet(name=OPSET_GELU, qc_options=default_config_options_16bit)
297
- hardsigmoid = schema.OperatorsSet(name=OPSET_HARDSIGMOID, qc_options=default_config_options_16bit)
298
- hardswish = schema.OperatorsSet(name=OPSET_HARDSWISH, qc_options=default_config_options_16bit)
299
-
300
- operator_set.extend(
301
- [conv, fc, any_relu, add, sub, mul, div, prelu, swish, sigmoid, tanh, min_max, gelu, hardsigmoid, hardswish])
302
- # Combine multiple operators into a single operator to avoid quantization between
303
- # them. To do this we define fusing patterns using the OperatorsSets that were created.
304
- # To group multiple sets with regard to fusing, an OperatorSetConcat can be created
305
- activations_after_conv_to_fuse = schema.OperatorSetConcat(operators_set=[any_relu, swish, prelu, sigmoid,
306
- tanh, gelu, hardswish, hardsigmoid])
307
- activations_after_fc_to_fuse = schema.OperatorSetConcat(operators_set=[any_relu, swish, sigmoid, tanh, gelu,
308
- hardswish, hardsigmoid])
309
- any_binary = schema.OperatorSetConcat(operators_set=[add, sub, mul, div])
310
-
311
- # ------------------- #
312
- # Fusions
313
- # ------------------- #
314
- fusing_patterns.append(schema.Fusing(operator_groups=(conv, activations_after_conv_to_fuse)))
315
- fusing_patterns.append(schema.Fusing(operator_groups=(fc, activations_after_fc_to_fuse)))
316
- fusing_patterns.append(schema.Fusing(operator_groups=(any_binary, any_relu)))
317
-
318
- # Create a TargetPlatformModel and set its default quantization config.
319
- # This default configuration will be used for all operations
320
- # unless specified otherwise (see OperatorsSet, for example):
321
- generated_tpm = schema.TargetPlatformModel(
322
- default_qco=default_configuration_options,
323
- tpc_minor_version=4,
324
- tpc_patch_version=0,
325
- tpc_platform_type=IMX500_TP_MODEL,
326
- operator_set=tuple(operator_set),
327
- fusing_patterns=tuple(fusing_patterns),
328
- add_metadata=True,
329
- name=name,
330
- is_simd_padding=True)
331
-
332
- return generated_tpm
@@ -1,140 +0,0 @@
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
- import model_compression_toolkit.target_platform_capabilities.schema.mct_current_schema as schema
19
- from model_compression_toolkit.defaultdict import DefaultDict
20
- from model_compression_toolkit.verify_packages import FOUND_SONY_CUSTOM_LAYERS
21
- from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, KERAS_DEPTHWISE_KERNEL, \
22
- KERAS_KERNEL, BIAS_ATTR, BIAS
23
-
24
- if FOUND_SONY_CUSTOM_LAYERS:
25
- from sony_custom_layers.keras.object_detection.ssd_post_process import SSDPostProcess
26
-
27
- if version.parse(tf.__version__) >= version.parse("2.13"):
28
- from keras.src.layers import Conv2D, DepthwiseConv2D, Dense, Reshape, ZeroPadding2D, Dropout, \
29
- MaxPooling2D, Activation, ReLU, Add, Subtract, Multiply, PReLU, Flatten, Cropping2D, LeakyReLU, Permute, \
30
- Conv2DTranspose, Identity, Concatenate, BatchNormalization, Minimum, Maximum
31
- else:
32
- from keras.layers import Conv2D, DepthwiseConv2D, Dense, Reshape, ZeroPadding2D, Dropout, \
33
- MaxPooling2D, Activation, ReLU, Add, Subtract, Multiply, PReLU, Flatten, Cropping2D, LeakyReLU, Permute, \
34
- Conv2DTranspose, Identity, Concatenate, BatchNormalization, Minimum, Maximum
35
-
36
- from model_compression_toolkit.target_platform_capabilities.tpc_models.imx500_tpc.v4.tp_model import get_tp_model
37
- import model_compression_toolkit as mct
38
- from model_compression_toolkit.target_platform_capabilities.tpc_models.imx500_tpc.v4 import __version__ as TPC_VERSION
39
- from model_compression_toolkit.target_platform_capabilities.tpc_models.imx500_tpc.v4.tp_model import OPSET_NO_QUANTIZATION, \
40
- OPSET_QUANTIZATION_PRESERVING, OPSET_DIMENSION_MANIPULATION_OPS_WITH_WEIGHTS, OPSET_DIMENSION_MANIPULATION_OPS, \
41
- OPSET_MERGE_OPS, OPSET_CONV, OPSET_FULLY_CONNECTED, OPSET_ANY_RELU, OPSET_ADD, OPSET_SUB, OPSET_MUL, OPSET_DIV, \
42
- OPSET_PRELU, OPSET_SWISH, OPSET_SIGMOID, OPSET_TANH, OPSET_GELU, OPSET_BATCH_NORM, OPSET_MIN_MAX, OPSET_HARDSIGMOID, \
43
- OPSET_SPLIT_OPS
44
-
45
- tp = mct.target_platform
46
-
47
-
48
- def get_keras_tpc() -> tp.TargetPlatformCapabilities:
49
- """
50
- get a Keras TargetPlatformCapabilities object with default operation sets to layers mapping.
51
-
52
- Returns: a Keras TargetPlatformCapabilities object for the given TargetPlatformModel.
53
- """
54
- imx500_tpc_tp_model = get_tp_model()
55
- return generate_keras_tpc(name='imx500_tpc_keras_tpc', tp_model=imx500_tpc_tp_model)
56
-
57
-
58
- def generate_keras_tpc(name: str, tp_model: schema.TargetPlatformModel):
59
- """
60
- Generates a TargetPlatformCapabilities object with default operation sets to layers mapping.
61
-
62
- Args:
63
- name: Name of the TargetPlatformCapabilities.
64
- tp_model: TargetPlatformModel object.
65
-
66
- Returns: a TargetPlatformCapabilities object for the given TargetPlatformModel.
67
- """
68
-
69
- keras_tpc = tp.TargetPlatformCapabilities(tp_model)
70
-
71
- no_quant_list = [tf.quantization.fake_quant_with_min_max_vars,
72
- tf.math.argmax,
73
- tf.shape,
74
- tf.math.equal,
75
- tf.nn.top_k,
76
- tf.image.combined_non_max_suppression,
77
- tf.compat.v1.shape]
78
- quantization_preserving = [Cropping2D,
79
- ZeroPadding2D,
80
- Dropout,
81
- MaxPooling2D,
82
- tf.cast]
83
- quantization_preserving_list_16bit_input = [Reshape,
84
- tf.reshape,
85
- Permute,
86
- tf.transpose,
87
- Flatten]
88
-
89
- if FOUND_SONY_CUSTOM_LAYERS:
90
- no_quant_list.append(SSDPostProcess)
91
-
92
- with keras_tpc:
93
- tp.OperationsSetToLayers(OPSET_NO_QUANTIZATION, no_quant_list)
94
- tp.OperationsSetToLayers(OPSET_QUANTIZATION_PRESERVING, quantization_preserving)
95
- tp.OperationsSetToLayers(OPSET_DIMENSION_MANIPULATION_OPS, quantization_preserving_list_16bit_input)
96
- tp.OperationsSetToLayers(OPSET_DIMENSION_MANIPULATION_OPS_WITH_WEIGHTS, [tf.gather, tf.compat.v1.gather])
97
- tp.OperationsSetToLayers(OPSET_SPLIT_OPS,[tf.unstack, tf.split, tf.strided_slice, tf.__operators__.getitem])
98
- tp.OperationsSetToLayers(OPSET_MERGE_OPS, [tf.stack, tf.concat, Concatenate])
99
- tp.OperationsSetToLayers(OPSET_CONV,
100
- [Conv2D,
101
- DepthwiseConv2D,
102
- Conv2DTranspose,
103
- tf.nn.conv2d,
104
- tf.nn.depthwise_conv2d,
105
- tf.nn.conv2d_transpose],
106
- # we provide attributes mapping that maps each layer type in the operations set
107
- # that has weights attributes with provided quantization config (in the tp model) to
108
- # its framework-specific attribute name.
109
- # note that a DefaultDict should be provided if not all the layer types in the
110
- # operation set are provided separately in the mapping.
111
- attr_mapping={
112
- KERNEL_ATTR: DefaultDict({
113
- DepthwiseConv2D: KERAS_DEPTHWISE_KERNEL,
114
- tf.nn.depthwise_conv2d: KERAS_DEPTHWISE_KERNEL}, default_value=KERAS_KERNEL),
115
- BIAS_ATTR: DefaultDict(default_value=BIAS)})
116
- tp.OperationsSetToLayers(OPSET_FULLY_CONNECTED, [Dense],
117
- attr_mapping={KERNEL_ATTR: DefaultDict(default_value=KERAS_KERNEL),
118
- BIAS_ATTR: DefaultDict(default_value=BIAS)})
119
- tp.OperationsSetToLayers(OPSET_BATCH_NORM, [BatchNormalization])
120
- tp.OperationsSetToLayers(OPSET_ANY_RELU, [tf.nn.relu,
121
- tf.nn.relu6,
122
- tf.nn.leaky_relu,
123
- ReLU,
124
- LeakyReLU,
125
- tp.LayerFilterParams(Activation, activation="relu"),
126
- tp.LayerFilterParams(Activation, activation="leaky_relu")])
127
- tp.OperationsSetToLayers(OPSET_ADD, [tf.add, Add])
128
- tp.OperationsSetToLayers(OPSET_SUB, [tf.subtract, Subtract])
129
- tp.OperationsSetToLayers(OPSET_MUL, [tf.math.multiply, Multiply])
130
- tp.OperationsSetToLayers(OPSET_DIV, [tf.math.divide, tf.math.truediv])
131
- tp.OperationsSetToLayers(OPSET_MIN_MAX, [tf.math.minimum, tf.math.maximum, Minimum, Maximum])
132
- tp.OperationsSetToLayers(OPSET_PRELU, [PReLU])
133
- tp.OperationsSetToLayers(OPSET_SWISH, [tf.nn.swish, tp.LayerFilterParams(Activation, activation="swish")])
134
- tp.OperationsSetToLayers(OPSET_SIGMOID, [tf.nn.sigmoid, tp.LayerFilterParams(Activation, activation="sigmoid")])
135
- tp.OperationsSetToLayers(OPSET_TANH, [tf.nn.tanh, tp.LayerFilterParams(Activation, activation="tanh")])
136
- tp.OperationsSetToLayers(OPSET_GELU, [tf.nn.gelu, tp.LayerFilterParams(Activation, activation="gelu")])
137
- tp.OperationsSetToLayers(OPSET_HARDSIGMOID, [tf.keras.activations.hard_sigmoid,
138
- tp.LayerFilterParams(Activation, activation="hard_sigmoid")])
139
-
140
- return keras_tpc
@@ -1,122 +0,0 @@
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, divide, flatten, reshape, split, unsqueeze, dropout, sigmoid, tanh, \
20
- chunk, unbind, topk, gather, equal, transpose, permute, argmax, squeeze, multiply, subtract, minimum, \
21
- maximum
22
- from torch.nn import Conv2d, Linear, ConvTranspose2d, MaxPool2d, BatchNorm2d
23
- from torch.nn import Dropout, Flatten, Hardtanh
24
- from torch.nn import ReLU, ReLU6, PReLU, SiLU, Sigmoid, Tanh, Hardswish, Hardsigmoid, LeakyReLU, GELU
25
- import torch.nn.functional as F
26
- from torch.nn.functional import relu, relu6, prelu, silu, hardtanh, hardswish, hardsigmoid, leaky_relu, gelu
27
-
28
- import model_compression_toolkit.target_platform_capabilities.schema.mct_current_schema as schema
29
- from model_compression_toolkit.defaultdict import DefaultDict
30
- from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, BIAS_ATTR, PYTORCH_KERNEL, \
31
- BIAS
32
- from model_compression_toolkit.target_platform_capabilities.tpc_models.imx500_tpc.v4.tp_model import get_tp_model
33
- import model_compression_toolkit as mct
34
- from model_compression_toolkit.target_platform_capabilities.tpc_models.imx500_tpc.v4 import __version__ as TPC_VERSION
35
- from model_compression_toolkit.target_platform_capabilities.tpc_models.imx500_tpc.v4.tp_model import OPSET_NO_QUANTIZATION, \
36
- OPSET_QUANTIZATION_PRESERVING, OPSET_DIMENSION_MANIPULATION_OPS_WITH_WEIGHTS, OPSET_DIMENSION_MANIPULATION_OPS, \
37
- OPSET_MERGE_OPS, OPSET_CONV, OPSET_FULLY_CONNECTED, OPSET_ANY_RELU, OPSET_ADD, OPSET_SUB, OPSET_MUL, OPSET_DIV, \
38
- OPSET_PRELU, OPSET_SWISH, OPSET_SIGMOID, OPSET_TANH, OPSET_GELU, OPSET_BATCH_NORM, OPSET_MIN_MAX, OPSET_HARDSIGMOID, \
39
- OPSET_HARDSWISH, OPSET_SPLIT_OPS
40
-
41
- tp = mct.target_platform
42
-
43
-
44
- def get_pytorch_tpc() -> tp.TargetPlatformCapabilities:
45
- """
46
- get a Pytorch TargetPlatformCapabilities object with default operation sets to layers mapping.
47
-
48
- Returns: a Pytorch TargetPlatformCapabilities object for the given TargetPlatformModel.
49
- """
50
- imx500_tpc_tp_model = get_tp_model()
51
- return generate_pytorch_tpc(name='imx500_tpc_pytorch_tpc', tp_model=imx500_tpc_tp_model)
52
-
53
-
54
- def generate_pytorch_tpc(name: str, tp_model: schema.TargetPlatformModel):
55
- """
56
- Generates a TargetPlatformCapabilities object with default operation sets to layers mapping.
57
- Args:
58
- name: Name of the TargetPlatformModel.
59
- tp_model: TargetPlatformModel object.
60
- Returns: a TargetPlatformCapabilities object for the given TargetPlatformModel.
61
- """
62
-
63
- pytorch_tpc = tp.TargetPlatformCapabilities(tp_model)
64
-
65
- # we provide attributes mapping that maps each layer type in the operations set
66
- # that has weights attributes with provided quantization config (in the tp model) to
67
- # its framework-specific attribute name.
68
- # note that a DefaultDict should be provided if not all the layer types in the
69
- # operation set are provided separately in the mapping.
70
- pytorch_linear_attr_mapping = {KERNEL_ATTR: DefaultDict(default_value=PYTORCH_KERNEL),
71
- BIAS_ATTR: DefaultDict(default_value=BIAS)}
72
-
73
- with pytorch_tpc:
74
- tp.OperationsSetToLayers(OPSET_NO_QUANTIZATION, [torch.Tensor.size,
75
- equal,
76
- argmax,
77
- topk])
78
- tp.OperationsSetToLayers(OPSET_QUANTIZATION_PRESERVING, [Dropout,
79
- dropout,
80
- MaxPool2d])
81
- tp.OperationsSetToLayers(OPSET_DIMENSION_MANIPULATION_OPS, [Flatten,
82
- flatten,
83
- operator.getitem,
84
- reshape,
85
- unsqueeze,
86
- squeeze,
87
- permute,
88
- transpose])
89
- tp.OperationsSetToLayers(OPSET_DIMENSION_MANIPULATION_OPS_WITH_WEIGHTS, [gather, torch.Tensor.expand])
90
- tp.OperationsSetToLayers(OPSET_SPLIT_OPS,[split, chunk, unbind])
91
- tp.OperationsSetToLayers(OPSET_MERGE_OPS,
92
- [torch.stack, torch.cat, torch.concat, torch.concatenate])
93
-
94
- tp.OperationsSetToLayers(OPSET_CONV, [Conv2d, ConvTranspose2d],
95
- attr_mapping=pytorch_linear_attr_mapping)
96
- tp.OperationsSetToLayers(OPSET_FULLY_CONNECTED, [Linear],
97
- attr_mapping=pytorch_linear_attr_mapping)
98
- tp.OperationsSetToLayers(OPSET_BATCH_NORM, [BatchNorm2d])
99
- tp.OperationsSetToLayers(OPSET_ANY_RELU, [torch.relu,
100
- ReLU,
101
- ReLU6,
102
- LeakyReLU,
103
- relu,
104
- relu6,
105
- leaky_relu,
106
- tp.LayerFilterParams(Hardtanh, min_val=0),
107
- tp.LayerFilterParams(hardtanh, min_val=0)])
108
-
109
- tp.OperationsSetToLayers(OPSET_ADD, [operator.add, add])
110
- tp.OperationsSetToLayers(OPSET_SUB, [operator.sub, sub, subtract])
111
- tp.OperationsSetToLayers(OPSET_MUL, [operator.mul, mul, multiply])
112
- tp.OperationsSetToLayers(OPSET_DIV, [operator.truediv, div, divide])
113
- tp.OperationsSetToLayers(OPSET_MIN_MAX, [minimum, maximum])
114
- tp.OperationsSetToLayers(OPSET_PRELU, [PReLU, prelu])
115
- tp.OperationsSetToLayers(OPSET_SWISH, [SiLU, silu])
116
- tp.OperationsSetToLayers(OPSET_SIGMOID, [Sigmoid, sigmoid, F.sigmoid])
117
- tp.OperationsSetToLayers(OPSET_TANH, [Tanh, tanh, F.tanh])
118
- tp.OperationsSetToLayers(OPSET_GELU, [GELU, gelu])
119
- tp.OperationsSetToLayers(OPSET_HARDSIGMOID, [Hardsigmoid, hardsigmoid])
120
- tp.OperationsSetToLayers(OPSET_HARDSWISH, [Hardswish, hardswish])
121
-
122
- return pytorch_tpc
@@ -1,55 +0,0 @@
1
- # Copyright 2022 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 model_compression_toolkit.constants import TENSORFLOW, PYTORCH
17
- from model_compression_toolkit.verify_packages import FOUND_TORCH, FOUND_TF
18
- from model_compression_toolkit.logger import Logger
19
- from model_compression_toolkit.target_platform_capabilities.constants import LATEST
20
-
21
- def get_tpc_dict_by_fw(fw_name):
22
- tpc_models_dict = None
23
- if fw_name == TENSORFLOW:
24
- ###############################
25
- # Build Tensorflow TPC models
26
- ###############################
27
- if FOUND_TF:
28
- from model_compression_toolkit.target_platform_capabilities.tpc_models.qnnpack_tpc.v1.tpc_keras import \
29
- get_keras_tpc as get_keras_tpc_v1
30
- from model_compression_toolkit.target_platform_capabilities.tpc_models.qnnpack_tpc.latest import \
31
- get_keras_tpc_latest
32
-
33
- # Keras: TPC versioning
34
- tpc_models_dict = {'v1': get_keras_tpc_v1,
35
- LATEST: get_keras_tpc_latest}
36
- elif fw_name == PYTORCH:
37
- ###############################
38
- # Build Pytorch TPC models
39
- ###############################
40
- if FOUND_TORCH:
41
- from model_compression_toolkit.target_platform_capabilities.tpc_models.qnnpack_tpc.v1.tpc_pytorch import \
42
- get_pytorch_tpc as get_pytorch_tpc_v1
43
- from model_compression_toolkit.target_platform_capabilities.tpc_models.qnnpack_tpc.latest import \
44
- get_pytorch_tpc_latest
45
-
46
- # Pytorch: TPC versioning
47
- tpc_models_dict = {'v1': get_pytorch_tpc_v1,
48
- LATEST: get_pytorch_tpc_latest}
49
- if tpc_models_dict is not None:
50
- return tpc_models_dict
51
- else:
52
- Logger.critical(f'Framework {fw_name} is not supported in imx500 or the relevant packages are not '
53
- f'installed. Please make sure the relevant packages are installed when using MCT for optimizing'
54
- f' a {fw_name} model. For Tensorflow, please install tensorflow. For PyTorch, please install '
55
- f'torch.') # pragma: no cover