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,106 +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
-
18
- from model_compression_toolkit.constants import FOUND_TORCH
19
- from model_compression_toolkit.target_platform_capabilities.target_platform import QuantizationMethod
20
- from model_compression_toolkit.quantizers_infrastructure.inferable_infrastructure.common.base_inferable_quantizer \
21
- import mark_quantizer, \
22
- QuantizationTarget
23
- from model_compression_toolkit.quantizers_infrastructure.inferable_infrastructure.common.constants \
24
- import MULTIPLIER_N_BITS, EPS
25
-
26
- if FOUND_TORCH:
27
- import torch
28
- from model_compression_toolkit.quantizers_infrastructure.inferable_infrastructure.pytorch.quantizer_utils \
29
- import to_torch_tensor, get_working_device, lut_quantizer
30
- from model_compression_toolkit.quantizers_infrastructure.inferable_infrastructure.pytorch.quantizers \
31
- .base_lut_symmetric_inferable_quantizer import BaseLUTSymmetricInferableQuantizer
32
-
33
-
34
- @mark_quantizer(quantization_target=QuantizationTarget.Weights,
35
- quantization_method=[QuantizationMethod.LUT_SYM_QUANTIZER],
36
- quantizer_type=None)
37
- class WeightsLUTSymmetricInferableQuantizer(BaseLUTSymmetricInferableQuantizer):
38
- """
39
- Class for quantizing weights using a lut symmetric quantizer
40
- """
41
-
42
- def __init__(self,
43
- num_bits: int,
44
- cluster_centers: np.ndarray,
45
- threshold: np.ndarray,
46
- per_channel: bool,
47
- channel_axis: int = None,
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 weights
56
- threshold: threshold for quantizing weights
57
- per_channel: whether to use per-channel quantization
58
- channel_axis: Axis of input to apply per-channel quantization on
59
- multiplier_n_bits: Number of bits that determines the quantization range
60
- eps: Small value for numerical stability in division
61
- """
62
-
63
- super(WeightsLUTSymmetricInferableQuantizer, self).__init__(threshold=threshold,
64
- num_bits=num_bits,
65
- cluster_centers=cluster_centers,
66
- signed=True,
67
- multiplier_n_bits=multiplier_n_bits,
68
- eps=eps)
69
-
70
- if per_channel:
71
- assert channel_axis is not None, f'Channel axis is missing in per channel quantization'
72
- assert len(
73
- threshold) >= 1, f'In per-channel quantization threshold should be of length >= 1 but is ' \
74
- f'{len(threshold)}'
75
- else:
76
- assert len(
77
- threshold) == 1, f'In per-tensor quantization threshold should be of length 1 but is ' \
78
- f'{len(threshold)}'
79
-
80
- self.per_channel = per_channel
81
- self.channel_axis = channel_axis
82
-
83
- self.threshold = to_torch_tensor(self.threshold).to(get_working_device())
84
- self.cluster_centers = to_torch_tensor(self.cluster_centers).to(get_working_device())
85
-
86
- def __call__(self, inputs: torch.Tensor) -> torch.Tensor:
87
- """
88
- Quantize the given inputs using the quantizer parameters.
89
-
90
- Args:
91
- inputs: input tensor to quantize
92
-
93
- Returns:
94
- quantized tensor.
95
- """
96
- inputs.requires_grad = False
97
- return lut_quantizer(inputs, cluster_centers=self.cluster_centers, signed=True,
98
- threshold=self.threshold, multiplier_n_bits=self.multiplier_n_bits, eps=self.eps)
99
-
100
-
101
- else:
102
- class WeightsLUTSymmetricInferableQuantizer: # pragma: no cover
103
- def __init__(self, *args, **kwargs):
104
- raise Exception('Installing torch is mandatory '
105
- 'when using WeightsLUTSymmetricInferableQuantizer. '
106
- 'Could not find torch package.')
@@ -1,66 +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
-
18
- from model_compression_toolkit.constants import FOUND_TORCH
19
- from model_compression_toolkit.target_platform_capabilities.target_platform import QuantizationMethod
20
- from model_compression_toolkit.quantizers_infrastructure.inferable_infrastructure.common.base_inferable_quantizer import mark_quantizer, \
21
- QuantizationTarget
22
-
23
- if FOUND_TORCH:
24
- from model_compression_toolkit.quantizers_infrastructure.inferable_infrastructure.pytorch.quantizers.weights_inferable_quantizers.weights_symmetric_inferable_quantizer import \
25
- WeightsSymmetricInferableQuantizer
26
-
27
-
28
- @mark_quantizer(quantization_target=QuantizationTarget.Weights,
29
- quantization_method=[QuantizationMethod.POWER_OF_TWO],
30
- quantizer_type=None)
31
- class WeightsPOTInferableQuantizer(WeightsSymmetricInferableQuantizer):
32
- """
33
- Class for quantizing weights using power-of-two quantizer
34
- """
35
-
36
- def __init__(self,
37
- num_bits: int,
38
- threshold: np.ndarray,
39
- per_channel: bool,
40
- channel_axis: int = None
41
- ):
42
- """
43
- Initialize the quantizer with the specified parameters.
44
-
45
- Args:
46
- num_bits: number of bits to use for quantization
47
- threshold: threshold for quantizing activations
48
- per_channel: whether to use per-channel quantization
49
- channel_axis: Axis of input to apply per-channel quantization on.
50
- """
51
- # target of Weights quantization
52
- super(WeightsPOTInferableQuantizer, self).__init__(num_bits=num_bits,
53
- threshold=threshold,
54
- per_channel=per_channel,
55
- channel_axis=channel_axis)
56
-
57
- is_threshold_pot = np.all(np.round(np.log2(threshold.flatten()))==np.log2(threshold.flatten()))
58
- assert is_threshold_pot, f'Expected threshold to be power of 2 but is {threshold}'
59
-
60
-
61
- else:
62
- class WeightsPOTInferableQuantizer: # pragma: no cover
63
- def __init__(self, *args, **kwargs):
64
- raise Exception('Installing torch is mandatory '
65
- 'when using WeightsPOTInferableQuantizer. '
66
- 'Could not find torch package.')
@@ -1,104 +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
-
18
- from model_compression_toolkit.constants import FOUND_TORCH
19
- from model_compression_toolkit.target_platform_capabilities.target_platform import QuantizationMethod
20
- from model_compression_toolkit.quantizers_infrastructure.inferable_infrastructure.common.base_inferable_quantizer import mark_quantizer, \
21
- QuantizationTarget
22
-
23
- if FOUND_TORCH:
24
- import torch
25
- from model_compression_toolkit.quantizers_infrastructure.inferable_infrastructure.pytorch.quantizer_utils import to_torch_tensor, \
26
- get_working_device
27
- from model_compression_toolkit.quantizers_infrastructure.inferable_infrastructure.pytorch.quantizers.base_symmetric_inferable_quantizer import \
28
- BaseSymmetricInferableQuantizer
29
-
30
-
31
- @mark_quantizer(quantization_target=QuantizationTarget.Weights,
32
- quantization_method=[QuantizationMethod.SYMMETRIC],
33
- quantizer_type=None)
34
- class WeightsSymmetricInferableQuantizer(BaseSymmetricInferableQuantizer):
35
- """
36
- Class for quantizing weights using a symmetric quantizer
37
- """
38
-
39
- def __init__(self,
40
- num_bits: int,
41
- threshold: np.ndarray,
42
- per_channel: bool,
43
- channel_axis: int = None
44
- ):
45
- """
46
- Initialize the quantizer with the specified parameters.
47
-
48
- Args:
49
- num_bits: number of bits to use for quantization
50
- threshold: threshold for quantizing weights
51
- per_channel: whether to use per-channel quantization
52
- channel_axis: Axis of input to apply per-channel quantization on.
53
- """
54
-
55
- super(WeightsSymmetricInferableQuantizer, self).__init__(threshold=threshold,
56
- num_bits=num_bits,
57
- signed=True)
58
-
59
- if per_channel:
60
- assert channel_axis is not None, f'Channel axis is missing in per channel quantization'
61
- assert len(
62
- threshold) >= 1, f'In per-channel quantization threshold should be of length >= 1 but is ' \
63
- f'{len(threshold)}'
64
- else:
65
- assert len(
66
- threshold) == 1, f'In per-tensor quantization threshold should be of length 1 but is {len(threshold)}'
67
-
68
- self.per_channel = per_channel
69
- self.channel_axis = channel_axis
70
-
71
- self.scales = to_torch_tensor(self.scales).to(get_working_device())
72
- self.zero_points = torch.zeros(len(threshold), dtype=torch.int32).to(get_working_device())
73
-
74
- def __call__(self, inputs: torch.Tensor) -> torch.Tensor:
75
- """
76
- Quantize the given inputs using the quantizer parameters.
77
-
78
- Args:
79
- inputs: input tensor to quantize
80
-
81
- Returns:
82
- quantized tensor.
83
- """
84
- inputs.requires_grad = False
85
- if self.per_channel:
86
- return torch.fake_quantize_per_channel_affine(inputs,
87
- self.scales,
88
- self.zero_points,
89
- axis=self.channel_axis,
90
- quant_min=self.min_quantized_domain,
91
- quant_max=self.max_quantized_domain)
92
- return torch.fake_quantize_per_tensor_affine(inputs,
93
- self.scales,
94
- self.zero_points,
95
- quant_min=self.min_quantized_domain,
96
- quant_max=self.max_quantized_domain)
97
-
98
-
99
- else:
100
- class WeightsSymmetricInferableQuantizer: # pragma: no cover
101
- def __init__(self, *args, **kwargs):
102
- raise Exception('Installing torch is mandatory '
103
- 'when using WeightsSymmetricInferableQuantizer. '
104
- 'Could not find torch package.')
@@ -1,109 +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
-
18
- from model_compression_toolkit.constants import FOUND_TORCH
19
- from model_compression_toolkit.logger import Logger
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 QuantizationTarget, \
22
- mark_quantizer
23
-
24
- if FOUND_TORCH:
25
- import torch
26
- from model_compression_toolkit.quantizers_infrastructure.inferable_infrastructure.pytorch.quantizer_utils import get_working_device, \
27
- fix_range_to_include_zero, to_torch_tensor
28
- from model_compression_toolkit.quantizers_infrastructure.inferable_infrastructure.pytorch.quantizers.base_uniform_inferable_quantizer import \
29
- BaseUniformInferableQuantizer
30
-
31
-
32
- @mark_quantizer(quantization_target=QuantizationTarget.Weights,
33
- quantization_method=[QuantizationMethod.UNIFORM],
34
- quantizer_type=None)
35
- class WeightsUniformInferableQuantizer(BaseUniformInferableQuantizer):
36
- """
37
- Class for quantizing weights using a uniform quantizer
38
- """
39
-
40
- def __init__(self,
41
- num_bits: int,
42
- min_range: np.ndarray,
43
- max_range: np.ndarray,
44
- per_channel: bool,
45
- channel_axis: int = None
46
- ):
47
- """
48
- Initialize the quantizer with the specified parameters.
49
-
50
- Args:
51
- num_bits: number of bits to use for quantization
52
- min_range: min quantization range for quantizing weights
53
- max_range: max quantization range for quantizing weights
54
- per_channel: whether to use per-channel quantization
55
- channel_axis: Axis of input to apply per-channel quantization on.
56
- """
57
- super(WeightsUniformInferableQuantizer, self).__init__(num_bits=num_bits,
58
- min_range=min_range,
59
- max_range=max_range)
60
-
61
- # Align mix/max numpy arrays so they are torch Tensors on the working device
62
- min_range = to_torch_tensor(min_range).to(get_working_device())
63
- max_range = to_torch_tensor(max_range).to(get_working_device())
64
-
65
- self.per_channel = per_channel
66
- self.channel_axis = channel_axis
67
-
68
- min_range, max_range = fix_range_to_include_zero(min_range,
69
- max_range,
70
- num_bits)
71
- # Compute the step size of quantized values.
72
- self.scales = (max_range - min_range) / (2 ** num_bits - 1)
73
- self.zero_points = -(
74
- min_range / self.scales).int() # zp has to be positive, and a <=0, so we multiply by -1
75
-
76
- self.scales = self.scales.to(get_working_device())
77
- self.zero_points = self.zero_points.to(get_working_device())
78
-
79
- def __call__(self,
80
- inputs: torch.Tensor) -> torch.Tensor:
81
- """
82
- Weight fake quantizer
83
- Args:
84
- inputs: weights to quantize.
85
-
86
- Returns:
87
- quantized weights
88
- """
89
- inputs.requires_grad = False
90
- if self.per_channel:
91
- return torch.fake_quantize_per_channel_affine(inputs,
92
- self.scales.flatten(),
93
- self.zero_points.flatten(),
94
- axis=self.channel_axis,
95
- quant_min=self.min_quantized_domain,
96
- quant_max=self.max_quantized_domain)
97
- return torch.fake_quantize_per_tensor_affine(inputs,
98
- self.scales,
99
- self.zero_points,
100
- quant_min=self.min_quantized_domain,
101
- quant_max=self.max_quantized_domain)
102
-
103
-
104
- else:
105
- class WeightsUniformInferableQuantizer: # pragma: no cover
106
- def __init__(self, *args, **kwargs):
107
- Logger.error('Installing torch is mandatory '
108
- 'when using WeightsUniformInferableQuantizer. '
109
- 'Could not find torch package.')
@@ -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,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,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,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
- # ==============================================================================