forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
466 lines (389 loc) · 15.7 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# This file used to build libtorch.so.
# Now it only builds the Torch python bindings.
if(NOT CAFFE2_CMAKE_BUILDING_WITH_MAIN_REPO)
cmake_minimum_required(VERSION 3.10 FATAL_ERROR)
project(torch CXX C)
find_package(torch REQUIRED)
option(USE_CUDA "Use CUDA" ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
endif()
if(NOT BUILD_PYTHON)
return()
endif()
if(USE_TBB)
include_directories(${TBB_INCLUDE_DIR})
endif()
set(TORCH_SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
set(TORCH_ROOT "${TORCH_SRC_DIR}/..")
if(NOT TORCH_INSTALL_LIB_DIR)
set(TORCH_INSTALL_LIB_DIR lib)
endif()
if(MSVC)
set(LIBSHM_SUBDIR libshm_windows)
else()
set(LIBSHM_SUBDIR libshm)
endif()
set(LIBSHM_SRCDIR ${TORCH_SRC_DIR}/lib/${LIBSHM_SUBDIR})
add_subdirectory(${LIBSHM_SRCDIR})
# Generate files
set(TOOLS_PATH "${TORCH_ROOT}/tools")
set(TORCH_PYTHON_SRCS
${GENERATED_THNN_CXX}
${GENERATED_CXX_PYTHON}
)
append_filelist("libtorch_python_core_sources" TORCH_PYTHON_SRCS)
list(APPEND TORCH_PYTHON_SRCS
${TORCH_SRC_DIR}/csrc/init_flatbuffer_module.cpp)
# NB: This has to match the condition under which the JIT test directory
# is included (at the time of writing that's in caffe2/CMakeLists.txt).
if(BUILD_TEST)
add_definitions(-DBUILDING_TESTS)
list(APPEND TORCH_PYTHON_SRCS
${TORCH_ROOT}/test/cpp/jit/torch_python_test.cpp
)
endif()
set(TORCH_PYTHON_INCLUDE_DIRECTORIES
${PYTHON_INCLUDE_DIR}
${TORCH_ROOT}
${TORCH_ROOT}/aten/src
${TORCH_ROOT}/aten/src/TH
${CMAKE_BINARY_DIR}
${CMAKE_BINARY_DIR}/aten/src
${CMAKE_BINARY_DIR}/caffe2/aten/src
${CMAKE_BINARY_DIR}/third_party
${CMAKE_BINARY_DIR}/third_party/onnx
${TORCH_ROOT}/third_party/valgrind-headers
${TORCH_ROOT}/third_party/gloo
${TORCH_ROOT}/third_party/onnx
${TORCH_ROOT}/third_party/flatbuffers/include
${TORCH_SRC_DIR}/csrc
${TORCH_SRC_DIR}/csrc/api/include
${TORCH_SRC_DIR}/lib
)
list(APPEND TORCH_PYTHON_INCLUDE_DIRECTORIES ${LIBSHM_SRCDIR})
set(TORCH_PYTHON_LINK_LIBRARIES
python::python
pybind::pybind11
shm
fmt::fmt-header-only
ATEN_CPU_FILES_GEN_LIB)
set(TORCH_PYTHON_COMPILE_DEFINITIONS)
set(TORCH_PYTHON_PUBLIC_COMPILE_DEFINITIONS)
set(TORCH_PYTHON_COMPILE_OPTIONS)
set(TORCH_PYTHON_LINK_FLAGS "")
if(MSVC)
string(APPEND TORCH_PYTHON_LINK_FLAGS " /NODEFAULTLIB:LIBCMT.LIB")
list(APPEND TORCH_PYTHON_LINK_LIBRARIES ${PYTHON_LIBRARIES} onnx_library)
if(NOT CMAKE_BUILD_TYPE MATCHES "Release")
string(APPEND TORCH_PYTHON_LINK_FLAGS " /DEBUG:FULL")
endif()
elseif(APPLE)
string(APPEND TORCH_PYTHON_LINK_FLAGS " -undefined dynamic_lookup")
else()
list(APPEND TORCH_PYTHON_COMPILE_OPTIONS
-fno-strict-aliasing
-Wno-write-strings
-Wno-strict-aliasing)
endif()
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
list(APPEND TORCH_PYTHON_COMPILE_OPTIONS
-Wno-writable-strings)
endif()
if(USE_CUDA)
include(${TORCH_ROOT}/cmake/public/cuda.cmake)
append_filelist("libtorch_python_cuda_core_sources" TORCH_PYTHON_SRCS)
list(APPEND TORCH_PYTHON_SRCS ${GENERATED_THNN_CXX_CUDA})
list(APPEND TORCH_PYTHON_COMPILE_DEFINITIONS USE_CUDA)
if(USE_CUDNN)
list(APPEND TORCH_PYTHON_COMPILE_DEFINITIONS USE_CUDNN)
endif()
list(APPEND TORCH_PYTHON_LINK_LIBRARIES torch::nvtoolsext)
endif()
if(USE_ROCM)
append_filelist("libtorch_python_cuda_core_sources" TORCH_PYTHON_SRCS)
list(APPEND TORCH_PYTHON_SRCS ${GENERATED_THNN_CXX_CUDA})
list(APPEND TORCH_PYTHON_COMPILE_DEFINITIONS
USE_ROCM
__HIP_PLATFORM_HCC__
)
list(APPEND TORCH_PYTHON_LINK_LIBRARIES ${ROCM_ROCTX_LIB})
list(APPEND TORCH_PYTHON_INCLUDE_DIRECTORIES ${roctracer_INCLUDE_DIRS})
endif()
if(USE_CUDNN OR USE_ROCM)
list(APPEND TORCH_PYTHON_SRCS
${TORCH_SRC_DIR}/csrc/cuda/shared/cudnn.cpp
)
if(USE_STATIC_CUDNN)
set_source_files_properties(
${TORCH_SRC_DIR}/csrc/cuda/shared/cudnn.cpp
PROPERTIES COMPILE_DEFINITIONS "USE_STATIC_CUDNN"
)
endif()
endif()
if(USE_MLCOMPUTE)
list(APPEND TORCH_PYTHON_SRCS ${MLC_PYTHON_SRCS})
endif()
if(USE_VALGRIND AND NOT WIN32)
list(APPEND TORCH_PYTHON_COMPILE_DEFINITIONS USE_VALGRIND)
endif()
# In the most recent CMake versions, a new 'TRANSFORM' subcommand of 'list' allows much of the boilerplate of defining the lists
# of type stub files to be omitted.
# For compatibility with older CMake versions, we omit it for now, but leave it as a comment in case compatibility with the older
# CMake versions is eventually dropped.
# set(Modules
# __init__
# activation
# adaptive
# batchnorm
# container
# conv
# distance
# dropout
# fold
# instancenorm
# linear
# loss
# module
# normalization
# padding
# pixelshuffle
# pooling
# rnn
# sparse
# upsampling
# )
# list(TRANSFORM Modules PREPEND "${TORCH_SRC_DIR}/nn/modules/")
add_custom_target(torch_python_stubs DEPENDS
"${TORCH_SRC_DIR}/_C/__init__.pyi"
"${TORCH_SRC_DIR}/_C/_VariableFunctions.pyi"
"${TORCH_SRC_DIR}/nn/functional.pyi"
"${TORCH_SRC_DIR}/utils/data/datapipes/datapipe.pyi"
)
add_custom_command(
OUTPUT
"${TORCH_SRC_DIR}/_C/__init__.pyi"
"${TORCH_SRC_DIR}/_C/_VariableFunctions.pyi"
"${TORCH_SRC_DIR}/nn/functional.pyi"
COMMAND
"${PYTHON_EXECUTABLE}" -mtools.pyi.gen_pyi
--native-functions-path "aten/src/ATen/native/native_functions.yaml"
--deprecated-functions-path "tools/autograd/deprecated.yaml"
DEPENDS
"${TORCH_SRC_DIR}/_C/__init__.pyi.in"
"${TORCH_SRC_DIR}/_C/_VariableFunctions.pyi.in"
"${TORCH_SRC_DIR}/nn/functional.pyi.in"
"${TOOLS_PATH}/pyi/gen_pyi.py"
"${TORCH_ROOT}/aten/src/ATen/native/native_functions.yaml"
"${TORCH_ROOT}/tools/autograd/deprecated.yaml"
WORKING_DIRECTORY
"${TORCH_ROOT}"
)
file(GLOB_RECURSE datapipe_files "${TORCH_SRC_DIR}/utils/data/datapipes/*.py")
add_custom_command(
OUTPUT
"${TORCH_SRC_DIR}/utils/data/datapipes/datapipe.pyi"
COMMAND
"${PYTHON_EXECUTABLE}" ${TORCH_SRC_DIR}/utils/data/datapipes/gen_pyi.py
DEPENDS
"${TORCH_SRC_DIR}/utils/data/datapipes/datapipe.pyi.in"
${datapipe_files}
WORKING_DIRECTORY
"${TORCH_ROOT}"
)
if(USE_DISTRIBUTED)
if(WIN32)
append_filelist("libtorch_python_distributed_core_sources" TORCH_PYTHON_SRCS)
else()
append_filelist("libtorch_python_distributed_sources" TORCH_PYTHON_SRCS)
endif()
# Disable certain warnings for GCC-9.X
if(CMAKE_COMPILER_IS_GNUCXX AND (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 9.0.0))
set_source_files_properties(${TORCH_SRC_DIR}/csrc/distributed/autograd/init.cpp PROPERTIES COMPILE_FLAGS "-Wno-cast-function-type")
set_source_files_properties(${TORCH_SRC_DIR}/csrc/distributed/rpc/testing/init.cpp PROPERTIES COMPILE_FLAGS "-Wno-cast-function-type")
set_source_files_properties(${TORCH_SRC_DIR}/csrc/distributed/c10d/init.cpp PROPERTIES COMPILE_FLAGS "-Wno-cast-function-type")
endif()
# NCCL is a private dependency of libtorch, but libtorch_python includes
# some private headers of libtorch, which in turn include NCCL. As a hacky
# alternative to making NCCL a public dependency of libtorch, we make it
# a private dependency of libtorch_python as well.
if(USE_NCCL)
list(APPEND TORCH_PYTHON_LINK_LIBRARIES __caffe2_nccl)
endif()
# Same for MPI.
if(USE_MPI)
list(APPEND TORCH_PYTHON_LINK_LIBRARIES ${MPI_CXX_LIBRARIES})
endif()
list(APPEND TORCH_PYTHON_COMPILE_DEFINITIONS USE_C10D)
endif()
if(USE_NCCL AND NOT WIN32)
list(APPEND TORCH_PYTHON_SRCS
${TORCH_SRC_DIR}/csrc/cuda/python_nccl.cpp)
list(APPEND TORCH_PYTHON_COMPILE_DEFINITIONS USE_NCCL)
endif()
# WARNING- any TORCH_PYTHON_COMPILE_DEFINITIONS above this line
# affect both torch_python and DEPLOY interpreter.
if(USE_DEPLOY)
add_library(torch_python_obj OBJECT ${TORCH_PYTHON_SRCS})
if(NOT MSVC)
target_compile_options(torch_python_obj PRIVATE -Wno-unused-variable)
endif()
if(USE_DISTRIBUTED)
# Set c10d-related compile definitions. For a "normal" build of
# libtorch_python, these are set on libtorch as PUBLIC so they are
# automatically propagated when libtorch_python links against libtorch. But
# since in the deploy build we are intentionally *not* linking against
# libtorch, we need to set them manually here.
list(APPEND TORCH_PYTHON_COMPILE_DEFINITIONS USE_DISTRIBUTED)
if(USE_GLOO AND USE_C10D_GLOO)
list(APPEND TORCH_PYTHON_COMPILE_DEFINITIONS USE_C10D_GLOO)
endif()
if(USE_NCCL AND USE_C10D_NCCL)
list(APPEND TORCH_PYTHON_COMPILE_DEFINITIONS USE_C10D_NCCL)
# Put nccl headers on the include path. We are specifically only setting
# include dirs here instead of linking against __caffe2_nccl wholesale
# to ensure we aren't accidentally replicating the nccl lib.
target_include_directories(torch_python_obj PRIVATE $<TARGET_PROPERTY:__caffe2_nccl,INTERFACE_INCLUDE_DIRECTORIES>)
endif()
if(USE_MPI AND USE_C10D_MPI)
list(APPEND TORCH_PYTHON_COMPILE_DEFINITIONS USE_C10D_MPI)
endif()
# Pass USE_RPC in order to reduce use of
# #if defined(USE_DISTRIBUTED) && !defined(_WIN32)
# need to be removed when RPC is supported
if(NOT WIN32)
target_compile_definitions(torch_cpu PUBLIC USE_RPC)
endif()
if(USE_TENSORPIPE)
list(APPEND TORCH_PYTHON_COMPILE_DEFINITIONS USE_TENSORPIPE)
endif()
# Set c10d-related include directories as well.
target_include_directories(torch_python_obj PRIVATE $<BUILD_INTERFACE:${TORCH_SRC_DIR}/csrc/distributed>)
endif()
target_compile_definitions(torch_python_obj PRIVATE "-DTHP_BUILD_MAIN_LIB -DUSE_DEPLOY")
target_compile_definitions(torch_python_obj PRIVATE ${TORCH_PYTHON_COMPILE_DEFINITIONS})
target_compile_definitions(torch_python_obj PUBLIC ${TORCH_PYTHON_PUBLIC_COMPILE_DEFINITIONS})
target_compile_options(torch_python_obj PRIVATE ${TORCH_PYTHON_COMPILE_OPTIONS})
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
target_compile_options(torch_python_obj PRIVATE -fno-gnu-unique)
endif()
target_include_directories(torch_python_obj PUBLIC ${TORCH_PYTHON_INCLUDE_DIRECTORIES})
target_include_directories(torch_python_obj PRIVATE ../third_party/fmt/include)
# need to specify the dependency so the generated headers exist,
# missing dependency since torch_python_obj doesn't link onnx, the interpreter lib does.
add_dependencies(torch_python_obj onnx)
target_include_directories(torch_python_obj SYSTEM PRIVATE
${PYTHON_INCLUDE_DIRS}
${pybind11_INCLUDE_DIRS})
if(HAVE_SOVERSION)
set_target_properties(torch_python_obj PROPERTIES
VERSION ${TORCH_VERSION} SOVERSION ${TORCH_SOVERSION})
endif()
add_dependencies(torch_python_obj torch_python_stubs)
# Required workaround for generated sources
# See https://samthursfield.wordpress.com/2015/11/21/cmake-dependencies-between-targets-and-files-and-custom-commands/#custom-commands-in-different-directories
add_dependencies(torch_python_obj generate-torch-sources)
set_source_files_properties(
${GENERATED_THNN_SOURCES}
${GENERATED_CXX_PYTHON}
PROPERTIES GENERATED TRUE
)
add_dependencies(torch_python_obj gen_torch_version)
endif()
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
# Workaround for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80947 in EmbeddingBag.cpp
set_source_files_properties(${TORCH_SRC_DIR}/csrc/utils/throughput_benchmark.cpp PROPERTIES COMPILE_FLAGS -Wno-attributes)
endif()
# coreml
if(USE_COREML_DELEGATE)
list(APPEND TORCH_PYTHON_SRCS ${TORCH_SRC_DIR}/csrc/jit/backends/coreml/cpp/backend.cpp)
list(APPEND TORCH_PYTHON_SRCS ${TORCH_SRC_DIR}/csrc/jit/backends/coreml/cpp/preprocess.cpp)
endif()
add_library(torch_python SHARED ${TORCH_PYTHON_SRCS})
# Avoid numpy for the DEPLOY build
if(USE_NUMPY)
target_link_libraries(torch_python PRIVATE numpy::numpy)
target_compile_definitions(torch_python PRIVATE USE_NUMPY)
endif()
if(HAVE_SOVERSION)
set_target_properties(torch_python PROPERTIES
VERSION ${TORCH_VERSION} SOVERSION ${TORCH_SOVERSION})
endif()
add_dependencies(torch_python torch_python_stubs)
add_dependencies(torch_python flatbuffers)
if(USE_PRECOMPILED_HEADERS)
target_precompile_headers(torch_python PRIVATE
"$<$<COMPILE_LANGUAGE:CXX>:ATen/ATen.h>")
endif()
if(NOT MSVC)
target_compile_options(torch_python PRIVATE -Wno-unused-variable)
endif()
# Required workaround for generated sources
# See https://samthursfield.wordpress.com/2015/11/21/cmake-dependencies-between-targets-and-files-and-custom-commands/#custom-commands-in-different-directories
add_dependencies(torch_python generate-torch-sources)
set_source_files_properties(
${GENERATED_THNN_SOURCES}
${GENERATED_CXX_PYTHON}
PROPERTIES GENERATED TRUE
)
# Disable certain warnings for GCC-9.X
if(CMAKE_COMPILER_IS_GNUCXX AND (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 9.0.0))
set_source_files_properties(${TORCH_SRC_DIR}/csrc/Module.cpp PROPERTIES COMPILE_FLAGS "-Wno-cast-function-type")
set_source_files_properties(
${TORCH_SRC_DIR}/csrc/init_flatbuffer_module.cpp
PROPERTIES COMPILE_FLAGS "-Wno-cast-function-type")
set_source_files_properties(${TORCH_SRC_DIR}/csrc/autograd/python_variable.cpp PROPERTIES COMPILE_FLAGS "-Wno-cast-function-type")
endif()
# Preserve CUDA_GENCODE flags
if(USE_CUDA)
torch_cuda_get_nvcc_gencode_flag(_ARCH_FLAGS)
set_source_files_properties(${TORCH_SRC_DIR}/csrc/cuda/Module.cpp PROPERTIES COMPILE_FLAGS "-DCUDA_ARCH_FLAGS=\"${_ARCH_FLAGS_readable}\"")
endif()
# Preserve HIP arch flags
if(USE_ROCM)
string(REPLACE ";" " " PYTORCH_ROCM_ARCH_readable "${PYTORCH_ROCM_ARCH}")
set_source_files_properties(${TORCH_SRC_DIR}/csrc/cuda/Module.cpp PROPERTIES COMPILE_FLAGS "-DCUDA_ARCH_FLAGS=\"${PYTORCH_ROCM_ARCH_readable}\"")
endif()
target_compile_definitions(torch_python PRIVATE "-DTHP_BUILD_MAIN_LIB")
target_link_libraries(torch_python PRIVATE torch_library ${TORCH_PYTHON_LINK_LIBRARIES})
target_compile_definitions(torch_python PRIVATE ${TORCH_PYTHON_COMPILE_DEFINITIONS})
target_compile_definitions(torch_python PUBLIC ${TORCH_PYTHON_PUBLIC_COMPILE_DEFINITIONS})
target_compile_options(torch_python PRIVATE ${TORCH_PYTHON_COMPILE_OPTIONS})
target_include_directories(torch_python PUBLIC ${TORCH_PYTHON_INCLUDE_DIRECTORIES})
if(NOT TORCH_PYTHON_LINK_FLAGS STREQUAL "")
set_target_properties(torch_python PROPERTIES LINK_FLAGS ${TORCH_PYTHON_LINK_FLAGS})
endif()
install(TARGETS torch_python DESTINATION "${TORCH_INSTALL_LIB_DIR}")
# Generate torch/version.py from the appropriate CMake cache variables.
if(${CMAKE_BUILD_TYPE} STREQUAL "Debug")
set(TORCH_VERSION_DEBUG 1)
else()
set(TORCH_VERSION_DEBUG 0)
endif()
add_custom_command(
OUTPUT ${TORCH_SRC_DIR}/version.py
COMMAND
"${PYTHON_EXECUTABLE}" -c \"from pathlib import Path\; Path('${TOOLS_PATH}/generate_torch_version.py').touch()\"
COMMAND
"${PYTHON_EXECUTABLE}" ${TOOLS_PATH}/generate_torch_version.py
--is_debug=${TORCH_VERSION_DEBUG}
--cuda_version=${CUDA_VERSION}
--hip_version=${HIP_VERSION}
DEPENDS ${TOOLS_PATH}/generate_torch_version.py
WORKING_DIRECTORY ${TORCH_ROOT}
)
add_custom_target(
gen_torch_version ALL
DEPENDS ${TORCH_SRC_DIR}/version.py
)
add_dependencies(torch_python gen_torch_version)
# Skip building this library under MacOS, since it is currently failing to build on Mac
# Github issue #61930
if(NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
# Add Android Nnapi delegate library
add_library(nnapi_backend SHARED
${TORCH_SRC_DIR}/csrc/jit/backends/nnapi/nnapi_backend_lib.cpp
${TORCH_SRC_DIR}/csrc/jit/backends/nnapi/nnapi_backend_preprocess.cpp
)
# Pybind11 requires explicit linking of the torch_python library
target_link_libraries(nnapi_backend PRIVATE torch torch_python pybind::pybind11)
endif()