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
22 changes: 22 additions & 0 deletions autonerves/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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``.
Expand All @@ -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(
[
Expand Down
31 changes: 31 additions & 0 deletions test_autonerves/test_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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"
Expand Down
Loading