From 7a321a4caff3d13953c321d0a2a659743b2849bd Mon Sep 17 00:00:00 2001 From: Jammy2211 Date: Wed, 25 Nov 2026 10:55:50 +0000 Subject: [PATCH] fix: detect no GPU when JAX_PLATFORMS excludes GPU platforms Part of the concise automatic compute-setup banner (PyAutoLabs/PyAutoGalaxy#754): one header line stating the automatic CPU/GPU choice, aligned Setup / Parallel / Estimate lines printed together, and notices when the setup acts outside modeling. Shares branch feature/auto-setup-banner across PyAutoNerves, PyAutoArray, PyAutoFit, PyAutoGalaxy and PyAutoLens. Co-Authored-By: Claude Opus 5.5 --- autonerves/environment.py | 22 ++++++++++++++++++++ test_autonerves/test_environment.py | 31 +++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/autonerves/environment.py b/autonerves/environment.py index 865052c..3d53c84 100644 --- a/autonerves/environment.py +++ b/autonerves/environment.py @@ -130,6 +130,22 @@ def _package_available(name: str) -> bool: return False +GPU_PLATFORMS = ("cuda", "rocm", "gpu") + + +def _jax_platforms_exclude_gpu() -> bool: + """ + Whether ``JAX_PLATFORMS`` (or the older ``JAX_PLATFORM_NAME``) restricts JAX to + platforms with no GPU, e.g. ``JAX_PLATFORMS=cpu``. Unset or empty means no + restriction (``False``). + """ + value = os.environ.get("JAX_PLATFORMS") or os.environ.get("JAX_PLATFORM_NAME") or "" + platforms = {p.strip().lower() for p in value.split(",") if p.strip()} + if not platforms: + return False + return not any(p in GPU_PLATFORMS for p in platforms) + + def detect_gpu(timeout: float = 5.0): """ ``(gpu, gpu_name, gpu_memory_gb)`` for the first GPU this process can see. @@ -140,6 +156,9 @@ def detect_gpu(timeout: float = 5.0): already chosen its platform, so this reflects what JAX will actually use, including ``JAX_PLATFORMS=cpu``). The name is the device kind; memory comes from ``memory_stats()["bytes_limit"]`` when the backend reports it. + - Otherwise, if ``JAX_PLATFORMS`` (or the older ``JAX_PLATFORM_NAME``) is set and + names no GPU platform (``cuda``, ``rocm`` or ``gpu``), JAX will not use a GPU + even if one is installed, so the answer is ``gpu=False`` without querying it. - Otherwise one ``nvidia-smi --query-gpu=name,memory.total`` subprocess runs with ``timeout`` seconds. A missing ``nvidia-smi``, a non-zero exit, a timeout or empty output all mean ``gpu=False``. @@ -162,6 +181,9 @@ def detect_gpu(timeout: float = 5.0): pass return True, getattr(device, "device_kind", None), memory_gb + if _jax_platforms_exclude_gpu(): + return False, None, None + try: result = subprocess.run( [ diff --git a/test_autonerves/test_environment.py b/test_autonerves/test_environment.py index 2fdec54..b4027ef 100644 --- a/test_autonerves/test_environment.py +++ b/test_autonerves/test_environment.py @@ -160,6 +160,8 @@ def test_optional_packages_present(monkeypatch): @pytest.fixture def no_jax(monkeypatch): monkeypatch.delitem(sys.modules, "jax", raising=False) + monkeypatch.delenv("JAX_PLATFORMS", raising=False) + monkeypatch.delenv("JAX_PLATFORM_NAME", raising=False) def _completed(stdout, returncode=0): @@ -200,6 +202,35 @@ def run(cmd, **kwargs): assert calls[0][1]["timeout"] > 0 +@pytest.mark.parametrize( + "variable, value", + [ + ("JAX_PLATFORMS", "cpu"), + ("JAX_PLATFORMS", "CPU"), + ("JAX_PLATFORMS", "cpu,tpu"), + ("JAX_PLATFORM_NAME", "cpu"), + ], +) +def test_gpu_jax_platforms_without_gpu_skips_nvidia_smi(monkeypatch, no_jax, variable, value): + def run(*args, **kwargs): + raise AssertionError("nvidia-smi must not run when JAX is restricted to CPU") + + monkeypatch.setattr(environment.subprocess, "run", run) + monkeypatch.setenv(variable, value) + assert environment.detect_gpu() == (False, None, None) + + +@pytest.mark.parametrize("value", ["cuda", "gpu", "cpu,cuda", "rocm", ""]) +def test_gpu_jax_platforms_with_gpu_or_empty_queries_nvidia_smi(monkeypatch, no_jax, value): + monkeypatch.setattr( + environment.subprocess, + "run", + lambda *a, **k: _completed("NVIDIA A100-SXM4-40GB, 40960\n"), + ) + monkeypatch.setenv("JAX_PLATFORMS", value) + assert environment.detect_gpu() == (True, "NVIDIA A100-SXM4-40GB", 40.0) + + def test_gpu_via_already_imported_jax(monkeypatch): class Device: platform = "gpu"