Skip to content

Commit f8afb8b

Browse files
seemetheremalfetpeterjc123janeyx99
authored
[v1.8.0] Various CUDA 11.1 with BUILD_SPLIT_CUDA_FIXES (pytorch#52518)
Co-authored-by: Nikita Shulga <[email protected]> Co-authored-by: peterjc123 <[email protected]> Co-authored-by: Jane Xu <[email protected]>
1 parent 0851cc4 commit f8afb8b

File tree

3 files changed

+33
-14
lines changed

3 files changed

+33
-14
lines changed

caffe2/CMakeLists.txt

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,10 @@ file(WRITE ${DUMMY_EMPTY_FILE} ${DUMMY_FILE_CONTENT})
745745
# Wrapper library for people who link against torch and expect both CPU and CUDA support
746746
# Contains "torch_cpu" and "torch_cuda"
747747
add_library(torch ${DUMMY_EMPTY_FILE})
748+
if(BUILD_SPLIT_CUDA)
749+
# When we split torch_cuda, we want a dummy torch_cuda library that contains both parts
750+
add_library(torch_cuda ${DUMMY_EMPTY_FILE})
751+
endif()
748752
if(HAVE_SOVERSION)
749753
set_target_properties(torch PROPERTIES
750754
VERSION ${TORCH_VERSION} SOVERSION ${TORCH_SOVERSION})
@@ -1237,34 +1241,39 @@ endif()
12371241

12381242
caffe2_interface_library(torch_cpu torch_cpu_library)
12391243

1240-
if(BUILD_SPLIT_CUDA)
1241-
caffe2_interface_library(torch_cuda_cu torch_cuda_cu_library)
1242-
caffe2_interface_library(torch_cuda_cpp torch_cuda_cpp_library)
1243-
elseif(USE_CUDA)
1244+
if(USE_CUDA)
12441245
caffe2_interface_library(torch_cuda torch_cuda_library)
1246+
if(BUILD_SPLIT_CUDA)
1247+
caffe2_interface_library(torch_cuda_cu torch_cuda_cu_library)
1248+
caffe2_interface_library(torch_cuda_cpp torch_cuda_cpp_library)
1249+
endif()
12451250
elseif(USE_ROCM)
12461251
caffe2_interface_library(torch_hip torch_hip_library)
12471252
endif()
12481253

12491254
caffe2_interface_library(torch torch_library)
12501255

12511256
install(TARGETS torch_cpu torch_cpu_library EXPORT Caffe2Targets DESTINATION "${TORCH_INSTALL_LIB_DIR}")
1252-
if(BUILD_SPLIT_CUDA)
1253-
install(TARGETS torch_cuda_cu torch_cuda_cu_library EXPORT Caffe2Targets DESTINATION "${TORCH_INSTALL_LIB_DIR}")
1254-
install(TARGETS torch_cuda_cpp torch_cuda_cpp_library EXPORT Caffe2Targets DESTINATION "${TORCH_INSTALL_LIB_DIR}")
1255-
elseif(USE_CUDA)
1257+
1258+
if(USE_CUDA)
12561259
install(TARGETS torch_cuda torch_cuda_library EXPORT Caffe2Targets DESTINATION "${TORCH_INSTALL_LIB_DIR}")
1260+
if(BUILD_SPLIT_CUDA)
1261+
install(TARGETS torch_cuda_cu torch_cuda_cu_library EXPORT Caffe2Targets DESTINATION "${TORCH_INSTALL_LIB_DIR}")
1262+
install(TARGETS torch_cuda_cpp torch_cuda_cpp_library EXPORT Caffe2Targets DESTINATION "${TORCH_INSTALL_LIB_DIR}")
1263+
endif()
12571264
elseif(USE_ROCM)
12581265
install(TARGETS torch_hip torch_hip_library EXPORT Caffe2Targets DESTINATION "${TORCH_INSTALL_LIB_DIR}")
12591266
endif()
12601267
install(TARGETS torch torch_library EXPORT Caffe2Targets DESTINATION "${TORCH_INSTALL_LIB_DIR}")
12611268

12621269
target_link_libraries(torch PUBLIC torch_cpu_library)
1263-
if(BUILD_SPLIT_CUDA)
1264-
target_link_libraries(torch PUBLIC torch_cuda_cu_library)
1265-
target_link_libraries(torch PUBLIC torch_cuda_cpp_library)
1266-
elseif(USE_CUDA)
1270+
1271+
if(USE_CUDA)
12671272
target_link_libraries(torch PUBLIC torch_cuda_library)
1273+
if(BUILD_SPLIT_CUDA)
1274+
target_link_libraries(torch_cuda PUBLIC torch_cuda_cu_library)
1275+
target_link_libraries(torch_cuda PUBLIC torch_cuda_cpp_library)
1276+
endif()
12681277
elseif(USE_ROCM)
12691278
target_link_libraries(torch PUBLIC torch_hip_library)
12701279
endif()

cmake/public/cuda.cmake

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,13 @@ if(CAFFE2_STATIC_LINK_CUDA AND NOT WIN32)
352352
set_property(
353353
TARGET caffe2::cublas APPEND PROPERTY INTERFACE_LINK_LIBRARIES
354354
"${CUDA_TOOLKIT_ROOT_DIR}/lib64/libcublasLt_static.a")
355+
# Add explicit dependency to cudart_static to fix
356+
# libcublasLt_static.a.o): undefined reference to symbol 'cudaStreamWaitEvent'
357+
# error adding symbols: DSO missing from command line
358+
set_property(
359+
TARGET caffe2::cublas APPEND PROPERTY INTERFACE_LINK_LIBRARIES
360+
"${CUDA_cudart_static_LIBRARY}" rt dl)
355361
endif()
356-
target_link_libraries(caffe2::cublas INTERFACE torch::cudart)
357362
else()
358363
set_property(
359364
TARGET caffe2::cublas PROPERTY INTERFACE_LINK_LIBRARIES

torch/utils/cpp_extension.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,21 @@
2222
from setuptools.command.build_ext import build_ext
2323
from pkg_resources import packaging # type: ignore
2424

25-
BUILD_SPLIT_CUDA = os.getenv('BUILD_SPLIT_CUDA')
2625
IS_WINDOWS = sys.platform == 'win32'
2726
LIB_EXT = '.pyd' if IS_WINDOWS else '.so'
2827
EXEC_EXT = '.exe' if IS_WINDOWS else ''
28+
CLIB_PREFIX = '' if IS_WINDOWS else 'lib'
29+
CLIB_EXT = '.dll' if IS_WINDOWS else '.so'
2930
SHARED_FLAG = '/DLL' if IS_WINDOWS else '-shared'
3031

3132
_HERE = os.path.abspath(__file__)
3233
_TORCH_PATH = os.path.dirname(os.path.dirname(_HERE))
3334
TORCH_LIB_PATH = os.path.join(_TORCH_PATH, 'lib')
3435

36+
37+
BUILD_SPLIT_CUDA = os.getenv('BUILD_SPLIT_CUDA') or (os.path.exists(os.path.join(
38+
TORCH_LIB_PATH, f'{CLIB_PREFIX}torch_cuda_cu{CLIB_EXT}')) and os.path.exists(os.path.join(TORCH_LIB_PATH, f'{CLIB_PREFIX}torch_cuda_cpp{CLIB_EXT}')))
39+
3540
# Taken directly from python stdlib < 3.9
3641
# See https://github.com/pytorch/pytorch/issues/48617
3742
def _nt_quote_args(args: Optional[List[str]]) -> List[str]:

0 commit comments

Comments
 (0)