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 std::vector<int32_t>& shape_signature, 38 const TensorType& type, std::unique_ptr<TensorT>* tensor); 39 40 // Create a new TensorT object with quantization parameters. 41 void MakeTensorWithQuantParam(const string& name, 42 const std::vector<int32_t>& shape, 43 const std::vector<int32_t>& shape_signature, 44 const TensorType& type, float scale, 45 int64_t zero_point, 46 std::unique_ptr<TensorT>* tensor); 47 48 // Checks if the tensor has scale and zero point populated. 49 bool QuantizationParametersExist(const TensorT* tensor); 50 51 bool HasBuffer(const ModelT* model, const SubGraphT* subgraph, 52 int tensor_index); 53 54 bool IsQuantized(const SubGraphT* subgraph, int tensor_index); 55 56 bool HasMinMax(const TensorT* tensor); 57 58 // Set version of OperatorCode. The version will only be applied for operations 59 // that have been quantized. 60 void SetOperatorCodeVersion(ModelT* model); 61 62 // Writes model buffer to file. 63 void WriteFile(const std::string& out_file, const uint8_t* bytes, 64 size_t num_bytes); 65 66 // Finishes model buffer and returns its builder. 67 std::unique_ptr<flatbuffers::FlatBufferBuilder> FinishModel( 68 const tflite::ModelT* model); 69 70 // Reads TensorFlow Lite model from the given path. 71 std::unique_ptr<tflite::ModelT> CreateMutableModelFromFile( 72 const string& model_filepath); 73 74 } // namespace utils 75 } // namespace optimize 76 } // namespace tflite 77 78 #endif // TENSORFLOW_LITE_TOOLS_OPTIMIZE_MODEL_UTILS_H_ 79