"""ML device selection (#872 — GPU enablement for the ml-worker). The ml-worker is GPU-capable but must run unchanged on CPU (CI, non-GPU hosts). Selection is a per-worker-HOST bootstrap concern (the GPU host runs CUDA, others CPU), so it's an env var, not a DB setting — different workers need different values. Each framework still ANDs this intent with its OWN runtime availability (onnxruntime providers / torch.cuda), so "want GPU but none present" falls back to CPU cleanly. Env: FC_ML_DEVICE auto (default) | cuda | gpu -> try GPU; cpu -> force CPU FC_ML_ONNX_GPU_MEM_GB ONNX CUDA arena cap, GB (default 3) — the P4 is 8GB total and torch shares it, so keep headroom. FC_ML_TORCH_MEM_FRACTION fraction of total VRAM torch may use (default 0.6). """ import os def gpu_requested() -> bool: return os.environ.get("FC_ML_DEVICE", "auto").strip().lower() in ( "auto", "cuda", "gpu", ) def onnx_gpu_mem_bytes() -> int: return int(float(os.environ.get("FC_ML_ONNX_GPU_MEM_GB", "3")) * 1024 ** 3) def torch_mem_fraction() -> float: return float(os.environ.get("FC_ML_TORCH_MEM_FRACTION", "0.6"))