lite.cmake 8.5 KB
Newer Older
石晓伟 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# Copyright (c) 2019 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.

W
Wilber 已提交
15 16
if(NOT LINUX)
  message("Paddle-lite will not build because the required Linux do not exist.")
石晓伟 已提交
17 18 19 20
  set(WITH_LITE OFF)
  return()
endif()

21
if(LITE_WITH_XPU)
22
  add_definitions(-DLITE_SUBGRAPH_WITH_XPU)
23 24 25 26 27 28 29 30 31 32 33 34 35
  if(WITH_AARCH64)
    set(XPU_SDK_ENV "kylin_aarch64")
  elseif(WITH_SUNWAY)
    set(XPU_SDK_ENV "deepin_sw6_64")
  elseif(WITH_BDCENTOS)
    set(XPU_SDK_ENV "bdcentos_x86_64")
  elseif(WITH_UBUNTU)
    set(XPU_SDK_ENV "ubuntu_x86_64")
  elseif(WITH_CENTOS)
    set(XPU_SDK_ENV "centos7_x86_64")
  else()
    set(XPU_SDK_ENV "ubuntu_x86_64")
  endif()
36 37
endif()

H
hong 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
if(WITH_ARM)
  if(LITE_WITH_XPU)
    set(LITE_OUTPUT_BIN_DIR inference_lite_lib.armlinux.armv8.xpu)
  elseif(LITE_WITH_NNADAPTER)
    message("Enable LITE_WITH_NNADAPTER")
    if(NNADAPTER_WITH_HUAWEI_ASCEND_NPU)
      set(LITE_OUTPUT_BIN_DIR inference_lite_lib.armlinux.armv8.nnadapter)
    endif()
  else()
    set(LITE_OUTPUT_BIN_DIR inference_lite_lib.armlinux.armv8)
  endif()
else()
  set(LITE_OUTPUT_BIN_DIR inference_lite_lib)
endif()

53 54 55
if(LITE_WITH_NNADAPTER)
  add_definitions(-DLITE_SUBGRAPH_WITH_NNADAPTER)
  if(NNADAPTER_WITH_HUAWEI_ASCEND_NPU)
56
    add_definitions(-DLITE_SUBGRAPH_WITH_NPU)
57 58 59
    set(NPU_SDK_ROOT
        "/usr/local/Ascend/ascend-toolkit/latest"
        CACHE STRING "default NPU SDK ROOT")
60 61 62
  endif()
endif()

63
if(NOT LITE_SOURCE_DIR OR NOT LITE_BINARY_DIR)
石晓伟 已提交
64 65
  include(ExternalProject)
  set(LITE_PROJECT extern_lite)
66
  set(LITE_PREFIX_DIR ${THIRD_PARTY_PATH}/lite)
石晓伟 已提交
67
  set(LITE_INSTALL_DIR ${THIRD_PARTY_PATH}/install/lite)
H
hong 已提交
68 69 70 71 72 73
  set(LITE_BINARY_DIR ${LITE_PREFIX_DIR}/src/extern_lite-build)
  set(LITE_SOURCE_DIR ${LITE_PREFIX_DIR}/src/extern_lite)

  set(LITE_SHARED_LIB
      ${LITE_BINARY_DIR}/${LITE_OUTPUT_BIN_DIR}/cxx/lib/libpaddle_full_api_shared.so
  )
石晓伟 已提交
74

75
  if(NOT LITE_GIT_TAG)
Z
zhupengyang 已提交
76
    set(LITE_GIT_TAG d06a1f36ec564fb618d555b342ca1076623d8b94)
77 78
  endif()

79 80 81 82
  if(NOT CUDA_ARCH_NAME)
    set(CUDA_ARCH_NAME "Auto")
  endif()

石晓伟 已提交
83
  # No quotes, so cmake can resolve it as a command with arguments.
W
Wilber 已提交
84
  if(WITH_ARM)
H
hong 已提交
85 86
    set(LITE_BUILD_COMMAND ${CMAKE_COMMAND} --build . --target
                           publish_inference -j)
87
    message(STATUS "BUILD_COMMAND: ${LITE_BUILD_COMMAND}")
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    set(LITE_OPTIONAL_ARGS
        -DWITH_MKL=OFF
        -DLITE_WITH_CUDA=OFF
        -DWITH_MKLDNN=OFF
        -DLITE_WITH_X86=OFF
        -DLITE_WITH_LIGHT_WEIGHT_FRAMEWORK=ON
        -DLITE_WITH_PROFILE=OFF
        -DARM_TARGET_OS=armlinux
        -DWITH_LITE=ON
        -DWITH_PYTHON=OFF
        -DWITH_TESTING=OFF
        -DLITE_BUILD_EXTRA=ON
        -DLITE_WITH_XPU=${LITE_WITH_XPU}
        -DXPU_SDK_URL=${XPU_BASE_URL}
        -DXPU_SDK_ENV=${XPU_SDK_ENV}
        -DLITE_WITH_NNADAPTER=${LITE_WITH_NNADAPTER}
        -DNNADAPTER_WITH_HUAWEI_ASCEND_NPU=${NNADAPTER_WITH_HUAWEI_ASCEND_NPU}
        -DNNADAPTER_HUAWEI_ASCEND_NPU_SDK_ROOT=${NPU_SDK_ROOT}
        -DLITE_WITH_CODE_META_INFO=OFF
        -DLITE_WITH_ARM=ON)
W
Wilber 已提交
108
    ExternalProject_Add(
石晓伟 已提交
109 110
      ${LITE_PROJECT}
      ${EXTERNAL_PROJECT_LOG_ARGS}
111 112 113 114 115 116 117 118 119 120 121 122
      GIT_REPOSITORY "${GIT_URL}/PaddlePaddle/Paddle-Lite.git"
      GIT_TAG ${LITE_GIT_TAG}
      PREFIX ${LITE_PREFIX_DIR}
      PATCH_COMMAND
        mkdir -p ${LITE_PREFIX_DIR}/src/extern_lite-build/lite/gen_code && touch
        ${LITE_PREFIX_DIR}/src/extern_lite-build/lite/gen_code/__generated_code__.cc
        && sed -i "/aarch64-linux-gnu-gcc/d"
        ${LITE_PREFIX_DIR}/src/extern_lite/cmake/os/armlinux.cmake && sed -i
        "/aarch64-linux-gnu-g++/d"
        ${LITE_PREFIX_DIR}/src/extern_lite/cmake/os/armlinux.cmake
      UPDATE_COMMAND ""
      BUILD_COMMAND ${LITE_BUILD_COMMAND}
123
      INSTALL_COMMAND ""
124 125 126 127 128 129 130 131 132 133 134
      CMAKE_ARGS -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
                 -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
                 -DCMAKE_CXX_FLAGS=${LITE_CMAKE_CXX_FLAGS}
                 -DCMAKE_CXX_FLAGS_RELEASE=${CMAKE_CXX_FLAGS_RELEASE}
                 -DCMAKE_CXX_FLAGS_DEBUG=${CMAKE_CXX_FLAGS_DEBUG}
                 -DCMAKE_C_FLAGS=${CMAKE_C_FLAGS}
                 -DCMAKE_C_FLAGS_DEBUG=${CMAKE_C_FLAGS_DEBUG}
                 -DCMAKE_C_FLAGS_RELEASE=${CMAKE_C_FLAGS_RELEASE}
                 -DCMAKE_POSITION_INDEPENDENT_CODE=ON
                 -DCMAKE_BUILD_TYPE=${THIRD_PARTY_BUILD_TYPE}
                 ${EXTERNAL_OPTIONAL_ARGS}
H
hong 已提交
135 136
                 ${LITE_OPTIONAL_ARGS}
      BUILD_BYPRODUCTS ${LITE_SHARED_LIB})
W
Wilber 已提交
137
  else()
H
hong 已提交
138 139
    set(LITE_BUILD_COMMAND ${CMAKE_COMMAND} --build . --target
                           publish_inference -j)
140
    message(STATUS "BUILD_COMMAND: ${LITE_BUILD_COMMAND}")
141 142
    set(LITE_OPTIONAL_ARGS
        -DWITH_MKL=ON
Z
zhupengyang 已提交
143
        -DLITE_WITH_CUDA=OFF
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
        -DWITH_MKLDNN=OFF
        -DLITE_WITH_X86=ON
        -DLITE_WITH_PROFILE=OFF
        -DWITH_LITE=OFF
        -DLITE_WITH_LIGHT_WEIGHT_FRAMEWORK=OFF
        -DWITH_PYTHON=OFF
        -DWITH_TESTING=OFF
        -DLITE_BUILD_EXTRA=ON
        -DLITE_WITH_XPU=${LITE_WITH_XPU}
        -DXPU_SDK_URL=${XPU_BASE_URL}
        -DXPU_SDK_ENV=${XPU_SDK_ENV}
        -DLITE_WITH_NNADAPTER=${LITE_WITH_NNADAPTER}
        -DNNADAPTER_WITH_HUAWEI_ASCEND_NPU=${NNADAPTER_WITH_HUAWEI_ASCEND_NPU}
        -DNNADAPTER_HUAWEI_ASCEND_NPU_SDK_ROOT=${NPU_SDK_ROOT}
        -DLITE_WITH_CODE_META_INFO=OFF
        -DLITE_WITH_ARM=OFF)
W
Wilber 已提交
160 161

    ExternalProject_Add(
162 163 164 165 166 167 168 169 170 171 172
      ${LITE_PROJECT}
      ${EXTERNAL_PROJECT_LOG_ARGS}
      GIT_REPOSITORY "${GIT_URL}/PaddlePaddle/Paddle-Lite.git"
      GIT_TAG ${LITE_GIT_TAG}
      PREFIX ${LITE_PREFIX_DIR}
      UPDATE_COMMAND ""
      PATCH_COMMAND
        sed -i
        "s?NNadapter_bridges_path = os.path.abspath('..')+\"\/lite\/kernels\/nnadapter\/bridges\/paddle_use_bridges.h\"?NNadapter_bridges_path = os.path.abspath(\'..\')+\"\/extern_lite\/lite\/kernels\/nnadapter\/bridges\/paddle_use_bridges.h\"?"
        ${LITE_PREFIX_DIR}/src/extern_lite//lite/tools/cmake_tools/record_supported_kernel_op.py
      BUILD_COMMAND ${LITE_BUILD_COMMAND}
173
      INSTALL_COMMAND ""
174 175 176 177 178 179 180 181 182 183 184
      CMAKE_ARGS -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
                 -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
                 -DCMAKE_CXX_FLAGS=${LITE_CMAKE_CXX_FLAGS}
                 -DCMAKE_CXX_FLAGS_RELEASE=${CMAKE_CXX_FLAGS_RELEASE}
                 -DCMAKE_CXX_FLAGS_DEBUG=${CMAKE_CXX_FLAGS_DEBUG}
                 -DCMAKE_C_FLAGS=${CMAKE_C_FLAGS}
                 -DCMAKE_C_FLAGS_DEBUG=${CMAKE_C_FLAGS_DEBUG}
                 -DCMAKE_C_FLAGS_RELEASE=${CMAKE_C_FLAGS_RELEASE}
                 -DCMAKE_POSITION_INDEPENDENT_CODE=ON
                 -DCMAKE_BUILD_TYPE=${THIRD_PARTY_BUILD_TYPE}
                 ${EXTERNAL_OPTIONAL_ARGS}
H
hong 已提交
185
                 ${LITE_OPTIONAL_ARGS}
R
risemeup1 已提交
186
      CMAKE_GENERATOR "Unix Makefiles"
H
hong 已提交
187
      BUILD_BYPRODUCTS ${LITE_SHARED_LIB})
W
Wilber 已提交
188
  endif()
189 190
endif()

石晓伟 已提交
191 192
message(STATUS "Paddle-lite BINARY_DIR: ${LITE_BINARY_DIR}")
message(STATUS "Paddle-lite SOURCE_DIR: ${LITE_SOURCE_DIR}")
193
include_directories(${LITE_BINARY_DIR}/${LITE_OUTPUT_BIN_DIR}/cxx/include)
194 195 196 197
if(LITE_WITH_XPU)
  include_directories(${LITE_BINARY_DIR}/third_party/install/xpu/xdnn/include/)
  include_directories(${LITE_BINARY_DIR}/third_party/install/xpu/xre/include/)
endif()
石晓伟 已提交
198

199
function(external_lite_libs alias path)
200
  add_library(${alias} SHARED IMPORTED GLOBAL)
201 202
  set_property(TARGET ${alias} PROPERTY IMPORTED_LOCATION ${path})
  if(LITE_PROJECT)
石晓伟 已提交
203 204 205 206
    add_dependencies(${alias} ${LITE_PROJECT})
  endif()
endfunction()

207
external_lite_libs(
H
hong 已提交
208
  lite_full_shared
209 210
  ${LITE_BINARY_DIR}/${LITE_OUTPUT_BIN_DIR}/cxx/lib/libpaddle_full_api_shared.so
)
石晓伟 已提交
211

212 213 214 215 216 217 218 219
if(LITE_WITH_NNADAPTER)
  set(LITE_NNADAPTER_LIB
      ${LITE_BINARY_DIR}/${LITE_OUTPUT_BIN_DIR}/cxx/lib/libnnadapter.so)
  if(NNADAPTER_WITH_HUAWEI_ASCEND_NPU)
    external_lite_libs(
      lite_nnadapter
      ${LITE_BINARY_DIR}/${LITE_OUTPUT_BIN_DIR}/cxx/lib/libnnadapter.so
      ${LITE_BINARY_DIR}/${LITE_OUTPUT_BIN_DIR}/cxx/lib/libhuawei_ascend_npu.so)
H
hong 已提交
220
    set(LITE_DEPS lite_full_shared lite_nnadapter)
221 222 223
    set(LITE_NNADAPTER_NPU_LIB
        ${LITE_BINARY_DIR}/${LITE_OUTPUT_BIN_DIR}/cxx/lib/libhuawei_ascend_npu.so
    )
224 225
  endif()
else()
H
hong 已提交
226
  set(LITE_DEPS lite_full_shared)
227 228
endif()

石晓伟 已提交
229
add_definitions(-DPADDLE_WITH_LITE)
230
add_definitions(-DLITE_WITH_LOG)