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,246 +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.mct_current_schema 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
-
29
- def get_tp_model() -> TargetPlatformModel:
30
- """
31
- A method that generates a default target platform model, with base 8-bit quantization configuration and 8, 4, 2
32
- bits configuration list for mixed-precision quantization.
33
- NOTE: in order to generate a target platform model with different configurations but with the same Operators Sets
34
- (for tests, experiments, etc.), use this method implementation as a test-case, i.e., override the
35
- 'get_op_quantization_configs' method and use its output to call 'generate_tp_model' with your configurations.
36
- This version enables metadata by default.
37
-
38
- Returns: A TargetPlatformModel object.
39
-
40
- """
41
- base_config, mixed_precision_cfg_list, default_config = get_op_quantization_configs()
42
- return generate_tp_model(default_config=default_config,
43
- base_config=base_config,
44
- mixed_precision_cfg_list=mixed_precision_cfg_list,
45
- name='imx500_tp_model')
46
-
47
-
48
- def get_op_quantization_configs() -> \
49
- Tuple[OpQuantizationConfig, List[OpQuantizationConfig], OpQuantizationConfig]:
50
- """
51
- Creates a default configuration object for 8-bit quantization, to be used to set a default TargetPlatformModel.
52
- In addition, creates a default configuration objects list (with 8, 4 and 2 bit quantization) to be used as
53
- default configuration for mixed-precision quantization.
54
-
55
- Returns: An OpQuantizationConfig config object and a list of OpQuantizationConfig objects.
56
-
57
- """
58
-
59
- # TODO: currently, we don't want to quantize any attribute but the kernel by default,
60
- # to preserve the current behavior of MCT, so quantization is disabled for all other attributes.
61
- # Other quantization parameters are set to what we eventually want to quantize by default
62
- # when we enable multi-attributes quantization - THIS NEED TO BE MODIFIED IN ALL TP MODELS!
63
-
64
- # define a default quantization config for all non-specified weights attributes.
65
- default_weight_attr_config = AttributeQuantizationConfig(
66
- weights_quantization_method=tp.QuantizationMethod.POWER_OF_TWO,
67
- weights_n_bits=8,
68
- weights_per_channel_threshold=False,
69
- enable_weights_quantization=False,
70
- # TODO: this will changed to True once implementing multi-attributes quantization
71
- lut_values_bitwidth=None)
72
-
73
- # define a quantization config to quantize the kernel (for layers where there is a kernel attribute).
74
- kernel_base_config = AttributeQuantizationConfig(
75
- weights_quantization_method=tp.QuantizationMethod.SYMMETRIC,
76
- weights_n_bits=8,
77
- weights_per_channel_threshold=True,
78
- enable_weights_quantization=True,
79
- lut_values_bitwidth=None)
80
-
81
- # define a quantization config to quantize the bias (for layers where there is a bias attribute).
82
- bias_config = AttributeQuantizationConfig(
83
- weights_quantization_method=tp.QuantizationMethod.POWER_OF_TWO,
84
- weights_n_bits=FLOAT_BITWIDTH,
85
- weights_per_channel_threshold=False,
86
- enable_weights_quantization=False,
87
- lut_values_bitwidth=None)
88
-
89
- # Create a quantization config.
90
- # A quantization configuration defines how an operator
91
- # should be quantized on the modeled hardware:
92
-
93
- # We define a default config for operation without kernel attribute.
94
- # This is the default config that should be used for non-linear operations.
95
- eight_bits_default = schema.OpQuantizationConfig(
96
- default_weight_attr_config=default_weight_attr_config,
97
- attr_weights_configs_mapping={},
98
- activation_quantization_method=tp.QuantizationMethod.POWER_OF_TWO,
99
- activation_n_bits=8,
100
- supported_input_activation_n_bits=8,
101
- enable_activation_quantization=True,
102
- quantization_preserving=False,
103
- fixed_scale=None,
104
- fixed_zero_point=None,
105
- simd_size=32,
106
- signedness=Signedness.AUTO)
107
-
108
- # We define an 8-bit config for linear operations quantization, that include a kernel and bias attributes.
109
- linear_eight_bits = schema.OpQuantizationConfig(
110
- default_weight_attr_config=default_weight_attr_config,
111
- attr_weights_configs_mapping={KERNEL_ATTR: kernel_base_config, BIAS_ATTR: bias_config},
112
- activation_quantization_method=tp.QuantizationMethod.POWER_OF_TWO,
113
- activation_n_bits=8,
114
- supported_input_activation_n_bits=8,
115
- enable_activation_quantization=True,
116
- quantization_preserving=False,
117
- fixed_scale=None,
118
- fixed_zero_point=None,
119
- simd_size=32,
120
- signedness=Signedness.AUTO)
121
-
122
- # To quantize a model using mixed-precision, create
123
- # a list with more than one OpQuantizationConfig.
124
- # In this example, we quantize some operations' weights
125
- # using 2, 4 or 8 bits, and when using 2 or 4 bits, it's possible
126
- # to quantize the operations' activations using LUT.
127
- four_bits = linear_eight_bits.clone_and_edit(attr_to_edit={KERNEL_ATTR: {WEIGHTS_N_BITS: 4}},
128
- simd_size=linear_eight_bits.simd_size * 2)
129
- two_bits = linear_eight_bits.clone_and_edit(attr_to_edit={KERNEL_ATTR: {WEIGHTS_N_BITS: 2}},
130
- simd_size=linear_eight_bits.simd_size * 4)
131
-
132
- mixed_precision_cfg_list = [linear_eight_bits, four_bits, two_bits]
133
-
134
- return linear_eight_bits, mixed_precision_cfg_list, eight_bits_default
135
-
136
-
137
- def generate_tp_model(default_config: OpQuantizationConfig,
138
- base_config: OpQuantizationConfig,
139
- mixed_precision_cfg_list: List[OpQuantizationConfig],
140
- name: str) -> TargetPlatformModel:
141
- """
142
- Generates TargetPlatformModel with default defined Operators Sets, based on the given base configuration and
143
- mixed-precision configurations options list.
144
-
145
- Args
146
- default_config: A default OpQuantizationConfig to set as the TP model default configuration.
147
- base_config: An OpQuantizationConfig to set as the TargetPlatformModel base configuration for mixed-precision purposes only.
148
- mixed_precision_cfg_list: A list of OpQuantizationConfig to be used as the TP model mixed-precision
149
- quantization configuration options.
150
- name: The name of the TargetPlatformModel.
151
-
152
- Returns: A TargetPlatformModel object.
153
-
154
- """
155
- # Create a QuantizationConfigOptions, which defines a set
156
- # of possible configurations to consider when quantizing a set of operations (in mixed-precision, for example).
157
- # If the QuantizationConfigOptions contains only one configuration,
158
- # this configuration will be used for the operation quantization:
159
- default_configuration_options = schema.QuantizationConfigOptions(quantization_configurations=tuple([default_config]))
160
-
161
- # Create a QuantizationConfigOptions for quantizing constants in functional ops.
162
- # Constant configuration is similar to the default eight bit configuration except for PoT
163
- # quantization method for the constant.
164
- # Since the constants are not named attributes of the layer, we use the default_weight_attr_config to
165
- # define the desired quantization properties for them.
166
- const_config = default_config.clone_and_edit(
167
- default_weight_attr_config=default_config.default_weight_attr_config.clone_and_edit(
168
- enable_weights_quantization=True, weights_per_channel_threshold=True,
169
- weights_quantization_method=tp.QuantizationMethod.POWER_OF_TWO))
170
- const_configuration_options = schema.QuantizationConfigOptions(quantization_configurations=tuple([const_config]))
171
-
172
- # 16 bits inputs and outputs. Currently, only defined for consts since they are used in operators that
173
- # support 16 bit as input and output.
174
- const_config_input16 = const_config.clone_and_edit(
175
- supported_input_activation_n_bits=(8, 16))
176
- const_config_input16_output16 = const_config_input16.clone_and_edit(
177
- activation_n_bits=16, signedness=Signedness.SIGNED)
178
- const_configuration_options_inout16 = schema.QuantizationConfigOptions(quantization_configurations=tuple([const_config_input16_output16,
179
- const_config_input16]),
180
- base_config=const_config_input16)
181
-
182
- # Create Mixed-Precision quantization configuration options from the given list of OpQuantizationConfig objects
183
- mixed_precision_configuration_options = schema.QuantizationConfigOptions(quantization_configurations=tuple(mixed_precision_cfg_list),
184
- base_config=base_config)
185
-
186
- # Create an OperatorsSet to represent a set of operations.
187
- # Each OperatorsSet has a unique label.
188
- # If a quantization configuration options is passed, these options will
189
- # be used for operations that will be attached to this set's label.
190
- # Otherwise, it will be a configure-less set (used in fusing):
191
- operator_set = []
192
- fusing_patterns = []
193
- # May suit for operations like: Dropout, Reshape, etc.
194
- operator_set.append(schema.OperatorsSet(name="NoQuantization",
195
- qc_options=default_configuration_options.clone_and_edit(
196
- enable_activation_quantization=False,
197
- supported_input_activation_n_bits=(8, 16))
198
- .clone_and_edit_weight_attribute(enable_weights_quantization=False)))
199
- operator_set.append(schema.OperatorsSet(name="Default16BitInout", qc_options=const_configuration_options_inout16))
200
-
201
- # Define operator sets that use mixed_precision_configuration_options:
202
- conv = schema.OperatorsSet(name="Conv", qc_options=mixed_precision_configuration_options)
203
- fc = schema.OperatorsSet(name="FullyConnected", qc_options=mixed_precision_configuration_options)
204
-
205
- # Define operations sets without quantization configuration
206
- # options (useful for creating fusing patterns, for example):
207
- any_relu = schema.OperatorsSet(name="AnyReLU")
208
- add = schema.OperatorsSet(name="Add", qc_options=const_configuration_options_inout16)
209
- sub = schema.OperatorsSet(name="Sub", qc_options=const_configuration_options_inout16)
210
- mul = schema.OperatorsSet(name="Mul", qc_options=const_configuration_options_inout16)
211
- div = schema.OperatorsSet(name="Div", qc_options=const_configuration_options)
212
- prelu = schema.OperatorsSet(name="PReLU")
213
- swish = schema.OperatorsSet(name="Swish")
214
- sigmoid = schema.OperatorsSet(name="Sigmoid")
215
- tanh = schema.OperatorsSet(name="Tanh")
216
-
217
- operator_set.extend([conv, fc, any_relu, add, sub, mul, div, prelu, swish, sigmoid, tanh])
218
- # Combine multiple operators into a single operator to avoid quantization between
219
- # them. To do this we define fusing patterns using the OperatorsSets that were created.
220
- # To group multiple sets with regard to fusing, an OperatorSetConcat can be created
221
- activations_after_conv_to_fuse = schema.OperatorSetConcat(operators_set=[any_relu, swish, prelu, sigmoid, tanh])
222
- activations_after_fc_to_fuse = schema.OperatorSetConcat(operators_set=[any_relu, swish, sigmoid])
223
- any_binary = schema.OperatorSetConcat(operators_set=[add, sub, mul, div])
224
-
225
- # ------------------- #
226
- # Fusions
227
- # ------------------- #
228
- fusing_patterns.append(schema.Fusing(operator_groups=(conv, activations_after_conv_to_fuse)))
229
- fusing_patterns.append(schema.Fusing(operator_groups=(fc, activations_after_fc_to_fuse)))
230
- fusing_patterns.append(schema.Fusing(operator_groups=(any_binary, any_relu)))
231
-
232
- # Create a TargetPlatformModel and set its default quantization config.
233
- # This default configuration will be used for all operations
234
- # unless specified otherwise (see OperatorsSet, for example):
235
- generated_tpm = schema.TargetPlatformModel(
236
- default_qco=default_configuration_options,
237
- tpc_minor_version=3,
238
- tpc_patch_version=0,
239
- tpc_platform_type=IMX500_TP_MODEL,
240
- operator_set=tuple(operator_set),
241
- fusing_patterns=tuple(fusing_patterns),
242
- add_metadata=True,
243
- name=name,
244
- is_simd_padding=True)
245
-
246
- return generated_tpm
@@ -1,135 +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
- from model_compression_toolkit.target_platform_capabilities.schema.mct_current_schema import TargetPlatformModel
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
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
35
-
36
- from model_compression_toolkit.target_platform_capabilities.tpc_models.imx500_tpc.v3.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.v3 import __version__ as TPC_VERSION
39
-
40
- tp = mct.target_platform
41
-
42
-
43
- def get_keras_tpc() -> tp.TargetPlatformCapabilities:
44
- """
45
- get a Keras TargetPlatformCapabilities object with default operation sets to layers mapping.
46
-
47
- Returns: a Keras TargetPlatformCapabilities object for the given TargetPlatformModel.
48
- """
49
- imx500_tpc_tp_model = get_tp_model()
50
- return generate_keras_tpc(name='imx500_tpc_keras_tpc', tp_model=imx500_tpc_tp_model)
51
-
52
-
53
- def generate_keras_tpc(name: str, tp_model: TargetPlatformModel):
54
- """
55
- Generates a TargetPlatformCapabilities object with default operation sets to layers mapping.
56
-
57
- Args:
58
- name: Name of the TargetPlatformCapabilities.
59
- tp_model: TargetPlatformModel object.
60
-
61
- Returns: a TargetPlatformCapabilities object for the given TargetPlatformModel.
62
- """
63
-
64
- keras_tpc = tp.TargetPlatformCapabilities(tp_model)
65
-
66
- no_quant_list = [Identity,
67
- tf.identity,
68
- Reshape,
69
- tf.reshape,
70
- Permute,
71
- tf.transpose,
72
- Flatten,
73
- Cropping2D,
74
- ZeroPadding2D,
75
- Dropout,
76
- MaxPooling2D,
77
- tf.split,
78
- tf.quantization.fake_quant_with_min_max_vars,
79
- tf.math.argmax,
80
- tf.shape,
81
- tf.math.equal,
82
- tf.gather,
83
- tf.cast,
84
- tf.unstack,
85
- tf.compat.v1.gather,
86
- tf.nn.top_k,
87
- tf.__operators__.getitem,
88
- tf.strided_slice,
89
- tf.image.combined_non_max_suppression,
90
- tf.compat.v1.shape]
91
-
92
- if FOUND_SONY_CUSTOM_LAYERS:
93
- no_quant_list.append(SSDPostProcess)
94
-
95
- with keras_tpc:
96
- tp.OperationsSetToLayers("NoQuantization", no_quant_list)
97
- tp.OperationsSetToLayers("Default16BitInout", [tf.stack,
98
- tf.concat, Concatenate])
99
- tp.OperationsSetToLayers("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("FullyConnected", [Dense],
117
- attr_mapping={KERNEL_ATTR: DefaultDict(default_value=KERAS_KERNEL),
118
- BIAS_ATTR: DefaultDict(default_value=BIAS)})
119
- tp.OperationsSetToLayers("AnyReLU", [tf.nn.relu,
120
- tf.nn.relu6,
121
- tf.nn.leaky_relu,
122
- ReLU,
123
- LeakyReLU,
124
- tp.LayerFilterParams(Activation, activation="relu"),
125
- tp.LayerFilterParams(Activation, activation="leaky_relu")])
126
- tp.OperationsSetToLayers("Add", [tf.add, Add])
127
- tp.OperationsSetToLayers("Sub", [tf.subtract, Subtract])
128
- tp.OperationsSetToLayers("Mul", [tf.math.multiply, Multiply])
129
- tp.OperationsSetToLayers("Div", [tf.math.divide, tf.math.truediv])
130
- tp.OperationsSetToLayers("PReLU", [PReLU])
131
- tp.OperationsSetToLayers("Swish", [tf.nn.swish, tp.LayerFilterParams(Activation, activation="swish")])
132
- tp.OperationsSetToLayers("Sigmoid", [tf.nn.sigmoid, tp.LayerFilterParams(Activation, activation="sigmoid")])
133
- tp.OperationsSetToLayers("Tanh", [tf.nn.tanh, tp.LayerFilterParams(Activation, activation="tanh")])
134
-
135
- return keras_tpc
@@ -1,113 +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, flatten, reshape, split, unsqueeze, dropout, sigmoid, tanh, chunk, unbind, topk, \
20
- gather, equal, transpose, permute, argmax, squeeze, multiply, subtract
21
- from torch.nn import Conv2d, Linear, ConvTranspose2d, MaxPool2d
22
- from torch.nn import Dropout, Flatten, Hardtanh, Identity
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.target_platform_capabilities.schema.mct_current_schema import TargetPlatformModel
27
- from model_compression_toolkit.defaultdict import DefaultDict
28
- from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, BIAS_ATTR, PYTORCH_KERNEL, \
29
- BIAS
30
- from model_compression_toolkit.target_platform_capabilities.tpc_models.imx500_tpc.v3.tp_model import get_tp_model
31
- import model_compression_toolkit as mct
32
- from model_compression_toolkit.target_platform_capabilities.tpc_models.imx500_tpc.v3 import __version__ as TPC_VERSION
33
-
34
- tp = mct.target_platform
35
-
36
-
37
- def get_pytorch_tpc() -> tp.TargetPlatformCapabilities:
38
- """
39
- get a Pytorch TargetPlatformCapabilities object with default operation sets to layers mapping.
40
-
41
- Returns: a Pytorch TargetPlatformCapabilities object for the given TargetPlatformModel.
42
- """
43
- imx500_tpc_tp_model = get_tp_model()
44
- return generate_pytorch_tpc(name='imx500_tpc_pytorch_tpc', tp_model=imx500_tpc_tp_model)
45
-
46
-
47
- def generate_pytorch_tpc(name: str, tp_model: TargetPlatformModel):
48
- """
49
- Generates a TargetPlatformCapabilities object with default operation sets to layers mapping.
50
- Args:
51
- name: Name of the TargetPlatformModel.
52
- tp_model: TargetPlatformModel object.
53
- Returns: a TargetPlatformCapabilities object for the given TargetPlatformModel.
54
- """
55
-
56
- pytorch_tpc = tp.TargetPlatformCapabilities(tp_model)
57
-
58
- # we provide attributes mapping that maps each layer type in the operations set
59
- # that has weights attributes with provided quantization config (in the tp model) to
60
- # its framework-specific attribute name.
61
- # note that a DefaultDict should be provided if not all the layer types in the
62
- # operation set are provided separately in the mapping.
63
- pytorch_linear_attr_mapping = {KERNEL_ATTR: DefaultDict(default_value=PYTORCH_KERNEL),
64
- BIAS_ATTR: DefaultDict(default_value=BIAS)}
65
-
66
- with pytorch_tpc:
67
- tp.OperationsSetToLayers("NoQuantization", [Identity,
68
- Dropout,
69
- Flatten,
70
- dropout,
71
- flatten,
72
- split,
73
- operator.getitem,
74
- reshape,
75
- unsqueeze,
76
- chunk,
77
- unbind,
78
- torch.Tensor.size,
79
- permute,
80
- transpose,
81
- equal,
82
- argmax,
83
- gather,
84
- topk,
85
- squeeze,
86
- MaxPool2d])
87
- tp.OperationsSetToLayers("Default16BitInout",
88
- [torch.stack, torch.cat, torch.concat, torch.concatenate])
89
-
90
- tp.OperationsSetToLayers("Conv", [Conv2d, ConvTranspose2d],
91
- attr_mapping=pytorch_linear_attr_mapping)
92
- tp.OperationsSetToLayers("FullyConnected", [Linear],
93
- attr_mapping=pytorch_linear_attr_mapping)
94
- tp.OperationsSetToLayers("AnyReLU", [torch.relu,
95
- ReLU,
96
- ReLU6,
97
- LeakyReLU,
98
- relu,
99
- relu6,
100
- leaky_relu,
101
- tp.LayerFilterParams(Hardtanh, min_val=0),
102
- tp.LayerFilterParams(hardtanh, min_val=0)])
103
-
104
- tp.OperationsSetToLayers("Add", [operator.add, add])
105
- tp.OperationsSetToLayers("Sub", [operator.sub, sub, subtract])
106
- tp.OperationsSetToLayers("Mul", [operator.mul, mul, multiply])
107
- tp.OperationsSetToLayers("Div", [operator.truediv, div])
108
- tp.OperationsSetToLayers("PReLU", [PReLU, prelu])
109
- tp.OperationsSetToLayers("Swish", [SiLU, silu, Hardswish, hardswish])
110
- tp.OperationsSetToLayers("Sigmoid", [Sigmoid, sigmoid])
111
- tp.OperationsSetToLayers("Tanh", [Tanh, tanh])
112
-
113
- return pytorch_tpc
@@ -1,16 +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
- __version__ = 'v3_lut'