install-rocm.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #!/usr/bin/env bash
  2. # Reinstall the AMD ROCm runtime stack (torch + CTranslate2 ROCm wheels) into the repo .venv.
  3. # Never creates or re-syncs the venv; run `uv sync` first, then this script.
  4. set -euo pipefail
  5. # Resolve the repo root from the script location
  6. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  7. REPO_ROOT="$(dirname "$SCRIPT_DIR")"
  8. CT2_VERSION="4.8.4"
  9. ROCM_INDEX="https://download.pytorch.org/whl/rocm7.2"
  10. help() {
  11. cat << 'EOF'
  12. ROCm runtime installer (torch + CTranslate2 ROCm wheels into .venv)
  13. Usage: scripts/install-rocm.sh [--ct2-version X.Y.Z] [--rocm-index <torch index url>]
  14. Options:
  15. --ct2-version X.Y.Z CTranslate2 release version (default: 4.8.4)
  16. --rocm-index <url> Torch ROCm index URL (default: https://download.pytorch.org/whl/rocm7.2)
  17. -h, --help Show this message
  18. Example:
  19. scripts/install-rocm.sh
  20. scripts/install-rocm.sh --ct2-version 4.8.4 --rocm-index https://download.pytorch.org/whl/rocm7.2
  21. Note: any later `uv sync` or plain `uv run` reverts these wheels, so re-run this script afterwards.
  22. EOF
  23. }
  24. main() {
  25. while [[ $# -gt 0 ]]; do
  26. case "${1:-}" in
  27. --ct2-version)
  28. CT2_VERSION="${2:-}"
  29. if [[ -z "$CT2_VERSION" ]]; then
  30. echo "Missing value for --ct2-version" >&2
  31. return 1
  32. fi
  33. shift 2
  34. ;;
  35. --rocm-index)
  36. ROCM_INDEX="${2:-}"
  37. if [[ -z "$ROCM_INDEX" ]]; then
  38. echo "Missing value for --rocm-index" >&2
  39. return 1
  40. fi
  41. shift 2
  42. ;;
  43. -h|--help|help)
  44. help
  45. return 0
  46. ;;
  47. *)
  48. echo "Unknown argument: ${1:-}" >&2
  49. echo "Use 'scripts/install-rocm.sh --help' for usage" >&2
  50. return 1
  51. ;;
  52. esac
  53. done
  54. local venv_python="$REPO_ROOT/.venv/bin/python"
  55. if [[ ! -x "$venv_python" ]]; then
  56. echo "Error: $venv_python not found." >&2
  57. echo "Run 'uv sync' first ('uv sync --extra speaker --extra triage' if you use the optional lanes), then re-run this script." >&2
  58. return 1
  59. fi
  60. export TMPDIR="${TMPDIR:-$HOME/.tmp}"
  61. mkdir -p "$TMPDIR"
  62. local venv_pip="$REPO_ROOT/.venv/bin/pip"
  63. "$venv_pip" install --force-reinstall torch torchvision torchaudio \
  64. --index-url "$ROCM_INDEX" \
  65. --extra-index-url "https://pypi.org/simple"
  66. local workdir
  67. workdir="$(mktemp -d "$TMPDIR/ct2-rocm-XXXXXX")"
  68. trap 'rm -rf "${workdir:-}"' EXIT
  69. mkdir -p "$workdir"
  70. local bundle_url="https://github.com/OpenNMT/CTranslate2/releases/download/v${CT2_VERSION}/rocm-python-wheels-Linux.zip"
  71. local bundle_zip="$workdir/rocm-python-wheels-Linux.zip"
  72. curl -fsSL -o "$bundle_zip" "$bundle_url"
  73. unzip -q -o "$bundle_zip" -d "$workdir"
  74. local py_tag
  75. py_tag="$("$venv_python" -c 'import sys; print(f"cp{sys.version_info.major}{sys.version_info.minor}")')"
  76. # The release archive layout is not stable across versions: v4.8.0 puts the
  77. # wheels at the top level, v4.8.4 nests them under temp-linux/. Search deeply.
  78. local wheel
  79. wheel="$(find "$workdir" -type f -name "ctranslate2-*-${py_tag}-*.whl" -print -quit)"
  80. if [[ -z "$wheel" ]]; then
  81. echo "Error: no CTranslate2 wheel for $py_tag found in $bundle_url" >&2
  82. return 1
  83. fi
  84. "$venv_pip" install --force-reinstall "$wheel"
  85. rm -rf "$workdir"
  86. trap - EXIT
  87. "$venv_python" -c 'import ctranslate2, torch; print(torch.__version__); print(torch.version.hip); print(ctranslate2.get_cuda_device_count()); import sys; sys.exit(1 if torch.version.hip is None or ctranslate2.get_cuda_device_count() == 0 else 0)'
  88. cat << 'EOF'
  89. ROCm stack installed. Next step: confirm the resolved device with the venv binary,
  90. not `uv run` (which syncs the venv and puts the CUDA wheels straight back):
  91. .venv/bin/stt-daemon --check # or: uv run --no-sync stt-daemon --check
  92. Expect `device=cuda` / `compute_type=float16`; `device=cpu` means a sync reverted the wheels.
  93. Note: any later `uv sync` or plain `uv run` reverts these wheels, so re-run this script afterwards.
  94. EOF
  95. }
  96. main "$@"