mct-nightly 2.1.0.20240821.444__py3-none-any.whl → 2.1.0.20240822.448__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: mct-nightly
3
- Version: 2.1.0.20240821.444
3
+ Version: 2.1.0.20240822.448
4
4
  Summary: A Model Compression Toolkit for neural networks
5
5
  Home-page: UNKNOWN
6
6
  License: UNKNOWN
@@ -1,4 +1,4 @@
1
- model_compression_toolkit/__init__.py,sha256=bgVjYSXQXW6v8ZyYSkCY0qnZUwsnF6W5MYVYsczk7LA,1573
1
+ model_compression_toolkit/__init__.py,sha256=VxopIov64itXZS8DVq4AbrhJWECuZqAt-hf9_rqheig,1573
2
2
  model_compression_toolkit/constants.py,sha256=i4wYheBkIdQmsQA-axIpcT3YiSO1USNc-jaNiNE8w6E,3920
3
3
  model_compression_toolkit/defaultdict.py,sha256=LSc-sbZYXENMCw3U9F4GiXuv67IKpdn0Qm7Fr11jy-4,2277
4
4
  model_compression_toolkit/logger.py,sha256=3DByV41XHRR3kLTJNbpaMmikL8icd9e1N-nkQAY9oDk,4567
@@ -8,7 +8,7 @@ model_compression_toolkit/core/__init__.py,sha256=tnDtL9KmT0vsOU27SsJ19TKDEbIH-t
8
8
  model_compression_toolkit/core/analyzer.py,sha256=X-2ZpkH1xdXnISnw1yJvXnvV-ssoUh-9LkLISSWNqiY,3691
9
9
  model_compression_toolkit/core/graph_prep_runner.py,sha256=7-b7Jd5jBVaXOWg5nSqbEyzBtdaGDbCxs8aqMV6GZ6I,11287
10
10
  model_compression_toolkit/core/quantization_prep_runner.py,sha256=K9eJ7VbB_rpeyxX4yEnorOmSxFW3DkvofzxS6QI8Hp8,6454
11
- model_compression_toolkit/core/runner.py,sha256=kiNClmonlaqNI2U72bzGoJUzLxKHLh61iak9-HvsfQM,13880
11
+ model_compression_toolkit/core/runner.py,sha256=ryHhW5Qqu7XHVkngLF0uLX8oa4CxNAIF4PoGBeUjoSk,14346
12
12
  model_compression_toolkit/core/common/__init__.py,sha256=Wh127PbXcETZX_d1PQqZ71ETK3J9XO5A-HpadGUbj6o,1447
13
13
  model_compression_toolkit/core/common/base_substitutions.py,sha256=xDFSmVVs_iFSZfajytI0cuQaNRNcwHX3uqOoHgVUvxQ,1666
14
14
  model_compression_toolkit/core/common/framework_implementation.py,sha256=kSg2f7wS7e2EyvX6y0eKfNTTFvVFVrB8lvldJvcPvN8,20724
@@ -441,7 +441,7 @@ model_compression_toolkit/target_platform_capabilities/target_platform/targetpla
441
441
  model_compression_toolkit/target_platform_capabilities/tpc_models/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
442
442
  model_compression_toolkit/target_platform_capabilities/tpc_models/get_target_platform_capabilities.py,sha256=CWind2Kd91lzBTRAh1A9sHuNw17xXhMb3gV436RpK8c,3033
443
443
  model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/__init__.py,sha256=lNJ29DYxaLUPDstRDA1PGI5r9Fulq_hvrZMlhst1Z5g,697
444
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/target_platform_capabilities.py,sha256=yIWwvfTpiT0wRf7GwPgK9elKbGh46jxCrkcLVEjvesU,6081
444
+ model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/target_platform_capabilities.py,sha256=YxQC_JjnnB_ZEVgUVXAXZIrRKvAj9A7nWIVZTL2HDVA,5631
445
445
  model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/latest/__init__.py,sha256=eVIRpx5O0JQI7TSdw5JAWtwrG3MQ8-7hYThQvB9da5c,1528
446
446
  model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v1/__init__.py,sha256=1mMOREEMoNHu_KTMGDp4crN61opKWX6aFn1DrDLvqcc,717
447
447
  model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v1/tp_model.py,sha256=6mbv-fNVz559j5XCSX5e8aENUJACYuJzQcZBLPh12gU,11057
@@ -464,17 +464,13 @@ model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v2_
464
464
  model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v2_lut/tpc_keras.py,sha256=EgDKcY-ba9cBmQmh0-sv45GLm-HP6Cc4Rgf-KxZXTgM,6601
465
465
  model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v2_lut/tpc_pytorch.py,sha256=dFQjzFlLDwoUqKNP1at1fS1N1WJadSSasRyzHl6vaB8,5733
466
466
  model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3/__init__.py,sha256=gAeebYCKyIXH9-Qwze7FwvTihudzAHk_Qsg94fQbkjQ,717
467
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3/tp_model.py,sha256=ChprWTT6hLoKBM7iTVhQWQZYAXM_XOMHaK8PC8GEu30,12018
468
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3/tpc_keras.py,sha256=gMQ2qNm7T5FukkR_3DwVB1B41CMEW-KnrT-mPDMfCL8,6593
469
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3/tpc_pytorch.py,sha256=HRo0W5l4IJesr_np4ZhXoMk_xfdiV53LgamquQIryJA,5800
467
+ model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3/tp_model.py,sha256=purcM7kLdh2HyJaxzROAoVSwz1Xlo6GPEe8JQKfMcuM,12911
468
+ model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3/tpc_keras.py,sha256=x_-lIQJ_Cv546EmXblZA2xjvcw8Yo5xg_cy43mhqsh0,6803
469
+ model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3/tpc_pytorch.py,sha256=5Q54Qgi7_NTByRSJNVCfPUCJZ0lADT98UUZOhyHaV24,5986
470
470
  model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3_lut/__init__.py,sha256=C2kwyDE1-rtukkbNSoKRv9q8Nt2GOCaBbl0BdOr3goA,721
471
471
  model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3_lut/tp_model.py,sha256=YANvT38YiwO9jE3dC04wHDZBGJQ34hGTvKygHwwbI_U,11751
472
472
  model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3_lut/tpc_keras.py,sha256=XM6qBLIvzsmdFf-AZq5WOlORK2GXC_X-gulReNxHb9E,6601
473
473
  model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3_lut/tpc_pytorch.py,sha256=nP05jqvh6uaj30a3W7zEkJfKtqfP0Nz5bobwRqbYrdM,5807
474
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v4/__init__.py,sha256=tHTUvsaerSfbe22pU0kIDauPpFD7Pq5EmZytVIDkHz4,717
475
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v4/tp_model.py,sha256=purcM7kLdh2HyJaxzROAoVSwz1Xlo6GPEe8JQKfMcuM,12911
476
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v4/tpc_keras.py,sha256=j57CrY5w4Bxpwx35yi3pYJtLyDgq3YOq_crTmSKarUY,6803
477
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v4/tpc_pytorch.py,sha256=DVBWc_97B0s9PHHPYRoT5FFYs8tcGY7LC3GU1TqP88c,5986
478
474
  model_compression_toolkit/target_platform_capabilities/tpc_models/qnnpack_tpc/__init__.py,sha256=cco4TmeIDIh32nj9ZZXVkws4dd9F2UDrmjKzTN8G0V0,697
479
475
  model_compression_toolkit/target_platform_capabilities/tpc_models/qnnpack_tpc/target_platform_capabilities.py,sha256=is00rNrDmmirYsyMtMkWz0DwOA92-x7hAJwpd6z1n2E,2806
480
476
  model_compression_toolkit/target_platform_capabilities/tpc_models/qnnpack_tpc/latest/__init__.py,sha256=CXC-HQolSDu7j8V-Xm-SWGCd74gXB3XnAkEhI_TVbIQ,1516
@@ -531,8 +527,8 @@ model_compression_toolkit/xquant/pytorch/model_analyzer.py,sha256=b93o800yVB3Z-i
531
527
  model_compression_toolkit/xquant/pytorch/pytorch_report_utils.py,sha256=bOc-hFL3gdoSM1Th_S2N_-9JJSlPGpZCTx_QLJHS6lg,3388
532
528
  model_compression_toolkit/xquant/pytorch/similarity_functions.py,sha256=CERxq5K8rqaiE-DlwhZBTUd9x69dtYJlkHOPLB54vm8,2354
533
529
  model_compression_toolkit/xquant/pytorch/tensorboard_utils.py,sha256=mkoEktLFFHtEKzzFRn_jCnxjhJolK12TZ5AQeDHzUO8,9767
534
- mct_nightly-2.1.0.20240821.444.dist-info/LICENSE.md,sha256=aYSSIb-5AFPeITTvXm1UAoe0uYBiMmSS8flvXaaFUks,10174
535
- mct_nightly-2.1.0.20240821.444.dist-info/METADATA,sha256=CB9zxyzGHxt_fKfaVpEJ0av1kBt6yPEN3gTzefmGHHY,19718
536
- mct_nightly-2.1.0.20240821.444.dist-info/WHEEL,sha256=eOLhNAGa2EW3wWl_TU484h7q1UNgy0JXjjoqKoxAAQc,92
537
- mct_nightly-2.1.0.20240821.444.dist-info/top_level.txt,sha256=gsYA8juk0Z-ZmQRKULkb3JLGdOdz8jW_cMRjisn9ga4,26
538
- mct_nightly-2.1.0.20240821.444.dist-info/RECORD,,
530
+ mct_nightly-2.1.0.20240822.448.dist-info/LICENSE.md,sha256=aYSSIb-5AFPeITTvXm1UAoe0uYBiMmSS8flvXaaFUks,10174
531
+ mct_nightly-2.1.0.20240822.448.dist-info/METADATA,sha256=FKbBDaOPXg2CWXPUpwPKk3NdJCjcYm9xCOrPh5HPfWM,19718
532
+ mct_nightly-2.1.0.20240822.448.dist-info/WHEEL,sha256=eOLhNAGa2EW3wWl_TU484h7q1UNgy0JXjjoqKoxAAQc,92
533
+ mct_nightly-2.1.0.20240822.448.dist-info/top_level.txt,sha256=gsYA8juk0Z-ZmQRKULkb3JLGdOdz8jW_cMRjisn9ga4,26
534
+ mct_nightly-2.1.0.20240822.448.dist-info/RECORD,,
@@ -27,4 +27,4 @@ from model_compression_toolkit import data_generation
27
27
  from model_compression_toolkit import pruning
28
28
  from model_compression_toolkit.trainable_infrastructure.keras.load_model import keras_load_quantized_model
29
29
 
30
- __version__ = "2.1.0.20240821.000444"
30
+ __version__ = "2.1.0.20240822.000448"
@@ -152,6 +152,12 @@ def core_runner(in_model: Any,
152
152
  f'Mixed Precision has overwrite bit-width configuration{core_config.mixed_precision_config.configuration_overwrite}')
153
153
  bit_widths_config = core_config.mixed_precision_config.configuration_overwrite
154
154
 
155
+ if (target_resource_utilization.activation_memory < np.inf or
156
+ target_resource_utilization.total_memory < np.inf):
157
+ Logger.warning(
158
+ f"Running mixed precision for activation compression, please note this feature is experimental and is "
159
+ f"subject to future changes. If you encounter an issue, please open an issue in our GitHub "
160
+ f"project https://github.com/sony/model_optimization")
155
161
  else:
156
162
  bit_widths_config = []
157
163
 
@@ -42,8 +42,6 @@ def get_tpc_dict_by_fw(fw_name):
42
42
  get_keras_tpc as get_keras_tpc_v3
43
43
  from model_compression_toolkit.target_platform_capabilities.tpc_models.imx500_tpc.v3_lut.tpc_keras import \
44
44
  get_keras_tpc as get_keras_tpc_v3_lut
45
- from model_compression_toolkit.target_platform_capabilities.tpc_models.imx500_tpc.v4.tpc_keras import \
46
- get_keras_tpc as get_keras_tpc_v4
47
45
 
48
46
  # Keras: TPC versioning
49
47
  tpc_models_dict = {'v1': get_keras_tpc_v1,
@@ -53,7 +51,6 @@ def get_tpc_dict_by_fw(fw_name):
53
51
  'v2_lut': get_keras_tpc_v2_lut,
54
52
  'v3': get_keras_tpc_v3,
55
53
  'v3_lut': get_keras_tpc_v3_lut,
56
- 'v4': get_keras_tpc_v4,
57
54
  LATEST: get_keras_tpc_latest}
58
55
  elif fw_name == PYTORCH:
59
56
  ###############################
@@ -76,8 +73,6 @@ def get_tpc_dict_by_fw(fw_name):
76
73
  get_pytorch_tpc as get_pytorch_tpc_v3
77
74
  from model_compression_toolkit.target_platform_capabilities.tpc_models.imx500_tpc.v3_lut.tpc_pytorch import \
78
75
  get_pytorch_tpc as get_pytorch_tpc_v3_lut
79
- from model_compression_toolkit.target_platform_capabilities.tpc_models.imx500_tpc.v4.tpc_pytorch import \
80
- get_pytorch_tpc as get_pytorch_tpc_v4
81
76
 
82
77
  # Pytorch: TPC versioning
83
78
  tpc_models_dict = {'v1': get_pytorch_tpc_v1,
@@ -87,7 +82,6 @@ def get_tpc_dict_by_fw(fw_name):
87
82
  'v2_lut': get_pytorch_tpc_v2_lut,
88
83
  'v3': get_pytorch_tpc_v3,
89
84
  'v3_lut': get_pytorch_tpc_v3_lut,
90
- 'v4': get_pytorch_tpc_v4,
91
85
  LATEST: get_pytorch_tpc_latest}
92
86
  if tpc_models_dict is not None:
93
87
  return tpc_models_dict
@@ -167,6 +167,16 @@ def generate_tp_model(default_config: OpQuantizationConfig,
167
167
  weights_quantization_method=tp.QuantizationMethod.POWER_OF_TWO))
168
168
  const_configuration_options = tp.QuantizationConfigOptions([const_config])
169
169
 
170
+ # 16 bits inputs and outputs. Currently, only defined for consts since they are used in operators that
171
+ # support 16 bit as input and output.
172
+ const_config_input16 = const_config.clone_and_edit(
173
+ supported_input_activation_n_bits=(8, 16))
174
+ const_config_input16_output16 = const_config_input16.clone_and_edit(
175
+ activation_n_bits=16, signedness=Signedness.SIGNED)
176
+ const_configuration_options_inout16 = tp.QuantizationConfigOptions([const_config_input16_output16,
177
+ const_config_input16],
178
+ base_config=const_config_input16)
179
+
170
180
  # Create a TargetPlatformModel and set its default quantization config.
171
181
  # This default configuration will be used for all operations
172
182
  # unless specified otherwise (see OperatorsSet, for example):
@@ -186,8 +196,10 @@ def generate_tp_model(default_config: OpQuantizationConfig,
186
196
  # May suit for operations like: Dropout, Reshape, etc.
187
197
  default_qco = tp.get_default_quantization_config_options()
188
198
  tp.OperatorsSet("NoQuantization",
189
- default_qco.clone_and_edit(enable_activation_quantization=False)
199
+ default_qco.clone_and_edit(enable_activation_quantization=False,
200
+ supported_input_activation_n_bits=(8, 16))
190
201
  .clone_and_edit_weight_attribute(enable_weights_quantization=False))
202
+ tp.OperatorsSet("Default16BitInout", const_configuration_options_inout16)
191
203
 
192
204
  # Create Mixed-Precision quantization configuration options from the given list of OpQuantizationConfig objects
193
205
  mixed_precision_configuration_options = tp.QuantizationConfigOptions(mixed_precision_cfg_list,
@@ -200,9 +212,9 @@ def generate_tp_model(default_config: OpQuantizationConfig,
200
212
  # Define operations sets without quantization configuration
201
213
  # options (useful for creating fusing patterns, for example):
202
214
  any_relu = tp.OperatorsSet("AnyReLU")
203
- add = tp.OperatorsSet("Add", const_configuration_options)
204
- sub = tp.OperatorsSet("Sub", const_configuration_options)
205
- mul = tp.OperatorsSet("Mul", const_configuration_options)
215
+ add = tp.OperatorsSet("Add", const_configuration_options_inout16)
216
+ sub = tp.OperatorsSet("Sub", const_configuration_options_inout16)
217
+ mul = tp.OperatorsSet("Mul", const_configuration_options_inout16)
206
218
  div = tp.OperatorsSet("Div", const_configuration_options)
207
219
  prelu = tp.OperatorsSet("PReLU")
208
220
  swish = tp.OperatorsSet("Swish")
@@ -26,11 +26,11 @@ if FOUND_SONY_CUSTOM_LAYERS:
26
26
  if version.parse(tf.__version__) >= version.parse("2.13"):
27
27
  from keras.src.layers import Conv2D, DepthwiseConv2D, Dense, Reshape, ZeroPadding2D, Dropout, \
28
28
  MaxPooling2D, Activation, ReLU, Add, Subtract, Multiply, PReLU, Flatten, Cropping2D, LeakyReLU, Permute, \
29
- Conv2DTranspose, Identity
29
+ Conv2DTranspose, Identity, Concatenate
30
30
  else:
31
31
  from keras.layers import Conv2D, DepthwiseConv2D, Dense, Reshape, ZeroPadding2D, Dropout, \
32
32
  MaxPooling2D, Activation, ReLU, Add, Subtract, Multiply, PReLU, Flatten, Cropping2D, LeakyReLU, Permute, \
33
- Conv2DTranspose, Identity
33
+ Conv2DTranspose, Identity, Concatenate
34
34
 
35
35
  from model_compression_toolkit.target_platform_capabilities.tpc_models.imx500_tpc.v3.tp_model import get_tp_model
36
36
  import model_compression_toolkit as mct
@@ -84,6 +84,7 @@ def generate_keras_tpc(name: str, tp_model: tp.TargetPlatformModel):
84
84
  tf.compat.v1.gather,
85
85
  tf.nn.top_k,
86
86
  tf.__operators__.getitem,
87
+ tf.strided_slice,
87
88
  tf.image.combined_non_max_suppression,
88
89
  tf.compat.v1.shape]
89
90
 
@@ -92,6 +93,8 @@ def generate_keras_tpc(name: str, tp_model: tp.TargetPlatformModel):
92
93
 
93
94
  with keras_tpc:
94
95
  tp.OperationsSetToLayers("NoQuantization", no_quant_list)
96
+ tp.OperationsSetToLayers("Default16BitInout", [tf.stack,
97
+ tf.concat, Concatenate])
95
98
  tp.OperationsSetToLayers("Conv",
96
99
  [Conv2D,
97
100
  DepthwiseConv2D,
@@ -17,7 +17,7 @@ import operator
17
17
 
18
18
  import torch
19
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
20
+ gather, equal, transpose, permute, argmax, squeeze, multiply, subtract
21
21
  from torch.nn import Conv2d, Linear, ConvTranspose2d, MaxPool2d
22
22
  from torch.nn import Dropout, Flatten, Hardtanh, Identity
23
23
  from torch.nn import ReLU, ReLU6, PReLU, SiLU, Sigmoid, Tanh, Hardswish, LeakyReLU
@@ -85,6 +85,8 @@ def generate_pytorch_tpc(name: str, tp_model: tp.TargetPlatformModel):
85
85
  topk,
86
86
  squeeze,
87
87
  MaxPool2d])
88
+ tp.OperationsSetToLayers("Default16BitInout",
89
+ [torch.stack, torch.cat, torch.concat, torch.concatenate])
88
90
 
89
91
  tp.OperationsSetToLayers("Conv", [Conv2d, ConvTranspose2d],
90
92
  attr_mapping=pytorch_linear_attr_mapping)
@@ -101,8 +103,8 @@ def generate_pytorch_tpc(name: str, tp_model: tp.TargetPlatformModel):
101
103
  tp.LayerFilterParams(hardtanh, min_val=0)])
102
104
 
103
105
  tp.OperationsSetToLayers("Add", [operator.add, add])
104
- tp.OperationsSetToLayers("Sub", [operator.sub, sub])
105
- tp.OperationsSetToLayers("Mul", [operator.mul, mul])
106
+ tp.OperationsSetToLayers("Sub", [operator.sub, sub, subtract])
107
+ tp.OperationsSetToLayers("Mul", [operator.mul, mul, multiply])
106
108
  tp.OperationsSetToLayers("Div", [operator.truediv, div])
107
109
  tp.OperationsSetToLayers("PReLU", [PReLU, prelu])
108
110
  tp.OperationsSetToLayers("Swish", [SiLU, silu, Hardswish, hardswish])
@@ -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__ = 'v4'
@@ -1,238 +0,0 @@
1
- # Copyright 2024 Sony Semiconductor Israel, Inc. All rights reserved.
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
- # ==============================================================================
15
- from typing import List, Tuple
16
-
17
- import model_compression_toolkit as mct
18
- 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
- from model_compression_toolkit.target_platform_capabilities.target_platform import OpQuantizationConfig, \
21
- TargetPlatformModel, Signedness
22
- from model_compression_toolkit.target_platform_capabilities.target_platform.op_quantization_config import \
23
- AttributeQuantizationConfig
24
-
25
- tp = mct.target_platform
26
-
27
-
28
- def get_tp_model() -> TargetPlatformModel:
29
- """
30
- A method that generates a default target platform model, with base 8-bit quantization configuration and 8, 4, 2
31
- bits configuration list for mixed-precision quantization.
32
- NOTE: in order to generate a target platform model with different configurations but with the same Operators Sets
33
- (for tests, experiments, etc.), use this method implementation as a test-case, i.e., override the
34
- 'get_op_quantization_configs' method and use its output to call 'generate_tp_model' with your configurations.
35
- This version enables metadata by default.
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_tp_model')
45
-
46
-
47
- def get_op_quantization_configs() -> \
48
- Tuple[OpQuantizationConfig, List[OpQuantizationConfig], OpQuantizationConfig]:
49
- """
50
- Creates a default configuration object for 8-bit quantization, to be used to set a default TargetPlatformModel.
51
- In addition, creates a default configuration objects list (with 8, 4 and 2 bit quantization) to be used as
52
- default configuration for mixed-precision quantization.
53
-
54
- Returns: An OpQuantizationConfig config object and a list of OpQuantizationConfig objects.
55
-
56
- """
57
-
58
- # TODO: currently, we don't want to quantize any attribute but the kernel by default,
59
- # to preserve the current behavior of MCT, so quantization is disabled for all other attributes.
60
- # Other quantization parameters are set to what we eventually want to quantize by default
61
- # when we enable multi-attributes quantization - THIS NEED TO BE MODIFIED IN ALL TP MODELS!
62
-
63
- # define a default quantization config for all non-specified weights attributes.
64
- default_weight_attr_config = AttributeQuantizationConfig(
65
- weights_quantization_method=tp.QuantizationMethod.POWER_OF_TWO,
66
- weights_n_bits=8,
67
- weights_per_channel_threshold=False,
68
- enable_weights_quantization=False, # TODO: this will changed to True once implementing multi-attributes quantization
69
- lut_values_bitwidth=None)
70
-
71
- # define a quantization config to quantize the kernel (for layers where there is a kernel attribute).
72
- kernel_base_config = AttributeQuantizationConfig(
73
- weights_quantization_method=tp.QuantizationMethod.SYMMETRIC,
74
- weights_n_bits=8,
75
- weights_per_channel_threshold=True,
76
- enable_weights_quantization=True,
77
- lut_values_bitwidth=None)
78
-
79
- # define a quantization config to quantize the bias (for layers where there is a bias attribute).
80
- bias_config = AttributeQuantizationConfig(
81
- weights_quantization_method=tp.QuantizationMethod.POWER_OF_TWO,
82
- weights_n_bits=FLOAT_BITWIDTH,
83
- weights_per_channel_threshold=False,
84
- enable_weights_quantization=False,
85
- lut_values_bitwidth=None)
86
-
87
- # Create a quantization config.
88
- # A quantization configuration defines how an operator
89
- # should be quantized on the modeled hardware:
90
-
91
- # We define a default config for operation without kernel attribute.
92
- # This is the default config that should be used for non-linear operations.
93
- eight_bits_default = tp.OpQuantizationConfig(
94
- default_weight_attr_config=default_weight_attr_config,
95
- attr_weights_configs_mapping={},
96
- activation_quantization_method=tp.QuantizationMethod.POWER_OF_TWO,
97
- activation_n_bits=8,
98
- supported_input_activation_n_bits=8,
99
- enable_activation_quantization=True,
100
- quantization_preserving=False,
101
- fixed_scale=None,
102
- fixed_zero_point=None,
103
- simd_size=32,
104
- signedness=Signedness.AUTO)
105
-
106
- # We define an 8-bit config for linear operations quantization, that include a kernel and bias attributes.
107
- linear_eight_bits = tp.OpQuantizationConfig(
108
- default_weight_attr_config=default_weight_attr_config,
109
- attr_weights_configs_mapping={KERNEL_ATTR: kernel_base_config, BIAS_ATTR: bias_config},
110
- activation_quantization_method=tp.QuantizationMethod.POWER_OF_TWO,
111
- activation_n_bits=8,
112
- supported_input_activation_n_bits=8,
113
- enable_activation_quantization=True,
114
- quantization_preserving=False,
115
- fixed_scale=None,
116
- fixed_zero_point=None,
117
- simd_size=32,
118
- signedness=Signedness.AUTO)
119
-
120
- # To quantize a model using mixed-precision, create
121
- # a list with more than one OpQuantizationConfig.
122
- # In this example, we quantize some operations' weights
123
- # using 2, 4 or 8 bits, and when using 2 or 4 bits, it's possible
124
- # to quantize the operations' activations using LUT.
125
- four_bits = linear_eight_bits.clone_and_edit(attr_to_edit={KERNEL_ATTR: {WEIGHTS_N_BITS: 4}},
126
- simd_size=linear_eight_bits.simd_size * 2)
127
- two_bits = linear_eight_bits.clone_and_edit(attr_to_edit={KERNEL_ATTR: {WEIGHTS_N_BITS: 2}},
128
- simd_size=linear_eight_bits.simd_size * 4)
129
-
130
- mixed_precision_cfg_list = [linear_eight_bits, four_bits, two_bits]
131
-
132
- return linear_eight_bits, mixed_precision_cfg_list, eight_bits_default
133
-
134
-
135
- def generate_tp_model(default_config: OpQuantizationConfig,
136
- base_config: OpQuantizationConfig,
137
- mixed_precision_cfg_list: List[OpQuantizationConfig],
138
- name: str) -> TargetPlatformModel:
139
- """
140
- Generates TargetPlatformModel with default defined Operators Sets, based on the given base configuration and
141
- mixed-precision configurations options list.
142
-
143
- Args
144
- default_config: A default OpQuantizationConfig to set as the TP model default configuration.
145
- base_config: An OpQuantizationConfig to set as the TargetPlatformModel base configuration for mixed-precision purposes only.
146
- mixed_precision_cfg_list: A list of OpQuantizationConfig to be used as the TP model mixed-precision
147
- quantization configuration options.
148
- name: The name of the TargetPlatformModel.
149
-
150
- Returns: A TargetPlatformModel object.
151
-
152
- """
153
- # Create a QuantizationConfigOptions, which defines a set
154
- # of possible configurations to consider when quantizing a set of operations (in mixed-precision, for example).
155
- # If the QuantizationConfigOptions contains only one configuration,
156
- # this configuration will be used for the operation quantization:
157
- default_configuration_options = tp.QuantizationConfigOptions([default_config])
158
-
159
- # Create a QuantizationConfigOptions for quantizing constants in functional ops.
160
- # Constant configuration is similar to the default eight bit configuration except for PoT
161
- # quantization method for the constant.
162
- # Since the constants are not named attributes of the layer, we use the default_weight_attr_config to
163
- # define the desired quantization properties for them.
164
- const_config = default_config.clone_and_edit(
165
- default_weight_attr_config=default_config.default_weight_attr_config.clone_and_edit(
166
- enable_weights_quantization=True, weights_per_channel_threshold=True,
167
- weights_quantization_method=tp.QuantizationMethod.POWER_OF_TWO))
168
- const_configuration_options = tp.QuantizationConfigOptions([const_config])
169
-
170
- # 16 bits inputs and outputs. Currently, only defined for consts since they are used in operators that
171
- # support 16 bit as input and output.
172
- const_config_input16 = const_config.clone_and_edit(
173
- supported_input_activation_n_bits=(8, 16))
174
- const_config_input16_output16 = const_config_input16.clone_and_edit(
175
- activation_n_bits=16, signedness=Signedness.SIGNED)
176
- const_configuration_options_inout16 = tp.QuantizationConfigOptions([const_config_input16_output16,
177
- const_config_input16],
178
- base_config=const_config_input16)
179
-
180
- # Create a TargetPlatformModel and set its default quantization config.
181
- # This default configuration will be used for all operations
182
- # unless specified otherwise (see OperatorsSet, for example):
183
- generated_tpm = tp.TargetPlatformModel(default_configuration_options, add_metadata=True, name=name)
184
-
185
- # To start defining the model's components (such as operator sets, and fusing patterns),
186
- # use 'with' the TargetPlatformModel instance, and create them as below:
187
- with generated_tpm:
188
- # Create an OperatorsSet to represent a set of operations.
189
- # Each OperatorsSet has a unique label.
190
- # If a quantization configuration options is passed, these options will
191
- # be used for operations that will be attached to this set's label.
192
- # Otherwise, it will be a configure-less set (used in fusing):
193
-
194
- generated_tpm.set_simd_padding(is_simd_padding=True)
195
-
196
- # May suit for operations like: Dropout, Reshape, etc.
197
- default_qco = tp.get_default_quantization_config_options()
198
- tp.OperatorsSet("NoQuantization",
199
- default_qco.clone_and_edit(enable_activation_quantization=False,
200
- supported_input_activation_n_bits=(8, 16))
201
- .clone_and_edit_weight_attribute(enable_weights_quantization=False))
202
- tp.OperatorsSet("Default16BitInout", const_configuration_options_inout16)
203
-
204
- # Create Mixed-Precision quantization configuration options from the given list of OpQuantizationConfig objects
205
- mixed_precision_configuration_options = tp.QuantizationConfigOptions(mixed_precision_cfg_list,
206
- base_config=base_config)
207
-
208
- # Define operator sets that use mixed_precision_configuration_options:
209
- conv = tp.OperatorsSet("Conv", mixed_precision_configuration_options)
210
- fc = tp.OperatorsSet("FullyConnected", mixed_precision_configuration_options)
211
-
212
- # Define operations sets without quantization configuration
213
- # options (useful for creating fusing patterns, for example):
214
- any_relu = tp.OperatorsSet("AnyReLU")
215
- add = tp.OperatorsSet("Add", const_configuration_options_inout16)
216
- sub = tp.OperatorsSet("Sub", const_configuration_options_inout16)
217
- mul = tp.OperatorsSet("Mul", const_configuration_options_inout16)
218
- div = tp.OperatorsSet("Div", const_configuration_options)
219
- prelu = tp.OperatorsSet("PReLU")
220
- swish = tp.OperatorsSet("Swish")
221
- sigmoid = tp.OperatorsSet("Sigmoid")
222
- tanh = tp.OperatorsSet("Tanh")
223
-
224
- # Combine multiple operators into a single operator to avoid quantization between
225
- # them. To do this we define fusing patterns using the OperatorsSets that were created.
226
- # To group multiple sets with regard to fusing, an OperatorSetConcat can be created
227
- activations_after_conv_to_fuse = tp.OperatorSetConcat(any_relu, swish, prelu, sigmoid, tanh)
228
- activations_after_fc_to_fuse = tp.OperatorSetConcat(any_relu, swish, sigmoid)
229
- any_binary = tp.OperatorSetConcat(add, sub, mul, div)
230
-
231
- # ------------------- #
232
- # Fusions
233
- # ------------------- #
234
- tp.Fusing([conv, activations_after_conv_to_fuse])
235
- tp.Fusing([fc, activations_after_fc_to_fuse])
236
- tp.Fusing([any_binary, any_relu])
237
-
238
- return generated_tpm
@@ -1,134 +0,0 @@
1
- # Copyright 2024 Sony Semiconductor Israel, Inc. All rights reserved.
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
- # ==============================================================================
15
- import tensorflow as tf
16
- from packaging import version
17
-
18
- from model_compression_toolkit.defaultdict import DefaultDict
19
- from model_compression_toolkit.verify_packages import FOUND_SONY_CUSTOM_LAYERS
20
- from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, KERAS_DEPTHWISE_KERNEL, \
21
- KERAS_KERNEL, BIAS_ATTR, BIAS
22
-
23
- if FOUND_SONY_CUSTOM_LAYERS:
24
- from sony_custom_layers.keras.object_detection.ssd_post_process import SSDPostProcess
25
-
26
- if version.parse(tf.__version__) >= version.parse("2.13"):
27
- from keras.src.layers import Conv2D, DepthwiseConv2D, Dense, Reshape, ZeroPadding2D, Dropout, \
28
- MaxPooling2D, Activation, ReLU, Add, Subtract, Multiply, PReLU, Flatten, Cropping2D, LeakyReLU, Permute, \
29
- Conv2DTranspose, Identity, Concatenate
30
- else:
31
- from keras.layers import Conv2D, DepthwiseConv2D, Dense, Reshape, ZeroPadding2D, Dropout, \
32
- MaxPooling2D, Activation, ReLU, Add, Subtract, Multiply, PReLU, Flatten, Cropping2D, LeakyReLU, Permute, \
33
- Conv2DTranspose, Identity, Concatenate
34
-
35
- from model_compression_toolkit.target_platform_capabilities.tpc_models.imx500_tpc.v4.tp_model import get_tp_model
36
- import model_compression_toolkit as mct
37
- from model_compression_toolkit.target_platform_capabilities.tpc_models.imx500_tpc.v4 import __version__ as TPC_VERSION
38
-
39
- tp = mct.target_platform
40
-
41
-
42
- def get_keras_tpc() -> tp.TargetPlatformCapabilities:
43
- """
44
- get a Keras TargetPlatformCapabilities object with default operation sets to layers mapping.
45
-
46
- Returns: a Keras TargetPlatformCapabilities object for the given TargetPlatformModel.
47
- """
48
- imx500_tpc_tp_model = get_tp_model()
49
- return generate_keras_tpc(name='imx500_tpc_keras_tpc', tp_model=imx500_tpc_tp_model)
50
-
51
-
52
- def generate_keras_tpc(name: str, tp_model: tp.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, name=name, version=TPC_VERSION)
64
-
65
- no_quant_list = [Identity,
66
- tf.identity,
67
- Reshape,
68
- tf.reshape,
69
- Permute,
70
- tf.transpose,
71
- Flatten,
72
- Cropping2D,
73
- ZeroPadding2D,
74
- Dropout,
75
- MaxPooling2D,
76
- tf.split,
77
- tf.quantization.fake_quant_with_min_max_vars,
78
- tf.math.argmax,
79
- tf.shape,
80
- tf.math.equal,
81
- tf.gather,
82
- tf.cast,
83
- tf.unstack,
84
- tf.compat.v1.gather,
85
- tf.nn.top_k,
86
- tf.__operators__.getitem,
87
- tf.strided_slice,
88
- tf.image.combined_non_max_suppression,
89
- tf.compat.v1.shape]
90
-
91
- if FOUND_SONY_CUSTOM_LAYERS:
92
- no_quant_list.append(SSDPostProcess)
93
-
94
- with keras_tpc:
95
- tp.OperationsSetToLayers("NoQuantization", no_quant_list)
96
- tp.OperationsSetToLayers("Default16BitInout", [tf.stack,
97
- tf.concat, Concatenate])
98
- tp.OperationsSetToLayers("Conv",
99
- [Conv2D,
100
- DepthwiseConv2D,
101
- Conv2DTranspose,
102
- tf.nn.conv2d,
103
- tf.nn.depthwise_conv2d,
104
- tf.nn.conv2d_transpose],
105
- # we provide attributes mapping that maps each layer type in the operations set
106
- # that has weights attributes with provided quantization config (in the tp model) to
107
- # its framework-specific attribute name.
108
- # note that a DefaultDict should be provided if not all the layer types in the
109
- # operation set are provided separately in the mapping.
110
- attr_mapping={
111
- KERNEL_ATTR: DefaultDict({
112
- DepthwiseConv2D: KERAS_DEPTHWISE_KERNEL,
113
- tf.nn.depthwise_conv2d: KERAS_DEPTHWISE_KERNEL}, default_value=KERAS_KERNEL),
114
- BIAS_ATTR: DefaultDict(default_value=BIAS)})
115
- tp.OperationsSetToLayers("FullyConnected", [Dense],
116
- attr_mapping={KERNEL_ATTR: DefaultDict(default_value=KERAS_KERNEL),
117
- BIAS_ATTR: DefaultDict(default_value=BIAS)})
118
- tp.OperationsSetToLayers("AnyReLU", [tf.nn.relu,
119
- tf.nn.relu6,
120
- tf.nn.leaky_relu,
121
- ReLU,
122
- LeakyReLU,
123
- tp.LayerFilterParams(Activation, activation="relu"),
124
- tp.LayerFilterParams(Activation, activation="leaky_relu")])
125
- tp.OperationsSetToLayers("Add", [tf.add, Add])
126
- tp.OperationsSetToLayers("Sub", [tf.subtract, Subtract])
127
- tp.OperationsSetToLayers("Mul", [tf.math.multiply, Multiply])
128
- tp.OperationsSetToLayers("Div", [tf.math.divide, tf.math.truediv])
129
- tp.OperationsSetToLayers("PReLU", [PReLU])
130
- tp.OperationsSetToLayers("Swish", [tf.nn.swish, tp.LayerFilterParams(Activation, activation="swish")])
131
- tp.OperationsSetToLayers("Sigmoid", [tf.nn.sigmoid, tp.LayerFilterParams(Activation, activation="sigmoid")])
132
- tp.OperationsSetToLayers("Tanh", [tf.nn.tanh, tp.LayerFilterParams(Activation, activation="tanh")])
133
-
134
- return keras_tpc
@@ -1,114 +0,0 @@
1
- # Copyright 2024 Sony Semiconductor Israel, Inc. All rights reserved.
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
- # ==============================================================================
15
-
16
- import operator
17
-
18
- import torch
19
- from torch import add, sub, mul, div, flatten, reshape, split, unsqueeze, dropout, sigmoid, tanh, chunk, unbind, topk, \
20
- gather, equal, transpose, permute, argmax, squeeze, multiply, subtract
21
- from torch.nn import Conv2d, Linear, ConvTranspose2d, MaxPool2d
22
- from torch.nn import Dropout, Flatten, Hardtanh, Identity
23
- from torch.nn import ReLU, ReLU6, PReLU, SiLU, Sigmoid, Tanh, Hardswish, LeakyReLU
24
- from torch.nn.functional import relu, relu6, prelu, silu, hardtanh, hardswish, leaky_relu
25
-
26
- from model_compression_toolkit.defaultdict import DefaultDict
27
- from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, BIAS_ATTR, PYTORCH_KERNEL, \
28
- BIAS
29
- from model_compression_toolkit.target_platform_capabilities.tpc_models.imx500_tpc.v4.tp_model import get_tp_model
30
- import model_compression_toolkit as mct
31
- from model_compression_toolkit.target_platform_capabilities.tpc_models.imx500_tpc.v4 import __version__ as TPC_VERSION
32
-
33
- tp = mct.target_platform
34
-
35
-
36
- def get_pytorch_tpc() -> tp.TargetPlatformCapabilities:
37
- """
38
- get a Pytorch TargetPlatformCapabilities object with default operation sets to layers mapping.
39
-
40
- Returns: a Pytorch TargetPlatformCapabilities object for the given TargetPlatformModel.
41
- """
42
- imx500_tpc_tp_model = get_tp_model()
43
- return generate_pytorch_tpc(name='imx500_tpc_pytorch_tpc', tp_model=imx500_tpc_tp_model)
44
-
45
-
46
- def generate_pytorch_tpc(name: str, tp_model: tp.TargetPlatformModel):
47
- """
48
- Generates a TargetPlatformCapabilities object with default operation sets to layers mapping.
49
- Args:
50
- name: Name of the TargetPlatformModel.
51
- tp_model: TargetPlatformModel object.
52
- Returns: a TargetPlatformCapabilities object for the given TargetPlatformModel.
53
- """
54
-
55
- pytorch_tpc = tp.TargetPlatformCapabilities(tp_model,
56
- name=name,
57
- version=TPC_VERSION)
58
-
59
- # we provide attributes mapping that maps each layer type in the operations set
60
- # that has weights attributes with provided quantization config (in the tp model) to
61
- # its framework-specific attribute name.
62
- # note that a DefaultDict should be provided if not all the layer types in the
63
- # operation set are provided separately in the mapping.
64
- pytorch_linear_attr_mapping = {KERNEL_ATTR: DefaultDict(default_value=PYTORCH_KERNEL),
65
- BIAS_ATTR: DefaultDict(default_value=BIAS)}
66
-
67
- with pytorch_tpc:
68
- tp.OperationsSetToLayers("NoQuantization", [Identity,
69
- Dropout,
70
- Flatten,
71
- dropout,
72
- flatten,
73
- split,
74
- operator.getitem,
75
- reshape,
76
- unsqueeze,
77
- chunk,
78
- unbind,
79
- torch.Tensor.size,
80
- permute,
81
- transpose,
82
- equal,
83
- argmax,
84
- gather,
85
- topk,
86
- squeeze,
87
- MaxPool2d])
88
- tp.OperationsSetToLayers("Default16BitInout",
89
- [torch.stack, torch.cat, torch.concat, torch.concatenate])
90
-
91
- tp.OperationsSetToLayers("Conv", [Conv2d, ConvTranspose2d],
92
- attr_mapping=pytorch_linear_attr_mapping)
93
- tp.OperationsSetToLayers("FullyConnected", [Linear],
94
- attr_mapping=pytorch_linear_attr_mapping)
95
- tp.OperationsSetToLayers("AnyReLU", [torch.relu,
96
- ReLU,
97
- ReLU6,
98
- LeakyReLU,
99
- relu,
100
- relu6,
101
- leaky_relu,
102
- tp.LayerFilterParams(Hardtanh, min_val=0),
103
- tp.LayerFilterParams(hardtanh, min_val=0)])
104
-
105
- tp.OperationsSetToLayers("Add", [operator.add, add])
106
- tp.OperationsSetToLayers("Sub", [operator.sub, sub, subtract])
107
- tp.OperationsSetToLayers("Mul", [operator.mul, mul, multiply])
108
- tp.OperationsSetToLayers("Div", [operator.truediv, div])
109
- tp.OperationsSetToLayers("PReLU", [PReLU, prelu])
110
- tp.OperationsSetToLayers("Swish", [SiLU, silu, Hardswish, hardswish])
111
- tp.OperationsSetToLayers("Sigmoid", [Sigmoid, sigmoid])
112
- tp.OperationsSetToLayers("Tanh", [Tanh, tanh])
113
-
114
- return pytorch_tpc