Skip to content

Commit 391c9ef

Browse files
q10facebook-github-bot
authored andcommitted
Modularize CMake Build [4/N] (pytorch#536)
Summary: Pull Request resolved: facebookresearch/FBGEMM#536 - Build shared instead of static libraries - Break up TBE ops into its own library X-link: pytorch#3417 Reviewed By: sryap, spcyppt Differential Revision: D66737287 Pulled By: q10 fbshipit-source-id: 93135e717f4e9f55b78a97d0c9788bccb69b2b5d
1 parent 8b87f9f commit 391c9ef

File tree

7 files changed

+316
-139
lines changed

7 files changed

+316
-139
lines changed

cmake/modules/GpuCppLibrary.cmake

Lines changed: 72 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ function(gpu_cpp_library)
149149
set(singleValueArgs
150150
PREFIX # Desired name for the library target (and by extension, the prefix for naming intermediate targets)
151151
TYPE # Target type, e.g., MODULE, OBJECT. See https://cmake.org/cmake/help/latest/command/add_library.html
152+
DESTINATION # The install destination directory to place the build target into
152153
)
153154
set(multiValueArgs
154155
CPU_SRCS # Sources for CPU-only build
@@ -182,30 +183,26 @@ function(gpu_cpp_library)
182183
INCLUDE_DIRS ${args_INCLUDE_DIRS})
183184
set(lib_sources ${${args_PREFIX}_sources})
184185

185-
############################################################################
186-
# Prepare Target Deps
187-
############################################################################
188-
189-
# Convert target dependency references into CMake target-dependent expressions
190-
# See https://cmake.org/cmake/help/latest/manual/cmake-generator-expressions.7.html#id34
191-
set(target_deps)
192-
foreach(dep ${args_DEPS})
193-
list(APPEND target_deps "$<TARGET_OBJECTS:${dep}>")
194-
endforeach()
195-
196186
############################################################################
197187
# Build the Library
198188
############################################################################
199189

190+
# Set the build target name
200191
set(lib_name ${args_PREFIX})
201-
if(USE_ROCM)
202-
# Fetch the equivalent HIPified sources if available.
203-
# This presumes that `hipify()` has already been run.
204-
get_hipified_list("${lib_sources}" lib_sources_hipified)
205192

206-
# Set properties for the HIPified sources
207-
set_source_files_properties(${lib_sources_hipified}
208-
PROPERTIES HIP_SOURCE_PROPERTY_FORMAT 1)
193+
if(USE_ROCM)
194+
if(lib_sources)
195+
# Fetch the equivalent HIPified sources if available.
196+
# This presumes that `hipify()` has already been run.
197+
#
198+
# This code is placed under an if-guard so that it won't fail for
199+
# targets that have nothing to do with HIP, e.g. asmjit
200+
get_hipified_list("${lib_sources}" lib_sources_hipified)
201+
202+
# Set properties for the HIPified sources
203+
set_source_files_properties(${lib_sources_hipified}
204+
PROPERTIES HIP_SOURCE_PROPERTY_FORMAT 1)
205+
endif()
209206

210207
# Set the include directories for HIP
211208
hip_include_directories("${args_INCLUDE_DIRS}")
@@ -214,7 +211,6 @@ function(gpu_cpp_library)
214211
hip_add_library(${lib_name} ${args_TYPE}
215212
${lib_sources_hipified}
216213
${args_OTHER_SRCS}
217-
${target_deps}
218214
${FBGEMM_HIP_HCC_LIBRARIES}
219215
HIPCC_OPTIONS
220216
${HIP_HCC_FLAGS})
@@ -230,39 +226,61 @@ function(gpu_cpp_library)
230226
# Create the CPU-only / CUDA library
231227
add_library(${lib_name} ${args_TYPE}
232228
${lib_sources}
233-
${args_OTHER_SRCS}
234-
${target_deps})
229+
${args_OTHER_SRCS})
235230
endif()
236231

237232
############################################################################
238233
# Library Includes and Linking
239234
############################################################################
240235

241-
# Add PyTorch include/
236+
# Add external include directories
242237
target_include_directories(${lib_name} PRIVATE
243238
${TORCH_INCLUDE_DIRS}
244239
${NCCL_INCLUDE_DIRS})
245240

246241
# Set additional target properties
247242
set_target_properties(${lib_name} PROPERTIES
248-
# Remove `lib` prefix from the output artifact name, e.g. `libfoo.so` -> `foo.so`
243+
# Remove `lib` prefix from the output artifact name, e.g.
244+
# `libfoo.so` -> `foo.so`
249245
PREFIX ""
250246
# Enforce -fPIC for STATIC library option, since they are to be
251247
# integrated into other libraries down the line
252248
# https://stackoverflow.com/questions/3961446/why-does-gcc-not-implicitly-supply-the-fpic-flag-when-compiling-static-librarie
253249
POSITION_INDEPENDENT_CODE ON)
254250

255-
# Link to PyTorch
256-
target_link_libraries(${lib_name}
251+
if (args_DEPS)
252+
# Only set this if the library has dependencies that we also build,
253+
# otherwise we will hit the following error:
254+
# `No valid ELF RPATH or RUNPATH entry exists in the file`
255+
set_target_properties(${lib_name} PROPERTIES
256+
BUILD_WITH_INSTALL_RPATH ON
257+
# Set the RPATH for the library to include $ORIGIN, so it can look
258+
# into the same directory for dependency .SO files to load, e.g.
259+
# fbgemm_gpu.so -> fbgemm.so, asmjit.so
260+
#
261+
# More info on RPATHS:
262+
# https://amir.rachum.com/shared-libraries/#debugging-cheat-sheet
263+
# https://stackoverflow.com/questions/43330165/how-to-link-a-shared-library-with-cmake-with-relative-path
264+
# https://stackoverflow.com/questions/57915564/cmake-how-to-set-rpath-to-origin-with-cmake
265+
# https://stackoverflow.com/questions/58360502/how-to-set-rpath-origin-in-cmake
266+
INSTALL_RPATH "\$ORIGIN")
267+
endif()
268+
269+
# Collect external libraries for linking
270+
set(library_dependencies
257271
${TORCH_LIBRARIES}
258272
${NCCL_LIBRARIES}
259-
${CUDA_DRIVER_LIBRARIES})
273+
${CUDA_DRIVER_LIBRARIES}
274+
${args_DEPS})
260275

261-
# Link to NVML if available
276+
# Add NVML if available
262277
if(NVML_LIB_PATH)
263-
target_link_libraries(${lib_name} ${NVML_LIB_PATH})
278+
list(APPEND library_dependencies ${NVML_LIB_PATH})
264279
endif()
265280

281+
# Link against the external libraries as needed
282+
target_link_libraries(${lib_name} PRIVATE ${library_dependencies})
283+
266284
# Silence compiler warnings (in asmjit)
267285
target_compile_options(${lib_name} PRIVATE
268286
-Wno-deprecated-anon-enum-enum-conversion
@@ -272,21 +290,36 @@ function(gpu_cpp_library)
272290
# Post-Build Steps
273291
############################################################################
274292

293+
# NOTE: Fix this in future PR
294+
275295
# Add a post-build step to remove errant RPATHs from the .SO
276-
add_custom_target(${lib_name}_postbuild ALL
277-
DEPENDS
278-
WORKING_DIRECTORY ${OUTPUT_DIR}
279-
COMMAND bash ${FBGEMM}/.github/scripts/fbgemm_gpu_postbuild.bash)
296+
# add_custom_target(${lib_name}_postbuild ALL
297+
# DEPENDS
298+
# WORKING_DIRECTORY ${OUTPUT_DIR}
299+
# COMMAND bash ${FBGEMM}/.github/scripts/fbgemm_gpu_postbuild.bash)
280300

281-
# Set the post-build steps to run AFTER the build completes
282-
add_dependencies(${lib_name}_postbuild ${lib_name})
301+
# # Set the post-build steps to run AFTER the build completes
302+
# add_dependencies(${lib_name}_postbuild ${lib_name})
283303

284304
############################################################################
285305
# Set the Output Variable(s)
286306
############################################################################
287307

288308
set(${args_PREFIX} ${lib_name} PARENT_SCOPE)
289309

310+
############################################################################
311+
# Add to Install Package
312+
############################################################################
313+
314+
if(args_DESTINATION)
315+
install(TARGETS ${args_PREFIX}
316+
DESTINATION ${args_DESTINATION})
317+
endif()
318+
319+
############################################################################
320+
# Debug Summary
321+
############################################################################
322+
290323
BLOCK_PRINT(
291324
"GPU CPP Library Target: ${args_PREFIX} (${args_TYPE})"
292325
" "
@@ -317,10 +350,13 @@ function(gpu_cpp_library)
317350
"HIPified Source Files:"
318351
"${lib_sources_hipified}"
319352
" "
320-
"Target Dependencies:"
321-
"${target_deps}"
353+
"Library Dependencies:"
354+
"${library_dependencies}"
322355
" "
323356
"Output Library:"
324357
"${lib_name}"
358+
" "
359+
"Destination Directory:"
360+
"${args_DESTINATION}"
325361
)
326362
endfunction()

fbgemm_gpu/CMakeLists.txt

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111
cmake_minimum_required(VERSION 3.25.0 FATAL_ERROR)
1212

1313
set(CMAKEMODULES ${CMAKE_CURRENT_SOURCE_DIR}/../cmake/modules)
14+
set(FBGEMM_GPU ${CMAKE_CURRENT_SOURCE_DIR})
1415
set(FBGEMM ${CMAKE_CURRENT_SOURCE_DIR}/..)
1516
set(THIRDPARTY ${FBGEMM}/external)
1617

1718
include(${CMAKEMODULES}/Utilities.cmake)
1819

19-
set(CMAKE_VERBOSE_MAKEFILE on)
20+
set(CMAKE_VERBOSE_MAKEFILE ON)
2021

2122
################################################################################
2223
# FBGEMM_GPU Build Options
@@ -108,6 +109,19 @@ set(fbgemm_sources_include_directories
108109
${NCCL_INCLUDE_DIRS})
109110

110111

112+
################################################################################
113+
# Build Library Dependencies
114+
################################################################################
115+
116+
# These dependencies should be declared and built before building FBGEMM_GPU
117+
118+
# Target: `asmjit`
119+
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/Asmjit.cmake)
120+
121+
# Target: `fbgemm`
122+
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/Fbgemm.cmake)
123+
124+
111125
################################################################################
112126
# TBE Code Generation
113127
################################################################################
@@ -138,9 +152,9 @@ foreach(script
138152
endforeach()
139153

140154

141-
# ################################################################################
155+
################################################################################
142156
# HIP Code Generation
143-
# ################################################################################
157+
################################################################################
144158

145159
if(USE_ROCM)
146160
set(include_dirs_for_hipification

0 commit comments

Comments
 (0)