1 /* Copyright 2019 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_MODEL_UTILS_H_ 16 #define TENSORFLOW_LITE_TOOLS_OPTIMIZE_MODEL_UTILS_H_ 17 18 #include "absl/memory/memory.h" 19 #include "tensorflow/lite/kernels/internal/types.h" 20 #include "tensorflow/lite/model.h" 21 #include "tensorflow/lite/schema/schema_generated.h" 22 23 namespace tflite { 24 namespace optimize { 25 namespace utils { 26 27 // Creates a Dequantize OperatorT object. 28 void MakeDequantizeOperator(ModelT* model, std::unique_ptr<OperatorT>* op, 29 int32_t input, int32_t output); 30 31 // Creates a Quantize OperatorT object. 32 void MakeQuantizeOperator(ModelT* model, std::unique_ptr<OperatorT>* op, 33 int32_t input, int32_t output); 34 35 // Create a new TensorT object without quantization parameters. 36 void MakeTensor(const string& name, const std::vector<int32_t>& shape, 37 const TensorType& type, std::unique_ptr<TensorT>* tensor); 38 39 // Create a new TensorT object with quantization parameters. 40 void MakeTensorWithQuantParam(const string& name, 41 const std::vector<int32_t>& shape, 42 const TensorType& type, float scale, 43 int64_t zero_point, 44 std::unique_ptr<TensorT>* tensor); 45 46 // Checks if the tensor has scale and zero point populated. 47 bool QuantizationParametersExist(const TensorT* tensor); 48 49 bool HasBuffer(const ModelT* model, const SubGraphT* subgraph, 50 int tensor_index); 51 52 bool IsQuantized(const SubGraphT* subgraph, int tensor_index); 53 54 bool HasMinMax(const TensorT* tensor); 55 56 // Set version of OperatorCode. The version will only be applied for operations 57 // that have been quantized. 58 void SetOperatorCodeVersion(ModelT* model); 59 60 } // namespace utils 61 } // namespace optimize 62 } // namespace tflite 63 64 #endif // TENSORFLOW_LITE_TOOLS_OPTIMIZE_MODEL_UTILS_H_ 65