2022-07-01 03:40:17 +08:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
2022-07-07 05:39:30 +08:00
|
|
|
set -xeu -o pipefail
|
2022-07-01 03:40:17 +08:00
|
|
|
|
|
|
|
SRC_ROOT="$( cd "$(dirname "$0")" ; pwd -P)/.."
|
|
|
|
PYTORCH_ROOT=${PYTORCH_ROOT:-$SRC_ROOT/externals/pytorch}
|
|
|
|
PYTORCH_INSTALL_PATH=${PYTORCH_INSTALL_PATH:-$SRC_ROOT/libtorch}
|
2022-07-15 08:16:51 +08:00
|
|
|
PYTORCH_REPO="${PYTORCH_REPO:-pytorch/pytorch}"
|
2022-07-01 03:40:17 +08:00
|
|
|
PYTORCH_BRANCH="${PYTORCH_BRANCH:-master}"
|
|
|
|
PT_C_COMPILER="${PT_C_COMPILER:-clang}"
|
|
|
|
PT_CXX_COMPILER="${PT_CXX_COMPILER:-clang++}"
|
2022-07-12 09:46:49 +08:00
|
|
|
CMAKE_OSX_ARCHITECTURES="${CMAKE_OSX_ARCHITECTURES:-arm64;x86_64}"
|
2022-07-06 01:25:43 +08:00
|
|
|
WHEELHOUSE="${WHEELHOUSE:-$SRC_ROOT/build_tools/python_deploy/wheelhouse}"
|
2022-07-12 09:46:49 +08:00
|
|
|
PYTHON_BIN="${TORCH_MLIR_PYTHON_VERSION:-python3}"
|
|
|
|
PIP_BIN="${TORCH_MLIR_PIP_VERSION:-pip3}"
|
|
|
|
CMAKE_C_COMPILER_LAUNCHER="${CMAKE_C_COMPILER_LAUNCHER:-""}"
|
|
|
|
CMAKE_CXX_COMPILER_LAUNCHER="${CMAKE_CXX_COMPILER_LAUNCHER:-""}"
|
2022-07-06 01:25:43 +08:00
|
|
|
|
|
|
|
Red='\033[0;31m'
|
|
|
|
Green='\033[0;32m'
|
|
|
|
Yellow='\033[1;33m'
|
|
|
|
NC='\033[0m'
|
2022-07-01 03:40:17 +08:00
|
|
|
|
|
|
|
echo "SRC_ROOT=${SRC_ROOT}"
|
|
|
|
echo "PYTORCH_ROOT=${PYTORCH_ROOT}"
|
2022-07-15 08:16:51 +08:00
|
|
|
echo "PYTORCH_REPO=${PYTORCH_REPO}"
|
2022-07-01 03:40:17 +08:00
|
|
|
echo "PYTORCH_BRANCH=${PYTORCH_BRANCH}"
|
2022-07-06 01:25:43 +08:00
|
|
|
echo "CMAKE_OSX_ARCHITECTURES=${CMAKE_OSX_ARCHITECTURES}"
|
|
|
|
export CMAKE_OSX_ARCHITECTURES=${CMAKE_OSX_ARCHITECTURES}
|
2022-07-12 09:46:49 +08:00
|
|
|
export CMAKE_C_COMPILER_LAUNCHER=${CMAKE_C_COMPILER_LAUNCHER}
|
|
|
|
export CMAKE_CXX_COMPILER_LAUNCHER=${CMAKE_CXX_COMPILER_LAUNCHER}
|
2022-07-01 03:40:17 +08:00
|
|
|
|
|
|
|
retry () {
|
2022-07-07 05:39:30 +08:00
|
|
|
"$@" || (sleep 1 && "$@") || (sleep 2 && "$@") || (sleep 4 && "$@") || (sleep 8 && "$@")
|
2022-07-01 03:40:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
install_requirements() {
|
2022-07-12 09:46:49 +08:00
|
|
|
${PIP_BIN} install -qr $PYTORCH_ROOT/requirements.txt
|
|
|
|
${PIP_BIN} list
|
2022-07-01 03:40:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
checkout_pytorch() {
|
|
|
|
if [[ ! -d "$PYTORCH_ROOT" ]]; then
|
2022-07-15 08:16:51 +08:00
|
|
|
git clone --depth 1 --single-branch --branch "${PYTORCH_BRANCH}" https://github.com/"$PYTORCH_REPO" "$PYTORCH_ROOT"
|
2022-07-01 03:40:17 +08:00
|
|
|
fi
|
2022-07-07 05:39:30 +08:00
|
|
|
cd "$PYTORCH_ROOT"
|
2022-07-12 09:46:49 +08:00
|
|
|
git reset --hard HEAD
|
|
|
|
git clean -df
|
|
|
|
for dep in protobuf pocketfft cpuinfo FP16 psimd fmt sleef pybind11 onnx flatbuffers foxi; do
|
|
|
|
git submodule update --init --depth 1 -- third_party/$dep
|
|
|
|
done
|
|
|
|
# setup.py will try to re-fetch
|
|
|
|
sed -i.bak -E 's/^[[:space:]]+check_submodules()/#check_submodules()/g' setup.py
|
2022-07-01 03:40:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
build_pytorch() {
|
2022-07-07 05:39:30 +08:00
|
|
|
cd "$PYTORCH_ROOT"
|
2022-07-06 01:25:43 +08:00
|
|
|
# Uncomment the next line if you want to iterate on source builds
|
2022-07-12 09:46:49 +08:00
|
|
|
# ${PYTHON_BIN} setup.py clean
|
|
|
|
rm -rf "${WHEELHOUSE:?}"/*
|
|
|
|
|
|
|
|
if [[ -z "${MAX_JOBS:-""}" ]]; then
|
|
|
|
if [[ "$(uname)" == 'Darwin' ]]; then
|
|
|
|
MAX_JOBS=$(sysctl -n hw.ncpu)
|
|
|
|
else
|
|
|
|
MAX_JOBS=$(nproc)
|
|
|
|
fi
|
2022-07-01 03:40:17 +08:00
|
|
|
fi
|
2022-07-12 09:46:49 +08:00
|
|
|
|
2022-07-15 08:16:51 +08:00
|
|
|
BUILD_SHARED_LIBS=ON \
|
2022-07-12 09:46:49 +08:00
|
|
|
BUILD_TEST=OFF \
|
2022-07-15 08:16:51 +08:00
|
|
|
GLIBCXX_USE_CXX11_ABI=1 \
|
2022-07-12 09:46:49 +08:00
|
|
|
CMAKE_OSX_ARCHITECTURES=${CMAKE_OSX_ARCHITECTURES} \
|
|
|
|
INTERN_BUILD_ATEN_OPS=OFF \
|
|
|
|
INTERN_DISABLE_ONNX=ON \
|
|
|
|
INTERN_USE_EIGEN_BLAS=ON \
|
|
|
|
MAX_JOBS=${MAX_JOBS} \
|
|
|
|
ONNX_ML=OFF \
|
|
|
|
USE_BREAKPAD=OFF \
|
|
|
|
USE_CUDA=OFF \
|
|
|
|
USE_DISTRIBUTED=OFF \
|
|
|
|
USE_EIGEN_FOR_BLAS=OFF \
|
|
|
|
USE_FBGEMM=OFF \
|
|
|
|
USE_GLOO=OFF \
|
|
|
|
USE_KINETO=OFF \
|
|
|
|
USE_MKL=OFF \
|
|
|
|
USE_MKLDNN=OFF \
|
|
|
|
USE_MPS=OFF \
|
|
|
|
USE_NCCL=OFF \
|
|
|
|
USE_NNPACK=OFF \
|
|
|
|
USE_OBSERVERS=OFF \
|
|
|
|
USE_OPENMP=OFF \
|
|
|
|
USE_PYTORCH_QNNPACK=OFF \
|
|
|
|
USE_QNNPACK=OFF \
|
|
|
|
USE_XNNPACK=OFF \
|
|
|
|
${PYTHON_BIN} setup.py bdist_wheel -d "$WHEELHOUSE"
|
2022-07-01 03:40:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
package_pytorch() {
|
2022-07-12 09:46:49 +08:00
|
|
|
mkdir -p libtorch
|
|
|
|
if [[ -d "libtorch/lib" ]]; then
|
|
|
|
rm -rf libtorch/{lib,bin,include,share}
|
|
|
|
fi
|
2022-07-01 03:40:17 +08:00
|
|
|
|
2022-07-06 01:25:43 +08:00
|
|
|
# Copy over all of the cmake files
|
|
|
|
mv build/lib*/torch/share libtorch/
|
|
|
|
mv build/lib*/torch/include libtorch/
|
|
|
|
mv build/lib*/torch/lib libtorch/
|
2022-07-01 03:40:17 +08:00
|
|
|
# Copy over all lib files
|
2022-07-06 01:25:43 +08:00
|
|
|
mv build/lib/* libtorch/lib/
|
2022-07-01 03:40:17 +08:00
|
|
|
# Copy over all include files
|
2022-07-06 01:25:43 +08:00
|
|
|
mv build/include/* libtorch/include/
|
2022-07-01 03:40:17 +08:00
|
|
|
|
2022-07-07 05:39:30 +08:00
|
|
|
(pushd "$PYTORCH_ROOT" && git rev-parse HEAD) > libtorch/build-hash
|
2022-07-01 03:40:17 +08:00
|
|
|
echo "Installing libtorch in ${PYTORCH_ROOT}/../../"
|
2022-07-12 09:46:49 +08:00
|
|
|
echo "deleting old ${PYTORCH_ROOT}/../../libtorch"
|
2022-07-07 05:39:30 +08:00
|
|
|
rm -rf "${PYTORCH_ROOT}"/../../libtorch
|
|
|
|
mv libtorch "${PYTORCH_ROOT}"/../../
|
2022-07-01 03:40:17 +08:00
|
|
|
}
|
|
|
|
|
2022-07-12 09:46:49 +08:00
|
|
|
install_pytorch() {
|
|
|
|
echo "pip installing Pytorch.."
|
|
|
|
${PIP_BIN} install --force-reinstall $WHEELHOUSE/*
|
|
|
|
}
|
|
|
|
|
2022-07-01 03:40:17 +08:00
|
|
|
#main
|
2022-07-15 08:16:51 +08:00
|
|
|
echo "Building libtorch from source"
|
|
|
|
checkout_pytorch
|
|
|
|
install_requirements
|
|
|
|
build_pytorch
|
|
|
|
package_pytorch
|
|
|
|
install_pytorch
|