inference_lib.cmake 9.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

L
Luo Tao 已提交
15
# make package for paddle fluid shared and static library
16 17

set(COPY_SCRIPT_DIR ${PADDLE_SOURCE_DIR}/cmake)
L
Luo Tao 已提交
18 19 20 21 22
function(copy TARGET)
    set(options "")
    set(oneValueArgs "")
    set(multiValueArgs SRCS DSTS DEPS)
    cmake_parse_arguments(copy_lib "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
23
    set(fluid_lib_dist_dep ${TARGET} ${fluid_lib_dist_dep} PARENT_SCOPE)
L
Luo Tao 已提交
24 25 26

    list(LENGTH copy_lib_SRCS copy_lib_SRCS_len)
    list(LENGTH copy_lib_DSTS copy_lib_DSTS_len)
27
    if (NOT ${copy_lib_SRCS_len} EQUAL ${copy_lib_DSTS_len})
L
Luo Tao 已提交
28
        message(FATAL_ERROR "${TARGET} source numbers are not equal to destination numbers")
29
    endif ()
L
Luo Tao 已提交
30 31
    math(EXPR len "${copy_lib_SRCS_len} - 1")
    add_custom_target(${TARGET} DEPENDS ${copy_lib_DEPS})
32
    foreach (index RANGE ${len})
L
Luo Tao 已提交
33 34
        list(GET copy_lib_SRCS ${index} src)
        list(GET copy_lib_DSTS ${index} dst)
35 36 37 38 39 40
        if (WIN32)   #windows
            file(TO_NATIVE_PATH ${src} native_src)
            file(TO_NATIVE_PATH ${dst} native_dst)
            add_custom_command(TARGET ${TARGET} POST_BUILD
                    COMMAND ${PYTHON_EXECUTABLE} ${COPY_SCRIPT_DIR}/copyfile.py ${native_src} ${native_dst})
        else (WIN32) #not windows
41 42 43 44 45 46
            add_custom_command(TARGET ${TARGET} PRE_BUILD
                    COMMAND mkdir -p "${dst}"
                    COMMAND cp -r "${src}" "${dst}"
                    COMMENT "copying ${src} -> ${dst}")
        endif (WIN32) # not windows
    endforeach ()
L
Luo Tao 已提交
47 48
endfunction()

L
Luo Tao 已提交
49
# third party
50
set(dst_dir "${FLUID_INSTALL_DIR}/third_party/eigen3")
L
Luo Tao 已提交
51
copy(eigen3_lib
52 53 54 55
        SRCS ${EIGEN_INCLUDE_DIR}/Eigen/Core ${EIGEN_INCLUDE_DIR}/Eigen/src ${EIGEN_INCLUDE_DIR}/unsupported/Eigen
        DSTS ${dst_dir}/Eigen ${dst_dir}/Eigen ${dst_dir}/unsupported
        DEPS eigen3
        )
L
Luo Tao 已提交
56

57
set(dst_dir "${FLUID_INSTALL_DIR}/third_party/install/gflags")
L
Luo Tao 已提交
58
copy(gflags_lib
59 60 61 62
        SRCS ${GFLAGS_INCLUDE_DIR} ${GFLAGS_LIBRARIES}
        DSTS ${dst_dir} ${dst_dir}/lib
        DEPS gflags
        )
L
Luo Tao 已提交
63

64
set(dst_dir "${FLUID_INSTALL_DIR}/third_party/install/glog")
L
Luo Tao 已提交
65
copy(glog_lib
66 67 68 69
        SRCS ${GLOG_INCLUDE_DIR} ${GLOG_LIBRARIES}
        DSTS ${dst_dir} ${dst_dir}/lib
        DEPS glog
        )
L
Luo Tao 已提交
70

71
set(dst_dir "${FLUID_INSTALL_DIR}/third_party/boost/")
Q
qiaolongfei 已提交
72
copy(boost_lib
73 74 75 76
        SRCS ${BOOST_INCLUDE_DIR}/boost
        DSTS ${dst_dir}
        DEPS boost
        )
Q
qiaolongfei 已提交
77

78 79
set(dst_dir "${FLUID_INSTALL_DIR}/third_party/install/xxhash")
copy(xxhash_lib
80 81 82 83
        SRCS ${XXHASH_INCLUDE_DIR} ${XXHASH_LIBRARIES}
        DSTS ${dst_dir} ${dst_dir}/lib
        DEPS xxhash
        )
84

P
peizhilin 已提交
85
if (NOT PROTOBUF_FOUND OR WIN32)
86
    set(dst_dir "${FLUID_INSTALL_DIR}/third_party/install/protobuf")
L
Luo Tao 已提交
87
    copy(protobuf_lib
88 89 90 91 92
            SRCS ${PROTOBUF_INCLUDE_DIR} ${PROTOBUF_LIBRARY}
            DSTS ${dst_dir} ${dst_dir}/lib
            DEPS extern_protobuf
            )
endif ()
93

P
peizhilin 已提交
94
if (WITH_MKLML)
95
    set(dst_dir "${FLUID_INSTALL_DIR}/third_party/install/mklml")
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    if(WIN32)
        copy(mklml_lib
                SRCS ${MKLML_LIB} ${MKLML_IOMP_LIB} ${MKLML_SHARED_LIB}
                    ${MKLML_SHARED_LIB_DEPS} ${MKLML_SHARED_IOMP_LIB} ${MKLML_INC_DIR}
                DSTS ${dst_dir}/lib ${dst_dir}/lib ${dst_dir}/lib
                    ${dst_dir}/lib ${dst_dir}/lib ${dst_dir}
                DEPS mklml
                )
    else()
        copy(mklml_lib
                SRCS ${MKLML_LIB} ${MKLML_IOMP_LIB} ${MKLML_INC_DIR}
                DSTS ${dst_dir}/lib ${dst_dir}/lib ${dst_dir}
                DEPS mklml
                )
    endif()
P
peizhilin 已提交
111 112 113 114 115 116 117
elseif (NOT CBLAS_FOUND OR WIN32)
    set(dst_dir "${FLUID_INSTALL_DIR}/third_party/install/openblas")
    copy(openblas_lib
            SRCS ${CBLAS_INSTALL_DIR}/lib ${CBLAS_INSTALL_DIR}/include
            DSTS ${dst_dir} ${dst_dir}
            DEPS extern_openblas
            )
118 119 120 121
endif ()

if (WITH_MKLDNN)
    set(dst_dir "${FLUID_INSTALL_DIR}/third_party/install/mkldnn")
122 123 124 125 126 127 128 129 130 131 132 133 134
    if(WIN32)
        copy(mkldnn_lib
                SRCS ${MKLDNN_INC_DIR} ${MKLDNN_SHARED_LIB} ${MKLDNN_LIB}
                DSTS ${dst_dir} ${dst_dir}/lib ${dst_dir}/lib
                DEPS mkldnn_shared_lib
                )
    else()
        copy(mkldnn_lib
                SRCS ${MKLDNN_INC_DIR} ${MKLDNN_SHARED_LIB}
                DSTS ${dst_dir} ${dst_dir}/lib
                DEPS mkldnn_shared_lib
                )
    endif()
135
endif ()
Q
qiaolongfei 已提交
136

137 138 139 140 141 142 143 144 145
if (WITH_NGRAPH)
    set(dst_dir "${FLUID_INSTALL_DIR}/third_party/install/ngraph")
    copy(ngraph_lib
            SRCS ${NGRAPH_INC_DIR} ${NGRAPH_LIB_DIR}
            DSTS ${dst_dir} ${dst_dir}
            DEPS ngraph
            )
endif ()

T
Tao Luo 已提交
146 147 148 149 150
set(dst_dir "${FLUID_INSTALL_DIR}/third_party/install/snappy")
copy(snappy_lib
        SRCS ${SNAPPY_INCLUDE_DIR} ${SNAPPY_LIBRARIES}
        DSTS ${dst_dir} ${dst_dir}/lib
        DEPS snappy)
151

T
Tao Luo 已提交
152 153 154 155 156
set(dst_dir "${FLUID_INSTALL_DIR}/third_party/install/snappystream")
copy(snappystream_lib
        SRCS ${SNAPPYSTREAM_INCLUDE_DIR} ${SNAPPYSTREAM_LIBRARIES}
        DSTS ${dst_dir} ${dst_dir}/lib
        DEPS snappystream)
157

T
Tao Luo 已提交
158 159 160 161 162
set(dst_dir "${FLUID_INSTALL_DIR}/third_party/install/zlib")
copy(zlib_lib
        SRCS ${ZLIB_INCLUDE_DIR} ${ZLIB_LIBRARIES}
        DSTS ${dst_dir} ${dst_dir}/lib
        DEPS zlib)
163

L
Luo Tao 已提交
164
# paddle fluid module
165
set(src_dir "${PADDLE_SOURCE_DIR}/paddle/fluid")
166
set(dst_dir "${FLUID_INSTALL_DIR}/paddle/fluid")
L
Luo Tao 已提交
167
set(module "framework")
168
set(framework_lib_deps framework_py_proto)
169

L
luotao1 已提交
170
copy(framework_lib DEPS ${framework_lib_deps}
171
        SRCS ${src_dir}/${module}/*.h ${src_dir}/${module}/details/*.h ${PADDLE_BINARY_DIR}/paddle/fluid/framework/framework.pb.h ${PADDLE_BINARY_DIR}/paddle/fluid/framework/data_feed.pb.h ${src_dir}/${module}/ir/memory_optimize_pass/*.h
172
        ${src_dir}/${module}/ir/*.h ${src_dir}/${module}/fleet/*.h
173
        DSTS ${dst_dir}/${module} ${dst_dir}/${module}/details ${dst_dir}/${module} ${dst_dir}/${module} ${dst_dir}/${module}/ir/memory_optimize_pass ${dst_dir}/${module}/ir ${dst_dir}/${module}/fleet
174
        )
L
Luo Tao 已提交
175

L
Luo Tao 已提交
176 177
set(module "memory")
copy(memory_lib
W
Wojciech Uss 已提交
178 179
        SRCS ${src_dir}/${module}/*.h ${src_dir}/${module}/detail/*.h ${src_dir}/${module}/allocation/*.h
        DSTS ${dst_dir}/${module} ${dst_dir}/${module}/detail ${dst_dir}/${module}/allocation
180
        )
L
Luo Tao 已提交
181

Y
Yan Chunwei 已提交
182
set(inference_deps paddle_fluid_shared paddle_fluid)
L
Luo Tao 已提交
183

184
set(module "inference/api")
185

N
nhzlx 已提交
186 187
if (TENSORRT_FOUND)
    copy(tensorrt_lib DEPS ${inference_deps} 
188
        SRCS ${TENSORRT_ROOT}/include/Nv*.h ${TENSORRT_ROOT}/lib/*nvinfer*
N
nhzlx 已提交
189 190 191
        DSTS ${FLUID_INSTALL_DIR}/third_party/install/tensorrt/include ${FLUID_INSTALL_DIR}/third_party/install/tensorrt/lib)
endif ()

192 193 194 195 196
if (ANAKIN_FOUND)
    copy(anakin_lib DEPS ${inference_deps} 
        SRCS ${ANAKIN_ROOT}/*
        DSTS ${FLUID_INSTALL_DIR}/third_party/install/anakin)
endif ()
N
nhzlx 已提交
197

Y
Yan Chunwei 已提交
198
set(module "inference")
P
peizhilin 已提交
199 200 201 202 203
if(WIN32)
    set(paddle_fluid_lib ${PADDLE_BINARY_DIR}/paddle/fluid/inference/${CMAKE_BUILD_TYPE}/libpaddle_fluid.*)
else(WIN32)
    set(paddle_fluid_lib ${PADDLE_BINARY_DIR}/paddle/fluid/inference/libpaddle_fluid.*)
endif(WIN32)
Y
Yan Chunwei 已提交
204
copy(inference_lib DEPS ${inference_deps}
P
peizhilin 已提交
205
  SRCS ${src_dir}/${module}/*.h ${paddle_fluid_lib}
206
       ${src_dir}/${module}/api/paddle_*.h
T
Tao Luo 已提交
207
  DSTS ${dst_dir}/${module} ${dst_dir}/${module} ${dst_dir}/${module}
P
peizhilin 已提交
208
        )
Y
Yan Chunwei 已提交
209

L
Luo Tao 已提交
210
set(module "platform")
X
Xin Pan 已提交
211
copy(platform_lib DEPS profiler_py_proto
212 213 214
        SRCS ${src_dir}/${module}/*.h ${src_dir}/${module}/dynload/*.h ${src_dir}/${module}/details/*.h
        DSTS ${dst_dir}/${module} ${dst_dir}/${module}/dynload ${dst_dir}/${module}/details
        )
L
Luo Tao 已提交
215

L
Luo Tao 已提交
216 217
set(module "string")
copy(string_lib
218 219 220
        SRCS ${src_dir}/${module}/*.h ${src_dir}/${module}/tinyformat/*.h
        DSTS ${dst_dir}/${module} ${dst_dir}/${module}/tinyformat
        )
L
Luo Tao 已提交
221

222 223
set(module "pybind")
copy(pybind_lib
224 225 226
        SRCS ${CMAKE_CURRENT_BINARY_DIR}/paddle/fluid/${module}/pybind.h
        DSTS ${dst_dir}/${module}
        )
227

228 229
# CMakeCache Info
copy(cmake_cache
230 231
        SRCS ${CMAKE_CURRENT_BINARY_DIR}/CMakeCache.txt
        DSTS ${FLUID_INSTALL_DIR})
232

233
# This command generates a complete fluid library for both train and inference
234
add_custom_target(fluid_lib_dist DEPENDS ${fluid_lib_dist_dep})
235

T
Tao Luo 已提交
236 237 238
# Following commands generate a inference-only fluid library
# third_party, version.txt and CMakeCache.txt are the same position with ${FLUID_INSTALL_DIR}
copy(third_party DEPS fluid_lib_dist
239 240 241
        SRCS ${FLUID_INSTALL_DIR}/third_party ${FLUID_INSTALL_DIR}/CMakeCache.txt
        DSTS ${FLUID_INFERENCE_INSTALL_DIR} ${FLUID_INFERENCE_INSTALL_DIR}
        )
T
Tao Luo 已提交
242

243
# only need libpaddle_fluid.so/a and paddle_*.h for inference-only library
T
Tao Luo 已提交
244
copy(inference_api_lib DEPS fluid_lib_dist
P
peizhilin 已提交
245
  SRCS ${paddle_fluid_lib}
246
       ${FLUID_INSTALL_DIR}/paddle/fluid/inference/paddle_*.h
T
Tao Luo 已提交
247 248 249 250 251
  DSTS ${FLUID_INFERENCE_INSTALL_DIR}/paddle/lib ${FLUID_INFERENCE_INSTALL_DIR}/paddle/include
)

add_custom_target(inference_lib_dist DEPENDS third_party inference_api_lib)

252
# paddle fluid version
T
Tao Luo 已提交
253
function(version version_file)
254 255 256 257 258 259 260 261 262 263 264 265 266 267
    execute_process(
            COMMAND ${GIT_EXECUTABLE} log --pretty=format:%H -1
            WORKING_DIRECTORY ${PADDLE_SOURCE_DIR}
            OUTPUT_VARIABLE PADDLE_GIT_COMMIT)
    file(WRITE ${version_file}
            "GIT COMMIT ID: ${PADDLE_GIT_COMMIT}\n"
            "WITH_MKL: ${WITH_MKL}\n"
            "WITH_MKLDNN: ${WITH_MKLDNN}\n"
            "WITH_GPU: ${WITH_GPU}\n")
    if (WITH_GPU)
        file(APPEND ${version_file}
                "CUDA version: ${CUDA_VERSION}\n"
                "CUDNN version: v${CUDNN_MAJOR_VERSION}\n")
    endif ()
T
Tao Luo 已提交
268 269 270
endfunction()
version(${FLUID_INSTALL_DIR}/version.txt)
version(${FLUID_INFERENCE_INSTALL_DIR}/version.txt)