PyTorch Tensor Devices
A device is the hardware a tensor lives on, like a processor or a graphics card. Every tensor sits on one device, and PyTorch runs its maths there.
See where a tensor lives
The device attribute names the hardware a tensor sits on. A new tensor starts on the CPU.
import torch
x = torch.tensor([1.0, 2.0, 3.0])
print(x.device)Ask if a GPU exists
is_available() answers honestly for the machine the code runs on. This machine has no CUDA device.
print(torch.cuda.is_available())Move a tensor with .to()
The move picks cuda when one exists and cpu when none does. The tensor lands on the CPU here.
target = torch.device("cuda" if torch.cuda.is_available() else "cpu")
y = x.to(target)
print(y.device)Example
import torch
x = torch.tensor([1.0, 2.0, 3.0])
print("CUDA available:", torch.cuda.is_available())
print("Tensor starts on:", x.device)
best = torch.device("cuda" if torch.cuda.is_available() else "cpu")
moved = x.to(best)
print("Best device:", best)
print("Moved tensor:", moved)
print("Moved device:", moved.device)Key points
- Device: the hardware that stores a tensor and runs its maths
- cpu: the name of the main processor in the computer
- cuda: the name PyTorch uses for an NVIDIA graphics card
- Use torch.cuda.is_available() to ask if a CUDA device exists
- Use tensor.to(device) to move a tensor onto a device
- A tensor always lives on exactly one device at a time
In this example, x starts as a small tensor of three values. torch.cuda.is_available() asks if a CUDA device exists, and it prints False on this machine. The tensor starts on the CPU, which x.device shows. best picks cuda when one exists and cpu when none does. x.to(best) moves the tensor onto that device. The moved tensor holds the same values, and its device prints as cpu.
Note
This machine has no graphics card for PyTorch, so every tensor here stays on the CPU
Try it Yourself
Edit the code, then press Run.
Running is simulated ยท Python Playground
Exercise
What does torch.cuda.is_available() return on a machine with no usable GPU?