Unify libtorch and libcaffe2 (#17783)

Summary:
This PR is an intermediate step toward the ultimate goal of eliminating "caffe2" in favor of "torch".  This PR moves all of the files that had constituted "libtorch.so" into the "libcaffe2.so" library, and wraps "libcaffe2.so" with a shell library named "libtorch.so".  This means that, for now, `caffe2/CMakeLists.txt` becomes a lot bigger, and `torch/CMakeLists.txt` becomes smaller.

The torch Python bindings (`torch_python.so`) still remain in `torch/CMakeLists.txt`.

The follow-up to this PR will rename references to `caffe2` to `torch`, and flatten the shell into one library.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/17783

Differential Revision: D15284178

Pulled By: kostmo

fbshipit-source-id: a08387d735ae20652527ced4e69fd75b8ff88b05
This commit is contained in:
Karl Ostmo 2019-05-10 09:44:49 -07:00 committed by Facebook Github Bot
parent 872bab22c6
commit 4ba28deb6e
15 changed files with 751 additions and 587 deletions

View File

@ -4,11 +4,22 @@ cd test\custom_operator
:: Build the custom operator library.
mkdir build
cd build
pushd build
echo "Executing CMake for custom_operator test..."
:: Note: Caffe2 does not support MSVC + CUDA + Debug mode (has to be Release mode)
cmake -DCMAKE_PREFIX_PATH=%TMP_DIR_WIN%\build\torch -DCMAKE_BUILD_TYPE=Release -GNinja ..
if ERRORLEVEL 1 exit /b 1
echo "Executing Ninja for custom_operator test..."
ninja -v
cd ..
if ERRORLEVEL 1 exit /b 1
echo "Ninja succeeded for custom_operator test."
popd
:: Run tests Python-side and export a script module.
python test_custom_ops.py -v

View File

@ -61,7 +61,6 @@ endif()
# Note to developers: if you add an option below, make sure you also add it to
# cmake/Summary.cmake so that the summary prints out the option values.
include(CMakeDependentOption)
option(BUILD_TORCH "Build Torch" OFF)
option(ATEN_NO_TEST "Do not build ATen test binaries" OFF)
option(BUILD_ATEN_ONLY "Build only a subset focused on ATen only" OFF)
option(BUILD_BINARY "Build C++ binaries" OFF)
@ -508,6 +507,7 @@ if (BUILD_SHARED_LIBS)
${PROJECT_SOURCE_DIR}/cmake/Modules_CUDA_fix
DESTINATION share/cmake/Caffe2/
COMPONENT dev)
install(EXPORT Caffe2Targets DESTINATION share/cmake/Caffe2
FILE Caffe2Targets.cmake
COMPONENT dev)

View File

@ -54,11 +54,6 @@ else()
# See cmake/Codegen.cmake for header installation
endif()
# ---[ Torch build
if(BUILD_TORCH)
add_subdirectory(../torch torch)
endif()
# ---[ Caffe2 build
# Note: the folders that are being commented out have not been properly
# addressed yet.
@ -211,12 +206,618 @@ if(NOT BUILD_ATEN_ONLY)
endif()
endif()
# ==========================================================
# formerly-libtorch
# ==========================================================
set(TORCH_SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../torch")
set(TORCH_ROOT "${TORCH_SRC_DIR}/..")
if(NOT TORCH_INSTALL_BIN_DIR)
set(TORCH_INSTALL_BIN_DIR bin)
endif()
if(NOT TORCH_INSTALL_INCLUDE_DIR)
set(TORCH_INSTALL_INCLUDE_DIR include)
endif()
if(NOT TORCH_INSTALL_LIB_DIR)
set(TORCH_INSTALL_LIB_DIR lib)
endif()
if (NOT INTERN_BUILD_MOBILE)
set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
# Generate files
set(TOOLS_PATH "${TORCH_ROOT}/tools")
configure_file("${TORCH_ROOT}/aten/src/ATen/common_with_cwrap.py"
"${TOOLS_PATH}/shared/cwrap_common.py"
COPYONLY)
configure_file("${TORCH_SRC_DIR}/_utils_internal.py"
"${TOOLS_PATH}/shared/_utils_internal.py"
COPYONLY)
set(GENERATED_CXX_TORCH
"${TORCH_SRC_DIR}/csrc/autograd/generated/Functions.cpp"
"${TORCH_SRC_DIR}/csrc/autograd/generated/VariableType_0.cpp"
"${TORCH_SRC_DIR}/csrc/autograd/generated/VariableType_1.cpp"
"${TORCH_SRC_DIR}/csrc/autograd/generated/VariableType_2.cpp"
"${TORCH_SRC_DIR}/csrc/autograd/generated/VariableType_3.cpp"
"${TORCH_SRC_DIR}/csrc/autograd/generated/VariableType_4.cpp"
"${TORCH_SRC_DIR}/csrc/jit/generated/register_aten_ops_0.cpp"
"${TORCH_SRC_DIR}/csrc/jit/generated/register_aten_ops_1.cpp"
"${TORCH_SRC_DIR}/csrc/jit/generated/register_aten_ops_2.cpp"
)
set(GENERATED_H_TORCH
"${TORCH_SRC_DIR}/csrc/autograd/generated/VariableType.h"
"${TORCH_SRC_DIR}/csrc/autograd/generated/Functions.h"
"${TORCH_SRC_DIR}/csrc/autograd/generated/variable_factories.h"
)
set(GENERATED_THNN_CXX_CUDA ${TORCH_SRC_DIR}/csrc/nn/THCUNN.cpp)
set(GENERATED_THNN_CXX ${TORCH_SRC_DIR}/csrc/nn/THNN.cpp)
set(GENERATED_CXX_PYTHON
"${TORCH_SRC_DIR}/csrc/autograd/generated/python_functions.cpp"
"${TORCH_SRC_DIR}/csrc/autograd/generated/python_variable_methods.cpp"
"${TORCH_SRC_DIR}/csrc/autograd/generated/python_torch_functions.cpp"
"${TORCH_SRC_DIR}/csrc/autograd/generated/python_nn_functions.cpp"
)
set(GENERATED_H_PYTHON
"${TORCH_SRC_DIR}/csrc/autograd/generated/python_functions.h"
"${TORCH_SRC_DIR}/csrc/autograd/generated/python_variable_methods_dispatch.h"
"${TORCH_SRC_DIR}/csrc/autograd/generated/python_torch_functions_dispatch.h"
"${TORCH_SRC_DIR}/csrc/autograd/generated/python_nn_functions.h"
"${TORCH_SRC_DIR}/csrc/autograd/generated/python_nn_functions_dispatch.h"
)
set(GENERATED_THNN_SOURCES
${GENERATED_THNN_CXX}
${GENERATED_THNN_CXX_CUDA}
)
set(TORCH_GENERATED_CODE
${GENERATED_CXX_TORCH}
${GENERATED_THNN_SOURCES}
${GENERATED_H_TORCH}
${GENERATED_CXX_PYTHON}
${GENERATED_H_PYTHON}
)
add_custom_command(
OUTPUT
${TORCH_GENERATED_CODE}
COMMAND
"${PYTHON_EXECUTABLE}" tools/setup_helpers/generate_code.py
--declarations-path "${CMAKE_BINARY_DIR}/aten/src/ATen/Declarations.yaml"
--nn-path "aten/src"
DEPENDS
"${CMAKE_BINARY_DIR}/aten/src/ATen/Declarations.yaml"
"${CMAKE_CURRENT_LIST_DIR}/../aten/src/THNN/generic/THNN.h"
"${TOOLS_PATH}/autograd/templates/VariableType.h"
"${TOOLS_PATH}/autograd/templates/VariableType.cpp"
"${TOOLS_PATH}/autograd/templates/Functions.h"
"${TOOLS_PATH}/autograd/templates/Functions.cpp"
"${TOOLS_PATH}/autograd/templates/python_functions.h"
"${TOOLS_PATH}/autograd/templates/python_functions.cpp"
"${TOOLS_PATH}/autograd/templates/python_variable_methods.cpp"
"${TOOLS_PATH}/autograd/templates/python_variable_methods_dispatch.h"
"${TOOLS_PATH}/autograd/templates/python_torch_functions.cpp"
"${TOOLS_PATH}/autograd/templates/python_torch_functions_dispatch.h"
"${TOOLS_PATH}/autograd/templates/python_nn_functions.cpp"
"${TOOLS_PATH}/autograd/templates/python_nn_functions.h"
"${TOOLS_PATH}/autograd/templates/python_nn_functions_dispatch.h"
"${TOOLS_PATH}/autograd/templates/variable_factories.h"
"${TOOLS_PATH}/autograd/deprecated.yaml"
"${TOOLS_PATH}/autograd/derivatives.yaml"
"${TOOLS_PATH}/autograd/gen_autograd_functions.py"
"${TOOLS_PATH}/autograd/gen_autograd.py"
"${TOOLS_PATH}/autograd/gen_python_functions.py"
"${TOOLS_PATH}/autograd/gen_variable_factories.py"
"${TOOLS_PATH}/autograd/gen_variable_type.py"
"${TOOLS_PATH}/autograd/load_derivatives.py"
"${TOOLS_PATH}/autograd/nested_dict.py"
"${TOOLS_PATH}/autograd/utils.py"
"${TOOLS_PATH}/jit/gen_jit_dispatch.py"
"${TOOLS_PATH}/jit/templates/register_aten_ops.cpp"
WORKING_DIRECTORY "${TORCH_ROOT}")
# Required workaround for libtorch_python.so build
# see https://samthursfield.wordpress.com/2015/11/21/cmake-dependencies-between-targets-and-files-and-custom-commands/#custom-commands-in-different-directories
add_custom_target(
generate-torch-sources
DEPENDS ${TORCH_GENERATED_CODE}
)
set(TORCH_SRCS
${GENERATED_CXX_TORCH}
${GENERATED_H_TORCH}
${TORCH_SRC_DIR}/csrc/autograd/anomaly_mode.cpp
${TORCH_SRC_DIR}/csrc/autograd/engine.cpp
${TORCH_SRC_DIR}/csrc/autograd/function.cpp
${TORCH_SRC_DIR}/csrc/autograd/function_hook.cpp
${TORCH_SRC_DIR}/csrc/autograd/functions/accumulate_grad.cpp
${TORCH_SRC_DIR}/csrc/autograd/functions/basic_ops.cpp
${TORCH_SRC_DIR}/csrc/autograd/functions/tensor.cpp
${TORCH_SRC_DIR}/csrc/autograd/functions/utils.cpp
${TORCH_SRC_DIR}/csrc/autograd/grad_mode.cpp
${TORCH_SRC_DIR}/csrc/autograd/input_buffer.cpp
${TORCH_SRC_DIR}/csrc/autograd/profiler.cpp
${TORCH_SRC_DIR}/csrc/autograd/record_function.cpp
${TORCH_SRC_DIR}/csrc/autograd/saved_variable.cpp
${TORCH_SRC_DIR}/csrc/autograd/variable.cpp
${TORCH_SRC_DIR}/csrc/autograd/VariableTypeManual.cpp
${TORCH_SRC_DIR}/csrc/jit/autodiff.cpp
${TORCH_SRC_DIR}/csrc/jit/attributes.cpp
${TORCH_SRC_DIR}/csrc/jit/argument_spec.cpp
${TORCH_SRC_DIR}/csrc/jit/export.cpp
${TORCH_SRC_DIR}/csrc/jit/pass_manager.cpp
${TORCH_SRC_DIR}/csrc/jit/pickler.cpp
${TORCH_SRC_DIR}/csrc/jit/graph_executor.cpp
${TORCH_SRC_DIR}/csrc/jit/import_source.cpp
${TORCH_SRC_DIR}/csrc/jit/import.cpp
${TORCH_SRC_DIR}/csrc/jit/import_export_helpers.cpp
${TORCH_SRC_DIR}/csrc/jit/interpreter.cpp
${TORCH_SRC_DIR}/csrc/jit/constants.cpp
${TORCH_SRC_DIR}/csrc/jit/node_hashing.cpp
${TORCH_SRC_DIR}/csrc/jit/ir.cpp
${TORCH_SRC_DIR}/csrc/jit/irparser.cpp
${TORCH_SRC_DIR}/csrc/jit/netdef_converter.cpp
${TORCH_SRC_DIR}/csrc/jit/operator.cpp
${TORCH_SRC_DIR}/csrc/jit/register_c10_ops.cpp
${TORCH_SRC_DIR}/csrc/jit/subgraph_matcher.cpp
${TORCH_SRC_DIR}/csrc/jit/symbolic_script.cpp
${TORCH_SRC_DIR}/csrc/jit/profiling_record.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/alias_analysis.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/batch_mm.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/canonicalize.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/constant_propagation.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/constant_pooling.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/common_subexpression_elimination.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/create_autodiff_subgraphs.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/inline_autodiff_subgraphs.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/dead_code_elimination.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/decompose_ops.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/canonicalize_ops.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/erase_number_types.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/inline_fork_wait.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/graph_fuser.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/inplace_check.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/loop_unrolling.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/lower_grad_of.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/lower_tuples.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/peephole.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/remove_expands.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/remove_inplace_ops.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/shape_analysis.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/requires_grad_analysis.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/specialize_autogradzero.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/subgraph_rewrite.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/python_print.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/utils/subgraph_utils.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/utils/check_alias_annotation.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/utils/memory_dag.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/quantization.cpp
${TORCH_SRC_DIR}/csrc/jit/fuser/interface.cpp
${TORCH_SRC_DIR}/csrc/jit/register_prim_ops.cpp
${TORCH_SRC_DIR}/csrc/jit/register_special_ops.cpp
${TORCH_SRC_DIR}/csrc/jit/register_quantized_ops.cpp
${TORCH_SRC_DIR}/csrc/jit/scope.cpp
${TORCH_SRC_DIR}/csrc/jit/script/compiler.cpp
${TORCH_SRC_DIR}/csrc/api/src/jit.cpp
${TORCH_SRC_DIR}/csrc/jit/testing/file_check.cpp
${TORCH_SRC_DIR}/csrc/jit/script/final_returns.cpp
${TORCH_SRC_DIR}/csrc/jit/script/schema_matching.cpp
${TORCH_SRC_DIR}/csrc/jit/script/script_type_parser.cpp
${TORCH_SRC_DIR}/csrc/jit/script/sugared_value.cpp
${TORCH_SRC_DIR}/csrc/jit/script/class_type.cpp
${TORCH_SRC_DIR}/csrc/jit/script/parser.cpp
${TORCH_SRC_DIR}/csrc/jit/script/builtin_functions.cpp
${TORCH_SRC_DIR}/csrc/jit/script/edit_distance.cpp
${TORCH_SRC_DIR}/csrc/jit/script/logging.cpp
${TORCH_SRC_DIR}/csrc/jit/script/module.cpp
${TORCH_SRC_DIR}/csrc/jit/script/jit_exception.cpp
${TORCH_SRC_DIR}/csrc/jit/tracer.cpp
${TORCH_SRC_DIR}/csrc/jit/hooks_for_testing.cpp
${TORCH_SRC_DIR}/csrc/utils/tensor_flatten.cpp
${TORCH_SRC_DIR}/csrc/utils/variadic.cpp
${TORCH_SRC_DIR}/csrc/jit/fuser/kernel_cache.cpp
${TORCH_SRC_DIR}/csrc/jit/fuser/compiler.cpp
${TORCH_SRC_DIR}/csrc/jit/fuser/executor.cpp
${TORCH_SRC_DIR}/csrc/jit/fuser/codegen.cpp
${TORCH_SRC_DIR}/csrc/jit/fuser/fallback.cpp
${TORCH_ROOT}/test/cpp/jit/test.cpp
)
if (WIN32)
list(APPEND TORCH_SRCS
${TORCH_SRC_DIR}/csrc/jit/fuser/cpu/dynamic_library_win.cpp
)
else ()
list(APPEND TORCH_SRCS
${TORCH_SRC_DIR}/csrc/jit/fuser/cpu/dynamic_library_unix.cpp
${TORCH_SRC_DIR}/csrc/jit/fuser/cpu/fused_kernel.cpp
)
if (USE_CUDA AND NOT USE_ROCM)
list(APPEND Caffe2_GPU_SRCS
${TORCH_SRC_DIR}/csrc/jit/fuser/cuda/fused_kernel.cpp
)
add_library(thnvrtc SHARED ${TORCH_SRC_DIR}/csrc/jit/fuser/cuda/thnvrtc.cpp)
target_link_libraries(thnvrtc ${CUDA_NVRTC} ${CUDA_CUDA_LIB} ${CUDA_NVRTC_LIB})
target_include_directories(thnvrtc PRIVATE ${CUDA_INCLUDE_DIRS})
install(TARGETS thnvrtc DESTINATION "${TORCH_INSTALL_LIB_DIR}")
endif()
endif ()
if (USE_CUDA)
list(APPEND Caffe2_GPU_SRCS
${TORCH_SRC_DIR}/csrc/autograd/profiler_cuda.cpp
${TORCH_SRC_DIR}/csrc/autograd/functions/comm.cpp
${TORCH_SRC_DIR}/csrc/cuda/comm.cpp
)
endif()
if (USE_ROCM)
list(APPEND Caffe2_HIP_SRCS
${TORCH_SRC_DIR}/csrc/autograd/functions/comm.cpp
${TORCH_SRC_DIR}/csrc/cuda/comm.cpp
)
endif()
if (NOT NO_API)
list(APPEND TORCH_SRCS
${TORCH_SRC_DIR}/csrc/api/src/cuda.cpp
${TORCH_SRC_DIR}/csrc/api/src/data/datasets/mnist.cpp
${TORCH_SRC_DIR}/csrc/api/src/data/samplers/distributed.cpp
${TORCH_SRC_DIR}/csrc/api/src/data/samplers/random.cpp
${TORCH_SRC_DIR}/csrc/api/src/data/samplers/sequential.cpp
${TORCH_SRC_DIR}/csrc/api/src/data/samplers/stream.cpp
${TORCH_SRC_DIR}/csrc/api/src/jit.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/init.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/module.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/modules/batchnorm.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/modules/conv.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/modules/dropout.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/modules/embedding.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/modules/functional.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/modules/linear.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/modules/named_any.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/modules/rnn.cpp
${TORCH_SRC_DIR}/csrc/api/src/optim/adagrad.cpp
${TORCH_SRC_DIR}/csrc/api/src/optim/adam.cpp
${TORCH_SRC_DIR}/csrc/api/src/optim/lbfgs.cpp
${TORCH_SRC_DIR}/csrc/api/src/optim/optimizer.cpp
${TORCH_SRC_DIR}/csrc/api/src/optim/rmsprop.cpp
${TORCH_SRC_DIR}/csrc/api/src/optim/serialize.cpp
${TORCH_SRC_DIR}/csrc/api/src/optim/sgd.cpp
${TORCH_SRC_DIR}/csrc/api/src/serialize/input-archive.cpp
${TORCH_SRC_DIR}/csrc/api/src/serialize/output-archive.cpp
)
endif()
list(APPEND Caffe2_CPU_SRCS ${TORCH_SRCS})
endif()
# ==========================================================
# END formerly-libtorch sources
# ==========================================================
# Compile exposed libraries.
add_library(caffe2 ${Caffe2_CPU_SRCS})
if (NOT WIN32)
target_compile_options(caffe2 PRIVATE "-fvisibility=hidden")
option(TORCH_STATIC "Build libtorch.a rather than libtorch.so" OFF)
# This is required for older versions of CMake, which don't allow
# specifying add_library() without a list of source files
set(DUMMY_EMPTY_FILE ${CMAKE_BINARY_DIR}/empty.cpp)
if (MSVC)
set(DUMMY_FILE_CONTENT "__declspec(dllexport) int ignore_this_library_placeholder(){return 0\\;}")
else()
set(DUMMY_FILE_CONTENT "")
endif()
file(WRITE ${DUMMY_EMPTY_FILE} ${DUMMY_FILE_CONTENT})
# Wrapper library for transition to merged libcaffe and libtorch.
# Only necessary on Windows?
# Contains "caffe2" and "caffe2_gpu".
if (TORCH_STATIC)
add_library(torch STATIC ${DUMMY_EMPTY_FILE})
else()
add_library(torch SHARED ${DUMMY_EMPTY_FILE})
endif()
target_link_libraries(torch caffe2)
# ==========================================================
# formerly-libtorch flags
# ==========================================================
if (NOT INTERN_BUILD_MOBILE)
# Forces caffe2.pb.h to be generated before its dependents are compiled.
# Adding the generated header file to the ${TORCH_SRCS} list is not sufficient
# to establish the dependency, since the generation procedure is declared in a different CMake file.
# See https://samthursfield.wordpress.com/2015/11/21/cmake-dependencies-between-targets-and-files-and-custom-commands/#custom-commands-in-different-directories
add_dependencies(caffe2 Caffe2_PROTO)
target_compile_definitions(caffe2 PUBLIC _THP_CORE)
# until they can be unified, keep these lists synced with setup.py
if(MSVC)
if (MSVC_Z7_OVERRIDE)
set(MSVC_DEBINFO_OPTION "/Z7")
else()
set(MSVC_DEBINFO_OPTION "/Zi")
endif()
target_compile_options(caffe2 PUBLIC
${MSVC_RUNTIME_LIBRARY_OPTION}
${MSVC_DEBINFO_OPTION}
/EHa
/DNOMINMAX
/wd4267
/wd4251
/wd4522
/wd4522
/wd4838
/wd4305
/wd4244
/wd4190
/wd4101
/wd4996
/wd4275
/bigobj
)
else()
target_compile_options(caffe2 PUBLIC
# -std=c++11
-Wall
-Wextra
-Wno-unused-parameter
-Wno-missing-field-initializers
-Wno-write-strings
-Wno-unknown-pragmas
# Clang has an unfixed bug leading to spurious missing braces
# warnings, see https://bugs.llvm.org/show_bug.cgi?id=21629
-Wno-missing-braces
)
if(NOT APPLE)
target_compile_options(caffe2 PRIVATE
# Considered to be flaky. See the discussion at
# https://github.com/pytorch/pytorch/pull/9608
-Wno-maybe-uninitialized)
endif()
endif()
if (MSVC)
elseif (WERROR)
target_compile_options(caffe2 PRIVATE -Werror -Wno-strict-overflow)
endif()
if (NOT NO_API)
target_include_directories(caffe2 PRIVATE
${TORCH_SRC_DIR}/csrc/api
${TORCH_SRC_DIR}/csrc/api/include)
endif()
if(USE_CUDA)
if(MSVC)
if (NOT NVTOOLEXT_HOME)
set(NVTOOLEXT_HOME "C:/Program Files/NVIDIA Corporation/NvToolsExt")
endif()
if ($ENV{NVTOOLEXT_HOME})
set(NVTOOLEXT_HOME $ENV{NVTOOLEXT_HOME})
endif()
set(TORCH_CUDA_LIBRARIES
${NVTOOLEXT_HOME}/lib/x64/nvToolsExt64_1.lib
${CUDA_LIBRARIES})
target_include_directories(caffe2 PUBLIC "${NVTOOLEXT_HOME}/include")
elseif(APPLE)
set(TORCH_CUDA_LIBRARIES
${CUDA_TOOLKIT_ROOT_DIR}/lib/libcudart.dylib
${CUDA_TOOLKIT_ROOT_DIR}/lib/libnvrtc.dylib
${CUDA_TOOLKIT_ROOT_DIR}/lib/libnvToolsExt.dylib
${CUDA_LIBRARIES})
else()
find_library(LIBNVTOOLSEXT libnvToolsExt.so PATHS ${CUDA_TOOLKIT_ROOT_DIR}/lib64/)
set(TORCH_CUDA_LIBRARIES
${LIBNVTOOLSEXT}
${CUDA_LIBRARIES})
endif()
target_compile_definitions(caffe2 PRIVATE USE_CUDA)
endif()
set(TH_CPU_INCLUDE
# dense
aten/src/TH
${CMAKE_CURRENT_BINARY_DIR}/aten/src/TH
${TORCH_ROOT}/aten/src
${CMAKE_CURRENT_BINARY_DIR}/aten/src
${CMAKE_BINARY_DIR}/aten/src)
target_include_directories(caffe2 PRIVATE ${TH_CPU_INCLUDE})
set(ATen_CPU_INCLUDE
${TORCH_ROOT}/aten/src
${CMAKE_CURRENT_BINARY_DIR}/../aten/src
${CMAKE_CURRENT_BINARY_DIR}/../aten/src/ATen
${CMAKE_BINARY_DIR}/aten/src)
target_include_directories(caffe2 PRIVATE ${ATen_CPU_INCLUDE})
target_include_directories(caffe2 PRIVATE
${TORCH_SRC_DIR}/csrc)
target_include_directories(caffe2 PRIVATE
${TORCH_ROOT}/third_party/miniz-2.0.8)
set_property(TARGET caffe2 PROPERTY CXX_STANDARD 11)
# Prevent the unused functions being optimized away
# Otherwise torch.dll will be linked without caffe2_gpu.dll
if (MSVC)
# TODO What to do with this line?
set_target_properties(caffe2 PROPERTIES LINK_FLAGS "/OPT:NOREF")
endif()
install(DIRECTORY "${TORCH_SRC_DIR}/csrc"
DESTINATION ${TORCH_INSTALL_INCLUDE_DIR}/torch
FILES_MATCHING PATTERN "*.h")
install(FILES "${TORCH_SRC_DIR}/script.h" "${TORCH_SRC_DIR}/extension.h"
DESTINATION ${TORCH_INSTALL_INCLUDE_DIR}/torch)
if (BUILD_TEST AND NOT MSVC AND NOT USE_ROCM)
add_subdirectory(${TORCH_ROOT}/test/cpp/jit ${CMAKE_BINARY_DIR}/test_jit)
endif()
if (BUILD_TEST AND NOT NO_API)
add_subdirectory(${TORCH_ROOT}/test/cpp/api ${CMAKE_BINARY_DIR}/test_api)
endif()
# XXX This ABI check cannot be run with arm-linux-androideabi-g++
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
message(STATUS "${CMAKE_CXX_COMPILER} ${TORCH_SRC_DIR}/abi-check.cpp -o ${CMAKE_BINARY_DIR}/abi-check")
execute_process(
COMMAND
"${CMAKE_CXX_COMPILER}"
"${TORCH_SRC_DIR}/abi-check.cpp"
"-o"
"${CMAKE_BINARY_DIR}/abi-check"
RESULT_VARIABLE ABI_CHECK_COMPILE_RESULT)
if (ABI_CHECK_COMPILE_RESULT)
message(FATAL_ERROR "Could not compile ABI Check: ${ABI_CHECK_COMPILE_RESULT}")
endif()
execute_process(
COMMAND "${CMAKE_BINARY_DIR}/abi-check"
RESULT_VARIABLE ABI_CHECK_RESULT
OUTPUT_VARIABLE GLIBCXX_USE_CXX11_ABI)
if (ABI_CHECK_RESULT)
message(WARNING "Could not run ABI Check: ${ABI_CHECK_RESULT}")
endif()
message(STATUS "Determined _GLIBCXX_USE_CXX11_ABI=${GLIBCXX_USE_CXX11_ABI}")
endif()
# CMake config for external projects.
configure_file(
${PROJECT_SOURCE_DIR}/cmake/TorchConfigVersion.cmake.in
${PROJECT_BINARY_DIR}/TorchConfigVersion.cmake
@ONLY)
configure_file(
${TORCH_ROOT}/cmake/TorchConfig.cmake.in
${PROJECT_BINARY_DIR}/TorchConfig.cmake
@ONLY)
install(FILES
${PROJECT_BINARY_DIR}/TorchConfigVersion.cmake
${PROJECT_BINARY_DIR}/TorchConfig.cmake
DESTINATION share/cmake/Torch)
if (USE_DISTRIBUTED)
add_subdirectory(${TORCH_SRC_DIR}/lib/THD lib_THD)
if (NOT MSVC AND NOT APPLE)
add_subdirectory(${TORCH_SRC_DIR}/lib/c10d lib_c10d)
endif()
endif()
# ---[ Torch python bindings build
add_subdirectory(../torch torch)
endif()
# ==========================================================
# END formerly-libtorch flags
# ==========================================================
if (NOT NO_API)
target_include_directories(caffe2 PUBLIC
$<BUILD_INTERFACE:${TORCH_SRC_DIR}/csrc/api>
$<BUILD_INTERFACE:${TORCH_SRC_DIR}/csrc/api/include>)
endif()
find_package(OpenMP QUIET)
if(USE_OPENMP AND OPENMP_FOUND)
message(STATUS "pytorch is compiling with OpenMP. \n"
"OpenMP CXX_FLAGS: ${OpenMP_CXX_FLAGS}. \n"
"OpenMP libraries: ${OpenMP_CXX_LIBRARIES}.")
target_compile_options(caffe2 INTERFACE ${OpenMP_CXX_FLAGS})
target_link_libraries(caffe2 PRIVATE ${OpenMP_CXX_LIBRARIES})
endif()
if(USE_ROCM)
# XXX kostmo
# target_link_libraries(caffe2 PUBLIC caffe2_hip_library)
target_compile_definitions(caffe2 PRIVATE
USE_ROCM
__HIP_PLATFORM_HCC__
)
target_include_directories(caffe2 PRIVATE
/opt/rocm/include
/opt/rocm/hcc/include
/opt/rocm/rocblas/include
/opt/rocm/hipsparse/include
)
endif()
if (NOT WIN32)
# FIXME kostmo
# target_compile_options(caffe2 PRIVATE "-fvisibility=hidden")
endif()
if(NOT BUILD_ATEN_ONLY)
caffe2_interface_library(caffe2_protos caffe2_protos_whole)
target_link_libraries(caffe2 PRIVATE caffe2_protos_whole)
@ -244,9 +845,9 @@ target_include_directories(caffe2 SYSTEM PRIVATE "${Caffe2_DEPENDENCY_INCLUDE}")
# Set standard properties on the target
torch_set_target_props(caffe2)
if (NOT MSVC)
target_compile_options(caffe2 INTERFACE "$<$<COMPILE_LANGUAGE:CXX>:-std=c++11>")
endif()
#if (NOT MSVC)
# target_compile_options(caffe2 INTERFACE "$<$<COMPILE_LANGUAGE:CXX>:-std=c++11>")
#endif()
target_compile_options(caffe2 PRIVATE "-DCAFFE2_BUILD_MAIN_LIB")
if (MSVC AND NOT BUILD_SHARED_LIBS)
@ -325,7 +926,17 @@ if (MSVC AND BUILD_SHARED_LIBS)
endif()
# Use -O2 for release builds (-O3 doesn't improve perf, and -Os results in perf regression)
target_compile_options(caffe2 PRIVATE "$<$<OR:$<CONFIG:Release>,$<CONFIG:RelWithDebInfo>>:-O2>")
install(TARGETS caffe2 EXPORT Caffe2Targets DESTINATION lib)
install(TARGETS torch DESTINATION "${TORCH_INSTALL_LIB_DIR}")
if (MSVC AND NOT TORCH_STATIC)
install(FILES $<TARGET_PDB_FILE:torch> DESTINATION "${TORCH_INSTALL_LIB_DIR}" OPTIONAL)
endif()
caffe2_interface_library(caffe2 caffe2_library)
list(APPEND Caffe2_MAIN_LIBS caffe2_library)
# Install PDB files for MSVC builds
@ -335,12 +946,15 @@ endif()
# ---[ CUDA library.
if(USE_CUDA)
set(CUDA_LINK_LIBRARIES_KEYWORD PRIVATE)
torch_cuda_based_add_library(caffe2_gpu ${Caffe2_GPU_SRCS})
set(CUDA_LINK_LIBRARIES_KEYWORD)
target_link_libraries(caffe2_gpu INTERFACE caffe2::cudart)
target_link_libraries(caffe2_gpu PUBLIC c10_cuda)
target_link_libraries(caffe2_gpu PUBLIC ${TORCH_CUDA_LIBRARIES})
target_include_directories(
caffe2_gpu INTERFACE $<INSTALL_INTERFACE:include>)
target_include_directories(
@ -375,10 +989,14 @@ if(USE_CUDA)
caffe2_interface_library(caffe2_gpu caffe2_gpu_library)
list(APPEND Caffe2_MAIN_LIBS caffe2_gpu_library)
# Install PDB files for MSVC builds
if (MSVC AND BUILD_SHARED_LIBS)
install(FILES $<TARGET_PDB_FILE:caffe2_gpu> DESTINATION lib OPTIONAL)
endif()
target_link_libraries(torch caffe2_gpu)
endif()
# ---[ Caffe2 HIP sources.

View File

@ -18,7 +18,7 @@ You can use a python script/module located in tools package to build libtorch
Alternatively, you can invoke a shell script in the same directory to achieve the same goal
::
cd <pytorch_root>
BUILD_TORCH=ON ONNX_NAMESPACE=onnx_torch bash tools/build_pytorch_libs.sh --use-nnpack caffe2
ONNX_NAMESPACE=onnx_torch bash tools/build_pytorch_libs.sh --use-nnpack caffe2
ls torch/lib/tmp_install # output is produced here
ls torch/lib/tmp_install/lib/libtorch.so # of particular interest

View File

@ -18,10 +18,6 @@ if NOT DEFINED BUILD_SHARED_LIBS (
set BUILD_SHARED_LIBS=OFF
)
if NOT DEFINED BUILD_TORCH (
set BUILD_TORCH=OFF
)
IF NOT DEFINED BUILDING_WITH_TORCH_LIBS (
set BUILDING_WITH_TORCH_LIBS=OFF
)
@ -50,6 +46,10 @@ if NOT DEFINED USE_OBSERVERS (
set USE_OBSERVERS=OFF
)
if NOT DEFINED MSVC_Z7_OVERRIDE (
set MSVC_Z7_OVERRIDE=OFF
)
if NOT DEFINED CMAKE_GENERATOR (
if DEFINED APPVEYOR_BUILD_WORKER_IMAGE (
if "%APPVEYOR_BUILD_WORKER_IMAGE%" == "Visual Studio 2017" (

View File

@ -30,7 +30,7 @@ endif()
add_executable(test_api ${TORCH_API_TEST_SOURCES})
target_include_directories(test_api PRIVATE ${ATen_CPU_INCLUDE})
target_link_libraries(test_api PRIVATE torch gtest)
target_link_libraries(test_api PRIVATE caffe2 gtest)
if (USE_CUDA)
target_link_libraries(test_api PRIVATE
@ -38,6 +38,9 @@ if (USE_CUDA)
${CUDA_NVRTC_LIB}
${CUDA_CUDA_LIB}
${TORCH_CUDA_LIBRARIES})
target_link_libraries(test_api PRIVATE caffe2_gpu)
target_compile_definitions(test_api PRIVATE "USE_CUDA")
endif()

View File

@ -4,7 +4,8 @@ add_executable(test_jit
${TORCH_ROOT}/test/cpp/common/main.cpp
${JIT_TEST_ROOT}/test.cpp)
target_link_libraries(test_jit PRIVATE torch gtest)
target_link_libraries(test_jit PRIVATE caffe2 gtest)
target_include_directories(test_jit PRIVATE ${ATen_CPU_INCLUDE})
target_compile_definitions(test_jit PRIVATE USE_GTEST)
if (USE_CUDA)
@ -13,5 +14,8 @@ if (USE_CUDA)
${CUDA_NVRTC_LIB}
${CUDA_CUDA_LIB}
${TORCH_CUDA_LIBRARIES})
target_link_libraries(test_jit PRIVATE caffe2_gpu)
target_compile_definitions(test_jit PRIVATE USE_CUDA)
endif()

View File

@ -139,6 +139,7 @@ def run_cmake(version,
elif IS_WINDOWS:
if IS_64BIT:
cmake_args.append('-GVisual Studio 15 2017 Win64')
cmake_args.append('-Thost=x64')
else:
cmake_args.append('-GVisual Studio 15 2017')
try:
@ -166,7 +167,7 @@ def run_cmake(version,
BUILDING_WITH_TORCH_LIBS=os.getenv("BUILDING_WITH_TORCH_LIBS", "ON"),
TORCH_BUILD_VERSION=version,
CMAKE_BUILD_TYPE=build_type,
BUILD_TORCH=os.getenv("BUILD_TORCH", "ON"),
CMAKE_VERBOSE_MAKEFILE="ON",
BUILD_PYTHON=build_python,
BUILD_SHARED_LIBS=os.getenv("BUILD_SHARED_LIBS", "ON"),
BUILD_BINARY=check_env_flag('BUILD_BINARY'),
@ -286,7 +287,7 @@ def build_caffe2(version,
check_call(build_cmd, cwd=build_dir, env=my_env)
else:
if USE_NINJA:
ninja_cmd = ['ninja', 'install']
ninja_cmd = ['ninja', 'install', '-v']
if max_jobs is not None:
ninja_cmd += ['-j', max_jobs]
check_call(ninja_cmd, cwd=build_dir, env=my_env)

View File

@ -16,7 +16,7 @@ if [[ ! -d build ]]; then
mkdir build
pushd build
# We really only need compile_commands.json, so no need to build!
time cmake -DBUILD_TORCH=ON ..
time cmake ..
popd
# Generate ATen files.

View File

@ -1,8 +1,7 @@
if (CAFFE2_CMAKE_BUILDING_WITH_MAIN_REPO)
if (NOT BUILD_TORCH)
return()
endif()
else()
# This file used to build libtorch.so.
# Now it only builds the Torch python bindings.
if (NOT CAFFE2_CMAKE_BUILDING_WITH_MAIN_REPO)
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
project(torch CXX C)
find_package(Caffe2 REQUIRED)
@ -10,498 +9,34 @@ else()
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
endif()
option(TORCH_STATIC "Build libtorch.a rather than libtorch.so" OFF)
if (NOT BUILD_PYTHON)
return()
endif()
set(TORCH_SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
set(TORCH_ROOT "${TORCH_SRC_DIR}/..")
if(NOT TORCH_INSTALL_BIN_DIR)
set(TORCH_INSTALL_BIN_DIR bin)
endif()
if(NOT TORCH_INSTALL_INCLUDE_DIR)
set(TORCH_INSTALL_INCLUDE_DIR include)
endif()
if(NOT TORCH_INSTALL_LIB_DIR)
set(TORCH_INSTALL_LIB_DIR lib)
endif()
set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
if (MSVC)
set(LIBSHM_SUBDIR libshm_windows)
else()
set(LIBSHM_SUBDIR libshm)
endif()
set(LIBSHM_SRCDIR ${TORCH_SRC_DIR}/lib/${LIBSHM_SUBDIR})
add_subdirectory(${LIBSHM_SRCDIR})
# Generate files
set(TOOLS_PATH "${TORCH_ROOT}/tools")
configure_file("${TORCH_ROOT}/aten/src/ATen/common_with_cwrap.py"
"${TOOLS_PATH}/shared/cwrap_common.py"
COPYONLY)
configure_file("${TORCH_SRC_DIR}/_utils_internal.py"
"${TOOLS_PATH}/shared/_utils_internal.py"
COPYONLY)
add_custom_command(
OUTPUT
"${TORCH_SRC_DIR}/csrc/nn/THNN.cpp"
"${TORCH_SRC_DIR}/csrc/nn/THCUNN.cpp"
"${TORCH_SRC_DIR}/csrc/autograd/generated/VariableType.h"
"${TORCH_SRC_DIR}/csrc/autograd/generated/VariableType_0.cpp"
"${TORCH_SRC_DIR}/csrc/autograd/generated/VariableType_1.cpp"
"${TORCH_SRC_DIR}/csrc/autograd/generated/VariableType_2.cpp"
"${TORCH_SRC_DIR}/csrc/autograd/generated/VariableType_3.cpp"
"${TORCH_SRC_DIR}/csrc/autograd/generated/VariableType_4.cpp"
"${TORCH_SRC_DIR}/csrc/autograd/generated/Functions.h"
"${TORCH_SRC_DIR}/csrc/autograd/generated/Functions.cpp"
"${TORCH_SRC_DIR}/csrc/autograd/generated/python_functions.h"
"${TORCH_SRC_DIR}/csrc/autograd/generated/python_functions.cpp"
"${TORCH_SRC_DIR}/csrc/autograd/generated/python_variable_methods.cpp"
"${TORCH_SRC_DIR}/csrc/autograd/generated/python_variable_methods_dispatch.h"
"${TORCH_SRC_DIR}/csrc/autograd/generated/python_torch_functions.cpp"
"${TORCH_SRC_DIR}/csrc/autograd/generated/python_torch_functions_dispatch.h"
"${TORCH_SRC_DIR}/csrc/autograd/generated/python_nn_functions.cpp"
"${TORCH_SRC_DIR}/csrc/autograd/generated/python_nn_functions.h"
"${TORCH_SRC_DIR}/csrc/autograd/generated/python_nn_functions_dispatch.h"
"${TORCH_SRC_DIR}/csrc/autograd/generated/variable_factories.h"
"${TORCH_SRC_DIR}/csrc/jit/generated/register_aten_ops_0.cpp"
"${TORCH_SRC_DIR}/csrc/jit/generated/register_aten_ops_1.cpp"
"${TORCH_SRC_DIR}/csrc/jit/generated/register_aten_ops_2.cpp"
COMMAND
"${PYTHON_EXECUTABLE}" tools/setup_helpers/generate_code.py
--declarations-path "${CMAKE_BINARY_DIR}/aten/src/ATen/Declarations.yaml"
--nn-path "aten/src/"
DEPENDS
"${CMAKE_BINARY_DIR}/aten/src/ATen/Declarations.yaml"
"${CMAKE_CURRENT_LIST_DIR}/../aten/src/THNN/generic/THNN.h"
"${TOOLS_PATH}/autograd/templates/VariableType.h"
"${TOOLS_PATH}/autograd/templates/VariableType.cpp"
"${TOOLS_PATH}/autograd/templates/Functions.h"
"${TOOLS_PATH}/autograd/templates/Functions.cpp"
"${TOOLS_PATH}/autograd/templates/python_functions.h"
"${TOOLS_PATH}/autograd/templates/python_functions.cpp"
"${TOOLS_PATH}/autograd/templates/python_variable_methods.cpp"
"${TOOLS_PATH}/autograd/templates/python_variable_methods_dispatch.h"
"${TOOLS_PATH}/autograd/templates/python_torch_functions.cpp"
"${TOOLS_PATH}/autograd/templates/python_torch_functions_dispatch.h"
"${TOOLS_PATH}/autograd/templates/python_nn_functions.cpp"
"${TOOLS_PATH}/autograd/templates/python_nn_functions.h"
"${TOOLS_PATH}/autograd/templates/python_nn_functions_dispatch.h"
"${TOOLS_PATH}/autograd/templates/variable_factories.h"
"${TOOLS_PATH}/autograd/deprecated.yaml"
"${TOOLS_PATH}/autograd/derivatives.yaml"
"${TOOLS_PATH}/autograd/gen_autograd_functions.py"
"${TOOLS_PATH}/autograd/gen_autograd.py"
"${TOOLS_PATH}/autograd/gen_python_functions.py"
"${TOOLS_PATH}/autograd/gen_variable_factories.py"
"${TOOLS_PATH}/autograd/gen_variable_type.py"
"${TOOLS_PATH}/autograd/load_derivatives.py"
"${TOOLS_PATH}/autograd/nested_dict.py"
"${TOOLS_PATH}/autograd/utils.py"
"${TOOLS_PATH}/jit/gen_jit_dispatch.py"
"${TOOLS_PATH}/jit/templates/register_aten_ops.cpp"
WORKING_DIRECTORY "${TORCH_ROOT}")
set(TORCH_SRCS
${TORCH_SRC_DIR}/csrc/autograd/anomaly_mode.cpp
${TORCH_SRC_DIR}/csrc/autograd/engine.cpp
${TORCH_SRC_DIR}/csrc/autograd/function.cpp
${TORCH_SRC_DIR}/csrc/autograd/function_hook.cpp
${TORCH_SRC_DIR}/csrc/autograd/functions/accumulate_grad.cpp
${TORCH_SRC_DIR}/csrc/autograd/functions/basic_ops.cpp
${TORCH_SRC_DIR}/csrc/autograd/functions/tensor.cpp
${TORCH_SRC_DIR}/csrc/autograd/functions/utils.cpp
${TORCH_SRC_DIR}/csrc/autograd/generated/Functions.cpp
${TORCH_SRC_DIR}/csrc/autograd/generated/VariableType_0.cpp
${TORCH_SRC_DIR}/csrc/autograd/generated/VariableType_1.cpp
${TORCH_SRC_DIR}/csrc/autograd/generated/VariableType_2.cpp
${TORCH_SRC_DIR}/csrc/autograd/generated/VariableType_3.cpp
${TORCH_SRC_DIR}/csrc/autograd/generated/VariableType_4.cpp
${TORCH_SRC_DIR}/csrc/autograd/grad_mode.cpp
${TORCH_SRC_DIR}/csrc/autograd/input_buffer.cpp
${TORCH_SRC_DIR}/csrc/autograd/profiler.cpp
${TORCH_SRC_DIR}/csrc/autograd/record_function.cpp
${TORCH_SRC_DIR}/csrc/autograd/saved_variable.cpp
${TORCH_SRC_DIR}/csrc/autograd/variable.cpp
${TORCH_SRC_DIR}/csrc/autograd/VariableTypeManual.cpp
${TORCH_SRC_DIR}/csrc/jit/autodiff.cpp
${TORCH_SRC_DIR}/csrc/jit/attributes.cpp
${TORCH_SRC_DIR}/csrc/jit/argument_spec.cpp
${TORCH_SRC_DIR}/csrc/jit/export.cpp
${TORCH_SRC_DIR}/csrc/jit/pass_manager.cpp
${TORCH_SRC_DIR}/csrc/jit/pickler.cpp
${TORCH_SRC_DIR}/csrc/jit/generated/register_aten_ops_0.cpp
${TORCH_SRC_DIR}/csrc/jit/generated/register_aten_ops_1.cpp
${TORCH_SRC_DIR}/csrc/jit/generated/register_aten_ops_2.cpp
${TORCH_SRC_DIR}/csrc/jit/graph_executor.cpp
${TORCH_SRC_DIR}/csrc/jit/import_source.cpp
${TORCH_SRC_DIR}/csrc/jit/import.cpp
${TORCH_SRC_DIR}/csrc/jit/import_export_helpers.cpp
${TORCH_SRC_DIR}/csrc/jit/interpreter.cpp
${TORCH_SRC_DIR}/csrc/jit/constants.cpp
${TORCH_SRC_DIR}/csrc/jit/node_hashing.cpp
${TORCH_SRC_DIR}/csrc/jit/ir.cpp
${TORCH_SRC_DIR}/csrc/jit/irparser.cpp
${TORCH_SRC_DIR}/csrc/jit/netdef_converter.cpp
${TORCH_SRC_DIR}/csrc/jit/operator.cpp
${TORCH_SRC_DIR}/csrc/jit/register_c10_ops.cpp
${TORCH_SRC_DIR}/csrc/jit/subgraph_matcher.cpp
${TORCH_SRC_DIR}/csrc/jit/symbolic_script.cpp
${TORCH_SRC_DIR}/csrc/jit/profiling_record.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/alias_analysis.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/batch_mm.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/canonicalize.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/constant_propagation.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/constant_pooling.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/common_subexpression_elimination.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/create_autodiff_subgraphs.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/inline_autodiff_subgraphs.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/dead_code_elimination.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/decompose_ops.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/canonicalize_ops.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/erase_number_types.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/inline_fork_wait.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/graph_fuser.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/inplace_check.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/loop_unrolling.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/lower_grad_of.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/lower_tuples.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/peephole.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/remove_expands.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/remove_inplace_ops.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/shape_analysis.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/requires_grad_analysis.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/specialize_autogradzero.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/subgraph_rewrite.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/python_print.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/utils/subgraph_utils.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/utils/check_alias_annotation.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/utils/memory_dag.cpp
${TORCH_SRC_DIR}/csrc/jit/passes/quantization.cpp
${TORCH_SRC_DIR}/csrc/jit/fuser/interface.cpp
${TORCH_SRC_DIR}/csrc/jit/register_prim_ops.cpp
${TORCH_SRC_DIR}/csrc/jit/register_special_ops.cpp
${TORCH_SRC_DIR}/csrc/jit/register_quantized_ops.cpp
${TORCH_SRC_DIR}/csrc/jit/scope.cpp
${TORCH_SRC_DIR}/csrc/jit/script/compiler.cpp
${TORCH_SRC_DIR}/csrc/api/src/jit.cpp
${TORCH_SRC_DIR}/csrc/jit/testing/file_check.cpp
${TORCH_SRC_DIR}/csrc/jit/script/final_returns.cpp
${TORCH_SRC_DIR}/csrc/jit/script/schema_matching.cpp
${TORCH_SRC_DIR}/csrc/jit/script/script_type_parser.cpp
${TORCH_SRC_DIR}/csrc/jit/script/sugared_value.cpp
${TORCH_SRC_DIR}/csrc/jit/script/class_type.cpp
${TORCH_SRC_DIR}/csrc/jit/script/parser.cpp
${TORCH_SRC_DIR}/csrc/jit/script/builtin_functions.cpp
${TORCH_SRC_DIR}/csrc/jit/script/edit_distance.cpp
${TORCH_SRC_DIR}/csrc/jit/script/logging.cpp
${TORCH_SRC_DIR}/csrc/jit/script/module.cpp
${TORCH_SRC_DIR}/csrc/jit/script/jit_exception.cpp
${TORCH_SRC_DIR}/csrc/jit/tracer.cpp
${TORCH_SRC_DIR}/csrc/jit/hooks_for_testing.cpp
${TORCH_SRC_DIR}/csrc/utils/tensor_flatten.cpp
${TORCH_SRC_DIR}/csrc/utils/variadic.cpp
${TORCH_SRC_DIR}/csrc/jit/fuser/kernel_cache.cpp
${TORCH_SRC_DIR}/csrc/jit/fuser/compiler.cpp
${TORCH_SRC_DIR}/csrc/jit/fuser/executor.cpp
${TORCH_SRC_DIR}/csrc/jit/fuser/codegen.cpp
${TORCH_SRC_DIR}/csrc/jit/fuser/fallback.cpp
${TORCH_ROOT}/test/cpp/jit/test.cpp
)
if (WIN32)
list(APPEND TORCH_SRCS
${TORCH_SRC_DIR}/csrc/jit/fuser/cpu/dynamic_library_win.cpp
)
else ()
list(APPEND TORCH_SRCS
${TORCH_SRC_DIR}/csrc/jit/fuser/cpu/dynamic_library_unix.cpp
${TORCH_SRC_DIR}/csrc/jit/fuser/cpu/fused_kernel.cpp
)
if (USE_CUDA AND NOT USE_ROCM)
list(APPEND TORCH_SRCS
${TORCH_SRC_DIR}/csrc/jit/fuser/cuda/fused_kernel.cpp
)
add_library(thnvrtc SHARED ${TORCH_SRC_DIR}/csrc/jit/fuser/cuda/thnvrtc.cpp)
target_link_libraries(thnvrtc ${CUDA_NVRTC} ${CUDA_CUDA_LIB} ${CUDA_NVRTC_LIB})
target_include_directories(thnvrtc PRIVATE ${CUDA_INCLUDE_DIRS})
install(TARGETS thnvrtc DESTINATION "${TORCH_INSTALL_LIB_DIR}")
endif()
endif ()
if (USE_CUDA)
list(APPEND TORCH_SRCS
${TORCH_SRC_DIR}/csrc/autograd/profiler_cuda.cpp
${TORCH_SRC_DIR}/csrc/autograd/functions/comm.cpp
${TORCH_SRC_DIR}/csrc/cuda/comm.cpp
)
endif()
if (USE_ROCM)
list(APPEND TORCH_SRCS
${TORCH_SRC_DIR}/csrc/cuda/comm.cpp
)
endif()
if (NOT NO_API)
list(APPEND TORCH_SRCS
${TORCH_SRC_DIR}/csrc/api/src/cuda.cpp
${TORCH_SRC_DIR}/csrc/api/src/data/datasets/mnist.cpp
${TORCH_SRC_DIR}/csrc/api/src/data/samplers/distributed.cpp
${TORCH_SRC_DIR}/csrc/api/src/data/samplers/random.cpp
${TORCH_SRC_DIR}/csrc/api/src/data/samplers/sequential.cpp
${TORCH_SRC_DIR}/csrc/api/src/data/samplers/stream.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/init.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/module.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/modules/batchnorm.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/modules/conv.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/modules/dropout.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/modules/embedding.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/modules/functional.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/modules/linear.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/modules/named_any.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/modules/rnn.cpp
${TORCH_SRC_DIR}/csrc/api/src/optim/adagrad.cpp
${TORCH_SRC_DIR}/csrc/api/src/optim/adam.cpp
${TORCH_SRC_DIR}/csrc/api/src/optim/lbfgs.cpp
${TORCH_SRC_DIR}/csrc/api/src/optim/optimizer.cpp
${TORCH_SRC_DIR}/csrc/api/src/optim/rmsprop.cpp
${TORCH_SRC_DIR}/csrc/api/src/optim/serialize.cpp
${TORCH_SRC_DIR}/csrc/api/src/optim/sgd.cpp
${TORCH_SRC_DIR}/csrc/api/src/serialize/input-archive.cpp
${TORCH_SRC_DIR}/csrc/api/src/serialize/output-archive.cpp
)
endif()
if (TORCH_STATIC)
add_library(torch STATIC ${TORCH_SRCS})
target_compile_definitions(torch PUBLIC TORCH_BUILD_STATIC_LIBS)
else()
add_library(torch SHARED ${TORCH_SRCS})
endif()
target_compile_definitions(torch PUBLIC _THP_CORE)
# until they can be unified, keep these lists synced with setup.py
if(MSVC)
target_compile_options(torch PUBLIC
${MSVC_RUNTIME_LIBRARY_OPTION}
/Z7
/EHa
/DNOMINMAX
/wd4267
/wd4251
/wd4522
/wd4522
/wd4838
/wd4305
/wd4244
/wd4190
/wd4101
/wd4996
/wd4275
/bigobj
)
else()
target_compile_options(torch PUBLIC
-std=c++11
-Wall
-Wextra
-Wno-unused-parameter
-Wno-missing-field-initializers
-Wno-write-strings
-Wno-unknown-pragmas
# Clang has an unfixed bug leading to spurious missing braces
# warnings, see https://bugs.llvm.org/show_bug.cgi?id=21629
-Wno-missing-braces
)
if(NOT APPLE)
target_compile_options(torch PRIVATE
# Considered to be flaky. See the discussion at
# https://github.com/pytorch/pytorch/pull/9608
-Wno-maybe-uninitialized)
endif()
endif()
if (MSVC)
elseif (WERROR)
target_compile_options(torch PRIVATE -Werror -Wno-strict-overflow)
endif()
if (MSVC)
target_link_libraries(torch onnx onnx_library)
endif()
target_link_libraries(torch caffe2_library)
find_package(OpenMP QUIET)
if(USE_OPENMP AND OPENMP_FOUND)
message(STATUS "pytorch is compiling with OpenMP. \n"
"OpenMP CXX_FLAGS: ${OpenMP_CXX_FLAGS}. \n"
"OpenMP libraries: ${OpenMP_CXX_LIBRARIES}.")
target_compile_options(torch INTERFACE ${OpenMP_CXX_FLAGS})
target_link_libraries(torch ${OpenMP_CXX_LIBRARIES})
endif()
if (NOT NO_API)
target_include_directories(torch PUBLIC
${TORCH_SRC_DIR}/csrc/api
${TORCH_SRC_DIR}/csrc/api/include)
endif()
if(USE_CUDA)
if(MSVC)
if (NOT NVTOOLEXT_HOME)
set(NVTOOLEXT_HOME "C:/Program Files/NVIDIA Corporation/NvToolsExt")
endif()
if ($ENV{NVTOOLEXT_HOME})
set(NVTOOLEXT_HOME $ENV{NVTOOLEXT_HOME})
endif()
set(TORCH_CUDA_LIBRARIES
${NVTOOLEXT_HOME}/lib/x64/nvToolsExt64_1.lib
${CUDA_LIBRARIES})
target_include_directories(torch PUBLIC "${NVTOOLEXT_HOME}/include")
elseif(APPLE)
set(TORCH_CUDA_LIBRARIES
${CUDA_TOOLKIT_ROOT_DIR}/lib/libcudart.dylib
${CUDA_TOOLKIT_ROOT_DIR}/lib/libnvrtc.dylib
${CUDA_TOOLKIT_ROOT_DIR}/lib/libnvToolsExt.dylib
${CUDA_LIBRARIES})
set_target_properties(torch PROPERTIES LINK_FLAGS "-undefined dynamic_lookup")
else()
find_library(LIBNVTOOLSEXT libnvToolsExt.so PATHS ${CUDA_TOOLKIT_ROOT_DIR}/lib64/)
set(TORCH_CUDA_LIBRARIES
${LIBNVTOOLSEXT}
${CUDA_LIBRARIES})
endif()
target_link_libraries(torch caffe2_gpu_library ${TORCH_CUDA_LIBRARIES})
target_compile_definitions(torch PRIVATE USE_CUDA)
endif()
if(USE_ROCM)
target_link_libraries(torch caffe2_hip_library)
target_compile_definitions(torch PRIVATE
USE_ROCM
__HIP_PLATFORM_HCC__
)
target_include_directories(torch PRIVATE
/opt/rocm/include
/opt/rocm/hcc/include
/opt/rocm/rocblas/include
/opt/rocm/hipsparse/include
)
endif()
set(TH_CPU_INCLUDE
# dense
${TORCH_ROOT}/aten/src/TH
${CMAKE_CURRENT_BINARY_DIR}/../aten/src/TH
${TORCH_ROOT}/aten/src
${CMAKE_CURRENT_BINARY_DIR}/../aten/src
${CMAKE_BINARY_DIR}/aten/src)
target_include_directories(torch PRIVATE ${TH_CPU_INCLUDE})
set(ATen_CPU_INCLUDE
${TORCH_ROOT}/aten/src
${CMAKE_CURRENT_BINARY_DIR}/../aten/src
${CMAKE_CURRENT_BINARY_DIR}/../aten/src/ATen
${CMAKE_BINARY_DIR}/aten/src)
target_include_directories(torch PUBLIC ${ATen_CPU_INCLUDE})
target_include_directories(torch PUBLIC
${TORCH_SRC_DIR}/csrc)
target_include_directories(torch PUBLIC
${TORCH_ROOT}/third_party/miniz-2.0.8)
set_target_properties(torch PROPERTIES VERSION 1 SOVERSION 1)
if(NOT ${CMAKE_VERSION} VERSION_LESS "3.1")
set_property(TARGET torch PROPERTY CXX_STANDARD 11)
endif()
# Prevent the unused functions being optimized away
# Otherwise torch.dll will be linked without caffe2_gpu.dll
if (MSVC)
set_target_properties(torch PROPERTIES LINK_FLAGS "/OPT:NOREF")
endif(MSVC)
install(DIRECTORY "${TORCH_SRC_DIR}/csrc"
DESTINATION ${TORCH_INSTALL_INCLUDE_DIR}/torch
FILES_MATCHING PATTERN "*.h")
install(FILES "${TORCH_SRC_DIR}/script.h" "${TORCH_SRC_DIR}/extension.h"
DESTINATION ${TORCH_INSTALL_INCLUDE_DIR}/torch)
install(TARGETS torch DESTINATION "${TORCH_INSTALL_LIB_DIR}")
if (MSVC AND NOT TORCH_STATIC)
install(FILES $<TARGET_PDB_FILE:torch> DESTINATION "${TORCH_INSTALL_LIB_DIR}" OPTIONAL)
endif()
if (BUILD_TEST AND NOT MSVC AND NOT USE_ROCM)
add_subdirectory(${TORCH_ROOT}/test/cpp/jit ${CMAKE_BINARY_DIR}/test_jit)
endif()
if (BUILD_TEST AND NOT NO_API)
add_subdirectory(${TORCH_ROOT}/test/cpp/api ${CMAKE_BINARY_DIR}/test_api)
endif()
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
message(STATUS "${CMAKE_CXX_COMPILER} ${CMAKE_CURRENT_LIST_DIR}/abi-check.cpp -o ${CMAKE_BINARY_DIR}/abi-check")
execute_process(
COMMAND
"${CMAKE_CXX_COMPILER}"
"${CMAKE_CURRENT_LIST_DIR}/abi-check.cpp"
"-o"
"${CMAKE_BINARY_DIR}/abi-check"
RESULT_VARIABLE ABI_CHECK_COMPILE_RESULT)
if (ABI_CHECK_COMPILE_RESULT)
message(FATAL_ERROR "Could not compile ABI Check: ${ABI_CHECK_COMPILE_RESULT}")
endif()
execute_process(
COMMAND "${CMAKE_BINARY_DIR}/abi-check"
RESULT_VARIABLE ABI_CHECK_RESULT
OUTPUT_VARIABLE GLIBCXX_USE_CXX11_ABI)
if (ABI_CHECK_RESULT)
message(WARNING "Could not run ABI Check: ${ABI_CHECK_RESULT}")
endif()
message(STATUS "Determined _GLIBCXX_USE_CXX11_ABI=${GLIBCXX_USE_CXX11_ABI}")
endif()
# CMake config for external projects.
configure_file(
${PROJECT_SOURCE_DIR}/cmake/TorchConfigVersion.cmake.in
${PROJECT_BINARY_DIR}/TorchConfigVersion.cmake
@ONLY)
configure_file(
${TORCH_ROOT}/cmake/TorchConfig.cmake.in
${PROJECT_BINARY_DIR}/TorchConfig.cmake
@ONLY)
install(FILES
${PROJECT_BINARY_DIR}/TorchConfigVersion.cmake
${PROJECT_BINARY_DIR}/TorchConfig.cmake
DESTINATION share/cmake/Torch)
if (USE_DISTRIBUTED)
add_subdirectory(${TORCH_SRC_DIR}/lib/THD)
if (NOT MSVC AND NOT APPLE)
add_subdirectory(${TORCH_SRC_DIR}/lib/c10d)
endif()
endif()
if (BUILD_PYTHON)
if (MSVC)
add_subdirectory(${TORCH_SRC_DIR}/lib/libshm_windows)
else()
add_subdirectory(${TORCH_SRC_DIR}/lib/libshm)
endif()
set(TORCH_PYTHON_SRCS
set(TORCH_PYTHON_SRCS
${GENERATED_THNN_CXX}
${GENERATED_CXX_PYTHON}
${TORCH_SRC_DIR}/csrc/CudaIPCTypes.cpp
${TORCH_SRC_DIR}/csrc/DataLoader.cpp
${TORCH_SRC_DIR}/csrc/Device.cpp
@ -517,10 +52,6 @@ if (BUILD_PYTHON)
${TORCH_SRC_DIR}/csrc/Storage.cpp
${TORCH_SRC_DIR}/csrc/api/src/python/init.cpp
${TORCH_SRC_DIR}/csrc/autograd/functions/init.cpp
${TORCH_SRC_DIR}/csrc/autograd/generated/python_functions.cpp
${TORCH_SRC_DIR}/csrc/autograd/generated/python_nn_functions.cpp
${TORCH_SRC_DIR}/csrc/autograd/generated/python_torch_functions.cpp
${TORCH_SRC_DIR}/csrc/autograd/generated/python_variable_methods.cpp
${TORCH_SRC_DIR}/csrc/autograd/init.cpp
${TORCH_SRC_DIR}/csrc/autograd/python_anomaly_mode.cpp
${TORCH_SRC_DIR}/csrc/autograd/python_cpp_function.cpp
@ -545,7 +76,6 @@ if (BUILD_PYTHON)
${TORCH_SRC_DIR}/csrc/jit/script/python_sugared_value.cpp
${TORCH_SRC_DIR}/csrc/jit/script/python_tree_views.cpp
${TORCH_SRC_DIR}/csrc/multiprocessing/init.cpp
${TORCH_SRC_DIR}/csrc/nn/THNN.cpp
${TORCH_SRC_DIR}/csrc/onnx/init.cpp
${TORCH_SRC_DIR}/csrc/serialization.cpp
${TORCH_SRC_DIR}/csrc/tensor/python_tensor.cpp
@ -565,7 +95,7 @@ if (BUILD_PYTHON)
${TORCH_SRC_DIR}/csrc/utils/tuple_parser.cpp
)
set(TORCH_PYTHON_INCLUDE_DIRECTORIES
set(TORCH_PYTHON_INCLUDE_DIRECTORIES
${PYTHON_INCLUDE_DIR}
${TORCH_ROOT}
@ -588,40 +118,35 @@ if (BUILD_PYTHON)
${TORCH_SRC_DIR}/lib
)
if (MSVC)
list(APPEND TORCH_PYTHON_INCLUDE_DIRECTORIES
${TORCH_SRC_DIR}/lib/libshm_windows)
else()
list(APPEND TORCH_PYTHON_INCLUDE_DIRECTORIES
${TORCH_SRC_DIR}/lib/libshm)
endif()
set(TORCH_PYTHON_LINK_LIBRARIES
torch
list(APPEND TORCH_PYTHON_INCLUDE_DIRECTORIES ${LIBSHM_SRCDIR})
set(TORCH_PYTHON_LINK_LIBRARIES
caffe2_library
shm)
set(TORCH_PYTHON_COMPILE_DEFINITIONS)
set(TORCH_PYTHON_COMPILE_DEFINITIONS)
set(TORCH_PYTHON_COMPILE_OPTIONS)
set(TORCH_PYTHON_COMPILE_OPTIONS)
set(TORCH_PYTHON_LINK_FLAGS "")
set(TORCH_PYTHON_LINK_FLAGS "")
if (MSVC)
if (MSVC)
string(APPEND TORCH_PYTHON_LINK_FLAGS " /NODEFAULTLIB:LIBCMT.LIB")
list(APPEND TORCH_PYTHON_LINK_LIBRARIES ${PYTHON_LIBRARIES})
if (NOT ${CMAKE_BUILD_TYPE} MATCHES "Release")
string(APPEND TORCH_PYTHON_LINK_FLAGS " /DEBUG:FULL")
endif()
elseif (APPLE)
elseif (APPLE)
string(APPEND TORCH_PYTHON_LINK_FLAGS " -undefined dynamic_lookup")
else()
else()
list(APPEND TORCH_PYTHON_COMPILE_OPTIONS
-fno-strict-aliasing
-Wno-write-strings
-Wno-strict-aliasing)
endif()
endif()
if (USE_CUDA)
if (USE_CUDA)
list(APPEND TORCH_PYTHON_SRCS
${TORCH_SRC_DIR}/csrc/cuda/Module.cpp
${TORCH_SRC_DIR}/csrc/cuda/Storage.cpp
@ -630,7 +155,7 @@ if (BUILD_PYTHON)
${TORCH_SRC_DIR}/csrc/cuda/utils.cpp
${TORCH_SRC_DIR}/csrc/cuda/python_comm.cpp
${TORCH_SRC_DIR}/csrc/cuda/serialization.cpp
${TORCH_SRC_DIR}/csrc/nn/THCUNN.cpp
${GENERATED_THNN_CXX_CUDA}
)
list(APPEND TORCH_PYTHON_COMPILE_DEFINITIONS USE_CUDA)
@ -643,9 +168,11 @@ if (BUILD_PYTHON)
find_library(LIBNVTOOLSEXT libnvToolsExt.so PATHS ${CUDA_TOOLKIT_ROOT_DIR}/lib64/)
list(APPEND TORCH_PYTHON_LINK_LIBRARIES ${LIBNVTOOLSEXT})
endif()
endif()
if (USE_CUDNN)
list(APPEND TORCH_PYTHON_LINK_LIBRARIES caffe2_gpu_library)
endif()
if (USE_CUDNN)
list(APPEND TORCH_PYTHON_COMPILE_DEFINITIONS USE_CUDNN)
# NOTE: these are at the front, in case there's another cuDNN in
@ -655,19 +182,19 @@ if (BUILD_PYTHON)
# we're not careful.
list(INSERT 0 TORCH_PYTHON_LINK_LIBRARIES ${CUDNN_LIBRARY})
list(INSERT 0 TORCH_PYTHON_INCLUDE_DIRECTORIES ${CUDNN_INCLUDE_DIR})
endif()
endif()
if (USE_MIOPEN)
if (USE_MIOPEN)
list(APPEND TORCH_PYTHON_COMPILE_DEFINITIONS USE_MIOPEN)
list(APPEND TORCH_PYTHON_INCLUDE_DIRECTORIES ${MIOPEN_INCLUDE_DIR})
list(APPEND TORCH_PYTHON_LINK_LIBRARIES ${MIOPEN_LIBRARY})
endif()
endif()
if (USE_NUMPY)
if (USE_NUMPY)
list(APPEND TORCH_PYTHON_COMPILE_DEFINITIONS USE_NUMPY)
endif()
endif()
if (USE_ROCM)
if (USE_ROCM)
list(APPEND TORCH_PYTHON_SRCS
${TORCH_SRC_DIR}/csrc/cuda/Module.cpp
${TORCH_SRC_DIR}/csrc/cuda/Storage.cpp
@ -676,16 +203,16 @@ if (BUILD_PYTHON)
${TORCH_SRC_DIR}/csrc/cuda/utils.cpp
${TORCH_SRC_DIR}/csrc/cuda/python_comm.cpp
${TORCH_SRC_DIR}/csrc/cuda/serialization.cpp
${TORCH_SRC_DIR}/csrc/nn/THCUNN.cpp
${GENERATED_THNN_CXX_CUDA}
)
list(APPEND TORCH_PYTHON_LINK_LIBRARIES caffe2_hip_library)
list(APPEND TORCH_PYTHON_COMPILE_DEFINITIONS
USE_ROCM
__HIP_PLATFORM_HCC__
)
endif()
endif()
if (USE_DISTRIBUTED)
if (USE_DISTRIBUTED)
list(APPEND TORCH_PYTHON_SRCS ${TORCH_SRC_DIR}/csrc/distributed/Module.cpp)
list(APPEND TORCH_PYTHON_INCLUDE_DIRECTORIES ${TORCH_SRC_DIR}/lib/THD)
list(APPEND TORCH_PYTHON_LINK_LIBRARIES THD)
@ -702,9 +229,9 @@ if (BUILD_PYTHON)
list(APPEND TORCH_PYTHON_SRCS ${TORCH_SRC_DIR}/csrc/distributed/c10d/ddp.cpp)
endif()
endif()
endif()
endif()
if (USE_NCCL)
if (USE_NCCL)
list(APPEND TORCH_PYTHON_SRCS
${TORCH_SRC_DIR}/csrc/cuda/nccl.cpp
${TORCH_SRC_DIR}/csrc/cuda/python_nccl.cpp)
@ -712,12 +239,16 @@ if (BUILD_PYTHON)
list(APPEND TORCH_PYTHON_LINK_LIBRARIES __caffe2_nccl)
if (USE_SYSTEM_NCCL)
endif()
endif()
endif()
add_custom_target(torch_python_stubs DEPENDS "${TORCH_SRC_DIR}/__init__.pyi")
# For Declarations.yaml dependency
add_dependencies(torch_python_stubs ATEN_CPU_FILES_GEN_TARGET)
add_custom_command(
add_custom_target(torch_python_stubs DEPENDS "${TORCH_SRC_DIR}/__init__.pyi")
# For Declarations.yaml dependency
add_dependencies(torch_python_stubs ATEN_CPU_FILES_GEN_TARGET)
add_custom_command(
OUTPUT
"${TORCH_SRC_DIR}/__init__.pyi"
COMMAND
@ -730,21 +261,34 @@ if (BUILD_PYTHON)
"${TORCH_ROOT}"
)
add_library(torch_python SHARED ${TORCH_PYTHON_SRCS})
add_dependencies(torch_python torch_python_stubs)
target_link_libraries(torch_python ${TORCH_PYTHON_LINK_LIBRARIES})
add_library(torch_python SHARED ${TORCH_PYTHON_SRCS})
target_compile_definitions(torch_python PRIVATE ${TORCH_PYTHON_COMPILE_DEFINITIONS})
# Required workaround for generated sources
# See https://samthursfield.wordpress.com/2015/11/21/cmake-dependencies-between-targets-and-files-and-custom-commands/#custom-commands-in-different-directories
add_dependencies(torch_python generate-torch-sources)
set_source_files_properties(
${GENERATED_THNN_SOURCES}
${GENERATED_CXX_PYTHON}
PROPERTIES GENERATED TRUE
)
target_compile_options(torch_python PRIVATE ${TORCH_PYTHON_COMPILE_OPTIONS})
target_compile_definitions(torch_python PUBLIC _THP_CORE)
target_include_directories(torch_python PUBLIC ${TORCH_PYTHON_INCLUDE_DIRECTORIES})
add_dependencies(torch_python torch_python_stubs)
if (NOT TORCH_PYTHON_LINK_FLAGS STREQUAL "")
target_link_libraries(torch_python ${TORCH_PYTHON_LINK_LIBRARIES})
target_compile_definitions(torch_python PRIVATE ${TORCH_PYTHON_COMPILE_DEFINITIONS})
target_compile_options(torch_python PRIVATE ${TORCH_PYTHON_COMPILE_OPTIONS})
target_include_directories(torch_python PUBLIC ${TORCH_PYTHON_INCLUDE_DIRECTORIES})
if (NOT TORCH_PYTHON_LINK_FLAGS STREQUAL "")
set_target_properties(torch_python PROPERTIES LINK_FLAGS ${TORCH_PYTHON_LINK_FLAGS})
endif()
install(TARGETS torch_python DESTINATION "${TORCH_INSTALL_LIB_DIR}")
endif()
install(TARGETS torch_python DESTINATION "${TORCH_INSTALL_LIB_DIR}")

View File

@ -1,21 +1,7 @@
#pragma once
#ifdef _WIN32
#if !defined(TORCH_BUILD_STATIC_LIBS)
#if defined(torch_EXPORTS)
#define TORCH_API __declspec(dllexport)
#else
#define TORCH_API __declspec(dllimport)
#endif
#else
#define TORCH_API
#endif
#elif defined(__GNUC__)
#if defined(torch_EXPORTS)
#define TORCH_API __attribute__((__visibility__("default")))
#else
#define TORCH_API
#endif
#else
#define TORCH_API
#endif
#include <c10/macros/Export.h>
// There's no difference between aten, torch and caffe2 libs any more
// TODO: clean up the naming for consistency
#define TORCH_API CAFFE2_API

View File

@ -15,7 +15,7 @@ namespace torch {
namespace autograd {
//TODO: change it to TORCH_API when we merge the libs
struct TORCH_API Scatter : public Function {
struct AT_CUDA_API Scatter : public Function {
explicit Scatter(
std::vector<at::Device> devices,
const c10::optional<std::vector<int64_t>>& chunk_sizes = c10::nullopt,
@ -34,7 +34,7 @@ struct TORCH_API Scatter : public Function {
bool unsqueeze_scalars_;
};
struct TORCH_API Gather : public Function {
struct AT_CUDA_API Gather : public Function {
explicit Gather(const at::Device& destination_device, int64_t dim = 0);
~Gather() override;

View File

@ -47,11 +47,11 @@ class AliasDb {
// Does `a` and `b` potentially share a memory location or do either
// hold in memory any element that exists in the other
bool mayContainAlias(Value* a, Value* b) const;
TORCH_API bool mayContainAlias(Value* a, Value* b) const;
// Do any values in group `a` share a memory location or hold in memory
// any element that exists in group `b`
bool mayContainAlias(
TORCH_API bool mayContainAlias(
const at::ArrayRef<Value*>& a,
const at::ArrayRef<Value*>& b) const;
@ -206,7 +206,7 @@ class AliasDb {
*/
void makeAllAlias(const std::vector<Value*>& values);
void makePointerTo(const Value* value, const Value* to);
void addToContainedElements(const Value* element, const Value* container);
TORCH_API void addToContainedElements(const Value* element, const Value* container);
void mapAliases(at::ArrayRef<Value*> to, at::ArrayRef<Value*> from);
void giveFreshAlias(const Value* value);
Element* getOrCreateElement(const Value* value);

View File

@ -45,7 +45,7 @@ class MemoryDAG {
TORCH_API bool mayAlias(Element* a, Element* b) const;
// Does a hold reference to any memory that is stored in elem, or vice versa?
bool mayContainAlias(const Element* a, const Element* b) const;
TORCH_API bool mayContainAlias(const Element* a, const Element* b) const;
bool mayContainAlias(Element* a, Element* b) const;
bool mayContainAlias(

View File

@ -429,7 +429,6 @@ def CppExtension(name, sources, *args, **kwargs):
libraries = kwargs.get('libraries', [])
libraries.append('c10')
libraries.append('caffe2')
libraries.append('torch')
libraries.append('torch_python')
libraries.append('_C')
kwargs['libraries'] = libraries
@ -476,7 +475,6 @@ def CUDAExtension(name, sources, *args, **kwargs):
libraries.append('c10')
libraries.append('c10_cuda')
libraries.append('caffe2')
libraries.append('torch')
libraries.append('torch_python')
libraries.append('caffe2_gpu')
libraries.append('_C')
@ -891,7 +889,6 @@ def _prepare_ldflags(extra_ldflags, with_cuda, verbose):
extra_ldflags.append('c10.lib')
extra_ldflags.append('caffe2.lib')
extra_ldflags.append('torch.lib')
extra_ldflags.append('torch_python.lib')
if with_cuda:
extra_ldflags.append('caffe2_gpu.lib')