mct-nightly 2.2.0.20241201.617__py3-none-any.whl → 2.2.0.20241202.131715__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.
- {mct_nightly-2.2.0.20241201.617.dist-info → mct_nightly-2.2.0.20241202.131715.dist-info}/METADATA +1 -1
- {mct_nightly-2.2.0.20241201.617.dist-info → mct_nightly-2.2.0.20241202.131715.dist-info}/RECORD +58 -58
- model_compression_toolkit/__init__.py +1 -1
- model_compression_toolkit/constants.py +0 -3
- model_compression_toolkit/core/common/graph/base_node.py +7 -5
- model_compression_toolkit/core/common/graph/functional_node.py +1 -1
- model_compression_toolkit/core/common/mixed_precision/resource_utilization_tools/resource_utilization_data.py +2 -2
- model_compression_toolkit/core/common/quantization/candidate_node_quantization_config.py +2 -2
- model_compression_toolkit/core/common/quantization/node_quantization_config.py +2 -2
- model_compression_toolkit/core/common/quantization/quantization_params_generation/qparams_activations_computation.py +2 -1
- model_compression_toolkit/core/common/quantization/set_node_quantization_config.py +1 -1
- model_compression_toolkit/core/common/statistics_correction/apply_activation_bias_correction_to_graph.py +1 -1
- model_compression_toolkit/core/common/statistics_correction/apply_bias_correction_to_graph.py +1 -1
- model_compression_toolkit/core/common/substitutions/batchnorm_reconstruction.py +2 -2
- model_compression_toolkit/core/common/substitutions/shift_negative_activation.py +2 -2
- model_compression_toolkit/core/keras/back2framework/keras_model_builder.py +5 -1
- model_compression_toolkit/metadata.py +14 -5
- model_compression_toolkit/target_platform_capabilities/schema/__init__.py +14 -0
- model_compression_toolkit/target_platform_capabilities/schema/mct_current_schema.py +11 -0
- model_compression_toolkit/target_platform_capabilities/schema/schema_functions.py +37 -0
- model_compression_toolkit/target_platform_capabilities/{target_platform/op_quantization_config.py → schema/v1.py} +377 -24
- model_compression_toolkit/target_platform_capabilities/target_platform/__init__.py +3 -5
- model_compression_toolkit/target_platform_capabilities/target_platform/target_platform_model.py +2 -214
- model_compression_toolkit/target_platform_capabilities/target_platform/targetplatform2framework/operations_to_layers.py +1 -2
- model_compression_toolkit/target_platform_capabilities/target_platform/targetplatform2framework/target_platform_capabilities.py +6 -10
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v1/tp_model.py +39 -32
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v1/tpc_keras.py +3 -2
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v1/tpc_pytorch.py +3 -5
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v1_lut/tp_model.py +36 -31
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v1_lut/tpc_keras.py +3 -2
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v1_lut/tpc_pytorch.py +3 -4
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v1_pot/tp_model.py +37 -32
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v1_pot/tpc_keras.py +3 -2
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v1_pot/tpc_pytorch.py +3 -4
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v2/tp_model.py +39 -32
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v2/tpc_keras.py +3 -2
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v2/tpc_pytorch.py +3 -4
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v2_lut/tp_model.py +36 -31
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v2_lut/tpc_keras.py +3 -2
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v2_lut/tpc_pytorch.py +3 -4
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3/tp_model.py +45 -38
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3/tpc_keras.py +3 -2
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3/tpc_pytorch.py +3 -4
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3_lut/tp_model.py +37 -32
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3_lut/tpc_keras.py +3 -2
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3_lut/tpc_pytorch.py +3 -4
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v4/tp_model.py +70 -62
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v4/tpc_keras.py +3 -2
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v4/tpc_pytorch.py +3 -4
- model_compression_toolkit/target_platform_capabilities/tpc_models/qnnpack_tpc/v1/tp_model.py +22 -17
- model_compression_toolkit/target_platform_capabilities/tpc_models/qnnpack_tpc/v1/tpc_keras.py +3 -4
- model_compression_toolkit/target_platform_capabilities/tpc_models/qnnpack_tpc/v1/tpc_pytorch.py +3 -4
- model_compression_toolkit/target_platform_capabilities/tpc_models/tflite_tpc/v1/tp_model.py +56 -51
- model_compression_toolkit/target_platform_capabilities/tpc_models/tflite_tpc/v1/tpc_keras.py +3 -4
- model_compression_toolkit/target_platform_capabilities/tpc_models/tflite_tpc/v1/tpc_pytorch.py +3 -4
- model_compression_toolkit/target_platform_capabilities/target_platform/fusing.py +0 -85
- model_compression_toolkit/target_platform_capabilities/target_platform/operators.py +0 -87
- model_compression_toolkit/target_platform_capabilities/target_platform/target_platform_model_component.py +0 -40
- {mct_nightly-2.2.0.20241201.617.dist-info → mct_nightly-2.2.0.20241202.131715.dist-info}/LICENSE.md +0 -0
- {mct_nightly-2.2.0.20241201.617.dist-info → mct_nightly-2.2.0.20241202.131715.dist-info}/WHEEL +0 -0
- {mct_nightly-2.2.0.20241201.617.dist-info → mct_nightly-2.2.0.20241202.131715.dist-info}/top_level.txt +0 -0
@@ -15,12 +15,12 @@
|
|
15
15
|
from typing import List, Tuple
|
16
16
|
|
17
17
|
import model_compression_toolkit as mct
|
18
|
+
import model_compression_toolkit.target_platform_capabilities.schema.mct_current_schema as schema
|
18
19
|
from model_compression_toolkit.constants import FLOAT_BITWIDTH
|
19
|
-
from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, BIAS_ATTR, WEIGHTS_N_BITS
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
AttributeQuantizationConfig
|
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, Signedness, \
|
23
|
+
AttributeQuantizationConfig, OpQuantizationConfig
|
24
24
|
|
25
25
|
tp = mct.target_platform
|
26
26
|
|
@@ -65,7 +65,8 @@ def get_op_quantization_configs() -> \
|
|
65
65
|
weights_quantization_method=tp.QuantizationMethod.POWER_OF_TWO,
|
66
66
|
weights_n_bits=8,
|
67
67
|
weights_per_channel_threshold=False,
|
68
|
-
enable_weights_quantization=False,
|
68
|
+
enable_weights_quantization=False,
|
69
|
+
# TODO: this will changed to True once implementing multi-attributes quantization
|
69
70
|
lut_values_bitwidth=None)
|
70
71
|
|
71
72
|
# define a quantization config to quantize the kernel (for layers where there is a kernel attribute).
|
@@ -90,7 +91,7 @@ def get_op_quantization_configs() -> \
|
|
90
91
|
|
91
92
|
# We define a default config for operation without kernel attribute.
|
92
93
|
# This is the default config that should be used for non-linear operations.
|
93
|
-
eight_bits_default =
|
94
|
+
eight_bits_default = schema.OpQuantizationConfig(
|
94
95
|
default_weight_attr_config=default_weight_attr_config,
|
95
96
|
attr_weights_configs_mapping={},
|
96
97
|
activation_quantization_method=tp.QuantizationMethod.POWER_OF_TWO,
|
@@ -104,7 +105,7 @@ def get_op_quantization_configs() -> \
|
|
104
105
|
signedness=Signedness.AUTO)
|
105
106
|
|
106
107
|
# We define an 8-bit config for linear operations quantization, that include a kernel and bias attributes.
|
107
|
-
linear_eight_bits =
|
108
|
+
linear_eight_bits = schema.OpQuantizationConfig(
|
108
109
|
default_weight_attr_config=default_weight_attr_config,
|
109
110
|
attr_weights_configs_mapping={KERNEL_ATTR: kernel_base_config, BIAS_ATTR: bias_config},
|
110
111
|
activation_quantization_method=tp.QuantizationMethod.POWER_OF_TWO,
|
@@ -154,12 +155,18 @@ def generate_tp_model(default_config: OpQuantizationConfig,
|
|
154
155
|
# of possible configurations to consider when quantizing a set of operations (in mixed-precision, for example).
|
155
156
|
# If the QuantizationConfigOptions contains only one configuration,
|
156
157
|
# this configuration will be used for the operation quantization:
|
157
|
-
default_configuration_options =
|
158
|
+
default_configuration_options = schema.QuantizationConfigOptions([default_config])
|
158
159
|
|
159
160
|
# Create a TargetPlatformModel and set its default quantization config.
|
160
161
|
# This default configuration will be used for all operations
|
161
162
|
# unless specified otherwise (see OperatorsSet, for example):
|
162
|
-
generated_tpm =
|
163
|
+
generated_tpm = schema.TargetPlatformModel(
|
164
|
+
default_configuration_options,
|
165
|
+
tpc_minor_version=2,
|
166
|
+
tpc_patch_version=0,
|
167
|
+
tpc_platform_type=IMX500_TP_MODEL,
|
168
|
+
add_metadata=True,
|
169
|
+
name=name)
|
163
170
|
|
164
171
|
# To start defining the model's components (such as operator sets, and fusing patterns),
|
165
172
|
# use 'with' the TargetPlatformModel instance, and create them as below:
|
@@ -174,42 +181,42 @@ def generate_tp_model(default_config: OpQuantizationConfig,
|
|
174
181
|
|
175
182
|
# May suit for operations like: Dropout, Reshape, etc.
|
176
183
|
default_qco = tp.get_default_quantization_config_options()
|
177
|
-
|
178
|
-
|
179
|
-
|
184
|
+
schema.OperatorsSet("NoQuantization",
|
185
|
+
default_qco.clone_and_edit(enable_activation_quantization=False)
|
186
|
+
.clone_and_edit_weight_attribute(enable_weights_quantization=False))
|
180
187
|
|
181
188
|
# Create Mixed-Precision quantization configuration options from the given list of OpQuantizationConfig objects
|
182
|
-
mixed_precision_configuration_options =
|
183
|
-
|
189
|
+
mixed_precision_configuration_options = schema.QuantizationConfigOptions(mixed_precision_cfg_list,
|
190
|
+
base_config=base_config)
|
184
191
|
|
185
192
|
# Define operator sets that use mixed_precision_configuration_options:
|
186
|
-
conv =
|
187
|
-
fc =
|
193
|
+
conv = schema.OperatorsSet("Conv", mixed_precision_configuration_options)
|
194
|
+
fc = schema.OperatorsSet("FullyConnected", mixed_precision_configuration_options)
|
188
195
|
|
189
196
|
# Define operations sets without quantization configuration
|
190
197
|
# options (useful for creating fusing patterns, for example):
|
191
|
-
any_relu =
|
192
|
-
add =
|
193
|
-
sub =
|
194
|
-
mul =
|
195
|
-
div =
|
196
|
-
prelu =
|
197
|
-
swish =
|
198
|
-
sigmoid =
|
199
|
-
tanh =
|
198
|
+
any_relu = schema.OperatorsSet("AnyReLU")
|
199
|
+
add = schema.OperatorsSet("Add")
|
200
|
+
sub = schema.OperatorsSet("Sub")
|
201
|
+
mul = schema.OperatorsSet("Mul")
|
202
|
+
div = schema.OperatorsSet("Div")
|
203
|
+
prelu = schema.OperatorsSet("PReLU")
|
204
|
+
swish = schema.OperatorsSet("Swish")
|
205
|
+
sigmoid = schema.OperatorsSet("Sigmoid")
|
206
|
+
tanh = schema.OperatorsSet("Tanh")
|
200
207
|
|
201
208
|
# Combine multiple operators into a single operator to avoid quantization between
|
202
209
|
# them. To do this we define fusing patterns using the OperatorsSets that were created.
|
203
210
|
# To group multiple sets with regard to fusing, an OperatorSetConcat can be created
|
204
|
-
activations_after_conv_to_fuse =
|
205
|
-
activations_after_fc_to_fuse =
|
206
|
-
any_binary =
|
211
|
+
activations_after_conv_to_fuse = schema.OperatorSetConcat(any_relu, swish, prelu, sigmoid, tanh)
|
212
|
+
activations_after_fc_to_fuse = schema.OperatorSetConcat(any_relu, swish, sigmoid)
|
213
|
+
any_binary = schema.OperatorSetConcat(add, sub, mul, div)
|
207
214
|
|
208
215
|
# ------------------- #
|
209
216
|
# Fusions
|
210
217
|
# ------------------- #
|
211
|
-
|
212
|
-
|
213
|
-
|
218
|
+
schema.Fusing([conv, activations_after_conv_to_fuse])
|
219
|
+
schema.Fusing([fc, activations_after_fc_to_fuse])
|
220
|
+
schema.Fusing([any_binary, any_relu])
|
214
221
|
|
215
222
|
return generated_tpm
|
model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v2/tpc_keras.py
CHANGED
@@ -15,6 +15,7 @@
|
|
15
15
|
import tensorflow as tf
|
16
16
|
from packaging import version
|
17
17
|
|
18
|
+
import model_compression_toolkit.target_platform_capabilities.schema.mct_current_schema as schema
|
18
19
|
from model_compression_toolkit.defaultdict import DefaultDict
|
19
20
|
from model_compression_toolkit.verify_packages import FOUND_SONY_CUSTOM_LAYERS
|
20
21
|
from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, KERAS_DEPTHWISE_KERNEL, \
|
@@ -49,7 +50,7 @@ def get_keras_tpc() -> tp.TargetPlatformCapabilities:
|
|
49
50
|
return generate_keras_tpc(name='imx500_tpc_keras_tpc', tp_model=imx500_tpc_tp_model)
|
50
51
|
|
51
52
|
|
52
|
-
def generate_keras_tpc(name: str, tp_model:
|
53
|
+
def generate_keras_tpc(name: str, tp_model: schema.TargetPlatformModel):
|
53
54
|
"""
|
54
55
|
Generates a TargetPlatformCapabilities object with default operation sets to layers mapping.
|
55
56
|
|
@@ -60,7 +61,7 @@ def generate_keras_tpc(name: str, tp_model: tp.TargetPlatformModel):
|
|
60
61
|
Returns: a TargetPlatformCapabilities object for the given TargetPlatformModel.
|
61
62
|
"""
|
62
63
|
|
63
|
-
keras_tpc = tp.TargetPlatformCapabilities(tp_model
|
64
|
+
keras_tpc = tp.TargetPlatformCapabilities(tp_model)
|
64
65
|
|
65
66
|
no_quant_list = [Identity,
|
66
67
|
tf.identity,
|
model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v2/tpc_pytorch.py
CHANGED
@@ -23,6 +23,7 @@ from torch.nn import Dropout, Flatten, Hardtanh, Identity
|
|
23
23
|
from torch.nn import ReLU, ReLU6, PReLU, SiLU, Sigmoid, Tanh, Hardswish, LeakyReLU
|
24
24
|
from torch.nn.functional import relu, relu6, prelu, silu, hardtanh, hardswish, leaky_relu
|
25
25
|
|
26
|
+
import model_compression_toolkit.target_platform_capabilities.schema.mct_current_schema as schema
|
26
27
|
from model_compression_toolkit.defaultdict import DefaultDict
|
27
28
|
from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, BIAS_ATTR, PYTORCH_KERNEL, \
|
28
29
|
BIAS
|
@@ -43,7 +44,7 @@ def get_pytorch_tpc() -> tp.TargetPlatformCapabilities:
|
|
43
44
|
return generate_pytorch_tpc(name='imx500_tpc_pytorch_tpc', tp_model=imx500_tpc_tp_model)
|
44
45
|
|
45
46
|
|
46
|
-
def generate_pytorch_tpc(name: str, tp_model:
|
47
|
+
def generate_pytorch_tpc(name: str, tp_model: schema.TargetPlatformModel):
|
47
48
|
"""
|
48
49
|
Generates a TargetPlatformCapabilities object with default operation sets to layers mapping.
|
49
50
|
Args:
|
@@ -52,9 +53,7 @@ def generate_pytorch_tpc(name: str, tp_model: tp.TargetPlatformModel):
|
|
52
53
|
Returns: a TargetPlatformCapabilities object for the given TargetPlatformModel.
|
53
54
|
"""
|
54
55
|
|
55
|
-
pytorch_tpc = tp.TargetPlatformCapabilities(tp_model
|
56
|
-
name=name,
|
57
|
-
version=TPC_VERSION)
|
56
|
+
pytorch_tpc = tp.TargetPlatformCapabilities(tp_model)
|
58
57
|
|
59
58
|
# we provide attributes mapping that maps each layer type in the operations set
|
60
59
|
# that has weights attributes with provided quantization config (in the tp model) to
|
model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v2_lut/tp_model.py
CHANGED
@@ -15,13 +15,12 @@
|
|
15
15
|
from typing import List, Tuple
|
16
16
|
|
17
17
|
import model_compression_toolkit as mct
|
18
|
+
import model_compression_toolkit.target_platform_capabilities.schema.mct_current_schema as schema
|
18
19
|
from model_compression_toolkit.constants import FLOAT_BITWIDTH
|
19
20
|
from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, BIAS_ATTR, WEIGHTS_N_BITS, \
|
20
|
-
WEIGHTS_QUANTIZATION_METHOD
|
21
|
-
from model_compression_toolkit.target_platform_capabilities.
|
22
|
-
|
23
|
-
from model_compression_toolkit.target_platform_capabilities.target_platform.op_quantization_config import \
|
24
|
-
AttributeQuantizationConfig
|
21
|
+
WEIGHTS_QUANTIZATION_METHOD, IMX500_TP_MODEL
|
22
|
+
from model_compression_toolkit.target_platform_capabilities.schema.mct_current_schema import TargetPlatformModel, Signedness, \
|
23
|
+
AttributeQuantizationConfig, OpQuantizationConfig
|
25
24
|
|
26
25
|
tp = mct.target_platform
|
27
26
|
|
@@ -86,7 +85,7 @@ def get_op_quantization_configs() -> \
|
|
86
85
|
|
87
86
|
# We define a default config for operation without kernel attribute.
|
88
87
|
# This is the default config that should be used for non-linear operations.
|
89
|
-
eight_bits_default =
|
88
|
+
eight_bits_default = schema.OpQuantizationConfig(
|
90
89
|
default_weight_attr_config=default_weight_attr_config,
|
91
90
|
attr_weights_configs_mapping={},
|
92
91
|
activation_quantization_method=tp.QuantizationMethod.POWER_OF_TWO,
|
@@ -100,7 +99,7 @@ def get_op_quantization_configs() -> \
|
|
100
99
|
signedness=Signedness.AUTO)
|
101
100
|
|
102
101
|
# We define an 8-bit config for linear operations quantization, that include a kernel and bias attributes.
|
103
|
-
linear_eight_bits =
|
102
|
+
linear_eight_bits = schema.OpQuantizationConfig(
|
104
103
|
default_weight_attr_config=default_weight_attr_config,
|
105
104
|
attr_weights_configs_mapping={KERNEL_ATTR: kernel_base_config, BIAS_ATTR: bias_config},
|
106
105
|
activation_quantization_method=tp.QuantizationMethod.POWER_OF_TWO,
|
@@ -153,12 +152,18 @@ def generate_tp_model(default_config: OpQuantizationConfig,
|
|
153
152
|
# of possible configurations to consider when quantizing a set of operations (in mixed-precision, for example).
|
154
153
|
# If the QuantizationConfigOptions contains only one configuration,
|
155
154
|
# this configuration will be used for the operation quantization:
|
156
|
-
default_configuration_options =
|
155
|
+
default_configuration_options = schema.QuantizationConfigOptions([default_config])
|
157
156
|
|
158
157
|
# Create a TargetPlatformModel and set its default quantization config.
|
159
158
|
# This default configuration will be used for all operations
|
160
159
|
# unless specified otherwise (see OperatorsSet, for example):
|
161
|
-
generated_tpm =
|
160
|
+
generated_tpm = schema.TargetPlatformModel(
|
161
|
+
default_configuration_options,
|
162
|
+
tpc_minor_version=2,
|
163
|
+
tpc_patch_version=0,
|
164
|
+
tpc_platform_type=IMX500_TP_MODEL,
|
165
|
+
add_metadata=True,
|
166
|
+
name=name)
|
162
167
|
|
163
168
|
# To start defining the model's components (such as operator sets, and fusing patterns),
|
164
169
|
# use 'with' the TargetPlatformModel instance, and create them as below:
|
@@ -171,42 +176,42 @@ def generate_tp_model(default_config: OpQuantizationConfig,
|
|
171
176
|
|
172
177
|
# May suit for operations like: Dropout, Reshape, etc.
|
173
178
|
default_qco = tp.get_default_quantization_config_options()
|
174
|
-
|
175
|
-
|
176
|
-
|
179
|
+
schema.OperatorsSet("NoQuantization",
|
180
|
+
default_qco.clone_and_edit(enable_activation_quantization=False)
|
181
|
+
.clone_and_edit_weight_attribute(enable_weights_quantization=False))
|
177
182
|
|
178
183
|
# Create Mixed-Precision quantization configuration options from the given list of OpQuantizationConfig objects
|
179
|
-
mixed_precision_configuration_options =
|
180
|
-
|
184
|
+
mixed_precision_configuration_options = schema.QuantizationConfigOptions(mixed_precision_cfg_list,
|
185
|
+
base_config=base_config)
|
181
186
|
|
182
187
|
# Define operator sets that use mixed_precision_configuration_options:
|
183
|
-
conv =
|
184
|
-
fc =
|
188
|
+
conv = schema.OperatorsSet("Conv", mixed_precision_configuration_options)
|
189
|
+
fc = schema.OperatorsSet("FullyConnected", mixed_precision_configuration_options)
|
185
190
|
|
186
191
|
# Define operations sets without quantization configuration
|
187
192
|
# options (useful for creating fusing patterns, for example):
|
188
|
-
any_relu =
|
189
|
-
add =
|
190
|
-
sub =
|
191
|
-
mul =
|
192
|
-
div =
|
193
|
-
prelu =
|
194
|
-
swish =
|
195
|
-
sigmoid =
|
196
|
-
tanh =
|
193
|
+
any_relu = schema.OperatorsSet("AnyReLU")
|
194
|
+
add = schema.OperatorsSet("Add")
|
195
|
+
sub = schema.OperatorsSet("Sub")
|
196
|
+
mul = schema.OperatorsSet("Mul")
|
197
|
+
div = schema.OperatorsSet("Div")
|
198
|
+
prelu = schema.OperatorsSet("PReLU")
|
199
|
+
swish = schema.OperatorsSet("Swish")
|
200
|
+
sigmoid = schema.OperatorsSet("Sigmoid")
|
201
|
+
tanh = schema.OperatorsSet("Tanh")
|
197
202
|
|
198
203
|
# Combine multiple operators into a single operator to avoid quantization between
|
199
204
|
# them. To do this we define fusing patterns using the OperatorsSets that were created.
|
200
205
|
# To group multiple sets with regard to fusing, an OperatorSetConcat can be created
|
201
|
-
activations_after_conv_to_fuse =
|
202
|
-
activations_after_fc_to_fuse =
|
203
|
-
any_binary =
|
206
|
+
activations_after_conv_to_fuse = schema.OperatorSetConcat(any_relu, swish, prelu, sigmoid, tanh)
|
207
|
+
activations_after_fc_to_fuse = schema.OperatorSetConcat(any_relu, swish, sigmoid)
|
208
|
+
any_binary = schema.OperatorSetConcat(add, sub, mul, div)
|
204
209
|
|
205
210
|
# ------------------- #
|
206
211
|
# Fusions
|
207
212
|
# ------------------- #
|
208
|
-
|
209
|
-
|
210
|
-
|
213
|
+
schema.Fusing([conv, activations_after_conv_to_fuse])
|
214
|
+
schema.Fusing([fc, activations_after_fc_to_fuse])
|
215
|
+
schema.Fusing([any_binary, any_relu])
|
211
216
|
|
212
217
|
return generated_tpm
|
model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v2_lut/tpc_keras.py
CHANGED
@@ -15,6 +15,7 @@
|
|
15
15
|
import tensorflow as tf
|
16
16
|
from packaging import version
|
17
17
|
|
18
|
+
import model_compression_toolkit.target_platform_capabilities.schema.mct_current_schema as schema
|
18
19
|
from model_compression_toolkit.defaultdict import DefaultDict
|
19
20
|
from model_compression_toolkit.verify_packages import FOUND_SONY_CUSTOM_LAYERS
|
20
21
|
from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, KERAS_KERNEL, BIAS_ATTR, \
|
@@ -48,7 +49,7 @@ def get_keras_tpc() -> tp.TargetPlatformCapabilities:
|
|
48
49
|
return generate_keras_tpc(name='imx500_tpc_keras_tpc', tp_model=imx500_tpc_tp_model)
|
49
50
|
|
50
51
|
|
51
|
-
def generate_keras_tpc(name: str, tp_model:
|
52
|
+
def generate_keras_tpc(name: str, tp_model: schema.TargetPlatformModel):
|
52
53
|
"""
|
53
54
|
Generates a TargetPlatformCapabilities object with default operation sets to layers mapping.
|
54
55
|
|
@@ -59,7 +60,7 @@ def generate_keras_tpc(name: str, tp_model: tp.TargetPlatformModel):
|
|
59
60
|
Returns: a TargetPlatformCapabilities object for the given TargetPlatformModel.
|
60
61
|
"""
|
61
62
|
|
62
|
-
keras_tpc = tp.TargetPlatformCapabilities(tp_model
|
63
|
+
keras_tpc = tp.TargetPlatformCapabilities(tp_model)
|
63
64
|
|
64
65
|
no_quant_list = [Identity,
|
65
66
|
tf.identity,
|
model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v2_lut/tpc_pytorch.py
CHANGED
@@ -23,6 +23,7 @@ from torch.nn import Dropout, Flatten, Hardtanh, Identity
|
|
23
23
|
from torch.nn import ReLU, ReLU6, PReLU, SiLU, Sigmoid, Tanh, Hardswish, LeakyReLU
|
24
24
|
from torch.nn.functional import relu, relu6, prelu, silu, hardtanh, hardswish, leaky_relu
|
25
25
|
|
26
|
+
import model_compression_toolkit.target_platform_capabilities.schema.mct_current_schema as schema
|
26
27
|
from model_compression_toolkit.defaultdict import DefaultDict
|
27
28
|
from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, PYTORCH_KERNEL, BIAS_ATTR, \
|
28
29
|
BIAS
|
@@ -42,7 +43,7 @@ def get_pytorch_tpc() -> tp.TargetPlatformCapabilities:
|
|
42
43
|
return generate_pytorch_tpc(name='imx500_tpc_pytorch_tpc', tp_model=imx500_tpc_tp_model)
|
43
44
|
|
44
45
|
|
45
|
-
def generate_pytorch_tpc(name: str, tp_model:
|
46
|
+
def generate_pytorch_tpc(name: str, tp_model: schema.TargetPlatformModel):
|
46
47
|
"""
|
47
48
|
Generates a TargetPlatformCapabilities object with default operation sets to layers mapping.
|
48
49
|
Args:
|
@@ -51,9 +52,7 @@ def generate_pytorch_tpc(name: str, tp_model: tp.TargetPlatformModel):
|
|
51
52
|
Returns: a TargetPlatformCapabilities object for the given TargetPlatformModel.
|
52
53
|
"""
|
53
54
|
|
54
|
-
pytorch_tpc = tp.TargetPlatformCapabilities(tp_model
|
55
|
-
name=name,
|
56
|
-
version=TPC_VERSION)
|
55
|
+
pytorch_tpc = tp.TargetPlatformCapabilities(tp_model)
|
57
56
|
|
58
57
|
# we provide attributes mapping that maps each layer type in the operations set
|
59
58
|
# that has weights attributes with provided quantization config (in the tp model) to
|
@@ -15,12 +15,12 @@
|
|
15
15
|
from typing import List, Tuple
|
16
16
|
|
17
17
|
import model_compression_toolkit as mct
|
18
|
+
import model_compression_toolkit.target_platform_capabilities.schema.mct_current_schema as schema
|
18
19
|
from model_compression_toolkit.constants import FLOAT_BITWIDTH
|
19
|
-
from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, BIAS_ATTR, WEIGHTS_N_BITS
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
AttributeQuantizationConfig
|
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, Signedness, \
|
23
|
+
AttributeQuantizationConfig, OpQuantizationConfig
|
24
24
|
|
25
25
|
tp = mct.target_platform
|
26
26
|
|
@@ -65,7 +65,8 @@ def get_op_quantization_configs() -> \
|
|
65
65
|
weights_quantization_method=tp.QuantizationMethod.POWER_OF_TWO,
|
66
66
|
weights_n_bits=8,
|
67
67
|
weights_per_channel_threshold=False,
|
68
|
-
enable_weights_quantization=False,
|
68
|
+
enable_weights_quantization=False,
|
69
|
+
# TODO: this will changed to True once implementing multi-attributes quantization
|
69
70
|
lut_values_bitwidth=None)
|
70
71
|
|
71
72
|
# define a quantization config to quantize the kernel (for layers where there is a kernel attribute).
|
@@ -90,7 +91,7 @@ def get_op_quantization_configs() -> \
|
|
90
91
|
|
91
92
|
# We define a default config for operation without kernel attribute.
|
92
93
|
# This is the default config that should be used for non-linear operations.
|
93
|
-
eight_bits_default =
|
94
|
+
eight_bits_default = schema.OpQuantizationConfig(
|
94
95
|
default_weight_attr_config=default_weight_attr_config,
|
95
96
|
attr_weights_configs_mapping={},
|
96
97
|
activation_quantization_method=tp.QuantizationMethod.POWER_OF_TWO,
|
@@ -104,7 +105,7 @@ def get_op_quantization_configs() -> \
|
|
104
105
|
signedness=Signedness.AUTO)
|
105
106
|
|
106
107
|
# We define an 8-bit config for linear operations quantization, that include a kernel and bias attributes.
|
107
|
-
linear_eight_bits =
|
108
|
+
linear_eight_bits = schema.OpQuantizationConfig(
|
108
109
|
default_weight_attr_config=default_weight_attr_config,
|
109
110
|
attr_weights_configs_mapping={KERNEL_ATTR: kernel_base_config, BIAS_ATTR: bias_config},
|
110
111
|
activation_quantization_method=tp.QuantizationMethod.POWER_OF_TWO,
|
@@ -154,7 +155,7 @@ def generate_tp_model(default_config: OpQuantizationConfig,
|
|
154
155
|
# of possible configurations to consider when quantizing a set of operations (in mixed-precision, for example).
|
155
156
|
# If the QuantizationConfigOptions contains only one configuration,
|
156
157
|
# this configuration will be used for the operation quantization:
|
157
|
-
default_configuration_options =
|
158
|
+
default_configuration_options = schema.QuantizationConfigOptions([default_config])
|
158
159
|
|
159
160
|
# Create a QuantizationConfigOptions for quantizing constants in functional ops.
|
160
161
|
# Constant configuration is similar to the default eight bit configuration except for PoT
|
@@ -165,7 +166,7 @@ def generate_tp_model(default_config: OpQuantizationConfig,
|
|
165
166
|
default_weight_attr_config=default_config.default_weight_attr_config.clone_and_edit(
|
166
167
|
enable_weights_quantization=True, weights_per_channel_threshold=True,
|
167
168
|
weights_quantization_method=tp.QuantizationMethod.POWER_OF_TWO))
|
168
|
-
const_configuration_options =
|
169
|
+
const_configuration_options = schema.QuantizationConfigOptions([const_config])
|
169
170
|
|
170
171
|
# 16 bits inputs and outputs. Currently, only defined for consts since they are used in operators that
|
171
172
|
# support 16 bit as input and output.
|
@@ -173,14 +174,20 @@ def generate_tp_model(default_config: OpQuantizationConfig,
|
|
173
174
|
supported_input_activation_n_bits=(8, 16))
|
174
175
|
const_config_input16_output16 = const_config_input16.clone_and_edit(
|
175
176
|
activation_n_bits=16, signedness=Signedness.SIGNED)
|
176
|
-
const_configuration_options_inout16 =
|
177
|
-
|
178
|
-
|
177
|
+
const_configuration_options_inout16 = schema.QuantizationConfigOptions([const_config_input16_output16,
|
178
|
+
const_config_input16],
|
179
|
+
base_config=const_config_input16)
|
179
180
|
|
180
181
|
# Create a TargetPlatformModel and set its default quantization config.
|
181
182
|
# This default configuration will be used for all operations
|
182
183
|
# unless specified otherwise (see OperatorsSet, for example):
|
183
|
-
generated_tpm =
|
184
|
+
generated_tpm = schema.TargetPlatformModel(
|
185
|
+
default_configuration_options,
|
186
|
+
tpc_minor_version=3,
|
187
|
+
tpc_patch_version=0,
|
188
|
+
tpc_platform_type=IMX500_TP_MODEL,
|
189
|
+
add_metadata=True,
|
190
|
+
name=name)
|
184
191
|
|
185
192
|
# To start defining the model's components (such as operator sets, and fusing patterns),
|
186
193
|
# use 'with' the TargetPlatformModel instance, and create them as below:
|
@@ -195,44 +202,44 @@ def generate_tp_model(default_config: OpQuantizationConfig,
|
|
195
202
|
|
196
203
|
# May suit for operations like: Dropout, Reshape, etc.
|
197
204
|
default_qco = tp.get_default_quantization_config_options()
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
205
|
+
schema.OperatorsSet("NoQuantization",
|
206
|
+
default_qco.clone_and_edit(enable_activation_quantization=False,
|
207
|
+
supported_input_activation_n_bits=(8, 16))
|
208
|
+
.clone_and_edit_weight_attribute(enable_weights_quantization=False))
|
209
|
+
schema.OperatorsSet("Default16BitInout", const_configuration_options_inout16)
|
203
210
|
|
204
211
|
# Create Mixed-Precision quantization configuration options from the given list of OpQuantizationConfig objects
|
205
|
-
mixed_precision_configuration_options =
|
206
|
-
|
212
|
+
mixed_precision_configuration_options = schema.QuantizationConfigOptions(mixed_precision_cfg_list,
|
213
|
+
base_config=base_config)
|
207
214
|
|
208
215
|
# Define operator sets that use mixed_precision_configuration_options:
|
209
|
-
conv =
|
210
|
-
fc =
|
216
|
+
conv = schema.OperatorsSet("Conv", mixed_precision_configuration_options)
|
217
|
+
fc = schema.OperatorsSet("FullyConnected", mixed_precision_configuration_options)
|
211
218
|
|
212
219
|
# Define operations sets without quantization configuration
|
213
220
|
# options (useful for creating fusing patterns, for example):
|
214
|
-
any_relu =
|
215
|
-
add =
|
216
|
-
sub =
|
217
|
-
mul =
|
218
|
-
div =
|
219
|
-
prelu =
|
220
|
-
swish =
|
221
|
-
sigmoid =
|
222
|
-
tanh =
|
221
|
+
any_relu = schema.OperatorsSet("AnyReLU")
|
222
|
+
add = schema.OperatorsSet("Add", const_configuration_options_inout16)
|
223
|
+
sub = schema.OperatorsSet("Sub", const_configuration_options_inout16)
|
224
|
+
mul = schema.OperatorsSet("Mul", const_configuration_options_inout16)
|
225
|
+
div = schema.OperatorsSet("Div", const_configuration_options)
|
226
|
+
prelu = schema.OperatorsSet("PReLU")
|
227
|
+
swish = schema.OperatorsSet("Swish")
|
228
|
+
sigmoid = schema.OperatorsSet("Sigmoid")
|
229
|
+
tanh = schema.OperatorsSet("Tanh")
|
223
230
|
|
224
231
|
# Combine multiple operators into a single operator to avoid quantization between
|
225
232
|
# them. To do this we define fusing patterns using the OperatorsSets that were created.
|
226
233
|
# To group multiple sets with regard to fusing, an OperatorSetConcat can be created
|
227
|
-
activations_after_conv_to_fuse =
|
228
|
-
activations_after_fc_to_fuse =
|
229
|
-
any_binary =
|
234
|
+
activations_after_conv_to_fuse = schema.OperatorSetConcat(any_relu, swish, prelu, sigmoid, tanh)
|
235
|
+
activations_after_fc_to_fuse = schema.OperatorSetConcat(any_relu, swish, sigmoid)
|
236
|
+
any_binary = schema.OperatorSetConcat(add, sub, mul, div)
|
230
237
|
|
231
238
|
# ------------------- #
|
232
239
|
# Fusions
|
233
240
|
# ------------------- #
|
234
|
-
|
235
|
-
|
236
|
-
|
241
|
+
schema.Fusing([conv, activations_after_conv_to_fuse])
|
242
|
+
schema.Fusing([fc, activations_after_fc_to_fuse])
|
243
|
+
schema.Fusing([any_binary, any_relu])
|
237
244
|
|
238
245
|
return generated_tpm
|
model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3/tpc_keras.py
CHANGED
@@ -15,6 +15,7 @@
|
|
15
15
|
import tensorflow as tf
|
16
16
|
from packaging import version
|
17
17
|
|
18
|
+
from model_compression_toolkit.target_platform_capabilities.schema.mct_current_schema import TargetPlatformModel
|
18
19
|
from model_compression_toolkit.defaultdict import DefaultDict
|
19
20
|
from model_compression_toolkit.verify_packages import FOUND_SONY_CUSTOM_LAYERS
|
20
21
|
from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, KERAS_DEPTHWISE_KERNEL, \
|
@@ -49,7 +50,7 @@ def get_keras_tpc() -> tp.TargetPlatformCapabilities:
|
|
49
50
|
return generate_keras_tpc(name='imx500_tpc_keras_tpc', tp_model=imx500_tpc_tp_model)
|
50
51
|
|
51
52
|
|
52
|
-
def generate_keras_tpc(name: str, tp_model:
|
53
|
+
def generate_keras_tpc(name: str, tp_model: TargetPlatformModel):
|
53
54
|
"""
|
54
55
|
Generates a TargetPlatformCapabilities object with default operation sets to layers mapping.
|
55
56
|
|
@@ -60,7 +61,7 @@ def generate_keras_tpc(name: str, tp_model: tp.TargetPlatformModel):
|
|
60
61
|
Returns: a TargetPlatformCapabilities object for the given TargetPlatformModel.
|
61
62
|
"""
|
62
63
|
|
63
|
-
keras_tpc = tp.TargetPlatformCapabilities(tp_model
|
64
|
+
keras_tpc = tp.TargetPlatformCapabilities(tp_model)
|
64
65
|
|
65
66
|
no_quant_list = [Identity,
|
66
67
|
tf.identity,
|
model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3/tpc_pytorch.py
CHANGED
@@ -23,6 +23,7 @@ from torch.nn import Dropout, Flatten, Hardtanh, Identity
|
|
23
23
|
from torch.nn import ReLU, ReLU6, PReLU, SiLU, Sigmoid, Tanh, Hardswish, LeakyReLU
|
24
24
|
from torch.nn.functional import relu, relu6, prelu, silu, hardtanh, hardswish, leaky_relu
|
25
25
|
|
26
|
+
from model_compression_toolkit.target_platform_capabilities.schema.mct_current_schema import TargetPlatformModel
|
26
27
|
from model_compression_toolkit.defaultdict import DefaultDict
|
27
28
|
from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, BIAS_ATTR, PYTORCH_KERNEL, \
|
28
29
|
BIAS
|
@@ -43,7 +44,7 @@ def get_pytorch_tpc() -> tp.TargetPlatformCapabilities:
|
|
43
44
|
return generate_pytorch_tpc(name='imx500_tpc_pytorch_tpc', tp_model=imx500_tpc_tp_model)
|
44
45
|
|
45
46
|
|
46
|
-
def generate_pytorch_tpc(name: str, tp_model:
|
47
|
+
def generate_pytorch_tpc(name: str, tp_model: TargetPlatformModel):
|
47
48
|
"""
|
48
49
|
Generates a TargetPlatformCapabilities object with default operation sets to layers mapping.
|
49
50
|
Args:
|
@@ -52,9 +53,7 @@ def generate_pytorch_tpc(name: str, tp_model: tp.TargetPlatformModel):
|
|
52
53
|
Returns: a TargetPlatformCapabilities object for the given TargetPlatformModel.
|
53
54
|
"""
|
54
55
|
|
55
|
-
pytorch_tpc = tp.TargetPlatformCapabilities(tp_model
|
56
|
-
name=name,
|
57
|
-
version=TPC_VERSION)
|
56
|
+
pytorch_tpc = tp.TargetPlatformCapabilities(tp_model)
|
58
57
|
|
59
58
|
# we provide attributes mapping that maps each layer type in the operations set
|
60
59
|
# that has weights attributes with provided quantization config (in the tp model) to
|