forked from MooreThreads/torch_musa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
255 lines (226 loc) · 9.86 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
cmake_minimum_required(VERSION 3.13 FATAL_ERROR)
project(Torch_MUSA CXX C)
if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
set(LINUX TRUE)
else()
message(FATAL_ERROR "torch_musa could be built only on Linux now!")
endif()
set(CMAKE_INSTALL_MESSAGE NEVER)
# check and set CMAKE_CXX_STANDARD
string(FIND "${CMAKE_CXX_FLAGS}" "-std=c++" env_cxx_standard)
if(env_cxx_standard GREATER -1)
message(
WARNING "C++ standard version definition detected in environment variable."
"PyTorch requires -std=c++17. Please remove -std=c++ settings in your environment.")
endif()
set(CMAKE_CXX_STANDARD 17 CACHE STRING "The C++ standard whose features are requested to build this target.")
set(CMAKE_C_STANDARD 11 CACHE STRING "The C standard whose features are requested to build this target.")
if(DEFINED GLIBCXX_USE_CXX11_ABI)
if(${GLIBCXX_USE_CXX11_ABI} EQUAL 1)
set(CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_GLIBCXX_USE_CXX11_ABI=1")
endif()
endif()
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# Googletest's cmake files are going to set it on once they are processed. Let's
# set it at the very beginning so that the entire build is deterministic.
set(THREADS_PREFER_PTHREAD_FLAG ON)
option(ENABLE_COMPILE_FP64 "Enable FP64" OFF)
option(USE_ASAN "Use Address Sanitizer" OFF)
option(USE_TSAN "Use Thread Sanitizer" OFF)
option(USE_MUSA "Use MUSA" ON)
include(CMakeDependentOption)
cmake_dependent_option(
USE_MUDNN "Use muDNN" ON
"USE_MUSA" OFF)
cmake_dependent_option(USE_CCACHE "Attempt using CCache to wrap the compilation" ON "UNIX" OFF)
if(USE_CCACHE)
find_program(CCACHE_PROGRAM ccache)
if(CCACHE_PROGRAM)
set(CMAKE_C_COMPILER_LAUNCHER "${CCACHE_PROGRAM}" CACHE STRING "C compiler launcher")
set(CMAKE_CXX_COMPILER_LAUNCHER "${CCACHE_PROGRAM}" CACHE STRING "CXX compiler launcher")
else()
message(STATUS "Could not find ccache. Consider installing ccache to speed up compilation.")
endif()
endif()
# ---[ Version numbers for generated libraries
file(READ version.txt TORCH_MUSA_DEFAULT_VERSION)
# Strip trailing newline
string(REGEX REPLACE "\n$" "" TORCH_MUSA_DEFAULT_VERSION "${TORCH_MUSA_DEFAULT_VERSION}")
if("${TORCH_MUSA_DEFAULT_VERSION} " STREQUAL " ")
message(WARNING "Could not get version from base 'version.txt'")
# If we can't get the version from the version file we should probably
# set it to something non-sensical like 0.0.0
set(TORCH_MUSA_DEFAULT_VERSION, "0.0.0")
endif()
include(${PROJECT_SOURCE_DIR}/cmake/utils.cmake)
torch_musa_parse_version_str(TORCH_MUSA ${TORCH_MUSA_DEFAULT_VERSION})
set(TORCH_MUSA_SOVERSION "${TORCH_MUSA_VERSION_MAJOR}.${TORCH_MUSA_VERSION_MINOR}")
# Set default build type
if(NOT CMAKE_BUILD_TYPE)
message(STATUS "Build type not set - defaulting to Release")
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build from: Debug Release RelWithDebInfo MinSizeRel Coverage." FORCE)
endif()
string(APPEND CMAKE_CXX_FLAGS " -O2 -fPIC")
string(APPEND CMAKE_CXX_FLAGS " -Wall")
string(APPEND CMAKE_CXX_FLAGS " -Wextra")
string(APPEND CMAKE_CXX_FLAGS " -Werror")
string(APPEND CMAKE_CXX_FLAGS " -Wno-unused-parameter")
string(APPEND CMAKE_CXX_FLAGS " -Wno-unused-variable")
string(APPEND CMAKE_CXX_FLAGS " -Wno-unused-function")
string(APPEND CMAKE_CXX_FLAGS " -Wno-sign-compare")
string(APPEND CMAKE_CXX_FLAGS " -Wno-missing-field-initializers")
string(APPEND CMAKE_CXX_FLAGS " -Wno-non-template-friend") #TODO(MT-AI): remove this CXX_FLAG after https://jira.mthreads.com/browse/SW-22268 resolved.
string(APPEND CMAKE_CXX_FLAGS " -Wno-comment") #TODO(MT-AI): remove this CXX_FLAG after https://jira.mthreads.com/browse/SW-22268 resolved.
# Use ld.gold if available, fall back to ld.bfd (the default ld) if not
if(USE_GOLD_LINKER)
if(USE_DISTRIBUTED AND USE_MPI)
message(WARNING "Refusing to use gold when USE_MPI=1")
else()
execute_process(
COMMAND
"${CMAKE_C_COMPILER}" -fuse-ld=gold -Wl,--version
ERROR_QUIET
OUTPUT_VARIABLE LD_VERSION)
if(NOT "${LD_VERSION}" MATCHES "GNU gold")
message(WARNING "USE_GOLD_LINKER was set but ld.gold isn't available, turning it off")
set(USE_GOLD_LINKER OFF)
else()
message(STATUS "ld.gold is available, using it to link")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=gold")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fuse-ld=gold")
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -fuse-ld=gold")
endif()
endif()
endif()
# These flags are not available in GCC-4.8.5. Set only when using clang.
# Compared against https://gcc.gnu.org/onlinedocs/gcc-4.8.5/gcc/Option-Summary.html
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
if(${COLORIZE_OUTPUT})
string(APPEND CMAKE_CXX_FLAGS " -fcolor-diagnostics")
endif()
endif()
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 4.9)
if(${COLORIZE_OUTPUT})
string(APPEND CMAKE_CXX_FLAGS " -fdiagnostics-color=always")
endif()
endif()
string(APPEND CMAKE_CXX_FLAGS_DEBUG " -fno-omit-frame-pointer -O0")
string(APPEND CMAKE_LINKER_FLAGS_DEBUG " -fno-omit-frame-pointer -O0")
string(APPEND CMAKE_CXX_FLAGS " -fno-math-errno")
string(APPEND CMAKE_CXX_FLAGS " -fno-trapping-math")
include(CheckCXXCompilerFlag)
check_cxx_compiler_flag("-Werror=format" HAS_WERROR_FORMAT)
if(HAS_WERROR_FORMAT)
string(APPEND CMAKE_CXX_FLAGS " -Werror=format")
endif()
check_cxx_compiler_flag("-Werror=cast-function-type" HAS_WERROR_CAST_FUNCTION_TYPE)
if(HAS_WERROR_CAST_FUNCTION_TYPE)
string(APPEND CMAKE_CXX_FLAGS " -Werror=cast-function-type")
endif()
check_cxx_compiler_flag("-Werror=sign-compare" HAS_WERROR_SIGN_COMPARE)
if(USE_ASAN)
string(APPEND CMAKE_CXX_FLAGS_DEBUG " -fsanitize=address")
string(APPEND CMAKE_LINKER_FLAGS_DEBUG " -fsanitize=address")
endif()
if(USE_TSAN)
string(APPEND CMAKE_CXX_FLAGS_DEBUG " -fsanitize=thread")
string(APPEND CMAKE_LINKER_FLAGS_DEBUG " -fsanitize=thread")
endif()
if(DEFINED ENV{MUSA_ARCH})
set(MUSA_ARCH $ENV{MUSA_ARCH})
string(APPEND MUSA_MCC_FLAGS " --cuda-gpu-arch=mp_${MUSA_ARCH}")
if(${MUSA_ARCH} EQUAL 21)
add_definitions(-DTORCH_MUSA_ARCH=210)
elseif(${MUSA_ARCH} EQUAL 22)
add_definitions(-DTORCH_MUSA_ARCH=220)
elseif(${MUSA_ARCH} EQUAL 31)
add_definitions(-DTORCH_MUSA_ARCH=310)
else()
add_definitions(-DTORCH_MUSA_ARCH=110)
endif()
endif()
#=============================================================
set(PLUGIN_NAME "musa_python")
set(MUSA_CSRCS)
set(MUSA_KERNELS_LIB "musa_kernels")
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/modules)
set(DEPENDENT_LIBRARIES "")
set(DEPENDENT_INCLUDE_DIRS "")
find_package(MUDNN)
if(MUDNN_FOUND)
list(APPEND DEPENDENT_INCLUDE_DIRS ${MUDNN_INCLUDE_DIRS})
list(APPEND DEPENDENT_LIBRARIES ${MUDNN_LIBRARIES})
else()
# set default mudnn library path
message(WARNING " The environment variable MUSA_HOME may be not specified. Using default MUDNN PATH: /usr/local/musa")
list(APPEND DEPENDENT_INCLUDE_DIRS "/usr/local/musa/include")
list(APPEND DEPENDENT_LIBRARIES "/usr/local/musa/lib/libmudnn.so")
set(MUDNN_PATH "/usr/local/musa")
set(MUDNN_LIBRARIES "/usr/local/musa/lib/libmudnn.so")
endif()
find_package(MCCL)
if(MCCL_FOUND)
list(APPEND DEPENDENT_INCLUDE_DIRS ${MCCL_INCLUDE_DIRS})
list(APPEND DEPENDENT_LIBRARIES ${MCCL_LIBRARIES})
else ()
message (WARNING " NO MCCL FOUND?")
list(APPEND DEPENDENT_INCLUDE_DIRS "/usr/local/musa/include")
list(APPEND DEPENDENT_LIBRARIES "/usr/local/musa/lib/libmccl.so")
set(MCCL_PATH "/usr/local/musa")
set(MCCL_LIBRARIES "/usr/local/musa/lib/libmccl.so")
endif ()
find_package(MUSAToolkits)
if(MUSAToolkits_FOUND)
list(APPEND DEPENDENT_INCLUDE_DIRS ${MUSAToolkits_INCLUDE_DIRS})
list(APPEND DEPENDENT_LIBRARIES ${MUSAToolkits_LIBRARIES})
else()
# set default musa_toolkits path
message(WARNING " The environment variable MUSA_HOME may be not specified. Using default MUSATOOLKITS PATH: /usr/local/musa")
list(APPEND DEPENDENT_INCLUDE_DIRS "/usr/local/musa/include/")
list(APPEND DEPENDENT_LIBRARIES "/usr/local/musa/lib/libmusart.so")
set(ENV{MUSA_HOME} "/usr/local/musa")
set(MUSATOOLKITS_PATH "/usr/local/musa")
set(MUSAToolkits_LIBRARIES "/usr/local/musa/lib/")
endif()
include_directories(${PROJECT_SOURCE_DIR})
include_directories(${PROJECT_SOURCE_DIR}/torch_musa/csrc/aten/core)
include_directories(${PROJECT_SOURCE_DIR}/torch_musa/csrc/aten/utils)
include_directories(${PROJECT_SOURCE_DIR}/torch_musa/csrc/aten/ops)
include_directories(${PROJECT_SOURCE_DIR}/torch_musa/csrc/aten/ops/musa)
include_directories(${DEPENDENT_INCLUDE_DIRS})
## Set PyTorch headers dir
include_directories(${CMAKE_BINARY_DIR}/${GENERATED_PORTING_DIR}/include)
include_directories(${CMAKE_BINARY_DIR}/${GENERATED_PORTING_DIR}/include/torch/csrc/api/include)
# Set Python include dir
if(DEFINED PYTHON_INCLUDE_DIR)
include_directories(${PYTHON_INCLUDE_DIR})
else()
message(FATAL_ERROR "Cannot find installed Python head file directory")
endif()
add_subdirectory(${PROJECT_SOURCE_DIR}/torch_musa/csrc)
set(CPP_SRCS ${MUSA_CSRCS})
add_library(${PLUGIN_NAME} SHARED ${CPP_SRCS})
target_link_libraries(${PLUGIN_NAME} PUBLIC ${DEPENDENT_LIBRARIES})
target_link_libraries(${PLUGIN_NAME} PUBLIC ${MUSA_KERNELS_LIB})
set(INSTALL_BIN_DIR "bin")
set(INSTALL_LIB_DIR "lib64")
set(INSTALL_INC_DIR "include")
set(INSTALL_SHARE_DIR "share")
set(INSTALL_DOC_DIR "docs")
set_target_properties(${PLUGIN_NAME} PROPERTIES
VERSION ${TORCH_MUSA_DEFAULT_VERSION}
SOVERSION ${TORCH_MUSA_VERSION_MAJOR}
)
set_target_properties(${PLUGIN_NAME} PROPERTIES
OUTPUT_NAME ${PLUGIN_NAME}
POSITION_INDEPENDENT_CODE true
INSTALL_RPATH_USE_LINK_PATH false
RUNTIME_OUTPUT_DIRECTORY ${INSTALL_BIN_DIR}
LIBRARY_OUTPUT_DIRECTORY ${INSTALL_LIB_DIR}
ARCHIVE_OUTPUT_DIRECTORY ${INSTALL_LIB_DIR}
)
set_target_properties(${PLUGIN_NAME} PROPERTIES LINK_FLAGS "-Wl,-rpath,$ORIGIN")
install(TARGETS ${PLUGIN_NAME})
include(${PROJECT_SOURCE_DIR}/cmake/summary.cmake)
torch_musa_build_configuration_summary()