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
270 changes: 270 additions & 0 deletions autonerves/environment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,270 @@
"""
The environment layer: what hardware and optional packages this process can use,
and what compute setup the user asked for through ``PYAUTO_SETUP``.

This module only *detects* and *parses*; it never chooses a setup. Choosing (the
backend, device, operator and precision that ``setup="auto"`` resolves to) belongs to
``Setup.auto`` in PyAutoArray, which reads :class:`Environment` and
:func:`parse_setup_intent` from here.

It imports nothing heavy at module level (no JAX, numba or nufftax), because
``autonerves.jax_wrapper`` calls :func:`parse_setup_intent` and
:func:`detect_cores` before JAX may be imported.
"""
import importlib.util
import os
import re
import socket
import subprocess
import sys
from dataclasses import dataclass
from typing import Optional

SETUP_ENV_VAR = "PYAUTO_SETUP"

SETUP_BACKENDS = ("jax", "numba", "numpy")
SETUP_DEVICES = ("cpu", "gpu")
SETUP_PRECISIONS = ("fp64", "fp32", "mp")

_SETUP_ID = re.compile(
r"^(?P<backend>jax|numba|numpy)"
r"_(?P<device>cpu|gpu)"
r"_(?P<operator>[a-z0-9]+)"
r"_(?P<precision>fp64|fp32|mp)"
r"(?:_(?P<transformer>[a-z0-9]+))?$"
)

_ALLOWED_FORMS = (
"unset, '' or 'auto'; 'cpu'; 'gpu'; or a setup_id "
"'<backend>_<device>_<operator>_<precision>[_<transformer>]' with backend in "
f"{SETUP_BACKENDS}, device in {SETUP_DEVICES}, precision in {SETUP_PRECISIONS} "
"and operator/transformer lowercase alphanumeric tokens "
"(e.g. 'numba_cpu_sparse_fp64', 'jax_gpu_dense_fp64', "
"'jax_gpu_sparse_fp64_wtilde')"
)


def parse_setup_intent(value: Optional[str]) -> str:
"""
The device intent of a ``PYAUTO_SETUP`` value: ``"auto"``, ``"cpu"`` or ``"gpu"``.

Accepted forms (case-insensitive, surrounding whitespace ignored):

- ``None`` (unset), ``""`` or ``"auto"`` -> ``"auto"``: nothing is pinned; JAX
picks the GPU when one is present.
- ``"cpu"`` / ``"gpu"`` -> that device.
- a setup_id ``<backend>_<device>_<operator>_<precision>[_<transformer>]``, the
vocabulary of the profiling campaigns (e.g. ``numba_cpu_sparse_fp64``,
``jax_gpu_dense_fp64``, ``jax_gpu_sparse_fp64_wtilde``): ``backend`` is one of
``jax``, ``numba``, ``numpy``; ``device`` is ``cpu`` or ``gpu``; ``precision``
is ``fp64``, ``fp32`` or ``mp``; ``operator`` (``dense``, ``sparse``, ``na``,
...) and the optional ``transformer`` (``wtilde``, ``nufft``, ...) are any
lowercase alphanumeric token. The device token decides the intent.

Raises
------
ValueError
If ``value`` is none of these; the message names the allowed forms.
"""
if value is None:
return "auto"
text = value.strip().lower()
if text in ("", "auto"):
return "auto"
if text in SETUP_DEVICES:
return text
match = _SETUP_ID.match(text)
if match is None:
raise ValueError(
f"{SETUP_ENV_VAR}={value!r} is not a valid setup. Allowed: {_ALLOWED_FORMS}."
)
return match.group("device")


def detect_cores() -> int:
"""
The number of CPU cores this process may use.

In order: ``SLURM_CPUS_PER_TASK`` (the cores Slurm allocated to the task), then
``len(os.sched_getaffinity(0))`` (the cores this process is pinned to, which
respects ``taskset`` and cgroup CPU sets), then ``os.cpu_count()``; at least 1.
"""
slurm = os.environ.get("SLURM_CPUS_PER_TASK")
if slurm:
try:
cores = int(slurm)
if cores > 0:
return cores
except ValueError:
pass
try:
return max(1, len(os.sched_getaffinity(0)))
except (AttributeError, OSError):
pass
return max(1, os.cpu_count() or 1)


def detect_ram_gb() -> Optional[float]:
"""
Total physical memory in GB, from ``MemTotal`` in ``/proc/meminfo``, falling back
to ``os.sysconf`` (pages x page size); ``None`` if neither is available.
"""
try:
with open("/proc/meminfo") as f:
for line in f:
if line.startswith("MemTotal:"):
return int(line.split()[1]) / 1024**2
except (OSError, ValueError, IndexError):
pass
try:
return os.sysconf("SC_PHYS_PAGES") * os.sysconf("SC_PAGE_SIZE") / 1024**3
except (AttributeError, ValueError, OSError):
return None


def _package_available(name: str) -> bool:
"""Whether ``name`` is importable, checked with ``find_spec`` (never imported)."""
try:
return importlib.util.find_spec(name) is not None
except (ImportError, ValueError):
return False


def detect_gpu(timeout: float = 5.0):
"""
``(gpu, gpu_name, gpu_memory_gb)`` for the first GPU this process can see.

This never imports JAX itself, so it is safe before JAX initialises:

- If ``jax`` is already in ``sys.modules``, ``jax.devices()`` is asked (JAX has
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 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``.
"""
if "jax" in sys.modules:
try:
jax = sys.modules["jax"]
gpus = [d for d in jax.devices() if d.platform == "gpu"]
except Exception:
gpus = []
if not gpus:
return False, None, None
device = gpus[0]
memory_gb = None
try:
stats = device.memory_stats() or {}
if "bytes_limit" in stats:
memory_gb = stats["bytes_limit"] / 1024**3
except Exception:
pass
return True, getattr(device, "device_kind", None), memory_gb

try:
result = subprocess.run(
[
"nvidia-smi",
"--query-gpu=name,memory.total",
"--format=csv,noheader,nounits",
],
capture_output=True,
text=True,
timeout=timeout,
)
except (OSError, subprocess.SubprocessError):
return False, None, None
if result.returncode != 0 or not result.stdout.strip():
return False, None, None
first = result.stdout.strip().splitlines()[0]
name, _, memory = first.rpartition(",")
if not name:
name, memory = memory, ""
try:
memory_gb = float(memory.strip()) / 1024 # nvidia-smi reports MiB
except ValueError:
memory_gb = None
return True, name.strip() or None, memory_gb


def detect_site() -> str:
"""
A hint naming the computing site.

``SLURM_CLUSTER_NAME`` (lowercased) when Slurm sets it; otherwise a class of the
hostname: the name of a site file shipped in ``autonerves/site/`` (the site layer
of :mod:`autonerves.site_config`) when the hostname starts with it, ``"colab"``
on Google Colab, else ``"local"``. It is a hint only; nothing is loaded.
"""
cluster = os.environ.get("SLURM_CLUSTER_NAME")
if cluster:
return cluster.strip().lower()
try:
hostname = socket.gethostname().lower()
except OSError:
hostname = ""
from autonerves.site_config import PACKAGE_SITE_DIRECTORY

for path in sorted(PACKAGE_SITE_DIRECTORY.glob("*.yaml")):
if hostname.startswith(path.stem.lower()):
return path.stem.lower()
if "COLAB_RELEASE_TAG" in os.environ or "COLAB_GPU" in os.environ:
return "colab"
return "local"


@dataclass(frozen=True)
class Environment:
"""
What this process can use, as detected by :meth:`Environment.detect`.

Attributes
----------
gpu
Whether a GPU is visible.
gpu_name
The first GPU's name, or ``None``.
gpu_memory_gb
The first GPU's memory in GB, or ``None`` if unknown.
cores
CPU cores this process may use (see :func:`detect_cores`).
ram_gb
Total physical memory in GB, or ``None`` if unknown.
numba
Whether ``numba`` is installed (checked without importing it).
nufftax
Whether ``nufftax`` is installed (checked without importing it).
site
A site hint (see :func:`detect_site`).
"""

gpu: bool
gpu_name: Optional[str]
gpu_memory_gb: Optional[float]
cores: int
ram_gb: Optional[float]
numba: bool
nufftax: bool
site: str

@classmethod
def detect(cls) -> "Environment":
"""
Probe the current process. Reports only; chooses nothing.

Never imports JAX: GPU facts come from ``jax.devices()`` only when JAX is
already imported, otherwise from a single ``nvidia-smi`` query with a short
timeout (see :func:`detect_gpu`).
"""
gpu, gpu_name, gpu_memory_gb = detect_gpu()
return cls(
gpu=gpu,
gpu_name=gpu_name,
gpu_memory_gb=gpu_memory_gb,
cores=detect_cores(),
ram_gb=detect_ram_gb(),
numba=_package_available("numba"),
nufftax=_package_available("nufftax"),
site=detect_site(),
)
50 changes: 42 additions & 8 deletions autonerves/jax_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,35 @@

import os

from autonerves.environment import SETUP_ENV_VAR, detect_cores, parse_setup_intent

# PYAUTO_SETUP is read before anything can import JAX, because JAX_PLATFORMS and the
# thread variables only take effect if set before JAX (and the BLAS / numba thread
# pools) initialise. An explicit CPU intent ("cpu", or a setup_id whose device token
# is cpu, e.g. numba_cpu_sparse_fp64) pins JAX to the CPU and sizes the thread pools
# to the detected cores; "auto", unset and "gpu" set nothing. A value the user has
# already set is never overwritten. Invalid values raise ValueError naming the
# allowed forms (see autonerves.environment.parse_setup_intent).
_setup_intent = parse_setup_intent(os.environ.get(SETUP_ENV_VAR))

if _setup_intent == "cpu":
_cores = str(detect_cores())
_applied = {}
for _key, _value in (
("JAX_PLATFORMS", "cpu"),
("OMP_NUM_THREADS", _cores),
("MKL_NUM_THREADS", _cores),
("OPENBLAS_NUM_THREADS", _cores),
("NUMBA_NUM_THREADS", _cores),
):
if _key not in os.environ:
os.environ[_key] = _value
_applied[_key] = _value
logger.info(
f"{SETUP_ENV_VAR}={os.environ[SETUP_ENV_VAR]!r} requests CPU: set {_applied} "
"(variables already set were left unchanged)."
)

if importlib.util.find_spec("jax") is None:
logger.warning(
"""
Expand Down Expand Up @@ -87,25 +116,30 @@

jax_enable_x64 = os.environ.get("JAX_ENABLE_X64")

# Unset -> force x64 on (double precision is the default for the PyAuto stack). An
# explicit false value ("false", "0") is a deliberate fp32 request and is respected;
# any other explicit value (e.g. "true", "1") is left to JAX as given.
if jax_enable_x64 is None:
jax_enable_x64 = False
elif isinstance(jax_enable_x64, str):
jax_enable_x64 = jax_enable_x64.lower() == "true"

if not jax_enable_x64:

os.environ["JAX_ENABLE_X64"] = "True"

logger.info(
""""
"""
JAX 64-bit precision has been automatically enabled for you (JAX_ENABLE_X64=True),
as double precision is required for most scientific computing applications.

To enable 64 precision as default in JAX, set the environment variable
JAX_ENABLE_X64=true before running your script.
To run JAX in 32-bit precision instead, set the environment variable
JAX_ENABLE_X64=False before running your script.
"""
)

elif jax_enable_x64.strip().lower() in ("false", "0"):

logger.info(
f"JAX_ENABLE_X64={jax_enable_x64!r}: 32-bit (fp32) JAX precision was "
"requested and is respected; 64-bit precision is not enabled."
)

if "JAX_COMPILATION_CACHE_DIR" not in os.environ:

_cache_root = os.environ.get("XDG_CACHE_HOME") or os.path.join(
Expand Down
Loading
Loading