File size: 2,842 Bytes
8914961
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env bash
# Check sam3edge-mini (and, optionally, sam3edge-micro and another publisher's SAM 3) on this machine.
#
#   bash gpu_validate.sh cuda        an NVIDIA GPU (Ubuntu, driver 525 or newer)
#   bash gpu_validate.sh cpu         the CPU only
#
# Anything after the first word is passed to validate.py, for example:
#   bash gpu_validate.sh cuda --micro fast-672,quality-336 --other tepirale/sam3-ONNX
#
# It makes a Python environment in ~/sam3edge-validate, installs ONNX Runtime with the CUDA libraries it
# needs (no system CUDA install is used or changed), downloads the public files and writes the results to
# ~/sam3edge-validate/kit/results/.  Nothing outside that folder is touched.
set -euo pipefail

PROVIDER="${1:-cuda}"
shift || true
DIR="${SAM3EDGE_DIR:-$HOME/sam3edge-validate}"
case "$PROVIDER" in cpu|cuda) ;; *) echo "first argument must be cuda or cpu"; exit 2 ;; esac

command -v python3 >/dev/null || { echo "python3 is missing: sudo apt install python3 python3-venv"; exit 1; }
MINOR=$(python3 -c 'import sys; print(sys.version_info.minor)')
MAJOR=$(python3 -c 'import sys; print(sys.version_info.major)')
if [ "$MAJOR" -ne 3 ] || [ "$MINOR" -lt 10 ]; then
  echo "Python 3.10 or newer is needed; this is $(python3 --version)"; exit 1
fi

if [ "$PROVIDER" = "cuda" ]; then
  command -v nvidia-smi >/dev/null || { echo "nvidia-smi is missing: install the NVIDIA driver first"; exit 1; }
  nvidia-smi --query-gpu=name,driver_version,memory.total --format=csv,noheader
  DRIVER=$(nvidia-smi --query-gpu=driver_version --format=csv,noheader | head -1 | cut -d. -f1)
  if [ "$DRIVER" -lt 525 ]; then
    echo "NVIDIA driver $DRIVER is older than 525, which the CUDA 12 libraries need. Update the driver."; exit 1
  fi
  # ONNX Runtime builds that use CUDA 12 (any driver from 525 on): 1.23 still supports Python 3.10
  if [ "$MINOR" -ge 11 ]; then ORT='onnxruntime-gpu[cuda,cudnn]==1.26.0'; else ORT='onnxruntime-gpu[cuda,cudnn]==1.23.2'; fi
else
  ORT='onnxruntime'
fi

mkdir -p "$DIR"
if [ ! -x "$DIR/venv/bin/python" ]; then
  python3 -m venv "$DIR/venv" || { echo "could not make a virtual environment: sudo apt install python3-venv"; exit 1; }
fi
PY="$DIR/venv/bin/python"
"$PY" -m pip install --quiet --upgrade pip
"$PY" -m pip install --quiet "$ORT" numpy pillow huggingface_hub tokenizers

"$PY" - "$DIR/kit" <<'PYCODE'
import sys
from huggingface_hub import snapshot_download
snapshot_download("eoinedge/sam3edge-mini", allow_patterns=["sam3edge_mini/*", "validate/*"], local_dir=sys.argv[1])
PYCODE

if [ "$PROVIDER" = "cuda" ]; then
  "$PY" - <<'PYCODE'
import onnxruntime as ort
if hasattr(ort, "preload_dlls"):
    ort.preload_dlls()
print("onnxruntime", ort.__version__, "providers:", ort.get_available_providers())
PYCODE
fi

exec "$PY" "$DIR/kit/validate/validate.py" --provider "$PROVIDER" "$@"