|
| 1 | +#! /bin/bash -e |
| 2 | + |
| 3 | +trap "echo -e '\nScript interrupted. Exiting gracefully.'; exit 1" SIGINT |
| 4 | + |
| 5 | +# Copyright (C) 2024-2025 Red Hat, Inc. |
| 6 | +# |
| 7 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +# you may not use this file except in compliance with the License. |
| 9 | +# You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, software |
| 14 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +# See the License for the specific language governing permissions and |
| 17 | +# limitations under the License. |
| 18 | +# |
| 19 | +# SPDX-License-Identifier: Apache-2.0 |
| 20 | +set -euo pipefail |
| 21 | + |
| 22 | +WORKSPACE=${WORKSPACE:-${HOME}} |
| 23 | + |
| 24 | +TORCH_DIR=${WORKSPACE}/torch |
| 25 | +TORCH_REPO=https://github.com/pytorch/pytorch.git |
| 26 | + |
| 27 | +declare -a PIP_INSTALL_ARGS |
| 28 | +PIP_TORCH_INDEX_URL_BASE=https://download.pytorch.org/whl |
| 29 | + |
| 30 | +SUDO='' |
| 31 | +if ((EUID != 0)) && command -v sudo &>/dev/null; then |
| 32 | + SUDO="sudo" |
| 33 | +elif ((EUID != 0)); then |
| 34 | + echo "ERROR: $(basename "$0") requires root privileges or sudo." >&2 |
| 35 | + exit 1 |
| 36 | +fi |
| 37 | + |
| 38 | +pip_install() { |
| 39 | + if command -v uv &>/dev/null; then |
| 40 | + uv pip install "$@" |
| 41 | + else |
| 42 | + pip install "$@" |
| 43 | + fi |
| 44 | +} |
| 45 | + |
| 46 | +# Extract the major.minor version from ROCM_VERSION, e.g. 6.4 from 6.4.4 |
| 47 | +get_rocm_version() { |
| 48 | + [[ "$ROCM_VERSION" =~ ^([0-9]+\.[0-9]+) ]] && echo "${BASH_REMATCH[1]}" || |
| 49 | + echo "$ROCM_VERSION" |
| 50 | +} |
| 51 | + |
| 52 | +setup_src() { |
| 53 | + echo "Downloading Torch source code and setting up the environment for building from source..." |
| 54 | + |
| 55 | + if [ ! -d "$TORCH_DIR" ]; then |
| 56 | + echo "Cloning the Torch repo $TORCH_REPO to $TORCH_DIR ..." |
| 57 | + git clone "$TORCH_REPO" "$TORCH_DIR" |
| 58 | + if [ ! -d "$TORCH_DIR" ]; then |
| 59 | + echo "$TORCH_DIR not found. ERROR Cloning repository..." |
| 60 | + exit 1 |
| 61 | + else |
| 62 | + pushd "$TORCH_DIR" 1>/dev/null || exit 1 |
| 63 | + git submodule sync |
| 64 | + git submodule update --init --recursive |
| 65 | + |
| 66 | + if [ -n "${TORCH_GITREF:-}" ]; then |
| 67 | + git checkout "" |
| 68 | + fi |
| 69 | + |
| 70 | + echo "Install pre-commit hooks into your local Torch git repo (one-time)" |
| 71 | + pip_install pre-commit |
| 72 | + pre-commit install |
| 73 | + popd 1>/dev/null |
| 74 | + fi |
| 75 | + else |
| 76 | + echo "Torch repo already present, not cloning ..." |
| 77 | + fi |
| 78 | +} |
| 79 | + |
| 80 | +install_build_deps() { |
| 81 | + echo "Installing Torch build dependencies ..." |
| 82 | + |
| 83 | + pushd "$TORCH_DIR" 1>/dev/null || exit 1 |
| 84 | + |
| 85 | + if [ -f requirements.txt ]; then |
| 86 | + pip_install --group dev |
| 87 | + pip_install mkl-static mkl-include |
| 88 | + make triton |
| 89 | + fi |
| 90 | + |
| 91 | + $SUDO dnf -y install numactl-devel |
| 92 | + |
| 93 | + if [ -n "${ROCM_VERSION:-}" ]; then |
| 94 | + python tools/amd_build/build_amd.py |
| 95 | + fi |
| 96 | + |
| 97 | + popd 1>/dev/null |
| 98 | +} |
| 99 | + |
| 100 | +install_deps() { |
| 101 | + echo "Installing Torch dependencies ..." |
| 102 | + pip_install numpy |
| 103 | +} |
| 104 | + |
| 105 | +install_whl() { |
| 106 | + echo "Installing Torch ${PIP_TORCH_INDEX_URL_BUILD:-release} from PyPI ..." |
| 107 | + |
| 108 | + if [ -n "${PIP_TORCH_VERSION:-}" ]; then |
| 109 | + echo "Using the specified version $PIP_TORCH_VERSION of torch" |
| 110 | + PIP_TORCH_VERSION="==$PIP_TORCH_VERSION" |
| 111 | + fi |
| 112 | + |
| 113 | + if [ -n "${PIP_TORCHVISION_VERSION:-}" ]; then |
| 114 | + echo "Installing the specified version $PIP_TORCHVISION_VERSION of torchvision" |
| 115 | + PIP_TORCHVISION_VERSION="==$PIP_TORCHVISION_VERSION" |
| 116 | + fi |
| 117 | + |
| 118 | + if [ -n "${PIP_TORCHAUDIO_VERSION:-}" ]; then |
| 119 | + echo "Installing the specified version $PIP_TORCHAUDIO_VERSION of torchaudio" |
| 120 | + PIP_TORCHAUDIO_VERSION="==$PIP_TORCHAUDIO_VERSION" |
| 121 | + fi |
| 122 | + |
| 123 | + declare -a TORCH_PACKAGES=( |
| 124 | + "torch${PIP_TORCH_VERSION:-}" |
| 125 | + "torchvision${PIP_TORCHVISION_VERSION:-}" |
| 126 | + "torchaudio${PIP_TORCHAUDIO_VERSION:-}" |
| 127 | + ) |
| 128 | + |
| 129 | + if [ -n "${PIP_TORCH_INDEX_URL:-}" ]; then |
| 130 | + echo "Using the specified index, $PIP_TORCH_INDEX_URL" |
| 131 | + PIP_INSTALL_ARGS+=("--index-url" "$PIP_TORCH_INDEX_URL") |
| 132 | + elif command -v uv &>/dev/null && [ -n "${UV_TORCH_BACKEND:-}" ]; then |
| 133 | + echo "Using the specified uv backend, $UV_TORCH_BACKEND" |
| 134 | + PIP_INSTALL_ARGS+=("--torch-backend" "$UV_TORCH_BACKEND") |
| 135 | + elif ! command -v uv &>/dev/null && [ -n "${UV_TORCH_BACKEND:-}" ]; then |
| 136 | + echo "Error: UV_TORCH_BACKEND is set to $UV_TORCH_BACKEND but uv is not available." |
| 137 | + exit 1 |
| 138 | + else |
| 139 | + # Set compute platform for torch wheel installation |
| 140 | + if [ -n "${ROCM_VERSION:-}" ]; then |
| 141 | + echo "Using the ROCm version $ROCM_VERSION backend" |
| 142 | + COMPUTE_PLATFORM="rocm$(get_rocm_version)" |
| 143 | + elif ((${TRITON_CPU_BACKEND:-0} == 1)); then |
| 144 | + echo "Using the CPU backend" |
| 145 | + COMPUTE_PLATFORM="cpu" |
| 146 | + elif [ -n "${CUDA_VERSION:-}" ]; then |
| 147 | + echo "Using the CUDA version $CUDA_VERSION backend" |
| 148 | + COMPUTE_PLATFORM="cu${CUDA_VERSION/[.-]/}" |
| 149 | + fi |
| 150 | + |
| 151 | + if [ -n "${COMPUTE_PLATFORM:-}" ]; then |
| 152 | + [[ -n "${PIP_TORCH_INDEX_URL_BUILD:-}" ]] && PIP_TORCH_INDEX_URL_BUILD="/${PIP_TORCH_INDEX_URL_BUILD}" |
| 153 | + PIP_TORCH_INDEX_URL="${PIP_TORCH_INDEX_URL_BASE}${PIP_TORCH_INDEX_URL_BUILD:-}/${COMPUTE_PLATFORM}" |
| 154 | + PIP_INSTALL_ARGS+=("--index-url" "$PIP_TORCH_INDEX_URL") |
| 155 | + fi |
| 156 | + fi |
| 157 | + |
| 158 | + pip_install -U --force-reinstall "${PIP_INSTALL_ARGS[@]}" "${TORCH_PACKAGES[@]}" |
| 159 | + |
| 160 | + # Fix up LD_LIBRARY_PATH for CUDA |
| 161 | + ldpretend |
| 162 | +} |
| 163 | + |
| 164 | +usage() { |
| 165 | + cat >&2 <<EOF |
| 166 | +Usage: $(basename "$0") [COMMAND] |
| 167 | + source Download Torch's source (if needed) and install the build deps |
| 168 | + release Install Torch |
| 169 | + nightly Install the Torch nightly wheel |
| 170 | + test Install the Torch test wheel |
| 171 | +EOF |
| 172 | +} |
| 173 | + |
| 174 | +## |
| 175 | +## Main |
| 176 | +## |
| 177 | +if [ $# -ne 1 ]; then |
| 178 | + usage |
| 179 | + exit 1 |
| 180 | +fi |
| 181 | + |
| 182 | +COMMAND=${1,,} |
| 183 | + |
| 184 | +case $COMMAND in |
| 185 | +source) |
| 186 | + setup_src |
| 187 | + install_build_deps |
| 188 | + install_deps |
| 189 | + ;; |
| 190 | +release) |
| 191 | + install_deps |
| 192 | + install_whl |
| 193 | + ;; |
| 194 | +nightly | test) |
| 195 | + PIP_TORCH_INDEX_URL_BUILD=$COMMAND |
| 196 | + install_deps |
| 197 | + install_whl |
| 198 | + ;; |
| 199 | +*) |
| 200 | + usage |
| 201 | + exit 1 |
| 202 | + ;; |
| 203 | +esac |
| 204 | + |
| 205 | + |
0 commit comments