Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compile and install the shared library of fluid inference #7572

Merged
merged 15 commits into from
Jan 22, 2018
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ option(WITH_COVERAGE "Compile PaddlePaddle with code coverage" OFF)
option(COVERALLS_UPLOAD "Package code coverage data to coveralls" OFF)
option(ON_TRAVIS "Exclude special unit test on Travis CI" OFF)
option(WITH_C_API "Compile PaddlePaddle with C-API(Prediction)" OFF)
option(WITH_FLUID "Compile PaddlePaddle fluid only" ON)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有一点我不确定,WITH_FLUID这个选项,设置了之后是只编译Fluid,而不是同时编译老Paddle和Fluid?

可能需要在这里注解下,或者把这个描述语句改下,因为这个PR引入WITH_FLUID并没有实现Compile PaddlePaddle fluid only这个功能。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DONE. 已经加了comments并改成“ompile PaddlePaddle fluid only(TODO)”。

WITH_FLUID这个选项,设置了之后是只编译Fluid,而不是同时编译老Paddle和Fluid

是的。关于WITH_FLUID的默认值,可以在后续功能开发完毕后再讨论定为OFF还是ON。

目前使用cc_library这种方式,libwarpctc.so虽然代码中是动态装载的,但是生成库的时候被直接链接了。这其实是一个已知的问题,可以后面的PR再解决

好的,会在下一个PR中解决。

option(WITH_GOLANG "Compile PaddlePaddle with GOLANG" OFF)
option(GLIDE_INSTALL "Download and install go dependencies " ON)
option(USE_NNPACK "Compile PaddlePaddle with NNPACK library" OFF)
Expand Down Expand Up @@ -107,6 +108,10 @@ if (WITH_C_API AND WITH_PYTHON)
"different Python interpreter from compiling.")
endif()

if (WITH_C_API)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about we rename WITH_C_API into PADDLE_BUILD_INFERENCE_LIB ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As WITH_C_API is used for a long time in old PaddlePaddle, users are familiar with it, we may remain it. And for fluid, we will add WITH_FLUID option for both training and inference. (see #7578 (comment))

set(WITH_FLUID OFF CACHE STRING "Disable install fluid when compile the C_API" FORCE)
endif()

if(MOBILE_INFERENCE)
set(THIRD_PARTY_BUILD_TYPE MinSizeRel)
else()
Expand Down
10 changes: 8 additions & 2 deletions cmake/external/eigen.cmake
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
INCLUDE(ExternalProject)

SET(EIGEN_SOURCE_DIR ${THIRD_PARTY_PATH}/eigen3)

INCLUDE_DIRECTORIES(${EIGEN_SOURCE_DIR}/src/extern_eigen3)
SET(EIGEN_INCLUDE_DIR ${EIGEN_SOURCE_DIR}/src/extern_eigen3)
INCLUDE_DIRECTORIES(${EIGEN_INCLUDE_DIR})

ExternalProject_Add(
extern_eigen3
Expand All @@ -28,3 +28,9 @@ endif()
add_dependencies(eigen3 extern_eigen3)

LIST(APPEND external_project_dependencies eigen3)

IF(NOT WITH_C_API AND WITH_FLUID)
INSTALL(FILES ${EIGEN_INCLUDE_DIR}/Eigen/Core DESTINATION third_party/eigen3/Eigen)
INSTALL(DIRECTORY ${EIGEN_INCLUDE_DIR}/Eigen/src DESTINATION third_party/eigen3/Eigen)
INSTALL(DIRECTORY ${EIGEN_INCLUDE_DIR}/unsupported/Eigen DESTINATION third_party/eigen3/unsupported)
ENDIF()
2 changes: 1 addition & 1 deletion cmake/external/gflags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ ADD_DEPENDENCIES(gflags extern_gflags)

LIST(APPEND external_project_dependencies gflags)

IF(WITH_C_API)
IF(WITH_C_API OR WITH_FLUID)
INSTALL(DIRECTORY ${GFLAGS_INCLUDE_DIR} DESTINATION third_party/gflags)
IF(ANDROID)
INSTALL(FILES ${GFLAGS_LIBRARIES} DESTINATION third_party/gflags/lib/${ANDROID_ABI})
Expand Down
2 changes: 1 addition & 1 deletion cmake/external/glog.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ LINK_LIBRARIES(glog gflags)

LIST(APPEND external_project_dependencies glog)

IF(WITH_C_API)
IF(WITH_C_API OR WITH_FLUID)
INSTALL(DIRECTORY ${GLOG_INCLUDE_DIR} DESTINATION third_party/glog)
IF(ANDROID)
INSTALL(FILES ${GLOG_LIBRARIES} DESTINATION third_party/glog/lib/${ANDROID_ABI})
Expand Down
2 changes: 1 addition & 1 deletion cmake/external/protobuf.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ IF(NOT PROTOBUF_FOUND)
SET(PROTOBUF_PROTOC_LIBRARY ${extern_protobuf_PROTOC_LIBRARY}
CACHE FILEPATH "protoc library." FORCE)

IF(WITH_C_API)
IF(WITH_C_API OR WITH_FLUID)
INSTALL(DIRECTORY ${PROTOBUF_INCLUDE_DIR} DESTINATION third_party/protobuf)
IF(ANDROID)
INSTALL(FILES ${PROTOBUF_LITE_LIBRARY} DESTINATION third_party/protobuf/lib/${ANDROID_ABI})
Expand Down
7 changes: 7 additions & 0 deletions paddle/framework/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,10 @@ cc_test(op_kernel_type_test SRCS op_kernel_type_test.cc DEPS place device_contex
cc_test(cow_ptr_tests SRCS details/cow_ptr_test.cc)
nv_test(data_device_transform_test SRCS data_device_transform_test.cu
DEPS operator op_registry init math_function)

if(NOT WITH_C_API AND WITH_FLUID)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have to call CMake's install function in these CMakeLists.txt files?

I am afraid that this would break the Bazel-style of the CMakeLists.txt files, which, was achieved by inventing the generic.cmake file.

Could we not install these header and library files, but leave them in the source directory and the build directory, but let users just build PaddlePaddle and include/link to these header files and library files?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have to call CMake's install function in these CMakeLists.txt files?

Yes, We need to install *.h files for inference package.

I am afraid that this would break the Bazel-style of the CMakeLists.txt files, which, was achieved by inventing the generic.cmake file.

The style of CMake's install function is indeed different from Bazel-style. However, Bazel doesn't have a similar install function. Thus, it's hard for the user to deploy it. For example, building-tensorflow-as-a-standalone-project need to manually copy include and libraries for users.
image

Could we not install these header and library files, but leave them in the source directory and the build directory, but let users just build PaddlePaddle and include/link to these header files and library files?

I think that make install is necessary and will make the deploy much easier.

  • Users can deploy it without building PaddlePaddle. For example like C-API, users can directly download the paddle.tgz in here.
  • For deploy in mobile, if we don't make install, users should manually copy corresponding header files and library files to their project like building-tensorflow-as-a-standalone-project.
  • We will continually build for minimum library size and header files, without make install automatically, users may not get the updated inference package at the first time.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

后面的PR,会将WITH_FLUID加到paddle/CMakeLists.txt里面吧,改成类似这样:

  if(WITH_FLUID)
    if(NOT Boost_FOUND)
      message(FATAL_ERROR "...")
    endif()
    add_subdirectory(string)
    add_subdirectory(memory)
    add_subdirectory(platform)
    add_subdirectory(framework)
    add_subdirectory(operators)
    add_subdirectory(pybind)
    add_subdirectory(inference)
  endif()

那么在paddle/framework/CMakeLists.txt里面就不需要加这个if语句了。

另外,需要install哪些头文件,后续看看能不能精简下。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

那么在paddle/framework/CMakeLists.txt里面就不需要加这个if语句了

是的。#6346 已经考虑加WITH_FLUID选项来编译出只含有fluid的版本。但如果是只含有fluid的话,默认应该是OFF吧?

需要install哪些头文件,后续看看能不能精简下

好的。

file(GLOB FRAMEWORK_HEADERS *.h)
install(FILES ${FRAMEWORK_HEADERS} DESTINATION include/paddle/framework)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/framework.pb.h DESTINATION include/paddle/framework)
install(FILES details/cow_ptr.h details/op_registry.h DESTINATION include/paddle/framework/details)
endif()
26 changes: 21 additions & 5 deletions paddle/inference/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
set(FLUID_CORE_MODULES
backward proto_desc paddle_memory executor prune init ${GLOB_OP_LIB})
set(FLUID_CORE_MODULES proto_desc paddle_memory executor prune init)

cc_library(paddle_fluid_api
SRCS inference.cc
DEPS ${FLUID_CORE_MODULES})
DEPS ${FLUID_CORE_MODULES} ${GLOB_OP_LIB})

# Merge all modules into a simgle static library
cc_library(paddle_fluid DEPS paddle_fluid_api ${FLUID_CORE_MODULES})
# Merge all modules into a single static library
cc_library(paddle_fluid DEPS paddle_fluid_api ${FLUID_CORE_MODULES} ${GLOB_OP_LIB})

# Create shared library
add_library(paddle_fluid_shared SHARED inference.cc)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shared library不能直接叫libpaddle_fluid.so

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

考虑到两点:

  1. 仿照了libpaddle_capi_shared.so
  2. 如果名字叫libpaddle_fluid.so,那第8行的cc_library(paddle_fluid ...)就要换名字了,不然两个库就重名了。

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

一个是静态库,一个是动态库,名字也不能一样?

Copy link
Contributor Author

@luotao1 luotao1 Jan 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

比如,如果按照下面这样写:

add_library(paddle_fluid_shared SHARED inference.cc)
add_library(paddle_fluid_shared STATIC inference.cc)

cmake会报错:

add_library cannot create target "paddle_fluid_shared" because another
  target with the same name already exists.  The existing target is a shared
  library created in source directory

不过能不能先编译出来,然后再在cmake里面改成一样的名字。


target_circle_link_libraries(paddle_fluid_shared
ARCHIVE_START
${GLOB_OP_LIB}
ARCHIVE_END
${FLUID_CORE_MODULES})

SET_TARGET_PROPERTIES(paddle_fluid_shared PROPERTIES OUTPUT_NAME paddle_fluid)

# install library & headers
if(NOT WITH_C_API AND WITH_FLUID)
install(FILES inference.h DESTINATION include/paddle/inference)
install(TARGETS paddle_fluid_shared DESTINATION lib)
endif()

# ptools
# just for testing, we may need to change the storing format for inference_model
Expand Down
7 changes: 7 additions & 0 deletions paddle/memory/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,10 @@ cc_library(paddle_memory
system_allocator)

cc_test(memory_test SRCS memory_test.cc DEPS place paddle_memory)

if(NOT WITH_C_API AND WITH_FLUID)
file(GLOB MEMORY_HEADERS *.h)
file(GLOB MEMORY_DETAIL_HEADERS detail/*.h)
install(FILES ${MEMORY_HEADERS} DESTINATION include/paddle/memory)
install(FILES ${MEMORY_DETAIL_HEADERS} DESTINATION include/paddle/memory/detail)
endif()
8 changes: 8 additions & 0 deletions paddle/platform/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,11 @@ nv_test(nccl_test SRCS nccl_test.cu DEPS dynload_cuda gpu_info device_context)

cc_library(profiler SRCS profiler.cc DEPS device_context)
cc_test(profiler_test SRCS profiler_test.cc DEPS profiler)

if(NOT WITH_C_API AND WITH_FLUID)
file(GLOB PLATFORM_HEADERS *.h)
file(GLOB PLATFORM_dynload_HEADERS dynload/*.h)
install(FILES ${PLATFORM_HEADERS} DESTINATION include/paddle/platform)
install(FILES ${PLATFORM_HEADERS} DESTINATION include/paddle/platform/dynload)
install(FILES details/device_ptr_cast.h DESTINATION include/paddle/platform/details)
endif()
7 changes: 6 additions & 1 deletion paddle/string/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
cc_library(stringpiece SRCS piece.cc)
cc_test(stringpiece_test SRCS piece_test.cc DEPS stringpiece glog gflags)

cc_test(stringprintf_test SRCS printf_test.cc DEPS glog gflags)
cc_test(to_string_test SRCS to_string_test.cc)

if(NOT WITH_C_API AND WITH_FLUID)
file(GLOB STRING_HEADERS *.h)
install(FILES ${STRING_HEADERS} DESTINATION include/paddle/string)
install(FILES tinyformat/tinyformat.h DESTINATION include/paddle/string/tinyformat)
endif()