mct-nightly 2.2.0.20250105.534__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.20250105.534.dist-info → mct_nightly-2.2.0.20250107.15510.dist-info}/METADATA +1 -1
  2. {mct_nightly-2.2.0.20250105.534.dist-info → mct_nightly-2.2.0.20250107.15510.dist-info}/RECORD +43 -78
  3. {mct_nightly-2.2.0.20250105.534.dist-info → mct_nightly-2.2.0.20250107.15510.dist-info}/WHEEL +1 -1
  4. model_compression_toolkit/__init__.py +1 -1
  5. model_compression_toolkit/core/__init__.py +1 -1
  6. model_compression_toolkit/core/common/graph/memory_graph/compute_graph_max_cut.py +1 -1
  7. model_compression_toolkit/core/common/graph/memory_graph/cut.py +5 -2
  8. model_compression_toolkit/core/common/graph/memory_graph/max_cut_astar.py +25 -25
  9. model_compression_toolkit/core/common/quantization/quantization_config.py +19 -1
  10. model_compression_toolkit/core/keras/back2framework/keras_model_builder.py +1 -33
  11. model_compression_toolkit/core/keras/graph_substitutions/substitutions/conv_funcs_to_layer.py +2 -2
  12. model_compression_toolkit/core/keras/resource_utilization_data_facade.py +11 -1
  13. model_compression_toolkit/core/pytorch/graph_substitutions/substitutions/matmul_decomposition.py +499 -0
  14. model_compression_toolkit/core/pytorch/pytorch_implementation.py +3 -0
  15. model_compression_toolkit/core/pytorch/resource_utilization_data_facade.py +11 -3
  16. model_compression_toolkit/gptq/keras/quantization_facade.py +10 -1
  17. model_compression_toolkit/gptq/pytorch/quantization_facade.py +10 -1
  18. model_compression_toolkit/pruning/keras/pruning_facade.py +8 -2
  19. model_compression_toolkit/pruning/pytorch/pruning_facade.py +8 -2
  20. model_compression_toolkit/ptq/keras/quantization_facade.py +10 -1
  21. model_compression_toolkit/ptq/pytorch/quantization_facade.py +9 -1
  22. model_compression_toolkit/qat/__init__.py +5 -2
  23. model_compression_toolkit/qat/keras/quantization_facade.py +9 -1
  24. model_compression_toolkit/qat/pytorch/quantization_facade.py +9 -1
  25. model_compression_toolkit/target_platform_capabilities/schema/mct_current_schema.py +1 -1
  26. model_compression_toolkit/target_platform_capabilities/schema/v1.py +63 -55
  27. model_compression_toolkit/target_platform_capabilities/target_platform/targetplatform2framework/attach2fw.py +29 -18
  28. model_compression_toolkit/target_platform_capabilities/target_platform/targetplatform2framework/attach2keras.py +78 -57
  29. model_compression_toolkit/target_platform_capabilities/target_platform/targetplatform2framework/attach2pytorch.py +69 -54
  30. model_compression_toolkit/target_platform_capabilities/target_platform/targetplatform2framework/operations_to_layers.py +2 -4
  31. model_compression_toolkit/target_platform_capabilities/target_platform/targetplatform2framework/target_platform_capabilities.py +0 -10
  32. model_compression_toolkit/target_platform_capabilities/tpc_io_handler.py +93 -0
  33. model_compression_toolkit/target_platform_capabilities/tpc_models/get_target_platform_capabilities.py +46 -28
  34. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/latest/__init__.py +6 -5
  35. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v1/tp_model.py +51 -19
  36. model_compression_toolkit/target_platform_capabilities/tpc_models/qnnpack_tpc/latest/__init__.py +8 -4
  37. model_compression_toolkit/target_platform_capabilities/tpc_models/qnnpack_tpc/v1/tp_model.py +19 -9
  38. model_compression_toolkit/target_platform_capabilities/tpc_models/tflite_tpc/latest/__init__.py +7 -4
  39. model_compression_toolkit/target_platform_capabilities/tpc_models/tflite_tpc/v1/tp_model.py +46 -32
  40. model_compression_toolkit/xquant/keras/keras_report_utils.py +11 -3
  41. model_compression_toolkit/xquant/pytorch/pytorch_report_utils.py +10 -2
  42. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/target_platform_capabilities.py +0 -98
  43. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v1/tpc_keras.py +0 -129
  44. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v1/tpc_pytorch.py +0 -108
  45. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v1_lut/__init__.py +0 -16
  46. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v1_lut/tp_model.py +0 -217
  47. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v1_lut/tpc_keras.py +0 -130
  48. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v1_lut/tpc_pytorch.py +0 -109
  49. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v1_pot/__init__.py +0 -16
  50. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v1_pot/tp_model.py +0 -215
  51. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v1_pot/tpc_keras.py +0 -130
  52. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v1_pot/tpc_pytorch.py +0 -110
  53. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v2/__init__.py +0 -16
  54. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v2/tp_model.py +0 -222
  55. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v2/tpc_keras.py +0 -132
  56. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v2/tpc_pytorch.py +0 -110
  57. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v2_lut/__init__.py +0 -16
  58. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v2_lut/tp_model.py +0 -219
  59. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v2_lut/tpc_keras.py +0 -132
  60. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v2_lut/tpc_pytorch.py +0 -109
  61. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3/__init__.py +0 -16
  62. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3/tp_model.py +0 -246
  63. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3/tpc_keras.py +0 -135
  64. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3/tpc_pytorch.py +0 -113
  65. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3_lut/__init__.py +0 -16
  66. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3_lut/tp_model.py +0 -230
  67. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3_lut/tpc_keras.py +0 -132
  68. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3_lut/tpc_pytorch.py +0 -110
  69. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v4/__init__.py +0 -16
  70. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v4/tp_model.py +0 -332
  71. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v4/tpc_keras.py +0 -140
  72. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v4/tpc_pytorch.py +0 -122
  73. model_compression_toolkit/target_platform_capabilities/tpc_models/qnnpack_tpc/target_platform_capabilities.py +0 -55
  74. model_compression_toolkit/target_platform_capabilities/tpc_models/qnnpack_tpc/v1/tpc_keras.py +0 -89
  75. model_compression_toolkit/target_platform_capabilities/tpc_models/qnnpack_tpc/v1/tpc_pytorch.py +0 -78
  76. model_compression_toolkit/target_platform_capabilities/tpc_models/tflite_tpc/target_platform_capabilities.py +0 -55
  77. model_compression_toolkit/target_platform_capabilities/tpc_models/tflite_tpc/v1/tpc_keras.py +0 -118
  78. model_compression_toolkit/target_platform_capabilities/tpc_models/tflite_tpc/v1/tpc_pytorch.py +0 -100
  79. {mct_nightly-2.2.0.20250105.534.dist-info → mct_nightly-2.2.0.20250107.15510.dist-info}/LICENSE.md +0 -0
  80. {mct_nightly-2.2.0.20250105.534.dist-info → mct_nightly-2.2.0.20250107.15510.dist-info}/top_level.txt +0 -0
@@ -1,215 +0,0 @@
1
- # Copyright 2023 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
-
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_pot_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
- # We define a default quantization config for all non-specified weights attributes.
58
- default_weight_attr_config = AttributeQuantizationConfig(
59
- weights_quantization_method=tp.QuantizationMethod.POWER_OF_TWO,
60
- weights_n_bits=8,
61
- weights_per_channel_threshold=False,
62
- enable_weights_quantization=False,
63
- lut_values_bitwidth=None)
64
-
65
- # We define a quantization config to quantize the kernel (for layers where there is a kernel attribute).
66
- kernel_base_config = AttributeQuantizationConfig(
67
- weights_quantization_method=tp.QuantizationMethod.POWER_OF_TWO,
68
- weights_n_bits=8,
69
- weights_per_channel_threshold=True,
70
- enable_weights_quantization=True,
71
- lut_values_bitwidth=None)
72
-
73
- # We define a quantization config to quantize the bias (for layers where there is a bias attribute).
74
- bias_config = AttributeQuantizationConfig(
75
- weights_quantization_method=tp.QuantizationMethod.POWER_OF_TWO,
76
- weights_n_bits=FLOAT_BITWIDTH,
77
- weights_per_channel_threshold=False,
78
- enable_weights_quantization=False,
79
- lut_values_bitwidth=None)
80
-
81
- # Create a quantization config.
82
- # A quantization configuration defines how an operator
83
- # should be quantized on the modeled hardware:
84
-
85
- # We define a default config for operation without kernel attribute.
86
- # This is the default config that should be used for non-linear operations.
87
- eight_bits_default = schema.OpQuantizationConfig(
88
- default_weight_attr_config=default_weight_attr_config,
89
- attr_weights_configs_mapping={},
90
- activation_quantization_method=tp.QuantizationMethod.POWER_OF_TWO,
91
- activation_n_bits=8,
92
- supported_input_activation_n_bits=8,
93
- enable_activation_quantization=True,
94
- quantization_preserving=False,
95
- fixed_scale=None,
96
- fixed_zero_point=None,
97
- simd_size=32,
98
- signedness=Signedness.AUTO)
99
-
100
- # We define an 8-bit config for linear operations quantization, that include a kernel and bias attributes.
101
- linear_eight_bits = schema.OpQuantizationConfig(
102
- activation_quantization_method=tp.QuantizationMethod.POWER_OF_TWO,
103
- default_weight_attr_config=default_weight_attr_config,
104
- attr_weights_configs_mapping={KERNEL_ATTR: kernel_base_config, BIAS_ATTR: bias_config},
105
- activation_n_bits=8,
106
- supported_input_activation_n_bits=8,
107
- enable_activation_quantization=True,
108
- quantization_preserving=False,
109
- fixed_scale=None,
110
- fixed_zero_point=None,
111
- simd_size=32,
112
- signedness=Signedness.AUTO)
113
-
114
- # To quantize a model using mixed-precision, create
115
- # a list with more than one OpQuantizationConfig.
116
- # In this example, we quantize some operations' weights
117
- # using 2, 4 or 8 bits, and when using 2 or 4 bits, it's possible
118
- # to quantize the operations' activations using LUT.
119
- four_bits = linear_eight_bits.clone_and_edit(attr_to_edit={KERNEL_ATTR: {WEIGHTS_N_BITS: 4}},
120
- simd_size=linear_eight_bits.simd_size * 2)
121
- two_bits = linear_eight_bits.clone_and_edit(attr_to_edit={KERNEL_ATTR: {WEIGHTS_N_BITS: 2}},
122
- simd_size=linear_eight_bits.simd_size * 4)
123
- mixed_precision_cfg_list = [linear_eight_bits, four_bits, two_bits]
124
-
125
- return linear_eight_bits, mixed_precision_cfg_list, eight_bits_default
126
-
127
-
128
- def generate_tp_model(default_config: OpQuantizationConfig,
129
- base_config: OpQuantizationConfig,
130
- mixed_precision_cfg_list: List[OpQuantizationConfig],
131
- name: str) -> TargetPlatformModel:
132
- """
133
- Generates TargetPlatformModel with default defined Operators Sets, based on the given base configuration and
134
- mixed-precision configurations options list.
135
-
136
- Args
137
- default_config: A default OpQuantizationConfig to set as the TP model default configuration.
138
- base_config: An OpQuantizationConfig to set as the TargetPlatformModel base configuration for mixed-precision purposes only.
139
- mixed_precision_cfg_list: A list of OpQuantizationConfig to be used as the TP model mixed-precision
140
- quantization configuration options.
141
- name: The name of the TargetPlatformModel.
142
-
143
- Returns: A TargetPlatformModel object.
144
-
145
- """
146
- # Create a QuantizationConfigOptions, which defines a set
147
- # of possible configurations to consider when quantizing a set of operations (in mixed-precision, for example).
148
- # If the QuantizationConfigOptions contains only one configuration,
149
- # this configuration will be used for the operation quantization:
150
- default_configuration_options = schema.QuantizationConfigOptions(quantization_configurations=tuple([default_config]))
151
-
152
- # Create Mixed-Precision quantization configuration options from the given list of OpQuantizationConfig objects
153
- mixed_precision_configuration_options = schema.QuantizationConfigOptions(quantization_configurations=tuple(mixed_precision_cfg_list),
154
- base_config=base_config)
155
-
156
- # Create an OperatorsSet to represent a set of operations.
157
- # Each OperatorsSet has a unique label.
158
- # If a quantization configuration options is passed, these options will
159
- # be used for operations that will be attached to this set's label.
160
- # Otherwise, it will be a configure-less set (used in fusing):
161
- operator_set = []
162
- fusing_patterns = []
163
-
164
- # May suit for operations like: Dropout, Reshape, etc.
165
- operator_set.append(schema.OperatorsSet(name="NoQuantization",
166
- qc_options=default_configuration_options.clone_and_edit(
167
- enable_activation_quantization=False)
168
- .clone_and_edit_weight_attribute(enable_weights_quantization=False)))
169
-
170
- # Define operator sets that use mixed_precision_configuration_options:
171
- conv = schema.OperatorsSet(name="Conv", qc_options=mixed_precision_configuration_options)
172
- fc = schema.OperatorsSet(name="FullyConnected", qc_options=mixed_precision_configuration_options)
173
-
174
- # Define operations sets without quantization configuration
175
- # options (useful for creating fusing patterns, for example):
176
- any_relu = schema.OperatorsSet(name="AnyReLU")
177
- add = schema.OperatorsSet(name="Add")
178
- sub = schema.OperatorsSet(name="Sub")
179
- mul = schema.OperatorsSet(name="Mul")
180
- div = schema.OperatorsSet(name="Div")
181
- prelu = schema.OperatorsSet(name="PReLU")
182
- swish = schema.OperatorsSet(name="Swish")
183
- sigmoid = schema.OperatorsSet(name="Sigmoid")
184
- tanh = schema.OperatorsSet(name="Tanh")
185
-
186
- operator_set.extend([conv, fc, any_relu, add, sub, mul, div, prelu, swish, sigmoid, tanh])
187
-
188
- # Combine multiple operators into a single operator to avoid quantization between
189
- # them. To do this we define fusing patterns using the OperatorsSets that were created.
190
- # To group multiple sets with regard to fusing, an OperatorSetConcat can be created
191
- activations_after_conv_to_fuse = schema.OperatorSetConcat(operators_set=[any_relu, swish, prelu, sigmoid, tanh])
192
- activations_after_fc_to_fuse = schema.OperatorSetConcat(operators_set=[any_relu, swish, sigmoid])
193
- any_binary = schema.OperatorSetConcat(operators_set=[add, sub, mul, div])
194
-
195
- # ------------------- #
196
- # Fusions
197
- # ------------------- #
198
- fusing_patterns.append(schema.Fusing(operator_groups=(conv, activations_after_conv_to_fuse)))
199
- fusing_patterns.append(schema.Fusing(operator_groups=(fc, activations_after_fc_to_fuse)))
200
- fusing_patterns.append(schema.Fusing(operator_groups=(any_binary, any_relu)))
201
-
202
- # Create a TargetPlatformModel and set its default quantization config.
203
- # This default configuration will be used for all operations
204
- # unless specified otherwise (see OperatorsSet, for example):
205
- generated_tpc = schema.TargetPlatformModel(
206
- default_qco=default_configuration_options,
207
- tpc_minor_version=1,
208
- tpc_patch_version=0,
209
- tpc_platform_type=IMX500_TP_MODEL,
210
- operator_set=tuple(operator_set),
211
- fusing_patterns=tuple(fusing_patterns),
212
- name=name,
213
- add_metadata=False,
214
- is_simd_padding=True)
215
- return generated_tpc
@@ -1,130 +0,0 @@
1
- # Copyright 2023 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
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
35
-
36
- from model_compression_toolkit.target_platform_capabilities.tpc_models.imx500_tpc.v1_pot.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.v1_pot 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
- Returns: a Keras TargetPlatformCapabilities object for the given TargetPlatformModel.
47
- """
48
- imx500_pot_tpc_tp_model = get_tp_model()
49
- return generate_keras_tpc(name='imx500_pot_tpc_keras_tpc', tp_model=imx500_pot_tpc_tp_model)
50
-
51
-
52
- def generate_keras_tpc(name: str, tp_model: schema.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)
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
-
94
- tp.OperationsSetToLayers("Conv",
95
- [Conv2D,
96
- DepthwiseConv2D,
97
- Conv2DTranspose,
98
- tf.nn.conv2d,
99
- tf.nn.depthwise_conv2d,
100
- tf.nn.conv2d_transpose],
101
- # we provide attributes mapping that maps each layer type in the operations set
102
- # that has weights attributes with provided quantization config (in the tp model) to
103
- # its framework-specific attribute name.
104
- # note that a DefaultDict should be provided if not all the layer types in the
105
- # operation set are provided separately in the mapping.
106
- attr_mapping={
107
- KERNEL_ATTR: DefaultDict({
108
- DepthwiseConv2D: KERAS_DEPTHWISE_KERNEL,
109
- tf.nn.depthwise_conv2d: KERAS_DEPTHWISE_KERNEL}, default_value=KERAS_KERNEL),
110
- BIAS_ATTR: DefaultDict(default_value=BIAS)})
111
- tp.OperationsSetToLayers("FullyConnected", [Dense],
112
- attr_mapping={KERNEL_ATTR: DefaultDict(default_value=KERAS_KERNEL),
113
- BIAS_ATTR: DefaultDict(default_value=BIAS)})
114
- tp.OperationsSetToLayers("AnyReLU", [tf.nn.relu,
115
- tf.nn.relu6,
116
- tf.nn.leaky_relu,
117
- ReLU,
118
- LeakyReLU,
119
- tp.LayerFilterParams(Activation, activation="relu"),
120
- tp.LayerFilterParams(Activation, activation="leaky_relu")])
121
- tp.OperationsSetToLayers("Add", [tf.add, Add])
122
- tp.OperationsSetToLayers("Sub", [tf.subtract, Subtract])
123
- tp.OperationsSetToLayers("Mul", [tf.math.multiply, Multiply])
124
- tp.OperationsSetToLayers("Div", [tf.math.divide])
125
- tp.OperationsSetToLayers("PReLU", [PReLU])
126
- tp.OperationsSetToLayers("Swish", [tf.nn.swish, tp.LayerFilterParams(Activation, activation="swish")])
127
- tp.OperationsSetToLayers("Sigmoid", [tf.nn.sigmoid, tp.LayerFilterParams(Activation, activation="sigmoid")])
128
- tp.OperationsSetToLayers("Tanh", [tf.nn.tanh, tp.LayerFilterParams(Activation, activation="tanh")])
129
-
130
- return keras_tpc
@@ -1,110 +0,0 @@
1
- # Copyright 2023 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
- import model_compression_toolkit.target_platform_capabilities.schema.mct_current_schema as schema
27
- from model_compression_toolkit.defaultdict import DefaultDict
28
- from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, PYTORCH_KERNEL, BIAS_ATTR, \
29
- BIAS
30
- from model_compression_toolkit.target_platform_capabilities.tpc_models.imx500_tpc.v1_pot.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.v1_pot import (
33
- __version__ as TPC_VERSION)
34
-
35
- tp = mct.target_platform
36
-
37
-
38
- def get_pytorch_tpc() -> tp.TargetPlatformCapabilities:
39
- """
40
- get a Pytorch TargetPlatformCapabilities object with default operation sets to layers mapping.
41
- Returns: a Pytorch TargetPlatformCapabilities object for the given TargetPlatformModel.
42
- """
43
- imx500_pot_tpc_tp_model = get_tp_model()
44
- return generate_pytorch_tpc(name='imx500_pot_tpc_pytorch_tpc', tp_model=imx500_pot_tpc_tp_model)
45
-
46
-
47
- def generate_pytorch_tpc(name: str, tp_model: schema.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", [Dropout,
68
- Flatten,
69
- dropout,
70
- flatten,
71
- split,
72
- operator.getitem,
73
- reshape,
74
- unsqueeze,
75
- BatchNorm2d,
76
- chunk,
77
- unbind,
78
- torch.Tensor.size,
79
- permute,
80
- transpose,
81
- equal,
82
- argmax,
83
- gather,
84
- topk,
85
- squeeze])
86
-
87
- tp.OperationsSetToLayers("Conv", [Conv2d, ConvTranspose2d],
88
- attr_mapping=pytorch_linear_attr_mapping)
89
- tp.OperationsSetToLayers("FullyConnected", [Linear],
90
- attr_mapping=pytorch_linear_attr_mapping)
91
- tp.OperationsSetToLayers("AnyReLU", [torch.relu,
92
- ReLU,
93
- ReLU6,
94
- LeakyReLU,
95
- relu,
96
- relu6,
97
- leaky_relu,
98
- tp.LayerFilterParams(Hardtanh, min_val=0),
99
- tp.LayerFilterParams(hardtanh, min_val=0)])
100
-
101
- tp.OperationsSetToLayers("Add", [operator.add, add])
102
- tp.OperationsSetToLayers("Sub", [operator.sub, sub])
103
- tp.OperationsSetToLayers("Mul", [operator.mul, mul])
104
- tp.OperationsSetToLayers("Div", [operator.truediv, div])
105
- tp.OperationsSetToLayers("PReLU", [PReLU, prelu])
106
- tp.OperationsSetToLayers("Swish", [SiLU, silu, Hardswish, hardswish])
107
- tp.OperationsSetToLayers("Sigmoid", [Sigmoid, sigmoid])
108
- tp.OperationsSetToLayers("Tanh", [Tanh, tanh])
109
-
110
- 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__ = 'v2'