Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .ci/scripts/test-cuda-build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 || {
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/cuda.yml
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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"
Expand Down
40 changes: 18 additions & 22 deletions install_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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."
)


Expand All @@ -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:
Expand All @@ -115,15 +107,19 @@ 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")
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("CUDA delegate enabled, detecting CUDA version...")
print("Attempting to detect CUDA via nvcc...")

# Get CUDA version
cuda_version = _get_cuda_version(supported_cuda_versions)
try:
cuda_version = _get_cuda_version(supported_cuda_versions)
except Exception 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}")
Expand Down
Loading