|
| 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 | +SUDO='' |
| 28 | +if ((EUID != 0)) && command -v sudo &>/dev/null; then |
| 29 | + SUDO="sudo" |
| 30 | +fi |
| 31 | + |
| 32 | +pip_install() { |
| 33 | + if command -v uv &>/dev/null; then |
| 34 | + uv pip install "$@" |
| 35 | + else |
| 36 | + pip install "$@" |
| 37 | + fi |
| 38 | +} |
| 39 | + |
| 40 | +# Remove the dashes or periods from the CUDA version, e.g. 128 from 12-8 |
| 41 | +get_cuda_version() { |
| 42 | + echo "${CUDA_VERSION//[.-]/}" |
| 43 | +} |
| 44 | + |
| 45 | +# Extract the major.minor version from ROCM_VERSION, e.g. 6.4 from 6.4.4 |
| 46 | +get_rocm_version() { |
| 47 | + [[ "$ROCM_VERSION" =~ ^([0-9]+\.[0-9]+) ]] && echo "${BASH_REMATCH[1]}" || |
| 48 | + echo "$ROCM_VERSION" |
| 49 | +} |
| 50 | + |
| 51 | +setup_src() { |
| 52 | + echo "Downloading Torch source code and setting up the environment for building from source..." |
| 53 | + |
| 54 | + if [ ! -d "$TORCH_DIR" ]; then |
| 55 | + echo "Cloning the Torch repo $TORCH_REPO to $TORCH_DIR ..." |
| 56 | + git clone "$TORCH_REPO" "$TORCH_DIR" |
| 57 | + if [ ! -d "$TORCH_DIR" ]; then |
| 58 | + echo "$TORCH_DIR not found. ERROR Cloning repository..." |
| 59 | + exit 1 |
| 60 | + else |
| 61 | + pushd "$TORCH_DIR" 1>/dev/null || exit 1 |
| 62 | + |
| 63 | + if [ -n "${TORCH_GITREF:-}" ]; then |
| 64 | + git checkout "$TORCH_GITREF" |
| 65 | + fi |
| 66 | + |
| 67 | + git submodule sync |
| 68 | + git submodule update --init --recursive |
| 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 | + fi |
| 89 | + |
| 90 | + if ((EUID == 0)) || [ -n "${SUDO:-}" ]; then |
| 91 | + $SUDO dnf -y install numactl-devel |
| 92 | + else |
| 93 | + echo "ERROR: Can't install some build deps without root or sudo permissions." >&2 |
| 94 | + exit 1 |
| 95 | + fi |
| 96 | + |
| 97 | + if [ -n "${ROCM_VERSION:-}" ]; then |
| 98 | + python tools/amd_build/build_amd.py |
| 99 | + fi |
| 100 | + |
| 101 | + popd 1>/dev/null |
| 102 | +} |
| 103 | + |
| 104 | +install_deps() { |
| 105 | + echo "Installing Torch dependencies ..." |
| 106 | + pip_install numpy |
| 107 | +} |
| 108 | + |
| 109 | +install_whl() { |
| 110 | + local pip_build="$1" |
| 111 | + |
| 112 | + local compute_platform |
| 113 | + local pip_torch_index_url_base |
| 114 | + local -a pip_install_args |
| 115 | + |
| 116 | + pip_torch_index_url_base="https://download.pytorch.org/whl" |
| 117 | + |
| 118 | + case "$pip_build" in |
| 119 | + release) ;; |
| 120 | + nightly | test) |
| 121 | + pip_torch_index_url_base="${pip_torch_index_url_base}/${pip_build}" |
| 122 | + ;; |
| 123 | + esac |
| 124 | + |
| 125 | + echo "Installing Torch $pip_build from PyPI ..." |
| 126 | + |
| 127 | + if [ -n "${PIP_TORCH_VERSION:-}" ]; then |
| 128 | + echo "Using the specified version $PIP_TORCH_VERSION of torch" |
| 129 | + PIP_TORCH_VERSION="==$PIP_TORCH_VERSION" |
| 130 | + fi |
| 131 | + |
| 132 | + if [ -n "${PIP_TORCHVISION_VERSION:-}" ]; then |
| 133 | + echo "Installing the specified version $PIP_TORCHVISION_VERSION of torchvision" |
| 134 | + PIP_TORCHVISION_VERSION="==$PIP_TORCHVISION_VERSION" |
| 135 | + fi |
| 136 | + |
| 137 | + if [ -n "${PIP_TORCHAUDIO_VERSION:-}" ]; then |
| 138 | + echo "Installing the specified version $PIP_TORCHAUDIO_VERSION of torchaudio" |
| 139 | + PIP_TORCHAUDIO_VERSION="==$PIP_TORCHAUDIO_VERSION" |
| 140 | + fi |
| 141 | + |
| 142 | + declare -a TORCH_PACKAGES=( |
| 143 | + "torch${PIP_TORCH_VERSION:-}" |
| 144 | + "torchvision${PIP_TORCHVISION_VERSION:-}" |
| 145 | + "torchaudio${PIP_TORCHAUDIO_VERSION:-}" |
| 146 | + ) |
| 147 | + |
| 148 | + if [ -n "${PIP_TORCH_INDEX_URL:-}" ]; then |
| 149 | + echo "Using the specified index, $PIP_TORCH_INDEX_URL" |
| 150 | + pip_install_args+=("--index-url" "$PIP_TORCH_INDEX_URL") |
| 151 | + elif command -v uv &>/dev/null && [ -n "${UV_TORCH_BACKEND:-}" ]; then |
| 152 | + echo "Using the specified uv backend, $UV_TORCH_BACKEND" |
| 153 | + pip_install_args+=("--torch-backend" "$UV_TORCH_BACKEND") |
| 154 | + elif ! command -v uv &>/dev/null && [ -n "${UV_TORCH_BACKEND:-}" ]; then |
| 155 | + echo "Error: UV_TORCH_BACKEND is set to $UV_TORCH_BACKEND but uv is not available." |
| 156 | + exit 1 |
| 157 | + else |
| 158 | + # Set compute platform for torch wheel installation |
| 159 | + if [ -n "${ROCM_VERSION:-}" ]; then |
| 160 | + echo "Using the ROCm version $ROCM_VERSION backend" |
| 161 | + compute_platform="rocm$(get_rocm_version)" |
| 162 | + elif ((${TRITON_CPU_BACKEND:-0} == 1)); then |
| 163 | + echo "Using the CPU backend" |
| 164 | + compute_platform="cpu" |
| 165 | + elif [ -n "${CUDA_VERSION:-}" ]; then |
| 166 | + echo "Using the CUDA version $CUDA_VERSION backend" |
| 167 | + compute_platform="cu$(get_cuda_version)" |
| 168 | + fi |
| 169 | + |
| 170 | + if [ -n "${compute_platform:-}" ]; then |
| 171 | + PIP_TORCH_INDEX_URL="${pip_torch_index_url_base}/${compute_platform}" |
| 172 | + pip_install_args+=("--index-url" "$PIP_TORCH_INDEX_URL") |
| 173 | + else |
| 174 | + PIP_TORCH_INDEX_URL="${pip_torch_index_url_base}" |
| 175 | + pip_install_args+=("--index-url" "$PIP_TORCH_INDEX_URL") |
| 176 | + fi |
| 177 | + fi |
| 178 | + |
| 179 | + pip_install -U --force-reinstall "${pip_install_args[@]}" "${TORCH_PACKAGES[@]}" |
| 180 | + |
| 181 | + # Fix up LD_LIBRARY_PATH for CUDA |
| 182 | + ldpretend |
| 183 | +} |
| 184 | + |
| 185 | +usage() { |
| 186 | + cat >&2 <<EOF |
| 187 | +Usage: $(basename "$0") [COMMAND] |
| 188 | + source Download Torch's source (if needed) and install the build deps |
| 189 | + release Install Torch |
| 190 | + nightly Install the Torch nightly wheel |
| 191 | + test Install the Torch test wheel |
| 192 | +EOF |
| 193 | +} |
| 194 | + |
| 195 | +## |
| 196 | +## Main |
| 197 | +## |
| 198 | +if [ $# -ne 1 ]; then |
| 199 | + usage |
| 200 | + exit 1 |
| 201 | +fi |
| 202 | + |
| 203 | +COMMAND=${1,,} |
| 204 | + |
| 205 | +case $COMMAND in |
| 206 | +source) |
| 207 | + setup_src |
| 208 | + install_build_deps |
| 209 | + install_deps |
| 210 | + ;; |
| 211 | +nightly | release | test) |
| 212 | + install_deps |
| 213 | + install_whl "$COMMAND" |
| 214 | + ;; |
| 215 | +*) |
| 216 | + usage |
| 217 | + exit 1 |
| 218 | + ;; |
| 219 | +esac |
0 commit comments