1 /* Copyright 2017 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_PYTHON_TOCO_PYTHON_API_H_ 16 #define TENSORFLOW_LITE_TOCO_PYTHON_TOCO_PYTHON_API_H_ 17 18 #include <Python.h> 19 20 #include <string> 21 #include <vector> 22 23 namespace toco { 24 25 // Convert a model represented in `input_contents`. `model_flags_proto` 26 // describes model parameters. `toco_flags_proto` describes conversion 27 // parameters (see relevant .protos for more information). Returns a string 28 // representing the contents of the converted model. When extended_return 29 // flag is set to true returns a dictionary that contains string representation 30 // of the converted model and some statistics like arithmetic ops count. 31 // `debug_info_str` contains the `GraphDebugInfo` proto. When 32 // `enable_mlir_converter` is True, use MLIR-based conversion instead of 33 // TOCO conversion. 34 PyObject* TocoConvert(PyObject* model_flags_proto_txt_raw, 35 PyObject* toco_flags_proto_txt_raw, 36 PyObject* input_contents_txt_raw, 37 bool extended_return = false, 38 PyObject* debug_info_txt_raw = nullptr, 39 bool enable_mlir_converter = false); 40 41 // Quantize the model with calibration data. Throw errors if `fully_quantize` 42 // is specified by the calibration data are not sufficient to quantize the 43 // model. 44 PyObject* MlirQuantizeModel(PyObject* data, bool disable_per_channel, 45 bool fully_quantize, int inference_type, 46 int input_data_type, int output_data_type, 47 bool enable_numeric_verify = false, 48 bool enable_whole_model_verify = false, 49 PyObject* op_denylist = nullptr, 50 PyObject* node_denylist = nullptr); 51 52 // Sparsifies model to encode sparse tensors with proper format. Throws error if 53 // sparsification fails. 54 PyObject* MlirSparsifyModel(PyObject* data); 55 56 // Registers the given custom opdefs to TensorFlow global op registry. 57 PyObject* RegisterCustomOpdefs(PyObject* list); 58 59 // Returns the collected TFLite conversion errors. 60 const std::vector<std::string> RetrieveCollectedErrors(); 61 62 // Returns MLIR string dump of the given Flatbuffer model. 63 std::string FlatBufferFileToMlir(const std::string& model, 64 bool input_is_filepath); 65 66 // All the exported functions should be listed in 67 // tensorflow/tools/def_file_filter/symbols_pybind.txt for the Windows build. 68 } // namespace toco 69 70 #endif // TENSORFLOW_LITE_TOCO_PYTHON_TOCO_PYTHON_API_H_ 71