irajkoohi's picture
Initial deployment: StocksAnalysisDashboard
4e7e313
Raw
History Blame Contribute Delete
1.12 kB
"""utils/device.py β€” Hardware device detection (CUDA / MPS / CPU)"""
import sys
def get_device() -> str:
try:
import torch
if torch.cuda.is_available():
return "cuda"
# Check MPS properly for Apple Silicon
if hasattr(torch, "mps") and hasattr(torch.mps, "is_available"):
if torch.mps.is_available():
return "mps"
if hasattr(torch.backends, "mps") and torch.backends.mps.is_available():
return "mps"
except ImportError:
pass
# Platform-based fallback: detect Apple Silicon without torch
import platform, subprocess
if platform.system() == "Darwin":
try:
result = subprocess.run(["sysctl", "-n", "hw.optional.arm64"],
capture_output=True, text=True)
if result.stdout.strip() == "1":
return "mps"
except Exception:
pass
return "cpu"
def get_device_label() -> str:
device = get_device()
return {"cuda": "🟒 CUDA GPU", "mps": "πŸ”΅ Apple MPS", "cpu": "βšͺ CPU"}.get(device, device)