Last active
November 20, 2024 12:24
-
-
Save vadim0x60/5b558e0a552627efb1c2d54b90a7cec6 to your computer and use it in GitHub Desktop.
Choose the right Pytorch device always
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment