From 0c17973ce981bb64884eb6bf5c292acb0a611267 Mon Sep 17 00:00:00 2001 From: Max Rakitin Date: Thu, 8 Feb 2024 21:48:14 -0500 Subject: [PATCH 01/14] Pytorch v1.13.1 --- .github/workflows/build.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index f557770..616e4ac 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -24,10 +24,10 @@ jobs: # - repo: https://github.com/kuelumbus/rdkit-pypi/ # name: rdkit-pypi # version: "rdkit-pypi" - - repo: https://github.com/ssciwr/ipywidgets-jsonschema - name: ipywidgets-jsonschema - package-name: ipywidgets_jsonschema - version: "v1.1.0" + - repo: https://github.com/pytorch/pytorch + name: pytorch + package-name: pytorch + version: "v1.13.1" steps: - name: Check out the repo From b3a742d93febd40ca6dd9219172a3b79777e3496 Mon Sep 17 00:00:00 2001 From: Max Rakitin Date: Thu, 8 Feb 2024 22:02:01 -0500 Subject: [PATCH 02/14] Enable more verbose output --- .github/workflows/build.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 616e4ac..ad28bcc 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -56,7 +56,8 @@ jobs: set -vxeuo pipefail git clone --depth 1 --branch ${{ matrix.version }} ${{ matrix.repo }} cd ${{ matrix.name }} - pyodide build + export DEBUG=1 + pyodide build -vv ls -la tree . From 442dbb839aadbf07d47a1b8a64c9ed48868523d5 Mon Sep 17 00:00:00 2001 From: Max Rakitin Date: Thu, 8 Feb 2024 22:06:02 -0500 Subject: [PATCH 03/14] More config flags per https://github.com/pytorch/pytorch/blob/v1.13.1/CMakeLists.txt --- .github/workflows/build.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index ad28bcc..a179955 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -57,7 +57,9 @@ jobs: git clone --depth 1 --branch ${{ matrix.version }} ${{ matrix.repo }} cd ${{ matrix.name }} export DEBUG=1 - pyodide build -vv + export USE_CUDA=0 + export CMAKE_BUILD_TYPE=Debug + pyodide build ls -la tree . From ad4d171f5a2b3c84173fa28d807afbec593cc588 Mon Sep 17 00:00:00 2001 From: Max Rakitin Date: Thu, 8 Feb 2024 22:28:17 -0500 Subject: [PATCH 04/14] More diagnostics --- .github/workflows/build.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index a179955..93bb61d 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -59,6 +59,8 @@ jobs: export DEBUG=1 export USE_CUDA=0 export CMAKE_BUILD_TYPE=Debug + ls -al /home/runner/work/build-pyodide/build-pyodide/pytorch/build/bin/ + ls -la /home/runner/work/build-pyodide/build-pyodide/pytorch/build/bin/protoc.js-3.13.0.0 pyodide build ls -la tree . From 9e8c696b5a5cd7c696db8f11f7e020c0f89ecdc7 Mon Sep 17 00:00:00 2001 From: Max Rakitin Date: Thu, 8 Feb 2024 22:39:53 -0500 Subject: [PATCH 05/14] Use conda-forge build script --- .github/workflows/build.yml | 180 +++++++++++++++++++++++++++++++++++- 1 file changed, 175 insertions(+), 5 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 93bb61d..6a68f1a 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -56,11 +56,181 @@ jobs: set -vxeuo pipefail git clone --depth 1 --branch ${{ matrix.version }} ${{ matrix.repo }} cd ${{ matrix.name }} - export DEBUG=1 - export USE_CUDA=0 - export CMAKE_BUILD_TYPE=Debug - ls -al /home/runner/work/build-pyodide/build-pyodide/pytorch/build/bin/ - ls -la /home/runner/work/build-pyodide/build-pyodide/pytorch/build/bin/protoc.js-3.13.0.0 + + # CF build script: https://github.com/conda-forge/pytorch-cpu-feedstock/blob/main/recipe/build.sh + # remove pyproject.toml to avoid installing deps from pip + rm -rf pyproject.toml + + # uncomment to debug cmake build + # export CMAKE_VERBOSE_MAKEFILE=1 + + export USE_NUMA=0 + export USE_ITT=0 + export CFLAGS="$(echo $CFLAGS | sed 's/-fvisibility-inlines-hidden//g')" + export CXXFLAGS="$(echo $CXXFLAGS | sed 's/-fvisibility-inlines-hidden//g')" + export LDFLAGS="$(echo $LDFLAGS | sed 's/-Wl,--as-needed//g')" + export LDFLAGS="$(echo $LDFLAGS | sed 's/-Wl,-dead_strip_dylibs//g')" + export LDFLAGS_LD="$(echo $LDFLAGS_LD | sed 's/-dead_strip_dylibs//g')" + if [[ "$c_compiler" == "clang" ]]; then + export CXXFLAGS="$CXXFLAGS -Wno-deprecated-declarations -Wno-unknown-warning-option -Wno-error=unused-command-line-argument" + export CFLAGS="$CFLAGS -Wno-deprecated-declarations -Wno-unknown-warning-option -Wno-error=unused-command-line-argument" + else + export CXXFLAGS="$CXXFLAGS -Wno-deprecated-declarations -Wno-error=maybe-uninitialized" + export CFLAGS="$CFLAGS -Wno-deprecated-declarations -Wno-error=maybe-uninitialized" + fi + + # This is not correctly found for linux-aarch64 since pytorch 2.0.0 for some reason + export _GLIBCXX_USE_CXX11_ABI=1 + + # KINETO seems to require CUPTI and will look quite hard for it. + # CUPTI seems to cause trouble when users install a version of + # cudatoolkit different than the one specified at compile time. + # https://github.com/conda-forge/pytorch-cpu-feedstock/issues/135 + export USE_KINETO=OFF + + if [[ "$target_platform" == "osx-64" ]]; then + export CXXFLAGS="$CXXFLAGS -DTARGET_OS_OSX=1" + export CFLAGS="$CFLAGS -DTARGET_OS_OSX=1" + fi + + # Dynamic libraries need to be lazily loaded so that torch + # can be imported on system without a GPU + LDFLAGS="${LDFLAGS//-Wl,-z,now/-Wl,-z,lazy}" + + export CMAKE_GENERATOR=Ninja + export CMAKE_LIBRARY_PATH=$PREFIX/lib:$PREFIX/include:$CMAKE_LIBRARY_PATH + export CMAKE_PREFIX_PATH=$PREFIX + export CMAKE_BUILD_TYPE=Release + + for ARG in $CMAKE_ARGS; do + if [[ "$ARG" == "-DCMAKE_"* ]]; then + cmake_arg=$(echo $ARG | cut -d= -f1) + cmake_arg=$(echo $cmake_arg| cut -dD -f2-) + cmake_val=$(echo $ARG | cut -d= -f2-) + printf -v $cmake_arg "$cmake_val" + export ${cmake_arg} + fi + done + unset CMAKE_INSTALL_PREFIX + export TH_BINARY_BUILD=1 + export PYTORCH_BUILD_VERSION=$PKG_VERSION + export PYTORCH_BUILD_NUMBER=$PKG_BUILDNUM + + export INSTALL_TEST=0 + export BUILD_TEST=0 + + export USE_SYSTEM_SLEEF=1 + # use our protobuf + export BUILD_CUSTOM_PROTOBUF=OFF + rm -rf $PREFIX/bin/protoc + + if [[ "${target_platform}" != "${build_platform}" ]]; then + # It helps cross compiled builds without emulation support to complete + # Use BUILD PREFIX protoc instead of the one that is from the host platform + sed -i.bak \ + "s,IMPORTED_LOCATION_RELEASE .*/bin/protoc,IMPORTED_LOCATION_RELEASE \"${BUILD_PREFIX}/bin/protoc," \ + ${PREFIX}/lib/cmake/protobuf/protobuf-targets-release.cmake + fi + + # I don't know where this folder comes from, but it's interfering with the build in osx-64 + rm -rf $PREFIX/git + + if [[ "$CONDA_BUILD_CROSS_COMPILATION" == 1 ]]; then + export COMPILER_WORKS_EXITCODE=0 + export COMPILER_WORKS_EXITCODE__TRYRUN_OUTPUT="" + fi + + export MAX_JOBS=${CPU_COUNT} + + if [[ "$blas_impl" == "generic" ]]; then + # Fake openblas + export BLAS=OpenBLAS + sed -i.bak "s#FIND_LIBRARY.*#set(OpenBLAS_LIB ${PREFIX}/lib/liblapack${SHLIB_EXT} ${PREFIX}/lib/libcblas${SHLIB_EXT} ${PREFIX}/lib/libblas${SHLIB_EXT})#g" cmake/Modules/FindOpenBLAS.cmake + else + export BLAS=MKL + fi + + if [[ "$PKG_NAME" == "pytorch" ]]; then + PIP_ACTION=install + sed "s/3.12/$PY_VER/g" build/CMakeCache.txt.orig > build/CMakeCache.txt + else + # For the main script we just build a wheel for so that the C++/CUDA + # parts are built. Then they are reused in each python version. + PIP_ACTION=wheel + fi + + # MacOS build is simple, and will not be for CUDA + if [[ "$OSTYPE" == "darwin"* ]]; then + # Produce macOS builds with torch.distributed support. + # This is enabled by default on Linux, but disabled by default on macOS, + # because it requires an non-bundled compile-time dependency (libuv + # through gloo). This dependency is made available through meta.yaml, so + # we can override the default and set USE_DISTRIBUTED=1. + export USE_DISTRIBUTED=1 + + if [[ "$target_platform" == "osx-arm64" ]]; then + # MKLDNN did not support on Apple M1 at the time support Apple M1 + # was added. Revisit later + export USE_MKLDNN=0 + fi + elif [[ ${cuda_compiler_version} != "None" ]]; then + # Even though cudnn is used for CUDA builds, it's good to enable + # for MKLDNN for CUDA builds when CUDA builds are used on a machine + # with no NVIDIA GPUs. However compilation fails with mkldnn and cuda enabled. + export USE_MKLDNN=OFF + export USE_CUDA=1 + if [[ ${cuda_compiler_version} == 9.0* ]]; then + export TORCH_CUDA_ARCH_LIST="3.5;5.0;6.0;7.0+PTX" + export CUDA_TOOLKIT_ROOT_DIR=$CUDA_HOME + elif [[ ${cuda_compiler_version} == 9.2* ]]; then + export TORCH_CUDA_ARCH_LIST="3.5;5.0;6.0;6.1;7.0+PTX" + export CUDA_TOOLKIT_ROOT_DIR=$CUDA_HOME + elif [[ ${cuda_compiler_version} == 10.* ]]; then + export TORCH_CUDA_ARCH_LIST="3.5;5.0;6.0;6.1;7.0;7.5+PTX" + export CUDA_TOOLKIT_ROOT_DIR=$CUDA_HOME + elif [[ ${cuda_compiler_version} == 11.0* ]]; then + export TORCH_CUDA_ARCH_LIST="3.5;5.0;6.0;6.1;7.0;7.5;8.0+PTX" + export CUDA_TOOLKIT_ROOT_DIR=$CUDA_HOME + elif [[ ${cuda_compiler_version} == 11.1 ]]; then + export TORCH_CUDA_ARCH_LIST="3.5;5.0;6.0;6.1;7.0;7.5;8.0;8.6+PTX" + export CUDA_TOOLKIT_ROOT_DIR=$CUDA_HOME + elif [[ ${cuda_compiler_version} == 11.2 ]]; then + export TORCH_CUDA_ARCH_LIST="3.5;5.0;6.0;6.1;7.0;7.5;8.0;8.6+PTX" + export CUDA_TOOLKIT_ROOT_DIR=$CUDA_HOME + elif [[ ${cuda_compiler_version} == 11.8 ]]; then + export TORCH_CUDA_ARCH_LIST="3.5;5.0;6.0;6.1;7.0;7.5;8.0;8.6;8.9+PTX" + export CUDA_TOOLKIT_ROOT_DIR=$CUDA_HOME + elif [[ ${cuda_compiler_version} == 12.0 ]]; then + export TORCH_CUDA_ARCH_LIST="5.0;6.0;6.1;7.0;7.5;8.0;8.6;8.9;9.0+PTX" + # $CUDA_HOME not set in CUDA 12.0. Using $PREFIX + export CUDA_TOOLKIT_ROOT_DIR="${PREFIX}" + else + echo "unsupported cuda version. edit build_pytorch.sh" + exit 1 + fi + export TORCH_NVCC_FLAGS="-Xfatbin -compress-all" + export NCCL_ROOT_DIR=$PREFIX + export NCCL_INCLUDE_DIR=$PREFIX/include + export USE_SYSTEM_NCCL=1 + export USE_STATIC_NCCL=0 + export USE_STATIC_CUDNN=0 + export MAGMA_HOME="${PREFIX}" + else + if [[ "$target_platform" != *-64 ]]; then + # Breakpad seems to not work on aarch64 or ppc64le + # https://github.com/pytorch/pytorch/issues/67083 + export USE_BREAKPAD=0 + fi + # MKLDNN is an Apache-2.0 licensed library for DNNs and is used + # for CPU builds. Not to be confused with MKL. + export USE_MKLDNN=1 + export USE_CUDA=0 + export CMAKE_TOOLCHAIN_FILE="${RECIPE_DIR}/cross-linux.cmake" + fi + + echo '${CXX}'=${CXX} + echo '${PREFIX}'=${PREFIX} + # End of CF build script pyodide build ls -la tree . From ee59cbe96548409cb91ec310b4834703df12d924 Mon Sep 17 00:00:00 2001 From: Max Rakitin Date: Thu, 8 Feb 2024 23:08:56 -0500 Subject: [PATCH 06/14] Remove unbound vars check --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 6a68f1a..d71787d 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -53,7 +53,7 @@ jobs: - name: Build with Pyodide run: | - set -vxeuo pipefail + set -vxeo pipefail git clone --depth 1 --branch ${{ matrix.version }} ${{ matrix.repo }} cd ${{ matrix.name }} From 9708117d70093fc715bad47b4e31bf21ac6a21ee Mon Sep 17 00:00:00 2001 From: Max Rakitin Date: Fri, 9 Feb 2024 07:53:15 -0500 Subject: [PATCH 07/14] Do not use cuda --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index d71787d..1a54b8a 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -224,10 +224,10 @@ jobs: # MKLDNN is an Apache-2.0 licensed library for DNNs and is used # for CPU builds. Not to be confused with MKL. export USE_MKLDNN=1 - export USE_CUDA=0 export CMAKE_TOOLCHAIN_FILE="${RECIPE_DIR}/cross-linux.cmake" fi + export USE_CUDA=0 echo '${CXX}'=${CXX} echo '${PREFIX}'=${PREFIX} # End of CF build script From 451d23657dd1f8737b091729519ccfb3aca4295b Mon Sep 17 00:00:00 2001 From: Max Rakitin Date: Tue, 13 Feb 2024 18:27:32 -0500 Subject: [PATCH 08/14] Remove some env vars --- .github/workflows/build.yml | 74 ++++--------------------------------- 1 file changed, 8 insertions(+), 66 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 1a54b8a..d3fa4a0 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -159,73 +159,15 @@ jobs: PIP_ACTION=wheel fi - # MacOS build is simple, and will not be for CUDA - if [[ "$OSTYPE" == "darwin"* ]]; then - # Produce macOS builds with torch.distributed support. - # This is enabled by default on Linux, but disabled by default on macOS, - # because it requires an non-bundled compile-time dependency (libuv - # through gloo). This dependency is made available through meta.yaml, so - # we can override the default and set USE_DISTRIBUTED=1. - export USE_DISTRIBUTED=1 - - if [[ "$target_platform" == "osx-arm64" ]]; then - # MKLDNN did not support on Apple M1 at the time support Apple M1 - # was added. Revisit later - export USE_MKLDNN=0 - fi - elif [[ ${cuda_compiler_version} != "None" ]]; then - # Even though cudnn is used for CUDA builds, it's good to enable - # for MKLDNN for CUDA builds when CUDA builds are used on a machine - # with no NVIDIA GPUs. However compilation fails with mkldnn and cuda enabled. - export USE_MKLDNN=OFF - export USE_CUDA=1 - if [[ ${cuda_compiler_version} == 9.0* ]]; then - export TORCH_CUDA_ARCH_LIST="3.5;5.0;6.0;7.0+PTX" - export CUDA_TOOLKIT_ROOT_DIR=$CUDA_HOME - elif [[ ${cuda_compiler_version} == 9.2* ]]; then - export TORCH_CUDA_ARCH_LIST="3.5;5.0;6.0;6.1;7.0+PTX" - export CUDA_TOOLKIT_ROOT_DIR=$CUDA_HOME - elif [[ ${cuda_compiler_version} == 10.* ]]; then - export TORCH_CUDA_ARCH_LIST="3.5;5.0;6.0;6.1;7.0;7.5+PTX" - export CUDA_TOOLKIT_ROOT_DIR=$CUDA_HOME - elif [[ ${cuda_compiler_version} == 11.0* ]]; then - export TORCH_CUDA_ARCH_LIST="3.5;5.0;6.0;6.1;7.0;7.5;8.0+PTX" - export CUDA_TOOLKIT_ROOT_DIR=$CUDA_HOME - elif [[ ${cuda_compiler_version} == 11.1 ]]; then - export TORCH_CUDA_ARCH_LIST="3.5;5.0;6.0;6.1;7.0;7.5;8.0;8.6+PTX" - export CUDA_TOOLKIT_ROOT_DIR=$CUDA_HOME - elif [[ ${cuda_compiler_version} == 11.2 ]]; then - export TORCH_CUDA_ARCH_LIST="3.5;5.0;6.0;6.1;7.0;7.5;8.0;8.6+PTX" - export CUDA_TOOLKIT_ROOT_DIR=$CUDA_HOME - elif [[ ${cuda_compiler_version} == 11.8 ]]; then - export TORCH_CUDA_ARCH_LIST="3.5;5.0;6.0;6.1;7.0;7.5;8.0;8.6;8.9+PTX" - export CUDA_TOOLKIT_ROOT_DIR=$CUDA_HOME - elif [[ ${cuda_compiler_version} == 12.0 ]]; then - export TORCH_CUDA_ARCH_LIST="5.0;6.0;6.1;7.0;7.5;8.0;8.6;8.9;9.0+PTX" - # $CUDA_HOME not set in CUDA 12.0. Using $PREFIX - export CUDA_TOOLKIT_ROOT_DIR="${PREFIX}" - else - echo "unsupported cuda version. edit build_pytorch.sh" - exit 1 - fi - export TORCH_NVCC_FLAGS="-Xfatbin -compress-all" - export NCCL_ROOT_DIR=$PREFIX - export NCCL_INCLUDE_DIR=$PREFIX/include - export USE_SYSTEM_NCCL=1 - export USE_STATIC_NCCL=0 - export USE_STATIC_CUDNN=0 - export MAGMA_HOME="${PREFIX}" - else - if [[ "$target_platform" != *-64 ]]; then - # Breakpad seems to not work on aarch64 or ppc64le - # https://github.com/pytorch/pytorch/issues/67083 - export USE_BREAKPAD=0 - fi - # MKLDNN is an Apache-2.0 licensed library for DNNs and is used - # for CPU builds. Not to be confused with MKL. - export USE_MKLDNN=1 - export CMAKE_TOOLCHAIN_FILE="${RECIPE_DIR}/cross-linux.cmake" + if [[ "$target_platform" != *-64 ]]; then + # Breakpad seems to not work on aarch64 or ppc64le + # https://github.com/pytorch/pytorch/issues/67083 + export USE_BREAKPAD=0 fi + # MKLDNN is an Apache-2.0 licensed library for DNNs and is used + # for CPU builds. Not to be confused with MKL. + export USE_MKLDNN=1 + export CMAKE_TOOLCHAIN_FILE="${RECIPE_DIR}/cross-linux.cmake" export USE_CUDA=0 echo '${CXX}'=${CXX} From eca7c1fd81c09c887b5c6136d98a1e4541bd0c15 Mon Sep 17 00:00:00 2001 From: Max Rakitin Date: Tue, 13 Feb 2024 19:13:04 -0500 Subject: [PATCH 09/14] Add CF build script --- scripts/build-pytorch.sh | 198 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 198 insertions(+) create mode 100644 scripts/build-pytorch.sh diff --git a/scripts/build-pytorch.sh b/scripts/build-pytorch.sh new file mode 100644 index 0000000..ad92fe9 --- /dev/null +++ b/scripts/build-pytorch.sh @@ -0,0 +1,198 @@ +#!/bin/bash + +set -ex + +# remove pyproject.toml to avoid installing deps from pip +rm -rf pyproject.toml + +# uncomment to debug cmake build +# export CMAKE_VERBOSE_MAKEFILE=1 + +export USE_NUMA=0 +export USE_ITT=0 +export CFLAGS="$(echo $CFLAGS | sed 's/-fvisibility-inlines-hidden//g')" +export CXXFLAGS="$(echo $CXXFLAGS | sed 's/-fvisibility-inlines-hidden//g')" +export LDFLAGS="$(echo $LDFLAGS | sed 's/-Wl,--as-needed//g')" +export LDFLAGS="$(echo $LDFLAGS | sed 's/-Wl,-dead_strip_dylibs//g')" +export LDFLAGS_LD="$(echo $LDFLAGS_LD | sed 's/-dead_strip_dylibs//g')" +if [[ "$c_compiler" == "clang" ]]; then + export CXXFLAGS="$CXXFLAGS -Wno-deprecated-declarations -Wno-unknown-warning-option -Wno-error=unused-command-line-argument" + export CFLAGS="$CFLAGS -Wno-deprecated-declarations -Wno-unknown-warning-option -Wno-error=unused-command-line-argument" +else + export CXXFLAGS="$CXXFLAGS -Wno-deprecated-declarations -Wno-error=maybe-uninitialized" + export CFLAGS="$CFLAGS -Wno-deprecated-declarations -Wno-error=maybe-uninitialized" +fi + +# This is not correctly found for linux-aarch64 since pytorch 2.0.0 for some reason +export _GLIBCXX_USE_CXX11_ABI=1 + +# KINETO seems to require CUPTI and will look quite hard for it. +# CUPTI seems to cause trouble when users install a version of +# cudatoolkit different than the one specified at compile time. +# https://github.com/conda-forge/pytorch-cpu-feedstock/issues/135 +export USE_KINETO=OFF + +if [[ "$target_platform" == "osx-64" ]]; then + export CXXFLAGS="$CXXFLAGS -DTARGET_OS_OSX=1" + export CFLAGS="$CFLAGS -DTARGET_OS_OSX=1" +fi + +# Dynamic libraries need to be lazily loaded so that torch +# can be imported on system without a GPU +LDFLAGS="${LDFLAGS//-Wl,-z,now/-Wl,-z,lazy}" + +export CMAKE_GENERATOR=Ninja +export CMAKE_LIBRARY_PATH=$PREFIX/lib:$PREFIX/include:$CMAKE_LIBRARY_PATH +export CMAKE_PREFIX_PATH=$PREFIX +export CMAKE_BUILD_TYPE=Release + +for ARG in $CMAKE_ARGS; do + if [[ "$ARG" == "-DCMAKE_"* ]]; then + cmake_arg=$(echo $ARG | cut -d= -f1) + cmake_arg=$(echo $cmake_arg| cut -dD -f2-) + cmake_val=$(echo $ARG | cut -d= -f2-) + printf -v $cmake_arg "$cmake_val" + export ${cmake_arg} + fi +done +unset CMAKE_INSTALL_PREFIX +export TH_BINARY_BUILD=1 +export PYTORCH_BUILD_VERSION=$PKG_VERSION +export PYTORCH_BUILD_NUMBER=$PKG_BUILDNUM + +export INSTALL_TEST=0 +export BUILD_TEST=0 + +export USE_SYSTEM_SLEEF=1 +# use our protobuf +export BUILD_CUSTOM_PROTOBUF=OFF +rm -rf $PREFIX/bin/protoc + +if [[ "${target_platform}" != "${build_platform}" ]]; then + # It helps cross compiled builds without emulation support to complete + # Use BUILD PREFIX protoc instead of the one that is from the host platform + sed -i.bak \ + "s,IMPORTED_LOCATION_RELEASE .*/bin/protoc,IMPORTED_LOCATION_RELEASE \"${BUILD_PREFIX}/bin/protoc," \ + ${PREFIX}/lib/cmake/protobuf/protobuf-targets-release.cmake +fi + +# I don't know where this folder comes from, but it's interfering with the build in osx-64 +rm -rf $PREFIX/git + +if [[ "$CONDA_BUILD_CROSS_COMPILATION" == 1 ]]; then + export COMPILER_WORKS_EXITCODE=0 + export COMPILER_WORKS_EXITCODE__TRYRUN_OUTPUT="" +fi + +export MAX_JOBS=${CPU_COUNT} + +if [[ "$blas_impl" == "generic" ]]; then + # Fake openblas + export BLAS=OpenBLAS + sed -i.bak "s#FIND_LIBRARY.*#set(OpenBLAS_LIB ${PREFIX}/lib/liblapack${SHLIB_EXT} ${PREFIX}/lib/libcblas${SHLIB_EXT} ${PREFIX}/lib/libblas${SHLIB_EXT})#g" cmake/Modules/FindOpenBLAS.cmake +else + export BLAS=MKL +fi + +if [[ "$PKG_NAME" == "pytorch" ]]; then + PIP_ACTION=install + sed "s/3.12/$PY_VER/g" build/CMakeCache.txt.orig > build/CMakeCache.txt +else + # For the main script we just build a wheel for so that the C++/CUDA + # parts are built. Then they are reused in each python version. + PIP_ACTION=wheel +fi + +# MacOS build is simple, and will not be for CUDA +if [[ "$OSTYPE" == "darwin"* ]]; then + # Produce macOS builds with torch.distributed support. + # This is enabled by default on Linux, but disabled by default on macOS, + # because it requires an non-bundled compile-time dependency (libuv + # through gloo). This dependency is made available through meta.yaml, so + # we can override the default and set USE_DISTRIBUTED=1. + export USE_DISTRIBUTED=1 + + if [[ "$target_platform" == "osx-arm64" ]]; then + # MKLDNN did not support on Apple M1 at the time support Apple M1 + # was added. Revisit later + export USE_MKLDNN=0 + fi +elif [[ ${cuda_compiler_version} != "None" ]]; then + # Even though cudnn is used for CUDA builds, it's good to enable + # for MKLDNN for CUDA builds when CUDA builds are used on a machine + # with no NVIDIA GPUs. However compilation fails with mkldnn and cuda enabled. + export USE_MKLDNN=OFF + export USE_CUDA=1 + if [[ ${cuda_compiler_version} == 9.0* ]]; then + export TORCH_CUDA_ARCH_LIST="3.5;5.0;6.0;7.0+PTX" + export CUDA_TOOLKIT_ROOT_DIR=$CUDA_HOME + elif [[ ${cuda_compiler_version} == 9.2* ]]; then + export TORCH_CUDA_ARCH_LIST="3.5;5.0;6.0;6.1;7.0+PTX" + export CUDA_TOOLKIT_ROOT_DIR=$CUDA_HOME + elif [[ ${cuda_compiler_version} == 10.* ]]; then + export TORCH_CUDA_ARCH_LIST="3.5;5.0;6.0;6.1;7.0;7.5+PTX" + export CUDA_TOOLKIT_ROOT_DIR=$CUDA_HOME + elif [[ ${cuda_compiler_version} == 11.0* ]]; then + export TORCH_CUDA_ARCH_LIST="3.5;5.0;6.0;6.1;7.0;7.5;8.0+PTX" + export CUDA_TOOLKIT_ROOT_DIR=$CUDA_HOME + elif [[ ${cuda_compiler_version} == 11.1 ]]; then + export TORCH_CUDA_ARCH_LIST="3.5;5.0;6.0;6.1;7.0;7.5;8.0;8.6+PTX" + export CUDA_TOOLKIT_ROOT_DIR=$CUDA_HOME + elif [[ ${cuda_compiler_version} == 11.2 ]]; then + export TORCH_CUDA_ARCH_LIST="3.5;5.0;6.0;6.1;7.0;7.5;8.0;8.6+PTX" + export CUDA_TOOLKIT_ROOT_DIR=$CUDA_HOME + elif [[ ${cuda_compiler_version} == 11.8 ]]; then + export TORCH_CUDA_ARCH_LIST="3.5;5.0;6.0;6.1;7.0;7.5;8.0;8.6;8.9+PTX" + export CUDA_TOOLKIT_ROOT_DIR=$CUDA_HOME + elif [[ ${cuda_compiler_version} == 12.0 ]]; then + export TORCH_CUDA_ARCH_LIST="5.0;6.0;6.1;7.0;7.5;8.0;8.6;8.9;9.0+PTX" + # $CUDA_HOME not set in CUDA 12.0. Using $PREFIX + export CUDA_TOOLKIT_ROOT_DIR="${PREFIX}" + else + echo "unsupported cuda version. edit build_pytorch.sh" + exit 1 + fi + export TORCH_NVCC_FLAGS="-Xfatbin -compress-all" + export NCCL_ROOT_DIR=$PREFIX + export NCCL_INCLUDE_DIR=$PREFIX/include + export USE_SYSTEM_NCCL=1 + export USE_STATIC_NCCL=0 + export USE_STATIC_CUDNN=0 + export MAGMA_HOME="${PREFIX}" +else + if [[ "$target_platform" != *-64 ]]; then + # Breakpad seems to not work on aarch64 or ppc64le + # https://github.com/pytorch/pytorch/issues/67083 + export USE_BREAKPAD=0 + fi + # MKLDNN is an Apache-2.0 licensed library for DNNs and is used + # for CPU builds. Not to be confused with MKL. + export USE_MKLDNN=1 + export USE_CUDA=0 + export CMAKE_TOOLCHAIN_FILE="${RECIPE_DIR}/cross-linux.cmake" +fi + +echo '${CXX}'=${CXX} +echo '${PREFIX}'=${PREFIX} +$PREFIX/bin/python -m pip $PIP_ACTION . --no-deps -vvv --no-clean \ + | sed "s,${CXX},\$\{CXX\},g" \ + | sed "s,${PREFIX},\$\{PREFIX\},g" + +if [[ "$PKG_NAME" == "libtorch" ]]; then + mkdir -p $SRC_DIR/dist + pushd $SRC_DIR/dist + wheel unpack ../torch-*.whl + pushd torch-* + mv torch/bin/* ${PREFIX}/bin + mv torch/lib/* ${PREFIX}/lib + mv torch/share/* ${PREFIX}/share + for f in ATen caffe2 tensorpipe torch c10; do + mv torch/include/$f ${PREFIX}/include/$f + done + rm ${PREFIX}/lib/libtorch_python.* + popd + popd + + # Keep the original backed up to sed later + cp build/CMakeCache.txt build/CMakeCache.txt.orig +fi From 1c1c49bd272025684c0b238d1b2170b17e0219a6 Mon Sep 17 00:00:00 2001 From: Max Rakitin Date: Tue, 13 Feb 2024 19:17:27 -0500 Subject: [PATCH 10/14] Source compiler vars from CF build script --- .github/workflows/build.yml | 119 ++---------------------------------- scripts/build-pytorch.sh | 44 ++++++------- 2 files changed, 26 insertions(+), 137 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index d3fa4a0..7151ad9 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -54,125 +54,14 @@ jobs: - name: Build with Pyodide run: | set -vxeo pipefail - git clone --depth 1 --branch ${{ matrix.version }} ${{ matrix.repo }} - cd ${{ matrix.name }} - - # CF build script: https://github.com/conda-forge/pytorch-cpu-feedstock/blob/main/recipe/build.sh - # remove pyproject.toml to avoid installing deps from pip - rm -rf pyproject.toml - - # uncomment to debug cmake build - # export CMAKE_VERBOSE_MAKEFILE=1 - - export USE_NUMA=0 - export USE_ITT=0 - export CFLAGS="$(echo $CFLAGS | sed 's/-fvisibility-inlines-hidden//g')" - export CXXFLAGS="$(echo $CXXFLAGS | sed 's/-fvisibility-inlines-hidden//g')" - export LDFLAGS="$(echo $LDFLAGS | sed 's/-Wl,--as-needed//g')" - export LDFLAGS="$(echo $LDFLAGS | sed 's/-Wl,-dead_strip_dylibs//g')" - export LDFLAGS_LD="$(echo $LDFLAGS_LD | sed 's/-dead_strip_dylibs//g')" - if [[ "$c_compiler" == "clang" ]]; then - export CXXFLAGS="$CXXFLAGS -Wno-deprecated-declarations -Wno-unknown-warning-option -Wno-error=unused-command-line-argument" - export CFLAGS="$CFLAGS -Wno-deprecated-declarations -Wno-unknown-warning-option -Wno-error=unused-command-line-argument" - else - export CXXFLAGS="$CXXFLAGS -Wno-deprecated-declarations -Wno-error=maybe-uninitialized" - export CFLAGS="$CFLAGS -Wno-deprecated-declarations -Wno-error=maybe-uninitialized" - fi - - # This is not correctly found for linux-aarch64 since pytorch 2.0.0 for some reason - export _GLIBCXX_USE_CXX11_ABI=1 - - # KINETO seems to require CUPTI and will look quite hard for it. - # CUPTI seems to cause trouble when users install a version of - # cudatoolkit different than the one specified at compile time. - # https://github.com/conda-forge/pytorch-cpu-feedstock/issues/135 - export USE_KINETO=OFF - - if [[ "$target_platform" == "osx-64" ]]; then - export CXXFLAGS="$CXXFLAGS -DTARGET_OS_OSX=1" - export CFLAGS="$CFLAGS -DTARGET_OS_OSX=1" - fi - - # Dynamic libraries need to be lazily loaded so that torch - # can be imported on system without a GPU - LDFLAGS="${LDFLAGS//-Wl,-z,now/-Wl,-z,lazy}" - - export CMAKE_GENERATOR=Ninja - export CMAKE_LIBRARY_PATH=$PREFIX/lib:$PREFIX/include:$CMAKE_LIBRARY_PATH - export CMAKE_PREFIX_PATH=$PREFIX - export CMAKE_BUILD_TYPE=Release - for ARG in $CMAKE_ARGS; do - if [[ "$ARG" == "-DCMAKE_"* ]]; then - cmake_arg=$(echo $ARG | cut -d= -f1) - cmake_arg=$(echo $cmake_arg| cut -dD -f2-) - cmake_val=$(echo $ARG | cut -d= -f2-) - printf -v $cmake_arg "$cmake_val" - export ${cmake_arg} - fi - done - unset CMAKE_INSTALL_PREFIX - export TH_BINARY_BUILD=1 - export PYTORCH_BUILD_VERSION=$PKG_VERSION - export PYTORCH_BUILD_NUMBER=$PKG_BUILDNUM + # Source the variables from the CF build script: https://github.com/conda-forge/pytorch-cpu-feedstock/blob/main/recipe/build.sh + source scripts/build-pytorch.sh - export INSTALL_TEST=0 - export BUILD_TEST=0 - - export USE_SYSTEM_SLEEF=1 - # use our protobuf - export BUILD_CUSTOM_PROTOBUF=OFF - rm -rf $PREFIX/bin/protoc - - if [[ "${target_platform}" != "${build_platform}" ]]; then - # It helps cross compiled builds without emulation support to complete - # Use BUILD PREFIX protoc instead of the one that is from the host platform - sed -i.bak \ - "s,IMPORTED_LOCATION_RELEASE .*/bin/protoc,IMPORTED_LOCATION_RELEASE \"${BUILD_PREFIX}/bin/protoc," \ - ${PREFIX}/lib/cmake/protobuf/protobuf-targets-release.cmake - fi - - # I don't know where this folder comes from, but it's interfering with the build in osx-64 - rm -rf $PREFIX/git - - if [[ "$CONDA_BUILD_CROSS_COMPILATION" == 1 ]]; then - export COMPILER_WORKS_EXITCODE=0 - export COMPILER_WORKS_EXITCODE__TRYRUN_OUTPUT="" - fi - - export MAX_JOBS=${CPU_COUNT} - - if [[ "$blas_impl" == "generic" ]]; then - # Fake openblas - export BLAS=OpenBLAS - sed -i.bak "s#FIND_LIBRARY.*#set(OpenBLAS_LIB ${PREFIX}/lib/liblapack${SHLIB_EXT} ${PREFIX}/lib/libcblas${SHLIB_EXT} ${PREFIX}/lib/libblas${SHLIB_EXT})#g" cmake/Modules/FindOpenBLAS.cmake - else - export BLAS=MKL - fi - - if [[ "$PKG_NAME" == "pytorch" ]]; then - PIP_ACTION=install - sed "s/3.12/$PY_VER/g" build/CMakeCache.txt.orig > build/CMakeCache.txt - else - # For the main script we just build a wheel for so that the C++/CUDA - # parts are built. Then they are reused in each python version. - PIP_ACTION=wheel - fi + git clone --depth 1 --branch ${{ matrix.version }} ${{ matrix.repo }} + cd ${{ matrix.name }} - if [[ "$target_platform" != *-64 ]]; then - # Breakpad seems to not work on aarch64 or ppc64le - # https://github.com/pytorch/pytorch/issues/67083 - export USE_BREAKPAD=0 - fi - # MKLDNN is an Apache-2.0 licensed library for DNNs and is used - # for CPU builds. Not to be confused with MKL. - export USE_MKLDNN=1 - export CMAKE_TOOLCHAIN_FILE="${RECIPE_DIR}/cross-linux.cmake" - export USE_CUDA=0 - echo '${CXX}'=${CXX} - echo '${PREFIX}'=${PREFIX} - # End of CF build script pyodide build ls -la tree . diff --git a/scripts/build-pytorch.sh b/scripts/build-pytorch.sh index ad92fe9..72a9ccd 100644 --- a/scripts/build-pytorch.sh +++ b/scripts/build-pytorch.sh @@ -174,25 +174,25 @@ fi echo '${CXX}'=${CXX} echo '${PREFIX}'=${PREFIX} -$PREFIX/bin/python -m pip $PIP_ACTION . --no-deps -vvv --no-clean \ - | sed "s,${CXX},\$\{CXX\},g" \ - | sed "s,${PREFIX},\$\{PREFIX\},g" - -if [[ "$PKG_NAME" == "libtorch" ]]; then - mkdir -p $SRC_DIR/dist - pushd $SRC_DIR/dist - wheel unpack ../torch-*.whl - pushd torch-* - mv torch/bin/* ${PREFIX}/bin - mv torch/lib/* ${PREFIX}/lib - mv torch/share/* ${PREFIX}/share - for f in ATen caffe2 tensorpipe torch c10; do - mv torch/include/$f ${PREFIX}/include/$f - done - rm ${PREFIX}/lib/libtorch_python.* - popd - popd - - # Keep the original backed up to sed later - cp build/CMakeCache.txt build/CMakeCache.txt.orig -fi +# $PREFIX/bin/python -m pip $PIP_ACTION . --no-deps -vvv --no-clean \ +# | sed "s,${CXX},\$\{CXX\},g" \ +# | sed "s,${PREFIX},\$\{PREFIX\},g" + +# if [[ "$PKG_NAME" == "libtorch" ]]; then +# mkdir -p $SRC_DIR/dist +# pushd $SRC_DIR/dist +# wheel unpack ../torch-*.whl +# pushd torch-* +# mv torch/bin/* ${PREFIX}/bin +# mv torch/lib/* ${PREFIX}/lib +# mv torch/share/* ${PREFIX}/share +# for f in ATen caffe2 tensorpipe torch c10; do +# mv torch/include/$f ${PREFIX}/include/$f +# done +# rm ${PREFIX}/lib/libtorch_python.* +# popd +# popd + +# # Keep the original backed up to sed later +# cp build/CMakeCache.txt build/CMakeCache.txt.orig +# fi From ee73af79c1f9a93e9ca08fea893c766477b22d90 Mon Sep 17 00:00:00 2001 From: Max Rakitin Date: Tue, 13 Feb 2024 19:18:54 -0500 Subject: [PATCH 11/14] Rearrange the order of sourcing and use v2.1.2 --- .github/workflows/build.yml | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 7151ad9..49ada81 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -27,7 +27,7 @@ jobs: - repo: https://github.com/pytorch/pytorch name: pytorch package-name: pytorch - version: "v1.13.1" + version: "v2.1.2" steps: - name: Check out the repo @@ -55,12 +55,11 @@ jobs: run: | set -vxeo pipefail - # Source the variables from the CF build script: https://github.com/conda-forge/pytorch-cpu-feedstock/blob/main/recipe/build.sh - source scripts/build-pytorch.sh - git clone --depth 1 --branch ${{ matrix.version }} ${{ matrix.repo }} cd ${{ matrix.name }} + # Source the variables from the CF build script: https://github.com/conda-forge/pytorch-cpu-feedstock/blob/main/recipe/build.sh + source ../scripts/build-pytorch.sh pyodide build ls -la From c0802c8fdad810cf6699f54f88a9da6d6d256fdc Mon Sep 17 00:00:00 2001 From: Max Rakitin Date: Tue, 13 Feb 2024 19:21:07 -0500 Subject: [PATCH 12/14] Print discovered cuda version --- scripts/build-pytorch.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/scripts/build-pytorch.sh b/scripts/build-pytorch.sh index 72a9ccd..92d8c12 100644 --- a/scripts/build-pytorch.sh +++ b/scripts/build-pytorch.sh @@ -123,6 +123,8 @@ elif [[ ${cuda_compiler_version} != "None" ]]; then # with no NVIDIA GPUs. However compilation fails with mkldnn and cuda enabled. export USE_MKLDNN=OFF export USE_CUDA=1 + + echo "cuda_compiler_version = ${cuda_compiler_version}" if [[ ${cuda_compiler_version} == 9.0* ]]; then export TORCH_CUDA_ARCH_LIST="3.5;5.0;6.0;7.0+PTX" export CUDA_TOOLKIT_ROOT_DIR=$CUDA_HOME From f7f6ab45e86771f85f760231435043218d833672 Mon Sep 17 00:00:00 2001 From: Max Rakitin Date: Tue, 13 Feb 2024 19:23:23 -0500 Subject: [PATCH 13/14] Do not deal with cuda versions --- scripts/build-pytorch.sh | 44 ---------------------------------------- 1 file changed, 44 deletions(-) diff --git a/scripts/build-pytorch.sh b/scripts/build-pytorch.sh index 92d8c12..3590887 100644 --- a/scripts/build-pytorch.sh +++ b/scripts/build-pytorch.sh @@ -117,50 +117,6 @@ if [[ "$OSTYPE" == "darwin"* ]]; then # was added. Revisit later export USE_MKLDNN=0 fi -elif [[ ${cuda_compiler_version} != "None" ]]; then - # Even though cudnn is used for CUDA builds, it's good to enable - # for MKLDNN for CUDA builds when CUDA builds are used on a machine - # with no NVIDIA GPUs. However compilation fails with mkldnn and cuda enabled. - export USE_MKLDNN=OFF - export USE_CUDA=1 - - echo "cuda_compiler_version = ${cuda_compiler_version}" - if [[ ${cuda_compiler_version} == 9.0* ]]; then - export TORCH_CUDA_ARCH_LIST="3.5;5.0;6.0;7.0+PTX" - export CUDA_TOOLKIT_ROOT_DIR=$CUDA_HOME - elif [[ ${cuda_compiler_version} == 9.2* ]]; then - export TORCH_CUDA_ARCH_LIST="3.5;5.0;6.0;6.1;7.0+PTX" - export CUDA_TOOLKIT_ROOT_DIR=$CUDA_HOME - elif [[ ${cuda_compiler_version} == 10.* ]]; then - export TORCH_CUDA_ARCH_LIST="3.5;5.0;6.0;6.1;7.0;7.5+PTX" - export CUDA_TOOLKIT_ROOT_DIR=$CUDA_HOME - elif [[ ${cuda_compiler_version} == 11.0* ]]; then - export TORCH_CUDA_ARCH_LIST="3.5;5.0;6.0;6.1;7.0;7.5;8.0+PTX" - export CUDA_TOOLKIT_ROOT_DIR=$CUDA_HOME - elif [[ ${cuda_compiler_version} == 11.1 ]]; then - export TORCH_CUDA_ARCH_LIST="3.5;5.0;6.0;6.1;7.0;7.5;8.0;8.6+PTX" - export CUDA_TOOLKIT_ROOT_DIR=$CUDA_HOME - elif [[ ${cuda_compiler_version} == 11.2 ]]; then - export TORCH_CUDA_ARCH_LIST="3.5;5.0;6.0;6.1;7.0;7.5;8.0;8.6+PTX" - export CUDA_TOOLKIT_ROOT_DIR=$CUDA_HOME - elif [[ ${cuda_compiler_version} == 11.8 ]]; then - export TORCH_CUDA_ARCH_LIST="3.5;5.0;6.0;6.1;7.0;7.5;8.0;8.6;8.9+PTX" - export CUDA_TOOLKIT_ROOT_DIR=$CUDA_HOME - elif [[ ${cuda_compiler_version} == 12.0 ]]; then - export TORCH_CUDA_ARCH_LIST="5.0;6.0;6.1;7.0;7.5;8.0;8.6;8.9;9.0+PTX" - # $CUDA_HOME not set in CUDA 12.0. Using $PREFIX - export CUDA_TOOLKIT_ROOT_DIR="${PREFIX}" - else - echo "unsupported cuda version. edit build_pytorch.sh" - exit 1 - fi - export TORCH_NVCC_FLAGS="-Xfatbin -compress-all" - export NCCL_ROOT_DIR=$PREFIX - export NCCL_INCLUDE_DIR=$PREFIX/include - export USE_SYSTEM_NCCL=1 - export USE_STATIC_NCCL=0 - export USE_STATIC_CUDNN=0 - export MAGMA_HOME="${PREFIX}" else if [[ "$target_platform" != *-64 ]]; then # Breakpad seems to not work on aarch64 or ppc64le From f54c4148f9c397cf75ff175e5154bc7e0654753d Mon Sep 17 00:00:00 2001 From: Max Rakitin Date: Tue, 19 Mar 2024 01:54:09 -0400 Subject: [PATCH 14/14] Install `pyyaml` build dep --- .github/workflows/build.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 49ada81..54c2f7c 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -27,7 +27,7 @@ jobs: - repo: https://github.com/pytorch/pytorch name: pytorch package-name: pytorch - version: "v2.1.2" + version: "v1.13.1" steps: - name: Check out the repo @@ -61,7 +61,7 @@ jobs: # Source the variables from the CF build script: https://github.com/conda-forge/pytorch-cpu-feedstock/blob/main/recipe/build.sh source ../scripts/build-pytorch.sh - pyodide build + pyodide build --build-dependencies pyyaml ls -la tree .