-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMakeLists.txt
393 lines (342 loc) · 12.9 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
cmake_minimum_required(VERSION 3.17)
project("InferFlow" C CXX)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
if (NOT XCODE AND NOT MSVC AND NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
endif()
#set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/../../bin)
#
# Option list
#
# general
option(STATIC_LINKING "enable static linking" OFF)
option(ENABLE_NATIVE "enable -march=native flag" OFF)
option(ENABLE_LTO "Enable link time optimization" OFF)
# debug
option(ENABLE_ALL_WARNINGS "Enable all compiler warnings" ON)
option(ENABLE_ALL_WARNINGS_3RD_PARTY "Enable compiler warnings in 3rd party libs" OFF)
# instruction set specific
option(ENABLE_AVX "Enable AVX" ON)
option(ENABLE_AVX2 "Enable AVX2" ON)
option(ENABLE_AVX512 "Enable AVX512" OFF)
option(ENABLE_AVX512_VBMI "Enable AVX512-VBMI" OFF)
option(ENABLE_AVX512_VNNI "Enable AVX512-VNNI" OFF)
option(ENABLE_FMA "Enable FMA" ON)
# in MSVC F16C is implied with AVX2/AVX512
if (NOT MSVC)
option(ENABLE_F16C "Enable F16C" ON)
endif()
# 3rd party libs
option(USE_CUDA "Use cuda (GPU support)" ON)
option(ENABLE_ACCELERATE "Enable Accelerate framework" ON)
option(USE_BLAS "Use BLAS" OFF)
option(USE_CUTLASS "Use cutlass" OFF)
option(USE_CLBLAST "Use CLBlast" OFF)
set(BLAS_VENDOR "Generic" CACHE STRING "BLAS library vendor")
if("${CMAKE_BUILD_TYPE}" STREQUAL "")
set(build_type "release")
else()
#set(build_type ${CMAKE_BUILD_TYPE})
string(TOLOWER "${CMAKE_BUILD_TYPE}" build_type)
endif()
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED true)
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED true)
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
#SET(CMAKE_INSTALL_RPATH "$ORIGIN")
SET(CMAKE_SKIP_BUILD_RPATH FALSE)
SET(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
SET(CMAKE_INSTALL_RPATH "\${ORIGIN}")
option(USE_PROFILER "USE_PROFILER" OFF)
if(${USE_PROFILER})
add_compile_definitions(USE_PROFILER)
endif()
message(STATUS "build_type: ${build_type}")
message(STATUS "CMAKE_INSTALL_RPATH: ${CMAKE_INSTALL_RPATH}")
if(${STATIC_LINKING})
message("Static linking is enabled!")
if(APPLE)
#set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libstdc++")
else()
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libstdc++ -static-libgcc")
endif()
endif()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DHAVE_CONFIG_H -fPIC -std=c++11")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
if(WIN32)
set(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR}/bin/${build_type})
else()
set(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR}/bin/${build_type})
#set(OPENSSL_LIBRARIES)
#find_library(temp libssl.a PATHS "${CMAKE_SOURCE_DIR}/3rd_party/openssl/lib/" )
#list(APPEND OPENSSL_LIBRARIES ${temp})
#unset(temp CACHE)
#find_library(temp libcrypto.a PATHS "${CMAKE_SOURCE_DIR}/3rd_party/openssl/lib/" )
#list(APPEND OPENSSL_LIBRARIES ${temp})
#unset(temp CACHE)
endif()
#if(${STATIC_LINKING})
# set(openssl_lib "OpenSSL::SSL")
# set(crypto_lib "OpenSSL::Crypto")
#else()
# set(openssl_lib "ssl")
# set(crypto_lib "crypto")
#endif()
if (APPLE AND ENABLE_ACCELERATE)
find_library(ACCELERATE_FRAMEWORK Accelerate)
if (ACCELERATE_FRAMEWORK)
message(STATUS "Accelerate framework found")
add_compile_definitions(GGML_USE_ACCELERATE)
set(INFERFLOW_EXTRA_LIBS ${INFERFLOW_EXTRA_LIBS} ${ACCELERATE_FRAMEWORK})
else()
message(WARNING "Accelerate framework not found")
endif()
endif()
if (USE_BLAS)
if (STATIC_LINKING)
set(BLA_STATIC ON)
endif()
if ($(CMAKE_VERSION) VERSION_GREATER_EQUAL 3.22)
set(BLA_SIZEOF_INTEGER 8)
endif()
set(BLA_VENDOR ${BLAS_VENDOR})
find_package(BLAS)
if (BLAS_FOUND)
message(STATUS "BLAS found, Libraries: ${BLAS_LIBRARIES}")
add_compile_options(${BLAS_LINKER_FLAGS})
add_compile_definitions(GGML_USE_OPENBLAS)
set(INFERFLOW_EXTRA_LIBS ${INFERFLOW_EXTRA_LIBS} ${BLAS_LIBRARIES})
message("${BLAS_LIBRARIES} ${BLAS_INCLUDE_DIRS}")
include_directories(${BLAS_INCLUDE_DIRS})
else()
message(WARNING "BLAS not found, please refer to "
"https://cmake.org/cmake/help/latest/module/FindBLAS.html#blas-lapack-vendors"
" to set correct BLAS_VENDOR")
endif()
endif()
if (USE_CUDA)
cmake_minimum_required(VERSION 3.17)
# set(CUDAToolkit_ROOT_DIR "/usr/local/cuda")
# set(CUDAToolkit_BIN_DIR "/usr/local/cuda/bin")
# set(CMAKE_CUDA_COMPILER "/usr/local/cuda/bin/nvcc")
find_package(CUDAToolkit REQUIRED)
find_package(CUDA REQUIRED)
set(INFER_FLOW_NVCC_ARCHS_SUPPORTED "")
if (NOT CUDA_VERSION VERSION_LESS 9.0)
list(APPEND INFER_FLOW_NVCC_ARCHS_SUPPORTED 70)
endif()
if (NOT CUDA_VERSION VERSION_LESS 9.2)
list(APPEND INFER_FLOW_NVCC_ARCHS_SUPPORTED 72)
endif()
if (NOT CUDA_VERSION VERSION_LESS 10.0)
list(APPEND INFER_FLOW_NVCC_ARCHS_SUPPORTED 75)
endif()
if (NOT CUDA_VERSION VERSION_LESS 11.0)
list(APPEND INFER_FLOW_NVCC_ARCHS_SUPPORTED 80)
endif()
message(STATUS "CUDA_VERSION: ${CUDA_VERSION}")
set(CUDA_ARCHITECTURES, ${INFER_FLOW_NVCC_ARCHS_SUPPORTED})
message(STATUS "CUDA_ARCHITECTURES: ${CUDA_ARCHITECTURES}")
message(STATUS "INFER_FLOW_NVCC_ARCHS_SUPPORTED: ${INFER_FLOW_NVCC_ARCHS_SUPPORTED}")
if (CUDAToolkit_FOUND)
message(STATUS "cuda_toolkit found")
enable_language(CUDA)
include_directories("${CUDA_INCLUDE_DIRS}")
add_compile_definitions(USE_CUDA)
if (USE_CUTLASS)
add_compile_definitions(USE_CUTLASS)
endif()
if (USE_CUDA)
if (STATIC_LINKING)
set(INFERFLOW_EXTRA_LIBS ${INFERFLOW_EXTRA_LIBS} CUDA::cudart_static CUDA::cublas_static CUDA::cublasLt_static)
else()
set(INFERFLOW_EXTRA_LIBS ${INFERFLOW_EXTRA_LIBS} CUDA::cudart CUDA::cublas CUDA::cublasLt)
endif()
else()
if (STATIC_LINKING)
set(INFERFLOW_EXTRA_LIBS ${INFERFLOW_EXTRA_LIBS} CUDA::cudart_static)
else()
set(INFERFLOW_EXTRA_LIBS ${INFERFLOW_EXTRA_LIBS} CUDA::cudart)
endif()
endif()
else()
message(WARNING "cuBLAS not found")
endif()
endif()
if (USE_CLBLAST)
find_package(CLBlast)
if (CLBlast_FOUND)
message(STATUS "CLBlast found")
add_compile_definitions(GGML_USE_CLBLAST)
set(INFERFLOW_EXTRA_LIBS ${INFERFLOW_EXTRA_LIBS} clblast)
else()
message(WARNING "CLBlast not found")
endif()
endif()
if (ENABLE_ALL_WARNINGS)
if (NOT MSVC)
set(c_flags
-Wall
-Wextra
-Wpedantic
-Wcast-qual
-Wdouble-promotion
-Wshadow
-Wstrict-prototypes
-Wpointer-arith
)
set(cxx_flags
-Wall
-Wextra
-Wpedantic
-Wcast-qual
-Wno-unused-function
-Wno-multichar
)
else()
endif()
add_compile_options(
"$<$<COMPILE_LANGUAGE:C>:${c_flags}>"
"$<$<COMPILE_LANGUAGE:CXX>:${cxx_flags}>"
)
endif()
if (MSVC)
add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
if (BUILD_SHARED_LIBS)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
endif()
endif()
if (ENABLE_LTO)
include(CheckIPOSupported)
check_ipo_supported(RESULT result OUTPUT output)
if (result)
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
else()
message(WARNING "IPO is not supported: ${output}")
endif()
endif()
message(STATUS "CMAKE_SYSTEM_PROCESSOR: ${CMAKE_SYSTEM_PROCESSOR}")
if (NOT MSVC)
if (STATIC_LINKING)
add_link_options(-static)
if (MINGW)
add_link_options(-static-libgcc -static-libstdc++)
endif()
endif()
if (ENABLE_GPROF)
add_compile_options(-pg)
endif()
if (ENABLE_NATIVE)
add_compile_options(-march=native)
endif()
endif()
if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "arm" OR ${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64")
message(STATUS "ARM detected")
if (MSVC)
# TODO: arm msvc?
else()
if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64")
# Apple M1, M2, etc.
# Raspberry Pi 3, 4, Zero 2 (64-bit)
add_compile_options(-mcpu=native)
endif()
if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "armv6")
# Raspberry Pi 1, Zero
add_compile_options(-mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access)
endif()
if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "armv7")
# Raspberry Pi 2
add_compile_options(-mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access -funsafe-math-optimizations)
endif()
if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "armv8")
# Raspberry Pi 3, 4, Zero 2 (32-bit)
add_compile_options(-mfp16-format=ieee -mno-unaligned-access)
endif()
endif()
elseif (${CMAKE_SYSTEM_PROCESSOR} MATCHES "^(x86_64|i686|AMD64)$")
message(STATUS "x86 detected")
if (MSVC)
if (ENABLE_AVX512)
add_compile_options($<$<COMPILE_LANGUAGE:C>:/arch:AVX512>)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:/arch:AVX512>)
# MSVC has no compile-time flags enabling specific
# AVX512 extensions, neither it defines the
# macros corresponding to the extensions.
# Do it manually.
if (ENABLE_AVX512_VBMI)
add_compile_definitions($<$<COMPILE_LANGUAGE:C>:__AVX512VBMI__>)
add_compile_definitions($<$<COMPILE_LANGUAGE:CXX>:__AVX512VBMI__>)
endif()
if (ENABLE_AVX512_VNNI)
add_compile_definitions($<$<COMPILE_LANGUAGE:C>:__AVX512VNNI__>)
add_compile_definitions($<$<COMPILE_LANGUAGE:CXX>:__AVX512VNNI__>)
endif()
elseif (ENABLE_AVX2)
add_compile_options($<$<COMPILE_LANGUAGE:C>:/arch:AVX2>)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:/arch:AVX2>)
elseif (ENABLE_AVX)
add_compile_options($<$<COMPILE_LANGUAGE:C>:/arch:AVX>)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:/arch:AVX>)
endif()
else()
if (ENABLE_F16C)
add_compile_options(-mf16c)
endif()
if (ENABLE_FMA)
add_compile_options(-mfma)
endif()
if (ENABLE_AVX)
add_compile_options(-mavx)
endif()
if (ENABLE_AVX2)
add_compile_options(-mavx2)
endif()
if (ENABLE_AVX512)
add_compile_options(-mavx512f)
add_compile_options(-mavx512bw)
endif()
if (ENABLE_AVX512_VBMI)
add_compile_options(-mavx512vbmi)
endif()
if (ENABLE_AVX512_VNNI)
add_compile_options(-mavx512vnni)
endif()
endif()
elseif (${CMAKE_SYSTEM_PROCESSOR} MATCHES "ppc64")
message(STATUS "PowerPC detected")
add_compile_options(-mcpu=native -mtune=native)
#TODO: Add targets for Power8/Power9 (Altivec/VSX) and Power10(MMA) and query for big endian systems (ppc64/le/be)
else()
message(STATUS "Unknown architecture")
endif()
if (USE_CUTLASS)
LINK_DIRECTORIES(${CMAKE_SOURCE_DIR}/3rd_party/cutlass/lib64/)
endif()
message("INFERFLOW_EXTRA_LIBS: ${INFERFLOW_EXTRA_LIBS}")
include_directories("src/")
include_directories("3rd_party/")
#include_directories("3rd_party/openssl/include/")
if (USE_CUTLASS)
include_directories("3rd_party/cutlass/include/")
endif()
set(bin_dir ${PROJECT_BINARY_DIR})
message("bin_dir: ${bin_dir}")
add_subdirectory(3rd_party/sslib ${bin_dir}/sslib)
# add_subdirectory(3rd_party/jsoncpp ${bin_dir}/jsoncpp)
include_directories("3rd_party/readerwriterqueue")
#
# Build libraries
#
add_library(ggml OBJECT
3rd_party/ggml/ggml.c
3rd_party/ggml/ggml.h)
target_include_directories(ggml PUBLIC .)
target_compile_features(ggml PUBLIC c_std_11)
target_link_libraries(ggml PUBLIC Threads::Threads ${INFERFLOW_EXTRA_LIBS})
if (BUILD_SHARED_LIBS)
set_target_properties(ggml PROPERTIES POSITION_INDEPENDENT_CODE ON)
endif()
add_subdirectory(src)