Skip to content

Commit b6e33f5

Browse files
committed
Accept indexed and probeable torch devices
1 parent 644d617 commit b6e33f5

2 files changed

Lines changed: 51 additions & 24 deletions

File tree

‎sdk/src/unplug/ml/device.py‎

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from __future__ import annotations
44

5+
from typing import Any
6+
57
from 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)

‎sdk/tests/unit/ml/test_device.py‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@ def test_forced_cuda_unavailable_raises_config_error() -> None:
2222
resolve_torch_device("cuda")
2323

2424

25+
def test_forced_indexed_cuda_unavailable_raises() -> None:
26+
with (
27+
patch("torch.cuda.is_available", return_value=False),
28+
pytest.raises(ConfigError, match="not available"),
29+
):
30+
resolve_torch_device("cuda:0")
31+
32+
33+
def test_forced_indexed_device_accepted_when_backend_available() -> None:
34+
with patch("torch.cuda.is_available", return_value=True):
35+
assert resolve_torch_device("cuda:1") == "cuda:1"
36+
37+
2538
def test_forced_cpu_always_ok() -> None:
2639
assert resolve_torch_device("cpu") == "cpu"
2740

@@ -33,6 +46,7 @@ def test_auto_prefers_cuda_when_available() -> None:
3346
):
3447
assert resolve_torch_device(None) == "cuda"
3548
assert resolve_torch_device("auto") == "cuda"
49+
assert resolve_torch_device(" auto ") == "cuda"
3650

3751

3852
def test_auto_falls_back_to_mps() -> None:
@@ -56,5 +70,5 @@ def test_auto_falls_back_to_cpu() -> None:
5670

5771

5872
def test_forced_unknown_device_raises() -> None:
59-
with pytest.raises(ConfigError, match="not available"):
60-
resolve_torch_device("tpu")
73+
with pytest.raises(ConfigError, match="Torch device"):
74+
resolve_torch_device("not-a-device")

0 commit comments

Comments
 (0)