pytorch

Форк
0
/
TorchConfig.cmake.in 
190 строк · 6.0 Кб
1
# FindTorch
2
# -------
3
#
4
# Finds the Torch library
5
#
6
# This will define the following variables:
7
#
8
#   TORCH_FOUND        -- True if the system has the Torch library
9
#   TORCH_INCLUDE_DIRS -- The include directories for torch
10
#   TORCH_LIBRARIES    -- Libraries to link against
11
#   TORCH_CXX_FLAGS    -- Additional (required) compiler flags
12
#
13
# and the following imported targets:
14
#
15
#   torch
16
macro(append_torchlib_if_found)
17
  foreach (_arg ${ARGN})
18
    find_library(${_arg}_LIBRARY ${_arg} PATHS "${TORCH_INSTALL_PREFIX}/lib")
19
    if(${_arg}_LIBRARY)
20
      list(APPEND TORCH_LIBRARIES ${${_arg}_LIBRARY})
21
    else()
22
      message(WARNING "static library ${${_arg}_LIBRARY} not found.")
23
    endif()
24
  endforeach()
25
endmacro()
26

27
macro(append_wholearchive_lib_if_found)
28
  foreach (_arg ${ARGN})
29
    find_library(${_arg}_LIBRARY ${_arg} PATHS "${TORCH_INSTALL_PREFIX}/lib")
30
    if(${_arg}_LIBRARY)
31
      if(APPLE)
32
        list(APPEND TORCH_LIBRARIES "-Wl,-force_load,${${_arg}_LIBRARY}")
33
      elseif(MSVC)
34
        list(APPEND TORCH_LIBRARIES "-WHOLEARCHIVE:${${_arg}_LIBRARY}")
35
      else()
36
        # Linux
37
        list(APPEND TORCH_LIBRARIES "-Wl,--whole-archive ${${_arg}_LIBRARY} -Wl,--no-whole-archive")
38
      endif()
39
    else()
40
      message(WARNING "static library ${${_arg}_LIBRARY} not found.")
41
    endif()
42
  endforeach()
43
endmacro()
44

45
include(FindPackageHandleStandardArgs)
46

47
if(DEFINED ENV{TORCH_INSTALL_PREFIX})
48
  set(TORCH_INSTALL_PREFIX $ENV{TORCH_INSTALL_PREFIX})
49
else()
50
  # Assume we are in <install-prefix>/share/cmake/Torch/TorchConfig.cmake
51
  get_filename_component(CMAKE_CURRENT_LIST_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
52
  get_filename_component(TORCH_INSTALL_PREFIX "${CMAKE_CURRENT_LIST_DIR}/../../../" ABSOLUTE)
53
endif()
54

55
# Include directories.
56
if(EXISTS "${TORCH_INSTALL_PREFIX}/include")
57
  set(TORCH_INCLUDE_DIRS
58
    ${TORCH_INSTALL_PREFIX}/include
59
    ${TORCH_INSTALL_PREFIX}/include/torch/csrc/api/include)
60
else()
61
  set(TORCH_INCLUDE_DIRS
62
    ${TORCH_INSTALL_PREFIX}/include
63
    ${TORCH_INSTALL_PREFIX}/include/torch/csrc/api/include)
64
endif()
65

66
# Library dependencies.
67
if(@BUILD_SHARED_LIBS@)
68
  find_package(Caffe2 REQUIRED PATHS ${CMAKE_CURRENT_LIST_DIR}/../Caffe2)
69
  set(TORCH_LIBRARIES torch ${Caffe2_MAIN_LIBS})
70
  append_torchlib_if_found(c10)
71
else()
72
  add_library(torch STATIC IMPORTED) # set imported_location at the bottom
73
  #library need whole archive
74
  append_wholearchive_lib_if_found(torch torch_cpu)
75
  if(@USE_CUDA@)
76
    append_wholearchive_lib_if_found(torch_cuda c10_cuda)
77
  endif()
78

79
  # We need manually add dependent libraries when they are not linked into the
80
  # shared library.
81
  # TODO: this list might be incomplete.
82
  append_torchlib_if_found(c10)
83
  if(@BUILD_CAFFE2@)
84
    append_torchlib_if_found(Caffe2_perfkernels_avx512 Caffe2_perfkernels_avx2 Caffe2_perfkernels_avx)
85
  endif()
86

87
  if(@USE_NNPACK@)
88
    append_torchlib_if_found(nnpack)
89
  endif()
90

91
  if(@USE_PYTORCH_QNNPACK@)
92
    append_torchlib_if_found(pytorch_qnnpack)
93
  endif()
94

95
  if(@USE_QNNPACK@)
96
    append_torchlib_if_found(qnnpack)
97
  endif()
98

99
  if(@USE_XNNPACK@)
100
    append_torchlib_if_found(XNNPACK)
101
  endif()
102

103
  append_torchlib_if_found(caffe2_protos protobuf-lite protobuf protoc)
104
  append_torchlib_if_found(onnx onnx_proto)
105

106
  append_torchlib_if_found(foxi_loader fmt)
107
  append_torchlib_if_found(cpuinfo clog)
108

109
  if(NOT @USE_INTERNAL_PTHREADPOOL_IMPL@)
110
    append_torchlib_if_found(pthreadpool)
111
  endif()
112

113
  append_torchlib_if_found(eigen_blas)
114

115
  if(@USE_FBGEMM@)
116
    append_torchlib_if_found(fbgemm)
117
  endif()
118

119
  if(@USE_MKLDNN@)
120
    append_torchlib_if_found(dnnl mkldnn)
121
  endif()
122

123
  append_torchlib_if_found(sleef asmjit)
124
endif()
125

126
if(@USE_KINETO@)
127
  append_torchlib_if_found(kineto)
128
endif()
129

130
if(@USE_CUDA@)
131
  if(MSVC)
132
    if(NOT NVTOOLEXT_HOME)
133
      set(NVTOOLEXT_HOME "C:/Program Files/NVIDIA Corporation/NvToolsExt")
134
    endif()
135
    if(DEFINED ENV{NVTOOLSEXT_PATH})
136
      set(NVTOOLEXT_HOME $ENV{NVTOOLSEXT_PATH})
137
    endif()
138
    set(TORCH_CUDA_LIBRARIES
139
      ${NVTOOLEXT_HOME}/lib/x64/nvToolsExt64_1.lib
140
      ${CUDA_LIBRARIES})
141
    list(APPEND TORCH_INCLUDE_DIRS ${NVTOOLEXT_HOME}/include)
142
    find_library(CAFFE2_NVRTC_LIBRARY caffe2_nvrtc PATHS "${TORCH_INSTALL_PREFIX}/lib")
143
    list(APPEND TORCH_CUDA_LIBRARIES ${CAFFE2_NVRTC_LIBRARY})
144
  elseif(APPLE)
145
    set(TORCH_CUDA_LIBRARIES
146
      ${CUDA_TOOLKIT_ROOT_DIR}/lib/libcudart.dylib
147
      ${CUDA_TOOLKIT_ROOT_DIR}/lib/libnvrtc.dylib
148
      ${CUDA_TOOLKIT_ROOT_DIR}/lib/libnvToolsExt.dylib
149
      ${CUDA_LIBRARIES})
150
  else()
151
    find_library(LIBNVTOOLSEXT libnvToolsExt.so PATHS ${CUDA_TOOLKIT_ROOT_DIR}/lib64/)
152
    set(TORCH_CUDA_LIBRARIES
153
      ${CUDA_CUDA_LIB}
154
      ${CUDA_NVRTC_LIB}
155
      ${LIBNVTOOLSEXT}
156
      ${CUDA_LIBRARIES})
157
  endif()
158
  if(@BUILD_SHARED_LIBS@)
159
    find_library(C10_CUDA_LIBRARY c10_cuda PATHS "${TORCH_INSTALL_PREFIX}/lib")
160
    list(APPEND TORCH_CUDA_LIBRARIES ${C10_CUDA_LIBRARY})
161
  endif()
162
  list(APPEND TORCH_LIBRARIES ${TORCH_CUDA_LIBRARIES})
163
endif()
164

165
# When we build libtorch with the old libstdc++ ABI, dependent libraries must too.
166
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
167
  set(TORCH_CXX_FLAGS "-D_GLIBCXX_USE_CXX11_ABI=@GLIBCXX_USE_CXX11_ABI@")
168
endif()
169

170
find_library(TORCH_LIBRARY torch PATHS "${TORCH_INSTALL_PREFIX}/lib")
171
# the statements below changes target properties on
172
# - the imported target from Caffe2Targets.cmake in shared library mode (see the find_package above)
173
#    - this is untested whether it is the correct (or desired) methodology in CMake
174
# - the imported target created in this file in static library mode
175
if(NOT @BUILD_SHARED_LIBS@)
176
  # do not set this property on the shared library target, as it will cause confusion in some builds
177
  # as the configuration specific property is set in the Caffe2Targets.cmake file
178
  set_target_properties(torch PROPERTIES
179
      IMPORTED_LOCATION "${TORCH_LIBRARY}"
180
  )
181
endif()
182
set_target_properties(torch PROPERTIES
183
    INTERFACE_INCLUDE_DIRECTORIES "${TORCH_INCLUDE_DIRS}"
184
    CXX_STANDARD 17
185
)
186
if(TORCH_CXX_FLAGS)
187
  set_property(TARGET torch PROPERTY INTERFACE_COMPILE_OPTIONS "${TORCH_CXX_FLAGS}")
188
endif()
189

190
find_package_handle_standard_args(Torch DEFAULT_MSG TORCH_LIBRARY TORCH_INCLUDE_DIRS)
191

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.