mct-nightly 2.2.0.20241222.533__py3-none-any.whl → 2.2.0.20241224.532__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 (31) hide show
  1. {mct_nightly-2.2.0.20241222.533.dist-info → mct_nightly-2.2.0.20241224.532.dist-info}/METADATA +1 -1
  2. {mct_nightly-2.2.0.20241222.533.dist-info → mct_nightly-2.2.0.20241224.532.dist-info}/RECORD +29 -28
  3. model_compression_toolkit/__init__.py +1 -1
  4. model_compression_toolkit/core/common/graph/base_graph.py +1 -1
  5. model_compression_toolkit/core/common/graph/base_node.py +3 -3
  6. model_compression_toolkit/core/common/quantization/set_node_quantization_config.py +4 -4
  7. model_compression_toolkit/core/common/substitutions/shift_negative_activation.py +2 -2
  8. model_compression_toolkit/target_platform_capabilities/schema/mct_current_schema.py +1 -0
  9. model_compression_toolkit/target_platform_capabilities/schema/schema_functions.py +4 -5
  10. model_compression_toolkit/target_platform_capabilities/schema/v1.py +66 -172
  11. model_compression_toolkit/target_platform_capabilities/target_platform/__init__.py +0 -1
  12. model_compression_toolkit/target_platform_capabilities/target_platform/targetplatform2framework/attach2fw.py +56 -0
  13. model_compression_toolkit/target_platform_capabilities/target_platform/targetplatform2framework/attach2keras.py +107 -0
  14. model_compression_toolkit/target_platform_capabilities/target_platform/targetplatform2framework/attach2pytorch.py +91 -0
  15. model_compression_toolkit/target_platform_capabilities/target_platform/targetplatform2framework/operations_to_layers.py +1 -1
  16. model_compression_toolkit/target_platform_capabilities/target_platform/targetplatform2framework/target_platform_capabilities.py +7 -4
  17. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v1/tp_model.py +50 -51
  18. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v1_lut/tp_model.py +54 -52
  19. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v1_pot/tp_model.py +57 -53
  20. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v2/tp_model.py +52 -51
  21. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v2_lut/tp_model.py +53 -51
  22. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3/tp_model.py +59 -57
  23. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3_lut/tp_model.py +54 -52
  24. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v4/tp_model.py +90 -83
  25. model_compression_toolkit/target_platform_capabilities/tpc_models/qnnpack_tpc/v1/tp_model.py +26 -24
  26. model_compression_toolkit/target_platform_capabilities/tpc_models/tflite_tpc/v1/tp_model.py +57 -55
  27. model_compression_toolkit/target_platform_capabilities/target_platform/current_tp_model.py +0 -67
  28. model_compression_toolkit/target_platform_capabilities/target_platform/target_platform_model.py +0 -30
  29. {mct_nightly-2.2.0.20241222.533.dist-info → mct_nightly-2.2.0.20241224.532.dist-info}/LICENSE.md +0 -0
  30. {mct_nightly-2.2.0.20241222.533.dist-info → mct_nightly-2.2.0.20241224.532.dist-info}/WHEEL +0 -0
  31. {mct_nightly-2.2.0.20241222.533.dist-info → mct_nightly-2.2.0.20241224.532.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,91 @@
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
+ from model_compression_toolkit import DefaultDict
29
+ from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, PYTORCH_KERNEL, BIAS, \
30
+ BIAS_ATTR
31
+ from model_compression_toolkit.target_platform_capabilities.schema.mct_current_schema import OperatorSetNames
32
+ from model_compression_toolkit.target_platform_capabilities.target_platform import LayerFilterParams
33
+ from model_compression_toolkit.target_platform_capabilities.target_platform.targetplatform2framework.attach2fw import \
34
+ AttachTpModelToFw
35
+
36
+
37
+ class AttachTpModelToPytorch(AttachTpModelToFw):
38
+ def __init__(self):
39
+ super().__init__()
40
+
41
+ self._opset2layer = {
42
+ OperatorSetNames.OPSET_CONV.value: [Conv2d],
43
+ OperatorSetNames.OPSET_CONV_TRANSPOSE.value: [ConvTranspose2d],
44
+ OperatorSetNames.OPSET_FULLY_CONNECTED.value: [Linear],
45
+ OperatorSetNames.OPSET_CONCATENATE.value: [torch.cat, torch.concat, torch.concatenate],
46
+ OperatorSetNames.OPSET_STACK.value: [torch.stack],
47
+ OperatorSetNames.OPSET_UNSTACK.value: [unbind],
48
+ OperatorSetNames.OPSET_GATHER.value: [gather],
49
+ OperatorSetNames.OPSET_EXPAND.value: [torch.Tensor.expand],
50
+ OperatorSetNames.OPSET_BATCH_NORM.value: [BatchNorm2d],
51
+ OperatorSetNames.OPSET_RELU.value: [torch.relu, ReLU, relu],
52
+ OperatorSetNames.OPSET_RELU6.value: [ReLU6, relu6],
53
+ OperatorSetNames.OPSET_LEAKY_RELU.value: [LeakyReLU, leaky_relu],
54
+ OperatorSetNames.OPSET_HARD_TANH.value: [LayerFilterParams(Hardtanh, min_val=0),
55
+ LayerFilterParams(hardtanh, min_val=0)],
56
+ OperatorSetNames.OPSET_ADD.value: [operator.add, add],
57
+ OperatorSetNames.OPSET_SUB.value: [operator.sub, sub, subtract],
58
+ OperatorSetNames.OPSET_MUL.value: [operator.mul, mul, multiply],
59
+ OperatorSetNames.OPSET_DIV.value: [operator.truediv, div, divide],
60
+ OperatorSetNames.OPSET_MIN.value: [minimum],
61
+ OperatorSetNames.OPSET_MAX.value: [maximum],
62
+ OperatorSetNames.OPSET_PRELU.value: [PReLU, prelu],
63
+ OperatorSetNames.OPSET_SWISH.value: [SiLU, silu],
64
+ OperatorSetNames.OPSET_SIGMOID.value: [Sigmoid, sigmoid, F.sigmoid],
65
+ OperatorSetNames.OPSET_TANH.value: [Tanh, tanh, F.tanh],
66
+ OperatorSetNames.OPSET_GELU.value: [GELU, gelu],
67
+ OperatorSetNames.OPSET_HARDSIGMOID.value: [Hardsigmoid, hardsigmoid],
68
+ OperatorSetNames.OPSET_HARDSWISH.value: [Hardswish, hardswish],
69
+ OperatorSetNames.OPSET_FLATTEN.value: [Flatten, flatten],
70
+ OperatorSetNames.OPSET_GET_ITEM.value: [operator.getitem],
71
+ OperatorSetNames.OPSET_RESHAPE.value: [reshape],
72
+ OperatorSetNames.OPSET_UNSQUEEZE.value: [unsqueeze],
73
+ OperatorSetNames.OPSET_SQUEEZE.value: [squeeze],
74
+ OperatorSetNames.OPSET_PERMUTE.value: [permute],
75
+ OperatorSetNames.OPSET_TRANSPOSE.value: [transpose],
76
+ OperatorSetNames.OPSET_DROPOUT.value: [Dropout, dropout],
77
+ OperatorSetNames.OPSET_SPLIT.value: [split],
78
+ OperatorSetNames.OPSET_CHUNK.value: [chunk],
79
+ OperatorSetNames.OPSET_MAXPOOL.value: [MaxPool2d],
80
+ OperatorSetNames.OPSET_SIZE.value: [torch.Tensor.size],
81
+ OperatorSetNames.OPSET_SHAPE.value: [torch.Tensor.shape],
82
+ OperatorSetNames.OPSET_EQUAL.value: [equal],
83
+ OperatorSetNames.OPSET_ARGMAX.value: [argmax],
84
+ OperatorSetNames.OPSET_TOPK.value: [topk],
85
+ }
86
+
87
+ pytorch_linear_attr_mapping = {KERNEL_ATTR: DefaultDict(default_value=PYTORCH_KERNEL),
88
+ BIAS_ATTR: DefaultDict(default_value=BIAS)}
89
+ self._opset2attr_mapping = {OperatorSetNames.OPSET_CONV.value: pytorch_linear_attr_mapping,
90
+ OperatorSetNames.OPSET_CONV_TRANSPOSE.value: pytorch_linear_attr_mapping,
91
+ OperatorSetNames.OPSET_FULLY_CONNECTED.value: pytorch_linear_attr_mapping}
@@ -90,7 +90,7 @@ class OperationsToLayers:
90
90
  return o.layers
91
91
  if isinstance(op, OperatorSetConcat): # If its a concat - return all layers from all OperatorsSets that in the OperatorSetConcat
92
92
  layers = []
93
- for o in op.op_set_list:
93
+ for o in op.operators_set:
94
94
  layers.extend(self.get_layers_by_op(o))
95
95
  return layers
96
96
  Logger.warning(f'{op.name} is not in model.')
@@ -100,8 +100,10 @@ class TargetPlatformCapabilities(ImmutableClass):
100
100
 
101
101
  """
102
102
  res = []
103
+ if self.tp_model.fusing_patterns is None:
104
+ return res
103
105
  for p in self.tp_model.fusing_patterns:
104
- ops = [self.get_layers_by_opset(x) for x in p.operator_groups_list]
106
+ ops = [self.get_layers_by_opset(x) for x in p.operator_groups]
105
107
  res.extend(itertools.product(*ops))
106
108
  return [list(x) for x in res]
107
109
 
@@ -207,9 +209,10 @@ class TargetPlatformCapabilities(ImmutableClass):
207
209
  Remove OperatorSets names from the list of the unused sets (so a warning
208
210
  will not be displayed).
209
211
  """
210
- for f in self.tp_model.fusing_patterns:
211
- for s in f.operator_groups_list:
212
- self.remove_opset_from_not_used_list(s.name)
212
+ if self.tp_model.fusing_patterns is not None:
213
+ for f in self.tp_model.fusing_patterns:
214
+ for s in f.operator_groups:
215
+ self.remove_opset_from_not_used_list(s.name)
213
216
 
214
217
  def remove_opset_from_not_used_list(self,
215
218
  opset_to_remove: str):
@@ -153,7 +153,54 @@ def generate_tp_model(default_config: OpQuantizationConfig,
153
153
  # of possible configurations to consider when quantizing a set of operations (in mixed-precision, for example).
154
154
  # If the QuantizationConfigOptions contains only one configuration,
155
155
  # this configuration will be used for the operation quantization:
156
- default_configuration_options = schema.QuantizationConfigOptions([default_config])
156
+ default_configuration_options = schema.QuantizationConfigOptions(tuple([default_config]))
157
+
158
+ # Create Mixed-Precision quantization configuration options from the given list of OpQuantizationConfig objects
159
+ mixed_precision_configuration_options = schema.QuantizationConfigOptions(tuple(mixed_precision_cfg_list),
160
+ base_config=base_config)
161
+
162
+ # Create an OperatorsSet to represent a set of operations.
163
+ # Each OperatorsSet has a unique label.
164
+ # If a quantization configuration options is passed, these options will
165
+ # be used for operations that will be attached to this set's label.
166
+ # Otherwise, it will be a configure-less set (used in fusing):
167
+ operator_set = []
168
+ fusing_patterns = []
169
+
170
+ operator_set.append(schema.OperatorsSet("NoQuantization",
171
+ default_configuration_options.clone_and_edit(enable_activation_quantization=False)
172
+ .clone_and_edit_weight_attribute(enable_weights_quantization=False)))
173
+
174
+ # Define operator sets that use mixed_precision_configuration_options:
175
+ conv = schema.OperatorsSet("Conv", mixed_precision_configuration_options)
176
+ fc = schema.OperatorsSet("FullyConnected", mixed_precision_configuration_options)
177
+
178
+ # Define operations sets without quantization configuration
179
+ # options (useful for creating fusing patterns, for example):
180
+ any_relu = schema.OperatorsSet("AnyReLU")
181
+ add = schema.OperatorsSet("Add")
182
+ sub = schema.OperatorsSet("Sub")
183
+ mul = schema.OperatorsSet("Mul")
184
+ div = schema.OperatorsSet("Div")
185
+ prelu = schema.OperatorsSet("PReLU")
186
+ swish = schema.OperatorsSet("Swish")
187
+ sigmoid = schema.OperatorsSet("Sigmoid")
188
+ tanh = schema.OperatorsSet("Tanh")
189
+
190
+ operator_set.extend([conv, fc, any_relu, add, sub, mul, div, prelu, swish, sigmoid, tanh])
191
+ # Combine multiple operators into a single operator to avoid quantization between
192
+ # them. To do this we define fusing patterns using the OperatorsSets that were created.
193
+ # To group multiple sets with regard to fusing, an OperatorSetConcat can be created
194
+ activations_after_conv_to_fuse = schema.OperatorSetConcat([any_relu, swish, prelu, sigmoid, tanh])
195
+ activations_after_fc_to_fuse = schema.OperatorSetConcat([any_relu, swish, sigmoid])
196
+ any_binary = schema.OperatorSetConcat([add, sub, mul, div])
197
+
198
+ # ------------------- #
199
+ # Fusions
200
+ # ------------------- #
201
+ fusing_patterns.append(schema.Fusing((conv, activations_after_conv_to_fuse)))
202
+ fusing_patterns.append(schema.Fusing((fc, activations_after_fc_to_fuse)))
203
+ fusing_patterns.append(schema.Fusing((any_binary, any_relu)))
157
204
 
158
205
  # Create a TargetPlatformModel and set its default quantization config.
159
206
  # This default configuration will be used for all operations
@@ -163,57 +210,9 @@ def generate_tp_model(default_config: OpQuantizationConfig,
163
210
  tpc_minor_version=1,
164
211
  tpc_patch_version=0,
165
212
  tpc_platform_type=IMX500_TP_MODEL,
213
+ operator_set=tuple(operator_set),
214
+ fusing_patterns=tuple(fusing_patterns),
166
215
  name=name,
167
216
  add_metadata=False,
168
217
  is_simd_padding=True)
169
-
170
- # To start defining the model's components (such as operator sets, and fusing patterns),
171
- # use 'with' the TargetPlatformModel instance, and create them as below:
172
- with generated_tpc:
173
- # Create an OperatorsSet to represent a set of operations.
174
- # Each OperatorsSet has a unique label.
175
- # If a quantization configuration options is passed, these options will
176
- # be used for operations that will be attached to this set's label.
177
- # Otherwise, it will be a configure-less set (used in fusing):
178
-
179
- # May suit for operations like: Dropout, Reshape, etc.
180
- default_qco = tp.get_default_quantization_config_options()
181
- schema.OperatorsSet("NoQuantization",
182
- default_qco.clone_and_edit(enable_activation_quantization=False)
183
- .clone_and_edit_weight_attribute(enable_weights_quantization=False))
184
-
185
- # Create Mixed-Precision quantization configuration options from the given list of OpQuantizationConfig objects
186
- mixed_precision_configuration_options = schema.QuantizationConfigOptions(mixed_precision_cfg_list,
187
- base_config=base_config)
188
-
189
- # Define operator sets that use mixed_precision_configuration_options:
190
- conv = schema.OperatorsSet("Conv", mixed_precision_configuration_options)
191
- fc = schema.OperatorsSet("FullyConnected", mixed_precision_configuration_options)
192
-
193
- # Define operations sets without quantization configuration
194
- # options (useful for creating fusing patterns, for example):
195
- any_relu = schema.OperatorsSet("AnyReLU")
196
- add = schema.OperatorsSet("Add")
197
- sub = schema.OperatorsSet("Sub")
198
- mul = schema.OperatorsSet("Mul")
199
- div = schema.OperatorsSet("Div")
200
- prelu = schema.OperatorsSet("PReLU")
201
- swish = schema.OperatorsSet("Swish")
202
- sigmoid = schema.OperatorsSet("Sigmoid")
203
- tanh = schema.OperatorsSet("Tanh")
204
-
205
- # Combine multiple operators into a single operator to avoid quantization between
206
- # them. To do this we define fusing patterns using the OperatorsSets that were created.
207
- # To group multiple sets with regard to fusing, an OperatorSetConcat can be created
208
- activations_after_conv_to_fuse = schema.OperatorSetConcat([any_relu, swish, prelu, sigmoid, tanh])
209
- activations_after_fc_to_fuse = schema.OperatorSetConcat([any_relu, swish, sigmoid])
210
- any_binary = schema.OperatorSetConcat([add, sub, mul, div])
211
-
212
- # ------------------- #
213
- # Fusions
214
- # ------------------- #
215
- schema.Fusing([conv, activations_after_conv_to_fuse])
216
- schema.Fusing([fc, activations_after_fc_to_fuse])
217
- schema.Fusing([any_binary, any_relu])
218
-
219
218
  return generated_tpc
@@ -19,7 +19,8 @@ import model_compression_toolkit.target_platform_capabilities.schema.mct_current
19
19
  from model_compression_toolkit.constants import FLOAT_BITWIDTH
20
20
  from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, BIAS_ATTR, WEIGHTS_N_BITS, \
21
21
  WEIGHTS_QUANTIZATION_METHOD, IMX500_TP_MODEL
22
- from model_compression_toolkit.target_platform_capabilities.schema.mct_current_schema import TargetPlatformModel, Signedness, \
22
+ from model_compression_toolkit.target_platform_capabilities.schema.mct_current_schema import TargetPlatformModel, \
23
+ Signedness, \
23
24
  AttributeQuantizationConfig, OpQuantizationConfig
24
25
 
25
26
  tp = mct.target_platform
@@ -150,7 +151,56 @@ def generate_tp_model(default_config: OpQuantizationConfig,
150
151
  # of possible configurations to consider when quantizing a set of operations (in mixed-precision, for example).
151
152
  # If the QuantizationConfigOptions contains only one configuration,
152
153
  # this configuration will be used for the operation quantization:
153
- default_configuration_options = schema.QuantizationConfigOptions([default_config])
154
+ default_configuration_options = schema.QuantizationConfigOptions(tuple([default_config]))
155
+
156
+ # Create Mixed-Precision quantization configuration options from the given list of OpQuantizationConfig objects
157
+ mixed_precision_configuration_options = schema.QuantizationConfigOptions(tuple(mixed_precision_cfg_list),
158
+ base_config=base_config)
159
+
160
+ # Create an OperatorsSet to represent a set of operations.
161
+ # Each OperatorsSet has a unique label.
162
+ # If a quantization configuration options is passed, these options will
163
+ # be used for operations that will be attached to this set's label.
164
+ # Otherwise, it will be a configure-less set (used in fusing):
165
+ operator_set = []
166
+ fusing_patterns = []
167
+
168
+ # May suit for operations like: Dropout, Reshape, etc.
169
+ operator_set.append(schema.OperatorsSet("NoQuantization",
170
+ default_configuration_options.clone_and_edit(
171
+ enable_activation_quantization=False)
172
+ .clone_and_edit_weight_attribute(enable_weights_quantization=False)))
173
+
174
+ # Define operator sets that use mixed_precision_configuration_options:
175
+ conv = schema.OperatorsSet("Conv", mixed_precision_configuration_options)
176
+ fc = schema.OperatorsSet("FullyConnected", mixed_precision_configuration_options)
177
+
178
+ # Define operations sets without quantization configuration
179
+ # options (useful for creating fusing patterns, for example):
180
+ any_relu = schema.OperatorsSet("AnyReLU")
181
+ add = schema.OperatorsSet("Add")
182
+ sub = schema.OperatorsSet("Sub")
183
+ mul = schema.OperatorsSet("Mul")
184
+ div = schema.OperatorsSet("Div")
185
+ prelu = schema.OperatorsSet("PReLU")
186
+ swish = schema.OperatorsSet("Swish")
187
+ sigmoid = schema.OperatorsSet("Sigmoid")
188
+ tanh = schema.OperatorsSet("Tanh")
189
+
190
+ operator_set.extend([conv, fc, any_relu, add, sub, mul, div, prelu, swish, sigmoid, tanh])
191
+ # Combine multiple operators into a single operator to avoid quantization between
192
+ # them. To do this we define fusing patterns using the OperatorsSets that were created.
193
+ # To group multiple sets with regard to fusing, an OperatorSetConcat can be created
194
+ activations_after_conv_to_fuse = schema.OperatorSetConcat([any_relu, swish, prelu, sigmoid, tanh])
195
+ activations_after_fc_to_fuse = schema.OperatorSetConcat([any_relu, swish, sigmoid])
196
+ any_binary = schema.OperatorSetConcat([add, sub, mul, div])
197
+
198
+ # ------------------- #
199
+ # Fusions
200
+ # ------------------- #
201
+ fusing_patterns.append(schema.Fusing((conv, activations_after_conv_to_fuse)))
202
+ fusing_patterns.append(schema.Fusing((fc, activations_after_fc_to_fuse)))
203
+ fusing_patterns.append(schema.Fusing((any_binary, any_relu)))
154
204
 
155
205
  # Create a TargetPlatformModel and set its default quantization config.
156
206
  # This default configuration will be used for all operations
@@ -160,56 +210,8 @@ def generate_tp_model(default_config: OpQuantizationConfig,
160
210
  tpc_minor_version=1,
161
211
  tpc_patch_version=0,
162
212
  tpc_platform_type=IMX500_TP_MODEL,
213
+ operator_set=tuple(operator_set),
214
+ fusing_patterns=tuple(fusing_patterns),
163
215
  add_metadata=False,
164
216
  name=name)
165
-
166
- # To start defining the model's components (such as operator sets, and fusing patterns),
167
- # use 'with' the TargetPlatformModel instance, and create them as below:
168
- with generated_tpc:
169
- # Create an OperatorsSet to represent a set of operations.
170
- # Each OperatorsSet has a unique label.
171
- # If a quantization configuration options is passed, these options will
172
- # be used for operations that will be attached to this set's label.
173
- # Otherwise, it will be a configure-less set (used in fusing):
174
-
175
- # May suit for operations like: Dropout, Reshape, etc.
176
- default_qco = tp.get_default_quantization_config_options()
177
- schema.OperatorsSet("NoQuantization",
178
- default_qco.clone_and_edit(enable_activation_quantization=False)
179
- .clone_and_edit_weight_attribute(enable_weights_quantization=False))
180
-
181
- # Create Mixed-Precision quantization configuration options from the given list of OpQuantizationConfig objects
182
- mixed_precision_configuration_options = schema.QuantizationConfigOptions(mixed_precision_cfg_list,
183
- base_config=base_config)
184
-
185
- # Define operator sets that use mixed_precision_configuration_options:
186
- conv = schema.OperatorsSet("Conv", mixed_precision_configuration_options)
187
- fc = schema.OperatorsSet("FullyConnected", mixed_precision_configuration_options)
188
-
189
- # Define operations sets without quantization configuration
190
- # options (useful for creating fusing patterns, for example):
191
- any_relu = schema.OperatorsSet("AnyReLU")
192
- add = schema.OperatorsSet("Add")
193
- sub = schema.OperatorsSet("Sub")
194
- mul = schema.OperatorsSet("Mul")
195
- div = schema.OperatorsSet("Div")
196
- prelu = schema.OperatorsSet("PReLU")
197
- swish = schema.OperatorsSet("Swish")
198
- sigmoid = schema.OperatorsSet("Sigmoid")
199
- tanh = schema.OperatorsSet("Tanh")
200
-
201
- # Combine multiple operators into a single operator to avoid quantization between
202
- # them. To do this we define fusing patterns using the OperatorsSets that were created.
203
- # To group multiple sets with regard to fusing, an OperatorSetConcat can be created
204
- activations_after_conv_to_fuse = schema.OperatorSetConcat([any_relu, swish, prelu, sigmoid, tanh])
205
- activations_after_fc_to_fuse = schema.OperatorSetConcat([any_relu, swish, sigmoid])
206
- any_binary = schema.OperatorSetConcat([add, sub, mul, div])
207
-
208
- # ------------------- #
209
- # Fusions
210
- # ------------------- #
211
- schema.Fusing([conv, activations_after_conv_to_fuse])
212
- schema.Fusing([fc, activations_after_fc_to_fuse])
213
- schema.Fusing([any_binary, any_relu])
214
-
215
217
  return generated_tpc
@@ -19,7 +19,8 @@ import model_compression_toolkit.target_platform_capabilities.schema.mct_current
19
19
  from model_compression_toolkit.constants import FLOAT_BITWIDTH
20
20
  from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, BIAS_ATTR, WEIGHTS_N_BITS, \
21
21
  IMX500_TP_MODEL
22
- from model_compression_toolkit.target_platform_capabilities.schema.mct_current_schema import TargetPlatformModel, Signedness, \
22
+ from model_compression_toolkit.target_platform_capabilities.schema.mct_current_schema import TargetPlatformModel, \
23
+ Signedness, \
23
24
  AttributeQuantizationConfig, OpQuantizationConfig
24
25
 
25
26
  tp = mct.target_platform
@@ -146,7 +147,57 @@ def generate_tp_model(default_config: OpQuantizationConfig,
146
147
  # of possible configurations to consider when quantizing a set of operations (in mixed-precision, for example).
147
148
  # If the QuantizationConfigOptions contains only one configuration,
148
149
  # this configuration will be used for the operation quantization:
149
- default_configuration_options = schema.QuantizationConfigOptions([default_config])
150
+ default_configuration_options = schema.QuantizationConfigOptions(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(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("NoQuantization",
166
+ 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("Conv", mixed_precision_configuration_options)
172
+ fc = schema.OperatorsSet("FullyConnected", 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("AnyReLU")
177
+ add = schema.OperatorsSet("Add")
178
+ sub = schema.OperatorsSet("Sub")
179
+ mul = schema.OperatorsSet("Mul")
180
+ div = schema.OperatorsSet("Div")
181
+ prelu = schema.OperatorsSet("PReLU")
182
+ swish = schema.OperatorsSet("Swish")
183
+ sigmoid = schema.OperatorsSet("Sigmoid")
184
+ tanh = schema.OperatorsSet("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([any_relu, swish, prelu, sigmoid, tanh])
192
+ activations_after_fc_to_fuse = schema.OperatorSetConcat([any_relu, swish, sigmoid])
193
+ any_binary = schema.OperatorSetConcat([add, sub, mul, div])
194
+
195
+ # ------------------- #
196
+ # Fusions
197
+ # ------------------- #
198
+ fusing_patterns.append(schema.Fusing((conv, activations_after_conv_to_fuse)))
199
+ fusing_patterns.append(schema.Fusing((fc, activations_after_fc_to_fuse)))
200
+ fusing_patterns.append(schema.Fusing((any_binary, any_relu)))
150
201
 
151
202
  # Create a TargetPlatformModel and set its default quantization config.
152
203
  # This default configuration will be used for all operations
@@ -156,56 +207,9 @@ def generate_tp_model(default_config: OpQuantizationConfig,
156
207
  tpc_minor_version=1,
157
208
  tpc_patch_version=0,
158
209
  tpc_platform_type=IMX500_TP_MODEL,
210
+ operator_set=tuple(operator_set),
211
+ fusing_patterns=tuple(fusing_patterns),
212
+ name=name,
159
213
  add_metadata=False,
160
- name=name)
161
-
162
- # To start defining the model's components (such as operator sets, and fusing patterns),
163
- # use 'with' the TargetPlatformModel instance, and create them as below:
164
- with generated_tpc:
165
- # Create an OperatorsSet to represent a set of operations.
166
- # Each OperatorsSet has a unique label.
167
- # If a quantization configuration options is passed, these options will
168
- # be used for operations that will be attached to this set's label.
169
- # Otherwise, it will be a configure-less set (used in fusing):
170
-
171
- # May suit for operations like: Dropout, Reshape, etc.
172
- default_qco = tp.get_default_quantization_config_options()
173
- schema.OperatorsSet("NoQuantization",
174
- default_qco.clone_and_edit(enable_activation_quantization=False)
175
- .clone_and_edit_weight_attribute(enable_weights_quantization=False))
176
-
177
- # Create Mixed-Precision quantization configuration options from the given list of OpQuantizationConfig objects
178
- mixed_precision_configuration_options = schema.QuantizationConfigOptions(mixed_precision_cfg_list,
179
- base_config=base_config)
180
-
181
- # Define operator sets that use mixed_precision_configuration_options:
182
- conv = schema.OperatorsSet("Conv", mixed_precision_configuration_options)
183
- fc = schema.OperatorsSet("FullyConnected", mixed_precision_configuration_options)
184
-
185
- # Define operations sets without quantization configuration
186
- # options (useful for creating fusing patterns, for example):
187
- any_relu = schema.OperatorsSet("AnyReLU")
188
- add = schema.OperatorsSet("Add")
189
- sub = schema.OperatorsSet("Sub")
190
- mul = schema.OperatorsSet("Mul")
191
- div = schema.OperatorsSet("Div")
192
- prelu = schema.OperatorsSet("PReLU")
193
- swish = schema.OperatorsSet("Swish")
194
- sigmoid = schema.OperatorsSet("Sigmoid")
195
- tanh = schema.OperatorsSet("Tanh")
196
-
197
- # Combine multiple operators into a single operator to avoid quantization between
198
- # them. To do this we define fusing patterns using the OperatorsSets that were created.
199
- # To group multiple sets with regard to fusing, an OperatorSetConcat can be created
200
- activations_after_conv_to_fuse = schema.OperatorSetConcat([any_relu, swish, prelu, sigmoid, tanh])
201
- activations_after_fc_to_fuse = schema.OperatorSetConcat([any_relu, swish, sigmoid])
202
- any_binary = schema.OperatorSetConcat([add, sub, mul, div])
203
-
204
- # ------------------- #
205
- # Fusions
206
- # ------------------- #
207
- schema.Fusing([conv, activations_after_conv_to_fuse])
208
- schema.Fusing([fc, activations_after_fc_to_fuse])
209
- schema.Fusing([any_binary, any_relu])
210
-
214
+ is_simd_padding=True)
211
215
  return generated_tpc
@@ -19,7 +19,8 @@ import model_compression_toolkit.target_platform_capabilities.schema.mct_current
19
19
  from model_compression_toolkit.constants import FLOAT_BITWIDTH
20
20
  from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, BIAS_ATTR, WEIGHTS_N_BITS, \
21
21
  IMX500_TP_MODEL
22
- from model_compression_toolkit.target_platform_capabilities.schema.mct_current_schema import TargetPlatformModel, Signedness, \
22
+ from model_compression_toolkit.target_platform_capabilities.schema.mct_current_schema import TargetPlatformModel, \
23
+ Signedness, \
23
24
  AttributeQuantizationConfig, OpQuantizationConfig
24
25
 
25
26
  tp = mct.target_platform
@@ -155,7 +156,54 @@ def generate_tp_model(default_config: OpQuantizationConfig,
155
156
  # of possible configurations to consider when quantizing a set of operations (in mixed-precision, for example).
156
157
  # If the QuantizationConfigOptions contains only one configuration,
157
158
  # this configuration will be used for the operation quantization:
158
- default_configuration_options = schema.QuantizationConfigOptions([default_config])
159
+ default_configuration_options = schema.QuantizationConfigOptions(tuple([default_config]))
160
+
161
+ # Create Mixed-Precision quantization configuration options from the given list of OpQuantizationConfig objects
162
+ mixed_precision_configuration_options = schema.QuantizationConfigOptions(tuple(mixed_precision_cfg_list),
163
+ base_config=base_config)
164
+
165
+ # Create an OperatorsSet to represent a set of operations.
166
+ # Each OperatorsSet has a unique label.
167
+ # If a quantization configuration options is passed, these options will
168
+ # be used for operations that will be attached to this set's label.
169
+ # Otherwise, it will be a configure-less set (used in fusing):
170
+ operator_set = []
171
+ fusing_patterns = []
172
+ # May suit for operations like: Dropout, Reshape, etc.
173
+ operator_set.append(schema.OperatorsSet("NoQuantization", default_configuration_options.clone_and_edit(
174
+ enable_activation_quantization=False).clone_and_edit_weight_attribute(enable_weights_quantization=False)))
175
+
176
+ # Define operator sets that use mixed_precision_configuration_options:
177
+ conv = schema.OperatorsSet("Conv", mixed_precision_configuration_options)
178
+ fc = schema.OperatorsSet("FullyConnected", mixed_precision_configuration_options)
179
+
180
+ # Define operations sets without quantization configuration
181
+ # options (useful for creating fusing patterns, for example):
182
+ any_relu = schema.OperatorsSet("AnyReLU")
183
+ add = schema.OperatorsSet("Add")
184
+ sub = schema.OperatorsSet("Sub")
185
+ mul = schema.OperatorsSet("Mul")
186
+ div = schema.OperatorsSet("Div")
187
+ prelu = schema.OperatorsSet("PReLU")
188
+ swish = schema.OperatorsSet("Swish")
189
+ sigmoid = schema.OperatorsSet("Sigmoid")
190
+ tanh = schema.OperatorsSet("Tanh")
191
+
192
+ operator_set.extend([conv, fc, any_relu, add, sub, mul, div, prelu, swish, sigmoid, tanh])
193
+
194
+ # Combine multiple operators into a single operator to avoid quantization between
195
+ # them. To do this we define fusing patterns using the OperatorsSets that were created.
196
+ # To group multiple sets with regard to fusing, an OperatorSetConcat can be created
197
+ activations_after_conv_to_fuse = schema.OperatorSetConcat([any_relu, swish, prelu, sigmoid, tanh])
198
+ activations_after_fc_to_fuse = schema.OperatorSetConcat([any_relu, swish, sigmoid])
199
+ any_binary = schema.OperatorSetConcat([add, sub, mul, div])
200
+
201
+ # ------------------- #
202
+ # Fusions
203
+ # ------------------- #
204
+ fusing_patterns.append(schema.Fusing((conv, activations_after_conv_to_fuse)))
205
+ fusing_patterns.append(schema.Fusing((fc, activations_after_fc_to_fuse)))
206
+ fusing_patterns.append(schema.Fusing((any_binary, any_relu)))
159
207
 
160
208
  # Create a TargetPlatformModel and set its default quantization config.
161
209
  # This default configuration will be used for all operations
@@ -165,57 +213,10 @@ def generate_tp_model(default_config: OpQuantizationConfig,
165
213
  tpc_minor_version=2,
166
214
  tpc_patch_version=0,
167
215
  tpc_platform_type=IMX500_TP_MODEL,
216
+ operator_set=tuple(operator_set),
217
+ fusing_patterns=tuple(fusing_patterns),
168
218
  add_metadata=True,
169
219
  name=name,
170
220
  is_simd_padding=True)
171
221
 
172
- # To start defining the model's components (such as operator sets, and fusing patterns),
173
- # use 'with' the TargetPlatformModel instance, and create them as below:
174
- with generated_tpm:
175
- # Create an OperatorsSet to represent a set of operations.
176
- # Each OperatorsSet has a unique label.
177
- # If a quantization configuration options is passed, these options will
178
- # be used for operations that will be attached to this set's label.
179
- # Otherwise, it will be a configure-less set (used in fusing):
180
-
181
- # May suit for operations like: Dropout, Reshape, etc.
182
- default_qco = tp.get_default_quantization_config_options()
183
- schema.OperatorsSet("NoQuantization",
184
- default_qco.clone_and_edit(enable_activation_quantization=False)
185
- .clone_and_edit_weight_attribute(enable_weights_quantization=False))
186
-
187
- # Create Mixed-Precision quantization configuration options from the given list of OpQuantizationConfig objects
188
- mixed_precision_configuration_options = schema.QuantizationConfigOptions(mixed_precision_cfg_list,
189
- base_config=base_config)
190
-
191
- # Define operator sets that use mixed_precision_configuration_options:
192
- conv = schema.OperatorsSet("Conv", mixed_precision_configuration_options)
193
- fc = schema.OperatorsSet("FullyConnected", mixed_precision_configuration_options)
194
-
195
- # Define operations sets without quantization configuration
196
- # options (useful for creating fusing patterns, for example):
197
- any_relu = schema.OperatorsSet("AnyReLU")
198
- add = schema.OperatorsSet("Add")
199
- sub = schema.OperatorsSet("Sub")
200
- mul = schema.OperatorsSet("Mul")
201
- div = schema.OperatorsSet("Div")
202
- prelu = schema.OperatorsSet("PReLU")
203
- swish = schema.OperatorsSet("Swish")
204
- sigmoid = schema.OperatorsSet("Sigmoid")
205
- tanh = schema.OperatorsSet("Tanh")
206
-
207
- # Combine multiple operators into a single operator to avoid quantization between
208
- # them. To do this we define fusing patterns using the OperatorsSets that were created.
209
- # To group multiple sets with regard to fusing, an OperatorSetConcat can be created
210
- activations_after_conv_to_fuse = schema.OperatorSetConcat([any_relu, swish, prelu, sigmoid, tanh])
211
- activations_after_fc_to_fuse = schema.OperatorSetConcat([any_relu, swish, sigmoid])
212
- any_binary = schema.OperatorSetConcat([add, sub, mul, div])
213
-
214
- # ------------------- #
215
- # Fusions
216
- # ------------------- #
217
- schema.Fusing([conv, activations_after_conv_to_fuse])
218
- schema.Fusing([fc, activations_after_fc_to_fuse])
219
- schema.Fusing([any_binary, any_relu])
220
-
221
222
  return generated_tpm