Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Split Build] option to build c10 using seperate wheel #125964

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ endif()

# This define is needed to preserve behavior given anticpated changes to cccl/thrust
# https://nvidia.github.io/libcudacxx/standard_api/numerics_library/complex.html
string(APPEND CMAKE_CUDA_FLAGS " -DLIBCUDACXX_ENABLE_SIMPLIFIED_COMPLEX_OPERATIONS")
string(APPEND CMAKE_CUDA_FLAGS "-DLIBCUDACXX_ENABLE_SIMPLIFIED_COMPLEX_OPERATIONS")

if(LINUX)
include(cmake/CheckAbi.cmake)
Expand Down Expand Up @@ -228,9 +228,12 @@ option(USE_FBGEMM "Use FBGEMM (quantized 8-bit server operators)" ON)
option(USE_KINETO "Use Kineto profiling library" ON)
option(USE_CUPTI_SO "Use CUPTI as a shared library" ON)
option(USE_FAKELOWP "Use FakeLowp operators" OFF)
option(USE_FFMPEG "Use ffmpeg" OFF)
option(USE_GFLAGS "Use GFLAGS" OFF)
option(USE_GLOG "Use GLOG" OFF)
option(USE_LEVELDB "Use LEVELDB" OFF)
option(USE_LITE_PROTO "Use lite protobuf instead of full." OFF)
option(USE_LMDB "Use LMDB" OFF)
option(USE_MAGMA "Use MAGMA" ON)
option(USE_METAL "Use Metal for Caffe2 iOS build" ON)
option(USE_PYTORCH_METAL "Use Metal for PyTorch iOS build" OFF)
Expand Down Expand Up @@ -261,12 +264,15 @@ cmake_dependent_option(
option(USE_NUMPY "Use NumPy" ON)
option(USE_OBSERVERS "Use observers module." OFF)
option(USE_OPENCL "Use OpenCL" OFF)
option(USE_OPENCV "Use OpenCV" OFF)
option(USE_OPENMP "Use OpenMP for parallel code" ON)
option(USE_PRECOMPILED_HEADERS "Use pre-compiled headers to accelerate build." OFF)

option(USE_PROF "Use profiling" OFF)
option(USE_QNNPACK "Use QNNPACK (quantized 8-bit operators)" ON)
option(USE_PYTORCH_QNNPACK "Use ATen/QNNPACK (quantized 8-bit operators)" ON)
option(USE_REDIS "Use Redis" OFF)
option(USE_ROCKSDB "Use RocksDB" OFF)
option(USE_SNPE "Use Qualcomm's SNPE library" OFF)
option(USE_SYSTEM_EIGEN_INSTALL
"Use system Eigen instead of the one under third_party" OFF)
Expand All @@ -288,6 +294,7 @@ option(USE_VULKAN_FP16_INFERENCE "Vulkan - Use fp16 inference" OFF)
option(USE_VULKAN_RELAXED_PRECISION "Vulkan - Use relaxed precision math in the kernels (mediump)" OFF)
# option USE_XNNPACK: try to enable xnnpack by default.
option(USE_XNNPACK "Use XNNPACK" ON)
option(USE_ZMQ "Use ZMQ" OFF)
option(USE_ZSTD "Use ZSTD" OFF)
option(USE_ROCM_KERNEL_ASSERT "Use Kernel Assert for ROCm" OFF)
# Ensure that an ITT build is the default for x86 CPUs
Expand Down Expand Up @@ -1253,7 +1260,11 @@ if(DEFINED USE_CUSTOM_DEBINFO)
foreach(SOURCE_FILE ${SOURCE_FILES_LIST})
# We have to specify the scope here. We do this by specifying the
# targets we care about and caffe2/ for all test targets defined there
set(ALL_PT_TARGETS "torch_python;c10;torch_cpu;torch")
if(BUILD_LIBTORCHLESS)
set(ALL_PT_TARGETS "torch_python;${C10_LIB};torch_cpu;torch")
else()
set(ALL_PT_TARGETS "torch_python;c10;torch_cpu;torch")
endif()
set_source_files_properties(${SOURCE_FILE} DIRECTORY "caffe2/" TARGET_DIRECTORY ${ALL_PT_TARGETS} PROPERTIES COMPILE_FLAGS "-g")
endforeach()

Expand Down
226 changes: 118 additions & 108 deletions c10/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,120 +12,127 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# protobuf header files, because protobuf header files will transitively force
# one to link against a specific protobuf version.

# ---[ Configure macro file.
set(C10_USE_GFLAGS ${USE_GFLAGS}) # used in cmake_macros.h.in
set(C10_USE_GLOG ${USE_GLOG}) # used in cmake_macros.h.in
set(C10_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS}) # used in cmake_macros.h.in
set(C10_USE_NUMA ${USE_NUMA})
set(C10_USE_MSVC_STATIC_RUNTIME ${CAFFE2_USE_MSVC_STATIC_RUNTIME})
set(C10_USE_ROCM_KERNEL_ASSERT ${USE_ROCM_KERNEL_ASSERT})
configure_file(
${CMAKE_CURRENT_LIST_DIR}/macros/cmake_macros.h.in
${CMAKE_BINARY_DIR}/c10/macros/cmake_macros.h)

# Note: if you want to add ANY dependency to the c10 library, make sure you
# check with the core PyTorch developers as the dependency will be
# transitively passed on to all libraries dependent on PyTorch.
file(GLOB C10_SRCS
*.cpp
core/*.cpp
core/impl/*.cpp
mobile/*.cpp
macros/*.cpp
util/*.cpp
)
file(GLOB C10_HEADERS
*.h
core/*.h
core/impl/*.h
mobile/*.h
macros/*.h
util/*.h
)
add_library(c10 ${C10_SRCS} ${C10_HEADERS})
target_compile_options_if_supported(c10 "-Wdeprecated")
if(HAVE_SOVERSION)
set_target_properties(c10 PROPERTIES
VERSION ${TORCH_VERSION} SOVERSION ${TORCH_SOVERSION})
endif()
# If building shared library, set dllimport/dllexport proper.
target_compile_options(c10 PRIVATE "-DC10_BUILD_MAIN_LIB")
# Enable hidden visibility if compiler supports it.
if(${COMPILER_SUPPORTS_HIDDEN_VISIBILITY})
target_compile_options(c10 PRIVATE "-fvisibility=hidden")
if(BUILD_LIBTORCHLESS)
find_library(C10_LIB c10 PATHS $ENV{LIBTORCH_LIB_PATH} NO_DEFAULT_PATH)
endif()

option(C10_USE_IWYU "Use include-what-you-use to clean up header inclusion" OFF)
if(C10_USE_IWYU)
find_program(iwyu NAMES include-what-you-use)
if(iwyu)
set(iwyu_cmd
"include-what-you-use"
"-Xiwyu"
"--transitive_includes_only"
"-Xiwyu"
"--no_fwd_decls"
"-Xiwyu"
"--prefix_header_includes=keep"
"-Xiwyu"
"--mapping_file=${CMAKE_CURRENT_LIST_DIR}/../tools/iwyu/all.imp"
)
set_property(TARGET c10 PROPERTY CXX_INCLUDE_WHAT_YOU_USE ${iwyu_cmd})
# ---[ Configure macro file.
set(C10_USE_GFLAGS ${USE_GFLAGS}) # used in cmake_macros.h.in
set(C10_USE_GLOG ${USE_GLOG}) # used in cmake_macros.h.in
set(C10_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS}) # used in cmake_macros.h.in
set(C10_USE_NUMA ${USE_NUMA})
set(C10_USE_MSVC_STATIC_RUNTIME ${CAFFE2_USE_MSVC_STATIC_RUNTIME})
set(C10_USE_ROCM_KERNEL_ASSERT ${USE_ROCM_KERNEL_ASSERT})
configure_file(
${CMAKE_CURRENT_LIST_DIR}/macros/cmake_macros.h.in
${CMAKE_BINARY_DIR}/c10/macros/cmake_macros.h)

# Note: if you want to add ANY dependency to the c10 library, make sure you
# check with the core PyTorch developers as the dependency will be
# transitively passed on to all libraries dependent on PyTorch.
file(GLOB C10_SRCS
*.cpp
core/*.cpp
core/impl/*.cpp
mobile/*.cpp
macros/*.cpp
util/*.cpp
)
file(GLOB C10_HEADERS
*.h
core/*.h
core/impl/*.h
mobile/*.h
macros/*.h
util/*.h
)
if(NOT BUILD_LIBTORCHLESS)
add_library(c10 ${C10_SRCS} ${C10_HEADERS})
target_compile_options_if_supported(c10 "-Wdeprecated")
if(HAVE_SOVERSION)
set_target_properties(c10 PROPERTIES
VERSION ${TORCH_VERSION} SOVERSION ${TORCH_SOVERSION})
endif()
# If building shared library, set dllimport/dllexport proper.
target_compile_options(c10 PRIVATE "-DC10_BUILD_MAIN_LIB")
# Enable hidden visibility if compiler supports it.
if(${COMPILER_SUPPORTS_HIDDEN_VISIBILITY})
target_compile_options(c10 PRIVATE "-fvisibility=hidden")
endif()
endif()

if(WERROR)
target_compile_options_if_supported(c10 PRIVATE "-Werror=sign-compare")
target_compile_options_if_supported(c10 PRIVATE "-Werror=shadow")
endif()
option(C10_USE_IWYU "Use include-what-you-use to clean up header inclusion" OFF)
if(C10_USE_IWYU)
find_program(iwyu NAMES include-what-you-use)
if(iwyu)
set(iwyu_cmd
"include-what-you-use"
"-Xiwyu"
"--transitive_includes_only"
"-Xiwyu"
"--no_fwd_decls"
"-Xiwyu"
"--prefix_header_includes=keep"
"-Xiwyu"
"--mapping_file=${CMAKE_CURRENT_LIST_DIR}/../tools/iwyu/all.imp"
)
set_property(TARGET c10 PROPERTY CXX_INCLUDE_WHAT_YOU_USE ${iwyu_cmd})
endif()
endif()

# ---[ Dependency of c10
if(C10_USE_GFLAGS)
target_link_libraries(c10 PUBLIC gflags)
endif()
if(WERROR)
target_compile_options_if_supported(c10 PRIVATE "-Werror=sign-compare")
target_compile_options_if_supported(c10 PRIVATE "-Werror=shadow")
endif()

if(C10_USE_GLOG)
target_link_libraries(c10 PUBLIC glog::glog)
endif()
target_link_libraries(c10 PRIVATE fmt::fmt-header-only)

if(C10_USE_NUMA)
message(STATUS "NUMA paths:")
message(STATUS ${Numa_INCLUDE_DIR})
message(STATUS ${Numa_LIBRARIES})
target_include_directories(c10 PRIVATE ${Numa_INCLUDE_DIR})
target_link_libraries(c10 PRIVATE ${Numa_LIBRARIES})
else()
message(STATUS "don't use NUMA")
endif()
# ---[ Dependency of c10
if(C10_USE_GFLAGS)
target_link_libraries(c10 PUBLIC gflags)
endif()

if(NOT CMAKE_SYSTEM_PROCESSOR MATCHES "s390x" AND NOT CMAKE_SYSTEM_PROCESSOR MATCHES "ppc64le")
target_link_libraries(c10 PRIVATE cpuinfo)
endif()
if(C10_USE_GLOG)
target_link_libraries(c10 PUBLIC glog::glog)
endif()
target_link_libraries(c10 PRIVATE fmt::fmt-header-only)

if(C10_USE_NUMA)
message(STATUS "NUMA paths:")
message(STATUS ${Numa_INCLUDE_DIR})
message(STATUS ${Numa_LIBRARIES})
target_include_directories(c10 PRIVATE ${Numa_INCLUDE_DIR})
target_link_libraries(c10 PRIVATE ${Numa_LIBRARIES})
else()
message(STATUS "don't use NUMA")
endif()

find_package(Backtrace)
if(Backtrace_FOUND)
target_include_directories(c10 PRIVATE ${Backtrace_INCLUDE_DIRS})
target_link_libraries(c10 PRIVATE ${Backtrace_LIBRARIES})
target_compile_definitions(c10 PRIVATE SUPPORTS_BACKTRACE=1)
else()
target_compile_definitions(c10 PRIVATE SUPPORTS_BACKTRACE=0)
endif()
if(NOT CMAKE_SYSTEM_PROCESSOR MATCHES "s390x" AND NOT CMAKE_SYSTEM_PROCESSOR MATCHES "ppc64le")
target_link_libraries(c10 PRIVATE cpuinfo)
endif()

if(USE_MIMALLOC)
target_link_libraries(c10 PRIVATE "mimalloc-static")
add_dependencies(c10 mimalloc-static)
endif()
find_package(Backtrace)
if(Backtrace_FOUND)
target_include_directories(c10 PRIVATE ${Backtrace_INCLUDE_DIRS})
target_link_libraries(c10 PRIVATE ${Backtrace_LIBRARIES})
target_compile_definitions(c10 PRIVATE SUPPORTS_BACKTRACE=1)
else()
target_compile_definitions(c10 PRIVATE SUPPORTS_BACKTRACE=0)
endif()

if(ANDROID)
target_link_libraries(c10 PRIVATE log)
endif()
if(USE_MIMALLOC)
target_link_libraries(c10 PRIVATE "mimalloc-static")
add_dependencies(c10 mimalloc-static)
endif()

if(ANDROID)
target_link_libraries(c10 PRIVATE log)
endif()

target_include_directories(
c10 PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../>
$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}>
$<INSTALL_INTERFACE:include>)
target_include_directories(
c10 PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../>
$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}>
$<INSTALL_INTERFACE:include>)
set(C10_LIB c10)
endif()

add_subdirectory(test)
add_subdirectory(benchmark)
Expand All @@ -144,11 +151,14 @@ if(USE_XPU)
add_subdirectory(xpu)
endif()

# ---[ Installation
# Note: for now, we will put all export path into one single Caffe2Targets group
# to deal with the cmake deployment need. Inside the Caffe2Targets set, the
# individual libraries like libc10.so and libcaffe2.so are still self-contained.
install(TARGETS c10 EXPORT Caffe2Targets DESTINATION lib)
if(NOT BUILD_LIBTORCHLESS)
# ---[ Installation
# Note: for now, we will put all export path into one single Caffe2Targets group
# to deal with the cmake deployment need. Inside the Caffe2Targets set, the
# individual libraries like libc10.so and libcaffe2.so are still self-contained.
install(TARGETS c10 EXPORT Caffe2Targets DESTINATION lib)
endif()

install(DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
DESTINATION include
FILES_MATCHING PATTERN "*.h")
Expand Down
2 changes: 1 addition & 1 deletion c10/benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ if(BUILD_TEST)
get_filename_component(bench_file_name ${bench_src} NAME_WE)
set(bench_name "c10_${bench_file_name}")
add_executable(${bench_name} "${bench_src}")
target_link_libraries(${bench_name} c10 benchmark)
target_link_libraries(${bench_name} ${C10_LIB} benchmark)
if(INSTALL_TEST)
install(TARGETS ${bench_name} DESTINATION test)
endif()
Expand Down
50 changes: 30 additions & 20 deletions c10/cuda/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ configure_file(
${CMAKE_CURRENT_LIST_DIR}/impl/cuda_cmake_macros.h.in
${CMAKE_BINARY_DIR}/c10/cuda/impl/cuda_cmake_macros.h)

if(BUILD_LIBTORCHLESS)
find_library(C10_CUDA_LIB c10_cuda PATHS $ENV{LIBTORCH_LIB_PATH} NO_DEFAULT_PATH)
endif()

# Note: if you want to add ANY dependency to the c10 library, make sure you
# check with the core PyTorch developers as the dependency will be
# transitively passed on to all libraries dependent on PyTorch.
Expand Down Expand Up @@ -47,36 +51,42 @@ set(C10_CUDA_HEADERS
impl/CUDATest.h
)
set(CUDA_LINK_LIBRARIES_KEYWORD PRIVATE)
torch_cuda_based_add_library(c10_cuda ${C10_CUDA_SRCS} ${C10_CUDA_HEADERS})
set(CUDA_LINK_LIBRARIES_KEYWORD)
# If building shared library, set dllimport/dllexport proper.
target_compile_options(c10_cuda PRIVATE "-DC10_CUDA_BUILD_MAIN_LIB")
# Enable hidden visibility if compiler supports it.
if(${COMPILER_SUPPORTS_HIDDEN_VISIBILITY})
target_compile_options(c10_cuda PRIVATE "-fvisibility=hidden")
endif()

# ---[ Dependency of c10_cuda
target_link_libraries(c10_cuda PUBLIC c10 torch::cudart)
if(NOT BUILD_LIBTORCHLESS)
torch_cuda_based_add_library(c10_cuda ${C10_CUDA_SRCS} ${C10_CUDA_HEADERS})
set(CUDA_LINK_LIBRARIES_KEYWORD)
# If building shared library, set dllimport/dllexport proper.
target_compile_options(c10_cuda PRIVATE "-DC10_CUDA_BUILD_MAIN_LIB")
# Enable hidden visibility if compiler supports it.
if(${COMPILER_SUPPORTS_HIDDEN_VISIBILITY})
target_compile_options(c10_cuda PRIVATE "-fvisibility=hidden")
endif()

if(NOT WIN32)
target_link_libraries(c10_cuda PRIVATE dl)
target_compile_options(c10_cuda PRIVATE "-DPYTORCH_C10_DRIVER_API_SUPPORTED")
endif()
# ---[ Dependency of c10_cuda
target_link_libraries(c10_cuda PUBLIC ${C10_LIB} torch::cudart)

target_include_directories(
c10_cuda PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../..>
$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}>
$<INSTALL_INTERFACE:include>)
if(NOT WIN32)
target_link_libraries(c10_cuda PRIVATE dl)
target_compile_options(c10_cuda PRIVATE "-DPYTORCH_C10_DRIVER_API_SUPPORTED")
endif()

add_subdirectory(test)
target_include_directories(
c10_cuda PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../..>
$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}>
$<INSTALL_INTERFACE:include>)
set(C10_CUDA_LIB c10_cuda)

# ---[ Installation
# Note: for now, we will put all export path into one single Caffe2Targets group
# to deal with the cmake deployment need. Inside the Caffe2Targets set, the
# individual libraries like libc10.so and libcaffe2.so are still self-contained.
install(TARGETS c10_cuda EXPORT Caffe2Targets DESTINATION lib)

endif()

add_subdirectory(test)

foreach(file ${C10_CUDA_HEADERS})
get_filename_component( dir ${file} DIRECTORY )
install( FILES ${file} DESTINATION include/c10/cuda/${dir} )
Expand Down
Loading
Loading