From 0fc22cd86da7a4920595399348fc80322bedee51 Mon Sep 17 00:00:00 2001 From: Mengwei Liu Date: Fri, 10 Oct 2025 14:28:25 -0700 Subject: [PATCH 1/4] Automate the detection of CUDA in the build script --- .ci/scripts/test-cuda-build.sh | 3 --- .github/workflows/cuda.yml | 8 +++---- install_utils.py | 38 +++++++++++++--------------------- 3 files changed, 18 insertions(+), 31 deletions(-) diff --git a/.ci/scripts/test-cuda-build.sh b/.ci/scripts/test-cuda-build.sh index bae7dd6af16..08673533927 100755 --- a/.ci/scripts/test-cuda-build.sh +++ b/.ci/scripts/test-cuda-build.sh @@ -27,9 +27,6 @@ test_executorch_cuda_build() { nvcc --version || echo "nvcc not found" nvidia-smi || echo "nvidia-smi not found" - # Set CMAKE_ARGS to enable CUDA build - ExecuTorch will handle PyTorch installation automatically - export CMAKE_ARGS="-DEXECUTORCH_BUILD_CUDA=ON" - echo "=== Starting ExecuTorch Installation ===" # Install ExecuTorch with CUDA support with timeout and error handling timeout 5400 ./install_executorch.sh || { diff --git a/.github/workflows/cuda.yml b/.github/workflows/cuda.yml index 8461e6d9e31..8a9dc101ff3 100644 --- a/.github/workflows/cuda.yml +++ b/.github/workflows/cuda.yml @@ -1,7 +1,7 @@ # Test ExecuTorch CUDA Build Compatibility # This workflow tests whether ExecuTorch can be successfully built with CUDA support # across different CUDA versions (12.6, 12.8, 12.9) using the command: -# CMAKE_ARGS="-DEXECUTORCH_BUILD_CUDA=ON" ./install_executorch.sh +# ./install_executorch.sh # # Note: ExecuTorch automatically detects the system CUDA version using nvcc and # installs the appropriate PyTorch wheel. No manual CUDA/PyTorch installation needed. @@ -43,7 +43,7 @@ jobs: set -eux # Test ExecuTorch CUDA build - ExecuTorch will automatically detect CUDA version - # and install the appropriate PyTorch wheel when CMAKE_ARGS="-DEXECUTORCH_BUILD_CUDA=ON" + # and install the appropriate PyTorch wheel source .ci/scripts/test-cuda-build.sh "${{ matrix.cuda-version }}" # This job will fail if any of the CUDA versions fail @@ -83,7 +83,7 @@ jobs: script: | set -eux - PYTHON_EXECUTABLE=python CMAKE_ARGS="-DEXECUTORCH_BUILD_CUDA=ON" ./install_executorch.sh + PYTHON_EXECUTABLE=python ./install_executorch.sh export LD_LIBRARY_PATH=/opt/conda/lib:$LD_LIBRARY_PATH PYTHON_EXECUTABLE=python source .ci/scripts/test_model.sh "${{ matrix.model }}" cmake cuda @@ -110,7 +110,7 @@ jobs: set -eux echo "::group::Setup ExecuTorch" - CMAKE_ARGS="-DEXECUTORCH_BUILD_CUDA=ON" ./install_executorch.sh + ./install_executorch.sh echo "::endgroup::" echo "::group::Setup Huggingface" diff --git a/install_utils.py b/install_utils.py index 113005ba1e4..39827636cf1 100644 --- a/install_utils.py +++ b/install_utils.py @@ -6,19 +6,12 @@ # LICENSE file in the root directory of this source tree. import functools -import os import platform import re import subprocess import sys -def _is_cuda_enabled(): - """Check if CUDA delegate is enabled via CMAKE_ARGS environment variable.""" - cmake_args = os.environ.get("CMAKE_ARGS", "") - return "-DEXECUTORCH_BUILD_CUDA=ON" in cmake_args - - def _cuda_version_to_pytorch_suffix(major, minor): """ Generate PyTorch CUDA wheel suffix from CUDA version numbers. @@ -33,6 +26,7 @@ def _cuda_version_to_pytorch_suffix(major, minor): return f"cu{major}{minor}" +@functools.lru_cache(maxsize=1) def _get_cuda_version(supported_cuda_versions): """ Get the CUDA version installed on the system using nvcc command. @@ -62,25 +56,23 @@ def _get_cuda_version(supported_cuda_versions): ) raise RuntimeError( f"Detected CUDA version {major}.{minor} is not supported. " - f"Only the following CUDA versions are supported: {available_versions}. " - f"Please install a supported CUDA version or try on CPU-only delegates." + f"Supported versions: {available_versions}." ) return (major, minor) else: raise RuntimeError( - "CUDA delegate is enabled but could not parse CUDA version from nvcc output. " - "Please ensure CUDA is properly installed or try on CPU-only delegates." + "Failed to parse CUDA version from nvcc output. " + "Ensure CUDA is properly installed." ) except FileNotFoundError: raise RuntimeError( - "CUDA delegate is enabled but nvcc (CUDA compiler) is not found in PATH. " - "Please install CUDA toolkit or try on CPU-only delegates." + "nvcc (CUDA compiler) is not found in PATH. Install the CUDA toolkit." ) except subprocess.CalledProcessError as e: raise RuntimeError( - f"CUDA delegate is enabled but nvcc command failed with error: {e}. " - "Please ensure CUDA is properly installed or try on CPU-only delegates." + f"nvcc command failed with error: {e}. " + "Ensure CUDA is properly installed." ) @@ -105,7 +97,7 @@ def _get_pytorch_cuda_url(cuda_version, torch_nightly_url_base): @functools.lru_cache(maxsize=1) def determine_torch_url(torch_nightly_url_base, supported_cuda_versions): """ - Determine the appropriate PyTorch installation URL based on CUDA availability and CMAKE_ARGS. + Determine the appropriate PyTorch installation URL based on CUDA availability. Uses @functools.lru_cache to avoid redundant CUDA detection and print statements. Args: @@ -115,15 +107,13 @@ def determine_torch_url(torch_nightly_url_base, supported_cuda_versions): Returns: URL string for PyTorch packages """ - # Check if CUDA delegate is enabled - if not _is_cuda_enabled(): - print("CUDA delegate not enabled, using CPU-only PyTorch") - return f"{torch_nightly_url_base}/cpu" + print("Attempting to detect CUDA via nvcc...") - print("CUDA delegate enabled, detecting CUDA version...") - - # Get CUDA version - cuda_version = _get_cuda_version(supported_cuda_versions) + try: + cuda_version = _get_cuda_version(supported_cuda_versions) + except RuntimeError as err: + print(f"CUDA detection failed ({err}), using CPU-only PyTorch") + return f"{torch_nightly_url_base}/cpu" major, minor = cuda_version print(f"Detected CUDA version: {major}.{minor}") From 41e326ef3ab10f23d609edc98fa1cab594f8faf6 Mon Sep 17 00:00:00 2001 From: Mengwei Liu Date: Fri, 10 Oct 2025 14:32:06 -0700 Subject: [PATCH 2/4] Catch exception --- install_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/install_utils.py b/install_utils.py index 39827636cf1..7178e514151 100644 --- a/install_utils.py +++ b/install_utils.py @@ -111,7 +111,7 @@ def determine_torch_url(torch_nightly_url_base, supported_cuda_versions): try: cuda_version = _get_cuda_version(supported_cuda_versions) - except RuntimeError as err: + except Exception as err: print(f"CUDA detection failed ({err}), using CPU-only PyTorch") return f"{torch_nightly_url_base}/cpu" From d75ff3b8bfe1859491ca5b807988def7c301cf66 Mon Sep 17 00:00:00 2001 From: Mengwei Liu Date: Mon, 13 Oct 2025 11:44:14 -0700 Subject: [PATCH 3/4] Fix windows --- install_utils.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/install_utils.py b/install_utils.py index 7178e514151..9ab3cf7b970 100644 --- a/install_utils.py +++ b/install_utils.py @@ -107,6 +107,10 @@ def determine_torch_url(torch_nightly_url_base, supported_cuda_versions): Returns: URL string for PyTorch packages """ + if platform.system().lower() == "windows": + print("Windows detected, using CPU-only PyTorch until CUDA support is available") + return f"{torch_nightly_url_base}/cpu" + print("Attempting to detect CUDA via nvcc...") try: From da03f52a23425d6a0173752ec59b5c11fd5c2fa6 Mon Sep 17 00:00:00 2001 From: Mengwei Liu Date: Mon, 13 Oct 2025 15:28:13 -0700 Subject: [PATCH 4/4] lint --- install_utils.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/install_utils.py b/install_utils.py index 9ab3cf7b970..648f8f61c37 100644 --- a/install_utils.py +++ b/install_utils.py @@ -108,7 +108,9 @@ def determine_torch_url(torch_nightly_url_base, supported_cuda_versions): URL string for PyTorch packages """ if platform.system().lower() == "windows": - print("Windows detected, using CPU-only PyTorch until CUDA support is available") + print( + "Windows detected, using CPU-only PyTorch until CUDA support is available" + ) return f"{torch_nightly_url_base}/cpu" print("Attempting to detect CUDA via nvcc...")