-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
327 lines (290 loc) · 11.6 KB
/
CMakeLists.txt
File metadata and controls
327 lines (290 loc) · 11.6 KB
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
cmake_minimum_required(VERSION 3.14)
# Keep the project/package version in one place: fakegpu/_version.py
set(FAKEGPU_VERSION_FILE "${CMAKE_CURRENT_LIST_DIR}/fakegpu/_version.py")
if(NOT EXISTS "${FAKEGPU_VERSION_FILE}")
message(FATAL_ERROR "Version file not found: ${FAKEGPU_VERSION_FILE}")
endif()
file(READ "${FAKEGPU_VERSION_FILE}" FAKEGPU_VERSION_FILE_CONTENTS)
string(REGEX MATCH "__version__ *=[ \t]*\"([0-9]+\\.[0-9]+\\.[0-9]+)\"" FAKEGPU_VERSION_MATCH "${FAKEGPU_VERSION_FILE_CONTENTS}")
if(NOT FAKEGPU_VERSION_MATCH)
message(FATAL_ERROR "Could not parse __version__ from: ${FAKEGPU_VERSION_FILE}")
endif()
set(FAKEGPU_VERSION "${CMAKE_MATCH_1}")
# macOS: always build with AppleClang.
# Homebrew/other GCC toolchains are not compatible with Apple SDK headers.
if(APPLE)
if(EXISTS "/usr/bin/clang" AND EXISTS "/usr/bin/clang++")
set(CMAKE_C_COMPILER "/usr/bin/clang" CACHE FILEPATH "C compiler" FORCE)
set(CMAKE_CXX_COMPILER "/usr/bin/clang++" CACHE FILEPATH "C++ compiler" FORCE)
else()
find_program(_FAKEGPU_CLANG clang)
find_program(_FAKEGPU_CLANGXX clang++)
if(NOT _FAKEGPU_CLANG OR NOT _FAKEGPU_CLANGXX)
message(FATAL_ERROR "clang/clang++ not found. Install Xcode Command Line Tools to build FakeGPU on macOS.")
endif()
set(CMAKE_C_COMPILER "${_FAKEGPU_CLANG}" CACHE FILEPATH "C compiler" FORCE)
set(CMAKE_CXX_COMPILER "${_FAKEGPU_CLANGXX}" CACHE FILEPATH "C++ compiler" FORCE)
endif()
endif()
project(FakeGPU VERSION "${FAKEGPU_VERSION}")
find_package(Threads REQUIRED)
set(FAKEGPU_POSIX_SHM_LIB "")
if(UNIX AND NOT APPLE)
# On older glibc targets such as manylinux2014, shm_open/shm_unlink live in librt.
find_library(FAKEGPU_POSIX_SHM_LIB rt)
endif()
function(fakegpu_link_common_system_libs target_name)
if(CMAKE_DL_LIBS)
target_link_libraries(${target_name} PRIVATE ${CMAKE_DL_LIBS})
endif()
target_link_libraries(${target_name} PRIVATE Threads::Threads)
if(FAKEGPU_POSIX_SHM_LIB)
target_link_libraries(${target_name} PRIVATE ${FAKEGPU_POSIX_SHM_LIB})
endif()
endfunction()
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Option to enable/disable debug logging
option(ENABLE_FAKEGPU_LOGGING "Enable debug logging output" OFF)
if(ENABLE_FAKEGPU_LOGGING)
add_compile_definitions(ENABLE_FAKEGPU_LOGGING)
message(STATUS "FakeGPU logging: ENABLED")
else()
message(STATUS "FakeGPU logging: DISABLED")
endif()
# Option to enable CPU-based compute for selected operators (e.g. cuBLAS GEMM)
option(ENABLE_FAKEGPU_CPU_SIMULATION "Execute supported FakeGPU operators on CPU for improved correctness" ON)
if(ENABLE_FAKEGPU_CPU_SIMULATION)
message(STATUS "FakeGPU CPU simulation: ENABLED")
else()
message(STATUS "FakeGPU CPU simulation: DISABLED")
endif()
# Embed YAML GPU profiles at configure time so defaults are available without runtime files
set(FAKEGPU_PROFILE_DIR "${CMAKE_SOURCE_DIR}/profiles")
file(GLOB FAKEGPU_PROFILE_FILES "${FAKEGPU_PROFILE_DIR}/*.yaml")
set(FAKEGPU_GENERATED_PROFILE_HEADER "${CMAKE_BINARY_DIR}/generated_profiles.hpp")
if(FAKEGPU_PROFILE_FILES)
file(WRITE ${FAKEGPU_GENERATED_PROFILE_HEADER}
"// Auto-generated from profiles/*.yaml - do not edit by hand\n"
"#pragma once\n"
"#include <string>\n"
"#include <vector>\n"
"namespace fake_gpu {\n"
"struct ProfileYamlBlob {\n"
" const char* filename;\n"
" const char* yaml;\n"
"};\n"
"inline const std::vector<ProfileYamlBlob>& builtin_profile_yamls() {\n"
" static const std::vector<ProfileYamlBlob> kProfiles = {\n"
)
set(_profile_idx 0)
foreach(_profile_path ${FAKEGPU_PROFILE_FILES})
file(READ ${_profile_path} _profile_content)
get_filename_component(_profile_name ${_profile_path} NAME)
file(APPEND ${FAKEGPU_GENERATED_PROFILE_HEADER}
" {\"${_profile_name}\", R\"fakegpu_${_profile_idx}(\n${_profile_content})fakegpu_${_profile_idx}\"},\n"
)
math(EXPR _profile_idx "${_profile_idx} + 1")
endforeach()
file(APPEND ${FAKEGPU_GENERATED_PROFILE_HEADER}
" };\n"
" return kProfiles;\n"
"}\n"
"} // namespace fake_gpu\n"
)
else()
file(WRITE ${FAKEGPU_GENERATED_PROFILE_HEADER}
"// Auto-generated: no YAML profiles were found\n"
"#pragma once\n"
"#include <string>\n"
"#include <vector>\n"
"namespace fake_gpu {\n"
"struct ProfileYamlBlob {\n"
" const char* filename;\n"
" const char* yaml;\n"
"};\n"
"inline const std::vector<ProfileYamlBlob>& builtin_profile_yamls() {\n"
" static const std::vector<ProfileYamlBlob> kProfiles;\n"
" return kProfiles;\n"
"}\n"
"} // namespace fake_gpu\n"
)
endif()
# Source directories
add_subdirectory(src/core)
add_subdirectory(src/nvml)
add_subdirectory(src/cuda)
add_subdirectory(src/monitor)
add_subdirectory(src/distributed)
add_subdirectory(src/nccl)
# Main Shared Library for NVML
# We link the OBJECT libraries from subdirectories to create one single shared library
add_library(fake_gpu SHARED
$<TARGET_OBJECTS:fake_gpu_core>
$<TARGET_OBJECTS:fake_gpu_nvml>
$<TARGET_OBJECTS:fake_gpu_cuda>
$<TARGET_OBJECTS:fake_gpu_monitor>
$<TARGET_OBJECTS:fake_gpu_distributed>
)
fakegpu_link_common_system_libs(fake_gpu)
# CUDA Driver Library (libcuda.so.1)
# This library intercepts CUDA Driver API calls
add_library(fake_cuda SHARED
$<TARGET_OBJECTS:fake_gpu_core>
$<TARGET_OBJECTS:fake_gpu_cuda>
$<TARGET_OBJECTS:fake_gpu_monitor>
$<TARGET_OBJECTS:fake_gpu_distributed>
)
fakegpu_link_common_system_libs(fake_cuda)
# CUDA Runtime Library (libcudart.so.12)
# This library intercepts CUDA Runtime API calls and uses fake_cuda Driver API internally
# We only include Driver API stubs, not Runtime API stubs from cuda_stubs.cpp
add_library(fake_cudart SHARED
src/cuda/cudart_stubs.cpp
src/cuda/cuda_driver_stubs.cpp
$<TARGET_OBJECTS:fake_gpu_core>
$<TARGET_OBJECTS:fake_gpu_monitor>
$<TARGET_OBJECTS:fake_gpu_distributed>
)
fakegpu_link_common_system_libs(fake_cudart)
target_include_directories(fake_cudart PRIVATE src/cuda src/core)
# cuBLAS Library (libcublas.so.12)
# This library intercepts cuBLAS/cuBLASLt calls; supported ops can run on CPU for correctness
# (see ENABLE_FAKEGPU_CPU_SIMULATION).
add_library(fake_cublas SHARED
src/cublas/cublas_stubs.cpp
$<TARGET_OBJECTS:fake_gpu_core>
$<TARGET_OBJECTS:fake_gpu_monitor>
$<TARGET_OBJECTS:fake_gpu_distributed>
)
fakegpu_link_common_system_libs(fake_cublas)
target_include_directories(fake_cublas PRIVATE src/cublas src/core)
target_compile_definitions(fake_cublas PRIVATE FAKEGPU_CPU_SIMULATION=$<BOOL:${ENABLE_FAKEGPU_CPU_SIMULATION}>)
target_link_libraries(fake_cublas PRIVATE m)
add_library(fake_nccl SHARED
$<TARGET_OBJECTS:fake_gpu_nccl>
$<TARGET_OBJECTS:fake_gpu_core>
$<TARGET_OBJECTS:fake_gpu_distributed>
)
fakegpu_link_common_system_libs(fake_nccl)
target_include_directories(fake_nccl PRIVATE src/nccl src/core src/distributed)
target_link_libraries(fake_nccl PRIVATE fake_cuda)
# Platform specific flags
if(APPLE)
# macOS: build dylibs with CUDA/NVML-compatible names so DYLD_* search works.
target_link_options(fake_gpu PRIVATE "-undefined" "dynamic_lookup")
target_link_options(fake_cuda PRIVATE "-undefined" "dynamic_lookup")
target_link_options(fake_cudart PRIVATE "-undefined" "dynamic_lookup")
target_link_options(fake_cublas PRIVATE "-undefined" "dynamic_lookup")
target_link_options(fake_nccl PRIVATE "-undefined" "dynamic_lookup")
set_target_properties(fake_gpu PROPERTIES
OUTPUT_NAME "nvidia-ml"
VERSION "${FAKEGPU_VERSION}"
SOVERSION "1"
)
add_custom_command(TARGET fake_gpu POST_BUILD
COMMAND ${CMAKE_COMMAND} -E create_symlink
$<TARGET_FILE_NAME:fake_gpu>
${CMAKE_BINARY_DIR}/libfake_gpu.dylib
COMMENT "Creating libfake_gpu.dylib symlink for backward compatibility"
)
set_target_properties(fake_cuda PROPERTIES
OUTPUT_NAME "cuda"
VERSION "${FAKEGPU_VERSION}"
SOVERSION "1"
)
set_target_properties(fake_cudart PROPERTIES
OUTPUT_NAME "cudart"
VERSION "${FAKEGPU_VERSION}"
SOVERSION "12"
)
set_target_properties(fake_cublas PROPERTIES
OUTPUT_NAME "cublas"
VERSION "${FAKEGPU_VERSION}"
SOVERSION "12"
)
set_target_properties(fake_nccl PROPERTIES
OUTPUT_NAME "nccl"
VERSION "${FAKEGPU_VERSION}"
SOVERSION "2"
)
else()
# Set OUTPUT_NAME to libnvidia-ml.so.1 so CMake sets the correct SONAME
set_target_properties(fake_gpu PROPERTIES
OUTPUT_NAME "nvidia-ml"
VERSION "${FAKEGPU_VERSION}"
SOVERSION "1"
)
# Create symlink for backward compatibility with libfake_gpu.so name
add_custom_command(TARGET fake_gpu POST_BUILD
COMMAND ${CMAKE_COMMAND} -E create_symlink
libnvidia-ml.so.1
${CMAKE_BINARY_DIR}/libfake_gpu.so
COMMENT "Creating libfake_gpu.so symlink for backward compatibility"
)
# Set OUTPUT_NAME to libcuda.so.1 for CUDA Driver API interception
set_target_properties(fake_cuda PROPERTIES
OUTPUT_NAME "cuda"
VERSION "${FAKEGPU_VERSION}"
SOVERSION "1"
)
# Set OUTPUT_NAME to libcudart.so.12 for CUDA Runtime API interception
set_target_properties(fake_cudart PROPERTIES
OUTPUT_NAME "cudart"
VERSION "${FAKEGPU_VERSION}"
SOVERSION "12"
)
# Set OUTPUT_NAME to libcublas.so.12 for cuBLAS API interception
set_target_properties(fake_cublas PROPERTIES
OUTPUT_NAME "cublas"
VERSION "${FAKEGPU_VERSION}"
SOVERSION "12"
)
set_target_properties(fake_nccl PROPERTIES
OUTPUT_NAME "nccl"
VERSION "${FAKEGPU_VERSION}"
SOVERSION "2"
)
endif()
add_executable(fakegpu_config_probe
verification/test_cluster_config_main.cpp
$<TARGET_OBJECTS:fake_gpu_core>
$<TARGET_OBJECTS:fake_gpu_distributed>
)
target_include_directories(fakegpu_config_probe PRIVATE src/core src/distributed)
fakegpu_link_common_system_libs(fakegpu_config_probe)
add_executable(fakegpu_topology_probe
verification/test_topology_model_main.cpp
$<TARGET_OBJECTS:fake_gpu_core>
$<TARGET_OBJECTS:fake_gpu_distributed>
)
target_include_directories(fakegpu_topology_probe PRIVATE src/core src/distributed)
fakegpu_link_common_system_libs(fakegpu_topology_probe)
add_executable(fakegpu_coordinator
src/distributed/coordinator_main.cpp
$<TARGET_OBJECTS:fake_gpu_core>
$<TARGET_OBJECTS:fake_gpu_distributed>
)
target_include_directories(fakegpu_coordinator PRIVATE src/distributed)
fakegpu_link_common_system_libs(fakegpu_coordinator)
set_target_properties(fakegpu_coordinator PROPERTIES OUTPUT_NAME "fakegpu-coordinator")
add_executable(fakegpu_nccl_direct_test
verification/test_nccl_direct.cpp
)
target_include_directories(fakegpu_nccl_direct_test PRIVATE src/nccl src/distributed)
target_link_libraries(fakegpu_nccl_direct_test PRIVATE fake_nccl fake_gpu Threads::Threads)
add_executable(fakegpu_staging_buffer_probe
verification/test_staging_buffer_main.cpp
)
target_include_directories(fakegpu_staging_buffer_probe PRIVATE src/distributed)
target_link_libraries(fakegpu_staging_buffer_probe PRIVATE fake_gpu Threads::Threads)
add_executable(fakegpu_collective_direct_test
verification/test_collective_direct.cpp
)
target_include_directories(fakegpu_collective_direct_test PRIVATE src/nccl src/distributed)
target_link_libraries(fakegpu_collective_direct_test PRIVATE fake_nccl fake_gpu Threads::Threads)
add_executable(fakegpu_group_semantics_test
verification/test_group_semantics_main.cpp
)
target_include_directories(fakegpu_group_semantics_test PRIVATE src/nccl src/distributed)
target_link_libraries(fakegpu_group_semantics_test PRIVATE fake_nccl fake_gpu Threads::Threads)