ai-edge-quantizer-nightly 0.0.1.dev20250115__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.
- ai_edge_quantizer/__init__.py +19 -0
- ai_edge_quantizer/algorithm_manager.py +167 -0
- ai_edge_quantizer/algorithm_manager_api.py +271 -0
- ai_edge_quantizer/algorithm_manager_api_test.py +210 -0
- ai_edge_quantizer/algorithms/__init__.py +15 -0
- ai_edge_quantizer/algorithms/nonlinear_quantize/__init__.py +15 -0
- ai_edge_quantizer/algorithms/nonlinear_quantize/float_casting.py +273 -0
- ai_edge_quantizer/algorithms/nonlinear_quantize/float_casting_test.py +664 -0
- ai_edge_quantizer/algorithms/uniform_quantize/__init__.py +15 -0
- ai_edge_quantizer/algorithms/uniform_quantize/naive_min_max_quantize.py +666 -0
- ai_edge_quantizer/algorithms/uniform_quantize/naive_min_max_quantize_test.py +184 -0
- ai_edge_quantizer/algorithms/uniform_quantize/uniform_quantize_tensor.py +371 -0
- ai_edge_quantizer/algorithms/uniform_quantize/uniform_quantize_tensor_test.py +357 -0
- ai_edge_quantizer/algorithms/utils/__init__.py +15 -0
- ai_edge_quantizer/algorithms/utils/min_max_quantize_utils.py +1067 -0
- ai_edge_quantizer/algorithms/utils/min_max_quantize_utils_test.py +512 -0
- ai_edge_quantizer/calibrator.py +288 -0
- ai_edge_quantizer/calibrator_test.py +297 -0
- ai_edge_quantizer/conftest.py +22 -0
- ai_edge_quantizer/default_policy.py +310 -0
- ai_edge_quantizer/model_modifier.py +176 -0
- ai_edge_quantizer/model_modifier_test.py +130 -0
- ai_edge_quantizer/model_validator.py +357 -0
- ai_edge_quantizer/model_validator_test.py +354 -0
- ai_edge_quantizer/params_generator.py +361 -0
- ai_edge_quantizer/params_generator_test.py +1041 -0
- ai_edge_quantizer/qtyping.py +483 -0
- ai_edge_quantizer/quantizer.py +372 -0
- ai_edge_quantizer/quantizer_test.py +532 -0
- ai_edge_quantizer/recipe.py +67 -0
- ai_edge_quantizer/recipe_manager.py +245 -0
- ai_edge_quantizer/recipe_manager_test.py +815 -0
- ai_edge_quantizer/recipe_test.py +97 -0
- ai_edge_quantizer/transformation_instruction_generator.py +584 -0
- ai_edge_quantizer/transformation_instruction_generator_test.py +1082 -0
- ai_edge_quantizer/transformation_performer.py +278 -0
- ai_edge_quantizer/transformation_performer_test.py +344 -0
- ai_edge_quantizer/transformations/__init__.py +15 -0
- ai_edge_quantizer/transformations/dequant_insert.py +87 -0
- ai_edge_quantizer/transformations/dequant_insert_test.py +304 -0
- ai_edge_quantizer/transformations/emulated_subchannel.py +363 -0
- ai_edge_quantizer/transformations/emulated_subchannel_test.py +212 -0
- ai_edge_quantizer/transformations/quant_insert.py +100 -0
- ai_edge_quantizer/transformations/quant_insert_test.py +284 -0
- ai_edge_quantizer/transformations/quantize_tensor.py +156 -0
- ai_edge_quantizer/transformations/quantize_tensor_test.py +227 -0
- ai_edge_quantizer/transformations/transformation_utils.py +132 -0
- ai_edge_quantizer/transformations/transformation_utils_test.py +162 -0
- ai_edge_quantizer/utils/__init__.py +15 -0
- ai_edge_quantizer/utils/calibration_utils.py +86 -0
- ai_edge_quantizer/utils/calibration_utils_test.py +77 -0
- ai_edge_quantizer/utils/test_utils.py +107 -0
- ai_edge_quantizer/utils/tfl_flatbuffer_utils.py +317 -0
- ai_edge_quantizer/utils/tfl_flatbuffer_utils_test.py +200 -0
- ai_edge_quantizer/utils/tfl_interpreter_utils.py +312 -0
- ai_edge_quantizer/utils/tfl_interpreter_utils_test.py +332 -0
- ai_edge_quantizer/utils/validation_utils.py +125 -0
- ai_edge_quantizer/utils/validation_utils_test.py +87 -0
- ai_edge_quantizer_nightly-0.0.1.dev20250115.dist-info/LICENSE +201 -0
- ai_edge_quantizer_nightly-0.0.1.dev20250115.dist-info/METADATA +32 -0
- ai_edge_quantizer_nightly-0.0.1.dev20250115.dist-info/RECORD +63 -0
- ai_edge_quantizer_nightly-0.0.1.dev20250115.dist-info/WHEEL +5 -0
- ai_edge_quantizer_nightly-0.0.1.dev20250115.dist-info/top_level.txt +1 -0
@@ -0,0 +1,357 @@
|
|
1
|
+
# Copyright 2024 The AI Edge Quantizer Authors.
|
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
|
+
"""Tests for tensor_utils."""
|
17
|
+
|
18
|
+
from absl.testing import parameterized
|
19
|
+
import numpy as np
|
20
|
+
from tensorflow.python.platform import googletest
|
21
|
+
from ai_edge_quantizer import qtyping
|
22
|
+
from ai_edge_quantizer.algorithms.uniform_quantize import uniform_quantize_tensor
|
23
|
+
|
24
|
+
_IntType = uniform_quantize_tensor.IntType
|
25
|
+
|
26
|
+
|
27
|
+
class TensorUtilsTest(parameterized.TestCase):
|
28
|
+
|
29
|
+
def setUp(self):
|
30
|
+
super().setUp()
|
31
|
+
self._test_data = np.array([[1, 2], [4, 5]])
|
32
|
+
self._expected_quantized_result = np.array(
|
33
|
+
[[9, 19], [22, 27]], dtype=np.int8
|
34
|
+
)
|
35
|
+
|
36
|
+
@parameterized.parameters(
|
37
|
+
(_IntType(8, True), (-128, 127)),
|
38
|
+
(_IntType(8, False), (0, 255)),
|
39
|
+
(_IntType(4, True), (-8, 7)),
|
40
|
+
(_IntType(4, False), (0, 15)),
|
41
|
+
(_IntType(2, True), (-2, 1)),
|
42
|
+
(_IntType(2, False), (0, 3)),
|
43
|
+
)
|
44
|
+
def test_get_quantized_range(self, qtype, expected_range):
|
45
|
+
self.assertEqual(
|
46
|
+
expected_range, uniform_quantize_tensor.get_quantized_range(qtype)
|
47
|
+
)
|
48
|
+
|
49
|
+
@parameterized.parameters(
|
50
|
+
(_IntType(8, True), np.int8),
|
51
|
+
(_IntType(16, False), np.uint16),
|
52
|
+
(_IntType(32, True), np.int32),
|
53
|
+
(_IntType(64, True), np.int64),
|
54
|
+
)
|
55
|
+
def test_assign_quantized_type(self, qtype, expected_type):
|
56
|
+
sample_input = np.array([2.0, 2.0])
|
57
|
+
result = uniform_quantize_tensor.assign_quantized_type(sample_input, qtype)
|
58
|
+
self.assertEqual(expected_type, result.dtype)
|
59
|
+
|
60
|
+
@parameterized.named_parameters(
|
61
|
+
dict(
|
62
|
+
testcase_name="scalar_tensor_1d_params",
|
63
|
+
scale=np.array([0.1]),
|
64
|
+
zero_point=np.array([-1], dtype=np.int8),
|
65
|
+
tensor_data=np.array(6.66),
|
66
|
+
quantized_dimension=None,
|
67
|
+
),
|
68
|
+
dict(
|
69
|
+
testcase_name="1d_tensor_scalar_params",
|
70
|
+
scale=np.array(0.1),
|
71
|
+
zero_point=np.array(-1, dtype=np.int8),
|
72
|
+
tensor_data=np.array([6.66, 8.88]),
|
73
|
+
quantized_dimension=None,
|
74
|
+
),
|
75
|
+
dict(
|
76
|
+
testcase_name="2d_tensor_1d_params",
|
77
|
+
scale=np.array([0.1, 0.2]),
|
78
|
+
zero_point=np.array([-1, 2], dtype=np.int8),
|
79
|
+
tensor_data=np.array([[1, 2], [4, 5]]),
|
80
|
+
quantized_dimension=0,
|
81
|
+
),
|
82
|
+
)
|
83
|
+
def test_fix_quantization_params_rank_succeed(
|
84
|
+
self, scale, zero_point, tensor_data, quantized_dimension
|
85
|
+
):
|
86
|
+
quant_params = qtyping.UniformQuantParams(
|
87
|
+
num_bits=8,
|
88
|
+
quantized_dimension=quantized_dimension,
|
89
|
+
scale=scale,
|
90
|
+
zero_point=zero_point,
|
91
|
+
symmetric=False,
|
92
|
+
)
|
93
|
+
fixed_quant_params = uniform_quantize_tensor.fix_quantization_params_rank(
|
94
|
+
tensor_data, quant_params
|
95
|
+
)
|
96
|
+
self.assertEqual(fixed_quant_params.scale.ndim, tensor_data.ndim)
|
97
|
+
self.assertEqual(fixed_quant_params.zero_point.ndim, tensor_data.ndim)
|
98
|
+
|
99
|
+
def test_fix_quantization_params_rank_scalar_tensor_1d_params_raise(self):
|
100
|
+
quant_params = qtyping.UniformQuantParams(
|
101
|
+
num_bits=8,
|
102
|
+
quantized_dimension=None,
|
103
|
+
scale=np.array([0.1, 0.5]),
|
104
|
+
zero_point=np.array([-1, -2], dtype=np.int8),
|
105
|
+
symmetric=False,
|
106
|
+
)
|
107
|
+
error_message = (
|
108
|
+
"Scale and zero_point must contain single element for scalar tensor."
|
109
|
+
)
|
110
|
+
with self.assertRaisesWithPredicateMatch(
|
111
|
+
ValueError, lambda err: error_message in str(err)
|
112
|
+
):
|
113
|
+
uniform_quantize_tensor.fix_quantization_params_rank(
|
114
|
+
np.array(6.66), quant_params
|
115
|
+
)
|
116
|
+
|
117
|
+
@parameterized.parameters(
|
118
|
+
(
|
119
|
+
[-3.0, 1.3, 2.4, 16.0],
|
120
|
+
[0.12598425],
|
121
|
+
[0],
|
122
|
+
8,
|
123
|
+
False,
|
124
|
+
[-24, 10, 19, 127],
|
125
|
+
),
|
126
|
+
(
|
127
|
+
[-3.0, 1.3, 2.4, 16.0],
|
128
|
+
[1.2666667],
|
129
|
+
[-6],
|
130
|
+
4,
|
131
|
+
False,
|
132
|
+
[-8, -5, -4, 7],
|
133
|
+
),
|
134
|
+
(
|
135
|
+
[-3.0, 1.3, 2.4, 16.0],
|
136
|
+
[1.2666667],
|
137
|
+
[-6],
|
138
|
+
4,
|
139
|
+
True,
|
140
|
+
[-7, -5, -4, 7],
|
141
|
+
),
|
142
|
+
)
|
143
|
+
def test_uniform_quantize(
|
144
|
+
self, tensor, scale, zero_points, num_bits, symmetric, expected_tensor
|
145
|
+
):
|
146
|
+
quant_params = qtyping.UniformQuantParams(
|
147
|
+
quantized_dimension=0,
|
148
|
+
num_bits=num_bits,
|
149
|
+
scale=np.array(scale),
|
150
|
+
zero_point=np.array(zero_points),
|
151
|
+
symmetric=symmetric,
|
152
|
+
)
|
153
|
+
|
154
|
+
quantized_tensor = uniform_quantize_tensor.uniform_quantize(
|
155
|
+
np.array(tensor), quant_params
|
156
|
+
)
|
157
|
+
|
158
|
+
self.assertSequenceAlmostEqual(expected_tensor, quantized_tensor)
|
159
|
+
|
160
|
+
def test_uniform_quantize_wrong_shape(self):
|
161
|
+
tensor = [-3.0, 1.3, 2.4, 16.0]
|
162
|
+
|
163
|
+
error_message = "scale and zero_point must have the same shape."
|
164
|
+
with self.assertRaisesWithPredicateMatch(
|
165
|
+
ValueError, lambda err: error_message in str(err)
|
166
|
+
):
|
167
|
+
uniform_quantize_tensor.uniform_quantize(
|
168
|
+
np.array(tensor),
|
169
|
+
qtyping.UniformQuantParams(
|
170
|
+
quantized_dimension=0,
|
171
|
+
num_bits=4,
|
172
|
+
scale=np.array([[[1.2666667]]]),
|
173
|
+
zero_point=np.array([[-6]]),
|
174
|
+
symmetric=True,
|
175
|
+
),
|
176
|
+
)
|
177
|
+
|
178
|
+
error_message = "Ranks of scales"
|
179
|
+
with self.assertRaisesWithPredicateMatch(
|
180
|
+
ValueError, lambda err: error_message in str(err)
|
181
|
+
):
|
182
|
+
uniform_quantize_tensor.uniform_quantize(
|
183
|
+
np.array(tensor),
|
184
|
+
qtyping.UniformQuantParams(
|
185
|
+
quantized_dimension=0,
|
186
|
+
num_bits=4,
|
187
|
+
scale=np.array([[1.2666667]]),
|
188
|
+
zero_point=np.array([[-6]]),
|
189
|
+
symmetric=True,
|
190
|
+
),
|
191
|
+
)
|
192
|
+
|
193
|
+
@parameterized.parameters(
|
194
|
+
(
|
195
|
+
8,
|
196
|
+
[-24, 10, 19, 127],
|
197
|
+
[0.12598425],
|
198
|
+
[0],
|
199
|
+
[-3.023622, 1.2598425, 2.3937008, 16.0],
|
200
|
+
),
|
201
|
+
(
|
202
|
+
4,
|
203
|
+
[-8, -5, -4, 7],
|
204
|
+
[1.2666667],
|
205
|
+
[-6],
|
206
|
+
[-2.5333335, 1.2666668, 2.5333335, 16.466667],
|
207
|
+
),
|
208
|
+
)
|
209
|
+
def test_uniform_dequantize(
|
210
|
+
self,
|
211
|
+
num_bits,
|
212
|
+
quantized_tensor,
|
213
|
+
scale,
|
214
|
+
zero_points,
|
215
|
+
expected_output_tensor,
|
216
|
+
):
|
217
|
+
quant_params = qtyping.UniformQuantParams(
|
218
|
+
quantized_dimension=0,
|
219
|
+
num_bits=num_bits,
|
220
|
+
scale=np.array(scale),
|
221
|
+
zero_point=np.array(zero_points),
|
222
|
+
symmetric=False,
|
223
|
+
)
|
224
|
+
|
225
|
+
dequantized_tensor = uniform_quantize_tensor.uniform_dequantize(
|
226
|
+
np.array(quantized_tensor), quant_params
|
227
|
+
)
|
228
|
+
|
229
|
+
self.assertSequenceAlmostEqual(
|
230
|
+
expected_output_tensor, dequantized_tensor, places=4
|
231
|
+
)
|
232
|
+
|
233
|
+
def test_uniform_dequantize_wrong_shape(self):
|
234
|
+
tensor = [-3.0, 1.3, 2.4, 16.0]
|
235
|
+
|
236
|
+
error_message = "scale and zero_point must have the same shape."
|
237
|
+
with self.assertRaisesWithPredicateMatch(
|
238
|
+
ValueError, lambda err: error_message in str(err)
|
239
|
+
):
|
240
|
+
uniform_quantize_tensor.uniform_dequantize(
|
241
|
+
np.array(tensor),
|
242
|
+
qtyping.UniformQuantParams(
|
243
|
+
quantized_dimension=0,
|
244
|
+
num_bits=4,
|
245
|
+
scale=np.array([[[1.2666667]]]),
|
246
|
+
zero_point=np.array([[-6]]),
|
247
|
+
symmetric=True,
|
248
|
+
),
|
249
|
+
)
|
250
|
+
|
251
|
+
error_message = "Ranks of scales"
|
252
|
+
with self.assertRaisesWithPredicateMatch(
|
253
|
+
ValueError, lambda err: error_message in str(err)
|
254
|
+
):
|
255
|
+
uniform_quantize_tensor.uniform_dequantize(
|
256
|
+
np.array(tensor),
|
257
|
+
qtyping.UniformQuantParams(
|
258
|
+
quantized_dimension=0,
|
259
|
+
num_bits=4,
|
260
|
+
scale=np.array([[1.2666667]]),
|
261
|
+
zero_point=np.array([[-6]]),
|
262
|
+
symmetric=True,
|
263
|
+
),
|
264
|
+
)
|
265
|
+
|
266
|
+
@parameterized.parameters(
|
267
|
+
(8, 8, True, True), (8, 4, False, True), (16, 8, True, False)
|
268
|
+
)
|
269
|
+
def test_quantize_bias_tensor(
|
270
|
+
self,
|
271
|
+
activation_num_bits,
|
272
|
+
weight_num_bits,
|
273
|
+
symmetric_weights,
|
274
|
+
channelwise_weight,
|
275
|
+
):
|
276
|
+
input_quant_config = qtyping.UniformQuantParams(
|
277
|
+
scale=np.array([0.8]),
|
278
|
+
zero_point=np.array([10]),
|
279
|
+
num_bits=activation_num_bits,
|
280
|
+
symmetric=False,
|
281
|
+
quantized_dimension=None,
|
282
|
+
)
|
283
|
+
weight_scale, weight_zp = [0.1], [-1]
|
284
|
+
num_channels, quantized_dimension = 1, None
|
285
|
+
if channelwise_weight:
|
286
|
+
num_channels = 2
|
287
|
+
weight_scale = weight_scale * num_channels
|
288
|
+
weight_zp = weight_zp * num_channels
|
289
|
+
quantized_dimension = 0
|
290
|
+
|
291
|
+
weight_quant_config = qtyping.UniformQuantParams(
|
292
|
+
quantized_dimension=0,
|
293
|
+
num_bits=weight_num_bits,
|
294
|
+
scale=np.array(weight_scale, dtype=np.float32),
|
295
|
+
zero_point=np.array(weight_zp, dtype=np.int8),
|
296
|
+
symmetric=symmetric_weights,
|
297
|
+
)
|
298
|
+
bias_tensor_data = np.array([66.0, 88.0])
|
299
|
+
|
300
|
+
bias_quant_config = uniform_quantize_tensor.symmetric_quantize_bias_tensor(
|
301
|
+
bias_tensor_data,
|
302
|
+
input_quant_config,
|
303
|
+
weight_quant_config,
|
304
|
+
)
|
305
|
+
bias_num_bits = 32 if activation_num_bits == 8 else 64
|
306
|
+
self.assertEqual(bias_quant_config.num_bits, bias_num_bits)
|
307
|
+
# Alwasys a 1D array
|
308
|
+
self.assertLen(bias_quant_config.scale.shape, 1)
|
309
|
+
self.assertLen(bias_quant_config.zero_point.shape, 1)
|
310
|
+
|
311
|
+
self.assertLen(bias_quant_config.scale, num_channels)
|
312
|
+
effective_scale = input_quant_config.scale[0] * weight_quant_config.scale[0]
|
313
|
+
self.assertEqual(bias_quant_config.scale[0], effective_scale)
|
314
|
+
self.assertEqual(bias_quant_config.zero_point[0], 0) # Always symmetric
|
315
|
+
self.assertEqual(bias_quant_config.symmetric, True)
|
316
|
+
self.assertEqual(bias_quant_config.quantized_dimension, quantized_dimension)
|
317
|
+
|
318
|
+
# Check quantized content
|
319
|
+
dequantized_bias = uniform_quantize_tensor.uniform_dequantize(
|
320
|
+
bias_quant_config.quantized_data, bias_quant_config
|
321
|
+
)
|
322
|
+
self.assertSequenceAlmostEqual(
|
323
|
+
list(dequantized_bias.flatten()), list(bias_tensor_data), places=5
|
324
|
+
)
|
325
|
+
expected_quantized_data = uniform_quantize_tensor.uniform_quantize(
|
326
|
+
bias_tensor_data, bias_quant_config
|
327
|
+
)
|
328
|
+
self.assertSequenceEqual(
|
329
|
+
list(expected_quantized_data.flatten()),
|
330
|
+
list(bias_quant_config.quantized_data.flatten()), # pytype: disable=attribute-error
|
331
|
+
)
|
332
|
+
|
333
|
+
@parameterized.parameters((8, True), (16, False))
|
334
|
+
def test_tensor_zp_scale_from_min_max(self, num_bits, symmetric):
|
335
|
+
min_val = np.min(self._test_data, keepdims=True)
|
336
|
+
max_val = np.max(self._test_data, keepdims=True)
|
337
|
+
|
338
|
+
zp, scale = uniform_quantize_tensor.tensor_zp_scale_from_min_max(
|
339
|
+
min_val, max_val, num_bits, symmetric
|
340
|
+
)
|
341
|
+
self.assertEqual(zp.shape, scale.shape)
|
342
|
+
max_q = 2**num_bits / 2 - 1
|
343
|
+
calculated_max = scale[0] * (max_q - zp[0])
|
344
|
+
self.assertAlmostEqual(calculated_max, max_val, delta=1e-3)
|
345
|
+
min_q = -(2**num_bits) / 2
|
346
|
+
if symmetric:
|
347
|
+
min_q += 1
|
348
|
+
calculated_min = scale[0] * (min_q - zp[0])
|
349
|
+
if symmetric:
|
350
|
+
self.assertAlmostEqual(calculated_min, -max_val, delta=1e-3)
|
351
|
+
else:
|
352
|
+
# Range has to be extended to include zero.
|
353
|
+
self.assertEqual(calculated_min, 0)
|
354
|
+
|
355
|
+
|
356
|
+
if __name__ == "__main__":
|
357
|
+
googletest.main()
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# Copyright 2024 The AI Edge Quantizer Authors.
|
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
|
+
|