Skip to content

Instantly share code, notes, and snippets.

View vadim0x60's full-sized avatar

Vadim Liventsev vadim0x60

  • TU Eindhoven
  • 's-Hertogenbosch, the Netherlands
View GitHub Profile
@vadim0x60
vadim0x60 / torch_device.py
Last active November 20, 2024 12:24
Choose the right Pytorch device always
def torch_device(priority=['xla', 'cuda', 'mps', 'xpu', 'cpu']):
import torch
t = torch.Tensor([0])
for device in map(torch.device, priority):
try:
t.to(device)
return device
except (RuntimeError, AssertionError):
pass