NOTE(aselle): We are using raw PyObject's here because we want to make sure we input and output bytes rather than unicode strings for Python3.
| 38 | // NOTE(aselle): We are using raw PyObject's here because we want to make |
| 39 | // sure we input and output bytes rather than unicode strings for Python3. |
| 40 | PyObject* TocoConvert(PyObject* model_flags_proto_txt_raw, |
| 41 | PyObject* toco_flags_proto_txt_raw, |
| 42 | PyObject* input_contents_txt_raw, bool extended_return, |
| 43 | PyObject* debug_info_txt_raw, |
| 44 | bool enable_mlir_converter) { |
| 45 | // Use Python C API to validate and convert arguments. In py3 (bytes), |
| 46 | // in py2 (str). |
| 47 | auto ConvertArg = [&](PyObject* obj, bool* error) { |
| 48 | char* buf; |
| 49 | Py_ssize_t len; |
| 50 | if (::tflite::python_utils::ConvertFromPyString(obj, &buf, &len) == -1) { |
| 51 | *error = true; |
| 52 | return std::string(); |
| 53 | } else { |
| 54 | *error = false; |
| 55 | return std::string(buf, len); |
| 56 | } |
| 57 | }; |
| 58 | |
| 59 | bool error; |
| 60 | std::string model_flags_proto_txt = |
| 61 | ConvertArg(model_flags_proto_txt_raw, &error); |
| 62 | if (error) { |
| 63 | PyErr_SetString(PyExc_ValueError, "Model flags are invalid."); |
| 64 | return nullptr; |
| 65 | } |
| 66 | std::string toco_flags_proto_txt = |
| 67 | ConvertArg(toco_flags_proto_txt_raw, &error); |
| 68 | if (error) { |
| 69 | PyErr_SetString(PyExc_ValueError, "Toco flags are invalid."); |
| 70 | return nullptr; |
| 71 | } |
| 72 | std::string input_contents_txt = ConvertArg(input_contents_txt_raw, &error); |
| 73 | if (error) { |
| 74 | PyErr_SetString(PyExc_ValueError, "Input GraphDef is invalid."); |
| 75 | return nullptr; |
| 76 | } |
| 77 | |
| 78 | // Use TOCO to produce new outputs. |
| 79 | toco::ModelFlags model_flags; |
| 80 | if (!model_flags.ParseFromString(model_flags_proto_txt)) { |
| 81 | PyErr_SetString(PyExc_ValueError, |
| 82 | "Failed to convert Model to Python String."); |
| 83 | return nullptr; |
| 84 | } |
| 85 | toco::TocoFlags toco_flags; |
| 86 | if (!toco_flags.ParseFromString(toco_flags_proto_txt)) { |
| 87 | PyErr_SetString(PyExc_ValueError, |
| 88 | "Failed to convert Toco to Python String."); |
| 89 | return nullptr; |
| 90 | } |
| 91 | |
| 92 | tensorflow::GraphDebugInfo debug_info; |
| 93 | if (debug_info_txt_raw && debug_info_txt_raw != Py_None) { |
| 94 | std::string debug_info_txt = ConvertArg(debug_info_txt_raw, &error); |
| 95 | if (error) { |
| 96 | PyErr_SetString(PyExc_ValueError, "Input DebugInfo is invalid."); |
| 97 | return nullptr; |
nothing calls this directly
no test coverage detected