xmake.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package("libtorch")
  2. set_homepage("https://pytorch.org/")
  3. set_description("An open source machine learning framework that accelerates the path from research prototyping to production deployment.")
  4. set_license("BSD-3-Clause")
  5. add_urls("https://github.com/pytorch/pytorch.git")
  6. add_versions("v1.8.0", "37c1f4a7fef115d719104e871d0cf39434aa9d56")
  7. add_versions("v1.8.1", "56b43f4fec1f76953f15a627694d4bba34588969")
  8. add_versions("v1.8.2", "e0495a7aa104471d95dc85a1b8f6473fbcc427a8")
  9. add_versions("v1.9.0", "d69c22dd61a2f006dcfe1e3ea8468a3ecaf931aa")
  10. add_versions("v1.9.1", "dfbd030854359207cb3040b864614affeace11ce")
  11. add_versions("v1.11.0", "bc2c6edaf163b1a1330e37a6e34caf8c553e4755")
  12. add_versions("v1.12.1", "664058fa83f1d8eede5d66418abff6e20bd76ca8")
  13. add_versions("v2.1.0", "7bcf7da3a268b435777fe87c7794c382f444e86d")
  14. add_versions("v2.1.2", "a8e7c98cb95ff97bb30a728c6b2a1ce6bff946eb")
  15. add_versions("v2.2.2", "39901f229520a5256505ec24782f716ee7ddc843")
  16. add_patches("1.9.x", "patches/1.9.0/gcc11.patch", "4191bb3296f18f040c230d7c5364fb160871962d6278e4ae0f8bc481f27d8e4b")
  17. add_patches("1.11.0", "patches/1.11.0/gcc11.patch", "1404b0bc6ce7433ecdc59d3412e3d9ed507bb5fd2cd59134a254d7d4a8d73012")
  18. -- Fix compile on macOS. Refer to https://github.com/pytorch/pytorch/pull/80916
  19. add_patches("1.12.1", "patches/1.12.1/clang.patch", "cdc3e00b2fea847678b1bcc6b25a4dbd924578d8fb25d40543521a09aab2f7d4")
  20. add_patches("1.12.1", "patches/1.12.1/vs2022.patch", "5a31b9772793c943ca752c92d6415293f7b3863813ca8c5eb9d92a6156afd21d")
  21. add_patches("2.2.2", "patches/2.2.2/pocketfft.patch", "8b756d867fb60839dcaeb1ee0bdf4189ee95e7f5c6f3810f8cbc8f6a5fae60e9")
  22. add_configs("shared", {description = "Build shared library.", default = true, type = "boolean"})
  23. add_configs("python", {description = "Build python interface.", default = false, type = "boolean"})
  24. add_configs("openmp", {description = "Use OpenMP for parallel code.", default = true, type = "boolean"})
  25. add_configs("cuda", {description = "Enable CUDA support.", default = false, type = "boolean"})
  26. -- https://github.com/pytorch/pytorch/issues/24186 only ninja is supported on windows
  27. add_configs("ninja", {description = "Use ninja as build tool.", default = is_plat("windows"), type = "boolean"})
  28. add_configs("blas", {description = "Set BLAS vendor.", default = "openblas", type = "string", values = {"mkl", "openblas", "eigen"}})
  29. add_configs("pybind11", {description = "Use pybind11 from xrepo.", default = false, type = "boolean"})
  30. add_configs("protobuf-cpp", {description = "Use protobuf from xrepo.", default = false, type = "boolean"})
  31. if not is_plat("macosx") then
  32. add_configs("distributed", {description = "Enable distributed support.", default = false, type = "boolean"})
  33. end
  34. add_deps("cmake")
  35. add_deps("python 3.x", {kind = "binary"})
  36. add_includedirs("include")
  37. add_includedirs("include/torch/csrc/api/include")
  38. if is_plat("linux") then
  39. add_syslinks("rt")
  40. end
  41. -- enable long paths for git submodule on windows
  42. if is_host("windows") and set_policy then
  43. set_policy("platform.longpaths", true)
  44. end
  45. on_load("windows|x64", "macosx", "linux", function (package)
  46. if package:config("ninja") then
  47. package:add("deps", "ninja")
  48. end
  49. if package:config("openmp") then
  50. package:add("deps", "openmp")
  51. end
  52. if package:config("cuda") then
  53. package:add("deps", "cuda", {configs = {utils = {"nvrtc", "cudnn", "cufft", "curand", "cublas", "cudart_static"}}})
  54. package:add("deps", "nvtx")
  55. end
  56. if package:config("distributed") then
  57. package:add("deps", "libuv")
  58. end
  59. if not package:is_plat("macosx") and package:config("blas") then
  60. package:add("deps", package:config("blas"))
  61. end
  62. if package:config("pybind11") then
  63. package:add("deps", "pybind11")
  64. end
  65. if package:config("protobuf-cpp") then
  66. package:add("deps", "protobuf-cpp")
  67. end
  68. end)
  69. on_install("windows|x64", "macosx", "linux", function (package)
  70. import("package.tools.cmake")
  71. import("core.tool.toolchain")
  72. if package:is_plat("windows") then
  73. local vs = toolchain.load("msvc"):config("vs")
  74. if tonumber(vs) < 2019 then
  75. raise("Your compiler is too old to use this library.")
  76. end
  77. end
  78. -- tackle link flags
  79. local libnames = {"torch", "torch_cpu"}
  80. if package:config("cuda") then
  81. table.insert(libnames, "torch_cuda")
  82. end
  83. table.insert(libnames, "c10")
  84. if package:config("cuda") then
  85. table.insert(libnames, "c10_cuda")
  86. end
  87. local suffix = ""
  88. if not package:is_plat("windows") and package:config("shared") then
  89. package:add("ldflags", "-Wl,-rpath," .. package:installdir("lib"))
  90. if package:is_plat("linux") then
  91. suffix = ".so"
  92. elseif package:is_plat("macosx") then
  93. suffix = ".dylib"
  94. end
  95. for _, lib in ipairs(libnames) do
  96. package:add("ldflags", (package:is_plat("linux") and "-Wl,--no-as-needed," or "") .. package:installdir("lib", "lib") .. lib .. suffix)
  97. end
  98. else
  99. for _, lib in ipairs(libnames) do
  100. package:add("links", lib)
  101. end
  102. end
  103. if not package:config("shared") then
  104. for _, lib in ipairs({"nnpack", "pytorch_qnnpack", "qnnpack", "XNNPACK", "caffe2_protos", "protobuf-lite", "protobuf", "protoc", "onnx", "onnx_proto", "foxi_loader", "pthreadpool", "eigen_blas", "fbgemm", "cpuinfo", "clog", "dnnl_graph", "dnnl", "mkldnn", "sleef", "asmjit", "fmt", "kineto"}) do
  105. package:add("links", lib)
  106. end
  107. end
  108. -- some patches to the third-party cmake files
  109. io.replace("cmake/MiscCheck.cmake", "if(UNIX)", "if(TRUE)", {plain = true})
  110. io.replace("third_party/fbgemm/CMakeLists.txt", "PRIVATE FBGEMM_STATIC", "PUBLIC FBGEMM_STATIC", {plain = true})
  111. io.replace("third_party/protobuf/cmake/install.cmake", "install%(DIRECTORY.-%)", "")
  112. if package:is_plat("windows") then
  113. if package:config("vs_runtime"):startswith("MD") then
  114. io.replace("third_party/fbgemm/CMakeLists.txt", "MT", "MD", {plain = true})
  115. io.replace("c10/macros/Macros.h", "extern \"C\" {\nC10_IMPORT", "extern \"C\" {\n__declspec(dllimport)", {plain = true})
  116. else
  117. io.replace("CMakeLists.txt", "\"NOT BUILD_SHARED_LIBS\" OFF", "\"NOT BUILD_SHARED_LIBS\" ON", {plain = true})
  118. io.replace("c10/macros/Macros.h", "extern \"C\" {\nC10_IMPORT", "extern \"C\" {", {plain = true})
  119. end
  120. end
  121. -- prepare python
  122. local python_exe = package:is_plat("windows") and "python" or "python3"
  123. os.vrun(python_exe .. " -m pip install typing_extensions pyyaml")
  124. local configs = {"-DUSE_MPI=OFF",
  125. "-DUSE_NUMA=OFF",
  126. "-DUSE_MAGMA=OFF",
  127. "-DBUILD_TEST=OFF",
  128. "-DATEN_NO_TEST=ON"}
  129. if package:config("python") then
  130. table.insert(configs, "-DBUILD_PYTHON=ON")
  131. os.vrun(python_exe .. " -m pip install numpy")
  132. else
  133. table.insert(configs, "-DBUILD_PYTHON=OFF")
  134. table.insert(configs, "-DUSE_NUMPY=OFF")
  135. end
  136. -- prepare for installation
  137. local envs = cmake.buildenvs(package)
  138. if not package:is_plat("macosx") then
  139. if package:config("blas") == "mkl" then
  140. table.insert(configs, "-DBLAS=MKL")
  141. local mkl = package:dep("mkl"):fetch()
  142. table.insert(configs, "-DINTEL_MKL_DIR=" .. path.directory(mkl.sysincludedirs[1]))
  143. elseif package:config("blas") == "openblas" then
  144. table.insert(configs, "-DBLAS=OpenBLAS")
  145. envs.OpenBLAS_HOME = package:dep("openblas"):installdir()
  146. elseif package:config("blas") == "eigen" then
  147. table.insert(configs, "-DBLAS=Eigen")
  148. end
  149. end
  150. if package:config("distributed") then
  151. envs.libuv_ROOT = package:dep("libuv"):installdir()
  152. end
  153. table.insert(configs, "-DCMAKE_BUILD_TYPE=" .. (package:debug() and "Debug" or "Release"))
  154. table.insert(configs, "-DBUILD_SHARED_LIBS=" .. (package:config("shared") and "ON" or "OFF"))
  155. table.insert(configs, "-DUSE_CUDA=" .. (package:config("cuda") and "ON" or "OFF"))
  156. table.insert(configs, "-DUSE_OPENMP=" .. (package:config("openmp") and "ON" or "OFF"))
  157. table.insert(configs, "-DUSE_DISTRIBUTED=" .. (package:config("distributed") and "ON" or "OFF"))
  158. table.insert(configs, "-DUSE_SYSTEM_PYBIND11=" .. (package:config("pybind11") and "ON" or "OFF"))
  159. table.insert(configs, "-DBUILD_CUSTOM_PROTOBUF=" .. (package:config("protobuf-cpp") and "OFF" or "ON"))
  160. local pythonpath, err = os.iorun(python_exe .. " -c \"import sys; print(sys.executable)\"")
  161. table.insert(configs, "-DPYTHON_EXECUTABLE=" .. pythonpath)
  162. if package:is_plat("windows") then
  163. table.insert(configs, "-DCAFFE2_USE_MSVC_STATIC_RUNTIME=" .. (package:config("vs_runtime"):startswith("MT") and "ON" or "OFF"))
  164. table.insert(configs, "-DCPUINFO_RUNTIME_TYPE=" .. (package:config("vs_runtime"):startswith("MT") and "static" or "shared"))
  165. local vs_sdkver = toolchain.load("msvc"):config("vs_sdkver")
  166. if vs_sdkver then
  167. local build_ver = string.match(vs_sdkver, "%d+%.%d+%.(%d+)%.?%d*")
  168. assert(tonumber(build_ver) >= 18362, "libtorch requires Windows SDK to be at least 10.0.18362.0")
  169. table.insert(configs, "-DCMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION=" .. vs_sdkver)
  170. table.insert(configs, "-DCMAKE_SYSTEM_VERSION=" .. vs_sdkver)
  171. end
  172. end
  173. local opt = {envs = envs}
  174. if package:config("ninja") then
  175. opt.cmake_generator = "Ninja"
  176. end
  177. cmake.install(package, configs, opt)
  178. -- These libs are not installed by cmake but are required for static link.
  179. local cp_libs = {"libonnx", "libonnx_proto"}
  180. if package:version():eq("v1.11.0") then
  181. table.insert(cp_libs, "libbreakpad")
  182. table.insert(cp_libs, "libbreakpad_common")
  183. end
  184. local static_lib_suffix = ".a"
  185. if package:is_plat("windows") then
  186. static_lib_suffix = ".lib"
  187. end
  188. for _, libname in ipairs(cp_libs) do
  189. os.trycp(path.join(package:buildir(), "lib", libname .. static_lib_suffix), package:installdir("lib"))
  190. end
  191. -- Following patches are needed for static link.
  192. io.replace(
  193. path.join(package:installdir("share/cmake/Torch/TorchConfig.cmake")),
  194. "append_torchlib_if_found(dnnl mkldnn)",
  195. "append_torchlib_if_found(dnnl_graph dnnl mkldnn)",
  196. {plain = true}
  197. )
  198. if package:version():eq("v1.11.0") then
  199. io.replace(
  200. path.join(package:installdir("share/cmake/Torch/TorchConfig.cmake")),
  201. "append_torchlib_if_found(sleef asmjit)",
  202. "append_torchlib_if_found(sleef asmjit)\n append_torchlib_if_found(breakpad breakpad_common)",
  203. {plain = true}
  204. )
  205. end
  206. end)
  207. on_test(function (package)
  208. assert(package:check_cxxsnippets({test = [[
  209. void test() {
  210. auto a = torch::ones(3);
  211. auto b = torch::tensor({1, 2, 3});
  212. auto c = torch::dot(a, b);
  213. }
  214. ]]}, {configs = {languages = "c++17"}, includes = "torch/torch.h"}))
  215. end)