Source code for unilab.utils.device

from __future__ import annotations

import torch


def _xpu_available() -> bool:
    xpu = getattr(torch, "xpu", None)
    is_available = getattr(xpu, "is_available", None)
    return bool(callable(is_available) and is_available())


[docs] def get_default_device() -> str: """Detect the best available device.""" if torch.cuda.is_available(): return "cuda" if _xpu_available(): return "xpu" if torch.backends.mps.is_available(): return "mps" return "cpu"