Integrate TensorFlow Lite 1.12 (#2838)
author박종현/On-Device Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Mon, 14 Jan 2019 07:48:55 +0000 (16:48 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Mon, 14 Jan 2019 07:48:55 +0000 (16:48 +0900)
* Integrate TensorFlow Lite 1.12

This commit introduces the following changes to use TensorFlow Lite 1.12
instead of TensorFlow Lite 1.7
- Use the latest Flatbuffers (v1.10)
- Update Flatbuffers Build methodology
- Update nnkit's tensorflow lite support library

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
* Fix a typo (avaialable -> available)

* Remove debug code

* Update status message

CMakeLists.txt
cmake/packages/FlatBuffersConfig.cmake
cmake/packages/FlatBuffersSourceConfig.cmake
contrib/nnkit/libs/support/tflite/CMakeLists.txt

index deea15d..4ab7734 100644 (file)
@@ -6,6 +6,12 @@ enable_testing()
 
 set(CMAKE_CXX_STANDARD 11)
 
+set(NNCC_OVERLAY_DIR "${CMAKE_BINARY_DIR}/overlay" CACHE
+    INTERNAL "Where locally built external dependencies are installed")
+
+# This allows find_package to access configurations installed inside overlay
+list(APPEND CMAKE_PREFIX_PATH "${NNCC_OVERLAY_DIR}")
+
 macro(nncc_include PREFIX)
   include("${CMAKE_SOURCE_DIR}/cmake/modules/${PREFIX}.cmake")
 endmacro(nncc_include)
index 38d949a..9412b4a 100644 (file)
@@ -1,50 +1,66 @@
 function(_FlatBuffers_import)
+  find_package(Flatbuffers QUIET)
+  set(FlatBuffers_FOUND ${Flatbuffers_FOUND} PARENT_SCOPE)
+endfunction(_FlatBuffers_import)
+
+function(_FlatBuffers_build)
   nncc_find_package(FlatBuffersSource QUIET)
 
   if(NOT FlatBuffersSource_FOUND)
-    set(FlatBuffers_FOUND FALSE PARENT_SCOPE)
+    # Source is not available
     return()
   endif(NOT FlatBuffersSource_FOUND)
 
-  # From FlatBuffers's CMakeLists.txt
-  list(APPEND FlatBuffers_Library_SRCS "${FlatBuffersSource_DIR}/src/code_generators.cpp")
-  list(APPEND FlatBuffers_Library_SRCS "${FlatBuffersSource_DIR}/src/idl_parser.cpp")
-  list(APPEND FlatBuffers_Library_SRCS "${FlatBuffersSource_DIR}/src/idl_gen_text.cpp")
-  list(APPEND FlatBuffers_Library_SRCS "${FlatBuffersSource_DIR}/src/reflection.cpp")
-  list(APPEND FlatBuffers_Library_SRCS "${FlatBuffersSource_DIR}/src/util.cpp")
-
-  # From FlatBuffers's CMakeLists.txt
-  list(APPEND FlatBuffers_Compiler_SRCS "${FlatBuffersSource_DIR}/src/idl_gen_cpp.cpp")
-  list(APPEND FlatBuffers_Compiler_SRCS "${FlatBuffersSource_DIR}/src/idl_gen_general.cpp")
-  list(APPEND FlatBuffers_Compiler_SRCS "${FlatBuffersSource_DIR}/src/idl_gen_go.cpp")
-  list(APPEND FlatBuffers_Compiler_SRCS "${FlatBuffersSource_DIR}/src/idl_gen_js.cpp")
-  list(APPEND FlatBuffers_Compiler_SRCS "${FlatBuffersSource_DIR}/src/idl_gen_php.cpp")
-  list(APPEND FlatBuffers_Compiler_SRCS "${FlatBuffersSource_DIR}/src/idl_gen_python.cpp")
-  list(APPEND FlatBuffers_Compiler_SRCS "${FlatBuffersSource_DIR}/src/idl_gen_fbs.cpp")
-  list(APPEND FlatBuffers_Compiler_SRCS "${FlatBuffersSource_DIR}/src/idl_gen_grpc.cpp")
-  list(APPEND FlatBuffers_Compiler_SRCS "${FlatBuffersSource_DIR}/src/idl_gen_json_schema.cpp")
-  list(APPEND FlatBuffers_Compiler_SRCS "${FlatBuffersSource_DIR}/src/flatc.cpp")
-  list(APPEND FlatBuffers_Compiler_SRCS "${FlatBuffersSource_DIR}/src/flatc_main.cpp")
-  list(APPEND FlatBuffers_Compiler_SRCS "${FlatBuffersSource_DIR}/grpc/src/compiler/cpp_generator.cc")
-  list(APPEND FlatBuffers_Compiler_SRCS "${FlatBuffersSource_DIR}/grpc/src/compiler/go_generator.cc")
-
-  if(NOT TARGET flatbuffers)
-    add_library(flatbuffers ${FlatBuffers_Library_SRCS})
-    target_include_directories(flatbuffers PUBLIC "${FlatBuffersSource_DIR}/include")
-  endif(NOT TARGET flatbuffers)
+  # TODO Introduce helper functions
+  set(FLATBUFFERS_BUILD "${CMAKE_BINARY_DIR}/externals/FLATBUFFERS/build")
+  set(FLATBUFFERS_INSTALL "${NNCC_OVERLAY_DIR}")
 
-  if(NOT TARGET flatc)
-    add_executable(flatc ${FlatBuffers_Compiler_SRCS})
-    target_include_directories(flatc PRIVATE "${FlatBuffersSource_DIR}/grpc")
-    target_link_libraries(flatc flatbuffers)
-  endif(NOT TARGET flatc)
-
-  set(FlatBuffers_FOUND TRUE PARENT_SCOPE)
-endfunction(_FlatBuffers_import)
+  set(STAMP_PATH "${FLATBUFFERS_INSTALL}/FLATBUFFERS.stamp")
+  set(LOG_PATH "${FLATBUFFERS_INSTALL}/FLATBUFFERS.log")
 
+  if(EXISTS ${STAMP_PATH})
+    return()
+  endif(EXISTS ${STAMP_PATH})
+
+  message(STATUS "Build Flatbuffers from ${FlatBuffersSource_DIR}")
+
+  file(MAKE_DIRECTORY ${FLATBUFFERS_BUILD})
+  file(MAKE_DIRECTORY ${FLATBUFFERS_INSTALL})
+
+  # NOTE Do NOT retry Flatbuffers build once it fails
+  file(WRITE "${STAMP_PATH}")
+
+  execute_process(COMMAND ${CMAKE_COMMAND}
+                            -DCMAKE_INSTALL_PREFIX=${FLATBUFFERS_INSTALL}
+                            -DCMAKE_BUILD_TYPE=Release
+                            ${FlatBuffersSource_DIR}
+                  OUTPUT_FILE ${LOG_PATH}
+                  ERROR_FILE ${LOG_PATH}
+                  WORKING_DIRECTORY ${FLATBUFFERS_BUILD}
+                  RESULT_VARIABLE BUILD_EXITCODE)
+
+  execute_process(COMMAND ${CMAKE_COMMAND} --build . -- install
+                  OUTPUT_FILE ${LOG_PATH}
+                  ERROR_FILE ${LOG_PATH}
+                  WORKING_DIRECTORY ${FLATBUFFERS_BUILD}
+                  RESULT_VARIABLE INSTALL_EXITCODE)
+
+  if(BUILD_EXITCODE EQUAL 0 AND INSTALL_EXITCODE EQUAL 0)
+    message(STATUS "Succeeded in building Flatbuffers")
+  else()
+    message(STATUS "Fail to build Flatbuffers (check '${LOG_PATH}' for details)")
+  endif(BUILD_EXITCODE EQUAL 0 AND INSTALL_EXITCODE EQUAL 0)
+endfunction(_FlatBuffers_build)
+
+_FlatBuffers_build()
 _FlatBuffers_import()
 
 if(FlatBuffers_FOUND)
+  if(NOT TARGET flatbuffers)
+    add_library(flatbuffers INTERFACE)
+    target_link_libraries(flatbuffers INTERFACE flatbuffers::flatbuffers)
+  endif(NOT TARGET flatbuffers)
+
   function(FlatBuffers_Generate PREFIX OUTPUT_DIR SCHEMA_DIR)
     get_filename_component(abs_output_dir ${OUTPUT_DIR} ABSOLUTE)
     get_filename_component(abs_schema_dir ${SCHEMA_DIR} ABSOLUTE)
@@ -61,11 +77,11 @@ if(FlatBuffers_FOUND)
 
     add_custom_command(OUTPUT ${OUTPUT_FILES}
                        COMMAND ${CMAKE_COMMAND} -E make_directory "${abs_output_dir}"
-                       COMMAND "$<TARGET_FILE:flatc>" -c --no-includes
+                       COMMAND "$<TARGET_FILE:flatbuffers::flatc>" -c --no-includes
                        --no-union-value-namespacing
                        --gen-object-api -o "${abs_output_dir}"
                        ${SCHEMA_FILES}
-                       DEPENDS flatc)
+                       DEPENDS flatbuffers::flatc)
 
     set(${PREFIX}_SOURCES ${OUTPUT_FILES} PARENT_SCOPE)
     set(${PREFIX}_INCLUDE_DIRS ${abs_output_dir} PARENT_SCOPE)
@@ -101,10 +117,10 @@ if(FlatBuffers_FOUND)
     # Generate output files before building TGT
     add_custom_command(TARGET ${TGT} PRE_BUILD
                        COMMAND ${CMAKE_COMMAND} -E make_directory "${abs_output_dir}"
-                       COMMAND "$<TARGET_FILE:flatc>" -c --no-includes
+                       COMMAND "$<TARGET_FILE:flatbuffers::flatc>" -c --no-includes
                        --no-union-value-namespacing
                        --gen-object-api -o "${abs_output_dir}"
                        ${SCHEMA_FILES}
-                       DEPENDS flatc)
+                       DEPENDS flatbuffers::flatc)
   endfunction(FlatBuffers_Target)
 endif(FlatBuffers_FOUND)
index 8546332..63a9ccd 100644 (file)
@@ -7,8 +7,14 @@ function(_FlatBuffersSource_import)
   nncc_include(ExternalSourceTools)
   nncc_include(OptionTools)
 
-  # NOTE TensorFlow 1.7 downloads FlatBuffers from the following URL
-  envoption(FLATBUFFERS_URL https://github.com/google/flatbuffers/archive/971a68110e4fc1bace10fcb6deeb189e7e1a34ce.tar.gz)
+  # Each TensorFlow needs a specific version of Flatbuffers
+  # - TensorFlow 1.7 downloads it from https://github.com/google/flatbuffers/archive/971a68110e4.tar.gz
+  # - TensorFlow 1.12 downloads it from https://github.com/google/flatbuffers/archive/1f5eae5d6a1.tar.gz
+  #
+  # Let's use 1.10 released in 2018.10 (compatible with 1f5eae5d6a1).
+  #
+  # TODO Manage multiple versions
+  envoption(FLATBUFFERS_URL https://github.com/google/flatbuffers/archive/v1.10.0.tar.gz)
 
   ExternalSource_Download(FLATBUFFERS ${FLATBUFFERS_URL})
 
index 928e6c2..061dd9b 100644 (file)
@@ -1,4 +1,4 @@
-nncc_find_package(TensorFlowLite QUIET EXACT 1.7)
+nncc_find_package(TensorFlowLite QUIET EXACT 1.12)
 
 if(NOT TensorFlowLite_FOUND)
   return()
@@ -6,10 +6,10 @@ endif(NOT TensorFlowLite_FOUND)
 
 file(GLOB_RECURSE SOURCES "src/*.cpp")
 
-add_library(nnkit_support_tflite-1.7 STATIC ${SOURCES})
-set_target_properties(nnkit_support_tflite-1.7 PROPERTIES POSITION_INDEPENDENT_CODE ON)
-target_include_directories(nnkit_support_tflite-1.7 PUBLIC include)
-target_link_libraries(nnkit_support_tflite-1.7 nnkit_intf_backend)
-target_link_libraries(nnkit_support_tflite-1.7 tensorflowlite-1.7)
+add_library(nnkit_support_tflite-1.12 STATIC ${SOURCES})
+set_target_properties(nnkit_support_tflite-1.12 PROPERTIES POSITION_INDEPENDENT_CODE ON)
+target_include_directories(nnkit_support_tflite-1.12 PUBLIC include)
+target_link_libraries(nnkit_support_tflite-1.12 nnkit_intf_backend)
+target_link_libraries(nnkit_support_tflite-1.12 tensorflowlite-1.12)
 
-add_library(nnkit_support_tflite ALIAS nnkit_support_tflite-1.7)
+add_library(nnkit_support_tflite ALIAS nnkit_support_tflite-1.12)