Spaces:
Paused
Paused
Download satquery_engine/models/device.py from SM737/satquery-api: direct link, hf CLI and curl.
- Browser
- Download file 1.05 kB
-
https://huggingface.co/spaces/SM737/satquery-api/resolve/main/satquery_engine/models/device.py
- Command line
-
hf download hf://spaces/SM737/satquery-api/satquery_engine/models/device.py
-
curl -L -o device.py https://huggingface.co/spaces/SM737/satquery-api/resolve/main/satquery_engine/models/device.py
1.05 kB
| """One runtime device policy for the active PyTorch and ONNX model routes.""" | |
| from __future__ import annotations | |
| import os | |
| def torch_device() -> str: | |
| import torch | |
| requested = os.getenv("SATQUERY_DEVICE", "auto").lower() | |
| if requested not in {"auto", "cuda", "cpu"}: | |
| raise ValueError("SATQUERY_DEVICE must be auto, cuda, or cpu") | |
| if requested == "cpu": | |
| return "cpu" | |
| if torch.cuda.is_available(): | |
| return "cuda" | |
| if requested == "cuda": | |
| raise RuntimeError("CUDA was requested but PyTorch cannot access the GPU") | |
| return "cpu" | |
| def onnx_providers() -> list[str]: | |
| import onnxruntime as ort | |
| if torch_device() == "cuda": | |
| if "CUDAExecutionProvider" not in ort.get_available_providers(): | |
| raise RuntimeError("CUDA is available, but ONNX Runtime has no CUDA execution provider") | |
| try: | |
| ort.preload_dlls() | |
| except AttributeError: | |
| pass | |
| return ["CUDAExecutionProvider", "CPUExecutionProvider"] | |
| return ["CPUExecutionProvider"] | |