Skip to content

Commit

Permalink
[v1.8.0] Various CUDA 11.1 with BUILD_SPLIT_CUDA_FIXES (#52518)
Browse files Browse the repository at this point in the history
Co-authored-by: Nikita Shulga <nshulga@fb.com>
Co-authored-by: peterjc123 <peterghost86@gmail.com>
Co-authored-by: Jane Xu <janeyx@fb.com>
  • Loading branch information
4 people committed Feb 19, 2021
1 parent 0851cc4 commit f8afb8b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
33 changes: 21 additions & 12 deletions caffe2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,10 @@ file(WRITE ${DUMMY_EMPTY_FILE} ${DUMMY_FILE_CONTENT})
# Wrapper library for people who link against torch and expect both CPU and CUDA support
# Contains "torch_cpu" and "torch_cuda"
add_library(torch ${DUMMY_EMPTY_FILE})
if(BUILD_SPLIT_CUDA)
# When we split torch_cuda, we want a dummy torch_cuda library that contains both parts
add_library(torch_cuda ${DUMMY_EMPTY_FILE})
endif()
if(HAVE_SOVERSION)
set_target_properties(torch PROPERTIES
VERSION ${TORCH_VERSION} SOVERSION ${TORCH_SOVERSION})
Expand Down Expand Up @@ -1237,34 +1241,39 @@ endif()

caffe2_interface_library(torch_cpu torch_cpu_library)

if(BUILD_SPLIT_CUDA)
caffe2_interface_library(torch_cuda_cu torch_cuda_cu_library)
caffe2_interface_library(torch_cuda_cpp torch_cuda_cpp_library)
elseif(USE_CUDA)
if(USE_CUDA)
caffe2_interface_library(torch_cuda torch_cuda_library)
if(BUILD_SPLIT_CUDA)
caffe2_interface_library(torch_cuda_cu torch_cuda_cu_library)
caffe2_interface_library(torch_cuda_cpp torch_cuda_cpp_library)
endif()
elseif(USE_ROCM)
caffe2_interface_library(torch_hip torch_hip_library)
endif()

caffe2_interface_library(torch torch_library)

install(TARGETS torch_cpu torch_cpu_library EXPORT Caffe2Targets DESTINATION "${TORCH_INSTALL_LIB_DIR}")
if(BUILD_SPLIT_CUDA)
install(TARGETS torch_cuda_cu torch_cuda_cu_library EXPORT Caffe2Targets DESTINATION "${TORCH_INSTALL_LIB_DIR}")
install(TARGETS torch_cuda_cpp torch_cuda_cpp_library EXPORT Caffe2Targets DESTINATION "${TORCH_INSTALL_LIB_DIR}")
elseif(USE_CUDA)

if(USE_CUDA)
install(TARGETS torch_cuda torch_cuda_library EXPORT Caffe2Targets DESTINATION "${TORCH_INSTALL_LIB_DIR}")
if(BUILD_SPLIT_CUDA)
install(TARGETS torch_cuda_cu torch_cuda_cu_library EXPORT Caffe2Targets DESTINATION "${TORCH_INSTALL_LIB_DIR}")
install(TARGETS torch_cuda_cpp torch_cuda_cpp_library EXPORT Caffe2Targets DESTINATION "${TORCH_INSTALL_LIB_DIR}")
endif()
elseif(USE_ROCM)
install(TARGETS torch_hip torch_hip_library EXPORT Caffe2Targets DESTINATION "${TORCH_INSTALL_LIB_DIR}")
endif()
install(TARGETS torch torch_library EXPORT Caffe2Targets DESTINATION "${TORCH_INSTALL_LIB_DIR}")

target_link_libraries(torch PUBLIC torch_cpu_library)
if(BUILD_SPLIT_CUDA)
target_link_libraries(torch PUBLIC torch_cuda_cu_library)
target_link_libraries(torch PUBLIC torch_cuda_cpp_library)
elseif(USE_CUDA)

if(USE_CUDA)
target_link_libraries(torch PUBLIC torch_cuda_library)
if(BUILD_SPLIT_CUDA)
target_link_libraries(torch_cuda PUBLIC torch_cuda_cu_library)
target_link_libraries(torch_cuda PUBLIC torch_cuda_cpp_library)
endif()
elseif(USE_ROCM)
target_link_libraries(torch PUBLIC torch_hip_library)
endif()
Expand Down
7 changes: 6 additions & 1 deletion cmake/public/cuda.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,13 @@ if(CAFFE2_STATIC_LINK_CUDA AND NOT WIN32)
set_property(
TARGET caffe2::cublas APPEND PROPERTY INTERFACE_LINK_LIBRARIES
"${CUDA_TOOLKIT_ROOT_DIR}/lib64/libcublasLt_static.a")
# Add explicit dependency to cudart_static to fix
# libcublasLt_static.a.o): undefined reference to symbol 'cudaStreamWaitEvent'
# error adding symbols: DSO missing from command line
set_property(
TARGET caffe2::cublas APPEND PROPERTY INTERFACE_LINK_LIBRARIES
"${CUDA_cudart_static_LIBRARY}" rt dl)
endif()
target_link_libraries(caffe2::cublas INTERFACE torch::cudart)
else()
set_property(
TARGET caffe2::cublas PROPERTY INTERFACE_LINK_LIBRARIES
Expand Down
7 changes: 6 additions & 1 deletion torch/utils/cpp_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,21 @@
from setuptools.command.build_ext import build_ext
from pkg_resources import packaging # type: ignore

BUILD_SPLIT_CUDA = os.getenv('BUILD_SPLIT_CUDA')
IS_WINDOWS = sys.platform == 'win32'
LIB_EXT = '.pyd' if IS_WINDOWS else '.so'
EXEC_EXT = '.exe' if IS_WINDOWS else ''
CLIB_PREFIX = '' if IS_WINDOWS else 'lib'
CLIB_EXT = '.dll' if IS_WINDOWS else '.so'
SHARED_FLAG = '/DLL' if IS_WINDOWS else '-shared'

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


BUILD_SPLIT_CUDA = os.getenv('BUILD_SPLIT_CUDA') or (os.path.exists(os.path.join(
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}')))

# Taken directly from python stdlib < 3.9
# See https://github.com/pytorch/pytorch/issues/48617
def _nt_quote_args(args: Optional[List[str]]) -> List[str]:
Expand Down

0 comments on commit f8afb8b

Please sign in to comment.