forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
373 lines (329 loc) · 12.6 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
# 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.5 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_ROOT_DIR}/include)
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}
${TORCH_SRC_DIR}/csrc/CudaIPCTypes.cpp
${TORCH_SRC_DIR}/csrc/DataLoader.cpp
${TORCH_SRC_DIR}/csrc/Device.cpp
${TORCH_SRC_DIR}/csrc/Dtype.cpp
${TORCH_SRC_DIR}/csrc/DynamicTypes.cpp
${TORCH_SRC_DIR}/csrc/Exceptions.cpp
${TORCH_SRC_DIR}/csrc/TypeInfo.cpp
${TORCH_SRC_DIR}/csrc/Generator.cpp
${TORCH_SRC_DIR}/csrc/Layout.cpp
${TORCH_SRC_DIR}/csrc/MemoryFormat.cpp
${TORCH_SRC_DIR}/csrc/python_dimname.cpp
${TORCH_SRC_DIR}/csrc/QScheme.cpp
${TORCH_SRC_DIR}/csrc/Module.cpp
${TORCH_SRC_DIR}/csrc/PtrWrapper.cpp
${TORCH_SRC_DIR}/csrc/Size.cpp
${TORCH_SRC_DIR}/csrc/Storage.cpp
${TORCH_SRC_DIR}/csrc/api/src/python/init.cpp
${TORCH_SRC_DIR}/csrc/autograd/functions/init.cpp
${TORCH_SRC_DIR}/csrc/autograd/init.cpp
${TORCH_SRC_DIR}/csrc/autograd/python_anomaly_mode.cpp
${TORCH_SRC_DIR}/csrc/autograd/python_cpp_function.cpp
${TORCH_SRC_DIR}/csrc/autograd/python_engine.cpp
${TORCH_SRC_DIR}/csrc/autograd/python_function.cpp
${TORCH_SRC_DIR}/csrc/autograd/python_hook.cpp
${TORCH_SRC_DIR}/csrc/autograd/python_legacy_variable.cpp
${TORCH_SRC_DIR}/csrc/autograd/python_variable.cpp
${TORCH_SRC_DIR}/csrc/autograd/python_variable_indexing.cpp
${TORCH_SRC_DIR}/csrc/byte_order.cpp
${TORCH_SRC_DIR}/csrc/jit/init.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/onnx.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/onnx/fixup_onnx_loop.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/onnx/prepare_division_for_onnx.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/onnx/peephole.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/onnx/cast_all_constant_to_floating.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/onnx/constant_fold.cpp
${TORCH_SRC_DIR}/csrc/jit/python_arg_flatten.cpp
${TORCH_SRC_DIR}/csrc/jit/python_interpreter.cpp
${TORCH_SRC_DIR}/csrc/jit/python_ir.cpp
${TORCH_SRC_DIR}/csrc/jit/python_tracer.cpp
${TORCH_SRC_DIR}/csrc/jit/script/init.cpp
${TORCH_SRC_DIR}/csrc/jit/script/python_sugared_value.cpp
${TORCH_SRC_DIR}/csrc/jit/script/python_tree_views.cpp
${TORCH_SRC_DIR}/csrc/multiprocessing/init.cpp
${TORCH_SRC_DIR}/csrc/onnx/init.cpp
${TORCH_SRC_DIR}/csrc/utils/init.cpp
${TORCH_SRC_DIR}/csrc/utils/throughput_benchmark.cpp
${TORCH_SRC_DIR}/csrc/serialization.cpp
${TORCH_SRC_DIR}/csrc/tensor/python_tensor.cpp
${TORCH_SRC_DIR}/csrc/utils.cpp
${TORCH_SRC_DIR}/csrc/utils/cuda_lazy_init.cpp
${TORCH_SRC_DIR}/csrc/utils/invalid_arguments.cpp
${TORCH_SRC_DIR}/csrc/utils/object_ptr.cpp
${TORCH_SRC_DIR}/csrc/utils/python_arg_parser.cpp
${TORCH_SRC_DIR}/csrc/utils/structseq.cpp
${TORCH_SRC_DIR}/csrc/utils/tensor_apply.cpp
${TORCH_SRC_DIR}/csrc/utils/tensor_dtypes.cpp
${TORCH_SRC_DIR}/csrc/utils/tensor_layouts.cpp
${TORCH_SRC_DIR}/csrc/utils/tensor_list.cpp
${TORCH_SRC_DIR}/csrc/utils/tensor_memoryformats.cpp
${TORCH_SRC_DIR}/csrc/utils/tensor_qschemes.cpp
${TORCH_SRC_DIR}/csrc/utils/tensor_new.cpp
${TORCH_SRC_DIR}/csrc/utils/tensor_numpy.cpp
${TORCH_SRC_DIR}/csrc/utils/tensor_types.cpp
${TORCH_SRC_DIR}/csrc/utils/tuple_parser.cpp
)
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/build/nccl/include
${TORCH_ROOT}/third_party/gloo
${TORCH_ROOT}/third_party/onnx
${TORCH_ROOT}/third_party/pybind11/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
torch_library
shm)
set(TORCH_PYTHON_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})
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 (USE_CUDA)
list(APPEND TORCH_PYTHON_SRCS
${TORCH_SRC_DIR}/csrc/cuda/Module.cpp
${TORCH_SRC_DIR}/csrc/cuda/Storage.cpp
${TORCH_SRC_DIR}/csrc/cuda/Stream.cpp
${TORCH_SRC_DIR}/csrc/cuda/Event.cpp
${TORCH_SRC_DIR}/csrc/cuda/utils.cpp
${TORCH_SRC_DIR}/csrc/cuda/python_comm.cpp
${TORCH_SRC_DIR}/csrc/cuda/serialization.cpp
${GENERATED_THNN_CXX_CUDA}
)
list(APPEND TORCH_PYTHON_COMPILE_DEFINITIONS USE_CUDA)
if(MSVC)
list(APPEND TORCH_PYTHON_LINK_LIBRARIES ${NVTOOLEXT_HOME}/lib/x64/nvToolsExt64_1.lib)
list(APPEND TORCH_PYTHON_INCLUDE_DIRECTORIES "${NVTOOLEXT_HOME}/include")
elseif(APPLE)
list(APPEND TORCH_PYTHON_LINK_LIBRARIES ${CUDA_TOOLKIT_ROOT_DIR}/lib/libnvToolsExt.dylib)
else()
find_library(LIBNVTOOLSEXT libnvToolsExt.so PATHS ${CUDA_TOOLKIT_ROOT_DIR}/lib64/)
list(APPEND TORCH_PYTHON_LINK_LIBRARIES ${LIBNVTOOLSEXT})
endif()
endif()
if (USE_CUDNN)
list(APPEND TORCH_PYTHON_COMPILE_DEFINITIONS USE_CUDNN)
# NOTE: these are at the front, in case there's another cuDNN in
# CUDA path.
# Basically, this is the case where $CUDA_HOME/lib64 has an old or
# incompatible libcudnn.so, which we can inadvertently link to if
# we're not careful.
list(INSERT 0 TORCH_PYTHON_LINK_LIBRARIES ${CUDNN_LIBRARY})
list(INSERT 0 TORCH_PYTHON_INCLUDE_DIRECTORIES ${CUDNN_INCLUDE_DIR})
endif()
if (USE_NUMPY)
list(APPEND TORCH_PYTHON_COMPILE_DEFINITIONS USE_NUMPY)
endif()
if (USE_ROCM)
list(APPEND TORCH_PYTHON_SRCS
${TORCH_SRC_DIR}/csrc/cuda/Module.cpp
${TORCH_SRC_DIR}/csrc/cuda/Storage.cpp
${TORCH_SRC_DIR}/csrc/cuda/Stream.cpp
${TORCH_SRC_DIR}/csrc/cuda/Event.cpp
${TORCH_SRC_DIR}/csrc/cuda/utils.cpp
${TORCH_SRC_DIR}/csrc/cuda/python_comm.cpp
${TORCH_SRC_DIR}/csrc/cuda/serialization.cpp
${GENERATED_THNN_CXX_CUDA}
)
list(APPEND TORCH_PYTHON_COMPILE_DEFINITIONS
USE_ROCM
__HIP_PLATFORM_HCC__
)
endif()
if (USE_DISTRIBUTED)
list(APPEND TORCH_PYTHON_COMPILE_DEFINITIONS USE_DISTRIBUTED)
if (NOT MSVC AND NOT APPLE)
list(APPEND TORCH_PYTHON_SRCS
${TORCH_SRC_DIR}/csrc/distributed/c10d/comm.cpp
${TORCH_SRC_DIR}/csrc/distributed/c10d/init.cpp
${TORCH_SRC_DIR}/csrc/distributed/c10d/reducer.cpp
)
list(APPEND TORCH_PYTHON_LINK_LIBRARIES c10d)
list(APPEND TORCH_PYTHON_COMPILE_DEFINITIONS USE_C10D)
if (USE_CUDA OR USE_ROCM)
list(APPEND TORCH_PYTHON_SRCS ${TORCH_SRC_DIR}/csrc/distributed/c10d/ddp.cpp)
endif()
endif()
endif()
if (USE_NCCL)
list(APPEND TORCH_PYTHON_SRCS
${TORCH_SRC_DIR}/csrc/cuda/nccl.cpp
${TORCH_SRC_DIR}/csrc/cuda/python_nccl.cpp)
list(APPEND TORCH_PYTHON_COMPILE_DEFINITIONS USE_NCCL)
list(APPEND TORCH_PYTHON_LINK_LIBRARIES __caffe2_nccl)
if (USE_SYSTEM_NCCL)
endif()
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 comptability with older CMake versions, we omit it for now, but leave it as a comment in case comptability 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/")
# set(ModuleStubIn ${Modules})
# set(ModuleStubOut ${Modules})
# list(TRANSFORM ModuleStubIn APPEND ".pyi.in")
# list(TRANSFORM ModuleStubOut APPEND ".pyi")
set(ModulesStubIn
${TORCH_SRC_DIR}/nn/modules/__init__.pyi.in
${TORCH_SRC_DIR}/nn/modules/activation.pyi.in
${TORCH_SRC_DIR}/nn/modules/adaptive.pyi.in
${TORCH_SRC_DIR}/nn/modules/batchnorm.pyi.in
${TORCH_SRC_DIR}/nn/modules/container.pyi.in
${TORCH_SRC_DIR}/nn/modules/conv.pyi.in
${TORCH_SRC_DIR}/nn/modules/distance.pyi.in
${TORCH_SRC_DIR}/nn/modules/dropout.pyi.in
${TORCH_SRC_DIR}/nn/modules/fold.pyi.in
${TORCH_SRC_DIR}/nn/modules/instancenorm.pyi.in
${TORCH_SRC_DIR}/nn/modules/linear.pyi.in
${TORCH_SRC_DIR}/nn/modules/loss.pyi.in
${TORCH_SRC_DIR}/nn/modules/module.pyi.in
${TORCH_SRC_DIR}/nn/modules/normalization.pyi.in
${TORCH_SRC_DIR}/nn/modules/padding.pyi.in
${TORCH_SRC_DIR}/nn/modules/pixelshuffle.pyi.in
${TORCH_SRC_DIR}/nn/modules/pooling.pyi.in
${TORCH_SRC_DIR}/nn/modules/rnn.pyi.in
${TORCH_SRC_DIR}/nn/modules/sparse.pyi.in
${TORCH_SRC_DIR}/nn/modules/upsampling.pyi.in
)
set(ModulesStubOut
${TORCH_SRC_DIR}/nn/modules/__init__.pyi
${TORCH_SRC_DIR}/nn/modules/activation.pyi
${TORCH_SRC_DIR}/nn/modules/adaptive.pyi
${TORCH_SRC_DIR}/nn/modules/batchnorm.pyi
${TORCH_SRC_DIR}/nn/modules/container.pyi
${TORCH_SRC_DIR}/nn/modules/conv.pyi
${TORCH_SRC_DIR}/nn/modules/distance.pyi
${TORCH_SRC_DIR}/nn/modules/dropout.pyi
${TORCH_SRC_DIR}/nn/modules/fold.pyi
${TORCH_SRC_DIR}/nn/modules/instancenorm.pyi
${TORCH_SRC_DIR}/nn/modules/linear.pyi
${TORCH_SRC_DIR}/nn/modules/loss.pyi
${TORCH_SRC_DIR}/nn/modules/module.pyi
${TORCH_SRC_DIR}/nn/modules/normalization.pyi
${TORCH_SRC_DIR}/nn/modules/padding.pyi
${TORCH_SRC_DIR}/nn/modules/pixelshuffle.pyi
${TORCH_SRC_DIR}/nn/modules/pooling.pyi
${TORCH_SRC_DIR}/nn/modules/rnn.pyi
${TORCH_SRC_DIR}/nn/modules/sparse.pyi
${TORCH_SRC_DIR}/nn/modules/upsampling.pyi
)
add_custom_target(torch_python_stubs DEPENDS
"${TORCH_SRC_DIR}/__init__.pyi"
"${TORCH_SRC_DIR}/nn/functional.pyi"
${ModuleStubOut}
)
# For Declarations.yaml dependency
add_dependencies(torch_python_stubs ATEN_CPU_FILES_GEN_TARGET)
add_custom_command(
OUTPUT
"${TORCH_SRC_DIR}/__init__.pyi"
"${TORCH_SRC_DIR}/nn/functional.pyi"
${ModuleStubOut}
COMMAND
"${PYTHON_EXECUTABLE}" -mtools.pyi.gen_pyi
--declarations-path "${CMAKE_BINARY_DIR}/aten/src/ATen/Declarations.yaml"
DEPENDS
"${CMAKE_BINARY_DIR}/aten/src/ATen/Declarations.yaml"
"${TORCH_SRC_DIR}/__init__.pyi.in"
"${TORCH_SRC_DIR}/nn/functional.pyi.in"
${ModuleStubIn}
"${TOOLS_PATH}/pyi/gen_pyi.py"
WORKING_DIRECTORY
"${TORCH_ROOT}"
)
add_library(torch_python SHARED ${TORCH_PYTHON_SRCS})
add_dependencies(torch_python 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 generate-torch-sources)
set_source_files_properties(
${GENERATED_THNN_SOURCES}
${GENERATED_CXX_PYTHON}
PROPERTIES GENERATED TRUE
)
target_compile_definitions(torch_python PUBLIC _THP_CORE)
target_link_libraries(torch_python ${TORCH_PYTHON_LINK_LIBRARIES})
target_compile_definitions(torch_python PRIVATE ${TORCH_PYTHON_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}")