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 #include <string>
16 #include <vector>
17 #include "tensorflow/core/platform/logging.h"
18
19 #include "tensorflow/lite/python/interpreter_wrapper/python_utils.h"
20 #include "tensorflow/lite/toco/model_flags.pb.h"
21 #include "tensorflow/lite/toco/python/toco_python_api.h"
22 #include "tensorflow/lite/toco/toco_flags.pb.h"
23 #include "tensorflow/lite/toco/toco_graphviz_dump_options.h"
24 #include "tensorflow/lite/toco/toco_port.h"
25 #include "tensorflow/lite/toco/toco_tooling.h"
26 #include "tensorflow/lite/toco/toco_types.h"
27
28 namespace toco {
29
30 // NOTE(aselle): We are using raw PyObject's here because we want to make
31 // sure we input and output bytes rather than unicode strings for Python3.
TocoConvert(PyObject * model_flags_proto_txt_raw,PyObject * toco_flags_proto_txt_raw,PyObject * input_contents_txt_raw,bool extended_return)32 PyObject* TocoConvert(PyObject* model_flags_proto_txt_raw,
33 PyObject* toco_flags_proto_txt_raw,
34 PyObject* input_contents_txt_raw, bool extended_return) {
35 // Use Python C API to validate and convert arguments. In py3 (bytes),
36 // in py2 (str).
37 auto ConvertArg = [&](PyObject* obj, bool* error) {
38 char* buf;
39 Py_ssize_t len;
40 if (::tflite::python_utils::ConvertFromPyString(obj, &buf, &len) == -1) {
41 *error = true;
42 return std::string();
43 } else {
44 *error = false;
45 return std::string(buf, len);
46 }
47 };
48
49 bool error;
50 std::string model_flags_proto_txt =
51 ConvertArg(model_flags_proto_txt_raw, &error);
52 if (error) return nullptr;
53 std::string toco_flags_proto_txt =
54 ConvertArg(toco_flags_proto_txt_raw, &error);
55 if (error) return nullptr;
56 std::string input_contents_txt = ConvertArg(input_contents_txt_raw, &error);
57 if (error) return nullptr;
58
59 // Use TOCO to produce new outputs.
60 toco::ModelFlags model_flags;
61 if (!model_flags.ParseFromString(model_flags_proto_txt)) {
62 LOG(FATAL) << "Model proto failed to parse." << std::endl;
63 }
64 toco::TocoFlags toco_flags;
65 if (!toco_flags.ParseFromString(toco_flags_proto_txt)) {
66 LOG(FATAL) << "Toco proto failed to parse." << std::endl;
67 }
68
69 auto& dump_options = *GraphVizDumpOptions::singleton();
70 if (toco_flags.has_dump_graphviz_dir()) {
71 dump_options.dump_graphviz = toco_flags.dump_graphviz_dir();
72 }
73 if (toco_flags.has_dump_graphviz_include_video()) {
74 dump_options.dump_graphviz_video = toco_flags.dump_graphviz_include_video();
75 }
76
77 // Convert model.
78 std::unique_ptr<toco::Model> model =
79 toco::Import(toco_flags, model_flags, input_contents_txt);
80 toco::Transform(toco_flags, model.get());
81 string output_file_contents_txt;
82 auto status = Export(toco_flags, *model, toco_flags.allow_custom_ops(),
83 &output_file_contents_txt);
84 if (!status.ok()) {
85 PyErr_SetString(PyExc_Exception, status.error_message().c_str());
86 return nullptr;
87 }
88 if (extended_return) {
89 PyObject* dict = PyDict_New();
90 PyDict_SetItemString(
91 dict, "flatbuffer",
92 ::tflite::python_utils::ConvertToPyString(
93 output_file_contents_txt.data(), output_file_contents_txt.size()));
94 PyDict_SetItemString(dict, "arithmetic_ops",
95 PyLong_FromLong(model->ArithmeticOpsCount()));
96 return dict;
97 }
98 // Convert arguments back to byte (py3) or str (py2)
99 return ::tflite::python_utils::ConvertToPyString(
100 output_file_contents_txt.data(), output_file_contents_txt.size());
101 }
102
103 } // namespace toco
104