|
| 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 | +HELION_DIR=${WORKSPACE}/helion |
| 25 | +HELION_REPO=https://github.com/pytorch/helion.git |
| 26 | + |
| 27 | +declare -a PIP_INSTALL_ARGS |
| 28 | + |
| 29 | +# Extract the major.minor version from ROCM_VERSION, e.g. 6.4 from 6.4.4 |
| 30 | +get_rocm_version() { |
| 31 | + [[ "$ROCM_VERSION" =~ ^([0-9]+\.[0-9]+) ]] && echo "${BASH_REMATCH[1]}" || |
| 32 | + echo "$ROCM_VERSION" |
| 33 | +} |
| 34 | + |
| 35 | +pip_install() { |
| 36 | + if command -v uv &>/dev/null; then |
| 37 | + uv pip install "$@" |
| 38 | + else |
| 39 | + pip install "$@" |
| 40 | + fi |
| 41 | +} |
| 42 | + |
| 43 | +setup_src() { |
| 44 | + echo "Downloading Helion source code and setting up the environment for building from source..." |
| 45 | + |
| 46 | + if [ ! -d "$HELION_DIR" ]; then |
| 47 | + echo "Cloning the Helion repo $HELION_REPO to $HELION_DIR ..." |
| 48 | + git clone "$HELION_REPO" "$HELION_DIR" |
| 49 | + if [ ! -d "$HELION_DIR" ]; then |
| 50 | + echo "$HELION_DIR not found. ERROR Cloning repository..." |
| 51 | + exit 1 |
| 52 | + else |
| 53 | + pushd "$HELION_DIR" 1>/dev/null || exit 1 |
| 54 | + git submodule sync |
| 55 | + git submodule update --init --recursive |
| 56 | + |
| 57 | + if [ -n "${HELION_GITREF:-}" ]; then |
| 58 | + git checkout "$HELION_GITREF" |
| 59 | + fi |
| 60 | + |
| 61 | + echo "Installing pre-commit hooks into your local Helion git repo (one-time)" |
| 62 | + pip_install pre-commit |
| 63 | + pre-commit install |
| 64 | + popd 1>/dev/null |
| 65 | + fi |
| 66 | + else |
| 67 | + echo "Torch repo already present, not cloning ..." |
| 68 | + fi |
| 69 | +} |
| 70 | + |
| 71 | +install_deps() { |
| 72 | + echo "Installing Helion dependencies ..." |
| 73 | + pip_install numpy |
| 74 | + |
| 75 | + "${WORKSPACE}"/setup_torch.sh release |
| 76 | +} |
| 77 | + |
| 78 | +install_release() { |
| 79 | + echo "Installing Helion from PyPI ..." |
| 80 | + |
| 81 | + if command -v uv &>/dev/null; then |
| 82 | + if [ -n "${UV_TORCH_BACKEND:-}" ]; then |
| 83 | + echo "Using the specified uv backend, $UV_TORCH_BACKEND" |
| 84 | + elif [ -n "${ROCM_VERSION:-}" ]; then |
| 85 | + echo "Using the torch ROCm version $ROCM_VERSION backend" |
| 86 | + UV_TORCH_BACKEND="rocm$(get_rocm_version)" |
| 87 | + elif ((${TRITON_CPU_BACKEND:-0} == 1)); then |
| 88 | + echo "Using the torch CPU backend" |
| 89 | + UV_TORCH_BACKEND=cpu |
| 90 | + elif [ -n "${CUDA_VERSION:-}" ]; then |
| 91 | + echo "Using the torch CUDA version $CUDA_VERSION backend" |
| 92 | + UV_TORCH_BACKEND="cu${CUDA_VERSION/[.-]/}" |
| 93 | + else |
| 94 | + echo "Using the torch auto backend" |
| 95 | + UV_TORCH_BACKEND=auto |
| 96 | + fi |
| 97 | + |
| 98 | + PIP_INSTALL_ARGS+=("--torch-backend" "$UV_TORCH_BACKEND") |
| 99 | + elif ! command -v uv &>/dev/null && [ -n "${UV_TORCH_BACKEND:-}" ]; then |
| 100 | + echo "Error: UV_TORCH_BACKEND is set to $UV_TORCH_BACKEND but uv is not available." |
| 101 | + exit 1 |
| 102 | + fi |
| 103 | + |
| 104 | + if [ -n "${PIP_HELION_INDEX_URL:-}" ]; then |
| 105 | + echo "Using the specified index, $PIP_HELION_INDEX_URL" |
| 106 | + PIP_INSTALL_ARGS+=("--index-url $PIP_HELION_INDEX_URL") |
| 107 | + fi |
| 108 | + |
| 109 | + if [ -n "${PIP_HELION_VERSION:-}" ]; then |
| 110 | + echo "Installing the specified Helion version $PIP_HELION_VERSION" |
| 111 | + PIP_HELION_VERSION="==$PIP_HELION_VERSION" |
| 112 | + fi |
| 113 | + |
| 114 | + pip_install "${PIP_INSTALL_ARGS[@]}" "helion${PIP_HELION_VERSION:-}" |
| 115 | + |
| 116 | + # Fix up LD_LIBRARY_PATH for CUDA |
| 117 | + "${WORKSPACE}"/ldpretend.sh |
| 118 | +} |
| 119 | + |
| 120 | +usage() { |
| 121 | + cat >&2 <<EOF |
| 122 | +Usage: $(basename "$0") [COMMAND] |
| 123 | + source Download Helion's source (if needed) and install the build deps |
| 124 | + release Install Helion |
| 125 | +EOF |
| 126 | +} |
| 127 | + |
| 128 | +## |
| 129 | +## Main |
| 130 | +## |
| 131 | + |
| 132 | +if [ $# -ne 1 ]; then |
| 133 | + usage |
| 134 | + exit 1 |
| 135 | +fi |
| 136 | + |
| 137 | +COMMAND=${1,,} |
| 138 | + |
| 139 | +case $COMMAND in |
| 140 | +source) |
| 141 | + setup_src |
| 142 | + install_deps |
| 143 | + ;; |
| 144 | +release) |
| 145 | + install_deps |
| 146 | + install_release |
| 147 | + ;; |
| 148 | +*) |
| 149 | + usage |
| 150 | + exit 1 |
| 151 | + ;; |
| 152 | +esac |
0 commit comments