You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
3.5 KiB

  1. if(TORCH)
  2. set(TORCH_VERSION "2.1.0")
  3. add_definitions(-DUSE_TORCH)
  4. if(NOT ANDROID)
  5. if(GPU)
  6. if (NOT ${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
  7. message(FATAL_ERROR "GPU is supported only Linux, you can use CPU version")
  8. else()
  9. add_definitions(-DUSE_GPU)
  10. endif()
  11. endif()
  12. if(${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
  13. if(${CMAKE_BUILD_TYPE} MATCHES "Release")
  14. set(LIBTORCH_URL "https://download.pytorch.org/libtorch/cpu/libtorch-win-shared-with-deps-${TORCH_VERSION}%2Bcpu.zip")
  15. set(URL_HASH "SHA256=77815aa799f15e91b6fbb0216ac78cc0479adb5cd0ca662072241484cf23f667")
  16. else()
  17. set(LIBTORCH_URL "https://download.pytorch.org/libtorch/cpu/libtorch-win-shared-with-deps-debug-${TORCH_VERSION}%2Bcpu.zip")
  18. set(URL_HASH "SHA256=5f887c02d9abf805c8b53fef89bf5a4dab9dd78771754344e73c98d9c484aa9d")
  19. endif()
  20. elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
  21. if(CXX11_ABI)
  22. if(NOT GPU)
  23. set(LIBTORCH_URL "https://download.pytorch.org/libtorch/cpu/libtorch-cxx11-abi-shared-with-deps-${TORCH_VERSION}%2Bcpu.zip")
  24. set(URL_HASH "SHA256=04f699d5181048b0062ef52de1df44b46859b8fbeeee12abdbcb9aac63e2a14b")
  25. else()
  26. set(LIBTORCH_URL "https://download.pytorch.org/libtorch/cu118/libtorch-cxx11-abi-shared-with-deps-${TORCH_VERSION}%2Bcu118.zip")
  27. set(URL_HASH "SHA256=7796249faa9828a53b72d3f616fc97a1d9e87e6a35ac72b392ca1ddc7b125188")
  28. endif()
  29. else()
  30. if(NOT GPU)
  31. set(LIBTORCH_URL "https://download.pytorch.org/libtorch/cpu/libtorch-shared-with-deps-${TORCH_VERSION}%2Bcpu.zip")
  32. set(URL_HASH "SHA256=0e86d364d05b83c6c66c3bb32e7eee932847843e4085487eefd9b3bbde4e2c58")
  33. else()
  34. set(LIBTORCH_URL "https://download.pytorch.org/libtorch/cu118/libtorch-shared-with-deps-${TORCH_VERSION}%2Bcu118.zip")
  35. set(URL_HASH "SHA256=f70cfae25b02ff419e1d51ad137a746941773d2c4b0155a44b4b6b50702d661a")
  36. endif()
  37. endif()
  38. elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")
  39. set(LIBTORCH_URL "https://download.pytorch.org/libtorch/cpu/libtorch-macos-${TORCH_VERSION}.zip")
  40. set(URL_HASH "SHA256=ce744d2d27a96df8f34d4227e8b1179dad5a76612dc7230b61db65affce6e7bd")
  41. elseif(${CMAKE_SYSTEM_NAME} STREQUAL "iOS")
  42. add_definitions(-DIOS)
  43. else()
  44. message(FATAL_ERROR "Unsupported System '${CMAKE_SYSTEM_NAME}' (expected 'Windows', 'Linux', 'Darwin' or 'iOS')")
  45. endif()
  46. # iOS use LibTorch from pod install
  47. if(NOT IOS)
  48. FetchContent_Declare(libtorch
  49. URL ${LIBTORCH_URL}
  50. URL_HASH ${URL_HASH}
  51. )
  52. FetchContent_MakeAvailable(libtorch)
  53. find_package(Torch REQUIRED PATHS ${libtorch_SOURCE_DIR} NO_DEFAULT_PATH)
  54. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS} -DC10_USE_GLOG")
  55. endif()
  56. if(MSVC)
  57. file(GLOB TORCH_DLLS "${TORCH_INSTALL_PREFIX}/lib/*.dll")
  58. file(COPY ${TORCH_DLLS} DESTINATION ${CMAKE_BINARY_DIR})
  59. endif()
  60. else()
  61. # Change version in runtime/android/app/build.gradle.
  62. file(GLOB PYTORCH_INCLUDE_DIRS "${build_DIR}/pytorch_android*.aar/headers")
  63. file(GLOB PYTORCH_LINK_DIRS "${build_DIR}/pytorch_android*.aar/jni/${ANDROID_ABI}")
  64. find_library(PYTORCH_LIBRARY pytorch_jni
  65. PATHS ${PYTORCH_LINK_DIRS}
  66. NO_CMAKE_FIND_ROOT_PATH
  67. )
  68. find_library(FBJNI_LIBRARY fbjni
  69. PATHS ${PYTORCH_LINK_DIRS}
  70. NO_CMAKE_FIND_ROOT_PATH
  71. )
  72. include_directories(
  73. ${PYTORCH_INCLUDE_DIRS}
  74. ${PYTORCH_INCLUDE_DIRS}/torch/csrc/api/include
  75. )
  76. endif()
  77. endif()