pytorch

Форк
0
/
Dependencies.cmake 
1988 строк · 73.6 Кб
1
# RPATH stuff
2
# see https://cmake.org/Wiki/CMake_RPATH_handling
3
if(APPLE)
4
  set(CMAKE_MACOSX_RPATH ON)
5
  set(_rpath_portable_origin "@loader_path")
6
else()
7
  set(_rpath_portable_origin $ORIGIN)
8
endif(APPLE)
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)
17

18
 # UBSAN triggers when compiling protobuf, so we need to disable it.
19
set(UBSAN_FLAG "-fsanitize=undefined")
20

21
macro(disable_ubsan)
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})
26
  endif()
27
endmacro()
28

29
macro(enable_ubsan)
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}")
33
  endif()
34
endmacro()
35

36
# ---[ CUDA
37
if(USE_CUDA)
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)
45
  if(CAFFE2_USE_CUDA)
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(CAFFE2_USE_NVRTC)
51
      list(APPEND Caffe2_PUBLIC_CUDA_DEPENDENCY_LIBS caffe2::cuda caffe2::nvrtc)
52
    else()
53
      caffe2_update_option(USE_NVRTC OFF)
54
    endif()
55
    list(APPEND Caffe2_CUDA_DEPENDENCY_LIBS caffe2::curand caffe2::cufft caffe2::cublas)
56
    if(CAFFE2_USE_CUDNN)
57
      list(APPEND Caffe2_CUDA_DEPENDENCY_LIBS torch::cudnn)
58
    else()
59
      caffe2_update_option(USE_CUDNN OFF)
60
    endif()
61
    if(CAFFE2_USE_CUSPARSELT)
62
      list(APPEND Caffe2_CUDA_DEPENDENCY_LIBS torch::cusparselt)
63
    else()
64
      caffe2_update_option(USE_CUSPARSELT OFF)
65
    endif()
66
    if(CAFFE2_USE_TENSORRT)
67
      list(APPEND Caffe2_PUBLIC_CUDA_DEPENDENCY_LIBS caffe2::tensorrt)
68
    else()
69
      caffe2_update_option(USE_TENSORRT OFF)
70
    endif()
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)
78
    endif()
79
  else()
80
    message(WARNING
81
      "Not compiling with CUDA. Suppress this warning with "
82
      "-DUSE_CUDA=OFF.")
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)
93
  endif()
94
endif()
95

96
# ---[ XPU
97
if(USE_XPU)
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)
103
  endif()
104
endif()
105

106
# ---[ Custom Protobuf
107
if(CAFFE2_CMAKE_BUILDING_WITH_MAIN_REPO AND NOT INTERN_BUILD_MOBILE)
108
  disable_ubsan()
109
  include(${CMAKE_CURRENT_LIST_DIR}/ProtoBuf.cmake)
110
  enable_ubsan()
111
endif()
112

113
if(USE_ASAN OR USE_TSAN)
114
  find_package(Sanitizer REQUIRED)
115
  if(USE_ASAN)
116
    if(TARGET Sanitizer::address)
117
      list(APPEND Caffe2_DEPENDENCY_LIBS Sanitizer::address)
118
    else()
119
      message(WARNING "Not ASAN found. Suppress this warning with -DUSE_ASAN=OFF.")
120
      caffe2_update_option(USE_ASAN OFF)
121
    endif()
122
    if(TARGET Sanitizer::undefined)
123
      list(APPEND Caffe2_DEPENDENCY_LIBS Sanitizer::undefined)
124
    endif()
125
  endif()
126
  if(USE_TSAN)
127
    if(TARGET Sanitizer::thread)
128
      list(APPEND Caffe2_DEPENDENCY_LIBS Sanitizer::thread)
129
    else()
130
      message(WARNING "Not TSAN found. Suppress this warning with -DUSE_TSAN=OFF.")
131
      caffe2_update_option(USE_TSAN OFF)
132
    endif()
133
  endif()
134
endif()
135

136
# ---[ Threads
137
find_package(Threads REQUIRED)
138
if(TARGET Threads::Threads)
139
  list(APPEND Caffe2_DEPENDENCY_LIBS Threads::Threads)
140
else()
141
  message(FATAL_ERROR
142
      "Cannot find threading library. PyTorch requires Threads to compile.")
143
endif()
144

145
if(USE_TBB)
146
  if(USE_SYSTEM_TBB)
147
    find_package(TBB 2018.0 REQUIRED CONFIG COMPONENTS tbb)
148

149
    get_target_property(TBB_INCLUDE_DIR TBB::tbb INTERFACE_INCLUDE_DIRECTORIES)
150
  else()
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})
155
    set(CMAKE_CXX_FLAGS)
156

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")
165

166
    set(CMAKE_CXX_FLAGS ${OLD_CMAKE_CXX_FLAGS})
167

168
    set(TBB_INCLUDE_DIR "${TBB_ROOT_DIR}/include")
169

170
    add_library(TBB::tbb ALIAS tbb)
171
  endif()
172
endif()
173

174
# ---[ protobuf
175
if(CAFFE2_CMAKE_BUILDING_WITH_MAIN_REPO)
176
  if(USE_LITE_PROTO)
177
    set(CAFFE2_USE_LITE_PROTO 1)
178
  endif()
179
endif()
180

181
# ---[ BLAS
182

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")
187
else()
188
  set(BLAS "Eigen" CACHE STRING "Selected BLAS library")
189
  set(AT_MKLDNN_ENABLED 0)
190
  set(AT_MKL_ENABLED 0)
191
endif()
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})
194

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")
204
  set(BLAS_FOUND 1)
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")
211
  set(BLAS_FOUND 1)
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")
218
  if(BLAS_SET_BY_USER)
219
    find_package(MKL REQUIRED)
220
  else()
221
    find_package(MKL QUIET)
222
  endif()
223
  include(${CMAKE_CURRENT_LIST_DIR}/public/mkl.cmake)
224
  if(MKL_FOUND)
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)
232
    set(BLAS_INFO "mkl")
233
    set(BLAS_FOUND 1)
234
    set(BLAS_LIBRARIES ${MKL_LIBRARIES})
235
  else()
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)
239
  endif()
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")
245
  set(BLAS_FOUND 1)
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")
255
  else()
256
    set(GENERIC_BLAS $ENV{GENERIC_BLAS_LIBRARIES})
257
  endif()
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")
263
  set(BLAS_FOUND 1)
264
else()
265
  message(FATAL_ERROR "Unrecognized BLAS option: " ${BLAS})
266
endif()
267

268
if(NOT INTERN_BUILD_MOBILE)
269
  set(AT_MKL_ENABLED 0)
270
  set(AT_MKL_SEQUENTIAL 0)
271
  set(USE_BLAS 1)
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")
274
    find_package(BLAS)
275
    if(NOT BLAS_FOUND)
276
      set(USE_BLAS 0)
277
    endif()
278
  endif()
279

280
  if(MKL_FOUND)
281
    if("${MKL_THREADING}" STREQUAL "SEQ")
282
      set(AT_MKL_SEQUENTIAL 1)
283
    endif()
284
    set(AT_MKL_ENABLED 1)
285
  endif()
286
elseif(INTERN_USE_EIGEN_BLAS)
287
  # Eigen BLAS for Mobile
288
  set(USE_BLAS 1)
289
  include(${CMAKE_CURRENT_LIST_DIR}/External/EigenBLAS.cmake)
290
  list(APPEND Caffe2_DEPENDENCY_LIBS eigen_blas)
291
endif()
292

293
# --- [ PocketFFT
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}")
301
  endif()
302

303
  set(AT_POCKETFFT_ENABLED 1)
304
  message(STATUS "Using pocketfft in directory: ${POCKETFFT_INCLUDE_DIR}")
305
endif()
306

307
# ---[ Dependencies
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.
313

314
if(USE_NNPACK OR USE_QNNPACK OR USE_PYTORCH_QNNPACK OR USE_XNNPACK)
315
  set(DISABLE_NNPACK_AND_FAMILY OFF)
316

317
  # Sanity checks - Can we actually build NNPACK and family given the configuration provided?
318
  # Disable them and warn the user if not.
319

320
  if(IOS)
321
    list(LENGTH IOS_ARCH IOS_ARCH_COUNT)
322
    if(IOS_ARCH_COUNT GREATER 1)
323
      message(WARNING
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)
328
    endif()
329
    if(NOT IOS_ARCH MATCHES "^(i386|x86_64|armv7.*|arm64.*)$")
330
      message(WARNING
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)
335
    endif()
336
  else()
337
    if(NOT IOS AND NOT (CMAKE_SYSTEM_NAME MATCHES "^(Android|Linux|Darwin|Windows)$"))
338
      message(WARNING
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)
343
    endif()
344
    if(NOT IOS AND NOT (CMAKE_SYSTEM_PROCESSOR MATCHES "^(i686|AMD64|x86_64|armv[0-9].*|arm64|aarch64)$"))
345
      message(WARNING
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)
350
    endif()
351
  endif()
352

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)
358
  else()
359
    # Disable unsupported NNPack combinations with MSVC
360
    if(MSVC)
361
      caffe2_update_option(USE_NNPACK OFF)
362
      caffe2_update_option(USE_QNNPACK OFF)
363
      caffe2_update_option(USE_PYTORCH_QNNPACK OFF)
364
    endif()
365

366
    set(CAFFE2_THIRD_PARTY_ROOT "${PROJECT_SOURCE_DIR}/third_party")
367

368
    if(NOT DEFINED CPUINFO_SOURCE_DIR)
369
      set(CPUINFO_SOURCE_DIR "${CAFFE2_THIRD_PARTY_ROOT}/cpuinfo" CACHE STRING "cpuinfo source directory")
370
    endif()
371
    if(NOT DEFINED FP16_SOURCE_DIR)
372
      set(FP16_SOURCE_DIR "${CAFFE2_THIRD_PARTY_ROOT}/FP16" CACHE STRING "FP16 source directory")
373
    endif()
374
    if(NOT DEFINED FXDIV_SOURCE_DIR)
375
      set(FXDIV_SOURCE_DIR "${CAFFE2_THIRD_PARTY_ROOT}/FXdiv" CACHE STRING "FXdiv source directory")
376
    endif()
377
    if(NOT DEFINED PSIMD_SOURCE_DIR)
378
      set(PSIMD_SOURCE_DIR "${CAFFE2_THIRD_PARTY_ROOT}/psimd" CACHE STRING "PSimd source directory")
379
    endif()
380
    if(NOT DEFINED PTHREADPOOL_SOURCE_DIR)
381
      set(PTHREADPOOL_SOURCE_DIR "${CAFFE2_THIRD_PARTY_ROOT}/pthreadpool" CACHE STRING "pthreadpool source directory")
382
    endif()
383
  endif()
384
else()
385
  set(DISABLE_NNPACK_AND_FAMILY ON)
386
endif()
387

388
if(USE_QNNPACK AND CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64" AND CMAKE_SYSTEM_NAME STREQUAL "Darwin")
389
  message(WARNING
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)
393
endif()
394

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")
399

400
# ---[ pthreadpool
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")
406

407
  # Always use third_party/pthreadpool.
408
  set(USE_INTERNAL_PTHREADPOOL_IMPL OFF CACHE BOOL "" FORCE)
409

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")
417
      endif()
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")
423
      endif()
424

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 "")
429
      add_subdirectory(
430
        "${PTHREADPOOL_SOURCE_DIR}"
431
        "${CONFU_DEPENDENCIES_BINARY_DIR}/pthreadpool")
432
      set_property(TARGET pthreadpool PROPERTY POSITION_INDEPENDENT_CODE ON)
433
    endif()
434

435
    if(USE_INTERNAL_PTHREADPOOL_IMPL)
436
      set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DUSE_INTERNAL_PTHREADPOOL_IMPL")
437
    else()
438
      list(APPEND Caffe2_DEPENDENCY_LIBS pthreadpool)
439
    endif()
440
  endif()
441
else()
442
  set(USE_PTHREADPOOL OFF CACHE BOOL "" FORCE)
443
endif()
444

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")
453
    endif()
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")
459
    endif()
460

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 "")
467
    if(MSVC)
468
      if(CAFFE2_USE_MSVC_STATIC_RUNTIME)
469
        set(CPUINFO_RUNTIME_TYPE "static" CACHE STRING "")
470
      else()
471
        set(CPUINFO_RUNTIME_TYPE "shared" CACHE STRING "")
472
      endif()
473
    endif()
474
    add_subdirectory(
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)
480
  endif()
481
  list(APPEND Caffe2_DEPENDENCY_LIBS cpuinfo)
482
endif()
483

484
# ---[ QNNPACK
485
if(USE_QNNPACK)
486
  set(CAFFE2_THIRD_PARTY_ROOT "${PROJECT_SOURCE_DIR}/third_party")
487

488
  if(NOT DEFINED QNNPACK_SOURCE_DIR)
489
    set(QNNPACK_SOURCE_DIR "${CAFFE2_THIRD_PARTY_ROOT}/QNNPACK" CACHE STRING "QNNPACK source directory")
490
  endif()
491

492
  if(NOT TARGET qnnpack)
493
    if(NOT USE_SYSTEM_PTHREADPOOL AND USE_INTERNAL_PTHREADPOOL_IMPL)
494
      set(QNNPACK_CUSTOM_THREADPOOL ON CACHE BOOL "")
495
    endif()
496

497
    set(QNNPACK_BUILD_TESTS OFF CACHE BOOL "")
498
    set(QNNPACK_BUILD_BENCHMARKS OFF CACHE BOOL "")
499
    set(QNNPACK_LIBRARY_TYPE "static" CACHE STRING "")
500
    add_subdirectory(
501
      "${QNNPACK_SOURCE_DIR}"
502
      "${CONFU_DEPENDENCIES_BINARY_DIR}/QNNPACK")
503

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)
507
    endif()
508

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)
513

514
    if(QNNPACK_CUSTOM_THREADPOOL)
515
      target_compile_definitions(
516
        qnnpack PRIVATE
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)
534
    endif()
535
  endif()
536

537
  list(APPEND Caffe2_DEPENDENCY_LIBS qnnpack)
538
endif()
539

540
# ---[ Caffe2 Int8 operators (enabled by USE_QNNPACK) depend on gemmlowp and neon2sse headers
541
if(USE_QNNPACK)
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")
545
endif()
546

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")
551
    endif()
552

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 "")
556
      endif()
557

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 "")
561
      add_subdirectory(
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)
568

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)
589
      endif()
590
    endif()
591

592
    list(APPEND Caffe2_DEPENDENCY_LIBS pytorch_qnnpack)
593
endif()
594

595
# ---[ NNPACK
596
if(USE_NNPACK)
597
  include(${CMAKE_CURRENT_LIST_DIR}/External/nnpack.cmake)
598
  if(NNPACK_FOUND)
599
    if(TARGET nnpack)
600
      # ---[ NNPACK is being built together with Caffe2: explicitly specify dependency
601
      list(APPEND Caffe2_DEPENDENCY_LIBS nnpack)
602
    else()
603
      include_directories(SYSTEM ${NNPACK_INCLUDE_DIRS})
604
      list(APPEND Caffe2_DEPENDENCY_LIBS ${NNPACK_LIBRARIES})
605
    endif()
606
  else()
607
    message(WARNING "Not compiling with NNPACK. Suppress this warning with -DUSE_NNPACK=OFF")
608
    caffe2_update_option(USE_NNPACK OFF)
609
  endif()
610
endif()
611

612
# ---[ XNNPACK
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")
616
  endif()
617

618
  if(NOT DEFINED XNNPACK_INCLUDE_DIR)
619
    set(XNNPACK_INCLUDE_DIR "${XNNPACK_SOURCE_DIR}/include" CACHE STRING "XNNPACK include directory")
620
  endif()
621

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 "")
626

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 "")
630

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 "")
634

635
    # Disable I8MM For CI since clang 9 does not support neon i8mm.
636
    set(XNNPACK_ENABLE_ARM_I8MM OFF CACHE BOOL "")
637

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)
643

644
    add_subdirectory(
645
      "${XNNPACK_SOURCE_DIR}"
646
      "${CONFU_DEPENDENCIES_BINARY_DIR}/XNNPACK")
647

648
    # Revert to whatever it was before
649
    set(CMAKE_POSITION_INDEPENDENT_CODE ${__caffe2_CMAKE_POSITION_INDEPENDENT_CODE_FLAG})
650
  endif()
651

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")
660
  endif()
661
  message("-- Found XNNPACK: ${XNNPACK_LIBRARY}")
662
  list(APPEND Caffe2_DEPENDENCY_LIBS XNNPACK)
663
endif()
664

665
# ---[ Vulkan deps
666
if(USE_VULKAN)
667
  set(Vulkan_DEFINES)
668
  set(Vulkan_INCLUDES)
669
  set(Vulkan_LIBS)
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})
674
endif()
675

676
# ---[ gflags
677
if(USE_GFLAGS)
678
  include(${CMAKE_CURRENT_LIST_DIR}/public/gflags.cmake)
679
  if(NOT TARGET gflags)
680
    message(WARNING
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)
685
  endif()
686
endif()
687

688
# ---[ Google-glog
689
if(USE_GLOG)
690
  include(${CMAKE_CURRENT_LIST_DIR}/public/glog.cmake)
691
  if(TARGET glog::glog)
692
    set(CAFFE2_USE_GOOGLE_GLOG 1)
693
  else()
694
    message(WARNING
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)
699
  endif()
700
endif()
701

702

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})
707

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)
710

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)
718
  endif()
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)
723
    execute_process(
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"
727
              "-DREVERT=0"
728
              "-P"
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.")
733
    endif()
734
  endif()
735

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})
747

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)
750

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)
757
  else()
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")
762
    endif()
763
    message("-- Found benchmark: ${BENCHMARK_LIBRARY}")
764
    set_property(TARGET benchmark PROPERTY IMPORTED_LOCATION ${BENCHMARK_LIBRARY})
765
  endif()
766
  include_directories(${CMAKE_CURRENT_LIST_DIR}/../third_party/benchmark/include)
767

768
  # Recover build options.
769
  set(BUILD_SHARED_LIBS ${TEMP_BUILD_SHARED_LIBS} CACHE BOOL "Build shared libs" FORCE)
770

771
  # To make the git clean after the build, we revert the changes here.
772
  if(MSVC AND MSVC_Z7_OVERRIDE)
773
    execute_process(
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"
777
              "-DREVERT=1"
778
              "-P"
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.")
783
    endif()
784
  endif()
785

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.")
792
    endif()
793
  endif()
794
endif()
795

796
# ---[ FBGEMM
797
if(USE_FBGEMM)
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")
801
  endif()
802
  if(NOT CAFFE2_COMPILER_SUPPORTS_AVX512_EXTENSIONS)
803
    message(WARNING
804
      "A compiler with AVX512 support is required for FBGEMM. "
805
      "Not compiling with FBGEMM. "
806
      "Turn this warning off by USE_FBGEMM=OFF.")
807
    set(USE_FBGEMM OFF)
808
  endif()
809
  if(NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
810
    message(WARNING
811
      "x64 operating system is required for FBGEMM. "
812
      "Not compiling with FBGEMM. "
813
      "Turn this warning off by USE_FBGEMM=OFF.")
814
    set(USE_FBGEMM OFF)
815
  endif()
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 "")
821
    else()
822
      set(FBGEMM_LIBRARY_TYPE "static" CACHE STRING "")
823
    endif()
824
    if(USE_ASAN)
825
      set(USE_SANITIZER "address,undefined" CACHE STRING "-fsanitize options for FBGEMM")
826
    endif()
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)
836
    endif()
837
  endif()
838

839
  if(USE_FBGEMM)
840
    list(APPEND Caffe2_DEPENDENCY_LIBS fbgemm)
841
  endif()
842
endif()
843

844
if(USE_FBGEMM)
845
  caffe2_update_option(USE_FBGEMM ON)
846
else()
847
  caffe2_update_option(USE_FBGEMM OFF)
848
  message(WARNING
849
    "Turning USE_FAKELOWP off as it depends on USE_FBGEMM.")
850
  caffe2_update_option(USE_FAKELOWP OFF)
851
endif()
852

853
# ---[ LMDB
854
if(USE_LMDB)
855
  find_package(LMDB)
856
  if(LMDB_FOUND)
857
    include_directories(SYSTEM ${LMDB_INCLUDE_DIR})
858
    list(APPEND Caffe2_DEPENDENCY_LIBS ${LMDB_LIBRARIES})
859
  else()
860
    message(WARNING "Not compiling with LMDB. Suppress this warning with -DUSE_LMDB=OFF")
861
    caffe2_update_option(USE_LMDB OFF)
862
  endif()
863
endif()
864

865
if(USE_OPENCL)
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})
871
endif()
872

873
# ---[ LevelDB
874
# ---[ Snappy
875
if(USE_LEVELDB)
876
  find_package(LevelDB)
877
  find_package(Snappy)
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})
883
  else()
884
    message(WARNING "Not compiling with LevelDB. Suppress this warning with -DUSE_LEVELDB=OFF")
885
    caffe2_update_option(USE_LEVELDB OFF)
886
  endif()
887
endif()
888

889
# ---[ NUMA
890
if(USE_NUMA)
891
  if(LINUX)
892
    find_package(Numa)
893
    if(NOT NUMA_FOUND)
894
      message(WARNING "Not compiling with NUMA. Suppress this warning with -DUSE_NUMA=OFF")
895
      caffe2_update_option(USE_NUMA OFF)
896
    endif()
897
  else()
898
    message(WARNING "NUMA is currently only supported under Linux.")
899
    caffe2_update_option(USE_NUMA OFF)
900
  endif()
901
endif()
902

903
# ---[ ZMQ
904
if(USE_ZMQ)
905
  find_package(ZMQ)
906
  if(ZMQ_FOUND)
907
    include_directories(SYSTEM ${ZMQ_INCLUDE_DIR})
908
    list(APPEND Caffe2_DEPENDENCY_LIBS ${ZMQ_LIBRARIES})
909
  else()
910
    message(WARNING "Not compiling with ZMQ. Suppress this warning with -DUSE_ZMQ=OFF")
911
    caffe2_update_option(USE_ZMQ OFF)
912
  endif()
913
endif()
914

915
# ---[ Redis
916
if(USE_REDIS)
917
  find_package(Hiredis)
918
  if(HIREDIS_FOUND)
919
    include_directories(SYSTEM ${Hiredis_INCLUDE})
920
    list(APPEND Caffe2_DEPENDENCY_LIBS ${Hiredis_LIBRARIES})
921
  else()
922
    message(WARNING "Not compiling with Redis. Suppress this warning with -DUSE_REDIS=OFF")
923
    caffe2_update_option(USE_REDIS OFF)
924
  endif()
925
endif()
926

927

928
# ---[ OpenCV
929
if(USE_OPENCV)
930
  # OpenCV 4
931
  find_package(OpenCV 4 QUIET COMPONENTS core highgui imgproc imgcodecs optflow videoio video)
932
  if(NOT OpenCV_FOUND)
933
    # OpenCV 3
934
    find_package(OpenCV 3 QUIET COMPONENTS core highgui imgproc imgcodecs videoio video)
935
    if(NOT OpenCV_FOUND)
936
      # OpenCV 2
937
      find_package(OpenCV QUIET COMPONENTS core highgui imgproc)
938
    endif()
939
  endif()
940
  if(OpenCV_FOUND)
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})
945
    endif()
946
    message(STATUS "OpenCV found (${OpenCV_CONFIG_PATH})")
947
  else()
948
    message(WARNING "Not compiling with OpenCV. Suppress this warning with -DUSE_OPENCV=OFF")
949
    caffe2_update_option(USE_OPENCV OFF)
950
  endif()
951
endif()
952

953
# ---[ FFMPEG
954
if(USE_FFMPEG)
955
  find_package(FFmpeg REQUIRED)
956
  if(FFMPEG_FOUND)
957
    message("Found FFMPEG/LibAV libraries")
958
    include_directories(SYSTEM ${FFMPEG_INCLUDE_DIR})
959
    list(APPEND Caffe2_DEPENDENCY_LIBS ${FFMPEG_LIBRARIES})
960
  else()
961
    message("Not compiling with FFmpeg. Suppress this warning with -DUSE_FFMPEG=OFF")
962
    caffe2_update_option(USE_FFMPEG OFF)
963
  endif()
964
endif()
965

966
if(USE_ITT)
967
  find_package(ITT)
968
  if(ITT_FOUND)
969
    include_directories(SYSTEM ${ITT_INCLUDE_DIR})
970
    list(APPEND Caffe2_DEPENDENCY_LIBS ${ITT_LIBRARIES})
971
    list(APPEND TORCH_PYTHON_LINK_LIBRARIES ${ITT_LIBRARIES})
972
  else()
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)
976
  endif()
977
endif()
978

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")
985
  endif()
986
  if(NOT DEFINED FP16_SOURCE_DIR)
987
    set(FP16_SOURCE_DIR "${CAFFE2_THIRD_PARTY_ROOT}/FP16" CACHE STRING "FP16 source directory")
988
  endif()
989

990
  set(FP16_BUILD_TESTS OFF CACHE BOOL "")
991
  set(FP16_BUILD_BENCHMARKS OFF CACHE BOOL "")
992
  add_subdirectory(
993
    "${FP16_SOURCE_DIR}"
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)
998
endif()
999
list(APPEND Caffe2_DEPENDENCY_LIBS fp16)
1000

1001
# ---[ EIGEN
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)
1006
  if(EIGEN3_FOUND)
1007
    message(STATUS "Found system Eigen at " ${EIGEN3_INCLUDE_DIR})
1008
  else()
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)
1012
  endif()
1013
else()
1014
  message(STATUS "Using third party subdirectory Eigen.")
1015
  set(EIGEN3_INCLUDE_DIR ${CMAKE_CURRENT_LIST_DIR}/../third_party/eigen)
1016
endif()
1017
include_directories(SYSTEM ${EIGEN3_INCLUDE_DIR})
1018

1019
# ---[ Python + Numpy
1020
if(BUILD_PYTHON)
1021
  # If not given a Python installation, then use the current active Python
1022
  if(NOT PYTHON_EXECUTABLE)
1023
    execute_process(
1024
      COMMAND "which" "python" RESULT_VARIABLE _exitcode OUTPUT_VARIABLE _py_exe)
1025
    if(${_exitcode} EQUAL 0)
1026
      if(NOT MSVC)
1027
        string(STRIP ${_py_exe} PYTHON_EXECUTABLE)
1028
      endif()
1029
      message(STATUS "Setting Python to ${PYTHON_EXECUTABLE}")
1030
    endif()
1031
  endif()
1032

1033
  # Check that Python works
1034
  set(PYTHON_VERSION)
1035
  if(DEFINED PYTHON_EXECUTABLE)
1036
    execute_process(
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.")
1041
    endif()
1042
    if(PYTHON_VERSION)
1043
      string(REGEX MATCH "([0-9]+)\\.([0-9]+)" PYTHON_VERSION ${PYTHON_VERSION})
1044
    endif()
1045
  endif()
1046

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
1050
  # each other).
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")
1057
    else()
1058
      message(WARNING "Could not set Python's include dir to ${_py_inc} from sysconfig")
1059
    endif()
1060
  endif(NOT PYTHON_INCLUDE_DIR)
1061

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}")
1066
      if(MSVC)
1067
        string(REPLACE "Lib" "libs" _py_static_lib ${_py_lib})
1068
        link_directories(${_py_static_lib})
1069
      endif()
1070
      message(STATUS "Setting Python's library to ${PYTHON_LIBRARY}")
1071
    endif()
1072
  endif(NOT PYTHON_LIBRARY)
1073

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)
1079

1080
  if(NOT PYTHONLIBS_VERSION_STRING)
1081
    message(FATAL_ERROR
1082
      "Python development libraries could not be found.")
1083
  endif()
1084

1085
  if(${PYTHONLIBS_VERSION_STRING} VERSION_LESS 3)
1086
    message(FATAL_ERROR
1087
      "Found Python libraries version ${PYTHONLIBS_VERSION_STRING}. Python 2 has reached end-of-life and is no longer supported by PyTorch.")
1088
  endif()
1089
  if(${PYTHONLIBS_VERSION_STRING} VERSION_LESS 3.8)
1090
    message(FATAL_ERROR
1091
      "Found Python libraries version ${PYTHONLIBS_VERSION_STRING}. Python < 3.8 is no longer supported by PyTorch.")
1092
  endif()
1093

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)
1097
    set(NUMPY_FOUND ON)
1098
  elseif(USE_NUMPY)
1099
    find_package(NumPy)
1100
    if(NOT NUMPY_FOUND)
1101
      message(WARNING "NumPy could not be found. Not building with NumPy. Suppress this warning with -DUSE_NUMPY=OFF")
1102
    endif()
1103
  endif()
1104

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})
1108
    if(WIN32)
1109
      target_link_libraries(python::python INTERFACE ${PYTHON_LIBRARIES})
1110
    endif()
1111

1112
    caffe2_update_option(USE_NUMPY OFF)
1113
    if(NUMPY_FOUND)
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})
1117
    endif()
1118
    # Observers are required in the python build
1119
    caffe2_update_option(USE_OBSERVERS ON)
1120
  else()
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)
1123
  endif()
1124
endif()
1125

1126
# ---[ pybind11
1127
if(USE_SYSTEM_PYBIND11)
1128
  find_package(pybind11 CONFIG)
1129
  if(NOT pybind11_FOUND)
1130
    find_package(pybind11)
1131
  endif()
1132
  if(NOT pybind11_FOUND)
1133
    message(FATAL "Cannot find system pybind11")
1134
  endif()
1135
else()
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")
1141
endif()
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)
1146
if(APPLE)
1147
  target_link_options(pybind::pybind11 INTERFACE -undefined dynamic_lookup)
1148
endif()
1149

1150
# ---[ MPI
1151
if(USE_MPI)
1152
  find_package(MPI)
1153
  if(MPI_CXX_FOUND)
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
1160
      NAMES ompi_info
1161
      HINTS ${MPI_CXX_LIBRARIES}/../bin)
1162
    if(OMPI_INFO)
1163
      execute_process(COMMAND ${OMPI_INFO}
1164
                      OUTPUT_VARIABLE _output)
1165
      if(_output MATCHES "smcuda")
1166
        message(STATUS "Found OpenMPI with CUDA support built.")
1167
      else()
1168
        message(WARNING "OpenMPI found, but it is not built with CUDA support.")
1169
        set(CAFFE2_FORCE_FALLBACK_CUDA_MPI 1)
1170
      endif()
1171
    endif()
1172
  else()
1173
    message(WARNING "Not compiling with MPI. Suppress this warning with -DUSE_MPI=OFF")
1174
    caffe2_update_option(USE_MPI OFF)
1175
  endif()
1176
endif()
1177

1178
# ---[ OpenMP
1179
if(USE_OPENMP AND NOT TARGET caffe2::openmp)
1180
  include(${CMAKE_CURRENT_LIST_DIR}/Modules/FindOpenMP.cmake)
1181
  if(OPENMP_FOUND)
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}")
1185
    endif()
1186
    if(OpenMP_CXX_LIBRARIES)
1187
      message(STATUS "Will link against OpenMP libraries: ${OpenMP_CXX_LIBRARIES}")
1188
    endif()
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")
1195
    endif()
1196
  else()
1197
    message(WARNING "Not compiling with OpenMP. Suppress this warning with -DUSE_OPENMP=OFF")
1198
    caffe2_update_option(USE_OPENMP OFF)
1199
  endif()
1200
endif()
1201

1202

1203

1204
# ---[ Android specific ones
1205
if(ANDROID)
1206
  list(APPEND Caffe2_DEPENDENCY_LIBS log)
1207
endif()
1208

1209
# ---[ LLVM
1210
if(USE_LLVM)
1211
  message(STATUS "Looking for LLVM in ${USE_LLVM}")
1212
  find_package(LLVM PATHS ${USE_LLVM} NO_DEFAULT_PATH)
1213

1214
  if(LLVM_FOUND)
1215
    message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
1216
    message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
1217

1218
    include_directories(${LLVM_INCLUDE_DIRS})
1219
    add_definitions(-DTORCH_ENABLE_LLVM)
1220
  endif(LLVM_FOUND)
1221
endif(USE_LLVM)
1222

1223
# ---[ cuDNN
1224
if(USE_CUDNN)
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`")
1227
  endif()
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})
1230
endif()
1231

1232
# ---[ HIP
1233
if(USE_ROCM)
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)
1242
      if(LIBTINFO_LOC)
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}")
1245
      endif()
1246
    endif()
1247
  endif()
1248

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)
1253

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)
1257
    endif()
1258

1259

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)
1275
    endif()
1276
    if(HIPBLASLT_CUSTOM_DATA_TYPE)
1277
      list(APPEND HIP_CXX_FLAGS -DHIPBLASLT_CUSTOM_DATA_TYPE)
1278
    endif()
1279
    if(HIPBLASLT_CUSTOM_COMPUTE_TYPE)
1280
      list(APPEND HIP_CXX_FLAGS -DHIPBLASLT_CUSTOM_COMPUTE_TYPE)
1281
    endif()
1282
    if(HIPBLASLT_HAS_GETINDEXFROMALGO)
1283
      list(APPEND HIP_CXX_FLAGS -DHIPBLASLT_HAS_GETINDEXFROMALGO)
1284
    endif()
1285
    if(HIP_NEW_TYPE_ENUMS)
1286
      list(APPEND HIP_CXX_FLAGS -DHIP_NEW_TYPE_ENUMS)
1287
    endif()
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")
1291

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)
1297

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})
1304
    endforeach()
1305

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})
1310

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})
1315
    endif()
1316

1317
    list(APPEND Caffe2_PUBLIC_HIP_DEPENDENCY_LIBS
1318
      roc::hipblas hip::hipfft hip::hiprand roc::hipsparse roc::hipsolver)
1319

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")
1325
    else()
1326
      message(STATUS "Disabling Kernel Assert for ROCm")
1327
    endif()
1328

1329
    if(USE_FLASH_ATTENTION)
1330
      include(${CMAKE_CURRENT_LIST_DIR}/External/oort.cmake)
1331
    endif()
1332
    if(USE_CUDA)
1333
      caffe2_update_option(USE_MEM_EFF_ATTENTION OFF)
1334
    endif()
1335
  else()
1336
    caffe2_update_option(USE_ROCM OFF)
1337
  endif()
1338
endif()
1339

1340
# ---[ ROCm
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
1343
  # in the if above
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})
1350
endif()
1351

1352
# ---[ NCCL
1353
if(USE_NCCL)
1354
  if(NOT (USE_CUDA OR USE_ROCM))
1355
    message(WARNING
1356
        "Not using CUDA/ROCM, so disabling USE_NCCL. Suppress this warning with "
1357
        "-DUSE_NCCL=OFF.")
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)
1362
  elseif(USE_CUDA)
1363
    include(${CMAKE_CURRENT_LIST_DIR}/External/nccl.cmake)
1364
    list(APPEND Caffe2_CUDA_DEPENDENCY_LIBS __caffe2_nccl)
1365
  elseif(USE_ROCM)
1366
    include(${CMAKE_CURRENT_LIST_DIR}/External/rccl.cmake)
1367
    list(APPEND Caffe2_CUDA_DEPENDENCY_LIBS __caffe2_nccl)
1368
  endif()
1369
endif()
1370

1371
# ---[ UCC
1372
if(USE_UCC)
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)
1376
  else()
1377
    include(${CMAKE_CURRENT_LIST_DIR}/External/ucc.cmake)
1378
  endif()
1379
endif()
1380

1381
# ---[ CUB
1382
if(USE_CUDA)
1383
  find_package(CUB)
1384
  if(CUB_FOUND)
1385
    include_directories(SYSTEM ${CUB_INCLUDE_DIRS})
1386
  else()
1387
    include_directories(SYSTEM ${CMAKE_CURRENT_LIST_DIR}/../third_party/cub)
1388
  endif()
1389
endif()
1390

1391
if(USE_DISTRIBUTED AND USE_TENSORPIPE)
1392
  if(MSVC)
1393
    message(WARNING "Tensorpipe cannot be used on Windows.")
1394
  else()
1395
    if(USE_CUDA)
1396
      set(TP_USE_CUDA ON CACHE BOOL "" FORCE)
1397
      set(TP_ENABLE_CUDA_IPC ON CACHE BOOL "" FORCE)
1398
    endif()
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)
1403

1404
    # Tensorpipe uses cuda_add_library
1405
    torch_update_find_cuda_flags()
1406
    add_subdirectory(${PROJECT_SOURCE_DIR}/third_party/tensorpipe)
1407

1408
    list(APPEND Caffe2_DEPENDENCY_LIBS tensorpipe)
1409
    if(USE_CUDA)
1410
      list(APPEND Caffe2_CUDA_DEPENDENCY_LIBS tensorpipe_cuda)
1411
    elseif(USE_ROCM)
1412
      message(WARNING "TensorPipe doesn't yet support ROCm")
1413
      # Not yet...
1414
      # list(APPEND Caffe2_HIP_DEPENDENCY_LIBS tensorpipe_hip)
1415
    endif()
1416
  endif()
1417
endif()
1418

1419
if(USE_GLOO)
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)
1423
  else()
1424
    # Don't install gloo
1425
    set(GLOO_INSTALL OFF CACHE BOOL "" FORCE)
1426
    set(GLOO_STATIC_OR_SHARED STATIC CACHE STRING "" FORCE)
1427

1428
    # Temporarily override variables to avoid building Gloo tests/benchmarks
1429
    set(__BUILD_TEST ${BUILD_TEST})
1430
    set(__BUILD_BENCHMARK ${BUILD_BENCHMARK})
1431
    set(BUILD_TEST OFF)
1432
    set(BUILD_BENCHMARK OFF)
1433
    if(USE_ROCM)
1434
      set(ENV{GLOO_ROCM_ARCH} "${PYTORCH_ROCM_ARCH}")
1435
    endif()
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}")
1440
      endif()
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)
1445
      endif()
1446
      set(GLOO_USE_CUDA_TOOLKIT ON CACHE BOOL "" FORCE)
1447
      add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../third_party/gloo)
1448
    else()
1449
      add_library(gloo SHARED IMPORTED)
1450
      find_library(GLOO_LIBRARY gloo)
1451
      if(NOT GLOO_LIBRARY)
1452
        message(FATAL_ERROR "Cannot find gloo")
1453
      endif()
1454
      message("Found gloo: ${GLOO_LIBRARY}")
1455
      set_target_properties(gloo PROPERTIES IMPORTED_LOCATION ${GLOO_LIBRARY})
1456
    endif()
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})
1464

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)
1470
    endif()
1471
    # Pick the right dependency depending on USE_CUDA
1472
    list(APPEND Caffe2_DEPENDENCY_LIBS gloo)
1473
    if(USE_CUDA)
1474
      list(APPEND Caffe2_CUDA_DEPENDENCY_LIBS gloo_cuda)
1475
    elseif(USE_ROCM)
1476
      list(APPEND Caffe2_HIP_DEPENDENCY_LIBS gloo_hip)
1477
    endif()
1478
    add_compile_options(-DCAFFE2_USE_GLOO)
1479
  endif()
1480
endif()
1481

1482
# ---[ profiling
1483
if(USE_PROF)
1484
  find_package(htrace)
1485
  if(htrace_FOUND)
1486
    set(USE_PROF_HTRACE ON)
1487
  else()
1488
    message(WARNING "htrace not found. Caffe2 will build without htrace prof")
1489
  endif()
1490
endif()
1491

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)
1500
  else()
1501
    caffe2_update_option(USE_SNPE OFF)
1502
  endif()
1503
endif()
1504

1505
if(USE_METAL)
1506
  if(NOT IOS)
1507
    message(WARNING "Metal is only used in ios builds.")
1508
    caffe2_update_option(USE_METAL OFF)
1509
  endif()
1510
endif()
1511

1512
if(USE_NNAPI AND NOT ANDROID)
1513
  message(WARNING "NNApi is only used in android builds.")
1514
  caffe2_update_option(USE_NNAPI OFF)
1515
endif()
1516

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)
1520
    if(USE_CUDA)
1521
      list(APPEND Caffe2_CUDA_DEPENDENCY_LIBS aten_op_header_gen)
1522
    endif()
1523
    include_directories(${PROJECT_BINARY_DIR}/caffe2/contrib/aten)
1524
  endif()
1525
endif()
1526

1527
if(USE_ZSTD)
1528
  if(USE_SYSTEM_ZSTD)
1529
    find_package(zstd REQUIRED)
1530
    if(TARGET zstd::libzstd_shared)
1531
      set(ZSTD_TARGET zstd::libzstd_shared)
1532
    else()
1533
      set(ZSTD_TARGET zstd::libzstd_static)
1534
    endif()
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})
1538
  else()
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)
1543
  endif()
1544
endif()
1545

1546
# ---[ Onnx
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})
1550
  endif()
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)
1560
  endif()
1561
  if(ONNX_ML)
1562
    add_definitions(-DONNX_ML=1)
1563
  endif()
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)
1569
    if(NOT MSVC)
1570
      set_target_properties(onnx_proto PROPERTIES CXX_STANDARD 17)
1571
    endif()
1572
  endif()
1573
  add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../third_party/foxi EXCLUDE_FROM_ALL)
1574

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.
1580
    if(ANDROID OR IOS)
1581
      caffe2_interface_library(onnx_proto onnx_library)
1582
    else()
1583
      caffe2_interface_library(onnx onnx_library)
1584
    endif()
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)
1589
    endif()
1590
  else()
1591
    add_library(onnx SHARED IMPORTED)
1592
    find_library(ONNX_LIBRARY onnx)
1593
    if(NOT ONNX_LIBRARY)
1594
      message(FATAL_ERROR "Cannot find onnx")
1595
    endif()
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")
1601
    endif()
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)
1605
  endif()
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})
1610
endif()
1611

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}")
1622
endfunction()
1623
if(CAFFE2_CMAKE_BUILDING_WITH_MAIN_REPO)
1624
  if(USE_TENSORRT)
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)
1630
  endif()
1631
endif()
1632

1633
# --[ ATen checks
1634
set(USE_LAPACK 0)
1635

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)
1639
endif()
1640

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)
1645

1646
  # Top-level build config
1647
  ############################################
1648
  # Flags
1649
  # When using MSVC
1650
  # Detect CUDA architecture and get best NVCC flags
1651
  # finding cuda must be first because other things depend on the result
1652
  #
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.
1658

1659
  if(MSVC)
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")
1663
  endif()
1664

1665
  string(APPEND CMAKE_CUDA_FLAGS " -Wno-deprecated-gpu-targets --expt-extended-lambda")
1666

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")
1671
  endif()
1672

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__")
1679

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")
1687
    else()
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})
1691
    endif()
1692
  endif()
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})
1695

1696
  set(CUDA_ATTACH_VS_BUILD_RULE_TO_CUDA_FILE OFF)
1697

1698
  if(USE_MAGMA)
1699
    find_package(MAGMA)
1700
  endif()
1701
  if((USE_CUDA OR USE_ROCM) AND MAGMA_FOUND)
1702
    set(USE_MAGMA 1)
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}")
1707
  elseif(USE_MAGMA)
1708
    message(WARNING
1709
      "Not compiling with MAGMA. Suppress this warning with "
1710
      "-DUSE_MAGMA=OFF.")
1711
    caffe2_update_option(USE_MAGMA OFF)
1712
  else()
1713
    message(STATUS "MAGMA not found. Compiling without MAGMA support")
1714
    caffe2_update_option(USE_MAGMA OFF)
1715
  endif()
1716

1717
  # ARM specific flags
1718
  find_package(ARM)
1719
  if(ASIMD_FOUND)
1720
    message(STATUS "asimd/Neon found with compiler flag : -D__NEON__")
1721
    add_compile_options(-D__NEON__)
1722
  elseif(NEON_FOUND)
1723
    if(APPLE)
1724
      message(STATUS "Neon found with compiler flag : -D__NEON__")
1725
      add_compile_options(-D__NEON__)
1726
    else()
1727
      message(STATUS "Neon found with compiler flag : -mfpu=neon -D__NEON__")
1728
      add_compile_options(-mfpu=neon -D__NEON__)
1729
    endif()
1730
  endif()
1731
  if(CORTEXA8_FOUND)
1732
    message(STATUS "Cortex-A8 Found with compiler flag : -mcpu=cortex-a8")
1733
    add_compile_options(-mcpu=cortex-a8 -fprefetch-loop-arrays)
1734
  endif()
1735
  if(CORTEXA9_FOUND)
1736
    message(STATUS "Cortex-A9 Found with compiler flag : -mcpu=cortex-a9")
1737
    add_compile_options(-mcpu=cortex-a9)
1738
  endif()
1739

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")
1743
  endif()
1744

1745
  find_package(LAPACK)
1746
  if(LAPACK_FOUND)
1747
    set(USE_LAPACK 1)
1748
    list(APPEND Caffe2_PRIVATE_DEPENDENCY_LIBS ${LAPACK_LIBRARIES})
1749
  endif()
1750

1751
  if(NOT USE_CUDA)
1752
    message("disabling CUDA because NOT USE_CUDA is set")
1753
    set(AT_CUDA_ENABLED 0)
1754
  else()
1755
    set(AT_CUDA_ENABLED 1)
1756
  endif()
1757

1758
  if(NOT USE_ROCM)
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)
1762
  else()
1763
    include_directories(BEFORE ${MIOPEN_INCLUDE_DIRS})
1764
    set(AT_ROCM_ENABLED 1)
1765
  endif()
1766

1767
  set(AT_MKLDNN_ENABLED 0)
1768
  set(AT_MKLDNN_ACL_ENABLED 0)
1769
  if(USE_MKLDNN)
1770
    if(NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
1771
      message(WARNING
1772
        "x64 operating system is required for MKLDNN. "
1773
        "Not compiling with MKLDNN. "
1774
        "Turn this warning off by USE_MKLDNN=OFF.")
1775
      set(USE_MKLDNN OFF)
1776
    endif()
1777
    if(USE_MKLDNN_ACL)
1778
      set(AT_MKLDNN_ACL_ENABLED 1)
1779
    endif()
1780
  endif()
1781
  if(USE_MKLDNN)
1782
    include(${CMAKE_CURRENT_LIST_DIR}/public/mkldnn.cmake)
1783
    if(MKLDNN_FOUND)
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)
1789
    else()
1790
      message(WARNING "MKLDNN could not be found.")
1791
      caffe2_update_option(USE_MKLDNN OFF)
1792
    endif()
1793
  else()
1794
    message("disabling MKLDNN because USE_MKLDNN is not set")
1795
  endif()
1796

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)
1801
     if(NEED_LIBRT)
1802
       list(APPEND Caffe2_DEPENDENCY_LIBS rt)
1803
       set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} rt)
1804
     endif(NEED_LIBRT)
1805
  endif(UNIX AND NOT APPLE)
1806

1807
  if(UNIX)
1808
    set(CMAKE_EXTRA_INCLUDE_FILES "sys/mman.h")
1809
    CHECK_FUNCTION_EXISTS(mmap HAVE_MMAP)
1810
    if(HAVE_MMAP)
1811
      add_definitions(-DHAVE_MMAP=1)
1812
    endif(HAVE_MMAP)
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)
1816
    if(HAVE_SHM_OPEN)
1817
      add_definitions(-DHAVE_SHM_OPEN=1)
1818
    endif(HAVE_SHM_OPEN)
1819
    CHECK_FUNCTION_EXISTS(shm_unlink HAVE_SHM_UNLINK)
1820
    if(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)
1827
  endif(UNIX)
1828

1829
  add_definitions(-DUSE_EXTERNAL_MZCRC)
1830
  add_definitions(-DMINIZ_DISABLE_ZIP_READER_CRC32_CHECKS)
1831

1832
  find_package(ZVECTOR) # s390x simd support
1833
endif()
1834

1835
#
1836
# End ATen checks
1837
#
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)
1841

1842
# Disable compiler feature checks for `fmt`.
1843
#
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 "")
1850

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)
1853

1854
# ---[ Kineto
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.")
1859
  set(USE_KINETO OFF)
1860
endif()
1861

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")
1864
endif()
1865

1866
if(USE_KINETO)
1867
  if((NOT USE_CUDA) OR MSVC)
1868
    set(LIBKINETO_NOCUPTI ON CACHE STRING "" FORCE)
1869
  else()
1870
    set(LIBKINETO_NOCUPTI OFF CACHE STRING "")
1871
    message(STATUS "Using Kineto with CUPTI support")
1872
  endif()
1873

1874
  if(NOT USE_ROCM)
1875
    set(LIBKINETO_NOROCTRACER ON CACHE STRING "" FORCE)
1876
  else()
1877
    set(LIBKINETO_NOROCTRACER OFF CACHE STRING "")
1878
    message(STATUS "Using Kineto with Roctracer support")
1879
  endif()
1880

1881
  if(LIBKINETO_NOCUPTI AND LIBKINETO_NOROCTRACER)
1882
    message(STATUS "Using CPU-only version of Kineto")
1883
  endif()
1884

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 "")
1889

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}")
1894

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}")
1899

1900
    if(NOT MSVC)
1901
      if(USE_CUPTI_SO)
1902
        set(CUPTI_LIB_NAME "libcupti.so")
1903
      else()
1904
        set(CUPTI_LIB_NAME "libcupti_static.a")
1905
      endif()
1906
    else()
1907
      set(CUPTI_LIB_NAME "cupti.lib")
1908
    endif()
1909

1910
    find_library(CUPTI_LIBRARY_PATH ${CUPTI_LIB_NAME} PATHS
1911
        ${CUDA_SOURCE_DIR}
1912
        ${CUDA_SOURCE_DIR}/extras/CUPTI/lib64
1913
        ${CUDA_SOURCE_DIR}/lib
1914
        ${CUDA_SOURCE_DIR}/lib64
1915
        NO_DEFAULT_PATH)
1916

1917
    find_path(CUPTI_INCLUDE_DIR cupti.h PATHS
1918
        ${CUDA_SOURCE_DIR}/extras/CUPTI/include
1919
        ${CUDA_INCLUDE_DIRS}
1920
        ${CUDA_SOURCE_DIR}
1921
        ${CUDA_SOURCE_DIR}/include
1922
        NO_DEFAULT_PATH)
1923

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)
1930

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
1937
        if(NOT APPLE)
1938
          set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} "dl" "pthread")
1939
        endif()
1940
        set(CMAKE_REQUIRED_LINK_OPTIONS "-Wl,--whole-archive,${CUPTI_LIBRARY_PATH},--no-whole-archive")
1941
        check_cxx_source_runs("#include <stdexcept>
1942
  int main() {
1943
    try {
1944
      throw std::runtime_error(\"error\");
1945
    } catch (...) {
1946
      return 0;
1947
    }
1948
    return 1;
1949
  }" EXCEPTIONS_WORK)
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")
1953
        endif()
1954
      endif()
1955

1956
    else()
1957
      message(STATUS "Could not find CUPTI library, using CPU-only Kineto build")
1958
      set(LIBKINETO_NOCUPTI ON CACHE STRING "" FORCE)
1959
    endif()
1960
  endif()
1961

1962
  if(NOT LIBKINETO_NOROCTRACER)
1963
    if("$ENV{ROCM_SOURCE_DIR}" STREQUAL "")
1964
      set(ENV{ROCM_SOURCE_DIR} "/opt/rocm")
1965
    endif()
1966
  endif()
1967

1968
  if(NOT TARGET kineto)
1969
    add_subdirectory("${KINETO_SOURCE_DIR}")
1970
    set_property(TARGET kineto PROPERTY POSITION_INDEPENDENT_CODE ON)
1971
  endif()
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")
1976
  endif()
1977
  if(LIBKINETO_NOROCTRACER)
1978
    string(APPEND CMAKE_CXX_FLAGS " -DLIBKINETO_NOROCTRACER")
1979
  endif()
1980
  if(LIBKINETO_NOCUPTI AND LIBKINETO_NOROCTRACER)
1981
    message(STATUS "Configured Kineto (CPU)")
1982
  else()
1983
    message(STATUS "Configured Kineto")
1984
  endif()
1985
endif()
1986

1987
# Include google/FlatBuffers
1988
include(${CMAKE_CURRENT_LIST_DIR}/FlatBuffers.cmake)
1989

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.