Skip to main content

How to Move a Tensor to the GPU in PyTorch

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

PyTorch makes every tensor on the CPU unless you say otherwise, and the GPU only speeds up work on tensors that are on it. Moving one there is a single call. This guide moves a tensor to the GPU of an Apple M1 Pro with .to() in PyTorch 2.15.0, in code that picks an NVIDIA GPU or the CPU on other computers.

Syntax​

Syntax
tensor.to(device)
PartWhat it means
tensorThe tensor to move
deviceWhere to: a torch.device, or its name as text, such as "mps" for the GPU of an Apple silicon Mac, "cuda" for an NVIDIA GPU, or "cpu"

.to() returns the moved tensor and leaves the original where it was. PyTorch's reference explains: "If the self Tensor already has the correct torch.dtype and torch.device, then self is returned. Otherwise, the returned tensor is a copy of self with the desired torch.dtype and torch.device."

The Example​

A script makes a tensor of three prices on the CPU, moves it to the GPU, and prints where each copy lives.

Prerequisites​

  • A uv project with PyTorch and NumPy. uv add torch numpy adds both; without NumPy, PyTorch 2.15.0 prints a warning each time it starts. This guide uses PyTorch 2.15.0 and NumPy 2.5.5 on Python 3.15.2, with uv 0.13.10, on an Apple M1 Pro.

Step 1: Write the Script​

to_gpu.py
import torch

accelerator = torch.accelerator.current_accelerator(check_available=True)
device = accelerator or torch.device("cpu")

prices = torch.tensor([4.0, 9.5, 12.0])
prices_on_gpu = prices.to(device)

print(prices.device)
print(prices_on_gpu.device)
print(prices_on_gpu)

The to_gpu.py script in a macOS-style code window: the accelerator or the CPU picked as the device, a tensor of prices made on the CPU, prices.to(device) kept as prices_on_gpu, and prints of both devices and the moved tensor.

The first two lines pick the device. torch.accelerator.current_accelerator(check_available=True) returns the GPU PyTorch can use right now, mps on an Apple silicon Mac or cuda with an NVIDIA GPU, or None when there's none, and or torch.device("cpu") falls back to the CPU. How to Check Whether PyTorch Can Use a GPU (CUDA or Apple MPS) explains the check. The same script then runs on any computer.

prices.to(device) makes the copy on that device, and prices_on_gpu keeps it.

Step 2: Run It​

Terminal
uv run python to_gpu.py
Output
cpu
mps:0
tensor([ 4.0000, 9.5000, 12.0000], device='mps:0')

The terminal after uv run python to_gpu.py: cpu, then mps:0, then tensor([ 4.0000,  9.5000, 12.0000], device='mps:0').

The original prices is still on cpu, and the copy is on mps:0, the M1 Pro's GPU. When PyTorch prints a tensor that isn't on the CPU, it adds device='mps:0' after the values. The command works unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
prices.to(device) without keeping the resultprices.device still printed cpu: .to() doesn't change the tensor it's called onprices = prices.to(device)
Combining the moved tensor with one left on the CPURuntimeError: Expected all tensors to be on the same device, but found at least two devices, mps:0 and cpu!Move both, as the fix shows
.to("cuda") on a MacAssertionError: Torch not compiled with CUDA enabledLet the code pick the device, as Step 1 does

FAQ​

Can I make a tensor on the GPU in the first place?​

Yes, and it skips the copy: the functions that make tensors take a device too. torch.zeros(2, 3, device=device) printed its table with device='mps:0'.

How do I bring a tensor back to the CPU?​

With .cpu(), as How to Move a Tensor Back to the CPU in PyTorch with .cpu() shows.

Conclusion​

prices.to(device) copied a tensor from the CPU to mps:0, the GPU of an Apple M1 Pro, while the original stayed on cpu. The device came from torch.accelerator, so the same code uses an NVIDIA GPU or the CPU elsewhere.

Additional Resources​