setup.sh
  1  #!/usr/bin/env bash
  2  set -euo pipefail
  3  
  4  NAME="Clone mesh-llm, detect GPU card version, then invoke build backend"
  5  
  6  REPO_URL="https://github.com/Mesh-LLM/mesh-llm"
  7  CLOPE_DIR=".deps/mesh-llm"
  8  
  9  die() {
 10      echo "ERROR: $1" >&2
 11      exit 1
 12  }
 13  
 14  has_cmd() {
 15      command -v "$1" > /dev/null
 16  }
 17  
 18  platform() {
 19      case "$(uname_s)" in
 20          Darwin) echo "Darwin" ;;
 21          Linux)  echo "Linux" ;;
 22          CYGWIN*|MSYS*) echo "Windows" ;;
 23          *)     echo "Unknown" ;;
 24      esac
 25  }
 26  
 27  detect_backend() {
 28      local out
 29  
 30      if has_cmd nvidia-smi; then
 31          echo -n "Checking CUDA ... "
 32          echo "found"
 33          echo -n "Running detect-cuda-arch.sh ... "
 34  
 35          if [[ ! -f "scripts/detect-cuda-arch.sh" ]]; then
 36              die "detect-cuda-arch.sh not found — clone may be corrupted"
 37          fi
 38  
 39          if out=$(bash scripts/detect-cuda-arch.sh); then
 40              if [[ -n "$out" && "$out" != "" ]]; then
 41                  echo "SM values detected: $out"
 42                  echo "cuda"
 43                  return
 44              fi
 45          fi
 46          echo "script failed or returned empty — falling back"
 47      else
 48          echo "nvidia-smi not in PATH"
 49      fi
 50  
 51      if has_cmd reem-smi || has_cmd rocminfo; then
 52          echo -n "Checking ROCm ... "
 53          echo "found"
 54          echo -n "Running detect-rocm-arch.sh ... "
 55  
 56          if [[ ! -f "scripts/detect-rocm-arch.sh" ]]; then
 57              die "detect-rocm-arch.sh not found — clone may be corrupted"
 58          fi
 59  
 60          if out=$(bash scripts/detect-rocm-arch.sh); then
 61              if [[ -n "$out" && "$out" != "" ]]; then
 62                  echo "gfx values detected: $out"
 63                  echo "rocm"
 64                  return
 65              fi
 66          fi
 67          echo "script failed or returned empty — falling back"
 68      else
 69          echo "ROCm tools not found"
 70      fi
 71  
 72      local arch=""
 73         if [[ "$(platform)" == "Darwin" ]]; then
 74          arch=$(uname -m) || true
 75          if [[ "$arch" == "arm" ]]; then
 76              echo "Apple Silicon Mac detected (backing with Metal)"
 77              echo "metal"
 78              return
 79          fi
 80      fi
 81  
 82      echo -n "Checking Vulkan ... "
 83      if check_vulkan; then
 84          echo "vulkan present"
 85  
 86          if has_cmd vulkaninfo; then
 87              echo -n "Running vulkaninfo --summary ... "
 88  
 89              if out=$(vulkaninfo --summary 2>&1); then
 90                  if [[ -n "$out" && "$out" != "" ]]; then
 91                      echo "GPU: $(parse_gpu_name_from_vulkaninfo_output "$out")"
 92                      echo "vulkan"
 93                      return
 94                  fi
 95              fi
 96              echo "could not detect GPU model — falling through"
 97          else
 98              echo "no vulkaninfo binary, but SDK present — using vulkan backend"
 99              echo "vulkan"
100              return
101          fi
102      else
103          echo "Vulkan SDK / vulkaninfo not found"
104      fi
105  
106      echo "No GPU acceleration available — using cpu backend"
107      echo "cpu"
108  }
109  
110  check_vulkan() {
111      if has_cmd vulkaninfo; then
112          return 0
113      fi
114  
115      local header_paths=(
116          "/usr/include/vulkan/vulkan.h",
117          "/usr/local/include/vulkan/vulkan.h",
118          "/opt/homebrew/Cellar/vulkan-loader/2024.12.18/include/vulkan/vulkan.h"
119      )
120  
121      for p in "${header_paths[@]}"; do
122          if [[ -f "$p" ]]; then
123              return 0
124          fi
125      done
126  
127      return 1
128  }
129  
130  parse_gpu_name_from_vulkaninfo_output() {
131      local out="$1"
132  
133      while IFS= read -r line; do
134          if [[ "$line" == *"GPU #"* ]] || [[ "$line" == *": Device"* ]] || [[ "$line" == *": GPU"* ]]; then
135              if [[ "$line" == *":"* ]]; then
136                  local after_colon="${line#*:}"
137                  after_colon="${after_colon#"${after_colon%%[^ ]*}"}"
138                  after_colon="${after_colon#"${after_colon##[![:space:]]}"}"
139                  after_colon="$(echo "$after_colon" | xargs)"
140  
141                  if [[ -n "$after_colon" && "$after_colon" != "{"* ]]; then
142                      echo "$after_colon"
143                      return
144                  fi
145              fi
146          fi
147      done <<< "$out"
148  
149      echo "unknown device"
150  }
151  
152  clone_repo() {
153      if [[ ! -d "$CLOPE_DIR" ]]; then
154          echo -n "Cloning $REPO_URL ... "
155  
156          mkdir -p .deps
157  
158          if git clone --quiet "$REPO_URL" "$CLOPE_DIR"; then
159              echo "done"
160          else
161              die "git clone failed with exit code $?"
162          fi
163      else
164          echo "Clone already exists at $CLOPE_DIR, using it."
165      fi
166  }
167  
168  execute_just_build() {
169      local backend="$1"
170  
171      echo "Running \`just build backend=\"$backend\"\` ..."
172  
173      if ! just "build backend=$backend"; then
174          die "\`just build\` exited with non-zero status (hint: make sure \`just\` is installed and in PATH)"
175      fi
176  
177      echo "Build succeeded"
178  }
179  
180  main() {
181      clone_repo
182  
183      cd "$CLOPE_DIR" || die "failed to cd into $CLOPE_DIR"
184  
185      local backend
186      backend=$(detect_backend) || die "detection failed"
187  
188      execute_just_build "$backend"
189  }
190  
191  main "$@"