From f3b89b47f0030c97c9afef6dc23fcd40f8cb378b Mon Sep 17 00:00:00 2001 From: Brendan Stork Date: Wed, 21 Oct 2026 01:52:47 -0700 Subject: [PATCH] Fix host memory detection on macOS --- pyproject.toml | 2 +- src/src_method/utils/_backend.py | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index f4b1098..d3160c6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -32,7 +32,7 @@ classifiers = [ "Typing :: Typed", ] dynamic = ["version"] -dependencies = ["array-api-compat>=1.12", "numpy>=2.0,<3", "opt_einsum>=3.4.0"] +dependencies = ["array-api-compat>=1.12", "numpy>=2.0,<3", "opt_einsum>=3.4.0", "psutil"] [project.optional-dependencies] diff --git a/src/src_method/utils/_backend.py b/src/src_method/utils/_backend.py index ff78a37..d42bd2b 100644 --- a/src/src_method/utils/_backend.py +++ b/src/src_method/utils/_backend.py @@ -19,6 +19,8 @@ import numpy as np from array_api_compat import numpy as np_compat +import psutil + if TYPE_CHECKING: from collections.abc import Iterator, Sequence from types import ModuleType @@ -378,7 +380,12 @@ def host_memory_available(meminfo: str = "/proc/meminfo") -> int: for line in path.read_text().splitlines(): if line.startswith("MemAvailable:"): return int(line.split()[1]) * 1024 - return os.sysconf("SC_AVPHYS_PAGES") * os.sysconf("SC_PAGE_SIZE") + + try: + return os.sysconf("SC_AVPHYS_PAGES") * os.sysconf("SC_PAGE_SIZE") + except (ValueError, OSError): + return psutil.virtual_memory().available + def sketch_dtype(dtype: DTypeLike | None, *inputs: Sequence[Site]) -> np.dtype: