Spaces:
Running
Running
Download lpu/compat.py from iparaco/Virtual-LPU: direct link, hf CLI and curl.
- Browser
- Download file 3.72 kB
-
https://huggingface.co/spaces/iparaco/Virtual-LPU/resolve/main/lpu/compat.py
- Command line
-
hf download hf://spaces/iparaco/Virtual-LPU/lpu/compat.py
-
curl -L -o compat.py https://huggingface.co/spaces/iparaco/Virtual-LPU/resolve/main/lpu/compat.py
3.72 kB
| """Optional-dependency imports. | |
| LPU is split into two runtime footprints: | |
| 1. The background controller / process discovery / monitoring stack runs with | |
| NO torch at all (psutil + PyYAML + nvidia-ml-py only, ~20 MB venv). This is | |
| a deliberate architectural decision so LPU never installs or upgrades | |
| anything inside a user's AI environment. | |
| 2. The GPU/training/benchmark stack imports torch lazily and only inside the | |
| *target project's* interpreter, where torch already exists. | |
| Every torch-touching module must use these helpers instead of a bare | |
| ``import torch`` so the controller footprint stays torch-free. | |
| """ | |
| from __future__ import annotations | |
| import importlib | |
| import importlib.util | |
| import sys | |
| from types import ModuleType | |
| from typing import Any, Optional | |
| # --- lazy torch ------------------------------------------------------------ | |
| # TORCH_AVAILABLE is a CHEAP spec probe (no import). `compat.torch` is resolved | |
| # lazily on first attribute access via module __getattr__, so importing lpu | |
| # never loads torch: the controller stays ~15 MB and every hooked process | |
| # avoids seconds of torch DLL load at interpreter startup. | |
| def _torch_available() -> bool: | |
| try: | |
| return importlib.util.find_spec("torch") is not None | |
| except (ImportError, ValueError, ModuleNotFoundError): | |
| return False | |
| TORCH_AVAILABLE = _torch_available() | |
| def __getattr__(name: str): | |
| if name == "torch": | |
| t = importlib.import_module("torch") | |
| globals()["torch"] = t # cache: subsequent lookups skip __getattr__ | |
| return t | |
| raise AttributeError(f"module {__name__!r} has no attribute {name!r}") | |
| def optional_import(name: str) -> Optional[ModuleType]: | |
| """Import a module, returning None when unavailable. Never raises.""" | |
| try: | |
| return importlib.import_module(name) | |
| except Exception: # noqa: BLE001 - optional by design | |
| return None | |
| def require_torch() -> ModuleType: | |
| """Return the torch module or raise an actionable error. | |
| Used by GPU/training/benchmark entry points. The error message tells the | |
| operator exactly which interpreter needs torch (the target project's | |
| interpreter, never an LPU-owned venv). | |
| """ | |
| try: | |
| return importlib.import_module("torch") | |
| except ImportError: | |
| raise RuntimeError( | |
| "PyTorch is required for this LPU component but is not importable " | |
| "in the current interpreter. LPU never installs packages into your " | |
| "environment. Run this component with the SAME Python interpreter " | |
| "your AI project uses (the one where torch is already installed), " | |
| "for example:\n" | |
| " C:/path/to/your/project/venv/Scripts/python.exe -m lpu doctor\n" | |
| "or pass it explicitly:\n" | |
| " lpu-run --python C:/path/to/venv/Scripts/python.exe train.py" | |
| ) from None | |
| def torch_version() -> Optional[str]: | |
| """Return the torch version string, or None when torch is absent.""" | |
| try: | |
| return importlib.import_module("torch").__version__ | |
| except Exception: # noqa: BLE001 | |
| return None | |
| def pynvml(): | |
| """Return an initialized pynvml module, or None when NVML is unavailable. | |
| nvidia-ml-py provides the ``pynvml`` import name. NVML comes from the | |
| NVIDIA display driver, so a healthy driver is a prerequisite. All callers | |
| must handle None and report the metric as unavailable - never as zero. | |
| """ | |
| nv = optional_import("pynvml") | |
| if nv is None: | |
| return None | |
| try: | |
| nv.nvmlInit() | |
| return nv | |
| except Exception: # noqa: BLE001 - driver may be absent | |
| return None | |
| def is_windows() -> bool: | |
| return sys.platform.startswith("win") | |