22
33from __future__ import annotations
44
5+ from typing import Any
6+
57from unplug .exceptions import ConfigError
68
79
@@ -14,8 +16,8 @@ def resolve_torch_device(preferred: str | None = None) -> str:
1416 Raises:
1517 ConfigError: when ``preferred`` names a device torch cannot use.
1618 """
17- if preferred and preferred not in ( "auto" , "" ):
18- device = preferred . strip ()
19+ device = ( preferred or "" ). strip ()
20+ if device and device != "auto" :
1921 if device == "cpu" :
2022 return device
2123 return _validate_forced_device (device )
@@ -35,29 +37,40 @@ def _validate_forced_device(device: str) -> str:
3537 from unplug .optional .ml import get_torch
3638
3739 torch = get_torch ()
38- mps_backend = getattr (torch .backends , "mps" , None )
39- if _device_available (device , torch = torch , mps_backend = mps_backend ):
40- return device
40+ try :
41+ parsed = torch .device (device )
42+ except (RuntimeError , ValueError ) as exc :
43+ msg = f"Torch device '{ device } ' is not valid. Available: { _available_devices (torch )} "
44+ raise ConfigError (msg ) from exc
45+
46+ if not _backend_available (torch , parsed .type ):
47+ msg = f"Torch device '{ device } ' is not available. Available: { _available_devices (torch )} "
48+ raise ConfigError (msg )
49+ return device
50+
51+
52+ def _backend_available (torch : Any , backend : str ) -> bool :
53+ if backend == "cpu" :
54+ return True
55+ if backend == "cuda" :
56+ return bool (torch .cuda .is_available ())
57+ if backend == "mps" :
58+ mps_backend = getattr (torch .backends , "mps" , None )
59+ return mps_backend is not None and bool (mps_backend .is_available ())
60+ # Other accelerators (xpu, hpu, ...): probe torch.<backend>.is_available()
61+ # when the module exposes one, otherwise trust torch's device parsing.
62+ module = getattr (torch , backend , None )
63+ is_available = getattr (module , "is_available" , None )
64+ if callable (is_available ):
65+ return bool (is_available ())
66+ return True
4167
68+
69+ def _available_devices (torch : Any ) -> str :
4270 available = ["cpu" ]
4371 if torch .cuda .is_available ():
4472 available .append ("cuda" )
73+ mps_backend = getattr (torch .backends , "mps" , None )
4574 if mps_backend is not None and mps_backend .is_available ():
4675 available .append ("mps" )
47- msg = f"Torch device '{ device } ' is not available. Available: { ', ' .join (available )} "
48- raise ConfigError (msg )
49-
50-
51- def _device_available (
52- device : str ,
53- * ,
54- torch : object ,
55- mps_backend : object | None ,
56- ) -> bool :
57- if device == "cpu" :
58- return True
59- if device == "cuda" or device .startswith ("cuda:" ):
60- return bool (torch .cuda .is_available ()) # type: ignore[attr-defined]
61- if device == "mps" :
62- return mps_backend is not None and bool (mps_backend .is_available ()) # type: ignore[attr-defined]
63- return False
76+ return ", " .join (available )
0 commit comments