From: A. Unique TensorFlower Date: Fri, 11 May 2018 22:03:34 +0000 (-0700) Subject: Modify the python interface to toco to provide arithmetic operations used by the... X-Git-Tag: upstream/v1.9.0_rc1~116^2^2~85 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=cd9ac6414531a8f7308a7698f0954084443d5120;p=platform%2Fupstream%2Ftensorflow.git Modify the python interface to toco to provide arithmetic operations used by the model. PiperOrigin-RevId: 196314416 --- diff --git a/tensorflow/contrib/lite/toco/model.h b/tensorflow/contrib/lite/toco/model.h index aefa9ac..d878ac5 100644 --- a/tensorflow/contrib/lite/toco/model.h +++ b/tensorflow/contrib/lite/toco/model.h @@ -1829,6 +1829,8 @@ class Model { } const ArrayMap& GetArrayMap() const { return arrays; } + int64 ArithmeticOpsCount() const { return ops_count; } + // Optional arrays are used for optional tensors, // these tensors do not have data, but with reserved names as op inputs. std::set optional_arrays; @@ -1845,6 +1847,8 @@ class Model { std::size_t transient_data_size = 0; // For code-generation only: required alignment of the transient_data buffer std::size_t transient_data_alignment = 0; + // Arithmatic operations performed in the model. + int64 ops_count = 0; private: // The associative array mapping names to Array's. diff --git a/tensorflow/contrib/lite/toco/python/toco.i b/tensorflow/contrib/lite/toco/python/toco.i index 3787cba..0d2fbdd 100644 --- a/tensorflow/contrib/lite/toco/python/toco.i +++ b/tensorflow/contrib/lite/toco/python/toco.i @@ -24,9 +24,12 @@ namespace toco { // Convert a model represented in `input_contents`. `model_flags_proto` // describes model parameters. `toco_flags_proto` describes conversion // parameters (see relevant .protos for more information). Returns a string -// representing the contents of the converted model. +// representing the contents of the converted model. When extended_return +// flag is set to true returns a dictionary that contains string representation +// of the converted model and some statitics like arithmetic ops count. PyObject* TocoConvert(PyObject* model_flags_proto_txt_raw, PyObject* toco_flags_proto_txt_raw, - PyObject* input_contents_txt_raw); + PyObject* input_contents_txt_raw, + bool extended_return = false); } // namespace toco \ No newline at end of file diff --git a/tensorflow/contrib/lite/toco/python/toco_python_api.cc b/tensorflow/contrib/lite/toco/python/toco_python_api.cc index 153c117..5b1db85 100644 --- a/tensorflow/contrib/lite/toco/python/toco_python_api.cc +++ b/tensorflow/contrib/lite/toco/python/toco_python_api.cc @@ -37,7 +37,7 @@ namespace toco { // sure we input and output bytes rather than unicode strings for Python3. PyObject* TocoConvert(PyObject* model_flags_proto_txt_raw, PyObject* toco_flags_proto_txt_raw, - PyObject* input_contents_txt_raw) { + PyObject* input_contents_txt_raw, bool extended_return) { // Use Python C API to validate and convert arguments. In py3 (bytes), // in py2 (str). auto ConvertArg = [&](PyObject* obj, bool* error) { @@ -78,6 +78,16 @@ PyObject* TocoConvert(PyObject* model_flags_proto_txt_raw, Export(toco_flags, *model, toco_flags.allow_custom_ops(), &output_file_contents_txt); + if (extended_return) { + PyObject* dict = PyDict_New(); + PyDict_SetItemString( + dict, "flatbuffer", + TOCO_FROM_CPPSTRING_TO_PY(output_file_contents_txt.data(), + output_file_contents_txt.size())); + PyDict_SetItemString(dict, "arithmetic_ops", + PyLong_FromLong(model->ArithmeticOpsCount())); + return dict; + } // Convert arguments back to byte (py3) or str (py2) return TOCO_FROM_CPPSTRING_TO_PY(output_file_contents_txt.data(), output_file_contents_txt.size()); diff --git a/tensorflow/contrib/lite/toco/python/toco_python_api.h b/tensorflow/contrib/lite/toco/python/toco_python_api.h index dc37835..9af38e9 100644 --- a/tensorflow/contrib/lite/toco/python/toco_python_api.h +++ b/tensorflow/contrib/lite/toco/python/toco_python_api.h @@ -23,10 +23,13 @@ namespace toco { // Convert a model represented in `input_contents`. `model_flags_proto` // describes model parameters. `toco_flags_proto` describes conversion // parameters (see relevant .protos for more information). Returns a string -// representing the contents of the converted model. +// representing the contents of the converted model. When extended_return +// flag is set to true returns a dictionary that contains string representation +// of the converted model and some statitics like arithmetic ops count. PyObject* TocoConvert(PyObject* model_flags_proto_txt_raw, PyObject* toco_flags_proto_txt_raw, - PyObject* input_contents_txt_raw); + PyObject* input_contents_txt_raw, + bool extended_return = false); } // namespace toco diff --git a/tensorflow/contrib/lite/toco/toco_tooling.cc b/tensorflow/contrib/lite/toco/toco_tooling.cc index d894916..b5531ca 100644 --- a/tensorflow/contrib/lite/toco/toco_tooling.cc +++ b/tensorflow/contrib/lite/toco/toco_tooling.cc @@ -373,6 +373,7 @@ void Transform(const TocoFlags& toco_flags, Model* model) { LOG(INFO) << "Estimated count of arithmetic ops: " << 1e-9 * ops_count << " billion (note that a multiply-add is counted as 2 ops)."; } + model->ops_count = ops_count; } void Export(const TocoFlags& toco_flags, const Model& model,