Skip to content

Commit 998e7a7

Browse files
cthifacebook-github-bot
authored andcommitted
Build changes to support integration with pytorch (#4354)
Summary: Pull Request resolved: #4354 We plan to start integration of FBGEMM GenAI build into pytorch directly. This change would make the FBGEMM CMake compatible with building FBGEMM GenAI as part of pytorch as a dependency. Later we will have a PR in pytorch to update the pinned fbgemm and update their build. Changes for torch: - Allow option to keep `lib` prefix, to keep it more standard with other libs - Allow passing in torch cmake targets so we can link against them in fbgemm - Use the library name `libfbgemm_gpu_gen_ai.so` (without experimental) as it felt a bit wierd to keep that in. - We will export the target so torch can later link with `libfbgemm_gpu_gen_ai.so` X-link: facebookresearch/FBGEMM#1422 Test Plan: Built locally and confirmed things work: ``` test_fbgemm_gpu_build_and_install $CONDA_ENV genai/cuda ``` ``` test_fbgemm_gpu_build_and_install $CONDA_ENV default/cuda ``` Will let OSS CI run for more comprehensive test. Reviewed By: q10 Differential Revision: D76734540 Pulled By: cthi fbshipit-source-id: c138ec93d3e78d93c77ade4d1b8b8ed902489db3
1 parent 940a285 commit 998e7a7

File tree

5 files changed

+60
-19
lines changed

5 files changed

+60
-19
lines changed

cmake/modules/CudaSetup.cmake

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
# This source code is licensed under the BSD-style license found in the
55
# LICENSE file in the root directory of this source tree.
66

7-
include(${CMAKE_CURRENT_SOURCE_DIR}/../cmake/modules/Utilities.cmake)
8-
9-
107
################################################################################
118
# CUDA Setup
129
################################################################################

cmake/modules/GpuCppLibrary.cmake

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
# This source code is licensed under the BSD-style license found in the
55
# LICENSE file in the root directory of this source tree.
66

7-
include(${CMAKE_CURRENT_SOURCE_DIR}/../cmake/modules/Utilities.cmake)
8-
97
function(prepare_target_sources)
108
# This function does the following:
119
#
@@ -150,6 +148,7 @@ function(gpu_cpp_library)
150148
PREFIX # Desired name for the library target (and by extension, the prefix for naming intermediate targets)
151149
TYPE # Target type, e.g., MODULE, OBJECT. See https://cmake.org/cmake/help/latest/command/add_library.html
152150
DESTINATION # The install destination directory to place the build target into
151+
KEEP_PREFIX # Whether to keep the prefix for the library target, e.g. libfoo.so vs foo.so
153152
)
154153
set(multiValueArgs
155154
CPU_SRCS # Sources for CPU-only build
@@ -162,6 +161,7 @@ function(gpu_cpp_library)
162161
HIPCC_FLAGS # Compilation flags specific to HIPCC
163162
INCLUDE_DIRS # Include directories for compilation
164163
DEPS # Target dependencies, i.e. built STATIC targets
164+
TORCH_LIBS # PyTorch libraries to link against. Note that we provide the TORCH_LIBS automatically - this is for PyTorch build.
165165
)
166166

167167
cmake_parse_arguments(
@@ -258,19 +258,23 @@ function(gpu_cpp_library)
258258
${NCCL_INCLUDE_DIRS})
259259

260260
# Set additional target properties
261-
set_target_properties(${lib_name} PROPERTIES
261+
if(NOT args_KEEP_PREFIX)
262262
# Remove `lib` prefix from the output artifact name, e.g.
263263
# `libfoo.so` -> `foo.so`
264-
PREFIX ""
264+
set_target_properties(${lib_name} PROPERTIES PREFIX "")
265+
endif()
266+
267+
set_target_properties(${lib_name} PROPERTIES
265268
# Enforce -fPIC for STATIC library option, since they are to be
266269
# integrated into other libraries down the line
267270
# https://stackoverflow.com/questions/3961446/why-does-gcc-not-implicitly-supply-the-fpic-flag-when-compiling-static-librarie
268271
POSITION_INDEPENDENT_CODE ON)
269272

270-
if (args_DEPS)
273+
if (args_DEPS OR CMAKE_INSTALL_RPATH)
271274
# Only set this if the library has dependencies that we also build,
272275
# otherwise we will hit the following error:
273276
# `No valid ELF RPATH or RUNPATH entry exists in the file`
277+
# However, if CMAKE_INSTALL_RPATH is set, respect that logic. Such as when we build with PyTorch.
274278
set_target_properties(${lib_name} PROPERTIES
275279
BUILD_WITH_INSTALL_RPATH ON
276280
# Set the RPATH for the library to include $ORIGIN, so it can look
@@ -288,6 +292,7 @@ function(gpu_cpp_library)
288292
# Collect external libraries for linking
289293
set(library_dependencies
290294
${TORCH_LIBRARIES}
295+
${args_TORCH_LIBS}
291296
${NCCL_LIBRARIES}
292297
${CUDA_DRIVER_LIBRARIES}
293298
${args_DEPS})
@@ -315,7 +320,7 @@ function(gpu_cpp_library)
315320
# Post-Build Steps
316321
############################################################################
317322

318-
if (args_DEPS)
323+
if (args_DEPS OR CMAKE_INSTALL_RPATH)
319324
# Only set this if the library has dependencies that we also build,
320325
# otherwise we will hit the following error:
321326
# `No valid ELF RPATH or RUNPATH entry exists in the file`
@@ -342,7 +347,10 @@ function(gpu_cpp_library)
342347
############################################################################
343348

344349
if(args_DESTINATION)
345-
install(TARGETS ${args_PREFIX}
350+
install(
351+
TARGETS ${args_PREFIX}
352+
# Allows args_PREFIX to be exported as a target, used for PyTorch build integration
353+
EXPORT fbgemmGenAILibraryConfig
346354
DESTINATION ${args_DESTINATION})
347355
endif()
348356

cmake/modules/PyTorchSetup.cmake

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
# This source code is licensed under the BSD-style license found in the
55
# LICENSE file in the root directory of this source tree.
66

7-
include(${CMAKE_CURRENT_SOURCE_DIR}/../cmake/modules/Utilities.cmake)
8-
9-
107
################################################################################
118
# PyTorch Dependencies Setup
129
################################################################################

cmake/modules/RocmSetup.cmake

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
# This source code is licensed under the BSD-style license found in the
55
# LICENSE file in the root directory of this source tree.
66

7-
include(${CMAKE_CURRENT_SOURCE_DIR}/../cmake/modules/Utilities.cmake)
8-
9-
107
################################################################################
118
# ROCm and HIPify Setup
129
################################################################################

fbgemm_gpu/experimental/gen_ai/CMakeLists.txt

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,24 @@
44
# This source code is licensed under the BSD-style license found in the
55
# LICENSE file in the root directory of this source tree.
66

7+
################################################################################
8+
# CMake Prelude
9+
################################################################################
10+
11+
cmake_minimum_required(VERSION 3.21 FATAL_ERROR)
12+
13+
set(CMAKEMODULES ${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/modules)
14+
include(${CMAKEMODULES}/Utilities.cmake)
15+
include(${CMAKEMODULES}/GpuCppLibrary.cmake)
16+
17+
# CUDA Setup
18+
include(${CMAKEMODULES}/CudaSetup.cmake)
19+
20+
# ROCm and HIPify Setup
21+
include(${CMAKEMODULES}/RocmSetup.cmake)
22+
23+
set(CMAKE_VERBOSE_MAKEFILE ON)
24+
725
################################################################################
826
# Target Sources
927
################################################################################
@@ -91,24 +109,48 @@ list(FILTER experimental_gen_ai_cpp_source_files_hip
91109
# Build Shared Library
92110
################################################################################
93111

112+
# Customizations for PyTorch build integration
113+
IF(DEFINED CAFFE2_THIRD_PARTY_ROOT)
114+
# Remove "experimental" from the library name
115+
set(FBGEMM_GENAI_LIB_NAME fbgemm_gpu_gen_ai)
116+
# Keep lib prefix
117+
set(KEEP_PREFIX ON)
118+
# Install the library in the build directory
119+
set(DESTINATION ${CMAKE_INSTALL_LIBDIR})
120+
else()
121+
set(FBGEMM_GENAI_LIB_NAME fbgemm_gpu_experimental_gen_ai)
122+
set(KEEP_PREFIX OFF)
123+
set(DESTINATION "")
124+
endif()
125+
94126
gpu_cpp_library(
95127
PREFIX
96-
fbgemm_gpu_experimental_gen_ai
128+
${FBGEMM_GENAI_LIB_NAME}
97129
TYPE
98130
SHARED
131+
DESTINATION
132+
${DESTINATION}
133+
KEEP_PREFIX
134+
${KEEP_PREFIX}
99135
INCLUDE_DIRS
100136
${fbgemm_sources_include_directories}
101137
${CMAKE_CURRENT_SOURCE_DIR}/src/quantize
102138
${CMAKE_CURRENT_SOURCE_DIR}/src/quantize/common/include
103139
${CMAKE_CURRENT_SOURCE_DIR}/src/kv_cache
140+
${CMAKE_CURRENT_SOURCE_DIR}/../../include
141+
${FBGEMM_GENAI_INCLUDE_DIRS}
104142
CPU_SRCS
105143
${experimental_gen_ai_cpp_source_files_cpu}
106144
GPU_SRCS
107145
${experimental_gen_ai_cpp_source_files_gpu}
108146
CUDA_SPECIFIC_SRCS
109147
${experimental_gen_ai_cpp_source_files_cuda}
110148
HIP_SPECIFIC_SRCS
111-
${experimental_gen_ai_cpp_source_files_hip})
149+
${experimental_gen_ai_cpp_source_files_hip}
150+
TORCH_LIBS
151+
# Used when building as part of PyTorch
152+
${FBGEMM_GENAI_TORCH_LIBS})
153+
112154

113155

114156
################################################################################
@@ -117,7 +159,7 @@ gpu_cpp_library(
117159

118160
add_to_package(
119161
DESTINATION fbgemm_gpu/experimental/gen_ai
120-
TARGETS fbgemm_gpu_experimental_gen_ai)
162+
TARGETS ${FBGEMM_GENAI_LIB_NAME})
121163

122164
install(
123165
DIRECTORY bench

0 commit comments

Comments
 (0)