Browse Source

libtorch v1.12.1, support static linking (#1503)

* libtorch: update to v1.12.1

* libtorch: static link related patches for v1.11.0

* libtorch: fix compile with vs2022.
Chen Yufei 2 years ago
parent
commit
2be6583eb5

+ 14 - 0
packages/l/libtorch/patches/1.12.1/clang.patch

@@ -0,0 +1,14 @@
+diff --git a/cmake/Dependencies.cmake b/cmake/Dependencies.cmake
+index 44a8bf1211..d0f1716850 100644
+--- a/cmake/Dependencies.cmake
++++ b/cmake/Dependencies.cmake
+@@ -816,7 +816,8 @@ if(USE_FBGEMM)
+     set_property(TARGET fbgemm_avx2 PROPERTY POSITION_INDEPENDENT_CODE ON)
+     set_property(TARGET fbgemm_avx512 PROPERTY POSITION_INDEPENDENT_CODE ON)
+     set_property(TARGET fbgemm PROPERTY POSITION_INDEPENDENT_CODE ON)
+-    if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 13.0.0)
++    if(("${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 13.1.6)
++        OR("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 13.0.0))
+         # See https://github.com/pytorch/pytorch/issues/74352
+         target_compile_options(asmjit PRIVATE -Wno-deprecated-copy -Wno-unused-but-set-variable)
+     endif()

+ 15 - 0
packages/l/libtorch/patches/1.12.1/vs2022.patch

@@ -0,0 +1,15 @@
+diff --git a/aten/src/ATen/cpu/vec/vec_base.h b/aten/src/ATen/cpu/vec/vec_base.h
+index 3bf1010efd..640c0ed109 100644
+--- a/aten/src/ATen/cpu/vec/vec_base.h
++++ b/aten/src/ATen/cpu/vec/vec_base.h
+@@ -131,8 +131,9 @@ public:
+   // versions GCC/Clang have buggy determinations on whether or not an
+   // identifier is odr-used or not, and in any case it's hard to tell if
+   // a variable is odr-used or not.  So best to just cut the problem at the root.
++  static constexpr size_type size_T = sizeof(T);  // Workaround to compile with VS2022.
+   static constexpr size_type size() {
+-    return VECTOR_WIDTH / sizeof(T);
++    return VECTOR_WIDTH / size_T;
+   }
+   Vectorized() : values{static_cast<T>(0)} {}
+   Vectorized(T val) {

+ 52 - 1
packages/l/libtorch/xmake.lua

@@ -11,9 +11,13 @@ package("libtorch")
     add_versions("v1.9.0", "d69c22dd61a2f006dcfe1e3ea8468a3ecaf931aa")
     add_versions("v1.9.1", "dfbd030854359207cb3040b864614affeace11ce")
     add_versions("v1.11.0", "bc2c6edaf163b1a1330e37a6e34caf8c553e4755")
+    add_versions("v1.12.1", "664058fa83f1d8eede5d66418abff6e20bd76ca8")
 
     add_patches("1.9.x", path.join(os.scriptdir(), "patches", "1.9.0", "gcc11.patch"), "4191bb3296f18f040c230d7c5364fb160871962d6278e4ae0f8bc481f27d8e4b")
     add_patches("1.11.0", path.join(os.scriptdir(), "patches", "1.11.0", "gcc11.patch"), "1404b0bc6ce7433ecdc59d3412e3d9ed507bb5fd2cd59134a254d7d4a8d73012")
+    -- Fix compile on macOS. Refer to https://github.com/pytorch/pytorch/pull/80916
+    add_patches("1.12.1", path.join(os.scriptdir(), "patches", "1.12.1", "clang.patch"), "cdc3e00b2fea847678b1bcc6b25a4dbd924578d8fb25d40543521a09aab2f7d4")
+    add_patches("1.12.1", path.join(os.scriptdir(), "patches", "1.12.1", "vs2022.patch"), "5a31b9772793c943ca752c92d6415293f7b3863813ca8c5eb9d92a6156afd21d")
 
     add_configs("shared", {description = "Build shared library.", default = true, type = "boolean"})
     add_configs("python", {description = "Build python interface.", default = false, type = "boolean"})
@@ -21,6 +25,8 @@ package("libtorch")
     add_configs("cuda",   {description = "Enable CUDA support.", default = false, type = "boolean"})
     add_configs("ninja",  {description = "Use ninja as build tool.", default = false, type = "boolean"})
     add_configs("blas",   {description = "Set BLAS vendor.", default = "openblas", type = "string", values = {"mkl", "openblas", "eigen"}})
+    add_configs("pybind11", {description = "Use pybind11 from xrepo.", default = false, type = "boolean"})
+    add_configs("protobuf-cpp", {description = "Use protobuf from xrepo.", default = false, type = "boolean"})
     if not is_plat("macosx") then
         add_configs("distributed", {description = "Enable distributed support.", default = false, type = "boolean"})
     end
@@ -56,6 +62,12 @@ package("libtorch")
         if not package:is_plat("macosx") and package:config("blas") then
             package:add("deps", package:config("blas"))
         end
+        if package:config("pybind11") then
+            package:add("deps", "pybind11")
+        end
+        if package:config("protobuf-cpp") then
+            package:add("deps", "protobuf-cpp")
+        end
     end)
 
     on_install("windows|x64", "macosx", "linux", function (package)
@@ -94,13 +106,20 @@ package("libtorch")
             end
         end
         if not package:config("shared") then
-            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", "mkldnn", "sleef", "asmjit", "fmt", "kineto"}) do
+            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
                 package:add("links", lib)
             end
         end
 
         -- some patches to the third-party cmake files
         io.replace("third_party/fbgemm/CMakeLists.txt", "PRIVATE FBGEMM_STATIC", "PUBLIC FBGEMM_STATIC", {plain = true})
+        -- Workaround to compile with GCC-12.
+        -- Refer to [this pytorch issue](https://github.com/pytorch/pytorch/issues/77939).
+        io.replace("third_party/fbgemm/CMakeLists.txt",
+            'string(APPEND CMAKE_CXX_FLAGS " -Werror")',
+            'string(APPEND CMAKE_CXX_FLAGS " -Werror")\n  string(APPEND CMAKE_CXX_FLAGS " -Wno-uninitialized")',
+            {plain = true}
+        )
         io.replace("third_party/protobuf/cmake/install.cmake", "install%(DIRECTORY.-%)", "")
         if package:is_plat("windows") and package:config("vs_runtime"):startswith("MD") then
             io.replace("third_party/fbgemm/CMakeLists.txt", "MT", "MD", {plain = true})
@@ -142,6 +161,8 @@ package("libtorch")
         table.insert(configs, "-DUSE_CUDA=" .. (package:config("cuda") and "ON" or "OFF"))
         table.insert(configs, "-DUSE_OPENMP=" .. (package:config("openmp") and "ON" or "OFF"))
         table.insert(configs, "-DUSE_DISTRIBUTED=" .. (package:config("distributed") and "ON" or "OFF"))
+        table.insert(configs, "-DUSE_SYSTEM_PYBIND11=" .. (package:config("pybind11") and "ON" or "OFF"))
+        table.insert(configs, "-DBUILD_CUSTOM_PROTOBUF=" .. (package:config("protobuf-cpp") and "OFF" or "ON"))
         if package:is_plat("windows") then
             table.insert(configs, "-DCAFFE2_USE_MSVC_STATIC_RUNTIME=" .. (package:config("vs_runtime"):startswith("MT") and "ON" or "OFF"))
         end
@@ -151,6 +172,36 @@ package("libtorch")
             opt.cmake_generator = "Ninja"
         end
         cmake.install(package, configs, opt)
+
+        -- These libs are not installed by cmake but are required for static link.
+        local cp_libs = {"libonnx", "libonnx_proto"}
+        if package:version():eq("v1.11.0") then
+            table.insert(cp_libs, "libbreakpad")
+            table.insert(cp_libs, "libbreakpad_common")
+        end
+        local static_lib_suffix = ".a"
+        if package:is_plat("windows") then
+            static_lib_suffix = ".lib"
+        end
+        for _, libname in ipairs(cp_libs) do
+            os.trycp(path.join(package:buildir(), "lib", libname .. static_lib_suffix), package:installdir("lib"))
+        end
+
+        -- Following patches are needed for static link.
+        io.replace(
+            path.join(package:installdir("share/cmake/Torch/TorchConfig.cmake")),
+            "append_torchlib_if_found(dnnl mkldnn)",
+            "append_torchlib_if_found(dnnl_graph dnnl mkldnn)",
+            {plain = true}
+        )
+        if package:version():eq("v1.11.0") then
+            io.replace(
+                path.join(package:installdir("share/cmake/Torch/TorchConfig.cmake")),
+                "append_torchlib_if_found(sleef asmjit)",
+                "append_torchlib_if_found(sleef asmjit)\n  append_torchlib_if_found(breakpad breakpad_common)",
+                {plain = true}
+            )
+        end
     end)
 
     on_test(function (package)