2
# see https://cmake.org/Wiki/CMake_RPATH_handling
4
set(CMAKE_MACOSX_RPATH ON)
5
set(_rpath_portable_origin "@loader_path")
7
set(_rpath_portable_origin $ORIGIN)
9
# Use separate rpaths during build and install phases
10
set(CMAKE_SKIP_BUILD_RPATH FALSE)
11
# Don't use the install-rpath during the build phase
12
set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
13
set(CMAKE_INSTALL_RPATH "${_rpath_portable_origin}")
14
# Automatically add all linked folders that are NOT in the build directory to
15
# the rpath (per library?)
16
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
18
# UBSAN triggers when compiling protobuf, so we need to disable it.
19
set(UBSAN_FLAG "-fsanitize=undefined")
22
if(CMAKE_C_FLAGS MATCHES ${UBSAN_FLAG} OR CMAKE_CXX_FLAGS MATCHES ${UBSAN_FLAG})
23
set(CAFFE2_UBSAN_ENABLED ON)
24
string(REPLACE ${UBSAN_FLAG} "" CMAKE_C_FLAGS ${CMAKE_C_FLAGS})
25
string(REPLACE ${UBSAN_FLAG} "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
30
if(CAFFE2_UBSAN_ENABLED)
31
set(CMAKE_C_FLAGS "${UBSAN_FLAG} ${CMAKE_C_FLAGS}")
32
set(CMAKE_CXX_FLAGS "${UBSAN_FLAG} ${CMAKE_CXX_FLAGS}")
38
# public/*.cmake uses CAFFE2_USE_*
39
set(CAFFE2_USE_CUDA ${USE_CUDA})
40
set(CAFFE2_USE_CUDNN ${USE_CUDNN})
41
set(CAFFE2_USE_CUSPARSELT ${USE_CUSPARSELT})
42
set(CAFFE2_USE_NVRTC ${USE_NVRTC})
43
set(CAFFE2_USE_TENSORRT ${USE_TENSORRT})
44
include(${CMAKE_CURRENT_LIST_DIR}/public/cuda.cmake)
46
# A helper variable recording the list of Caffe2 dependent libraries
47
# torch::cudart is dealt with separately, due to CUDA_ADD_LIBRARY
48
# design reason (it adds CUDA_LIBRARIES itself).
49
set(Caffe2_PUBLIC_CUDA_DEPENDENCY_LIBS )
51
list(APPEND Caffe2_PUBLIC_CUDA_DEPENDENCY_LIBS caffe2::cuda caffe2::nvrtc)
53
caffe2_update_option(USE_NVRTC OFF)
55
list(APPEND Caffe2_CUDA_DEPENDENCY_LIBS caffe2::curand caffe2::cufft caffe2::cublas)
57
list(APPEND Caffe2_CUDA_DEPENDENCY_LIBS torch::cudnn)
59
caffe2_update_option(USE_CUDNN OFF)
61
if(CAFFE2_USE_CUSPARSELT)
62
list(APPEND Caffe2_CUDA_DEPENDENCY_LIBS torch::cusparselt)
64
caffe2_update_option(USE_CUSPARSELT OFF)
66
if(CAFFE2_USE_TENSORRT)
67
list(APPEND Caffe2_PUBLIC_CUDA_DEPENDENCY_LIBS caffe2::tensorrt)
69
caffe2_update_option(USE_TENSORRT OFF)
71
find_program(SCCACHE_EXECUTABLE sccache)
72
if(SCCACHE_EXECUTABLE)
73
# Using RSP/--options-file renders output noncacheable by sccache
74
# as they fall under `multiple input files` non-cacheable rule
75
set(CMAKE_CUDA_USE_RESPONSE_FILE_FOR_INCLUDES 0)
76
set(CMAKE_CUDA_USE_RESPONSE_FILE_FOR_LIBRARIES 0)
77
set(CMAKE_CUDA_USE_RESPONSE_FILE_FOR_OBJECTS 0)
81
"Not compiling with CUDA. Suppress this warning with "
83
caffe2_update_option(USE_CUDA OFF)
84
caffe2_update_option(USE_CUDNN OFF)
85
caffe2_update_option(USE_CUSPARSELT OFF)
86
caffe2_update_option(USE_NVRTC OFF)
87
caffe2_update_option(USE_TENSORRT OFF)
88
set(CAFFE2_USE_CUDA OFF)
89
set(CAFFE2_USE_CUDNN OFF)
90
set(CAFFE2_USE_CUSPARSELT OFF)
91
set(CAFFE2_USE_NVRTC OFF)
92
set(CAFFE2_USE_TENSORRT OFF)
98
include(${CMAKE_CURRENT_LIST_DIR}/public/xpu.cmake)
99
if(NOT PYTORCH_FOUND_XPU)
100
# message(WARNING "Not compiling with XPU. Could NOT find SYCL."
101
# "Suppress this warning with -DUSE_XPU=OFF.")
102
caffe2_update_option(USE_XPU OFF)
106
# ---[ Custom Protobuf
107
if(CAFFE2_CMAKE_BUILDING_WITH_MAIN_REPO AND NOT INTERN_BUILD_MOBILE)
109
include(${CMAKE_CURRENT_LIST_DIR}/ProtoBuf.cmake)
113
if(USE_ASAN OR USE_TSAN)
114
find_package(Sanitizer REQUIRED)
116
if(TARGET Sanitizer::address)
117
list(APPEND Caffe2_DEPENDENCY_LIBS Sanitizer::address)
119
message(WARNING "Not ASAN found. Suppress this warning with -DUSE_ASAN=OFF.")
120
caffe2_update_option(USE_ASAN OFF)
122
if(TARGET Sanitizer::undefined)
123
list(APPEND Caffe2_DEPENDENCY_LIBS Sanitizer::undefined)
127
if(TARGET Sanitizer::thread)
128
list(APPEND Caffe2_DEPENDENCY_LIBS Sanitizer::thread)
130
message(WARNING "Not TSAN found. Suppress this warning with -DUSE_TSAN=OFF.")
131
caffe2_update_option(USE_TSAN OFF)
137
find_package(Threads REQUIRED)
138
if(TARGET Threads::Threads)
139
list(APPEND Caffe2_DEPENDENCY_LIBS Threads::Threads)
142
"Cannot find threading library. PyTorch requires Threads to compile.")
147
find_package(TBB 2018.0 REQUIRED CONFIG COMPONENTS tbb)
149
get_target_property(TBB_INCLUDE_DIR TBB::tbb INTERFACE_INCLUDE_DIRECTORIES)
151
message(STATUS "Compiling TBB from source")
152
# Unset our restrictive C++ flags here and reset them later.
153
# Remove this once we use proper target_compile_options.
154
set(OLD_CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
157
set(TBB_ROOT_DIR "${PROJECT_SOURCE_DIR}/third_party/tbb")
158
set(TBB_BUILD_STATIC OFF CACHE BOOL " " FORCE)
159
set(TBB_BUILD_SHARED ON CACHE BOOL " " FORCE)
160
set(TBB_BUILD_TBBMALLOC OFF CACHE BOOL " " FORCE)
161
set(TBB_BUILD_TBBMALLOC_PROXY OFF CACHE BOOL " " FORCE)
162
set(TBB_BUILD_TESTS OFF CACHE BOOL " " FORCE)
163
add_subdirectory(${PROJECT_SOURCE_DIR}/aten/src/ATen/cpu/tbb)
164
set_property(TARGET tbb tbb_def_files PROPERTY FOLDER "dependencies")
166
set(CMAKE_CXX_FLAGS ${OLD_CMAKE_CXX_FLAGS})
168
set(TBB_INCLUDE_DIR "${TBB_ROOT_DIR}/include")
170
add_library(TBB::tbb ALIAS tbb)
175
if(CAFFE2_CMAKE_BUILDING_WITH_MAIN_REPO)
177
set(CAFFE2_USE_LITE_PROTO 1)
183
set(AT_MKLDNN_ACL_ENABLED 0)
184
# setting default preferred BLAS options if not already present.
185
if(NOT INTERN_BUILD_MOBILE)
186
set(BLAS "MKL" CACHE STRING "Selected BLAS library")
188
set(BLAS "Eigen" CACHE STRING "Selected BLAS library")
189
set(AT_MKLDNN_ENABLED 0)
190
set(AT_MKL_ENABLED 0)
192
set_property(CACHE BLAS PROPERTY STRINGS "ATLAS;BLIS;Eigen;FLAME;Generic;MKL;OpenBLAS;vecLib")
193
message(STATUS "Trying to find preferred BLAS backend of choice: " ${BLAS})
195
if(BLAS STREQUAL "Eigen")
196
# Eigen is header-only and we do not have any dependent libraries
197
set(CAFFE2_USE_EIGEN_FOR_BLAS ON)
198
elseif(BLAS STREQUAL "ATLAS")
199
find_package(Atlas REQUIRED)
200
include_directories(SYSTEM ${ATLAS_INCLUDE_DIRS})
201
list(APPEND Caffe2_DEPENDENCY_LIBS ${ATLAS_LIBRARIES})
202
list(APPEND Caffe2_DEPENDENCY_LIBS cblas)
203
set(BLAS_INFO "atlas")
205
set(BLAS_LIBRARIES ${ATLAS_LIBRARIES} cblas)
206
elseif(BLAS STREQUAL "OpenBLAS")
207
find_package(OpenBLAS REQUIRED)
208
include_directories(SYSTEM ${OpenBLAS_INCLUDE_DIR})
209
list(APPEND Caffe2_DEPENDENCY_LIBS ${OpenBLAS_LIB})
210
set(BLAS_INFO "open")
212
set(BLAS_LIBRARIES ${OpenBLAS_LIB})
213
elseif(BLAS STREQUAL "BLIS")
214
find_package(BLIS REQUIRED)
215
include_directories(SYSTEM ${BLIS_INCLUDE_DIR})
216
list(APPEND Caffe2_DEPENDENCY_LIBS ${BLIS_LIB})
217
elseif(BLAS STREQUAL "MKL")
219
find_package(MKL REQUIRED)
221
find_package(MKL QUIET)
223
include(${CMAKE_CURRENT_LIST_DIR}/public/mkl.cmake)
225
message(STATUS "MKL libraries: ${MKL_LIBRARIES}")
226
message(STATUS "MKL include directory: ${MKL_INCLUDE_DIR}")
227
message(STATUS "MKL OpenMP type: ${MKL_OPENMP_TYPE}")
228
message(STATUS "MKL OpenMP library: ${MKL_OPENMP_LIBRARY}")
229
include_directories(AFTER SYSTEM ${MKL_INCLUDE_DIR})
230
list(APPEND Caffe2_PUBLIC_DEPENDENCY_LIBS caffe2::mkl)
231
set(CAFFE2_USE_MKL ON)
234
set(BLAS_LIBRARIES ${MKL_LIBRARIES})
236
message(WARNING "MKL could not be found. Defaulting to Eigen")
237
set(CAFFE2_USE_EIGEN_FOR_BLAS ON)
238
set(CAFFE2_USE_MKL OFF)
240
elseif(BLAS STREQUAL "vecLib")
241
find_package(vecLib REQUIRED)
242
include_directories(SYSTEM ${vecLib_INCLUDE_DIR})
243
list(APPEND Caffe2_DEPENDENCY_LIBS ${vecLib_LINKER_LIBS})
244
set(BLAS_INFO "veclib")
246
set(BLAS_LIBRARIES ${vecLib_LINKER_LIBS})
247
elseif(BLAS STREQUAL "FlexiBLAS")
248
find_package(FlexiBLAS REQUIRED)
249
include_directories(SYSTEM ${FlexiBLAS_INCLUDE_DIR})
250
list(APPEND Caffe2_DEPENDENCY_LIBS ${FlexiBLAS_LIB})
251
elseif(BLAS STREQUAL "Generic")
252
# On Debian family, the CBLAS ABIs have been merged into libblas.so
253
if(ENV{GENERIC_BLAS_LIBRARIES} STREQUAL "")
254
set(GENERIC_BLAS "blas")
256
set(GENERIC_BLAS $ENV{GENERIC_BLAS_LIBRARIES})
258
find_library(BLAS_LIBRARIES NAMES ${GENERIC_BLAS})
259
message("-- Using BLAS: ${BLAS_LIBRARIES}")
260
list(APPEND Caffe2_DEPENDENCY_LIBS ${BLAS_LIBRARIES})
261
set(GENERIC_BLAS_FOUND TRUE)
262
set(BLAS_INFO "generic")
265
message(FATAL_ERROR "Unrecognized BLAS option: " ${BLAS})
268
if(NOT INTERN_BUILD_MOBILE)
269
set(AT_MKL_ENABLED 0)
270
set(AT_MKL_SEQUENTIAL 0)
272
if(NOT (ATLAS_FOUND OR BLIS_FOUND OR GENERIC_BLAS_FOUND OR MKL_FOUND OR OpenBLAS_FOUND OR VECLIB_FOUND OR FlexiBLAS_FOUND))
273
message(WARNING "Preferred BLAS (" ${BLAS} ") cannot be found, now searching for a general BLAS library")
281
if("${MKL_THREADING}" STREQUAL "SEQ")
282
set(AT_MKL_SEQUENTIAL 1)
284
set(AT_MKL_ENABLED 1)
286
elseif(INTERN_USE_EIGEN_BLAS)
287
# Eigen BLAS for Mobile
289
include(${CMAKE_CURRENT_LIST_DIR}/External/EigenBLAS.cmake)
290
list(APPEND Caffe2_DEPENDENCY_LIBS eigen_blas)
294
set(AT_POCKETFFT_ENABLED 0)
295
if(NOT AT_MKL_ENABLED)
296
set(POCKETFFT_INCLUDE_DIR "${Torch_SOURCE_DIR}/third_party/pocketfft/")
297
if(NOT EXISTS "${POCKETFFT_INCLUDE_DIR}")
298
message(FATAL_ERROR "pocketfft directory not found, expected ${POCKETFFT_INCLUDE_DIR}")
299
elif(NOT EXISTS "${POCKETFFT_INCLUDE_DIR}/pocketfft_hdronly.h")
300
message(FATAL_ERROR "pocketfft headers not found in ${POCKETFFT_INCLUDE_DIR}")
303
set(AT_POCKETFFT_ENABLED 1)
304
message(STATUS "Using pocketfft in directory: ${POCKETFFT_INCLUDE_DIR}")
308
# NNPACK and family (QNNPACK, PYTORCH_QNNPACK, and XNNPACK) can download and
309
# compile their dependencies in isolation as part of their build. These dependencies
310
# are then linked statically with PyTorch. To avoid the possibility of a version
311
# mismatch between these shared dependencies, explicitly declare our intent to these
312
# libraries that we are interested in using the exact same source dependencies for all.
314
if(USE_NNPACK OR USE_QNNPACK OR USE_PYTORCH_QNNPACK OR USE_XNNPACK)
315
set(DISABLE_NNPACK_AND_FAMILY OFF)
317
# Sanity checks - Can we actually build NNPACK and family given the configuration provided?
318
# Disable them and warn the user if not.
321
list(LENGTH IOS_ARCH IOS_ARCH_COUNT)
322
if(IOS_ARCH_COUNT GREATER 1)
324
"Multi-architecture (${IOS_ARCH}) builds are not supported in {Q/X}NNPACK. "
325
"Specify a single architecture in IOS_ARCH and re-configure, or "
326
"turn this warning off by USE_{Q/X}NNPACK=OFF.")
327
set(DISABLE_NNPACK_AND_FAMILY ON)
329
if(NOT IOS_ARCH MATCHES "^(i386|x86_64|armv7.*|arm64.*)$")
331
"Target architecture \"${IOS_ARCH}\" is not supported in {Q/X}NNPACK. "
332
"Supported architectures are x86, x86-64, ARM, and ARM64. "
333
"Turn this warning off by USE_{Q/X}NNPACK=OFF.")
334
set(DISABLE_NNPACK_AND_FAMILY ON)
337
if(NOT IOS AND NOT (CMAKE_SYSTEM_NAME MATCHES "^(Android|Linux|Darwin|Windows)$"))
339
"Target platform \"${CMAKE_SYSTEM_NAME}\" is not supported in {Q/X}NNPACK. "
340
"Supported platforms are Android, iOS, Linux, and macOS. "
341
"Turn this warning off by USE_{Q/X}NNPACK=OFF.")
342
set(DISABLE_NNPACK_AND_FAMILY ON)
344
if(NOT IOS AND NOT (CMAKE_SYSTEM_PROCESSOR MATCHES "^(i686|AMD64|x86_64|armv[0-9].*|arm64|aarch64)$"))
346
"Target architecture \"${CMAKE_SYSTEM_PROCESSOR}\" is not supported in {Q/X}NNPACK. "
347
"Supported architectures are x86, x86-64, ARM, and ARM64. "
348
"Turn this warning off by USE_{Q/X}NNPACK=OFF.")
349
set(DISABLE_NNPACK_AND_FAMILY ON)
353
if(DISABLE_NNPACK_AND_FAMILY)
354
caffe2_update_option(USE_NNPACK OFF)
355
caffe2_update_option(USE_QNNPACK OFF)
356
caffe2_update_option(USE_PYTORCH_QNNPACK OFF)
357
caffe2_update_option(USE_XNNPACK OFF)
359
# Disable unsupported NNPack combinations with MSVC
361
caffe2_update_option(USE_NNPACK OFF)
362
caffe2_update_option(USE_QNNPACK OFF)
363
caffe2_update_option(USE_PYTORCH_QNNPACK OFF)
366
set(CAFFE2_THIRD_PARTY_ROOT "${PROJECT_SOURCE_DIR}/third_party")
368
if(NOT DEFINED CPUINFO_SOURCE_DIR)
369
set(CPUINFO_SOURCE_DIR "${CAFFE2_THIRD_PARTY_ROOT}/cpuinfo" CACHE STRING "cpuinfo source directory")
371
if(NOT DEFINED FP16_SOURCE_DIR)
372
set(FP16_SOURCE_DIR "${CAFFE2_THIRD_PARTY_ROOT}/FP16" CACHE STRING "FP16 source directory")
374
if(NOT DEFINED FXDIV_SOURCE_DIR)
375
set(FXDIV_SOURCE_DIR "${CAFFE2_THIRD_PARTY_ROOT}/FXdiv" CACHE STRING "FXdiv source directory")
377
if(NOT DEFINED PSIMD_SOURCE_DIR)
378
set(PSIMD_SOURCE_DIR "${CAFFE2_THIRD_PARTY_ROOT}/psimd" CACHE STRING "PSimd source directory")
380
if(NOT DEFINED PTHREADPOOL_SOURCE_DIR)
381
set(PTHREADPOOL_SOURCE_DIR "${CAFFE2_THIRD_PARTY_ROOT}/pthreadpool" CACHE STRING "pthreadpool source directory")
385
set(DISABLE_NNPACK_AND_FAMILY ON)
388
if(USE_QNNPACK AND CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64" AND CMAKE_SYSTEM_NAME STREQUAL "Darwin")
390
"QNNPACK does not compile for Apple Silicon. "
391
"Turn this warning off by explicit USE_QNNPACK=OFF.")
392
caffe2_update_option(USE_QNNPACK OFF)
395
set(CONFU_DEPENDENCIES_SOURCE_DIR ${PROJECT_BINARY_DIR}/confu-srcs
396
CACHE PATH "Confu-style dependencies source directory")
397
set(CONFU_DEPENDENCIES_BINARY_DIR ${PROJECT_BINARY_DIR}/confu-deps
398
CACHE PATH "Confu-style dependencies binary directory")
401
# Only add a dependency on pthreadpool if we are on a mobile build
402
# or are building any of the libraries in the {Q/X}NNPACK family.
403
if(INTERN_BUILD_MOBILE OR NOT DISABLE_NNPACK_AND_FAMILY)
404
set(USE_PTHREADPOOL ON CACHE BOOL "" FORCE)
405
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DUSE_PTHREADPOOL")
407
# Always use third_party/pthreadpool.
408
set(USE_INTERNAL_PTHREADPOOL_IMPL OFF CACHE BOOL "" FORCE)
410
if(NOT TARGET pthreadpool)
411
if(USE_SYSTEM_PTHREADPOOL)
412
add_library(pthreadpool SHARED IMPORTED)
413
find_library(PTHREADPOOL_LIBRARY pthreadpool)
414
set_property(TARGET pthreadpool PROPERTY IMPORTED_LOCATION "${PTHREADPOOL_LIBRARY}")
415
if(NOT PTHREADPOOL_LIBRARY)
416
message(FATAL_ERROR "Cannot find pthreadpool")
418
message("-- Found pthreadpool: ${PTHREADPOOL_LIBRARY}")
419
elseif(NOT USE_INTERNAL_PTHREADPOOL_IMPL)
420
if(NOT DEFINED PTHREADPOOL_SOURCE_DIR)
421
set(CAFFE2_THIRD_PARTY_ROOT "${PROJECT_SOURCE_DIR}/third_party")
422
set(PTHREADPOOL_SOURCE_DIR "${CAFFE2_THIRD_PARTY_ROOT}/pthreadpool" CACHE STRING "pthreadpool source directory")
425
set(PTHREADPOOL_BUILD_TESTS OFF CACHE BOOL "")
426
set(PTHREADPOOL_BUILD_BENCHMARKS OFF CACHE BOOL "")
427
set(PTHREADPOOL_LIBRARY_TYPE "static" CACHE STRING "")
428
set(PTHREADPOOL_ALLOW_DEPRECATED_API ON CACHE BOOL "")
430
"${PTHREADPOOL_SOURCE_DIR}"
431
"${CONFU_DEPENDENCIES_BINARY_DIR}/pthreadpool")
432
set_property(TARGET pthreadpool PROPERTY POSITION_INDEPENDENT_CODE ON)
435
if(USE_INTERNAL_PTHREADPOOL_IMPL)
436
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DUSE_INTERNAL_PTHREADPOOL_IMPL")
438
list(APPEND Caffe2_DEPENDENCY_LIBS pthreadpool)
442
set(USE_PTHREADPOOL OFF CACHE BOOL "" FORCE)
445
if(NOT CMAKE_SYSTEM_PROCESSOR MATCHES "^(s390x|ppc64le)$")
446
# ---[ Caffe2 uses cpuinfo library in the thread pool
447
# ---[ But it doesn't support s390x/powerpc and thus not used on s390x/powerpc
448
if(NOT TARGET cpuinfo AND USE_SYSTEM_CPUINFO)
449
add_library(cpuinfo SHARED IMPORTED)
450
find_library(CPUINFO_LIBRARY cpuinfo)
451
if(NOT CPUINFO_LIBRARY)
452
message(FATAL_ERROR "Cannot find cpuinfo")
454
message("Found cpuinfo: ${CPUINFO_LIBRARY}")
455
set_target_properties(cpuinfo PROPERTIES IMPORTED_LOCATION "${CPUINFO_LIBRARY}")
456
elseif(NOT TARGET cpuinfo)
457
if(NOT DEFINED CPUINFO_SOURCE_DIR)
458
set(CPUINFO_SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/../third_party/cpuinfo" CACHE STRING "cpuinfo source directory")
461
set(CPUINFO_BUILD_TOOLS OFF CACHE BOOL "")
462
set(CPUINFO_BUILD_UNIT_TESTS OFF CACHE BOOL "")
463
set(CPUINFO_BUILD_MOCK_TESTS OFF CACHE BOOL "")
464
set(CPUINFO_BUILD_BENCHMARKS OFF CACHE BOOL "")
465
set(CPUINFO_LIBRARY_TYPE "static" CACHE STRING "")
466
set(CPUINFO_LOG_LEVEL "error" CACHE STRING "")
468
if(CAFFE2_USE_MSVC_STATIC_RUNTIME)
469
set(CPUINFO_RUNTIME_TYPE "static" CACHE STRING "")
471
set(CPUINFO_RUNTIME_TYPE "shared" CACHE STRING "")
475
"${CPUINFO_SOURCE_DIR}"
476
"${CONFU_DEPENDENCIES_BINARY_DIR}/cpuinfo")
477
# We build static version of cpuinfo but link
478
# them into a shared library for Caffe2, so they need PIC.
479
set_property(TARGET cpuinfo PROPERTY POSITION_INDEPENDENT_CODE ON)
481
list(APPEND Caffe2_DEPENDENCY_LIBS cpuinfo)
486
set(CAFFE2_THIRD_PARTY_ROOT "${PROJECT_SOURCE_DIR}/third_party")
488
if(NOT DEFINED QNNPACK_SOURCE_DIR)
489
set(QNNPACK_SOURCE_DIR "${CAFFE2_THIRD_PARTY_ROOT}/QNNPACK" CACHE STRING "QNNPACK source directory")
492
if(NOT TARGET qnnpack)
493
if(NOT USE_SYSTEM_PTHREADPOOL AND USE_INTERNAL_PTHREADPOOL_IMPL)
494
set(QNNPACK_CUSTOM_THREADPOOL ON CACHE BOOL "")
497
set(QNNPACK_BUILD_TESTS OFF CACHE BOOL "")
498
set(QNNPACK_BUILD_BENCHMARKS OFF CACHE BOOL "")
499
set(QNNPACK_LIBRARY_TYPE "static" CACHE STRING "")
501
"${QNNPACK_SOURCE_DIR}"
502
"${CONFU_DEPENDENCIES_BINARY_DIR}/QNNPACK")
504
# TODO: See https://github.com/pytorch/pytorch/issues/56285
505
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
506
target_compile_options(qnnpack PRIVATE -Wno-deprecated-declarations)
509
# We build static versions of QNNPACK and pthreadpool but link
510
# them into a shared library for Caffe2, so they need PIC.
511
set_property(TARGET qnnpack PROPERTY POSITION_INDEPENDENT_CODE ON)
512
set_property(TARGET cpuinfo PROPERTY POSITION_INDEPENDENT_CODE ON)
514
if(QNNPACK_CUSTOM_THREADPOOL)
515
target_compile_definitions(
517
pthreadpool_t=legacy_pthreadpool_t
518
pthreadpool_function_1d_t=legacy_pthreadpool_function_1d_t
519
pthreadpool_function_1d_tiled_t=legacy_pthreadpool_function_1d_tiled_t
520
pthreadpool_function_2d_t=legacy_pthreadpool_function_2d_t
521
pthreadpool_function_2d_tiled_t=legacy_pthreadpool_function_2d_tiled_t
522
pthreadpool_function_3d_tiled_t=legacy_pthreadpool_function_3d_tiled_t
523
pthreadpool_function_4d_tiled_t=legacy_pthreadpool_function_4d_tiled_t
524
pthreadpool_create=legacy_pthreadpool_create
525
pthreadpool_destroy=legacy_pthreadpool_destroy
526
pthreadpool_get_threads_count=legacy_pthreadpool_get_threads_count
527
pthreadpool_compute_1d=legacy_pthreadpool_compute_1d
528
pthreadpool_parallelize_1d=legacy_pthreadpool_parallelize_1d
529
pthreadpool_compute_1d_tiled=legacy_pthreadpool_compute_1d_tiled
530
pthreadpool_compute_2d=legacy_pthreadpool_compute_2d
531
pthreadpool_compute_2d_tiled=legacy_pthreadpool_compute_2d_tiled
532
pthreadpool_compute_3d_tiled=legacy_pthreadpool_compute_3d_tiled
533
pthreadpool_compute_4d_tiled=legacy_pthreadpool_compute_4d_tiled)
537
list(APPEND Caffe2_DEPENDENCY_LIBS qnnpack)
540
# ---[ Caffe2 Int8 operators (enabled by USE_QNNPACK) depend on gemmlowp and neon2sse headers
542
set(CAFFE2_THIRD_PARTY_ROOT "${PROJECT_SOURCE_DIR}/third_party")
543
include_directories(SYSTEM "${CAFFE2_THIRD_PARTY_ROOT}/gemmlowp")
544
include_directories(SYSTEM "${CAFFE2_THIRD_PARTY_ROOT}/neon2sse")
547
# ---[ PYTORCH_QNNPACK
548
if(USE_PYTORCH_QNNPACK)
549
if(NOT DEFINED PYTORCH_QNNPACK_SOURCE_DIR)
550
set(PYTORCH_QNNPACK_SOURCE_DIR "${PROJECT_SOURCE_DIR}/aten/src/ATen/native/quantized/cpu/qnnpack" CACHE STRING "QNNPACK source directory")
553
if(NOT TARGET pytorch_qnnpack)
554
if(NOT USE_SYSTEM_PTHREADPOOL AND USE_INTERNAL_PTHREADPOOL_IMPL)
555
set(PYTORCH_QNNPACK_CUSTOM_THREADPOOL ON CACHE BOOL "")
558
set(PYTORCH_QNNPACK_BUILD_TESTS OFF CACHE BOOL "")
559
set(PYTORCH_QNNPACK_BUILD_BENCHMARKS OFF CACHE BOOL "")
560
set(PYTORCH_QNNPACK_LIBRARY_TYPE "static" CACHE STRING "")
562
"${PYTORCH_QNNPACK_SOURCE_DIR}"
563
"${CONFU_DEPENDENCIES_BINARY_DIR}/pytorch_qnnpack")
564
# We build static versions of QNNPACK and pthreadpool but link
565
# them into a shared library for Caffe2, so they need PIC.
566
set_property(TARGET pytorch_qnnpack PROPERTY POSITION_INDEPENDENT_CODE ON)
567
set_property(TARGET cpuinfo PROPERTY POSITION_INDEPENDENT_CODE ON)
569
if(PYTORCH_QNNPACK_CUSTOM_THREADPOOL)
570
target_compile_definitions(
571
pytorch_qnnpack PRIVATE
572
pthreadpool_t=legacy_pthreadpool_t
573
pthreadpool_function_1d_t=legacy_pthreadpool_function_1d_t
574
pthreadpool_function_1d_tiled_t=legacy_pthreadpool_function_1d_tiled_t
575
pthreadpool_function_2d_t=legacy_pthreadpool_function_2d_t
576
pthreadpool_function_2d_tiled_t=legacy_pthreadpool_function_2d_tiled_t
577
pthreadpool_function_3d_tiled_t=legacy_pthreadpool_function_3d_tiled_t
578
pthreadpool_function_4d_tiled_t=legacy_pthreadpool_function_4d_tiled_t
579
pthreadpool_create=legacy_pthreadpool_create
580
pthreadpool_destroy=legacy_pthreadpool_destroy
581
pthreadpool_get_threads_count=legacy_pthreadpool_get_threads_count
582
pthreadpool_compute_1d=legacy_pthreadpool_compute_1d
583
pthreadpool_parallelize_1d=legacy_pthreadpool_parallelize_1d
584
pthreadpool_compute_1d_tiled=legacy_pthreadpool_compute_1d_tiled
585
pthreadpool_compute_2d=legacy_pthreadpool_compute_2d
586
pthreadpool_compute_2d_tiled=legacy_pthreadpool_compute_2d_tiled
587
pthreadpool_compute_3d_tiled=legacy_pthreadpool_compute_3d_tiled
588
pthreadpool_compute_4d_tiled=legacy_pthreadpool_compute_4d_tiled)
592
list(APPEND Caffe2_DEPENDENCY_LIBS pytorch_qnnpack)
597
include(${CMAKE_CURRENT_LIST_DIR}/External/nnpack.cmake)
600
# ---[ NNPACK is being built together with Caffe2: explicitly specify dependency
601
list(APPEND Caffe2_DEPENDENCY_LIBS nnpack)
603
include_directories(SYSTEM ${NNPACK_INCLUDE_DIRS})
604
list(APPEND Caffe2_DEPENDENCY_LIBS ${NNPACK_LIBRARIES})
607
message(WARNING "Not compiling with NNPACK. Suppress this warning with -DUSE_NNPACK=OFF")
608
caffe2_update_option(USE_NNPACK OFF)
613
if(USE_XNNPACK AND NOT USE_SYSTEM_XNNPACK)
614
if(NOT DEFINED XNNPACK_SOURCE_DIR)
615
set(XNNPACK_SOURCE_DIR "${CAFFE2_THIRD_PARTY_ROOT}/XNNPACK" CACHE STRING "XNNPACK source directory")
618
if(NOT DEFINED XNNPACK_INCLUDE_DIR)
619
set(XNNPACK_INCLUDE_DIR "${XNNPACK_SOURCE_DIR}/include" CACHE STRING "XNNPACK include directory")
622
if(NOT TARGET XNNPACK)
623
set(XNNPACK_LIBRARY_TYPE "static" CACHE STRING "")
624
set(XNNPACK_BUILD_BENCHMARKS OFF CACHE BOOL "")
625
set(XNNPACK_BUILD_TESTS OFF CACHE BOOL "")
627
# Disable ARM BF16 and FP16 vector for now; unused and causes build failures because
628
# these new ISA features may not be supported on older compilers
629
set(XNNPACK_ENABLE_ARM_BF16 OFF CACHE BOOL "")
631
# Disable AVXVNNI for now, older clang versions seem not to support it
632
# (clang 12 is where avx-vnni support is added)
633
set(XNNPACK_ENABLE_AVXVNNI OFF CACHE BOOL "")
635
# Disable I8MM For CI since clang 9 does not support neon i8mm.
636
set(XNNPACK_ENABLE_ARM_I8MM OFF CACHE BOOL "")
638
# Setting this global PIC flag for all XNNPACK targets.
639
# This is needed for Object libraries within XNNPACK which must
640
# be PIC to successfully link this static libXNNPACK with pytorch
641
set(__caffe2_CMAKE_POSITION_INDEPENDENT_CODE_FLAG ${CMAKE_POSITION_INDEPENDENT_CODE})
642
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
645
"${XNNPACK_SOURCE_DIR}"
646
"${CONFU_DEPENDENCIES_BINARY_DIR}/XNNPACK")
648
# Revert to whatever it was before
649
set(CMAKE_POSITION_INDEPENDENT_CODE ${__caffe2_CMAKE_POSITION_INDEPENDENT_CODE_FLAG})
652
include_directories(SYSTEM ${XNNPACK_INCLUDE_DIR})
653
list(APPEND Caffe2_DEPENDENCY_LIBS XNNPACK)
654
elseif(NOT TARGET XNNPACK AND USE_SYSTEM_XNNPACK)
655
add_library(XNNPACK SHARED IMPORTED)
656
find_library(XNNPACK_LIBRARY XNNPACK)
657
set_property(TARGET XNNPACK PROPERTY IMPORTED_LOCATION "${XNNPACK_LIBRARY}")
658
if(NOT XNNPACK_LIBRARY)
659
message(FATAL_ERROR "Cannot find XNNPACK")
661
message("-- Found XNNPACK: ${XNNPACK_LIBRARY}")
662
list(APPEND Caffe2_DEPENDENCY_LIBS XNNPACK)
670
include(${CMAKE_CURRENT_LIST_DIR}/VulkanDependencies.cmake)
671
string(APPEND CMAKE_CXX_FLAGS ${Vulkan_DEFINES})
672
include_directories(SYSTEM ${Vulkan_INCLUDES})
673
list(APPEND Caffe2_DEPENDENCY_LIBS ${Vulkan_LIBS})
678
include(${CMAKE_CURRENT_LIST_DIR}/public/gflags.cmake)
679
if(NOT TARGET gflags)
681
"gflags is not found. Caffe2 will build without gflags support but "
682
"it is strongly recommended that you install gflags. Suppress this "
683
"warning with -DUSE_GFLAGS=OFF")
684
caffe2_update_option(USE_GFLAGS OFF)
690
include(${CMAKE_CURRENT_LIST_DIR}/public/glog.cmake)
691
if(TARGET glog::glog)
692
set(CAFFE2_USE_GOOGLE_GLOG 1)
695
"glog is not found. Caffe2 will build without glog support but it is "
696
"strongly recommended that you install glog. Suppress this warning "
697
"with -DUSE_GLOG=OFF")
698
caffe2_update_option(USE_GLOG OFF)
703
# ---[ Googletest and benchmark
704
if(BUILD_TEST OR BUILD_MOBILE_BENCHMARK OR BUILD_MOBILE_TEST)
705
# Preserve build options.
706
set(TEMP_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS})
708
# We will build gtest as static libs and embed it directly into the binary.
709
set(BUILD_SHARED_LIBS OFF CACHE BOOL "Build shared libs" FORCE)
711
# For gtest, we will simply embed it into our test binaries, so we won't
712
# need to install it.
713
set(INSTALL_GTEST OFF CACHE BOOL "Install gtest." FORCE)
714
set(BUILD_GMOCK ON CACHE BOOL "Build gmock." FORCE)
715
# For Windows, we will check the runtime used is correctly passed in.
716
if(NOT CAFFE2_USE_MSVC_STATIC_RUNTIME)
717
set(gtest_force_shared_crt ON CACHE BOOL "force shared crt on gtest" FORCE)
719
# We need to replace googletest cmake scripts too.
720
# Otherwise, it will sometimes break the build.
721
# To make the git clean after the build, we make a backup first.
722
if((MSVC AND MSVC_Z7_OVERRIDE) OR USE_CUDA)
724
COMMAND ${CMAKE_COMMAND}
725
"-DFILENAME=${CMAKE_CURRENT_LIST_DIR}/../third_party/googletest/googletest/cmake/internal_utils.cmake"
726
"-DBACKUP=${CMAKE_CURRENT_LIST_DIR}/../third_party/googletest/googletest/cmake/internal_utils.cmake.bak"
729
"${CMAKE_CURRENT_LIST_DIR}/GoogleTestPatch.cmake"
730
RESULT_VARIABLE _exitcode)
731
if(NOT _exitcode EQUAL 0)
732
message(WARNING "Patching failed for Google Test. The build may fail.")
736
# Add googletest subdirectory but make sure our INCLUDE_DIRECTORIES
737
# don't bleed into it. This is because libraries installed into the root conda
738
# env (e.g. MKL) add a global /opt/conda/include directory, and if there's
739
# gtest installed in conda, the third_party/googletest/**.cc source files
740
# would try to include headers from /opt/conda/include/gtest/**.h instead of
741
# its own. Once we have proper target-based include directories,
742
# this shouldn't be necessary anymore.
743
get_property(INC_DIR_temp DIRECTORY PROPERTY INCLUDE_DIRECTORIES)
744
set_property(DIRECTORY PROPERTY INCLUDE_DIRECTORIES "")
745
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../third_party/googletest)
746
set_property(DIRECTORY PROPERTY INCLUDE_DIRECTORIES ${INC_DIR_temp})
748
include_directories(BEFORE SYSTEM ${CMAKE_CURRENT_LIST_DIR}/../third_party/googletest/googletest/include)
749
include_directories(BEFORE SYSTEM ${CMAKE_CURRENT_LIST_DIR}/../third_party/googletest/googlemock/include)
751
# We will not need to test benchmark lib itself.
752
set(BENCHMARK_ENABLE_TESTING OFF CACHE BOOL "Disable benchmark testing as we don't need it.")
753
# We will not need to install benchmark since we link it statically.
754
set(BENCHMARK_ENABLE_INSTALL OFF CACHE BOOL "Disable benchmark install to avoid overwriting vendor install.")
755
if(NOT USE_SYSTEM_BENCHMARK)
756
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../third_party/benchmark)
758
add_library(benchmark SHARED IMPORTED)
759
find_library(BENCHMARK_LIBRARY benchmark)
760
if(NOT BENCHMARK_LIBRARY)
761
message(FATAL_ERROR "Cannot find google benchmark library")
763
message("-- Found benchmark: ${BENCHMARK_LIBRARY}")
764
set_property(TARGET benchmark PROPERTY IMPORTED_LOCATION ${BENCHMARK_LIBRARY})
766
include_directories(${CMAKE_CURRENT_LIST_DIR}/../third_party/benchmark/include)
768
# Recover build options.
769
set(BUILD_SHARED_LIBS ${TEMP_BUILD_SHARED_LIBS} CACHE BOOL "Build shared libs" FORCE)
771
# To make the git clean after the build, we revert the changes here.
772
if(MSVC AND MSVC_Z7_OVERRIDE)
774
COMMAND ${CMAKE_COMMAND}
775
"-DFILENAME=${CMAKE_CURRENT_LIST_DIR}/../third_party/googletest/googletest/cmake/internal_utils.cmake"
776
"-DBACKUP=${CMAKE_CURRENT_LIST_DIR}/../third_party/googletest/googletest/cmake/internal_utils.cmake.bak"
779
"${CMAKE_CURRENT_LIST_DIR}/GoogleTestPatch.cmake"
780
RESULT_VARIABLE _exitcode)
781
if(NOT _exitcode EQUAL 0)
782
message(WARNING "Reverting changes failed for Google Test. The build may fail.")
786
# Cacheing variables to enable incremental build.
787
# Without this is cross compiling we end up having to blow build directory
788
# and rebuild from scratch.
789
if(CMAKE_CROSSCOMPILING)
790
if(COMPILE_HAVE_STD_REGEX)
791
set(RUN_HAVE_STD_REGEX 0 CACHE INTERNAL "Cache RUN_HAVE_STD_REGEX output for cross-compile.")
798
set(CAFFE2_THIRD_PARTY_ROOT "${PROJECT_SOURCE_DIR}/third_party")
799
if(NOT DEFINED FBGEMM_SOURCE_DIR)
800
set(FBGEMM_SOURCE_DIR "${CAFFE2_THIRD_PARTY_ROOT}/fbgemm" CACHE STRING "FBGEMM source directory")
802
if(NOT CAFFE2_COMPILER_SUPPORTS_AVX512_EXTENSIONS)
804
"A compiler with AVX512 support is required for FBGEMM. "
805
"Not compiling with FBGEMM. "
806
"Turn this warning off by USE_FBGEMM=OFF.")
809
if(NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
811
"x64 operating system is required for FBGEMM. "
812
"Not compiling with FBGEMM. "
813
"Turn this warning off by USE_FBGEMM=OFF.")
816
if(USE_FBGEMM AND NOT TARGET fbgemm)
817
set(FBGEMM_BUILD_TESTS OFF CACHE BOOL "")
818
set(FBGEMM_BUILD_BENCHMARKS OFF CACHE BOOL "")
819
if(MSVC AND BUILD_SHARED_LIBS)
820
set(FBGEMM_LIBRARY_TYPE "shared" CACHE STRING "")
822
set(FBGEMM_LIBRARY_TYPE "static" CACHE STRING "")
825
set(USE_SANITIZER "address,undefined" CACHE STRING "-fsanitize options for FBGEMM")
827
add_subdirectory("${FBGEMM_SOURCE_DIR}")
828
set_property(TARGET fbgemm_generic PROPERTY POSITION_INDEPENDENT_CODE ON)
829
set_property(TARGET fbgemm_avx2 PROPERTY POSITION_INDEPENDENT_CODE ON)
830
set_property(TARGET fbgemm_avx512 PROPERTY POSITION_INDEPENDENT_CODE ON)
831
set_property(TARGET fbgemm PROPERTY POSITION_INDEPENDENT_CODE ON)
832
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 13.0.0)
833
# See https://github.com/pytorch/pytorch/issues/74352
834
target_compile_options_if_supported(asmjit -Wno-deprecated-copy)
835
target_compile_options_if_supported(asmjit -Wno-unused-but-set-variable)
840
list(APPEND Caffe2_DEPENDENCY_LIBS fbgemm)
845
caffe2_update_option(USE_FBGEMM ON)
847
caffe2_update_option(USE_FBGEMM OFF)
849
"Turning USE_FAKELOWP off as it depends on USE_FBGEMM.")
850
caffe2_update_option(USE_FAKELOWP OFF)
857
include_directories(SYSTEM ${LMDB_INCLUDE_DIR})
858
list(APPEND Caffe2_DEPENDENCY_LIBS ${LMDB_LIBRARIES})
860
message(WARNING "Not compiling with LMDB. Suppress this warning with -DUSE_LMDB=OFF")
861
caffe2_update_option(USE_LMDB OFF)
866
message(INFO "USING OPENCL")
867
find_package(OpenCL REQUIRED)
868
include_directories(SYSTEM ${OpenCL_INCLUDE_DIRS})
869
include_directories(${CMAKE_CURRENT_LIST_DIR}/../caffe2/contrib/opencl)
870
list(APPEND Caffe2_DEPENDENCY_LIBS ${OpenCL_LIBRARIES})
876
find_package(LevelDB)
878
if(LEVELDB_FOUND AND SNAPPY_FOUND)
879
include_directories(SYSTEM ${LevelDB_INCLUDE})
880
list(APPEND Caffe2_DEPENDENCY_LIBS ${LevelDB_LIBRARIES})
881
include_directories(SYSTEM ${Snappy_INCLUDE_DIR})
882
list(APPEND Caffe2_DEPENDENCY_LIBS ${Snappy_LIBRARIES})
884
message(WARNING "Not compiling with LevelDB. Suppress this warning with -DUSE_LEVELDB=OFF")
885
caffe2_update_option(USE_LEVELDB OFF)
894
message(WARNING "Not compiling with NUMA. Suppress this warning with -DUSE_NUMA=OFF")
895
caffe2_update_option(USE_NUMA OFF)
898
message(WARNING "NUMA is currently only supported under Linux.")
899
caffe2_update_option(USE_NUMA OFF)
907
include_directories(SYSTEM ${ZMQ_INCLUDE_DIR})
908
list(APPEND Caffe2_DEPENDENCY_LIBS ${ZMQ_LIBRARIES})
910
message(WARNING "Not compiling with ZMQ. Suppress this warning with -DUSE_ZMQ=OFF")
911
caffe2_update_option(USE_ZMQ OFF)
917
find_package(Hiredis)
919
include_directories(SYSTEM ${Hiredis_INCLUDE})
920
list(APPEND Caffe2_DEPENDENCY_LIBS ${Hiredis_LIBRARIES})
922
message(WARNING "Not compiling with Redis. Suppress this warning with -DUSE_REDIS=OFF")
923
caffe2_update_option(USE_REDIS OFF)
931
find_package(OpenCV 4 QUIET COMPONENTS core highgui imgproc imgcodecs optflow videoio video)
934
find_package(OpenCV 3 QUIET COMPONENTS core highgui imgproc imgcodecs videoio video)
937
find_package(OpenCV QUIET COMPONENTS core highgui imgproc)
941
include_directories(SYSTEM ${OpenCV_INCLUDE_DIRS})
942
list(APPEND Caffe2_DEPENDENCY_LIBS ${OpenCV_LIBS})
943
if(MSVC AND USE_CUDA)
944
list(APPEND Caffe2_CUDA_DEPENDENCY_LIBS ${OpenCV_LIBS})
946
message(STATUS "OpenCV found (${OpenCV_CONFIG_PATH})")
948
message(WARNING "Not compiling with OpenCV. Suppress this warning with -DUSE_OPENCV=OFF")
949
caffe2_update_option(USE_OPENCV OFF)
955
find_package(FFmpeg REQUIRED)
957
message("Found FFMPEG/LibAV libraries")
958
include_directories(SYSTEM ${FFMPEG_INCLUDE_DIR})
959
list(APPEND Caffe2_DEPENDENCY_LIBS ${FFMPEG_LIBRARIES})
961
message("Not compiling with FFmpeg. Suppress this warning with -DUSE_FFMPEG=OFF")
962
caffe2_update_option(USE_FFMPEG OFF)
969
include_directories(SYSTEM ${ITT_INCLUDE_DIR})
970
list(APPEND Caffe2_DEPENDENCY_LIBS ${ITT_LIBRARIES})
971
list(APPEND TORCH_PYTHON_LINK_LIBRARIES ${ITT_LIBRARIES})
973
message(WARNING "Not compiling with ITT. Suppress this warning with -DUSE_ITT=OFF")
974
set(USE_ITT OFF CACHE BOOL "" FORCE)
975
caffe2_update_option(USE_ITT OFF)
979
# ---[ Caffe2 depends on FP16 library for half-precision conversions
980
if(NOT TARGET fp16 AND NOT USE_SYSTEM_FP16)
981
set(CAFFE2_THIRD_PARTY_ROOT "${PROJECT_SOURCE_DIR}/third_party")
982
# PSIMD is required by FP16
983
if(NOT DEFINED PSIMD_SOURCE_DIR)
984
set(PSIMD_SOURCE_DIR "${CAFFE2_THIRD_PARTY_ROOT}/psimd" CACHE STRING "PSimd source directory")
986
if(NOT DEFINED FP16_SOURCE_DIR)
987
set(FP16_SOURCE_DIR "${CAFFE2_THIRD_PARTY_ROOT}/FP16" CACHE STRING "FP16 source directory")
990
set(FP16_BUILD_TESTS OFF CACHE BOOL "")
991
set(FP16_BUILD_BENCHMARKS OFF CACHE BOOL "")
994
"${CONFU_DEPENDENCIES_BINARY_DIR}/FP16")
995
elseif(NOT TARGET fp16 AND USE_SYSTEM_FP16)
996
add_library(fp16 STATIC "/usr/include/fp16.h")
997
set_target_properties(fp16 PROPERTIES LINKER_LANGUAGE C)
999
list(APPEND Caffe2_DEPENDENCY_LIBS fp16)
1002
# Due to license considerations, we will only use the MPL2 parts of Eigen.
1003
set(EIGEN_MPL2_ONLY 1)
1004
if(USE_SYSTEM_EIGEN_INSTALL)
1005
find_package(Eigen3)
1007
message(STATUS "Found system Eigen at " ${EIGEN3_INCLUDE_DIR})
1009
message(STATUS "Did not find system Eigen. Using third party subdirectory.")
1010
set(EIGEN3_INCLUDE_DIR ${CMAKE_CURRENT_LIST_DIR}/../third_party/eigen)
1011
caffe2_update_option(USE_SYSTEM_EIGEN_INSTALL OFF)
1014
message(STATUS "Using third party subdirectory Eigen.")
1015
set(EIGEN3_INCLUDE_DIR ${CMAKE_CURRENT_LIST_DIR}/../third_party/eigen)
1017
include_directories(SYSTEM ${EIGEN3_INCLUDE_DIR})
1019
# ---[ Python + Numpy
1021
# If not given a Python installation, then use the current active Python
1022
if(NOT PYTHON_EXECUTABLE)
1024
COMMAND "which" "python" RESULT_VARIABLE _exitcode OUTPUT_VARIABLE _py_exe)
1025
if(${_exitcode} EQUAL 0)
1027
string(STRIP ${_py_exe} PYTHON_EXECUTABLE)
1029
message(STATUS "Setting Python to ${PYTHON_EXECUTABLE}")
1033
# Check that Python works
1035
if(DEFINED PYTHON_EXECUTABLE)
1037
COMMAND "${PYTHON_EXECUTABLE}" "--version"
1038
RESULT_VARIABLE _exitcode OUTPUT_VARIABLE PYTHON_VERSION)
1039
if(NOT _exitcode EQUAL 0)
1040
message(FATAL_ERROR "The Python executable ${PYTHON_EXECUTABLE} cannot be run. Make sure that it is an absolute path.")
1043
string(REGEX MATCH "([0-9]+)\\.([0-9]+)" PYTHON_VERSION ${PYTHON_VERSION})
1047
# Seed PYTHON_INCLUDE_DIR and PYTHON_LIBRARY to be consistent with the
1048
# executable that we already found (if we didn't actually find an executable
1049
# then these will just use "python", but at least they'll be consistent with
1051
if(NOT PYTHON_INCLUDE_DIR)
1052
# TODO: Verify that sysconfig isn't inaccurate
1053
pycmd_no_exit(_py_inc _exitcode "import sysconfig; print(sysconfig.get_path('include'))")
1054
if("${_exitcode}" EQUAL 0 AND IS_DIRECTORY "${_py_inc}")
1055
set(PYTHON_INCLUDE_DIR "${_py_inc}")
1056
message(STATUS "Setting Python's include dir to ${_py_inc} from sysconfig")
1058
message(WARNING "Could not set Python's include dir to ${_py_inc} from sysconfig")
1060
endif(NOT PYTHON_INCLUDE_DIR)
1062
if(NOT PYTHON_LIBRARY)
1063
pycmd_no_exit(_py_lib _exitcode "import sysconfig; print(sysconfig.get_path('stdlib'))")
1064
if("${_exitcode}" EQUAL 0 AND EXISTS "${_py_lib}" AND EXISTS "${_py_lib}")
1065
set(PYTHON_LIBRARY "${_py_lib}")
1067
string(REPLACE "Lib" "libs" _py_static_lib ${_py_lib})
1068
link_directories(${_py_static_lib})
1070
message(STATUS "Setting Python's library to ${PYTHON_LIBRARY}")
1072
endif(NOT PYTHON_LIBRARY)
1074
# These should fill in the rest of the variables, like versions, but resepct
1075
# the variables we set above
1076
set(Python_ADDITIONAL_VERSIONS ${PYTHON_VERSION} 3.8)
1077
find_package(PythonInterp 3.0)
1078
find_package(PythonLibs 3.0)
1080
if(NOT PYTHONLIBS_VERSION_STRING)
1082
"Python development libraries could not be found.")
1085
if(${PYTHONLIBS_VERSION_STRING} VERSION_LESS 3)
1087
"Found Python libraries version ${PYTHONLIBS_VERSION_STRING}. Python 2 has reached end-of-life and is no longer supported by PyTorch.")
1089
if(${PYTHONLIBS_VERSION_STRING} VERSION_LESS 3.8)
1091
"Found Python libraries version ${PYTHONLIBS_VERSION_STRING}. Python < 3.8 is no longer supported by PyTorch.")
1094
# When building pytorch, we pass this in directly from setup.py, and
1095
# don't want to overwrite it because we trust python more than cmake
1096
if(NUMPY_INCLUDE_DIR)
1101
message(WARNING "NumPy could not be found. Not building with NumPy. Suppress this warning with -DUSE_NUMPY=OFF")
1105
if(PYTHONINTERP_FOUND AND PYTHONLIBS_FOUND)
1106
add_library(python::python INTERFACE IMPORTED)
1107
target_include_directories(python::python SYSTEM INTERFACE ${PYTHON_INCLUDE_DIRS})
1109
target_link_libraries(python::python INTERFACE ${PYTHON_LIBRARIES})
1112
caffe2_update_option(USE_NUMPY OFF)
1114
caffe2_update_option(USE_NUMPY ON)
1115
add_library(numpy::numpy INTERFACE IMPORTED)
1116
target_include_directories(numpy::numpy SYSTEM INTERFACE ${NUMPY_INCLUDE_DIR})
1118
# Observers are required in the python build
1119
caffe2_update_option(USE_OBSERVERS ON)
1121
message(WARNING "Python dependencies not met. Not compiling with python. Suppress this warning with -DBUILD_PYTHON=OFF")
1122
caffe2_update_option(BUILD_PYTHON OFF)
1127
if(USE_SYSTEM_PYBIND11)
1128
find_package(pybind11 CONFIG)
1129
if(NOT pybind11_FOUND)
1130
find_package(pybind11)
1132
if(NOT pybind11_FOUND)
1133
message(FATAL "Cannot find system pybind11")
1136
message(STATUS "Using third_party/pybind11.")
1137
set(pybind11_INCLUDE_DIRS ${CMAKE_CURRENT_LIST_DIR}/../third_party/pybind11/include)
1138
install(DIRECTORY ${pybind11_INCLUDE_DIRS}
1139
DESTINATION ${CMAKE_INSTALL_PREFIX}
1140
FILES_MATCHING PATTERN "*.h")
1142
message(STATUS "pybind11 include dirs: " "${pybind11_INCLUDE_DIRS}")
1143
add_library(pybind::pybind11 INTERFACE IMPORTED)
1144
target_include_directories(pybind::pybind11 SYSTEM INTERFACE ${pybind11_INCLUDE_DIRS})
1145
target_link_libraries(pybind::pybind11 INTERFACE python::python)
1147
target_link_options(pybind::pybind11 INTERFACE -undefined dynamic_lookup)
1154
message(STATUS "MPI support found")
1155
message(STATUS "MPI compile flags: " ${MPI_CXX_COMPILE_FLAGS})
1156
message(STATUS "MPI include path: " ${MPI_CXX_INCLUDE_PATH})
1157
message(STATUS "MPI LINK flags path: " ${MPI_CXX_LINK_FLAGS})
1158
message(STATUS "MPI libraries: " ${MPI_CXX_LIBRARIES})
1159
find_program(OMPI_INFO
1161
HINTS ${MPI_CXX_LIBRARIES}/../bin)
1163
execute_process(COMMAND ${OMPI_INFO}
1164
OUTPUT_VARIABLE _output)
1165
if(_output MATCHES "smcuda")
1166
message(STATUS "Found OpenMPI with CUDA support built.")
1168
message(WARNING "OpenMPI found, but it is not built with CUDA support.")
1169
set(CAFFE2_FORCE_FALLBACK_CUDA_MPI 1)
1173
message(WARNING "Not compiling with MPI. Suppress this warning with -DUSE_MPI=OFF")
1174
caffe2_update_option(USE_MPI OFF)
1179
if(USE_OPENMP AND NOT TARGET caffe2::openmp)
1180
include(${CMAKE_CURRENT_LIST_DIR}/Modules/FindOpenMP.cmake)
1182
message(STATUS "Adding OpenMP CXX_FLAGS: " ${OpenMP_CXX_FLAGS})
1183
if(APPLE AND USE_MPS)
1184
string(APPEND CMAKE_OBJCXX_FLAGS " ${OpenMP_CXX_FLAGS}")
1186
if(OpenMP_CXX_LIBRARIES)
1187
message(STATUS "Will link against OpenMP libraries: ${OpenMP_CXX_LIBRARIES}")
1189
add_library(caffe2::openmp INTERFACE IMPORTED)
1190
target_link_libraries(caffe2::openmp INTERFACE OpenMP::OpenMP_CXX)
1191
list(APPEND Caffe2_DEPENDENCY_LIBS caffe2::openmp)
1192
if(MSVC AND OpenMP_CXX_LIBRARIES MATCHES ".*libiomp5md\\.lib.*")
1193
target_compile_definitions(caffe2::openmp INTERFACE _OPENMP_NOFORCE_MANIFEST)
1194
target_link_options(caffe2::openmp INTERFACE "/NODEFAULTLIB:vcomp")
1197
message(WARNING "Not compiling with OpenMP. Suppress this warning with -DUSE_OPENMP=OFF")
1198
caffe2_update_option(USE_OPENMP OFF)
1204
# ---[ Android specific ones
1206
list(APPEND Caffe2_DEPENDENCY_LIBS log)
1211
message(STATUS "Looking for LLVM in ${USE_LLVM}")
1212
find_package(LLVM PATHS ${USE_LLVM} NO_DEFAULT_PATH)
1215
message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
1216
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
1218
include_directories(${LLVM_INCLUDE_DIRS})
1219
add_definitions(-DTORCH_ENABLE_LLVM)
1225
if(CUDNN_VERSION VERSION_LESS 8.5)
1226
message(FATAL_ERROR "PyTorch needs CuDNN-8.5 or above, but found ${CUDNN_VERSION}. Builds are still possible with `USE_CUDNN=0`")
1228
set(CUDNN_FRONTEND_INCLUDE_DIR ${CMAKE_CURRENT_LIST_DIR}/../third_party/cudnn_frontend/include)
1229
target_include_directories(torch::cudnn INTERFACE ${CUDNN_FRONTEND_INCLUDE_DIR})
1234
# This prevents linking in the libtinfo from /opt/conda/lib which conflicts with ROCm libtinfo.
1235
# Currently only active for Ubuntu 20.04 and greater versions.
1236
if(UNIX AND EXISTS "/etc/os-release")
1237
file(STRINGS /etc/os-release OS_RELEASE)
1238
string(REGEX REPLACE "NAME=\"([A-Za-z]+).*" "\\1" OS_DISTRO ${OS_RELEASE})
1239
string(REGEX REPLACE ".*VERSION_ID=\"([0-9\.]+).*" "\\1" OS_VERSION ${OS_RELEASE})
1240
if(OS_DISTRO STREQUAL "Ubuntu" AND OS_VERSION VERSION_GREATER_EQUAL "20.04")
1241
find_library(LIBTINFO_LOC tinfo NO_CMAKE_PATH NO_CMAKE_ENVIRONMENT_PATH)
1243
get_filename_component(LIBTINFO_LOC_PARENT ${LIBTINFO_LOC} DIRECTORY)
1244
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-rpath-link,${LIBTINFO_LOC_PARENT}")
1249
include(${CMAKE_CURRENT_LIST_DIR}/public/LoadHIP.cmake)
1250
if(PYTORCH_FOUND_HIP)
1251
message(INFO "Compiling with HIP for AMD.")
1252
caffe2_update_option(USE_ROCM ON)
1254
if(USE_NCCL AND NOT USE_SYSTEM_NCCL)
1255
message(INFO "Forcing USE_SYSTEM_NCCL to ON since it's required by using RCCL")
1256
caffe2_update_option(USE_SYSTEM_NCCL ON)
1260
list(APPEND HIP_CXX_FLAGS -fPIC)
1261
list(APPEND HIP_CXX_FLAGS -D__HIP_PLATFORM_AMD__=1)
1262
list(APPEND HIP_CXX_FLAGS -DCUDA_HAS_FP16=1)
1263
list(APPEND HIP_CXX_FLAGS -DUSE_ROCM)
1264
list(APPEND HIP_CXX_FLAGS -D__HIP_NO_HALF_OPERATORS__=1)
1265
list(APPEND HIP_CXX_FLAGS -D__HIP_NO_HALF_CONVERSIONS__=1)
1266
list(APPEND HIP_CXX_FLAGS -DTORCH_HIP_VERSION=${TORCH_HIP_VERSION})
1267
list(APPEND HIP_CXX_FLAGS -Wno-shift-count-negative)
1268
list(APPEND HIP_CXX_FLAGS -Wno-shift-count-overflow)
1269
list(APPEND HIP_CXX_FLAGS -Wno-duplicate-decl-specifier)
1270
list(APPEND HIP_CXX_FLAGS -DCAFFE2_USE_MIOPEN)
1271
list(APPEND HIP_CXX_FLAGS -DTHRUST_DEVICE_SYSTEM=THRUST_DEVICE_SYSTEM_HIP)
1272
list(APPEND HIP_CXX_FLAGS -std=c++17)
1273
if(ROCM_VERSION_DEV VERSION_GREATER_EQUAL "6.0.0")
1274
list(APPEND HIP_CXX_FLAGS -DHIPBLAS_V2)
1276
if(HIPBLASLT_CUSTOM_DATA_TYPE)
1277
list(APPEND HIP_CXX_FLAGS -DHIPBLASLT_CUSTOM_DATA_TYPE)
1279
if(HIPBLASLT_CUSTOM_COMPUTE_TYPE)
1280
list(APPEND HIP_CXX_FLAGS -DHIPBLASLT_CUSTOM_COMPUTE_TYPE)
1282
if(HIPBLASLT_HAS_GETINDEXFROMALGO)
1283
list(APPEND HIP_CXX_FLAGS -DHIPBLASLT_HAS_GETINDEXFROMALGO)
1285
if(HIP_NEW_TYPE_ENUMS)
1286
list(APPEND HIP_CXX_FLAGS -DHIP_NEW_TYPE_ENUMS)
1288
add_definitions(-DROCM_VERSION=${ROCM_VERSION_DEV_INT})
1289
add_definitions(-DTORCH_HIP_VERSION=${TORCH_HIP_VERSION})
1290
message("TORCH_HIP_VERSION=${TORCH_HIP_VERSION} is added as a compiler defines")
1292
if(CMAKE_BUILD_TYPE MATCHES Debug)
1293
list(APPEND HIP_CXX_FLAGS -g2)
1294
list(APPEND HIP_CXX_FLAGS -O0)
1295
list(APPEND HIP_HIPCC_FLAGS -fdebug-info-for-profiling)
1296
endif(CMAKE_BUILD_TYPE MATCHES Debug)
1298
set(HIP_CLANG_FLAGS ${HIP_CXX_FLAGS})
1299
# Ask hcc to generate device code during compilation so we can use
1300
# host linker to link.
1301
list(APPEND HIP_CLANG_FLAGS -fno-gpu-rdc)
1302
foreach(pytorch_rocm_arch ${PYTORCH_ROCM_ARCH})
1303
list(APPEND HIP_CLANG_FLAGS --offload-arch=${pytorch_rocm_arch})
1306
set(Caffe2_HIP_INCLUDE
1307
$<INSTALL_INTERFACE:include> ${Caffe2_HIP_INCLUDE})
1308
# This is needed for library added by hip_add_library (same for hip_add_executable)
1309
hip_include_directories(${Caffe2_HIP_INCLUDE})
1311
set(Caffe2_PUBLIC_HIP_DEPENDENCY_LIBS
1312
${PYTORCH_HIP_LIBRARIES} ${PYTORCH_MIOPEN_LIBRARIES} ${hipcub_LIBRARIES} ${ROCM_HIPRTC_LIB} ${ROCM_ROCTX_LIB})
1313
if(ROCM_VERSION_DEV VERSION_GREATER_EQUAL "5.7.0")
1314
list(APPEND Caffe2_PUBLIC_HIP_DEPENDENCY_LIBS ${hipblaslt_LIBRARIES})
1317
list(APPEND Caffe2_PUBLIC_HIP_DEPENDENCY_LIBS
1318
roc::hipblas hip::hipfft hip::hiprand roc::hipsparse roc::hipsolver)
1320
# ---[ Kernel asserts
1321
# Kernel asserts is disabled for ROCm by default.
1322
# It can be turned on by turning on the env USE_ROCM_KERNEL_ASSERT to the build system.
1323
if(USE_ROCM_KERNEL_ASSERT)
1324
message(STATUS "Enabling Kernel Assert for ROCm")
1326
message(STATUS "Disabling Kernel Assert for ROCm")
1329
if(USE_FLASH_ATTENTION)
1330
include(${CMAKE_CURRENT_LIST_DIR}/External/oort.cmake)
1333
caffe2_update_option(USE_MEM_EFF_ATTENTION OFF)
1336
caffe2_update_option(USE_ROCM OFF)
1341
if(USE_ROCM AND ROCM_VERSION_DEV VERSION_LESS "5.2.0")
1342
# We check again for USE_ROCM because it might have been set to OFF
1344
include_directories(SYSTEM ${HIP_PATH}/include)
1345
include_directories(SYSTEM ${HIPBLAS_PATH}/include)
1346
include_directories(SYSTEM ${HIPFFT_PATH}/include)
1347
include_directories(SYSTEM ${HIPSPARSE_PATH}/include)
1348
include_directories(SYSTEM ${HIPRAND_PATH}/include)
1349
include_directories(SYSTEM ${THRUST_PATH})
1354
if(NOT (USE_CUDA OR USE_ROCM))
1356
"Not using CUDA/ROCM, so disabling USE_NCCL. Suppress this warning with "
1358
caffe2_update_option(USE_NCCL OFF)
1359
elseif(NOT CMAKE_SYSTEM_NAME STREQUAL "Linux")
1360
message(WARNING "NCCL is currently only supported under Linux.")
1361
caffe2_update_option(USE_NCCL OFF)
1363
include(${CMAKE_CURRENT_LIST_DIR}/External/nccl.cmake)
1364
list(APPEND Caffe2_CUDA_DEPENDENCY_LIBS __caffe2_nccl)
1366
include(${CMAKE_CURRENT_LIST_DIR}/External/rccl.cmake)
1367
list(APPEND Caffe2_CUDA_DEPENDENCY_LIBS __caffe2_nccl)
1373
if(NOT CMAKE_SYSTEM_NAME STREQUAL "Linux")
1374
message(WARNING "UCC is currently only supported under Linux.")
1375
caffe2_update_option(USE_UCC OFF)
1377
include(${CMAKE_CURRENT_LIST_DIR}/External/ucc.cmake)
1385
include_directories(SYSTEM ${CUB_INCLUDE_DIRS})
1387
include_directories(SYSTEM ${CMAKE_CURRENT_LIST_DIR}/../third_party/cub)
1391
if(USE_DISTRIBUTED AND USE_TENSORPIPE)
1393
message(WARNING "Tensorpipe cannot be used on Windows.")
1396
set(TP_USE_CUDA ON CACHE BOOL "" FORCE)
1397
set(TP_ENABLE_CUDA_IPC ON CACHE BOOL "" FORCE)
1399
set(TP_BUILD_LIBUV ON CACHE BOOL "" FORCE)
1400
add_compile_options(-DTORCH_USE_LIBUV)
1401
include_directories(BEFORE SYSTEM ${CMAKE_CURRENT_LIST_DIR}/../third_party/tensorpipe/third_party/libuv/include)
1402
set(TP_STATIC_OR_SHARED STATIC CACHE STRING "" FORCE)
1404
# Tensorpipe uses cuda_add_library
1405
torch_update_find_cuda_flags()
1406
add_subdirectory(${PROJECT_SOURCE_DIR}/third_party/tensorpipe)
1408
list(APPEND Caffe2_DEPENDENCY_LIBS tensorpipe)
1410
list(APPEND Caffe2_CUDA_DEPENDENCY_LIBS tensorpipe_cuda)
1412
message(WARNING "TensorPipe doesn't yet support ROCm")
1414
# list(APPEND Caffe2_HIP_DEPENDENCY_LIBS tensorpipe_hip)
1420
if(NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
1421
message(WARNING "Gloo can only be used on 64-bit systems.")
1422
caffe2_update_option(USE_GLOO OFF)
1424
# Don't install gloo
1425
set(GLOO_INSTALL OFF CACHE BOOL "" FORCE)
1426
set(GLOO_STATIC_OR_SHARED STATIC CACHE STRING "" FORCE)
1428
# Temporarily override variables to avoid building Gloo tests/benchmarks
1429
set(__BUILD_TEST ${BUILD_TEST})
1430
set(__BUILD_BENCHMARK ${BUILD_BENCHMARK})
1432
set(BUILD_BENCHMARK OFF)
1434
set(ENV{GLOO_ROCM_ARCH} "${PYTORCH_ROCM_ARCH}")
1436
if(NOT USE_SYSTEM_GLOO)
1437
if(USE_DISTRIBUED AND USE_TENSORPIPE)
1438
get_target_property(_include_dirs uv_a INCLUDE_DIRECTORIES)
1439
set_target_properties(uv_a PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${_include_dirs}")
1441
if(USE_NCCL AND NOT USE_SYSTEM_NCCL)
1442
# Tell Gloo build system to use bundled NCCL, see
1443
# https://github.com/facebookincubator/gloo/blob/950c0e23819779a9e0c70b861db4c52b31d1d1b2/cmake/Dependencies.cmake#L123
1444
set(NCCL_EXTERNAL ON)
1446
set(GLOO_USE_CUDA_TOOLKIT ON CACHE BOOL "" FORCE)
1447
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../third_party/gloo)
1449
add_library(gloo SHARED IMPORTED)
1450
find_library(GLOO_LIBRARY gloo)
1451
if(NOT GLOO_LIBRARY)
1452
message(FATAL_ERROR "Cannot find gloo")
1454
message("Found gloo: ${GLOO_LIBRARY}")
1455
set_target_properties(gloo PROPERTIES IMPORTED_LOCATION ${GLOO_LIBRARY})
1457
# Here is a little bit hacky. We have to put PROJECT_BINARY_DIR in front
1458
# of PROJECT_SOURCE_DIR with/without conda system. The reason is that
1459
# gloo generates a new config.h in the binary diretory.
1460
include_directories(BEFORE SYSTEM ${CMAKE_CURRENT_LIST_DIR}/../third_party/gloo)
1461
include_directories(BEFORE SYSTEM ${PROJECT_BINARY_DIR}/third_party/gloo)
1462
set(BUILD_TEST ${__BUILD_TEST})
1463
set(BUILD_BENCHMARK ${__BUILD_BENCHMARK})
1465
# Add explicit dependency since NCCL is built from third_party.
1466
# Without dependency, make -jN with N>1 can fail if the NCCL build
1467
# hasn't finished when CUDA targets are linked.
1468
if(NOT USE_SYSTEM_NCCL AND USE_NCCL AND NOT USE_ROCM)
1469
add_dependencies(gloo_cuda nccl_external)
1471
# Pick the right dependency depending on USE_CUDA
1472
list(APPEND Caffe2_DEPENDENCY_LIBS gloo)
1474
list(APPEND Caffe2_CUDA_DEPENDENCY_LIBS gloo_cuda)
1476
list(APPEND Caffe2_HIP_DEPENDENCY_LIBS gloo_hip)
1478
add_compile_options(-DCAFFE2_USE_GLOO)
1484
find_package(htrace)
1486
set(USE_PROF_HTRACE ON)
1488
message(WARNING "htrace not found. Caffe2 will build without htrace prof")
1492
if(USE_SNPE AND ANDROID)
1493
if(SNPE_LOCATION AND SNPE_HEADERS)
1494
message(STATUS "Using SNPE location specified by -DSNPE_LOCATION: " ${SNPE_LOCATION})
1495
message(STATUS "Using SNPE headers specified by -DSNPE_HEADERS: " ${SNPE_HEADERS})
1496
include_directories(SYSTEM ${SNPE_HEADERS})
1497
add_library(snpe SHARED IMPORTED)
1498
set_property(TARGET snpe PROPERTY IMPORTED_LOCATION ${SNPE_LOCATION})
1499
list(APPEND Caffe2_DEPENDENCY_LIBS snpe)
1501
caffe2_update_option(USE_SNPE OFF)
1507
message(WARNING "Metal is only used in ios builds.")
1508
caffe2_update_option(USE_METAL OFF)
1512
if(USE_NNAPI AND NOT ANDROID)
1513
message(WARNING "NNApi is only used in android builds.")
1514
caffe2_update_option(USE_NNAPI OFF)
1517
if(NOT INTERN_BUILD_MOBILE AND BUILD_CAFFE2_OPS)
1518
if(CAFFE2_CMAKE_BUILDING_WITH_MAIN_REPO)
1519
list(APPEND Caffe2_DEPENDENCY_LIBS aten_op_header_gen)
1521
list(APPEND Caffe2_CUDA_DEPENDENCY_LIBS aten_op_header_gen)
1523
include_directories(${PROJECT_BINARY_DIR}/caffe2/contrib/aten)
1529
find_package(zstd REQUIRED)
1530
if(TARGET zstd::libzstd_shared)
1531
set(ZSTD_TARGET zstd::libzstd_shared)
1533
set(ZSTD_TARGET zstd::libzstd_static)
1535
list(APPEND Caffe2_DEPENDENCY_LIBS ${ZSTD_TARGET})
1536
get_property(ZSTD_INCLUDE_DIR TARGET ${ZSTD_TARGET} PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
1537
include_directories(SYSTEM ${ZSTD_INCLUDE_DIR})
1539
list(APPEND Caffe2_DEPENDENCY_LIBS libzstd_static)
1540
include_directories(SYSTEM ${CMAKE_CURRENT_LIST_DIR}/../third_party/zstd/lib)
1541
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../third_party/zstd/build/cmake)
1542
set_property(TARGET libzstd_static PROPERTY POSITION_INDEPENDENT_CODE ON)
1547
if(CAFFE2_CMAKE_BUILDING_WITH_MAIN_REPO AND NOT INTERN_DISABLE_ONNX)
1548
if(EXISTS "${CAFFE2_CUSTOM_PROTOC_EXECUTABLE}")
1549
set(ONNX_CUSTOM_PROTOC_EXECUTABLE ${CAFFE2_CUSTOM_PROTOC_EXECUTABLE})
1551
set(TEMP_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS})
1552
set(BUILD_SHARED_LIBS OFF)
1553
set(ONNX_USE_MSVC_STATIC_RUNTIME ${CAFFE2_USE_MSVC_STATIC_RUNTIME})
1554
set(ONNX_USE_LITE_PROTO ${CAFFE2_USE_LITE_PROTO})
1555
# If linking local protobuf, make sure ONNX has the same protobuf
1556
# patches as Caffe2 and Caffe proto. This forces some functions to
1557
# not be inline and instead route back to the statically-linked protobuf.
1558
if(CAFFE2_LINK_LOCAL_PROTOBUF)
1559
set(ONNX_PROTO_POST_BUILD_SCRIPT ${PROJECT_SOURCE_DIR}/cmake/ProtoBufPatch.cmake)
1562
add_definitions(-DONNX_ML=1)
1564
add_definitions(-DONNXIFI_ENABLE_EXT=1)
1565
# Add op schemas in "ai.onnx.pytorch" domain
1566
add_subdirectory("${CMAKE_CURRENT_LIST_DIR}/../caffe2/onnx/torch_ops")
1567
if(NOT USE_SYSTEM_ONNX)
1568
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../third_party/onnx EXCLUDE_FROM_ALL)
1570
set_target_properties(onnx_proto PROPERTIES CXX_STANDARD 17)
1573
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../third_party/foxi EXCLUDE_FROM_ALL)
1575
add_definitions(-DONNX_NAMESPACE=${ONNX_NAMESPACE})
1576
if(NOT USE_SYSTEM_ONNX)
1577
include_directories(${ONNX_INCLUDE_DIRS})
1578
# In mobile build we care about code size, and so we need drop
1579
# everything (e.g. checker) in onnx but the pb definition.
1581
caffe2_interface_library(onnx_proto onnx_library)
1583
caffe2_interface_library(onnx onnx_library)
1585
list(APPEND Caffe2_DEPENDENCY_WHOLE_LINK_LIBS onnx_library)
1586
# TODO: Delete this line once https://github.com/pytorch/pytorch/pull/55889 lands
1587
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
1588
target_compile_options(onnx PRIVATE -Wno-deprecated-declarations)
1591
add_library(onnx SHARED IMPORTED)
1592
find_library(ONNX_LIBRARY onnx)
1593
if(NOT ONNX_LIBRARY)
1594
message(FATAL_ERROR "Cannot find onnx")
1596
set_property(TARGET onnx PROPERTY IMPORTED_LOCATION ${ONNX_LIBRARY})
1597
add_library(onnx_proto SHARED IMPORTED)
1598
find_library(ONNX_PROTO_LIBRARY onnx_proto)
1599
if(NOT ONNX_PROTO_LIBRARY)
1600
message(FATAL_ERROR "Cannot find onnx")
1602
set_property(TARGET onnx_proto PROPERTY IMPORTED_LOCATION ${ONNX_PROTO_LIBRARY})
1603
message("-- Found onnx: ${ONNX_LIBRARY} ${ONNX_PROTO_LIBRARY}")
1604
list(APPEND Caffe2_DEPENDENCY_LIBS onnx_proto onnx)
1606
include_directories(${FOXI_INCLUDE_DIRS})
1607
list(APPEND Caffe2_DEPENDENCY_LIBS foxi_loader)
1608
# Recover the build shared libs option.
1609
set(BUILD_SHARED_LIBS ${TEMP_BUILD_SHARED_LIBS})
1612
# --[ TensorRT integration with onnx-trt
1613
function(add_onnx_tensorrt_subdir)
1614
# We pass the paths we found to onnx tensorrt.
1615
set(CUDNN_INCLUDE_DIR "${CUDNN_INCLUDE_PATH}")
1616
set(CUDNN_LIBRARY "${CUDNN_LIBRARY_PATH}")
1617
set(CMAKE_VERSION_ORIG "{CMAKE_VERSION}")
1618
# TODO: this WAR is for https://github.com/pytorch/pytorch/issues/18524
1619
set(CMAKE_VERSION "3.9.0")
1620
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../third_party/onnx-tensorrt EXCLUDE_FROM_ALL)
1621
set(CMAKE_VERSION "{CMAKE_VERSION_ORIG}")
1623
if(CAFFE2_CMAKE_BUILDING_WITH_MAIN_REPO)
1625
add_onnx_tensorrt_subdir()
1626
include_directories("${CMAKE_CURRENT_LIST_DIR}/../third_party/onnx-tensorrt")
1627
caffe2_interface_library(nvonnxparser_static onnx_trt_library)
1628
list(APPEND Caffe2_DEPENDENCY_WHOLE_LINK_LIBS onnx_trt_library)
1629
set(CAFFE2_USE_TRT 1)
1636
# we need to build all targets to be linked with PIC
1637
if(USE_KINETO AND INTERN_BUILD_MOBILE AND USE_LITE_INTERPRETER_PROFILER)
1638
set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
1641
if(NOT INTERN_BUILD_MOBILE)
1642
set(TORCH_CUDA_ARCH_LIST $ENV{TORCH_CUDA_ARCH_LIST})
1643
string(APPEND CMAKE_CUDA_FLAGS " $ENV{TORCH_NVCC_FLAGS}")
1644
set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
1646
# Top-level build config
1647
############################################
1650
# Detect CUDA architecture and get best NVCC flags
1651
# finding cuda must be first because other things depend on the result
1653
# NB: We MUST NOT run this find_package if NOT USE_CUDA is set, because upstream
1654
# FindCUDA has a bug where it will still attempt to make use of NOTFOUND
1655
# compiler variables to run various probe tests. We could try to fix
1656
# this, but since FindCUDA upstream is subsumed by first-class support
1657
# for CUDA language, it seemed not worth fixing.
1660
# we want to respect the standard, and we are bored of those **** .
1661
add_definitions(-D_CRT_SECURE_NO_DEPRECATE=1)
1662
string(APPEND CMAKE_CUDA_FLAGS " -Xcompiler=/wd4819,/wd4503,/wd4190,/wd4244,/wd4251,/wd4275,/wd4522")
1665
string(APPEND CMAKE_CUDA_FLAGS " -Wno-deprecated-gpu-targets --expt-extended-lambda")
1667
# use cub in a safe manner, see:
1668
# https://github.com/pytorch/pytorch/pull/55292
1669
if(NOT ${CUDA_VERSION} LESS 11.5)
1670
string(APPEND CMAKE_CUDA_FLAGS " -DCUB_WRAPPED_NAMESPACE=at_cuda_detail")
1673
message(STATUS "Found CUDA with FP16 support, compiling with torch.cuda.HalfTensor")
1674
string(APPEND CMAKE_CUDA_FLAGS " -DCUDA_HAS_FP16=1"
1675
" -D__CUDA_NO_HALF_OPERATORS__"
1676
" -D__CUDA_NO_HALF_CONVERSIONS__"
1677
" -D__CUDA_NO_HALF2_OPERATORS__"
1678
" -D__CUDA_NO_BFLOAT16_CONVERSIONS__")
1680
string(APPEND CMAKE_C_FLAGS_RELEASE " -DNDEBUG")
1681
string(APPEND CMAKE_CXX_FLAGS_RELEASE " -DNDEBUG")
1682
if(NOT GENERATOR_IS_MULTI_CONFIG)
1683
if(${CMAKE_BUILD_TYPE} STREQUAL "Release")
1684
message(STATUS "Adding -DNDEBUG to compile flags")
1685
string(APPEND CMAKE_C_FLAGS " -DNDEBUG")
1686
string(APPEND CMAKE_CXX_FLAGS " -DNDEBUG")
1688
message(STATUS "Removing -DNDEBUG from compile flags")
1689
string(REGEX REPLACE "[-/]DNDEBUG" "" CMAKE_C_FLAGS "" ${CMAKE_C_FLAGS})
1690
string(REGEX REPLACE "[-/]DNDEBUG" "" CMAKE_CXX_FLAGS "" ${CMAKE_CXX_FLAGS})
1693
string(REGEX REPLACE "[-/]DNDEBUG" "" CMAKE_C_FLAGS_DEBUG "" ${CMAKE_C_FLAGS_DEBUG})
1694
string(REGEX REPLACE "[-/]DNDEBUG" "" CMAKE_CXX_FLAGS_DEBUG "" ${CMAKE_CXX_FLAGS_DEBUG})
1696
set(CUDA_ATTACH_VS_BUILD_RULE_TO_CUDA_FILE OFF)
1701
if((USE_CUDA OR USE_ROCM) AND MAGMA_FOUND)
1703
message(STATUS "Compiling with MAGMA support")
1704
message(STATUS "MAGMA INCLUDE DIRECTORIES: ${MAGMA_INCLUDE_DIR}")
1705
message(STATUS "MAGMA LIBRARIES: ${MAGMA_LIBRARIES}")
1706
message(STATUS "MAGMA V2 check: ${MAGMA_V2}")
1709
"Not compiling with MAGMA. Suppress this warning with "
1711
caffe2_update_option(USE_MAGMA OFF)
1713
message(STATUS "MAGMA not found. Compiling without MAGMA support")
1714
caffe2_update_option(USE_MAGMA OFF)
1717
# ARM specific flags
1720
message(STATUS "asimd/Neon found with compiler flag : -D__NEON__")
1721
add_compile_options(-D__NEON__)
1724
message(STATUS "Neon found with compiler flag : -D__NEON__")
1725
add_compile_options(-D__NEON__)
1727
message(STATUS "Neon found with compiler flag : -mfpu=neon -D__NEON__")
1728
add_compile_options(-mfpu=neon -D__NEON__)
1732
message(STATUS "Cortex-A8 Found with compiler flag : -mcpu=cortex-a8")
1733
add_compile_options(-mcpu=cortex-a8 -fprefetch-loop-arrays)
1736
message(STATUS "Cortex-A9 Found with compiler flag : -mcpu=cortex-a9")
1737
add_compile_options(-mcpu=cortex-a9)
1740
if(WIN32 AND NOT CYGWIN)
1741
set(BLAS_INSTALL_LIBRARIES "OFF"
1742
CACHE BOOL "Copy the required BLAS DLLs into the TH install dirs")
1745
find_package(LAPACK)
1748
list(APPEND Caffe2_PRIVATE_DEPENDENCY_LIBS ${LAPACK_LIBRARIES})
1752
message("disabling CUDA because NOT USE_CUDA is set")
1753
set(AT_CUDA_ENABLED 0)
1755
set(AT_CUDA_ENABLED 1)
1759
message("disabling ROCM because NOT USE_ROCM is set")
1760
message(STATUS "MIOpen not found. Compiling without MIOpen support")
1761
set(AT_ROCM_ENABLED 0)
1763
include_directories(BEFORE ${MIOPEN_INCLUDE_DIRS})
1764
set(AT_ROCM_ENABLED 1)
1767
set(AT_MKLDNN_ENABLED 0)
1768
set(AT_MKLDNN_ACL_ENABLED 0)
1770
if(NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
1772
"x64 operating system is required for MKLDNN. "
1773
"Not compiling with MKLDNN. "
1774
"Turn this warning off by USE_MKLDNN=OFF.")
1778
set(AT_MKLDNN_ACL_ENABLED 1)
1782
include(${CMAKE_CURRENT_LIST_DIR}/public/mkldnn.cmake)
1784
set(AT_MKLDNN_ENABLED 1)
1785
include_directories(AFTER SYSTEM ${MKLDNN_INCLUDE_DIR})
1786
if(BUILD_CAFFE2_OPS)
1787
list(APPEND Caffe2_DEPENDENCY_LIBS caffe2::mkldnn)
1788
endif(BUILD_CAFFE2_OPS)
1790
message(WARNING "MKLDNN could not be found.")
1791
caffe2_update_option(USE_MKLDNN OFF)
1794
message("disabling MKLDNN because USE_MKLDNN is not set")
1797
if(UNIX AND NOT APPLE)
1798
include(CheckLibraryExists)
1799
# https://github.com/libgit2/libgit2/issues/2128#issuecomment-35649830
1800
CHECK_LIBRARY_EXISTS(rt clock_gettime "time.h" NEED_LIBRT)
1802
list(APPEND Caffe2_DEPENDENCY_LIBS rt)
1803
set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} rt)
1805
endif(UNIX AND NOT APPLE)
1808
set(CMAKE_EXTRA_INCLUDE_FILES "sys/mman.h")
1809
CHECK_FUNCTION_EXISTS(mmap HAVE_MMAP)
1811
add_definitions(-DHAVE_MMAP=1)
1813
# done for lseek: https://www.gnu.org/software/libc/manual/html_node/File-Position-Primitive.html
1814
add_definitions(-D_FILE_OFFSET_BITS=64)
1815
CHECK_FUNCTION_EXISTS(shm_open HAVE_SHM_OPEN)
1817
add_definitions(-DHAVE_SHM_OPEN=1)
1818
endif(HAVE_SHM_OPEN)
1819
CHECK_FUNCTION_EXISTS(shm_unlink HAVE_SHM_UNLINK)
1821
add_definitions(-DHAVE_SHM_UNLINK=1)
1822
endif(HAVE_SHM_UNLINK)
1823
CHECK_FUNCTION_EXISTS(malloc_usable_size HAVE_MALLOC_USABLE_SIZE)
1824
if(HAVE_MALLOC_USABLE_SIZE)
1825
add_definitions(-DHAVE_MALLOC_USABLE_SIZE=1)
1826
endif(HAVE_MALLOC_USABLE_SIZE)
1829
add_definitions(-DUSE_EXTERNAL_MZCRC)
1830
add_definitions(-DMINIZ_DISABLE_ZIP_READER_CRC32_CHECKS)
1832
find_package(ZVECTOR) # s390x simd support
1838
set(TEMP_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS})
1839
set(BUILD_SHARED_LIBS OFF CACHE BOOL "Build shared libs" FORCE)
1840
add_subdirectory(${PROJECT_SOURCE_DIR}/third_party/fmt)
1842
# Disable compiler feature checks for `fmt`.
1844
# CMake compiles a little program to check compiler features. Some of our build
1845
# configurations (notably the mobile build analyzer) will populate
1846
# CMAKE_CXX_FLAGS in ways that break feature checks. Since we already know
1847
# `fmt` is compatible with a superset of the compilers that PyTorch is, it
1848
# shouldn't be too bad to just disable the checks.
1849
set_target_properties(fmt-header-only PROPERTIES INTERFACE_COMPILE_FEATURES "")
1851
list(APPEND Caffe2_DEPENDENCY_LIBS fmt::fmt-header-only)
1852
set(BUILD_SHARED_LIBS ${TEMP_BUILD_SHARED_LIBS} CACHE BOOL "Build shared libs" FORCE)
1855
# edge profiler depends on KinetoProfiler but it only does cpu
1856
# profiling. Thus we dont need USE_CUDA/USE_ROCM
1857
if(USE_KINETO AND INTERN_BUILD_MOBILE AND NOT (BUILD_LITE_INTERPRETER AND USE_LITE_INTERPRETER_PROFILER))
1858
message(STATUS "Not using libkineto in a mobile build.")
1862
if(USE_KINETO AND INTERN_BUILD_MOBILE AND USE_LITE_INTERPRETER_PROFILER AND (USE_CUDA OR USE_ROCM))
1863
message(FATAL_ERROR "Mobile build with profiler does not support CUDA or ROCM")
1867
if((NOT USE_CUDA) OR MSVC)
1868
set(LIBKINETO_NOCUPTI ON CACHE STRING "" FORCE)
1870
set(LIBKINETO_NOCUPTI OFF CACHE STRING "")
1871
message(STATUS "Using Kineto with CUPTI support")
1875
set(LIBKINETO_NOROCTRACER ON CACHE STRING "" FORCE)
1877
set(LIBKINETO_NOROCTRACER OFF CACHE STRING "")
1878
message(STATUS "Using Kineto with Roctracer support")
1881
if(LIBKINETO_NOCUPTI AND LIBKINETO_NOROCTRACER)
1882
message(STATUS "Using CPU-only version of Kineto")
1885
set(CAFFE2_THIRD_PARTY_ROOT "${PROJECT_SOURCE_DIR}/third_party" CACHE STRING "")
1886
set(KINETO_SOURCE_DIR "${CAFFE2_THIRD_PARTY_ROOT}/kineto/libkineto" CACHE STRING "")
1887
set(KINETO_BUILD_TESTS OFF CACHE BOOL "")
1888
set(KINETO_LIBRARY_TYPE "static" CACHE STRING "")
1890
message(STATUS "Configuring Kineto dependency:")
1891
message(STATUS " KINETO_SOURCE_DIR = ${KINETO_SOURCE_DIR}")
1892
message(STATUS " KINETO_BUILD_TESTS = ${KINETO_BUILD_TESTS}")
1893
message(STATUS " KINETO_LIBRARY_TYPE = ${KINETO_LIBRARY_TYPE}")
1895
if(NOT LIBKINETO_NOCUPTI)
1896
set(CUDA_SOURCE_DIR "${CUDA_TOOLKIT_ROOT_DIR}" CACHE STRING "")
1897
message(STATUS " CUDA_SOURCE_DIR = ${CUDA_SOURCE_DIR}")
1898
message(STATUS " CUDA_INCLUDE_DIRS = ${CUDA_INCLUDE_DIRS}")
1902
set(CUPTI_LIB_NAME "libcupti.so")
1904
set(CUPTI_LIB_NAME "libcupti_static.a")
1907
set(CUPTI_LIB_NAME "cupti.lib")
1910
find_library(CUPTI_LIBRARY_PATH ${CUPTI_LIB_NAME} PATHS
1912
${CUDA_SOURCE_DIR}/extras/CUPTI/lib64
1913
${CUDA_SOURCE_DIR}/lib
1914
${CUDA_SOURCE_DIR}/lib64
1917
find_path(CUPTI_INCLUDE_DIR cupti.h PATHS
1918
${CUDA_SOURCE_DIR}/extras/CUPTI/include
1919
${CUDA_INCLUDE_DIRS}
1921
${CUDA_SOURCE_DIR}/include
1924
if(CUPTI_LIBRARY_PATH AND CUPTI_INCLUDE_DIR)
1925
message(STATUS " CUPTI_INCLUDE_DIR = ${CUPTI_INCLUDE_DIR}")
1926
set(CUDA_cupti_LIBRARY ${CUPTI_LIBRARY_PATH})
1927
message(STATUS " CUDA_cupti_LIBRARY = ${CUDA_cupti_LIBRARY}")
1928
message(STATUS "Found CUPTI")
1929
set(LIBKINETO_NOCUPTI OFF CACHE STRING "" FORCE)
1931
# I've only tested this sanity check on Linux; if someone
1932
# runs into this bug on another platform feel free to
1933
# generalize it accordingly
1934
if(NOT USE_CUPTI_SO AND UNIX)
1935
include(CheckCXXSourceRuns)
1936
# rt is handled by the CMAKE_REQUIRED_LIBRARIES set above
1938
set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} "dl" "pthread")
1940
set(CMAKE_REQUIRED_LINK_OPTIONS "-Wl,--whole-archive,${CUPTI_LIBRARY_PATH},--no-whole-archive")
1941
check_cxx_source_runs("#include <stdexcept>
1944
throw std::runtime_error(\"error\");
1950
set(CMAKE_REQUIRED_LINK_OPTIONS "")
1951
if(NOT EXCEPTIONS_WORK)
1952
message(FATAL_ERROR "Detected that statically linking against CUPTI causes exceptions to stop working. See https://github.com/pytorch/pytorch/issues/57744 for more details. Perhaps try: USE_CUPTI_SO=1 python setup.py develop --cmake")
1957
message(STATUS "Could not find CUPTI library, using CPU-only Kineto build")
1958
set(LIBKINETO_NOCUPTI ON CACHE STRING "" FORCE)
1962
if(NOT LIBKINETO_NOROCTRACER)
1963
if("$ENV{ROCM_SOURCE_DIR}" STREQUAL "")
1964
set(ENV{ROCM_SOURCE_DIR} "/opt/rocm")
1968
if(NOT TARGET kineto)
1969
add_subdirectory("${KINETO_SOURCE_DIR}")
1970
set_property(TARGET kineto PROPERTY POSITION_INDEPENDENT_CODE ON)
1972
list(APPEND Caffe2_DEPENDENCY_LIBS kineto)
1973
string(APPEND CMAKE_CXX_FLAGS " -DUSE_KINETO")
1974
if(LIBKINETO_NOCUPTI)
1975
string(APPEND CMAKE_CXX_FLAGS " -DLIBKINETO_NOCUPTI")
1977
if(LIBKINETO_NOROCTRACER)
1978
string(APPEND CMAKE_CXX_FLAGS " -DLIBKINETO_NOROCTRACER")
1980
if(LIBKINETO_NOCUPTI AND LIBKINETO_NOROCTRACER)
1981
message(STATUS "Configured Kineto (CPU)")
1983
message(STATUS "Configured Kineto")
1987
# Include google/FlatBuffers
1988
include(${CMAKE_CURRENT_LIST_DIR}/FlatBuffers.cmake)