litert-quantizer 0.11.0__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.
- litert_quantizer/__init__.py +19 -0
- litert_quantizer/algorithm_manager.py +480 -0
- litert_quantizer/algorithm_manager_api.py +440 -0
- litert_quantizer/algorithm_manager_api_test.py +281 -0
- litert_quantizer/algorithms/__init__.py +15 -0
- litert_quantizer/algorithms/nonlinear_quantize/__init__.py +15 -0
- litert_quantizer/algorithms/nonlinear_quantize/float_casting.py +340 -0
- litert_quantizer/algorithms/nonlinear_quantize/float_casting_test.py +788 -0
- litert_quantizer/algorithms/uniform_quantize/__init__.py +15 -0
- litert_quantizer/algorithms/uniform_quantize/common_quantize.py +1497 -0
- litert_quantizer/algorithms/uniform_quantize/common_quantize_test.py +186 -0
- litert_quantizer/algorithms/uniform_quantize/dequantized_weight_recovery.py +362 -0
- litert_quantizer/algorithms/uniform_quantize/dequantized_weight_recovery_test.py +449 -0
- litert_quantizer/algorithms/uniform_quantize/gptq.py +303 -0
- litert_quantizer/algorithms/uniform_quantize/gptq_test.py +402 -0
- litert_quantizer/algorithms/uniform_quantize/hadamard_rotation.py +500 -0
- litert_quantizer/algorithms/uniform_quantize/hadamard_rotation_test.py +490 -0
- litert_quantizer/algorithms/uniform_quantize/mse.py +128 -0
- litert_quantizer/algorithms/uniform_quantize/mse_test.py +195 -0
- litert_quantizer/algorithms/uniform_quantize/naive_min_max_quantize.py +229 -0
- litert_quantizer/algorithms/uniform_quantize/naive_min_max_quantize_test.py +346 -0
- litert_quantizer/algorithms/uniform_quantize/octav.py +230 -0
- litert_quantizer/algorithms/uniform_quantize/octav_test.py +240 -0
- litert_quantizer/algorithms/uniform_quantize/op_architecture_tests/add_test.py +166 -0
- litert_quantizer/algorithms/uniform_quantize/op_architecture_tests/average_pool_2d_test.py +119 -0
- litert_quantizer/algorithms/uniform_quantize/op_architecture_tests/batch_matmul_test.py +307 -0
- litert_quantizer/algorithms/uniform_quantize/op_architecture_tests/broadcast_to_test.py +101 -0
- litert_quantizer/algorithms/uniform_quantize/op_architecture_tests/concatenation_test.py +117 -0
- litert_quantizer/algorithms/uniform_quantize/op_architecture_tests/conv2d_test.py +214 -0
- litert_quantizer/algorithms/uniform_quantize/op_architecture_tests/conv2d_transpose_test.py +222 -0
- litert_quantizer/algorithms/uniform_quantize/op_architecture_tests/depthwise_conv2d_test.py +209 -0
- litert_quantizer/algorithms/uniform_quantize/op_architecture_tests/div_test.py +99 -0
- litert_quantizer/algorithms/uniform_quantize/op_architecture_tests/dynamic_update_slice_test.py +113 -0
- litert_quantizer/algorithms/uniform_quantize/op_architecture_tests/embedding_lookup_test.py +128 -0
- litert_quantizer/algorithms/uniform_quantize/op_architecture_tests/equal_test.py +100 -0
- litert_quantizer/algorithms/uniform_quantize/op_architecture_tests/fully_connected_test.py +167 -0
- litert_quantizer/algorithms/uniform_quantize/op_architecture_tests/gather_nd_test.py +168 -0
- litert_quantizer/algorithms/uniform_quantize/op_architecture_tests/gather_test.py +101 -0
- litert_quantizer/algorithms/uniform_quantize/op_architecture_tests/gelu_test.py +115 -0
- litert_quantizer/algorithms/uniform_quantize/op_architecture_tests/hard_swish_test.py +96 -0
- litert_quantizer/algorithms/uniform_quantize/op_architecture_tests/input_output_test.py +307 -0
- litert_quantizer/algorithms/uniform_quantize/op_architecture_tests/logistic_test.py +115 -0
- litert_quantizer/algorithms/uniform_quantize/op_architecture_tests/max_pool_2d_test.py +102 -0
- litert_quantizer/algorithms/uniform_quantize/op_architecture_tests/maximum_test.py +100 -0
- litert_quantizer/algorithms/uniform_quantize/op_architecture_tests/mean_test.py +117 -0
- litert_quantizer/algorithms/uniform_quantize/op_architecture_tests/mirror_pad_test.py +101 -0
- litert_quantizer/algorithms/uniform_quantize/op_architecture_tests/mul_test.py +161 -0
- litert_quantizer/algorithms/uniform_quantize/op_architecture_tests/not_equal_test.py +100 -0
- litert_quantizer/algorithms/uniform_quantize/op_architecture_tests/pack_test.py +100 -0
- litert_quantizer/algorithms/uniform_quantize/op_architecture_tests/pad_test.py +104 -0
- litert_quantizer/algorithms/uniform_quantize/op_architecture_tests/padv2_test.py +106 -0
- litert_quantizer/algorithms/uniform_quantize/op_architecture_tests/reduce_min_test.py +101 -0
- litert_quantizer/algorithms/uniform_quantize/op_architecture_tests/relu_test.py +98 -0
- litert_quantizer/algorithms/uniform_quantize/op_architecture_tests/reshape_test.py +118 -0
- litert_quantizer/algorithms/uniform_quantize/op_architecture_tests/resize_bilinear_test.py +104 -0
- litert_quantizer/algorithms/uniform_quantize/op_architecture_tests/resize_nearest_neighbor_test.py +102 -0
- litert_quantizer/algorithms/uniform_quantize/op_architecture_tests/rsqrt_test.py +108 -0
- litert_quantizer/algorithms/uniform_quantize/op_architecture_tests/select_test.py +103 -0
- litert_quantizer/algorithms/uniform_quantize/op_architecture_tests/select_v2_test.py +113 -0
- litert_quantizer/algorithms/uniform_quantize/op_architecture_tests/slice_test.py +113 -0
- litert_quantizer/algorithms/uniform_quantize/op_architecture_tests/softmax_test.py +115 -0
- litert_quantizer/algorithms/uniform_quantize/op_architecture_tests/space_to_depth_test.py +97 -0
- litert_quantizer/algorithms/uniform_quantize/op_architecture_tests/split_test.py +119 -0
- litert_quantizer/algorithms/uniform_quantize/op_architecture_tests/sqrt_test.py +98 -0
- litert_quantizer/algorithms/uniform_quantize/op_architecture_tests/squared_difference_test.py +102 -0
- litert_quantizer/algorithms/uniform_quantize/op_architecture_tests/strided_slice_test.py +117 -0
- litert_quantizer/algorithms/uniform_quantize/op_architecture_tests/sub_test.py +159 -0
- litert_quantizer/algorithms/uniform_quantize/op_architecture_tests/sum_test.py +110 -0
- litert_quantizer/algorithms/uniform_quantize/op_architecture_tests/tanh_test.py +115 -0
- litert_quantizer/algorithms/uniform_quantize/op_architecture_tests/test_utils.py +698 -0
- litert_quantizer/algorithms/uniform_quantize/op_architecture_tests/transpose_test.py +118 -0
- litert_quantizer/algorithms/uniform_quantize/op_architecture_tests/unpack_test.py +100 -0
- litert_quantizer/algorithms/uniform_quantize/oscar.py +682 -0
- litert_quantizer/algorithms/uniform_quantize/oscar_test.py +547 -0
- litert_quantizer/algorithms/uniform_quantize/uniform_quantize_tensor.py +645 -0
- litert_quantizer/algorithms/uniform_quantize/uniform_quantize_tensor_test.py +519 -0
- litert_quantizer/algorithms/utils/__init__.py +15 -0
- litert_quantizer/algorithms/utils/common_utils.py +1304 -0
- litert_quantizer/algorithms/utils/common_utils_test.py +660 -0
- litert_quantizer/calibrator.py +685 -0
- litert_quantizer/calibrator_test.py +699 -0
- litert_quantizer/conftest.py +22 -0
- litert_quantizer/default_policy.py +440 -0
- litert_quantizer/examples/mnist/quantize_toy_model.py +613 -0
- litert_quantizer/litert_quantizer.py +297 -0
- litert_quantizer/litert_quantizer_test.py +182 -0
- litert_quantizer/model_modifier.py +394 -0
- litert_quantizer/model_modifier_test.py +324 -0
- litert_quantizer/model_validator.py +447 -0
- litert_quantizer/model_validator_test.py +438 -0
- litert_quantizer/params_generator.py +565 -0
- litert_quantizer/params_generator_test.py +1166 -0
- litert_quantizer/policies/dummy_config_policy.json +17 -0
- litert_quantizer/policies/example_config_policy.json +174 -0
- litert_quantizer/qtyping.py +709 -0
- litert_quantizer/qtyping_test.py +238 -0
- litert_quantizer/quantizer.py +620 -0
- litert_quantizer/quantizer_test.py +892 -0
- litert_quantizer/recipe.py +397 -0
- litert_quantizer/recipe_manager.py +414 -0
- litert_quantizer/recipe_manager_test.py +1027 -0
- litert_quantizer/recipe_test.py +180 -0
- litert_quantizer/recipes/default_a16w8_recipe.json +25 -0
- litert_quantizer/recipes/default_a8w8_recipe.json +25 -0
- litert_quantizer/recipes/default_af32w4float_recipe.json +19 -0
- litert_quantizer/recipes/default_af32w8float_recipe.json +19 -0
- litert_quantizer/recipes/dynamic_legacy_wi8_afp32_recipe.json +20 -0
- litert_quantizer/recipes/dynamic_wi4_afp32_hadamard_recipe.json +39 -0
- litert_quantizer/recipes/dynamic_wi8_afp32_hadamard_recipe.json +39 -0
- litert_quantizer/recipes/dynamic_wi8_afp32_litertlm_recipe.json +3 -0
- litert_quantizer/recipes/dynamic_wi8_afp32_recipe.json +19 -0
- litert_quantizer/recipes/sample_advanced_usage_recipe.json +47 -0
- litert_quantizer/tests/end_to_end_tests/add_test.py +141 -0
- litert_quantizer/tests/end_to_end_tests/batch_matmul_test.py +113 -0
- litert_quantizer/tests/end_to_end_tests/broadcast_to_test.py +69 -0
- litert_quantizer/tests/end_to_end_tests/composite_test.py +148 -0
- litert_quantizer/tests/end_to_end_tests/concatenation_test.py +96 -0
- litert_quantizer/tests/end_to_end_tests/conv2d_transpose_test.py +199 -0
- litert_quantizer/tests/end_to_end_tests/depthwise_conv2d_test.py +198 -0
- litert_quantizer/tests/end_to_end_tests/div_test.py +69 -0
- litert_quantizer/tests/end_to_end_tests/dynamic_update_slice_test.py +126 -0
- litert_quantizer/tests/end_to_end_tests/embedding_lookup_test.py +153 -0
- litert_quantizer/tests/end_to_end_tests/equal_test.py +68 -0
- litert_quantizer/tests/end_to_end_tests/fully_connected_test.py +473 -0
- litert_quantizer/tests/end_to_end_tests/gather_nd_test.py +96 -0
- litert_quantizer/tests/end_to_end_tests/gather_test.py +67 -0
- litert_quantizer/tests/end_to_end_tests/gelu_test.py +99 -0
- litert_quantizer/tests/end_to_end_tests/hard_swish_test.py +67 -0
- litert_quantizer/tests/end_to_end_tests/input_output_test.py +273 -0
- litert_quantizer/tests/end_to_end_tests/logistic_test.py +94 -0
- litert_quantizer/tests/end_to_end_tests/max_pool_2d_test.py +69 -0
- litert_quantizer/tests/end_to_end_tests/maximum_test.py +69 -0
- litert_quantizer/tests/end_to_end_tests/mean_test.py +96 -0
- litert_quantizer/tests/end_to_end_tests/mirror_pad_test.py +67 -0
- litert_quantizer/tests/end_to_end_tests/mul_test.py +177 -0
- litert_quantizer/tests/end_to_end_tests/not_equal_test.py +67 -0
- litert_quantizer/tests/end_to_end_tests/pack_test.py +68 -0
- litert_quantizer/tests/end_to_end_tests/pad_test.py +74 -0
- litert_quantizer/tests/end_to_end_tests/padv2_test.py +67 -0
- litert_quantizer/tests/end_to_end_tests/reduce_min_test.py +70 -0
- litert_quantizer/tests/end_to_end_tests/relu_test.py +67 -0
- litert_quantizer/tests/end_to_end_tests/resize_bilinear_test.py +69 -0
- litert_quantizer/tests/end_to_end_tests/resize_nearest_neighbor_test.py +70 -0
- litert_quantizer/tests/end_to_end_tests/rsqrt_test.py +98 -0
- litert_quantizer/tests/end_to_end_tests/select_test.py +69 -0
- litert_quantizer/tests/end_to_end_tests/select_v2_test.py +123 -0
- litert_quantizer/tests/end_to_end_tests/slice_test.py +123 -0
- litert_quantizer/tests/end_to_end_tests/space_to_depth_test.py +65 -0
- litert_quantizer/tests/end_to_end_tests/split_test.py +98 -0
- litert_quantizer/tests/end_to_end_tests/sqrt_test.py +70 -0
- litert_quantizer/tests/end_to_end_tests/squared_difference_test.py +75 -0
- litert_quantizer/tests/end_to_end_tests/strided_slice_test.py +96 -0
- litert_quantizer/tests/end_to_end_tests/sub_test.py +141 -0
- litert_quantizer/tests/end_to_end_tests/sum_test.py +129 -0
- litert_quantizer/tests/end_to_end_tests/tanh_test.py +94 -0
- litert_quantizer/tests/end_to_end_tests/transpose_test.py +162 -0
- litert_quantizer/tests/end_to_end_tests/unpack_test.py +70 -0
- litert_quantizer/tests/mnist_test.py +280 -0
- litert_quantizer/tests/padv2_inf_max_pool_2d_test.py +68 -0
- litert_quantizer/tests/shared_buffer_test.py +447 -0
- litert_quantizer/transformation_instruction_generator.py +862 -0
- litert_quantizer/transformation_instruction_generator_test.py +1552 -0
- litert_quantizer/transformation_performer.py +350 -0
- litert_quantizer/transformation_performer_test.py +521 -0
- litert_quantizer/transformations/__init__.py +15 -0
- litert_quantizer/transformations/dequant_insert.py +86 -0
- litert_quantizer/transformations/dequant_insert_test.py +292 -0
- litert_quantizer/transformations/duplicate_buffer.py +46 -0
- litert_quantizer/transformations/duplicate_buffer_test.py +108 -0
- litert_quantizer/transformations/duplicate_tensor.py +62 -0
- litert_quantizer/transformations/duplicate_tensor_test.py +133 -0
- litert_quantizer/transformations/insert_decomposed_hadamard_rotation.py +265 -0
- litert_quantizer/transformations/insert_decomposed_hadamard_rotation_test.py +242 -0
- litert_quantizer/transformations/insert_hadamard_rotation.py +156 -0
- litert_quantizer/transformations/insert_hadamard_rotation_test.py +196 -0
- litert_quantizer/transformations/insert_multiply.py +129 -0
- litert_quantizer/transformations/insert_multiply_test.py +196 -0
- litert_quantizer/transformations/quant_insert.py +98 -0
- litert_quantizer/transformations/quant_insert_test.py +272 -0
- litert_quantizer/transformations/quantize_tensor.py +227 -0
- litert_quantizer/transformations/quantize_tensor_test.py +387 -0
- litert_quantizer/transformations/transformation_utils.py +419 -0
- litert_quantizer/transformations/transformation_utils_test.py +278 -0
- litert_quantizer/utils/__init__.py +15 -0
- litert_quantizer/utils/calibration_utils.py +372 -0
- litert_quantizer/utils/calibration_utils_test.py +218 -0
- litert_quantizer/utils/constrained_ops_utils.py +112 -0
- litert_quantizer/utils/constrained_ops_utils_test.py +56 -0
- litert_quantizer/utils/histogram_utils.py +480 -0
- litert_quantizer/utils/histogram_utils_test.py +299 -0
- litert_quantizer/utils/litertlm_utils.py +283 -0
- litert_quantizer/utils/litertlm_utils_test.py +91 -0
- litert_quantizer/utils/progress_utils.py +168 -0
- litert_quantizer/utils/progress_utils_test.py +134 -0
- litert_quantizer/utils/qsv_utils.py +171 -0
- litert_quantizer/utils/qsv_utils_test.py +245 -0
- litert_quantizer/utils/recipe_utils.py +248 -0
- litert_quantizer/utils/recipe_utils_test.py +126 -0
- litert_quantizer/utils/test_utils.py +248 -0
- litert_quantizer/utils/tfl_flatbuffer_utils.py +417 -0
- litert_quantizer/utils/tfl_flatbuffer_utils_test.py +228 -0
- litert_quantizer/utils/tfl_interpreter_utils.py +475 -0
- litert_quantizer/utils/tfl_interpreter_utils_test.py +436 -0
- litert_quantizer/utils/validation_utils.py +255 -0
- litert_quantizer/utils/validation_utils_test.py +173 -0
- litert_quantizer-0.11.0.dist-info/METADATA +564 -0
- litert_quantizer-0.11.0.dist-info/RECORD +211 -0
- litert_quantizer-0.11.0.dist-info/WHEEL +5 -0
- litert_quantizer-0.11.0.dist-info/entry_points.txt +2 -0
- litert_quantizer-0.11.0.dist-info/licenses/LICENSE +201 -0
- litert_quantizer-0.11.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,440 @@
|
|
|
1
|
+
# Copyright 2024 Google LLC
|
|
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
|
+
"""The Python API for Algorithm Manager of Quantizer."""
|
|
17
|
+
|
|
18
|
+
from collections.abc import MutableMapping, Sequence
|
|
19
|
+
import dataclasses
|
|
20
|
+
from typing import Literal, Protocol, overload
|
|
21
|
+
|
|
22
|
+
import numpy as np
|
|
23
|
+
|
|
24
|
+
from litert_quantizer import qtyping
|
|
25
|
+
from litert_quantizer.algorithms.utils import common_utils
|
|
26
|
+
from litert_quantizer.utils import qsv_utils
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class CheckOpQuantConfigFunc(Protocol):
|
|
30
|
+
"""Type hint and documentation for config checking functions."""
|
|
31
|
+
|
|
32
|
+
def __call__(
|
|
33
|
+
self,
|
|
34
|
+
op_name: qtyping.TFLOperationName,
|
|
35
|
+
op_quant_config: qtyping.OpQuantizationConfig,
|
|
36
|
+
config_check_policy: qtyping.ConfigCheckPolicyDict,
|
|
37
|
+
):
|
|
38
|
+
"""Checks the op quantization config against the given policy.
|
|
39
|
+
|
|
40
|
+
Args:
|
|
41
|
+
op_name: The name of the op.
|
|
42
|
+
op_quant_config: The quantization config for the op.
|
|
43
|
+
config_check_policy: The policy used to check the op quantization config.
|
|
44
|
+
|
|
45
|
+
Raises:
|
|
46
|
+
ValueError: If the op quantization config is invalid.
|
|
47
|
+
"""
|
|
48
|
+
...
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
class InitQSVFunc(Protocol):
|
|
52
|
+
"""Type hint and documentation for QSV initialization functions."""
|
|
53
|
+
|
|
54
|
+
def __call__(
|
|
55
|
+
self,
|
|
56
|
+
op_info: qtyping.OpInfo,
|
|
57
|
+
graph_info: qtyping.GraphInfo,
|
|
58
|
+
inputs_to_ignore: Sequence[int] | None = None,
|
|
59
|
+
outputs_to_ignore: Sequence[int] | None = None,
|
|
60
|
+
**kwargs,
|
|
61
|
+
) -> qtyping.QSV:
|
|
62
|
+
"""Initialize the QSVs for a given Operation.
|
|
63
|
+
|
|
64
|
+
Args:
|
|
65
|
+
op_info: Aggregated information about the op (e.g., quantization config).
|
|
66
|
+
graph_info: Graph information needed to perform quantization for the op.
|
|
67
|
+
inputs_to_ignore: Operand indices to ignore.
|
|
68
|
+
outputs_to_ignore: Result indices to ignore.
|
|
69
|
+
**kwargs: Optional algorithm-specific keyword parameters.
|
|
70
|
+
|
|
71
|
+
Returns:
|
|
72
|
+
The initialized QSVs for the operation.
|
|
73
|
+
"""
|
|
74
|
+
...
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
class UpdateQSVFunc(Protocol):
|
|
78
|
+
"""Type hint and documentation for QSV update functions."""
|
|
79
|
+
|
|
80
|
+
def __call__(
|
|
81
|
+
self, qsv: qtyping.QSV, new_qsv: qtyping.QSV, **kwargs
|
|
82
|
+
) -> qtyping.QSV:
|
|
83
|
+
"""Updates the given QSV.
|
|
84
|
+
|
|
85
|
+
Args:
|
|
86
|
+
qsv: The quantization statistical value of the tensor that need to be
|
|
87
|
+
updated.
|
|
88
|
+
new_qsv: The new QSVs (e.g., from new round of calibration).
|
|
89
|
+
**kwargs: Optional algorithm-specific keyword parameters.
|
|
90
|
+
|
|
91
|
+
Returns:
|
|
92
|
+
The updated QSV for the tensor.
|
|
93
|
+
"""
|
|
94
|
+
...
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
class CalibrationFunc(Protocol):
|
|
98
|
+
"""Type hint and documentation for calibration functions."""
|
|
99
|
+
|
|
100
|
+
def __call__(
|
|
101
|
+
self,
|
|
102
|
+
tfl_op: qtyping.OperatorT,
|
|
103
|
+
graph_info: qtyping.GraphInfo,
|
|
104
|
+
tensor_content_map: MutableMapping[str, np.ndarray],
|
|
105
|
+
inputs_to_ignore: Sequence[int] | None = None,
|
|
106
|
+
outputs_to_ignore: Sequence[int] | None = None,
|
|
107
|
+
**kwargs,
|
|
108
|
+
) -> dict[str, qtyping.QSV]:
|
|
109
|
+
"""Collects quantization statistics variables (QSVs) for the op.
|
|
110
|
+
|
|
111
|
+
Args:
|
|
112
|
+
tfl_op: The tfl operation.
|
|
113
|
+
graph_info: Graph information needed to perform quantization for the op.
|
|
114
|
+
tensor_content_map: A map of tensor name to tensor content.
|
|
115
|
+
inputs_to_ignore: Input tensor indices to ignore.
|
|
116
|
+
outputs_to_ignore: Output tensor indices to ignore.
|
|
117
|
+
**kwargs: Optional algorithm-specific keyword parameters.
|
|
118
|
+
|
|
119
|
+
Returns:
|
|
120
|
+
A dictionary mapping tensor names to the collected QSV.
|
|
121
|
+
"""
|
|
122
|
+
...
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
class MaterializeFunc(Protocol):
|
|
126
|
+
"""Type hint and documentation for materialize functions."""
|
|
127
|
+
|
|
128
|
+
def __call__(
|
|
129
|
+
self,
|
|
130
|
+
op_info: qtyping.OpInfo,
|
|
131
|
+
graph_info: qtyping.GraphInfo,
|
|
132
|
+
tensor_name_to_qsv: MutableMapping[str, qtyping.QSV],
|
|
133
|
+
tensor_quant_params_cache: common_utils.TensorQuantParamsCache,
|
|
134
|
+
**kwargs,
|
|
135
|
+
) -> list[qtyping.TensorTransformationParams]:
|
|
136
|
+
"""Materializes tensors for the given op.
|
|
137
|
+
|
|
138
|
+
Args:
|
|
139
|
+
op_info: Aggregated information about the op (e.g., quantization config).
|
|
140
|
+
graph_info: Graph information needed to perform quantization for the op.
|
|
141
|
+
tensor_name_to_qsv: A map of tensor name to quantization parameters.
|
|
142
|
+
tensor_quant_params_cache: Cache of already computed
|
|
143
|
+
`UniformQuantParams|NonLinearQuantParams` objects keyed on a tuple of
|
|
144
|
+
the buffer ID and the `TensorQuantizationConfig` used to compute it.
|
|
145
|
+
**kwargs: Optional algorithm-specific keyword parameters.
|
|
146
|
+
|
|
147
|
+
Returns:
|
|
148
|
+
A list of `_TensorTransformationParams` for the tensors in the op.
|
|
149
|
+
"""
|
|
150
|
+
...
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
@dataclasses.dataclass
|
|
154
|
+
class QuantizedOperationInfo:
|
|
155
|
+
"""Stores all quantization functions for a given op."""
|
|
156
|
+
|
|
157
|
+
tfl_op_key: qtyping.TFLOperationName
|
|
158
|
+
init_qsv_func: InitQSVFunc
|
|
159
|
+
calibration_func: CalibrationFunc
|
|
160
|
+
materialize_func: MaterializeFunc
|
|
161
|
+
update_qsv_func: UpdateQSVFunc = qsv_utils.moving_average_update # pyrefly: ignore[bad-assignment]
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
@dataclasses.dataclass
|
|
165
|
+
class QuantizationAlgorithmInfo:
|
|
166
|
+
quantization_algorithm: str
|
|
167
|
+
quantized_ops: dict[qtyping.TFLOperationName, QuantizedOperationInfo]
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
class AlgorithmManagerApi:
|
|
171
|
+
"""Quantizer API client to manage quantization configs and functions."""
|
|
172
|
+
|
|
173
|
+
def __init__(self):
|
|
174
|
+
self._algorithm_registry: dict[str, QuantizationAlgorithmInfo] = dict()
|
|
175
|
+
# Check if an op quantization config is supported for a given algorithm.
|
|
176
|
+
self._config_check_registry: dict[str, CheckOpQuantConfigFunc] = dict()
|
|
177
|
+
# Policy to check if an op quantization config is supported for a given
|
|
178
|
+
# algorithm.
|
|
179
|
+
self._config_check_policy_registry: dict[
|
|
180
|
+
str, qtyping.ConfigCheckPolicyDict | None
|
|
181
|
+
] = dict()
|
|
182
|
+
|
|
183
|
+
def register_op_quant_config_validation_func(
|
|
184
|
+
self,
|
|
185
|
+
algorithm_key: str,
|
|
186
|
+
config_check_func: CheckOpQuantConfigFunc,
|
|
187
|
+
):
|
|
188
|
+
"""Register functions to check if an op quantization config is supported."""
|
|
189
|
+
self._config_check_registry[algorithm_key] = config_check_func
|
|
190
|
+
|
|
191
|
+
def register_quantized_op(
|
|
192
|
+
self,
|
|
193
|
+
algorithm_key: str,
|
|
194
|
+
tfl_op_name: qtyping.TFLOperationName,
|
|
195
|
+
init_qsv_func: InitQSVFunc,
|
|
196
|
+
calibration_func: CalibrationFunc,
|
|
197
|
+
materialize_func: MaterializeFunc,
|
|
198
|
+
update_qsv_func: UpdateQSVFunc = qsv_utils.moving_average_update, # pyrefly: ignore[bad-function-definition]
|
|
199
|
+
):
|
|
200
|
+
"""Register functions to support a quantization operation.
|
|
201
|
+
|
|
202
|
+
This function registers the relevant information to support the quantized
|
|
203
|
+
version of given tfl_operation, for the algorithm specified by
|
|
204
|
+
quantization_algorithm.
|
|
205
|
+
|
|
206
|
+
Args:
|
|
207
|
+
algorithm_key: Quantization algorithm keyword for which the quantized
|
|
208
|
+
operation is for.
|
|
209
|
+
tfl_op_name: TFLite op name.
|
|
210
|
+
init_qsv_func: QSV init function to be called.
|
|
211
|
+
calibration_func: Quantized operation to be called during calibration.
|
|
212
|
+
materialize_func: Quantized operation to be called during materialization.
|
|
213
|
+
update_qsv_func: QSV update function to be called during calibration.
|
|
214
|
+
"""
|
|
215
|
+
quantized_algorithm_info = self._algorithm_registry.setdefault(
|
|
216
|
+
algorithm_key, QuantizationAlgorithmInfo(algorithm_key, dict())
|
|
217
|
+
)
|
|
218
|
+
|
|
219
|
+
quantized_algorithm_info.quantized_ops[tfl_op_name] = (
|
|
220
|
+
QuantizedOperationInfo(
|
|
221
|
+
tfl_op_name,
|
|
222
|
+
init_qsv_func,
|
|
223
|
+
calibration_func,
|
|
224
|
+
materialize_func,
|
|
225
|
+
update_qsv_func,
|
|
226
|
+
)
|
|
227
|
+
)
|
|
228
|
+
|
|
229
|
+
def is_op_registered(
|
|
230
|
+
self,
|
|
231
|
+
quantization_algorithm: str,
|
|
232
|
+
tfl_op_name: qtyping.TFLOperationName,
|
|
233
|
+
) -> bool:
|
|
234
|
+
"""Check if the given key for quantization is valid.
|
|
235
|
+
|
|
236
|
+
Args:
|
|
237
|
+
quantization_algorithm: Target quantization algorithm.
|
|
238
|
+
tfl_op_name: TFL operation name.
|
|
239
|
+
|
|
240
|
+
Returns:
|
|
241
|
+
True if the given op is registered for the given algorithm, false
|
|
242
|
+
otherwise.
|
|
243
|
+
"""
|
|
244
|
+
if not self.is_algorithm_registered(quantization_algorithm):
|
|
245
|
+
return False
|
|
246
|
+
|
|
247
|
+
return (
|
|
248
|
+
tfl_op_name
|
|
249
|
+
in self._algorithm_registry[quantization_algorithm].quantized_ops
|
|
250
|
+
)
|
|
251
|
+
|
|
252
|
+
def is_algorithm_registered(self, quantization_algorithm: str) -> bool:
|
|
253
|
+
"""Check if the given algorithm is registered.
|
|
254
|
+
|
|
255
|
+
Args:
|
|
256
|
+
quantization_algorithm: Target quantization algorithm.
|
|
257
|
+
|
|
258
|
+
Returns:
|
|
259
|
+
True if the given algorithm is registered, false otherwise.
|
|
260
|
+
"""
|
|
261
|
+
return quantization_algorithm in self._algorithm_registry
|
|
262
|
+
|
|
263
|
+
def check_op_quantization_config(
|
|
264
|
+
self,
|
|
265
|
+
quantization_algorithm: str,
|
|
266
|
+
tfl_op_name: qtyping.TFLOperationName,
|
|
267
|
+
op_quantization_config: qtyping.OpQuantizationConfig,
|
|
268
|
+
) -> None:
|
|
269
|
+
"""Checks if the given op quantization config is valid.
|
|
270
|
+
|
|
271
|
+
Args:
|
|
272
|
+
quantization_algorithm: Target quantization algorithm.
|
|
273
|
+
tfl_op_name: TFL operation name.
|
|
274
|
+
op_quantization_config: Op quantization config to be checked.
|
|
275
|
+
|
|
276
|
+
Raises:
|
|
277
|
+
ValueError if the given op is not registered for the given algorithm, or
|
|
278
|
+
the given algorithm is not registered.
|
|
279
|
+
"""
|
|
280
|
+
if op_quantization_config.skip_checks:
|
|
281
|
+
return
|
|
282
|
+
if not self.is_op_registered(quantization_algorithm, tfl_op_name):
|
|
283
|
+
raise ValueError(
|
|
284
|
+
f"Unsupported operation {tfl_op_name} for Algorithm:"
|
|
285
|
+
f" {quantization_algorithm}."
|
|
286
|
+
)
|
|
287
|
+
if quantization_algorithm not in self._config_check_registry:
|
|
288
|
+
raise ValueError(
|
|
289
|
+
f"Config checking function for algorithm {quantization_algorithm} is"
|
|
290
|
+
" not registered. Please use"
|
|
291
|
+
" `register_op_quant_config_validation_func` to register the"
|
|
292
|
+
" validation function."
|
|
293
|
+
)
|
|
294
|
+
self._config_check_registry[quantization_algorithm](
|
|
295
|
+
tfl_op_name,
|
|
296
|
+
op_quantization_config,
|
|
297
|
+
self._config_check_policy_registry[quantization_algorithm], # pyrefly: ignore[bad-argument-type]
|
|
298
|
+
)
|
|
299
|
+
|
|
300
|
+
def get_supported_ops(self, alg_key: str) -> list[qtyping.TFLOperationName]:
|
|
301
|
+
"""Returns the list of supported ops for the given algorithm.
|
|
302
|
+
|
|
303
|
+
Args:
|
|
304
|
+
alg_key: Algorithm key.
|
|
305
|
+
|
|
306
|
+
Returns:
|
|
307
|
+
The list of supported JAX operations.
|
|
308
|
+
|
|
309
|
+
Raises:
|
|
310
|
+
ValueError if the alg_key is not registered.
|
|
311
|
+
"""
|
|
312
|
+
if alg_key not in self._algorithm_registry:
|
|
313
|
+
raise ValueError(f"Unregistered algorithm: {alg_key}")
|
|
314
|
+
|
|
315
|
+
return list(self._algorithm_registry[alg_key].quantized_ops.keys())
|
|
316
|
+
|
|
317
|
+
@overload
|
|
318
|
+
def get_quantization_func(
|
|
319
|
+
self,
|
|
320
|
+
algorithm_key: str,
|
|
321
|
+
tfl_op_name: qtyping.TFLOperationName,
|
|
322
|
+
quantize_mode: Literal[qtyping.QuantizeMode.CALIBRATE],
|
|
323
|
+
) -> CalibrationFunc:
|
|
324
|
+
...
|
|
325
|
+
|
|
326
|
+
@overload
|
|
327
|
+
def get_quantization_func(
|
|
328
|
+
self,
|
|
329
|
+
algorithm_key: str,
|
|
330
|
+
tfl_op_name: qtyping.TFLOperationName,
|
|
331
|
+
quantize_mode: Literal[qtyping.QuantizeMode.MATERIALIZE],
|
|
332
|
+
) -> MaterializeFunc:
|
|
333
|
+
...
|
|
334
|
+
|
|
335
|
+
def get_quantization_func(
|
|
336
|
+
self,
|
|
337
|
+
algorithm_key: str,
|
|
338
|
+
tfl_op_name: qtyping.TFLOperationName,
|
|
339
|
+
quantize_mode: qtyping.QuantizeMode,
|
|
340
|
+
) -> CalibrationFunc | MaterializeFunc:
|
|
341
|
+
"""Gets the quantization function.
|
|
342
|
+
|
|
343
|
+
Args:
|
|
344
|
+
algorithm_key: Target quantization algorithm key (e.g.,
|
|
345
|
+
AlgorithmName.MIN_MAX_UNIFORM_QUANT).
|
|
346
|
+
tfl_op_name: TFLite op name.
|
|
347
|
+
quantize_mode: Quantization mode to be used.
|
|
348
|
+
|
|
349
|
+
Returns:
|
|
350
|
+
A quantized operation (function) corresponds to the requested algorithm
|
|
351
|
+
for the TFL op.
|
|
352
|
+
"""
|
|
353
|
+
if not self.is_op_registered(algorithm_key, tfl_op_name):
|
|
354
|
+
raise ValueError(
|
|
355
|
+
f"Unsupported operation {tfl_op_name} for Algorithm: {algorithm_key}."
|
|
356
|
+
f" Supported ops for algorithm {algorithm_key}:"
|
|
357
|
+
f" {self.get_supported_ops(algorithm_key)}"
|
|
358
|
+
)
|
|
359
|
+
|
|
360
|
+
quantized_algorithm_info = self._algorithm_registry[algorithm_key]
|
|
361
|
+
quantized_op_info = quantized_algorithm_info.quantized_ops
|
|
362
|
+
quantized_func = self._get_target_func(
|
|
363
|
+
quantized_op_info, tfl_op_name, quantize_mode
|
|
364
|
+
)
|
|
365
|
+
if quantized_func is None:
|
|
366
|
+
raise ValueError(
|
|
367
|
+
"Cannot retrieve appropriate quantization function for"
|
|
368
|
+
f" {tfl_op_name} for algorithm {algorithm_key} under quantization"
|
|
369
|
+
f" mode {quantize_mode}. Check if the op is registed in"
|
|
370
|
+
" algorithm_manager."
|
|
371
|
+
)
|
|
372
|
+
|
|
373
|
+
return quantized_func
|
|
374
|
+
|
|
375
|
+
def get_update_qsv_func(
|
|
376
|
+
self,
|
|
377
|
+
algorithm_key: str,
|
|
378
|
+
tfl_op_name: qtyping.TFLOperationName,
|
|
379
|
+
) -> UpdateQSVFunc:
|
|
380
|
+
"""Gets the QSV update function for a given algorithm."""
|
|
381
|
+
quantized_algorithm_info = self._algorithm_registry[algorithm_key]
|
|
382
|
+
quantized_op_info = quantized_algorithm_info.quantized_ops
|
|
383
|
+
if update_qsv_func := quantized_op_info[tfl_op_name].update_qsv_func:
|
|
384
|
+
return update_qsv_func
|
|
385
|
+
else:
|
|
386
|
+
raise ValueError(
|
|
387
|
+
f"Unsupported operation {tfl_op_name} for Algorithm: {algorithm_key}."
|
|
388
|
+
f" Supported ops for algorithm {algorithm_key}:"
|
|
389
|
+
f" {self.get_supported_ops(algorithm_key)}"
|
|
390
|
+
)
|
|
391
|
+
|
|
392
|
+
def get_init_qsv_func(
|
|
393
|
+
self,
|
|
394
|
+
algorithm_key: str,
|
|
395
|
+
tfl_op_name: qtyping.TFLOperationName,
|
|
396
|
+
) -> InitQSVFunc:
|
|
397
|
+
"""Gets the initial Quantization Statistics Variable function for a given op.
|
|
398
|
+
|
|
399
|
+
Args:
|
|
400
|
+
algorithm_key: Quantization algorithm to search.
|
|
401
|
+
tfl_op_name: Target TFL operation.
|
|
402
|
+
|
|
403
|
+
Returns:
|
|
404
|
+
A function for qsv initialization.
|
|
405
|
+
"""
|
|
406
|
+
|
|
407
|
+
if not self.is_op_registered(algorithm_key, tfl_op_name):
|
|
408
|
+
raise ValueError(
|
|
409
|
+
f"Unsupported operation {tfl_op_name} for Algorithm: {algorithm_key}."
|
|
410
|
+
f" Supported ops for algorithm {algorithm_key}:"
|
|
411
|
+
f" {self.get_supported_ops(algorithm_key)}"
|
|
412
|
+
)
|
|
413
|
+
quantized_algorithm_info = self._algorithm_registry[algorithm_key]
|
|
414
|
+
quantized_op_info = quantized_algorithm_info.quantized_ops
|
|
415
|
+
|
|
416
|
+
return quantized_op_info[tfl_op_name].init_qsv_func
|
|
417
|
+
|
|
418
|
+
def _get_target_func(
|
|
419
|
+
self,
|
|
420
|
+
quantized_op_info,
|
|
421
|
+
tfl_op_name: qtyping.TFLOperationName,
|
|
422
|
+
quantize_mode: qtyping.QuantizeMode,
|
|
423
|
+
) -> CalibrationFunc | MaterializeFunc:
|
|
424
|
+
"""Gets the function corresponding to the given JAX quantization phase and op."""
|
|
425
|
+
match quantize_mode:
|
|
426
|
+
case qtyping.QuantizeMode.CALIBRATE:
|
|
427
|
+
return quantized_op_info[tfl_op_name].calibration_func
|
|
428
|
+
case qtyping.QuantizeMode.MATERIALIZE:
|
|
429
|
+
return quantized_op_info[tfl_op_name].materialize_func
|
|
430
|
+
|
|
431
|
+
# TODO: b/53780772 - Merge this function with
|
|
432
|
+
# register_op_quant_config_validation_func after full transition to new check
|
|
433
|
+
# mechanism.
|
|
434
|
+
def register_config_check_policy(
|
|
435
|
+
self,
|
|
436
|
+
algorithm_key: str,
|
|
437
|
+
config_check_policy: qtyping.ConfigCheckPolicyDict | None,
|
|
438
|
+
):
|
|
439
|
+
"""Registers a policy to check the op quantization config."""
|
|
440
|
+
self._config_check_policy_registry[algorithm_key] = config_check_policy
|