-
Notifications
You must be signed in to change notification settings - Fork 30
/
CMakeLists.txt
188 lines (152 loc) · 6.12 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
# Copyright 2024 Tencent Inc. All rights reserved.
#
# ==============================================================================
cmake_minimum_required(VERSION 3.13 FATAL_ERROR)
option(WITH_CUDA "Enable CUDA" ON)
option(WITH_ACL "Enable Ascend" OFF)
option(WITH_TESTING "Enable testing" OFF)
option(WITH_STANDALONE_TEST "Enable standalone testing" OFF)
option(WITH_EVENT_RECORD "Enable event record" OFF)
# Build external libraries statically by default
set(BUILD_SHARED_LIBS OFF)
if(WITH_CUDA)
project(ksana_llm LANGUAGES CXX CUDA)
else()
set(ASCEND_PRODUCT_TYPE "ascend910")
set(ASCEND_PLATFORM_NAME "Ascend910B2C")
set(ASCEND_CORE_TYPE "AiCore")
set(ASCEND_RUN_MODE "ONBOARD")
set(ASCEND_INSTALL_PATH "/usr/local/Ascend/ascend-toolkit/latest")
set(CCE_CMAKE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/module)
list(APPEND CMAKE_MODULE_PATH ${CCE_CMAKE_PATH})
# NOTE(karlluo): languages cce need configured cmake module before
project(LLMKernels LANGUAGES CCE CXX)
endif()
if(NOT WITH_CUDA AND NOT WITH_ACL)
message(FATAL_ERROR "WITH_CUDA=OFF and WITH_ACL=OFF is not allow")
endif()
set(USE_CXX11_ABI, "False")
# according prepare ABI version
execute_process(COMMAND python "-c" "import torch;print(torch._C._GLIBCXX_USE_CXX11_ABI,end='');"
RESULT_VARIABLE _PYTHON_SUCCESS
OUTPUT_VARIABLE USE_CXX11_ABI)
if(NOT _PYTHON_SUCCESS EQUAL 0)
message(FATAL_ERROR "run python -c \"import torch;print(torch._C._GLIBCXX_USE_CXX11_ABI,end='');\" failed.")
endif()
# set compiler flags
if("${USE_CXX11_ABI}" STREQUAL "True")
add_compile_definitions(_GLIBCXX_USE_CXX11_ABI=1)
message(STATUS "Compile with CXX11 ABI")
else()
add_compile_definitions(_GLIBCXX_USE_CXX11_ABI=0)
message(STATUS "Compile without CXX11 ABI")
endif()
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(THIRD_PARTY_PATH ${CMAKE_BINARY_DIR}/third_party)
find_package(Git REQUIRED)
if(WITH_CUDA)
# dedicate for Nvidia GPU
option(CUDA_PTX_VERBOSE_INFO "build nvidia kernels with detailed ptx info" OFF)
find_package(CUDA 11.8 REQUIRED)
find_package(NCCL REQUIRED)
if(NOT DEFINED SM OR "${SM}" STREQUAL "")
message(STATUS "finding sm with ${PROJECT_SOURCE_DIR}/tools/get_nvidia_gpu_properties.py")
execute_process(COMMAND python ${PROJECT_SOURCE_DIR}/tools/get_nvidia_gpu_properties.py OUTPUT_VARIABLE SM OUTPUT_STRIP_TRAILING_WHITESPACE)
message(STATUS "Auto detect SM is ${SM}")
endif()
include(FlashAttention)
endif()
include(LLM_kernels)
include(base)
# set compiler flags
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g3 -DWMMA -gdwarf-4 -gstrict-dwarf")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -Wall -O0 -gdwarf-4 -gstrict-dwarf")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O3 -gdwarf-4 -gstrict-dwarf")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g3 -DWMMA -gdwarf-4 -gstrict-dwarf")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ggdb -O3 -Werror=return-type -Wall -Wno-strict-aliasing -Wno-pointer-arith -Wno-ignored-attributes -Wno-deprecated -finline-functions -Wno-unknown-pragmas -Wno-pointer-arith -Wno-attributes -Wabi-tag")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall -O0 -gdwarf-4 -gstrict-dwarf")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3 -gdwarf-4 -gstrict-dwarf")
set(CXX_STD "17" CACHE STRING "C++ standard")
set(CMAKE_CXX_STANDARD ${CXX_STD})
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_INSTALL_RPATH $ORIGIN)
message(STATUS "Build mode: ${CMAKE_BUILD_TYPE}")
message(STATUS "CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS}")
message(STATUS "CMAKE_CXX_FLAGS_DEBUG: ${CMAKE_CXX_FLAGS_DEBUG}")
message(STATUS "CMAKE_CXX_FLAGS_RELEASE: ${CMAKE_CXX_FLAGS_RELEASE}")
if(WITH_CUDA)
include(nvidia)
endif()
if(WITH_ACL)
include(ascend)
endif()
# set cmake output path
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
# set include headers
set(COMMON_HEADER_DIRS
${PROJECT_SOURCE_DIR}
${PROJECT_SOURCE_DIR}/src
)
# set headers
include_directories(
${COMMON_HEADER_DIRS}
${CUDA_INC_DIRS}
${ACL_INC_DIRS}
)
# set linked libraries
link_directories(
${CUDA_LIB_DIRS}
${ACL_LIB_DIRS}
)
if(WITH_TESTING)
enable_testing()
include(external/gtest)
endif()
if(WITH_EVENT_RECORD)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DENABLE_RECORD_EVENT")
endif()
# include torch
set(PYTHON_PATH "python" CACHE STRING "Python path")
execute_process(COMMAND ${PYTHON_PATH} "-c" "from __future__ import print_function; import torch; print(torch.__version__,end='');"
RESULT_VARIABLE _PYTHON_SUCCESS
OUTPUT_VARIABLE TORCH_VERSION)
if(TORCH_VERSION VERSION_LESS "1.5.0")
message(FATAL_ERROR "PyTorch >= 1.5.0 is needed for TorchScript mode.")
endif()
execute_process(COMMAND ${PYTHON_PATH} "-c" "from __future__ import print_function; import os; import torch;
print(os.path.dirname(torch.__file__),end='');"
RESULT_VARIABLE _PYTHON_SUCCESS
OUTPUT_VARIABLE TORCH_DIR)
if(NOT _PYTHON_SUCCESS MATCHES 0)
message(FATAL_ERROR "Torch config Error.")
endif()
list(APPEND CMAKE_PREFIX_PATH ${TORCH_DIR})
find_package(Torch REQUIRED)
execute_process(COMMAND ${PYTHON_PATH} "-c" "from __future__ import print_function; from distutils import sysconfig;
print(sysconfig.get_python_inc());"
RESULT_VARIABLE _PYTHON_SUCCESS
OUTPUT_VARIABLE PY_INCLUDE_DIR)
if(NOT _PYTHON_SUCCESS MATCHES 0)
message(FATAL_ERROR "Python config Error.")
endif()
execute_process(COMMAND ${PYTHON_PATH} "-c" "from __future__ import print_function; import torch;
print(torch._C._GLIBCXX_USE_CXX11_ABI,end='');"
RESULT_VARIABLE _PYTHON_SUCCESS
OUTPUT_VARIABLE USE_CXX11_ABI)
include_directories(${PY_INCLUDE_DIR})
include_directories(${TORCH_DIR}/include/torch/csrc/api/include/)
include_directories(${TORCH_DIR}/include/)
find_library(TORCH_PYTHON_LIBRARY NAMES torch_python PATHS "${TORCH_DIR}/lib" NO_DEFAULT_PATH)
find_package(PythonLibs REQUIRED)
include_directories(${PYTHON_INCLUDE_DIRS})
find_package(Python3 REQUIRED COMPONENTS Interpreter Development)
add_subdirectory(3rdparty)
add_subdirectory(examples)
add_subdirectory(src)
# for integration test
if(WITH_TESTING)
add_subdirectory(tests)
endif()