From c717b2d6c84a03c963b9ec952c4be0fe59dac080 Mon Sep 17 00:00:00 2001 From: cuddandr Date: Thu, 17 Sep 2026 10:05:40 -0700 Subject: [PATCH 1/3] Improve CUDA detection in setup.py Improve the CUDA version detection in setup.py to use multiple methods. First it tries to read it from the `nvcc` CUDA compiler, and then checks CUDA_HOME if the compiler check fails. The logic for CUDA_HOME was also improved to be more robust. The logic for adding the correct version of CuPy (or skipping it) was also tweaked, and new print statements for better clarity when installing. --- setup.py | 79 ++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 48 insertions(+), 31 deletions(-) diff --git a/setup.py b/setup.py index e0dc3698..311d275d 100644 --- a/setup.py +++ b/setup.py @@ -1,11 +1,13 @@ from setuptools import setup import os +import re +import subprocess dependencies = [ + "cupy", "numpy", - "pytest", "numba>=0.52", - # "larpix-control", + "pytest", "larpix-control @ git+https://github.com/larpix/larpix-control.git@release", "larpix-geometry", "tqdm", @@ -13,41 +15,56 @@ "nvidia-ml-py", ] +# Extra development tools or CI/CD dependencies optional_dep = { "linting" : ["MonkeyType", "mypy", "ruff"], "runner" : ["pandas", "rich"] } -try: - import cupy - msg = ''' - ############ INFORMATION ############ - Detected and using the installed cupy - Version: %s - Source : %s - #####################################\n - ''' - print(msg % (str(cupy.__version__),str(cupy.__file__))) -except ImportError: - dependencies.append('cupy') - -try: - cuda_dir = os.path.basename(os.environ['CUDA_HOME']) - cuda_ver = float(cuda_dir) - cuda_major_ver = int(cuda_ver) - print(f"larnd-sim -- Found CUDA version: {cuda_ver}") -except: - cuda_ver = cuda_major_ver = -1 - -if 'cupy' in dependencies: - if 'SKIP_CUPY_INSTALL' in os.environ: - dependencies.remove('cupy') +def get_cuda_version(): + # Try nvcc --version (the CUDA compiler) + try: + res = subprocess.check_output(["nvcc", "--version"], stderr=subprocess.STDOUT).decode() + match = re.search(r"release (\d+)", res) + if match: + return int(match.group(1)) + except (subprocess.CalledProcessError, FileNotFoundError): + pass + + # Try $CUDA_HOME and extract version from directory name + cuda_home = os.environ.get("CUDA_HOME") + if cuda_home: + # Match digits in the basename, e.g. "12.2" -> 12 + match = re.search(r"(\d+)", os.path.basename(cuda_home)) + if match: + return int(match.group(1)) + + return None + +cuda_major_ver = get_cuda_version() + +# We only support CUDA 11 and higher +if cuda_major_ver is None or cuda_major_ver < 11: + if "SKIP_CUPY_INSTALL" not in os.environ: + raise RuntimeError( + f"CUDA version {cuda_major_ver} not detected or unsupported (<11). " + "Please set CUDA_HOME or ensure nvcc is in PATH, or use SKIP_CUPY_INSTALL=1 to skip CuPy." + ) +else: + print(f"larnd-sim -- Found CUDA version: {cuda_major_ver}") + +# Confirmed to have supported CUDA, check how to (or if) install CuPy +if "cupy" in dependencies: + if "SKIP_CUPY_INSTALL" in os.environ: + print("larnd-sim -- Skipping CuPy installation.") + dependencies.remove("cupy") + elif "ALWAYS_COMPILE_CUPY" not in os.environ: + print("larnd-sim -- Using pre-built wheel for CuPy and numba-cuda.") + dependencies.remove("cupy") + dependencies.append(f"cupy-cuda{cuda_major_ver}x") + dependencies.append(f"numba-cuda[cu{cuda_major_ver}]") # TODO: Update numba-cuda to numba-cuda-mlir else: - if 'ALWAYS_COMPILE_CUPY' not in os.environ: - if cuda_major_ver in [11, 12, 13]: - dependencies.remove('cupy') - dependencies.append(f'cupy-cuda{cuda_major_ver}x') - dependencies.append(f'numba-cuda[cu{cuda_major_ver}]') + print("larnd-sim -- Attempting to build CuPy from source.") setup( install_requires=dependencies, From 850eb3258226b99531482cf90a997da91726f436 Mon Sep 17 00:00:00 2001 From: cuddandr Date: Tue, 13 Oct 2026 10:48:35 -0700 Subject: [PATCH 2/3] Pin NumPy to >=2.0,<2.5 to prevent numba-cuda row_stack errors While Numba itself has fixed the use of NumPy row_stack (which was removed in 2.5+), numba-cuda still relies on it and will cause a crash if NumPy 2.5+ is installed (which is our current default). Pin NumPy to use something before 2.5 so row_stack is still available for numba-cuda (usually 2.4.6). Once we move to numba-cuda-mlir this is no longer a problem. --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 311d275d..1bcc3387 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ dependencies = [ "cupy", - "numpy", + "numpy>=2.0,<2.5", # capped at <2.5 until we update numba-cuda to numba-cuda-mlir "numba>=0.52", "pytest", "larpix-control @ git+https://github.com/larpix/larpix-control.git@release", From 62d357b6c140e694134396cb1a3e83307a635f6e Mon Sep 17 00:00:00 2001 From: cuddandr Date: Tue, 13 Oct 2026 11:02:31 -0700 Subject: [PATCH 3/3] Fix broken Sphinx docs GitHub Action The new setup.py will throw a runtime error if CUDA is not detected unless told to skip the CuPy install. Set SKIP_CUPY_INSTALL in the workflow so the docs build. --- .github/workflows/docs.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index ceafd00b..43d371af 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -26,6 +26,8 @@ jobs: build-docs: name: Build Sphinx documentation runs-on: ubuntu-latest + env: + SKIP_CUPY_INSTALL: 1 steps: - name: Checkout repository