PyTorch 2 · Tensors

Pick the best device

Use a GPU if available, including Apple Silicon.

device = (
    "cuda" if torch.cuda.is_available()
    else "mps" if torch.backends.mps.is_available()
    else "cpu"
)
x = x.to(device)

Official PyTorch 2 documentation

More tensors commands

All tensors commands · PyTorch 2 cheatsheet

Was this page helpful?