xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/tools/optimize/quantize_model.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_TOOLS_OPTIMIZE_QUANTIZE_MODEL_H_
16 #define TENSORFLOW_LITE_TOOLS_OPTIMIZE_QUANTIZE_MODEL_H_
17 
18 #include <memory>
19 #include <unordered_set>
20 
21 #include "tensorflow/lite/context.h"
22 #include "tensorflow/lite/core/api/error_reporter.h"
23 #include "tensorflow/lite/model.h"
24 #include "tensorflow/lite/schema/schema_generated.h"
25 #include "tensorflow/lite/util.h"
26 
27 namespace tflite {
28 namespace optimize {
29 
30 // Quantizes input_model and populates the provided builder with the new model.
31 // input_model is required to have min/max information populated in its
32 // quantization params.
33 //
34 // Inputs and output types default to float instead of a quantized type.
35 //
36 // Note: This is a private API, subject to change.
37 TfLiteStatus QuantizeModel(flatbuffers::FlatBufferBuilder* builder,
38                            ModelT* input_model, ErrorReporter* error_reporter);
39 
40 // Same as above, but the types of quantized inputs and outputs are
41 // configurable.
42 //
43 // Note: This is a private API, subject to change.
44 TfLiteStatus QuantizeModel(flatbuffers::FlatBufferBuilder* builder,
45                            ModelT* input_model, const TensorType& input_type,
46                            const TensorType& output_type,
47                            ErrorReporter* error_reporter);
48 
49 // Same as above, but can enable allowing float intermediate operations for ops
50 // that do not yet support quantizable.
51 //
52 // Note: This is a private API, subject to change.
53 TfLiteStatus QuantizeModel(flatbuffers::FlatBufferBuilder* builder,
54                            ModelT* input_model, const TensorType& input_type,
55                            const TensorType& output_type, bool allow_float,
56                            ErrorReporter* error_reporter);
57 
58 // Same as above, but enables only quantizing an allowlist of operations,
59 // specified by their operator output name.
60 //
61 // Note: This is a private API, subject to change.
62 TfLiteStatus QuantizeModel(flatbuffers::FlatBufferBuilder* builder,
63                            ModelT* input_model, const TensorType& input_type,
64                            const TensorType& output_type, bool allow_float,
65                            const std::unordered_set<string>& operator_names,
66                            ErrorReporter* error_reporter);
67 
68 // Same as above, but enables to provide activation type, which
69 // could be TensorType_INT16 or TensorType_INT8.
70 //
71 // Note: This is a private API, subject to change.
72 TfLiteStatus QuantizeModel(flatbuffers::FlatBufferBuilder* builder,
73                            ModelT* model, const TensorType& input_type,
74                            const TensorType& output_type, bool allow_float,
75                            const std::unordered_set<string>& operator_names,
76                            const TensorType& activations_type,
77                            const TensorType& bias_type,
78                            ErrorReporter* error_reporter);
79 
80 // Same as above, but all operators supporting quantization are quantized.
81 //
82 // Note: This is a private API, subject to change.
83 TfLiteStatus QuantizeModelAllOperators(
84     flatbuffers::FlatBufferBuilder* builder, ModelT* model,
85     const TensorType& input_type, const TensorType& output_type,
86     bool allow_float, const TensorType& activations_type,
87     const TensorType& bias_type, ErrorReporter* error_reporter);
88 
89 // Same as above, but allows disabling per channel quantization.
90 //
91 // Note: This is a private API, subject to change.
92 TfLiteStatus QuantizeModelAllOperators(
93     flatbuffers::FlatBufferBuilder* builder, ModelT* model,
94     const TensorType& input_type, const TensorType& output_type,
95     bool allow_float, const TensorType& activations_type,
96     const TensorType& bias_type, bool disable_per_channel,
97     ErrorReporter* error_reporter);
98 
99 // Quantizes input_model and populates the provided builder with the new model
100 // with all possible input parameters including disabling per_channel
101 // quantization.
102 //
103 // All functions above call this function underneath.
104 TfLiteStatus QuantizeModel(flatbuffers::FlatBufferBuilder* builder,
105                            ModelT* model, const TensorType& input_type,
106                            const TensorType& output_type, bool allow_float,
107                            const std::unordered_set<string>& operator_names,
108                            const TensorType& activations_type,
109                            const TensorType& bias_type,
110                            bool disable_per_channel,
111                            ErrorReporter* error_reporter);
112 
113 }  // namespace optimize
114 }  // namespace tflite
115 
116 #endif  // TENSORFLOW_LITE_TOOLS_OPTIMIZE_QUANTIZE_MODEL_H_
117