From 137c142341c9f10a1e105403fbfb1123a1802eb9 Mon Sep 17 00:00:00 2001 From: Andrew Selle Date: Tue, 8 Jan 2019 03:57:48 -0800 Subject: [PATCH] Make toco_port trivial initialize-able for test cases. Problem is toco_port indirects a way of initializing google. One impl has a static way of finding out if it is initialized (internal). One impl has a non-static way to find out. In the non-static way, test cases won't work since we initialize the tensorflow google emulation elsewhere (gtest override) PiperOrigin-RevId: 228306710 --- tensorflow/lite/toco/BUILD | 2 ++ tensorflow/lite/toco/import_tensorflow_test.cc | 2 ++ tensorflow/lite/toco/toco_convert_test.cc | 2 ++ tensorflow/lite/toco/toco_port.cc | 7 +++++++ tensorflow/lite/toco/toco_port.h | 4 ++++ tensorflow/lite/toco/toco_port_test.cc | 1 + tensorflow/lite/toco/tooling_util_test.cc | 2 ++ 7 files changed, 20 insertions(+) diff --git a/tensorflow/lite/toco/BUILD b/tensorflow/lite/toco/BUILD index d77234c80a9..40bceedd6a1 100644 --- a/tensorflow/lite/toco/BUILD +++ b/tensorflow/lite/toco/BUILD @@ -342,6 +342,7 @@ tf_cc_test( name = "import_tensorflow_test", srcs = ["import_tensorflow_test.cc"], deps = [ + ":toco_port", ":toco_tooling", "//tensorflow/core:framework", "//tensorflow/core:graph", @@ -386,6 +387,7 @@ tf_cc_test( srcs = ["tooling_util_test.cc"], deps = [ ":model", + ":toco_port", ":tooling_util", "//tensorflow/core:lib", "//tensorflow/lite/testing:util", diff --git a/tensorflow/lite/toco/import_tensorflow_test.cc b/tensorflow/lite/toco/import_tensorflow_test.cc index 70382f546b8..de7f4cdb7e3 100644 --- a/tensorflow/lite/toco/import_tensorflow_test.cc +++ b/tensorflow/lite/toco/import_tensorflow_test.cc @@ -13,6 +13,7 @@ See the License for the specific language governing permissions and limitations under the License. ==============================================================================*/ #include "tensorflow/lite/toco/import_tensorflow.h" +#include "tensorflow/lite/toco/toco_port.h" #include #include @@ -569,5 +570,6 @@ TEST(ImportTest, UnsupportedOpWithMultipleOutputs) { int main(int argc, char** argv) { ::tflite::LogToStderr(); ::testing::InitGoogleTest(&argc, argv); + ::toco::port::InitGoogleWasDoneElsewhere(); return RUN_ALL_TESTS(); } diff --git a/tensorflow/lite/toco/toco_convert_test.cc b/tensorflow/lite/toco/toco_convert_test.cc index 3730c53ae1b..739b924607e 100644 --- a/tensorflow/lite/toco/toco_convert_test.cc +++ b/tensorflow/lite/toco/toco_convert_test.cc @@ -16,6 +16,7 @@ limitations under the License. #include #include #include "tensorflow/lite/testing/util.h" +#include "tensorflow/lite/toco/toco_port.h" namespace toco { namespace { @@ -176,5 +177,6 @@ TEST(TocoTest, TransientStringTensors) { int main(int argc, char** argv) { ::tflite::LogToStderr(); ::testing::InitGoogleTest(&argc, argv); + ::toco::port::InitGoogleWasDoneElsewhere(); return RUN_ALL_TESTS(); } diff --git a/tensorflow/lite/toco/toco_port.cc b/tensorflow/lite/toco/toco_port.cc index fb8c1b8337f..b222032e614 100644 --- a/tensorflow/lite/toco/toco_port.cc +++ b/tensorflow/lite/toco/toco_port.cc @@ -57,6 +57,11 @@ void InitGoogle(const char* usage, int* argc, char*** argv, bool remove_flags) { ::InitGoogle(usage, argc, argv, remove_flags); } +void InitGoogleWasDoneElsewhere() { + // Nothing need be done since ::CheckInitGoogleIsDone() is aware of other + // possible initialization entry points. +} + void CheckInitGoogleIsDone(const char* message) { ::CheckInitGoogleIsDone(message); } @@ -152,6 +157,8 @@ constexpr int kFileWriteFlags = O_CREAT | O_WRONLY; static bool port_initialized = false; +void InitGoogleWasDoneElsewhere() { port_initialized = true; } + void InitGoogle(const char* usage, int* argc, char*** argv, bool remove_flags) { if (!port_initialized) { #if defined(PLATFORM_GOOGLE) diff --git a/tensorflow/lite/toco/toco_port.h b/tensorflow/lite/toco/toco_port.h index 2f39e3d6d5c..231612ecd43 100644 --- a/tensorflow/lite/toco/toco_port.h +++ b/tensorflow/lite/toco/toco_port.h @@ -55,6 +55,10 @@ double round(double x); namespace toco { namespace port { +// Things like tests use other initialization routines that need control +// of flags. However, for testing we still want to use toco_port.h facilities. +// This function sets initialized flag trivially. +void InitGoogleWasDoneElsewhere(); void InitGoogle(const char* usage, int* argc, char*** argv, bool remove_flags); void CheckInitGoogleIsDone(const char* message); diff --git a/tensorflow/lite/toco/toco_port_test.cc b/tensorflow/lite/toco/toco_port_test.cc index d80d423ed74..997da58b8f6 100644 --- a/tensorflow/lite/toco/toco_port_test.cc +++ b/tensorflow/lite/toco/toco_port_test.cc @@ -61,5 +61,6 @@ TEST(TocoPortTest, JoinPath) { int main(int argc, char** argv) { ::tflite::LogToStderr(); ::testing::InitGoogleTest(&argc, argv); + ::toco::port::InitGoogleWasDoneElsewhere(); return RUN_ALL_TESTS(); } diff --git a/tensorflow/lite/toco/tooling_util_test.cc b/tensorflow/lite/toco/tooling_util_test.cc index e44b94b7125..faa6fe412ec 100644 --- a/tensorflow/lite/toco/tooling_util_test.cc +++ b/tensorflow/lite/toco/tooling_util_test.cc @@ -19,6 +19,7 @@ limitations under the License. #include "tensorflow/core/lib/core/status.h" #include "tensorflow/lite/testing/util.h" #include "tensorflow/lite/toco/model.h" +#include "tensorflow/lite/toco/toco_port.h" #include "tensorflow/lite/toco/tooling_util.h" namespace toco { @@ -208,5 +209,6 @@ TEST(FusedActivationTest, DefaultsToUnfused) { int main(int argc, char** argv) { ::tflite::LogToStderr(); ::testing::InitGoogleTest(&argc, argv); + ::toco::port::InitGoogleWasDoneElsewhere(); return RUN_ALL_TESTS(); }