|
| 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 | +import os |
| 17 | + |
| 18 | +from absl.testing import parameterized |
| 19 | +import numpy as np |
| 20 | + |
| 21 | +from tensorflow.python.platform import googletest |
| 22 | +from ai_edge_quantizer import qtyping |
| 23 | +from ai_edge_quantizer.algorithms.uniform_quantize import common_quantize |
| 24 | +from ai_edge_quantizer.algorithms.uniform_quantize import naive_min_max_quantize |
| 25 | +from ai_edge_quantizer.algorithms.uniform_quantize import octav |
| 26 | +from ai_edge_quantizer.algorithms.uniform_quantize.op_architecture_tests import test_utils as op_test_utils |
| 27 | +from ai_edge_quantizer.utils import test_utils |
| 28 | +from ai_edge_quantizer.utils import tfl_flatbuffer_utils |
| 29 | + |
| 30 | + |
| 31 | +_TEST_DATA_PREFIX_PATH = test_utils.get_path_to_datafile( |
| 32 | + "../../../tests/models" |
| 33 | +) |
| 34 | + |
| 35 | + |
| 36 | +class SelectTest(op_test_utils.BaseQuantizeTest): |
| 37 | + |
| 38 | + def setUp(self): |
| 39 | + super().setUp() |
| 40 | + np.random.seed(666) |
| 41 | + self._test_model_path = os.path.join( |
| 42 | + _TEST_DATA_PREFIX_PATH, "single_select.tflite" |
| 43 | + ) |
| 44 | + self._op_test_info = op_test_utils.OpTestInfo( |
| 45 | + test_model=tfl_flatbuffer_utils.read_model(self._test_model_path), |
| 46 | + op_tensor_names={}, |
| 47 | + input_range=(np.array([[-10]]), np.array([[10]])), |
| 48 | + output_range=(np.array([[-10]]), np.array([[10]])), |
| 49 | + ) |
| 50 | + # The test model has one subgraph for now. |
| 51 | + self._graph_info = qtyping.GraphInfo( |
| 52 | + subgraph_tensors=self._op_test_info.test_model.subgraphs[0].tensors, |
| 53 | + buffers=self._op_test_info.test_model.buffers, |
| 54 | + ) |
| 55 | + |
| 56 | + @parameterized.parameters( |
| 57 | + # get_tensor_quant_params_func, activations_num_bits, symmetric |
| 58 | + (naive_min_max_quantize.get_tensor_quant_params, 8, True), |
| 59 | + (naive_min_max_quantize.get_tensor_quant_params, 8, False), |
| 60 | + (naive_min_max_quantize.get_tensor_quant_params, 16, True), |
| 61 | + (octav.get_tensor_quant_params, 8, True), |
| 62 | + (octav.get_tensor_quant_params, 16, True), |
| 63 | + ) |
| 64 | + def test_materialize_select_succeeds( |
| 65 | + self, get_tensor_quant_params_func, activations_num_bits, symmetric |
| 66 | + ): |
| 67 | + activation_config = test_utils.get_static_activation_quant_setting( |
| 68 | + activations_num_bits, symmetric |
| 69 | + ) |
| 70 | + op_quant_config = test_utils.get_static_op_quant_config(activation_config) |
| 71 | + |
| 72 | + # Read from Model Explorer. |
| 73 | + subgraph0 = self._op_test_info.test_model.subgraphs[0] |
| 74 | + subgraph_op_id = 0 |
| 75 | + op = subgraph0.operators[subgraph_op_id] |
| 76 | + op_info = qtyping.OpInfo( |
| 77 | + op=op, |
| 78 | + op_name=qtyping.TFLOperationName.SELECT, |
| 79 | + subgraph_op_index=subgraph_op_id, |
| 80 | + op_quant_config=op_quant_config, |
| 81 | + ) |
| 82 | + |
| 83 | + # Test settings. |
| 84 | + op_tensor_names = {} |
| 85 | + op_tensor_names["input"] = "serving_default_condition:0" |
| 86 | + op_tensor_names["input2"] = "serving_default_x:0" |
| 87 | + op_tensor_names["input3"] = "serving_default_y:0" |
| 88 | + op_tensor_names["output"] = "PartitionedCall:0" |
| 89 | + self._op_test_info.op_tensor_names = op_tensor_names |
| 90 | + self._test_no_weights_op( |
| 91 | + op_info, |
| 92 | + self._graph_info, |
| 93 | + self._op_test_info, |
| 94 | + common_quantize.materialize_select, |
| 95 | + get_tensor_quant_params_func, |
| 96 | + same_input_output_params=True, |
| 97 | + inputs_to_ignore=[0], # Condition tensor does not need to be quantized. |
| 98 | + ) |
| 99 | + |
| 100 | + |
| 101 | +if __name__ == "__main__": |
| 102 | + googletest.main() |
0 commit comments