ai-edge-quantizer-nightly 0.0.1.dev20241122__py3-none-any.whl → 0.0.1.dev20241124__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/transformations/quantize_tensor.py +10 -10
- ai_edge_quantizer/transformations/quantize_tensor_test.py +23 -18
- {ai_edge_quantizer_nightly-0.0.1.dev20241122.dist-info → ai_edge_quantizer_nightly-0.0.1.dev20241124.dist-info}/METADATA +1 -1
- {ai_edge_quantizer_nightly-0.0.1.dev20241122.dist-info → ai_edge_quantizer_nightly-0.0.1.dev20241124.dist-info}/RECORD +7 -7
- {ai_edge_quantizer_nightly-0.0.1.dev20241122.dist-info → ai_edge_quantizer_nightly-0.0.1.dev20241124.dist-info}/WHEEL +1 -1
- {ai_edge_quantizer_nightly-0.0.1.dev20241122.dist-info → ai_edge_quantizer_nightly-0.0.1.dev20241124.dist-info}/LICENSE +0 -0
- {ai_edge_quantizer_nightly-0.0.1.dev20241122.dist-info → ai_edge_quantizer_nightly-0.0.1.dev20241124.dist-info}/top_level.txt +0 -0
@@ -22,19 +22,19 @@ from ai_edge_quantizer.transformations import transformation_utils
|
|
22
22
|
from ai_edge_litert import schema_py_generated # pylint: disable=g-direct-tensorflow-import
|
23
23
|
|
24
24
|
|
25
|
-
# TODO: b/335014051 -
|
25
|
+
# TODO: b/335014051 - Support distinguishing INT, FLOAT & UINT, BFLOAT.
|
26
26
|
def quant_params_to_tflite_type(
|
27
27
|
bitwidth: int,
|
28
28
|
) -> Optional[schema_py_generated.TensorType]:
|
29
|
-
"""Given specifications from quant param return the corresponding
|
29
|
+
"""Given specifications from quant param return the corresponding TFLite dtype.
|
30
30
|
|
31
31
|
Args:
|
32
|
-
bitwidth:
|
32
|
+
bitwidth: Bit width from UniformQuantParams.
|
33
33
|
|
34
34
|
Returns:
|
35
|
-
|
35
|
+
The corresponding TFLite tensor type.
|
36
36
|
"""
|
37
|
-
if bitwidth
|
37
|
+
if bitwidth == 4:
|
38
38
|
return schema_py_generated.TensorType.INT4
|
39
39
|
elif bitwidth <= 8:
|
40
40
|
return schema_py_generated.TensorType.INT8
|
@@ -68,19 +68,19 @@ def nonlinear_quant_params_to_tflite_type(
|
|
68
68
|
|
69
69
|
|
70
70
|
def _pack_data(bitwidth: int, flattened_data: np.ndarray) -> np.ndarray:
|
71
|
-
"""Pack the data to the corresponding
|
71
|
+
"""Pack the data to the corresponding bit width.
|
72
72
|
|
73
|
-
If no packing is needed, the original data is
|
74
|
-
|
73
|
+
Currently only support 4 bits. If no packing is needed, the original data is
|
74
|
+
returned.
|
75
75
|
|
76
76
|
Args:
|
77
|
-
bitwidth:
|
77
|
+
bitwidth: Bit width from NonLinearQuantParams.
|
78
78
|
flattened_data: The data to be packed.
|
79
79
|
|
80
80
|
Returns:
|
81
81
|
Packed data.
|
82
82
|
"""
|
83
|
-
if bitwidth
|
83
|
+
if bitwidth == 4:
|
84
84
|
even_data = flattened_data[::2] & 0x0F
|
85
85
|
odd_data = np.left_shift(flattened_data[1::2], 4).astype(np.uint8)
|
86
86
|
if odd_data.shape[0] == even_data.shape[0] - 1:
|
@@ -18,6 +18,7 @@
|
|
18
18
|
import os
|
19
19
|
import numpy as np
|
20
20
|
from tensorflow.python.platform import googletest
|
21
|
+
from absl.testing import parameterized
|
21
22
|
from ai_edge_quantizer import qtyping
|
22
23
|
from ai_edge_quantizer.transformations import quantize_tensor
|
23
24
|
from ai_edge_quantizer.transformations import transformation_utils
|
@@ -28,7 +29,7 @@ from ai_edge_litert import schema_py_generated # pylint: disable=g-direct-tenso
|
|
28
29
|
TEST_DATA_PREFIX_PATH = test_utils.get_path_to_datafile("..")
|
29
30
|
|
30
31
|
|
31
|
-
class QuantizeTensorTest(
|
32
|
+
class QuantizeTensorTest(parameterized.TestCase):
|
32
33
|
|
33
34
|
def setUp(self):
|
34
35
|
super().setUp()
|
@@ -179,40 +180,44 @@ class QuantizeTensorTest(googletest.TestCase):
|
|
179
180
|
np.testing.assert_array_equal(quant_param.zeroPoint, [1])
|
180
181
|
self.assertEqual(quant_param.quantizedDimension, 0)
|
181
182
|
|
182
|
-
|
183
|
+
@parameterized.named_parameters(
|
184
|
+
dict(
|
185
|
+
testcase_name="int5",
|
186
|
+
num_bits=5,
|
187
|
+
),
|
188
|
+
dict(
|
189
|
+
testcase_name="int2",
|
190
|
+
num_bits=2,
|
191
|
+
),
|
192
|
+
)
|
193
|
+
def test_int_constant_not_packed(self, num_bits):
|
183
194
|
subgraph = self._model.subgraphs[0]
|
184
195
|
model = self._model
|
185
|
-
|
186
|
-
|
187
|
-
0x0,
|
188
|
-
0x1,
|
189
|
-
0x2,
|
190
|
-
0x3,
|
191
|
-
0x4,
|
192
|
-
0x5,
|
193
|
-
0x6,
|
194
|
-
0x7,
|
195
|
-
],
|
196
|
-
dtype=np.int8,
|
197
|
-
)
|
196
|
+
tensor_id = 7
|
197
|
+
data = np.array([0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7], dtype=np.int8)
|
198
198
|
expected = np.array([0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7])
|
199
199
|
ret = quantize_tensor.quantize_tensor(
|
200
200
|
transformation_utils.TransformationInput(
|
201
|
-
tensor_id=
|
201
|
+
tensor_id=tensor_id,
|
202
202
|
op_codes=model.operatorCodes,
|
203
203
|
buffers=model.buffers,
|
204
204
|
subgraph=subgraph,
|
205
205
|
producer=-1,
|
206
206
|
consumers=[4],
|
207
207
|
quant_params=qtyping.UniformQuantParams(
|
208
|
-
|
208
|
+
num_bits=num_bits,
|
209
|
+
quantized_dimension=None,
|
210
|
+
scale=np.ones(1),
|
211
|
+
zero_point=np.ones(1),
|
212
|
+
symmetric=True,
|
213
|
+
quantized_data=data,
|
209
214
|
),
|
210
215
|
)
|
211
216
|
)
|
212
217
|
self.assertEqual(ret.op_id, 0)
|
213
218
|
self.assertEqual(ret.num_ops_added, 0)
|
214
219
|
np.testing.assert_array_equal(model.buffers[8].data, expected)
|
215
|
-
quant_param = subgraph.tensors[
|
220
|
+
quant_param = subgraph.tensors[tensor_id].quantization
|
216
221
|
np.testing.assert_array_equal(quant_param.scale, [1])
|
217
222
|
np.testing.assert_array_equal(quant_param.zeroPoint, [1])
|
218
223
|
self.assertEqual(quant_param.quantizedDimension, 0)
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: ai-edge-quantizer-nightly
|
3
|
-
Version: 0.0.1.
|
3
|
+
Version: 0.0.1.dev20241124
|
4
4
|
Summary: A quantizer for advanced developers to quantize converted AI Edge models.
|
5
5
|
Home-page: https://github.com/google-ai-edge/ai-edge-quantizer
|
6
6
|
Keywords: On-Device ML,AI,Google,TFLite,Quantization,LLMs,GenAI
|
@@ -42,8 +42,8 @@ ai_edge_quantizer/transformations/emulated_subchannel.py,sha256=HVaRxoC8PCAvy3xe
|
|
42
42
|
ai_edge_quantizer/transformations/emulated_subchannel_test.py,sha256=gZP6u9NdPXl7s19qB_Un8evou9ZZV6I9Gy0E1rdobHM,7722
|
43
43
|
ai_edge_quantizer/transformations/quant_insert.py,sha256=jn6HsJaV-sqBiFPY-Aqbd64t8zgcYVkEkZI375x_FWY,3958
|
44
44
|
ai_edge_quantizer/transformations/quant_insert_test.py,sha256=X9ptPDvJCFkR5tejKnD1SlHFGPazQTW-wNNMV9MEAuw,10107
|
45
|
-
ai_edge_quantizer/transformations/quantize_tensor.py,sha256=
|
46
|
-
ai_edge_quantizer/transformations/quantize_tensor_test.py,sha256=
|
45
|
+
ai_edge_quantizer/transformations/quantize_tensor.py,sha256=KsJbvhoyBu3D1G5R4nkl54w0TbdYPyit6JfABwlvtbw,5437
|
46
|
+
ai_edge_quantizer/transformations/quantize_tensor_test.py,sha256=xfbVNdMbvfJXQcl0vPtmyqKhifVxNZlhu_Xq7RLL2NI,7638
|
47
47
|
ai_edge_quantizer/transformations/transformation_utils.py,sha256=BaKy5LYWgqli62XGo3AGRDNtHjwpBNp5VF5XgFbfVmg,4298
|
48
48
|
ai_edge_quantizer/transformations/transformation_utils_test.py,sha256=ks81nNvruOC88Tjdk3_qwku0V8p54p3gOqfObzNhWMM,5371
|
49
49
|
ai_edge_quantizer/utils/__init__.py,sha256=lpq1g2ayg3lCPLy79t2VicYcnGKw64FfYIj1V7J-4m8,676
|
@@ -56,8 +56,8 @@ ai_edge_quantizer/utils/tfl_interpreter_utils.py,sha256=GzrsaL3fkOXN5iPRJv7lqhNI
|
|
56
56
|
ai_edge_quantizer/utils/tfl_interpreter_utils_test.py,sha256=Op3JxtOqlrjzmYF18jnnstL1k9xiY9kKJ8S2vklKGkc,11327
|
57
57
|
ai_edge_quantizer/utils/validation_utils.py,sha256=oYw33Sg547AqtGw-choPUJmp9SAKkV46J_ddqSsum2Q,3950
|
58
58
|
ai_edge_quantizer/utils/validation_utils_test.py,sha256=V_qNDikPD4OPB-siOLQCWNVWTAu87h2IgNYt7teFd-o,2934
|
59
|
-
ai_edge_quantizer_nightly-0.0.1.
|
60
|
-
ai_edge_quantizer_nightly-0.0.1.
|
61
|
-
ai_edge_quantizer_nightly-0.0.1.
|
62
|
-
ai_edge_quantizer_nightly-0.0.1.
|
63
|
-
ai_edge_quantizer_nightly-0.0.1.
|
59
|
+
ai_edge_quantizer_nightly-0.0.1.dev20241124.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
|
60
|
+
ai_edge_quantizer_nightly-0.0.1.dev20241124.dist-info/METADATA,sha256=vwBpgUIxXbDMs0CweYWXOgsDLAi_DXSDc_c7pVl7RCM,1484
|
61
|
+
ai_edge_quantizer_nightly-0.0.1.dev20241124.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
|
62
|
+
ai_edge_quantizer_nightly-0.0.1.dev20241124.dist-info/top_level.txt,sha256=8QTfPnFXNVUhScFLaa-NWZMFWMn72M50DVPubpwWB1g,18
|
63
|
+
ai_edge_quantizer_nightly-0.0.1.dev20241124.dist-info/RECORD,,
|
File without changes
|