forked from facebookresearch/fairseq2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
333 lines (267 loc) · 7.85 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
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
cmake_minimum_required(VERSION 3.21.0)
project(fairseq2n VERSION 0.3.0 LANGUAGES C CXX)
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY VALUE RelWithDebInfo)
endif()
if(DEFINED ENV{CONDA_PREFIX} AND NOT DEFINED ENV{CONDA_BUILD_SYSROOT})
message(FATAL_ERROR
"It looks like you are in a Conda environment, but the `compilers` package is not installed. Please run `conda install -c conda-forge compilers=1.2.0` first."
)
endif()
include(CMakeDependentOption)
include(CMakePackageConfigHelpers)
include(CheckLanguage)
include(GNUInstallDirs)
if(PROJECT_IS_TOP_LEVEL)
include(CTest)
endif()
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/modules)
include(cmake/base.cmake)
include(cmake/summary.cmake)
# Prefer pthread over other thread libraries.
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
# ------------------------------------------------------------
# Options
# ------------------------------------------------------------
option(FAIRSEQ2N_BUILD_FOR_NATIVE
#DESCRIPTION
"Builds for the processor type of the compiling machine."
#VALUE
OFF
)
option(FAIRSEQ2N_INSTALL_STANDALONE
#DESCRIPTION
"Installs with relative rpaths."
#VALUE
ON
)
option(FAIRSEQ2N_PERFORM_LTO
#DESCRIPTION
"Performs link-time optimization."
#VALUE
OFF
)
option(FAIRSEQ2N_RUN_CLANG_TIDY
#DESCRIPTION
"Runs clang-tidy as static analyzer during compilation."
#VALUE
OFF
)
set(FAIRSEQ2N_SANITIZERS
#VALUE
""
#TYPE
CACHE STRING
#DESCRIPTION
"Sanitizers to enable."
)
set_property(CACHE FAIRSEQ2N_SANITIZERS PROPERTY
STRINGS
"" "asan" "ubsan" "tsan"
)
option(FAIRSEQ2N_TREAT_WARNINGS_AS_ERRORS
#DESCRIPTION
"Treats compilation warnings as errors."
#VALUE
OFF
)
option(FAIRSEQ2N_SUPPORT_IMAGE
#DESCRIPTION
"Supports JPEG/PNG decoding."
#VALUE
ON
)
option(FAIRSEQ2N_USE_LIBTORCH
#DESCRIPTION
"Uses libtorch instead of PyTorch."
#VALUE
OFF
)
option(FAIRSEQ2N_USE_CUDA
#DESCRIPTION
"Builds the CUDA kernels."
#VALUE
OFF
)
if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
set(default_thread_lib tbb)
else()
set(default_thread_lib)
endif()
set(FAIRSEQ2N_THREAD_LIB
#VALUE
${default_thread_lib}
#TYPE
CACHE STRING
#DESCRIPTION
"Thread library to use."
)
set_property(CACHE FAIRSEQ2N_THREAD_LIB PROPERTY
STRINGS
"" "tbb"
)
if(FAIRSEQ2N_THREAD_LIB STREQUAL "tbb")
if(NOT CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
message(FATAL_ERROR "Intel oneTBB is only supported on x86-64 systems.")
endif()
endif()
option(FAIRSEQ2N_BUILD_PYTHON_BINDINGS
#DESCRIPTION
"Builds the Python bindings."
#VALUE
ON
)
cmake_dependent_option(FAIRSEQ2N_PYTHON_DEVEL
#DESCRIPTION
"Copies the Python extension module to the source tree for `pip install --editable`."
#VALUE
ON
#DEPENDS_ON
FAIRSEQ2N_BUILD_PYTHON_BINDINGS
#HIDDEN_VALUE
OFF
)
# ------------------------------------------------------------
# Sanitizers
# ------------------------------------------------------------
fairseq2n_set_sanitizers()
# ------------------------------------------------------------
# Dependencies
# ------------------------------------------------------------
find_package(Iconv REQUIRED)
find_package(SndFile 1.0.25 REQUIRED)
find_package(Threads REQUIRED)
if(FAIRSEQ2N_THREAD_LIB STREQUAL "tbb")
find_package(TBB 2021.8 REQUIRED)
endif()
find_package(Torch 1.13 REQUIRED)
if(FAIRSEQ2N_BUILD_PYTHON_BINDINGS)
find_package(Python3 REQUIRED COMPONENTS Interpreter Development.Module)
if(Python3_VERSION VERSION_LESS 3.8)
message(FATAL_ERROR "fairseq2n requires CPython 3.8 or greater!")
endif()
endif()
if(FAIRSEQ2N_RUN_CLANG_TIDY)
if(NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang")
message(FATAL_ERROR "fairseq2n requires Clang when `FAIRSEQ2N_RUN_CLANG_TIDY` is set!")
endif()
find_package(ClangTidy REQUIRED)
endif()
add_subdirectory(third-party)
fairseq2n_add_fmt()
fairseq2n_add_kaldi_native_fbank()
fairseq2n_add_natsort()
fairseq2n_add_sentencepiece()
fairseq2n_add_zip()
if(FAIRSEQ2N_SUPPORT_IMAGE)
fairseq2n_add_libjpeg_turbo()
fairseq2n_add_libpng()
endif()
if(FAIRSEQ2N_BUILD_PYTHON_BINDINGS)
fairseq2n_add_pybind11()
endif()
if(PROJECT_IS_TOP_LEVEL AND BUILD_TESTING)
fairseq2n_add_gtest()
endif()
# ------------------------------------------------------------
# CUDA
# ------------------------------------------------------------
# By default, we build our CUDA kernels only for the Volta architecture.
if(NOT DEFINED CMAKE_CUDA_ARCHITECTURES)
set(CMAKE_CUDA_ARCHITECTURES 70-real 70-virtual)
endif()
if(FAIRSEQ2N_USE_CUDA)
if(NOT TORCH_CUDA_VERSION)
message(FATAL_ERROR
"fairseq2n requires a CUDA version of PyTorch when `FAIRSEQ2N_USE_CUDA` is set!"
)
endif()
enable_language(CUDA)
find_package(CUDAToolkit REQUIRED)
if(NOT CUDAToolkit_VERSION_MAJOR EQUAL TORCH_CUDA_VERSION_MAJOR)
message(FATAL_ERROR
"fairseq2n requires the version of the CUDA Toolkit (${CUDAToolkit_VERSION}) to match the version of CUDA that was used to build PyTorch (${TORCH_CUDA_VERSION})!"
)
endif()
if(NOT CUDAToolkit_VERSION_MINOR EQUAL TORCH_CUDA_VERSION_MINOR)
message(WARNING
"fairseq2n will use CUDA Toolkit ${CUDAToolkit_VERSION} which has a minor version mismatch with CUDA ${TORCH_CUDA_VERSION} that was used to build PyTorch. Most likely this should not be a problem."
)
endif()
endif()
# ------------------------------------------------------------
# Package Installation
# ------------------------------------------------------------
if(FAIRSEQ2N_INSTALL_STANDALONE)
set(install_lib_dir lib)
set(install_inc_dir include)
else()
set(install_lib_dir ${CMAKE_INSTALL_LIBDIR})
set(install_inc_dir ${CMAKE_INSTALL_INCLUDEDIR})
endif()
set(install_pkg_dir ${install_lib_dir}/cmake/fairseq2n-${PROJECT_VERSION})
configure_package_config_file(
#INPUT
${PROJECT_SOURCE_DIR}/src/fairseq2n-config.cmake.in
#OUTPUT
${PROJECT_BINARY_DIR}/lib/cmake/fairseq2n/fairseq2n-config.cmake
INSTALL_DESTINATION
${install_pkg_dir}
NO_SET_AND_CHECK_MACRO
)
write_basic_package_version_file(
#OUTPUT
${PROJECT_BINARY_DIR}/lib/cmake/fairseq2n/fairseq2n-config-version.cmake
VERSION
${PROJECT_VERSION}
COMPATIBILITY
AnyNewerVersion
)
install(
FILES
${PROJECT_BINARY_DIR}/lib/cmake/fairseq2n/fairseq2n-config.cmake
${PROJECT_BINARY_DIR}/lib/cmake/fairseq2n/fairseq2n-config-version.cmake
DESTINATION
${install_pkg_dir}
COMPONENT
devel
)
install(
EXPORT
fairseq2n-targets
FILE
fairseq2n-targets.cmake
DESTINATION
${install_pkg_dir}
COMPONENT
devel
NAMESPACE
fairseq2n::
)
export(
EXPORT
fairseq2n-targets
FILE
${PROJECT_BINARY_DIR}/lib/cmake/fairseq2n/fairseq2n-targets.cmake
NAMESPACE
fairseq2n::
)
# ------------------------------------------------------------
# Targets
# ------------------------------------------------------------
add_subdirectory(src/fairseq2n)
if(FAIRSEQ2N_BUILD_PYTHON_BINDINGS)
add_subdirectory(python/src/fairseq2n)
endif()
if(PROJECT_IS_TOP_LEVEL AND BUILD_TESTING)
add_subdirectory(tests)
endif()
# ------------------------------------------------------------
# Summary
# ------------------------------------------------------------
fairseq2n_print_project_summary()