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_CUFILE ${USE_CUFILE})
43
set(CAFFE2_USE_NVRTC ${USE_NVRTC})
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 )
50
if(NOT CAFFE2_USE_NVRTC)
51
caffe2_update_option(USE_NVRTC OFF)
53
list(APPEND Caffe2_CUDA_DEPENDENCY_LIBS caffe2::curand caffe2::cufft caffe2::cublas)
55
list(APPEND Caffe2_CUDA_DEPENDENCY_LIBS torch::cudnn)
57
caffe2_update_option(USE_CUDNN OFF)
59
if(CAFFE2_USE_CUSPARSELT)
60
list(APPEND Caffe2_CUDA_DEPENDENCY_LIBS torch::cusparselt)
62
caffe2_update_option(USE_CUSPARSELT OFF)
65
list(APPEND Caffe2_CUDA_DEPENDENCY_LIBS torch::cufile)
67
find_program(SCCACHE_EXECUTABLE sccache)
68
if(SCCACHE_EXECUTABLE)
69
# Using RSP/--options-file renders output noncacheable by sccache
70
# as they fall under `multiple input files` non-cacheable rule
71
set(CMAKE_CUDA_USE_RESPONSE_FILE_FOR_INCLUDES 0)
72
set(CMAKE_CUDA_USE_RESPONSE_FILE_FOR_LIBRARIES 0)
73
set(CMAKE_CUDA_USE_RESPONSE_FILE_FOR_OBJECTS 0)
77
"Not compiling with CUDA. Suppress this warning with "
79
caffe2_update_option(USE_CUDA OFF)
80
caffe2_update_option(USE_CUDNN OFF)
81
caffe2_update_option(USE_CUSPARSELT OFF)
82
caffe2_update_option(USE_NVRTC OFF)
83
set(CAFFE2_USE_CUDA OFF)
84
set(CAFFE2_USE_CUDNN OFF)
85
set(CAFFE2_USE_CUSPARSELT OFF)
86
set(CAFFE2_USE_CUFILE OFF)
87
set(CAFFE2_USE_NVRTC OFF)
93
include(${CMAKE_CURRENT_LIST_DIR}/public/xpu.cmake)
94
if(NOT PYTORCH_FOUND_XPU)
95
message(WARNING "Not compiling with XPU. Could NOT find SYCL."
96
"Suppress this warning with -DUSE_XPU=OFF.")
97
caffe2_update_option(USE_XPU OFF)
100
string(APPEND CMAKE_CXX_FLAGS " -D__INTEL_PREVIEW_BREAKING_CHANGES")
105
# ---[ Custom Protobuf
106
if(CAFFE2_CMAKE_BUILDING_WITH_MAIN_REPO AND NOT INTERN_BUILD_MOBILE)
108
include(${CMAKE_CURRENT_LIST_DIR}/ProtoBuf.cmake)
112
if(USE_ASAN OR USE_TSAN)
113
find_package(Sanitizer REQUIRED)
115
if(TARGET Sanitizer::address)
116
list(APPEND Caffe2_DEPENDENCY_LIBS Sanitizer::address)
118
message(WARNING "Not ASAN found. Suppress this warning with -DUSE_ASAN=OFF.")
119
caffe2_update_option(USE_ASAN OFF)
121
if(TARGET Sanitizer::undefined)
122
list(APPEND Caffe2_DEPENDENCY_LIBS Sanitizer::undefined)
126
if(TARGET Sanitizer::thread)
127
list(APPEND Caffe2_DEPENDENCY_LIBS Sanitizer::thread)
129
message(WARNING "Not TSAN found. Suppress this warning with -DUSE_TSAN=OFF.")
130
caffe2_update_option(USE_TSAN OFF)
136
find_package(Threads REQUIRED)
137
if(TARGET Threads::Threads)
138
list(APPEND Caffe2_DEPENDENCY_LIBS Threads::Threads)
141
"Cannot find threading library. PyTorch requires Threads to compile.")
145
if(CAFFE2_CMAKE_BUILDING_WITH_MAIN_REPO)
147
set(CAFFE2_USE_LITE_PROTO 1)
153
set(AT_MKLDNN_ACL_ENABLED 0)
154
set(AT_MKLDNN_ENABLED 0)
156
# setting default preferred BLAS options if not already present.
157
if(NOT INTERN_BUILD_MOBILE)
158
set(BLAS "MKL" CACHE STRING "Selected BLAS library")
160
set(BLAS "Eigen" CACHE STRING "Selected BLAS library")
161
set(AT_MKLDNN_ENABLED 0)
162
set(AT_MKL_ENABLED 0)
164
set_property(CACHE BLAS PROPERTY STRINGS "ATLAS;BLIS;Eigen;FLAME;Generic;MKL;OpenBLAS;vecLib")
165
message(STATUS "Trying to find preferred BLAS backend of choice: " ${BLAS})
167
if(BLAS STREQUAL "Eigen")
168
# Eigen is header-only and we do not have any dependent libraries
169
set(CAFFE2_USE_EIGEN_FOR_BLAS ON)
170
elseif(BLAS STREQUAL "ATLAS")
171
find_package(Atlas REQUIRED)
172
include_directories(SYSTEM ${ATLAS_INCLUDE_DIRS})
173
list(APPEND Caffe2_DEPENDENCY_LIBS ${ATLAS_LIBRARIES})
174
list(APPEND Caffe2_DEPENDENCY_LIBS cblas)
175
set(BLAS_INFO "atlas")
177
set(BLAS_LIBRARIES ${ATLAS_LIBRARIES} cblas)
178
elseif(BLAS STREQUAL "OpenBLAS")
179
find_package(OpenBLAS REQUIRED)
180
include_directories(SYSTEM ${OpenBLAS_INCLUDE_DIR})
181
list(APPEND Caffe2_DEPENDENCY_LIBS ${OpenBLAS_LIB})
182
set(BLAS_INFO "open")
184
set(BLAS_LIBRARIES ${OpenBLAS_LIB})
185
elseif(BLAS STREQUAL "BLIS")
186
find_package(BLIS REQUIRED)
187
include_directories(SYSTEM ${BLIS_INCLUDE_DIR})
188
list(APPEND Caffe2_DEPENDENCY_LIBS ${BLIS_LIB})
189
elseif(BLAS STREQUAL "MKL")
191
find_package(MKL REQUIRED)
193
find_package(MKL QUIET)
195
include(${CMAKE_CURRENT_LIST_DIR}/public/mkl.cmake)
197
message(STATUS "MKL libraries: ${MKL_LIBRARIES}")
198
message(STATUS "MKL include directory: ${MKL_INCLUDE_DIR}")
199
message(STATUS "MKL OpenMP type: ${MKL_OPENMP_TYPE}")
200
message(STATUS "MKL OpenMP library: ${MKL_OPENMP_LIBRARY}")
201
include_directories(AFTER SYSTEM ${MKL_INCLUDE_DIR})
202
list(APPEND Caffe2_PUBLIC_DEPENDENCY_LIBS caffe2::mkl)
203
set(CAFFE2_USE_MKL ON)
206
set(BLAS_LIBRARIES ${MKL_LIBRARIES})
208
message(WARNING "MKL could not be found. Defaulting to Eigen")
209
set(CAFFE2_USE_EIGEN_FOR_BLAS ON)
210
set(CAFFE2_USE_MKL OFF)
212
elseif(BLAS STREQUAL "NVPL")
213
find_package(NVPL_BLAS REQUIRED)
214
list(APPEND Caffe2_DEPENDENCY_LIBS nvpl::blas_lp64_omp)
215
set(BLAS_INFO "nvpl")
217
set(BLAS_USE_CBLAS_DOT TRUE)
218
elseif(BLAS STREQUAL "vecLib")
219
find_package(vecLib REQUIRED)
220
include_directories(SYSTEM ${vecLib_INCLUDE_DIR})
221
list(APPEND Caffe2_DEPENDENCY_LIBS ${vecLib_LINKER_LIBS})
222
set(BLAS_INFO "veclib")
224
set(BLAS_LIBRARIES ${vecLib_LINKER_LIBS})
225
elseif(BLAS STREQUAL "FlexiBLAS")
226
find_package(FlexiBLAS REQUIRED)
227
include_directories(SYSTEM ${FlexiBLAS_INCLUDE_DIR})
228
list(APPEND Caffe2_DEPENDENCY_LIBS ${FlexiBLAS_LIB})
229
elseif(BLAS STREQUAL "Generic")
230
# On Debian family, the CBLAS ABIs have been merged into libblas.so
231
if(ENV{GENERIC_BLAS_LIBRARIES} STREQUAL "")
232
set(GENERIC_BLAS "blas")
234
set(GENERIC_BLAS $ENV{GENERIC_BLAS_LIBRARIES})
236
find_library(BLAS_LIBRARIES NAMES ${GENERIC_BLAS})
237
message("-- Using BLAS: ${BLAS_LIBRARIES}")
238
list(APPEND Caffe2_DEPENDENCY_LIBS ${BLAS_LIBRARIES})
239
set(GENERIC_BLAS_FOUND TRUE)
240
set(BLAS_INFO "generic")
243
message(FATAL_ERROR "Unrecognized BLAS option: " ${BLAS})
246
if(NOT INTERN_BUILD_MOBILE)
247
set(AT_MKL_SEQUENTIAL 0)
249
if(NOT (ATLAS_FOUND OR BLIS_FOUND OR GENERIC_BLAS_FOUND OR MKL_FOUND OR OpenBLAS_FOUND OR VECLIB_FOUND OR FlexiBLAS_FOUND OR NVPL_BLAS_FOUND))
250
message(WARNING "Preferred BLAS (" ${BLAS} ") cannot be found, now searching for a general BLAS library")
258
if("${MKL_THREADING}" STREQUAL "SEQ")
259
set(AT_MKL_SEQUENTIAL 1)
261
set(AT_MKL_ENABLED 1)
263
elseif(INTERN_USE_EIGEN_BLAS)
264
# Eigen BLAS for Mobile
266
include(${CMAKE_CURRENT_LIST_DIR}/External/EigenBLAS.cmake)
267
list(APPEND Caffe2_DEPENDENCY_LIBS eigen_blas)
271
set(AT_POCKETFFT_ENABLED 0)
272
if(NOT AT_MKL_ENABLED)
273
set(POCKETFFT_INCLUDE_DIR "${Torch_SOURCE_DIR}/third_party/pocketfft/")
274
if(NOT EXISTS "${POCKETFFT_INCLUDE_DIR}")
275
message(FATAL_ERROR "pocketfft directory not found, expected ${POCKETFFT_INCLUDE_DIR}")
276
elif(NOT EXISTS "${POCKETFFT_INCLUDE_DIR}/pocketfft_hdronly.h")
277
message(FATAL_ERROR "pocketfft headers not found in ${POCKETFFT_INCLUDE_DIR}")
280
set(AT_POCKETFFT_ENABLED 1)
281
message(STATUS "Using pocketfft in directory: ${POCKETFFT_INCLUDE_DIR}")
285
# NNPACK and family (QNNPACK, PYTORCH_QNNPACK, and XNNPACK) can download and
286
# compile their dependencies in isolation as part of their build. These dependencies
287
# are then linked statically with PyTorch. To avoid the possibility of a version
288
# mismatch between these shared dependencies, explicitly declare our intent to these
289
# libraries that we are interested in using the exact same source dependencies for all.
291
if(USE_NNPACK OR USE_PYTORCH_QNNPACK OR USE_XNNPACK)
292
set(DISABLE_NNPACK_AND_FAMILY OFF)
294
# Sanity checks - Can we actually build NNPACK and family given the configuration provided?
295
# Disable them and warn the user if not.
298
list(LENGTH IOS_ARCH IOS_ARCH_COUNT)
299
if(IOS_ARCH_COUNT GREATER 1)
301
"Multi-architecture (${IOS_ARCH}) builds are not supported in {Q/X}NNPACK. "
302
"Specify a single architecture in IOS_ARCH and re-configure, or "
303
"turn this warning off by USE_{Q/X}NNPACK=OFF.")
304
set(DISABLE_NNPACK_AND_FAMILY ON)
306
if(NOT IOS_ARCH MATCHES "^(i386|x86_64|armv7.*|arm64.*)$")
308
"Target architecture \"${IOS_ARCH}\" is not supported in {Q/X}NNPACK. "
309
"Supported architectures are x86, x86-64, ARM, and ARM64. "
310
"Turn this warning off by USE_{Q/X}NNPACK=OFF.")
311
set(DISABLE_NNPACK_AND_FAMILY ON)
314
if(NOT IOS AND NOT (CMAKE_SYSTEM_NAME MATCHES "^(Android|Linux|Darwin|Windows)$"))
316
"Target platform \"${CMAKE_SYSTEM_NAME}\" is not supported in {Q/X}NNPACK. "
317
"Supported platforms are Android, iOS, Linux, and macOS. "
318
"Turn this warning off by USE_{Q/X}NNPACK=OFF.")
319
set(DISABLE_NNPACK_AND_FAMILY ON)
321
if(NOT IOS AND NOT (CMAKE_SYSTEM_PROCESSOR MATCHES "^(i686|AMD64|x86_64|armv[0-9].*|arm64|aarch64)$"))
323
"Target architecture \"${CMAKE_SYSTEM_PROCESSOR}\" is not supported in {Q/X}NNPACK. "
324
"Supported architectures are x86, x86-64, ARM, and ARM64. "
325
"Turn this warning off by USE_{Q/X}NNPACK=OFF.")
326
set(DISABLE_NNPACK_AND_FAMILY ON)
330
if(DISABLE_NNPACK_AND_FAMILY)
331
caffe2_update_option(USE_NNPACK OFF)
332
caffe2_update_option(USE_PYTORCH_QNNPACK OFF)
333
caffe2_update_option(USE_XNNPACK OFF)
335
# Disable unsupported NNPack combinations with MSVC
337
caffe2_update_option(USE_NNPACK OFF)
338
caffe2_update_option(USE_PYTORCH_QNNPACK OFF)
341
set(CAFFE2_THIRD_PARTY_ROOT "${PROJECT_SOURCE_DIR}/third_party")
343
if(NOT DEFINED CPUINFO_SOURCE_DIR)
344
set(CPUINFO_SOURCE_DIR "${CAFFE2_THIRD_PARTY_ROOT}/cpuinfo" CACHE STRING "cpuinfo source directory")
346
if(NOT DEFINED FP16_SOURCE_DIR)
347
set(FP16_SOURCE_DIR "${CAFFE2_THIRD_PARTY_ROOT}/FP16" CACHE STRING "FP16 source directory")
349
if(NOT DEFINED FXDIV_SOURCE_DIR)
350
set(FXDIV_SOURCE_DIR "${CAFFE2_THIRD_PARTY_ROOT}/FXdiv" CACHE STRING "FXdiv source directory")
352
if(NOT DEFINED PSIMD_SOURCE_DIR)
353
set(PSIMD_SOURCE_DIR "${CAFFE2_THIRD_PARTY_ROOT}/psimd" CACHE STRING "PSimd source directory")
355
if(NOT DEFINED PTHREADPOOL_SOURCE_DIR)
356
set(PTHREADPOOL_SOURCE_DIR "${CAFFE2_THIRD_PARTY_ROOT}/pthreadpool" CACHE STRING "pthreadpool source directory")
360
set(DISABLE_NNPACK_AND_FAMILY ON)
363
set(CONFU_DEPENDENCIES_SOURCE_DIR ${PROJECT_BINARY_DIR}/confu-srcs
364
CACHE PATH "Confu-style dependencies source directory")
365
set(CONFU_DEPENDENCIES_BINARY_DIR ${PROJECT_BINARY_DIR}/confu-deps
366
CACHE PATH "Confu-style dependencies binary directory")
369
# Only add a dependency on pthreadpool if we are on a mobile build
370
# or are building any of the libraries in the {Q/X}NNPACK family.
371
if(INTERN_BUILD_MOBILE OR NOT DISABLE_NNPACK_AND_FAMILY)
372
set(USE_PTHREADPOOL ON CACHE BOOL "" FORCE)
373
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DUSE_PTHREADPOOL")
375
# Always use third_party/pthreadpool.
376
set(USE_INTERNAL_PTHREADPOOL_IMPL OFF CACHE BOOL "" FORCE)
378
if(NOT TARGET pthreadpool)
379
if(USE_SYSTEM_PTHREADPOOL)
380
add_library(pthreadpool SHARED IMPORTED)
381
find_library(PTHREADPOOL_LIBRARY pthreadpool)
382
set_property(TARGET pthreadpool PROPERTY IMPORTED_LOCATION "${PTHREADPOOL_LIBRARY}")
383
if(NOT PTHREADPOOL_LIBRARY)
384
message(FATAL_ERROR "Cannot find pthreadpool")
386
message("-- Found pthreadpool: ${PTHREADPOOL_LIBRARY}")
387
elseif(NOT USE_INTERNAL_PTHREADPOOL_IMPL)
388
if(NOT DEFINED PTHREADPOOL_SOURCE_DIR)
389
set(CAFFE2_THIRD_PARTY_ROOT "${PROJECT_SOURCE_DIR}/third_party")
390
set(PTHREADPOOL_SOURCE_DIR "${CAFFE2_THIRD_PARTY_ROOT}/pthreadpool" CACHE STRING "pthreadpool source directory")
393
set(PTHREADPOOL_BUILD_TESTS OFF CACHE BOOL "")
394
set(PTHREADPOOL_BUILD_BENCHMARKS OFF CACHE BOOL "")
395
set(PTHREADPOOL_LIBRARY_TYPE "static" CACHE STRING "")
396
set(PTHREADPOOL_ALLOW_DEPRECATED_API ON CACHE BOOL "")
398
"${PTHREADPOOL_SOURCE_DIR}"
399
"${CONFU_DEPENDENCIES_BINARY_DIR}/pthreadpool")
400
set_property(TARGET pthreadpool PROPERTY POSITION_INDEPENDENT_CODE ON)
403
if(USE_INTERNAL_PTHREADPOOL_IMPL)
404
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DUSE_INTERNAL_PTHREADPOOL_IMPL")
406
list(APPEND Caffe2_DEPENDENCY_LIBS pthreadpool)
410
set(USE_PTHREADPOOL OFF CACHE BOOL "" FORCE)
413
if(NOT CMAKE_SYSTEM_PROCESSOR MATCHES "^(s390x|ppc64le)$")
414
# ---[ Caffe2 uses cpuinfo library in the thread pool
415
# ---[ But it doesn't support s390x/powerpc and thus not used on s390x/powerpc
416
if(NOT TARGET cpuinfo AND USE_SYSTEM_CPUINFO)
417
add_library(cpuinfo SHARED IMPORTED)
418
find_library(CPUINFO_LIBRARY cpuinfo)
419
if(NOT CPUINFO_LIBRARY)
420
message(FATAL_ERROR "Cannot find cpuinfo")
422
message("Found cpuinfo: ${CPUINFO_LIBRARY}")
423
set_target_properties(cpuinfo PROPERTIES IMPORTED_LOCATION "${CPUINFO_LIBRARY}")
424
elseif(NOT TARGET cpuinfo)
425
if(NOT DEFINED CPUINFO_SOURCE_DIR)
426
set(CPUINFO_SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/../third_party/cpuinfo" CACHE STRING "cpuinfo source directory")
429
set(CPUINFO_BUILD_TOOLS OFF CACHE BOOL "")
430
set(CPUINFO_BUILD_UNIT_TESTS OFF CACHE BOOL "")
431
set(CPUINFO_BUILD_MOCK_TESTS OFF CACHE BOOL "")
432
set(CPUINFO_BUILD_BENCHMARKS OFF CACHE BOOL "")
433
set(CPUINFO_LIBRARY_TYPE "static" CACHE STRING "")
434
set(CPUINFO_LOG_LEVEL "error" CACHE STRING "")
436
if(CAFFE2_USE_MSVC_STATIC_RUNTIME)
437
set(CPUINFO_RUNTIME_TYPE "static" CACHE STRING "")
439
set(CPUINFO_RUNTIME_TYPE "shared" CACHE STRING "")
443
"${CPUINFO_SOURCE_DIR}"
444
"${CONFU_DEPENDENCIES_BINARY_DIR}/cpuinfo")
445
# We build static version of cpuinfo but link
446
# them into a shared library for Caffe2, so they need PIC.
447
set_property(TARGET cpuinfo PROPERTY POSITION_INDEPENDENT_CODE ON)
449
list(APPEND Caffe2_DEPENDENCY_LIBS cpuinfo)
453
# ---[ PYTORCH_QNNPACK
454
set(CAFFE2_THIRD_PARTY_ROOT "${PROJECT_SOURCE_DIR}/third_party")
455
if(USE_PYTORCH_QNNPACK)
456
if(NOT DEFINED PYTORCH_QNNPACK_SOURCE_DIR)
457
set(PYTORCH_QNNPACK_SOURCE_DIR "${PROJECT_SOURCE_DIR}/aten/src/ATen/native/quantized/cpu/qnnpack" CACHE STRING "QNNPACK source directory")
460
if(NOT TARGET pytorch_qnnpack)
461
if(NOT USE_SYSTEM_PTHREADPOOL AND USE_INTERNAL_PTHREADPOOL_IMPL)
462
set(PYTORCH_QNNPACK_CUSTOM_THREADPOOL ON CACHE BOOL "")
465
set(PYTORCH_QNNPACK_BUILD_TESTS OFF CACHE BOOL "")
466
set(PYTORCH_QNNPACK_BUILD_BENCHMARKS OFF CACHE BOOL "")
467
set(PYTORCH_QNNPACK_LIBRARY_TYPE "static" CACHE STRING "")
469
"${PYTORCH_QNNPACK_SOURCE_DIR}"
470
"${CONFU_DEPENDENCIES_BINARY_DIR}/pytorch_qnnpack")
471
# We build static versions of QNNPACK and pthreadpool but link
472
# them into a shared library for Caffe2, so they need PIC.
473
set_property(TARGET pytorch_qnnpack PROPERTY POSITION_INDEPENDENT_CODE ON)
474
set_property(TARGET cpuinfo PROPERTY POSITION_INDEPENDENT_CODE ON)
475
# QNNPACK depends on gemmlowp headers
476
target_include_directories(pytorch_qnnpack PRIVATE "${CAFFE2_THIRD_PARTY_ROOT}/gemmlowp")
478
if(PYTORCH_QNNPACK_CUSTOM_THREADPOOL)
479
target_compile_definitions(
480
pytorch_qnnpack PRIVATE
481
pthreadpool_t=legacy_pthreadpool_t
482
pthreadpool_function_1d_t=legacy_pthreadpool_function_1d_t
483
pthreadpool_function_1d_tiled_t=legacy_pthreadpool_function_1d_tiled_t
484
pthreadpool_function_2d_t=legacy_pthreadpool_function_2d_t
485
pthreadpool_function_2d_tiled_t=legacy_pthreadpool_function_2d_tiled_t
486
pthreadpool_function_3d_tiled_t=legacy_pthreadpool_function_3d_tiled_t
487
pthreadpool_function_4d_tiled_t=legacy_pthreadpool_function_4d_tiled_t
488
pthreadpool_create=legacy_pthreadpool_create
489
pthreadpool_destroy=legacy_pthreadpool_destroy
490
pthreadpool_get_threads_count=legacy_pthreadpool_get_threads_count
491
pthreadpool_compute_1d=legacy_pthreadpool_compute_1d
492
pthreadpool_parallelize_1d=legacy_pthreadpool_parallelize_1d
493
pthreadpool_compute_1d_tiled=legacy_pthreadpool_compute_1d_tiled
494
pthreadpool_compute_2d=legacy_pthreadpool_compute_2d
495
pthreadpool_compute_2d_tiled=legacy_pthreadpool_compute_2d_tiled
496
pthreadpool_compute_3d_tiled=legacy_pthreadpool_compute_3d_tiled
497
pthreadpool_compute_4d_tiled=legacy_pthreadpool_compute_4d_tiled)
501
list(APPEND Caffe2_DEPENDENCY_LIBS pytorch_qnnpack)
506
include(${CMAKE_CURRENT_LIST_DIR}/External/nnpack.cmake)
509
# ---[ NNPACK is being built together with Caffe2: explicitly specify dependency
510
list(APPEND Caffe2_DEPENDENCY_LIBS nnpack)
512
include_directories(SYSTEM ${NNPACK_INCLUDE_DIRS})
513
list(APPEND Caffe2_DEPENDENCY_LIBS ${NNPACK_LIBRARIES})
516
message(WARNING "Not compiling with NNPACK. Suppress this warning with -DUSE_NNPACK=OFF")
517
caffe2_update_option(USE_NNPACK OFF)
522
if(USE_XNNPACK AND NOT USE_SYSTEM_XNNPACK)
523
if(NOT DEFINED XNNPACK_SOURCE_DIR)
524
set(XNNPACK_SOURCE_DIR "${CAFFE2_THIRD_PARTY_ROOT}/XNNPACK" CACHE STRING "XNNPACK source directory")
527
if(NOT DEFINED XNNPACK_INCLUDE_DIR)
528
set(XNNPACK_INCLUDE_DIR "${XNNPACK_SOURCE_DIR}/include" CACHE STRING "XNNPACK include directory")
531
if(NOT TARGET XNNPACK)
532
set(XNNPACK_LIBRARY_TYPE "static" CACHE STRING "")
533
set(XNNPACK_BUILD_BENCHMARKS OFF CACHE BOOL "")
534
set(XNNPACK_BUILD_TESTS OFF CACHE BOOL "")
536
# Disable ARM BF16 and FP16 vector for now; unused and causes build failures because
537
# these new ISA features may not be supported on older compilers
538
set(XNNPACK_ENABLE_ARM_BF16 OFF CACHE BOOL "")
540
# Disable AVXVNNI for now, older clang versions seem not to support it
541
# (clang 12 is where avx-vnni support is added)
542
set(XNNPACK_ENABLE_AVXVNNI OFF CACHE BOOL "")
544
# Disable I8MM For CI since clang 9 does not support neon i8mm.
545
set(XNNPACK_ENABLE_ARM_I8MM OFF CACHE BOOL "")
547
# Older MSVC versions don't support AVX512FP. TODO Minimum version support?
548
IF(CMAKE_C_COMPILER_ID STREQUAL "MSVC")
549
set(XNNPACK_ENABLE_AVX512FP16 OFF CACHE BOOL "")
552
# Conditionally disable AVX512AMX, as it requires Clang 11 or later. Note that
553
# XNNPACK does conditionally compile this based on GCC version. Once it also does
554
# so based on Clang version, this logic can be removed.
555
IF(CMAKE_C_COMPILER_ID STREQUAL "Clang")
556
IF(CMAKE_C_COMPILER_VERSION VERSION_LESS "11")
557
set(XNNPACK_ENABLE_AVX512AMX OFF CACHE BOOL "")
561
# Setting this global PIC flag for all XNNPACK targets.
562
# This is needed for Object libraries within XNNPACK which must
563
# be PIC to successfully link this static libXNNPACK with pytorch
564
set(__caffe2_CMAKE_POSITION_INDEPENDENT_CODE_FLAG ${CMAKE_POSITION_INDEPENDENT_CODE})
565
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
568
"${XNNPACK_SOURCE_DIR}"
569
"${CONFU_DEPENDENCIES_BINARY_DIR}/XNNPACK")
571
# Revert to whatever it was before
572
set(CMAKE_POSITION_INDEPENDENT_CODE ${__caffe2_CMAKE_POSITION_INDEPENDENT_CODE_FLAG})
575
include_directories(SYSTEM ${XNNPACK_INCLUDE_DIR})
576
list(APPEND Caffe2_DEPENDENCY_LIBS XNNPACK)
577
elseif(NOT TARGET XNNPACK AND USE_SYSTEM_XNNPACK)
578
add_library(XNNPACK SHARED IMPORTED)
579
find_library(XNNPACK_LIBRARY XNNPACK)
580
set_property(TARGET XNNPACK PROPERTY IMPORTED_LOCATION "${XNNPACK_LIBRARY}")
581
if(NOT XNNPACK_LIBRARY)
582
message(FATAL_ERROR "Cannot find XNNPACK")
584
message("-- Found XNNPACK: ${XNNPACK_LIBRARY}")
585
list(APPEND Caffe2_DEPENDENCY_LIBS XNNPACK)
593
include(${CMAKE_CURRENT_LIST_DIR}/VulkanDependencies.cmake)
594
string(APPEND CMAKE_CXX_FLAGS ${Vulkan_DEFINES})
595
include_directories(SYSTEM ${Vulkan_INCLUDES})
596
list(APPEND Caffe2_DEPENDENCY_LIBS ${Vulkan_LIBS})
601
include(${CMAKE_CURRENT_LIST_DIR}/public/gflags.cmake)
602
if(NOT TARGET gflags)
604
"gflags is not found. Caffe2 will build without gflags support but "
605
"it is strongly recommended that you install gflags. Suppress this "
606
"warning with -DUSE_GFLAGS=OFF")
607
caffe2_update_option(USE_GFLAGS OFF)
613
include(${CMAKE_CURRENT_LIST_DIR}/public/glog.cmake)
614
if(TARGET glog::glog)
615
set(CAFFE2_USE_GOOGLE_GLOG 1)
618
"glog is not found. Caffe2 will build without glog support but it is "
619
"strongly recommended that you install glog. Suppress this warning "
620
"with -DUSE_GLOG=OFF")
621
caffe2_update_option(USE_GLOG OFF)
626
# ---[ Googletest and benchmark
627
if(BUILD_TEST OR BUILD_MOBILE_BENCHMARK OR BUILD_MOBILE_TEST)
628
# Preserve build options.
629
set(TEMP_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS})
631
# We will build gtest as static libs and embed it directly into the binary.
632
set(BUILD_SHARED_LIBS OFF CACHE BOOL "Build shared libs" FORCE)
634
# For gtest, we will simply embed it into our test binaries, so we won't
635
# need to install it.
636
set(INSTALL_GTEST OFF CACHE BOOL "Install gtest." FORCE)
637
set(BUILD_GMOCK ON CACHE BOOL "Build gmock." FORCE)
638
# For Windows, we will check the runtime used is correctly passed in.
639
if(NOT CAFFE2_USE_MSVC_STATIC_RUNTIME)
640
set(gtest_force_shared_crt ON CACHE BOOL "force shared crt on gtest" FORCE)
642
# We need to replace googletest cmake scripts too.
643
# Otherwise, it will sometimes break the build.
644
# To make the git clean after the build, we make a backup first.
645
if((MSVC AND MSVC_Z7_OVERRIDE) OR USE_CUDA)
647
COMMAND ${CMAKE_COMMAND}
648
"-DFILENAME=${CMAKE_CURRENT_LIST_DIR}/../third_party/googletest/googletest/cmake/internal_utils.cmake"
649
"-DBACKUP=${CMAKE_CURRENT_LIST_DIR}/../third_party/googletest/googletest/cmake/internal_utils.cmake.bak"
652
"${CMAKE_CURRENT_LIST_DIR}/GoogleTestPatch.cmake"
653
RESULT_VARIABLE _exitcode)
654
if(NOT _exitcode EQUAL 0)
655
message(WARNING "Patching failed for Google Test. The build may fail.")
659
# Add googletest subdirectory but make sure our INCLUDE_DIRECTORIES
660
# don't bleed into it. This is because libraries installed into the root conda
661
# env (e.g. MKL) add a global /opt/conda/include directory, and if there's
662
# gtest installed in conda, the third_party/googletest/**.cc source files
663
# would try to include headers from /opt/conda/include/gtest/**.h instead of
664
# its own. Once we have proper target-based include directories,
665
# this shouldn't be necessary anymore.
666
get_property(INC_DIR_temp DIRECTORY PROPERTY INCLUDE_DIRECTORIES)
667
set_property(DIRECTORY PROPERTY INCLUDE_DIRECTORIES "")
668
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../third_party/googletest)
669
set_property(DIRECTORY PROPERTY INCLUDE_DIRECTORIES ${INC_DIR_temp})
671
include_directories(BEFORE SYSTEM ${CMAKE_CURRENT_LIST_DIR}/../third_party/googletest/googletest/include)
672
include_directories(BEFORE SYSTEM ${CMAKE_CURRENT_LIST_DIR}/../third_party/googletest/googlemock/include)
674
# We will not need to test benchmark lib itself.
675
set(BENCHMARK_ENABLE_TESTING OFF CACHE BOOL "Disable benchmark testing as we don't need it.")
676
# We will not need to install benchmark since we link it statically.
677
set(BENCHMARK_ENABLE_INSTALL OFF CACHE BOOL "Disable benchmark install to avoid overwriting vendor install.")
678
if(NOT USE_SYSTEM_BENCHMARK)
679
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../third_party/benchmark)
681
add_library(benchmark SHARED IMPORTED)
682
find_library(BENCHMARK_LIBRARY benchmark)
683
if(NOT BENCHMARK_LIBRARY)
684
message(FATAL_ERROR "Cannot find google benchmark library")
686
message("-- Found benchmark: ${BENCHMARK_LIBRARY}")
687
set_property(TARGET benchmark PROPERTY IMPORTED_LOCATION ${BENCHMARK_LIBRARY})
689
include_directories(${CMAKE_CURRENT_LIST_DIR}/../third_party/benchmark/include)
691
# Recover build options.
692
set(BUILD_SHARED_LIBS ${TEMP_BUILD_SHARED_LIBS} CACHE BOOL "Build shared libs" FORCE)
694
# To make the git clean after the build, we revert the changes here.
695
if(MSVC AND MSVC_Z7_OVERRIDE)
697
COMMAND ${CMAKE_COMMAND}
698
"-DFILENAME=${CMAKE_CURRENT_LIST_DIR}/../third_party/googletest/googletest/cmake/internal_utils.cmake"
699
"-DBACKUP=${CMAKE_CURRENT_LIST_DIR}/../third_party/googletest/googletest/cmake/internal_utils.cmake.bak"
702
"${CMAKE_CURRENT_LIST_DIR}/GoogleTestPatch.cmake"
703
RESULT_VARIABLE _exitcode)
704
if(NOT _exitcode EQUAL 0)
705
message(WARNING "Reverting changes failed for Google Test. The build may fail.")
709
# Cacheing variables to enable incremental build.
710
# Without this is cross compiling we end up having to blow build directory
711
# and rebuild from scratch.
712
if(CMAKE_CROSSCOMPILING)
713
if(COMPILE_HAVE_STD_REGEX)
714
set(RUN_HAVE_STD_REGEX 0 CACHE INTERNAL "Cache RUN_HAVE_STD_REGEX output for cross-compile.")
721
set(CAFFE2_THIRD_PARTY_ROOT "${PROJECT_SOURCE_DIR}/third_party")
722
if(NOT DEFINED FBGEMM_SOURCE_DIR)
723
set(FBGEMM_SOURCE_DIR "${CAFFE2_THIRD_PARTY_ROOT}/fbgemm" CACHE STRING "FBGEMM source directory")
725
if(NOT CAFFE2_COMPILER_SUPPORTS_AVX512_EXTENSIONS)
727
"A compiler with AVX512 support is required for FBGEMM. "
728
"Not compiling with FBGEMM. "
729
"Turn this warning off by USE_FBGEMM=OFF.")
732
if(NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
734
"x64 operating system is required for FBGEMM. "
735
"Not compiling with FBGEMM. "
736
"Turn this warning off by USE_FBGEMM=OFF.")
739
if(USE_FBGEMM AND NOT TARGET fbgemm)
740
set(FBGEMM_BUILD_TESTS OFF CACHE BOOL "")
741
set(FBGEMM_BUILD_BENCHMARKS OFF CACHE BOOL "")
742
if(MSVC AND BUILD_SHARED_LIBS)
743
set(FBGEMM_LIBRARY_TYPE "shared" CACHE STRING "")
745
set(FBGEMM_LIBRARY_TYPE "static" CACHE STRING "")
748
set(USE_SANITIZER "address,undefined" CACHE STRING "-fsanitize options for FBGEMM")
750
add_subdirectory("${FBGEMM_SOURCE_DIR}")
751
set_property(TARGET fbgemm_generic PROPERTY POSITION_INDEPENDENT_CODE ON)
752
set_property(TARGET fbgemm_avx2 PROPERTY POSITION_INDEPENDENT_CODE ON)
753
set_property(TARGET fbgemm_avx512 PROPERTY POSITION_INDEPENDENT_CODE ON)
754
set_property(TARGET fbgemm PROPERTY POSITION_INDEPENDENT_CODE ON)
755
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 13.0.0)
756
# See https://github.com/pytorch/pytorch/issues/74352
757
target_compile_options_if_supported(asmjit -Wno-deprecated-copy)
758
target_compile_options_if_supported(asmjit -Wno-unused-but-set-variable)
763
list(APPEND Caffe2_DEPENDENCY_LIBS fbgemm)
768
caffe2_update_option(USE_FBGEMM ON)
770
caffe2_update_option(USE_FBGEMM OFF)
772
"Turning USE_FAKELOWP off as it depends on USE_FBGEMM.")
773
caffe2_update_option(USE_FAKELOWP OFF)
777
message(INFO "USING OPENCL")
778
find_package(OpenCL REQUIRED)
779
include_directories(SYSTEM ${OpenCL_INCLUDE_DIRS})
780
list(APPEND Caffe2_DEPENDENCY_LIBS ${OpenCL_LIBRARIES})
788
message(WARNING "Not compiling with NUMA. Suppress this warning with -DUSE_NUMA=OFF")
789
caffe2_update_option(USE_NUMA OFF)
792
message(WARNING "NUMA is currently only supported under Linux.")
793
caffe2_update_option(USE_NUMA OFF)
800
include_directories(SYSTEM ${ITT_INCLUDE_DIR})
801
list(APPEND Caffe2_DEPENDENCY_LIBS ${ITT_LIBRARIES})
802
list(APPEND TORCH_PYTHON_LINK_LIBRARIES ${ITT_LIBRARIES})
804
message(WARNING "Not compiling with ITT. Suppress this warning with -DUSE_ITT=OFF")
805
set(USE_ITT OFF CACHE BOOL "" FORCE)
806
caffe2_update_option(USE_ITT OFF)
810
# ---[ Caffe2 depends on FP16 library for half-precision conversions
811
if(NOT TARGET fp16 AND NOT USE_SYSTEM_FP16)
812
set(CAFFE2_THIRD_PARTY_ROOT "${PROJECT_SOURCE_DIR}/third_party")
813
# PSIMD is required by FP16
814
if(NOT DEFINED PSIMD_SOURCE_DIR)
815
set(PSIMD_SOURCE_DIR "${CAFFE2_THIRD_PARTY_ROOT}/psimd" CACHE STRING "PSimd source directory")
817
if(NOT DEFINED FP16_SOURCE_DIR)
818
set(FP16_SOURCE_DIR "${CAFFE2_THIRD_PARTY_ROOT}/FP16" CACHE STRING "FP16 source directory")
821
set(FP16_BUILD_TESTS OFF CACHE BOOL "")
822
set(FP16_BUILD_BENCHMARKS OFF CACHE BOOL "")
825
"${CONFU_DEPENDENCIES_BINARY_DIR}/FP16")
826
elseif(NOT TARGET fp16 AND USE_SYSTEM_FP16)
827
add_library(fp16 STATIC "/usr/include/fp16.h")
828
set_target_properties(fp16 PROPERTIES LINKER_LANGUAGE C)
830
list(APPEND Caffe2_DEPENDENCY_LIBS fp16)
833
# Due to license considerations, we will only use the MPL2 parts of Eigen.
834
set(EIGEN_MPL2_ONLY 1)
835
if(USE_SYSTEM_EIGEN_INSTALL)
838
message(STATUS "Found system Eigen at " ${EIGEN3_INCLUDE_DIR})
840
message(STATUS "Did not find system Eigen. Using third party subdirectory.")
841
set(EIGEN3_INCLUDE_DIR ${CMAKE_CURRENT_LIST_DIR}/../third_party/eigen)
842
caffe2_update_option(USE_SYSTEM_EIGEN_INSTALL OFF)
845
message(STATUS "Using third party subdirectory Eigen.")
846
set(EIGEN3_INCLUDE_DIR ${CMAKE_CURRENT_LIST_DIR}/../third_party/eigen)
848
include_directories(SYSTEM ${EIGEN3_INCLUDE_DIR})
851
# ---[ Python Interpreter
852
# If not given a Python installation, then use the current active Python
853
if(NOT Python_EXECUTABLE)
855
COMMAND "which" "python3" RESULT_VARIABLE _exitcode OUTPUT_VARIABLE _py_exe)
856
if(${_exitcode} EQUAL 0)
858
string(STRIP ${_py_exe} Python_EXECUTABLE)
860
message(STATUS "Setting Python to ${Python_EXECUTABLE}")
865
set(PYTHON_COMPONENTS Development.Module)
867
list(APPEND PYTHON_COMPONENTS NumPy)
869
find_package(Python COMPONENTS Interpreter OPTIONAL_COMPONENTS ${PYTHON_COMPONENTS})
871
find_package(Python COMPONENTS Interpreter)
874
if(NOT Python_Interpreter_FOUND)
875
message(FATAL_ERROR "Python3 could not be found.")
878
if(${Python_VERSION} VERSION_LESS 3.8)
880
"Found Python libraries version ${Python_VERSION}. Python < 3.8 is no longer supported by PyTorch.")
885
if(Python_Development.Module_FOUND)
887
if(NOT Python_NumPy_FOUND)
888
message(WARNING "NumPy could not be found. Not building with NumPy. Suppress this warning with -DUSE_NUMPY=OFF")
889
caffe2_update_option(USE_NUMPY OFF)
891
caffe2_update_option(USE_NUMPY ON)
894
# Observers are required in the python build
895
caffe2_update_option(USE_OBSERVERS ON)
897
message(WARNING "Python dependencies not met. Not compiling with python. Suppress this warning with -DBUILD_PYTHON=OFF")
898
caffe2_update_option(BUILD_PYTHON OFF)
903
if(USE_SYSTEM_PYBIND11)
904
find_package(pybind11 CONFIG)
905
if(NOT pybind11_FOUND)
906
find_package(pybind11)
908
if(NOT pybind11_FOUND)
909
message(FATAL "Cannot find system pybind11")
912
message(STATUS "Using third_party/pybind11.")
913
set(pybind11_INCLUDE_DIRS ${CMAKE_CURRENT_LIST_DIR}/../third_party/pybind11/include)
914
install(DIRECTORY ${pybind11_INCLUDE_DIRS}
915
DESTINATION ${CMAKE_INSTALL_PREFIX}
916
FILES_MATCHING PATTERN "*.h")
918
message(STATUS "pybind11 include dirs: " "${pybind11_INCLUDE_DIRS}")
919
add_library(pybind::pybind11 INTERFACE IMPORTED)
920
target_include_directories(pybind::pybind11 SYSTEM INTERFACE ${pybind11_INCLUDE_DIRS})
921
target_link_libraries(pybind::pybind11 INTERFACE Python::Module)
923
# ---[ OpenTelemetry API headers
924
find_package(OpenTelemetryApi)
925
if(NOT OpenTelemetryApi_FOUND)
926
message(STATUS "Using third_party/opentelemetry-cpp.")
927
set(OpenTelemetryApi_INCLUDE_DIRS ${CMAKE_CURRENT_LIST_DIR}/../third_party/opentelemetry-cpp/api/include)
929
message(STATUS "opentelemetry api include dirs: " "${OpenTelemetryApi_INCLUDE_DIRS}")
930
add_library(opentelemetry::api INTERFACE IMPORTED)
931
target_include_directories(opentelemetry::api SYSTEM INTERFACE ${OpenTelemetryApi_INCLUDE_DIRS})
937
message(STATUS "MPI support found")
938
message(STATUS "MPI compile flags: " ${MPI_CXX_COMPILE_FLAGS})
939
message(STATUS "MPI include path: " ${MPI_CXX_INCLUDE_PATH})
940
message(STATUS "MPI LINK flags path: " ${MPI_CXX_LINK_FLAGS})
941
message(STATUS "MPI libraries: " ${MPI_CXX_LIBRARIES})
942
find_program(OMPI_INFO
944
HINTS ${MPI_CXX_LIBRARIES}/../bin)
946
execute_process(COMMAND ${OMPI_INFO}
947
OUTPUT_VARIABLE _output)
948
if(_output MATCHES "smcuda")
949
message(STATUS "Found OpenMPI with CUDA support built.")
951
message(WARNING "OpenMPI found, but it is not built with CUDA support.")
952
set(CAFFE2_FORCE_FALLBACK_CUDA_MPI 1)
956
message(WARNING "Not compiling with MPI. Suppress this warning with -DUSE_MPI=OFF")
957
caffe2_update_option(USE_MPI OFF)
962
if(USE_OPENMP AND NOT TARGET caffe2::openmp)
963
include(${CMAKE_CURRENT_LIST_DIR}/Modules/FindOpenMP.cmake)
965
message(STATUS "Adding OpenMP CXX_FLAGS: " ${OpenMP_CXX_FLAGS})
966
if(APPLE AND USE_MPS)
967
string(APPEND CMAKE_OBJCXX_FLAGS " ${OpenMP_CXX_FLAGS}")
969
if(OpenMP_CXX_LIBRARIES)
970
message(STATUS "Will link against OpenMP libraries: ${OpenMP_CXX_LIBRARIES}")
972
add_library(caffe2::openmp INTERFACE IMPORTED)
973
target_link_libraries(caffe2::openmp INTERFACE OpenMP::OpenMP_CXX)
974
list(APPEND Caffe2_DEPENDENCY_LIBS caffe2::openmp)
975
if(MSVC AND OpenMP_CXX_LIBRARIES MATCHES ".*libiomp5md\\.lib.*")
976
target_compile_definitions(caffe2::openmp INTERFACE _OPENMP_NOFORCE_MANIFEST)
977
target_link_options(caffe2::openmp INTERFACE "/NODEFAULTLIB:vcomp")
980
message(WARNING "Not compiling with OpenMP. Suppress this warning with -DUSE_OPENMP=OFF")
981
caffe2_update_option(USE_OPENMP OFF)
987
# ---[ Android specific ones
989
list(APPEND Caffe2_DEPENDENCY_LIBS log)
994
message(STATUS "Looking for LLVM in ${USE_LLVM}")
995
find_package(LLVM PATHS ${USE_LLVM} NO_DEFAULT_PATH)
998
message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
999
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
1001
include_directories(${LLVM_INCLUDE_DIRS})
1002
add_definitions(-DTORCH_ENABLE_LLVM)
1008
if(CUDNN_VERSION VERSION_LESS 8.5)
1009
message(FATAL_ERROR "PyTorch needs CuDNN-8.5 or above, but found ${CUDNN_VERSION}. Builds are still possible with `USE_CUDNN=0`")
1011
set(CUDNN_FRONTEND_INCLUDE_DIR ${CMAKE_CURRENT_LIST_DIR}/../third_party/cudnn_frontend/include)
1012
target_include_directories(torch::cudnn INTERFACE ${CUDNN_FRONTEND_INCLUDE_DIR})
1017
# This prevents linking in the libtinfo from /opt/conda/lib which conflicts with ROCm libtinfo.
1018
# Currently only active for Ubuntu 20.04 and greater versions.
1019
if(UNIX AND EXISTS "/etc/os-release")
1020
file(STRINGS /etc/os-release OS_RELEASE)
1022
set(DISTRO_VERSION "")
1023
foreach(line ${OS_RELEASE})
1024
string(REGEX MATCH "^NAME=" DISTRO_NAME_MATCH ${line})
1025
if(NOT DISTRO_NAME_MATCH STREQUAL "")
1026
string(REGEX REPLACE "^NAME=\"(.*)\"" "\\1" DISTRO_NAME ${line})
1028
string(REGEX MATCH "^VERSION_ID=" DISTRO_VERSION_MATCH ${line})
1029
if(NOT DISTRO_VERSION_MATCH STREQUAL "")
1030
string(REGEX REPLACE "^VERSION_ID=\"(.*)\"" "\\1" DISTRO_VERSION ${line})
1033
if(DISTRO_NAME STREQUAL "Ubuntu" AND DISTRO_VERSION VERSION_GREATER_EQUAL "20.04")
1034
find_library(LIBTINFO_LOC tinfo NO_CMAKE_PATH NO_CMAKE_ENVIRONMENT_PATH)
1036
get_filename_component(LIBTINFO_LOC_PARENT ${LIBTINFO_LOC} DIRECTORY)
1037
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-rpath-link,${LIBTINFO_LOC_PARENT}")
1042
include(${CMAKE_CURRENT_LIST_DIR}/public/LoadHIP.cmake)
1043
if(PYTORCH_FOUND_HIP)
1044
message(INFO "Compiling with HIP for AMD.")
1045
caffe2_update_option(USE_ROCM ON)
1047
if(USE_NCCL AND NOT USE_SYSTEM_NCCL)
1048
message(INFO "Forcing USE_SYSTEM_NCCL to ON since it's required by using RCCL")
1049
caffe2_update_option(USE_SYSTEM_NCCL ON)
1052
list(APPEND HIP_CXX_FLAGS -fPIC)
1053
list(APPEND HIP_CXX_FLAGS -D__HIP_PLATFORM_AMD__=1)
1054
list(APPEND HIP_CXX_FLAGS -DCUDA_HAS_FP16=1)
1055
list(APPEND HIP_CXX_FLAGS -DUSE_ROCM)
1056
list(APPEND HIP_CXX_FLAGS -D__HIP_NO_HALF_OPERATORS__=1)
1057
list(APPEND HIP_CXX_FLAGS -D__HIP_NO_HALF_CONVERSIONS__=1)
1058
list(APPEND HIP_CXX_FLAGS -DTORCH_HIP_VERSION=${TORCH_HIP_VERSION})
1059
list(APPEND HIP_CXX_FLAGS -Wno-shift-count-negative)
1060
list(APPEND HIP_CXX_FLAGS -Wno-shift-count-overflow)
1061
list(APPEND HIP_CXX_FLAGS -Wno-duplicate-decl-specifier)
1062
list(APPEND HIP_CXX_FLAGS -DCAFFE2_USE_MIOPEN)
1063
list(APPEND HIP_CXX_FLAGS -DTHRUST_DEVICE_SYSTEM=THRUST_DEVICE_SYSTEM_HIP)
1064
list(APPEND HIP_CXX_FLAGS -std=c++17)
1065
list(APPEND HIP_CXX_FLAGS -DHIPBLAS_V2)
1066
if(HIP_NEW_TYPE_ENUMS)
1067
list(APPEND HIP_CXX_FLAGS -DHIP_NEW_TYPE_ENUMS)
1069
add_definitions(-DROCM_VERSION=${ROCM_VERSION_DEV_INT})
1070
add_definitions(-DTORCH_HIP_VERSION=${TORCH_HIP_VERSION})
1071
message("TORCH_HIP_VERSION=${TORCH_HIP_VERSION} is added as a compiler defines")
1073
if(CMAKE_BUILD_TYPE MATCHES Debug)
1074
list(APPEND HIP_CXX_FLAGS -g2)
1075
list(APPEND HIP_CXX_FLAGS -O0)
1076
list(APPEND HIP_HIPCC_FLAGS -fdebug-info-for-profiling)
1077
endif(CMAKE_BUILD_TYPE MATCHES Debug)
1079
# needed for compat with newer versions of hip-clang that introduced C++20 mangling rules
1080
list(APPEND HIP_HIPCC_FLAGS -fclang-abi-compat=17)
1082
set(HIP_CLANG_FLAGS ${HIP_CXX_FLAGS})
1083
# Ask hcc to generate device code during compilation so we can use
1084
# host linker to link.
1085
list(APPEND HIP_CLANG_FLAGS -fno-gpu-rdc)
1086
foreach(pytorch_rocm_arch ${PYTORCH_ROCM_ARCH})
1087
list(APPEND HIP_CLANG_FLAGS --offload-arch=${pytorch_rocm_arch})
1090
set(Caffe2_HIP_INCLUDE
1091
$<INSTALL_INTERFACE:include> ${Caffe2_HIP_INCLUDE})
1092
# This is needed for library added by hip_add_library (same for hip_add_executable)
1093
hip_include_directories(${Caffe2_HIP_INCLUDE})
1095
set(Caffe2_PUBLIC_HIP_DEPENDENCY_LIBS
1096
${PYTORCH_HIP_LIBRARIES} ${PYTORCH_MIOPEN_LIBRARIES} ${hipcub_LIBRARIES} ${ROCM_HIPRTC_LIB} ${ROCM_ROCTX_LIB})
1097
list(APPEND Caffe2_PUBLIC_HIP_DEPENDENCY_LIBS ${hipblaslt_LIBRARIES})
1099
list(APPEND Caffe2_PUBLIC_HIP_DEPENDENCY_LIBS
1100
roc::hipblas hip::hipfft hip::hiprand roc::hipsparse roc::hipsolver)
1102
# ---[ Kernel asserts
1103
# Kernel asserts is disabled for ROCm by default.
1104
# It can be turned on by turning on the env USE_ROCM_KERNEL_ASSERT to the build system.
1105
if(USE_ROCM_KERNEL_ASSERT)
1106
message(STATUS "Enabling Kernel Assert for ROCm")
1108
message(STATUS "Disabling Kernel Assert for ROCm")
1112
caffe2_update_option(USE_ROCM OFF)
1118
if(NOT (USE_CUDA OR USE_ROCM))
1120
"Not using CUDA/ROCM, so disabling USE_NCCL. Suppress this warning with "
1122
caffe2_update_option(USE_NCCL OFF)
1123
elseif(NOT CMAKE_SYSTEM_NAME STREQUAL "Linux")
1124
message(WARNING "NCCL is currently only supported under Linux.")
1125
caffe2_update_option(USE_NCCL OFF)
1127
include(${CMAKE_CURRENT_LIST_DIR}/External/nccl.cmake)
1128
list(APPEND Caffe2_CUDA_DEPENDENCY_LIBS __caffe2_nccl)
1130
include(${CMAKE_CURRENT_LIST_DIR}/External/rccl.cmake)
1131
list(APPEND Caffe2_CUDA_DEPENDENCY_LIBS __caffe2_nccl)
1137
if(NOT CMAKE_SYSTEM_NAME STREQUAL "Linux")
1138
message(WARNING "UCC is currently only supported under Linux.")
1139
caffe2_update_option(USE_UCC OFF)
1141
include(${CMAKE_CURRENT_LIST_DIR}/External/ucc.cmake)
1149
message(FATAL_ERROR "Cannot find CUB.")
1151
include_directories(SYSTEM ${CUB_INCLUDE_DIRS})
1154
if(USE_DISTRIBUTED AND USE_TENSORPIPE)
1156
message(WARNING "Tensorpipe cannot be used on Windows.")
1159
set(TP_USE_CUDA ON CACHE BOOL "" FORCE)
1160
set(TP_ENABLE_CUDA_IPC ON CACHE BOOL "" FORCE)
1162
set(TP_BUILD_LIBUV ON CACHE BOOL "" FORCE)
1163
add_compile_options(-DTORCH_USE_LIBUV)
1164
include_directories(BEFORE SYSTEM ${CMAKE_CURRENT_LIST_DIR}/../third_party/tensorpipe/third_party/libuv/include)
1165
set(TP_STATIC_OR_SHARED STATIC CACHE STRING "" FORCE)
1167
# Tensorpipe uses cuda_add_library
1168
torch_update_find_cuda_flags()
1169
add_subdirectory(${PROJECT_SOURCE_DIR}/third_party/tensorpipe)
1171
list(APPEND Caffe2_DEPENDENCY_LIBS tensorpipe)
1172
list(APPEND Caffe2_DEPENDENCY_LIBS nlohmann)
1174
list(APPEND Caffe2_CUDA_DEPENDENCY_LIBS tensorpipe_cuda)
1176
message(WARNING "TensorPipe doesn't yet support ROCm")
1178
# list(APPEND Caffe2_HIP_DEPENDENCY_LIBS tensorpipe_hip)
1184
if(NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
1185
message(WARNING "Gloo can only be used on 64-bit systems.")
1186
caffe2_update_option(USE_GLOO OFF)
1188
# Don't install gloo
1189
set(GLOO_INSTALL OFF CACHE BOOL "" FORCE)
1190
set(GLOO_STATIC_OR_SHARED STATIC CACHE STRING "" FORCE)
1192
# Temporarily override variables to avoid building Gloo tests/benchmarks
1193
set(__BUILD_TEST ${BUILD_TEST})
1194
set(__BUILD_BENCHMARK ${BUILD_BENCHMARK})
1196
set(BUILD_BENCHMARK OFF)
1198
set(ENV{GLOO_ROCM_ARCH} "${PYTORCH_ROCM_ARCH}")
1200
if(NOT USE_SYSTEM_GLOO)
1201
if(USE_DISTRIBUED AND USE_TENSORPIPE)
1202
get_target_property(_include_dirs uv_a INCLUDE_DIRECTORIES)
1203
set_target_properties(uv_a PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${_include_dirs}")
1205
if(USE_NCCL AND NOT USE_SYSTEM_NCCL)
1206
# Tell Gloo build system to use bundled NCCL, see
1207
# https://github.com/facebookincubator/gloo/blob/950c0e23819779a9e0c70b861db4c52b31d1d1b2/cmake/Dependencies.cmake#L123
1208
set(NCCL_EXTERNAL ON)
1210
set(GLOO_USE_CUDA_TOOLKIT ON CACHE BOOL "" FORCE)
1211
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../third_party/gloo)
1213
add_library(gloo SHARED IMPORTED)
1214
find_library(GLOO_LIBRARY gloo)
1215
if(NOT GLOO_LIBRARY)
1216
message(FATAL_ERROR "Cannot find gloo")
1218
message("Found gloo: ${GLOO_LIBRARY}")
1219
set_target_properties(gloo PROPERTIES IMPORTED_LOCATION ${GLOO_LIBRARY})
1221
# Here is a little bit hacky. We have to put PROJECT_BINARY_DIR in front
1222
# of PROJECT_SOURCE_DIR with/without conda system. The reason is that
1223
# gloo generates a new config.h in the binary diretory.
1224
include_directories(BEFORE SYSTEM ${CMAKE_CURRENT_LIST_DIR}/../third_party/gloo)
1225
include_directories(BEFORE SYSTEM ${PROJECT_BINARY_DIR}/third_party/gloo)
1226
set(BUILD_TEST ${__BUILD_TEST})
1227
set(BUILD_BENCHMARK ${__BUILD_BENCHMARK})
1229
# Add explicit dependency since NCCL is built from third_party.
1230
# Without dependency, make -jN with N>1 can fail if the NCCL build
1231
# hasn't finished when CUDA targets are linked.
1232
if(NOT USE_SYSTEM_NCCL AND USE_NCCL AND NOT USE_ROCM)
1233
add_dependencies(gloo_cuda nccl_external)
1235
# Pick the right dependency depending on USE_CUDA
1236
list(APPEND Caffe2_DEPENDENCY_LIBS gloo)
1238
list(APPEND Caffe2_CUDA_DEPENDENCY_LIBS gloo_cuda)
1240
list(APPEND Caffe2_HIP_DEPENDENCY_LIBS gloo_hip)
1242
add_compile_options(-DCAFFE2_USE_GLOO)
1248
find_package(htrace)
1250
set(USE_PROF_HTRACE ON)
1252
message(WARNING "htrace not found. Caffe2 will build without htrace prof")
1256
if(USE_SNPE AND ANDROID)
1257
if(SNPE_LOCATION AND SNPE_HEADERS)
1258
message(STATUS "Using SNPE location specified by -DSNPE_LOCATION: " ${SNPE_LOCATION})
1259
message(STATUS "Using SNPE headers specified by -DSNPE_HEADERS: " ${SNPE_HEADERS})
1260
include_directories(SYSTEM ${SNPE_HEADERS})
1261
add_library(snpe SHARED IMPORTED)
1262
set_property(TARGET snpe PROPERTY IMPORTED_LOCATION ${SNPE_LOCATION})
1263
list(APPEND Caffe2_DEPENDENCY_LIBS snpe)
1265
caffe2_update_option(USE_SNPE OFF)
1269
if(USE_NNAPI AND NOT ANDROID)
1270
message(WARNING "NNApi is only used in android builds.")
1271
caffe2_update_option(USE_NNAPI OFF)
1275
if(CAFFE2_CMAKE_BUILDING_WITH_MAIN_REPO AND NOT INTERN_DISABLE_ONNX)
1276
if(EXISTS "${CAFFE2_CUSTOM_PROTOC_EXECUTABLE}")
1277
set(ONNX_CUSTOM_PROTOC_EXECUTABLE ${CAFFE2_CUSTOM_PROTOC_EXECUTABLE})
1279
set(TEMP_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS})
1280
set(BUILD_SHARED_LIBS OFF)
1281
set(ONNX_USE_MSVC_STATIC_RUNTIME ${CAFFE2_USE_MSVC_STATIC_RUNTIME})
1282
set(ONNX_USE_LITE_PROTO ${CAFFE2_USE_LITE_PROTO})
1283
# If linking local protobuf, make sure ONNX has the same protobuf
1284
# patches as Caffe2 and Caffe proto. This forces some functions to
1285
# not be inline and instead route back to the statically-linked protobuf.
1286
if(CAFFE2_LINK_LOCAL_PROTOBUF)
1287
set(ONNX_PROTO_POST_BUILD_SCRIPT ${PROJECT_SOURCE_DIR}/cmake/ProtoBufPatch.cmake)
1290
add_definitions(-DONNX_ML=1)
1292
add_definitions(-DONNXIFI_ENABLE_EXT=1)
1293
if(NOT USE_SYSTEM_ONNX)
1294
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../third_party/onnx EXCLUDE_FROM_ALL)
1296
set_target_properties(onnx_proto PROPERTIES CXX_STANDARD 17)
1300
add_definitions(-DONNX_NAMESPACE=${ONNX_NAMESPACE})
1301
if(NOT USE_SYSTEM_ONNX)
1302
include_directories(${ONNX_INCLUDE_DIRS})
1303
# In mobile build we care about code size, and so we need drop
1304
# everything (e.g. checker) in onnx but the pb definition.
1306
caffe2_interface_library(onnx_proto onnx_library)
1308
caffe2_interface_library(onnx onnx_library)
1310
list(APPEND Caffe2_DEPENDENCY_WHOLE_LINK_LIBS onnx_library)
1312
add_library(onnx SHARED IMPORTED)
1313
find_library(ONNX_LIBRARY onnx)
1314
if(NOT ONNX_LIBRARY)
1315
message(FATAL_ERROR "Cannot find onnx")
1317
set_property(TARGET onnx PROPERTY IMPORTED_LOCATION ${ONNX_LIBRARY})
1318
add_library(onnx_proto SHARED IMPORTED)
1319
find_library(ONNX_PROTO_LIBRARY onnx_proto)
1320
if(NOT ONNX_PROTO_LIBRARY)
1321
message(FATAL_ERROR "Cannot find onnx")
1323
set_property(TARGET onnx_proto PROPERTY IMPORTED_LOCATION ${ONNX_PROTO_LIBRARY})
1324
message("-- Found onnx: ${ONNX_LIBRARY} ${ONNX_PROTO_LIBRARY}")
1325
list(APPEND Caffe2_DEPENDENCY_LIBS onnx_proto onnx)
1327
# Recover the build shared libs option.
1328
set(BUILD_SHARED_LIBS ${TEMP_BUILD_SHARED_LIBS})
1334
# we need to build all targets to be linked with PIC
1335
if(USE_KINETO AND INTERN_BUILD_MOBILE AND USE_LITE_INTERPRETER_PROFILER)
1336
set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
1339
if(NOT INTERN_BUILD_MOBILE)
1340
set(TORCH_CUDA_ARCH_LIST $ENV{TORCH_CUDA_ARCH_LIST})
1341
string(APPEND CMAKE_CUDA_FLAGS " $ENV{TORCH_NVCC_FLAGS}")
1342
set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
1344
# Top-level build config
1345
############################################
1348
# Detect CUDA architecture and get best NVCC flags
1349
# finding cuda must be first because other things depend on the result
1351
# NB: We MUST NOT run this find_package if NOT USE_CUDA is set, because upstream
1352
# FindCUDA has a bug where it will still attempt to make use of NOTFOUND
1353
# compiler variables to run various probe tests. We could try to fix
1354
# this, but since FindCUDA upstream is subsumed by first-class support
1355
# for CUDA language, it seemed not worth fixing.
1358
# we want to respect the standard, and we are bored of those **** .
1359
add_definitions(-D_CRT_SECURE_NO_DEPRECATE=1)
1360
string(APPEND CMAKE_CUDA_FLAGS " -Xcompiler=/wd4819,/wd4503,/wd4190,/wd4244,/wd4251,/wd4275,/wd4522")
1363
string(APPEND CMAKE_CUDA_FLAGS " -Wno-deprecated-gpu-targets --expt-extended-lambda")
1365
# use cub in a safe manner, see:
1366
# https://github.com/pytorch/pytorch/pull/55292
1367
string(APPEND CMAKE_CUDA_FLAGS " -DCUB_WRAPPED_NAMESPACE=at_cuda_detail")
1369
message(STATUS "Found CUDA with FP16 support, compiling with torch.cuda.HalfTensor")
1370
string(APPEND CMAKE_CUDA_FLAGS " -DCUDA_HAS_FP16=1"
1371
" -D__CUDA_NO_HALF_OPERATORS__"
1372
" -D__CUDA_NO_HALF_CONVERSIONS__"
1373
" -D__CUDA_NO_HALF2_OPERATORS__"
1374
" -D__CUDA_NO_BFLOAT16_CONVERSIONS__")
1376
string(APPEND CMAKE_C_FLAGS_RELEASE " -DNDEBUG")
1377
string(APPEND CMAKE_CXX_FLAGS_RELEASE " -DNDEBUG")
1378
if(NOT GENERATOR_IS_MULTI_CONFIG)
1379
if(${CMAKE_BUILD_TYPE} STREQUAL "Release")
1380
message(STATUS "Adding -DNDEBUG to compile flags")
1381
string(APPEND CMAKE_C_FLAGS " -DNDEBUG")
1382
string(APPEND CMAKE_CXX_FLAGS " -DNDEBUG")
1384
message(STATUS "Removing -DNDEBUG from compile flags")
1385
string(REGEX REPLACE "[-/]DNDEBUG" "" CMAKE_C_FLAGS "" ${CMAKE_C_FLAGS})
1386
string(REGEX REPLACE "[-/]DNDEBUG" "" CMAKE_CXX_FLAGS "" ${CMAKE_CXX_FLAGS})
1389
string(REGEX REPLACE "[-/]DNDEBUG" "" CMAKE_C_FLAGS_DEBUG "" ${CMAKE_C_FLAGS_DEBUG})
1390
string(REGEX REPLACE "[-/]DNDEBUG" "" CMAKE_CXX_FLAGS_DEBUG "" ${CMAKE_CXX_FLAGS_DEBUG})
1392
set(CUDA_ATTACH_VS_BUILD_RULE_TO_CUDA_FILE OFF)
1394
if(USE_CUDA OR USE_ROCM)
1398
message(STATUS "Compiling with MAGMA support")
1399
message(STATUS "MAGMA INCLUDE DIRECTORIES: ${MAGMA_INCLUDE_DIR}")
1400
message(STATUS "MAGMA LIBRARIES: ${MAGMA_LIBRARIES}")
1401
message(STATUS "MAGMA V2 check: ${MAGMA_V2}")
1403
message(STATUS "MAGMA not found. Compiling without MAGMA support")
1404
caffe2_update_option(USE_MAGMA OFF)
1409
"Not compiling with MAGMA. Suppress this warning with "
1411
caffe2_update_option(USE_MAGMA OFF)
1414
# ARM specific flags
1417
message(STATUS "asimd/Neon found with compiler flag : -D__NEON__")
1418
add_compile_options(-D__NEON__)
1421
message(STATUS "Neon found with compiler flag : -D__NEON__")
1422
add_compile_options(-D__NEON__)
1424
message(STATUS "Neon found with compiler flag : -mfpu=neon -D__NEON__")
1425
add_compile_options(-mfpu=neon -D__NEON__)
1429
message(STATUS "Cortex-A8 Found with compiler flag : -mcpu=cortex-a8")
1430
add_compile_options(-mcpu=cortex-a8 -fprefetch-loop-arrays)
1433
message(STATUS "Cortex-A9 Found with compiler flag : -mcpu=cortex-a9")
1434
add_compile_options(-mcpu=cortex-a9)
1437
find_package(LAPACK)
1440
list(APPEND Caffe2_PRIVATE_DEPENDENCY_LIBS ${LAPACK_LIBRARIES})
1444
message("disabling CUDA because NOT USE_CUDA is set")
1445
set(AT_CUDA_ENABLED 0)
1447
set(AT_CUDA_ENABLED 1)
1451
message("disabling ROCM because NOT USE_ROCM is set")
1452
message(STATUS "MIOpen not found. Compiling without MIOpen support")
1453
set(AT_ROCM_ENABLED 0)
1455
include_directories(BEFORE ${MIOPEN_INCLUDE_DIRS})
1456
set(AT_ROCM_ENABLED 1)
1460
if(NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
1462
"x64 operating system is required for MKLDNN. "
1463
"Not compiling with MKLDNN. "
1464
"Turn this warning off by USE_MKLDNN=OFF.")
1468
set(AT_MKLDNN_ACL_ENABLED 1)
1472
include(${CMAKE_CURRENT_LIST_DIR}/public/mkldnn.cmake)
1474
set(AT_MKLDNN_ENABLED 1)
1475
include_directories(AFTER SYSTEM ${MKLDNN_INCLUDE_DIR})
1477
message(WARNING "MKLDNN could not be found.")
1478
caffe2_update_option(USE_MKLDNN OFF)
1481
message("disabling MKLDNN because USE_MKLDNN is not set")
1484
if(UNIX AND NOT APPLE)
1485
include(CheckLibraryExists)
1486
# https://github.com/libgit2/libgit2/issues/2128#issuecomment-35649830
1487
CHECK_LIBRARY_EXISTS(rt clock_gettime "time.h" NEED_LIBRT)
1489
list(APPEND Caffe2_DEPENDENCY_LIBS rt)
1490
set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} rt)
1492
endif(UNIX AND NOT APPLE)
1495
set(CMAKE_EXTRA_INCLUDE_FILES "sys/mman.h")
1496
CHECK_FUNCTION_EXISTS(mmap HAVE_MMAP)
1498
add_definitions(-DHAVE_MMAP=1)
1500
# done for lseek: https://www.gnu.org/software/libc/manual/html_node/File-Position-Primitive.html
1501
add_definitions(-D_FILE_OFFSET_BITS=64)
1502
CHECK_FUNCTION_EXISTS(shm_open HAVE_SHM_OPEN)
1504
add_definitions(-DHAVE_SHM_OPEN=1)
1505
endif(HAVE_SHM_OPEN)
1506
CHECK_FUNCTION_EXISTS(shm_unlink HAVE_SHM_UNLINK)
1508
add_definitions(-DHAVE_SHM_UNLINK=1)
1509
endif(HAVE_SHM_UNLINK)
1510
CHECK_FUNCTION_EXISTS(malloc_usable_size HAVE_MALLOC_USABLE_SIZE)
1511
if(HAVE_MALLOC_USABLE_SIZE)
1512
add_definitions(-DHAVE_MALLOC_USABLE_SIZE=1)
1513
endif(HAVE_MALLOC_USABLE_SIZE)
1516
add_definitions(-DUSE_EXTERNAL_MZCRC)
1517
add_definitions(-DMINIZ_DISABLE_ZIP_READER_CRC32_CHECKS)
1519
find_package(ZVECTOR) # s390x simd support
1525
set(TEMP_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS})
1526
set(BUILD_SHARED_LIBS OFF CACHE BOOL "Build shared libs" FORCE)
1527
add_subdirectory(${PROJECT_SOURCE_DIR}/third_party/fmt)
1529
# Disable compiler feature checks for `fmt`.
1531
# CMake compiles a little program to check compiler features. Some of our build
1532
# configurations (notably the mobile build analyzer) will populate
1533
# CMAKE_CXX_FLAGS in ways that break feature checks. Since we already know
1534
# `fmt` is compatible with a superset of the compilers that PyTorch is, it
1535
# shouldn't be too bad to just disable the checks.
1536
set_target_properties(fmt-header-only PROPERTIES INTERFACE_COMPILE_FEATURES "")
1538
list(APPEND Caffe2_DEPENDENCY_LIBS fmt::fmt-header-only)
1539
set(BUILD_SHARED_LIBS ${TEMP_BUILD_SHARED_LIBS} CACHE BOOL "Build shared libs" FORCE)
1542
# edge profiler depends on KinetoProfiler but it only does cpu
1543
# profiling. Thus we dont need USE_CUDA/USE_ROCM
1544
if(USE_KINETO AND INTERN_BUILD_MOBILE AND NOT (BUILD_LITE_INTERPRETER AND USE_LITE_INTERPRETER_PROFILER))
1545
message(STATUS "Not using libkineto in a mobile build.")
1549
if(USE_KINETO AND INTERN_BUILD_MOBILE AND USE_LITE_INTERPRETER_PROFILER AND (USE_CUDA OR USE_ROCM))
1550
message(FATAL_ERROR "Mobile build with profiler does not support CUDA or ROCM")
1554
if((NOT USE_CUDA) OR MSVC)
1555
set(LIBKINETO_NOCUPTI ON CACHE STRING "" FORCE)
1557
set(LIBKINETO_NOCUPTI OFF CACHE STRING "")
1558
message(STATUS "Using Kineto with CUPTI support")
1562
set(LIBKINETO_NOROCTRACER ON CACHE STRING "" FORCE)
1564
set(LIBKINETO_NOROCTRACER OFF CACHE STRING "")
1565
message(STATUS "Using Kineto with Roctracer support")
1568
if((NOT USE_XPU) OR WIN32)
1569
set(LIBKINETO_NOXPUPTI ON CACHE STRING "" FORCE)
1571
set(LIBKINETO_NOXPUPTI OFF CACHE STRING "")
1572
message(STATUS "Using Kineto with XPUPTI support")
1575
if(LIBKINETO_NOCUPTI AND LIBKINETO_NOROCTRACER AND LIBKINETO_NOXPUPTI)
1576
message(STATUS "Using CPU-only version of Kineto")
1579
set(CAFFE2_THIRD_PARTY_ROOT "${PROJECT_SOURCE_DIR}/third_party" CACHE STRING "")
1580
set(KINETO_SOURCE_DIR "${CAFFE2_THIRD_PARTY_ROOT}/kineto/libkineto" CACHE STRING "")
1581
set(KINETO_BUILD_TESTS OFF CACHE BOOL "")
1582
set(KINETO_LIBRARY_TYPE "static" CACHE STRING "")
1584
message(STATUS "Configuring Kineto dependency:")
1585
message(STATUS " KINETO_SOURCE_DIR = ${KINETO_SOURCE_DIR}")
1586
message(STATUS " KINETO_BUILD_TESTS = ${KINETO_BUILD_TESTS}")
1587
message(STATUS " KINETO_LIBRARY_TYPE = ${KINETO_LIBRARY_TYPE}")
1589
if(NOT LIBKINETO_NOCUPTI)
1590
set(CUDA_SOURCE_DIR "${CUDA_TOOLKIT_ROOT_DIR}" CACHE STRING "")
1591
message(STATUS " CUDA_SOURCE_DIR = ${CUDA_SOURCE_DIR}")
1592
message(STATUS " CUDA_INCLUDE_DIRS = ${CUDA_INCLUDE_DIRS}")
1596
set(CUPTI_LIB_NAME "libcupti.so")
1598
set(CUPTI_LIB_NAME "libcupti_static.a")
1601
set(CUPTI_LIB_NAME "cupti.lib")
1604
find_library(CUPTI_LIBRARY_PATH ${CUPTI_LIB_NAME} PATHS
1606
${CUDA_SOURCE_DIR}/extras/CUPTI/lib64
1607
${CUDA_SOURCE_DIR}/lib
1608
${CUDA_SOURCE_DIR}/lib64
1611
find_path(CUPTI_INCLUDE_DIR cupti.h PATHS
1612
${CUDA_SOURCE_DIR}/extras/CUPTI/include
1613
${CUDA_INCLUDE_DIRS}
1615
${CUDA_SOURCE_DIR}/include
1618
if(CUPTI_LIBRARY_PATH AND CUPTI_INCLUDE_DIR)
1619
message(STATUS " CUPTI_INCLUDE_DIR = ${CUPTI_INCLUDE_DIR}")
1620
set(CUDA_cupti_LIBRARY ${CUPTI_LIBRARY_PATH})
1621
message(STATUS " CUDA_cupti_LIBRARY = ${CUDA_cupti_LIBRARY}")
1622
message(STATUS "Found CUPTI")
1623
set(LIBKINETO_NOCUPTI OFF CACHE STRING "" FORCE)
1625
# I've only tested this sanity check on Linux; if someone
1626
# runs into this bug on another platform feel free to
1627
# generalize it accordingly
1628
if(NOT USE_CUPTI_SO AND UNIX)
1629
include(CheckCXXSourceRuns)
1630
# rt is handled by the CMAKE_REQUIRED_LIBRARIES set above
1632
set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} "dl" "pthread")
1634
set(CMAKE_REQUIRED_LINK_OPTIONS "-Wl,--whole-archive,${CUPTI_LIBRARY_PATH},--no-whole-archive")
1635
check_cxx_source_runs("#include <stdexcept>
1638
throw std::runtime_error(\"error\");
1644
set(CMAKE_REQUIRED_LINK_OPTIONS "")
1645
if(NOT EXCEPTIONS_WORK)
1646
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")
1651
message(STATUS "Could not find CUPTI library, using CPU-only Kineto build")
1652
set(LIBKINETO_NOCUPTI ON CACHE STRING "" FORCE)
1656
if(NOT LIBKINETO_NOROCTRACER)
1657
if("$ENV{ROCM_SOURCE_DIR}" STREQUAL "")
1658
set(ENV{ROCM_SOURCE_DIR} "/opt/rocm")
1662
if(NOT TARGET kineto)
1663
add_subdirectory("${KINETO_SOURCE_DIR}")
1664
set_property(TARGET kineto PROPERTY POSITION_INDEPENDENT_CODE ON)
1666
list(APPEND Caffe2_DEPENDENCY_LIBS kineto)
1667
string(APPEND CMAKE_CXX_FLAGS " -DUSE_KINETO")
1668
if(LIBKINETO_NOCUPTI)
1669
string(APPEND CMAKE_CXX_FLAGS " -DLIBKINETO_NOCUPTI")
1671
if(LIBKINETO_NOROCTRACER)
1672
string(APPEND CMAKE_CXX_FLAGS " -DLIBKINETO_NOROCTRACER")
1674
if(LIBKINETO_NOXPUPTI)
1675
string(APPEND CMAKE_CXX_FLAGS " -DLIBKINETO_NOXPUPTI=ON")
1677
string(APPEND CMAKE_CXX_FLAGS " -DLIBKINETO_NOXPUPTI=OFF")
1679
if(LIBKINETO_NOCUPTI AND LIBKINETO_NOROCTRACER AND LIBKINETO_NOXPUPTI)
1680
message(STATUS "Configured Kineto (CPU)")
1682
message(STATUS "Configured Kineto")
1686
# Include google/FlatBuffers
1687
include(${CMAKE_CURRENT_LIST_DIR}/FlatBuffers.cmake)
1689
# Include cpp-httplib
1690
add_library(httplib INTERFACE IMPORTED)
1691
target_include_directories(httplib SYSTEM INTERFACE ${PROJECT_SOURCE_DIR}/third_party/cpp-httplib)
1693
# Include nlohmann-json
1694
add_library(nlohmann INTERFACE IMPORTED)
1695
include_directories(nlohmann SYSTEM INTERFACE ${PROJECT_SOURCE_DIR}/third_party/nlohmann/include)