mct-nightly 1.8.0.20052023.post401__py3-none-any.whl → 1.8.0.20230610.post356__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (115) hide show
  1. {mct_nightly-1.8.0.20052023.post401.dist-info → mct_nightly-1.8.0.20230610.post356.dist-info}/METADATA +10 -7
  2. {mct_nightly-1.8.0.20052023.post401.dist-info → mct_nightly-1.8.0.20230610.post356.dist-info}/RECORD +68 -115
  3. model_compression_toolkit/__init__.py +23 -3
  4. model_compression_toolkit/core/common/framework_info.py +1 -1
  5. model_compression_toolkit/core/keras/back2framework/instance_builder.py +16 -9
  6. model_compression_toolkit/core/keras/back2framework/keras_model_builder.py +8 -34
  7. model_compression_toolkit/core/pytorch/back2framework/instance_builder.py +5 -1
  8. model_compression_toolkit/core/pytorch/back2framework/pytorch_model_builder.py +103 -28
  9. model_compression_toolkit/exporter/model_exporter/keras/fakely_quant_keras_exporter.py +39 -44
  10. model_compression_toolkit/exporter/model_exporter/keras/fakely_quant_tflite_exporter.py +1 -1
  11. model_compression_toolkit/exporter/model_exporter/keras/int8_tflite_exporter.py +20 -18
  12. model_compression_toolkit/exporter/model_exporter/pytorch/fakely_quant_onnx_pytorch_exporter.py +3 -3
  13. model_compression_toolkit/exporter/model_exporter/pytorch/fakely_quant_torchscript_pytorch_exporter.py +1 -1
  14. model_compression_toolkit/exporter/model_wrapper/keras/builder/fully_quantized_model_builder.py +36 -9
  15. model_compression_toolkit/exporter/model_wrapper/keras/builder/node_to_quantizer.py +4 -4
  16. model_compression_toolkit/exporter/model_wrapper/keras/validate_layer.py +24 -32
  17. model_compression_toolkit/exporter/model_wrapper/pytorch/builder/fully_quantized_model_builder.py +31 -8
  18. model_compression_toolkit/exporter/model_wrapper/pytorch/builder/node_to_quantizer.py +5 -5
  19. model_compression_toolkit/exporter/model_wrapper/pytorch/validate_layer.py +34 -8
  20. model_compression_toolkit/gptq/keras/gptq_training.py +15 -16
  21. model_compression_toolkit/gptq/keras/graph_info.py +2 -2
  22. model_compression_toolkit/gptq/keras/quantizer/base_keras_gptq_quantizer.py +4 -5
  23. model_compression_toolkit/gptq/keras/quantizer/quantization_builder.py +5 -7
  24. model_compression_toolkit/gptq/keras/quantizer/soft_rounding/soft_quantizer_reg.py +1 -1
  25. model_compression_toolkit/gptq/keras/quantizer/soft_rounding/symmetric_soft_quantizer.py +6 -6
  26. model_compression_toolkit/gptq/keras/quantizer/soft_rounding/uniform_soft_quantizer.py +7 -7
  27. model_compression_toolkit/gptq/keras/quantizer/ste_rounding/symmetric_ste.py +6 -6
  28. model_compression_toolkit/gptq/pytorch/gptq_training.py +30 -10
  29. model_compression_toolkit/gptq/pytorch/graph_info.py +5 -2
  30. model_compression_toolkit/gptq/pytorch/quantization_facade.py +4 -2
  31. model_compression_toolkit/gptq/pytorch/quantizer/base_pytorch_gptq_quantizer.py +4 -4
  32. model_compression_toolkit/gptq/pytorch/quantizer/quantization_builder.py +5 -7
  33. model_compression_toolkit/gptq/pytorch/quantizer/soft_rounding/soft_quantizer_reg.py +1 -1
  34. model_compression_toolkit/gptq/pytorch/quantizer/soft_rounding/symmetric_soft_quantizer.py +7 -7
  35. model_compression_toolkit/gptq/pytorch/quantizer/soft_rounding/uniform_soft_quantizer.py +7 -8
  36. model_compression_toolkit/gptq/pytorch/quantizer/ste_rounding/symmetric_ste.py +7 -8
  37. model_compression_toolkit/qat/common/__init__.py +2 -1
  38. model_compression_toolkit/qat/common/qat_config.py +2 -2
  39. model_compression_toolkit/qat/keras/quantization_facade.py +18 -8
  40. model_compression_toolkit/qat/keras/quantizer/base_keras_qat_quantizer.py +1 -1
  41. model_compression_toolkit/qat/keras/quantizer/quantization_builder.py +11 -11
  42. model_compression_toolkit/qat/keras/quantizer/ste_rounding/symmetric_ste.py +11 -12
  43. model_compression_toolkit/qat/keras/quantizer/ste_rounding/uniform_ste.py +12 -13
  44. model_compression_toolkit/qat/pytorch/quantization_facade.py +27 -16
  45. model_compression_toolkit/qat/pytorch/quantizer/base_pytorch_qat_quantizer.py +2 -2
  46. model_compression_toolkit/qat/pytorch/quantizer/quantization_builder.py +31 -4
  47. model_compression_toolkit/qat/pytorch/quantizer/ste_rounding/symmetric_ste.py +10 -9
  48. model_compression_toolkit/qat/pytorch/quantizer/ste_rounding/uniform_ste.py +11 -10
  49. model_compression_toolkit/target_platform_capabilities/target_platform/__init__.py +2 -1
  50. model_compression_toolkit/target_platform_capabilities/target_platform/op_quantization_config.py +1 -25
  51. model_compression_toolkit/{quantizers_infrastructure/inferable_infrastructure/keras/quantizers/constants.py → trainable_infrastructure/__init__.py} +3 -10
  52. model_compression_toolkit/{quantizers_infrastructure/trainable_infrastructure → trainable_infrastructure}/common/base_trainable_quantizer.py +3 -3
  53. model_compression_toolkit/{quantizers_infrastructure/trainable_infrastructure → trainable_infrastructure}/common/get_quantizer_config.py +1 -1
  54. model_compression_toolkit/{quantizers_infrastructure/trainable_infrastructure → trainable_infrastructure}/common/get_quantizers.py +3 -3
  55. model_compression_toolkit/{quantizers_infrastructure/trainable_infrastructure → trainable_infrastructure}/keras/base_keras_quantizer.py +4 -4
  56. model_compression_toolkit/{quantizers_infrastructure/trainable_infrastructure → trainable_infrastructure}/keras/config_serialization.py +2 -2
  57. model_compression_toolkit/{quantizers_infrastructure/inferable_infrastructure → trainable_infrastructure}/keras/load_model.py +16 -23
  58. model_compression_toolkit/{quantizers_infrastructure/trainable_infrastructure → trainable_infrastructure}/pytorch/base_pytorch_quantizer.py +3 -3
  59. model_compression_toolkit/quantizers_infrastructure/__init__.py +0 -23
  60. model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/common/base_inferable_quantizer.py +0 -87
  61. model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/common/constants.py +0 -46
  62. model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/common/get_all_subclasses.py +0 -31
  63. model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/common/get_quantizers.py +0 -53
  64. model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/common/quant_utils.py +0 -49
  65. model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/keras/activation_quantization_holder.py +0 -147
  66. model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/keras/quantize_wrapper.py +0 -345
  67. model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/keras/quantizer_utils.py +0 -85
  68. model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/keras/quantizers/__init__.py +0 -27
  69. model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/keras/quantizers/activation_inferable_quantizers/__init__.py +0 -14
  70. model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/keras/quantizers/activation_inferable_quantizers/activation_lut_pot_inferable_quantizer.py +0 -148
  71. model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/keras/quantizers/activation_inferable_quantizers/activation_pot_inferable_quantizer.py +0 -65
  72. model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/keras/quantizers/activation_inferable_quantizers/activation_symmetric_inferable_quantizer.py +0 -86
  73. model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/keras/quantizers/activation_inferable_quantizers/activation_uniform_inferable_quantizer.py +0 -111
  74. model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/keras/quantizers/base_keras_inferable_quantizer.py +0 -56
  75. model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/keras/quantizers/weights_inferable_quantizers/__init__.py +0 -14
  76. model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/keras/quantizers/weights_inferable_quantizers/weights_lut_pot_inferable_quantizer.py +0 -79
  77. model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/keras/quantizers/weights_inferable_quantizers/weights_lut_symmetric_inferable_quantizer.py +0 -179
  78. model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/keras/quantizers/weights_inferable_quantizers/weights_pot_inferable_quantizer.py +0 -67
  79. model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/keras/quantizers/weights_inferable_quantizers/weights_symmetric_inferable_quantizer.py +0 -87
  80. model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/keras/quantizers/weights_inferable_quantizers/weights_uniform_inferable_quantizer.py +0 -163
  81. model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/keras/validation_functions.py +0 -66
  82. model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/pytorch/__init__.py +0 -14
  83. model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/pytorch/quantize_wrapper.py +0 -269
  84. model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/pytorch/quantizer_utils.py +0 -152
  85. model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/pytorch/quantizers/__init__.py +0 -35
  86. model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/pytorch/quantizers/activation_inferable_quantizers/__init__.py +0 -14
  87. model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/pytorch/quantizers/activation_inferable_quantizers/activation_lut_pot_inferable_quantizer.py +0 -96
  88. model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/pytorch/quantizers/activation_inferable_quantizers/activation_pot_inferable_quantizer.py +0 -62
  89. model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/pytorch/quantizers/activation_inferable_quantizers/activation_symmetric_inferable_quantizer.py +0 -83
  90. model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/pytorch/quantizers/activation_inferable_quantizers/activation_uniform_inferable_quantizer.py +0 -100
  91. model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/pytorch/quantizers/base_lut_symmetric_inferable_quantizer.py +0 -95
  92. model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/pytorch/quantizers/base_pytorch_inferable_quantizer.py +0 -48
  93. model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/pytorch/quantizers/base_symmetric_inferable_quantizer.py +0 -70
  94. model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/pytorch/quantizers/base_uniform_inferable_quantizer.py +0 -57
  95. model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/pytorch/quantizers/constants.py +0 -26
  96. model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/pytorch/quantizers/weights_inferable_quantizers/__init__.py +0 -14
  97. model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/pytorch/quantizers/weights_inferable_quantizers/weights_lut_pot_inferable_quantizer.py +0 -77
  98. model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/pytorch/quantizers/weights_inferable_quantizers/weights_lut_symmetric_inferable_quantizer.py +0 -106
  99. model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/pytorch/quantizers/weights_inferable_quantizers/weights_pot_inferable_quantizer.py +0 -66
  100. model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/pytorch/quantizers/weights_inferable_quantizers/weights_symmetric_inferable_quantizer.py +0 -104
  101. model_compression_toolkit/quantizers_infrastructure/inferable_infrastructure/pytorch/quantizers/weights_inferable_quantizers/weights_uniform_inferable_quantizer.py +0 -109
  102. model_compression_toolkit/quantizers_infrastructure/trainable_infrastructure/__init__.py +0 -14
  103. model_compression_toolkit/quantizers_infrastructure/trainable_infrastructure/common/__init__.py +0 -14
  104. model_compression_toolkit/quantizers_infrastructure/trainable_infrastructure/keras/__init__.py +0 -14
  105. model_compression_toolkit/quantizers_infrastructure/trainable_infrastructure/pytorch/__init__.py +0 -14
  106. {mct_nightly-1.8.0.20052023.post401.dist-info → mct_nightly-1.8.0.20230610.post356.dist-info}/LICENSE.md +0 -0
  107. {mct_nightly-1.8.0.20052023.post401.dist-info → mct_nightly-1.8.0.20230610.post356.dist-info}/WHEEL +0 -0
  108. {mct_nightly-1.8.0.20052023.post401.dist-info → mct_nightly-1.8.0.20230610.post356.dist-info}/top_level.txt +0 -0
  109. /model_compression_toolkit/{quantizers_infrastructure/inferable_infrastructure → trainable_infrastructure/common}/__init__.py +0 -0
  110. /model_compression_toolkit/{quantizers_infrastructure → trainable_infrastructure/common}/constants.py +0 -0
  111. /model_compression_toolkit/{quantizers_infrastructure/trainable_infrastructure → trainable_infrastructure}/common/quant_utils.py +0 -0
  112. /model_compression_toolkit/{quantizers_infrastructure/trainable_infrastructure → trainable_infrastructure}/common/trainable_quantizer_config.py +0 -0
  113. /model_compression_toolkit/{quantizers_infrastructure/inferable_infrastructure/common → trainable_infrastructure/keras}/__init__.py +0 -0
  114. /model_compression_toolkit/{quantizers_infrastructure/trainable_infrastructure → trainable_infrastructure}/keras/quantizer_utils.py +0 -0
  115. /model_compression_toolkit/{quantizers_infrastructure/inferable_infrastructure/keras → trainable_infrastructure/pytorch}/__init__.py +0 -0
@@ -1,147 +0,0 @@
1
- # Copyright 2023 Sony Semiconductor Israel, Inc. All rights reserved.
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
- # ==============================================================================
15
-
16
- from model_compression_toolkit.constants import FOUND_TF
17
- from model_compression_toolkit.logger import Logger
18
- from model_compression_toolkit.quantizers_infrastructure import BaseInferableQuantizer, BaseKerasTrainableQuantizer
19
- from model_compression_toolkit.quantizers_infrastructure.inferable_infrastructure.common.constants import \
20
- ACTIVATION_HOLDER_QUANTIZER, TRAINING, STEPS
21
-
22
- if FOUND_TF:
23
- import tensorflow as tf
24
- from keras.utils import tf_inspect
25
- from tensorflow_model_optimization.python.core.keras import utils
26
-
27
- def _make_quantizer_fn(quantizer, x, training):
28
- """Use currying to return True/False specialized fns to the cond."""
29
-
30
- def quantizer_fn():
31
- return quantizer(x, training)
32
-
33
- return quantizer_fn
34
-
35
- keras = tf.keras
36
-
37
- class ActivationQuantizationHolder(keras.layers.Layer):
38
- """
39
- Keras layer to hold an activation quantizer and quantize during inference.
40
- """
41
- def __init__(self,
42
- activation_holder_quantizer: BaseInferableQuantizer,
43
- **kwargs):
44
- """
45
-
46
- Args:
47
- activation_holder_quantizer: Quantizer to use during inference.
48
- **kwargs: Key-word arguments for the base layer
49
- """
50
-
51
- super(ActivationQuantizationHolder, self).__init__(**kwargs)
52
- self.activation_holder_quantizer = activation_holder_quantizer
53
-
54
- def get_config(self):
55
- """
56
- Returns: Configuration of ActivationQuantizationHolder.
57
-
58
- """
59
- base_config = super(ActivationQuantizationHolder, self).get_config()
60
- config = {
61
- ACTIVATION_HOLDER_QUANTIZER: keras.utils.serialize_keras_object(self.activation_holder_quantizer)}
62
-
63
- return dict(list(base_config.items()) + list(config.items()))
64
-
65
- @classmethod
66
- def from_config(cls, config):
67
- """
68
-
69
- Args:
70
- config(dict): dictionary of ActivationQuantizationHolder Configuration
71
-
72
- Returns: A ActivationQuantizationHolder object
73
-
74
- """
75
- config = config.copy()
76
- activation_holder_quantizer = keras.utils.deserialize_keras_object(config.pop(ACTIVATION_HOLDER_QUANTIZER),
77
- module_objects=globals(),
78
- custom_objects=None)
79
-
80
- return cls(activation_holder_quantizer=activation_holder_quantizer,
81
- **config)
82
-
83
- def build(self, input_shape):
84
- """
85
- ActivationQuantizationHolder build function.
86
- Args:
87
- input_shape: the layer input shape
88
-
89
- Returns: None
90
-
91
- """
92
- super(ActivationQuantizationHolder, self).build(input_shape)
93
-
94
- self.optimizer_step = self.add_weight(
95
- STEPS,
96
- initializer=tf.keras.initializers.Constant(-1),
97
- dtype=tf.dtypes.int32,
98
- trainable=False)
99
-
100
- self.activation_holder_quantizer.initialize_quantization(None,
101
- self.name + '/out_',
102
- self)
103
-
104
- def call(self,
105
- inputs: tf.Tensor,
106
- training=None) -> tf.Tensor:
107
- """
108
- Quantizes the input tensor using the activation quantizer the ActivationQuantizationHolder holds.
109
-
110
- Args:
111
- inputs: Input tensors to quantize use the activation quantizer the object holds
112
- training: a boolean stating if layer is in training mode.
113
-
114
- Returns: Output of the activation quantizer (quantized input tensor).
115
-
116
- """
117
- if training is None:
118
- training = tf.keras.backend.learning_phase()
119
-
120
- activation_quantizer_args_spec = tf_inspect.getfullargspec(self.activation_holder_quantizer.__call__).args
121
- if TRAINING in activation_quantizer_args_spec:
122
- return utils.smart_cond(
123
- training,
124
- _make_quantizer_fn(self.activation_holder_quantizer, inputs, True),
125
- _make_quantizer_fn(self.activation_holder_quantizer, inputs, False))
126
-
127
- return self.activation_holder_quantizer(inputs)
128
-
129
- def convert_to_inferable_quantizers(self):
130
- """
131
- Convert layer's quantizer to inferable quantizer.
132
-
133
- Returns:
134
- None
135
- """
136
- if isinstance(self.activation_holder_quantizer, BaseKerasTrainableQuantizer):
137
- self.activation_holder_quantizer = self.activation_holder_quantizer.convert2inferable()
138
-
139
-
140
-
141
-
142
- else:
143
- class ActivationQuantizationHolder: # pragma: no cover
144
- def __init__(self, *args, **kwargs):
145
- Logger.error('Installing tensorflow and tensorflow_model_optimization is mandatory '
146
- 'when using ActivationQuantizationHolder. '
147
- 'Could not find Tensorflow package.')
@@ -1,345 +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
- from typing import Dict, List, Any, Tuple
16
- from model_compression_toolkit import quantizers_infrastructure as qi
17
- from model_compression_toolkit.constants import FOUND_TF
18
- from model_compression_toolkit.logger import Logger
19
- from model_compression_toolkit.quantizers_infrastructure import BaseInferableQuantizer
20
- from model_compression_toolkit.quantizers_infrastructure.inferable_infrastructure.common.constants import WEIGHTS_QUANTIZERS, ACTIVATION_QUANTIZERS, LAYER, STEPS, TRAINING
21
-
22
- if FOUND_TF:
23
- import tensorflow as tf
24
- from tensorflow.python.util import tf_inspect
25
- from tensorflow_model_optimization.python.core.keras import utils
26
-
27
- keras = tf.keras
28
-
29
- def _make_quantizer_fn(quantizer, x, training):
30
- """Use currying to return True/False specialized fns to the cond."""
31
-
32
- def quantizer_fn():
33
- return quantizer(x, training)
34
-
35
- return quantizer_fn
36
-
37
-
38
- def _weight_name(name: str) -> str:
39
- """Extracts the weight name from the full TensorFlow variable name.
40
-
41
- For example, returns 'kernel' for 'dense_2/kernel:0'.
42
-
43
- Args:
44
- name: TensorFlow variable name.
45
-
46
- Returns:
47
- Extracted weight name.
48
- """
49
- return name.split(':')[0].split('/')[-1]
50
-
51
-
52
- class KerasQuantizationWrapper(tf.keras.layers.Wrapper):
53
- def __init__(self,
54
- layer,
55
- weights_quantizers: Dict[str, BaseInferableQuantizer] = None,
56
- activation_quantizers: List[BaseInferableQuantizer] = None,
57
- **kwargs):
58
- """
59
- Keras Quantization Wrapper takes a keras layer and quantizers and infer a quantized layer.
60
-
61
- Args:
62
- layer: A keras layer.
63
- weights_quantizers: A dictionary between a weight's name to its quantizer.
64
- activation_quantizers: A list of activations quantization, one for each layer output.
65
- """
66
- super(KerasQuantizationWrapper, self).__init__(layer, **kwargs)
67
- self._track_trackable(layer, name='layer')
68
- self.weights_quantizers = weights_quantizers if weights_quantizers is not None else dict()
69
- self.activation_quantizers = activation_quantizers if activation_quantizers is not None else list()
70
-
71
- def add_weights_quantizer(self, param_name: str, quantizer: BaseInferableQuantizer):
72
- """
73
- This function adds a weights quantizer to existing wrapper
74
-
75
- Args:
76
- param_name: The name of the parameter to quantize
77
- quantizer: A quantizer.
78
-
79
- Returns: None
80
-
81
- """
82
- self.weights_quantizers.update({param_name: quantizer})
83
-
84
- @property
85
- def is_activation_quantization(self) -> bool:
86
- """
87
- This function check activation quantizer exists in wrapper.
88
- Returns: a boolean if activation quantizer exists
89
-
90
- """
91
- return self.num_activation_quantizers > 0
92
-
93
- @property
94
- def is_weights_quantization(self) -> bool:
95
- """
96
- This function check weights quantizer exists in wrapper.
97
-
98
- Returns: a boolean if weights quantizer exists
99
-
100
- """
101
- return self.num_weights_quantizers > 0
102
-
103
- @property
104
- def num_weights_quantizers(self) -> int:
105
- """
106
- Returns: number of weights quantizers
107
- """
108
- return len(self.weights_quantizers)
109
-
110
- @property
111
- def num_activation_quantizers(self) -> int:
112
- """
113
- Returns: number of activations quantizers
114
- """
115
- return len(self.activation_quantizers)
116
-
117
- def get_config(self):
118
- """
119
- Returns: Configuration of KerasQuantizationWrapper.
120
-
121
- """
122
- base_config = super(KerasQuantizationWrapper, self).get_config()
123
- config = {
124
- ACTIVATION_QUANTIZERS: [keras.utils.serialize_keras_object(act) for act in self.activation_quantizers],
125
- WEIGHTS_QUANTIZERS: {k: keras.utils.serialize_keras_object(v) for k, v in self.weights_quantizers.items()}}
126
- return dict(list(base_config.items()) + list(config.items()))
127
-
128
- def _set_weights_vars(self, is_training: bool = True):
129
- """
130
- This function sets weights quantizers vars to the layer
131
-
132
- Args:
133
- is_training: Flag to indicate whether training or not
134
-
135
- Returns: None
136
- """
137
- self._weights_vars = []
138
- for name, quantizer in self.weights_quantizers.items():
139
- weight = getattr(self.layer, name)
140
- quantizer.initialize_quantization(weight.shape, _weight_name(weight.name) if is_training else None,
141
- self)
142
- self._weights_vars.append((name, weight, quantizer))
143
- self._trainable_weights.append(weight) # Must when inherit from tf.keras.layers.Wrapper in tf2.10 and below
144
-
145
- def _set_activations_vars(self):
146
- """
147
- This function sets activations quantizers vars to the layer
148
-
149
- Returns: None
150
- """
151
- self._activation_vars = []
152
- for i, quantizer in enumerate(self.activation_quantizers):
153
- quantizer.initialize_quantization(None, self.layer.name + f'/out{i}', self)
154
- self._activation_vars.append(quantizer)
155
-
156
- @classmethod
157
- def from_config(cls, config):
158
- """
159
-
160
- Args:
161
- config(dict): dictionary of KerasQuantizationWrapper Configuration
162
-
163
- Returns: A KerasQuantizationWrapper
164
-
165
- """
166
- config = config.copy()
167
- activation_quantizers = [keras.utils.deserialize_keras_object(act,
168
- module_objects=globals(),
169
- custom_objects=None) for act in
170
- config.pop(ACTIVATION_QUANTIZERS)]
171
- weights_quantizers = {k: keras.utils.deserialize_keras_object(v,
172
- module_objects=globals(),
173
- custom_objects=None) for k, v in
174
- config.pop(WEIGHTS_QUANTIZERS).items()}
175
- layer = tf.keras.layers.deserialize(config.pop(LAYER))
176
- return cls(layer=layer, weights_quantizers=weights_quantizers, activation_quantizers=activation_quantizers, **config)
177
-
178
- def build(self, input_shape):
179
- """
180
- KerasQuantization Wrapper build function.
181
- Args:
182
- input_shape: the layer input shape
183
-
184
- Returns: None
185
-
186
- """
187
- super(KerasQuantizationWrapper, self).build(input_shape)
188
-
189
- self.optimizer_step = self.add_weight(
190
- STEPS,
191
- initializer=tf.keras.initializers.Constant(-1),
192
- dtype=tf.dtypes.int32,
193
- trainable=False)
194
-
195
- self._set_weights_vars()
196
- self._set_activations_vars()
197
-
198
- def set_quantize_weights(self, quantized_weights: dict):
199
- """
200
- This function update layer weights after quantization.
201
-
202
- Args:
203
- quantized_weights: a dict of weight to update
204
-
205
- Returns: None
206
-
207
- """
208
- for weight_attr in self.weights_quantizers.keys():
209
- weight = quantized_weights.get(weight_attr)
210
- current_weight = getattr(self.layer, weight_attr)
211
- if current_weight.shape != weight.shape:
212
- Logger.error(
213
- f"Existing layer weight shape {current_weight.shape} is incompatible with provided weight "
214
- f"shape {weight.shape}") # pragma: no cover
215
-
216
- setattr(self.layer, weight_attr, weight)
217
-
218
- def call(self, inputs, training=None, **kwargs):
219
- """
220
- KerasQuantizationWrapper call functions
221
- Args:
222
- inputs: Input tensors to specified layer
223
- training: a boolean stating if layer is in training mode.
224
- **kwargs:
225
-
226
- Returns: tensors that simulate a quantized layer.
227
-
228
- """
229
- if training is None:
230
- training = tf.keras.backend.learning_phase()
231
-
232
- # Quantize all weights, and replace them in the underlying layer.
233
- quantized_weights = {}
234
- for name, unquantized_weight, quantizer in self._weights_vars:
235
-
236
- weights_quantizer_args_spec = tf_inspect.getfullargspec(quantizer.__call__).args
237
- if TRAINING in weights_quantizer_args_spec:
238
- quantized_weight = utils.smart_cond(
239
- training,
240
- _make_quantizer_fn(quantizer, unquantized_weight, True),
241
- _make_quantizer_fn(quantizer, unquantized_weight, False))
242
- quantized_weights.update({name: quantized_weight})
243
- else:
244
- # Keras weights inferable quantizer
245
- quantized_weight = quantizer(unquantized_weight)
246
- quantized_weights.update({name: quantized_weight})
247
-
248
- self.set_quantize_weights(quantized_weights)
249
-
250
- args_spec = tf_inspect.getfullargspec(self.layer.call).args
251
- if TRAINING in args_spec:
252
- outputs = self.layer.call(inputs, training=training, **kwargs)
253
- else:
254
- outputs = self.layer.call(inputs, **kwargs)
255
-
256
- # Quantize all activations if quantizers exist.
257
- if self.is_activation_quantization:
258
- num_outputs = len(outputs) if isinstance(outputs, (list, tuple)) else 1
259
- if self.num_activation_quantizers != num_outputs:
260
- Logger.error('Quantization wrapper output quantization error: '
261
- f'number of outputs and quantizers mismatch ({num_outputs}!='
262
- f'{self.num_activation_quantizers}')
263
- if num_outputs == 1:
264
- outputs = [outputs]
265
-
266
- _outputs = []
267
- for _output, act_quant in zip(outputs, self.activation_quantizers):
268
- activation_quantizer_args_spec = tf_inspect.getfullargspec(act_quant.__call__).args
269
- if TRAINING in activation_quantizer_args_spec:
270
- _outputs.append(utils.smart_cond(
271
- training,
272
- _make_quantizer_fn(act_quant, _output, True),
273
- _make_quantizer_fn(act_quant, _output, False)))
274
- else:
275
- # Keras activation inferable quantizer.
276
- _outputs.append(act_quant(_output))
277
- outputs = _outputs[0] if num_outputs == 1 else _outputs
278
-
279
- return outputs
280
-
281
- def convert_to_inferable_quantizers(self):
282
- """
283
- Convert layer's quantizers to inferable.
284
-
285
- Returns:
286
- None
287
- """
288
- # Activations quantizers
289
- inferable_activation_quantizers = []
290
- if self.is_activation_quantization:
291
- for quantizer in self.activation_quantizers:
292
- if isinstance(quantizer, qi.BaseKerasTrainableQuantizer):
293
- inferable_activation_quantizers.append(quantizer.convert2inferable())
294
- self.activation_quantizers = inferable_activation_quantizers
295
- self._set_activations_vars()
296
-
297
- # Weight quantizers
298
- inferable_weight_quantizers = {}
299
- if self.is_weights_quantization:
300
- for name, quantizer in self.weights_quantizers.items():
301
- if isinstance(quantizer, qi.BaseKerasTrainableQuantizer):
302
- inferable_weight_quantizers.update({name: quantizer.convert2inferable()})
303
- self.weights_quantizers = inferable_weight_quantizers
304
- self._set_weights_vars(False)
305
-
306
- def get_weights_vars(self) -> List[Tuple[str, Any, BaseInferableQuantizer]]:
307
- """
308
- A getter of the layer's weights variables.
309
-
310
- Returns:
311
- List pf tuples of the wrapped layer's weights variables with weight name, values and assigned quantizer.
312
-
313
- """
314
-
315
- return self._weights_vars
316
-
317
- def get_quantized_weights(self) -> Dict[str, tf.Tensor]:
318
- """
319
-
320
- Returns: A dictionary of weights attributes to quantized weights.
321
-
322
- """
323
- quantized_weights = {}
324
- weights_var = self.get_weights_vars()
325
- for name, w, quantizer in weights_var:
326
- quantized_weights[name] = quantizer(w)
327
- return quantized_weights
328
-
329
- else:
330
- class KerasQuantizationWrapper(object):
331
- def __init__(self,
332
- layer,
333
- weights_quantizers: Dict[str, BaseInferableQuantizer] = None,
334
- activation_quantizers: List[BaseInferableQuantizer] = None):
335
- """
336
- Keras Quantization Wrapper takes a keras layer and quantizers and infer a quantized layer.
337
-
338
- Args:
339
- layer: A keras layer.
340
- weights_quantizers: A dictionary between a weight's name to its quantizer.
341
- activation_quantizers: A list of activations quantization, one for each layer output.
342
- """
343
- Logger.critical('Installing tensorflow and tensorflow_model_optimization is mandatory '
344
- 'when using KerasQuantizationWrapper. '
345
- 'Could not find Tensorflow package.') # pragma: no cover
@@ -1,85 +0,0 @@
1
- # Copyright 2023 Sony Semiconductor Israel, Inc. All rights reserved.
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
- # ==============================================================================
15
-
16
- import numpy as np
17
- import tensorflow as tf
18
-
19
-
20
- def lut_quantizer(tensor_data: tf.Tensor,
21
- cluster_centers: np.ndarray,
22
- signed: bool,
23
- threshold: np.ndarray,
24
- multiplier_n_bits: int,
25
- eps: float) -> tf.Tensor:
26
- """
27
- Quantize a tensor using a non-uniform quantization based on the pre-defined clusters.
28
- 1. Scales tensor_data with the threshold into multiplier_n_bits quantization range.
29
- 2. Assigns cluster centers to each value.
30
- 3. Scales back by multiplying the result by threshold and dividing with the quantization range max value.
31
- The result is the quantized tensor.
32
-
33
- Args:
34
- tensor_data: Input activation tensor.
35
- cluster_centers: the cluster centers to assign the tensor values.
36
- signed: Whether the quantization is signed or not.
37
- threshold: threshold for quantization.
38
- multiplier_n_bits: Number of bits that determines the quantization range
39
- eps: Small value for numerical stability in division.
40
-
41
- Returns: Quantized tensor.
42
- """
43
-
44
- tensor = int_quantization_with_threshold(tensor_data, n_bits=multiplier_n_bits, signed=signed, threshold=threshold,
45
- eps=eps)
46
- tensor = tf.expand_dims(tensor, -1)
47
-
48
- expanded_cluster_centers = cluster_centers.reshape([*[1 for _ in range(len(tensor.shape) - 1)], -1])
49
- cluster_assignments = tf.argmin(tf.abs(tensor - expanded_cluster_centers), axis=-1)
50
- centers = tf.gather(cluster_centers.flatten(), cluster_assignments)
51
-
52
- quant_tensor = (centers / (2 ** (multiplier_n_bits - int(signed)))) * threshold
53
-
54
- return quant_tensor
55
-
56
-
57
- def int_quantization_with_threshold(data: tf.Tensor,
58
- n_bits: int,
59
- signed: bool,
60
- threshold: np.ndarray,
61
- eps: float) -> tf.Tensor:
62
- """
63
- Divides data by threshold and quantize it to integers in the quantization range (depends on signed value).
64
-
65
- Args:
66
- data: tensor data.
67
- n_bits: number of bits that determines the quantization range.
68
- signed: Whether the quantization is signed or not.
69
- threshold: threshold for quantization.
70
- eps: Small value for numerical stability in division.
71
-
72
- Returns:
73
- Uniform Quantized tensor.
74
-
75
- """
76
-
77
- if signed:
78
- clip_max = 2 ** (n_bits - 1) - 1
79
- clip_min = -2 ** (n_bits - 1)
80
- else:
81
- clip_max = 2 ** n_bits - 1
82
- clip_min = 0
83
-
84
- return tf.clip_by_value((data / (threshold + eps)) * (2 ** (n_bits - int(signed))),
85
- clip_value_max=clip_max, clip_value_min=clip_min)
@@ -1,27 +0,0 @@
1
- # Copyright 2023 Sony Semiconductor Israel, Inc. All rights reserved.
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
- # ==============================================================================
15
- from model_compression_toolkit.quantizers_infrastructure.inferable_infrastructure.keras.quantizers.base_keras_inferable_quantizer import \
16
- BaseKerasInferableQuantizer
17
-
18
- from model_compression_toolkit.quantizers_infrastructure.inferable_infrastructure.keras.quantizers.weights_inferable_quantizers.weights_pot_inferable_quantizer import WeightsPOTInferableQuantizer
19
- from model_compression_toolkit.quantizers_infrastructure.inferable_infrastructure.keras.quantizers.weights_inferable_quantizers.weights_symmetric_inferable_quantizer import WeightsSymmetricInferableQuantizer
20
- from model_compression_toolkit.quantizers_infrastructure.inferable_infrastructure.keras.quantizers.weights_inferable_quantizers.weights_uniform_inferable_quantizer import WeightsUniformInferableQuantizer
21
- from model_compression_toolkit.quantizers_infrastructure.inferable_infrastructure.keras.quantizers.weights_inferable_quantizers.weights_lut_symmetric_inferable_quantizer import WeightsLUTSymmetricInferableQuantizer
22
- from model_compression_toolkit.quantizers_infrastructure.inferable_infrastructure.keras.quantizers.weights_inferable_quantizers.weights_lut_pot_inferable_quantizer import WeightsLUTPOTInferableQuantizer
23
-
24
- from model_compression_toolkit.quantizers_infrastructure.inferable_infrastructure.keras.quantizers.activation_inferable_quantizers.activation_pot_inferable_quantizer import ActivationPOTInferableQuantizer
25
- from model_compression_toolkit.quantizers_infrastructure.inferable_infrastructure.keras.quantizers.activation_inferable_quantizers.activation_symmetric_inferable_quantizer import ActivationSymmetricInferableQuantizer
26
- from model_compression_toolkit.quantizers_infrastructure.inferable_infrastructure.keras.quantizers.activation_inferable_quantizers.activation_uniform_inferable_quantizer import ActivationUniformInferableQuantizer
27
- from model_compression_toolkit.quantizers_infrastructure.inferable_infrastructure.keras.quantizers.activation_inferable_quantizers.activation_lut_pot_inferable_quantizer import ActivationLutPOTInferableQuantizer
@@ -1,14 +0,0 @@
1
- # Copyright 2023 Sony Semiconductor Israel, Inc. All rights reserved.
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
- # ==============================================================================