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,148 +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
- import warnings
16
- from typing import List
17
-
18
- import numpy as np
19
-
20
- from model_compression_toolkit.logger import Logger
21
- from model_compression_toolkit.constants import FOUND_TF
22
-
23
- from model_compression_toolkit.target_platform_capabilities.target_platform import QuantizationMethod
24
- from model_compression_toolkit.quantizers_infrastructure import QuantizationTarget
25
- from model_compression_toolkit.quantizers_infrastructure.inferable_infrastructure.common.base_inferable_quantizer import mark_quantizer
26
- from model_compression_toolkit.quantizers_infrastructure.inferable_infrastructure.common.constants import MULTIPLIER_N_BITS, EPS
27
-
28
- if FOUND_TF:
29
- import tensorflow as tf
30
- from model_compression_toolkit.quantizers_infrastructure.inferable_infrastructure.keras.quantizers.base_keras_inferable_quantizer \
31
- import BaseKerasInferableQuantizer
32
- from model_compression_toolkit.quantizers_infrastructure.inferable_infrastructure.keras.quantizer_utils import lut_quantizer
33
-
34
-
35
- @mark_quantizer(quantization_target=QuantizationTarget.Activation,
36
- quantization_method=[QuantizationMethod.LUT_POT_QUANTIZER],
37
- quantizer_type=None)
38
- class ActivationLutPOTInferableQuantizer(BaseKerasInferableQuantizer):
39
- """
40
- Class for quantizing activations using lut power-of-two quantizer
41
- """
42
-
43
- def __init__(self,
44
- num_bits: int,
45
- cluster_centers: np.ndarray,
46
- threshold: List[float],
47
- signed: bool,
48
- multiplier_n_bits: int = MULTIPLIER_N_BITS,
49
- eps: float = EPS):
50
- """
51
- Initialize the quantizer with the specified parameters.
52
-
53
- Args:
54
- num_bits: number of bits to use for quantization
55
- cluster_centers: the cluster centers to assign the activations
56
- threshold: threshold for quantizing activations
57
- signed: whether or not to use signed quantization
58
- multiplier_n_bits: Number of bits that determines the quantization range
59
- eps: Small value for numerical stability in division
60
- """
61
- # Call the superclass constructor with the given parameters, along with the target of Activation
62
- # quantization
63
- super(ActivationLutPOTInferableQuantizer, self).__init__()
64
-
65
- assert isinstance(threshold, list), f'Expected threshold to be of type list but is {type(threshold)}'
66
- assert all([isinstance(x, (float, np.float32, tf.float32)) for x in
67
- threshold]), f'Expected threshold list to contain float or np.float values but found ' \
68
- f'{[type(x) for x in threshold]}'
69
-
70
- # In activation per-channel quantization is not supported thus we expect a single threshold value.
71
- assert len(threshold) == 1, f'In per-tensor quantization threshold should be of ' \
72
- f'length 1 but is {len(threshold)}'
73
-
74
- is_threshold_pot = np.all([int(np.log2(x)) == np.log2(x) for x in threshold])
75
- assert is_threshold_pot, f'Expected threshold to be power of 2 but is {threshold}'
76
-
77
- self.threshold = threshold[0]
78
-
79
- assert len(np.unique(cluster_centers)) <= 2 ** num_bits, \
80
- f'Expected num of cluster centers to be less or equal than {2 ** num_bits} ' \
81
- f'but got {len(cluster_centers)}'
82
-
83
- assert not np.any(cluster_centers - cluster_centers.astype(int)), f'Expected cluster centers to be integers'
84
-
85
- if signed:
86
- assert np.all((-1 * (2 ** (multiplier_n_bits - int(signed))) <= cluster_centers) &
87
- (cluster_centers <= (2 ** (multiplier_n_bits - int(signed)) - 1))), \
88
- f'Expected cluster centers in the quantization range'
89
- else:
90
- assert np.all(cluster_centers <= (2 ** multiplier_n_bits)), \
91
- f'Expected cluster centers in the quantization range'
92
-
93
- # num_bits must be less than multiplier_n_bits
94
- assert num_bits <= multiplier_n_bits, f'Look-Up-Table bit configuration has {num_bits} bits. It must be ' \
95
- f'less then {multiplier_n_bits}'
96
- if num_bits == multiplier_n_bits:
97
- warnings.warn("Num of bits equal to multiplier n bits, Please be aware LUT quantizier may be "
98
- "inefficient in that case, consider using SymmetricInferableQuantizer instead")
99
-
100
- # If unsigned activation quantization, all cluster_centers must have the same sign
101
- if not signed:
102
- assert np.all(cluster_centers >= 0), f'Expected unsigned cluster centers in unsigned activation ' \
103
- f'quantization '
104
-
105
- self.num_bits = num_bits
106
- self.cluster_centers = cluster_centers
107
- self.signed = signed
108
- self.multiplier_n_bits = multiplier_n_bits
109
- self.eps = eps
110
-
111
- def __call__(self, inputs: tf.Tensor) -> tf.Tensor:
112
- """
113
- Quantize the given inputs using the quantizer parameters.
114
-
115
- Args:
116
- inputs: input tensor to quantize
117
-
118
- Returns:
119
- quantized tensor.
120
- """
121
- assert inputs.dtype == tf.float32, f'Input tensor was expected to be a float tensor but is of type ' \
122
- f'{inputs.dtype}'
123
-
124
- return lut_quantizer(inputs, cluster_centers=self.cluster_centers, signed=self.signed,
125
- threshold=self.threshold, multiplier_n_bits=self.multiplier_n_bits, eps=self.eps)
126
-
127
- def get_config(self):
128
- """
129
- Return a dictionary with the configuration of the quantizer.
130
-
131
- Returns:
132
- Dictionary with the following keys: 'num_bits', 'cluster_centers', 'threshold', 'signed',
133
- 'multiplier_n_bits', 'eps'
134
- """
135
- return {'num_bits': self.num_bits,
136
- 'cluster_centers': self.cluster_centers,
137
- 'threshold': self.threshold,
138
- 'signed': self.signed,
139
- 'multiplier_n_bits': self.multiplier_n_bits,
140
- 'eps': self.eps}
141
-
142
-
143
- else:
144
- class ActivationLutPOTInferableQuantizer: # pragma: no cover
145
- def __init__(self, *args, **kwargs):
146
- Logger.error('Installing tensorflow and tensorflow_model_optimization is mandatory '
147
- 'when using ActivationLutPOTInferableQuantizer. '
148
- 'Could not find Tensorflow package.')
@@ -1,65 +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 typing import List
16
-
17
- import numpy as np
18
-
19
- from model_compression_toolkit.logger import Logger
20
- from model_compression_toolkit.constants import FOUND_TF
21
-
22
- from model_compression_toolkit.target_platform_capabilities.target_platform import QuantizationMethod
23
- from model_compression_toolkit.quantizers_infrastructure import QuantizationTarget
24
- from model_compression_toolkit.quantizers_infrastructure.inferable_infrastructure.common.base_inferable_quantizer import mark_quantizer
25
-
26
-
27
- if FOUND_TF:
28
- from model_compression_toolkit.quantizers_infrastructure.inferable_infrastructure.keras.quantizers.activation_inferable_quantizers.activation_symmetric_inferable_quantizer \
29
- import ActivationSymmetricInferableQuantizer
30
-
31
- @mark_quantizer(quantization_target=QuantizationTarget.Activation,
32
- quantization_method=[QuantizationMethod.POWER_OF_TWO],
33
- quantizer_type=None)
34
- class ActivationPOTInferableQuantizer(ActivationSymmetricInferableQuantizer):
35
- """
36
- Class for quantizing activations using power-of-two quantizer
37
- """
38
-
39
- def __init__(self,
40
- num_bits: int,
41
- threshold: List[float],
42
- signed: bool):
43
- """
44
- Initialize the quantizer with the specified parameters.
45
-
46
- Args:
47
- num_bits: number of bits to use for quantization
48
- threshold: threshold for quantizing activations
49
- signed: whether or not to use signed quantization
50
- """
51
- # Call the superclass constructor with the given parameters, along with the target of Activation
52
- # quantization
53
- super(ActivationPOTInferableQuantizer, self).__init__(num_bits=num_bits,
54
- threshold=threshold,
55
- signed=signed)
56
-
57
- is_threshold_pot = np.all([int(np.log2(x)) == np.log2(x) for x in self.threshold.flatten()])
58
- assert is_threshold_pot, f'Expected threshold to be power of 2 but is {self.threshold}'
59
-
60
- else:
61
- class ActivationPOTInferableQuantizer: # pragma: no cover
62
- def __init__(self, *args, **kwargs):
63
- Logger.error('Installing tensorflow and tensorflow_model_optimization is mandatory '
64
- 'when using ActivationPOTInferableQuantizer. '
65
- 'Could not find Tensorflow package.')
@@ -1,86 +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 typing import List
16
-
17
- import numpy as np
18
-
19
- from model_compression_toolkit.constants import FOUND_TF
20
-
21
- from model_compression_toolkit.target_platform_capabilities.target_platform import QuantizationMethod
22
- from model_compression_toolkit.quantizers_infrastructure.inferable_infrastructure.common.base_inferable_quantizer import mark_quantizer, \
23
- QuantizationTarget
24
-
25
-
26
- if FOUND_TF:
27
- import tensorflow as tf
28
- from model_compression_toolkit.quantizers_infrastructure.inferable_infrastructure.keras.quantizers.activation_inferable_quantizers.activation_uniform_inferable_quantizer \
29
- import ActivationUniformInferableQuantizer
30
-
31
- @mark_quantizer(quantization_target=QuantizationTarget.Activation,
32
- quantization_method=[QuantizationMethod.SYMMETRIC],
33
- quantizer_type=None)
34
- class ActivationSymmetricInferableQuantizer(ActivationUniformInferableQuantizer):
35
-
36
- """
37
- Class for quantizing activations using a symmetric quantizer
38
- """
39
-
40
- def __init__(self,
41
- num_bits: int,
42
- threshold: List[float],
43
- signed: bool):
44
- """
45
- Initialize the quantizer with the specified parameters.
46
-
47
- Args:
48
- num_bits: number of bits to use for quantization
49
- threshold: threshold for quantizing activations
50
- signed: whether or not to use signed quantization
51
- """
52
- assert isinstance(threshold, list), f'Expected threshold to be of type list but is {type(threshold)}'
53
- assert all([isinstance(x, (float, np.float32, tf.float32)) for x in
54
- threshold]), f'Expected threshold list to contain float or np.float values but found ' \
55
- f'{[type(x) for x in threshold]}'
56
-
57
- self.threshold = np.asarray(threshold)
58
- self.signed = signed
59
-
60
- delta = self.threshold / (2 ** (num_bits - int(self.signed)))
61
- # In activation quantization is per-tensor only - thus we pass the threshold as a list with a len of 1
62
- min_range = list(-self.threshold) if self.signed else [0.0]
63
- max_range = list(self.threshold - delta)
64
-
65
- super(ActivationSymmetricInferableQuantizer, self).__init__(num_bits=num_bits,
66
- min_range=min_range,
67
- max_range=max_range)
68
-
69
- def get_config(self):
70
- """
71
- Return a dictionary with the configuration of the quantizer.
72
-
73
- Returns:
74
- Dictionary with the following keys: 'num_bits', 'signed', 'threshold'
75
- """
76
- return {'num_bits': self.num_bits,
77
- 'signed': self.signed,
78
- 'threshold': self.threshold}
79
-
80
-
81
- else:
82
- class ActivationSymmetricInferableQuantizer: # pragma: no cover
83
- def __init__(self, *args, **kwargs):
84
- raise Exception('Installing tensorflow and tensorflow_model_optimization is mandatory '
85
- 'when using ActivationSymmetricInferableQuantizer. '
86
- 'Could not find Tensorflow package.')
@@ -1,111 +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 typing import List
16
-
17
- import numpy as np
18
-
19
- from model_compression_toolkit.logger import Logger
20
- from model_compression_toolkit.constants import FOUND_TF
21
- from model_compression_toolkit.target_platform_capabilities.target_platform import QuantizationMethod
22
- from model_compression_toolkit.quantizers_infrastructure import QuantizationTarget
23
- from model_compression_toolkit.quantizers_infrastructure.inferable_infrastructure.common.base_inferable_quantizer import mark_quantizer
24
- from model_compression_toolkit.quantizers_infrastructure.inferable_infrastructure.common.quant_utils import \
25
- adjust_range_to_include_zero
26
- from model_compression_toolkit.quantizers_infrastructure.inferable_infrastructure.keras.validation_functions import \
27
- validate_uniform_min_max_ranges, validate_adjusted_min_max_ranges
28
-
29
- if FOUND_TF:
30
- import tensorflow as tf
31
- from model_compression_toolkit.quantizers_infrastructure.inferable_infrastructure.keras.quantizers.base_keras_inferable_quantizer import BaseKerasInferableQuantizer
32
-
33
- @mark_quantizer(quantization_target=QuantizationTarget.Activation,
34
- quantization_method=[QuantizationMethod.UNIFORM],
35
- quantizer_type=None)
36
- class ActivationUniformInferableQuantizer(BaseKerasInferableQuantizer):
37
- """
38
- Class for quantizing activations using an uniform quantizer
39
- """
40
-
41
- def __init__(self,
42
- num_bits: int,
43
- min_range: List[float],
44
- max_range: List[float],
45
- ):
46
- """
47
- Initialize the quantizer with the specified parameters.
48
-
49
- Args:
50
- num_bits: number of bits to use for quantization
51
- min_range: min range for quantizing activations
52
- max_range: max range for quantizing activations
53
- """
54
- super(ActivationUniformInferableQuantizer, self).__init__()
55
-
56
- # Validate ranges properties
57
- validate_uniform_min_max_ranges(min_range,
58
- max_range)
59
-
60
- # In activation per-channel quantization is not supported thus we expect a single min/max value.
61
- assert len(min_range) == 1, f'In per-tensor quantization min_range should be of length 1 but is {len(min_range)}'
62
- assert len(max_range) == 1, f'In per-tensor quantization max_range should be of length 1 but is {len(max_range)}'
63
-
64
- self.num_bits = num_bits
65
-
66
- # Convert min/max to numpy arrays
67
- min_range, max_range = np.asarray(min_range), np.asarray(max_range)
68
- _min_range, _max_range = adjust_range_to_include_zero(min_range, max_range, num_bits)
69
- validate_adjusted_min_max_ranges(min_range=min_range,
70
- max_range=max_range,
71
- adj_min=_min_range,
72
- adj_max=_max_range)
73
-
74
- self.max_range = _max_range[0]
75
- self.min_range = _min_range[0]
76
-
77
-
78
- def __call__(self, inputs:tf.Tensor) -> tf.Tensor:
79
- """
80
- Quantize the given inputs using the quantizer parameters.
81
-
82
- Args:
83
- inputs: input tensor to quantize
84
-
85
- Returns:
86
- quantized tensor.
87
- """
88
- assert inputs.dtype==tf.float32, f'Input tensor was expected to be a float tensor but is of type {inputs.dtype}'
89
-
90
- return tf.quantization.fake_quant_with_min_max_vars(inputs,
91
- min=self.min_range,
92
- max=self.max_range,
93
- num_bits=self.num_bits)
94
-
95
- def get_config(self):
96
- """
97
- Return a dictionary with the configuration of the quantizer.
98
-
99
- Returns:
100
- Dictionary with the following keys: 'num_bits', 'min_range', 'max_range'
101
- """
102
- return {'num_bits': self.num_bits,
103
- 'min_range': self.min_range,
104
- 'max_range': self.max_range}
105
-
106
- else:
107
- class ActivationUniformInferableQuantizer: # pragma: no cover
108
- def __init__(self, *args, **kwargs):
109
- Logger.error('Installing tensorflow and tensorflow_model_optimization is mandatory '
110
- 'when using ActivationUniformInferableQuantizer. '
111
- 'Could not find Tensorflow package.')
@@ -1,56 +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 abc import abstractmethod
16
-
17
- from model_compression_toolkit.constants import FOUND_TF
18
- from model_compression_toolkit.quantizers_infrastructure import BaseInferableQuantizer
19
-
20
- if FOUND_TF:
21
- import tensorflow as tf
22
-
23
- class BaseKerasInferableQuantizer(BaseInferableQuantizer):
24
- def __init__(self):
25
- """
26
- This class is a base quantizer for Keras quantizers for inference only.
27
- """
28
- super(BaseKerasInferableQuantizer, self).__init__()
29
-
30
- @abstractmethod
31
- def get_config(self):
32
- """
33
- Return a dictionary with the configuration of the quantizer.
34
- """
35
- raise NotImplemented(f'{self.__class__.__name__} did not implement get_config') # pragma: no cover
36
-
37
- @abstractmethod
38
- def __call__(self, inputs: tf.Tensor):
39
- """
40
- Quantize the given inputs using the quantizer parameters.
41
-
42
- Args:
43
- inputs: input tensor to quantize
44
-
45
- Returns:
46
- quantized tensor.
47
- """
48
- raise NotImplemented(f'{self.__class__.__name__} did not implement __call__') # pragma: no cover
49
- else:
50
- class BaseKerasInferableQuantizer: # pragma: no cover
51
- def __init__(self, *args, **kwargs):
52
- raise Exception('Installing tensorflow and tensorflow_model_optimization is mandatory '
53
- 'when using BaseKerasInferableQuantizer. '
54
- 'Could not find Tensorflow package.')
55
-
56
-
@@ -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
- # ==============================================================================
@@ -1,79 +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 typing import List
16
-
17
- import numpy as np
18
-
19
- from model_compression_toolkit.constants import FOUND_TF
20
- from model_compression_toolkit.target_platform_capabilities.target_platform import QuantizationMethod
21
- from model_compression_toolkit.quantizers_infrastructure.inferable_infrastructure.common.base_inferable_quantizer import mark_quantizer, \
22
- QuantizationTarget
23
- from model_compression_toolkit.quantizers_infrastructure.inferable_infrastructure.common.constants import MULTIPLIER_N_BITS, EPS
24
-
25
- if FOUND_TF:
26
- from model_compression_toolkit.quantizers_infrastructure.inferable_infrastructure.keras.quantizers.weights_inferable_quantizers.\
27
- weights_lut_symmetric_inferable_quantizer import WeightsLUTSymmetricInferableQuantizer
28
-
29
-
30
- @mark_quantizer(quantization_target=QuantizationTarget.Weights,
31
- quantization_method=[QuantizationMethod.LUT_POT_QUANTIZER],
32
- quantizer_type=None)
33
- class WeightsLUTPOTInferableQuantizer(WeightsLUTSymmetricInferableQuantizer):
34
- """
35
- Class for quantizing weights using a lut power-of-two quantizer
36
- """
37
-
38
- def __init__(self,
39
- num_bits: int,
40
- cluster_centers: np.ndarray,
41
- threshold: List[float],
42
- per_channel: bool,
43
- channel_axis: int = None,
44
- input_rank: int = None,
45
- multiplier_n_bits: int = MULTIPLIER_N_BITS,
46
- eps: float = EPS):
47
- """
48
- Initialize the quantizer with the specified parameters.
49
-
50
- Args:
51
- num_bits: number of bits to use for quantization
52
- cluster_centers: the cluster centers to assign the weights
53
- threshold: threshold for quantizing weights
54
- per_channel: whether to use per-channel quantization
55
- channel_axis: axis along which to apply per-channel quantization
56
- input_rank: number of dimensions of input tensor the quantizer quantizes
57
- multiplier_n_bits: Number of bits that determines the quantization range
58
- eps: Small value for numerical stability in division
59
- """
60
-
61
- super(WeightsLUTPOTInferableQuantizer, self).__init__(num_bits=num_bits,
62
- cluster_centers=cluster_centers,
63
- threshold=threshold,
64
- per_channel=per_channel,
65
- channel_axis=channel_axis,
66
- input_rank=input_rank,
67
- multiplier_n_bits=multiplier_n_bits,
68
- eps=eps)
69
-
70
- is_threshold_pot = np.all([int(np.log2(x)) == np.log2(x) for x in self.threshold.flatten()])
71
- assert is_threshold_pot, f'Expected threshold to be power of 2 but is {self.threshold}'
72
-
73
-
74
- else:
75
- class WeightsLUTPOTInferableQuantizer: # pragma: no cover
76
- def __init__(self, *args, **kwargs):
77
- raise Exception('Installing tensorflow and tensorflow_model_optimization is mandatory '
78
- 'when using WeightsLUTPOTInferableQuantizer. '
79
- 'Could not find Tensorflow package.')