xmake.lua 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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_patches("1.9.x", path.join(os.scriptdir(), "patches", "1.9.0", "gcc11.patch"), "4191bb3296f18f040c230d7c5364fb160871962d6278e4ae0f8bc481f27d8e4b")
  14. add_patches("1.11.0", path.join(os.scriptdir(), "patches", "1.11.0", "gcc11.patch"), "1404b0bc6ce7433ecdc59d3412e3d9ed507bb5fd2cd59134a254d7d4a8d73012")
  15. -- Fix compile on macOS. Refer to https://github.com/pytorch/pytorch/pull/80916
  16. add_patches("1.12.1", path.join(os.scriptdir(), "patches", "1.12.1", "clang.patch"), "cdc3e00b2fea847678b1bcc6b25a4dbd924578d8fb25d40543521a09aab2f7d4")
  17. add_patches("1.12.1", path.join(os.scriptdir(), "patches", "1.12.1", "vs2022.patch"), "5a31b9772793c943ca752c92d6415293f7b3863813ca8c5eb9d92a6156afd21d")
  18. add_configs("shared", {description = "Build shared library.", default = true, type = "boolean"})
  19. add_configs("python", {description = "Build python interface.", default = false, type = "boolean"})
  20. add_configs("openmp", {description = "Use OpenMP for parallel code.", default = true, type = "boolean"})
  21. add_configs("cuda", {description = "Enable CUDA support.", default = false, type = "boolean"})
  22. add_configs("ninja", {description = "Use ninja as build tool.", default = false, type = "boolean"})
  23. add_configs("blas", {description = "Set BLAS vendor.", default = "openblas", type = "string", values = {"mkl", "openblas", "eigen"}})
  24. add_configs("pybind11", {description = "Use pybind11 from xrepo.", default = false, type = "boolean"})
  25. add_configs("protobuf-cpp", {description = "Use protobuf from xrepo.", default = false, type = "boolean"})
  26. if not is_plat("macosx") then
  27. add_configs("distributed", {description = "Enable distributed support.", default = false, type = "boolean"})
  28. end
  29. add_deps("cmake")
  30. add_deps("python 3.x", {kind = "binary"})
  31. add_includedirs("include")
  32. add_includedirs("include/torch/csrc/api/include")
  33. if is_plat("linux") then
  34. add_syslinks("rt")
  35. end
  36. -- enable long paths for git submodule on windows
  37. if is_host("windows") and set_policy then
  38. set_policy("platform.longpaths", true)
  39. end
  40. on_load("windows|x64", "macosx", "linux", function (package)
  41. if package:config("ninja") then
  42. package:add("deps", "ninja")
  43. end
  44. if package:config("openmp") then
  45. package:add("deps", "openmp")
  46. end
  47. if package:config("cuda") then
  48. package:add("deps", "cuda", {configs = {utils = {"nvrtc", "cudnn", "cufft", "curand", "cublas", "cudart_static"}}})
  49. package:add("deps", "nvtx")
  50. end
  51. if package:config("distributed") then
  52. package:add("deps", "libuv")
  53. end
  54. if not package:is_plat("macosx") and package:config("blas") then
  55. package:add("deps", package:config("blas"))
  56. end
  57. if package:config("pybind11") then
  58. package:add("deps", "pybind11")
  59. end
  60. if package:config("protobuf-cpp") then
  61. package:add("deps", "protobuf-cpp")
  62. end
  63. end)
  64. on_install("windows|x64", "macosx", "linux", function (package)
  65. import("package.tools.cmake")
  66. if package:is_plat("windows") then
  67. local vs = import("core.tool.toolchain").load("msvc"):config("vs")
  68. if tonumber(vs) < 2019 then
  69. raise("Your compiler is too old to use this library.")
  70. end
  71. end
  72. -- tackle link flags
  73. local libnames = {"torch", "torch_cpu"}
  74. if package:config("cuda") then
  75. table.insert(libnames, "torch_cuda")
  76. end
  77. table.insert(libnames, "c10")
  78. if package:config("cuda") then
  79. table.insert(libnames, "c10_cuda")
  80. end
  81. local suffix = ""
  82. if not package:is_plat("windows") and package:config("shared") then
  83. package:add("ldflags", "-Wl,-rpath," .. package:installdir("lib"))
  84. if package:is_plat("linux") then
  85. suffix = ".so"
  86. elseif package:is_plat("macosx") then
  87. suffix = ".dylib"
  88. end
  89. for _, lib in ipairs(libnames) do
  90. package:add("ldflags", (package:is_plat("linux") and "-Wl,--no-as-needed," or "") .. package:installdir("lib", "lib") .. lib .. suffix)
  91. end
  92. else
  93. for _, lib in ipairs(libnames) do
  94. package:add("links", lib)
  95. end
  96. end
  97. if not package:config("shared") then
  98. 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
  99. package:add("links", lib)
  100. end
  101. end
  102. -- some patches to the third-party cmake files
  103. io.replace("third_party/fbgemm/CMakeLists.txt", "PRIVATE FBGEMM_STATIC", "PUBLIC FBGEMM_STATIC", {plain = true})
  104. -- Workaround to compile with GCC-12.
  105. -- Refer to [this pytorch issue](https://github.com/pytorch/pytorch/issues/77939).
  106. io.replace("third_party/fbgemm/CMakeLists.txt",
  107. 'string(APPEND CMAKE_CXX_FLAGS " -Werror")',
  108. 'string(APPEND CMAKE_CXX_FLAGS " -Werror")\n string(APPEND CMAKE_CXX_FLAGS " -Wno-uninitialized")',
  109. {plain = true}
  110. )
  111. io.replace("third_party/protobuf/cmake/install.cmake", "install%(DIRECTORY.-%)", "")
  112. if package:is_plat("windows") and package:config("vs_runtime"):startswith("MD") then
  113. io.replace("third_party/fbgemm/CMakeLists.txt", "MT", "MD", {plain = true})
  114. end
  115. -- prepare python
  116. os.vrun("python -m pip install typing_extensions pyyaml")
  117. local configs = {"-DUSE_MPI=OFF",
  118. "-DCMAKE_INSTALL_LIBDIR=lib",
  119. "-DBUILD_TEST=OFF",
  120. "-DATEN_NO_TEST=ON"}
  121. if package:config("python") then
  122. table.insert(configs, "-DBUILD_PYTHON=ON")
  123. os.vrun("python -m pip install numpy")
  124. else
  125. table.insert(configs, "-DBUILD_PYTHON=OFF")
  126. table.insert(configs, "-DUSE_NUMPY=OFF")
  127. end
  128. -- prepare for installation
  129. local envs = cmake.buildenvs(package, {cmake_generator = "Ninja"})
  130. if not package:is_plat("macosx") then
  131. if package:config("blas") == "mkl" then
  132. table.insert(configs, "-DBLAS=MKL")
  133. local mkl = package:dep("mkl"):fetch()
  134. table.insert(configs, "-DINTEL_MKL_DIR=" .. path.directory(mkl.sysincludedirs[1]))
  135. elseif package:config("blas") == "openblas" then
  136. table.insert(configs, "-DBLAS=OpenBLAS")
  137. envs.OpenBLAS_HOME = package:dep("openblas"):installdir()
  138. elseif package:config("blas") == "eigen" then
  139. table.insert(configs, "-DBLAS=Eigen")
  140. end
  141. end
  142. if package:config("distributed") then
  143. envs.libuv_ROOT = package:dep("libuv"):installdir()
  144. end
  145. table.insert(configs, "-DCMAKE_BUILD_TYPE=" .. (package:debug() and "Debug" or "Release"))
  146. table.insert(configs, "-DBUILD_SHARED_LIBS=" .. (package:config("shared") and "ON" or "OFF"))
  147. table.insert(configs, "-DUSE_CUDA=" .. (package:config("cuda") and "ON" or "OFF"))
  148. table.insert(configs, "-DUSE_OPENMP=" .. (package:config("openmp") and "ON" or "OFF"))
  149. table.insert(configs, "-DUSE_DISTRIBUTED=" .. (package:config("distributed") and "ON" or "OFF"))
  150. table.insert(configs, "-DUSE_SYSTEM_PYBIND11=" .. (package:config("pybind11") and "ON" or "OFF"))
  151. table.insert(configs, "-DBUILD_CUSTOM_PROTOBUF=" .. (package:config("protobuf-cpp") and "OFF" or "ON"))
  152. if package:is_plat("windows") then
  153. table.insert(configs, "-DCAFFE2_USE_MSVC_STATIC_RUNTIME=" .. (package:config("vs_runtime"):startswith("MT") and "ON" or "OFF"))
  154. end
  155. local opt = {envs = envs}
  156. if package:config("ninja") then
  157. opt.cmake_generator = "Ninja"
  158. end
  159. cmake.install(package, configs, opt)
  160. -- These libs are not installed by cmake but are required for static link.
  161. local cp_libs = {"libonnx", "libonnx_proto"}
  162. if package:version():eq("v1.11.0") then
  163. table.insert(cp_libs, "libbreakpad")
  164. table.insert(cp_libs, "libbreakpad_common")
  165. end
  166. local static_lib_suffix = ".a"
  167. if package:is_plat("windows") then
  168. static_lib_suffix = ".lib"
  169. end
  170. for _, libname in ipairs(cp_libs) do
  171. os.trycp(path.join(package:buildir(), "lib", libname .. static_lib_suffix), package:installdir("lib"))
  172. end
  173. -- Following patches are needed for static link.
  174. io.replace(
  175. path.join(package:installdir("share/cmake/Torch/TorchConfig.cmake")),
  176. "append_torchlib_if_found(dnnl mkldnn)",
  177. "append_torchlib_if_found(dnnl_graph dnnl mkldnn)",
  178. {plain = true}
  179. )
  180. if package:version():eq("v1.11.0") then
  181. io.replace(
  182. path.join(package:installdir("share/cmake/Torch/TorchConfig.cmake")),
  183. "append_torchlib_if_found(sleef asmjit)",
  184. "append_torchlib_if_found(sleef asmjit)\n append_torchlib_if_found(breakpad breakpad_common)",
  185. {plain = true}
  186. )
  187. end
  188. end)
  189. on_test(function (package)
  190. assert(package:check_cxxsnippets({test = [[
  191. void test() {
  192. auto a = torch::ones(3);
  193. auto b = torch::tensor({1, 2, 3});
  194. auto c = torch::dot(a, b);
  195. }
  196. ]]}, {configs = {languages = "c++14"}, includes = "torch/torch.h"}))
  197. end)