Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions speechbrain/utils/distributed.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ def get_local_rank() -> Optional[int]:
def infer_device() -> str:
"""Make a basic guess about intended running device based on
availability and distributed environment variable 'LOCAL_RANK'"""
if torch.cuda.is_available():
device = "cuda"
if torch.accelerator.is_available():
device = str(torch.accelerator.current_accelerator())
local_rank = get_local_rank()
if local_rank is not None:
device += f":{local_rank}"
Expand Down
21 changes: 21 additions & 0 deletions tests/unittests/test_distributed.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,24 @@ def test_run_on_main(tmpdir):
world_size,
join=True,
)


# Test infer_device.


def test_infer_device():
device = distributed.infer_device()
if torch.accelerator.is_available():
assert device.startswith(torch.accelerator.current_accelerator())
else:
assert device == "cpu"


def test_infer_device_local_rank(monkeypatch):
monkeypatch.setenv("LOCAL_RANK", "1")
device = distributed.infer_device()
if torch.accelerator.is_available():
assert device == f"{torch.accelerator.current_accelerator()}:1"
else:
assert device == "cpu"