|
| 1 | +# SPDX-License-Identifier: MPL-2.0 |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import os |
| 6 | +import platform |
| 7 | +import subprocess |
| 8 | +import sys |
| 9 | +from functools import cache |
| 10 | +from typing import TYPE_CHECKING |
| 11 | + |
| 12 | +import numba |
| 13 | + |
| 14 | +from . import LAYERS |
| 15 | + |
| 16 | + |
| 17 | +if TYPE_CHECKING: |
| 18 | + from . import TheadingCategory, ThreadingLayer |
| 19 | + |
| 20 | + |
| 21 | +__all__ = ["_needs_parallel_runtime_probe", "_parallel_numba_runtime_is_safe"] |
| 22 | + |
| 23 | + |
| 24 | +type _ParallelRuntimeProbeKey = tuple[str, ThreadingLayer | TheadingCategory, tuple[ThreadingLayer, ...], tuple[str, ...]] |
| 25 | + |
| 26 | + |
| 27 | +_PARALLEL_RUNTIME_PROBE_SENTINEL = "FAST_ARRAY_UTILS_NUMBA_PROBE_OK" |
| 28 | +_PARALLEL_RUNTIME_PROBE_TIMEOUT = 20 |
| 29 | +_PARALLEL_RUNTIME_PROBE_MODULE_WHITELIST = ("torch",) |
| 30 | +_PARALLEL_RUNTIME_PROBE_CODE = f""" |
| 31 | +import numba |
| 32 | +import numpy as np |
| 33 | +
|
| 34 | +@numba.njit(parallel=True, cache=False) |
| 35 | +def _probe(values): |
| 36 | + total = 0.0 |
| 37 | + for i in numba.prange(values.shape[0]): |
| 38 | + total += values[i] |
| 39 | + return total |
| 40 | +
|
| 41 | +values = np.arange(32, dtype=np.float64) |
| 42 | +assert _probe(values) == np.sum(values) |
| 43 | +print({_PARALLEL_RUNTIME_PROBE_SENTINEL!r}) |
| 44 | +""" |
| 45 | + |
| 46 | + |
| 47 | +def _is_apple_silicon() -> bool: |
| 48 | + return sys.platform == "darwin" and platform.machine() == "arm64" |
| 49 | + |
| 50 | + |
| 51 | +def _needs_parallel_runtime_probe() -> bool: |
| 52 | + if not _is_apple_silicon() or "torch" not in sys.modules: |
| 53 | + return False |
| 54 | + |
| 55 | + match numba.config.THREADING_LAYER: |
| 56 | + case "omp": |
| 57 | + return True |
| 58 | + case "tbb" | "workqueue": |
| 59 | + return False |
| 60 | + case "default" | "safe" | "threadsafe" | "forksafe" as category: |
| 61 | + return "omp" in LAYERS[category] |
| 62 | + |
| 63 | + |
| 64 | +def _loaded_relevant_parallel_runtime_probe_modules() -> tuple[str, ...]: |
| 65 | + return tuple(module for module in _PARALLEL_RUNTIME_PROBE_MODULE_WHITELIST if module in sys.modules) |
| 66 | + |
| 67 | + |
| 68 | +def _parallel_runtime_probe_code(modules: tuple[str, ...]) -> str: |
| 69 | + return "\n".join(f"import {module}" for module in modules) + _PARALLEL_RUNTIME_PROBE_CODE |
| 70 | + |
| 71 | + |
| 72 | +def _parallel_runtime_probe_key() -> _ParallelRuntimeProbeKey: |
| 73 | + return ( |
| 74 | + sys.executable, |
| 75 | + numba.config.THREADING_LAYER, |
| 76 | + tuple(numba.config.THREADING_LAYER_PRIORITY), |
| 77 | + _loaded_relevant_parallel_runtime_probe_modules(), |
| 78 | + ) |
| 79 | + |
| 80 | + |
| 81 | +def _build_parallel_runtime_probe_env(key: _ParallelRuntimeProbeKey | None = None) -> dict[str, str]: |
| 82 | + _, layer_or_category, priority, _ = _parallel_runtime_probe_key() if key is None else key |
| 83 | + env = dict(os.environ) |
| 84 | + env["NUMBA_THREADING_LAYER"] = layer_or_category |
| 85 | + env["NUMBA_THREADING_LAYER_PRIORITY"] = " ".join(priority) |
| 86 | + return env |
| 87 | + |
| 88 | + |
| 89 | +@cache |
| 90 | +def _parallel_numba_runtime_is_safe_cached(key: _ParallelRuntimeProbeKey) -> bool: |
| 91 | + try: |
| 92 | + # The probe command is built from `sys.executable` plus a generated script |
| 93 | + # that only imports modules from a fixed whitelist. |
| 94 | + result = subprocess.run( # noqa: S603 |
| 95 | + [key[0], "-c", _parallel_runtime_probe_code(key[3])], |
| 96 | + capture_output=True, |
| 97 | + check=False, |
| 98 | + env=_build_parallel_runtime_probe_env(key), |
| 99 | + text=True, |
| 100 | + timeout=_PARALLEL_RUNTIME_PROBE_TIMEOUT, |
| 101 | + ) |
| 102 | + except Exception: # noqa: BLE001 |
| 103 | + return False |
| 104 | + return result.returncode == 0 and _PARALLEL_RUNTIME_PROBE_SENTINEL in result.stdout |
| 105 | + |
| 106 | + |
| 107 | +def _parallel_numba_runtime_is_safe() -> bool: |
| 108 | + return _parallel_numba_runtime_is_safe_cached(_parallel_runtime_probe_key()) |
0 commit comments