From d5c96a7fd9b209c06b1132eb8812b560abbe72a3 Mon Sep 17 00:00:00 2001 From: Gabriel Ebner Date: Thu, 14 Jan 2021 18:48:07 +0100 Subject: [PATCH] git subrepo clone https://github.com/rocm-arch/python-pytorch-rocm subrepo: subdir: "python-pytorch-rocm" merged: "bc28dc1" upstream: origin: "https://github.com/rocm-arch/python-pytorch-rocm" branch: "master" commit: "bc28dc1" git-subrepo: version: "0.4.3" origin: "???" commit: "???" --- python-pytorch-rocm/.SRCINFO | 99 ++++++++++ python-pytorch-rocm/.gitrepo | 12 ++ python-pytorch-rocm/PKGBUILD | 185 +++++++++++++++++++ python-pytorch-rocm/disable_non_x86_64.patch | 15 ++ python-pytorch-rocm/fix_include_system.patch | 13 ++ python-pytorch-rocm/nccl_version.patch | 25 +++ python-pytorch-rocm/use-system-libuv.patch | 13 ++ python-pytorch-rocm/use-system-libuv2.patch | 13 ++ 8 files changed, 375 insertions(+) create mode 100644 python-pytorch-rocm/.SRCINFO create mode 100644 python-pytorch-rocm/.gitrepo create mode 100644 python-pytorch-rocm/PKGBUILD create mode 100644 python-pytorch-rocm/disable_non_x86_64.patch create mode 100644 python-pytorch-rocm/fix_include_system.patch create mode 100644 python-pytorch-rocm/nccl_version.patch create mode 100644 python-pytorch-rocm/use-system-libuv.patch create mode 100644 python-pytorch-rocm/use-system-libuv2.patch diff --git a/python-pytorch-rocm/.SRCINFO b/python-pytorch-rocm/.SRCINFO new file mode 100644 index 0000000..4e2bec1 --- /dev/null +++ b/python-pytorch-rocm/.SRCINFO @@ -0,0 +1,99 @@ +pkgbase = python-pytorch-rocm + pkgdesc = Tensors and Dynamic neural networks in Python with strong GPU acceleration + pkgver = 1.7.1 + pkgrel = 7 + url = https://pytorch.org + arch = x86_64 + license = BSD + makedepends = python + makedepends = python-setuptools + makedepends = python-yaml + makedepends = python-numpy + makedepends = cmake + makedepends = rocm + makedepends = rocm-libs + makedepends = miopen + makedepends = git + makedepends = ninja + makedepends = pkgconfig + makedepends = doxygen + depends = google-glog + depends = gflags + depends = opencv + depends = openmp + depends = rccl + depends = pybind11 + depends = python + depends = python-yaml + depends = libuv + depends = python-numpy + depends = protobuf + depends = ffmpeg + depends = python-future + depends = qt5-base + depends = onednn + depends = intel-mkl + source = pytorch-1.7.1::git+https://github.com/pytorch/pytorch.git#tag=v1.7.1 + source = fix_include_system.patch + source = use-system-libuv.patch + source = use-system-libuv2.patch + source = nccl_version.patch + source = disable_non_x86_64.patch + source = find-hsa-runtime.patch::https://patch-diff.githubusercontent.com/raw/pytorch/pytorch/pull/45550.patch + sha256sums = SKIP + sha256sums = 83c81ec6a461110da6ae6182529f58100986b068c5182ca62cd53c648b4e4fb0 + sha256sums = 26b1dd596f1e21a011ee18cab939924483d6c6d4d98e543bf76f5a9312d54d67 + sha256sums = 7b65c3b209fc39f92ba58a58be6d3da40799f1922910b1171ccd9209eda1f9eb + sha256sums = e4a96887b41cbdfd4204ce5f16fcb16a23558d23126331794ab6aa30a66f2e0d + sha256sums = d3ef8491718ed7e814fe63e81df2f49862fffbea891d2babbcb464796a1bd680 + sha256sums = SKIP + +pkgname = python-pytorch-rocm + pkgdesc = Tensors and Dynamic neural networks in Python with strong GPU acceleration (with ROCM) + depends = google-glog + depends = gflags + depends = opencv + depends = openmp + depends = rccl + depends = pybind11 + depends = python + depends = python-yaml + depends = libuv + depends = python-numpy + depends = protobuf + depends = ffmpeg + depends = python-future + depends = qt5-base + depends = onednn + depends = intel-mkl + depends = rocm + depends = rocm-libs + depends = miopen + provides = python-pytorch + conflicts = python-pytorch + +pkgname = python-pytorch-opt-rocm + pkgdesc = Tensors and Dynamic neural networks in Python with strong GPU acceleration (with ROCM and AVX2 CPU optimizations) + depends = google-glog + depends = gflags + depends = opencv + depends = openmp + depends = rccl + depends = pybind11 + depends = python + depends = python-yaml + depends = libuv + depends = python-numpy + depends = protobuf + depends = ffmpeg + depends = python-future + depends = qt5-base + depends = onednn + depends = intel-mkl + depends = rocm + depends = rocm-libs + depends = miopen + provides = python-pytorch + provides = python-pytorch-rocm + conflicts = python-pytorch + diff --git a/python-pytorch-rocm/.gitrepo b/python-pytorch-rocm/.gitrepo new file mode 100644 index 0000000..ae3b490 --- /dev/null +++ b/python-pytorch-rocm/.gitrepo @@ -0,0 +1,12 @@ +; DO NOT EDIT (unless you know what you are doing) +; +; This subdirectory is a git "subrepo", and this file is maintained by the +; git-subrepo command. See https://github.com/git-commands/git-subrepo#readme +; +[subrepo] + remote = https://github.com/rocm-arch/python-pytorch-rocm + branch = master + commit = bc28dc180c97c76542995d38276781a7357c784b + parent = 91ea9ad6727e316c492bfec38dae8f788432e55e + method = merge + cmdver = 0.4.3 diff --git a/python-pytorch-rocm/PKGBUILD b/python-pytorch-rocm/PKGBUILD new file mode 100644 index 0000000..a3199a5 --- /dev/null +++ b/python-pytorch-rocm/PKGBUILD @@ -0,0 +1,185 @@ +# Maintainer: acxz +# Contributor: Sven-Hendrik Haase +# Contributor: Stephen Zhang + +pkgbase=python-pytorch-rocm + +# Flags for building without/with cpu optimizations +_build_no_opt=1 +_build_opt=1 + +pkgname=() +[ "$_build_no_opt" -eq 1 ] && pkgname+=("python-pytorch-rocm") +[ "$_build_opt" -eq 1 ] && pkgname+=("python-pytorch-opt-rocm") + +_pkgname="pytorch" +pkgver=1.7.1 +_pkgver=1.7.1 +pkgrel=7 +pkgdesc="Tensors and Dynamic neural networks in Python with strong GPU acceleration" +arch=('x86_64') +url="https://pytorch.org" +license=('BSD') +depends=('google-glog' 'gflags' 'opencv' 'openmp' 'rccl' 'pybind11' 'python' 'python-yaml' 'libuv' + 'python-numpy' 'protobuf' 'ffmpeg' 'python-future' 'qt5-base' 'onednn' 'intel-mkl') +makedepends=('python' 'python-setuptools' 'python-yaml' 'python-numpy' 'cmake' 'rocm' + 'rocm-libs' 'miopen' 'git' 'ninja' 'pkgconfig' 'doxygen') +source=("${_pkgname}-${pkgver}::git+https://github.com/pytorch/pytorch.git#tag=v$_pkgver" + fix_include_system.patch + use-system-libuv.patch + use-system-libuv2.patch + nccl_version.patch + disable_non_x86_64.patch + "find-hsa-runtime.patch::https://patch-diff.githubusercontent.com/raw/pytorch/pytorch/pull/45550.patch") +sha256sums=('SKIP' + '83c81ec6a461110da6ae6182529f58100986b068c5182ca62cd53c648b4e4fb0' + '26b1dd596f1e21a011ee18cab939924483d6c6d4d98e543bf76f5a9312d54d67' + '7b65c3b209fc39f92ba58a58be6d3da40799f1922910b1171ccd9209eda1f9eb' + 'e4a96887b41cbdfd4204ce5f16fcb16a23558d23126331794ab6aa30a66f2e0d' + 'd3ef8491718ed7e814fe63e81df2f49862fffbea891d2babbcb464796a1bd680' + 'SKIP') + +prepare() { + cd "${_pkgname}-${pkgver}" + + # This is the lazy way since pytorch has sooo many submodules and they keep + # changing them around but we've run into more problems so far doing it the + # manual than the lazy way. This lazy way (not explicitly specifying all + # submodules) will make building inefficient but for now I'll take it. + # It will result in the same package, don't worry. + git submodule update --init --recursive + + # https://bugs.archlinux.org/task/64981 + patch -N torch/utils/cpp_extension.py "${srcdir}"/fix_include_system.patch + + # Use system libuv + patch -Np1 -i "${srcdir}"/use-system-libuv.patch + + # FindNCCL patch to export correct nccl version + # patch -Np1 -i "${srcdir}"/nccl_version.patch + + # https://github.com/pytorch/pytorch/pull/45550 + patch -Np1 -i "${srcdir}"/find-hsa-runtime.patch + + # remove local nccl + rm -rf third_party/nccl/nccl + + cd .. + + [ "$_build_no_opt" -eq 1 ] && cp -a "${_pkgname}-${pkgver}" "${_pkgname}-${pkgver}-rocm" + [ "$_build_opt" -eq 1 ] && cp -a "${_pkgname}-${pkgver}" "${_pkgname}-${pkgver}-opt-rocm" + + export VERBOSE=1 + export PYTORCH_BUILD_VERSION="${pkgver}" + export PYTORCH_BUILD_NUMBER=1 + + # Check tools/setup_helpers/cmake.py, setup.py and CMakeLists.txt for a list of flags that can be set via env vars. + export USE_MKLDNN=ON + export BUILD_CUSTOM_PROTOBUF=ON + # export BUILD_SHARED_LIBS=OFF + export USE_FFMPEG=ON + export USE_GFLAGS=ON + export USE_GLOG=ON + export BUILD_BINARY=ON + export USE_OPENCV=ON + export USE_SYSTEM_NCCL=ON + # export USE_SYSTEM_LIBS=ON + export NCCL_VERSION=$(pkg-config nccl --modversion) + export NCCL_VER_CODE=$(sed -n 's/^#define NCCL_VERSION_CODE\s*\(.*\).*/\1/p' /usr/include/nccl.h) + export CUDAHOSTCXX=g++ + export CUDA_HOME=/opt/cuda + export CUDNN_LIB_DIR=/usr/lib + export CUDNN_INCLUDE_DIR=/usr/include + # export TORCH_NVCC_FLAGS="-Xfatbin -compress-all" + export TORCH_CUDA_ARCH_LIST="5.2;5.3;6.0;6.1;6.2;7.0;7.0+PTX;7.2;7.2+PTX;7.5;7.5+PTX;8.0;8.0+PTX;8.6;8.6+PTX" +} + +build() { + if [ "$_build_no_opt" -eq 1 ]; then + echo "Building with rocm and without non-x86-64 optimizations" + export USE_CUDA=OFF + export USE_ROCM=ON + cd "${srcdir}/${_pkgname}-${pkgver}-rocm" + patch -Np1 -i "${srcdir}/disable_non_x86_64.patch" + echo "add_definitions(-march=x86-64)" >> cmake/MiscCheck.cmake + + # Apply changes needed for ROCm + python tools/amd_build/build_amd.py + + # Fix so amdip64 library ending with a dash + python setup.py build --cmake-only + sed -E -i 's#opt/rocm/hip/lib/libamdhip64\.so\.[0-9.]+-#opt/rocm/hip/lib/libamdhip64.so#' build/build.ninja + + python setup.py build + fi + + if [ "$_build_opt" -eq 1 ]; then + echo "Building with rocm and with non-x86-64 optimizations" + export USE_CUDA=OFF + export USE_ROCM=ON + cd "${srcdir}/${_pkgname}-${pkgver}-opt-rocm" + echo "add_definitions(-march=haswell)" >> cmake/MiscCheck.cmake + + # Apply changes needed for ROCm + python tools/amd_build/build_amd.py + + # Fix so amdip64 library ending with a dash + python setup.py build --cmake-only + sed -E -i 's#opt/rocm/hip/lib/libamdhip64\.so\.[0-9.]+-#opt/rocm/hip/lib/libamdhip64.so#' build/build.ninja + + python setup.py build + fi +} + +_package() { + # Prevent setup.py from re-running CMake and rebuilding + sed -e 's/RUN_BUILD_DEPS = True/RUN_BUILD_DEPS = False/g' -i setup.py + + python setup.py install --root="${pkgdir}"/ --optimize=1 --skip-build + + install -Dm644 LICENSE "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE" + + pytorchpath="usr/lib/python3.9/site-packages/torch" + install -d "${pkgdir}/usr/lib" + + # put CMake files in correct place + mv "${pkgdir}/${pytorchpath}/share/cmake" "${pkgdir}/usr/lib/cmake" + + # put C++ API in correct place + mv "${pkgdir}/${pytorchpath}/include" "${pkgdir}/usr/include" + mv "${pkgdir}/${pytorchpath}/lib"/*.so* "${pkgdir}/usr/lib/" + + # clean up duplicates + # TODO: move towards direct shared library dependecy of: + # c10, caffe2, libcpuinfo, CUDA RT, gloo, GTest, Intel MKL, + # NVRTC, ONNX, protobuf, libthreadpool, QNNPACK + rm -rf "${pkgdir}/usr/include/pybind11" + + # python module is hardcoded to look there at runtime + ln -s /usr/include "${pkgdir}/${pytorchpath}/include" + find "${pkgdir}"/usr/lib -type f -name "*.so*" -print0 | while read -rd $'\0' _lib; do + ln -s ${_lib#"$pkgdir"} "${pkgdir}/${pytorchpath}/lib/" + done +} + +package_python-pytorch-rocm() { + pkgdesc="Tensors and Dynamic neural networks in Python with strong GPU acceleration (with ROCM)" + depends+=(rocm rocm-libs miopen) + conflicts=(python-pytorch) + provides=(python-pytorch) + + cd "${srcdir}/${_pkgname}-${pkgver}-rocm" + _package +} + +package_python-pytorch-opt-rocm() { + pkgdesc="Tensors and Dynamic neural networks in Python with strong GPU acceleration (with ROCM and AVX2 CPU optimizations)" + depends+=(rocm rocm-libs miopen) + conflicts=(python-pytorch) + provides=(python-pytorch python-pytorch-rocm) + + cd "${srcdir}/${_pkgname}-${pkgver}-opt-rocm" + _package +} + +# vim:set ts=2 sw=2 et: diff --git a/python-pytorch-rocm/disable_non_x86_64.patch b/python-pytorch-rocm/disable_non_x86_64.patch new file mode 100644 index 0000000..3b1d380 --- /dev/null +++ b/python-pytorch-rocm/disable_non_x86_64.patch @@ -0,0 +1,15 @@ +diff --git a/tools/setup_helpers/cmake.py b/tools/setup_helpers/cmake.py +index d5db749d15..fd54cca6b8 100644 +--- a/tools/setup_helpers/cmake.py ++++ b/tools/setup_helpers/cmake.py +@@ -295,6 +295,10 @@ class CMake: + build_options.update(cmake__options) + + CMake.defines(args, ++ DISABLE_AVX2=1, ++ DISABLE_AVX512F=1, ++ DISABLE_FMA4=1, ++ DISABLE_SSE4=1, + PYTHON_EXECUTABLE=sys.executable, + PYTHON_LIBRARY=cmake_python_library, + PYTHON_INCLUDE_DIR=distutils.sysconfig.get_python_inc(), diff --git a/python-pytorch-rocm/fix_include_system.patch b/python-pytorch-rocm/fix_include_system.patch new file mode 100644 index 0000000..6166f41 --- /dev/null +++ b/python-pytorch-rocm/fix_include_system.patch @@ -0,0 +1,13 @@ +diff --git a/torch/utils/cpp_extension.py b/torch/utils/cpp_extension.py +index 7692cad5cd..d6e4a82221 100644 +--- a/torch/utils/cpp_extension.py ++++ b/torch/utils/cpp_extension.py +@@ -1614,7 +1614,7 @@ def _write_ninja_file_to_build_library(path, + common_cflags.append(f'-DPYBIND11_{pname}=\\"{pval}\\"') + + common_cflags += [f'-I{include}' for include in user_includes] +- common_cflags += [f'-isystem {include}' for include in system_includes] ++ common_cflags += [f'-I{include}' for include in system_includes] + common_cflags += ['-D_GLIBCXX_USE_CXX11_ABI=' + str(int(torch._C._GLIBCXX_USE_CXX11_ABI))] + + if IS_WINDOWS: diff --git a/python-pytorch-rocm/nccl_version.patch b/python-pytorch-rocm/nccl_version.patch new file mode 100644 index 0000000..c0cd000 --- /dev/null +++ b/python-pytorch-rocm/nccl_version.patch @@ -0,0 +1,25 @@ +diff --git a/cmake/Modules/FindNCCL.cmake b/cmake/Modules/FindNCCL.cmake +index a16c9aca67..51eb54f14c 100644 +--- a/cmake/Modules/FindNCCL.cmake ++++ b/cmake/Modules/FindNCCL.cmake +@@ -55,9 +55,10 @@ if(NCCL_FOUND) # obtaining NCCL version and some sanity checks + set (OLD_CMAKE_REQUIRED_INCLUDES ${CMAKE_REQUIRED_INCLUDES}) + list (APPEND CMAKE_REQUIRED_INCLUDES ${NCCL_INCLUDE_DIRS}) + include(CheckCXXSymbolExists) +- check_cxx_symbol_exists(NCCL_VERSION_CODE nccl.h NCCL_VERSION_DEFINED) ++ set(NCCL_VERSION_CODE $ENV{NCCL_VER_CODE}) ++ set(NCCL_VERSION_DEFINED $ENV{NCCL_VER_CODE}) + +- if (NCCL_VERSION_DEFINED) ++ if (DEFINED NCCL_VERSION_DEFINED) + set(file "${PROJECT_BINARY_DIR}/detect_nccl_version.cc") + file(WRITE ${file} " + #include +@@ -72,6 +73,7 @@ if(NCCL_FOUND) # obtaining NCCL version and some sanity checks + } + ") + try_run(NCCL_VERSION_MATCHED compile_result ${PROJECT_BINARY_DIR} ${file} ++ CMAKE_FLAGS -DINCLUDE_DIRECTORIES=/opt/cuda/include + RUN_OUTPUT_VARIABLE NCCL_VERSION_FROM_HEADER + CMAKE_FLAGS "-DINCLUDE_DIRECTORIES=${NCCL_INCLUDE_DIRS}" + LINK_LIBRARIES ${NCCL_LIBRARIES}) diff --git a/python-pytorch-rocm/use-system-libuv.patch b/python-pytorch-rocm/use-system-libuv.patch new file mode 100644 index 0000000..919d278 --- /dev/null +++ b/python-pytorch-rocm/use-system-libuv.patch @@ -0,0 +1,13 @@ +diff --git a/cmake/Dependencies.cmake b/cmake/Dependencies.cmake +index 023bbe9e8d..e25aa0aa7f 100644 +--- a/cmake/Dependencies.cmake ++++ b/cmake/Dependencies.cmake +@@ -1308,7 +1308,7 @@ if(USE_DISTRIBUTED AND USE_TENSORPIPE) + if(MSVC) + message(WARNING "Tensorpipe cannot be used on Windows.") + else() +- set(TP_BUILD_LIBUV ON CACHE BOOL "" FORCE) ++ set(TP_BUILD_LIBUV OFF CACHE BOOL "" FORCE) + set(TP_ENABLE_SHM OFF CACHE BOOL "" FORCE) + set(TP_ENABLE_CMA OFF CACHE BOOL "" FORCE) + set(TP_STATIC_OR_SHARED STATIC CACHE STRING "" FORCE) diff --git a/python-pytorch-rocm/use-system-libuv2.patch b/python-pytorch-rocm/use-system-libuv2.patch new file mode 100644 index 0000000..b9a3cc1 --- /dev/null +++ b/python-pytorch-rocm/use-system-libuv2.patch @@ -0,0 +1,13 @@ +diff --git a/cmake/pytorch.cmake b/cmake/pytorch.cmake +index e4d6b9c..48e0669 100644 +--- a/cmake/pytorch.cmake ++++ b/cmake/pytorch.cmake +@@ -138,7 +138,7 @@ endif() + + add_library(tensorpipe ${TENSORPIPE_SRC}) + +-set(TP_BUILD_LIBUV ON) ++set(TP_BUILD_LIBUV OFF) + find_package(uv REQUIRED) + target_link_libraries(tensorpipe PRIVATE uv::uv) +