|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Install Triton from source |
| 4 | +# This script clones and installs Triton from the main repository |
| 5 | + |
| 6 | +set -e |
| 7 | + |
| 8 | +echo "🚀 Installing Triton from source..." |
| 9 | +START_TIME=$(date +%s) |
| 10 | + |
| 11 | +# Function to show elapsed time |
| 12 | +show_elapsed() { |
| 13 | + CURRENT_TIME=$(date +%s) |
| 14 | + ELAPSED=$((CURRENT_TIME - START_TIME)) |
| 15 | + echo "⏱️ Elapsed time: ${ELAPSED}s" |
| 16 | +} |
| 17 | + |
| 18 | +# Pre-flight checks |
| 19 | +echo "🔍 Running pre-flight checks..." |
| 20 | + |
| 21 | +# Set Triton version/commit for cache consistency |
| 22 | +TRITON_COMMIT=${TRITON_COMMIT:-"main"} |
| 23 | +echo "🎯 Target Triton commit/branch: $TRITON_COMMIT" |
| 24 | +TRITON_CACHE_DIR="/tmp/triton-cache" |
| 25 | +TRITON_SOURCE_DIR="/tmp/triton" |
| 26 | + |
| 27 | +# Check disk space (need at least 10GB for Triton compilation) |
| 28 | +AVAILABLE_SPACE=$(df /tmp | tail -1 | awk '{print $4}') |
| 29 | +REQUIRED_SPACE=10485760 # 10GB in KB |
| 30 | +if [ "$AVAILABLE_SPACE" -lt "$REQUIRED_SPACE" ]; then |
| 31 | + echo "⚠️ WARNING: Low disk space. Available: $(($AVAILABLE_SPACE / 1024 / 1024))GB, Recommended: 10GB" |
| 32 | +else |
| 33 | + echo "✅ Sufficient disk space available: $(($AVAILABLE_SPACE / 1024 / 1024))GB" |
| 34 | +fi |
| 35 | + |
| 36 | +# Ensure we're in the conda environment |
| 37 | +if [ -z "$CONDA_ENV" ]; then |
| 38 | + echo "ERROR: CONDA_ENV is not set" |
| 39 | + exit 1 |
| 40 | +fi |
| 41 | + |
| 42 | +# Activate conda environment |
| 43 | +source /opt/miniconda3/etc/profile.d/conda.sh |
| 44 | +conda activate "$CONDA_ENV" |
| 45 | + |
| 46 | +# Create cache directory |
| 47 | +mkdir -p "$TRITON_CACHE_DIR" |
| 48 | + |
| 49 | +# Check if we have cached source with correct commit |
| 50 | +if [ -f "$TRITON_CACHE_DIR/commit" ] && [ -d "$TRITON_SOURCE_DIR" ]; then |
| 51 | + CACHED_COMMIT=$(cat "$TRITON_CACHE_DIR/commit") |
| 52 | + if [ "$CACHED_COMMIT" = "$TRITON_COMMIT" ] && [ "$TRITON_COMMIT" != "main" ]; then |
| 53 | + echo "Found cached Triton source with correct commit ($CACHED_COMMIT)" |
| 54 | + echo "Will use cached source and re-install to new conda environment" |
| 55 | + USE_CACHED_SOURCE=true |
| 56 | + elif [ "$TRITON_COMMIT" = "main" ]; then |
| 57 | + echo "Target is 'main' branch (API fallback), will rebuild from scratch" |
| 58 | + echo "Cached commit: $CACHED_COMMIT" |
| 59 | + USE_CACHED_SOURCE=false |
| 60 | + else |
| 61 | + echo "Cached source commit mismatch: cached=$CACHED_COMMIT, target=$TRITON_COMMIT" |
| 62 | + echo "Will rebuild from scratch" |
| 63 | + USE_CACHED_SOURCE=false |
| 64 | + fi |
| 65 | +else |
| 66 | + echo "No cached source found or no commit info, will build from scratch" |
| 67 | + USE_CACHED_SOURCE=false |
| 68 | +fi |
| 69 | + |
| 70 | +# Update libstdc++ to match system version |
| 71 | +# Otherwise, we get errors like: |
| 72 | +# ImportError: /opt/miniconda3/envs/tritonparse/bin/../lib/libstdc++.so.6: |
| 73 | +# version `GLIBCXX_3.4.30' not found (required by /tmp/triton/python/triton/_C/libtriton.so) |
| 74 | +echo "Updating libstdc++ to match system version..." |
| 75 | +conda install -y -c conda-forge libstdcxx-ng=12.3.0 |
| 76 | +# Check if the update was successful |
| 77 | +strings /opt/miniconda3/envs/tritonparse/lib/libstdc++.so.6 | grep GLIBCXX | tail -5 |
| 78 | + |
| 79 | +# Uninstall existing pytorch-triton |
| 80 | +echo "Uninstalling existing pytorch-triton..." |
| 81 | +pip uninstall -y pytorch-triton || true |
| 82 | +pip uninstall -y triton || true |
| 83 | + |
| 84 | +# Setup Triton repository based on cache status |
| 85 | +if [ "$USE_CACHED_SOURCE" = "true" ]; then |
| 86 | + echo "Using cached Triton source..." |
| 87 | + cd "$TRITON_SOURCE_DIR" |
| 88 | + ACTUAL_COMMIT=$(git rev-parse HEAD) |
| 89 | + echo "Using cached Triton commit: $ACTUAL_COMMIT" |
| 90 | +else |
| 91 | + echo "Setting up Triton repository from scratch..." |
| 92 | + if [ -d "$TRITON_SOURCE_DIR" ]; then |
| 93 | + echo "Removing existing source directory..." |
| 94 | + rm -rf "$TRITON_SOURCE_DIR" |
| 95 | + fi |
| 96 | + |
| 97 | + echo "Cloning Triton repository..." |
| 98 | + if ! git clone https://github.com/triton-lang/triton.git "$TRITON_SOURCE_DIR"; then |
| 99 | + echo "❌ ERROR: Failed to clone Triton repository" |
| 100 | + echo "This might be due to network issues or GitHub rate limiting" |
| 101 | + exit 1 |
| 102 | + fi |
| 103 | + |
| 104 | + cd "$TRITON_SOURCE_DIR" |
| 105 | + |
| 106 | + # Checkout specific commit for reproducibility |
| 107 | + echo "Checking out commit: $TRITON_COMMIT" |
| 108 | + if ! git checkout "$TRITON_COMMIT"; then |
| 109 | + echo "❌ ERROR: Failed to checkout commit $TRITON_COMMIT" |
| 110 | + echo "This might be due to an invalid commit hash or network issues" |
| 111 | + exit 1 |
| 112 | + fi |
| 113 | + |
| 114 | + ACTUAL_COMMIT=$(git rev-parse HEAD) |
| 115 | + echo "✅ Using Triton commit: $ACTUAL_COMMIT" |
| 116 | +fi |
| 117 | + |
| 118 | +# Install build dependencies |
| 119 | +echo "Installing build dependencies..." |
| 120 | +pip install ninja cmake wheel pybind11 |
| 121 | + |
| 122 | +# Install Triton requirements |
| 123 | +echo "Installing Triton requirements..." |
| 124 | +pip install -r python/requirements.txt |
| 125 | + |
| 126 | +# Set environment to use clang compiler for faster compilation |
| 127 | +echo "Setting up clang compiler for faster compilation..." |
| 128 | +export CC=clang |
| 129 | +export CXX=clang++ |
| 130 | +echo "Using CC: $CC" |
| 131 | +echo "Using CXX: $CXX" |
| 132 | + |
| 133 | +# Install Triton in editable mode with clang |
| 134 | +if [ "$USE_CACHED_SOURCE" = "true" ]; then |
| 135 | + echo "Installing cached Triton to new conda environment..." |
| 136 | + echo "This should be fast since build artifacts are cached" |
| 137 | +else |
| 138 | + echo "Compiling and installing Triton from scratch..." |
| 139 | + echo "This will take 30-50 minutes for compilation" |
| 140 | +fi |
| 141 | +pip install -e . |
| 142 | +show_elapsed |
| 143 | + |
| 144 | +# Verify Triton installation |
| 145 | +echo "Verifying Triton installation..." |
| 146 | +if python -c "import triton; print(f'Triton version: {triton.__version__}')" 2>/dev/null; then |
| 147 | + python -c "import triton; print(f'Triton path: {triton.__file__}')" |
| 148 | + echo "✅ Triton installation verified successfully" |
| 149 | + |
| 150 | + # Only save commit info after successful verification |
| 151 | + echo "$ACTUAL_COMMIT" >"$TRITON_CACHE_DIR/commit" |
| 152 | + echo "✅ Cache information saved" |
| 153 | + |
| 154 | + show_elapsed |
| 155 | + echo "🎉 Triton installation completed successfully!" |
| 156 | +else |
| 157 | + echo "❌ ERROR: Failed to import triton" |
| 158 | + echo "This might be due to libstdc++ version issues" |
| 159 | + echo "Checking system libstdc++ version:" |
| 160 | + strings /usr/lib/x86_64-linux-gnu/libstdc++.so.6 | grep GLIBCXX | tail -5 || echo "Could not check system libstdc++" |
| 161 | + echo "Checking conda libstdc++ version:" |
| 162 | + strings /opt/miniconda3/envs/tritonparse/lib/libstdc++.so.6 | grep GLIBCXX | tail -5 || echo "Could not check conda libstdc++" |
| 163 | + |
| 164 | + # Clean up cache on failure to prevent corruption |
| 165 | + echo "🧹 Cleaning up cache due to installation failure..." |
| 166 | + rm -f "$TRITON_CACHE_DIR/commit" |
| 167 | + |
| 168 | + exit 1 |
| 169 | +fi |
0 commit comments