From 22c012faede99bc063711facfff52673d8777501 Mon Sep 17 00:00:00 2001 From: PaliC Date: Fri, 10 May 2024 14:21:24 -0700 Subject: [PATCH] Update [ghstack-poisoned] --- CMakeLists.txt | 15 ++- c10/CMakeLists.txt | 226 +++++++++++++++++--------------- c10/benchmark/CMakeLists.txt | 2 +- c10/cuda/CMakeLists.txt | 50 ++++--- c10/cuda/test/CMakeLists.txt | 2 +- c10/hip/CMakeLists.txt | 48 ++++--- c10/test/CMakeLists.txt | 2 +- c10/xpu/CMakeLists.txt | 14 +- c10/xpu/test/CMakeLists.txt | 2 +- caffe2/CMakeLists.txt | 33 ++++- setup.py | 26 +++- torch/lib/libshm/CMakeLists.txt | 2 +- 12 files changed, 257 insertions(+), 165 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d61f3a7619990..4a1bda1e07f55 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) @@ -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) @@ -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) @@ -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 @@ -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() diff --git a/c10/CMakeLists.txt b/c10/CMakeLists.txt index 1f742f4c17683..a2f78538787d2 100644 --- a/c10/CMakeLists.txt +++ b/c10/CMakeLists.txt @@ -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 - $ - $ - $) + target_include_directories( + c10 PUBLIC + $ + $ + $) + set(C10_LIB c10) +endif() add_subdirectory(test) add_subdirectory(benchmark) @@ -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") diff --git a/c10/benchmark/CMakeLists.txt b/c10/benchmark/CMakeLists.txt index 9658e60376167..16b268e3800a0 100644 --- a/c10/benchmark/CMakeLists.txt +++ b/c10/benchmark/CMakeLists.txt @@ -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() diff --git a/c10/cuda/CMakeLists.txt b/c10/cuda/CMakeLists.txt index c5c45c68d8f25..893a85562976d 100644 --- a/c10/cuda/CMakeLists.txt +++ b/c10/cuda/CMakeLists.txt @@ -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. @@ -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 - $ - $ - $) + 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 + $ + $ + $) + 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} ) diff --git a/c10/cuda/test/CMakeLists.txt b/c10/cuda/test/CMakeLists.txt index eed7fdff42ca1..26d2f5f13caa0 100644 --- a/c10/cuda/test/CMakeLists.txt +++ b/c10/cuda/test/CMakeLists.txt @@ -15,7 +15,7 @@ if(BUILD_TEST) get_filename_component(test_file_name ${test_src} NAME_WE) set(test_name "c10_cuda_${test_file_name}") add_executable(${test_name} "${test_src}") - target_link_libraries(${test_name} c10_cuda gtest_main) + target_link_libraries(${test_name} ${C10_CUDA_LIB} gtest_main) add_test(NAME ${test_name} COMMAND $) if(INSTALL_TEST) install(TARGETS ${test_name} DESTINATION test) diff --git a/c10/hip/CMakeLists.txt b/c10/hip/CMakeLists.txt index f4273ca2055c1..a6442e01d2e2e 100644 --- a/c10/hip/CMakeLists.txt +++ b/c10/hip/CMakeLists.txt @@ -5,6 +5,10 @@ include(../../cmake/public/utils.cmake) +if(BUILD_LIBTORCHLESS) + find_library(C10_HIP_LIB c10_hip PATHS $ENV{LIBTORCH_LIB_PATH} NO_DEFAULT_PATH) +endif() + # ---[ Configure macro file. set(C10_HIP_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS}) # used in cmake_macros.h.in configure_file( @@ -26,36 +30,40 @@ file(GLOB __c10_hip_srcs_cpp *.cc impl/*.cc) set_source_files_properties(${__c10_hip_srcs_cpp} PROPERTIES HIP_SOURCE_PROPERTY_FORMAT 1) file(GLOB_RECURSE C10_HIP_HEADERS *.h) -hip_add_library(c10_hip ${C10_HIP_SRCS} ${C10_HIP_HEADERS}) -# Propagate HIP_CXX_FLAGS that were set from Dependencies.cmake -target_compile_options(c10_hip PRIVATE ${HIP_CXX_FLAGS}) +if(NOT BUILD_LIBTORCHLESS) + hip_add_library(c10_hip ${C10_HIP_SRCS} ${C10_HIP_HEADERS}) -# caffe2_hip adds a bunch of dependencies like rocsparse, but c10/hip is supposed to be -# minimal. I'm not sure if we need hip_hcc or not; for now leave it out + # Propagate HIP_CXX_FLAGS that were set from Dependencies.cmake + target_compile_options(c10_hip PRIVATE ${HIP_CXX_FLAGS}) -# If building shared library, set dllimport/dllexport proper. -target_compile_options(c10_hip PRIVATE "-DC10_HIP_BUILD_MAIN_LIB") -# Enable hidden visibility if compiler supports it. -if(${COMPILER_SUPPORTS_HIDDEN_VISIBILITY}) - target_compile_options(c10_hip PRIVATE "-fvisibility=hidden") -endif() + # caffe2_hip adds a bunch of dependencies like rocsparse, but c10/hip is supposed to be + # minimal. I'm not sure if we need hip_hcc or not; for now leave it out -# ---[ Dependency of c10_hip -target_link_libraries(c10_hip PUBLIC c10) + # If building shared library, set dllimport/dllexport proper. + target_compile_options(c10_hip PRIVATE "-DC10_HIP_BUILD_MAIN_LIB") + # Enable hidden visibility if compiler supports it. + if(${COMPILER_SUPPORTS_HIDDEN_VISIBILITY}) + target_compile_options(c10_hip PRIVATE "-fvisibility=hidden") + endif() -target_link_libraries(c10_hip PUBLIC ${PYTORCH_HIP_LIBRARIES}) + # ---[ Dependency of c10_hip + target_link_libraries(c10_hip PUBLIC c10) -target_include_directories( - c10_hip PUBLIC - $ - $ - $) + target_link_libraries(c10_hip PUBLIC ${PYTORCH_HIP_LIBRARIES}) + + target_include_directories( + c10_hip PUBLIC + $ + $ + $) + install(TARGETS c10_hip EXPORT Caffe2Targets DESTINATION lib) + set(C10_HIP_LIB c10_hip) +endif() add_subdirectory(test) # ---[ Installation -install(TARGETS c10_hip EXPORT Caffe2Targets DESTINATION lib) install(DIRECTORY ${CMAKE_CURRENT_LIST_DIR} DESTINATION include FILES_MATCHING PATTERN "*.h") diff --git a/c10/test/CMakeLists.txt b/c10/test/CMakeLists.txt index a1ca4bb51b776..7f2a61246c6c6 100644 --- a/c10/test/CMakeLists.txt +++ b/c10/test/CMakeLists.txt @@ -9,7 +9,7 @@ if(BUILD_TEST) if(NOT MSVC) target_compile_options(${test_name} PRIVATE -Wno-unused-variable) endif() - target_link_libraries(${test_name} c10 gmock gtest gtest_main) + target_link_libraries(${test_name} ${C10_LIB} gmock gtest gtest_main) add_test(NAME ${test_name} COMMAND $) if(INSTALL_TEST) install(TARGETS ${test_name} DESTINATION test) diff --git a/c10/xpu/CMakeLists.txt b/c10/xpu/CMakeLists.txt index c14f1790d9d8d..d06d0f0aa92ab 100644 --- a/c10/xpu/CMakeLists.txt +++ b/c10/xpu/CMakeLists.txt @@ -4,6 +4,10 @@ include(../../cmake/public/xpu.cmake) +if(NOT BUILD_LIBTORCHLESS) + find_library(C10_XPU_LIB c10_xpu PATHS $ENV{LIBTORCH_LIB_PATH} NO_DEFAULT_PATH) +endif() + set(C10_XPU_SRCS XPUCachingAllocator.cpp XPUFunctions.cpp @@ -19,7 +23,7 @@ set(C10_XPU_HEADERS XPUStream.h impl/XPUGuardImpl.h ) - +if(NOT BUILD_LIBTORCHLESS) add_library(c10_xpu ${C10_XPU_SRCS} ${C10_XPU_HEADERS}) target_compile_options(c10_xpu PRIVATE "-DC10_XPU_BUILD_MAIN_LIB") # Enable hidden visibility if compiler supports it. @@ -35,11 +39,13 @@ target_include_directories( $ $ ) - -add_subdirectory(test) + install(TARGETS c10_xpu EXPORT Caffe2Targets DESTINATION lib) + set(C10_XPU_LIB c10_xpu) + add_subdirectory(test) +endif() # ---[ Installation -install(TARGETS c10_xpu EXPORT Caffe2Targets DESTINATION lib) + foreach(file ${C10_XPU_HEADERS}) get_filename_component(dir ${file} DIRECTORY) install(FILES ${file} DESTINATION include/c10/xpu/${dir}) diff --git a/c10/xpu/test/CMakeLists.txt b/c10/xpu/test/CMakeLists.txt index fa91cc9d171a3..0f0c85c68c8f8 100644 --- a/c10/xpu/test/CMakeLists.txt +++ b/c10/xpu/test/CMakeLists.txt @@ -11,7 +11,7 @@ if(BUILD_TEST) get_filename_component(test_file_name ${test_src} NAME_WE) set(test_name "c10_xpu_${test_file_name}") add_executable(${test_name} "${test_src}") - target_link_libraries(${test_name} c10_xpu gtest_main) + target_link_libraries(${test_name} ${C10_XPU_LIB} gtest_main) add_test(NAME ${test_name} COMMAND $) if(INSTALL_TEST) install(TARGETS ${test_name} DESTINATION test) diff --git a/caffe2/CMakeLists.txt b/caffe2/CMakeLists.txt index 569bc010f40fd..d0ebebd3d5fe7 100644 --- a/caffe2/CMakeLists.txt +++ b/caffe2/CMakeLists.txt @@ -1483,7 +1483,13 @@ if($ENV{TH_BINARY_BUILD}) endif() endif() -target_link_libraries(torch_cpu PUBLIC c10) +if(BUILD_LIBTORCHLESS) + target_link_libraries(torch_cpu PUBLIC ${C10_LIB}) +else() + # unclear why we need to use c10 here otherwise c10 symbols are not included + target_link_libraries(torch_cpu PUBLIC c10) +endif() + target_link_libraries(torch_cpu PUBLIC ${Caffe2_PUBLIC_DEPENDENCY_LIBS}) target_link_libraries(torch_cpu PRIVATE ${Caffe2_DEPENDENCY_LIBS}) target_link_libraries(torch_cpu PRIVATE ${Caffe2_DEPENDENCY_WHOLE_LINK_LIBS}) @@ -1646,7 +1652,12 @@ if(USE_CUDA) target_link_libraries(torch_cpu PRIVATE torch::cudart) endif() target_link_libraries(torch_cuda INTERFACE torch::cudart) - target_link_libraries(torch_cuda PUBLIC c10_cuda torch::nvtoolsext) + if(BUILD_LIBTORCHLESS) + target_link_libraries(torch_cuda PUBLIC ${C10_CUDA_LIB} torch::nvtoolsext) + else() + # unclear why we need to use c10 here otherwise c10 symbols are not included + target_link_libraries(torch_cuda PUBLIC c10_cuda torch::nvtoolsext) + endif() target_include_directories( torch_cuda INTERFACE $) @@ -1664,7 +1675,13 @@ endif() # ---[ XPU library. if(USE_XPU) target_link_libraries(torch_xpu INTERFACE torch::xpurt) - target_link_libraries(torch_xpu PUBLIC c10_xpu) + + if(BUILD_LIBTORCHLESS) + target_link_libraries(torch_xpu PUBLIC ${C10_XPU_LIB}) + else() + # unclear why we need to use c10 here otherwise c10 symbols are not included + target_link_libraries(torch_xpu PUBLIC c10_xpu) + endif() target_include_directories( torch_xpu INTERFACE $) @@ -1743,7 +1760,13 @@ if(USE_ROCM) # Since PyTorch files contain HIP headers, these flags are required for the necessary definitions to be added. target_compile_options(torch_hip PUBLIC ${HIP_CXX_FLAGS}) # experiment - target_link_libraries(torch_hip PUBLIC c10_hip) + + if(BUILD_LIBTORCHLESS) + target_link_libraries(torch_hip PUBLIC ${C10_HIP_LIB}) + else() + # unclear why we need to use c10 here otherwise c10 symbols are not included + target_link_libraries(torch_hip PUBLIC c10_hip) + endif() if(NOT INTERN_BUILD_MOBILE) # TODO: Cut this over to ATEN_HIP_FILES_GEN_LIB. At the moment, we @@ -1806,7 +1829,7 @@ if(BUILD_TEST) if(NOT MSVC) add_executable(${test_name}_${CPU_CAPABILITY} "${test_src}" ../aten/src/ATen/native/quantized/AffineQuantizerBase.cpp) # TODO: Get rid of c10 dependency (which is only needed for the implementation of AT_ERROR) - target_link_libraries(${test_name}_${CPU_CAPABILITY} c10 sleef gtest_main) + target_link_libraries(${test_name}_${CPU_CAPABILITY} ${C10_LIB} sleef gtest_main) if(USE_FBGEMM) target_link_libraries(${test_name}_${CPU_CAPABILITY} fbgemm) endif() diff --git a/setup.py b/setup.py index e7aece00d8dd1..894b5f594102c 100644 --- a/setup.py +++ b/setup.py @@ -235,6 +235,7 @@ import os import sys +import pkgutil if sys.platform == "win32" and sys.maxsize.bit_length() == 31: print( @@ -244,9 +245,20 @@ import platform +def _get_package_path(package_name): + loader = pkgutil.find_loader(package_name) + if loader: + # The package might be a namespace package, so get_data may fail + try: + file_path = loader.get_filename() + return os.path.dirname(file_path) + except AttributeError: + pass + return None + BUILD_LIBTORCH_WHL = os.getenv("BUILD_LIBTORCH_WHL", "0") == "1" BUILD_PYTORCH_USING_LIBTORCH_WHL = ( - os.getenv("BUILD_PYTORCH_USING_LIBTORCH_WHL", "0") == "1" + os.getenv("BUILD_PYTHON_ONLY", "0") == "1" ) # set up appropriate env variables @@ -258,6 +270,11 @@ else: os.environ["BUILD_PYTHONLESS"] = "OFF" + +if BUILD_PYTORCH_USING_LIBTORCH_WHL: + os.environ["BUILD_LIBTORCHLESS"] = "ON" + os.environ["LIBTORCH_LIB_PATH"] = f"{_get_package_path('libtorch')}/lib" + python_min_version = (3, 8, 0) python_min_version_str = ".".join(map(str, python_min_version)) if sys.version_info < python_min_version: @@ -363,6 +380,10 @@ def report(*args): # Version, create_version_file, and package_name ################################################################################ + + + + DEFAULT_PACKAGE_NAME = "libtorch" if BUILD_LIBTORCH_WHL else "torch" package_name = os.getenv("TORCH_PACKAGE_NAME", DEFAULT_PACKAGE_NAME) @@ -1176,6 +1197,9 @@ def main(): 'mkl>=2021.1.1,<=2021.4.0; platform_system == "Windows"', ] + if BUILD_PYTORCH_USING_LIBTORCH_WHL: + install_requires.append("libtorch") + use_prioritized_text = str(os.getenv("USE_PRIORITIZED_TEXT_FOR_LD", "")) if ( use_prioritized_text == "" diff --git a/torch/lib/libshm/CMakeLists.txt b/torch/lib/libshm/CMakeLists.txt index a3b41d0a0b05e..f5e2f19f5ca26 100644 --- a/torch/lib/libshm/CMakeLists.txt +++ b/torch/lib/libshm/CMakeLists.txt @@ -60,7 +60,7 @@ if(UNIX AND NOT APPLE) endif() add_executable(torch_shm_manager manager.cpp) -target_link_libraries(torch_shm_manager PRIVATE shm c10) +target_link_libraries(torch_shm_manager PRIVATE shm ${C10_LIB}) set_target_properties(torch_shm_manager PROPERTIES INSTALL_RPATH "${_rpath_portable_origin}/../lib")