xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/toco/graph_transformations/quantization_util.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2018 The TensorFlow Authors. 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 #ifndef TENSORFLOW_LITE_TOCO_GRAPH_TRANSFORMATIONS_QUANTIZATION_UTIL_H_
16 #define TENSORFLOW_LITE_TOCO_GRAPH_TRANSFORMATIONS_QUANTIZATION_UTIL_H_
17 
18 #include <string>
19 
20 #include "tensorflow/lite/kernels/internal/quantization_util.h"
21 #include "tensorflow/lite/toco/graph_transformations/graph_transformations.h"
22 #include "tensorflow/lite/toco/model.h"
23 
24 namespace toco {
25 
26 // Gets the target quantized data type of an array based on the fake quant op.
27 // For example, if the num_bits is 8 the data type will be kUint8.
28 bool InferQuantizedDataTypeFromFakeQuant(
29     const FakeQuantOperator& op, ArrayDataType* out_quantized_data_type);
30 
31 // Gets the min/max numerical range for the given quantized data type.
32 // For example, kUint8 will return [0,255].
33 // Returns true if the ranges were set and false if the type is not quantized.
34 bool GetQuantizedDataTypeNumericalRange(ArrayDataType data_type,
35                                         double* out_min_value,
36                                         double* out_max_value);
37 
38 // Returns the quantized data type of an array, falling back to the provided
39 // default data type.
40 ArrayDataType GetQuantizedDataType(const Array& array,
41                                    ArrayDataType default_type);
42 
43 // Chooses the quantization params for a given array and a given target
44 // quantized data type (which may not be the array's current data type).
45 void ChooseQuantizationParamsForArrayAndQuantizedDataType(
46     const Array& array, ArrayDataType quantized_data_type,
47     QuantizationParams* quantization_params);
48 
49 // Quantizes an array by setting its data type and (if constant) quantizing
50 // all values in the array.
51 void QuantizeArray(GraphTransformation* transformation, Model* model,
52                    const std::string& name, ArrayDataType quantized_data_type,
53                    const QuantizationParams& quantization_params);
54 
55 // Returns true if the given array, when quantized, contains only values between
56 // the provided clamp min/max.
57 // Either clamp_min or clamp_max may be +/-infinity to indicate that the value
58 // is unbounded on that side.
59 bool IsArrayQuantizedRangeSubset(GraphTransformation* transformation,
60                                  const Array& array, double clamp_min,
61                                  double clamp_max);
62 
63 }  // namespace toco
64 
65 #endif  // TENSORFLOW_LITE_TOCO_GRAPH_TRANSFORMATIONS_QUANTIZATION_UTIL_H_
66