mct-nightly 2.2.0.20250106.546__py3-none-any.whl → 2.2.0.20250107.15510__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {mct_nightly-2.2.0.20250106.546.dist-info → mct_nightly-2.2.0.20250107.15510.dist-info}/METADATA +1 -1
- {mct_nightly-2.2.0.20250106.546.dist-info → mct_nightly-2.2.0.20250107.15510.dist-info}/RECORD +43 -78
- model_compression_toolkit/__init__.py +1 -1
- model_compression_toolkit/core/__init__.py +1 -1
- model_compression_toolkit/core/common/graph/memory_graph/compute_graph_max_cut.py +1 -1
- model_compression_toolkit/core/common/graph/memory_graph/cut.py +5 -2
- model_compression_toolkit/core/common/graph/memory_graph/max_cut_astar.py +25 -25
- model_compression_toolkit/core/common/quantization/quantization_config.py +19 -1
- model_compression_toolkit/core/keras/back2framework/keras_model_builder.py +1 -33
- model_compression_toolkit/core/keras/graph_substitutions/substitutions/conv_funcs_to_layer.py +2 -2
- model_compression_toolkit/core/keras/resource_utilization_data_facade.py +11 -1
- model_compression_toolkit/core/pytorch/graph_substitutions/substitutions/matmul_decomposition.py +499 -0
- model_compression_toolkit/core/pytorch/pytorch_implementation.py +3 -0
- model_compression_toolkit/core/pytorch/resource_utilization_data_facade.py +11 -3
- model_compression_toolkit/gptq/keras/quantization_facade.py +10 -1
- model_compression_toolkit/gptq/pytorch/quantization_facade.py +10 -1
- model_compression_toolkit/pruning/keras/pruning_facade.py +8 -2
- model_compression_toolkit/pruning/pytorch/pruning_facade.py +8 -2
- model_compression_toolkit/ptq/keras/quantization_facade.py +10 -1
- model_compression_toolkit/ptq/pytorch/quantization_facade.py +9 -1
- model_compression_toolkit/qat/__init__.py +5 -2
- model_compression_toolkit/qat/keras/quantization_facade.py +9 -1
- model_compression_toolkit/qat/pytorch/quantization_facade.py +9 -1
- model_compression_toolkit/target_platform_capabilities/schema/mct_current_schema.py +1 -1
- model_compression_toolkit/target_platform_capabilities/schema/v1.py +63 -55
- model_compression_toolkit/target_platform_capabilities/target_platform/targetplatform2framework/attach2fw.py +29 -18
- model_compression_toolkit/target_platform_capabilities/target_platform/targetplatform2framework/attach2keras.py +78 -57
- model_compression_toolkit/target_platform_capabilities/target_platform/targetplatform2framework/attach2pytorch.py +69 -54
- model_compression_toolkit/target_platform_capabilities/target_platform/targetplatform2framework/operations_to_layers.py +2 -4
- model_compression_toolkit/target_platform_capabilities/target_platform/targetplatform2framework/target_platform_capabilities.py +0 -10
- model_compression_toolkit/target_platform_capabilities/tpc_io_handler.py +93 -0
- model_compression_toolkit/target_platform_capabilities/tpc_models/get_target_platform_capabilities.py +46 -28
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/latest/__init__.py +6 -5
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v1/tp_model.py +51 -19
- model_compression_toolkit/target_platform_capabilities/tpc_models/qnnpack_tpc/latest/__init__.py +8 -4
- model_compression_toolkit/target_platform_capabilities/tpc_models/qnnpack_tpc/v1/tp_model.py +19 -9
- model_compression_toolkit/target_platform_capabilities/tpc_models/tflite_tpc/latest/__init__.py +7 -4
- model_compression_toolkit/target_platform_capabilities/tpc_models/tflite_tpc/v1/tp_model.py +46 -32
- model_compression_toolkit/xquant/keras/keras_report_utils.py +11 -3
- model_compression_toolkit/xquant/pytorch/pytorch_report_utils.py +10 -2
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/target_platform_capabilities.py +0 -98
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v1/tpc_keras.py +0 -129
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v1/tpc_pytorch.py +0 -108
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v1_lut/__init__.py +0 -16
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v1_lut/tp_model.py +0 -217
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v1_lut/tpc_keras.py +0 -130
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v1_lut/tpc_pytorch.py +0 -109
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v1_pot/__init__.py +0 -16
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v1_pot/tp_model.py +0 -215
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v1_pot/tpc_keras.py +0 -130
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v1_pot/tpc_pytorch.py +0 -110
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v2/__init__.py +0 -16
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v2/tp_model.py +0 -222
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v2/tpc_keras.py +0 -132
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v2/tpc_pytorch.py +0 -110
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v2_lut/__init__.py +0 -16
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v2_lut/tp_model.py +0 -219
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v2_lut/tpc_keras.py +0 -132
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v2_lut/tpc_pytorch.py +0 -109
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3/__init__.py +0 -16
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3/tp_model.py +0 -246
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3/tpc_keras.py +0 -135
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3/tpc_pytorch.py +0 -113
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3_lut/__init__.py +0 -16
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3_lut/tp_model.py +0 -230
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3_lut/tpc_keras.py +0 -132
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3_lut/tpc_pytorch.py +0 -110
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v4/__init__.py +0 -16
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v4/tp_model.py +0 -332
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v4/tpc_keras.py +0 -140
- model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v4/tpc_pytorch.py +0 -122
- model_compression_toolkit/target_platform_capabilities/tpc_models/qnnpack_tpc/target_platform_capabilities.py +0 -55
- model_compression_toolkit/target_platform_capabilities/tpc_models/qnnpack_tpc/v1/tpc_keras.py +0 -89
- model_compression_toolkit/target_platform_capabilities/tpc_models/qnnpack_tpc/v1/tpc_pytorch.py +0 -78
- model_compression_toolkit/target_platform_capabilities/tpc_models/tflite_tpc/target_platform_capabilities.py +0 -55
- model_compression_toolkit/target_platform_capabilities/tpc_models/tflite_tpc/v1/tpc_keras.py +0 -118
- model_compression_toolkit/target_platform_capabilities/tpc_models/tflite_tpc/v1/tpc_pytorch.py +0 -100
- {mct_nightly-2.2.0.20250106.546.dist-info → mct_nightly-2.2.0.20250107.15510.dist-info}/LICENSE.md +0 -0
- {mct_nightly-2.2.0.20250106.546.dist-info → mct_nightly-2.2.0.20250107.15510.dist-info}/WHEEL +0 -0
- {mct_nightly-2.2.0.20250106.546.dist-info → mct_nightly-2.2.0.20250107.15510.dist-info}/top_level.txt +0 -0
model_compression_toolkit/target_platform_capabilities/tpc_models/qnnpack_tpc/v1/tpc_keras.py
DELETED
@@ -1,89 +0,0 @@
|
|
1
|
-
# Copyright 2022 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
|
-
|
17
|
-
from packaging import version
|
18
|
-
|
19
|
-
import model_compression_toolkit.target_platform_capabilities.schema.mct_current_schema as schema
|
20
|
-
from model_compression_toolkit.defaultdict import DefaultDict
|
21
|
-
from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, KERAS_KERNEL, BIAS_ATTR, \
|
22
|
-
KERAS_DEPTHWISE_KERNEL, BIAS
|
23
|
-
from model_compression_toolkit.target_platform_capabilities.tpc_models.qnnpack_tpc.v1 import __version__ as TPC_VERSION
|
24
|
-
|
25
|
-
if version.parse(tf.__version__) >= version.parse("2.13"):
|
26
|
-
from keras.src.layers import Conv2D, DepthwiseConv2D, Conv2DTranspose, Dense, BatchNormalization, ReLU, Activation
|
27
|
-
else:
|
28
|
-
from keras.layers import Conv2D, DepthwiseConv2D, Conv2DTranspose, Dense, BatchNormalization, ReLU, Activation
|
29
|
-
|
30
|
-
from model_compression_toolkit.target_platform_capabilities.tpc_models.qnnpack_tpc.v1.tp_model import get_tp_model
|
31
|
-
import model_compression_toolkit as mct
|
32
|
-
|
33
|
-
tp = mct.target_platform
|
34
|
-
|
35
|
-
|
36
|
-
def get_keras_tpc() -> tp.TargetPlatformCapabilities:
|
37
|
-
"""
|
38
|
-
get a Keras TargetPlatformCapabilities object with default operation sets to layers mapping.
|
39
|
-
Returns: a Keras TargetPlatformCapabilities object for the given TargetPlatformModel.
|
40
|
-
"""
|
41
|
-
qnnpack_tp_model = get_tp_model()
|
42
|
-
return generate_keras_tpc(name='qnnpack_keras', tp_model=qnnpack_tp_model)
|
43
|
-
|
44
|
-
|
45
|
-
def generate_keras_tpc(name: str, tp_model: schema.TargetPlatformModel):
|
46
|
-
"""
|
47
|
-
Generates a TargetPlatformCapabilities object with default operation sets to layers mapping.
|
48
|
-
|
49
|
-
Args:
|
50
|
-
name: Name of the TargetPlatformCapabilities.
|
51
|
-
tp_model: TargetPlatformModel object.
|
52
|
-
|
53
|
-
Returns: a TargetPlatformCapabilities object for the given TargetPlatformModel.
|
54
|
-
"""
|
55
|
-
|
56
|
-
keras_tpc = tp.TargetPlatformCapabilities(tp_model)
|
57
|
-
|
58
|
-
with keras_tpc:
|
59
|
-
tp.OperationsSetToLayers("Conv",
|
60
|
-
[Conv2D,
|
61
|
-
DepthwiseConv2D,
|
62
|
-
Conv2DTranspose,
|
63
|
-
tf.nn.conv2d,
|
64
|
-
tf.nn.depthwise_conv2d,
|
65
|
-
tf.nn.conv2d_transpose],
|
66
|
-
# we provide attributes mapping that maps each layer type in the operations set
|
67
|
-
# that has weights attributes with provided quantization config (in the tp model) to
|
68
|
-
# its framework-specific attribute name.
|
69
|
-
# note that a DefaultDict should be provided if not all the layer types in the
|
70
|
-
# operation set are provided separately in the mapping.
|
71
|
-
attr_mapping={
|
72
|
-
KERNEL_ATTR: DefaultDict({
|
73
|
-
DepthwiseConv2D: KERAS_DEPTHWISE_KERNEL,
|
74
|
-
tf.nn.depthwise_conv2d: KERAS_DEPTHWISE_KERNEL}, default_value=KERAS_KERNEL),
|
75
|
-
BIAS_ATTR: DefaultDict(default_value=BIAS)})
|
76
|
-
|
77
|
-
tp.OperationsSetToLayers("Linear", [Dense],
|
78
|
-
attr_mapping={KERNEL_ATTR: DefaultDict(default_value=KERAS_KERNEL),
|
79
|
-
BIAS_ATTR: DefaultDict(default_value=BIAS)})
|
80
|
-
|
81
|
-
tp.OperationsSetToLayers("BatchNorm", [BatchNormalization,
|
82
|
-
tf.nn.batch_normalization])
|
83
|
-
|
84
|
-
tp.OperationsSetToLayers("Relu", [tf.nn.relu,
|
85
|
-
tf.nn.relu6,
|
86
|
-
tp.LayerFilterParams(ReLU, negative_slope=0.0),
|
87
|
-
tp.LayerFilterParams(Activation, activation="relu")])
|
88
|
-
|
89
|
-
return keras_tpc
|
model_compression_toolkit/target_platform_capabilities/tpc_models/qnnpack_tpc/v1/tpc_pytorch.py
DELETED
@@ -1,78 +0,0 @@
|
|
1
|
-
# Copyright 2022 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 torch
|
16
|
-
from torch.nn import Conv2d, Linear, BatchNorm2d, ConvTranspose2d, Hardtanh, ReLU, ReLU6
|
17
|
-
from torch.nn.functional import relu, relu6, hardtanh
|
18
|
-
|
19
|
-
import model_compression_toolkit.target_platform_capabilities.schema.mct_current_schema as schema
|
20
|
-
from model_compression_toolkit.defaultdict import DefaultDict
|
21
|
-
from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, PYTORCH_KERNEL, BIAS_ATTR, \
|
22
|
-
BIAS
|
23
|
-
from model_compression_toolkit.target_platform_capabilities.tpc_models.qnnpack_tpc.v1.tp_model import get_tp_model
|
24
|
-
import model_compression_toolkit as mct
|
25
|
-
from model_compression_toolkit.target_platform_capabilities.tpc_models.qnnpack_tpc.v1 import __version__ as TPC_VERSION
|
26
|
-
|
27
|
-
tp = mct.target_platform
|
28
|
-
|
29
|
-
|
30
|
-
def get_pytorch_tpc() -> tp.TargetPlatformCapabilities:
|
31
|
-
"""
|
32
|
-
get a Pytorch TargetPlatformCapabilities object with default operation sets to layers mapping.
|
33
|
-
Returns: a Pytorch TargetPlatformCapabilities object for the given TargetPlatformModel.
|
34
|
-
"""
|
35
|
-
qnnpack_pytorch = get_tp_model()
|
36
|
-
return generate_pytorch_tpc(name='qnnpack_pytorch', tp_model=qnnpack_pytorch)
|
37
|
-
|
38
|
-
|
39
|
-
def generate_pytorch_tpc(name: str, tp_model: schema.TargetPlatformModel):
|
40
|
-
"""
|
41
|
-
Generates a TargetPlatformCapabilities object with default operation sets to layers mapping.
|
42
|
-
Args:
|
43
|
-
name: Name of the TargetPlatformModel.
|
44
|
-
tp_model: TargetPlatformModel object.
|
45
|
-
Returns: a TargetPlatformCapabilities object for the given TargetPlatformModel.
|
46
|
-
"""
|
47
|
-
|
48
|
-
pytorch_tpc = tp.TargetPlatformCapabilities(tp_model)
|
49
|
-
|
50
|
-
# we provide attributes mapping that maps each layer type in the operations set
|
51
|
-
# that has weights attributes with provided quantization config (in the tp model) to
|
52
|
-
# its framework-specific attribute name.
|
53
|
-
# note that a DefaultDict should be provided if not all the layer types in the
|
54
|
-
# operation set are provided separately in the mapping.
|
55
|
-
pytorch_linear_attr_mapping = {KERNEL_ATTR: DefaultDict(default_value=PYTORCH_KERNEL),
|
56
|
-
BIAS_ATTR: DefaultDict(default_value=BIAS)}
|
57
|
-
|
58
|
-
with pytorch_tpc:
|
59
|
-
tp.OperationsSetToLayers("Conv", [Conv2d,
|
60
|
-
torch.nn.functional.conv2d,
|
61
|
-
ConvTranspose2d,
|
62
|
-
torch.nn.functional.conv_transpose2d],
|
63
|
-
attr_mapping=pytorch_linear_attr_mapping)
|
64
|
-
|
65
|
-
tp.OperationsSetToLayers("Linear", [Linear],
|
66
|
-
attr_mapping=pytorch_linear_attr_mapping)
|
67
|
-
|
68
|
-
tp.OperationsSetToLayers("BatchNorm", [BatchNorm2d])
|
69
|
-
|
70
|
-
tp.OperationsSetToLayers("Relu", [torch.relu,
|
71
|
-
ReLU,
|
72
|
-
ReLU6,
|
73
|
-
relu,
|
74
|
-
relu6,
|
75
|
-
tp.LayerFilterParams(Hardtanh, min_val=0),
|
76
|
-
tp.LayerFilterParams(hardtanh, min_val=0)])
|
77
|
-
|
78
|
-
return pytorch_tpc
|
@@ -1,55 +0,0 @@
|
|
1
|
-
# Copyright 2022 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
|
-
from model_compression_toolkit.constants import TENSORFLOW, PYTORCH
|
17
|
-
from model_compression_toolkit.verify_packages import FOUND_TORCH, FOUND_TF
|
18
|
-
from model_compression_toolkit.logger import Logger
|
19
|
-
from model_compression_toolkit.target_platform_capabilities.constants import LATEST
|
20
|
-
|
21
|
-
def get_tpc_dict_by_fw(fw_name):
|
22
|
-
tpc_models_dict = None
|
23
|
-
if fw_name == TENSORFLOW:
|
24
|
-
###############################
|
25
|
-
# Build Tensorflow TPC models
|
26
|
-
###############################
|
27
|
-
if FOUND_TF:
|
28
|
-
from model_compression_toolkit.target_platform_capabilities.tpc_models.tflite_tpc.v1.tpc_keras import \
|
29
|
-
get_keras_tpc as get_keras_tpc_v1
|
30
|
-
from model_compression_toolkit.target_platform_capabilities.tpc_models.tflite_tpc.latest import \
|
31
|
-
get_keras_tpc_latest
|
32
|
-
|
33
|
-
# Keras: TPC versioning
|
34
|
-
tpc_models_dict = {'v1': get_keras_tpc_v1,
|
35
|
-
LATEST: get_keras_tpc_latest}
|
36
|
-
elif fw_name == PYTORCH:
|
37
|
-
###############################
|
38
|
-
# Build Pytorch TPC models
|
39
|
-
###############################
|
40
|
-
if FOUND_TORCH:
|
41
|
-
from model_compression_toolkit.target_platform_capabilities.tpc_models.tflite_tpc.v1.tpc_pytorch import \
|
42
|
-
get_pytorch_tpc as get_pytorch_tpc_v1
|
43
|
-
from model_compression_toolkit.target_platform_capabilities.tpc_models.tflite_tpc.latest import \
|
44
|
-
get_pytorch_tpc_latest
|
45
|
-
|
46
|
-
# Pytorch: TPC versioning
|
47
|
-
tpc_models_dict = {'v1': get_pytorch_tpc_v1,
|
48
|
-
LATEST: get_pytorch_tpc_latest}
|
49
|
-
if tpc_models_dict is not None:
|
50
|
-
return tpc_models_dict
|
51
|
-
else:
|
52
|
-
Logger.critical(f'Framework {fw_name} is not supported in imx500 or the relevant packages are not '
|
53
|
-
f'installed. Please make sure the relevant packages are installed when using MCT for optimizing'
|
54
|
-
f' a {fw_name} model. For Tensorflow, please install tensorflow. For PyTorch, please install '
|
55
|
-
f'torch.') # pragma: no cover
|
model_compression_toolkit/target_platform_capabilities/tpc_models/tflite_tpc/v1/tpc_keras.py
DELETED
@@ -1,118 +0,0 @@
|
|
1
|
-
# Copyright 2022 Sony Semiconductor Israel, Inc. All rights reserved.
|
2
|
-
#
|
3
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
-
# you may not use this file except in compliance with the License.
|
5
|
-
# You may obtain a copy of the License at
|
6
|
-
#
|
7
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
-
#
|
9
|
-
# Unless required by applicable law or agreed to in writing, software
|
10
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
-
# See the License for the specific language governing permissions and
|
13
|
-
# limitations under the License.
|
14
|
-
# ==============================================================================
|
15
|
-
import tensorflow as tf
|
16
|
-
from packaging import version
|
17
|
-
|
18
|
-
import model_compression_toolkit.target_platform_capabilities.schema.mct_current_schema as schema
|
19
|
-
from model_compression_toolkit.defaultdict import DefaultDict
|
20
|
-
from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, KERAS_KERNEL, BIAS_ATTR, BIAS
|
21
|
-
|
22
|
-
if version.parse(tf.__version__) >= version.parse("2.13"):
|
23
|
-
from keras.src.layers import Conv2D, Dense, Reshape, ZeroPadding2D, AveragePooling2D, Activation, DepthwiseConv2D, \
|
24
|
-
MaxPooling2D, ReLU, Add, Softmax, Concatenate, Multiply, Maximum, Minimum, BatchNormalization
|
25
|
-
else:
|
26
|
-
from keras.layers import Conv2D, Dense, Reshape, ZeroPadding2D, AveragePooling2D, Activation, DepthwiseConv2D, \
|
27
|
-
MaxPooling2D, ReLU, Add, Softmax, Concatenate, Multiply, Maximum, Minimum, BatchNormalization
|
28
|
-
|
29
|
-
from tensorflow.python.keras.layers.core import SlicingOpLambda
|
30
|
-
from tensorflow.python.ops.image_ops_impl import ResizeMethod
|
31
|
-
from model_compression_toolkit.target_platform_capabilities.target_platform.targetplatform2framework.attribute_filter import \
|
32
|
-
Eq
|
33
|
-
|
34
|
-
from model_compression_toolkit.target_platform_capabilities.tpc_models.tflite_tpc.v1.tp_model import get_tp_model
|
35
|
-
import model_compression_toolkit as mct
|
36
|
-
from model_compression_toolkit.target_platform_capabilities.tpc_models.tflite_tpc.v1 import __version__ as TPC_VERSION
|
37
|
-
|
38
|
-
tp = mct.target_platform
|
39
|
-
|
40
|
-
|
41
|
-
def get_keras_tpc() -> tp.TargetPlatformCapabilities:
|
42
|
-
"""
|
43
|
-
get a Keras TargetPlatformCapabilities object with default operation sets to layers mapping.
|
44
|
-
Returns: a Keras TargetPlatformCapabilities object for the given TargetPlatformModel.
|
45
|
-
"""
|
46
|
-
tflite_tp_model = get_tp_model()
|
47
|
-
return generate_keras_tpc(name='tflite_keras', tp_model=tflite_tp_model)
|
48
|
-
|
49
|
-
|
50
|
-
def generate_keras_tpc(name: str, tp_model: schema.TargetPlatformModel):
|
51
|
-
"""
|
52
|
-
Generates a TargetPlatformCapabilities object with default operation sets to layers mapping.
|
53
|
-
|
54
|
-
Args:
|
55
|
-
name: Name of the TargetPlatformCapabilities.
|
56
|
-
tp_model: TargetPlatformModel object.
|
57
|
-
|
58
|
-
Returns: a TargetPlatformCapabilities object for the given TargetPlatformModel.
|
59
|
-
"""
|
60
|
-
|
61
|
-
keras_tpc = tp.TargetPlatformCapabilities(tp_model)
|
62
|
-
|
63
|
-
with keras_tpc:
|
64
|
-
tp.OperationsSetToLayers("NoQuantization", [AveragePooling2D,
|
65
|
-
tf.nn.avg_pool2d,
|
66
|
-
Concatenate,
|
67
|
-
tf.concat,
|
68
|
-
MaxPooling2D,
|
69
|
-
Multiply,
|
70
|
-
tf.multiply,
|
71
|
-
Reshape,
|
72
|
-
tf.reshape,
|
73
|
-
tp.LayerFilterParams(tf.image.resize,
|
74
|
-
method=ResizeMethod.BILINEAR),
|
75
|
-
tf.nn.space_to_depth,
|
76
|
-
ZeroPadding2D,
|
77
|
-
tf.unstack,
|
78
|
-
tf.gather,
|
79
|
-
tf.compat.v1.batch_to_space_nd,
|
80
|
-
tf.space_to_batch_nd,
|
81
|
-
tf.transpose,
|
82
|
-
tf.maximum,
|
83
|
-
Maximum,
|
84
|
-
tf.minimum,
|
85
|
-
Minimum,
|
86
|
-
tf.pad,
|
87
|
-
tf.slice,
|
88
|
-
SlicingOpLambda])
|
89
|
-
|
90
|
-
tp.OperationsSetToLayers("FullyConnected", [Dense],
|
91
|
-
# we provide attributes mapping that maps each layer type in the operations set
|
92
|
-
# that has weights attributes with provided quantization config (in the tp model) to
|
93
|
-
# its framework-specific attribute name.
|
94
|
-
# note that a DefaultDict should be provided if not all the layer types in the
|
95
|
-
# operation set are provided separately in the mapping.
|
96
|
-
attr_mapping={
|
97
|
-
KERNEL_ATTR: DefaultDict(default_value=KERAS_KERNEL),
|
98
|
-
BIAS_ATTR: DefaultDict(default_value=BIAS)})
|
99
|
-
tp.OperationsSetToLayers("L2Normalization", [tf.math.l2_normalize])
|
100
|
-
tp.OperationsSetToLayers("LogSoftmax", [tf.nn.log_softmax])
|
101
|
-
tp.OperationsSetToLayers("Tanh", [tf.nn.tanh, tp.LayerFilterParams(Activation, activation="tanh")])
|
102
|
-
tp.OperationsSetToLayers("Softmax", [tf.nn.softmax,
|
103
|
-
Softmax,
|
104
|
-
tp.LayerFilterParams(Activation, activation="softmax")])
|
105
|
-
tp.OperationsSetToLayers("Logistic", [tf.sigmoid, tp.LayerFilterParams(Activation, activation="sigmoid")])
|
106
|
-
|
107
|
-
tp.OperationsSetToLayers("Conv2d", [Conv2D, DepthwiseConv2D])
|
108
|
-
tp.OperationsSetToLayers("Relu", [tf.nn.relu,
|
109
|
-
tf.nn.relu6,
|
110
|
-
tp.LayerFilterParams(ReLU, Eq("max_value", None) | Eq("max_value", 6)),
|
111
|
-
tp.LayerFilterParams(Activation, activation="relu")])
|
112
|
-
tp.OperationsSetToLayers("Elu", [tf.nn.elu, tp.LayerFilterParams(Activation, activation="elu")])
|
113
|
-
tp.OperationsSetToLayers("BatchNorm", [BatchNormalization, tf.nn.batch_normalization])
|
114
|
-
tp.OperationsSetToLayers("Squeeze", [tf.squeeze])
|
115
|
-
tp.OperationsSetToLayers("BiasAdd", [tf.nn.bias_add])
|
116
|
-
tp.OperationsSetToLayers("Add", [tf.add, Add])
|
117
|
-
|
118
|
-
return keras_tpc
|
model_compression_toolkit/target_platform_capabilities/tpc_models/tflite_tpc/v1/tpc_pytorch.py
DELETED
@@ -1,100 +0,0 @@
|
|
1
|
-
# Copyright 2022 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 torch
|
16
|
-
from torch.nn import AvgPool2d, MaxPool2d
|
17
|
-
from torch.nn.functional import avg_pool2d, max_pool2d, interpolate
|
18
|
-
|
19
|
-
import model_compression_toolkit.target_platform_capabilities.schema.mct_current_schema as schema
|
20
|
-
from model_compression_toolkit.defaultdict import DefaultDict
|
21
|
-
from model_compression_toolkit.target_platform_capabilities.constants import KERNEL_ATTR, PYTORCH_KERNEL, BIAS_ATTR, \
|
22
|
-
BIAS
|
23
|
-
from model_compression_toolkit.target_platform_capabilities.target_platform.targetplatform2framework.attribute_filter import Eq
|
24
|
-
|
25
|
-
from model_compression_toolkit.target_platform_capabilities.tpc_models.tflite_tpc.v1.tp_model import get_tp_model
|
26
|
-
import model_compression_toolkit as mct
|
27
|
-
from model_compression_toolkit.target_platform_capabilities.tpc_models.tflite_tpc.v1 import __version__ as TPC_VERSION
|
28
|
-
|
29
|
-
tp = mct.target_platform
|
30
|
-
|
31
|
-
|
32
|
-
def get_pytorch_tpc() -> tp.TargetPlatformCapabilities:
|
33
|
-
"""
|
34
|
-
get a Pytorch TargetPlatformCapabilities object with default operation sets to layers mapping.
|
35
|
-
Returns: a Pytorch TargetPlatformCapabilities object for the given TargetPlatformModel.
|
36
|
-
"""
|
37
|
-
tflite_tp_model = get_tp_model()
|
38
|
-
return generate_pytorch_tpc(name='tflite_torch', tp_model=tflite_tp_model)
|
39
|
-
|
40
|
-
|
41
|
-
def generate_pytorch_tpc(name: str, tp_model: schema.TargetPlatformModel):
|
42
|
-
"""
|
43
|
-
Generates a TargetPlatformCapabilities object with default operation sets to layers mapping.
|
44
|
-
Args:
|
45
|
-
name: Name of the TargetPlatformModel.
|
46
|
-
tp_model: TargetPlatformModel object.
|
47
|
-
Returns: a TargetPlatformCapabilities object for the given TargetPlatformModel.
|
48
|
-
"""
|
49
|
-
|
50
|
-
pytorch_tpc = tp.TargetPlatformCapabilities(tp_model)
|
51
|
-
|
52
|
-
with pytorch_tpc:
|
53
|
-
tp.OperationsSetToLayers("NoQuantization", [AvgPool2d,
|
54
|
-
avg_pool2d,
|
55
|
-
torch.cat,
|
56
|
-
torch.concat,
|
57
|
-
MaxPool2d,
|
58
|
-
max_pool2d,
|
59
|
-
torch.mul,
|
60
|
-
torch.multiply,
|
61
|
-
torch.reshape,
|
62
|
-
tp.LayerFilterParams(interpolate, mode='bilinear'),
|
63
|
-
torch.nn.ZeroPad2d,
|
64
|
-
torch.gather,
|
65
|
-
torch.transpose,
|
66
|
-
torch.maximum,
|
67
|
-
torch.max,
|
68
|
-
torch.minimum,
|
69
|
-
torch.min,
|
70
|
-
torch.nn.functional.pad,
|
71
|
-
torch.select,
|
72
|
-
torch.unbind])
|
73
|
-
|
74
|
-
tp.OperationsSetToLayers("FullyConnected", [torch.nn.Linear, torch.nn.functional.linear],
|
75
|
-
# we provide attributes mapping that maps each layer type in the operations set
|
76
|
-
# that has weights attributes with provided quantization config (in the tp model) to
|
77
|
-
# its framework-specific attribute name.
|
78
|
-
# note that a DefaultDict should be provided if not all the layer types in the
|
79
|
-
# operation set are provided separately in the mapping.
|
80
|
-
attr_mapping={KERNEL_ATTR: DefaultDict(default_value=PYTORCH_KERNEL),
|
81
|
-
BIAS_ATTR: DefaultDict(default_value=BIAS)})
|
82
|
-
tp.OperationsSetToLayers("L2Normalization",
|
83
|
-
[tp.LayerFilterParams(torch.nn.functional.normalize, Eq('p', 2) | Eq('p', None))])
|
84
|
-
tp.OperationsSetToLayers("LogSoftmax", [torch.nn.LogSoftmax])
|
85
|
-
tp.OperationsSetToLayers("Tanh", [torch.nn.Tanh, torch.nn.functional.tanh])
|
86
|
-
tp.OperationsSetToLayers("Softmax", [torch.nn.Softmax, torch.nn.functional.softmax])
|
87
|
-
tp.OperationsSetToLayers("Logistic", [torch.nn.Sigmoid, torch.nn.functional.sigmoid])
|
88
|
-
tp.OperationsSetToLayers("Conv2d", [torch.nn.Conv2d, torch.nn.functional.conv2d])
|
89
|
-
tp.OperationsSetToLayers("Relu", [torch.relu,
|
90
|
-
torch.nn.ReLU,
|
91
|
-
torch.nn.ReLU6,
|
92
|
-
torch.nn.functional.relu,
|
93
|
-
torch.nn.functional.relu6,
|
94
|
-
tp.LayerFilterParams(torch.nn.Hardtanh, min_val=0, max_val=6),
|
95
|
-
tp.LayerFilterParams(torch.nn.functional.hardtanh, min_val=0, max_val=6)])
|
96
|
-
tp.OperationsSetToLayers("Elu", [torch.nn.ELU, torch.nn.functional.elu])
|
97
|
-
tp.OperationsSetToLayers("BatchNorm", [torch.nn.BatchNorm2d, torch.nn.functional.batch_norm])
|
98
|
-
tp.OperationsSetToLayers("Squeeze", [torch.squeeze])
|
99
|
-
|
100
|
-
return pytorch_tpc
|
{mct_nightly-2.2.0.20250106.546.dist-info → mct_nightly-2.2.0.20250107.15510.dist-info}/LICENSE.md
RENAMED
File without changes
|
{mct_nightly-2.2.0.20250106.546.dist-info → mct_nightly-2.2.0.20250107.15510.dist-info}/WHEEL
RENAMED
File without changes
|
File without changes
|