|
| 1 | +#!/bin/bash |
| 2 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | +# All rights reserved. |
| 4 | +# |
| 5 | +# This source code is licensed under the BSD-style license found in the |
| 6 | +# LICENSE file in the root directory of this source tree. |
| 7 | + |
| 8 | +# Install mingw-w64 cross-compiler and Windows CUDA toolkit for cross-compilation |
| 9 | + |
| 10 | +set -ex |
| 11 | + |
| 12 | +INSTALL_DIR="${WINDOWS_CUDA_INSTALL_DIR:-/opt/cuda-windows}" |
| 13 | + |
| 14 | +# Mapping of CUDA versions to their corresponding driver versions for Windows installers |
| 15 | +# Source: https://developer.nvidia.com/cuda-toolkit-archive |
| 16 | +declare -A CUDA_DRIVER_MAP=( |
| 17 | + ["12.6"]="12.6.3:561.17" |
| 18 | + ["12.8"]="12.8.1:572.61" |
| 19 | + ["12.9"]="12.9.1:576.57" |
| 20 | +) |
| 21 | + |
| 22 | +install_mingw() { |
| 23 | + echo "Installing mingw-w64 cross-compiler..." |
| 24 | + |
| 25 | + apt-get update |
| 26 | + # Install the POSIX threads version of mingw-w64 which supports C++11 threading |
| 27 | + # primitives (std::mutex, std::condition_variable, std::shared_mutex). |
| 28 | + # The default win32 threads version does not support these. |
| 29 | + apt-get install -y --no-install-recommends \ |
| 30 | + g++-mingw-w64-x86-64-posix \ |
| 31 | + mingw-w64-tools \ |
| 32 | + p7zip-full \ |
| 33 | + wget |
| 34 | + |
| 35 | + # Verify installation shows POSIX threads |
| 36 | + x86_64-w64-mingw32-g++ --version |
| 37 | + |
| 38 | + # Cleanup |
| 39 | + apt-get clean |
| 40 | + rm -rf /var/lib/apt/lists/* |
| 41 | + |
| 42 | + echo "mingw-w64 installation complete (POSIX threads version)" |
| 43 | +} |
| 44 | + |
| 45 | +get_torch_cuda_version() { |
| 46 | + # Query PyTorch for its CUDA version using conda environment |
| 47 | + conda run -n "py_${PYTHON_VERSION}" python3 -c "import torch; print(torch.version.cuda)" 2>/dev/null || echo "" |
| 48 | +} |
| 49 | + |
| 50 | +install_windows_cuda() { |
| 51 | + # Get CUDA version from torch |
| 52 | + TORCH_CUDA_VERSION=$(get_torch_cuda_version) |
| 53 | + |
| 54 | + if [ -z "${TORCH_CUDA_VERSION}" ] || [ "${TORCH_CUDA_VERSION}" = "None" ]; then |
| 55 | + echo "ERROR: Could not detect CUDA version from PyTorch." |
| 56 | + echo "Make sure PyTorch with CUDA support is installed before running this script." |
| 57 | + exit 1 |
| 58 | + fi |
| 59 | + |
| 60 | + echo "Detected PyTorch CUDA version: ${TORCH_CUDA_VERSION}" |
| 61 | + |
| 62 | + # Extract major.minor version (e.g., "12.8" from "12.8.1" or "12.8") |
| 63 | + CUDA_MAJOR_MINOR=$(echo "${TORCH_CUDA_VERSION}" | cut -d. -f1,2) |
| 64 | + |
| 65 | + # Look up the full version and driver version |
| 66 | + if [ -z "${CUDA_DRIVER_MAP[${CUDA_MAJOR_MINOR}]}" ]; then |
| 67 | + echo "ERROR: CUDA version ${CUDA_MAJOR_MINOR} is not in the known version map." |
| 68 | + echo "Known versions: ${!CUDA_DRIVER_MAP[*]}" |
| 69 | + exit 1 |
| 70 | + fi |
| 71 | + |
| 72 | + CUDA_INFO="${CUDA_DRIVER_MAP[${CUDA_MAJOR_MINOR}]}" |
| 73 | + CUDA_VERSION=$(echo "${CUDA_INFO}" | cut -d: -f1) |
| 74 | + CUDA_DRIVER_VERSION=$(echo "${CUDA_INFO}" | cut -d: -f2) |
| 75 | + |
| 76 | + echo "Using CUDA ${CUDA_VERSION} with driver ${CUDA_DRIVER_VERSION}" |
| 77 | + |
| 78 | + echo "Installing Windows CUDA toolkit ${CUDA_VERSION}..." |
| 79 | + |
| 80 | + mkdir -p "${INSTALL_DIR}" |
| 81 | + cd "${INSTALL_DIR}" |
| 82 | + |
| 83 | + CUDA_INSTALLER="cuda_${CUDA_VERSION}_${CUDA_DRIVER_VERSION}_windows.exe" |
| 84 | + CUDA_URL="https://developer.download.nvidia.com/compute/cuda/${CUDA_VERSION}/local_installers/${CUDA_INSTALLER}" |
| 85 | + |
| 86 | + # Check if already downloaded and extracted |
| 87 | + if [ -d "${INSTALL_DIR}/extracted/cuda_cudart" ]; then |
| 88 | + echo "Windows CUDA toolkit already installed, skipping download..." |
| 89 | + return 0 |
| 90 | + fi |
| 91 | + |
| 92 | + echo "Downloading CUDA installer from ${CUDA_URL}..." |
| 93 | + wget -q "${CUDA_URL}" -O "${CUDA_INSTALLER}" |
| 94 | + |
| 95 | + echo "Extracting CUDA toolkit..." |
| 96 | + 7z x "${CUDA_INSTALLER}" -o"extracted" -y |
| 97 | + |
| 98 | + # Fix permissions so ci-user can access the files |
| 99 | + chmod -R a+rX "${INSTALL_DIR}" |
| 100 | + |
| 101 | + # Clean up installer to save space |
| 102 | + rm -f "${CUDA_INSTALLER}" |
| 103 | + |
| 104 | + echo "Windows CUDA toolkit installation complete" |
| 105 | + echo "WINDOWS_CUDA_HOME=${INSTALL_DIR}/extracted/cuda_cudart/cudart" |
| 106 | +} |
| 107 | + |
| 108 | +# Parse command line arguments |
| 109 | +INSTALL_MINGW=false |
| 110 | +INSTALL_CUDA=false |
| 111 | + |
| 112 | +while [[ $# -gt 0 ]]; do |
| 113 | + case $1 in |
| 114 | + --mingw) |
| 115 | + INSTALL_MINGW=true |
| 116 | + shift |
| 117 | + ;; |
| 118 | + --cuda) |
| 119 | + INSTALL_CUDA=true |
| 120 | + shift |
| 121 | + ;; |
| 122 | + --all) |
| 123 | + INSTALL_MINGW=true |
| 124 | + INSTALL_CUDA=true |
| 125 | + shift |
| 126 | + ;; |
| 127 | + *) |
| 128 | + echo "Unknown option: $1" |
| 129 | + echo "Usage: $0 [--mingw] [--cuda] [--all]" |
| 130 | + exit 1 |
| 131 | + ;; |
| 132 | + esac |
| 133 | +done |
| 134 | + |
| 135 | +# Default to installing everything if no options specified |
| 136 | +if [ "${INSTALL_MINGW}" = false ] && [ "${INSTALL_CUDA}" = false ]; then |
| 137 | + INSTALL_MINGW=true |
| 138 | + INSTALL_CUDA=true |
| 139 | +fi |
| 140 | + |
| 141 | +if [ "${INSTALL_MINGW}" = true ]; then |
| 142 | + install_mingw |
| 143 | +fi |
| 144 | + |
| 145 | +if [ "${INSTALL_CUDA}" = true ]; then |
| 146 | + install_windows_cuda |
| 147 | +fi |
| 148 | + |
| 149 | +echo "Installation complete" |
0 commit comments