third_party.cmake 17.3 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.

15
include(ExternalProject)
16
# Creat a target named "third_party", which can compile external dependencies on all platform(windows/linux/mac)
17

18 19 20 21 22 23 24 25
set(THIRD_PARTY_PATH
    "${CMAKE_BINARY_DIR}/third_party"
    CACHE STRING
          "A path setting third party libraries download & build directories.")
set(THIRD_PARTY_CACHE_PATH
    "${CMAKE_SOURCE_DIR}"
    CACHE STRING
          "A path cache third party source code to avoid repeated download.")
26 27

set(THIRD_PARTY_BUILD_TYPE Release)
28
set(third_party_deps)
29

30 31
include(ProcessorCount)
ProcessorCount(NPROC)
32
if(NOT WITH_SETUP_INSTALL)
R
risemeup1 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45
  #NOTE(risemeup1):Initialize any submodules.
  message(
    STATUS
      "Check submodules of paddle, and run 'git submodule update --init --recursive'"
  )
  execute_process(
    COMMAND git submodule update --init --recursive
    WORKING_DIRECTORY ${PADDLE_SOURCE_DIR}
    RESULT_VARIABLE result_var)
  if(NOT result_var EQUAL 0)
    message(FATAL_ERROR "Failed to get submodule, please check your network !")
  endif()

46
endif()
47
# cache funciton to avoid repeat download code of third_party.
48 49 50 51 52
# This function has 4 parameters, URL / REPOSITOR / TAG / DIR:
# 1. URL:           specify download url of 3rd party
# 2. REPOSITORY:    specify git REPOSITORY of 3rd party
# 3. TAG:           specify git tag/branch/commitID of 3rd party
# 4. DIR:           overwrite the original SOURCE_DIR when cache directory
53
#
54
# The function Return 1 PARENT_SCOPE variables:
55
#  - ${TARGET}_DOWNLOAD_CMD: Simply place "${TARGET}_DOWNLOAD_CMD" in ExternalProject_Add,
56 57
#                            and you no longer need to set any donwnload steps in ExternalProject_Add.
# For example:
58
#    Cache_third_party(${TARGET}
59
#            REPOSITORY ${TARGET_REPOSITORY}
60 61
#            TAG        ${TARGET_TAG}
#            DIR        ${TARGET_SOURCE_DIR})
62

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
function(cache_third_party TARGET)
  set(options "")
  set(oneValueArgs URL REPOSITORY TAG DIR)
  set(multiValueArgs "")
  cmake_parse_arguments(cache_third_party "${optionps}" "${oneValueArgs}"
                        "${multiValueArgs}" ${ARGN})

  string(REPLACE "extern_" "" TARGET_NAME ${TARGET})
  string(REGEX REPLACE "[0-9]+" "" TARGET_NAME ${TARGET_NAME})
  string(TOUPPER ${TARGET_NAME} TARGET_NAME)
  if(cache_third_party_REPOSITORY)
    set(${TARGET_NAME}_DOWNLOAD_CMD GIT_REPOSITORY
                                    ${cache_third_party_REPOSITORY})
    if(cache_third_party_TAG)
      list(APPEND ${TARGET_NAME}_DOWNLOAD_CMD GIT_TAG ${cache_third_party_TAG})
    endif()
  elseif(cache_third_party_URL)
    set(${TARGET_NAME}_DOWNLOAD_CMD URL ${cache_third_party_URL})
  else()
    message(
      FATAL_ERROR "Download link (Git repo or URL) must be specified for cache!"
    )
  endif()
  if(WITH_TP_CACHE)
    if(NOT cache_third_party_DIR)
      message(
        FATAL_ERROR
          "Please input the ${TARGET_NAME}_SOURCE_DIR for overwriting when -DWITH_TP_CACHE=ON"
      )
    endif()
    # Generate and verify cache dir for third_party source code
    set(cache_third_party_REPOSITORY ${cache_third_party_REPOSITORY}
                                     ${cache_third_party_URL})
    if(cache_third_party_REPOSITORY AND cache_third_party_TAG)
      string(MD5 HASH_REPO ${cache_third_party_REPOSITORY})
      string(MD5 HASH_GIT ${cache_third_party_TAG})
      string(SUBSTRING ${HASH_REPO} 0 8 HASH_REPO)
      string(SUBSTRING ${HASH_GIT} 0 8 HASH_GIT)
      string(CONCAT HASH ${HASH_REPO} ${HASH_GIT})
      # overwrite the original SOURCE_DIR when cache directory
      set(${cache_third_party_DIR}
          ${THIRD_PARTY_CACHE_PATH}/third_party/${TARGET}_${HASH})
    elseif(cache_third_party_REPOSITORY)
      string(MD5 HASH_REPO ${cache_third_party_REPOSITORY})
      string(SUBSTRING ${HASH_REPO} 0 16 HASH)
      # overwrite the original SOURCE_DIR when cache directory
      set(${cache_third_party_DIR}
          ${THIRD_PARTY_CACHE_PATH}/third_party/${TARGET}_${HASH})
    endif()

    if(EXISTS ${${cache_third_party_DIR}})
      # judge whether the cache dir is empty
      file(GLOB files ${${cache_third_party_DIR}}/*)
      list(LENGTH files files_len)
      if(files_len GREATER 0)
        list(APPEND ${TARGET_NAME}_DOWNLOAD_CMD DOWNLOAD_COMMAND "")
      endif()
    endif()
    set(${cache_third_party_DIR}
        ${${cache_third_party_DIR}}
        PARENT_SCOPE)
  endif()

  # Pass ${TARGET_NAME}_DOWNLOAD_CMD to parent scope, the double quotation marks can't be removed
  set(${TARGET_NAME}_DOWNLOAD_CMD
      "${${TARGET_NAME}_DOWNLOAD_CMD}"
      PARENT_SCOPE)
endfunction()

macro(UNSET_VAR VAR_NAME)
  unset(${VAR_NAME} CACHE)
  unset(${VAR_NAME})
endmacro()
136

137 138 139 140 141
# Funciton to Download the dependencies during compilation
# This function has 2 parameters, URL / DIRNAME:
# 1. URL:           The download url of 3rd dependencies
# 2. NAME:          The name of file, that determin the dirname
#
142
function(file_download_and_uncompress URL NAME)
143 144 145
  set(options "")
  set(oneValueArgs MD5)
  set(multiValueArgs "")
146 147 148 149 150 151
  cmake_parse_arguments(URL "${options}" "${oneValueArgs}" "${multiValueArgs}"
                        ${ARGN})
  message(STATUS "Download dependence[${NAME}] from ${URL}, MD5: ${URL_MD5}")
  set(${NAME}_INCLUDE_DIR
      ${THIRD_PARTY_PATH}/${NAME}/data
      PARENT_SCOPE)
152
  ExternalProject_Add(
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
    download_${NAME}
    ${EXTERNAL_PROJECT_LOG_ARGS}
    PREFIX ${THIRD_PARTY_PATH}/${NAME}
    URL ${URL}
    URL_MD5 ${URL_MD5}
    TIMEOUT 120
    DOWNLOAD_DIR ${THIRD_PARTY_PATH}/${NAME}/data/
    SOURCE_DIR ${THIRD_PARTY_PATH}/${NAME}/data/
    DOWNLOAD_NO_PROGRESS 1
    CONFIGURE_COMMAND ""
    BUILD_COMMAND ""
    UPDATE_COMMAND ""
    INSTALL_COMMAND "")
  set(third_party_deps
      ${third_party_deps} download_${NAME}
      PARENT_SCOPE)
endfunction()
170

171
# Correction of flags on different Platform(WIN/MAC) and Print Warning Message
172 173 174 175 176 177 178 179 180
if(APPLE)
  if(WITH_MKL)
    message(
      WARNING "Mac is not supported with MKL in Paddle yet. Force WITH_MKL=OFF."
    )
    set(WITH_MKL
        OFF
        CACHE STRING "Disable MKL for building on mac" FORCE)
  endif()
181 182 183
endif()

if(WIN32 OR APPLE)
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
  message(STATUS "Disable XBYAK in Windows and MacOS")
  set(WITH_XBYAK
      OFF
      CACHE STRING "Disable XBYAK in Windows and MacOS" FORCE)

  if(WITH_LIBXSMM)
    message(WARNING "Windows, Mac are not supported with libxsmm in Paddle yet."
                    "Force WITH_LIBXSMM=OFF")
    set(WITH_LIBXSMM
        OFF
        CACHE STRING "Disable LIBXSMM in Windows and MacOS" FORCE)
  endif()

  if(WITH_BOX_PS)
    message(WARNING "Windows or Mac is not supported with BOX_PS in Paddle yet."
                    "Force WITH_BOX_PS=OFF")
    set(WITH_BOX_PS
        OFF
        CACHE STRING "Disable BOX_PS package in Windows and MacOS" FORCE)
  endif()

  if(WITH_PSLIB)
    message(WARNING "Windows or Mac is not supported with PSLIB in Paddle yet."
                    "Force WITH_PSLIB=OFF")
    set(WITH_PSLIB
        OFF
        CACHE STRING "Disable PSLIB package in Windows and MacOS" FORCE)
  endif()

  if(WITH_ARM_BRPC)
    message(
      WARNING "Windows or Mac is not supported with ARM_BRPC in Paddle yet."
              "Force WITH_ARM_BRPC=OFF")
    set(WITH_ARM_BRPC
        OFF
        CACHE STRING "Disable ARM_BRPC package in Windows and MacOS" FORCE)
  endif()

  if(WITH_LIBMCT)
    message(WARNING "Windows or Mac is not supported with LIBMCT in Paddle yet."
                    "Force WITH_LIBMCT=OFF")
    set(WITH_LIBMCT
        OFF
        CACHE STRING "Disable LIBMCT package in Windows and MacOS" FORCE)
  endif()

  if(WITH_PSLIB_BRPC)
    message(
      WARNING "Windows or Mac is not supported with PSLIB_BRPC in Paddle yet."
              "Force WITH_PSLIB_BRPC=OFF")
    set(WITH_PSLIB_BRPC
        OFF
        CACHE STRING "Disable PSLIB_BRPC package in Windows and MacOS" FORCE)
  endif()
238 239
endif()

240
set(WITH_MKLML ${WITH_MKL})
241
if(NOT DEFINED WITH_MKLDNN)
242 243 244
  if(WITH_MKL AND AVX2_FOUND)
    set(WITH_MKLDNN ON)
  else()
245
    message(STATUS "Do not have AVX2 intrinsics and disabled MKL-DNN.")
246 247
    set(WITH_MKLDNN OFF)
  endif()
248 249
endif()

250 251 252
if(WIN32
   OR APPLE
   OR NOT WITH_GPU
253
   OR (ON_INFER AND NOT WITH_PYTHON))
254
  set(WITH_DGC OFF)
255 256
endif()

257
if(${CMAKE_VERSION} VERSION_GREATER "3.5.2")
258 259
  set(SHALLOW_CLONE "GIT_SHALLOW TRUE"
  )# adds --depth=1 arg to git clone of External_Projects
260 261
endif()

262 263 264 265
include(external/zlib) # download, build, install zlib
include(external/gflags) # download, build, install gflags
include(external/glog) # download, build, install glog

T
tianshuo78520a 已提交
266
########################### include third_party according to flags ###############################
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
if(WITH_CINN)
  if(WITH_MKL)
    add_definitions(-DCINN_WITH_MKL_CBLAS)
  endif()
  if(WITH_MKLDNN)
    add_definitions(-DCINN_WITH_MKLDNN)
  endif()
  include(cmake/cinn/version.cmake)
  if(NOT EXISTS ${CMAKE_BINARY_DIR}/cmake/cinn/config.cmake)
    file(COPY ${PROJECT_SOURCE_DIR}/cmake/cinn/config.cmake
         DESTINATION ${CMAKE_BINARY_DIR}/cmake/cinn)
  endif()
  include(${CMAKE_BINARY_DIR}/cmake/cinn/config.cmake)
  include(cmake/cinn/external/absl.cmake)
  include(cmake/cinn/external/llvm.cmake)
  include(cmake/cinn/external/isl.cmake)
  include(cmake/cinn/external/ginac.cmake)
  include(cmake/cinn/external/openmp.cmake)
  include(cmake/cinn/external/jitify.cmake)
endif()
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303

# cinn_only includes third-party libraries separately
if(CINN_ONLY)
  include(external/gtest)
  include(external/protobuf)
  if(WITH_PYTHON)
    include(external/pybind11)
  endif()
  if(WITH_MKL)
    include(external/mklml)
  endif()
  if(WITH_MKLDNN)
    include(external/mkldnn)
  endif()
  return()
endif()

304 305 306 307 308
include(external/eigen) # download eigen3
include(external/threadpool) # download threadpool
include(external/dlpack) # download dlpack
include(external/xxhash) # download, build, install xxhash
include(external/warpctc) # download, build, install warpctc
H
Hui Zhang 已提交
309
include(external/warprnnt) # download, build, install warprnnt
310 311
include(external/utf8proc) # download, build, install utf8proc

R
Ruibiao Chen 已提交
312 313
list(APPEND third_party_deps extern_eigen3 extern_gflags extern_glog
     extern_xxhash)
314 315 316 317 318 319
list(
  APPEND
  third_party_deps
  extern_zlib
  extern_dlpack
  extern_warpctc
H
Hui Zhang 已提交
320
  extern_warprnnt
321 322 323 324
  extern_threadpool
  extern_utf8proc)
include(external/lapack) # download, build, install lapack

R
Ruibiao Chen 已提交
325 326
list(APPEND third_party_deps extern_eigen3 extern_gflags extern_glog
     extern_xxhash)
327 328 329 330 331 332
list(
  APPEND
  third_party_deps
  extern_zlib
  extern_dlpack
  extern_warpctc
H
Hui Zhang 已提交
333
  extern_warprnnt
334 335 336 337
  extern_threadpool
  extern_lapack)

include(cblas) # find first, then download, build, install openblas
338 339

message(STATUS "CBLAS_PROVIDER: ${CBLAS_PROVIDER}")
340
if(${CBLAS_PROVIDER} STREQUAL MKLML)
341
  list(APPEND third_party_deps extern_mklml)
342
elseif(${CBLAS_PROVIDER} STREQUAL EXTERN_OPENBLAS)
343
  list(APPEND third_party_deps extern_openblas)
344 345 346
endif()

if(WITH_MKLDNN)
347 348
  include(external/mkldnn) # download, build, install mkldnn
  list(APPEND third_party_deps extern_mkldnn)
349 350
endif()

351
include(external/protobuf) # find first, then download, build, install protobuf
352
if(TARGET extern_protobuf)
353
  list(APPEND third_party_deps extern_protobuf)
354 355
endif()

356
if(NOT ((NOT WITH_PYTHON) AND ON_INFER))
357
  include(external/python) # find python and python_module
R
RedContritio 已提交
358
  include(external/pybind11) # prepare submodule pybind11
359
  list(APPEND third_party_deps extern_pybind)
360 361
endif()

362 363 364 365
if(WITH_TESTING OR WITH_DISTRIBUTE)
  include(external/gtest) # download, build, install gtest
  list(APPEND third_party_deps extern_gtest)
endif()
366

367
if(WITH_ONNXRUNTIME)
368 369 370 371
  include(external/onnxruntime
  )# download, build, install onnxruntime、paddle2onnx
  include(external/paddle2onnx)
  list(APPEND third_party_deps extern_onnxruntime extern_paddle2onnx)
372 373
endif()

374
if(WITH_GPU)
375
  if(${CMAKE_CUDA_COMPILER_VERSION} LESS 11.0)
376 377 378 379 380 381 382 383 384 385
    include(external/cub) # download cub
    list(APPEND third_party_deps extern_cub)
  endif()
  set(URL
      "https://paddlepaddledeps.bj.bcebos.com/externalErrorMsg_20210928.tar.gz"
      CACHE STRING "" FORCE)
  file_download_and_uncompress(
    ${URL} "externalError" MD5 a712a49384e77ca216ad866712f7cafa
  )# download file externalErrorMsg.tar.gz
  if(WITH_TESTING)
386
    # copy externalErrorMsg.pb for UnitTest
387
    set(SRC_DIR ${THIRD_PARTY_PATH}/externalError/data)
388 389
    # for python UT 'test_exception.py'
    set(DST_DIR1
390 391
        ${CMAKE_BINARY_DIR}/python/paddle/include/third_party/externalError/data
    )
392 393
    # for C++ UT 'enforce_test'
    set(DST_DIR2 ${CMAKE_BINARY_DIR}/paddle/third_party/externalError/data)
394 395 396 397 398
    add_custom_command(
      TARGET download_externalError
      POST_BUILD
      COMMAND ${CMAKE_COMMAND} -E copy_directory ${SRC_DIR} ${DST_DIR1}
      COMMAND ${CMAKE_COMMAND} -E copy_directory ${SRC_DIR} ${DST_DIR2}
399 400
      COMMENT "copy_directory from ${SRC_DIR} to ${DST_DIR1}"
      COMMENT "copy_directory from ${SRC_DIR} to ${DST_DIR2}")
401
  endif()
402
endif()
403

404
if(WITH_XPU)
405 406
  include(external/xpu) # download, build, install xpu
  list(APPEND third_party_deps extern_xpu)
407
endif()
408

409
if(WITH_PSLIB)
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
  include(external/pslib) # download, build, install pslib
  list(APPEND third_party_deps extern_pslib)
  if(WITH_LIBMCT)
    include(external/libmct) # download, build, install libmct
    list(APPEND third_party_deps extern_libxsmm)
  endif()
  if(WITH_PSLIB_BRPC)
    include(external/pslib_brpc) # download, build, install pslib_brpc
    list(APPEND third_party_deps extern_pslib_brpc)
  else()
    include(external/snappy)
    list(APPEND third_party_deps extern_snappy)

    include(external/leveldb)
    list(APPEND third_party_deps extern_leveldb)
    if(NOT WITH_HETERPS)
      include(external/brpc)
      list(APPEND third_party_deps extern_brpc)
428
    endif()
429
  endif()
430
endif()
431

T
Thunderbrook 已提交
432
if(NOT WIN32 AND NOT APPLE)
433 434
  include(external/gloo)
  list(APPEND third_party_deps extern_gloo)
T
Thunderbrook 已提交
435
endif()
436

437
if(WITH_BOX_PS)
438 439
  include(external/box_ps)
  list(APPEND third_party_deps extern_box_ps)
440
endif()
441

442 443 444
if(WITH_PSCORE)
  include(external/snappy)
  list(APPEND third_party_deps extern_snappy)
445

446 447 448 449 450 451 452 453 454 455
  include(external/leveldb)
  list(APPEND third_party_deps extern_leveldb)

  if(WITH_ARM_BRPC)
    include(external/arm_brpc)
    list(APPEND third_party_deps extern_arm_brpc)
  else()
    include(external/brpc)
    list(APPEND third_party_deps extern_brpc)
  endif()
T
tangwei12 已提交
456

457 458
  include(external/libmct) # download, build, install libmct
  list(APPEND third_party_deps extern_libmct)
459

460 461
  include(external/rocksdb) # download, build, install rocksdb
  list(APPEND third_party_deps extern_rocksdb)
L
lxsbupt 已提交
462 463 464

  include(external/jemalloc) # download, build, install jemalloc
  list(APPEND third_party_deps extern_jemalloc)
465 466
endif()

X
Xinger 已提交
467 468 469 470 471 472 473 474 475 476 477 478 479
if(WITH_RPC
   AND NOT WITH_PSCORE
   AND NOT WITH_PSLIB)
  include(external/snappy)
  list(APPEND third_party_deps extern_snappy)

  include(external/leveldb)
  list(APPEND third_party_deps extern_leveldb)

  include(external/brpc)
  list(APPEND third_party_deps extern_brpc)
endif()

L
LiYuRio 已提交
480 481
if(WITH_DISTRIBUTE
   AND NOT WITH_PSLIB
482 483
   AND NOT WITH_PSCORE
   AND NOT WITH_RPC)
L
LiYuRio 已提交
484 485 486 487 488 489 490 491 492
  include(external/snappy)
  list(APPEND third_party_deps extern_snappy)

  include(external/leveldb)
  list(APPEND third_party_deps extern_leveldb)
  include(external/brpc)
  list(APPEND third_party_deps extern_brpc)
endif()

493
if(WITH_XBYAK)
494
  include(external/xbyak) # prepare submodule xbyak
495
  list(APPEND third_party_deps extern_xbyak)
496 497 498
endif()

if(WITH_LIBXSMM)
499 500
  include(external/libxsmm) # download, build, install libxsmm
  list(APPEND third_party_deps extern_libxsmm)
501 502 503
endif()

if(WITH_DGC)
504 505 506 507
  message(STATUS "add dgc lib.")
  include(external/dgc) # download, build, install dgc
  add_definitions(-DPADDLE_WITH_DGC)
  list(APPEND third_party_deps extern_dgc)
508 509
endif()

510 511 512
if(WITH_LITE)
  message(STATUS "Compile Paddle with Lite Engine.")
  include(external/lite)
513
endif()
514 515 516 517 518

if(WITH_CRYPTO)
  include(external/cryptopp) # download, build, install cryptopp
  list(APPEND third_party_deps extern_cryptopp)
  add_definitions(-DPADDLE_WITH_CRYPTO)
519
endif()
520 521 522 523 524

if(WITH_POCKETFFT)
  include(external/pocketfft)
  list(APPEND third_party_deps extern_pocketfft)
  add_definitions(-DPADDLE_WITH_POCKETFFT)
525
endif()
526 527 528 529

if(WIN32)
  include(external/dirent)
  list(APPEND third_party_deps extern_dirent)
530
endif()
531 532 533 534

if(WITH_IPU)
  include(external/poplar)
  list(APPEND third_party_deps extern_poplar)
J
jianghaicheng 已提交
535 536
endif()

537 538 539 540 541
if(WITH_CUSPARSELT)
  include(external/cusparselt) # download, build, install cusparselt
  list(APPEND third_party_deps extern_cusparselt)
endif()

542 543 544 545 546 547 548
if(WITH_GPU
   AND NOT WITH_ARM
   AND NOT WIN32
   AND NOT APPLE)
  if(${CMAKE_CUDA_COMPILER_VERSION} GREATER_EQUAL 11.0)
    include(external/cutlass) # download, build, install cusparselt
    list(APPEND third_party_deps extern_cutlass)
549
    set(WITH_CUTLASS ON)
C
Chitsing KUI 已提交
550
  endif()
551 552 553 554 555 556 557 558 559
  if(${CMAKE_CUDA_COMPILER_VERSION} GREATER_EQUAL 11.4)
    foreach(arch ${NVCC_ARCH_BIN})
      if(${arch} GREATER_EQUAL 80)
        include(external/flashattn)
        list(APPEND third_party_deps extern_flashattn)
        set(WITH_FLASHATTN ON)
        break()
      endif()
    endforeach()
560 561 562
  endif()
endif()

563 564 565 566 567
if(WITH_CUDNN_FRONTEND)
  include(external/cudnn-frontend) # download cudnn-frontend
  list(APPEND third_party_deps extern_cudnn_frontend)
endif()

568
add_custom_target(third_party ALL DEPENDS ${third_party_deps})